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 struct connman_profile *default_profile = NULL;
44 static DBusConnection *connection = NULL;
46 static void name_changed(struct connman_profile *profile)
48 connman_dbus_property_changed_basic(profile->path,
49 CONNMAN_PROFILE_INTERFACE, "Name",
50 DBUS_TYPE_STRING, &profile->name);
53 static void offlinemode_changed(struct connman_profile *profile)
55 connman_dbus_property_changed_basic(profile->path,
56 CONNMAN_PROFILE_INTERFACE, "OfflineMode",
57 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
59 if (profile != default_profile)
62 connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
63 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
64 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
67 connman_bool_t __connman_profile_get_offlinemode(void)
69 if (default_profile == NULL)
72 DBG("offlinemode %d", default_profile->offlinemode);
74 return default_profile->offlinemode;
77 int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
78 connman_bool_t all_devices)
80 DBG("offlinemode %d", offlinemode);
82 if (default_profile == NULL)
85 if (default_profile->offlinemode == offlinemode)
88 default_profile->offlinemode = offlinemode;
89 offlinemode_changed(default_profile);
92 __connman_device_set_offlinemode(offlinemode);
97 int __connman_profile_save_default(void)
101 if (default_profile != NULL)
102 __connman_storage_save_profile(default_profile);
107 const char *__connman_profile_active_ident(void)
111 return PROFILE_DEFAULT_IDENT;
114 const char *__connman_profile_active_path(void)
118 if (default_profile == NULL)
121 return default_profile->path;
124 static guint changed_timeout = 0;
126 static gboolean services_changed(gpointer user_data)
130 if (default_profile == NULL)
133 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
134 CONNMAN_MANAGER_INTERFACE, "Services",
135 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
138 connman_dbus_property_changed_array(default_profile->path,
139 CONNMAN_PROFILE_INTERFACE, "Services",
140 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
146 void __connman_profile_changed(gboolean delayed)
150 if (changed_timeout > 0) {
151 g_source_remove(changed_timeout);
155 if (__connman_connection_update_gateway() == TRUE) {
156 services_changed(NULL);
160 if (delayed == FALSE) {
161 services_changed(NULL);
165 changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
168 static DBusMessage *get_properties(DBusConnection *conn,
169 DBusMessage *msg, void *data)
171 struct connman_profile *profile = data;
173 DBusMessageIter array, dict;
175 DBG("conn %p", conn);
177 reply = dbus_message_new_method_return(msg);
181 dbus_message_iter_init_append(reply, &array);
183 connman_dbus_dict_open(&array, &dict);
185 if (profile->name != NULL)
186 connman_dbus_dict_append_basic(&dict, "Name",
187 DBUS_TYPE_STRING, &profile->name);
189 connman_dbus_dict_append_basic(&dict, "OfflineMode",
190 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
192 connman_dbus_dict_append_array(&dict, "Services",
193 DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
195 connman_dbus_dict_close(&array, &dict);
200 static DBusMessage *set_property(DBusConnection *conn,
201 DBusMessage *msg, void *data)
203 struct connman_profile *profile = data;
204 DBusMessageIter iter, value;
208 DBG("conn %p", conn);
210 if (dbus_message_iter_init(msg, &iter) == FALSE)
211 return __connman_error_invalid_arguments(msg);
213 dbus_message_iter_get_basic(&iter, &name);
214 dbus_message_iter_next(&iter);
215 dbus_message_iter_recurse(&iter, &value);
217 type = dbus_message_iter_get_arg_type(&value);
219 if (g_str_equal(name, "Name") == TRUE) {
222 if (type != DBUS_TYPE_STRING)
223 return __connman_error_invalid_arguments(msg);
225 dbus_message_iter_get_basic(&value, &name);
227 g_free(profile->name);
228 profile->name = g_strdup(name);
230 if (profile->name != NULL)
231 name_changed(profile);
233 __connman_storage_save_profile(profile);
234 } else if (g_str_equal(name, "OfflineMode") == TRUE) {
235 connman_bool_t offlinemode;
237 if (type != DBUS_TYPE_BOOLEAN)
238 return __connman_error_invalid_arguments(msg);
240 dbus_message_iter_get_basic(&value, &offlinemode);
242 if (profile->offlinemode == offlinemode)
243 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
245 profile->offlinemode = offlinemode;
246 offlinemode_changed(profile);
248 __connman_storage_save_profile(profile);
250 return __connman_error_invalid_property(msg);
252 return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
255 static GDBusMethodTable profile_methods[] = {
256 { "GetProperties", "", "a{sv}", get_properties },
257 { "SetProperty", "sv", "", set_property },
261 static GDBusSignalTable profile_signals[] = {
262 { "PropertyChanged", "sv" },
266 static void free_profile(struct connman_profile *profile)
268 g_free(profile->name);
269 g_free(profile->path);
270 g_free(profile->ident);
274 static int profile_init(void)
278 default_profile = g_try_new0(struct connman_profile, 1);
279 if (default_profile == NULL)
282 default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
283 default_profile->path = g_strdup_printf("/profile/%s",
284 PROFILE_DEFAULT_IDENT);
286 if (default_profile->ident == NULL || default_profile->path == NULL) {
287 free_profile(default_profile);
291 default_profile->name = g_strdup("Default");
293 __connman_storage_load_profile(default_profile);
295 connman_info("Adding default profile");
297 g_dbus_register_interface(connection, default_profile->path,
298 CONNMAN_PROFILE_INTERFACE,
299 profile_methods, profile_signals,
300 NULL, default_profile, NULL);
303 DBG("profile %p path %s", default_profile, default_profile->path);
308 static int profile_load(struct connman_profile *profile)
311 GError *error = NULL;
312 connman_bool_t offlinemode;
315 DBG("profile %p", profile);
317 keyfile = __connman_storage_open_profile(profile->ident);
321 name = g_key_file_get_string(keyfile, "global", "Name", NULL);
323 g_free(profile->name);
324 profile->name = name;
327 offlinemode = g_key_file_get_boolean(keyfile, "global",
328 "OfflineMode", &error);
330 profile->offlinemode = offlinemode;
331 g_clear_error(&error);
333 __connman_storage_close_profile(profile->ident, keyfile, FALSE);
338 static int profile_save(struct connman_profile *profile)
342 DBG("profile %p", profile);
344 keyfile = __connman_storage_open_profile(profile->ident);
348 if (profile->name != NULL)
349 g_key_file_set_string(keyfile, "global",
350 "Name", profile->name);
352 g_key_file_set_boolean(keyfile, "global",
353 "OfflineMode", profile->offlinemode);
355 __connman_storage_close_profile(profile->ident, keyfile, TRUE);
360 static struct connman_storage profile_storage = {
362 .priority = CONNMAN_STORAGE_PRIORITY_LOW,
363 .profile_init = profile_init,
364 .profile_load = profile_load,
365 .profile_save = profile_save,
368 int __connman_profile_init(void)
372 connection = connman_dbus_get_connection();
373 if (connection == NULL)
376 if (connman_storage_register(&profile_storage) < 0)
377 connman_error("Failed to register profile storage");
382 void __connman_profile_cleanup(void)
386 if (connection == NULL)
389 connman_storage_unregister(&profile_storage);
391 dbus_connection_unref(connection);