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, void *user_data)
58 g_hash_table_foreach(profile_hash, append_path, iter);
61 static void profiles_changed(void)
68 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
69 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
73 dbus_message_iter_init_append(signal, &iter);
74 connman_dbus_property_append_variable_array(&iter, "Profiles",
75 DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
77 g_dbus_send_message(connection, signal);
80 static void name_changed(struct connman_profile *profile)
82 connman_dbus_property_changed_basic(profile->path,
83 CONNMAN_PROFILE_INTERFACE, "Name",
84 DBUS_TYPE_STRING, &profile->name);
87 static void offlinemode_changed(struct connman_profile *profile)
89 connman_dbus_property_changed_basic(profile->path,
90 CONNMAN_PROFILE_INTERFACE, "OfflineMode",
91 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
94 connman_bool_t __connman_profile_get_offlinemode(void)
96 if (default_profile == NULL)
99 DBG("offlinemode %d", default_profile->offlinemode);
101 return default_profile->offlinemode;
104 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
106 DBG("offlinemode %d", offlinemode);
108 if (default_profile == NULL)
111 if (default_profile->offlinemode == offlinemode)
114 default_profile->offlinemode = offlinemode;
115 offlinemode_changed(default_profile);
117 __connman_device_set_offlinemode(offlinemode);
122 int __connman_profile_save_default(void)
126 if (default_profile != NULL)
127 __connman_storage_save_profile(default_profile);
132 const char *__connman_profile_active_ident(void)
136 return PROFILE_DEFAULT_IDENT;
139 const char *__connman_profile_active_path(void)
143 if (default_profile == NULL)
146 return default_profile->path;
149 static guint changed_timeout = 0;
151 static gboolean services_changed(gpointer user_data)
153 struct connman_profile *profile = default_profile;
155 DBusMessageIter iter;
162 signal = dbus_message_new_signal(profile->path,
163 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
167 dbus_message_iter_init_append(signal, &iter);
168 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
169 connman_dbus_property_append_variable_array(&iter, "Services",
170 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
172 connman_dbus_property_append_variable_array(&iter, "Services",
173 DBUS_TYPE_OBJECT_PATH, NULL, NULL);
175 g_dbus_send_message(connection, signal);
177 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) != 0)
180 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
181 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
185 dbus_message_iter_init_append(signal, &iter);
186 connman_dbus_property_append_variable_array(&iter, "Services",
187 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
189 g_dbus_send_message(connection, signal);
194 void __connman_profile_changed(gboolean delayed)
198 if (changed_timeout > 0) {
199 g_source_remove(changed_timeout);
203 if (__connman_connection_update_gateway() == TRUE) {
204 services_changed(NULL);
208 if (delayed == FALSE) {
209 services_changed(NULL);
213 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
216 int __connman_profile_add_network(struct connman_network *network)
218 struct connman_service *service;
220 DBG("network %p", network);
222 service = __connman_service_create_from_network(network);
229 int __connman_profile_update_network(struct connman_network *network)
231 DBG("network %p", network);
233 __connman_service_update_from_network(network);
238 int __connman_profile_remove_network(struct connman_network *network)
240 DBG("network %p", network);
242 __connman_service_remove_from_network(network);
247 static DBusMessage *get_properties(DBusConnection *conn,
248 DBusMessage *msg, void *data)
250 struct connman_profile *profile = data;
252 DBusMessageIter array, dict;
254 DBG("conn %p", conn);
256 reply = dbus_message_new_method_return(msg);
260 dbus_message_iter_init_append(reply, &array);
262 connman_dbus_dict_open(&array, &dict);
264 if (profile->name != NULL)
265 connman_dbus_dict_append_basic(&dict, "Name",
266 DBUS_TYPE_STRING, &profile->name);
268 connman_dbus_dict_append_basic(&dict, "OfflineMode",
269 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
271 connman_dbus_dict_append_variable_array(&dict, "Services",
272 DBUS_TYPE_OBJECT_PATH, __connman_service_list);
274 connman_dbus_dict_close(&array, &dict);
279 static DBusMessage *set_property(DBusConnection *conn,
280 DBusMessage *msg, void *data)
282 struct connman_profile *profile = data;
283 DBusMessageIter iter, value;
287 DBG("conn %p", conn);
289 if (dbus_message_iter_init(msg, &iter) == FALSE)
290 return __connman_error_invalid_arguments(msg);
292 dbus_message_iter_get_basic(&iter, &name);
293 dbus_message_iter_next(&iter);
294 dbus_message_iter_recurse(&iter, &value);
296 if (__connman_security_check_privilege(msg,
297 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
298 return __connman_error_permission_denied(msg);
300 type = dbus_message_iter_get_arg_type(&value);
302 if (g_str_equal(name, "Name") == TRUE) {
305 if (type != DBUS_TYPE_STRING)
306 return __connman_error_invalid_arguments(msg);
308 dbus_message_iter_get_basic(&value, &name);
310 g_free(profile->name);
311 profile->name = g_strdup(name);
313 if (profile->name != NULL)
314 name_changed(profile);
316 __connman_storage_save_profile(profile);
317 } else if (g_str_equal(name, "OfflineMode") == TRUE) {
318 connman_bool_t offlinemode;
320 if (type != DBUS_TYPE_BOOLEAN)
321 return __connman_error_invalid_arguments(msg);
323 dbus_message_iter_get_basic(&value, &offlinemode);
325 if (profile->offlinemode == offlinemode)
326 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
328 profile->offlinemode = offlinemode;
329 offlinemode_changed(profile);
331 __connman_storage_save_profile(profile);
333 return __connman_error_invalid_property(msg);
335 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
338 static GDBusMethodTable profile_methods[] = {
339 { "GetProperties", "", "a{sv}", get_properties },
340 { "SetProperty", "sv", "", set_property },
344 static GDBusSignalTable profile_signals[] = {
345 { "PropertyChanged", "sv" },
349 static void free_profile(struct connman_profile *profile)
351 g_free(profile->name);
352 g_free(profile->path);
353 g_free(profile->ident);
357 static void unregister_profile(gpointer data)
359 struct connman_profile *profile = data;
361 DBG("profile %p", profile);
363 connman_info("Removing profile %s", profile->ident);
365 g_dbus_unregister_interface(connection, profile->path,
366 CONNMAN_PROFILE_INTERFACE);
368 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
369 default_profile = NULL;
371 free_profile(profile);
374 static int create_profile(const char *ident, const char *name,
377 struct connman_profile *profile;
379 DBG("ident %s name %s", ident, name);
381 profile = g_try_new0(struct connman_profile, 1);
385 profile->ident = g_strdup(ident);
386 profile->path = g_strdup_printf("/profile/%s", ident);
388 if (profile->ident == NULL || profile->path == NULL) {
389 free_profile(profile);
393 if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
394 free_profile(profile);
398 profile->name = g_strdup(name);
400 __connman_storage_load_profile(profile);
402 g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
404 connman_info("Adding profile %s", ident);
406 if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
407 default_profile = profile;
409 g_dbus_register_interface(connection, profile->path,
410 CONNMAN_PROFILE_INTERFACE,
411 profile_methods, profile_signals,
412 NULL, profile, NULL);
415 *path = profile->path;
417 DBG("profile %p path %s", profile, profile->path);
422 int __connman_profile_create(const char *name, const char **path)
424 struct connman_profile *profile;
427 DBG("name %s", name);
429 if (connman_dbus_validate_ident(name) == FALSE)
432 err = create_profile(name, NULL, path);
436 profile = g_hash_table_lookup(profile_hash, *path);
440 __connman_storage_save_profile(profile);
447 int __connman_profile_remove(const char *path)
449 struct connman_profile *profile;
451 DBG("path %s", path);
453 if (default_profile != NULL &&
454 g_strcmp0(path, default_profile->path) == 0)
457 profile = g_hash_table_lookup(profile_hash, path);
461 __connman_storage_delete_profile(profile->ident);
463 g_hash_table_remove(profile_hash, path);
470 static int profile_init(void)
477 dir = g_dir_open(STORAGEDIR, 0, NULL);
479 while ((file = g_dir_read_name(dir)) != NULL) {
483 if (g_str_has_suffix(file, ".profile") == FALSE)
486 ident = g_strrstr(file, ".profile");
490 str = g_string_new_len(file, ident - file);
494 ident = g_string_free(str, FALSE);
496 if (connman_dbus_validate_ident(ident) == TRUE)
497 create_profile(ident, NULL, NULL);
505 if (g_hash_table_size(profile_hash) == 0)
506 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
513 static int profile_load(struct connman_profile *profile)
516 GError *error = NULL;
517 connman_bool_t offlinemode;
520 DBG("profile %p", profile);
522 keyfile = __connman_storage_open_profile(profile->ident);
526 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
528 g_free(profile->name);
529 profile->name = name;
532 offlinemode = g_key_file_get_boolean(keyfile, "global",
533 "OfflineMode", &error);
535 profile->offlinemode = offlinemode;
536 g_clear_error(&error);
538 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
543 static int profile_save(struct connman_profile *profile)
547 DBG("profile %p", profile);
549 keyfile = __connman_storage_open_profile(profile->ident);
553 if (profile->name != NULL)
554 g_key_file_set_string(keyfile, "global",
555 "Name", profile->name);
557 g_key_file_set_boolean(keyfile, "global",
558 "OfflineMode", profile->offlinemode);
560 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
565 static struct connman_storage profile_storage = {
567 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
568 .profile_init = profile_init,
569 .profile_load = profile_load,
570 .profile_save = profile_save,
573 int __connman_profile_init(void)
577 connection = connman_dbus_get_connection();
578 if (connection == NULL)
581 if (connman_storage_register(&profile_storage) < 0)
582 connman_error("Failed to register profile storage");
584 profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
585 g_free, unregister_profile);
590 void __connman_profile_cleanup(void)
594 if (connection == NULL)
597 g_hash_table_destroy(profile_hash);
600 connman_storage_unregister(&profile_storage);
602 dbus_connection_unref(connection);