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)
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);
82 connman_bool_t __connman_profile_get_offlinemode(void)
84 if (default_profile == NULL)
87 DBG("offlinemode %d", default_profile->offlinemode);
89 return default_profile->offlinemode;
92 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
94 DBG("offlinemode %d", offlinemode);
96 if (default_profile == NULL)
99 if (default_profile->offlinemode == offlinemode)
102 default_profile->offlinemode = offlinemode;
103 offlinemode_changed(default_profile);
105 __connman_device_set_offlinemode(offlinemode);
110 int __connman_profile_save_default(void)
114 if (default_profile != NULL)
115 __connman_storage_save_profile(default_profile);
120 const char *__connman_profile_active_ident(void)
124 return PROFILE_DEFAULT_IDENT;
127 const char *__connman_profile_active_path(void)
131 if (default_profile == NULL)
134 return default_profile->path;
137 static guint changed_timeout = 0;
139 static gboolean services_changed(gpointer user_data)
141 struct connman_profile *profile = default_profile;
142 connman_dbus_append_cb_t function = NULL;
149 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0) {
150 function = __connman_service_list;
152 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
153 CONNMAN_MANAGER_INTERFACE, "Services",
154 DBUS_TYPE_OBJECT_PATH, function, NULL);
157 connman_dbus_property_changed_array(profile->path,
158 CONNMAN_PROFILE_INTERFACE, "Services",
159 DBUS_TYPE_OBJECT_PATH, function, NULL);
164 void __connman_profile_changed(gboolean delayed)
168 if (changed_timeout > 0) {
169 g_source_remove(changed_timeout);
173 if (__connman_connection_update_gateway() == TRUE) {
174 services_changed(NULL);
178 if (delayed == FALSE) {
179 services_changed(NULL);
183 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
186 int __connman_profile_add_network(struct connman_network *network)
188 struct connman_service *service;
190 DBG("network %p", network);
192 service = __connman_service_create_from_network(network);
199 int __connman_profile_update_network(struct connman_network *network)
201 DBG("network %p", network);
203 __connman_service_update_from_network(network);
208 int __connman_profile_remove_network(struct connman_network *network)
210 DBG("network %p", network);
212 __connman_service_remove_from_network(network);
217 static DBusMessage *get_properties(DBusConnection *conn,
218 DBusMessage *msg, void *data)
220 struct connman_profile *profile = data;
222 DBusMessageIter array, dict;
224 DBG("conn %p", conn);
226 reply = dbus_message_new_method_return(msg);
230 dbus_message_iter_init_append(reply, &array);
232 connman_dbus_dict_open(&array, &dict);
234 if (profile->name != NULL)
235 connman_dbus_dict_append_basic(&dict, "Name",
236 DBUS_TYPE_STRING, &profile->name);
238 connman_dbus_dict_append_basic(&dict, "OfflineMode",
239 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
241 connman_dbus_dict_append_array(&dict, "Services",
242 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
244 connman_dbus_dict_close(&array, &dict);
249 static DBusMessage *set_property(DBusConnection *conn,
250 DBusMessage *msg, void *data)
252 struct connman_profile *profile = data;
253 DBusMessageIter iter, value;
257 DBG("conn %p", conn);
259 if (dbus_message_iter_init(msg, &iter) == FALSE)
260 return __connman_error_invalid_arguments(msg);
262 dbus_message_iter_get_basic(&iter, &name);
263 dbus_message_iter_next(&iter);
264 dbus_message_iter_recurse(&iter, &value);
266 if (__connman_security_check_privilege(msg,
267 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
268 return __connman_error_permission_denied(msg);
270 type = dbus_message_iter_get_arg_type(&value);
272 if (g_str_equal(name, "Name") == TRUE) {
275 if (type != DBUS_TYPE_STRING)
276 return __connman_error_invalid_arguments(msg);
278 dbus_message_iter_get_basic(&value, &name);
280 g_free(profile->name);
281 profile->name = g_strdup(name);
283 if (profile->name != NULL)
284 name_changed(profile);
286 __connman_storage_save_profile(profile);
287 } else if (g_str_equal(name, "OfflineMode") == TRUE) {
288 connman_bool_t offlinemode;
290 if (type != DBUS_TYPE_BOOLEAN)
291 return __connman_error_invalid_arguments(msg);
293 dbus_message_iter_get_basic(&value, &offlinemode);
295 if (profile->offlinemode == offlinemode)
296 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
298 profile->offlinemode = offlinemode;
299 offlinemode_changed(profile);
301 __connman_storage_save_profile(profile);
303 return __connman_error_invalid_property(msg);
305 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
308 static GDBusMethodTable profile_methods[] = {
309 { "GetProperties", "", "a{sv}", get_properties },
310 { "SetProperty", "sv", "", set_property },
314 static GDBusSignalTable profile_signals[] = {
315 { "PropertyChanged", "sv" },
319 static void free_profile(struct connman_profile *profile)
321 g_free(profile->name);
322 g_free(profile->path);
323 g_free(profile->ident);
327 static void unregister_profile(gpointer data)
329 struct connman_profile *profile = data;
331 DBG("profile %p", profile);
333 connman_info("Removing profile %s", profile->ident);
335 g_dbus_unregister_interface(connection, profile->path,
336 CONNMAN_PROFILE_INTERFACE);
338 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
339 default_profile = NULL;
341 free_profile(profile);
344 static int create_profile(const char *ident, const char *name,
347 struct connman_profile *profile;
349 DBG("ident %s name %s", ident, name);
351 profile = g_try_new0(struct connman_profile, 1);
355 profile->ident = g_strdup(ident);
356 profile->path = g_strdup_printf("/profile/%s", ident);
358 if (profile->ident == NULL || profile->path == NULL) {
359 free_profile(profile);
363 if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
364 free_profile(profile);
368 profile->name = g_strdup(name);
370 __connman_storage_load_profile(profile);
372 g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
374 connman_info("Adding profile %s", ident);
376 if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
377 default_profile = profile;
379 g_dbus_register_interface(connection, profile->path,
380 CONNMAN_PROFILE_INTERFACE,
381 profile_methods, profile_signals,
382 NULL, profile, NULL);
385 *path = profile->path;
387 DBG("profile %p path %s", profile, profile->path);
392 int __connman_profile_create(const char *name, const char **path)
394 struct connman_profile *profile;
397 DBG("name %s", name);
399 if (connman_dbus_validate_ident(name) == FALSE)
402 err = create_profile(name, NULL, path);
406 profile = g_hash_table_lookup(profile_hash, *path);
410 __connman_storage_save_profile(profile);
417 int __connman_profile_remove(const char *path)
419 struct connman_profile *profile;
421 DBG("path %s", path);
423 if (default_profile != NULL &&
424 g_strcmp0(path, default_profile->path) == 0)
427 profile = g_hash_table_lookup(profile_hash, path);
431 __connman_storage_delete_profile(profile->ident);
433 g_hash_table_remove(profile_hash, path);
440 static int profile_init(void)
447 dir = g_dir_open(STORAGEDIR, 0, NULL);
449 while ((file = g_dir_read_name(dir)) != NULL) {
453 if (g_str_has_suffix(file, ".profile") == FALSE)
456 ident = g_strrstr(file, ".profile");
460 str = g_string_new_len(file, ident - file);
464 ident = g_string_free(str, FALSE);
466 if (connman_dbus_validate_ident(ident) == TRUE)
467 create_profile(ident, NULL, NULL);
475 if (g_hash_table_size(profile_hash) == 0)
476 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
483 static int profile_load(struct connman_profile *profile)
486 GError *error = NULL;
487 connman_bool_t offlinemode;
490 DBG("profile %p", profile);
492 keyfile = __connman_storage_open_profile(profile->ident);
496 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
498 g_free(profile->name);
499 profile->name = name;
502 offlinemode = g_key_file_get_boolean(keyfile, "global",
503 "OfflineMode", &error);
505 profile->offlinemode = offlinemode;
506 g_clear_error(&error);
508 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
513 static int profile_save(struct connman_profile *profile)
517 DBG("profile %p", profile);
519 keyfile = __connman_storage_open_profile(profile->ident);
523 if (profile->name != NULL)
524 g_key_file_set_string(keyfile, "global",
525 "Name", profile->name);
527 g_key_file_set_boolean(keyfile, "global",
528 "OfflineMode", profile->offlinemode);
530 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
535 static struct connman_storage profile_storage = {
537 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
538 .profile_init = profile_init,
539 .profile_load = profile_load,
540 .profile_save = profile_save,
543 int __connman_profile_init(void)
547 connection = connman_dbus_get_connection();
548 if (connection == NULL)
551 if (connman_storage_register(&profile_storage) < 0)
552 connman_error("Failed to register profile storage");
554 profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
555 g_free, unregister_profile);
560 void __connman_profile_cleanup(void)
564 if (connection == NULL)
567 g_hash_table_destroy(profile_hash);
570 connman_storage_unregister(&profile_storage);
572 dbus_connection_unref(connection);