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
|
/*
*
* Connection Manager
*
* Copyright (C) 2011 BWM CarIT GmbH. 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 <errno.h>
#include <gdbus/gdbus.h>
#include "test-connman.h"
static enum connman_session_state string2state(const char *state)
{
if (g_strcmp0(state, "connected") == 0)
return CONNMAN_SESSION_STATE_CONNECTED;
if (g_strcmp0(state, "online") == 0)
return CONNMAN_SESSION_STATE_ONLINE;
return CONNMAN_SESSION_STATE_DISCONNECTED;
}
static enum connman_session_state string2type(const char *type)
{
if (g_strcmp0(type, "local") == 0)
return CONNMAN_SESSION_TYPE_LOCAL;
if (g_strcmp0(type, "internet") == 0)
return CONNMAN_SESSION_TYPE_INTERNET;
return CONNMAN_SESSION_TYPE_ANY;
}
static const char *roamingpolicy2string(enum connman_session_roaming_policy policy)
{
switch (policy) {
case CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN:
break;
case CONNMAN_SESSION_ROAMING_POLICY_DEFAULT:
return "default";
case CONNMAN_SESSION_ROAMING_POLICY_ALWAYS:
return "always";
case CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN:
return "forbidden";
case CONNMAN_SESSION_ROAMING_POLICY_NATIONAL:
return "national";
case CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL:
return "international";
}
return "";
}
static enum connman_session_roaming_policy string2roamingpolicy(const char *policy)
{
if (g_strcmp0(policy, "default") == 0)
return CONNMAN_SESSION_ROAMING_POLICY_DEFAULT;
else if (g_strcmp0(policy, "always") == 0)
return CONNMAN_SESSION_ROAMING_POLICY_ALWAYS;
else if (g_strcmp0(policy, "forbidden") == 0)
return CONNMAN_SESSION_ROAMING_POLICY_FORBIDDEN;
else if (g_strcmp0(policy, "national") == 0)
return CONNMAN_SESSION_ROAMING_POLICY_NATIONAL;
else if (g_strcmp0(policy, "international") == 0)
return CONNMAN_SESSION_ROAMING_POLICY_INTERNATIONAL;
else
return CONNMAN_SESSION_ROAMING_POLICY_UNKNOWN;
}
void bearer_info_cleanup(gpointer data, gpointer user_data)
{
struct test_bearer_info *info = data;
g_free(info->name);
g_free(info);
}
static GSList *session_parse_allowed_bearers(DBusMessageIter *iter)
{
struct test_bearer_info *info;
DBusMessageIter array;
GSList *list = NULL;
dbus_message_iter_recurse(iter, &array);
while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_STRING) {
char *bearer = NULL;
dbus_message_iter_get_basic(&array, &bearer);
info = g_try_new0(struct test_bearer_info, 1);
if (info == NULL) {
g_slist_foreach(list, bearer_info_cleanup, NULL);
g_slist_free(list);
return NULL;
}
info->name = g_strdup(bearer);
list = g_slist_append(list, info);
dbus_message_iter_next(&array);
}
return list;
}
static DBusMessage *notify_release(DBusConnection *conn,
DBusMessage *msg, void *user_data)
{
struct test_session *session = user_data;
LOG("session %p", session);
if (session->notify != NULL)
session->notify(session);
return NULL;
}
static DBusMessage *notify_update(DBusConnection *conn,
DBusMessage *msg, void *user_data)
{
struct test_session *session = user_data;
struct test_session_info *info = session->info;
DBusMessageIter iter, array;
GSList *allowed_bearers;
LOG("session %p notify %s", session, session->notify_path);
dbus_message_iter_init(msg, &iter);
dbus_message_iter_recurse(&iter, &array);
while (dbus_message_iter_get_arg_type(&array) == DBUS_TYPE_DICT_ENTRY) {
DBusMessageIter entry, value;
const char *key;
dbus_message_iter_recurse(&array, &entry);
dbus_message_iter_get_basic(&entry, &key);
dbus_message_iter_next(&entry);
dbus_message_iter_recurse(&entry, &value);
switch (dbus_message_iter_get_arg_type(&value)) {
case DBUS_TYPE_ARRAY:
if (g_str_equal(key, "AllowedBearers") == TRUE) {
allowed_bearers = session_parse_allowed_bearers(&value);
g_slist_foreach(info->allowed_bearers,
bearer_info_cleanup, NULL);
g_slist_free(info->allowed_bearers);
info->allowed_bearers = allowed_bearers;
} else if (g_str_equal(key, "IPv4") == TRUE) {
/* XXX */
} else if (g_str_equal(key, "IPv6") == TRUE) {
/* XXX */
} else {
g_assert(FALSE);
return __connman_error_invalid_arguments(msg);
}
break;
case DBUS_TYPE_BOOLEAN:
if (g_str_equal(key, "Priority") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->priority);
} else if (g_str_equal(key, "AvoidHandover") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->avoid_handover);
} else if (g_str_equal(key, "StayConnected") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->stay_connected);
} else if (g_str_equal(key, "EmergencyCall") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->ecall);
} else {
g_assert(FALSE);
return __connman_error_invalid_arguments(msg);
}
break;
case DBUS_TYPE_UINT32:
if (g_str_equal(key, "PeriodicConnect") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->periodic_connect);
} else if (g_str_equal(key, "IdleTimeout") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->idle_timeout);
} else if (g_str_equal(key, "SessionMarker") == TRUE) {
dbus_message_iter_get_basic(&value,
&info->marker);
} else {
g_assert(FALSE);
return __connman_error_invalid_arguments(msg);
}
break;
case DBUS_TYPE_STRING:
if (g_str_equal(key, "State") == TRUE) {
const char *val;
dbus_message_iter_get_basic(&value, &val);
info->state = string2state(val);
} else if (g_str_equal(key, "Bearer") == TRUE) {
const char *val;
dbus_message_iter_get_basic(&value, &val);
if (info->bearer != NULL)
g_free(info->bearer);
info->bearer = g_strdup(val);
} else if (g_str_equal(key, "Name") == TRUE) {
const char *val;
dbus_message_iter_get_basic(&value, &val);
if (info->name != NULL)
g_free(info->name);
info->name = g_strdup(val);
} else if (g_str_equal(key, "RoamingPolicy") == TRUE) {
const char *val;
dbus_message_iter_get_basic(&value, &val);
info->roaming_policy =
string2roamingpolicy(val);
} else if (g_str_equal(key, "Interface") == TRUE) {
const char *val;
dbus_message_iter_get_basic(&value, &val);
if (info->interface != NULL)
g_free(info->interface);
info->interface = g_strdup(val);
} else if (g_str_equal(key, "ConnectionType")
== TRUE) {
const char *val;
dbus_message_iter_get_basic(&value, &val);
info->type = string2type(val);
} else {
g_assert(FALSE);
return __connman_error_invalid_arguments(msg);
}
break;
default:
g_assert(FALSE);
return __connman_error_invalid_arguments(msg);
}
dbus_message_iter_next(&array);
}
if (session->notify != NULL)
session->notify(session);
return g_dbus_create_reply(msg, DBUS_TYPE_INVALID);
}
static const GDBusMethodTable notify_methods[] = {
{ GDBUS_METHOD("Release", NULL, NULL, notify_release) },
{ GDBUS_METHOD("Update",
GDBUS_ARGS({ "settings", "a{sv}" }), NULL,
notify_update) },
{ },
};
int session_notify_register(struct test_session *session,
const char *notify_path)
{
if (g_dbus_register_interface(session->connection, notify_path,
CONNMAN_NOTIFICATION_INTERFACE,
notify_methods, NULL, NULL,
session, NULL) == FALSE) {
return -EINVAL;
}
return 0;
}
int session_notify_unregister(struct test_session *session,
const char *notify_path)
{
if (g_dbus_unregister_interface(session->connection, notify_path,
CONNMAN_NOTIFICATION_INTERFACE) == FALSE) {
return -EINVAL;
}
return 0;
}
static void append_allowed_bearers(DBusMessageIter *iter, void *user_data)
{
struct test_session_info *info = user_data;
GSList *list;
for (list = info->allowed_bearers;
list != NULL; list = list->next) {
struct test_bearer_info *info = list->data;
dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
&info->name);
}
}
void session_append_settings(DBusMessageIter *dict,
struct test_session_info *info)
{
const char *policy;
connman_dbus_dict_append_basic(dict, "Priority",
DBUS_TYPE_BOOLEAN,
&info->priority);
connman_dbus_dict_append_array(dict, "AllowedBearers",
DBUS_TYPE_STRING,
append_allowed_bearers,
info);
connman_dbus_dict_append_basic(dict, "AvoidHandover",
DBUS_TYPE_BOOLEAN,
&info->avoid_handover);
connman_dbus_dict_append_basic(dict, "StayConnected",
DBUS_TYPE_BOOLEAN,
&info->stay_connected);
connman_dbus_dict_append_basic(dict, "PeriodicConnect",
DBUS_TYPE_UINT32,
&info->periodic_connect);
connman_dbus_dict_append_basic(dict, "IdleTimeout",
DBUS_TYPE_UINT32,
&info->idle_timeout);
connman_dbus_dict_append_basic(dict, "EmergencyCall",
DBUS_TYPE_BOOLEAN,
&info->ecall);
policy = roamingpolicy2string(info->roaming_policy);
connman_dbus_dict_append_basic(dict, "RoamingPolicy",
DBUS_TYPE_STRING,
&policy);
}
DBusMessage *session_connect(DBusConnection *connection,
struct test_session *session)
{
DBusMessage *message, *reply;
DBusError error;
message = dbus_message_new_method_call(CONNMAN_SERVICE,
session->session_path,
CONNMAN_SESSION_INTERFACE,
"Connect");
if (message == NULL)
return NULL;
dbus_error_init(&error);
reply = dbus_connection_send_with_reply_and_block(connection,
message, -1, &error);
if (reply == NULL) {
if (dbus_error_is_set(&error) == TRUE) {
LOG("%s", error.message);
dbus_error_free(&error);
} else {
LOG("Failed to get properties");
}
dbus_message_unref(message);
return NULL;
}
dbus_message_unref(message);
return reply;
}
DBusMessage *session_disconnect(DBusConnection *connection,
struct test_session *session)
{
DBusMessage *message, *reply;
DBusError error;
message = dbus_message_new_method_call(CONNMAN_SERVICE,
session->session_path,
CONNMAN_SESSION_INTERFACE,
"Disconnect");
if (message == NULL)
return NULL;
dbus_error_init(&error);
reply = dbus_connection_send_with_reply_and_block(connection,
message, -1, &error);
if (reply == NULL) {
if (dbus_error_is_set(&error) == TRUE) {
LOG("%s", error.message);
dbus_error_free(&error);
} else {
LOG("Failed to get properties");
}
dbus_message_unref(message);
return NULL;
}
dbus_message_unref(message);
return reply;
}
|