5 * Copyright (C) 2013 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 as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "dbus_helpers.h"
32 void __connmanctl_dbus_print(DBusMessageIter *iter, const char *pre,
33 const char *dict, const char *sep)
43 DBusMessageIter entry;
48 while ((arg_type = dbus_message_iter_get_arg_type(iter))
49 != DBUS_TYPE_INVALID) {
51 fprintf(stdout, "%s", pre);
54 case DBUS_TYPE_STRUCT:
55 fprintf(stdout, "{ ");
56 dbus_message_iter_recurse(iter, &entry);
57 __connmanctl_dbus_print(&entry, "", "=", " ");
58 fprintf(stdout, " }");
62 fprintf(stdout, "[ ");
64 dbus_message_iter_recurse(iter, &entry);
65 __connmanctl_dbus_print(&entry, "", "=", ", ");
67 fprintf(stdout, " ]");
70 case DBUS_TYPE_DICT_ENTRY:
72 dbus_message_iter_recurse(iter, &entry);
73 __connmanctl_dbus_print(&entry, "", dict, dict);
76 case DBUS_TYPE_STRING:
77 case DBUS_TYPE_OBJECT_PATH:
78 dbus_message_iter_get_basic(iter, &str);
79 fprintf(stdout, "%s", str);
82 case DBUS_TYPE_VARIANT:
83 dbus_message_iter_recurse(iter, &entry);
84 __connmanctl_dbus_print(&entry, pre, dict, sep);
87 case DBUS_TYPE_BOOLEAN:
88 dbus_message_iter_get_basic(iter, &b);
90 fprintf(stdout, "False");
92 fprintf(stdout, "True");
96 dbus_message_iter_get_basic(iter, &c);
97 fprintf(stdout, "%d", c);
100 case DBUS_TYPE_UINT16:
101 case DBUS_TYPE_UINT32:
102 dbus_message_iter_get_basic(iter, &u);
103 fprintf(stdout, "%d", u);
106 case DBUS_TYPE_INT32:
107 dbus_message_iter_get_basic(iter, &i);
108 fprintf(stdout, "%d", i);
111 case DBUS_TYPE_DOUBLE:
112 dbus_message_iter_get_basic(iter, &d);
113 fprintf(stdout, "%f", d);
117 fprintf(stdout, "<type %c>", arg_type);
121 if (dbus_message_iter_has_next(iter) == TRUE)
122 fprintf(stdout, "%s", sep);
124 dbus_message_iter_next(iter);
128 struct dbus_callback {
129 connmanctl_dbus_method_return_func_t cb;
133 static void dbus_method_reply(DBusPendingCall *call, void *user_data)
135 struct dbus_callback *callback = user_data;
138 DBusMessageIter iter;
140 __connmanctl_save_rl();
142 reply = dbus_pending_call_steal_reply(call);
143 if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
146 dbus_error_init(&err);
147 dbus_set_error_from_message(&err, reply);
149 callback->cb(NULL, err.message, callback->user_data);
151 dbus_error_free(&err);
155 dbus_message_iter_init(reply, &iter);
156 res = callback->cb(&iter, NULL, callback->user_data);
159 __connmanctl_redraw_rl();
160 if (__connmanctl_is_interactive() == false && res != -EINPROGRESS)
164 dbus_message_unref(reply);
167 static int send_method_call(DBusConnection *connection,
168 DBusMessage *message, connmanctl_dbus_method_return_func_t cb,
172 DBusPendingCall *call;
173 struct dbus_callback *callback;
175 if (dbus_connection_send_with_reply(connection, message, &call,
183 callback = g_new0(struct dbus_callback, 1);
185 callback->user_data = user_data;
186 dbus_pending_call_set_notify(call, dbus_method_reply,
192 dbus_message_unref(message);
196 static int append_variant(DBusMessageIter *iter, const char *property,
197 int type, void *value)
199 DBusMessageIter variant;
203 case DBUS_TYPE_BOOLEAN:
204 type_str = DBUS_TYPE_BOOLEAN_AS_STRING;
207 type_str = DBUS_TYPE_BYTE_AS_STRING;
209 case DBUS_TYPE_STRING:
210 type_str = DBUS_TYPE_STRING_AS_STRING;
216 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &property);
217 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, type_str,
219 dbus_message_iter_append_basic(&variant, type, value);
220 dbus_message_iter_close_container(iter, &variant);
225 int __connmanctl_dbus_method_call(DBusConnection *connection,
226 const char *service, const char *path, const char *interface,
227 const char *method, connmanctl_dbus_method_return_func_t cb,
228 void *user_data, int arg1, ...)
230 DBusMessage *message;
233 message = dbus_message_new_method_call(service, path, interface,
239 va_start(args, arg1);
240 dbus_message_append_args_valist(message, arg1, args);
243 return send_method_call(connection, message, cb, user_data);
246 int __connmanctl_dbus_set_property(DBusConnection *connection,
247 const char *path, const char *interface,
248 connmanctl_dbus_method_return_func_t cb, void * user_data,
249 const char *property, int type, void *value)
251 DBusMessage *message;
252 DBusMessageIter iter;
254 message = dbus_message_new_method_call("net.connman", path,
255 interface, "SetProperty");
260 dbus_message_iter_init_append(message, &iter);
262 if (append_variant(&iter, property, type, value) < 0) {
263 dbus_message_unref(message);
267 return send_method_call(connection, message, cb, user_data);
270 void __connmanctl_dbus_append_dict_entry(DBusMessageIter *iter,
271 const char *property, int type, void *value)
273 DBusMessageIter dict_entry;
275 dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL,
278 append_variant(&dict_entry, property, type, value);
280 dbus_message_iter_close_container(iter, &dict_entry);
283 int __connmanctl_dbus_set_property_dict(DBusConnection *connection,
284 const char *path, const char *interface,
285 connmanctl_dbus_method_return_func_t cb, void *user_data,
286 const char *property, int type,
287 connmanctl_dbus_append_func_t append_fn,
288 void *append_user_data)
290 DBusMessage *message;
291 DBusMessageIter iter, variant, dict;
293 message = dbus_message_new_method_call("net.connman", path,
294 interface, "SetProperty");
299 dbus_message_iter_init_append(message, &iter);
300 dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &property);
301 dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
302 DBUS_TYPE_ARRAY_AS_STRING
303 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
304 DBUS_TYPE_STRING_AS_STRING
305 DBUS_TYPE_VARIANT_AS_STRING
306 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
309 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
310 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
311 DBUS_TYPE_STRING_AS_STRING
312 DBUS_TYPE_VARIANT_AS_STRING
313 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
316 append_fn(&dict, append_user_data);
318 dbus_message_iter_close_container(&variant, &dict);
319 dbus_message_iter_close_container(&iter, &variant);
321 return send_method_call(connection, message, cb, user_data);
324 static void append_variant_array(DBusMessageIter *iter, const char *property,
325 connmanctl_dbus_append_func_t append_fn,
326 void *append_user_data)
328 DBusMessageIter variant, array;
330 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &property);
331 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
332 DBUS_TYPE_ARRAY_AS_STRING
333 DBUS_TYPE_STRING_AS_STRING,
336 dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
337 DBUS_TYPE_STRING_AS_STRING, &array);
339 append_fn(&array, append_user_data);
341 dbus_message_iter_close_container(&variant, &array);
342 dbus_message_iter_close_container(iter, &variant);
345 void __connmanctl_dbus_append_dict_string_array(DBusMessageIter *iter,
346 const char *property, connmanctl_dbus_append_func_t append_fn,
347 void *append_user_data)
349 DBusMessageIter dict_entry;
351 dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL,
354 append_variant_array(&dict_entry, property, append_fn,
357 dbus_message_iter_close_container(iter, &dict_entry);
360 int __connmanctl_dbus_set_property_array(DBusConnection *connection,
361 const char *path, const char *interface,
362 connmanctl_dbus_method_return_func_t cb, void *user_data,
363 const char *property, int type,
364 connmanctl_dbus_append_func_t append_fn,
365 void *append_user_data)
367 DBusMessage *message;
368 DBusMessageIter iter;
370 if (type != DBUS_TYPE_STRING)
373 message = dbus_message_new_method_call("net.connman", path,
374 interface, "SetProperty");
379 dbus_message_iter_init_append(message, &iter);
381 append_variant_array(&iter, property, append_fn, append_user_data);
383 return send_method_call(connection, message, cb, user_data);