manager: Remove the Create/RemoveProfile D-Bus API
[framework/connectivity/connman.git] / src / profile.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #include <glib.h>
29 #include <gdbus.h>
30
31 #include "connman.h"
32
33 #define PROFILE_DEFAULT_IDENT  "default"
34
35 struct connman_profile {
36         char *ident;
37         char *path;
38         char *name;
39         connman_bool_t offlinemode;
40 };
41
42 static struct connman_profile *default_profile = NULL;
43
44 static DBusConnection *connection = NULL;
45
46 static void name_changed(struct connman_profile *profile)
47 {
48         connman_dbus_property_changed_basic(profile->path,
49                                 CONNMAN_PROFILE_INTERFACE, "Name",
50                                         DBUS_TYPE_STRING, &profile->name);
51 }
52
53 static void offlinemode_changed(struct connman_profile *profile)
54 {
55         connman_dbus_property_changed_basic(profile->path,
56                                 CONNMAN_PROFILE_INTERFACE, "OfflineMode",
57                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
58
59         if (profile != default_profile)
60                 return;
61
62         connman_dbus_property_changed_basic(CONNMAN_MANAGER_PATH,
63                                 CONNMAN_MANAGER_INTERFACE, "OfflineMode",
64                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
65 }
66
67 connman_bool_t __connman_profile_get_offlinemode(void)
68 {
69         if (default_profile == NULL)
70                 return FALSE;
71
72         DBG("offlinemode %d", default_profile->offlinemode);
73
74         return default_profile->offlinemode;
75 }
76
77 int __connman_profile_set_offlinemode(connman_bool_t offlinemode,
78                                         connman_bool_t all_devices)
79 {
80         DBG("offlinemode %d", offlinemode);
81
82         if (default_profile == NULL)
83                 return -EINVAL;
84
85         if (default_profile->offlinemode == offlinemode)
86                 return -EALREADY;
87
88         default_profile->offlinemode = offlinemode;
89         offlinemode_changed(default_profile);
90
91         if (all_devices)
92                 __connman_device_set_offlinemode(offlinemode);
93
94         return 0;
95 }
96
97 int __connman_profile_save_default(void)
98 {
99         DBG("");
100
101         if (default_profile != NULL)
102                 __connman_storage_save_profile(default_profile);
103
104         return 0;
105 }
106
107 const char *__connman_profile_active_ident(void)
108 {
109         DBG("");
110
111         return PROFILE_DEFAULT_IDENT;
112 }
113
114 const char *__connman_profile_active_path(void)
115 {
116         DBG("");
117
118         if (default_profile == NULL)
119                 return NULL;
120
121         return default_profile->path;
122 }
123
124 static guint changed_timeout = 0;
125
126 static gboolean services_changed(gpointer user_data)
127 {
128         changed_timeout = 0;
129
130         if (default_profile == NULL)
131                 return FALSE;
132
133         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
134                                 CONNMAN_MANAGER_INTERFACE, "Services",
135                                 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
136                                 NULL);
137
138         connman_dbus_property_changed_array(default_profile->path,
139                                 CONNMAN_PROFILE_INTERFACE, "Services",
140                                 DBUS_TYPE_OBJECT_PATH, __connman_service_list,
141                                 NULL);
142
143         return FALSE;
144 }
145
146 void __connman_profile_changed(gboolean delayed)
147 {
148         DBG("");
149
150         if (changed_timeout > 0) {
151                 g_source_remove(changed_timeout);
152                 changed_timeout = 0;
153         }
154
155         if (__connman_connection_update_gateway() == TRUE) {
156                 services_changed(NULL);
157                 return;
158         }
159
160         if (delayed == FALSE) {
161                 services_changed(NULL);
162                 return;
163         }
164
165         changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
166 }
167
168 static DBusMessage *get_properties(DBusConnection *conn,
169                                         DBusMessage *msg, void *data)
170 {
171         struct connman_profile *profile = data;
172         DBusMessage *reply;
173         DBusMessageIter array, dict;
174
175         DBG("conn %p", conn);
176
177         reply = dbus_message_new_method_return(msg);
178         if (reply == NULL)
179                 return NULL;
180
181         dbus_message_iter_init_append(reply, &array);
182
183         connman_dbus_dict_open(&array, &dict);
184
185         if (profile->name != NULL)
186                 connman_dbus_dict_append_basic(&dict, "Name",
187                                         DBUS_TYPE_STRING, &profile->name);
188
189         connman_dbus_dict_append_basic(&dict, "OfflineMode",
190                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
191
192         connman_dbus_dict_append_array(&dict, "Services",
193                         DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
194
195         connman_dbus_dict_close(&array, &dict);
196
197         return reply;
198 }
199
200 static DBusMessage *set_property(DBusConnection *conn,
201                                         DBusMessage *msg, void *data)
202 {
203         struct connman_profile *profile = data;
204         DBusMessageIter iter, value;
205         const char *name;
206         int type;
207
208         DBG("conn %p", conn);
209
210         if (dbus_message_iter_init(msg, &iter) == FALSE)
211                 return __connman_error_invalid_arguments(msg);
212
213         dbus_message_iter_get_basic(&iter, &name);
214         dbus_message_iter_next(&iter);
215         dbus_message_iter_recurse(&iter, &value);
216
217         type = dbus_message_iter_get_arg_type(&value);
218
219         if (g_str_equal(name, "Name") == TRUE) {
220                 const char *name;
221
222                 if (type != DBUS_TYPE_STRING)
223                         return __connman_error_invalid_arguments(msg);
224
225                 dbus_message_iter_get_basic(&value, &name);
226
227                 g_free(profile->name);
228                 profile->name = g_strdup(name);
229
230                 if (profile->name != NULL)
231                         name_changed(profile);
232
233                 __connman_storage_save_profile(profile);
234         } else if (g_str_equal(name, "OfflineMode") == TRUE) {
235                 connman_bool_t offlinemode;
236
237                 if (type != DBUS_TYPE_BOOLEAN)
238                         return __connman_error_invalid_arguments(msg);
239
240                 dbus_message_iter_get_basic(&value, &offlinemode);
241
242                 if (profile->offlinemode == offlinemode)
243                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
244
245                 profile->offlinemode = offlinemode;
246                 offlinemode_changed(profile);
247
248                 __connman_storage_save_profile(profile);
249         } else
250                 return __connman_error_invalid_property(msg);
251
252         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
253 }
254
255 static GDBusMethodTable profile_methods[] = {
256         { "GetProperties", "",   "a{sv}", get_properties },
257         { "SetProperty",   "sv", "",      set_property   },
258         { },
259 };
260
261 static GDBusSignalTable profile_signals[] = {
262         { "PropertyChanged", "sv" },
263         { },
264 };
265
266 static void free_profile(struct connman_profile *profile)
267 {
268         g_free(profile->name);
269         g_free(profile->path);
270         g_free(profile->ident);
271         g_free(profile);
272 }
273
274 static int profile_init(void)
275 {
276         DBG("");
277
278         default_profile = g_try_new0(struct connman_profile, 1);
279         if (default_profile == NULL)
280                 return -ENOMEM;
281
282         default_profile->ident = g_strdup(PROFILE_DEFAULT_IDENT);
283         default_profile->path = g_strdup_printf("/profile/%s",
284                                         PROFILE_DEFAULT_IDENT);
285
286         if (default_profile->ident == NULL || default_profile->path == NULL) {
287                 free_profile(default_profile);
288                 return -ENOMEM;
289         }
290
291         default_profile->name = g_strdup("Default");
292
293         __connman_storage_load_profile(default_profile);
294
295         connman_info("Adding default profile");
296
297         g_dbus_register_interface(connection, default_profile->path,
298                                         CONNMAN_PROFILE_INTERFACE,
299                                         profile_methods, profile_signals,
300                                                 NULL, default_profile, NULL);
301
302
303         DBG("profile %p path %s", default_profile, default_profile->path);
304
305         return 0;
306 }
307
308 static int profile_load(struct connman_profile *profile)
309 {
310         GKeyFile *keyfile;
311         GError *error = NULL;
312         connman_bool_t offlinemode;
313         char *name;
314
315         DBG("profile %p", profile);
316
317         keyfile = __connman_storage_open_profile(profile->ident);
318         if (keyfile == NULL)
319                 return -EIO;
320
321         name = g_key_file_get_string(keyfile, "global", "Name", NULL);
322         if (name != NULL) {
323                 g_free(profile->name);
324                 profile->name = name;
325         }
326
327         offlinemode = g_key_file_get_boolean(keyfile, "global",
328                                                 "OfflineMode", &error);
329         if (error == NULL)
330                 profile->offlinemode = offlinemode;
331         g_clear_error(&error);
332
333         __connman_storage_close_profile(profile->ident, keyfile, FALSE);
334
335         return 0;
336 }
337
338 static int profile_save(struct connman_profile *profile)
339 {
340         GKeyFile *keyfile;
341
342         DBG("profile %p", profile);
343
344         keyfile = __connman_storage_open_profile(profile->ident);
345         if (keyfile == NULL)
346                 return -EIO;
347
348         if (profile->name != NULL)
349                 g_key_file_set_string(keyfile, "global",
350                                                 "Name", profile->name);
351
352         g_key_file_set_boolean(keyfile, "global",
353                                         "OfflineMode", profile->offlinemode);
354
355         __connman_storage_close_profile(profile->ident, keyfile, TRUE);
356
357         return 0;
358 }
359
360 static struct connman_storage profile_storage = {
361         .name           = "profile",
362         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
363         .profile_init   = profile_init,
364         .profile_load   = profile_load,
365         .profile_save   = profile_save,
366 };
367
368 int __connman_profile_init(void)
369 {
370         DBG("");
371
372         connection = connman_dbus_get_connection();
373         if (connection == NULL)
374                 return -1;
375
376         if (connman_storage_register(&profile_storage) < 0)
377                 connman_error("Failed to register profile storage");
378
379         return 0;
380 }
381
382 void __connman_profile_cleanup(void)
383 {
384         DBG("");
385
386         if (connection == NULL)
387                 return;
388
389         connman_storage_unregister(&profile_storage);
390
391         dbus_connection_unref(connection);
392 }