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 enum connman_service_state {
34 CONNMAN_SERVICE_STATE_UNKNOWN = 0,
35 CONNMAN_SERVICE_STATE_IDLE = 1,
38 struct connman_group {
44 connman_uint8_t strength;
45 connman_bool_t favorite;
46 enum connman_service_state state;
47 struct connman_network *network;
50 static GHashTable *groups = NULL;
52 static DBusConnection *connection = NULL;
54 static const char *state2string(enum connman_service_state state)
57 case CONNMAN_SERVICE_STATE_UNKNOWN:
59 case CONNMAN_SERVICE_STATE_IDLE:
66 static DBusMessage *get_properties(DBusConnection *conn,
67 DBusMessage *msg, void *data)
69 struct connman_group *group = data;
71 DBusMessageIter array, dict;
76 reply = dbus_message_new_method_return(msg);
80 dbus_message_iter_init_append(reply, &array);
82 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
83 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
84 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
85 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
87 str = state2string(group->state);
89 connman_dbus_dict_append_variant(&dict, "State",
90 DBUS_TYPE_STRING, &str);
92 if (group->type != NULL)
93 connman_dbus_dict_append_variant(&dict, "Type",
94 DBUS_TYPE_STRING, &group->type);
96 if (group->name != NULL)
97 connman_dbus_dict_append_variant(&dict, "Name",
98 DBUS_TYPE_STRING, &group->name);
100 if (group->mode != NULL)
101 connman_dbus_dict_append_variant(&dict, "Mode",
102 DBUS_TYPE_STRING, &group->mode);
104 if (group->security != NULL)
105 connman_dbus_dict_append_variant(&dict, "Security",
106 DBUS_TYPE_STRING, &group->security);
108 if (group->strength > 0)
109 connman_dbus_dict_append_variant(&dict, "Strength",
110 DBUS_TYPE_BYTE, &group->strength);
112 connman_dbus_dict_append_variant(&dict, "Favorite",
113 DBUS_TYPE_BOOLEAN, &group->favorite);
115 dbus_message_iter_close_container(&array, &dict);
120 static DBusMessage *connect_service(DBusConnection *conn,
121 DBusMessage *msg, void *data)
123 return __connman_error_not_implemented(msg);
126 static DBusMessage *disconnect_service(DBusConnection *conn,
127 DBusMessage *msg, void *data)
129 return __connman_error_not_implemented(msg);
132 static DBusMessage *remove_service(DBusConnection *conn,
133 DBusMessage *msg, void *data)
135 return __connman_error_not_implemented(msg);
138 static DBusMessage *move_before(DBusConnection *conn,
139 DBusMessage *msg, void *data)
141 return __connman_error_not_implemented(msg);
144 static DBusMessage *move_after(DBusConnection *conn,
145 DBusMessage *msg, void *data)
147 return __connman_error_not_implemented(msg);
150 static GDBusMethodTable service_methods[] = {
151 { "GetProperties", "", "a{sv}", get_properties },
152 { "Connect", "", "", connect_service },
153 { "Disconnect", "", "", disconnect_service },
154 { "Remove", "", "", remove_service },
155 { "MoveBefore", "o", "", move_before },
156 { "MoveAfter", "o", "", move_after },
160 static GDBusSignalTable service_signals[] = {
161 { "PropertyChanged", "sv" },
165 static void free_group(gpointer data)
167 struct connman_group *group = data;
169 DBG("group %p", group);
171 g_dbus_unregister_interface(connection, group->path,
172 CONNMAN_SERVICE_INTERFACE);
174 g_free(group->security);
182 static struct connman_group *lookup_group(const char *name)
184 struct connman_group *group;
186 DBG("name %s", name);
191 group = g_hash_table_lookup(groups, name);
195 group = g_try_new0(struct connman_group, 1);
199 group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
200 group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
202 group->favorite = FALSE;
204 group->state = CONNMAN_SERVICE_STATE_IDLE;
206 g_hash_table_insert(groups, g_strdup(name), group);
208 g_dbus_register_interface(connection, group->path,
209 CONNMAN_SERVICE_INTERFACE,
210 service_methods, service_signals,
214 DBG("group %p", group);
219 int __connman_profile_add_device(struct connman_device *device)
221 struct connman_group *group;
224 DBG("device %p", device);
226 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
227 connman_device_get_index(device));
228 group = lookup_group(name);
234 group->type = g_strdup(__connman_device_get_type(device));
239 int __connman_profile_remove_device(struct connman_device *device)
241 struct connman_group *group;
244 DBG("device %p", device);
246 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
247 connman_device_get_index(device));
248 group = lookup_group(name);
257 int __connman_profile_add_network(struct connman_network *network)
259 struct connman_group *group;
261 DBG("network %p", network);
263 group = lookup_group(__connman_network_get_group(network));
270 group->type = g_strdup(__connman_network_get_type(network));
271 group->name = g_strdup(connman_network_get_string(network, "Name"));
273 group->strength = connman_network_get_uint8(network, "Strength");
275 if (group->network == NULL) {
276 group->network = network;
278 group->mode = g_strdup(connman_network_get_string(network,
280 group->security = g_strdup(connman_network_get_string(network,
287 int __connman_profile_remove_network(struct connman_network *network)
289 struct connman_group *group;
291 DBG("network %p", network);
293 group = lookup_group(__connman_network_get_group(network));
297 if (group->network == network) {
298 g_free(group->security);
299 group->security = NULL;
304 group->network = NULL;
310 const char *__connman_profile_active(void)
314 return PROFILE_DEFAULT;
317 void __connman_profile_list(DBusMessageIter *iter)
319 const char *path = __connman_profile_active();
323 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
326 static void append_path(gpointer key, gpointer value, gpointer user_data)
328 struct connman_group *group = value;
329 DBusMessageIter *iter = user_data;
331 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
335 void __connman_profile_list_services(DBusMessageIter *iter)
339 g_hash_table_foreach(groups, append_path, iter);
342 static void append_services(DBusMessageIter *dict)
344 DBusMessageIter entry, value, iter;
345 const char *key = "Services";
347 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
350 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
352 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
353 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
356 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
357 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
358 __connman_profile_list_services(&iter);
359 dbus_message_iter_close_container(&value, &iter);
361 dbus_message_iter_close_container(&entry, &value);
363 dbus_message_iter_close_container(dict, &entry);
366 static DBusMessage *profile_properties(DBusConnection *conn,
367 DBusMessage *msg, void *data)
369 const char *name = "Default";
371 DBusMessageIter array, dict;
373 DBG("conn %p", conn);
375 reply = dbus_message_new_method_return(msg);
379 dbus_message_iter_init_append(reply, &array);
381 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
382 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
383 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
384 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
386 connman_dbus_dict_append_variant(&dict, "Name",
387 DBUS_TYPE_STRING, &name);
389 append_services(&dict);
391 dbus_message_iter_close_container(&array, &dict);
396 static GDBusMethodTable profile_methods[] = {
397 { "GetProperties", "", "a{sv}", profile_properties },
401 int __connman_profile_init(DBusConnection *conn)
403 DBG("conn %p", conn);
405 connection = dbus_connection_ref(conn);
406 if (connection == NULL)
409 groups = g_hash_table_new_full(g_str_hash, g_str_equal,
412 g_dbus_register_interface(connection, PROFILE_DEFAULT,
413 CONNMAN_PROFILE_INTERFACE,
415 NULL, NULL, NULL, NULL);
420 void __connman_profile_cleanup(void)
422 DBG("conn %p", connection);
424 g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
425 CONNMAN_PROFILE_INTERFACE);
427 g_hash_table_destroy(groups);
430 if (connection == NULL)
433 dbus_connection_unref(connection);