ofono: Get ConnectionManager properties
[platform/upstream/connman.git] / plugins / ofono.c
index 63ca7c6..b90709b 100644 (file)
@@ -38,6 +38,8 @@
 #include <connman/dbus.h>
 #include <connman/log.h>
 
+#define uninitialized_var(x) x = x
+
 #define OFONO_SERVICE                  "org.ofono"
 
 #define OFONO_MANAGER_INTERFACE                OFONO_SERVICE ".Manager"
@@ -72,6 +74,8 @@ static GHashTable *modem_hash;
 struct modem_data {
        char *path;
 
+       struct connman_device *device;
+
        /* Modem Interface */
        char *serial;
        connman_bool_t powered;
@@ -79,6 +83,10 @@ struct modem_data {
        uint8_t interfaces;
 
        connman_bool_t set_powered;
+       connman_bool_t set_online;
+
+       /* ConnectionManager Interface */
+       connman_bool_t attached;
 
        /* SimManager Interface */
        char *imsi;
@@ -285,6 +293,35 @@ static int get_properties(const char *path, const char *interface,
        return -EINPROGRESS;
 }
 
+static void modem_set_online_reply(struct modem_data *modem,
+                                       connman_bool_t success)
+{
+       DBG("%s", modem->path);
+
+       if (success == TRUE) {
+               /*
+                * Don't handle do anything on success here. oFono will send
+                * the change via PropertyChanged singal.
+                */
+               return;
+       }
+
+       modem->set_online = FALSE;
+}
+
+static int modem_set_online(struct modem_data *modem)
+{
+       DBG("%s", modem->path);
+
+       modem->set_online = TRUE;
+
+       return set_property(modem, modem->path,
+                               OFONO_MODEM_INTERFACE,
+                               "Online", DBUS_TYPE_BOOLEAN,
+                               &modem->set_online,
+                               modem_set_online_reply);
+}
+
 static int modem_set_powered(struct modem_data *modem)
 {
        DBG("%s", modem->path);
@@ -332,6 +369,84 @@ static uint8_t extract_interfaces(DBusMessageIter *array)
        return interfaces;
 }
 
+static connman_bool_t ready_to_create_device(struct modem_data *modem)
+{
+       /*
+        * There are three different modem types which behave slightly
+        * 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.
+        *
+        * This functions tests if we have the necessary information gathered
+        * before we are able to create a device.
+        */
+
+       if (modem->device != NULL)
+               return FALSE;
+
+       if (modem->imsi != NULL || modem->serial != NULL)
+               return TRUE;
+
+       return FALSE;
+}
+
+static void create_device(struct modem_data *modem)
+{
+       struct connman_device *device;
+       char *uninitialized_var(ident);
+
+       DBG("%s", modem->path);
+
+       if (modem->imsi != NULL)
+               ident = modem->imsi;
+       else if (modem->serial != NULL)
+               ident = modem->serial;
+
+       if (connman_dbus_validate_ident(ident) == FALSE)
+               ident = connman_dbus_encode_string(ident);
+       else
+               ident = g_strdup(ident);
+
+       device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
+       if (device == NULL)
+               goto out;
+
+       DBG("device %p", device);
+
+       connman_device_set_ident(device, ident);
+
+       connman_device_set_string(device, "Path", modem->path);
+
+       connman_device_set_data(device, modem);
+
+       if (connman_device_register(device) < 0) {
+               connman_error("Failed to register cellular device");
+               connman_device_unref(device);
+               goto out;
+       }
+
+       modem->device = device;
+
+out:
+       g_free(ident);
+}
+
+static void destroy_device(struct modem_data *modem)
+{
+       DBG("%s", modem->path);
+
+       connman_device_set_powered(modem->device, FALSE);
+
+       connman_device_unregister(modem->device);
+       connman_device_unref(modem->device);
+
+       modem->device = NULL;
+}
+
 static gboolean context_changed(DBusConnection *connection,
                                DBusMessage *message,
                                void *user_data)
@@ -362,9 +477,65 @@ static gboolean netreg_changed(DBusConnection *connection, DBusMessage *message,
 static gboolean cm_changed(DBusConnection *connection, DBusMessage *message,
                                void *user_data)
 {
+       const char *path = dbus_message_get_path(message);
+       struct modem_data *modem;
+       DBusMessageIter iter, value;
+       const char *key;
+
+       modem = g_hash_table_lookup(modem_hash, path);
+       if (modem == NULL)
+               return TRUE;
+
+       if (dbus_message_iter_init(message, &iter) == FALSE)
+               return TRUE;
+
+       dbus_message_iter_get_basic(&iter, &key);
+
+       dbus_message_iter_next(&iter);
+       dbus_message_iter_recurse(&iter, &value);
+
+       if (g_str_equal(key, "Attached") == TRUE) {
+               dbus_message_iter_get_basic(&value, &modem->attached);
+
+               DBG("%s Attached %d", modem->path, modem->attached);
+       }
+
        return TRUE;
 }
 
+static void cm_properties_reply(struct modem_data *modem, DBusMessageIter *dict)
+{
+       DBG("%s", modem->path);
+
+       while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
+               DBusMessageIter entry, value;
+               const char *key;
+
+               dbus_message_iter_recurse(dict, &entry);
+               dbus_message_iter_get_basic(&entry, &key);
+
+               dbus_message_iter_next(&entry);
+               dbus_message_iter_recurse(&entry, &value);
+
+               if (g_str_equal(key, "Attached") == TRUE) {
+                       dbus_message_iter_get_basic(&value, &modem->attached);
+
+                       DBG("%s Attached %d", modem->path,
+                               modem->attached);
+
+                       return;
+               }
+
+               dbus_message_iter_next(dict);
+       }
+}
+
+static int cm_get_properties(struct modem_data *modem)
+{
+       return get_properties(modem->path, OFONO_CM_INTERFACE,
+                               cm_properties_reply, modem);
+}
+
 static void update_sim_imsi(struct modem_data *modem,
                                const char *imsi)
 {
@@ -403,6 +574,14 @@ static gboolean sim_changed(DBusConnection *connection, DBusMessage *message,
                dbus_message_iter_get_basic(&value, &imsi);
 
                update_sim_imsi(modem, imsi);
+
+               if (modem->online == FALSE) {
+                       modem_set_online(modem);
+               } else if (has_interface(modem->interfaces,
+                                               OFONO_API_CM) == TRUE) {
+                       if (ready_to_create_device(modem) == TRUE)
+                               create_device(modem);
+               }
        }
 
        return TRUE;
@@ -430,6 +609,17 @@ static void sim_properties_reply(struct modem_data *modem,
 
                        update_sim_imsi(modem, imsi);
 
+                       if (modem->online == FALSE) {
+                               modem_set_online(modem);
+                               break;
+                       }
+
+                       if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+                               if (ready_to_create_device(modem) == TRUE)
+                                       create_device(modem);
+                               if (modem->device != NULL)
+                                       cm_get_properties(modem);
+                       }
                        return;
                }
 
@@ -474,6 +664,16 @@ static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
                dbus_message_iter_get_basic(&value, &modem->online);
 
                DBG("%s Online %d", modem->path, modem->online);
+
+               if (modem->online == FALSE)
+                       return TRUE;
+
+               if (has_interface(modem->interfaces, OFONO_API_CM) == FALSE)
+                       return TRUE;
+               if (ready_to_create_device(modem) == TRUE)
+                       create_device(modem);
+               if (modem->device != NULL)
+                       cm_get_properties(modem);
        } else if (g_str_equal(key, "Interfaces") == TRUE) {
                modem->interfaces = extract_interfaces(&value);
 
@@ -491,6 +691,16 @@ static gboolean modem_changed(DBusConnection *connection, DBusMessage *message,
                                return TRUE;
                        }
                }
+
+               if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+                       if (ready_to_create_device(modem) == TRUE)
+                               create_device(modem);
+                       if (modem->device != NULL)
+                               cm_get_properties(modem);
+               } else {
+                       if (modem->device != NULL)
+                               destroy_device(modem);
+               }
        } else if (g_str_equal(key, "Serial") == TRUE) {
                char *serial;
 
@@ -500,6 +710,13 @@ 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_CM) == TRUE) {
+                       if (ready_to_create_device(modem) == TRUE)
+                               create_device(modem);
+                       if (modem->device != NULL)
+                               cm_get_properties(modem);
+               }
        }
 
        return TRUE;
@@ -564,10 +781,16 @@ static void add_modem(const char *path, DBusMessageIter *prop)
                dbus_message_iter_next(prop);
        }
 
-       if (modem->powered == FALSE)
+       if (modem->powered == FALSE) {
                modem_set_powered(modem);
-       else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE)
+       } else if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
                sim_get_properties(modem);
+       } else if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+               if (ready_to_create_device(modem) == TRUE)
+                       create_device(modem);
+               if (modem->device != NULL)
+                       cm_get_properties(modem);
+       }
 }
 
 static void remove_modem(gpointer data)
@@ -582,6 +805,9 @@ static void remove_modem(gpointer data)
        if (modem->call_get_properties != NULL)
                dbus_pending_call_cancel(modem->call_get_properties);
 
+       if (modem->device != NULL)
+               destroy_device(modem);
+
        g_free(modem->serial);
        g_free(modem->imsi);
        g_free(modem->path);
@@ -765,26 +991,34 @@ static struct connman_network_driver network_driver = {
 
 static int modem_probe(struct connman_device *device)
 {
-       DBG("device %p", device);
+       struct modem_data *modem = connman_device_get_data(device);
+
+       DBG("%s device %p", modem->path, device);
 
        return 0;
 }
 
 static void modem_remove(struct connman_device *device)
 {
-       DBG("device %p", device);
+       struct modem_data *modem = connman_device_get_data(device);
+
+       DBG("%s device %p", modem->path, device);
 }
 
 static int modem_enable(struct connman_device *device)
 {
-       DBG("device %p", device);
+       struct modem_data *modem = connman_device_get_data(device);
+
+       DBG("%s device %p", modem->path, device);
 
        return 0;
 }
 
 static int modem_disable(struct connman_device *device)
 {
-       DBG("device %p", device);
+       struct modem_data *modem = connman_device_get_data(device);
+
+       DBG("%s device %p", modem->path, device);
 
        return 0;
 }