Add support for ConnectedTechnologies property
authorMarcel Holtmann <marcel@holtmann.org>
Sun, 19 Jul 2009 17:57:50 +0000 (19:57 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Sun, 19 Jul 2009 17:57:50 +0000 (19:57 +0200)
doc/manager-api.txt
src/connman.h
src/device.c
src/manager.c
src/notifier.c

index 6df87de..509817b 100644 (file)
@@ -135,6 +135,11 @@ Properties string State [readonly]
                        The list of enabled technologies. The strings
                        are the same as the ones from the service types.
 
+               array{string} ConnectedTechnologies [readonly]
+
+                       The list of connected technologies. The strings
+                       are the same as the ones from the service type.
+
                boolean OfflineMode [readwrite]
 
                        The offline mode indicates the global setting for
index f9a7b27..407bbe7 100644 (file)
@@ -286,12 +286,16 @@ void __connman_service_auto_connect(void);
 int __connman_notifier_init(void);
 void __connman_notifier_cleanup(void);
 
-void __connman_notifier_list(gboolean powered, DBusMessageIter *iter);
+void __connman_notifier_list_registered(DBusMessageIter *iter);
+void __connman_notifier_list_enabled(DBusMessageIter *iter);
+void __connman_notifier_list_connected(DBusMessageIter *iter);
 
 void __connman_notifier_register(enum connman_service_type type);
 void __connman_notifier_unregister(enum connman_service_type type);
 void __connman_notifier_enable(enum connman_service_type type);
 void __connman_notifier_disable(enum connman_service_type type);
+void __connman_notifier_connect(enum connman_service_type type);
+void __connman_notifier_disconnect(enum connman_service_type type);
 void __connman_notifier_offline_mode(connman_bool_t enabled);
 
 #include <connman/rtnl.h>
index 807c5c0..d95c027 100644 (file)
@@ -1733,12 +1733,22 @@ int __connman_device_set_offlinemode(connman_bool_t offlinemode)
 
 void __connman_device_increase_connections(struct connman_device *device)
 {
+       enum connman_service_type type;
+
        device->connections++;
+
+       type = __connman_device_get_service_type(device);
+       __connman_notifier_connect(type);
 }
 
 void __connman_device_decrease_connections(struct connman_device *device)
 {
+       enum connman_service_type type;
+
        device->connections--;
+
+       type = __connman_device_get_service_type(device);
+       __connman_notifier_disconnect(type);
 }
 
 /**
index 999c7ad..580fe52 100644 (file)
@@ -125,15 +125,58 @@ static void append_connections(DBusMessageIter *dict)
        dbus_message_iter_close_container(dict, &entry);
 }
 
-static void append_technologies(gboolean powered, DBusMessageIter *dict)
+static void append_available_technologies(DBusMessageIter *dict)
 {
        DBusMessageIter entry, value, iter;
-       const char *key;
+       const char *key = "AvailableTechnologies";
 
-       if (powered)
-               key = "EnabledTechnologies";
-       else
-               key = "AvailableTechnologies";
+       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_STRING_AS_STRING,
+                                                               &value);
+
+       dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
+                                       DBUS_TYPE_STRING_AS_STRING, &iter);
+       __connman_notifier_list_registered(&iter);
+       dbus_message_iter_close_container(&value, &iter);
+
+       dbus_message_iter_close_container(&entry, &value);
+
+       dbus_message_iter_close_container(dict, &entry);
+}
+
+static void append_enabled_technologies(DBusMessageIter *dict)
+{
+       DBusMessageIter entry, value, iter;
+       const char *key = "EnabledTechnologies";
+
+       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_STRING_AS_STRING,
+                                                               &value);
+
+       dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
+                                       DBUS_TYPE_STRING_AS_STRING, &iter);
+       __connman_notifier_list_enabled(&iter);
+       dbus_message_iter_close_container(&value, &iter);
+
+       dbus_message_iter_close_container(&entry, &value);
+
+       dbus_message_iter_close_container(dict, &entry);
+}
+
+static void append_connected_technologies(DBusMessageIter *dict)
+{
+       DBusMessageIter entry, value, iter;
+       const char *key = "ConnectedTechnologies";
 
        dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
                                                                NULL, &entry);
@@ -146,7 +189,7 @@ static void append_technologies(gboolean powered, DBusMessageIter *dict)
 
        dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
                                        DBUS_TYPE_STRING_AS_STRING, &iter);
-       __connman_notifier_list(powered, &iter);
+       __connman_notifier_list_connected(&iter);
        dbus_message_iter_close_container(&value, &iter);
 
        dbus_message_iter_close_container(&entry, &value);
@@ -200,8 +243,9 @@ static DBusMessage *get_properties(DBusConnection *conn,
        connman_dbus_dict_append_variant(&dict, "OfflineMode",
                                DBUS_TYPE_BOOLEAN, &global_offlinemode);
 
-       append_technologies(FALSE, &dict);
-       append_technologies(TRUE, &dict);
+       append_available_technologies(&dict);
+       append_enabled_technologies(&dict);
+       append_connected_technologies(&dict);
 
        dbus_message_iter_close_container(&array, &dict);
 
index ced0f4c..822cd5c 100644 (file)
@@ -70,6 +70,80 @@ void connman_notifier_unregister(struct connman_notifier *notifier)
        notifier_list = g_slist_remove(notifier_list, notifier);
 }
 
+static const char *type2string(enum connman_service_type type)
+{
+       switch (type) {
+       case CONNMAN_SERVICE_TYPE_UNKNOWN:
+               break;
+       case CONNMAN_SERVICE_TYPE_ETHERNET:
+               return "ethernet";
+       case CONNMAN_SERVICE_TYPE_WIFI:
+               return "wifi";
+       case CONNMAN_SERVICE_TYPE_WIMAX:
+               return "wimax";
+       case CONNMAN_SERVICE_TYPE_BLUETOOTH:
+               return "bluetooth";
+       case CONNMAN_SERVICE_TYPE_CELLULAR:
+               return "cellular";
+       }
+
+       return NULL;
+}
+
+#define MAX_TECHNOLOGIES 10
+
+static volatile gint registered[MAX_TECHNOLOGIES];
+static volatile gint enabled[MAX_TECHNOLOGIES];
+static volatile gint connected[MAX_TECHNOLOGIES];
+
+void __connman_notifier_list_registered(DBusMessageIter *iter)
+{
+       int i;
+
+       for (i = 0; i < 10; i++) {
+               const char *type = type2string(i);
+
+               if (type == NULL)
+                       continue;
+
+               if (g_atomic_int_get(&registered[i]) > 0)
+                       dbus_message_iter_append_basic(iter,
+                                               DBUS_TYPE_STRING, &type);
+       }
+}
+
+void __connman_notifier_list_enabled(DBusMessageIter *iter)
+{
+       int i;
+
+       for (i = 0; i < 10; i++) {
+               const char *type = type2string(i);
+
+               if (type == NULL)
+                       continue;
+
+               if (g_atomic_int_get(&enabled[i]) > 0)
+                       dbus_message_iter_append_basic(iter,
+                                               DBUS_TYPE_STRING, &type);
+       }
+}
+
+void __connman_notifier_list_connected(DBusMessageIter *iter)
+{
+       int i;
+
+       for (i = 0; i < 10; i++) {
+               const char *type = type2string(i);
+
+               if (type == NULL)
+                       continue;
+
+               if (g_atomic_int_get(&connected[i]) > 0)
+                       dbus_message_iter_append_basic(iter,
+                                               DBUS_TYPE_STRING, &type);
+       }
+}
+
 static void technology_enabled(enum connman_device_type type,
                                                connman_bool_t enabled)
 {
@@ -95,7 +169,7 @@ static void technology_enabled(enum connman_device_type type,
 
        dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
                                        DBUS_TYPE_STRING_AS_STRING, &iter);
-       __connman_notifier_list(TRUE, &iter);
+       __connman_notifier_list_registered(&iter);
        dbus_message_iter_close_container(&value, &iter);
 
        dbus_message_iter_close_container(&entry, &value);
@@ -135,7 +209,7 @@ static void technology_registered(enum connman_service_type type,
 
        dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
                                        DBUS_TYPE_STRING_AS_STRING, &iter);
-       __connman_notifier_list(FALSE, &iter);
+       __connman_notifier_list_enabled(&iter);
        dbus_message_iter_close_container(&value, &iter);
 
        dbus_message_iter_close_container(&entry, &value);
@@ -143,49 +217,36 @@ static void technology_registered(enum connman_service_type type,
        g_dbus_send_message(connection, signal);
 }
 
-static const char *type2string(enum connman_service_type type)
+static void technology_connected(enum connman_service_type type,
+                                               connman_bool_t connected)
 {
-       switch (type) {
-       case CONNMAN_SERVICE_TYPE_UNKNOWN:
-               break;
-       case CONNMAN_SERVICE_TYPE_ETHERNET:
-               return "ethernet";
-       case CONNMAN_SERVICE_TYPE_WIFI:
-               return "wifi";
-       case CONNMAN_SERVICE_TYPE_WIMAX:
-               return "wimax";
-       case CONNMAN_SERVICE_TYPE_BLUETOOTH:
-               return "bluetooth";
-       case CONNMAN_SERVICE_TYPE_CELLULAR:
-               return "cellular";
-       }
+       DBusMessage *signal;
+       DBusMessageIter entry, value, iter;
+       const char *key = "ConnectedTechnologies";
 
-       return NULL;
-}
+       DBG("type %d connected %d", type, connected);
 
-static volatile gint registered[10];
-static volatile gint enabled[10];
+       signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
+                               CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
+       if (signal == NULL)
+               return;
 
-void __connman_notifier_list(gboolean powered, DBusMessageIter *iter)
-{
-       int i;
+       dbus_message_iter_init_append(signal, &entry);
 
-       for (i = 0; i < 10; i++) {
-               const char *type = type2string(i);
-               gint count;
+       dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
 
-               if (type == NULL)
-                       continue;
+       dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
+                       DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
+                                                               &value);
 
-               if (powered == TRUE)
-                       count = g_atomic_int_get(&enabled[i]);
-               else
-                       count = g_atomic_int_get(&registered[i]);
+       dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
+                                       DBUS_TYPE_STRING_AS_STRING, &iter);
+       __connman_notifier_list_connected(&iter);
+       dbus_message_iter_close_container(&value, &iter);
 
-               if (count > 0)
-                       dbus_message_iter_append_basic(iter,
-                                               DBUS_TYPE_STRING, &type);
-       }
+       dbus_message_iter_close_container(&entry, &value);
+
+       g_dbus_send_message(connection, signal);
 }
 
 void __connman_notifier_register(enum connman_service_type type)
@@ -264,6 +325,44 @@ void __connman_notifier_disable(enum connman_service_type type)
                technology_enabled(type, FALSE);
 }
 
+void __connman_notifier_connect(enum connman_service_type type)
+{
+       DBG("type %d", type);
+
+       switch (type) {
+       case CONNMAN_SERVICE_TYPE_UNKNOWN:
+               return;
+       case CONNMAN_SERVICE_TYPE_ETHERNET:
+       case CONNMAN_SERVICE_TYPE_WIFI:
+       case CONNMAN_SERVICE_TYPE_WIMAX:
+       case CONNMAN_SERVICE_TYPE_BLUETOOTH:
+       case CONNMAN_SERVICE_TYPE_CELLULAR:
+               break;
+       }
+
+       if (g_atomic_int_exchange_and_add(&connected[type], 1) == 0)
+               technology_connected(type, TRUE);
+}
+
+void __connman_notifier_disconnect(enum connman_service_type type)
+{
+       DBG("type %d", type);
+
+       switch (type) {
+       case CONNMAN_SERVICE_TYPE_UNKNOWN:
+               return;
+       case CONNMAN_SERVICE_TYPE_ETHERNET:
+       case CONNMAN_SERVICE_TYPE_WIFI:
+       case CONNMAN_SERVICE_TYPE_WIMAX:
+       case CONNMAN_SERVICE_TYPE_BLUETOOTH:
+       case CONNMAN_SERVICE_TYPE_CELLULAR:
+               break;
+       }
+
+       if (g_atomic_int_dec_and_test(&connected[type]) == TRUE)
+               technology_connected(type, FALSE);
+}
+
 void __connman_notifier_offline_mode(connman_bool_t enabled)
 {
        GSList *list;