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;
279 DBG("network %p", network);
281 if (__connman_network_get_group(network) == NULL)
284 name = g_strdup_printf("%s_%s", __connman_network_get_type(network),
285 __connman_network_get_group(network));
286 group = lookup_group(name);
295 group->type = g_strdup(__connman_network_get_type(network));
296 group->name = g_strdup(connman_network_get_string(network, "Name"));
298 group->strength = connman_network_get_uint8(network, "Strength");
300 if (group->network == NULL) {
301 group->network = network;
303 group->mode = g_strdup(connman_network_get_string(network,
305 group->security = g_strdup(connman_network_get_string(network,
312 int __connman_profile_remove_network(struct connman_network *network)
314 struct connman_group *group;
317 DBG("network %p", network);
319 if (__connman_network_get_group(network) == NULL)
322 name = g_strdup_printf("%s_%s", __connman_network_get_type(network),
323 __connman_network_get_group(network));
324 group = lookup_group(name);
330 if (group->network == network) {
331 g_free(group->security);
332 group->security = NULL;
337 group->network = NULL;
343 const char *__connman_profile_active(void)
347 return PROFILE_DEFAULT;
350 void __connman_profile_list(DBusMessageIter *iter)
352 const char *path = __connman_profile_active();
356 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
359 static void append_path(gpointer value, gpointer user_data)
361 struct connman_group *group = value;
362 DBusMessageIter *iter = user_data;
364 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
368 void __connman_profile_list_services(DBusMessageIter *iter)
372 g_sequence_foreach(groups, append_path, iter);
375 static void append_services(DBusMessageIter *dict)
377 DBusMessageIter entry, value, iter;
378 const char *key = "Services";
380 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
383 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
385 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
386 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
389 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
390 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
391 __connman_profile_list_services(&iter);
392 dbus_message_iter_close_container(&value, &iter);
394 dbus_message_iter_close_container(&entry, &value);
396 dbus_message_iter_close_container(dict, &entry);
399 static DBusMessage *profile_properties(DBusConnection *conn,
400 DBusMessage *msg, void *data)
402 const char *name = "Default";
404 DBusMessageIter array, dict;
406 DBG("conn %p", conn);
408 reply = dbus_message_new_method_return(msg);
412 dbus_message_iter_init_append(reply, &array);
414 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
415 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
416 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
417 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
419 connman_dbus_dict_append_variant(&dict, "Name",
420 DBUS_TYPE_STRING, &name);
422 append_services(&dict);
424 dbus_message_iter_close_container(&array, &dict);
429 static GDBusMethodTable profile_methods[] = {
430 { "GetProperties", "", "a{sv}", profile_properties },
434 int __connman_profile_init(DBusConnection *conn)
436 DBG("conn %p", conn);
438 connection = dbus_connection_ref(conn);
439 if (connection == NULL)
442 groups = g_sequence_new(free_group);
444 g_dbus_register_interface(connection, PROFILE_DEFAULT,
445 CONNMAN_PROFILE_INTERFACE,
447 NULL, NULL, NULL, NULL);
452 void __connman_profile_cleanup(void)
454 DBG("conn %p", connection);
456 g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
457 CONNMAN_PROFILE_INTERFACE);
459 g_sequence_free(groups);
462 if (connection == NULL)
465 dbus_connection_unref(connection);