4 Copyright (C) 2004 Nokia Corporation. All rights reserved.
5 Copyright (C) 2003-2009, Jouni Malinen <j@w1.fi> and contributors.
6 Copyright (C) 2009 Javier S. Pedro
8 @author Janne Ylalehto <janne.ylalehto@nokia.com>
9 @author Johan Hedberg <johan.hedberg@nokia.com>
10 @author Jouni Malinen <j@w1.fi> and contributors
11 @author Javier S. Pedro <javispedro@javispedro.com>
13 This program is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by the
15 Free Software Foundation; either version 2 of the License, or (at your
16 option) any later version.
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
23 You should have received a copy of the GNU General Public License along
24 with this program; if not, write to the Free Software Foundation, Inc.,
25 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #define DBUS_API_SUBJECT_TO_CHANGE
34 #include <dbus/dbus.h>
36 #include "dbus-helper.h"
38 DBusMessage *new_dbus_signal(const char *path,
39 const char *interface,
41 const char *destination) {
44 signal = dbus_message_new_signal(path, interface, name);
46 die("Out of memory during dbus_message_new_error()");
50 if (!dbus_message_set_destination(signal, destination)) {
51 die("Out of memory during dbus_message_set_destination()");
55 dbus_message_set_no_reply(signal, TRUE);
60 DBusMessage *new_dbus_method_call(const char *service,
62 const char *interface,
66 message = dbus_message_new_method_call(service, path, interface, method);
67 if (message == NULL) {
68 die("Out of memory during dbus_message_new_method_call()");
74 DBusMessage *new_dbus_method_return(DBusMessage *message) {
77 reply = dbus_message_new_method_return(message);
79 die("Out of memory during dbus_message_new_method_return()");
85 DBusMessage *new_dbus_error(DBusMessage *message, const char *name) {
88 error = dbus_message_new_error(message, name, NULL);
90 die("Out of memory during dbus_message_new_error()");
96 int send_and_unref(DBusConnection *connection, DBusMessage *message) {
97 if (!dbus_connection_send(connection, message, NULL)) {
98 dbus_message_unref(message);
102 dbus_connection_flush(connection);
103 dbus_message_unref(message);
108 int send_invalid_args(DBusConnection *connection, DBusMessage *message) {
111 reply = new_dbus_error(message, DBUS_ERROR_INVALID_ARGS);
113 return send_and_unref(connection, reply);
116 void append_dbus_args(DBusMessage *message, int first_arg_type, ...) {
120 va_start(ap, first_arg_type);
121 ret = dbus_message_append_args_valist(message, first_arg_type, ap);
125 die("dbus_message_append_args failed");
130 * Start a dict in a dbus message. Should be paired with a call to
131 * {@link wpa_dbus_dict_close_write}.
133 * @param iter A valid dbus message iterator
134 * @param iter_dict (out) A dict iterator to pass to further dict functions
135 * @return 0 on success, -1 on failure
138 int dbus_dict_open_write(DBusMessageIter *iter,
139 DBusMessageIter *iter_dict)
143 if (!iter || !iter_dict)
146 result = dbus_message_iter_open_container(
149 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
150 DBUS_TYPE_STRING_AS_STRING
151 DBUS_TYPE_VARIANT_AS_STRING
152 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
155 return (result ? 0 : -1);
159 * End a dict element in a dbus message. Should be paired with
160 * a call to {@link wpa_dbus_dict_open_write}.
162 * @param iter valid dbus message iterator, same as passed to
163 * wpa_dbus_dict_open_write()
164 * @param iter_dict a dbus dict iterator returned from
165 * {@link wpa_dbus_dict_open_write}
166 * @return 0 on success, -1 on failure
169 int dbus_dict_close_write(DBusMessageIter *iter,
170 DBusMessageIter *iter_dict)
172 if (!iter || !iter_dict)
175 return dbus_message_iter_close_container(iter, iter_dict) ? 0 : -1;
178 static const char * _get_type_as_string_from_type(const int type)
182 return DBUS_TYPE_BYTE_AS_STRING;
183 case DBUS_TYPE_BOOLEAN:
184 return DBUS_TYPE_BOOLEAN_AS_STRING;
185 case DBUS_TYPE_INT16:
186 return DBUS_TYPE_INT16_AS_STRING;
187 case DBUS_TYPE_UINT16:
188 return DBUS_TYPE_UINT16_AS_STRING;
189 case DBUS_TYPE_INT32:
190 return DBUS_TYPE_INT32_AS_STRING;
191 case DBUS_TYPE_UINT32:
192 return DBUS_TYPE_UINT32_AS_STRING;
193 case DBUS_TYPE_INT64:
194 return DBUS_TYPE_INT64_AS_STRING;
195 case DBUS_TYPE_UINT64:
196 return DBUS_TYPE_UINT64_AS_STRING;
197 case DBUS_TYPE_DOUBLE:
198 return DBUS_TYPE_DOUBLE_AS_STRING;
199 case DBUS_TYPE_STRING:
200 return DBUS_TYPE_STRING_AS_STRING;
201 case DBUS_TYPE_OBJECT_PATH:
202 return DBUS_TYPE_OBJECT_PATH_AS_STRING;
203 case DBUS_TYPE_ARRAY:
204 return DBUS_TYPE_ARRAY_AS_STRING;
210 static int _dbus_add_dict_entry_start(
211 DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
212 const char *key, const int value_type)
214 if (!dbus_message_iter_open_container(iter_dict,
215 DBUS_TYPE_DICT_ENTRY, NULL,
219 if (!dbus_message_iter_append_basic(iter_dict_entry, DBUS_TYPE_STRING,
227 static dbus_bool_t _dbus_add_dict_entry_end(
228 DBusMessageIter *iter_dict, DBusMessageIter *iter_dict_entry,
229 DBusMessageIter *iter_dict_val)
231 if (!dbus_message_iter_close_container(iter_dict_entry, iter_dict_val))
233 if (!dbus_message_iter_close_container(iter_dict, iter_dict_entry))
239 static dbus_bool_t _dbus_add_dict_entry_basic(DBusMessageIter *iter_dict,
241 const int value_type,
244 DBusMessageIter iter_dict_entry, iter_dict_val;
245 const char *type_as_string = NULL;
247 type_as_string = _get_type_as_string_from_type(value_type);
251 if (!_dbus_add_dict_entry_start(iter_dict, &iter_dict_entry,
255 if (!dbus_message_iter_open_container(&iter_dict_entry,
257 type_as_string, &iter_dict_val))
260 if (!dbus_message_iter_append_basic(&iter_dict_val, value_type, value))
263 if (!_dbus_add_dict_entry_end(iter_dict, &iter_dict_entry,
271 * Add a string entry to the dict.
273 * @param iter_dict A valid DBusMessageIter returned from
274 * {@link wpa_dbus_dict_open_write}
275 * @param key The key of the dict item
276 * @param value The string value
277 * @return TRUE on success, FALSE on failure
280 int dbus_dict_append_string(DBusMessageIter *iter_dict,
281 const char *key, const char *value)
285 return _dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_STRING,
290 * Add a 32-bit signed integer to the dict.
292 * @param iter_dict A valid DBusMessageIter returned from
293 * {@link wpa_dbus_dict_open_write}
294 * @param key The key of the dict item
295 * @param value The 32-bit signed integer value
296 * @return TRUE on success, FALSE on failure
299 int dbus_dict_append_int32(DBusMessageIter *iter_dict,
301 const dbus_int32_t value)
305 return _dbus_add_dict_entry_basic(iter_dict, key, DBUS_TYPE_INT32,