From: Daniel Wagner <[email protected]>

---
 plugins/elect.c |   81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 80 insertions(+), 1 deletions(-)

diff --git a/plugins/elect.c b/plugins/elect.c
index 7528653..8cf8522 100644
--- a/plugins/elect.c
+++ b/plugins/elect.c
@@ -34,18 +34,25 @@
 #include <connman/dbus.h>
 
 #define ELECT_SERVICE                  "org.ofono.elect"
+#define ELECT_MANAGER_INTERFACE                ELECT_SERVICE ".Manager"
+
+#define DEVICE_ADDED                   "DeviceAdded"
+#define DEVICE_REMOVED                 "DeviceRemoved"
 
 static DBusConnection *connection;
 
 static GHashTable *elect_devices = NULL;
 
 struct elect_data {
+       char *path;
 };
 
 static void device_destroy(gpointer data)
 {
        struct elect_data *info = data;
 
+       g_free(info->path);
+
        g_free(info);
 }
 
@@ -119,6 +126,62 @@ static struct connman_device_driver elect_driver = {
        .disable        = elect_disable,
 };
 
+static void add_device(const char *path, DBusMessageIter *properties)
+{
+       struct elect_data *info;
+
+       info = g_hash_table_lookup(elect_devices, path);
+       if (info != NULL)
+               return;
+
+       info = g_try_new0(struct elect_data, 1);
+       if (info == NULL)
+               return;
+
+       info->path = g_strdup(path);
+
+       g_hash_table_insert(elect_devices, g_strdup(path), info);
+}
+
+static gboolean device_added(DBusConnection *connection, DBusMessage *message,
+                               void *user_data)
+{
+       DBusMessageIter iter, properties;
+       const char *path;
+
+       DBG("");
+
+       if (dbus_message_iter_init(message, &iter) == FALSE)
+               return TRUE;
+
+       dbus_message_iter_get_basic(&iter, &path);
+
+       dbus_message_iter_next(&iter);
+       dbus_message_iter_recurse(&iter, &properties);
+
+       add_device(path, &properties);
+
+       return TRUE;
+}
+
+static void remove_device(DBusConnection *connection, const char *path)
+{
+       DBG("path %s", path);
+
+       g_hash_table_remove(elect_devices, path);
+}
+
+static gboolean device_removed(DBusConnection *connection, DBusMessage 
*message,
+                               void *user_data)
+{
+       const char *path;
+
+       dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
+                               DBUS_TYPE_INVALID);
+       remove_device(connection, path);
+       return TRUE;
+}
+
 static void elect_connect(DBusConnection *connection, void *user_data)
 {
        DBG("connection %p", connection);
@@ -136,6 +199,8 @@ static void elect_disconnect(DBusConnection *connection, 
void *user_data)
 }
 
 static guint watch;
+static guint added_watch;
+static guint removed_watch;
 
 static int elect_init(void)
 {
@@ -148,7 +213,17 @@ static int elect_init(void)
        watch = g_dbus_add_service_watch(connection, ELECT_SERVICE,
                        elect_connect, elect_disconnect, NULL, NULL);
 
-       if (watch == 0) {
+       added_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+                                               ELECT_MANAGER_INTERFACE,
+                                               DEVICE_ADDED, device_added,
+                                               NULL, NULL);
+
+       removed_watch = g_dbus_add_signal_watch(connection, NULL, NULL,
+                                               ELECT_MANAGER_INTERFACE,
+                                               DEVICE_REMOVED, device_removed,
+                                               NULL, NULL);
+
+       if (watch == 0 || added_watch == 0 || removed_watch == 0) {
                err = -EIO;
                goto remove;
        }
@@ -167,6 +242,8 @@ static int elect_init(void)
 
 remove:
        g_dbus_remove_watch(connection, watch);
+       g_dbus_remove_watch(connection, added_watch);
+       g_dbus_remove_watch(connection, removed_watch);
 
        dbus_connection_unref(connection);
 
@@ -176,6 +253,8 @@ remove:
 static void elect_exit(void)
 {
        g_dbus_remove_watch(connection, watch);
+       g_dbus_remove_watch(connection, added_watch);
+       g_dbus_remove_watch(connection, removed_watch);
 
        connman_device_driver_unregister(&elect_driver);
        connman_network_driver_unregister(&network_driver);
-- 
1.7.8.110.g4cb5d1

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

Reply via email to