From: Pekka Pessi <[email protected]>

Create modem only once GetProperties succeed, thus avoiding races with
PropertyChanged. Do not call GetProperties on existing modem.
---
 plugins/ofono.c |   49 +++++++++++++++++++++++++++----------------------
 1 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/plugins/ofono.c b/plugins/ofono.c
index 1a2a798..22682fb 100644
--- a/plugins/ofono.c
+++ b/plugins/ofono.c
@@ -1029,14 +1029,11 @@ static struct modem_data *add_modem(const char *path)
        if (path == NULL)
                return NULL;
 
-       DBG("");
+       DBG("path %s", path);
 
        modem = g_hash_table_lookup(modem_hash, path);
-       if (modem != NULL) {
-               modem->available = TRUE;
-
-               return modem;
-       }
+       if (modem != NULL)
+               return NULL;
 
        modem = g_try_new0(struct modem_data, 1);
        if (modem == NULL)
@@ -1044,7 +1041,6 @@ static struct modem_data *add_modem(const char *path)
 
        modem->path = g_strdup(path);
        modem->device = NULL;
-       modem->available = TRUE;
 
        g_hash_table_insert(modem_hash, g_strdup(path), modem);
 
@@ -1077,6 +1073,9 @@ static void modem_properties_reply(DBusPendingCall *call, 
void *user_data)
        DBusError error;
        DBusMessageIter array, dict;
        const char *path = user_data;
+       dbus_bool_t powered = FALSE;
+       dbus_bool_t has_gprs = FALSE;
+       struct modem_data *new_modem;
 
        DBG("path %s", path);
 
@@ -1103,7 +1102,6 @@ static void modem_properties_reply(DBusPendingCall *call, 
void *user_data)
        while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
                DBusMessageIter entry, value;
                const char *key;
-               dbus_bool_t powered;
 
                dbus_message_iter_recurse(&dict, &entry);
                dbus_message_iter_get_basic(&entry, &key);
@@ -1113,35 +1111,39 @@ static void modem_properties_reply(DBusPendingCall 
*call, void *user_data)
 
                if (g_str_equal(key, "Powered") == TRUE) {
                        dbus_message_iter_get_basic(&value, &powered);
-
-                       if (powered == FALSE) {
-                               modem_change_powered(path, TRUE);
-                               break;
-                       }
                } else if (g_str_equal(key, "Interfaces") == TRUE) {
                        if (modem_has_gprs(&value) == TRUE)
-                               get_imsi(path);
+                               has_gprs = TRUE;
                }
+
                dbus_message_iter_next(&dict);
        }
 
+       new_modem = add_modem(path);
+       if (new_modem) {
+               if (!powered)
+                       modem_change_powered(path, TRUE);
+               if (has_gprs)
+                       get_imsi(path);
+       }
+
 done:
        dbus_message_unref(reply);
 
        dbus_pending_call_unref(call);
 }
 
-static void get_modem_properties(struct modem_data *modem)
+static void get_modem_properties(const char *path)
 {
        DBusMessage *message;
        DBusPendingCall *call;
 
-       DBG("path %s", modem->path);
+       DBG("path %s", path);
 
-       if (modem->path == NULL)
+       if (path == NULL)
                return;
 
-       message = dbus_message_new_method_call(OFONO_SERVICE, modem->path,
+       message = dbus_message_new_method_call(OFONO_SERVICE, path,
                                OFONO_MODEM_INTERFACE, GET_PROPERTIES);
        if (message == NULL)
                return;
@@ -1160,7 +1162,7 @@ static void get_modem_properties(struct modem_data *modem)
        }
 
        dbus_pending_call_set_notify(call, modem_properties_reply,
-                                       (void *)g_strdup(modem->path), g_free);
+                                       (void *)g_strdup(path), g_free);
 
 done:
        dbus_message_unref(message);
@@ -1208,9 +1210,12 @@ static void update_modems(DBusMessageIter *array)
 
                dbus_message_iter_get_basic(&entry, &path);
 
-               modem = add_modem(path);
-               if (modem != NULL)
-                       get_modem_properties(modem);
+               modem = g_hash_table_lookup(modem_hash, path);
+
+               if (modem)
+                       modem->available = TRUE;
+               else
+                       get_modem_properties(path);
 
                dbus_message_iter_next(&entry);
        }
-- 
1.7.0.4

_______________________________________________
connman mailing list
[email protected]
http://lists.connman.net/listinfo/connman

Reply via email to