service: Save/load service hidden status
[platform/upstream/connman.git] / src / dbus.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 dbus_bool_t connman_dbus_property_changed_dict(const char *path,
235                                 const char *interface, const char *key,
236                         connman_dbus_append_cb_t function, void *user_data)
237 {
238         DBusMessage *signal;
239         DBusMessageIter iter;
240
241         if (path == NULL)
242                 return FALSE;
243
244         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
245         if (signal == NULL)
246                 return FALSE;
247
248         dbus_message_iter_init_append(signal, &iter);
249         connman_dbus_property_append_dict(&iter, key, function, user_data);
250
251         g_dbus_send_message(connection, signal);
252
253         return TRUE;
254 }
255
256 dbus_bool_t connman_dbus_property_changed_array(const char *path,
257                         const char *interface, const char *key, int type,
258                         connman_dbus_append_cb_t function, void *user_data)
259 {
260         DBusMessage *signal;
261         DBusMessageIter iter;
262
263         if (path == NULL)
264                 return FALSE;
265
266         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
267         if (signal == NULL)
268                 return FALSE;
269
270         dbus_message_iter_init_append(signal, &iter);
271         connman_dbus_property_append_array(&iter, key, type,
272                                                 function, user_data);
273
274         g_dbus_send_message(connection, signal);
275
276         return TRUE;
277 }
278
279 dbus_bool_t connman_dbus_setting_changed_basic(const char *owner,
280                                 const char *path, const char *key,
281                                 int type, void *val)
282 {
283         DBusMessage *msg;
284         DBusMessageIter array, dict;
285
286         if (owner == NULL || path == NULL)
287                 return FALSE;
288
289         msg = dbus_message_new_method_call(owner, path,
290                                                 CONNMAN_NOTIFICATION_INTERFACE,
291                                                 "Update");
292         if (msg == NULL)
293                 return FALSE;
294
295         dbus_message_iter_init_append(msg, &array);
296         connman_dbus_dict_open(&array, &dict);
297
298         connman_dbus_dict_append_basic(&dict, key, type, val);
299
300         connman_dbus_dict_close(&array, &dict);
301
302         g_dbus_send_message(connection, msg);
303
304         return TRUE;
305 }
306
307 dbus_bool_t connman_dbus_setting_changed_dict(const char *owner,
308                                 const char *path, const char *key,
309                                 connman_dbus_append_cb_t function,
310                                 void *user_data)
311 {
312         DBusMessage *msg;
313         DBusMessageIter array, dict;
314
315         if (owner == NULL || path == NULL)
316                 return FALSE;
317
318         msg = dbus_message_new_method_call(owner, path,
319                                                 CONNMAN_NOTIFICATION_INTERFACE,
320                                                 "Update");
321         if (msg == NULL)
322                 return FALSE;
323
324         dbus_message_iter_init_append(msg, &array);
325         connman_dbus_dict_open(&array, &dict);
326
327         connman_dbus_dict_append_dict(&dict, key, function, user_data);
328
329         connman_dbus_dict_close(&array, &dict);
330
331         g_dbus_send_message(connection, msg);
332
333         return TRUE;
334 }
335
336 dbus_bool_t connman_dbus_setting_changed_array(const char *owner,
337                                 const char *path, const char *key, int type,
338                                 connman_dbus_append_cb_t function,
339                                 void *user_data)
340 {
341         DBusMessage *msg;
342         DBusMessageIter array, dict;
343
344         if (owner == NULL || path == NULL)
345                 return FALSE;
346
347         msg = dbus_message_new_method_call(owner, path,
348                                                 CONNMAN_NOTIFICATION_INTERFACE,
349                                                 "Update");
350         if (msg == NULL)
351                 return FALSE;
352
353         dbus_message_iter_init_append(msg, &array);
354         connman_dbus_dict_open(&array, &dict);
355
356         connman_dbus_dict_append_array(&dict, key, type, function, user_data);
357
358         connman_dbus_dict_close(&array, &dict);
359
360         g_dbus_send_message(connection, msg);
361
362         return TRUE;
363 }
364
365 dbus_bool_t __connman_dbus_append_objpath_dict_array(DBusMessage *msg,
366                 connman_dbus_append_cb_t function, void *user_data)
367 {
368         DBusMessageIter iter, array;
369
370         if (msg == NULL || function == NULL)
371                 return FALSE;
372
373         dbus_message_iter_init_append(msg, &iter);
374         dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
375                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
376                         DBUS_TYPE_OBJECT_PATH_AS_STRING
377                         DBUS_TYPE_ARRAY_AS_STRING
378                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
379                                         DBUS_TYPE_STRING_AS_STRING
380                                         DBUS_TYPE_VARIANT_AS_STRING
381                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING
382                         DBUS_STRUCT_END_CHAR_AS_STRING, &array);
383
384         function(&array, user_data);
385
386         dbus_message_iter_close_container(&iter, &array);
387
388         return TRUE;
389 }
390
391 DBusConnection *connman_dbus_get_connection(void)
392 {
393         if (connection == NULL)
394                 return NULL;
395
396         return dbus_connection_ref(connection);
397 }
398
399 int __connman_dbus_init(DBusConnection *conn)
400 {
401         DBG("");
402
403         connection = conn;
404
405         return 0;
406 }
407
408 void __connman_dbus_cleanup(void)
409 {
410         DBG("");
411
412         connection = NULL;
413 }