gsupplicant: Fix D-Bus interface name arguments
[framework/connectivity/connman.git] / gsupplicant / dbus.c
1 /*
2  *
3  *  WPA supplicant library with GLib integration
4  *
5  *  Copyright (C) 2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <dbus/dbus.h>
30
31 #include "dbus.h"
32
33 #define TIMEOUT 5000
34
35 static DBusConnection *connection;
36
37 void supplicant_dbus_setup(DBusConnection *conn)
38 {
39         connection = conn;
40 }
41
42 void supplicant_dbus_array_foreach(DBusMessageIter *iter,
43                                 supplicant_dbus_array_function function,
44                                                         void *user_data)
45 {
46         DBusMessageIter entry;
47
48         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
49                 return;
50
51         dbus_message_iter_recurse(iter, &entry);
52
53         while (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_INVALID) {
54                 if (function != NULL)
55                         function(&entry, user_data);
56
57                 dbus_message_iter_next(&entry);
58         }
59 }
60
61 void supplicant_dbus_property_foreach(DBusMessageIter *iter,
62                                 supplicant_dbus_property_function function,
63                                                         void *user_data)
64 {
65         DBusMessageIter dict;
66
67         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_ARRAY)
68                 return;
69
70         dbus_message_iter_recurse(iter, &dict);
71
72         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
73                 DBusMessageIter entry, value;
74                 const char *key;
75
76                 dbus_message_iter_recurse(&dict, &entry);
77
78                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_STRING)
79                         return;
80
81                 dbus_message_iter_get_basic(&entry, &key);
82                 dbus_message_iter_next(&entry);
83
84                 if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
85                         return;
86
87                 dbus_message_iter_recurse(&entry, &value);
88
89                 if (key != NULL) {
90                         if (strcmp(key, "Properties") == 0)
91                                 supplicant_dbus_property_foreach(&value,
92                                                         function, user_data);
93                         else if (function != NULL)
94                                 function(key, &value, user_data);
95                 }
96
97                 dbus_message_iter_next(&dict);
98         }
99 }
100
101 struct property_get_data {
102         supplicant_dbus_property_function function;
103         void *user_data;
104 };
105
106 static void property_get_all_reply(DBusPendingCall *call, void *user_data)
107 {
108         struct property_get_data *data = user_data;
109         DBusMessage *reply;
110         DBusMessageIter iter;
111
112         reply = dbus_pending_call_steal_reply(call);
113
114         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
115                 goto done;
116
117         if (dbus_message_iter_init(reply, &iter) == FALSE)
118                 goto done;
119
120         supplicant_dbus_property_foreach(&iter, data->function,
121                                                         data->user_data);
122
123         if (data->function != NULL)
124                 data->function(NULL, NULL, data->user_data);
125
126 done:
127         dbus_message_unref(reply);
128
129         dbus_pending_call_unref(call);
130 }
131
132 int supplicant_dbus_property_get_all(const char *path, const char *interface,
133                                 supplicant_dbus_property_function function,
134                                                         void *user_data)
135 {
136         struct property_get_data *data;
137         DBusMessage *message;
138         DBusPendingCall *call;
139
140         if (connection == NULL)
141                 return -EINVAL;
142
143         if (path == NULL || interface == NULL)
144                 return -EINVAL;
145
146         data = dbus_malloc0(sizeof(*data));
147         if (data == NULL)
148                 return -ENOMEM;
149
150         message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
151                                         DBUS_INTERFACE_PROPERTIES, "GetAll");
152         if (message == NULL) {
153                 dbus_free(data);
154                 return -ENOMEM;
155         }
156
157         dbus_message_set_auto_start(message, FALSE);
158
159         dbus_message_append_args(message, DBUS_TYPE_STRING, &interface, NULL);
160
161         if (dbus_connection_send_with_reply(connection, message,
162                                                 &call, TIMEOUT) == FALSE) {
163                 dbus_message_unref(message);
164                 dbus_free(data);
165                 return -EIO;
166         }
167
168         if (call == NULL) {
169                 dbus_message_unref(message);
170                 dbus_free(data);
171                 return -EIO;
172         }
173
174         data->function = function;
175         data->user_data = user_data;
176
177         dbus_pending_call_set_notify(call, property_get_all_reply,
178                                                         data, dbus_free);
179
180         dbus_message_unref(message);
181
182         return 0;
183 }
184
185 static void property_get_reply(DBusPendingCall *call, void *user_data)
186 {
187         struct property_get_data *data = user_data;
188         DBusMessage *reply;
189         DBusMessageIter iter;
190
191         reply = dbus_pending_call_steal_reply(call);
192
193         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
194                 goto done;
195
196         if (dbus_message_iter_init(reply, &iter) == FALSE)
197                 goto done;
198
199         if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_VARIANT) {
200                 DBusMessageIter variant;
201
202                 dbus_message_iter_recurse(&iter, &variant);
203
204                 if (data->function != NULL)
205                         data->function(NULL, &variant, data->user_data);
206         }
207 done:
208         dbus_message_unref(reply);
209
210         dbus_pending_call_unref(call);
211 }
212
213 int supplicant_dbus_property_get(const char *path, const char *interface,
214                                 const char *method,
215                                 supplicant_dbus_property_function function,
216                                                         void *user_data)
217 {
218         struct property_get_data *data;
219         DBusMessage *message;
220         DBusPendingCall *call;
221
222         if (connection == NULL)
223                 return -EINVAL;
224
225         if (path == NULL || interface == NULL || method == NULL)
226                 return -EINVAL;
227
228         data = dbus_malloc0(sizeof(*data));
229         if (data == NULL)
230                 return -ENOMEM;
231
232         message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
233                                         DBUS_INTERFACE_PROPERTIES, "Get");
234
235         if (message == NULL) {
236                 dbus_free(data);
237                 return -ENOMEM;
238         }
239
240         dbus_message_set_auto_start(message, FALSE);
241
242         dbus_message_append_args(message, DBUS_TYPE_STRING, &interface,
243                                         DBUS_TYPE_STRING, &method, NULL);
244
245         if (dbus_connection_send_with_reply(connection, message,
246                                                 &call, TIMEOUT) == FALSE) {
247                 dbus_message_unref(message);
248                 dbus_free(data);
249                 return -EIO;
250         }
251
252         if (call == NULL) {
253                 dbus_message_unref(message);
254                 dbus_free(data);
255                 return -EIO;
256         }
257
258         data->function = function;
259         data->user_data = user_data;
260
261         dbus_pending_call_set_notify(call, property_get_reply,
262                                                         data, dbus_free);
263
264         dbus_message_unref(message);
265
266         return 0;
267 }
268
269 struct property_set_data {
270         supplicant_dbus_result_function function;
271         void *user_data;
272 };
273
274 static void property_set_reply(DBusPendingCall *call, void *user_data)
275 {
276         struct property_set_data *data = user_data;
277         DBusMessage *reply;
278         DBusMessageIter iter;
279         const char *error;
280
281         reply = dbus_pending_call_steal_reply(call);
282
283         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
284                 error = dbus_message_get_error_name(reply);
285         else
286                 error = NULL;
287
288         if (dbus_message_iter_init(reply, &iter) == FALSE)
289                 goto done;
290
291         if (data->function != NULL)
292                 data->function(error, &iter, data->user_data);
293
294 done:
295         dbus_message_unref(reply);
296
297         dbus_pending_call_unref(call);
298 }
299
300 int supplicant_dbus_property_set(const char *path, const char *interface,
301                                 const char *key, const char *signature,
302                                 supplicant_dbus_setup_function setup,
303                                 supplicant_dbus_result_function function,
304                                                         void *user_data)
305 {
306         struct property_set_data *data;
307         DBusMessage *message;
308         DBusMessageIter iter, value;
309         DBusPendingCall *call;
310
311         if (connection == NULL)
312                 return -EINVAL;
313
314         if (path == NULL || interface == NULL)
315                 return -EINVAL;
316
317         if (key == NULL || signature == NULL || setup == NULL)
318                 return -EINVAL;
319
320         data = dbus_malloc0(sizeof(*data));
321         if (data == NULL)
322                 return -ENOMEM;
323
324         message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
325                                         DBUS_INTERFACE_PROPERTIES, "Set");
326         if (message == NULL) {
327                 dbus_free(data);
328                 return -ENOMEM;
329         }
330
331         dbus_message_set_auto_start(message, FALSE);
332
333         dbus_message_iter_init_append(message, &iter);
334         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &interface);
335         dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);
336
337         dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
338                                                         signature, &value);
339         setup(&value, user_data);
340         dbus_message_iter_close_container(&iter, &value);
341
342         if (dbus_connection_send_with_reply(connection, message,
343                                                 &call, TIMEOUT) == FALSE) {
344                 dbus_message_unref(message);
345                 dbus_free(data);
346                 return -EIO;
347         }
348
349         if (call == NULL) {
350                 dbus_message_unref(message);
351                 dbus_free(data);
352                 return -EIO;
353         }
354
355         data->function = function;
356         data->user_data = user_data;
357
358         dbus_pending_call_set_notify(call, property_set_reply,
359                                                         data, dbus_free);
360
361         dbus_message_unref(message);
362
363         return 0;
364 }
365
366 struct method_call_data {
367         supplicant_dbus_result_function function;
368         void *user_data;
369 };
370
371 static void method_call_reply(DBusPendingCall *call, void *user_data)
372 {
373         struct method_call_data *data = user_data;
374         DBusMessage *reply;
375         DBusMessageIter iter;
376         const char *error;
377
378         reply = dbus_pending_call_steal_reply(call);
379
380         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
381                 error = dbus_message_get_error_name(reply);
382         else
383                 error = NULL;
384
385         dbus_message_iter_init(reply, &iter);
386
387         if (data->function != NULL)
388                 data->function(error, &iter, data->user_data);
389
390         dbus_message_unref(reply);
391
392         dbus_pending_call_unref(call);
393 }
394
395 int supplicant_dbus_method_call(const char *path,
396                                 const char *interface, const char *method,
397                                 supplicant_dbus_setup_function setup,
398                                 supplicant_dbus_result_function function,
399                                                         void *user_data)
400 {
401         struct method_call_data *data;
402         DBusMessage *message;
403         DBusMessageIter iter;
404         DBusPendingCall *call;
405
406         if (connection == NULL)
407                 return -EINVAL;
408
409         if (path == NULL || interface == NULL || method == NULL)
410                 return -EINVAL;
411
412         data = dbus_malloc0(sizeof(*data));
413         if (data == NULL)
414                 return -ENOMEM;
415
416         message = dbus_message_new_method_call(SUPPLICANT_SERVICE, path,
417                                                         interface, method);
418         if (message == NULL) {
419                 dbus_free(data);
420                 return -ENOMEM;
421         }
422
423         dbus_message_set_auto_start(message, FALSE);
424
425         dbus_message_iter_init_append(message, &iter);
426         if (setup != NULL)
427                 setup(&iter, user_data);
428
429         if (dbus_connection_send_with_reply(connection, message,
430                                                 &call, TIMEOUT) == FALSE) {
431                 dbus_message_unref(message);
432                 dbus_free(data);
433                 return -EIO;
434         }
435
436         if (call == NULL) {
437                 dbus_message_unref(message);
438                 dbus_free(data);
439                 return -EIO;
440         }
441
442         data->function = function;
443         data->user_data = user_data;
444
445         dbus_pending_call_set_notify(call, method_call_reply,
446                                                         data, dbus_free);
447
448         dbus_message_unref(message);
449
450         return 0;
451 }
452
453 void supplicant_dbus_property_append_basic(DBusMessageIter *iter,
454                                         const char *key, int type, void *val)
455 {
456         DBusMessageIter value;
457         const char *signature;
458
459         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
460
461         switch (type) {
462         case DBUS_TYPE_BOOLEAN:
463                 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
464                 break;
465         case DBUS_TYPE_STRING:
466                 signature = DBUS_TYPE_STRING_AS_STRING;
467                 break;
468         case DBUS_TYPE_BYTE:
469                 signature = DBUS_TYPE_BYTE_AS_STRING;
470                 break;
471         case DBUS_TYPE_UINT16:
472                 signature = DBUS_TYPE_UINT16_AS_STRING;
473                 break;
474         case DBUS_TYPE_INT16:
475                 signature = DBUS_TYPE_INT16_AS_STRING;
476                 break;
477         case DBUS_TYPE_UINT32:
478                 signature = DBUS_TYPE_UINT32_AS_STRING;
479                 break;
480         case DBUS_TYPE_INT32:
481                 signature = DBUS_TYPE_INT32_AS_STRING;
482                 break;
483         case DBUS_TYPE_OBJECT_PATH:
484                 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
485                 break;
486         default:
487                 signature = DBUS_TYPE_VARIANT_AS_STRING;
488                 break;
489         }
490
491         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
492                                                         signature, &value);
493         dbus_message_iter_append_basic(&value, type, val);
494         dbus_message_iter_close_container(iter, &value);
495 }
496
497 void supplicant_dbus_property_append_fixed_array(DBusMessageIter *iter,
498                                 const char *key, int type, void *val, int len)
499 {
500         DBusMessageIter value, array;
501         const char *variant_sig, *array_sig;
502
503         switch (type) {
504         case DBUS_TYPE_BYTE:
505                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING
506                                         DBUS_TYPE_BYTE_AS_STRING;
507                 array_sig = DBUS_TYPE_BYTE_AS_STRING;
508                 break;
509         default:
510                 return;
511         }
512
513         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
514
515         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
516                                                         variant_sig, &value);
517
518         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
519                                                         array_sig, &array);
520         dbus_message_iter_append_fixed_array(&array, type, val, len);
521         dbus_message_iter_close_container(&value, &array);
522
523         dbus_message_iter_close_container(iter, &value);
524 }