commit 5ef95437a939691a40b8d10819f386a6af866053
Author: Norbert <nf2.email@gmail.com>
Date:   Wed Jan 13 13:39:04 2010 +0100

    Initial try enable Modem to check if PIN or PUK is required

diff --git a/src/mm-generic-gsm.c b/src/mm-generic-gsm.c
index ba8422f..c82dbff 100644
--- a/src/mm-generic-gsm.c
+++ b/src/mm-generic-gsm.c
@@ -50,6 +50,7 @@ typedef struct {
     char *device;
 
     gboolean valid;
+    gboolean pin_checked;
 
     char *oper_code;
     char *oper_name;
@@ -171,6 +172,65 @@ typedef struct {
 } CPinResult;
 
 static void
+check_valid (MMGenericGsm *self);
+
+static void
+initial_disable_after_enable_finished(MMModem *modem, GError *error, gpointer user_data)
+{
+    if (error)
+        printf("initial disable after try-enable failed %d %s\n", error->code, error->message);
+    else
+        printf("initial disable after try-enable succeeded\n");
+
+    check_valid (MM_GENERIC_GSM(modem));
+}
+
+static void
+initial_try_enable_finished (MMModem *modem, GError *error, gpointer user_data)
+{
+    MMGenericGsmPrivate *priv;
+    gchar * unlock_required_str = NULL;
+
+    g_return_if_fail (MM_IS_GENERIC_GSM (modem));
+
+    priv = MM_GENERIC_GSM_GET_PRIVATE (modem);
+
+    priv->pin_checked = TRUE;
+
+    if (error)
+    {
+        printf("initial try enable for pin-check %d %s\n", error->code, error->message);
+
+        switch(error->code)
+        {
+            case MM_MOBILE_ERROR_SIM_PIN: unlock_required_str = "sim-pin"; break;
+            case MM_MOBILE_ERROR_SIM_PUK: unlock_required_str = "sim-puk"; break;
+            case MM_MOBILE_ERROR_PH_SIM_PIN: unlock_required_str = "ph-sim-pin"; break;
+            case MM_MOBILE_ERROR_PH_FSIM_PIN: unlock_required_str = "ph-fsim-pin"; break;
+            case MM_MOBILE_ERROR_PH_FSIM_PUK: unlock_required_str = "ph-fsim-puk"; break;
+            case MM_MOBILE_ERROR_SIM_PIN2: unlock_required_str = "sim-pin2"; break;
+            case MM_MOBILE_ERROR_SIM_PUK2: unlock_required_str = "sim-puk2"; break;
+            case MM_MOBILE_ERROR_NETWORK_PIN: unlock_required_str = "ph-net-pin"; break;
+            case MM_MOBILE_ERROR_NETWORK_PUK: unlock_required_str = "ph-net-puk"; break;
+            case MM_MOBILE_ERROR_NETWORK_SUBSET_PIN: unlock_required_str = "ph-netsub-pin"; break;
+            case MM_MOBILE_ERROR_NETWORK_SUBSET_PUK: unlock_required_str = "ph-netsub-puk"; break;
+            case MM_MOBILE_ERROR_SERVICE_PIN: unlock_required_str = "ph-sp-pin"; break;
+            case MM_MOBILE_ERROR_SERVICE_PUK: unlock_required_str = "ph-sp-puk"; break;
+            case MM_MOBILE_ERROR_CORP_PIN: unlock_required_str = "ph-corp-pin"; break;
+            case MM_MOBILE_ERROR_CORP_PUK: unlock_required_str = "ph-corp-puk"; break;
+            case MM_MOBILE_ERROR_PHONE_FAILURE: unlock_required_str = "ph-failure"; break;
+        }
+        mm_modem_base_set_unlock_required(MM_MODEM_BASE(modem), unlock_required_str);
+        check_valid (MM_GENERIC_GSM(modem));
+    }
+    else
+    {
+        printf("initial try enable for pin-check finished (no unlock required) --> disable modem again\n");
+        mm_modem_disable(modem, initial_disable_after_enable_finished, NULL);
+    }
+}
+
+static void
 pin_check_done (MMSerialPort *port,
                 GString *response,
                 GError *error,
@@ -287,7 +347,7 @@ check_valid (MMGenericGsm *self)
     MMGenericGsmPrivate *priv = MM_GENERIC_GSM_GET_PRIVATE (self);
     gboolean new_valid = FALSE;
 
-    if (priv->primary && priv->data)
+    if (priv->primary && priv->data && priv->pin_checked)
         new_valid = TRUE;
 
     mm_modem_base_set_valid (MM_MODEM_BASE (self), new_valid);
@@ -329,6 +389,10 @@ mm_generic_gsm_grab_port (MMGenericGsm *self,
                 priv->data = port;
                 g_object_notify (G_OBJECT (self), MM_MODEM_DATA_DEVICE);
             }
+
+            /* do the initial pin check - the enable function does this for us */
+            mm_modem_enable(MM_MODEM (self), initial_try_enable_finished, NULL);
+
             check_valid (self);
         } else if (ptype == MM_PORT_TYPE_SECONDARY)
             priv->secondary = MM_SERIAL_PORT (port);
diff --git a/src/mm-modem-gsm-card.c b/src/mm-modem-gsm-card.c
index dea4590..f2ffe1a 100644
--- a/src/mm-modem-gsm-card.c
+++ b/src/mm-modem-gsm-card.c
@@ -15,6 +15,7 @@
  */
 
 #include <dbus/dbus-glib.h>
+#include <stdio.h>
 
 #include "mm-modem-gsm-card.h"
 #include "mm-errors.h"
@@ -86,6 +87,24 @@ async_call_done (MMModem *modem, GError *error, gpointer user_data)
         dbus_g_method_return (context);
 }
 
+/* hack: method signature */
+void mm_modem_base_set_unlock_required (void *self, const char *unlock_required);
+
+static void
+async_send_pin_or_puk_call_done (MMModem *modem, GError *error, gpointer user_data)
+{
+    if (!error)
+    {
+        /* on success always reset the "unlock_required" property */
+        mm_modem_base_set_unlock_required(modem, NULL);
+    }
+    else
+    {
+        printf("async_send_pin_or_puk_call_done %d %s\n", error->code, error->message);
+    }
+    async_call_done(modem, error, user_data);
+}
+
 static void
 async_call_not_supported (MMModemGsmCard *self,
                           MMModemFn callback,
@@ -220,7 +239,7 @@ static void
                             const char *pin,
                             DBusGMethodInvocation *context)
 {
-    mm_modem_gsm_card_send_puk (modem, puk, pin, async_call_done, context);
+    mm_modem_gsm_card_send_puk (modem, puk, pin, async_send_pin_or_puk_call_done, context);
 }
 
 static void
@@ -228,7 +247,7 @@ impl_gsm_modem_send_pin (MMModemGsmCard *modem,
                          const char *pin,
                          DBusGMethodInvocation *context)
 {
-    mm_modem_gsm_card_send_pin (modem, pin, async_call_done, context);
+    mm_modem_gsm_card_send_pin (modem, pin, async_send_pin_or_puk_call_done, context);
 }
 
 static void
