Rename D-Bus helper for basic property types
[framework/connectivity/connman.git] / src / dbus.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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_OBJECT_PATH:
108                 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
109                 break;
110         default:
111                 signature = DBUS_TYPE_VARIANT_AS_STRING;
112                 break;
113         }
114
115         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
116                                                         signature, &value);
117         dbus_message_iter_append_basic(&value, type, val);
118         dbus_message_iter_close_container(iter, &value);
119 }
120
121 void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key,
122                         connman_dbus_append_cb_t function, void *user_data)
123 {
124         DBusMessageIter value, dict;
125
126         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
127
128         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
129                         DBUS_TYPE_ARRAY_AS_STRING
130                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
131                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
132                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &value);
133
134         connman_dbus_dict_open(&value, &dict);
135         if (function)
136                 function(&dict, user_data);
137         connman_dbus_dict_close(&value, &dict);
138
139         dbus_message_iter_close_container(iter, &value);
140 }
141
142 void connman_dbus_property_append_fixed_array(DBusMessageIter *iter,
143                                 const char *key, int type, void *val, int len)
144 {
145         DBusMessageIter value, array;
146         const char *variant_sig, *array_sig;
147
148         switch (type) {
149         case DBUS_TYPE_BYTE:
150                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
151                 array_sig = DBUS_TYPE_BYTE_AS_STRING;
152                 break;
153         default:
154                 return;
155         }
156
157         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
158
159         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
160                                                         variant_sig, &value);
161
162         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
163                                                         array_sig, &array);
164         dbus_message_iter_append_fixed_array(&array, type, val, len);
165         dbus_message_iter_close_container(&value, &array);
166
167         dbus_message_iter_close_container(iter, &value);
168 }
169
170 void connman_dbus_property_append_variable_array(DBusMessageIter *iter,
171                 const char *key, int type, connman_dbus_append_cb_t function)
172 {
173         DBusMessageIter value, array;
174         const char *variant_sig, *array_sig;
175
176         switch (type) {
177         case DBUS_TYPE_STRING:
178                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
179                 array_sig = DBUS_TYPE_STRING_AS_STRING;
180                 break;
181         case DBUS_TYPE_OBJECT_PATH:
182                 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING;
183                 array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
184                 break;
185         default:
186                 return;
187         }
188
189         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
190
191         dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
192                                                         variant_sig, &value);
193
194         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
195                                                         array_sig, &array);
196         if (function)
197                 function(&array, NULL);
198         dbus_message_iter_close_container(&value, &array);
199
200         dbus_message_iter_close_container(iter, &value);
201 }
202
203 static DBusConnection *connection = NULL;
204
205 dbus_bool_t connman_dbus_property_changed_basic(const char *path,
206                                 const char *interface, const char *key,
207                                                         int type, void *val)
208 {
209         DBusMessage *signal;
210         DBusMessageIter iter;
211
212         if (path == NULL)
213                 return FALSE;
214
215         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
216         if (signal == NULL)
217                 return FALSE;
218
219         dbus_message_iter_init_append(signal, &iter);
220         connman_dbus_property_append_basic(&iter, key, type, val);
221
222         g_dbus_send_message(connection, signal);
223
224         return TRUE;
225 }
226
227 dbus_bool_t connman_dbus_property_changed_dict(const char *path,
228                                 const char *interface, const char *key,
229                         connman_dbus_append_cb_t function, void *user_data)
230 {
231         DBusMessage *signal;
232         DBusMessageIter iter;
233
234         if (path == NULL)
235                 return FALSE;
236
237         signal = dbus_message_new_signal(path, interface, "PropertyChanged");
238         if (signal == NULL)
239                 return FALSE;
240
241         dbus_message_iter_init_append(signal, &iter);
242         connman_dbus_property_append_dict(&iter, key, function, user_data);
243
244         g_dbus_send_message(connection, signal);
245
246         return TRUE;
247 }
248
249 DBusConnection *connman_dbus_get_connection(void)
250 {
251         if (connection == NULL)
252                 return NULL;
253
254         return dbus_connection_ref(connection);
255 }
256
257 int __connman_dbus_init(DBusConnection *conn)
258 {
259         connection = conn;
260
261         return 0;
262 }
263
264 void __connman_dbus_cleanup(void)
265 {
266         connection = NULL;
267 }