device: Combine two if statements with identical outcome
[framework/connectivity/connman.git] / plugins / ofono.c
index 8b8dcb0..ae419be 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"
@@ -172,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;
@@ -312,6 +311,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);
 }
 
@@ -830,7 +832,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;
@@ -933,7 +935,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);
 
@@ -1058,7 +1060,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;
 
@@ -1103,8 +1105,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);
        }
 
@@ -1118,6 +1129,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;
 }
 
@@ -1127,10 +1144,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,
@@ -1178,6 +1203,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;
@@ -1516,7 +1574,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);
@@ -2388,7 +2447,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,