5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <dbus/dbus.h>
33 #define CONNMAN_SERVICE "org.moblin.connman"
35 #define CONNMAN_MANAGER_INTERFACE CONNMAN_SERVICE ".Manager"
36 #define CONNMAN_MANAGER_PATH "/"
44 static DBusMessage *get_services(DBusConnection *connection)
46 DBusMessage *message, *reply;
49 message = dbus_message_new_method_call(CONNMAN_SERVICE,
51 CONNMAN_MANAGER_INTERFACE,
56 dbus_error_init(&error);
58 reply = dbus_connection_send_with_reply_and_block(connection,
61 if (dbus_error_is_set(&error) == TRUE) {
62 fprintf(stderr, "%s\n", error.message);
63 dbus_error_free(&error);
65 fprintf(stderr, "Failed to get properties\n");
66 dbus_message_unref(message);
70 dbus_message_unref(message);
75 static DBusMessage *lookup_service(DBusConnection *connection,
78 DBusMessage *message, *reply;
81 message = dbus_message_new_method_call(CONNMAN_SERVICE,
83 CONNMAN_MANAGER_INTERFACE,
88 dbus_message_append_args(message, DBUS_TYPE_STRING, &pattern,
91 dbus_error_init(&error);
93 reply = dbus_connection_send_with_reply_and_block(connection,
96 if (dbus_error_is_set(&error) == TRUE) {
97 fprintf(stderr, "%s\n", error.message);
98 dbus_error_free(&error);
100 fprintf(stderr, "Failed to get properties\n");
101 dbus_message_unref(message);
105 dbus_message_unref(message);
110 static void extract_properties(DBusMessageIter *dict,
111 struct service_data *service)
113 while (dbus_message_iter_get_arg_type(dict) == DBUS_TYPE_DICT_ENTRY) {
114 DBusMessageIter entry, value;
117 dbus_message_iter_recurse(dict, &entry);
118 dbus_message_iter_get_basic(&entry, &key);
120 dbus_message_iter_next(&entry);
122 dbus_message_iter_recurse(&entry, &value);
124 //type = dbus_message_iter_get_arg_type(&value);
125 //dbus_message_iter_get_basic(&value, &val);
127 if (strcmp(key, "Name") == 0)
128 dbus_message_iter_get_basic(&value, &service->name);
129 else if (strcmp(key, "Favorite") == 0)
130 dbus_message_iter_get_basic(&value, &service->favorite);
132 dbus_message_iter_next(dict);
136 static void extract_services(DBusMessage *message)
138 DBusMessageIter iter, array;
140 dbus_message_iter_init(message, &iter);
141 dbus_message_iter_recurse(&iter, &array);
143 while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRUCT) {
144 DBusMessageIter entry, dict;
145 struct service_data service;
148 dbus_message_iter_recurse(&array, &entry);
149 dbus_message_iter_get_basic(&entry, &path);
151 service.path = strrchr(path, '/') + 1;
153 dbus_message_iter_next(&entry);
155 dbus_message_iter_recurse(&entry, &dict);
156 extract_properties(&dict, &service);
158 printf("%c %-20s { %-50s }\n",
159 service.favorite == TRUE ? '*' : ' ',
160 service.name, service.path);
162 dbus_message_iter_next(&array);
166 static int cmd_list_services(DBusConnection *connection)
168 DBusMessage *message;
170 message = get_services(connection);
174 extract_services(message);
176 dbus_message_unref(message);
181 static int cmd_show_service(DBusConnection *connection, const char *pattern)
183 DBusMessage *message;
186 message = lookup_service(connection, pattern);
190 dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
193 printf("Service: %s\n", path);
195 dbus_message_unref(message);
200 static void usage(const char *program)
202 printf("ConnMan utility ver %s\n\n", VERSION);
205 "\t%s <command> [options]\n\n", program);
214 int main(int argc, char *argv[])
216 DBusConnection *conn;
218 if (argc > 1 && strcmp(argv[1], "help") == 0) {
223 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
225 fprintf(stderr, "Can't get on system bus\n");
230 if (strcmp(argv[1], "list") == 0)
231 cmd_list_services(conn);
232 else if (strcmp(argv[1], "show") == 0) {
234 cmd_show_service(conn, argv[2]);
241 dbus_connection_unref(conn);