diff options
author | Guido Günther <agx@sigxcpu.org> | 2014-02-05 08:38:30 +0100 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2014-02-05 08:38:30 +0100 |
commit | 13ed135b9ae78c692dc359976eb8b54d0a3629b8 (patch) | |
tree | ae2ea713ad51d73980cf83db1411d6589dac5e8b /plugins/icera | |
parent | 14d771b90f5a7d3887e5e900d1fb4737477ad305 (diff) |
Imported Upstream version 0.7.991upstream/0.7.991
Diffstat (limited to 'plugins/icera')
-rw-r--r-- | plugins/icera/mm-broadband-bearer-icera.c | 1189 | ||||
-rw-r--r-- | plugins/icera/mm-broadband-bearer-icera.h | 74 | ||||
-rw-r--r-- | plugins/icera/mm-broadband-modem-icera.c | 1933 | ||||
-rw-r--r-- | plugins/icera/mm-broadband-modem-icera.h | 53 |
4 files changed, 3249 insertions, 0 deletions
diff --git a/plugins/icera/mm-broadband-bearer-icera.c b/plugins/icera/mm-broadband-bearer-icera.c new file mode 100644 index 0000000..b7ea8df --- /dev/null +++ b/plugins/icera/mm-broadband-bearer-icera.c @@ -0,0 +1,1189 @@ +/* -*- 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) 2012 Google, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#include <config.h> + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <ctype.h> +#include <arpa/inet.h> + +#include <ModemManager.h> +#define _LIBMM_INSIDE_MM +#include <libmm-glib.h> + +#include "mm-broadband-bearer-icera.h" +#include "mm-base-modem-at.h" +#include "mm-log.h" +#include "mm-modem-helpers.h" +#include "mm-error-helpers.h" + +G_DEFINE_TYPE (MMBroadbandBearerIcera, mm_broadband_bearer_icera, MM_TYPE_BROADBAND_BEARER); + +enum { + PROP_0, + PROP_DEFAULT_IP_METHOD, + PROP_LAST +}; + +static GParamSpec *properties[PROP_LAST]; + +struct _MMBroadbandBearerIceraPrivate { + MMBearerIpMethod default_ip_method; + + /* Connection related */ + gpointer connect_pending; + guint connect_pending_id; + gulong connect_cancellable_id; + gulong connect_port_closed_id; + + /* Disconnection related */ + gpointer disconnect_pending; + guint disconnect_pending_id; +}; + +/*****************************************************************************/ +/* 3GPP IP config retrieval (sub-step of the 3GPP Connection sequence) */ + +typedef struct { + MMBroadbandBearerIcera *self; + MMBaseModem *modem; + MMAtSerialPort *primary; + guint cid; + GSimpleAsyncResult *result; +} GetIpConfig3gppContext; + +static GetIpConfig3gppContext * +get_ip_config_3gpp_context_new (MMBroadbandBearerIcera *self, + MMBaseModem *modem, + MMAtSerialPort *primary, + guint cid, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GetIpConfig3gppContext *ctx; + + ctx = g_new0 (GetIpConfig3gppContext, 1); + ctx->self = g_object_ref (self); + ctx->modem = g_object_ref (modem); + ctx->primary = g_object_ref (primary); + ctx->cid = cid; + ctx->result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + get_ip_config_3gpp_context_new); + return ctx; +} + +static void +get_ip_config_context_complete_and_free (GetIpConfig3gppContext *ctx) +{ + g_simple_async_result_complete_in_idle (ctx->result); + g_object_unref (ctx->result); + g_object_unref (ctx->primary); + g_object_unref (ctx->modem); + g_object_unref (ctx->self); + g_free (ctx); +} + +static gboolean +get_ip_config_3gpp_finish (MMBroadbandBearer *self, + GAsyncResult *res, + MMBearerIpConfig **ipv4_config, + MMBearerIpConfig **ipv6_config, + GError **error) +{ + MMBearerIpConfig *ip_config; + + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return FALSE; + + /* No IPv6 for now */ + ip_config = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)); + *ipv4_config = g_object_ref (ip_config); + *ipv6_config = NULL; + return TRUE; +} + +#define IPDPADDR_TAG "%IPDPADDR: " + +static void +ip_config_ready (MMBaseModem *modem, + GAsyncResult *res, + GetIpConfig3gppContext *ctx) +{ + MMBearerIpConfig *ip_config = NULL; + const gchar *response; + GError *error = NULL; + gchar **items; + gchar *dns[3] = { 0 }; + guint i; + guint dns_i; + + response = mm_base_modem_at_command_full_finish (MM_BASE_MODEM (modem), res, &error); + if (error) { + g_simple_async_result_take_error (ctx->result, error); + get_ip_config_context_complete_and_free (ctx); + return; + } + + /* TODO: use a regex to parse this */ + + /* Check result */ + if (!g_str_has_prefix (response, IPDPADDR_TAG)) { + g_simple_async_result_set_error (ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Couldn't get IP config: invalid response '%s'", + response); + get_ip_config_context_complete_and_free (ctx); + return; + } + + /* %IPDPADDR: <cid>,<ip>,<gw>,<dns1>,<dns2>[,<nbns1>,<nbns2>[,<??>,<netmask>,<gw>]] + * + * Sierra USB305: %IPDPADDR: 2, 21.93.217.11, 21.93.217.10, 10.177.0.34, 10.161.171.220, 0.0.0.0, 0.0.0.0 + * K3805-Z: %IPDPADDR: 2, 21.93.217.11, 21.93.217.10, 10.177.0.34, 10.161.171.220, 0.0.0.0, 0.0.0.0, 255.0.0.0, 255.255.255.0, 21.93.217.10, + */ + response = mm_strip_tag (response, IPDPADDR_TAG); + items = g_strsplit (response, ", ", 0); + + ip_config = mm_bearer_ip_config_new (); + + for (i = 0, dns_i = 0; items[i]; i++) { + if (i == 0) { /* CID */ + gint num; + + if (!mm_get_int_from_str (items[i], &num) || + num != ctx->cid) { + error = g_error_new (MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Unknown CID in IPDPADDR response (" + "got %d, expected %d)", + (guint) num, + ctx->cid); + break; + } + } else if (i == 1) { /* IP address */ + guint32 tmp = 0; + + if (!inet_pton (AF_INET, items[i], &tmp)) { + mm_warn ("Couldn't parse IP address '%s'", items[i]); + g_clear_object (&ip_config); + break; + } + + mm_bearer_ip_config_set_method (ip_config, MM_BEARER_IP_METHOD_STATIC); + mm_bearer_ip_config_set_address (ip_config, items[i]); + } else if (i == 2) { /* Gateway */ + guint32 tmp = 0; + + if (!inet_pton (AF_INET, items[i], &tmp)) { + mm_warn ("Couldn't parse gateway address '%s'", items[i]); + g_clear_object (&ip_config); + break; + } + + if (tmp) + mm_bearer_ip_config_set_gateway (ip_config, items[i]); + } else if (i == 3 || i == 4) { /* DNS entries */ + guint32 tmp = 0; + + if (!inet_pton (AF_INET, items[i], &tmp)) { + mm_warn ("Couldn't parse DNS address '%s'", items[i]); + g_clear_object (&ip_config); + break; + } + + if (tmp) + dns[dns_i++] = items[i]; + } else if (i == 8) { /* Netmask */ + guint32 tmp = 0; + + if (!inet_pton (AF_INET, items[i], &tmp)) { + mm_warn ("Couldn't parse netmask '%s'", items[i]); + g_clear_object (&ip_config); + break; + } + + mm_bearer_ip_config_set_prefix (ip_config, mm_netmask_to_cidr (items[i])); + } else if (i == 9) { /* Duplicate Gateway */ + if (!mm_bearer_ip_config_get_gateway (ip_config)) { + guint32 tmp = 0; + + if (!inet_pton (AF_INET, items[i], &tmp)) { + mm_warn ("Couldn't parse (duplicate) gateway address '%s'", items[i]); + g_clear_object (&ip_config); + break; + } + + if (tmp) + mm_bearer_ip_config_set_gateway (ip_config, items[i]); + } + } + } + + if (!ip_config) { + if (error) + g_simple_async_result_take_error (ctx->result, error); + else + g_simple_async_result_set_error (ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Couldn't get IP config: couldn't parse response '%s'", + response); + } else { + /* If we got DNS entries, set them in the IP config */ + if (dns[0]) + mm_bearer_ip_config_set_dns (ip_config, (const gchar **)dns); + + g_simple_async_result_set_op_res_gpointer (ctx->result, + ip_config, + (GDestroyNotify)g_object_unref); + } + + get_ip_config_context_complete_and_free (ctx); + g_strfreev (items); +} + +static void +get_ip_config_3gpp (MMBroadbandBearer *self, + MMBroadbandModem *modem, + MMAtSerialPort *primary, + MMAtSerialPort *secondary, + MMPort *data, + guint cid, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GetIpConfig3gppContext *ctx; + + ctx = get_ip_config_3gpp_context_new (MM_BROADBAND_BEARER_ICERA (self), + MM_BASE_MODEM (modem), + primary, + cid, + callback, + user_data); + + if (ctx->self->priv->default_ip_method == MM_BEARER_IP_METHOD_STATIC) { + gchar *command; + + command = g_strdup_printf ("%%IPDPADDR=%d", cid); + mm_base_modem_at_command_full (MM_BASE_MODEM (modem), + primary, + command, + 3, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)ip_config_ready, + ctx); + g_free (command); + return; + } + + /* Otherwise, DHCP */ + if (ctx->self->priv->default_ip_method == MM_BEARER_IP_METHOD_DHCP) { + MMBearerIpConfig *ip_config; + + ip_config = mm_bearer_ip_config_new (); + mm_bearer_ip_config_set_method (ip_config, MM_BEARER_IP_METHOD_DHCP); + g_simple_async_result_set_op_res_gpointer (ctx->result, + ip_config, + (GDestroyNotify)g_object_unref); + get_ip_config_context_complete_and_free (ctx); + return; + } + + g_assert_not_reached (); +} + +/*****************************************************************************/ +/* 3GPP disconnection */ + +typedef struct { + MMBroadbandBearerIcera *self; + GSimpleAsyncResult *result; +} Disconnect3gppContext; + +static void +disconnect_3gpp_context_complete_and_free (Disconnect3gppContext *ctx) +{ + g_simple_async_result_complete (ctx->result); + g_object_unref (ctx->result); + g_object_unref (ctx->self); + g_free (ctx); +} + +static gboolean +disconnect_3gpp_finish (MMBroadbandBearer *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static gboolean +disconnect_3gpp_timed_out_cb (MMBroadbandBearerIcera *self) +{ + Disconnect3gppContext *ctx; + + /* Recover context */ + ctx = self->priv->disconnect_pending; + + self->priv->disconnect_pending = NULL; + self->priv->disconnect_pending_id = 0; + + g_simple_async_result_set_error (ctx->result, + MM_SERIAL_ERROR, + MM_SERIAL_ERROR_RESPONSE_TIMEOUT, + "Disconnection attempt timed out"); + + disconnect_3gpp_context_complete_and_free (ctx); + return FALSE; +} + +static void +report_disconnect_status (MMBroadbandBearerIcera *self, + MMBroadbandBearerIceraConnectionStatus status) +{ + Disconnect3gppContext *ctx; + + /* Recover context */ + ctx = self->priv->disconnect_pending; + self->priv->disconnect_pending = NULL; + + /* Cleanup timeout, if any */ + if (self->priv->disconnect_pending_id) { + g_source_remove (self->priv->disconnect_pending_id); + self->priv->disconnect_pending_id = 0; + } + + switch (status) { + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_UNKNOWN: + g_warn_if_reached (); + break; + + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTED: + if (!ctx) + break; + + g_simple_async_result_set_error (ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Disconnection failed"); + disconnect_3gpp_context_complete_and_free (ctx); + return; + + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTION_FAILED: + if (!ctx) + break; + + /* Well, this actually means disconnection, right? */ + g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); + disconnect_3gpp_context_complete_and_free (ctx); + return; + + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_DISCONNECTED: + if (!ctx) { + mm_dbg ("Received spontaneous %%IPDPACT disconnect"); + mm_bearer_report_disconnection (MM_BEARER (self)); + break; + } + + g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); + disconnect_3gpp_context_complete_and_free (ctx); + return; + } +} + +static void +disconnect_ipdpact_ready (MMBaseModem *modem, + GAsyncResult *res, + MMBroadbandBearerIcera *self) +{ + Disconnect3gppContext *ctx; + GError *error = NULL; + + /* Try to recover the disconnection context. If none found, it means the + * context was already completed and we have nothing else to do. */ + ctx = self->priv->disconnect_pending; + + /* Balance refcount with the extra ref we passed to command_full() */ + g_object_unref (self); + + if (!ctx) { + mm_dbg ("Disconnection context was finished already by an unsolicited message"); + + /* Run _finish() to finalize the async call, even if we don't care + * the result */ + mm_base_modem_at_command_full_finish (modem, res, NULL); + return; + } + + mm_base_modem_at_command_full_finish (MM_BASE_MODEM (modem), res, &error); + if (error) { + self->priv->disconnect_pending = NULL; + g_simple_async_result_take_error (ctx->result, error); + disconnect_3gpp_context_complete_and_free (ctx); + return; + } + + /* Set a 60-second disconnection-failure timeout */ + self->priv->disconnect_pending_id = g_timeout_add_seconds (60, + (GSourceFunc)disconnect_3gpp_timed_out_cb, + self); +} + +static void +disconnect_3gpp (MMBroadbandBearer *bearer, + MMBroadbandModem *modem, + MMAtSerialPort *primary, + MMAtSerialPort *secondary, + MMPort *data, + guint cid, + GAsyncReadyCallback callback, + gpointer user_data) +{ + MMBroadbandBearerIcera *self = MM_BROADBAND_BEARER_ICERA (bearer); + gchar *command; + Disconnect3gppContext *ctx; + + ctx = g_new0 (Disconnect3gppContext, 1); + ctx->self = g_object_ref (self); + ctx->result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + disconnect_3gpp); + + /* The unsolicited response to %IPDPACT may come before the OK does. + * We will keep the disconnection context in the bearer private data so + * that it is accessible from the unsolicited message handler. Note + * also that we do NOT pass the ctx to the GAsyncReadyCallback, as it + * may not be valid any more when the callback is called (it may be + * already completed in the unsolicited handling) */ + g_assert (ctx->self->priv->disconnect_pending == NULL); + ctx->self->priv->disconnect_pending = ctx; + + command = g_strdup_printf ("%%IPDPACT=%d,0", cid); + mm_base_modem_at_command_full ( + MM_BASE_MODEM (modem), + primary, + command, + 60, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)disconnect_ipdpact_ready, + g_object_ref (ctx->self)); /* we pass the bearer object! */ + g_free (command); +} + +/*****************************************************************************/ +/* 3GPP Dialing (sub-step of the 3GPP Connection sequence) */ + +typedef struct { + MMBroadbandBearerIcera *self; + MMBaseModem *modem; + MMAtSerialPort *primary; + guint cid; + GCancellable *cancellable; + GSimpleAsyncResult *result; + MMPort *data; + guint authentication_retries; + GError *saved_error; +} Dial3gppContext; + +static void +dial_3gpp_context_complete_and_free (Dial3gppContext *ctx) +{ + g_simple_async_result_complete_in_idle (ctx->result); + if (ctx->data) + g_object_unref (ctx->data); + g_object_unref (ctx->cancellable); + g_object_unref (ctx->result); + g_object_unref (ctx->primary); + g_object_unref (ctx->modem); + g_object_unref (ctx->self); + g_slice_free (Dial3gppContext, ctx); +} + +static gboolean +dial_3gpp_context_set_error_if_cancelled (Dial3gppContext *ctx, + GError **error) +{ + if (!g_cancellable_is_cancelled (ctx->cancellable)) + return FALSE; + + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_CANCELLED, + "Dial operation has been cancelled"); + return TRUE; +} + +static gboolean +dial_3gpp_context_complete_and_free_if_cancelled (Dial3gppContext *ctx) +{ + GError *error = NULL; + + if (!dial_3gpp_context_set_error_if_cancelled (ctx, &error)) + return FALSE; + + g_simple_async_result_take_error (ctx->result, error); + dial_3gpp_context_complete_and_free (ctx); + return TRUE; +} + +static MMPort * +dial_3gpp_finish (MMBroadbandBearer *self, + GAsyncResult *res, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return NULL; + + return MM_PORT (g_object_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res)))); +} + +static void +connect_reset_ready (MMBaseModem *modem, + GAsyncResult *res, + Dial3gppContext *ctx) +{ + mm_base_modem_at_command_full_finish (modem, res, NULL); + + /* error should have already been set in the simple async result */ + dial_3gpp_context_complete_and_free (ctx); +} + +static void +connect_reset (Dial3gppContext *ctx) +{ + gchar *command; + + /* Need to reset the connection attempt */ + command = g_strdup_printf ("%%IPDPACT=%d,0", ctx->cid); + mm_base_modem_at_command_full (ctx->modem, + ctx->primary, + command, + 3, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)connect_reset_ready, + ctx); + g_free (command); +} + +static gboolean +connect_timed_out_cb (MMBroadbandBearerIcera *self) +{ + Dial3gppContext *ctx; + + /* Recover context and remove it from the private info */ + ctx = self->priv->connect_pending; + self->priv->connect_pending = NULL; + + /* Remove cancellation, if found */ + if (self->priv->connect_cancellable_id) { + g_cancellable_disconnect (ctx->cancellable, + self->priv->connect_cancellable_id); + self->priv->connect_cancellable_id = 0; + } + + /* Remove closed port watch, if found */ + if (ctx && self->priv->connect_port_closed_id) { + g_signal_handler_disconnect (ctx->primary, self->priv->connect_port_closed_id); + self->priv->connect_port_closed_id = 0; + } + + /* Cleanup timeout ID */ + self->priv->connect_pending_id = 0; + + /* If we were cancelled, prefer that error */ + if (ctx->saved_error) { + g_simple_async_result_take_error (ctx->result, ctx->saved_error); + ctx->saved_error = NULL; + } else + g_simple_async_result_set_error (ctx->result, + MM_MOBILE_EQUIPMENT_ERROR, + MM_MOBILE_EQUIPMENT_ERROR_NETWORK_TIMEOUT, + "Connection attempt timed out"); + + /* It's probably pointless to try to reset this here, but anyway... */ + connect_reset (ctx); + + return FALSE; +} + +static void +connect_cancelled_cb (GCancellable *cancellable, + MMBroadbandBearerIcera *self) +{ + Dial3gppContext *ctx; + + /* Recover context but DON'T remove it from the private info */ + ctx = self->priv->connect_pending; + + /* Remove the cancellable + * NOTE: we shouldn't remove the timeout yet. We still need to wait + * to get connected before running the explicit connection reset */ + self->priv->connect_cancellable_id = 0; + + /* Store cancelled error */ + g_assert (dial_3gpp_context_set_error_if_cancelled (ctx, &ctx->saved_error)); + + /* We cannot reset right here, we need to wait for the connection + * attempt to finish */ +} + +static void +forced_close_cb (MMSerialPort *port, + MMBroadbandBearerIcera *self) +{ + /* Just treat the forced close event as any other unsolicited message */ + mm_broadband_bearer_icera_report_connection_status ( + self, + MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTION_FAILED); +} + +static void +ier_query_ready (MMBaseModem *modem, + GAsyncResult *res, + Dial3gppContext *ctx) +{ + const gchar *response; + GError *activation_error = NULL; + + response = mm_base_modem_at_command_full_finish (modem, res, NULL); + if (response) { + gint nw_activation_err; + + response = mm_strip_tag (response, "%IER:"); + if (sscanf (response, "%*d,%*d,%d", &nw_activation_err)) { + /* 3GPP TS 24.008 Annex G error codes: + * 27 - Unknown or missing access point name + * 33 - Requested service option not subscribed + */ + if (nw_activation_err == 27 || nw_activation_err == 33) + activation_error = mm_mobile_equipment_error_for_code ( + MM_MOBILE_EQUIPMENT_ERROR_GPRS_SERVICE_OPTION_NOT_SUBSCRIBED); + } + } + + if (activation_error) + g_simple_async_result_take_error (ctx->result, activation_error); + else + g_simple_async_result_set_error (ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Call setup failed"); + dial_3gpp_context_complete_and_free (ctx); +} + +static void +report_connect_status (MMBroadbandBearerIcera *self, + MMBroadbandBearerIceraConnectionStatus status) +{ + Dial3gppContext *ctx; + + /* Recover context and remove it from the private info */ + ctx = self->priv->connect_pending; + self->priv->connect_pending = NULL; + + /* Cleanup cancellable, timeout and port closed watch, if any */ + if (self->priv->connect_pending_id) { + g_source_remove (self->priv->connect_pending_id); + self->priv->connect_pending_id = 0; + } + + if (ctx && self->priv->connect_cancellable_id) { + g_cancellable_disconnect (ctx->cancellable, + self->priv->connect_cancellable_id); + self->priv->connect_cancellable_id = 0; + } + + if (ctx && self->priv->connect_port_closed_id) { + g_signal_handler_disconnect (ctx->primary, self->priv->connect_port_closed_id); + self->priv->connect_port_closed_id = 0; + } + + switch (status) { + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_UNKNOWN: + break; + + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTED: + if (!ctx) + /* We may get this if the timeout for the connection attempt is + * reached before the unsolicited response. We should probably + * keep the CID around to request explicit disconnection in this + * case. */ + break; + + /* If we wanted to get cancelled before, do it now */ + if (ctx->saved_error) { + /* Keep error */ + g_simple_async_result_take_error (ctx->result, ctx->saved_error); + ctx->saved_error = NULL; + /* Cancel connection */ + connect_reset (ctx); + return; + } + + g_simple_async_result_set_op_res_gpointer (ctx->result, + g_object_ref (ctx->data), + (GDestroyNotify)g_object_unref); + dial_3gpp_context_complete_and_free (ctx); + return; + + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTION_FAILED: + if (!ctx) + break; + + /* If we wanted to get cancelled before and now we couldn't connect, + * use the cancelled error and return */ + if (ctx->saved_error) { + g_simple_async_result_take_error (ctx->result, ctx->saved_error); + ctx->saved_error = NULL; + dial_3gpp_context_complete_and_free (ctx); + return; + } + + /* Try to gather additional info about the connection failure */ + mm_base_modem_at_command_full ( + ctx->modem, + ctx->primary, + "%IER?", + 60, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)ier_query_ready, + ctx); + return; + + case MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_DISCONNECTED: + if (ctx) { + /* If we wanted to get cancelled before and now we couldn't connect, + * use the cancelled error and return */ + if (ctx->saved_error) { + g_simple_async_result_take_error (ctx->result, ctx->saved_error); + ctx->saved_error = NULL; + dial_3gpp_context_complete_and_free (ctx); + return; + } + + g_simple_async_result_set_error (ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Call setup failed"); + dial_3gpp_context_complete_and_free (ctx); + return; + } + + /* Just ensure we mark ourselves as being disconnected... */ + mm_bearer_report_disconnection (MM_BEARER (self)); + return; + } + + g_warn_if_reached (); +} + +static void +activate_ready (MMBaseModem *modem, + GAsyncResult *res, + MMBroadbandBearerIcera *self) +{ + Dial3gppContext *ctx; + GError *error = NULL; + + /* Try to recover the connection context. If none found, it means the + * context was already completed and we have nothing else to do. */ + ctx = self->priv->connect_pending; + + /* Balance refcount with the extra ref we passed to command_full() */ + g_object_unref (self); + + if (!ctx) { + mm_dbg ("Connection context was finished already by an unsolicited message"); + + /* Run _finish() to finalize the async call, even if we don't care + * the result */ + mm_base_modem_at_command_full_finish (modem, res, NULL); + return; + } + + /* Errors on the dial command are fatal */ + if (!mm_base_modem_at_command_full_finish (modem, res, &error)) { + self->priv->connect_pending = NULL; + g_simple_async_result_take_error (ctx->result, error); + dial_3gpp_context_complete_and_free (ctx); + return; + } + + /* We will now setup a timeout and keep the context in the bearer's private. + * Reports of modem being connected will arrive via unsolicited messages. + * This timeout should be long enough. Actually... ideally should never get + * reached. */ + self->priv->connect_pending_id = g_timeout_add_seconds (60, + (GSourceFunc)connect_timed_out_cb, + self); + + /* From now on, if we get cancelled, we'll still need to wait for the connection + * attempt to finish before resetting it */ + self->priv->connect_cancellable_id = g_cancellable_connect (ctx->cancellable, + G_CALLBACK (connect_cancelled_cb), + self, + NULL); + + /* If we get the port closed, we treat as a connect error */ + self->priv->connect_port_closed_id = g_signal_connect (ctx->primary, + "forced-close", + G_CALLBACK (forced_close_cb), + self); +} + +static void +deactivate_ready (MMBaseModem *modem, + GAsyncResult *res, + Dial3gppContext *ctx) +{ + gchar *command; + + /* + * Ignore any error here; %IPDPACT=ctx,0 will produce an error 767 + * if the context is not, in fact, connected. This is annoying but + * harmless. + */ + mm_base_modem_at_command_full_finish (modem, res, NULL); + + /* The unsolicited response to %IPDPACT may come before the OK does. + * We will keep the connection context in the bearer private data so + * that it is accessible from the unsolicited message handler. Note + * also that we do NOT pass the ctx to the GAsyncReadyCallback, as it + * may not be valid any more when the callback is called (it may be + * already completed in the unsolicited handling) */ + g_assert (ctx->self->priv->connect_pending == NULL); + ctx->self->priv->connect_pending = ctx; + + command = g_strdup_printf ("%%IPDPACT=%d,1", ctx->cid); + mm_base_modem_at_command_full ( + ctx->modem, + ctx->primary, + command, + 60, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)activate_ready, + g_object_ref (ctx->self)); /* we pass the bearer object! */ + g_free (command); +} + +static void authenticate (Dial3gppContext *ctx); + +static gboolean +retry_authentication_cb (Dial3gppContext *ctx) +{ + authenticate (ctx); + return FALSE; +} + +static void +authenticate_ready (MMBaseModem *modem, + GAsyncResult *res, + Dial3gppContext *ctx) +{ + GError *error = NULL; + gchar *command; + + /* If cancelled, complete */ + if (dial_3gpp_context_complete_and_free_if_cancelled (ctx)) + return; + + if (!mm_base_modem_at_command_full_finish (modem, res, &error)) { + /* Retry configuring the context. It sometimes fails with a 583 + * error ["a profile (CID) is currently active"] if a connect + * is attempted too soon after a disconnect. */ + if (++ctx->authentication_retries < 3) { + mm_dbg ("Authentication failed: '%s'; retrying...", error->message); + g_error_free (error); + g_timeout_add_seconds (1, (GSourceFunc)retry_authentication_cb, ctx); + return; + } + + /* Return an error */ + g_simple_async_result_take_error (ctx->result, error); + dial_3gpp_context_complete_and_free (ctx); + return; + } + + /* + * Deactivate the context we want to use before we try to activate + * it. This handles the case where ModemManager crashed while + * connected and is now trying to reconnect. (Should some part of + * the core or modem driver have made sure of this already?) + */ + command = g_strdup_printf ("%%IPDPACT=%d,0", ctx->cid); + mm_base_modem_at_command_full ( + ctx->modem, + ctx->primary, + command, + 60, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)deactivate_ready, + ctx); + g_free (command); +} + +static void +authenticate (Dial3gppContext *ctx) +{ + gchar *command; + const gchar *user; + const gchar *password; + MMBearerAllowedAuth allowed_auth; + + user = mm_bearer_properties_get_user (mm_bearer_peek_config (MM_BEARER (ctx->self))); + password = mm_bearer_properties_get_password (mm_bearer_peek_config (MM_BEARER (ctx->self))); + allowed_auth = mm_bearer_properties_get_allowed_auth (mm_bearer_peek_config (MM_BEARER (ctx->self))); + + /* Both user and password are required; otherwise firmware returns an error */ + if (!user || !password || allowed_auth == MM_BEARER_ALLOWED_AUTH_NONE) { + mm_dbg ("Not using authentication"); + command = g_strdup_printf ("%%IPDPCFG=%d,0,0,\"\",\"\"", ctx->cid); + } else { + gchar *quoted_user; + gchar *quoted_password; + guint icera_auth; + + if (allowed_auth == MM_BEARER_ALLOWED_AUTH_UNKNOWN) { + mm_dbg ("Using default (PAP) authentication method"); + icera_auth = 1; + } else if (allowed_auth & MM_BEARER_ALLOWED_AUTH_PAP) { + mm_dbg ("Using PAP authentication method"); + icera_auth = 1; + } else if (allowed_auth & MM_BEARER_ALLOWED_AUTH_CHAP) { + mm_dbg ("Using CHAP authentication method"); + icera_auth = 2; + } else { + gchar *str; + + str = mm_bearer_allowed_auth_build_string_from_mask (allowed_auth); + g_simple_async_result_set_error ( + ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_UNSUPPORTED, + "Cannot use any of the specified authentication methods (%s)", + str); + g_free (str); + dial_3gpp_context_complete_and_free (ctx); + return; + } + + quoted_user = mm_at_serial_port_quote_string (user); + quoted_password = mm_at_serial_port_quote_string (password); + command = g_strdup_printf ("%%IPDPCFG=%d,0,%u,%s,%s", + ctx->cid, icera_auth, quoted_user, quoted_password); + g_free (quoted_user); + g_free (quoted_password); + } + + mm_base_modem_at_command_full (ctx->modem, + ctx->primary, + command, + 60, + FALSE, + FALSE, /* raw */ + NULL, /* cancellable */ + (GAsyncReadyCallback)authenticate_ready, + ctx); + g_free (command); +} + +static void +dial_3gpp (MMBroadbandBearer *self, + MMBaseModem *modem, + MMAtSerialPort *primary, + guint cid, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + Dial3gppContext *ctx; + + g_assert (primary != NULL); + + ctx = g_slice_new0 (Dial3gppContext); + ctx->self = g_object_ref (self); + ctx->modem = g_object_ref (modem); + ctx->primary = g_object_ref (primary); + ctx->cid = cid; + ctx->result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + dial_3gpp); + ctx->cancellable = g_object_ref (cancellable); + + /* We need a net data port */ + ctx->data = mm_base_modem_get_best_data_port (modem, MM_PORT_TYPE_NET); + if (!ctx->data) { + g_simple_async_result_set_error ( + ctx->result, + MM_CORE_ERROR, + MM_CORE_ERROR_NOT_FOUND, + "No valid data port found to launch connection"); + dial_3gpp_context_complete_and_free (ctx); + return; + } + + authenticate (ctx); +} + +/*****************************************************************************/ + +void +mm_broadband_bearer_icera_report_connection_status (MMBroadbandBearerIcera *self, + MMBroadbandBearerIceraConnectionStatus status) +{ + if (self->priv->connect_pending) + report_connect_status (self, status); + + if (self->priv->disconnect_pending) + report_disconnect_status (self, status); +} + +/*****************************************************************************/ + +MMBearer * +mm_broadband_bearer_icera_new_finish (GAsyncResult *res, + GError **error) +{ + GObject *source; + GObject *bearer; + + source = g_async_result_get_source_object (res); + bearer = g_async_initable_new_finish (G_ASYNC_INITABLE (source), res, error); + g_object_unref (source); + + if (!bearer) + return NULL; + + /* Only export valid bearers */ + mm_bearer_export (MM_BEARER (bearer)); + + return MM_BEARER (bearer); +} + +void +mm_broadband_bearer_icera_new (MMBroadbandModem *modem, + MMBearerIpMethod ip_method, + MMBearerProperties *config, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_async_initable_new_async ( + MM_TYPE_BROADBAND_BEARER_ICERA, + G_PRIORITY_DEFAULT, + cancellable, + callback, + user_data, + MM_BEARER_MODEM, modem, + MM_BEARER_CONFIG, config, + MM_BROADBAND_BEARER_ICERA_DEFAULT_IP_METHOD, ip_method, + NULL); +} + +static void +set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MMBroadbandBearerIcera *self = MM_BROADBAND_BEARER_ICERA (object); + + switch (prop_id) { + case PROP_DEFAULT_IP_METHOD: + self->priv->default_ip_method = g_value_get_enum (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MMBroadbandBearerIcera *self = MM_BROADBAND_BEARER_ICERA (object); + + switch (prop_id) { + case PROP_DEFAULT_IP_METHOD: + g_value_set_enum (value, self->priv->default_ip_method); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +mm_broadband_bearer_icera_init (MMBroadbandBearerIcera *self) +{ + /* Initialize private data */ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), + MM_TYPE_BROADBAND_BEARER_ICERA, + MMBroadbandBearerIceraPrivate); + + /* Defaults */ + self->priv->default_ip_method = MM_BEARER_IP_METHOD_STATIC; +} + +static void +mm_broadband_bearer_icera_class_init (MMBroadbandBearerIceraClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + MMBroadbandBearerClass *broadband_bearer_class = MM_BROADBAND_BEARER_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (MMBroadbandBearerIceraPrivate)); + + object_class->get_property = get_property; + object_class->set_property = set_property; + broadband_bearer_class->dial_3gpp = dial_3gpp; + broadband_bearer_class->dial_3gpp_finish = dial_3gpp_finish; + broadband_bearer_class->get_ip_config_3gpp = get_ip_config_3gpp; + broadband_bearer_class->get_ip_config_3gpp_finish = get_ip_config_3gpp_finish; + broadband_bearer_class->disconnect_3gpp = disconnect_3gpp; + broadband_bearer_class->disconnect_3gpp_finish = disconnect_3gpp_finish; + + properties[PROP_DEFAULT_IP_METHOD] = + g_param_spec_enum (MM_BROADBAND_BEARER_ICERA_DEFAULT_IP_METHOD, + "Default IP method", + "Default IP Method (static or DHCP) to use.", + MM_TYPE_BEARER_IP_METHOD, + MM_BEARER_IP_METHOD_STATIC, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_DEFAULT_IP_METHOD, properties[PROP_DEFAULT_IP_METHOD]); +} diff --git a/plugins/icera/mm-broadband-bearer-icera.h b/plugins/icera/mm-broadband-bearer-icera.h new file mode 100644 index 0000000..4edd189 --- /dev/null +++ b/plugins/icera/mm-broadband-bearer-icera.h @@ -0,0 +1,74 @@ +/* -*- 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: + * + * Author: Nathan Williams <njw@google.com> + * + * Copyright (C) 2012 Google, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#ifndef MM_BROADBAND_BEARER_ICERA_H +#define MM_BROADBAND_BEARER_ICERA_H + +#include <glib.h> +#include <glib-object.h> + +#define _LIBMM_INSIDE_MM +#include <libmm-glib.h> + +#include "mm-broadband-bearer.h" + +#define MM_TYPE_BROADBAND_BEARER_ICERA (mm_broadband_bearer_icera_get_type ()) +#define MM_BROADBAND_BEARER_ICERA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_BROADBAND_BEARER_ICERA, MMBroadbandBearerIcera)) +#define MM_BROADBAND_BEARER_ICERA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_BROADBAND_BEARER_ICERA, MMBroadbandBearerIceraClass)) +#define MM_IS_BROADBAND_BEARER_ICERA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_BROADBAND_BEARER_ICERA)) +#define MM_IS_BROADBAND_BEARER_ICERA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_BROADBAND_BEARER_ICERA)) +#define MM_BROADBAND_BEARER_ICERA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_BROADBAND_BEARER_ICERA, MMBroadbandBearerIceraClass)) + +#define MM_BROADBAND_BEARER_ICERA_DEFAULT_IP_METHOD "broadband-bearer-icera-default-ip-method" + +typedef enum { + MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_UNKNOWN, + MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTED, + MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTION_FAILED, + MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_DISCONNECTED +} MMBroadbandBearerIceraConnectionStatus; + +typedef struct _MMBroadbandBearerIcera MMBroadbandBearerIcera; +typedef struct _MMBroadbandBearerIceraClass MMBroadbandBearerIceraClass; +typedef struct _MMBroadbandBearerIceraPrivate MMBroadbandBearerIceraPrivate; + +struct _MMBroadbandBearerIcera { + MMBroadbandBearer parent; + MMBroadbandBearerIceraPrivate *priv; +}; + +struct _MMBroadbandBearerIceraClass { + MMBroadbandBearerClass parent; +}; + +GType mm_broadband_bearer_icera_get_type (void); + +/* Default bearer creation implementation */ +void mm_broadband_bearer_icera_new (MMBroadbandModem *modem, + MMBearerIpMethod ip_method, + MMBearerProperties *config, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +MMBearer *mm_broadband_bearer_icera_new_finish (GAsyncResult *res, + GError **error); + +void mm_broadband_bearer_icera_report_connection_status (MMBroadbandBearerIcera *self, + MMBroadbandBearerIceraConnectionStatus status); + +#endif /* MM_BROADBAND_BEARER_ICERA_H */ diff --git a/plugins/icera/mm-broadband-modem-icera.c b/plugins/icera/mm-broadband-modem-icera.c new file mode 100644 index 0000000..84e998b --- /dev/null +++ b/plugins/icera/mm-broadband-modem-icera.c @@ -0,0 +1,1933 @@ +/* -*- 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) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 - 2012 Red Hat, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#include <config.h> + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <ctype.h> + +#include "ModemManager.h" +#include "mm-serial-parsers.h" +#include "mm-log.h" +#include "mm-modem-helpers.h" +#include "mm-errors-types.h" +#include "mm-iface-modem.h" +#include "mm-iface-modem-3gpp.h" +#include "mm-iface-modem-time.h" +#include "mm-base-modem-at.h" +#include "mm-bearer-list.h" +#include "mm-broadband-bearer-icera.h" +#include "mm-broadband-modem-icera.h" + +static void iface_modem_init (MMIfaceModem *iface); +static void iface_modem_3gpp_init (MMIfaceModem3gpp *iface); +static void iface_modem_time_init (MMIfaceModemTime *iface); + +static MMIfaceModem *iface_modem_parent; +static MMIfaceModem3gpp *iface_modem_3gpp_parent; + +G_DEFINE_TYPE_EXTENDED (MMBroadbandModemIcera, mm_broadband_modem_icera, 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_TIME, iface_modem_time_init)) + +enum { + PROP_0, + PROP_DEFAULT_IP_METHOD, + PROP_LAST +}; + +static GParamSpec *properties[PROP_LAST]; + +struct _MMBroadbandModemIceraPrivate { + MMBearerIpMethod default_ip_method; + + GRegex *nwstate_regex; + GRegex *pacsp_regex; + GRegex *ipdpact_regex; + + /* Cache of the most recent value seen by the unsolicited message handler */ + MMModemAccessTechnology last_act; +}; + +/*****************************************************************************/ +/* Load supported modes (Modem interface) */ + +static GArray * +load_supported_modes_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return NULL; + + return g_array_ref (g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (res))); +} + +static void +parent_load_supported_modes_ready (MMIfaceModem *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + GArray *all; + GArray *combinations; + GArray *filtered; + MMModemModeCombination mode; + + all = iface_modem_parent->load_supported_modes_finish (self, res, &error); + if (!all) { + g_simple_async_result_take_error (simple, error); + g_simple_async_result_complete (simple); + g_object_unref (simple); + return; + } + + /* Build list of combinations */ + combinations = g_array_sized_new (FALSE, FALSE, sizeof (MMModemModeCombination), 5); + + /* 2G only */ + mode.allowed = MM_MODEM_MODE_2G; + mode.preferred = MM_MODEM_MODE_NONE; + g_array_append_val (combinations, mode); + /* 3G only */ + mode.allowed = MM_MODEM_MODE_3G; + mode.preferred = MM_MODEM_MODE_NONE; + g_array_append_val (combinations, mode); + /* 2G and 3G */ + mode.allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + mode.preferred = MM_MODEM_MODE_NONE; + g_array_append_val (combinations, mode); + /* 2G and 3G, 2G preferred */ + mode.allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + mode.preferred = MM_MODEM_MODE_2G; + g_array_append_val (combinations, mode); + /* 2G and 3G, 3G preferred */ + mode.allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + mode.preferred = MM_MODEM_MODE_3G; + g_array_append_val (combinations, mode); + + /* Filter out those unsupported modes */ + filtered = mm_filter_supported_modes (all, combinations); + g_array_unref (all); + g_array_unref (combinations); + + g_simple_async_result_set_op_res_gpointer (simple, filtered, (GDestroyNotify) g_array_unref); + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +load_supported_modes (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + /* Run parent's loading */ + iface_modem_parent->load_supported_modes ( + MM_IFACE_MODEM (self), + (GAsyncReadyCallback)parent_load_supported_modes_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + load_supported_modes)); +} + +/*****************************************************************************/ +/* Load initial allowed/preferred modes (Modem interface) */ + +static gboolean +modem_load_current_modes_finish (MMIfaceModem *self, + GAsyncResult *res, + MMModemMode *allowed, + MMModemMode *preferred, + GError **error) +{ + const gchar *response; + const gchar *str; + gint mode, domain; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error); + if (!response) + return FALSE; + + str = mm_strip_tag (response, "%IPSYS:"); + + if (!sscanf (str, "%d,%d", &mode, &domain)) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Couldn't parse %%IPSYS response: '%s'", + response); + return FALSE; + } + + switch (mode) { + case 0: + *allowed = MM_MODEM_MODE_2G; + *preferred = MM_MODEM_MODE_NONE; + return TRUE; + case 1: + *allowed = MM_MODEM_MODE_3G; + *preferred = MM_MODEM_MODE_NONE; + return TRUE; + case 2: + *allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + *preferred = MM_MODEM_MODE_2G; + return TRUE; + case 3: + *allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + *preferred = MM_MODEM_MODE_3G; + return TRUE; + case 5: /* any */ + *allowed = (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G); + *preferred = MM_MODEM_MODE_NONE; + return TRUE; + default: + break; + } + + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Couldn't parse unexpected %%IPSYS response: '%s'", + response); + return FALSE; +} + +static void +modem_load_current_modes (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command (MM_BASE_MODEM (self), + "%IPSYS?", + 3, + FALSE, + callback, + user_data); +} + +/*****************************************************************************/ +/* Set allowed modes (Modem interface) */ + +static gboolean +modem_set_current_modes_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static void +allowed_mode_update_ready (MMBaseModem *self, + GAsyncResult *res, + GSimpleAsyncResult *operation_result) +{ + GError *error = NULL; + + mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); + if (error) + /* Let the error be critical. */ + g_simple_async_result_take_error (operation_result, error); + else + g_simple_async_result_set_op_res_gboolean (operation_result, TRUE); + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); +} + +static void +modem_set_current_modes (MMIfaceModem *self, + MMModemMode allowed, + MMModemMode preferred, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + gchar *command; + gint icera_mode = -1; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_set_current_modes); + + /* + * The core has checked the following: + * - that 'allowed' are a subset of the 'supported' modes + * - that 'preferred' is one mode, and a subset of 'allowed' + */ + if (allowed == MM_MODEM_MODE_2G) + icera_mode = 0; + else if (allowed == MM_MODEM_MODE_3G) + icera_mode = 1; + else if (allowed == (MM_MODEM_MODE_2G | MM_MODEM_MODE_3G)) { + if (preferred == MM_MODEM_MODE_2G) + icera_mode = 2; + else if (preferred == MM_MODEM_MODE_3G) + icera_mode = 3; + else /* none preferred, so AUTO */ + icera_mode = 5; + } else if (allowed == MM_MODEM_MODE_ANY && preferred == MM_MODEM_MODE_NONE) + icera_mode = 5; + + if (icera_mode < 0) { + gchar *allowed_str; + gchar *preferred_str; + + allowed_str = mm_modem_mode_build_string_from_mask (allowed); + preferred_str = mm_modem_mode_build_string_from_mask (preferred); + g_simple_async_result_set_error (result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Requested mode (allowed: '%s', preferred: '%s') not " + "supported by the modem.", + allowed_str, + preferred_str); + g_free (allowed_str); + g_free (preferred_str); + + g_simple_async_result_complete_in_idle (result); + g_object_unref (result); + return; + } + + command = g_strdup_printf ("%%IPSYS=%d", icera_mode); + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + command, + 3, + FALSE, + (GAsyncReadyCallback)allowed_mode_update_ready, + result); + g_free (command); +} + +/*****************************************************************************/ +/* Icera-specific unsolicited events handling */ + +typedef struct { + guint cid; + MMBroadbandBearerIceraConnectionStatus status; +} BearerListReportStatusForeachContext; + +static void +bearer_list_report_status_foreach (MMBearer *bearer, + BearerListReportStatusForeachContext *ctx) +{ + if (mm_broadband_bearer_get_3gpp_cid (MM_BROADBAND_BEARER (bearer)) != ctx->cid) + return; + + if (!MM_IS_BROADBAND_BEARER_ICERA (bearer)) + return; + + mm_broadband_bearer_icera_report_connection_status (MM_BROADBAND_BEARER_ICERA (bearer), + ctx->status); +} + +static void +ipdpact_received (MMAtSerialPort *port, + GMatchInfo *match_info, + MMBroadbandModemIcera *self) +{ + MMBearerList *list = NULL; + BearerListReportStatusForeachContext ctx; + guint cid; + guint status; + + /* Ensure we got proper parsed values */ + if (!mm_get_uint_from_match_info (match_info, 1, &cid) || + !mm_get_uint_from_match_info (match_info, 2, &status)) + return; + + /* Setup context */ + ctx.cid = cid; + ctx.status = MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_UNKNOWN; + + switch (status) { + case 0: + ctx.status = MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_DISCONNECTED; + break; + case 1: + ctx.status = MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTED; + break; + case 2: + /* activating */ + break; + case 3: + ctx.status = MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_CONNECTION_FAILED; + break; + default: + mm_warn ("Unknown Icera connect status %d", status); + break; + } + + /* If unknown status, don't try to report anything */ + if (ctx.status == MM_BROADBAND_BEARER_ICERA_CONNECTION_STATUS_UNKNOWN) + return; + + /* If empty bearer list, nothing else to do */ + g_object_get (self, + MM_IFACE_MODEM_BEARER_LIST, &list, + NULL); + if (!list) + return; + + /* Will report status only in the bearer with the specific CID */ + mm_bearer_list_foreach (list, + (MMBearerListForeachFunc)bearer_list_report_status_foreach, + &ctx); + g_object_unref (list); +} + +static MMModemAccessTechnology +nwstate_to_act (const gchar *str) +{ + /* small 'g' means CS, big 'G' means PS */ + if (!strcmp (str, "2g")) + return MM_MODEM_ACCESS_TECHNOLOGY_GSM; + else if (!strcmp (str, "2G-GPRS")) + return MM_MODEM_ACCESS_TECHNOLOGY_GPRS; + else if (!strcmp (str, "2G-EDGE")) + return MM_MODEM_ACCESS_TECHNOLOGY_EDGE; + else if (!strcmp (str, "3G")) + return MM_MODEM_ACCESS_TECHNOLOGY_UMTS; + else if (!strcmp (str, "3g")) + return MM_MODEM_ACCESS_TECHNOLOGY_UMTS; + else if (!strcmp (str, "R99")) + return MM_MODEM_ACCESS_TECHNOLOGY_UMTS; + else if (!strcmp (str, "3G-HSDPA") || !strcmp (str, "HSDPA")) + return MM_MODEM_ACCESS_TECHNOLOGY_HSDPA; + else if (!strcmp (str, "3G-HSUPA") || !strcmp (str, "HSUPA")) + return MM_MODEM_ACCESS_TECHNOLOGY_HSUPA; + else if (!strcmp (str, "3G-HSDPA-HSUPA") || !strcmp (str, "HSDPA-HSUPA")) + return MM_MODEM_ACCESS_TECHNOLOGY_HSPA; + else if (!strcmp (str, "3G-HSDPA-HSUPA-HSPA+") || !strcmp (str, "HSDPA-HSUPA-HSPA+")) + return MM_MODEM_ACCESS_TECHNOLOGY_HSPA_PLUS; + + return MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN; +} + +static void +nwstate_changed (MMAtSerialPort *port, + GMatchInfo *info, + MMBroadbandModemIcera *self) +{ + gchar *str; + + /* + * %NWSTATE: <rssi>,<mccmnc>,<tech>,<connection state>,<regulation> + * + * <connection state> shows the actual access technology in-use when a + * PS connection is active. + */ + + /* Process signal quality... */ + str = g_match_info_fetch (info, 1); + if (str) { + gint rssi; + + rssi = atoi (str); + rssi = CLAMP (rssi, 0, 5) * 100 / 5; + g_free (str); + + mm_iface_modem_update_signal_quality (MM_IFACE_MODEM (self), + (guint)rssi); + } + + /* Process access technology... */ + str = g_match_info_fetch (info, 4); + if (!str || (strcmp (str, "-") == 0)) { + g_free (str); + str = g_match_info_fetch (info, 3); + } + if (str) { + MMModemAccessTechnology act; + + act = nwstate_to_act (str); + g_free (str); + + /* Cache last received value, needed for explicit access technology + * query handling */ + self->priv->last_act = act; + mm_iface_modem_update_access_technologies (MM_IFACE_MODEM (self), + act, + MM_MODEM_ACCESS_TECHNOLOGY_ANY); + } +} + +static void +set_unsolicited_events_handlers (MMBroadbandModemIcera *self, + gboolean enable) +{ + MMAtSerialPort *ports[2]; + guint i; + + ports[0] = mm_base_modem_peek_port_primary (MM_BASE_MODEM (self)); + ports[1] = mm_base_modem_peek_port_secondary (MM_BASE_MODEM (self)); + + /* Enable unsolicited events in given port */ + for (i = 0; i < 2; i++) { + if (!ports[i]) + continue; + + /* Access technology related */ + mm_at_serial_port_add_unsolicited_msg_handler ( + ports[i], + self->priv->nwstate_regex, + enable ? (MMAtSerialUnsolicitedMsgFn)nwstate_changed : NULL, + enable ? self : NULL, + NULL); + + /* Connection status related */ + mm_at_serial_port_add_unsolicited_msg_handler ( + ports[i], + self->priv->ipdpact_regex, + enable ? (MMAtSerialUnsolicitedMsgFn)ipdpact_received : NULL, + enable ? self : NULL, + NULL); + + /* Always to ignore */ + if (!enable) { + mm_at_serial_port_add_unsolicited_msg_handler ( + ports[i], + self->priv->pacsp_regex, + NULL, + NULL, + NULL); + } + } +} + +/*****************************************************************************/ +/* Load access technologies (Modem interface) */ + +static gboolean +modem_load_access_technologies_finish (MMIfaceModem *self, + GAsyncResult *res, + MMModemAccessTechnology *access_technologies, + guint *mask, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return FALSE; + + *access_technologies = ((MMModemAccessTechnology) GPOINTER_TO_UINT ( + g_simple_async_result_get_op_res_gpointer ( + G_SIMPLE_ASYNC_RESULT (res)))); + *mask = MM_MODEM_ACCESS_TECHNOLOGY_ANY; + return TRUE; +} + +static void +nwstate_query_ready (MMBroadbandModemIcera *self, + GAsyncResult *res, + GSimpleAsyncResult *operation_result) +{ + GError *error = NULL; + + mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); + if (error) { + mm_dbg ("Couldn't query access technology: '%s'", error->message); + g_simple_async_result_take_error (operation_result, error); + } else { + /* + * The unsolicited message handler will already have run and + * removed the NWSTATE response, so we use the result from there. + */ + g_simple_async_result_set_op_res_gpointer (operation_result, + GUINT_TO_POINTER (self->priv->last_act), + NULL); + } + + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); +} + +static void +modem_load_access_technologies (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_load_access_technologies); + + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + "%NWSTATE", + 3, + FALSE, + (GAsyncReadyCallback)nwstate_query_ready, + result); +} + +/*****************************************************************************/ +/* Setup/Cleanup unsolicited events (3GPP interface) */ + +static gboolean +modem_3gpp_setup_cleanup_unsolicited_events_finish (MMIfaceModem3gpp *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static void +parent_setup_unsolicited_events_ready (MMIfaceModem3gpp *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!iface_modem_3gpp_parent->setup_unsolicited_events_finish (self, res, &error)) + g_simple_async_result_take_error (simple, error); + else { + /* Our own setup now */ + set_unsolicited_events_handlers (MM_BROADBAND_MODEM_ICERA (self), TRUE); + g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res), TRUE); + } + + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +modem_3gpp_setup_unsolicited_events (MMIfaceModem3gpp *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + /* Chain up parent's setup */ + iface_modem_3gpp_parent->setup_unsolicited_events ( + self, + (GAsyncReadyCallback)parent_setup_unsolicited_events_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_3gpp_setup_unsolicited_events)); +} + +static void +parent_cleanup_unsolicited_events_ready (MMIfaceModem3gpp *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!iface_modem_3gpp_parent->cleanup_unsolicited_events_finish (self, res, &error)) + g_simple_async_result_take_error (simple, error); + else + g_simple_async_result_set_op_res_gboolean (G_SIMPLE_ASYNC_RESULT (res), TRUE); + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +modem_3gpp_cleanup_unsolicited_events (MMIfaceModem3gpp *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_3gpp_cleanup_unsolicited_events); + + /* Our own cleanup first */ + set_unsolicited_events_handlers (MM_BROADBAND_MODEM_ICERA (self), FALSE); + + /* And now chain up parent's cleanup */ + iface_modem_3gpp_parent->cleanup_unsolicited_events ( + self, + (GAsyncReadyCallback)parent_cleanup_unsolicited_events_ready, + result); +} + +/*****************************************************************************/ +/* Enable/disable unsolicited events (3GPP interface) */ + +static gboolean +modem_3gpp_enable_disable_unsolicited_events_finish (MMIfaceModem3gpp *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static void +own_enable_unsolicited_events_ready (MMIfaceModem3gpp *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error)) + g_simple_async_result_take_error (simple, error); + else + g_simple_async_result_set_op_res_gboolean (simple, TRUE); + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +parent_enable_unsolicited_events_ready (MMIfaceModem3gpp *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!iface_modem_3gpp_parent->enable_unsolicited_events_finish (self, res, &error)) { + g_simple_async_result_take_error (simple, error); + g_simple_async_result_complete (simple); + g_object_unref (simple); + return; + } + + /* Our own enable now */ + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + "%NWSTATE=1", + 3, + FALSE, + (GAsyncReadyCallback)own_enable_unsolicited_events_ready, + simple); +} + +static void +modem_3gpp_enable_unsolicited_events (MMIfaceModem3gpp *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + /* Chain up parent's enable */ + iface_modem_3gpp_parent->enable_unsolicited_events ( + self, + (GAsyncReadyCallback)parent_enable_unsolicited_events_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_3gpp_enable_unsolicited_events)); +} + +static void +parent_disable_unsolicited_events_ready (MMIfaceModem3gpp *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!iface_modem_3gpp_parent->disable_unsolicited_events_finish (self, res, &error)) + g_simple_async_result_take_error (simple, error); + else + g_simple_async_result_set_op_res_gboolean (simple, TRUE); + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +own_disable_unsolicited_events_ready (MMIfaceModem3gpp *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error)) { + g_simple_async_result_take_error (simple, error); + g_simple_async_result_complete (simple); + g_object_unref (simple); + return; + } + + /* Next, chain up parent's disable */ + iface_modem_3gpp_parent->disable_unsolicited_events ( + MM_IFACE_MODEM_3GPP (self), + (GAsyncReadyCallback)parent_disable_unsolicited_events_ready, + simple); +} + +static void +modem_3gpp_disable_unsolicited_events (MMIfaceModem3gpp *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + "%NWSTATE=0", + 3, + FALSE, + (GAsyncReadyCallback)own_disable_unsolicited_events_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_3gpp_disable_unsolicited_events)); +} + +/*****************************************************************************/ +/* Create bearer (Modem interface) */ + +static MMBearer * +modem_create_bearer_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return NULL; + + return MM_BEARER (g_object_ref ( + g_simple_async_result_get_op_res_gpointer ( + G_SIMPLE_ASYNC_RESULT (res)))); +} + +static void +broadband_bearer_icera_new_ready (GObject *source, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + MMBearer *bearer = NULL; + GError *error = NULL; + + bearer = mm_broadband_bearer_icera_new_finish (res, &error); + if (!bearer) + g_simple_async_result_take_error (simple, error); + else + g_simple_async_result_set_op_res_gpointer (simple, + bearer, + (GDestroyNotify)g_object_unref); + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +broadband_bearer_new_ready (GObject *source, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + MMBearer *bearer = NULL; + GError *error = NULL; + + bearer = mm_broadband_bearer_new_finish (res, &error); + if (!bearer) + g_simple_async_result_take_error (simple, error); + else + g_simple_async_result_set_op_res_gpointer (simple, + bearer, + (GDestroyNotify)g_object_unref); + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +modem_create_bearer (MMIfaceModem *self, + MMBearerProperties *properties, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_create_bearer); + + /* If we get a NET port, create Icera bearer */ + if (mm_base_modem_peek_best_data_port (MM_BASE_MODEM (self), MM_PORT_TYPE_NET)) { + mm_broadband_bearer_icera_new ( + MM_BROADBAND_MODEM (self), + MM_BROADBAND_MODEM_ICERA (self)->priv->default_ip_method, + properties, + NULL, /* cancellable */ + (GAsyncReadyCallback)broadband_bearer_icera_new_ready, + result); + return; + } + + /* Otherwise, plain generic broadband bearer */ + mm_broadband_bearer_new ( + MM_BROADBAND_MODEM (self), + properties, + NULL, /* cancellable */ + (GAsyncReadyCallback)broadband_bearer_new_ready, + result); +} + +/*****************************************************************************/ +/* Modem power up (Modem interface) */ + +static gboolean +modem_power_up_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static void +cfun_enable_ready (MMBaseModem *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + + if (!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error)) { + /* Ignore all errors except NOT_ALLOWED, which means Airplane Mode */ + if (g_error_matches (error, + MM_MOBILE_EQUIPMENT_ERROR, + MM_MOBILE_EQUIPMENT_ERROR_NOT_ALLOWED)) + g_simple_async_result_take_error (simple, error); + else + g_error_free (error); + } + + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static void +modem_power_up (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_power_up); + + mm_base_modem_at_command (MM_BASE_MODEM (self), + "+CFUN=1", + 10, + FALSE, + (GAsyncReadyCallback)cfun_enable_ready, + result); +} + +/*****************************************************************************/ +/* Modem power down (Modem interface) */ + +static gboolean +modem_power_down_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + return !!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error); +} + +static void +modem_power_down (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + /* Use AT+CFUN=4 for power down. It will stop the RF (IMSI detach), and + * keeps access to the SIM */ + mm_base_modem_at_command (MM_BASE_MODEM (self), + "+CFUN=4", + /* The modem usually completes +CFUN=4 within 1-2 seconds, + * but sometimes takes a ridiculously long time (~30-35 seconds). + * It's better to have a long timeout here than to have the + * modem not responding to subsequent AT commands until +CFUN=4 + * completes. */ + 40, + FALSE, + callback, + user_data); +} + +/*****************************************************************************/ +/* Reset (Modem interface) */ + +static gboolean +modem_reset_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + return !!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error); +} + +static void +modem_reset (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command (MM_BASE_MODEM (self), + "%IRESET", + 3, + FALSE, + callback, + user_data); +} + +/*****************************************************************************/ +/* Load unlock retries (Modem interface) */ + +static MMUnlockRetries * +modem_load_unlock_retries_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return NULL; + return (MMUnlockRetries *) g_object_ref (g_simple_async_result_get_op_res_gpointer ( + G_SIMPLE_ASYNC_RESULT (res))); +} + +static void +load_unlock_retries_ready (MMBaseModem *self, + GAsyncResult *res, + GSimpleAsyncResult *operation_result) +{ + const gchar *response; + GError *error = NULL; + int pin1, puk1, pin2, puk2; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); + if (!response) { + mm_dbg ("Couldn't query unlock retries: '%s'", error->message); + g_simple_async_result_take_error (operation_result, error); + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); + return; + } + + response = mm_strip_tag (response, "%PINNUM:"); + if (sscanf (response, " %d, %d, %d, %d", &pin1, &puk1, &pin2, &puk2) == 4) { + MMUnlockRetries *retries; + retries = mm_unlock_retries_new (); + mm_unlock_retries_set (retries, MM_MODEM_LOCK_SIM_PIN, pin1); + mm_unlock_retries_set (retries, MM_MODEM_LOCK_SIM_PUK, puk1); + mm_unlock_retries_set (retries, MM_MODEM_LOCK_SIM_PIN2, pin2); + mm_unlock_retries_set (retries, MM_MODEM_LOCK_SIM_PUK2, puk2); + g_simple_async_result_set_op_res_gpointer (operation_result, + retries, + (GDestroyNotify)g_object_unref); + } else { + g_simple_async_result_set_error (operation_result, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Invalid unlock retries response: '%s'", + response); + } + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); +} + +static void +modem_load_unlock_retries (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + "%PINNUM?", + 3, + FALSE, + (GAsyncReadyCallback)load_unlock_retries_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_load_unlock_retries)); +} + +/*****************************************************************************/ +/* Generic band handling utilities */ + +typedef struct { + MMModemBand band; + char *name; + gboolean enabled; +} Band; + +static void +band_free (Band *b) +{ + g_free (b->name); + g_free (b); +} + +static const Band modem_bands[] = { + /* Sort 3G first since it's preferred */ + { MM_MODEM_BAND_U2100, "FDD_BAND_I", FALSE }, + { MM_MODEM_BAND_U1900, "FDD_BAND_II", FALSE }, + { MM_MODEM_BAND_U1800, "FDD_BAND_III", FALSE }, + { MM_MODEM_BAND_U17IV, "FDD_BAND_IV", FALSE }, + { MM_MODEM_BAND_U800, "FDD_BAND_VI", FALSE }, + { MM_MODEM_BAND_U850, "FDD_BAND_V", FALSE }, + { MM_MODEM_BAND_U900, "FDD_BAND_VIII", FALSE }, + /* 2G second */ + { MM_MODEM_BAND_G850, "G850", FALSE }, + { MM_MODEM_BAND_DCS, "DCS", FALSE }, + { MM_MODEM_BAND_EGSM, "EGSM", FALSE }, + { MM_MODEM_BAND_PCS, "PCS", FALSE }, + /* And ANY last since it's most inclusive */ + { MM_MODEM_BAND_ANY, "ANY", FALSE }, +}; + +static const guint modem_band_any_bit = 1 << (G_N_ELEMENTS (modem_bands) - 1); + +static MMModemBand +icera_band_to_mm (const char *icera) +{ + int i; + + for (i = 0 ; i < G_N_ELEMENTS (modem_bands); i++) { + if (g_strcmp0 (icera, modem_bands[i].name) == 0) + return modem_bands[i].band; + } + return MM_MODEM_BAND_UNKNOWN; +} + +static GSList * +parse_bands (const gchar *response, guint32 *out_len) +{ + GRegex *r; + GMatchInfo *info; + GSList *bands = NULL; + + g_return_val_if_fail (out_len != NULL, NULL); + + /* + * Response is a number of lines of the form: + * "EGSM": 0 + * "FDD_BAND_I": 1 + * ... + * with 1 and 0 indicating whether the particular band is enabled or not. + */ + r = g_regex_new ("^\"(\\w+)\": (\\d)", + G_REGEX_MULTILINE, G_REGEX_MATCH_NEWLINE_ANY, + NULL); + g_assert (r != NULL); + + g_regex_match (r, response, 0, &info); + while (g_match_info_matches (info)) { + gchar *name, *enabled; + Band *b; + MMModemBand band; + + name = g_match_info_fetch (info, 1); + enabled = g_match_info_fetch (info, 2); + band = icera_band_to_mm (name); + if (band != MM_MODEM_BAND_UNKNOWN) { + b = g_malloc0 (sizeof (Band)); + b->band = band; + b->name = g_strdup (name); + b->enabled = (enabled[0] == '1' ? TRUE : FALSE); + bands = g_slist_append (bands, b); + *out_len = *out_len + 1; + } + g_free (name); + g_free (enabled); + g_match_info_next (info, NULL); + } + g_match_info_free (info); + g_regex_unref (r); + + return bands; +} + +/*****************************************************************************/ +/* Load supported bands (Modem interface) */ + +typedef struct { + MMBaseModemAtCommand *cmds; + GSList *check_bands; + GSList *enabled_bands; + guint32 idx; +} SupportedBandsContext; + +static void +supported_bands_context_free (SupportedBandsContext *ctx) +{ + guint i; + + for (i = 0; ctx->cmds[i].command; i++) + g_free (ctx->cmds[i].command); + g_free (ctx->cmds); + g_slist_free_full (ctx->check_bands, (GDestroyNotify) band_free); + g_slist_free_full (ctx->enabled_bands, (GDestroyNotify) band_free); + g_free (ctx); +} + +static GArray * +modem_load_supported_bands_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return NULL; + + return (GArray *) g_array_ref (g_simple_async_result_get_op_res_gpointer ( + G_SIMPLE_ASYNC_RESULT (res))); +} + +static void +load_supported_bands_ready (MMBaseModem *self, + GAsyncResult *res, + GSimpleAsyncResult *simple) +{ + GError *error = NULL; + SupportedBandsContext *ctx = NULL; + GArray *bands; + GSList *iter; + + mm_base_modem_at_sequence_finish (self, res, (gpointer) &ctx, &error); + if (error) + g_simple_async_result_take_error (simple, error); + else { + bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), ctx->idx); + + /* Add already enabled bands */ + for (iter = ctx->enabled_bands; iter; iter = g_slist_next (iter)) { + Band *b = iter->data; + + g_array_prepend_val (bands, b->band); + } + + /* Add any checked bands that are supported */ + for (iter = ctx->check_bands; iter; iter = g_slist_next (iter)) { + Band *b = iter->data; + + /* 'enabled' here really means supported/unsupported */ + if (b->enabled) + g_array_prepend_val (bands, b->band); + } + + g_simple_async_result_set_op_res_gpointer (simple, + bands, + (GDestroyNotify) g_array_unref); + } + g_simple_async_result_complete (simple); + g_object_unref (simple); +} + +static gboolean +load_supported_bands_response_processor (MMBaseModem *self, + gpointer context, + const gchar *command, + const gchar *response, + gboolean last_command, + const GError *error, + GVariant **result, + GError **result_error) +{ + SupportedBandsContext *ctx = context; + Band *b = g_slist_nth_data (ctx->check_bands, ctx->idx++); + + /* If there was no error setting the band, that band is supported. We + * abuse the 'enabled' item to mean supported/unsupported. + */ + b->enabled = !error; + + /* Continue to next band */ + return FALSE; +} + +static void +load_supported_bands_get_current_bands_ready (MMIfaceModem *self, + GAsyncResult *res, + GSimpleAsyncResult *operation_result) +{ + SupportedBandsContext *ctx; + const gchar *response; + GError *error = NULL; + GSList *iter, *new; + guint32 len = 0, i = 0; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); + if (!response) { + mm_dbg ("Couldn't query current bands: '%s'", error->message); + g_simple_async_result_take_error (operation_result, error); + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); + return; + } + + ctx = g_new0 (SupportedBandsContext, 1); + + /* For each reported band, build up an AT command to set that band + * to its current enabled/disabled state. + */ + iter = ctx->check_bands = parse_bands (response, &len); + ctx->cmds = g_new0 (MMBaseModemAtCommand, len + 1); + + while (iter) { + Band *b = iter->data; + + if (b->enabled || b->band == MM_MODEM_BAND_ANY) { + /* Move known-supported band to the enabled list */ + new = g_slist_next (iter); + ctx->check_bands = g_slist_remove_link (ctx->check_bands, iter); + ctx->enabled_bands = g_slist_prepend (ctx->enabled_bands, iter->data); + g_slist_free (iter); + iter = new; + } else { + /* Check support for disabled band */ + ctx->cmds[i].command = g_strdup_printf ("%%IPBM=\"%s\",0", b->name); + ctx->cmds[i].timeout = 10; + ctx->cmds[i].allow_cached = FALSE; + ctx->cmds[i].response_processor = load_supported_bands_response_processor; + i++; + iter = g_slist_next (iter); + } + } + + mm_base_modem_at_sequence (MM_BASE_MODEM (self), + ctx->cmds, + ctx, + (GDestroyNotify) supported_bands_context_free, + (GAsyncReadyCallback) load_supported_bands_ready, + operation_result); +} + +static void +modem_load_supported_bands (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + /* The modems report some bands as disabled that they don't actually + * support enabling. Thanks Icera! So we have to try setting each + * band to it's current enabled/disabled value, and the modem will + * return an error if it doesn't support that band at all. + */ + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + "%IPBM?", + 3, + FALSE, + (GAsyncReadyCallback)load_supported_bands_get_current_bands_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_load_supported_bands)); +} + +/*****************************************************************************/ +/* Load current bands (Modem interface) */ + +static GArray * +modem_load_current_bands_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + if (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error)) + return NULL; + + return (GArray *) g_array_ref (g_simple_async_result_get_op_res_gpointer ( + G_SIMPLE_ASYNC_RESULT (res))); +} + +static void +load_current_bands_ready (MMIfaceModem *self, + GAsyncResult *res, + GSimpleAsyncResult *operation_result) +{ + GArray *bands; + const gchar *response; + GError *error = NULL; + GSList *parsed, *iter; + guint32 len = 0; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error); + if (!response) { + mm_dbg ("Couldn't query current bands: '%s'", error->message); + g_simple_async_result_take_error (operation_result, error); + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); + } else { + /* Parse bands from Icera response into MM band numbers */ + parsed = parse_bands (response, &len); + bands = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), len); + for (iter = parsed; iter; iter = g_slist_next (iter)) { + Band *b = iter->data; + + if (b->enabled) + g_array_append_val (bands, b->band); + } + g_slist_free_full (parsed, (GDestroyNotify) band_free); + + g_simple_async_result_set_op_res_gpointer (operation_result, + bands, + (GDestroyNotify)g_array_unref); + g_simple_async_result_complete (operation_result); + g_object_unref (operation_result); + } +} + +static void +modem_load_current_bands (MMIfaceModem *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + "%IPBM?", + 3, + FALSE, + (GAsyncReadyCallback)load_current_bands_ready, + g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_load_current_bands)); +} + +/*****************************************************************************/ +/* Set current bands (Modem interface) */ + +typedef struct { + GSimpleAsyncResult *result; + guint bandbits; + guint enablebits; + guint disablebits; +} SetCurrentBandsContext; + +/* + * The modem's band-setting command (%IPBM=) enables or disables one + * band at a time, and one band must always be enabled. Here, we first + * get the set of enabled bands, compute the difference between that + * set and the requested set, enable any added bands, and finally + * disable any removed bands. + */ +static gboolean +modem_set_current_bands_finish (MMIfaceModem *self, + GAsyncResult *res, + GError **error) +{ + return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error); +} + +static void set_one_band (MMIfaceModem *self, SetCurrentBandsContext *ctx); + +static void +set_current_bands_context_complete_and_free (SetCurrentBandsContext *ctx) +{ + g_simple_async_result_complete (ctx->result); + g_object_unref (ctx->result); + g_slice_free (SetCurrentBandsContext, ctx); +} + +static void +set_current_bands_next (MMIfaceModem *self, + GAsyncResult *res, + SetCurrentBandsContext *ctx) +{ + GError *error = NULL; + + if (!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error)) { + mm_dbg ("Couldn't set current bands: '%s'", error->message); + g_simple_async_result_take_error (ctx->result, error); + set_current_bands_context_complete_and_free (ctx); + return; + } + + set_one_band (self, ctx); +} + +static void +set_one_band (MMIfaceModem *self, + SetCurrentBandsContext *ctx) +{ + guint enable, band; + gchar *command; + + /* Find the next band to enable or disable, always doing enables first */ + enable = 1; + band = ffs (ctx->enablebits); + if (band == 0) { + enable = 0; + band = ffs (ctx->disablebits); + } + if (band == 0) { + /* Both enabling and disabling are done */ + g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE); + set_current_bands_context_complete_and_free (ctx); + return; + } + + /* Note that ffs() returning 2 corresponds to 1 << 1, not 1 << 2 */ + band--; + mm_dbg("1. enablebits %x disablebits %x band %d enable %d", + ctx->enablebits, ctx->disablebits, band, enable); + + if (enable) + ctx->enablebits &= ~(1 << band); + else + ctx->disablebits &= ~(1 << band); + mm_dbg("2. enablebits %x disablebits %x", + ctx->enablebits, ctx->disablebits); + + command = g_strdup_printf ("%%IPBM=\"%s\",%d", + modem_bands[band].name, + enable); + mm_base_modem_at_command ( + MM_BASE_MODEM (self), + command, + 10, + FALSE, + (GAsyncReadyCallback)set_current_bands_next, + ctx); + g_free (command); +} + +static guint +band_array_to_bandbits (GArray *bands) +{ + MMModemBand band; + guint i, j, bandbits; + + bandbits = 0; + for (i = 0 ; i < bands->len ; i++) { + band = g_array_index (bands, MMModemBand, i); + for (j = 0 ; j < G_N_ELEMENTS (modem_bands) ; j++) { + if (modem_bands[j].band == band) { + bandbits |= 1 << j; + break; + } + } + g_assert (j < G_N_ELEMENTS (modem_bands)); + } + + return bandbits; +} + +static void +set_current_bands_got_current_bands (MMIfaceModem *self, + GAsyncResult *res, + SetCurrentBandsContext *ctx) +{ + GArray *bands; + GError *error = NULL; + guint currentbits; + + bands = modem_load_current_bands_finish (self, res, &error); + if (!bands) { + g_simple_async_result_take_error (ctx->result, error); + set_current_bands_context_complete_and_free (ctx); + return; + } + + currentbits = band_array_to_bandbits (bands); + ctx->enablebits = ctx->bandbits & ~currentbits; + ctx->disablebits = currentbits & ~ctx->bandbits; + + set_one_band (self, ctx); +} + +static void +modem_set_current_bands (MMIfaceModem *self, + GArray *bands_array, + GAsyncReadyCallback callback, + gpointer user_data) +{ + SetCurrentBandsContext *ctx; + + ctx = g_slice_new0 (SetCurrentBandsContext); + ctx->result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_set_current_bands); + ctx->bandbits = band_array_to_bandbits (bands_array); + + /* + * If ANY is requested, simply enable ANY to activate all bands except for + * those forbidden. */ + if (ctx->bandbits & modem_band_any_bit) { + ctx->enablebits = modem_band_any_bit; + ctx->disablebits = 0; + set_one_band (self, ctx); + return; + } + + modem_load_current_bands (self, + (GAsyncReadyCallback)set_current_bands_got_current_bands, + ctx); +} + +/*****************************************************************************/ +/* Load network timezone (Time interface) */ + +static gboolean +parse_tlts_query_reply (const gchar *response, + gchar **iso8601, + MMNetworkTimezone **tz, + GError **error) +{ + gint year; + gint month; + gint day; + gint hour; + gint minute; + gint second; + gchar sign; + gint offset; + GDateTime *utc, *adjusted; + + /* TLTS reports UTC time with the TZ offset to *local* time */ + response = mm_strip_tag (response, "*TLTS: "); + if (sscanf (response, + "\"%02d/%02d/%02d,%02d:%02d:%02d%c%02d\"", + &year, + &month, + &day, + &hour, + &minute, + &second, + &sign, + &offset) != 8) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Unknown *TLTS response: %s", + response); + return FALSE; + } + + /* Icera modems only report a 2-digit year, while ISO-8601 requires + * a 4-digit year. Assume 2000. + */ + if (year < 100) + year += 2000; + + /* Offset comes in 15-min units */ + offset *= 15; + /* Apply sign to offset; */ + if (sign == '-') + offset *= -1; + + utc = g_date_time_new_utc (year, month, day, hour, minute, second); + if (!utc) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Invalid *TLTS date/time: %s", + response); + return FALSE; + } + + /* Convert UTC time to local time by adjusting by the timezone offset */ + adjusted = g_date_time_add_minutes (utc, offset); + g_date_time_unref (utc); + if (!adjusted) { + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_FAILED, + "Failed to convert modem time to local time (offset %d)", + offset); + return FALSE; + } + + /* Convert offset from minutes-to-UTC to minutes-from-UTC */ + offset *= -1; + + if (tz) { + *tz = mm_network_timezone_new (); + mm_network_timezone_set_offset (*tz, offset); + } + + if (iso8601) { + *iso8601 = mm_new_iso8601_time (g_date_time_get_year (adjusted), + g_date_time_get_month (adjusted), + g_date_time_get_day_of_month (adjusted), + g_date_time_get_hour (adjusted), + g_date_time_get_minute (adjusted), + g_date_time_get_second (adjusted), + TRUE, + offset); + } + + g_date_time_unref (adjusted); + return TRUE; +} + +static MMNetworkTimezone * +modem_time_load_network_timezone_finish (MMIfaceModemTime *self, + GAsyncResult *res, + GError **error) +{ + const gchar *response; + MMNetworkTimezone *tz; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, NULL); + if (!response) { + /* We'll assume we can retry a bit later */ + g_set_error (error, + MM_CORE_ERROR, + MM_CORE_ERROR_RETRY, + "Retry"); + return NULL; + } + + return (parse_tlts_query_reply (response, NULL, &tz, error) ? tz : NULL); +} + +static void +modem_time_load_network_timezone (MMIfaceModemTime *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command (MM_BASE_MODEM (self), + "*TLTS", + 3, + FALSE, + callback, + user_data); +} + +/*****************************************************************************/ +/* Load network time (Time interface) */ + +static gchar * +modem_time_load_network_time_finish (MMIfaceModemTime *self, + GAsyncResult *res, + GError **error) +{ + const gchar *response; + gchar *iso8601; + + response = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, error); + if (!response) + return NULL; + + return (parse_tlts_query_reply (response, &iso8601, NULL, error) ? iso8601 : NULL); +} + +static void +modem_time_load_network_time (MMIfaceModemTime *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + mm_base_modem_at_command (MM_BASE_MODEM (self), + "*TLTS", + 3, + FALSE, + callback, + user_data); +} + +/*****************************************************************************/ +/* Check support (Time interface) */ + +static gboolean +modem_time_check_support_finish (MMIfaceModemTime *self, + GAsyncResult *res, + GError **error) +{ + /* We assume Icera devices always support *TLTS, since they appear + * to return ERROR if the modem is not powered up, and thus we cannot + * check for *TLTS support during modem initialization. + */ + return TRUE; +} + +static void +modem_time_check_support (MMIfaceModemTime *self, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GSimpleAsyncResult *result; + + result = g_simple_async_result_new (G_OBJECT (self), + callback, + user_data, + modem_time_check_support); + + g_simple_async_result_complete_in_idle (result); + g_object_unref (result); +} + +/*****************************************************************************/ +/* Setup ports (Broadband modem class) */ + +static void +setup_ports (MMBroadbandModem *self) +{ + /* Call parent's setup ports first always */ + MM_BROADBAND_MODEM_CLASS (mm_broadband_modem_icera_parent_class)->setup_ports (self); + + /* Now reset the unsolicited messages we'll handle when enabled */ + set_unsolicited_events_handlers (MM_BROADBAND_MODEM_ICERA (self), FALSE); +} + +/*****************************************************************************/ + +MMBroadbandModemIcera * +mm_broadband_modem_icera_new (const gchar *device, + const gchar **drivers, + const gchar *plugin, + guint16 vendor_id, + guint16 product_id) +{ + return g_object_new (MM_TYPE_BROADBAND_MODEM_ICERA, + MM_BASE_MODEM_DEVICE, device, + MM_BASE_MODEM_DRIVERS, drivers, + MM_BASE_MODEM_PLUGIN, plugin, + MM_BASE_MODEM_VENDOR_ID, vendor_id, + MM_BASE_MODEM_PRODUCT_ID, product_id, + NULL); +} + +static void +set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MMBroadbandModemIcera *self = MM_BROADBAND_MODEM_ICERA (object); + + switch (prop_id) { + case PROP_DEFAULT_IP_METHOD: + self->priv->default_ip_method = g_value_get_enum (value); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MMBroadbandModemIcera *self = MM_BROADBAND_MODEM_ICERA (object); + + switch (prop_id) { + case PROP_DEFAULT_IP_METHOD: + g_value_set_enum (value, self->priv->default_ip_method); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +mm_broadband_modem_icera_init (MMBroadbandModemIcera *self) +{ + /* Initialize private data */ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self), + MM_TYPE_BROADBAND_MODEM_ICERA, + MMBroadbandModemIceraPrivate); + + self->priv->nwstate_regex = g_regex_new ("%NWSTATE:\\s*(-?\\d+),(\\d+),([^,]*),([^,]*),(\\d+)", + G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL); + self->priv->pacsp_regex = g_regex_new ("\\r\\n\\+PACSP(\\d)\\r\\n", + G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL); + self->priv->ipdpact_regex = g_regex_new ("\\r\\n%IPDPACT:\\s*(\\d+),\\s*(\\d+),\\s*(\\d+)\\r\\n", + G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL); + + self->priv->default_ip_method = MM_BEARER_IP_METHOD_STATIC; + self->priv->last_act = MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN; +} + +static void +finalize (GObject *object) +{ + MMBroadbandModemIcera *self = MM_BROADBAND_MODEM_ICERA (object); + + g_regex_unref (self->priv->nwstate_regex); + g_regex_unref (self->priv->pacsp_regex); + g_regex_unref (self->priv->ipdpact_regex); + + G_OBJECT_CLASS (mm_broadband_modem_icera_parent_class)->finalize (object); +} + +static void +iface_modem_init (MMIfaceModem *iface) +{ + iface_modem_parent = g_type_interface_peek_parent (iface); + + iface->load_supported_modes = load_supported_modes; + iface->load_supported_modes_finish = load_supported_modes_finish; + iface->load_current_modes = modem_load_current_modes; + iface->load_current_modes_finish = modem_load_current_modes_finish; + iface->set_current_modes = modem_set_current_modes; + iface->set_current_modes_finish = modem_set_current_modes_finish; + iface->load_access_technologies = modem_load_access_technologies; + iface->load_access_technologies_finish = modem_load_access_technologies_finish; + iface->load_unlock_retries = modem_load_unlock_retries; + iface->load_unlock_retries_finish = modem_load_unlock_retries_finish; + iface->load_supported_bands = modem_load_supported_bands; + iface->load_supported_bands_finish = modem_load_supported_bands_finish; + iface->load_current_bands = modem_load_current_bands; + iface->load_current_bands_finish = modem_load_current_bands_finish; + iface->modem_power_up = modem_power_up; + iface->modem_power_up_finish = modem_power_up_finish; + /* Note: don't implement modem_init_power_down, as CFUN=4 here may take + * looong to reply */ + iface->modem_power_down = modem_power_down; + iface->modem_power_down_finish = modem_power_down_finish; + iface->reset = modem_reset; + iface->reset_finish = modem_reset_finish; + iface->set_current_bands = modem_set_current_bands; + iface->set_current_bands_finish = modem_set_current_bands_finish; + iface->create_bearer = modem_create_bearer; + iface->create_bearer_finish = modem_create_bearer_finish; +} + +static void +iface_modem_3gpp_init (MMIfaceModem3gpp *iface) +{ + iface_modem_3gpp_parent = g_type_interface_peek_parent (iface); + + iface->setup_unsolicited_events = modem_3gpp_setup_unsolicited_events; + iface->setup_unsolicited_events_finish = modem_3gpp_setup_cleanup_unsolicited_events_finish; + iface->cleanup_unsolicited_events = modem_3gpp_cleanup_unsolicited_events; + iface->cleanup_unsolicited_events_finish = modem_3gpp_setup_cleanup_unsolicited_events_finish; + iface->enable_unsolicited_events = modem_3gpp_enable_unsolicited_events; + iface->enable_unsolicited_events_finish = modem_3gpp_enable_disable_unsolicited_events_finish; + iface->disable_unsolicited_events = modem_3gpp_disable_unsolicited_events; + iface->disable_unsolicited_events_finish = modem_3gpp_enable_disable_unsolicited_events_finish; +} + +static void +iface_modem_time_init (MMIfaceModemTime *iface) +{ + iface->check_support = modem_time_check_support; + iface->check_support_finish = modem_time_check_support_finish; + iface->load_network_time = modem_time_load_network_time; + iface->load_network_time_finish = modem_time_load_network_time_finish; + iface->load_network_timezone = modem_time_load_network_timezone; + iface->load_network_timezone_finish = modem_time_load_network_timezone_finish; +} + +static void +mm_broadband_modem_icera_class_init (MMBroadbandModemIceraClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + MMBroadbandModemClass *broadband_modem_class = MM_BROADBAND_MODEM_CLASS (klass); + + g_type_class_add_private (object_class, sizeof (MMBroadbandModemIceraPrivate)); + + object_class->get_property = get_property; + object_class->set_property = set_property; + object_class->finalize = finalize; + broadband_modem_class->setup_ports = setup_ports; + + properties[PROP_DEFAULT_IP_METHOD] = + g_param_spec_enum (MM_BROADBAND_MODEM_ICERA_DEFAULT_IP_METHOD, + "Default IP method", + "Default IP Method (static or DHCP) to use.", + MM_TYPE_BEARER_IP_METHOD, + MM_BEARER_IP_METHOD_STATIC, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); + g_object_class_install_property (object_class, PROP_DEFAULT_IP_METHOD, properties[PROP_DEFAULT_IP_METHOD]); +} diff --git a/plugins/icera/mm-broadband-modem-icera.h b/plugins/icera/mm-broadband-modem-icera.h new file mode 100644 index 0000000..7826c66 --- /dev/null +++ b/plugins/icera/mm-broadband-modem-icera.h @@ -0,0 +1,53 @@ +/* -*- 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) 2008 - 2009 Novell, Inc. + * Copyright (C) 2009 - 2012 Red Hat, Inc. + * Copyright (C) 2012 Aleksander Morgado <aleksander@gnu.org> + */ + +#ifndef MM_BROADBAND_MODEM_ICERA_H +#define MM_BROADBAND_MODEM_ICERA_H + +#include "mm-broadband-modem.h" + +#define MM_TYPE_BROADBAND_MODEM_ICERA (mm_broadband_modem_icera_get_type ()) +#define MM_BROADBAND_MODEM_ICERA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_BROADBAND_MODEM_ICERA, MMBroadbandModemIcera)) +#define MM_BROADBAND_MODEM_ICERA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MM_TYPE_BROADBAND_MODEM_ICERA, MMBroadbandModemIceraClass)) +#define MM_IS_BROADBAND_MODEM_ICERA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_BROADBAND_MODEM_ICERA)) +#define MM_IS_BROADBAND_MODEM_ICERA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MM_TYPE_BROADBAND_MODEM_ICERA)) +#define MM_BROADBAND_MODEM_ICERA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MM_TYPE_BROADBAND_MODEM_ICERA, MMBroadbandModemIceraClass)) + +#define MM_BROADBAND_MODEM_ICERA_DEFAULT_IP_METHOD "broadband-modem-icera-default-ip-method" + +typedef struct _MMBroadbandModemIcera MMBroadbandModemIcera; +typedef struct _MMBroadbandModemIceraClass MMBroadbandModemIceraClass; +typedef struct _MMBroadbandModemIceraPrivate MMBroadbandModemIceraPrivate; + +struct _MMBroadbandModemIcera { + MMBroadbandModem parent; + MMBroadbandModemIceraPrivate *priv; +}; + +struct _MMBroadbandModemIceraClass{ + MMBroadbandModemClass parent; +}; + +GType mm_broadband_modem_icera_get_type (void); + +MMBroadbandModemIcera *mm_broadband_modem_icera_new (const gchar *device, + const gchar **drivers, + const gchar *plugin, + guint16 vendor_id, + guint16 product_id); + +#endif /* MM_BROADBAND_MODEM_ICERA_H */ |