5 * Copyright (C) 2007-2010 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)
63 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
64 CONNMAN_MANAGER_INTERFACE, "Profiles",
65 DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
68 static void name_changed(struct connman_profile *profile)
70 connman_dbus_property_changed_basic(profile->path,
71 CONNMAN_PROFILE_INTERFACE, "Name",
72 DBUS_TYPE_STRING, &profile->name);
75 static void offlinemode_changed(struct connman_profile *profile)
77 connman_dbus_property_changed_basic(profile->path,
78 CONNMAN_PROFILE_INTERFACE, "OfflineMode",
79 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
81 if (profile != default_profile)
84 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
85 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
86 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
89 connman_bool_t __connman_profile_get_offlinemode(void)
91 if (default_profile == NULL)
94 DBG("offlinemode %d", default_profile->offlinemode);
96 return default_profile->offlinemode;
99 int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
100 connman_bool_t all_devices)
102 DBG("offlinemode %d", offlinemode);
104 if (default_profile == NULL)
107 if (default_profile->offlinemode == offlinemode)
110 default_profile->offlinemode = offlinemode;
111 offlinemode_changed(default_profile);
114 __connman_device_set_offlinemode(offlinemode);
119 int __connman_profile_save_default(void)
123 if (default_profile != NULL)
124 __connman_storage_save_profile(default_profile);
129 const char *__connman_profile_active_ident(void)
133 return PROFILE_DEFAULT_IDENT;
136 const char *__connman_profile_active_path(void)
140 if (default_profile == NULL)
143 return default_profile->path;
146 static guint changed_timeout = 0;
148 static gboolean services_changed(gpointer user_data)
150 struct connman_profile *profile = default_profile;
151 connman_dbus_append_cb_t function = NULL;
158 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0) {
159 function = __connman_service_list;
161 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
162 CONNMAN_MANAGER_INTERFACE, "Services",
163 DBUS_TYPE_OBJECT_PATH, function, NULL);
166 connman_dbus_property_changed_array(profile->path,
167 CONNMAN_PROFILE_INTERFACE, "Services",
168 DBUS_TYPE_OBJECT_PATH, function, NULL);
173 void __connman_profile_changed(gboolean delayed)
177 if (changed_timeout > 0) {
178 g_source_remove(changed_timeout);
182 if (__connman_connection_update_gateway() == TRUE) {
183 services_changed(NULL);
187 if (delayed == FALSE) {
188 services_changed(NULL);
192 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
195 int __connman_profile_add_network(struct connman_network *network)
197 struct connman_service *service;
199 DBG("network %p", network);
201 service = __connman_service_create_from_network(network);
208 int __connman_profile_update_network(struct connman_network *network)
210 DBG("network %p", network);
212 __connman_service_update_from_network(network);
217 int __connman_profile_remove_network(struct connman_network *network)
219 DBG("network %p", network);
221 __connman_service_remove_from_network(network);
226 static DBusMessage *get_properties(DBusConnection *conn,
227 DBusMessage *msg, void *data)
229 struct connman_profile *profile = data;
231 DBusMessageIter array, dict;
233 DBG("conn %p", conn);
235 reply = dbus_message_new_method_return(msg);
239 dbus_message_iter_init_append(reply, &array);
241 connman_dbus_dict_open(&array, &dict);
243 if (profile->name != NULL)
244 connman_dbus_dict_append_basic(&dict, "Name",
245 DBUS_TYPE_STRING, &profile->name);
247 connman_dbus_dict_append_basic(&dict, "OfflineMode",
248 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
250 connman_dbus_dict_append_array(&dict, "Services",
251 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
253 connman_dbus_dict_close(&array, &dict);
258 static DBusMessage *set_property(DBusConnection *conn,
259 DBusMessage *msg, void *data)
261 struct connman_profile *profile = data;
262 DBusMessageIter iter, value;
266 DBG("conn %p", conn);
268 if (dbus_message_iter_init(msg, &iter) == FALSE)
269 return __connman_error_invalid_arguments(msg);
271 dbus_message_iter_get_basic(&iter, &name);
272 dbus_message_iter_next(&iter);
273 dbus_message_iter_recurse(&iter, &value);
275 if (__connman_security_check_privilege(msg,
276 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
277 return __connman_error_permission_denied(msg);
279 type = dbus_message_iter_get_arg_type(&value);
281 if (g_str_equal(name, "Name") == TRUE) {
284 if (type != DBUS_TYPE_STRING)
285 return __connman_error_invalid_arguments(msg);
287 dbus_message_iter_get_basic(&value, &name);
289 g_free(profile->name);
290 profile->name = g_strdup(name);
292 if (profile->name != NULL)
293 name_changed(profile);
295 __connman_storage_save_profile(profile);
296 } else if (g_str_equal(name, "OfflineMode") == TRUE) {
297 connman_bool_t offlinemode;
299 if (type != DBUS_TYPE_BOOLEAN)
300 return __connman_error_invalid_arguments(msg);
302 dbus_message_iter_get_basic(&value, &offlinemode);
304 if (profile->offlinemode == offlinemode)
305 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
307 profile->offlinemode = offlinemode;
308 offlinemode_changed(profile);
310 __connman_storage_save_profile(profile);
312 return __connman_error_invalid_property(msg);
314 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
317 static GDBusMethodTable profile_methods[] = {
318 { "GetProperties", "", "a{sv}", get_properties },
319 { "SetProperty", "sv", "", set_property },
323 static GDBusSignalTable profile_signals[] = {
324 { "PropertyChanged", "sv" },
328 static void free_profile(struct connman_profile *profile)
330 g_free(profile->name);
331 g_free(profile->path);
332 g_free(profile->ident);
336 static void unregister_profile(gpointer data)
338 struct connman_profile *profile = data;
340 DBG("profile %p", profile);
342 connman_info("Removing profile %s", profile->ident);
344 g_dbus_unregister_interface(connection, profile->path,
345 CONNMAN_PROFILE_INTERFACE);
347 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
348 default_profile = NULL;
350 free_profile(profile);
353 static int create_profile(const char *ident, const char *name,
356 struct connman_profile *profile;
358 DBG("ident %s name %s", ident, name);
360 profile = g_try_new0(struct connman_profile, 1);
364 profile->ident = g_strdup(ident);
365 profile->path = g_strdup_printf("/profile/%s", ident);
367 if (profile->ident == NULL || profile->path == NULL) {
368 free_profile(profile);
372 if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
373 free_profile(profile);
377 profile->name = g_strdup(name);
379 __connman_storage_load_profile(profile);
381 g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
383 connman_info("Adding profile %s", ident);
385 if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
386 default_profile = profile;
388 g_dbus_register_interface(connection, profile->path,
389 CONNMAN_PROFILE_INTERFACE,
390 profile_methods, profile_signals,
391 NULL, profile, NULL);
394 *path = profile->path;
396 DBG("profile %p path %s", profile, profile->path);
401 int __connman_profile_create(const char *name, const char **path)
403 struct connman_profile *profile;
406 DBG("name %s", name);
408 if (connman_dbus_validate_ident(name) == FALSE)
411 err = create_profile(name, NULL, path);
415 profile = g_hash_table_lookup(profile_hash, *path);
419 __connman_storage_save_profile(profile);
426 int __connman_profile_remove(const char *path)
428 struct connman_profile *profile;
430 DBG("path %s", path);
432 if (default_profile != NULL &&
433 g_strcmp0(path, default_profile->path) == 0)
436 profile = g_hash_table_lookup(profile_hash, path);
440 __connman_storage_delete_profile(profile->ident);
442 g_hash_table_remove(profile_hash, path);
449 static int profile_init(void)
456 dir = g_dir_open(STORAGEDIR, 0, NULL);
458 while ((file = g_dir_read_name(dir)) != NULL) {
462 if (g_str_has_suffix(file, ".profile") == FALSE)
465 ident = g_strrstr(file, ".profile");
469 str = g_string_new_len(file, ident - file);
473 ident = g_string_free(str, FALSE);
475 if (connman_dbus_validate_ident(ident) == TRUE)
476 create_profile(ident, NULL, NULL);
484 if (g_hash_table_size(profile_hash) == 0)
485 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
492 static int profile_load(struct connman_profile *profile)
495 GError *error = NULL;
496 connman_bool_t offlinemode;
499 DBG("profile %p", profile);
501 keyfile = __connman_storage_open_profile(profile->ident);
505 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
507 g_free(profile->name);
508 profile->name = name;
511 offlinemode = g_key_file_get_boolean(keyfile, "global",
512 "OfflineMode", &error);
514 profile->offlinemode = offlinemode;
515 g_clear_error(&error);
517 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
522 static int profile_save(struct connman_profile *profile)
526 DBG("profile %p", profile);
528 keyfile = __connman_storage_open_profile(profile->ident);
532 if (profile->name != NULL)
533 g_key_file_set_string(keyfile, "global",
534 "Name", profile->name);
536 g_key_file_set_boolean(keyfile, "global",
537 "OfflineMode", profile->offlinemode);
539 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
544 static struct connman_storage profile_storage = {
546 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
547 .profile_init = profile_init,
548 .profile_load = profile_load,
549 .profile_save = profile_save,
552 int __connman_profile_init(void)
556 connection = connman_dbus_get_connection();
557 if (connection == NULL)
560 if (connman_storage_register(&profile_storage) < 0)
561 connman_error("Failed to register profile storage");
563 profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
564 g_free, unregister_profile);
569 void __connman_profile_cleanup(void)
573 if (connection == NULL)
576 g_hash_table_destroy(profile_hash);
579 connman_storage_unregister(&profile_storage);
581 dbus_connection_unref(connection);