Disconnect service on removal if still connected
[connman] / src / profile.c
index e5aca9f..8984872 100644 (file)
 
 #include "connman.h"
 
-#define PROFILE_DEFAULT  "/profile/default"
-
-struct connman_group {
-       char *path;
-       char *type;
-       char *name;
-       GSList *networks;
-};
-
-static GHashTable *groups = NULL;
+#define PROFILE_DEFAULT_IDENT  "default"
 
 static DBusConnection *connection = NULL;
 
-static DBusMessage *get_properties(DBusConnection *conn,
-                                       DBusMessage *msg, void *data)
+const char *__connman_profile_active_ident(void)
 {
-       struct connman_group *group = data;
-       DBusMessage *reply;
-       DBusMessageIter array, dict;
-
-       DBG("conn %p", conn);
-
-       reply = dbus_message_new_method_return(msg);
-       if (reply == NULL)
-               return NULL;
-
-       dbus_message_iter_init_append(reply, &array);
-
-       dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
-                       DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
-                       DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
-                       DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
-
-       if (group->type != NULL)
-               connman_dbus_dict_append_variant(&dict, "Type",
-                                       DBUS_TYPE_STRING, &group->type);
-
-       if (group->name != NULL)
-               connman_dbus_dict_append_variant(&dict, "Name",
-                                       DBUS_TYPE_STRING, &group->name);
-
-       dbus_message_iter_close_container(&array, &dict);
+       DBG("");
 
-       return reply;
+       return PROFILE_DEFAULT_IDENT;
 }
 
-static GDBusMethodTable service_methods[] = {
-       { "GetProperties", "", "a{sv}", get_properties },
-       { },
-};
-
-static void free_group(gpointer data)
+const char *__connman_profile_active_path(void)
 {
-       struct connman_group *group = data;
-
-       DBG("group %p", group);
-
-       g_dbus_unregister_interface(connection, group->path,
-                                               CONNMAN_SERVICE_INTERFACE);
+       DBG("");
 
-       g_free(group->name);
-       g_free(group->type);
-       g_free(group->path);
-       g_free(group);
+       return "/profile/" PROFILE_DEFAULT_IDENT;
 }
 
-static struct connman_group *lookup_group(const char *name)
+static void append_services(DBusMessageIter *entry)
 {
-       struct connman_group *group;
+       DBusMessageIter value, iter;
+       const char *key = "Services";
 
-       DBG("name %s", name);
+       dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
 
-       if (name == NULL)
-               return NULL;
+       dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
+               DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
+                                                               &value);
 
-       group = g_hash_table_lookup(groups, name);
-       if (group != NULL)
-               goto done;
+       dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
+                               DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
+       __connman_service_list(&iter);
+       dbus_message_iter_close_container(&value, &iter);
 
-       group = g_try_new0(struct connman_group, 1);
-       if (group == NULL)
-               return NULL;
+       dbus_message_iter_close_container(entry, &value);
+}
 
-       group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
-       group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
+void __connman_profile_changed(void)
+{
+       const char *path = __connman_profile_active_path();
+       DBusMessage *signal;
+       DBusMessageIter entry;
 
-       g_hash_table_insert(groups, g_strdup(name), group);
+       signal = dbus_message_new_signal(path,
+                               CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
+       if (signal == NULL)
+               return;
 
-       g_dbus_register_interface(connection, group->path,
-                                               CONNMAN_SERVICE_INTERFACE,
-                                               service_methods,
-                                               NULL, NULL, group, NULL);
+       dbus_message_iter_init_append(signal, &entry);
+       append_services(&entry);
+       g_dbus_send_message(connection, signal);
 
-done:
-       DBG("group %p", group);
+       signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
+                               CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
+       if (signal == NULL)
+               return;
 
-       return group;
+       dbus_message_iter_init_append(signal, &entry);
+       append_services(&entry);
+       g_dbus_send_message(connection, signal);
 }
 
 int __connman_profile_add_device(struct connman_device *device)
 {
-       struct connman_group *group;
-       char *name;
+       struct connman_service *service;
 
        DBG("device %p", device);
 
-       name = g_strdup_printf("device%d", connman_device_get_index(device));
-       group = lookup_group(name);
-       g_free(name);
-
-       if (group == NULL)
+       service = __connman_service_create_from_device(device);
+       if (service == NULL)
                return -EINVAL;
 
-       group->type = g_strdup(__connman_device_get_type(device));
-       group->name = g_strdup(connman_device_get_string(device, "Name"));
-
        return 0;
 }
 
 int __connman_profile_remove_device(struct connman_device *device)
 {
-       struct connman_group *group;
-       char *name;
+       struct connman_service *service;
 
        DBG("device %p", device);
 
-       name = g_strdup_printf("device%d", connman_device_get_index(device));
-       group = lookup_group(name);
-       g_free(name);
-
-       if (group == NULL)
+       service = __connman_service_lookup_from_device(device);
+       if (service == NULL)
                return -EINVAL;
 
+       __connman_service_put(service);
+
        return 0;
 }
 
 int __connman_profile_add_network(struct connman_network *network)
 {
-       struct connman_group *group;
+       struct connman_service *service;
 
        DBG("network %p", network);
 
-       group = lookup_group(__connman_network_get_group(network));
-       if (group == NULL)
+       service = __connman_service_create_from_network(network);
+       if (service == NULL)
                return -EINVAL;
 
-       g_free(group->type);
-       g_free(group->name);
-
-       group->type = g_strdup(__connman_network_get_type(network));
-       group->name = g_strdup(connman_network_get_string(network, "Name"));
-
        return 0;
 }
 
 int __connman_profile_remove_network(struct connman_network *network)
 {
-       struct connman_group *group;
+       struct connman_service *service;
 
        DBG("network %p", network);
 
-       group = lookup_group(__connman_network_get_group(network));
-       if (group == NULL)
+       service = __connman_service_lookup_from_network(network);
+       if (service == NULL)
                return -EINVAL;
 
-       return 0;
-}
-
-const char *__connman_profile_active(void)
-{
-       DBG("");
+       __connman_service_put(service);
 
-       return PROFILE_DEFAULT;
+       return 0;
 }
 
 void __connman_profile_list(DBusMessageIter *iter)
 {
-       const char *path = __connman_profile_active();
+       const char *path = __connman_profile_active_path();
 
        DBG("");
 
        dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
 }
 
-static void append_path(gpointer key, gpointer value, gpointer user_data)
-{
-       struct connman_group *group = value;
-       DBusMessageIter *iter = user_data;
-
-       dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
-                                                       &group->path);
-}
-
-void __connman_profile_list_services(DBusMessageIter *iter)
-{
-       DBG("");
-
-       g_hash_table_foreach(groups, append_path, iter);
-}
-
-static void append_services(DBusMessageIter *dict)
-{
-       DBusMessageIter entry, value, iter;
-       const char *key = "Services";
-
-       dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
-                                                               NULL, &entry);
-
-       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
-
-       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
-               DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
-                                                               &value);
-
-       dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
-                               DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
-       __connman_profile_list_services(&iter);
-       dbus_message_iter_close_container(&value, &iter);
-
-       dbus_message_iter_close_container(&entry, &value);
-
-       dbus_message_iter_close_container(dict, &entry);
-}
-
-static DBusMessage *profile_properties(DBusConnection *conn,
+static DBusMessage *get_properties(DBusConnection *conn,
                                        DBusMessage *msg, void *data)
 {
        const char *name = "Default";
        DBusMessage *reply;
-       DBusMessageIter array, dict;
+       DBusMessageIter array, dict, entry;
 
        DBG("conn %p", conn);
 
@@ -275,7 +178,10 @@ static DBusMessage *profile_properties(DBusConnection *conn,
        connman_dbus_dict_append_variant(&dict, "Name",
                                                DBUS_TYPE_STRING, &name);
 
-       append_services(&dict);
+       dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
+                                                               NULL, &entry);
+       append_services(&entry);
+       dbus_message_iter_close_container(&dict, &entry);
 
        dbus_message_iter_close_container(&array, &dict);
 
@@ -283,39 +189,42 @@ static DBusMessage *profile_properties(DBusConnection *conn,
 }
 
 static GDBusMethodTable profile_methods[] = {
-       { "GetProperties", "", "a{sv}", profile_properties },
+       { "GetProperties", "", "a{sv}", get_properties },
+       { },
+};
+
+static GDBusSignalTable profile_signals[] = {
+       { "PropertyChanged", "sv" },
        { },
 };
 
 int __connman_profile_init(DBusConnection *conn)
 {
+       const char *path = __connman_profile_active_path();
+
        DBG("conn %p", conn);
 
        connection = dbus_connection_ref(conn);
        if (connection == NULL)
                return -1;
 
-       groups = g_hash_table_new_full(g_str_hash, g_str_equal,
-                                                       g_free, free_group);
-
-       g_dbus_register_interface(connection, PROFILE_DEFAULT,
-                                               CONNMAN_PROFILE_INTERFACE,
-                                               profile_methods,
-                                               NULL, NULL, NULL, NULL);
+       g_dbus_register_interface(connection, path,
+                                       CONNMAN_PROFILE_INTERFACE,
+                                       profile_methods, profile_signals,
+                                                       NULL, NULL, NULL);
 
        return 0;
 }
 
 void __connman_profile_cleanup(void)
 {
+       const char *path = __connman_profile_active_path();
+
        DBG("conn %p", connection);
 
-       g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
+       g_dbus_unregister_interface(connection, path,
                                                CONNMAN_PROFILE_INTERFACE);
 
-       g_hash_table_destroy(groups);
-       groups = NULL;
-
        if (connection == NULL)
                return;