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 {
39 connman_uint8_t strength;
40 connman_bool_t favorite;
41 struct connman_network *network;
44 static GHashTable *groups = NULL;
46 static DBusConnection *connection = NULL;
48 static DBusMessage *get_properties(DBusConnection *conn,
49 DBusMessage *msg, void *data)
51 struct connman_group *group = data;
53 DBusMessageIter array, dict;
57 reply = dbus_message_new_method_return(msg);
61 dbus_message_iter_init_append(reply, &array);
63 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
64 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
65 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
66 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
68 if (group->type != NULL)
69 connman_dbus_dict_append_variant(&dict, "Type",
70 DBUS_TYPE_STRING, &group->type);
72 if (group->name != NULL)
73 connman_dbus_dict_append_variant(&dict, "Name",
74 DBUS_TYPE_STRING, &group->name);
76 if (group->mode != NULL)
77 connman_dbus_dict_append_variant(&dict, "Mode",
78 DBUS_TYPE_STRING, &group->mode);
80 if (group->security != NULL)
81 connman_dbus_dict_append_variant(&dict, "Security",
82 DBUS_TYPE_STRING, &group->security);
84 if (group->strength > 0)
85 connman_dbus_dict_append_variant(&dict, "Strength",
86 DBUS_TYPE_BYTE, &group->strength);
88 connman_dbus_dict_append_variant(&dict, "Favorite",
89 DBUS_TYPE_BOOLEAN, &group->favorite);
91 dbus_message_iter_close_container(&array, &dict);
96 static DBusMessage *connect_service(DBusConnection *conn,
97 DBusMessage *msg, void *data)
99 return __connman_error_not_implemented(msg);
102 static DBusMessage *disconnect_service(DBusConnection *conn,
103 DBusMessage *msg, void *data)
105 return __connman_error_not_implemented(msg);
108 static DBusMessage *remove_service(DBusConnection *conn,
109 DBusMessage *msg, void *data)
111 return __connman_error_not_implemented(msg);
114 static DBusMessage *move_before(DBusConnection *conn,
115 DBusMessage *msg, void *data)
117 return __connman_error_not_implemented(msg);
120 static DBusMessage *move_after(DBusConnection *conn,
121 DBusMessage *msg, void *data)
123 return __connman_error_not_implemented(msg);
126 static GDBusMethodTable service_methods[] = {
127 { "GetProperties", "", "a{sv}", get_properties },
128 { "Connect", "", "", connect_service },
129 { "Disconnect", "", "", disconnect_service },
130 { "Remove", "", "", remove_service },
131 { "MoveBefore", "o", "", move_before },
132 { "MoveAfter", "o", "", move_after },
136 static GDBusSignalTable service_signals[] = {
137 { "PropertyChanged", "sv" },
141 static void free_group(gpointer data)
143 struct connman_group *group = data;
145 DBG("group %p", group);
147 g_dbus_unregister_interface(connection, group->path,
148 CONNMAN_SERVICE_INTERFACE);
150 g_free(group->security);
158 static struct connman_group *lookup_group(const char *name)
160 struct connman_group *group;
162 DBG("name %s", name);
167 group = g_hash_table_lookup(groups, name);
171 group = g_try_new0(struct connman_group, 1);
175 group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
176 group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
178 group->favorite = FALSE;
180 g_hash_table_insert(groups, g_strdup(name), group);
182 g_dbus_register_interface(connection, group->path,
183 CONNMAN_SERVICE_INTERFACE,
184 service_methods, service_signals,
188 DBG("group %p", group);
193 int __connman_profile_add_device(struct connman_device *device)
195 struct connman_group *group;
198 DBG("device %p", device);
200 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
201 connman_device_get_index(device));
202 group = lookup_group(name);
208 group->type = g_strdup(__connman_device_get_type(device));
213 int __connman_profile_remove_device(struct connman_device *device)
215 struct connman_group *group;
218 DBG("device %p", device);
220 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
221 connman_device_get_index(device));
222 group = lookup_group(name);
231 int __connman_profile_add_network(struct connman_network *network)
233 struct connman_group *group;
235 DBG("network %p", network);
237 group = lookup_group(__connman_network_get_group(network));
244 group->type = g_strdup(__connman_network_get_type(network));
245 group->name = g_strdup(connman_network_get_string(network, "Name"));
247 group->strength = connman_network_get_uint8(network, "Strength");
249 if (group->network == NULL) {
250 group->network = network;
252 group->mode = g_strdup(connman_network_get_string(network,
254 group->security = g_strdup(connman_network_get_string(network,
261 int __connman_profile_remove_network(struct connman_network *network)
263 struct connman_group *group;
265 DBG("network %p", network);
267 group = lookup_group(__connman_network_get_group(network));
271 if (group->network == network) {
272 g_free(group->security);
273 group->security = NULL;
278 group->network = NULL;
284 const char *__connman_profile_active(void)
288 return PROFILE_DEFAULT;
291 void __connman_profile_list(DBusMessageIter *iter)
293 const char *path = __connman_profile_active();
297 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
300 static void append_path(gpointer key, gpointer value, gpointer user_data)
302 struct connman_group *group = value;
303 DBusMessageIter *iter = user_data;
305 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
309 void __connman_profile_list_services(DBusMessageIter *iter)
313 g_hash_table_foreach(groups, append_path, iter);
316 static void append_services(DBusMessageIter *dict)
318 DBusMessageIter entry, value, iter;
319 const char *key = "Services";
321 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
324 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
326 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
327 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
330 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
331 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
332 __connman_profile_list_services(&iter);
333 dbus_message_iter_close_container(&value, &iter);
335 dbus_message_iter_close_container(&entry, &value);
337 dbus_message_iter_close_container(dict, &entry);
340 static DBusMessage *profile_properties(DBusConnection *conn,
341 DBusMessage *msg, void *data)
343 const char *name = "Default";
345 DBusMessageIter array, dict;
347 DBG("conn %p", conn);
349 reply = dbus_message_new_method_return(msg);
353 dbus_message_iter_init_append(reply, &array);
355 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
356 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
357 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
358 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
360 connman_dbus_dict_append_variant(&dict, "Name",
361 DBUS_TYPE_STRING, &name);
363 append_services(&dict);
365 dbus_message_iter_close_container(&array, &dict);
370 static GDBusMethodTable profile_methods[] = {
371 { "GetProperties", "", "a{sv}", profile_properties },
375 int __connman_profile_init(DBusConnection *conn)
377 DBG("conn %p", conn);
379 connection = dbus_connection_ref(conn);
380 if (connection == NULL)
383 groups = g_hash_table_new_full(g_str_hash, g_str_equal,
386 g_dbus_register_interface(connection, PROFILE_DEFAULT,
387 CONNMAN_PROFILE_INTERFACE,
389 NULL, NULL, NULL, NULL);
394 void __connman_profile_cleanup(void)
396 DBG("conn %p", connection);
398 g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
399 CONNMAN_PROFILE_INTERFACE);
401 g_hash_table_destroy(groups);
404 if (connection == NULL)
407 dbus_connection_unref(connection);