diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2010-05-14 15:35:47 +0200 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2010-05-14 15:35:47 +0200 |
commit | 15d47635f6932d807a8f59267de6179a20e14b9b (patch) | |
tree | e0b38559656ea35fb3cae08747d7a4a2547799a6 /client/main.c | |
parent | e44e2d1e1564aa08d39b81312e074a89940c61ac (diff) | |
download | connman-15d47635f6932d807a8f59267de6179a20e14b9b.tar.gz connman-15d47635f6932d807a8f59267de6179a20e14b9b.tar.bz2 connman-15d47635f6932d807a8f59267de6179a20e14b9b.zip |
Add some simple commands to cm utility
Diffstat (limited to 'client/main.c')
-rw-r--r-- | client/main.c | 161 |
1 files changed, 92 insertions, 69 deletions
diff --git a/client/main.c b/client/main.c index 5d8fd410..3d29c168 100644 --- a/client/main.c +++ b/client/main.c @@ -35,7 +35,13 @@ #define CONNMAN_MANAGER_INTERFACE CONNMAN_SERVICE ".Manager" #define CONNMAN_MANAGER_PATH "/" -static DBusMessage *get_properties(DBusConnection *connection) +struct service_data { + const char *path; + const char *name; + dbus_bool_t favorite; +}; + +static DBusMessage *get_services(DBusConnection *connection) { DBusMessage *message, *reply; DBusError error; @@ -43,7 +49,7 @@ static DBusMessage *get_properties(DBusConnection *connection) message = dbus_message_new_method_call(CONNMAN_SERVICE, CONNMAN_MANAGER_PATH, CONNMAN_MANAGER_INTERFACE, - "GetProperties"); + "GetServices"); if (message == NULL) return NULL; @@ -66,18 +72,49 @@ static DBusMessage *get_properties(DBusConnection *connection) return reply; } -static const char *extract_state(DBusMessage *message) +static DBusMessage *lookup_service(DBusConnection *connection, + const char *pattern) { - DBusMessageIter array, dict; + DBusMessage *message, *reply; + DBusError error; + + message = dbus_message_new_method_call(CONNMAN_SERVICE, + CONNMAN_MANAGER_PATH, + CONNMAN_MANAGER_INTERFACE, + "LookupService"); + if (message == NULL) + return NULL; + + dbus_message_append_args(message, DBUS_TYPE_STRING, &pattern, + DBUS_TYPE_INVALID); + + dbus_error_init(&error); + + reply = dbus_connection_send_with_reply_and_block(connection, + message, -1, &error); + if (reply == NULL) { + if (dbus_error_is_set(&error) == TRUE) { + fprintf(stderr, "%s\n", error.message); + dbus_error_free(&error); + } else + fprintf(stderr, "Failed to get properties\n"); + dbus_message_unref(message); + return NULL; + } - dbus_message_iter_init(message, &array); - dbus_message_iter_recurse(&array, &dict); + dbus_message_unref(message); + + return reply; +} - while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) { +static void extract_properties(DBusMessageIter *dict, + struct service_data *service) +{ + while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) { DBusMessageIter entry, value; const char *key; - dbus_message_iter_recurse(&dict, &entry); + dbus_message_iter_recurse(dict, &entry); dbus_message_iter_get_basic(&entry, &key); dbus_message_iter_next(&entry); @@ -87,94 +124,73 @@ static const char *extract_state(DBusMessage *message) //type = dbus_message_iter_get_arg_type(&value); //dbus_message_iter_get_basic(&value, &val); - if (strcmp(key, "State") == 0) { - const char *val; - dbus_message_iter_get_basic(&value, &val); - return val; - } + if (strcmp(key, "Name") == 0) + dbus_message_iter_get_basic(&value, &service->name); + else if (strcmp(key, "Favorite") == 0) + dbus_message_iter_get_basic(&value, &service->favorite); - dbus_message_iter_next(&dict); + dbus_message_iter_next(dict); } - - return NULL; } -static void print_objects(DBusMessageIter *array) +static void extract_services(DBusMessage *message) { - DBusMessageIter value; - - if (dbus_message_iter_get_arg_type(array) != DBUS_TYPE_ARRAY) - return; + DBusMessageIter iter, array; - dbus_message_iter_recurse(array, &value); + dbus_message_iter_init(message, &iter); + dbus_message_iter_recurse(&iter, &array); - while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_OBJECT_PATH) { + while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) { + DBusMessageIter entry, dict; + struct service_data service; const char *path; - dbus_message_iter_get_basic(&value, &path); - - printf("%s\n", path); - - dbus_message_iter_next(&value); - } -} - -static void extract_devices(DBusMessage *message) -{ - DBusMessageIter array, dict; + dbus_message_iter_recurse(&array, &entry); + dbus_message_iter_get_basic(&entry, &path); - dbus_message_iter_init(message, &array); - dbus_message_iter_recurse(&array, &dict); - - while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) { - DBusMessageIter entry, value; - const char *key; - - dbus_message_iter_recurse(&dict, &entry); - dbus_message_iter_get_basic(&entry, &key); + service.path = strrchr(path, '/') + 1; dbus_message_iter_next(&entry); - dbus_message_iter_recurse(&entry, &value); + dbus_message_iter_recurse(&entry, &dict); + extract_properties(&dict, &service); - //type = dbus_message_iter_get_arg_type(&value); - //dbus_message_iter_get_basic(&value, &val); + printf("%c %-20s { %-50s }\n", + service.favorite == TRUE ? '*' : ' ', + service.name, service.path); - if (strcmp(key, "Devices") == 0) { - print_objects(&value); - return; - } - - dbus_message_iter_next(&dict); + dbus_message_iter_next(&array); } } -static int cmd_status(DBusConnection *connection) +static int cmd_list_services(DBusConnection *connection) { DBusMessage *message; - const char *state; - message = get_properties(connection); + message = get_services(connection); + if (message == NULL) + return -1; - state = extract_state(message); + extract_services(message); dbus_message_unref(message); - if (state == NULL) - return -EINVAL; - - printf("System is %s\n", state); - return 0; } -static int cmd_devices(DBusConnection *connection) +static int cmd_show_service(DBusConnection *connection, const char *pattern) { DBusMessage *message; + const char *path; + + message = lookup_service(connection, pattern); + if (message == NULL) + return -1; - message = get_properties(connection); + dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path, + DBUS_TYPE_INVALID); - extract_devices(message); + printf("Service: %s\n", path); dbus_message_unref(message); @@ -186,11 +202,12 @@ static void usage(const char *program) printf("ConnMan utility ver %s\n\n", VERSION); printf("Usage:\n" - "\t%s <command>\n\n", program); + "\t%s <command> [options]\n\n", program); printf("Commands:\n" "\thelp\n" - "\tdev\n" + "\tlist\n" + "\tshow <service>\n" "\n"); } @@ -210,10 +227,16 @@ int main(int argc, char *argv[]) } if (argc > 1) { - if (strcmp(argv[1], "dev") == 0) - cmd_devices(conn); + if (strcmp(argv[1], "list") == 0) + cmd_list_services(conn); + else if (strcmp(argv[1], "show") == 0) { + if (argc > 2) + cmd_show_service(conn, argv[2]); + else + usage(argv[0]); + } } else - cmd_status(conn); + usage(argv[0]); dbus_connection_unref(conn); |