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