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 {
38 static GHashTable *groups = NULL;
40 static void free_group(gpointer data)
42 struct connman_group *group = data;
44 DBG("group %p", group);
50 static struct connman_group *lookup_group(const char *name)
52 struct connman_group *group;
59 group = g_hash_table_lookup(groups, name);
63 group = g_try_new0(struct connman_group, 1);
67 group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
69 g_hash_table_insert(groups, g_strdup(name), group);
72 DBG("group %p", group);
77 int __connman_profile_add_device(struct connman_device *device)
79 struct connman_group *group;
82 DBG("device %p", device);
84 name = g_strdup_printf("device%d", connman_device_get_index(device));
85 group = lookup_group(name);
94 int __connman_profile_remove_device(struct connman_device *device)
96 struct connman_group *group;
99 DBG("device %p", device);
101 name = g_strdup_printf("device%d", connman_device_get_index(device));
102 group = lookup_group(name);
111 int __connman_profile_add_network(struct connman_network *network)
113 struct connman_group *group;
115 DBG("network %p", network);
117 group = lookup_group(__connman_network_get_group(network));
124 int __connman_profile_remove_network(struct connman_network *network)
126 struct connman_group *group;
128 DBG("network %p", network);
130 group = lookup_group(__connman_network_get_group(network));
137 const char *__connman_profile_active(void)
141 return PROFILE_DEFAULT;
144 void __connman_profile_list(DBusMessageIter *iter)
146 const char *path = __connman_profile_active();
150 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
153 static void append_path(gpointer key, gpointer value, gpointer user_data)
155 struct connman_group *group = value;
156 DBusMessageIter *iter = user_data;
158 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
162 void __connman_profile_list_services(DBusMessageIter *iter)
166 g_hash_table_foreach(groups, append_path, iter);
169 static void append_services(DBusMessageIter *dict)
171 DBusMessageIter entry, value, iter;
172 const char *key = "Services";
174 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
177 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
179 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
180 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
183 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
184 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
185 __connman_profile_list_services(&iter);
186 dbus_message_iter_close_container(&value, &iter);
188 dbus_message_iter_close_container(&entry, &value);
190 dbus_message_iter_close_container(dict, &entry);
193 static DBusMessage *get_properties(DBusConnection *conn,
194 DBusMessage *msg, void *data)
196 const char *name = "Default";
198 DBusMessageIter array, dict;
200 DBG("conn %p", conn);
202 reply = dbus_message_new_method_return(msg);
206 dbus_message_iter_init_append(reply, &array);
208 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
209 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
210 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
211 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
213 connman_dbus_dict_append_variant(&dict, "Name",
214 DBUS_TYPE_STRING, &name);
216 append_services(&dict);
218 dbus_message_iter_close_container(&array, &dict);
223 static GDBusMethodTable profile_methods[] = {
224 { "GetProperties", "", "a{sv}", get_properties },
228 static DBusConnection *connection = NULL;
230 int __connman_profile_init(DBusConnection *conn)
232 DBG("conn %p", conn);
234 connection = dbus_connection_ref(conn);
235 if (connection == NULL)
238 groups = g_hash_table_new_full(g_str_hash, g_str_equal,
241 g_dbus_register_interface(connection, PROFILE_DEFAULT,
242 CONNMAN_PROFILE_INTERFACE,
244 NULL, NULL, NULL, NULL);
249 void __connman_profile_cleanup(void)
251 DBG("conn %p", connection);
253 g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
254 CONNMAN_PROFILE_INTERFACE);
256 g_hash_table_destroy(groups);
259 if (connection == NULL)
262 dbus_connection_unref(connection);