Adding connman-test subpackage
[framework/connectivity/connman.git] / src / dbus.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-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 <string.h>
27 #include <gdbus.h>
28
29 #include "connman.h"
30
31 dbus_bool_t connman_dbus_validate_ident(const char *ident)
32 {
33         unsigned int i;
34
35         if (ident == NULL)
36                 return FALSE;
37
38         for (i = 0; i < strlen(ident); i++) {
39                 if (ident[i] >= '0' && ident[i] <= '9')
40                         continue;
41                 if (ident[i] >= 'a' && ident[i] <= 'z')
42                         continue;
43                 if (ident[i] >= 'A' && ident[i] <= 'Z')
44                         continue;
45                 return FALSE;
46         }
47
48         return TRUE;
49 }
50
51 char *connman_dbus_encode_string(const char *value)
52 {
53         GString *str;
54         unsigned int i, size;
55
56         if (value == NULL)
57                 return NULL;
58
59         size = strlen(value);
60
61         str = g_string_new(NULL);
62         if (str == NULL)
63                 return NULL;
64
65         for (i = 0; i < size; i++) {
66                 const char tmp = value[i];
67                 if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
68                                                 (tmp < 'a' || tmp > 'z'))
69                         g_string_append_printf(str, "_%02x", tmp);
70                 else
71                         str = g_string_append_c(str, tmp);
72         }
73
74         return g_string_free(str, FALSE);
75 }
76
77 void connman_dbus_property_append_basic(DBusMessageIter *iter,
78                                         const char *key, int type, void *val)
79 {
80         DBusMessageIter value;
81         const char *signature;
82
83         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
84
85         switch (type) {
86         case DBUS_TYPE_BOOLEAN:
87                 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
88                 break;
89         case DBUS_TYPE_STRING:
90                 signature = DBUS_TYPE_STRING_AS_STRING;
91                 break;
92         case DBUS_TYPE_BYTE:
93                 signature = DBUS_TYPE_BYTE_AS_STRING;
94                 break;
95         case DBUS_TYPE_UINT16:
96                 signature = DBUS_TYPE_UINT16_AS_STRING;
97                 break;
98         case DBUS_TYPE_INT16:
99                 signature = DBUS_TYPE_INT16_AS_STRING;
100                 break;
101         case DBUS_TYPE_UINT32:
102                 signature = DBUS_TYPE_UINT32_AS_STRING;
103                 break;
104         case DBUS_TYPE_INT32:
105                 signature = DBUS_TYPE_INT32_AS_STRING;
106                 break;
107         case DBUS_TYPE_UINT64:
108                 signature = DBUS_TYPE_UINT64_AS_STRING;
109                 break;
110         case DBUS_TYPE_INT64:
111                 signature = DBUS_TYPE_INT64_AS_STRING;
112                 break;
113         case DBUS_TYPE_OBJECT_PATH:
114                 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
115                 break;
116         default:
117                 signature = DBUS_TYPE_VARIANT_AS_STRING;
118                 break;
119         }
120
121         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
122                                                         signature, &value);
123         dbus_message_iter_append_basic(&value, type, val);
124         dbus_message_iter_close_container(iter, &value);
125 }
126
127 void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key,
128                         connman_dbus_append_cb_t function, void *user_data)
129 {
130         DBusMessageIter value, dict;
131
132         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
133
134         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
135                         DBUS_TYPE_ARRAY_AS_STRING
136                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
137                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
138                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value);
139
140         connman_dbus_dict_open(&value, &dict);
141         if (function)
142                 function(&dict, user_data);
143         connman_dbus_dict_close(&value, &dict);
144
145         dbus_message_iter_close_container(iter, &value);
146 }
147
148 void connman_dbus_property_append_fixed_array(DBusMessageIter *iter,
149                                 const char *key, int type, void *val, int len)
150 {
151         DBusMessageIter value, array;
152         const char *variant_sig, *array_sig;
153
154         switch (type) {
155         case DBUS_TYPE_BYTE:
156                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
157                 array_sig = DBUS_TYPE_BYTE_AS_STRING;
158                 break;
159         default:
160                 return;
161         }
162
163         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
164
165         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
166                                                         variant_sig, &value);
167
168         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
169                                                         array_sig, &array);
170         dbus_message_iter_append_fixed_array(&array, type, val, len);
171         dbus_message_iter_close_container(&value, &array);
172
173         dbus_message_iter_close_container(iter, &value);
174 }
175
176 void connman_dbus_property_append_array(DBusMessageIter *iter,
177                                                 const char *key, int type,
178                         connman_dbus_append_cb_t function, void *user_data)
179 {
180         DBusMessageIter value, array;
181         const char *variant_sig, *array_sig;
182
183         switch (type) {
184         case DBUS_TYPE_STRING:
185                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
186                 array_sig = DBUS_TYPE_STRING_AS_STRING;
187                 break;
188         case DBUS_TYPE_OBJECT_PATH:
189                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING;
190                 array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
191                 break;
192         default:
193                 return;
194         }
195
196         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
197
198         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
199                                                         variant_sig, &value);
200
201         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
202                                                         array_sig, &array);
203         if (function)
204                 function(&array, user_data);
205         dbus_message_iter_close_container(&value, &array);
206
207         dbus_message_iter_close_container(iter, &value);
208 }
209
210 static DBusConnection *connection = NULL;
211
212 dbus_bool_t connman_dbus_property_changed_basic(const char *path,
213                                 const char *interface, const char *key,
214                                                         int type, void *val)
215 {
216         DBusMessage *signal;
217         DBusMessageIter iter;
218
219         if (path == NULL)
220                 return FALSE;
221
222         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
223         if (signal == NULL)
224                 return FALSE;
225
226         dbus_message_iter_init_append(signal, &iter);
227         connman_dbus_property_append_basic(&iter, key, type, val);
228
229         g_dbus_send_message(connection, signal);
230
231         return TRUE;
232 }
233
234 #if defined TIZEN_EXT
235 /*
236  * Description: Extends state_changed signal to notify service error cause
237  */
238 dbus_bool_t connman_dbus_service_property_changed_with_error_cause(const char *path,
239                         const char *interface, const char *key1, int type1, void *val1,
240                         const char *key2, int type2, void *val2)
241 {
242         DBusMessage *signal;
243         DBusMessageIter iter;
244
245         if (path == NULL)
246                 return FALSE;
247
248         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
249         if (signal == NULL)
250                 return FALSE;
251
252         dbus_message_iter_init_append(signal, &iter);
253         connman_dbus_property_append_basic(&iter, key1, type1, val1);
254         connman_dbus_property_append_basic(&iter, key2, type2, val2);
255
256         g_dbus_send_message(connection, signal);
257
258         return TRUE;
259 }
260 #endif
261
262 dbus_bool_t connman_dbus_property_changed_dict(const char *path,
263                                 const char *interface, const char *key,
264                         connman_dbus_append_cb_t function, void *user_data)
265 {
266         DBusMessage *signal;
267         DBusMessageIter iter;
268
269         if (path == NULL)
270                 return FALSE;
271
272         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
273         if (signal == NULL)
274                 return FALSE;
275
276         dbus_message_iter_init_append(signal, &iter);
277         connman_dbus_property_append_dict(&iter, key, function, user_data);
278
279         g_dbus_send_message(connection, signal);
280
281         return TRUE;
282 }
283
284 dbus_bool_t connman_dbus_property_changed_array(const char *path,
285                         const char *interface, const char *key, int type,
286                         connman_dbus_append_cb_t function, void *user_data)
287 {
288         DBusMessage *signal;
289         DBusMessageIter iter;
290
291         if (path == NULL)
292                 return FALSE;
293
294         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
295         if (signal == NULL)
296                 return FALSE;
297
298         dbus_message_iter_init_append(signal, &iter);
299         connman_dbus_property_append_array(&iter, key, type,
300                                                 function, user_data);
301
302         g_dbus_send_message(connection, signal);
303
304         return TRUE;
305 }
306
307 dbus_bool_t connman_dbus_setting_changed_basic(const char *owner,
308                                 const char *path, const char *key,
309                                 int type, void *val)
310 {
311         DBusMessage *msg;
312         DBusMessageIter array, dict;
313
314         if (owner == NULL || path == NULL)
315                 return FALSE;
316
317         msg = dbus_message_new_method_call(owner, path,
318                                                 CONNMAN_NOTIFICATION_INTERFACE,
319                                                 "Update");
320         if (msg == NULL)
321                 return FALSE;
322
323         dbus_message_iter_init_append(msg, &array);
324         connman_dbus_dict_open(&array, &dict);
325
326         connman_dbus_dict_append_basic(&dict, key, type, val);
327
328         connman_dbus_dict_close(&array, &dict);
329
330         g_dbus_send_message(connection, msg);
331
332         return TRUE;
333 }
334
335 dbus_bool_t connman_dbus_setting_changed_dict(const char *owner,
336                                 const char *path, const char *key,
337                                 connman_dbus_append_cb_t function,
338                                 void *user_data)
339 {
340         DBusMessage *msg;
341         DBusMessageIter array, dict;
342
343         if (owner == NULL || path == NULL)
344                 return FALSE;
345
346         msg = dbus_message_new_method_call(owner, path,
347                                                 CONNMAN_NOTIFICATION_INTERFACE,
348                                                 "Update");
349         if (msg == NULL)
350                 return FALSE;
351
352         dbus_message_iter_init_append(msg, &array);
353         connman_dbus_dict_open(&array, &dict);
354
355         connman_dbus_dict_append_dict(&dict, key, function, user_data);
356
357         connman_dbus_dict_close(&array, &dict);
358
359         g_dbus_send_message(connection, msg);
360
361         return TRUE;
362 }
363
364 dbus_bool_t connman_dbus_setting_changed_array(const char *owner,
365                                 const char *path, const char *key, int type,
366                                 connman_dbus_append_cb_t function,
367                                 void *user_data)
368 {
369         DBusMessage *msg;
370         DBusMessageIter array, dict;
371
372         if (owner == NULL || path == NULL)
373                 return FALSE;
374
375         msg = dbus_message_new_method_call(owner, path,
376                                                 CONNMAN_NOTIFICATION_INTERFACE,
377                                                 "Update");
378         if (msg == NULL)
379                 return FALSE;
380
381         dbus_message_iter_init_append(msg, &array);
382         connman_dbus_dict_open(&array, &dict);
383
384         connman_dbus_dict_append_array(&dict, key, type, function, user_data);
385
386         connman_dbus_dict_close(&array, &dict);
387
388         g_dbus_send_message(connection, msg);
389
390         return TRUE;
391 }
392
393 #if defined TIZEN_EXT
394 /*
395  * August 22nd, 2011. TIZEN
396  *
397  * This part is added to send a DBus signal which means scan is completed
398  * because scan UX of a Wi-Fi setting application has an active scan procedure
399  * and it needs scan complete signal whether success or not
400  */
401
402 dbus_bool_t connman_dbus_scan_completed_basic(const char *path,
403                                 const char *interface, int type, void *val)
404 {
405         DBusMessage *signal;
406         DBusMessageIter iter;
407
408         if (path == NULL)
409                 return FALSE;
410
411         signal = dbus_message_new_signal(path, interface, "ScanCompleted");
412         if (signal == NULL)
413                 return FALSE;
414
415         dbus_message_iter_init_append(signal, &iter);
416         
417         dbus_message_iter_append_basic(&iter, type, val);
418
419         g_dbus_send_message(connection, signal);
420
421         return TRUE;
422 }
423 #endif
424
425 DBusConnection *connman_dbus_get_connection(void)
426 {
427         if (connection == NULL)
428                 return NULL;
429
430         return dbus_connection_ref(connection);
431 }
432
433 int __connman_dbus_init(DBusConnection *conn)
434 {
435         DBG("");
436
437         connection = conn;
438
439         return 0;
440 }
441
442 void __connman_dbus_cleanup(void)
443 {
444         DBG("");
445
446         connection = NULL;
447 }