5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
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.
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.
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
31 dbus_bool_t connman_dbus_validate_ident(const char *ident)
38 for (i = 0; i < strlen(ident); i++) {
39 if (ident[i] >= '0' && ident[i] <= '9')
41 if (ident[i] >= 'a' && ident[i] <= 'z')
43 if (ident[i] >= 'A' && ident[i] <= 'Z')
51 char *connman_dbus_encode_string(const char *value)
61 str = g_string_new(NULL);
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);
71 str = g_string_append_c(str, tmp);
74 return g_string_free(str, FALSE);
77 void connman_dbus_property_append_basic(DBusMessageIter *iter,
78 const char *key, int type, void *val)
80 DBusMessageIter value;
81 const char *signature;
83 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
86 case DBUS_TYPE_BOOLEAN:
87 signature = DBUS_TYPE_BOOLEAN_AS_STRING;
89 case DBUS_TYPE_STRING:
90 signature = DBUS_TYPE_STRING_AS_STRING;
93 signature = DBUS_TYPE_BYTE_AS_STRING;
95 case DBUS_TYPE_UINT16:
96 signature = DBUS_TYPE_UINT16_AS_STRING;
99 signature = DBUS_TYPE_INT16_AS_STRING;
101 case DBUS_TYPE_UINT32:
102 signature = DBUS_TYPE_UINT32_AS_STRING;
104 case DBUS_TYPE_INT32:
105 signature = DBUS_TYPE_INT32_AS_STRING;
107 case DBUS_TYPE_UINT64:
108 signature = DBUS_TYPE_UINT64_AS_STRING;
110 case DBUS_TYPE_INT64:
111 signature = DBUS_TYPE_INT64_AS_STRING;
113 case DBUS_TYPE_OBJECT_PATH:
114 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
117 signature = DBUS_TYPE_VARIANT_AS_STRING;
121 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
123 dbus_message_iter_append_basic(&value, type, val);
124 dbus_message_iter_close_container(iter, &value);
127 void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key,
128 connman_dbus_append_cb_t function, void *user_data)
130 DBusMessageIter value, dict;
132 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
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);
140 connman_dbus_dict_open(&value, &dict);
142 function(&dict, user_data);
143 connman_dbus_dict_close(&value, &dict);
145 dbus_message_iter_close_container(iter, &value);
148 void connman_dbus_property_append_fixed_array(DBusMessageIter *iter,
149 const char *key, int type, void *val, int len)
151 DBusMessageIter value, array;
152 const char *variant_sig, *array_sig;
156 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
157 array_sig = DBUS_TYPE_BYTE_AS_STRING;
163 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
165 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
166 variant_sig, &value);
168 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
170 dbus_message_iter_append_fixed_array(&array, type, val, len);
171 dbus_message_iter_close_container(&value, &array);
173 dbus_message_iter_close_container(iter, &value);
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)
180 DBusMessageIter value, array;
181 const char *variant_sig, *array_sig;
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;
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;
196 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
198 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
199 variant_sig, &value);
201 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
204 function(&array, user_data);
205 dbus_message_iter_close_container(&value, &array);
207 dbus_message_iter_close_container(iter, &value);
210 static DBusConnection *connection = NULL;
212 dbus_bool_t connman_dbus_property_changed_basic(const char *path,
213 const char *interface, const char *key,
217 DBusMessageIter iter;
222 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
226 dbus_message_iter_init_append(signal, &iter);
227 connman_dbus_property_append_basic(&iter, key, type, val);
229 g_dbus_send_message(connection, signal);
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)
239 DBusMessageIter iter;
244 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
248 dbus_message_iter_init_append(signal, &iter);
249 connman_dbus_property_append_dict(&iter, key, function, user_data);
251 g_dbus_send_message(connection, signal);
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)
261 DBusMessageIter iter;
266 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
270 dbus_message_iter_init_append(signal, &iter);
271 connman_dbus_property_append_array(&iter, key, type,
272 function, user_data);
274 g_dbus_send_message(connection, signal);
279 dbus_bool_t connman_dbus_setting_changed_basic(const char *owner,
280 const char *path, const char *key,
284 DBusMessageIter array, dict;
286 if (owner == NULL || path == NULL)
289 msg = dbus_message_new_method_call(owner, path,
290 CONNMAN_NOTIFICATION_INTERFACE,
295 dbus_message_iter_init_append(msg, &array);
296 connman_dbus_dict_open(&array, &dict);
298 connman_dbus_dict_append_basic(&dict, key, type, val);
300 connman_dbus_dict_close(&array, &dict);
302 g_dbus_send_message(connection, msg);
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,
313 DBusMessageIter array, dict;
315 if (owner == NULL || path == NULL)
318 msg = dbus_message_new_method_call(owner, path,
319 CONNMAN_NOTIFICATION_INTERFACE,
324 dbus_message_iter_init_append(msg, &array);
325 connman_dbus_dict_open(&array, &dict);
327 connman_dbus_dict_append_dict(&dict, key, function, user_data);
329 connman_dbus_dict_close(&array, &dict);
331 g_dbus_send_message(connection, msg);
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,
342 DBusMessageIter array, dict;
344 if (owner == NULL || path == NULL)
347 msg = dbus_message_new_method_call(owner, path,
348 CONNMAN_NOTIFICATION_INTERFACE,
353 dbus_message_iter_init_append(msg, &array);
354 connman_dbus_dict_open(&array, &dict);
356 connman_dbus_dict_append_array(&dict, key, type, function, user_data);
358 connman_dbus_dict_close(&array, &dict);
360 g_dbus_send_message(connection, msg);
365 dbus_bool_t __connman_dbus_append_objpath_dict_array(DBusMessage *msg,
366 connman_dbus_append_cb_t function, void *user_data)
368 DBusMessageIter iter, array;
370 if (msg == NULL || function == NULL)
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);
384 function(&array, user_data);
386 dbus_message_iter_close_container(&iter, &array);
391 DBusConnection *connman_dbus_get_connection(void)
393 if (connection == NULL)
396 return dbus_connection_ref(connection);
399 int __connman_dbus_init(DBusConnection *conn)
408 void __connman_dbus_cleanup(void)