wifi: Add support for autoscan request
[framework/connectivity/connman.git] / plugins / ofono.c
index 4906dc5..d9696d7 100644 (file)
@@ -2,7 +2,7 @@
  *
  *  Connection Manager
  *
- *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
+ *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
  *  Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  *  Copyright (C) 2011  BWM Car IT GmbH. All rights reserved.
  *
@@ -43,8 +43,6 @@
 
 #include "mcc.h"
 
-#define uninitialized_var(x) x = x
-
 #define OFONO_SERVICE                  "org.ofono"
 
 #define OFONO_MANAGER_INTERFACE                OFONO_SERVICE ".Manager"
@@ -83,13 +81,48 @@ enum ofono_api {
  *   powered -> SubscriberIdentity or Online = True -> gprs, context ->
  *     attached -> netreg -> ready
  *
- * Enabling and disabling modems are steered through the rfkill
- * interface. That means when ConnMan toggles the rfkill bit oFono
- * will add or remove the modems.
+ * Depending on the modem type, this plugin will behave differently.
+ *
+ * GSM working flow:
+ *
+ * When a new modem appears, the plugin always powers it up. This
+ * allows the plugin to create a connman_device. The core will call
+ * modem_enable() if the technology is enabled. modem_enable() will
+ * then set the modem online. If the technology is disabled then
+ * modem_disable() will just set the modem offline. The modem is
+ * always kept powered all the time.
+ *
+ * After setting the modem online the plugin waits for the
+ * ConnectionManager and ConnectionContext to appear. When the context
+ * signals that it is attached and the NetworkRegistration interface
+ * appears, a new Service will be created and registered at the core.
+ *
+ * When asked to connect to the network (network_connect()) the plugin
+ * will set the Active property on the context. If this operation is
+ * successful the modem is connected to the network. oFono will inform
+ * the plugin about IP configuration through the updating the context's
+ * properties.
+ *
+ * CDMA working flow:
+ *
+ * When a new modem appears, the plugin always powers it up. This
+ * allows the plugin to create connman_device either using IMSI either
+ * using modem Serial if the modem got a SIM interface or not.
+ *
+ * As for GSM, the core will call modem_enable() if the technology
+ * is enabled. modem_enable() will then set the modem online.
+ * If the technology is disabled then modem_disable() will just set the
+ * modem offline. The modem is always kept powered all the time.
+ *
+ * After setting the modem online the plugin waits for CdmaConnectionManager
+ * interface to appear. Then, once CdmaNetworkRegistration appears, a new
+ * Service will be created and registered at the core.
  *
- * ConnMan will always power up (set Powered and Online) the
- * modems. No need to power them down because this will be done
- * through the rfkill inteface.
+ * When asked to connect to the network (network_connect()) the plugin
+ * will power up the CdmaConnectionManager interface.
+ * If the operation is successful the modem is connected to the network.
+ * oFono will inform the plugin about IP configuration through the
+ * updating CdmaConnectionManager settings properties.
  */
 
 static DBusConnection *connection;
@@ -137,6 +170,7 @@ struct modem_data {
        /* ConnectionContext Interface */
        connman_bool_t active;
        connman_bool_t set_active;
+       connman_bool_t valid_apn; /* APN is 'valid' if length > 0 */
 
        /* SimManager Interface */
        char *imsi;
@@ -226,6 +260,12 @@ static void set_connected(struct modem_data *modem)
 
        DBG("%s", modem->path);
 
+       if (modem->context->index < 0 ||
+                       modem->context->ipv4_address == NULL) {
+               connman_error("Invalid index and/or address");
+               return;
+       }
+
        connman_network_set_index(modem->network, modem->context->index);
 
        switch (modem->context->ipv4_method) {
@@ -277,6 +317,9 @@ static void set_disconnected(struct modem_data *modem)
 {
        DBG("%s", modem->path);
 
+       if (modem->network == NULL)
+               return;
+
        connman_network_set_connected(modem->network, FALSE);
 }
 
@@ -338,8 +381,10 @@ static int set_property(struct modem_data *modem,
        DBG("%s path %s %s.%s", modem->path, path, interface, property);
 
        if (modem->call_set_property != NULL) {
-               connman_error("Pending SetProperty");
-               return -EBUSY;
+               DBG("Cancel pending SetProperty");
+
+               dbus_pending_call_cancel(modem->call_set_property);
+               modem->call_set_property = NULL;
        }
 
        message = dbus_message_new_method_call(OFONO_SERVICE, path,
@@ -793,7 +838,7 @@ static void extract_ipv6_settings(DBusMessageIter *array,
 {
        DBusMessageIter dict;
        char *address = NULL, *gateway = NULL;
-       unsigned char prefix_length;
+       unsigned char prefix_length = 0;
        char *nameservers = NULL;
        const char *interface = NULL;
        int index = -1;
@@ -877,8 +922,6 @@ static connman_bool_t ready_to_create_device(struct modem_data *modem)
         * different:
         * - GSM modems will expose the SIM interface then the
         *   CM interface.
-        * - DUN modems will expose first a unique serial number (BDADDR)
-        *   and then the CM interface.
         * - CDMA modems will expose CM first and sometime later
         *   a unique serial number.
         *
@@ -898,7 +941,7 @@ static connman_bool_t ready_to_create_device(struct modem_data *modem)
 static void create_device(struct modem_data *modem)
 {
        struct connman_device *device;
-       char *uninitialized_var(ident);
+       char *ident = NULL;
 
        DBG("%s", modem->path);
 
@@ -998,6 +1041,12 @@ static void add_network(struct modem_data *modem)
                modem->network = NULL;
                return;
        }
+
+       /*
+        * Create the ipconfig layer before trying to connect. Withouth
+        * the ipconfig layer the core is not ready to process errors.
+        */
+       connman_network_set_index(modem->network, -1);
 }
 
 static void remove_network(struct modem_data *modem)
@@ -1017,7 +1066,7 @@ static void remove_network(struct modem_data *modem)
 static int add_cm_context(struct modem_data *modem, const char *context_path,
                                DBusMessageIter *dict)
 {
-       const char *context_type;
+       const char *context_type = NULL;
        struct network_context *context = NULL;
        connman_bool_t active = FALSE;
 
@@ -1062,8 +1111,17 @@ static int add_cm_context(struct modem_data *modem, const char *context_path,
                        dbus_message_iter_get_basic(&value, &active);
 
                        DBG("%s Active %d", modem->path, active);
-               }
+               } else if (g_str_equal(key, "AccessPointName") == TRUE) {
+                       const char *apn;
+
+                       dbus_message_iter_get_basic(&value, &apn);
+                       if (apn != NULL && strlen(apn) > 0)
+                               modem->valid_apn = TRUE;
+                       else
+                               modem->valid_apn = FALSE;
 
+                       DBG("%s AccessPointName '%s'", modem->path, apn);
+               }
                dbus_message_iter_next(dict);
        }
 
@@ -1077,6 +1135,12 @@ static int add_cm_context(struct modem_data *modem, const char *context_path,
 
        g_hash_table_replace(context_hash, g_strdup(context_path), modem);
 
+       if (modem->valid_apn == TRUE && modem->attached == TRUE &&
+                       has_interface(modem->interfaces,
+                               OFONO_API_NETREG) == TRUE) {
+               add_network(modem);
+       }
+
        return 0;
 }
 
@@ -1086,10 +1150,18 @@ static void remove_cm_context(struct modem_data *modem,
        if (modem->context == NULL)
                return;
 
+       if (modem->network != NULL)
+               remove_network(modem);
+
        g_hash_table_remove(context_hash, context_path);
 
        network_context_free(modem->context);
        modem->context = NULL;
+
+       modem->valid_apn = FALSE;
+
+       if (modem->network != NULL)
+               remove_network(modem);
 }
 
 static gboolean context_changed(DBusConnection *connection,
@@ -1137,6 +1209,39 @@ static gboolean context_changed(DBusConnection *connection,
                        set_connected(modem);
                else
                        set_disconnected(modem);
+       } else if (g_str_equal(key, "AccessPointName") == TRUE) {
+               const char *apn;
+
+               dbus_message_iter_get_basic(&value, &apn);
+
+               DBG("%s AccessPointName %s", modem->path, apn);
+
+               if (apn != NULL && strlen(apn) > 0) {
+                       modem->valid_apn = TRUE;
+
+                       if (modem->network != NULL)
+                               return TRUE;
+
+                       if (modem->attached == FALSE)
+                               return TRUE;
+
+                       if (has_interface(modem->interfaces,
+                                       OFONO_API_NETREG) == FALSE) {
+                               return TRUE;
+                       }
+
+                       add_network(modem);
+
+                       if (modem->active == TRUE)
+                               set_connected(modem);
+               } else {
+                       modem->valid_apn = FALSE;
+
+                       if (modem->network == NULL)
+                               return TRUE;
+
+                       remove_network(modem);
+               }
        }
 
        return TRUE;
@@ -1239,7 +1344,7 @@ static gboolean cm_context_added(DBusConnection *connection,
 
        DBG("%s", path);
 
-       modem = g_hash_table_lookup(modem_hash, context_path);
+       modem = g_hash_table_lookup(modem_hash, path);
        if (modem == NULL)
                return TRUE;
 
@@ -1393,11 +1498,12 @@ static void netreg_update_regdom(struct modem_data *modem,
 
 
        mcc = atoi(mobile_country_code);
-       if (mcc > 799)
+       if (mcc > 799 || mcc < 200)
                return;
 
        alpha2 = mcc_country_codes[mcc - 200];
-       connman_technology_set_regdom(alpha2);
+       if (alpha2 != NULL)
+               connman_technology_set_regdom(alpha2);
 }
 
 static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
@@ -1474,7 +1580,8 @@ static void netreg_properties_reply(struct modem_data *modem,
                return;
        }
 
-       add_network(modem);
+       if (modem->valid_apn == TRUE)
+               add_network(modem);
 
        if (modem->active == TRUE)
                set_connected(modem);
@@ -1499,6 +1606,9 @@ static void add_cdma_network(struct modem_data *modem)
        if (modem->context == NULL)
                modem->context = network_context_alloc(modem->path);
 
+       if (modem->name == NULL)
+               modem->name = g_strdup("CDMA Network");
+
        add_network(modem);
 
        if (modem->cdma_cm_powered == TRUE)
@@ -1665,9 +1775,6 @@ static void cdma_cm_update_settings(struct modem_data *modem,
 {
        DBG("%s Settings", modem->path);
 
-       if (modem->context != NULL)
-               return;
-
        extract_ipv4_settings(value, modem->context);
 }
 
@@ -2016,6 +2123,12 @@ static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
                modem->serial = g_strdup(serial);
 
                DBG("%s Serial %s", modem->path, modem->serial);
+
+               if (has_interface(modem->interfaces,
+                                        OFONO_API_CDMA_CM) == TRUE) {
+                       if (ready_to_create_device(modem) == TRUE)
+                               create_device(modem);
+               }
        }
 
        return TRUE;
@@ -2340,7 +2453,7 @@ static int network_disconnect(struct connman_network *network)
 }
 
 static struct connman_network_driver network_driver = {
-       .name           = "network",
+       .name           = "cellular",
        .type           = CONNMAN_NETWORK_TYPE_CELLULAR,
        .probe          = network_probe,
        .remove         = network_remove,
@@ -2397,6 +2510,22 @@ static struct connman_device_driver modem_driver = {
        .disable        = modem_disable,
 };
 
+static int tech_probe(struct connman_technology *technology)
+{
+       return 0;
+}
+
+static void tech_remove(struct connman_technology *technology)
+{
+}
+
+static struct connman_technology_driver tech_driver = {
+       .name           = "cellular",
+       .type           = CONNMAN_SERVICE_TYPE_CELLULAR,
+       .probe          = tech_probe,
+       .remove         = tech_remove,
+};
+
 static guint watch;
 static guint modem_added_watch;
 static guint modem_removed_watch;
@@ -2424,68 +2553,71 @@ static int ofono_init(void)
                                        OFONO_SERVICE, ofono_connect,
                                        ofono_disconnect, NULL, NULL);
 
-       modem_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
-                                               OFONO_MANAGER_INTERFACE,
+       modem_added_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
+                                               NULL, OFONO_MANAGER_INTERFACE,
                                                MODEM_ADDED,
                                                modem_added,
                                                NULL, NULL);
 
-       modem_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       modem_removed_watch = g_dbus_add_signal_watch(connection,
+                                               OFONO_SERVICE, NULL,
                                                OFONO_MANAGER_INTERFACE,
                                                MODEM_REMOVED,
                                                modem_removed,
                                                NULL, NULL);
 
-       modem_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       modem_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
                                                OFONO_MODEM_INTERFACE,
                                                PROPERTY_CHANGED,
                                                modem_changed,
                                                NULL, NULL);
 
-       cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
                                                OFONO_CM_INTERFACE,
                                                PROPERTY_CHANGED,
                                                cm_changed,
                                                NULL, NULL);
 
-       sim_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       sim_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
                                                OFONO_SIM_INTERFACE,
                                                PROPERTY_CHANGED,
                                                sim_changed,
                                                NULL, NULL);
 
-       context_added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       context_added_watch = g_dbus_add_signal_watch(connection,
+                                               OFONO_SERVICE, NULL,
                                                OFONO_CM_INTERFACE,
                                                CONTEXT_ADDED,
                                                cm_context_added,
                                                NULL, NULL);
 
-       context_removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       context_removed_watch = g_dbus_add_signal_watch(connection,
+                                               OFONO_SERVICE, NULL,
                                                OFONO_CM_INTERFACE,
                                                CONTEXT_REMOVED,
                                                cm_context_removed,
                                                NULL, NULL);
 
-       context_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
-                                               OFONO_CONTEXT_INTERFACE,
+       context_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
+                                               NULL, OFONO_CONTEXT_INTERFACE,
                                                PROPERTY_CHANGED,
                                                context_changed,
                                                NULL, NULL);
 
-       netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+       netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE, NULL,
                                                OFONO_NETREG_INTERFACE,
                                                PROPERTY_CHANGED,
                                                netreg_changed,
                                                NULL, NULL);
 
-       cdma_cm_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
-                                               OFONO_CDMA_CM_INTERFACE,
+       cdma_cm_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
+                                               NULL, OFONO_CDMA_CM_INTERFACE,
                                                PROPERTY_CHANGED,
                                                cdma_cm_changed,
                                                NULL, NULL);
 
-       cdma_netreg_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
-                                               OFONO_CDMA_NETREG_INTERFACE,
+       cdma_netreg_watch = g_dbus_add_signal_watch(connection, OFONO_SERVICE,
+                                               NULL, OFONO_CDMA_NETREG_INTERFACE,
                                                PROPERTY_CHANGED,
                                                cdma_netreg_changed,
                                                NULL, NULL);
@@ -2511,6 +2643,13 @@ static int ofono_init(void)
                goto remove;
        }
 
+       err = connman_technology_driver_register(&tech_driver);
+       if (err < 0) {
+               connman_device_driver_unregister(&modem_driver);
+               connman_network_driver_unregister(&network_driver);
+               goto remove;
+       }
+
        return 0;
 
 remove:
@@ -2554,6 +2693,7 @@ static void ofono_exit(void)
                context_hash = NULL;
        }
 
+       connman_technology_driver_unregister(&tech_driver);
        connman_device_driver_unregister(&modem_driver);
        connman_network_driver_unregister(&network_driver);