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