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 {
46 connman_uint8_t strength;
47 connman_bool_t favorite;
48 enum connman_service_state state;
49 struct connman_network *network;
52 static GSequence *groups = NULL;
54 static DBusConnection *connection = NULL;
56 static const char *state2string(enum connman_service_state state)
59 case CONNMAN_SERVICE_STATE_UNKNOWN:
61 case CONNMAN_SERVICE_STATE_IDLE:
68 static DBusMessage *get_properties(DBusConnection *conn,
69 DBusMessage *msg, void *data)
71 struct connman_group *group = data;
73 DBusMessageIter array, dict;
78 reply = dbus_message_new_method_return(msg);
82 dbus_message_iter_init_append(reply, &array);
84 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
85 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
86 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
87 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
89 str = state2string(group->state);
91 connman_dbus_dict_append_variant(&dict, "State",
92 DBUS_TYPE_STRING, &str);
94 if (group->type != NULL)
95 connman_dbus_dict_append_variant(&dict, "Type",
96 DBUS_TYPE_STRING, &group->type);
98 if (group->name != NULL)
99 connman_dbus_dict_append_variant(&dict, "Name",
100 DBUS_TYPE_STRING, &group->name);
102 if (group->mode != NULL)
103 connman_dbus_dict_append_variant(&dict, "Mode",
104 DBUS_TYPE_STRING, &group->mode);
106 if (group->security != NULL)
107 connman_dbus_dict_append_variant(&dict, "Security",
108 DBUS_TYPE_STRING, &group->security);
110 if (group->strength > 0)
111 connman_dbus_dict_append_variant(&dict, "Strength",
112 DBUS_TYPE_BYTE, &group->strength);
114 connman_dbus_dict_append_variant(&dict, "Favorite",
115 DBUS_TYPE_BOOLEAN, &group->favorite);
117 dbus_message_iter_close_container(&array, &dict);
122 static DBusMessage *connect_service(DBusConnection *conn,
123 DBusMessage *msg, void *data)
125 return __connman_error_not_implemented(msg);
128 static DBusMessage *disconnect_service(DBusConnection *conn,
129 DBusMessage *msg, void *data)
131 return __connman_error_not_implemented(msg);
134 static DBusMessage *remove_service(DBusConnection *conn,
135 DBusMessage *msg, void *data)
137 return __connman_error_not_implemented(msg);
140 static DBusMessage *move_before(DBusConnection *conn,
141 DBusMessage *msg, void *data)
143 return __connman_error_not_implemented(msg);
146 static DBusMessage *move_after(DBusConnection *conn,
147 DBusMessage *msg, void *data)
149 return __connman_error_not_implemented(msg);
152 static GDBusMethodTable service_methods[] = {
153 { "GetProperties", "", "a{sv}", get_properties },
154 { "Connect", "", "", connect_service },
155 { "Disconnect", "", "", disconnect_service },
156 { "Remove", "", "", remove_service },
157 { "MoveBefore", "o", "", move_before },
158 { "MoveAfter", "o", "", move_after },
162 static GDBusSignalTable service_signals[] = {
163 { "PropertyChanged", "sv" },
167 static void free_group(gpointer data)
169 struct connman_group *group = data;
171 DBG("group %p", group);
173 g_dbus_unregister_interface(connection, group->path,
174 CONNMAN_SERVICE_INTERFACE);
176 g_free(group->security);
185 static gint compare_group(gconstpointer a, gconstpointer b, gpointer user_data)
187 struct connman_group *group = (void *) a;
189 return g_strcmp0(group->id, user_data);
192 static struct connman_group *lookup_group(const char *name)
195 struct connman_group *group;
197 DBG("name %s", name);
202 iter = g_sequence_search(groups, NULL, compare_group, (char *) name);
203 if (g_sequence_iter_is_begin(iter) == FALSE &&
204 g_sequence_iter_is_end(iter) == FALSE) {
205 group = g_sequence_get(iter);
210 group = g_try_new0(struct connman_group, 1);
214 group->id = g_strdup(name);
216 group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
217 group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
219 group->favorite = FALSE;
221 group->state = CONNMAN_SERVICE_STATE_IDLE;
223 group->iter = g_sequence_append(groups, group);
225 g_dbus_register_interface(connection, group->path,
226 CONNMAN_SERVICE_INTERFACE,
227 service_methods, service_signals,
231 DBG("group %p", group);
236 int __connman_profile_add_device(struct connman_device *device)
238 struct connman_group *group;
241 DBG("device %p", device);
243 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
244 connman_device_get_index(device));
245 group = lookup_group(name);
251 group->type = g_strdup(__connman_device_get_type(device));
256 int __connman_profile_remove_device(struct connman_device *device)
258 struct connman_group *group;
261 DBG("device %p", device);
263 name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
264 connman_device_get_index(device));
265 group = lookup_group(name);
274 int __connman_profile_add_network(struct connman_network *network)
276 struct connman_group *group;
278 DBG("network %p", network);
280 group = lookup_group(__connman_network_get_group(network));
287 group->type = g_strdup(__connman_network_get_type(network));
288 group->name = g_strdup(connman_network_get_string(network, "Name"));
290 group->strength = connman_network_get_uint8(network, "Strength");
292 if (group->network == NULL) {
293 group->network = network;
295 group->mode = g_strdup(connman_network_get_string(network,
297 group->security = g_strdup(connman_network_get_string(network,
304 int __connman_profile_remove_network(struct connman_network *network)
306 struct connman_group *group;
308 DBG("network %p", network);
310 group = lookup_group(__connman_network_get_group(network));
314 if (group->network == network) {
315 g_free(group->security);
316 group->security = NULL;
321 group->network = NULL;
327 const char *__connman_profile_active(void)
331 return PROFILE_DEFAULT;
334 void __connman_profile_list(DBusMessageIter *iter)
336 const char *path = __connman_profile_active();
340 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
343 static void append_path(gpointer value, gpointer user_data)
345 struct connman_group *group = value;
346 DBusMessageIter *iter = user_data;
348 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
352 void __connman_profile_list_services(DBusMessageIter *iter)
356 g_sequence_foreach(groups, append_path, iter);
359 static void append_services(DBusMessageIter *dict)
361 DBusMessageIter entry, value, iter;
362 const char *key = "Services";
364 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
367 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
369 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
370 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
373 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
374 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
375 __connman_profile_list_services(&iter);
376 dbus_message_iter_close_container(&value, &iter);
378 dbus_message_iter_close_container(&entry, &value);
380 dbus_message_iter_close_container(dict, &entry);
383 static DBusMessage *profile_properties(DBusConnection *conn,
384 DBusMessage *msg, void *data)
386 const char *name = "Default";
388 DBusMessageIter array, dict;
390 DBG("conn %p", conn);
392 reply = dbus_message_new_method_return(msg);
396 dbus_message_iter_init_append(reply, &array);
398 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
399 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
400 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
401 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
403 connman_dbus_dict_append_variant(&dict, "Name",
404 DBUS_TYPE_STRING, &name);
406 append_services(&dict);
408 dbus_message_iter_close_container(&array, &dict);
413 static GDBusMethodTable profile_methods[] = {
414 { "GetProperties", "", "a{sv}", profile_properties },
418 int __connman_profile_init(DBusConnection *conn)
420 DBG("conn %p", conn);
422 connection = dbus_connection_ref(conn);
423 if (connection == NULL)
426 groups = g_sequence_new(free_group);
428 g_dbus_register_interface(connection, PROFILE_DEFAULT,
429 CONNMAN_PROFILE_INTERFACE,
431 NULL, NULL, NULL, NULL);
436 void __connman_profile_cleanup(void)
438 DBG("conn %p", connection);
440 g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
441 CONNMAN_PROFILE_INTERFACE);
443 g_sequence_free(groups);
446 if (connection == NULL)
449 dbus_connection_unref(connection);