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 append_profiles(DBusMessageIter *entry)
65 DBusMessageIter value, iter;
66 const char *key = "Profiles";
68 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
70 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
71 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
74 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
75 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
76 __connman_profile_list(&iter);
77 dbus_message_iter_close_container(&value, &iter);
79 dbus_message_iter_close_container(entry, &value);
82 static void profiles_changed(void)
85 DBusMessageIter entry;
89 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
90 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
94 dbus_message_iter_init_append(signal, &entry);
95 append_profiles(&entry);
96 g_dbus_send_message(connection, signal);
99 static void name_changed(struct connman_profile *profile)
102 DBusMessageIter entry, value;
103 const char *key = "Name";
105 DBG("profile %p", profile);
107 signal = dbus_message_new_signal(profile->path,
108 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
112 dbus_message_iter_init_append(signal, &entry);
114 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
116 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
117 DBUS_TYPE_STRING_AS_STRING, &value);
118 dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING,
120 dbus_message_iter_close_container(&entry, &value);
122 g_dbus_send_message(connection, signal);
125 static void offlinemode_changed(struct connman_profile *profile)
128 DBusMessageIter entry, value;
129 const char *key = "OfflineMode";
131 DBG("profile %p", profile);
133 signal = dbus_message_new_signal(profile->path,
134 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
138 dbus_message_iter_init_append(signal, &entry);
140 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
142 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
143 DBUS_TYPE_BOOLEAN_AS_STRING, &value);
144 dbus_message_iter_append_basic(&value, DBUS_TYPE_BOOLEAN,
145 &profile->offlinemode);
146 dbus_message_iter_close_container(&entry, &value);
148 g_dbus_send_message(connection, signal);
151 connman_bool_t __connman_profile_get_offlinemode(void)
153 if (default_profile == NULL)
156 DBG("offlinemode %d", default_profile->offlinemode);
158 return default_profile->offlinemode;
161 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
163 DBG("offlinemode %d", offlinemode);
165 if (default_profile == NULL)
168 if (default_profile->offlinemode == offlinemode)
171 default_profile->offlinemode = offlinemode;
172 offlinemode_changed(default_profile);
174 __connman_device_set_offlinemode(offlinemode);
179 int __connman_profile_save_default(void)
183 if (default_profile != NULL)
184 __connman_storage_save_profile(default_profile);
189 const char *__connman_profile_active_ident(void)
193 return PROFILE_DEFAULT_IDENT;
196 const char *__connman_profile_active_path(void)
200 if (default_profile == NULL)
203 return default_profile->path;
206 static void append_services(struct connman_profile *profile,
207 DBusMessageIter *entry)
209 DBusMessageIter value, iter;
210 const char *key = "Services";
212 dbus_message_iter_append_basic(entry, DBUS_TYPE_STRING, &key);
214 dbus_message_iter_open_container(entry, DBUS_TYPE_VARIANT,
215 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
218 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
219 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
221 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
222 __connman_service_list(&iter);
224 dbus_message_iter_close_container(&value, &iter);
226 dbus_message_iter_close_container(entry, &value);
229 static guint changed_timeout = 0;
231 static gboolean services_changed(gpointer user_data)
233 struct connman_profile *profile = default_profile;
235 DBusMessageIter entry;
242 signal = dbus_message_new_signal(profile->path,
243 CONNMAN_PROFILE_INTERFACE, "PropertyChanged");
247 dbus_message_iter_init_append(signal, &entry);
248 append_services(profile, &entry);
249 g_dbus_send_message(connection, signal);
251 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) != 0)
254 signal = dbus_message_new_signal(CONNMAN_MANAGER_PATH,
255 CONNMAN_MANAGER_INTERFACE, "PropertyChanged");
259 dbus_message_iter_init_append(signal, &entry);
260 append_services(profile, &entry);
261 g_dbus_send_message(connection, signal);
266 void __connman_profile_changed(gboolean delayed)
270 if (changed_timeout > 0) {
271 g_source_remove(changed_timeout);
275 if (__connman_connection_update_gateway() == TRUE) {
276 services_changed(NULL);
280 if (delayed == FALSE) {
281 services_changed(NULL);
285 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
288 int __connman_profile_add_network(struct connman_network *network)
290 struct connman_service *service;
292 DBG("network %p", network);
294 service = __connman_service_create_from_network(network);
301 int __connman_profile_update_network(struct connman_network *network)
303 DBG("network %p", network);
305 __connman_service_update_from_network(network);
310 int __connman_profile_remove_network(struct connman_network *network)
312 DBG("network %p", network);
314 __connman_service_remove_from_network(network);
319 static DBusMessage *get_properties(DBusConnection *conn,
320 DBusMessage *msg, void *data)
322 struct connman_profile *profile = data;
324 DBusMessageIter array, dict, entry;
326 DBG("conn %p", conn);
328 reply = dbus_message_new_method_return(msg);
332 dbus_message_iter_init_append(reply, &array);
334 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
335 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
336 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
337 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
339 if (profile->name != NULL)
340 connman_dbus_dict_append_variant(&dict, "Name",
341 DBUS_TYPE_STRING, &profile->name);
343 connman_dbus_dict_append_variant(&dict, "OfflineMode",
344 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
346 dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY,
348 append_services(profile, &entry);
349 dbus_message_iter_close_container(&dict, &entry);
351 dbus_message_iter_close_container(&array, &dict);
356 static DBusMessage *set_property(DBusConnection *conn,
357 DBusMessage *msg, void *data)
359 struct connman_profile *profile = data;
360 DBusMessageIter iter, value;
364 DBG("conn %p", conn);
366 if (dbus_message_iter_init(msg, &iter) == FALSE)
367 return __connman_error_invalid_arguments(msg);
369 dbus_message_iter_get_basic(&iter, &name);
370 dbus_message_iter_next(&iter);
371 dbus_message_iter_recurse(&iter, &value);
373 if (__connman_security_check_privilege(msg,
374 CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
375 return __connman_error_permission_denied(msg);
377 type = dbus_message_iter_get_arg_type(&value);
379 if (g_str_equal(name, "Name") == TRUE) {
382 if (type != DBUS_TYPE_STRING)
383 return __connman_error_invalid_arguments(msg);
385 dbus_message_iter_get_basic(&value, &name);
387 g_free(profile->name);
388 profile->name = g_strdup(name);
390 if (profile->name != NULL)
391 name_changed(profile);
393 __connman_storage_save_profile(profile);
394 } else if (g_str_equal(name, "OfflineMode") == TRUE) {
395 connman_bool_t offlinemode;
397 if (type != DBUS_TYPE_BOOLEAN)
398 return __connman_error_invalid_arguments(msg);
400 dbus_message_iter_get_basic(&value, &offlinemode);
402 if (profile->offlinemode == offlinemode)
403 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
405 profile->offlinemode = offlinemode;
406 offlinemode_changed(profile);
408 __connman_storage_save_profile(profile);
410 return __connman_error_invalid_property(msg);
412 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
415 static GDBusMethodTable profile_methods[] = {
416 { "GetProperties", "", "a{sv}", get_properties },
417 { "SetProperty", "sv", "", set_property },
421 static GDBusSignalTable profile_signals[] = {
422 { "PropertyChanged", "sv" },
426 static void free_profile(struct connman_profile *profile)
428 g_free(profile->name);
429 g_free(profile->path);
430 g_free(profile->ident);
434 static void unregister_profile(gpointer data)
436 struct connman_profile *profile = data;
438 DBG("profile %p", profile);
440 connman_info("Removing profile %s", profile->ident);
442 g_dbus_unregister_interface(connection, profile->path,
443 CONNMAN_PROFILE_INTERFACE);
445 if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
446 default_profile = NULL;
448 free_profile(profile);
451 static int create_profile(const char *ident, const char *name,
454 struct connman_profile *profile;
456 DBG("ident %s name %s", ident, name);
458 profile = g_try_new0(struct connman_profile, 1);
462 profile->ident = g_strdup(ident);
463 profile->path = g_strdup_printf("/profile/%s", ident);
465 if (profile->ident == NULL || profile->path == NULL) {
466 free_profile(profile);
470 if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
471 free_profile(profile);
475 profile->name = g_strdup(name);
477 __connman_storage_load_profile(profile);
479 g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
481 connman_info("Adding profile %s", ident);
483 if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
484 default_profile = profile;
486 g_dbus_register_interface(connection, profile->path,
487 CONNMAN_PROFILE_INTERFACE,
488 profile_methods, profile_signals,
489 NULL, profile, NULL);
492 *path = profile->path;
494 DBG("profile %p path %s", profile, profile->path);
499 static gboolean validate_ident(const char *ident)
503 for (i = 0; i < strlen(ident); i++) {
504 if (ident[i] >= '0' && ident[i] <= '9')
506 if (ident[i] >= 'a' && ident[i] <= 'z')
508 if (ident[i] >= 'A' && ident[i] <= 'Z')
516 int __connman_profile_create(const char *name, const char **path)
518 struct connman_profile *profile;
521 DBG("name %s", name);
523 if (validate_ident(name) == FALSE)
526 err = create_profile(name, NULL, path);
530 profile = g_hash_table_lookup(profile_hash, *path);
534 __connman_storage_save_profile(profile);
541 int __connman_profile_remove(const char *path)
543 struct connman_profile *profile;
545 DBG("path %s", path);
547 if (default_profile != NULL &&
548 g_strcmp0(path, default_profile->path) == 0)
551 profile = g_hash_table_lookup(profile_hash, path);
555 __connman_storage_delete(profile->ident);
557 g_hash_table_remove(profile_hash, path);
564 static int profile_init(void)
571 dir = g_dir_open(STORAGEDIR, 0, NULL);
573 while ((file = g_dir_read_name(dir)) != NULL) {
577 if (g_str_has_suffix(file, ".profile") == FALSE)
580 ident = g_strrstr(file, ".profile");
584 str = g_string_new_len(file, ident - file);
588 ident = g_string_free(str, FALSE);
590 if (validate_ident(ident) == TRUE)
591 create_profile(ident, NULL, NULL);
599 if (g_hash_table_size(profile_hash) == 0)
600 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
607 static int profile_load(struct connman_profile *profile)
610 GError *error = NULL;
611 connman_bool_t offlinemode;
614 DBG("profile %p", profile);
616 keyfile = __connman_storage_open(profile->ident);
620 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
622 g_free(profile->name);
623 profile->name = name;
626 offlinemode = g_key_file_get_boolean(keyfile, "global",
627 "OfflineMode", &error);
629 profile->offlinemode = offlinemode;
630 g_clear_error(&error);
632 __connman_storage_close(profile->ident, keyfile, FALSE);
637 static int profile_save(struct connman_profile *profile)
641 DBG("profile %p", profile);
643 keyfile = __connman_storage_open(profile->ident);
647 if (profile->name != NULL)
648 g_key_file_set_string(keyfile, "global",
649 "Name", profile->name);
651 g_key_file_set_boolean(keyfile, "global",
652 "OfflineMode", profile->offlinemode);
654 __connman_storage_close(profile->ident, keyfile, TRUE);
659 static struct connman_storage profile_storage = {
661 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
662 .profile_init = profile_init,
663 .profile_load = profile_load,
664 .profile_save = profile_save,
667 int __connman_profile_init(void)
671 connection = connman_dbus_get_connection();
672 if (connection == NULL)
675 if (connman_storage_register(&profile_storage) < 0)
676 connman_error("Failed to register profile storage");
678 profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
679 g_free, unregister_profile);
684 void __connman_profile_cleanup(void)
688 if (connection == NULL)
691 g_hash_table_destroy(profile_hash);
694 connman_storage_unregister(&profile_storage);
696 dbus_connection_unref(connection);