Hi Daniel,

On 22/11/2011 14:24, Daniel Wagner wrote:
From: Daniel Wagner<daniel.wag...@bmw-carit.de>

Create a device object when the ConnectionManager interface
has been added.  Destroy the device object when
either the modem disappears or the ConnectionManager goes away.
---
  plugins/ofono.c |   94 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
  1 files changed, 90 insertions(+), 4 deletions(-)

diff --git a/plugins/ofono.c b/plugins/ofono.c
index 75a62cb..eac1c32 100644
--- a/plugins/ofono.c
+++ b/plugins/ofono.c
@@ -72,6 +72,8 @@ static GHashTable *modem_hash;
  struct modem_data {
        char *path;

+       struct connman_device *device;
+
        /* Modem Interface */
        char *serial;
        connman_bool_t powered;
@@ -362,6 +364,66 @@ static uint8_t extract_interfaces(DBusMessageIter *array)
        return interfaces;
  }

+static void create_device(struct modem_data *modem)
+{
+       struct connman_device *device;
+       char *ident;
+
+       DBG("%s", modem->path);
+
+       if (modem->device != NULL) {
+               DBG("Modem is already registered at core, bug?");
+               return;
+       }
+
+       if (has_interface(modem->interfaces, OFONO_API_SIM) == TRUE) {
+               ident = modem->imsi;
+       } else {
+               /*
+                * This modem doesn't have a SIM interface for some reason.
+                *
+                * This here is just a workaround for the time
+                * being. We are going to use the serial number in the
+                * hope it is unique enough.
+                */
+               connman_info("Modem %s has no SIM interface", modem->path);
+
+               ident = modem->serial;
+       }
+
+       device = connman_device_create(ident, CONNMAN_DEVICE_TYPE_CELLULAR);
+       if (device == NULL)
+               return;
+
+       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);
+               return;
+       }
+
+       modem->device = device;
+}
+
+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)
@@ -436,6 +498,8 @@ static gboolean sim_changed(DBusConnection *connection, 
DBusMessage *message,

                if (modem->online == FALSE)
                        modem_set_online(modem);
+               else if (modem->device == NULL)
+                       create_device(modem);
        }

        return TRUE;
@@ -468,6 +532,9 @@ static void sim_properties_reply(struct modem_data *modem,
                                break;
                        }

+                       if (modem->device == NULL)
+                               create_device(modem);
+
                        return;
                }

@@ -529,6 +596,14 @@ static gboolean modem_changed(DBusConnection *connection, 
DBusMessage *message,
                                return TRUE;
                        }
                }
+
+               if (has_interface(modem->interfaces, OFONO_API_CM) == TRUE) {
+                       if (modem->device == NULL)
+                               create_device(modem);

In the case your modem has no SIM interface, shouldn't you test (modem->serial != NULL || modem->imsi!= NULL) ? It is possible that modem serial is sent after the ConnectionManager interface is created. Thus, you would try to create_device() too early with a Serial equal to NULL.

+               } else {
+                       if (modem->device != NULL)
+                               destroy_device(modem);
+               }
        } else if (g_str_equal(key, "Serial") == TRUE) {
                char *serial;

Here you should do:
if (modem->device == NULL && has_interface(modem->interfaces, OFONO_API_SIM) == FALSE)
        create_device(modem);

As devinfo and sim interface are created at the same time into oFono, if you receive modem Serial and no SIM interface exists. You can use the serial as unique identifier of your device.

This is how I would mange it for CDMA modems case but maybe it is not the case for dunmodem :)


@@ -620,6 +695,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);
@@ -803,26 +881,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;
  }

Kind regards,
Guillaume
_______________________________________________
connman mailing list
connman@connman.net
http://lists.connman.net/listinfo/connman

Reply via email to