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
33 #define PROFILE_DEFAULT_IDENT "default"
35 struct connman_profile {
39 connman_bool_t offlinemode;
42 static GHashTable *profile_hash = NULL;
43 static struct connman_profile *default_profile = NULL;
45 static DBusConnection *connection = NULL;
47 static void append_path(gpointer key, gpointer value, gpointer user_data)
49 struct connman_profile *profile = value;
50 DBusMessageIter *iter = user_data;
52 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
56 void __connman_profile_list(DBusMessageIter *iter)
60 g_hash_table_foreach(profile_hash, append_path, iter);
63 static void profiles_changed(void)
70 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
71 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
75 dbus_message_iter_init_append(signal, &iter);
76 connman_dbus_property_append_variable_array(&iter, "Profiles",
77 DBUS_TYPE_OBJECT_PATH, __connman_profile_list);
79 g_dbus_send_message(connection, signal);
82 static void name_changed(struct connman_profile *profile)
87 DBG("profile %p", profile);
89 signal = dbus_message_new_signal(profile->path,
90 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
94 dbus_message_iter_init_append(signal, &iter);
95 connman_dbus_property_append_variant(&iter, "Name",
96 DBUS_TYPE_STRING, &profile->name);
98 g_dbus_send_message(connection, signal);
101 static void offlinemode_changed(struct connman_profile *profile)
104 DBusMessageIter iter;
106 DBG("profile %p", profile);
108 signal = dbus_message_new_signal(profile->path,
109 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
113 dbus_message_iter_init_append(signal, &iter);
114 connman_dbus_property_append_variant(&iter, "OfflineMode",
115 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
117 g_dbus_send_message(connection, signal);
120 connman_bool_t __connman_profile_get_offlinemode(void)
122 if (default_profile == NULL)
125 DBG("offlinemode %d", default_profile->offlinemode);
127 return default_profile->offlinemode;
130 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
132 DBG("offlinemode %d", offlinemode);
134 if (default_profile == NULL)
137 if (default_profile->offlinemode == offlinemode)
140 default_profile->offlinemode = offlinemode;
141 offlinemode_changed(default_profile);
143 __connman_device_set_offlinemode(offlinemode);
148 int __connman_profile_save_default(void)
152 if (default_profile != NULL)
153 __connman_storage_save_profile(default_profile);
158 const char *__connman_profile_active_ident(void)
162 return PROFILE_DEFAULT_IDENT;
165 const char *__connman_profile_active_path(void)
169 if (default_profile == NULL)
172 return default_profile->path;
175 static guint changed_timeout = 0;
177 static gboolean services_changed(gpointer user_data)
179 struct connman_profile *profile = default_profile;
181 DBusMessageIter iter;
188 signal = dbus_message_new_signal(profile->path,
189 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
193 dbus_message_iter_init_append(signal, &iter);
194 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
195 connman_dbus_property_append_variable_array(&iter, "Services",
196 DBUS_TYPE_OBJECT_PATH, __connman_service_list);
198 connman_dbus_property_append_variable_array(&iter, "Services",
199 DBUS_TYPE_OBJECT_PATH, NULL);
201 g_dbus_send_message(connection, signal);
203 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) != 0)
206 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
207 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
211 dbus_message_iter_init_append(signal, &iter);
212 connman_dbus_property_append_variable_array(&iter, "Services",
213 DBUS_TYPE_OBJECT_PATH, __connman_service_list);
215 g_dbus_send_message(connection, signal);
220 void __connman_profile_changed(gboolean delayed)
224 if (changed_timeout > 0) {
225 g_source_remove(changed_timeout);
229 if (__connman_connection_update_gateway() == TRUE) {
230 services_changed(NULL);
234 if (delayed == FALSE) {
235 services_changed(NULL);
239 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
242 int __connman_profile_add_network(struct connman_network *network)
244 struct connman_service *service;
246 DBG("network %p", network);
248 service = __connman_service_create_from_network(network);
255 int __connman_profile_update_network(struct connman_network *network)
257 DBG("network %p", network);
259 __connman_service_update_from_network(network);
264 int __connman_profile_remove_network(struct connman_network *network)
266 DBG("network %p", network);
268 __connman_service_remove_from_network(network);
273 static DBusMessage *get_properties(DBusConnection *conn,
274 DBusMessage *msg, void *data)
276 struct connman_profile *profile = data;
278 DBusMessageIter array, dict;
280 DBG("conn %p", conn);
282 reply = dbus_message_new_method_return(msg);
286 dbus_message_iter_init_append(reply, &array);
288 connman_dbus_dict_open(&array, &dict);
290 if (profile->name != NULL)
291 connman_dbus_dict_append_variant(&dict, "Name",
292 DBUS_TYPE_STRING, &profile->name);
294 connman_dbus_dict_append_variant(&dict, "OfflineMode",
295 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
297 connman_dbus_dict_append_variable_array(&dict, "Services",
298 DBUS_TYPE_OBJECT_PATH, __connman_service_list);
300 connman_dbus_dict_close(&array, &dict);
305 static DBusMessage *set_property(DBusConnection *conn,
306 DBusMessage *msg, void *data)
308 struct connman_profile *profile = data;
309 DBusMessageIter iter, value;
313 DBG("conn %p", conn);
315 if (dbus_message_iter_init(msg, &iter) == FALSE)
316 return __connman_error_invalid_arguments(msg);
318 dbus_message_iter_get_basic(&iter, &name);
319 dbus_message_iter_next(&iter);
320 dbus_message_iter_recurse(&iter, &value);
322 if (__connman_security_check_privilege(msg,
323 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
324 return __connman_error_permission_denied(msg);
326 type = dbus_message_iter_get_arg_type(&value);
328 if (g_str_equal(name, "Name") == TRUE) {
331 if (type != DBUS_TYPE_STRING)
332 return __connman_error_invalid_arguments(msg);
334 dbus_message_iter_get_basic(&value, &name);
336 g_free(profile->name);
337 profile->name = g_strdup(name);
339 if (profile->name != NULL)
340 name_changed(profile);
342 __connman_storage_save_profile(profile);
343 } else if (g_str_equal(name, "OfflineMode") == TRUE) {
344 connman_bool_t offlinemode;
346 if (type != DBUS_TYPE_BOOLEAN)
347 return __connman_error_invalid_arguments(msg);
349 dbus_message_iter_get_basic(&value, &offlinemode);
351 if (profile->offlinemode == offlinemode)
352 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
354 profile->offlinemode = offlinemode;
355 offlinemode_changed(profile);
357 __connman_storage_save_profile(profile);
359 return __connman_error_invalid_property(msg);
361 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
364 static GDBusMethodTable profile_methods[] = {
365 { "GetProperties", "", "a{sv}", get_properties },
366 { "SetProperty", "sv", "", set_property },
370 static GDBusSignalTable profile_signals[] = {
371 { "PropertyChanged", "sv" },
375 static void free_profile(struct connman_profile *profile)
377 g_free(profile->name);
378 g_free(profile->path);
379 g_free(profile->ident);
383 static void unregister_profile(gpointer data)
385 struct connman_profile *profile = data;
387 DBG("profile %p", profile);
389 connman_info("Removing profile %s", profile->ident);
391 g_dbus_unregister_interface(connection, profile->path,
392 CONNMAN_PROFILE_INTERFACE);
394 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
395 default_profile = NULL;
397 free_profile(profile);
400 static int create_profile(const char *ident, const char *name,
403 struct connman_profile *profile;
405 DBG("ident %s name %s", ident, name);
407 profile = g_try_new0(struct connman_profile, 1);
411 profile->ident = g_strdup(ident);
412 profile->path = g_strdup_printf("/profile/%s", ident);
414 if (profile->ident == NULL || profile->path == NULL) {
415 free_profile(profile);
419 if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
420 free_profile(profile);
424 profile->name = g_strdup(name);
426 __connman_storage_load_profile(profile);
428 g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
430 connman_info("Adding profile %s", ident);
432 if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
433 default_profile = profile;
435 g_dbus_register_interface(connection, profile->path,
436 CONNMAN_PROFILE_INTERFACE,
437 profile_methods, profile_signals,
438 NULL, profile, NULL);
441 *path = profile->path;
443 DBG("profile %p path %s", profile, profile->path);
448 int __connman_profile_create(const char *name, const char **path)
450 struct connman_profile *profile;
453 DBG("name %s", name);
455 if (connman_dbus_validate_ident(name) == FALSE)
458 err = create_profile(name, NULL, path);
462 profile = g_hash_table_lookup(profile_hash, *path);
466 __connman_storage_save_profile(profile);
473 int __connman_profile_remove(const char *path)
475 struct connman_profile *profile;
477 DBG("path %s", path);
479 if (default_profile != NULL &&
480 g_strcmp0(path, default_profile->path) == 0)
483 profile = g_hash_table_lookup(profile_hash, path);
487 __connman_storage_delete_profile(profile->ident);
489 g_hash_table_remove(profile_hash, path);
496 static int profile_init(void)
503 dir = g_dir_open(STORAGEDIR, 0, NULL);
505 while ((file = g_dir_read_name(dir)) != NULL) {
509 if (g_str_has_suffix(file, ".profile") == FALSE)
512 ident = g_strrstr(file, ".profile");
516 str = g_string_new_len(file, ident - file);
520 ident = g_string_free(str, FALSE);
522 if (connman_dbus_validate_ident(ident) == TRUE)
523 create_profile(ident, NULL, NULL);
531 if (g_hash_table_size(profile_hash) == 0)
532 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
539 static int profile_load(struct connman_profile *profile)
542 GError *error = NULL;
543 connman_bool_t offlinemode;
546 DBG("profile %p", profile);
548 keyfile = __connman_storage_open_profile(profile->ident);
552 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
554 g_free(profile->name);
555 profile->name = name;
558 offlinemode = g_key_file_get_boolean(keyfile, "global",
559 "OfflineMode", &error);
561 profile->offlinemode = offlinemode;
562 g_clear_error(&error);
564 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
569 static int profile_save(struct connman_profile *profile)
573 DBG("profile %p", profile);
575 keyfile = __connman_storage_open_profile(profile->ident);
579 if (profile->name != NULL)
580 g_key_file_set_string(keyfile, "global",
581 "Name", profile->name);
583 g_key_file_set_boolean(keyfile, "global",
584 "OfflineMode", profile->offlinemode);
586 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
591 static struct connman_storage profile_storage = {
593 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
594 .profile_init = profile_init,
595 .profile_load = profile_load,
596 .profile_save = profile_save,
599 int __connman_profile_init(void)
603 connection = connman_dbus_get_connection();
604 if (connection == NULL)
607 if (connman_storage_register(&profile_storage) < 0)
608 connman_error("Failed to register profile storage");
610 profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
611 g_free, unregister_profile);
616 void __connman_profile_cleanup(void)
620 if (connection == NULL)
623 g_hash_table_destroy(profile_hash);
626 connman_storage_unregister(&profile_storage);
628 dbus_connection_unref(connection);