blob: 62b2fcd78497baf3c0b1bb012034c5b01bbb69e1 [file] [log] [blame]
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "mm-modem-helpers-mbim.h"
#include "mm-broadband-modem-mbim.h"
#include "mm-bearer-mbim.h"
#include "mm-sim-mbim.h"
#include "mm-sms-mbim.h"
#include "ModemManager.h"
#include "mm-log.h"
#include "mm-errors-types.h"
#include "mm-error-helpers.h"
#include "mm-modem-helpers.h"
#include "mm-bearer-list.h"
#include "mm-iface-modem.h"
#include "mm-iface-modem-3gpp.h"
#include "mm-iface-modem-3gpp-ussd.h"
#include "mm-iface-modem-location.h"
#include "mm-iface-modem-messaging.h"
#include "mm-iface-modem-signal.h"
#include "mm-sms-part-3gpp.h"
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
# include <libqmi-glib.h>
# include "mm-shared-qmi.h"
#endif
static void iface_modem_init (MMIfaceModem *iface);
static void iface_modem_3gpp_init (MMIfaceModem3gpp *iface);
static void iface_modem_3gpp_ussd_init (MMIfaceModem3gppUssd *iface);
static void iface_modem_location_init (MMIfaceModemLocation *iface);
static void iface_modem_messaging_init (MMIfaceModemMessaging *iface);
static void iface_modem_signal_init (MMIfaceModemSignal *iface);
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
static void shared_qmi_init (MMSharedQmi *iface);
#endif
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
static MMIfaceModemLocation *iface_modem_location_parent;
#endif
static MMIfaceModemSignal *iface_modem_signal_parent;
G_DEFINE_TYPE_EXTENDED (MMBroadbandModemMbim, mm_broadband_modem_mbim, MM_TYPE_BROADBAND_MODEM, 0,
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM, iface_modem_init)
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM_3GPP, iface_modem_3gpp_init)
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM_3GPP_USSD, iface_modem_3gpp_ussd_init)
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM_LOCATION, iface_modem_location_init)
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM_MESSAGING, iface_modem_messaging_init)
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM_SIGNAL, iface_modem_signal_init)
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
G_IMPLEMENT_INTERFACE (MM_TYPE_SHARED_QMI, shared_qmi_init)
#endif
)
typedef enum {
PROCESS_NOTIFICATION_FLAG_NONE = 0,
PROCESS_NOTIFICATION_FLAG_SIGNAL_QUALITY = 1 << 0,
PROCESS_NOTIFICATION_FLAG_REGISTRATION_UPDATES = 1 << 1,
PROCESS_NOTIFICATION_FLAG_SMS_READ = 1 << 2,
PROCESS_NOTIFICATION_FLAG_CONNECT = 1 << 3,
PROCESS_NOTIFICATION_FLAG_SUBSCRIBER_INFO = 1 << 4,
PROCESS_NOTIFICATION_FLAG_PACKET_SERVICE = 1 << 5,
PROCESS_NOTIFICATION_FLAG_PCO = 1 << 6,
PROCESS_NOTIFICATION_FLAG_USSD = 1 << 7,
PROCESS_NOTIFICATION_FLAG_LTE_ATTACH_STATUS = 1 << 8,
} ProcessNotificationFlag;
struct _MMBroadbandModemMbimPrivate {
/* Queried and cached capabilities */
MbimCellularClass caps_cellular_class;
MbimDataClass caps_data_class;
MbimSmsCaps caps_sms;
guint caps_max_sessions;
gchar *caps_device_id;
gchar *caps_firmware_info;
gchar *caps_hardware_info;
/* Supported features */
gboolean is_pco_supported;
gboolean is_lte_attach_status_supported;
gboolean is_ussd_supported;
gboolean is_atds_location_supported;
gboolean is_atds_signal_supported;
/* Process unsolicited notifications */
guint notification_id;
ProcessNotificationFlag setup_flags;
ProcessNotificationFlag enable_flags;
GList *pco_list;
/* 3GPP registration helpers */
gchar *current_operator_id;
gchar *current_operator_name;
/* USSD helpers */
GTask *pending_ussd_action;
/* Access technology updates */
MbimDataClass available_data_classes;
MbimDataClass highest_available_data_class;
MbimSubscriberReadyState last_ready_state;
/* For notifying when the mbim-proxy connection is dead */
gulong mbim_device_removed_id;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
/* Flag when QMI-based capability/mode switching is in use */
gboolean qmi_capability_and_mode_switching;
#endif
};
/*****************************************************************************/
static gboolean
peek_device (gpointer self,
MbimDevice **o_device,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMPortMbim *port;
port = mm_base_modem_peek_port_mbim (MM_BASE_MODEM (self));
if (!port) {
g_task_report_new_error (self,
callback,
user_data,
peek_device,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't peek MBIM port");
return FALSE;
}
*o_device = mm_port_mbim_peek_device (port);
return TRUE;
}
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
static QmiClient *
shared_qmi_peek_client (MMSharedQmi *self,
QmiService service,
MMPortQmiFlag flag,
GError **error)
{
MMPortMbim *port;
QmiClient *client;
g_assert (flag == MM_PORT_QMI_FLAG_DEFAULT);
port = mm_base_modem_peek_port_mbim (MM_BASE_MODEM (self));
if (!port) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't peek MBIM port");
return NULL;
}
if (!mm_port_mbim_supports_qmi (port)) {
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_UNSUPPORTED,
"Unsupported");
return NULL;
}
client = mm_port_mbim_peek_qmi_client (port, service);
if (!client)
g_set_error (error,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't peek client for service '%s'",
qmi_service_get_string (service));
return client;
}
#endif
/*****************************************************************************/
/* Current capabilities (Modem interface) */
typedef struct {
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
MMModemCapability current_qmi;
#endif
MbimDevice *device;
MMModemCapability current_mbim;
} LoadCurrentCapabilitiesContext;
static void
load_current_capabilities_context_free (LoadCurrentCapabilitiesContext *ctx)
{
g_object_unref (ctx->device);
g_free (ctx);
}
static MMModemCapability
modem_load_current_capabilities_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
GError *inner_error = NULL;
gssize value;
value = g_task_propagate_int (G_TASK (res), &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return MM_MODEM_CAPABILITY_NONE;
}
return (MMModemCapability)value;
}
static void
complete_current_capabilities (GTask *task)
{
MMBroadbandModemMbim *self;
LoadCurrentCapabilitiesContext *ctx;
MMModemCapability result = 0;
self = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
/* Warn if the MBIM loaded capabilities isn't a subset of the QMI loaded ones */
if (ctx->current_qmi && ctx->current_mbim) {
gchar *mbim_caps_str;
gchar *qmi_caps_str;
mbim_caps_str = mm_common_build_capabilities_string ((const MMModemCapability *)&(ctx->current_mbim), 1);
qmi_caps_str = mm_common_build_capabilities_string ((const MMModemCapability *)&(ctx->current_qmi), 1);
if ((ctx->current_mbim & ctx->current_qmi) != ctx->current_mbim)
mm_warn ("MBIM reported current capabilities (%s) not found in QMI-over-MBIM reported ones (%s)",
mbim_caps_str, qmi_caps_str);
else
mm_dbg ("MBIM reported current capabilities (%s) is a subset of the QMI-over-MBIM reported ones (%s)",
mbim_caps_str, qmi_caps_str);
g_free (mbim_caps_str);
g_free (qmi_caps_str);
result = ctx->current_qmi;
self->priv->qmi_capability_and_mode_switching = TRUE;
} else if (ctx->current_qmi) {
result = ctx->current_qmi;
self->priv->qmi_capability_and_mode_switching = TRUE;
} else
result = ctx->current_mbim;
/* If current capabilities loading is done via QMI, we can safely assume that all the other
* capability and mode related operations are going to be done via QMI as well, so that we
* don't mix both logics */
if (self->priv->qmi_capability_and_mode_switching)
mm_info ("QMI-based capability and mode switching support enabled");
#else
result = ctx->current_mbim;
#endif
g_task_return_int (task, (gint) result);
g_object_unref (task);
}
static void
device_caps_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MMBroadbandModemMbim *self;
MbimMessage *response;
GError *error = NULL;
LoadCurrentCapabilitiesContext *ctx;
self = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
response = mbim_device_command_finish (device, res, &error);
if (!response ||
!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) ||
!mbim_message_device_caps_response_parse (
response,
NULL, /* device_type */
&self->priv->caps_cellular_class,
NULL, /* voice_class */
NULL, /* sim_class */
&self->priv->caps_data_class,
&self->priv->caps_sms,
NULL, /* ctrl_caps */
&self->priv->caps_max_sessions,
NULL, /* custom_data_class */
&self->priv->caps_device_id,
&self->priv->caps_firmware_info,
&self->priv->caps_hardware_info,
&error)) {
g_task_return_error (task, error);
g_object_unref (task);
goto out;
}
ctx->current_mbim = mm_modem_capability_from_mbim_device_caps (self->priv->caps_cellular_class,
self->priv->caps_data_class);
complete_current_capabilities (task);
out:
if (response)
mbim_message_unref (response);
}
static void
load_current_capabilities_mbim (GTask *task)
{
MbimMessage *message;
LoadCurrentCapabilitiesContext *ctx;
ctx = g_task_get_task_data (task);
mm_dbg ("loading current capabilities...");
message = mbim_message_device_caps_query_new (NULL);
mbim_device_command (ctx->device,
message,
10,
NULL,
(GAsyncReadyCallback)device_caps_query_ready,
task);
mbim_message_unref (message);
}
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
static void
qmi_load_current_capabilities_ready (MMIfaceModem *self,
GAsyncResult *res,
GTask *task)
{
LoadCurrentCapabilitiesContext *ctx;
GError *error = NULL;
ctx = g_task_get_task_data (task);
ctx->current_qmi = mm_shared_qmi_load_current_capabilities_finish (self, res, &error);
if (!ctx->current_qmi) {
mm_dbg ("Couldn't load currrent capabilities using QMI over MBIM: %s", error->message);
g_clear_error (&error);
}
load_current_capabilities_mbim (task);
}
#endif
static void
modem_load_current_capabilities (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
GTask *task;
LoadCurrentCapabilitiesContext *ctx;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
ctx = g_new0 (LoadCurrentCapabilitiesContext, 1);
ctx->device = g_object_ref (device);
g_task_set_task_data (task, ctx, (GDestroyNotify) load_current_capabilities_context_free);
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
mm_shared_qmi_load_current_capabilities (self,
(GAsyncReadyCallback)qmi_load_current_capabilities_ready,
task);
#else
load_current_capabilities_mbim (task);
#endif
}
/*****************************************************************************/
/* Supported Capabilities loading (Modem interface) */
static GArray *
modem_load_supported_capabilities_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching)
return mm_shared_qmi_load_supported_capabilities_finish (self, res, error);
#endif
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
load_supported_capabilities_mbim (GTask *task)
{
MMBroadbandModemMbim *self;
MMModemCapability current;
GArray *supported = NULL;
self = g_task_get_source_object (task);
/* Current capabilities should have been cached already, just assume them */
current = mm_modem_capability_from_mbim_device_caps (self->priv->caps_cellular_class, self->priv->caps_data_class);
if (current != 0) {
supported = g_array_sized_new (FALSE, FALSE, sizeof (MMModemCapability), 1);
g_array_append_val (supported, current);
}
if (!supported)
g_task_return_new_error (task, MM_CORE_ERROR, MM_CORE_ERROR_FAILED,
"Couldn't load supported capabilities: no previously catched current capabilities");
else
g_task_return_pointer (task, supported, (GDestroyNotify) g_array_unref);
g_object_unref (task);
}
static void
modem_load_supported_capabilities (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching) {
mm_shared_qmi_load_supported_capabilities (self, callback, user_data);
return;
}
#endif
task = g_task_new (self, NULL, callback, user_data);
load_supported_capabilities_mbim (task);
}
/*****************************************************************************/
/* Capabilities switching (Modem interface) */
static gboolean
modem_set_current_capabilities_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching)
return mm_shared_qmi_set_current_capabilities_finish (self, res, error);
#endif
g_assert (error);
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
modem_set_current_capabilities (MMIfaceModem *self,
MMModemCapability capabilities,
GAsyncReadyCallback callback,
gpointer user_data)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching) {
mm_shared_qmi_set_current_capabilities (self, capabilities, callback, user_data);
return;
}
#endif
g_task_report_new_error (self, callback, user_data,
modem_set_current_capabilities,
MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Capability switching is not supported");
}
/*****************************************************************************/
/* Manufacturer loading (Modem interface) */
static gchar *
modem_load_manufacturer_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_load_manufacturer (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
gchar *manufacturer = NULL;
MMPortMbim *port;
port = mm_base_modem_peek_port_mbim (MM_BASE_MODEM (self));
if (port) {
manufacturer = g_strdup (mm_kernel_device_get_physdev_manufacturer (
mm_port_peek_kernel_device (MM_PORT (port))));
}
if (!manufacturer)
manufacturer = g_strdup (mm_base_modem_get_plugin (MM_BASE_MODEM (self)));
task = g_task_new (self, NULL, callback, user_data);
g_task_return_pointer (task, manufacturer, g_free);
g_object_unref (task);
}
/*****************************************************************************/
/* Model loading (Modem interface) */
static gchar *
modem_load_model_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_load_model (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
gchar *model = NULL;
GTask *task;
MMPortMbim *port;
port = mm_base_modem_peek_port_mbim (MM_BASE_MODEM (self));
if (port) {
model = g_strdup (mm_kernel_device_get_physdev_product (
mm_port_peek_kernel_device (MM_PORT (port))));
}
if (!model)
model = g_strdup_printf ("MBIM [%04X:%04X]",
(mm_base_modem_get_vendor_id (MM_BASE_MODEM (self)) & 0xFFFF),
(mm_base_modem_get_product_id (MM_BASE_MODEM (self)) & 0xFFFF));
task = g_task_new (self, NULL, callback, user_data);
g_task_return_pointer (task, model, g_free);
g_object_unref (task);
}
/*****************************************************************************/
/* Revision loading (Modem interface) */
static gchar *
modem_load_revision_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_load_revision (MMIfaceModem *_self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMBroadbandModemMbim *self = MM_BROADBAND_MODEM_MBIM (_self);
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
if (self->priv->caps_firmware_info)
g_task_return_pointer (task,
g_strdup (self->priv->caps_firmware_info),
g_free);
else
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Firmware revision information not given in device capabilities");
g_object_unref (task);
}
/*****************************************************************************/
/* Hardware Revision loading (Modem interface) */
static gchar *
modem_load_hardware_revision_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_load_hardware_revision (MMIfaceModem *_self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMBroadbandModemMbim *self = MM_BROADBAND_MODEM_MBIM (_self);
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
if (self->priv->caps_hardware_info)
g_task_return_pointer (task,
g_strdup (self->priv->caps_hardware_info),
g_free);
else
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Hardware revision information not given in device capabilities");
g_object_unref (task);
}
/*****************************************************************************/
/* Equipment Identifier loading (Modem interface) */
static gchar *
modem_load_equipment_identifier_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_load_equipment_identifier (MMIfaceModem *_self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMBroadbandModemMbim *self = MM_BROADBAND_MODEM_MBIM (_self);
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
if (self->priv->caps_device_id)
g_task_return_pointer (task,
g_strdup (self->priv->caps_device_id),
g_free);
else
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Device ID not given in device capabilities");
g_object_unref (task);
}
/*****************************************************************************/
/* Device identifier loading (Modem interface) */
static gchar *
modem_load_device_identifier_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_load_device_identifier (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
gchar *device_identifier;
GTask *task;
/* Just use dummy ATI/ATI1 replies, all the other internal info should be
* enough for uniqueness */
device_identifier = mm_broadband_modem_create_device_identifier (MM_BROADBAND_MODEM (self), "", "");
task = g_task_new (self, NULL, callback, user_data);
g_task_return_pointer (task, device_identifier, g_free);
g_object_unref (task);
}
/*****************************************************************************/
/* Supported modes loading (Modem interface) */
static GArray *
modem_load_supported_modes_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching)
return mm_shared_qmi_load_supported_modes_finish (self, res, error);
#endif
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
load_supported_modes_mbim (GTask *task)
{
MMBroadbandModemMbim *self;
MMModemModeCombination mode;
MMModemMode all;
GArray *supported;
self = g_task_get_source_object (task);
if (self->priv->caps_data_class == 0) {
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Data class not given in device capabilities");
g_object_unref (task);
return;
}
all = 0;
/* 3GPP... */
if (self->priv->caps_data_class & (MBIM_DATA_CLASS_GPRS |
MBIM_DATA_CLASS_EDGE))
all |= MM_MODEM_MODE_2G;
if (self->priv->caps_data_class & (MBIM_DATA_CLASS_UMTS |
MBIM_DATA_CLASS_HSDPA |
MBIM_DATA_CLASS_HSUPA))
all |= MM_MODEM_MODE_3G;
if (self->priv->caps_data_class & MBIM_DATA_CLASS_LTE)
all |= MM_MODEM_MODE_4G;
/* 3GPP2... */
if (self->priv->caps_data_class & MBIM_DATA_CLASS_1XRTT)
all |= MM_MODEM_MODE_2G;
if (self->priv->caps_data_class & (MBIM_DATA_CLASS_1XEVDO |
MBIM_DATA_CLASS_1XEVDO_REVA |
MBIM_DATA_CLASS_1XEVDV |
MBIM_DATA_CLASS_3XRTT |
MBIM_DATA_CLASS_1XEVDO_REVB))
all |= MM_MODEM_MODE_3G;
if (self->priv->caps_data_class & MBIM_DATA_CLASS_UMB)
all |= MM_MODEM_MODE_4G;
/* Build a mask with all supported modes */
supported = g_array_sized_new (FALSE, FALSE, sizeof (MMModemModeCombination), 1);
mode.allowed = all;
mode.preferred = MM_MODEM_MODE_NONE;
g_array_append_val (supported, mode);
g_task_return_pointer (task, supported, (GDestroyNotify) g_array_unref);
g_object_unref (task);
}
static void
modem_load_supported_modes (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching) {
mm_shared_qmi_load_supported_modes (self, callback, user_data);
return;
}
#endif
task = g_task_new (self, NULL, callback, user_data);
load_supported_modes_mbim (task);
}
/*****************************************************************************/
/* Current modes loading (Modem interface) */
static gboolean
modem_load_current_modes_finish (MMIfaceModem *self,
GAsyncResult *res,
MMModemMode *allowed,
MMModemMode *preferred,
GError **error)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching)
return mm_shared_qmi_load_current_modes_finish (self, res, allowed, preferred, error);
#endif
g_assert (error);
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
modem_load_current_modes (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching) {
mm_shared_qmi_load_current_modes (self, callback, user_data);
return;
}
#endif
g_task_report_new_error (self, callback, user_data,
modem_set_current_capabilities,
MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Current mode loading is not supported");
}
/*****************************************************************************/
/* Current modes switching (Modem interface) */
static gboolean
modem_set_current_modes_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching)
return mm_shared_qmi_set_current_modes_finish (self, res, error);
#endif
g_assert (error);
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
modem_set_current_modes (MMIfaceModem *self,
MMModemMode allowed,
MMModemMode preferred,
GAsyncReadyCallback callback,
gpointer user_data)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
if (MM_BROADBAND_MODEM_MBIM (self)->priv->qmi_capability_and_mode_switching) {
mm_shared_qmi_set_current_modes (self, allowed, preferred, callback, user_data);
return;
}
#endif
g_task_report_new_error (self, callback, user_data,
modem_set_current_capabilities,
MM_CORE_ERROR, MM_CORE_ERROR_UNSUPPORTED,
"Capability switching is not supported");
}
/*****************************************************************************/
/* Load supported IP families (Modem interface) */
static MMBearerIpFamily
modem_load_supported_ip_families_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
GError *inner_error = NULL;
gssize value;
value = g_task_propagate_int (G_TASK (res), &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return MM_BEARER_IP_FAMILY_NONE;
}
return (MMBearerIpFamily)value;
}
static void
modem_load_supported_ip_families (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
/* Assume IPv4 + IPv6 + IPv4v6 supported */
g_task_return_int (task,
MM_BEARER_IP_FAMILY_IPV4 |
MM_BEARER_IP_FAMILY_IPV6 |
MM_BEARER_IP_FAMILY_IPV4V6);
g_object_unref (task);
}
/*****************************************************************************/
/* Unlock required loading (Modem interface) */
typedef struct {
guint n_ready_status_checks;
MbimDevice *device;
} LoadUnlockRequiredContext;
static void
load_unlock_required_context_free (LoadUnlockRequiredContext *ctx)
{
g_object_unref (ctx->device);
g_slice_free (LoadUnlockRequiredContext, ctx);
}
static MMModemLock
modem_load_unlock_required_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
GError *inner_error = NULL;
gssize value;
value = g_task_propagate_int (G_TASK (res), &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return MM_MODEM_LOCK_UNKNOWN;
}
return (MMModemLock)value;
}
static void
pin_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
MbimPinType pin_type;
MbimPinState pin_state;
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_pin_response_parse (
response,
&pin_type,
&pin_state,
NULL,
&error)) {
MMModemLock unlock_required;
if (pin_state == MBIM_PIN_STATE_UNLOCKED)
unlock_required = MM_MODEM_LOCK_NONE;
else
unlock_required = mm_modem_lock_from_mbim_pin_type (pin_type);
g_task_return_int (task, unlock_required);
}
/* VZ20M reports an error when SIM-PIN is required... */
else if (g_error_matches (error, MBIM_STATUS_ERROR, MBIM_STATUS_ERROR_PIN_REQUIRED)) {
g_error_free (error);
g_task_return_int (task, MBIM_PIN_TYPE_PIN1);
}
else
g_task_return_error (task, error);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static gboolean wait_for_sim_ready (GTask *task);
static void
unlock_required_subscriber_ready_state_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
LoadUnlockRequiredContext *ctx;
MMBroadbandModemMbim *self;
MbimMessage *response;
GError *error = NULL;
MbimSubscriberReadyState ready_state = MBIM_SUBSCRIBER_READY_STATE_NOT_INITIALIZED;
ctx = g_task_get_task_data (task);
self = g_task_get_source_object (task);
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_subscriber_ready_status_response_parse (
response,
&ready_state,
NULL, /* subscriber_id */
NULL, /* sim_iccid */
NULL, /* ready_info */
NULL, /* telephone_numbers_count */
NULL, /* telephone_numbers */
&error)) {
switch (ready_state) {
case MBIM_SUBSCRIBER_READY_STATE_NOT_INITIALIZED:
case MBIM_SUBSCRIBER_READY_STATE_INITIALIZED:
case MBIM_SUBSCRIBER_READY_STATE_DEVICE_LOCKED:
/* Don't set error */
break;
case MBIM_SUBSCRIBER_READY_STATE_SIM_NOT_INSERTED:
/* This is an error, but we still want to retry.
* The MC7710 may use this while the SIM is not ready yet. */
break;
case MBIM_SUBSCRIBER_READY_STATE_BAD_SIM:
error = mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_SIM_WRONG);
break;
case MBIM_SUBSCRIBER_READY_STATE_FAILURE:
case MBIM_SUBSCRIBER_READY_STATE_NOT_ACTIVATED:
default:
error = mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_SIM_FAILURE);
break;
}
}
self->priv->last_ready_state = ready_state;
/* Fatal errors are reported right away */
if (error) {
g_task_return_error (task, error);
g_object_unref (task);
}
/* Need to retry? */
else if (ready_state == MBIM_SUBSCRIBER_READY_STATE_NOT_INITIALIZED ||
ready_state == MBIM_SUBSCRIBER_READY_STATE_SIM_NOT_INSERTED) {
if (--ctx->n_ready_status_checks == 0) {
/* All retries consumed, issue error */
if (ready_state == MBIM_SUBSCRIBER_READY_STATE_SIM_NOT_INSERTED)
g_task_return_error (
task,
mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_SIM_NOT_INSERTED));
else
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Error waiting for SIM to get initialized");
g_object_unref (task);
} else {
/* Retry */
g_timeout_add_seconds (1, (GSourceFunc)wait_for_sim_ready, task);
}
}
/* Initialized but locked? */
else if (ready_state == MBIM_SUBSCRIBER_READY_STATE_DEVICE_LOCKED) {
MbimMessage *message;
/* Query which lock is to unlock */
message = mbim_message_pin_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)pin_query_ready,
task);
mbim_message_unref (message);
}
/* Initialized but locked? */
else if (ready_state == MBIM_SUBSCRIBER_READY_STATE_INITIALIZED) {
g_task_return_boolean (task, TRUE);
g_object_unref (task);
} else
g_assert_not_reached ();
if (response)
mbim_message_unref (response);
}
static gboolean
wait_for_sim_ready (GTask *task)
{
LoadUnlockRequiredContext *ctx;
MbimMessage *message;
ctx = g_task_get_task_data (task);
message = mbim_message_subscriber_ready_status_query_new (NULL);
mbim_device_command (ctx->device,
message,
10,
NULL,
(GAsyncReadyCallback)unlock_required_subscriber_ready_state_ready,
task);
mbim_message_unref (message);
return G_SOURCE_REMOVE;
}
static void
modem_load_unlock_required (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
LoadUnlockRequiredContext *ctx;
MbimDevice *device;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
ctx = g_slice_new (LoadUnlockRequiredContext);
ctx->device = g_object_ref (device);
ctx->n_ready_status_checks = 10;
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, (GDestroyNotify)load_unlock_required_context_free);
wait_for_sim_ready (task);
}
/*****************************************************************************/
/* Unlock retries loading (Modem interface) */
static MMUnlockRetries *
modem_load_unlock_retries_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
pin_query_unlock_retries_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
MbimPinType pin_type;
guint32 remaining_attempts;
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_pin_response_parse (
response,
&pin_type,
NULL,
&remaining_attempts,
&error)) {
MMIfaceModem *self;
MMModemLock lock;
MMUnlockRetries *retries;
self = g_task_get_source_object (task);
lock = mm_modem_lock_from_mbim_pin_type (pin_type);
retries = mm_unlock_retries_new ();
/* If PIN1 is disabled and we have tried to enable it with a wrong PIN,
* the modem would have indicated the number of remaining attempts for
* PIN1 (unless PUK1 is engaged) in the response to the failed
* MBIM_CID_PIN set operation. Thus, MMSimMbim would have updated
* MMIfaceModem's MMUnlockRetries with information about PIN1.
*
* However, a MBIM_CID_PIN query may be issued (e.g. MMBaseSim calls
* mm_iface_modem_update_lock_info()) after the MBIM_CID_PIN set
* operation to query the number of remaining attempts for a PIN type.
* Unfortunately, we can't specify a particular PIN type in a
* MBIM_CID_PIN query. The modem may not reply with information about
* PIN1 if PIN1 is disabled. When that happens, we would like to
* preserve our knowledge about the number of remaining attempts for
* PIN1. Here we thus carry over any existing information on PIN1 from
* MMIfaceModem's MMUnlockRetries if the MBIM_CID_PIN query reports
* something other than PIN1. */
if (lock != MM_MODEM_LOCK_SIM_PIN) {
MMUnlockRetries *previous_retries;
guint previous_sim_pin_retries;
previous_retries = mm_iface_modem_get_unlock_retries (self);
previous_sim_pin_retries = mm_unlock_retries_get (previous_retries,
MM_MODEM_LOCK_SIM_PIN);
if (previous_sim_pin_retries != MM_UNLOCK_RETRIES_UNKNOWN) {
mm_unlock_retries_set (retries,
MM_MODEM_LOCK_SIM_PIN,
previous_sim_pin_retries);
}
g_object_unref (previous_retries);
}
/* According to the MBIM specification, RemainingAttempts is set to
* 0xffffffff if the device does not support this information. */
if (remaining_attempts != G_MAXUINT32)
mm_unlock_retries_set (retries, lock, remaining_attempts);
g_task_return_pointer (task, retries, g_object_unref);
} else
g_task_return_error (task, error);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static void
modem_load_unlock_retries (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_pin_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)pin_query_unlock_retries_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Own numbers loading */
static GStrv
modem_load_own_numbers_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
own_numbers_subscriber_ready_state_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
gchar **telephone_numbers;
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_subscriber_ready_status_response_parse (
response,
NULL, /* ready_state */
NULL, /* subscriber_id */
NULL, /* sim_iccid */
NULL, /* ready_info */
NULL, /* telephone_numbers_count */
&telephone_numbers,
&error)) {
g_task_return_pointer (task, telephone_numbers, (GDestroyNotify)g_strfreev);
} else
g_task_return_error (task, error);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static void
modem_load_own_numbers (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_subscriber_ready_status_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)own_numbers_subscriber_ready_state_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Initial power state loading */
static MMModemPowerState
modem_load_power_state_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
GError *inner_error = NULL;
gssize value;
value = g_task_propagate_int (G_TASK (res), &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return MM_MODEM_POWER_STATE_UNKNOWN;
}
return (MMModemPowerState)value;
}
static void
radio_state_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
MbimRadioSwitchState hardware_radio_state;
MbimRadioSwitchState software_radio_state;
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_radio_state_response_parse (
response,
&hardware_radio_state,
&software_radio_state,
&error)) {
MMModemPowerState state;
if (hardware_radio_state == MBIM_RADIO_SWITCH_STATE_OFF ||
software_radio_state == MBIM_RADIO_SWITCH_STATE_OFF)
state = MM_MODEM_POWER_STATE_LOW;
else
state = MM_MODEM_POWER_STATE_ON;
g_task_return_int (task, state);
} else
g_task_return_error (task, error);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static void
modem_load_power_state (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_radio_state_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)radio_state_query_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Power up (Modem interface) */
typedef enum {
POWER_UP_CONTEXT_STEP_FIRST,
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
POWER_UP_CONTEXT_STEP_FCC_AUTH,
POWER_UP_CONTEXT_STEP_RETRY,
#endif
POWER_UP_CONTEXT_STEP_LAST,
} PowerUpContextStep;
typedef struct {
MbimDevice *device;
PowerUpContextStep step;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
QmiClient *qmi_client_dms;
GError *saved_error;
#endif
} PowerUpContext;
static void
power_up_context_free (PowerUpContext *ctx)
{
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
g_clear_object (&ctx->qmi_client_dms);
if (ctx->saved_error)
g_error_free (ctx->saved_error);
#endif
g_object_unref (ctx->device);
g_slice_free (PowerUpContext, ctx);
}
static gboolean
power_up_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static void power_up_context_step (GTask *task);
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
static void
set_fcc_authentication_ready (QmiClientDms *qmi_client_dms,
GAsyncResult *res,
GTask *task)
{
PowerUpContext *ctx;
QmiMessageDmsSetFccAuthenticationOutput *output;
GError *error = NULL;
ctx = g_task_get_task_data (task);
output = qmi_client_dms_set_fcc_authentication_finish (qmi_client_dms, res, &error);
if (!output || !qmi_message_dms_set_fcc_authentication_output_get_result (output, &error)) {
mm_dbg ("error: couldn't set FCC auth: %s", error->message);
g_error_free (error);
g_assert (ctx->saved_error);
g_task_return_error (task, ctx->saved_error);
ctx->saved_error = NULL;
g_object_unref (task);
goto out;
}
ctx->step++;
power_up_context_step (task);
out:
if (output)
qmi_message_dms_set_fcc_authentication_output_unref (output);
}
static void
set_radio_state_fcc_auth (GTask *task)
{
PowerUpContext *ctx;
ctx = g_task_get_task_data (task);
g_assert (ctx->qmi_client_dms);
qmi_client_dms_set_fcc_authentication (QMI_CLIENT_DMS (ctx->qmi_client_dms),
NULL,
10,
NULL, /* cancellable */
(GAsyncReadyCallback)set_fcc_authentication_ready,
task);
}
#endif
static void
radio_state_set_up_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
PowerUpContext *ctx;
MbimMessage *response;
GError *error = NULL;
MbimRadioSwitchState hardware_radio_state;
MbimRadioSwitchState software_radio_state;
ctx = g_task_get_task_data (task);
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_radio_state_response_parse (
response,
&hardware_radio_state,
&software_radio_state,
&error)) {
if (hardware_radio_state == MBIM_RADIO_SWITCH_STATE_OFF)
error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Cannot power-up: hardware radio switch is OFF");
else if (software_radio_state == MBIM_RADIO_SWITCH_STATE_OFF)
error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Cannot power-up: sotware radio switch is OFF");
}
if (response)
mbim_message_unref (response);
/* Nice! we're done, quick exit */
if (!error) {
ctx->step = POWER_UP_CONTEXT_STEP_LAST;
power_up_context_step (task);
return;
}
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
/* Only the first attempt isn't fatal, if we have a QMI DMS client */
if ((ctx->step == POWER_UP_CONTEXT_STEP_FIRST) && ctx->qmi_client_dms) {
/* Warn and keep, will retry */
mm_warn ("%s", error->message);
g_assert (!ctx->saved_error);
ctx->saved_error = error;
ctx->step++;
power_up_context_step (task);
return;
}
#endif
/* Fatal */
g_task_return_error (task, error);
g_object_unref (task);
}
static void
set_radio_state_up (GTask *task)
{
PowerUpContext *ctx;
MbimMessage *message;
ctx = g_task_get_task_data (task);
message = mbim_message_radio_state_set_new (MBIM_RADIO_SWITCH_STATE_ON, NULL);
mbim_device_command (ctx->device,
message,
20,
NULL,
(GAsyncReadyCallback)radio_state_set_up_ready,
task);
mbim_message_unref (message);
}
static void
power_up_context_step (GTask *task)
{
PowerUpContext *ctx;
ctx = g_task_get_task_data (task);
switch (ctx->step) {
case POWER_UP_CONTEXT_STEP_FIRST:
set_radio_state_up (task);
return;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
case POWER_UP_CONTEXT_STEP_FCC_AUTH:
set_radio_state_fcc_auth (task);
return;
case POWER_UP_CONTEXT_STEP_RETRY:
set_radio_state_up (task);
return;
#endif
case POWER_UP_CONTEXT_STEP_LAST:
/* Good! */
g_task_return_boolean (task, TRUE);
g_object_unref (task);
return;
default:
g_assert_not_reached ();
}
}
static void
modem_power_up (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
PowerUpContext *ctx;
MbimDevice *device;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
ctx = g_slice_new0 (PowerUpContext);
ctx->device = g_object_ref (device);
ctx->step = POWER_UP_CONTEXT_STEP_FIRST;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
ctx->qmi_client_dms = mm_shared_qmi_peek_client (MM_SHARED_QMI (self),
QMI_SERVICE_DMS,
MM_PORT_QMI_FLAG_DEFAULT,
NULL);
if (ctx->qmi_client_dms)
g_object_ref (ctx->qmi_client_dms);
#endif
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, (GDestroyNotify)power_up_context_free);
power_up_context_step (task);
}
/*****************************************************************************/
/* Power down (Modem interface) */
static gboolean
power_down_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
radio_state_set_down_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
response = mbim_device_command_finish (device, res, &error);
if (response) {
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error);
mbim_message_unref (response);
}
if (error)
g_task_return_error (task, error);
else
g_task_return_boolean (task, TRUE);
g_object_unref (task);
}
static void
modem_power_down (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_radio_state_set_new (MBIM_RADIO_SWITCH_STATE_OFF, NULL);
mbim_device_command (device,
message,
20,
NULL,
(GAsyncReadyCallback)radio_state_set_down_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Signal quality loading (Modem interface) */
static guint
modem_load_signal_quality_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
gssize value;
value = g_task_propagate_int (G_TASK (res), error);
return value < 0 ? 0 : value;
}
static void
signal_state_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
guint32 rssi;
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_signal_state_response_parse (
response,
&rssi,
NULL, /* error_rate */
NULL, /* signal_strength_interval */
NULL, /* rssi_threshold */
NULL, /* error_rate_threshold */
&error)) {
guint32 quality;
/* Normalize the quality. 99 means unknown, we default it to 0 */
quality = CLAMP (rssi == 99 ? 0 : rssi, 0, 31) * 100 / 31;
g_task_return_int (task, quality);
} else
g_task_return_error (task, error);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static void
modem_load_signal_quality (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_signal_state_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)signal_state_query_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Create Bearer (Modem interface) */
static MMBaseBearer *
modem_create_bearer_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
typedef struct {
guint32 session_id;
gboolean found;
} FindSessionId;
static void
bearer_list_session_id_foreach (MMBaseBearer *bearer,
gpointer user_data)
{
FindSessionId *ctx = user_data;
if (!ctx->found &&
MM_IS_BEARER_MBIM (bearer) &&
mm_bearer_mbim_get_session_id (MM_BEARER_MBIM (bearer)) == ctx->session_id)
ctx->found = TRUE;
}
static gint
find_next_bearer_session_id (MMBroadbandModemMbim *self)
{
MMBearerList *bearer_list;
guint i;
g_object_get (self,
MM_IFACE_MODEM_BEARER_LIST, &bearer_list,
NULL);
if (!bearer_list)
return 0;
for (i = 0; i <= 255; i++) {
FindSessionId ctx;
ctx.session_id = i;
ctx.found = FALSE;
mm_bearer_list_foreach (bearer_list,
bearer_list_session_id_foreach,
&ctx);
if (!ctx.found) {
g_object_unref (bearer_list);
return (gint)i;
}
}
/* no valid session id found */
g_object_unref (bearer_list);
return -1;
}
static void
modem_create_bearer (MMIfaceModem *_self,
MMBearerProperties *properties,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMBroadbandModemMbim *self = MM_BROADBAND_MODEM_MBIM (_self);
MMBaseBearer *bearer;
GTask *task;
gint session_id;
task = g_task_new (self, NULL, callback, user_data);
/* Find a new session ID */
session_id = find_next_bearer_session_id (self);
if (session_id < 0) {
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Not enough session IDs");
g_object_unref (task);
return;
}
/* We just create a MMBearerMbim */
mm_dbg ("Creating MBIM bearer in MBIM modem");
bearer = mm_bearer_mbim_new (self,
properties,
(guint)session_id);
g_task_return_pointer (task, bearer, g_object_unref);
g_object_unref (task);
}
/*****************************************************************************/
/* Create SIM (Modem interface) */
static MMBaseSim *
create_sim_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return mm_sim_mbim_new_finish (res, error);
}
static void
create_sim (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
/* New MBIM SIM */
mm_sim_mbim_new (MM_BASE_MODEM (self),
NULL, /* cancellable */
callback,
user_data);
}
/*****************************************************************************/
/* First enabling step */
static gboolean
enabling_started_finish (MMBroadbandModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
parent_enabling_started_ready (MMBroadbandModem *self,
GAsyncResult *res,
GTask *task)
{
GError *error = NULL;
if (!MM_BROADBAND_MODEM_CLASS (mm_broadband_modem_mbim_parent_class)->enabling_started_finish (
self,
res,
&error)) {
/* Don't treat this as fatal. Parent enabling may fail if it cannot grab a primary
* AT port, which isn't really an issue in MBIM-based modems */
mm_dbg ("Couldn't start parent enabling: %s", error->message);
g_error_free (error);
}
g_task_return_boolean (task, TRUE);
g_object_unref (task);
}
static void
enabling_started (MMBroadbandModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
MM_BROADBAND_MODEM_CLASS (mm_broadband_modem_mbim_parent_class)->enabling_started (
self,
(GAsyncReadyCallback)parent_enabling_started_ready,
task);
}
/*****************************************************************************/
/* First initialization step */
typedef struct {
MMPortMbim *mbim;
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
guint qmi_service_index;
#endif
} InitializationStartedContext;
static void
initialization_started_context_free (InitializationStartedContext *ctx)
{
if (ctx->mbim)
g_object_unref (ctx->mbim);
g_slice_free (InitializationStartedContext, ctx);
}
static gpointer
initialization_started_finish (MMBroadbandModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
parent_initialization_started_ready (MMBroadbandModem *self,
GAsyncResult *res,
GTask *task)
{
gpointer parent_ctx;
GError *error = NULL;
parent_ctx = MM_BROADBAND_MODEM_CLASS (mm_broadband_modem_mbim_parent_class)->initialization_started_finish (
self,
res,
&error);
if (error) {
/* Don't treat this as fatal. Parent initialization may fail if it cannot grab a primary
* AT port, which isn't really an issue in MBIM-based modems */
mm_dbg ("Couldn't start parent initialization: %s", error->message);
g_error_free (error);
}
/* Just parent's pointer passed here */
g_task_return_pointer (task, parent_ctx, NULL);
g_object_unref (task);
}
static void
parent_initialization_started (GTask *task)
{
MMBroadbandModem *self;
self = g_task_get_source_object (task);
MM_BROADBAND_MODEM_CLASS (mm_broadband_modem_mbim_parent_class)->initialization_started (
self,
(GAsyncReadyCallback)parent_initialization_started_ready,
task);
}
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
static const QmiService qmi_services[] = {
QMI_SERVICE_DMS,
QMI_SERVICE_NAS,
QMI_SERVICE_PDS,
QMI_SERVICE_LOC,
QMI_SERVICE_PDC,
};
static void allocate_next_qmi_client (GTask *task);
static void
mbim_port_allocate_qmi_client_ready (MMPortMbim *mbim,
GAsyncResult *res,
GTask *task)
{
InitializationStartedContext *ctx;
GError *error = NULL;
ctx = g_task_get_task_data (task);
if (!mm_port_mbim_allocate_qmi_client_finish (mbim, res, &error)) {
mm_dbg ("Couldn't allocate QMI client for service '%s': %s",
qmi_service_get_string (qmi_services[ctx->qmi_service_index]),
error->message);
g_error_free (error);
}
ctx->qmi_service_index++;
allocate_next_qmi_client (task);
}
static void
allocate_next_qmi_client (GTask *task)
{
InitializationStartedContext *ctx;
MMBroadbandModemMbim *self;
self = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
if (ctx->qmi_service_index == G_N_ELEMENTS (qmi_services)) {
parent_initialization_started (task);
return;
}
/* Otherwise, allocate next client */
mm_port_mbim_allocate_qmi_client (ctx->mbim,
qmi_services[ctx->qmi_service_index],
NULL,
(GAsyncReadyCallback)mbim_port_allocate_qmi_client_ready,
task);
}
#endif
static void
query_device_services_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MMBroadbandModemMbim *self;
MbimMessage *response;
GError *error = NULL;
MbimDeviceServiceElement **device_services;
guint32 device_services_count;
self = g_task_get_source_object (task);
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_device_services_response_parse (
response,
&device_services_count,
NULL, /* max_dss_sessions */
&device_services,
&error)) {
guint32 i;
for (i = 0; i < device_services_count; i++) {
MbimService service;
guint32 j;
service = mbim_uuid_to_service (&device_services[i]->device_service_id);
switch (service) {
case MBIM_SERVICE_MS_BASIC_CONNECT_EXTENSIONS:
for (j = 0; j < device_services[i]->cids_count; j++) {
if (device_services[i]->cids[j] == MBIM_CID_MS_BASIC_CONNECT_EXTENSIONS_PCO) {
mm_dbg ("PCO is supported");
self->priv->is_pco_supported = TRUE;
} else if (device_services[i]->cids[j] == MBIM_CID_MS_BASIC_CONNECT_EXTENSIONS_LTE_ATTACH_STATUS) {
mm_dbg ("LTE attach status is supported");
self->priv->is_lte_attach_status_supported = TRUE;
}
}
break;
case MBIM_SERVICE_USSD:
for (j = 0; j < device_services[i]->cids_count; j++) {
if (device_services[i]->cids[j] == MBIM_CID_USSD) {
mm_dbg ("USSD is supported");
self->priv->is_ussd_supported = TRUE;
break;
}
}
break;
case MBIM_SERVICE_ATDS:
for (j = 0; j < device_services[i]->cids_count; j++) {
if (device_services[i]->cids[j] == MBIM_CID_ATDS_LOCATION) {
mm_dbg ("ATDS location is supported");
self->priv->is_atds_location_supported = TRUE;
} else if (device_services[i]->cids[j] == MBIM_CID_ATDS_SIGNAL) {
mm_dbg ("ATDS signal is supported");
self->priv->is_atds_signal_supported = TRUE;
}
}
break;
default:
break;
}
}
mbim_device_service_element_array_free (device_services);
} else {
/* Ignore error */
mm_warn ("Couldn't query device services: %s", error->message);
g_error_free (error);
}
if (response)
mbim_message_unref (response);
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
allocate_next_qmi_client (task);
#else
parent_initialization_started (task);
#endif
}
static void
query_device_services (GTask *task)
{
InitializationStartedContext *ctx;
MbimMessage *message;
MbimDevice *device;
ctx = g_task_get_task_data (task);
device = mm_port_mbim_peek_device (ctx->mbim);
g_assert (device);
mm_dbg ("querying device services...");
message = mbim_message_device_services_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)query_device_services_ready,
task);
mbim_message_unref (message);
}
static void
mbim_device_removed_cb (MbimDevice *device,
MMBroadbandModemMbim *self)
{
/* We have to do a full re-probe here because simply reopening the device
* and restarting mbim-proxy will leave us without MBIM notifications. */
mm_info ("Connection to mbim-proxy for %s lost, reprobing",
mbim_device_get_path_display (device));
g_signal_handler_disconnect (device,
self->priv->mbim_device_removed_id);
self->priv->mbim_device_removed_id = 0;
mm_base_modem_set_reprobe (MM_BASE_MODEM (self), TRUE);
mm_base_modem_set_valid (MM_BASE_MODEM (self), FALSE);
}
static void
track_mbim_device_removed (MMBroadbandModemMbim *self,
MMPortMbim *mbim)
{
MbimDevice *device;
device = mm_port_mbim_peek_device (mbim);
g_assert (device);
/* Register removal handler so we can handle mbim-proxy crashes */
self->priv->mbim_device_removed_id = g_signal_connect (
device,
MBIM_DEVICE_SIGNAL_REMOVED,
G_CALLBACK (mbim_device_removed_cb),
self);
}
static void
untrack_mbim_device_removed (MMBroadbandModemMbim *self,
MMPortMbim *mbim)
{
MbimDevice *device;
if (self->priv->mbim_device_removed_id == 0)
return;
device = mm_port_mbim_peek_device (mbim);
if (!device)
return;
g_signal_handler_disconnect (device, self->priv->mbim_device_removed_id);
self->priv->mbim_device_removed_id = 0;
}
static void
mbim_port_open_ready (MMPortMbim *mbim,
GAsyncResult *res,
GTask *task)
{
GError *error = NULL;
if (!mm_port_mbim_open_finish (mbim, res, &error)) {
g_task_return_error (task, error);
g_object_unref (task);
return;
}
/* Make sure we know if mbim-proxy dies on us, and then do the parent's
* initialization */
track_mbim_device_removed (MM_BROADBAND_MODEM_MBIM (g_task_get_source_object (task)), mbim);
query_device_services (task);
}
static void
initialization_started (MMBroadbandModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
InitializationStartedContext *ctx;
GTask *task;
ctx = g_slice_new0 (InitializationStartedContext);
ctx->mbim = mm_base_modem_get_port_mbim (MM_BASE_MODEM (self));
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, ctx, (GDestroyNotify)initialization_started_context_free);
/* This may happen if we unplug the modem unexpectedly */
if (!ctx->mbim) {
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Cannot initialize: MBIM port went missing");
g_object_unref (task);
return;
}
if (mm_port_mbim_is_open (ctx->mbim)) {
/* Nothing to be done, just connect to a signal and launch parent's
* callback */
track_mbim_device_removed (MM_BROADBAND_MODEM_MBIM (self), ctx->mbim);
query_device_services (task);
return;
}
/* Now open our MBIM port */
mm_port_mbim_open (ctx->mbim,
#if defined WITH_QMI && QMI_MBIM_QMUX_SUPPORTED
TRUE, /* With QMI over MBIM support if available */
#endif
NULL,
(GAsyncReadyCallback)mbim_port_open_ready,
task);
}
/*****************************************************************************/
/* IMEI loading (3GPP interface) */
static gchar *
modem_3gpp_load_imei_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_pointer (G_TASK (res), error);
}
static void
modem_3gpp_load_imei (MMIfaceModem3gpp *_self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMBroadbandModemMbim *self = MM_BROADBAND_MODEM_MBIM (_self);
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
if (self->priv->caps_device_id)
g_task_return_pointer (task,
g_strdup (self->priv->caps_device_id),
g_free);
else
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Device doesn't report a valid IMEI");
g_object_unref (task);
}
/*****************************************************************************/
/* Facility locks status loading (3GPP interface) */
static MMModem3gppFacility
modem_3gpp_load_enabled_facility_locks_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
GError *inner_error = NULL;
gssize value;
value = g_task_propagate_int (G_TASK (res), &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return MM_MODEM_3GPP_FACILITY_NONE;
}
return (MMModem3gppFacility)value;
}
static void
pin_list_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MbimMessage *response;
GError *error = NULL;
MbimPinDesc *pin_desc_pin1;
MbimPinDesc *pin_desc_pin2;
MbimPinDesc *pin_desc_device_sim_pin;
MbimPinDesc *pin_desc_device_first_sim_pin;
MbimPinDesc *pin_desc_network_pin;
MbimPinDesc *pin_desc_network_subset_pin;
MbimPinDesc *pin_desc_service_provider_pin;
MbimPinDesc *pin_desc_corporate_pin;
response = mbim_device_command_finish (device, res, &error);
if (response &&
mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) &&
mbim_message_pin_list_response_parse (
response,
&pin_desc_pin1,
&pin_desc_pin2,
&pin_desc_device_sim_pin,
&pin_desc_device_first_sim_pin,
&pin_desc_network_pin,
&pin_desc_network_subset_pin,
&pin_desc_service_provider_pin,
&pin_desc_corporate_pin,
NULL, /* pin_desc_subsidy_lock */
NULL, /* pin_desc_custom */
&error)) {
MMModem3gppFacility mask = MM_MODEM_3GPP_FACILITY_NONE;
if (pin_desc_pin1->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_SIM;
mbim_pin_desc_free (pin_desc_pin1);
if (pin_desc_pin2->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_FIXED_DIALING;
mbim_pin_desc_free (pin_desc_pin2);
if (pin_desc_device_sim_pin->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_PH_SIM;
mbim_pin_desc_free (pin_desc_device_sim_pin);
if (pin_desc_device_first_sim_pin->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_PH_FSIM;
mbim_pin_desc_free (pin_desc_device_first_sim_pin);
if (pin_desc_network_pin->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_NET_PERS;
mbim_pin_desc_free (pin_desc_network_pin);
if (pin_desc_network_subset_pin->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_NET_SUB_PERS;
mbim_pin_desc_free (pin_desc_network_subset_pin);
if (pin_desc_service_provider_pin->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_PROVIDER_PERS;
mbim_pin_desc_free (pin_desc_service_provider_pin);
if (pin_desc_corporate_pin->pin_mode == MBIM_PIN_MODE_ENABLED)
mask |= MM_MODEM_3GPP_FACILITY_CORP_PERS;
mbim_pin_desc_free (pin_desc_corporate_pin);
g_task_return_int (task, mask);
} else
g_task_return_error (task, error);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static void
modem_3gpp_load_enabled_facility_locks (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_pin_list_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)pin_list_query_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Initial EPS bearer info loading */
static MMBearerProperties *
modem_3gpp_load_initial_eps_bearer_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return MM_BEARER_PROPERTIES (g_task_propagate_pointer (G_TASK (res), error));
}
static MMBearerProperties *
common_process_lte_attach_status (MMBroadbandModemMbim *self,
MbimLteAttachStatus *status,
GError **error)
{
MMBearerProperties *properties;
MMBearerIpFamily ip_family;
MMBearerAllowedAuth auth;
/* Remove LTE attach bearer info */
if (status->lte_attach_state == MBIM_LTE_ATTACH_STATE_DETACHED) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_WRONG_STATE, "Not attached to LTE");
return NULL;
}
properties = mm_bearer_properties_new ();
if (status->access_string)
mm_bearer_properties_set_apn (properties, status->access_string);
if (status->user_name)
mm_bearer_properties_set_user (properties, status->user_name);
if (status->password)
mm_bearer_properties_set_password (properties, status->password);
ip_family = mm_bearer_ip_family_from_mbim_context_ip_type (status->ip_type);
if (ip_family != MM_BEARER_IP_FAMILY_NONE)
mm_bearer_properties_set_ip_type (properties, ip_family);
auth = mm_bearer_allowed_auth_from_mbim_auth_protocol (status->auth_protocol);
if (auth != MM_BEARER_ALLOWED_AUTH_UNKNOWN)
mm_bearer_properties_set_allowed_auth (properties, auth);
/* note: we don't expose compression settings */
return properties;
}
static void
lte_attach_status_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MMBroadbandModemMbim *self;
MbimMessage *response;
GError *error = NULL;
MbimLteAttachStatus *status = NULL;
MMBearerProperties *properties;
self = g_task_get_source_object (task);
response = mbim_device_command_finish (device, res, &error);
if (!response ||
!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) ||
!mbim_message_ms_basic_connect_extensions_lte_attach_status_response_parse (
response,
&status,
&error)) {
g_task_return_error (task, error);
g_object_unref (task);
goto out;
}
properties = common_process_lte_attach_status (self, status, &error);
mbim_lte_attach_status_free (status);
g_assert (properties || error);
if (properties)
g_task_return_pointer (task, properties, g_object_unref);
else
g_task_return_error (task, error);
g_object_unref (task);
out:
if (response)
mbim_message_unref (response);
}
static void
modem_3gpp_load_initial_eps_bearer (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_ms_basic_connect_extensions_lte_attach_status_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)lte_attach_status_query_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Initial EPS bearer settings loading */
static MMBearerProperties *
modem_3gpp_load_initial_eps_bearer_settings_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return MM_BEARER_PROPERTIES (g_task_propagate_pointer (G_TASK (res), error));
}
static MMBearerProperties *
common_process_lte_attach_configuration (MMBroadbandModemMbim *self,
MbimLteAttachConfiguration *config,
GError **error)
{
MMBearerProperties *properties;
MMBearerIpFamily ip_family = MM_BEARER_IP_FAMILY_NONE;
MMBearerAllowedAuth auth;
properties = mm_bearer_properties_new ();
if (config->access_string)
mm_bearer_properties_set_apn (properties, config->access_string);
if (config->user_name)
mm_bearer_properties_set_user (properties, config->user_name);
if (config->password)
mm_bearer_properties_set_password (properties, config->password);
ip_family = mm_bearer_ip_family_from_mbim_context_ip_type (config->ip_type);
if (ip_family != MM_BEARER_IP_FAMILY_NONE)
mm_bearer_properties_set_ip_type (properties, ip_family);
auth = mm_bearer_allowed_auth_from_mbim_auth_protocol (config->auth_protocol);
if (auth != MM_BEARER_ALLOWED_AUTH_UNKNOWN)
mm_bearer_properties_set_allowed_auth (properties, auth);
/* note: we don't expose compression settings or the configuration source details */
return properties;
}
static void
lte_attach_configuration_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MMBroadbandModemMbim *self;
MbimMessage *response;
GError *error = NULL;
MMBearerProperties *properties = NULL;
guint32 n_configurations = 0;
MbimLteAttachConfiguration **configurations = NULL;
guint i;
self = g_task_get_source_object (task);
response = mbim_device_command_finish (device, res, &error);
if (!response ||
!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) ||
!mbim_message_ms_basic_connect_extensions_lte_attach_configuration_response_parse (
response,
&n_configurations,
&configurations,
&error)) {
g_task_return_error (task, error);
g_object_unref (task);
goto out;
}
/* We should always receive 3 configurations but the MBIM API doesn't force
* that so we'll just assume we don't get always the same fixed number */
for (i = 0; i < n_configurations; i++) {
/* We only support configuring the HOME settings */
if (configurations[i]->roaming != MBIM_LTE_ATTACH_CONTEXT_ROAMING_CONTROL_HOME)
continue;
properties = common_process_lte_attach_configuration (self, configurations[i], &error);
break;
}
mbim_lte_attach_configuration_array_free (configurations);
if (!properties && !error)
error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_NOT_FOUND,
"Couldn't find home network LTE attach settings");
g_assert (properties || error);
if (properties)
g_task_return_pointer (task, properties, g_object_unref);
else
g_task_return_error (task, error);
g_object_unref (task);
out:
if (response)
mbim_message_unref (response);
}
static void
modem_3gpp_load_initial_eps_bearer_settings (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MbimDevice *device;
MbimMessage *message;
GTask *task;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
message = mbim_message_ms_basic_connect_extensions_lte_attach_configuration_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)lte_attach_configuration_query_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Set initial EPS bearer settings
*
* The logic to set the EPS bearer settings requires us to first load the current
* settings from the module, because we are only going to change the settings
* associated to the HOME slot, we will leave untouched the PARTNER and NON-PARTNER
* slots.
*/
static gboolean
modem_3gpp_set_initial_eps_bearer_settings_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static void
set_lte_attach_configuration_set_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MMBroadbandModemMbim *self;
MbimMessage *response;
GError *error = NULL;
self = g_task_get_source_object (task);
response = mbim_device_command_finish (device, res, &error);
if (!response || !mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error))
g_task_return_error (task, error);
else
g_task_return_boolean (task, TRUE);
g_object_unref (task);
if (response)
mbim_message_unref (response);
}
static void
before_set_lte_attach_configuration_query_ready (MbimDevice *device,
GAsyncResult *res,
GTask *task)
{
MMBroadbandModemMbim *self;
MbimMessage *request;
MbimMessage *response;
GError *error = NULL;
MMBearerProperties *config;
guint32 n_configurations = 0;
MbimLteAttachConfiguration **configurations = NULL;
guint i;
self = g_task_get_source_object (task);
config = g_task_get_task_data (task);
response = mbim_device_command_finish (device, res, &error);
if (!response ||
!mbim_message_response_get_result (response, MBIM_MESSAGE_TYPE_COMMAND_DONE, &error) ||
!mbim_message_ms_basic_connect_extensions_lte_attach_configuration_response_parse (
response,
&n_configurations,
&configurations,
&error)) {
g_task_return_error (task, error);
g_object_unref (task);
goto out;
}
/* We should always receive 3 configurations but the MBIM API doesn't force
* that so we'll just assume we don't get always the same fixed number */
for (i = 0; i < n_configurations; i++) {
MMBearerIpFamily ip_family;
MMBearerAllowedAuth auth;
/* We only support configuring the HOME settings */
if (configurations[i]->roaming != MBIM_LTE_ATTACH_CONTEXT_ROAMING_CONTROL_HOME)
continue;
ip_family = mm_bearer_properties_get_ip_type (config);
if (ip_family == MM_BEARER_IP_FAMILY_NONE || ip_family == MM_BEARER_IP_FAMILY_ANY)
configurations[i]->ip_type = MBIM_CONTEXT_IP_TYPE_DEFAULT;
else {
configurations[i]->ip_type = mm_bearer_ip_family_to_mbim_context_ip_type (ip_family, &error);
if (error) {
configurations[i]->ip_type = MBIM_CONTEXT_IP_TYPE_DEFAULT;
mm_warn ("unexpected IP type settings requested: %s", error->message);
g_clear_error (&error);
}
}
auth = mm_bearer_properties_get_allowed_auth (config);
if (auth == MM_BEARER_ALLOWED_AUTH_UNKNOWN)
configurations[i]->auth_protocol = MBIM_AUTH_PROTOCOL_NONE;
else {
configurations[i]->auth_protocol = mm_bearer_allowed_auth_to_mbim_auth_protocol (auth, &error);
if (error) {
configurations[i]->auth_protocol = MBIM_AUTH_PROTOCOL_NONE;
mm_warn ("unexpected auth settings requested: %s", error->message);
g_clear_error (&error);
}
}
g_clear_pointer (&(configurations[i]->access_string), g_free);
configurations[i]->access_string = g_strdup (mm_bearer_properties_get_apn (config));
g_clear_pointer (&(configurations[i]->user_name), g_free);
configurations[i]->user_name = g_strdup (mm_bearer_properties_get_user (config));
g_clear_pointer (&(configurations[i]->password), g_free);
configurations[i]->password = g_strdup (mm_bearer_properties_get_user (config));
configurations[i]->source = MBIM_CONTEXT_SOURCE_USER;
configurations[i]->compression = MBIM_COMPRESSION_NONE;
break;
}
request = mbim_message_ms_basic_connect_extensions_lte_attach_configuration_set_new (
MBIM_LTE_ATTACH_CONTEXT_OPERATION_DEFAULT,
n_configurations,
(const MbimLteAttachConfiguration *const *)configurations,
&error);
if (!request) {
g_task_return_error (task, error);
g_object_unref (task);
goto out;
}
mbim_device_command (device,
request,
10,
NULL,
(GAsyncReadyCallback)set_lte_attach_configuration_set_ready,
task);
mbim_message_unref (request);
out:
if (configurations)
mbim_lte_attach_configuration_array_free (configurations);
if (response)
mbim_message_unref (response);
}
static void
modem_3gpp_set_initial_eps_bearer_settings (MMIfaceModem3gpp *self,
MMBearerProperties *config,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
MbimDevice *device;
MbimMessage *message;
if (!peek_device (self, &device, callback, user_data))
return;
task = g_task_new (self, NULL, callback, user_data);
g_task_set_task_data (task, g_object_ref (config), g_object_unref);
message = mbim_message_ms_basic_connect_extensions_lte_attach_configuration_query_new (NULL);
mbim_device_command (device,
message,
10,
NULL,
(GAsyncReadyCallback)before_set_lte_attach_configuration_query_ready,
task);
mbim_message_unref (message);
}
/*****************************************************************************/
/* Common unsolicited events setup and cleanup */
static void
basic_connect_notification_signal_state (MMBroadbandModemMbim *self,
MbimMessage *notification)
{
guint32 rssi;
if (mbim_message_signal_state_notification_parse (
notification,
&rssi,
NULL, /* error_rate */
NULL, /* signal_strength_interval */
NULL, /* rssi_threshold */
NULL, /* error_rate_threshold */
NULL)) {
guint32 quality;
/* Normalize the quality. 99 means unknown, we default it to 0 */
quality = CLAMP (rssi == 99 ? 0 : rssi, 0, 31) * 100 / 31;
mm_dbg ("Signal state indication: %u --> %u%%", rssi, quality);
mm_iface_modem_update_signal_quality (MM_IFACE_MODEM (self), quality);
}
}
static void
update_access_technologies (MMBroadbandModemMbim *self)
{
MMModemAccessTechnology act;
act = mm_modem_access_technology_from_mbim_data_class (self->priv->highest_available_data_class);
if (act == MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN)
act = mm_modem_access_technology_from_mbim_data_class (self->priv->available_data_classes);
mm_iface_modem_3gpp_update_access_technologies (MM_IFACE_MODEM_3GPP (self), act);
}
static void
update_registration_info (MMBroadbandModemMbim *self,
MbimRegisterState state,
MbimDataClass available_data_classes,
gchar *operator_id_take,
gchar *operator_name_take)
{
MMModem3gppRegistrationState reg_state;
reg_state = mm_modem_3gpp_registration_state_from_mbim_register_state (state);
if (reg_state == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
reg_state == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) {
if (self->priv->current_operator_id && operator_id_take &&
g_str_equal (self->priv->current_operator_id, operator_id_take)) {
g_free (operator_id_take);
} else {
g_free (self->priv->current_operator_id);
self->priv->current_operator_id = operator_id_take;
}
if (self->priv->current_operator_name && operator_name_take &&
g_str_equal (self->priv->current_operator_name, operator_name_take)) {
g_free (operator_name_take);
} else {
g_free (self->priv->current_operator_name);
self->priv->current_operator_name = operator_name_take;
}
} else {
g_clear_pointer (&self->priv->current_operator_id, g_free);
g_clear_pointer (&self->priv->current_operator_name, g_free);
g_free (operator_id_take);
g_free (operator_name_take);
}
mm_iface_modem_3gpp_update_ps_registration_state (
MM_IFACE_MODEM_3GPP (self),
reg_state);
self->priv->available_data_classes = available_data_classes;
update_access_technologies (self);
}
static void
basic_connect_notification_register_state (MMBroadbandModemMbim *self,
MbimMessage *notification)
{
MbimRegisterState register_state;
MbimDataClass available_data_classes;
gchar *provider_id;
gchar *provider_name;
if (mbim_message_register_state_notification_parse (
notification,
NULL, /* nw_error */
&register_state,
NULL, /* register_mode */
&available_data_classes,
NULL, /* current_cellular_class */
&provider_id,
&provider_name,
NULL, /* roaming_text */
NULL, /* registration_flag */
NULL)) {
update_registration_info (self,
register_state,
available_data_classes,
provider_id,
provider_name);
}
}
typedef struct {
guint32 session_id;
} ReportDisconnectedStatusContext;
static void
bearer_list_report_disconnected_status (MMBaseBearer *bearer,
gpointer user_data)
{
ReportDisconnectedStatusContext *ctx = user_data;
if (MM_IS_BEARER_MBIM (bearer) &&
mm_bearer_mbim_get_session_id (MM_BEARER_MBIM (bearer)) == ctx->session_id) {
mm_dbg ("Bearer '%s' was disconnected.", mm_base_bearer_get_path (bearer));
mm_base_bearer_report_connection_status (bearer, MM_BEARER_CONNECTION_STATUS_DISCONNECTED);
}
}