[PATCH] Telit plugin: add load_unlock_retries interface

Carlo Lobrano c.lobrano at gmail.com
Mon Dec 14 03:51:34 PST 2015


---
 plugins/telit/mm-broadband-modem-telit.c | 169 +++++++++++++++++++++++++++++++
 plugins/telit/mm-broadband-modem-telit.h |   2 +
 2 files changed, 171 insertions(+)

diff --git a/plugins/telit/mm-broadband-modem-telit.c b/plugins/telit/mm-broadband-modem-telit.c
index 0f77231..b34503d 100644
--- a/plugins/telit/mm-broadband-modem-telit.c
+++ b/plugins/telit/mm-broadband-modem-telit.c
@@ -39,6 +39,165 @@ G_DEFINE_TYPE_EXTENDED (MMBroadbandModemTelit, mm_broadband_modem_telit, MM_TYPE
                         G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM, iface_modem_init)
                         G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM_3GPP, iface_modem_3gpp_init));
 
+typedef struct {
+    gint pin;
+    gint pin2;
+    gint puk;
+    gint puk2;
+} ModemLockRetries;
+
+struct _MMBroadbandModemTelitPrivate {
+    ModemLockRetries mm_lock_retries;
+};
+
+
+/*****************************************************************************/
+/* Load unlock retries (Modem interface) */
+
+#define CSIM_QUERY_PIN_RETRIES_STR  "+CSIM=10,0020000100"
+#define CSIM_QUERY_PUK_RETRIES_STR  "+CSIM=10,002C000100"
+#define CSIM_QUERY_PIN2_RETRIES_STR "+CSIM=10,0020008100"
+#define CSIM_QUERY_PUK2_RETRIES_STR "+CSIM=10,002C008100"
+
+static gboolean
+csim_query_response_processor (MMBaseModem *_self,
+                               gpointer none,
+                               const gchar *command,
+                               const gchar *response,
+                               gboolean last_command,
+                               const GError *error,
+                               GVariant **result,
+                               GError **result_error)
+{
+    GRegex *r = NULL;
+    GMatchInfo *match_info = NULL;
+
+    r = g_regex_new ("\\+CSIM:\\s*.*63C(.*)\"", G_REGEX_RAW, 0, NULL);
+    g_assert (NULL != r);
+
+    if (!g_regex_match (r, response, 0, &match_info)) {
+        mm_err ("Could not parse +CSIM response '%s'", response);
+        goto end;
+    }
+
+    if (!g_match_info_matches (match_info)) {
+        mm_err ("Could not get SIM unlock retries for query '%s'. Response was '%s'",
+                command,
+                response);
+        goto end;
+    } else {
+        MMBroadbandModemTelit *self = MM_BROADBAND_MODEM_TELIT (_self);
+        gchar* retries_hex_str;
+        guint retries;
+
+        retries_hex_str = mm_get_string_unquoted_from_match_info (match_info, 1);
+        g_assert (NULL != retries_hex_str);
+
+        // convert hex value to uint
+        sscanf (retries_hex_str, "%x", &retries);
+        mm_dbg ("%s: CSIM retries 0x%s => %d", command, retries_hex_str, retries);
+
+        g_free (retries_hex_str);
+
+        if(strstr(CSIM_QUERY_PIN_RETRIES_STR, command)) {
+            self->priv->mm_lock_retries.pin = retries;
+        } else if(strstr(CSIM_QUERY_PIN2_RETRIES_STR, command)) {
+            self->priv->mm_lock_retries.pin2 = retries;
+        } else if(strstr(CSIM_QUERY_PUK_RETRIES_STR, command)) {
+            self->priv->mm_lock_retries.puk = retries;
+        } else if(strstr(CSIM_QUERY_PUK2_RETRIES_STR, command)) {
+            self->priv->mm_lock_retries.puk2 = retries;
+        } else {
+            mm_err ("Could not assign SIM unlock retry value: unrecognized command '%s'",
+                    command);
+        }
+    }
+
+end:
+    g_match_info_free (match_info);
+    g_regex_unref (r);
+
+    return FALSE;
+}
+
+static const MMBaseModemAtCommand load_unlock_retries_sequence[] = {
+ { CSIM_QUERY_PIN_RETRIES_STR, 10, FALSE, csim_query_response_processor },
+ { CSIM_QUERY_PIN2_RETRIES_STR, 10, FALSE, csim_query_response_processor },
+ { CSIM_QUERY_PUK_RETRIES_STR, 10, FALSE, csim_query_response_processor },
+ { CSIM_QUERY_PUK2_RETRIES_STR, 10, FALSE, csim_query_response_processor },
+ { NULL }
+};
+
+static MMUnlockRetries *
+modem_load_unlock_retries_finish (MMIfaceModem *_self,
+                                  GAsyncResult *res,
+                                  GError **error)
+{
+    MMBroadbandModemTelit *self = MM_BROADBAND_MODEM_TELIT (_self);
+    MMUnlockRetries* unlock_retries;
+
+    mm_dbg("%s", __FUNCTION__);
+
+
+    unlock_retries = mm_unlock_retries_new ();
+    if (self->priv->mm_lock_retries.pin > 0) {
+        mm_dbg("SIM PIN retries left:%d",self->priv->mm_lock_retries.pin);
+        mm_unlock_retries_set (unlock_retries, MM_MODEM_LOCK_SIM_PIN,
+                               self->priv->mm_lock_retries.pin);
+    } else {
+        mm_dbg("SIM PIN retries left: UNKNOWN");
+    }
+
+    if (self->priv->mm_lock_retries.pin2 > 0) {
+        mm_dbg("SIM PIN2 retries left:%d",self->priv->mm_lock_retries.pin2);
+        mm_unlock_retries_set (unlock_retries, MM_MODEM_LOCK_SIM_PIN2,
+                               self->priv->mm_lock_retries.pin2);
+    } else {
+        mm_dbg("SIM PIN2 retries left: UNKNOWN");
+    }
+
+    if (self->priv->mm_lock_retries.puk > 0) {
+        mm_dbg("SIM PUK retries left:%d",self->priv->mm_lock_retries.puk);
+        mm_unlock_retries_set (unlock_retries, MM_MODEM_LOCK_SIM_PUK,
+                               self->priv->mm_lock_retries.puk);
+    } else {
+        mm_dbg("SIM PUK retries left: UNKNOWN");
+    }
+
+    if (self->priv->mm_lock_retries.puk2 > 0) {
+        mm_dbg("SIM PUK2 retries left:%d",self->priv->mm_lock_retries.puk2);
+        mm_unlock_retries_set (unlock_retries, MM_MODEM_LOCK_SIM_PUK2,
+                               self->priv->mm_lock_retries.puk2);
+    } else {
+        mm_dbg("SIM PUK2 retries left: UNKNOWN");
+    }
+
+    return unlock_retries;
+}
+
+static void
+modem_load_unlock_retries (MMIfaceModem *self,
+                           GAsyncReadyCallback callback,
+                           gpointer user_data)
+{
+    GSimpleAsyncResult *result;
+
+    mm_dbg("%s", __FUNCTION__);
+
+    result = g_simple_async_result_new (G_OBJECT (self),
+                                        callback,
+                                        user_data,
+                                        modem_load_unlock_retries);
+
+    mm_base_modem_at_sequence (
+        MM_BASE_MODEM (self),
+        load_unlock_retries_sequence,
+        NULL, /* response_processor_context */
+        NULL, /* response_processor_context_free */
+        callback,
+        user_data);
+}
+
 /*****************************************************************************/
 /* Modem power down (Modem interface) */
 
@@ -331,11 +490,21 @@ mm_broadband_modem_telit_new (const gchar *device,
 static void
 mm_broadband_modem_telit_init (MMBroadbandModemTelit *self)
 {
+    /* Initialize private data */
+    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
+                                              MM_TYPE_BROADBAND_MODEM_TELIT,
+                                              MMBroadbandModemTelitPrivate);
+    self->priv->mm_lock_retries.pin = -1;
+    self->priv->mm_lock_retries.pin2 = -1;
+    self->priv->mm_lock_retries.puk = -1;
+    self->priv->mm_lock_retries.puk2 = -1;
 }
 
 static void
 iface_modem_init (MMIfaceModem *iface)
 {
+    iface->load_unlock_retries_finish = modem_load_unlock_retries_finish;
+    iface->load_unlock_retries = modem_load_unlock_retries;
     iface->reset = modem_reset;
     iface->reset_finish = modem_reset_finish;
     iface->modem_power_down = modem_power_down;
diff --git a/plugins/telit/mm-broadband-modem-telit.h b/plugins/telit/mm-broadband-modem-telit.h
index 50e6365..9b32ec6 100644
--- a/plugins/telit/mm-broadband-modem-telit.h
+++ b/plugins/telit/mm-broadband-modem-telit.h
@@ -29,9 +29,11 @@
 
 typedef struct _MMBroadbandModemTelit MMBroadbandModemTelit;
 typedef struct _MMBroadbandModemTelitClass MMBroadbandModemTelitClass;
+typedef struct _MMBroadbandModemTelitPrivate MMBroadbandModemTelitPrivate;
 
 struct _MMBroadbandModemTelit {
     MMBroadbandModem parent;
+    MMBroadbandModemTelitPrivate* priv;
 };
 
 struct _MMBroadbandModemTelitClass{
-- 
2.1.4



More information about the ModemManager-devel mailing list