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