Add method to retrieve sorted list of tuples for services
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 13 May 2010 15:09:08 +0000 (17:09 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 13 May 2010 15:09:08 +0000 (17:09 +0200)
Makefile.am
doc/manager-api.txt
src/connman.h
src/manager.c
src/service.c
test/get-services [new file with mode: 0755]

index 4797b8788185669f84b767bcd7b3a05ef20f39e1..0f0f9448dc6183469ee6b17bdb80c8cad189387e 100644 (file)
@@ -143,7 +143,8 @@ test_scripts = test/get-state test/list-profiles test/list-services \
                test/test-manager test/test-connman test/monitor-connman \
                test/connect-vpn test/disconnect-vpn test/list-providers \
                test/monitor-manager test/test-counter test/set-ip-method \
-               test/set-nameservers test/set-domains test/find-service
+               test/set-nameservers test/set-domains test/find-service \
+               test/get-services
 
 if TEST
 testdir = $(pkglibdir)/test
index 1aeb0557951622c3317422357d56335f5a37f8ad..dff2e149576d8f04a1b097e0c810996e29c018d5 100644 (file)
@@ -69,6 +69,16 @@ Methods              dict GetProperties()
 
                        Possible Errors: [service].Error.InvalidArguments
 
+               arracy{object,dict} GetServices()
+
+                       Returns a sorted list of tuples with service
+                       object path and dictionary of service properties.
+
+                       This list will not contain sensitive information
+                       like passphrases etc.
+
+                       Possible Errors: [service].Error.InvalidArguments
+
                object LookupService(string pattern)
 
                        Lookup a service matching the specific pattern.
index 0ddb3ade8026a729718133467eef71c8941115e9..ad9faf4eb5caa7fe7130049505c6375db4b93334 100644 (file)
@@ -384,6 +384,7 @@ int __connman_service_init(void);
 void __connman_service_cleanup(void);
 
 void __connman_service_list(DBusMessageIter *iter, void *user_data);
+void __connman_service_list_struct(DBusMessageIter *iter);
 const char *__connman_service_default(void);
 
 void __connman_service_put(struct connman_service *service);
index b806ba29ae33a7f770038058940b4dcc5892457c..bfa2eb64eeab253a45c66fb71a665280b6fcf792 100644 (file)
@@ -413,6 +413,35 @@ static DBusMessage *disable_technology(DBusConnection *conn,
        return NULL;
 }
 
+static DBusMessage *get_services(DBusConnection *conn,
+                                       DBusMessage *msg, void *data)
+{
+       DBusMessage *reply;
+       DBusMessageIter iter, array;
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_iter_init_append(reply, &iter);
+
+       dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
+                       DBUS_STRUCT_BEGIN_CHAR_AS_STRING
+                       DBUS_TYPE_OBJECT_PATH_AS_STRING
+                       DBUS_TYPE_ARRAY_AS_STRING
+                               DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
+                                       DBUS_TYPE_STRING_AS_STRING
+                                       DBUS_TYPE_VARIANT_AS_STRING
+                               DBUS_DICT_ENTRY_END_CHAR_AS_STRING
+                       DBUS_STRUCT_END_CHAR_AS_STRING, &array);
+
+       __connman_service_list_struct(&array);
+
+       dbus_message_iter_close_container(&iter, &array);
+
+       return reply;
+}
+
 static DBusMessage *lookup_service(DBusConnection *conn,
                                        DBusMessage *msg, void *data)
 {
@@ -615,6 +644,7 @@ static GDBusMethodTable manager_methods[] = {
                                                G_DBUS_METHOD_FLAG_ASYNC },
        { "DisableTechnology", "s",     "",      disable_technology,
                                                G_DBUS_METHOD_FLAG_ASYNC },
+       { "GetServices",       "",      "a(oa{sv})", get_services   },
        { "LookupService",     "s",     "o",     lookup_service,    },
        { "ConnectService",    "a{sv}", "o",     connect_service,
                                                G_DBUS_METHOD_FLAG_ASYNC },
index c33381860b01a4b4c373abaa2571c77ade09d612..60262b610a7a29deaef857d4b0577467f4e49ef3 100644 (file)
@@ -669,69 +669,56 @@ static void domain_configuration_changed(struct connman_service *service)
                                DBUS_TYPE_STRING, append_domainconfig, service);
 }
 
-static DBusMessage *get_properties(DBusConnection *conn,
-                                       DBusMessage *msg, void *user_data)
+static void append_properties(DBusMessageIter *dict, dbus_bool_t limited,
+                                       struct connman_service *service)
 {
-       struct connman_service *service = user_data;
-       DBusMessage *reply;
-       DBusMessageIter array, dict;
        dbus_bool_t required;
        const char *str;
 
-       DBG("service %p", service);
-
-       reply = dbus_message_new_method_return(msg);
-       if (reply == NULL)
-               return NULL;
-
-       dbus_message_iter_init_append(reply, &array);
-
-       connman_dbus_dict_open(&array, &dict);
-
        str = __connman_service_type2string(service->type);
        if (str != NULL)
-               connman_dbus_dict_append_basic(&dict, "Type",
+               connman_dbus_dict_append_basic(dict, "Type",
                                                DBUS_TYPE_STRING, &str);
 
        str = mode2string(service->mode);
        if (str != NULL)
-               connman_dbus_dict_append_basic(&dict, "Mode",
+               connman_dbus_dict_append_basic(dict, "Mode",
                                                DBUS_TYPE_STRING, &str);
 
        str = security2string(service->security);
        if (str != NULL)
-               connman_dbus_dict_append_basic(&dict, "Security",
+               connman_dbus_dict_append_basic(dict, "Security",
                                                DBUS_TYPE_STRING, &str);
 
        str = state2string(service->state);
        if (str != NULL)
-               connman_dbus_dict_append_basic(&dict, "State",
+               connman_dbus_dict_append_basic(dict, "State",
                                                DBUS_TYPE_STRING, &str);
 
        str = error2string(service->error);
        if (str != NULL)
-               connman_dbus_dict_append_basic(&dict, "Error",
+               connman_dbus_dict_append_basic(dict, "Error",
                                                DBUS_TYPE_STRING, &str);
 
        if (service->strength > 0)
-               connman_dbus_dict_append_basic(&dict, "Strength",
+               connman_dbus_dict_append_basic(dict, "Strength",
                                        DBUS_TYPE_BYTE, &service->strength);
 
-       connman_dbus_dict_append_basic(&dict, "Favorite",
+       connman_dbus_dict_append_basic(dict, "Favorite",
                                        DBUS_TYPE_BOOLEAN, &service->favorite);
 
-       connman_dbus_dict_append_basic(&dict, "Immutable",
+       connman_dbus_dict_append_basic(dict, "Immutable",
                                        DBUS_TYPE_BOOLEAN, &service->immutable);
 
        if (service->favorite == TRUE)
-               connman_dbus_dict_append_basic(&dict, "AutoConnect",
+               connman_dbus_dict_append_basic(dict, "AutoConnect",
                                DBUS_TYPE_BOOLEAN, &service->autoconnect);
        else
-               connman_dbus_dict_append_basic(&dict, "AutoConnect",
+               connman_dbus_dict_append_basic(dict, "AutoConnect",
                                        DBUS_TYPE_BOOLEAN, &service->favorite);
 
        if (service->name != NULL)
-               connman_dbus_dict_append_basic(&dict, "Name",
+               connman_dbus_dict_append_basic(dict, "Name",
                                        DBUS_TYPE_STRING, &service->name);
 
        switch (service->type) {
@@ -741,27 +728,27 @@ static DBusMessage *get_properties(DBusConnection *conn,
        case CONNMAN_SERVICE_TYPE_VPN:
                break;
        case CONNMAN_SERVICE_TYPE_CELLULAR:
-               connman_dbus_dict_append_basic(&dict, "Roaming",
+               connman_dbus_dict_append_basic(dict, "Roaming",
                                        DBUS_TYPE_BOOLEAN, &service->roaming);
 
                if (service->mcc != NULL && service->mnc != NULL) {
-                       connman_dbus_dict_append_basic(&dict, "MCC",
+                       connman_dbus_dict_append_basic(dict, "MCC",
                                        DBUS_TYPE_STRING, &service->mcc);
-                       connman_dbus_dict_append_basic(&dict, "MNC",
+                       connman_dbus_dict_append_basic(dict, "MNC",
                                        DBUS_TYPE_STRING, &service->mnc);
                }
 
                if (service->apn != NULL) {
-                       connman_dbus_dict_append_basic(&dict, "APN",
+                       connman_dbus_dict_append_basic(dict, "APN",
                                        DBUS_TYPE_STRING, &service->apn);
 
                        if (service->username != NULL)
-                               connman_dbus_dict_append_basic(&dict,
+                               connman_dbus_dict_append_basic(dict,
                                        "Username", DBUS_TYPE_STRING,
                                                        &service->username);
 
                        if (service->password != NULL)
-                               connman_dbus_dict_append_basic(&dict,
+                               connman_dbus_dict_append_basic(dict,
                                        "Password", DBUS_TYPE_STRING,
                                                        &service->password);
 
@@ -769,16 +756,14 @@ static DBusMessage *get_properties(DBusConnection *conn,
                } else
                        required = TRUE;
 
-               connman_dbus_dict_append_basic(&dict, "SetupRequired",
+               connman_dbus_dict_append_basic(dict, "SetupRequired",
                                                DBUS_TYPE_BOOLEAN, &required);
-               connman_dbus_dict_append_dict(&dict, "Ethernet",
+               connman_dbus_dict_append_dict(dict, "Ethernet",
                                                append_ethernet, service);
                break;
        case CONNMAN_SERVICE_TYPE_WIFI:
-               if (service->passphrase != NULL &&
-                               __connman_security_check_privilege(msg,
-                                       CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
-                       connman_dbus_dict_append_basic(&dict, "Passphrase",
+               if (service->passphrase != NULL && limited == FALSE)
+                       connman_dbus_dict_append_basic(dict, "Passphrase",
                                DBUS_TYPE_STRING, &service->passphrase);
 
                required = FALSE;
@@ -798,36 +783,85 @@ static DBusMessage *get_properties(DBusConnection *conn,
                        break;
                }
 
-               connman_dbus_dict_append_basic(&dict, "PassphraseRequired",
+               connman_dbus_dict_append_basic(dict, "PassphraseRequired",
                                                DBUS_TYPE_BOOLEAN, &required);
                /* fall through */
        case CONNMAN_SERVICE_TYPE_ETHERNET:
        case CONNMAN_SERVICE_TYPE_WIMAX:
        case CONNMAN_SERVICE_TYPE_BLUETOOTH:
-               connman_dbus_dict_append_dict(&dict, "Ethernet",
+               connman_dbus_dict_append_dict(dict, "Ethernet",
                                                append_ethernet, service);
                break;
        }
 
-       connman_dbus_dict_append_dict(&dict, "IPv4", append_ipv4, service);
+       connman_dbus_dict_append_dict(dict, "IPv4", append_ipv4, service);
 
-       connman_dbus_dict_append_dict(&dict, "IPv4.Configuration",
+       connman_dbus_dict_append_dict(dict, "IPv4.Configuration",
                                                append_ipv4config, service);
 
-       connman_dbus_dict_append_array(&dict, "Nameservers",
+       connman_dbus_dict_append_array(dict, "Nameservers",
                                DBUS_TYPE_STRING, append_dns, service);
 
-       connman_dbus_dict_append_array(&dict, "Nameservers.Configuration",
+       connman_dbus_dict_append_array(dict, "Nameservers.Configuration",
                                DBUS_TYPE_STRING, append_dnsconfig, service);
 
-       connman_dbus_dict_append_array(&dict, "Domains",
+       connman_dbus_dict_append_array(dict, "Domains",
                                DBUS_TYPE_STRING, append_domain, service);
 
-       connman_dbus_dict_append_array(&dict, "Domains.Configuration",
+       connman_dbus_dict_append_array(dict, "Domains.Configuration",
                                DBUS_TYPE_STRING, append_domainconfig, service);
 
-       connman_dbus_dict_append_dict(&dict, "Proxy", append_proxy, service);
+       connman_dbus_dict_append_dict(dict, "Proxy", append_proxy, service);
+}
+
+static void append_struct(gpointer value, gpointer user_data)
+{
+       struct connman_service *service = value;
+       DBusMessageIter *iter = user_data;
+       DBusMessageIter entry, dict;
+
+       if (service->path == NULL || service->hidden == TRUE)
+               return;
+
+       dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &entry);
+
+       dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
+                                                       &service->path);
+
+       connman_dbus_dict_open(&entry, &dict);
+       append_properties(&dict, TRUE, service);
+       connman_dbus_dict_close(&entry, &dict);
+
+       dbus_message_iter_close_container(iter, &entry);
+}
+
+void __connman_service_list_struct(DBusMessageIter *iter)
+{
+       g_sequence_foreach(service_list, append_struct, iter);
+}
 
+static DBusMessage *get_properties(DBusConnection *conn,
+                                       DBusMessage *msg, void *user_data)
+{
+       struct connman_service *service = user_data;
+       DBusMessage *reply;
+       DBusMessageIter array, dict;
+       dbus_bool_t limited = TRUE;
+
+       DBG("service %p", service);
+
+       if (__connman_security_check_privilege(msg,
+                               CONNMAN_SECURITY_PRIVILEGE_SECRET) == 0)
+               limited = FALSE;
+
+       reply = dbus_message_new_method_return(msg);
+       if (reply == NULL)
+               return NULL;
+
+       dbus_message_iter_init_append(reply, &array);
+
+       connman_dbus_dict_open(&array, &dict);
+       append_properties(&dict, limited, service);
        connman_dbus_dict_close(&array, &dict);
 
        return reply;
diff --git a/test/get-services b/test/get-services
new file mode 100755 (executable)
index 0000000..1afebc5
--- /dev/null
@@ -0,0 +1,51 @@
+#!/usr/bin/python
+
+import dbus
+
+def extract_values(values):
+       val = "{"
+       for key in values.keys():
+               val += " " + key + "="
+               val += str(values[key])
+       val += " }"
+       return val
+
+def extract_list(list):
+       val = "["
+       for i in list:
+               val += " " + str(i)
+       val += " ]"
+       return val
+
+bus = dbus.SystemBus()
+
+manager = dbus.Interface(bus.get_object('org.moblin.connman', '/'),
+                                       'org.moblin.connman.Manager')
+
+services = manager.GetServices()
+
+for entry in services:
+       path = entry[0]
+       properties = entry[1]
+
+       print "[ %s ]" % (path)
+
+       for key in properties.keys():
+               if key in ["IPv4", "IPv4.Configuration", "Proxy", "Ethernet"]:
+                       val = extract_values(properties[key])
+               elif key in ["Nameservers", "Nameservers.Configuration",
+                                       "Domains", "Domains.Configuration"]:
+                       val = extract_list(properties[key])
+               elif key in ["Favorite", "Immutable", "AutoConnect",
+                               "SetupRequired", "PassphraseRequired"]:
+                       if properties[key] == dbus.Boolean(1):
+                               val = "true"
+                       else:
+                               val = "false"
+               elif key in ["Strength"]:
+                       val = int(properties[key])
+               else:
+                       val = str(properties[key])
+               print "    %s = %s" % (key, val)
+
+       print