5 * Copyright (C) 2007-2009 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_OBJECT_PATH:
108 signature = DBUS_TYPE_OBJECT_PATH_AS_STRING;
111 signature = DBUS_TYPE_VARIANT_AS_STRING;
115 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
117 dbus_message_iter_append_basic(&value, type, val);
118 dbus_message_iter_close_container(iter, &value);
121 void connman_dbus_property_append_dict(DBusMessageIter *iter, const char *key,
122 connman_dbus_append_cb_t function, void *user_data)
124 DBusMessageIter value, dict;
126 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
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);
134 connman_dbus_dict_open(&value, &dict);
136 function(&dict, user_data);
137 connman_dbus_dict_close(&value, &dict);
139 dbus_message_iter_close_container(iter, &value);
142 void connman_dbus_property_append_fixed_array(DBusMessageIter *iter,
143 const char *key, int type, void *val, int len)
145 DBusMessageIter value, array;
146 const char *variant_sig, *array_sig;
150 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING;
151 array_sig = DBUS_TYPE_BYTE_AS_STRING;
157 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
159 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
160 variant_sig, &value);
162 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
164 dbus_message_iter_append_fixed_array(&array, type, val, len);
165 dbus_message_iter_close_container(&value, &array);
167 dbus_message_iter_close_container(iter, &value);
170 void connman_dbus_property_append_variable_array(DBusMessageIter *iter,
171 const char *key, int type,
172 connman_dbus_append_cb_t function, void *user_data)
174 DBusMessageIter value, array;
175 const char *variant_sig, *array_sig;
178 case DBUS_TYPE_STRING:
179 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING;
180 array_sig = DBUS_TYPE_STRING_AS_STRING;
182 case DBUS_TYPE_OBJECT_PATH:
183 variant_sig = DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING;
184 array_sig = DBUS_TYPE_OBJECT_PATH_AS_STRING;
190 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &key);
192 dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
193 variant_sig, &value);
195 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
198 function(&array, user_data);
199 dbus_message_iter_close_container(&value, &array);
201 dbus_message_iter_close_container(iter, &value);
204 static DBusConnection *connection = NULL;
206 dbus_bool_t connman_dbus_property_changed_basic(const char *path,
207 const char *interface, const char *key,
211 DBusMessageIter iter;
216 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
220 dbus_message_iter_init_append(signal, &iter);
221 connman_dbus_property_append_basic(&iter, key, type, val);
223 g_dbus_send_message(connection, signal);
228 dbus_bool_t connman_dbus_property_changed_dict(const char *path,
229 const char *interface, const char *key,
230 connman_dbus_append_cb_t function, void *user_data)
233 DBusMessageIter iter;
238 signal = dbus_message_new_signal(path, interface, "PropertyChanged");
242 dbus_message_iter_init_append(signal, &iter);
243 connman_dbus_property_append_dict(&iter, key, function, user_data);
245 g_dbus_send_message(connection, signal);
250 DBusConnection *connman_dbus_get_connection(void)
252 if (connection == NULL)
255 return dbus_connection_ref(connection);
258 int __connman_dbus_init(DBusConnection *conn)
265 void __connman_dbus_cleanup(void)