Rename D-Bus helper function for variable arrays
[framework/connectivity/connman.git] / src / profile.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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 GHashTable *profile_hash = NULL;
43 static struct connman_profile *default_profile = NULL;
44
45 static DBusConnection *connection = NULL;
46
47 static void append_path(gpointer key, gpointer value, gpointer user_data)
48 {
49         struct connman_profile *profile = value;
50         DBusMessageIter *iter = user_data;
51
52         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
53                                                         &profile->path);
54 }
55
56 void __connman_profile_list(DBusMessageIter *iter, void *user_data)
57 {
58         g_hash_table_foreach(profile_hash, append_path, iter);
59 }
60
61 static void profiles_changed(void)
62 {
63         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
64                         CONNMAN_MANAGER_INTERFACE, "Profiles",
65                         DBUS_TYPE_OBJECT_PATH, __connman_profile_list, NULL);
66 }
67
68 static void name_changed(struct connman_profile *profile)
69 {
70         connman_dbus_property_changed_basic(profile->path,
71                                 CONNMAN_PROFILE_INTERFACE, "Name",
72                                         DBUS_TYPE_STRING, &profile->name);
73 }
74
75 static void offlinemode_changed(struct connman_profile *profile)
76 {
77         connman_dbus_property_changed_basic(profile->path,
78                                 CONNMAN_PROFILE_INTERFACE, "OfflineMode",
79                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
80 }
81
82 connman_bool_t __connman_profile_get_offlinemode(void)
83 {
84         if (default_profile == NULL)
85                 return FALSE;
86
87         DBG("offlinemode %d", default_profile->offlinemode);
88
89         return default_profile->offlinemode;
90 }
91
92 int __connman_profile_set_offlinemode(connman_bool_t offlinemode)
93 {
94         DBG("offlinemode %d", offlinemode);
95
96         if (default_profile == NULL)
97                 return -EINVAL;
98
99         if (default_profile->offlinemode == offlinemode)
100                 return -EALREADY;
101
102         default_profile->offlinemode = offlinemode;
103         offlinemode_changed(default_profile);
104
105         __connman_device_set_offlinemode(offlinemode);
106
107         return 0;
108 }
109
110 int __connman_profile_save_default(void)
111 {
112         DBG("");
113
114         if (default_profile != NULL)
115                 __connman_storage_save_profile(default_profile);
116
117         return 0;
118 }
119
120 const char *__connman_profile_active_ident(void)
121 {
122         DBG("");
123
124         return PROFILE_DEFAULT_IDENT;
125 }
126
127 const char *__connman_profile_active_path(void)
128 {
129         DBG("");
130
131         if (default_profile == NULL)
132                 return NULL;
133
134         return default_profile->path;
135 }
136
137 static guint changed_timeout = 0;
138
139 static gboolean services_changed(gpointer user_data)
140 {
141         struct connman_profile *profile = default_profile;
142         connman_dbus_append_cb_t function = NULL;
143
144         changed_timeout = 0;
145
146         if (profile == NULL)
147                 return FALSE;
148
149         if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0) {
150                 function = __connman_service_list;
151
152                 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
153                                 CONNMAN_MANAGER_INTERFACE, "Services",
154                                 DBUS_TYPE_OBJECT_PATH, function, NULL);
155         }
156
157         connman_dbus_property_changed_array(profile->path,
158                                 CONNMAN_PROFILE_INTERFACE, "Services",
159                                 DBUS_TYPE_OBJECT_PATH, function, NULL);
160
161         return FALSE;
162 }
163
164 void __connman_profile_changed(gboolean delayed)
165 {
166         DBG("");
167
168         if (changed_timeout > 0) {
169                 g_source_remove(changed_timeout);
170                 changed_timeout = 0;
171         }
172
173         if (__connman_connection_update_gateway() == TRUE) {
174                 services_changed(NULL);
175                 return;
176         }
177
178         if (delayed == FALSE) {
179                 services_changed(NULL);
180                 return;
181         }
182
183         changed_timeout = g_timeout_add_seconds(1, services_changed, NULL);
184 }
185
186 int __connman_profile_add_network(struct connman_network *network)
187 {
188         struct connman_service *service;
189
190         DBG("network %p", network);
191
192         service = __connman_service_create_from_network(network);
193         if (service == NULL)
194                 return -EINVAL;
195
196         return 0;
197 }
198
199 int __connman_profile_update_network(struct connman_network *network)
200 {
201         DBG("network %p", network);
202
203         __connman_service_update_from_network(network);
204
205         return 0;
206 }
207
208 int __connman_profile_remove_network(struct connman_network *network)
209 {
210         DBG("network %p", network);
211
212         __connman_service_remove_from_network(network);
213
214         return 0;
215 }
216
217 static DBusMessage *get_properties(DBusConnection *conn,
218                                         DBusMessage *msg, void *data)
219 {
220         struct connman_profile *profile = data;
221         DBusMessage *reply;
222         DBusMessageIter array, dict;
223
224         DBG("conn %p", conn);
225
226         reply = dbus_message_new_method_return(msg);
227         if (reply == NULL)
228                 return NULL;
229
230         dbus_message_iter_init_append(reply, &array);
231
232         connman_dbus_dict_open(&array, &dict);
233
234         if (profile->name != NULL)
235                 connman_dbus_dict_append_basic(&dict, "Name",
236                                         DBUS_TYPE_STRING, &profile->name);
237
238         connman_dbus_dict_append_basic(&dict, "OfflineMode",
239                                 DBUS_TYPE_BOOLEAN, &profile->offlinemode);
240
241         connman_dbus_dict_append_array(&dict, "Services",
242                         DBUS_TYPE_OBJECT_PATH, __connman_service_list, NULL);
243
244         connman_dbus_dict_close(&array, &dict);
245
246         return reply;
247 }
248
249 static DBusMessage *set_property(DBusConnection *conn,
250                                         DBusMessage *msg, void *data)
251 {
252         struct connman_profile *profile = data;
253         DBusMessageIter iter, value;
254         const char *name;
255         int type;
256
257         DBG("conn %p", conn);
258
259         if (dbus_message_iter_init(msg, &iter) == FALSE)
260                 return __connman_error_invalid_arguments(msg);
261
262         dbus_message_iter_get_basic(&iter, &name);
263         dbus_message_iter_next(&iter);
264         dbus_message_iter_recurse(&iter, &value);
265
266         if (__connman_security_check_privilege(msg,
267                                         CONNMAN_SECURITY_PRIVILEGE_MODIFY) < 0)
268                 return __connman_error_permission_denied(msg);
269
270         type = dbus_message_iter_get_arg_type(&value);
271
272         if (g_str_equal(name, "Name") == TRUE) {
273                 const char *name;
274
275                 if (type != DBUS_TYPE_STRING)
276                         return __connman_error_invalid_arguments(msg);
277
278                 dbus_message_iter_get_basic(&value, &name);
279
280                 g_free(profile->name);
281                 profile->name = g_strdup(name);
282
283                 if (profile->name != NULL)
284                         name_changed(profile);
285
286                 __connman_storage_save_profile(profile);
287         } else if (g_str_equal(name, "OfflineMode") == TRUE) {
288                 connman_bool_t offlinemode;
289
290                 if (type != DBUS_TYPE_BOOLEAN)
291                         return __connman_error_invalid_arguments(msg);
292
293                 dbus_message_iter_get_basic(&value, &offlinemode);
294
295                 if (profile->offlinemode == offlinemode)
296                         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
297
298                 profile->offlinemode = offlinemode;
299                 offlinemode_changed(profile);
300
301                 __connman_storage_save_profile(profile);
302         } else
303                 return __connman_error_invalid_property(msg);
304
305         return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
306 }
307
308 static GDBusMethodTable profile_methods[] = {
309         { "GetProperties", "",   "a{sv}", get_properties },
310         { "SetProperty",   "sv", "",      set_property   },
311         { },
312 };
313
314 static GDBusSignalTable profile_signals[] = {
315         { "PropertyChanged", "sv" },
316         { },
317 };
318
319 static void free_profile(struct connman_profile *profile)
320 {
321         g_free(profile->name);
322         g_free(profile->path);
323         g_free(profile->ident);
324         g_free(profile);
325 }
326
327 static void unregister_profile(gpointer data)
328 {
329         struct connman_profile *profile = data;
330
331         DBG("profile %p", profile);
332
333         connman_info("Removing profile %s", profile->ident);
334
335         g_dbus_unregister_interface(connection, profile->path,
336                                                 CONNMAN_PROFILE_INTERFACE);
337
338         if (g_strcmp0(profile->ident, PROFILE_DEFAULT_IDENT) == 0)
339                 default_profile = NULL;
340
341         free_profile(profile);
342 }
343
344 static int create_profile(const char *ident, const char *name,
345                                                         const char **path)
346 {
347         struct connman_profile *profile;
348
349         DBG("ident %s name %s", ident, name);
350
351         profile = g_try_new0(struct connman_profile, 1);
352         if (profile == NULL)
353                 return -ENOMEM;
354
355         profile->ident = g_strdup(ident);
356         profile->path = g_strdup_printf("/profile/%s", ident);
357
358         if (profile->ident == NULL || profile->path == NULL) {
359                 free_profile(profile);
360                 return -ENOMEM;
361         }
362
363         if (g_hash_table_lookup(profile_hash, profile->path) != NULL) {
364                 free_profile(profile);
365                 return -EEXIST;
366         }
367
368         profile->name = g_strdup(name);
369
370         __connman_storage_load_profile(profile);
371
372         g_hash_table_insert(profile_hash, g_strdup(profile->path), profile);
373
374         connman_info("Adding profile %s", ident);
375
376         if (g_strcmp0(ident, PROFILE_DEFAULT_IDENT) == 0)
377                 default_profile = profile;
378
379         g_dbus_register_interface(connection, profile->path,
380                                         CONNMAN_PROFILE_INTERFACE,
381                                         profile_methods, profile_signals,
382                                                         NULL, profile, NULL);
383
384         if (path != NULL)
385                 *path = profile->path;
386
387         DBG("profile %p path %s", profile, profile->path);
388
389         return 0;
390 }
391
392 int __connman_profile_create(const char *name, const char **path)
393 {
394         struct connman_profile *profile;
395         int err;
396
397         DBG("name %s", name);
398
399         if (connman_dbus_validate_ident(name) == FALSE)
400                 return -EINVAL;
401
402         err = create_profile(name, NULL, path);
403         if (err < 0)
404                 return err;
405
406         profile = g_hash_table_lookup(profile_hash, *path);
407         if (profile == NULL)
408                 return -EIO;
409
410         __connman_storage_save_profile(profile);
411
412         profiles_changed();
413
414         return 0;
415 }
416
417 int __connman_profile_remove(const char *path)
418 {
419         struct connman_profile *profile;
420
421         DBG("path %s", path);
422
423         if (default_profile != NULL &&
424                                 g_strcmp0(path, default_profile->path) == 0)
425                 return -EINVAL;
426
427         profile = g_hash_table_lookup(profile_hash, path);
428         if (profile == NULL)
429                 return -ENXIO;
430
431         __connman_storage_delete_profile(profile->ident);
432
433         g_hash_table_remove(profile_hash, path);
434
435         profiles_changed();
436
437         return 0;
438 }
439
440 static int profile_init(void)
441 {
442         GDir *dir;
443         const gchar *file;
444
445         DBG("");
446
447         dir = g_dir_open(STORAGEDIR, 0, NULL);
448         if (dir != NULL) {
449                 while ((file = g_dir_read_name(dir)) != NULL) {
450                         GString *str;
451                         gchar *ident;
452
453                         if (g_str_has_suffix(file, ".profile") == FALSE)
454                                 continue;
455
456                         ident = g_strrstr(file, ".profile");
457                         if (ident == NULL)
458                                 continue;
459
460                         str = g_string_new_len(file, ident - file);
461                         if (str == NULL)
462                                 continue;
463
464                         ident = g_string_free(str, FALSE);
465
466                         if (connman_dbus_validate_ident(ident) == TRUE)
467                                 create_profile(ident, NULL, NULL);
468
469                         g_free(ident);
470                 }
471
472                 g_dir_close(dir);
473         }
474
475         if (g_hash_table_size(profile_hash) == 0)
476                 create_profile(PROFILE_DEFAULT_IDENT, "Default", NULL);
477
478         profiles_changed();
479
480         return 0;
481 }
482
483 static int profile_load(struct connman_profile *profile)
484 {
485         GKeyFile *keyfile;
486         GError *error = NULL;
487         connman_bool_t offlinemode;
488         char *name;
489
490         DBG("profile %p", profile);
491
492         keyfile = __connman_storage_open_profile(profile->ident);
493         if (keyfile == NULL)
494                 return -EIO;
495
496         name = g_key_file_get_string(keyfile, "global", "Name", NULL);
497         if (name != NULL) {
498                 g_free(profile->name);
499                 profile->name = name;
500         }
501
502         offlinemode = g_key_file_get_boolean(keyfile, "global",
503                                                 "OfflineMode", &error);
504         if (error == NULL)
505                 profile->offlinemode = offlinemode;
506         g_clear_error(&error);
507
508         __connman_storage_close_profile(profile->ident, keyfile, FALSE);
509
510         return 0;
511 }
512
513 static int profile_save(struct connman_profile *profile)
514 {
515         GKeyFile *keyfile;
516
517         DBG("profile %p", profile);
518
519         keyfile = __connman_storage_open_profile(profile->ident);
520         if (keyfile == NULL)
521                 return -EIO;
522
523         if (profile->name != NULL)
524                 g_key_file_set_string(keyfile, "global",
525                                                 "Name", profile->name);
526
527         g_key_file_set_boolean(keyfile, "global",
528                                         "OfflineMode", profile->offlinemode);
529
530         __connman_storage_close_profile(profile->ident, keyfile, TRUE);
531
532         return 0;
533 }
534
535 static struct connman_storage profile_storage = {
536         .name           = "profile",
537         .priority       = CONNMAN_STORAGE_PRIORITY_LOW,
538         .profile_init   = profile_init,
539         .profile_load   = profile_load,
540         .profile_save   = profile_save,
541 };
542
543 int __connman_profile_init(void)
544 {
545         DBG("");
546
547         connection = connman_dbus_get_connection();
548         if (connection == NULL)
549                 return -1;
550
551         if (connman_storage_register(&profile_storage) < 0)
552                 connman_error("Failed to register profile storage");
553
554         profile_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
555                                                 g_free, unregister_profile);
556
557         return 0;
558 }
559
560 void __connman_profile_cleanup(void)
561 {
562         DBG("");
563
564         if (connection == NULL)
565                 return;
566
567         g_hash_table_destroy(profile_hash);
568         profile_hash = NULL;
569
570         connman_storage_unregister(&profile_storage);
571
572         dbus_connection_unref(connection);
573 }