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 #define PROFILE_DEFAULT "/profile/default"
33 struct connman_group {
40 static GHashTable *groups = NULL;
42 static DBusConnection *connection = NULL;
44 static DBusMessage *get_properties(DBusConnection *conn,
45 DBusMessage *msg, void *data)
47 struct connman_group *group = data;
49 DBusMessageIter array, dict;
53 reply = dbus_message_new_method_return(msg);
57 dbus_message_iter_init_append(reply, &array);
59 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
60 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
61 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
62 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
64 if (group->type != NULL)
65 connman_dbus_dict_append_variant(&dict, "Type",
66 DBUS_TYPE_STRING, &group->type);
68 if (group->name != NULL)
69 connman_dbus_dict_append_variant(&dict, "Name",
70 DBUS_TYPE_STRING, &group->name);
72 dbus_message_iter_close_container(&array, &dict);
77 static GDBusMethodTable service_methods[] = {
78 { "GetProperties", "", "a{sv}", get_properties },
82 static void free_group(gpointer data)
84 struct connman_group *group = data;
86 DBG("group %p", group);
88 g_dbus_unregister_interface(connection, group->path,
89 CONNMAN_SERVICE_INTERFACE);
97 static struct connman_group *lookup_group(const char *name)
99 struct connman_group *group;
101 DBG("name %s", name);
106 group = g_hash_table_lookup(groups, name);
110 group = g_try_new0(struct connman_group, 1);
114 group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
115 group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
117 g_hash_table_insert(groups, g_strdup(name), group);
119 g_dbus_register_interface(connection, group->path,
120 CONNMAN_SERVICE_INTERFACE,
122 NULL, NULL, group, NULL);
125 DBG("group %p", group);
130 int __connman_profile_add_device(struct connman_device *device)
132 struct connman_group *group;
135 DBG("device %p", device);
137 name = g_strdup_printf("device%d", connman_device_get_index(device));
138 group = lookup_group(name);
144 group->type = g_strdup(__connman_device_get_type(device));
145 group->name = g_strdup(connman_device_get_string(device, "Name"));
150 int __connman_profile_remove_device(struct connman_device *device)
152 struct connman_group *group;
155 DBG("device %p", device);
157 name = g_strdup_printf("device%d", connman_device_get_index(device));
158 group = lookup_group(name);
167 int __connman_profile_add_network(struct connman_network *network)
169 struct connman_group *group;
171 DBG("network %p", network);
173 group = lookup_group(__connman_network_get_group(network));
180 group->type = g_strdup(__connman_network_get_type(network));
181 group->name = g_strdup(connman_network_get_string(network, "Name"));
186 int __connman_profile_remove_network(struct connman_network *network)
188 struct connman_group *group;
190 DBG("network %p", network);
192 group = lookup_group(__connman_network_get_group(network));
199 const char *__connman_profile_active(void)
203 return PROFILE_DEFAULT;
206 void __connman_profile_list(DBusMessageIter *iter)
208 const char *path = __connman_profile_active();
212 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
215 static void append_path(gpointer key, gpointer value, gpointer user_data)
217 struct connman_group *group = value;
218 DBusMessageIter *iter = user_data;
220 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
224 void __connman_profile_list_services(DBusMessageIter *iter)
228 g_hash_table_foreach(groups, append_path, iter);
231 static void append_services(DBusMessageIter *dict)
233 DBusMessageIter entry, value, iter;
234 const char *key = "Services";
236 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
239 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
241 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
242 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
245 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
246 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
247 __connman_profile_list_services(&iter);
248 dbus_message_iter_close_container(&value, &iter);
250 dbus_message_iter_close_container(&entry, &value);
252 dbus_message_iter_close_container(dict, &entry);
255 static DBusMessage *profile_properties(DBusConnection *conn,
256 DBusMessage *msg, void *data)
258 const char *name = "Default";
260 DBusMessageIter array, dict;
262 DBG("conn %p", conn);
264 reply = dbus_message_new_method_return(msg);
268 dbus_message_iter_init_append(reply, &array);
270 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
271 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
272 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
273 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
275 connman_dbus_dict_append_variant(&dict, "Name",
276 DBUS_TYPE_STRING, &name);
278 append_services(&dict);
280 dbus_message_iter_close_container(&array, &dict);
285 static GDBusMethodTable profile_methods[] = {
286 { "GetProperties", "", "a{sv}", profile_properties },
290 int __connman_profile_init(DBusConnection *conn)
292 DBG("conn %p", conn);
294 connection = dbus_connection_ref(conn);
295 if (connection == NULL)
298 groups = g_hash_table_new_full(g_str_hash, g_str_equal,
301 g_dbus_register_interface(connection, PROFILE_DEFAULT,
302 CONNMAN_PROFILE_INTERFACE,
304 NULL, NULL, NULL, NULL);
309 void __connman_profile_cleanup(void)
311 DBG("conn %p", connection);
313 g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
314 CONNMAN_PROFILE_INTERFACE);
316 g_hash_table_destroy(groups);
319 if (connection == NULL)
322 dbus_connection_unref(connection);