summaryrefslogtreecommitdiff
path: root/dundee/device.c
blob: 709919a82cbdad289f27de491766bd658692c275 (plain)
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2012  Intel Corporation. All rights reserved.
 *  Copyright (C) 2012  BMW Car IT 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 <string.h>
#include <stdio.h>
#include <netinet/ether.h>

#include <glib.h>
#include <gdbus.h>
#include <gatchat.h>
#include <gatppp.h>

#include "dundee.h"

static int next_device_id = 0;
static GHashTable *device_hash;

static const char *none_prefix[] = { NULL };

struct ipv4_settings {
	char *interface;
	char *ip;
	char **nameservers;
};

struct dundee_device {
	char *path;
	struct dundee_device_driver *driver;
	gboolean registered;

	GAtPPP *ppp;
	GAtChat *chat;

	char *name;
	gboolean active;
	struct ipv4_settings settings;

	DBusMessage *pending;
	void *data;
};

const char *__dundee_device_get_path(struct dundee_device *device)
{
	return device->path;
}

static void settings_append(struct dundee_device *device,
					DBusMessageIter *iter)
{
	DBusMessageIter variant;
	DBusMessageIter array;
	char typesig[5];
	char arraysig[6];

	arraysig[0] = DBUS_TYPE_ARRAY;
	arraysig[1] = typesig[0] = DBUS_DICT_ENTRY_BEGIN_CHAR;
	arraysig[2] = typesig[1] = DBUS_TYPE_STRING;
	arraysig[3] = typesig[2] = DBUS_TYPE_VARIANT;
	arraysig[4] = typesig[3] = DBUS_DICT_ENTRY_END_CHAR;
	arraysig[5] = typesig[4] = '\0';

	dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
						arraysig, &variant);

	dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
						typesig, &array);

	if (device->active == FALSE)
		goto out;

	if (device->settings.interface)
		ofono_dbus_dict_append(&array, "Interface",
				DBUS_TYPE_STRING, &device->settings.interface);

	if (device->settings.ip)
		ofono_dbus_dict_append(&array, "Address", DBUS_TYPE_STRING,
					&device->settings.ip);

	if (device->settings.nameservers)
		ofono_dbus_dict_append_array(&array, "DomainNameServers",
						DBUS_TYPE_STRING,
						&device->settings.nameservers);

out:
	dbus_message_iter_close_container(&variant, &array);

	dbus_message_iter_close_container(iter, &variant);
}

static void settings_append_dict(struct dundee_device *device,
				DBusMessageIter *dict)
{
	DBusMessageIter entry;
	const char *key = "Settings";

	dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
						NULL, &entry);

	dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);

	settings_append(device, &entry);

	dbus_message_iter_close_container(dict, &entry);
}

void __dundee_device_append_properties(struct dundee_device *device,
					DBusMessageIter *dict)
{
	settings_append_dict(device, dict);

	ofono_dbus_dict_append(dict, "Name", DBUS_TYPE_STRING,
				&device->name);

	ofono_dbus_dict_append(dict, "Active", DBUS_TYPE_BOOLEAN,
				&device->active);
}

void __dundee_device_foreach(dundee_device_foreach_func func, void *userdata)
{
	GHashTableIter iter;
	gpointer key, value;

	DBG("");

	g_hash_table_iter_init(&iter, device_hash);

	while (g_hash_table_iter_next(&iter, &key, &value) == TRUE) {
		struct dundee_device *device = value;

		func(device, userdata);
	}
}

static void settings_changed(struct dundee_device *device)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	DBusMessage *signal;
	DBusMessageIter iter;
	const char *key = "Settings";

	signal = dbus_message_new_signal(device->path,
					DUNDEE_DEVICE_INTERFACE,
					"PropertyChanged");

	if (signal == NULL)
		return;
	dbus_message_iter_init_append(signal, &iter);
	dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &key);

	settings_append(device, &iter);

	g_dbus_send_message(conn, signal);
}

static DBusMessage *device_get_properties(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	struct dundee_device *device = data;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter dict;

	reply = dbus_message_new_method_return(msg);
	if (reply == NULL)
		return NULL;

	dbus_message_iter_init_append(reply, &iter);

	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
					OFONO_PROPERTIES_ARRAY_SIGNATURE,
					&dict);

	__dundee_device_append_properties(device, &dict);

	dbus_message_iter_close_container(&iter, &dict);

	return reply;
}


static void debug(const char *str, void *data)
{
	DBG("%s: %s\n", (const char *) data, str);
}

static void ppp_connect(const char *iface, const char *local, const char *peer,
			const char *dns1, const char *dns2,
			gpointer user_data)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct dundee_device *device = user_data;
	const char *dns[3] = { dns1, dns2, 0 };

	DBG("%p", device);
	DBG("Network Device: %s\n", iface);
	DBG("IP Address: %s\n", local);
	DBG("Peer IP Address: %s\n", peer);
	DBG("Primary DNS Server: %s\n", dns1);
	DBG("Secondary DNS Server: %s\n", dns2);

	g_free(device->settings.interface);
	device->settings.interface = g_strdup(iface);
	if (device->settings.interface == NULL)
		goto err;

	g_free(device->settings.ip);
	device->settings.ip = g_strdup(local);
	if (device->settings.ip == NULL)
		goto err;

	g_strfreev(device->settings.nameservers);
	device->settings.nameservers = g_strdupv((gchar **)dns);
	if (device->settings.nameservers == NULL)
		goto err;

	__ofono_dbus_pending_reply(&device->pending,
			dbus_message_new_method_return(device->pending));
	device->pending = NULL;

	device->active = TRUE;

	settings_changed(device);
	ofono_dbus_signal_property_changed(conn, device->path,
					DUNDEE_DEVICE_INTERFACE, "Active",
					DBUS_TYPE_BOOLEAN, &device->active);

	return;

err:
	g_free(device->settings.interface);
	g_free(device->settings.ip);
	g_strfreev(device->settings.nameservers);
	device->settings.interface = NULL;
	device->settings.ip = NULL;
	device->settings.nameservers = NULL;

	__ofono_dbus_pending_reply(&device->pending,
				__dundee_error_failed(device->pending));
	device->pending = NULL;
}

static void disconnect_callback(const struct dundee_error *error, void *data)
{
	struct dundee_device *device = data;

	DBG("%p", device);

	g_at_chat_unref(device->chat);
	device->chat = NULL;

	if (device->pending == NULL)
		return;

	if (error->type != DUNDEE_ERROR_TYPE_NO_ERROR) {
		__ofono_dbus_pending_reply(&device->pending,
					__dundee_error_failed(device->pending));
		goto out;
	}

	__ofono_dbus_pending_reply(&device->pending,
		dbus_message_new_method_return(device->pending));

out:
	device->pending = NULL;
}

static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct dundee_device *device = user_data;

	DBG("%p", device);
	DBG("PPP Link down: %d\n", reason);

	g_at_ppp_unref(device->ppp);
	device->ppp = NULL;

	g_at_chat_resume(device->chat);

	g_free(device->settings.interface);
	g_free(device->settings.ip);
	g_strfreev(device->settings.nameservers);
	device->settings.interface = NULL;
	device->settings.ip = NULL;
	device->settings.nameservers = NULL;

	device->active = FALSE;

	settings_changed(device);
	ofono_dbus_signal_property_changed(conn, device->path,
					DUNDEE_DEVICE_INTERFACE, "Active",
					DBUS_TYPE_BOOLEAN, &device->active);

	device->driver->disconnect(device, disconnect_callback, device);
}

static void dial_cb(gboolean ok, GAtResult *result, gpointer user_data)
{
	struct dundee_device *device = user_data;
	GAtIO *io;

	if (!ok) {
		DBG("Unable to define context\n");
		goto err;
	}

	/* get the data IO channel */
	io = g_at_chat_get_io(device->chat);

	/*
	 * shutdown gatchat or else it tries to take all the input
	 * from the modem and does not let PPP get it.
	 */
	g_at_chat_suspend(device->chat);

	/* open ppp */
	device->ppp = g_at_ppp_new();
	if (device->ppp == NULL) {
		DBG("Unable to create PPP object\n");
		goto err;
	}
	g_at_ppp_set_debug(device->ppp, debug, "PPP");

	/* set connect and disconnect callbacks */
	g_at_ppp_set_connect_function(device->ppp, ppp_connect, device);
	g_at_ppp_set_disconnect_function(device->ppp, ppp_disconnect, device);

	/* open the ppp connection */
	g_at_ppp_open(device->ppp, io);

	return;

err:
	__ofono_dbus_pending_reply(&device->pending,
				__dundee_error_failed(device->pending));
	device->pending = NULL;
}

static int device_dial_setup(struct dundee_device *device, int fd)
{
	GAtSyntax *syntax;
	GIOChannel *io;

	io = g_io_channel_unix_new(fd);
	if (io == NULL)
		return -EIO;

	syntax = g_at_syntax_new_gsm_permissive();
	device->chat = g_at_chat_new(io, syntax);
	g_io_channel_unref(io);
	g_at_syntax_unref(syntax);

	if (device->chat == NULL)
		return -EIO;

	g_at_chat_set_debug(device->chat, debug, "Control");

	g_at_chat_send(device->chat, "ATD*99#", none_prefix, dial_cb,
			device, NULL);

	return 0;
}

static void connect_callback(const struct dundee_error *error,
				int fd, void *data)
{
	struct dundee_device *device = data;
	int err;

	DBG("%p", device);

	if (error->type != DUNDEE_ERROR_TYPE_NO_ERROR)
		goto err;

	err = device_dial_setup(device, fd);
	if (err < 0)
		goto err;

	return;

err:
	__ofono_dbus_pending_reply(&device->pending,
				__dundee_error_failed(device->pending));
	device->pending = NULL;
}

static DBusMessage *set_property_active(struct dundee_device *device,
					DBusMessage *msg,
					DBusMessageIter *var)
{
	ofono_bool_t active;

	DBG("%p path %s", device, device->path);

	if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_BOOLEAN)
		return __dundee_error_invalid_args(msg);

	dbus_message_iter_get_basic(var, &active);

	device->pending = dbus_message_ref(msg);

	if (active)
		device->driver->connect(device, connect_callback, device);
	else if (device->ppp)
		g_at_ppp_shutdown(device->ppp);

	return NULL;
}

static DBusMessage *device_set_property(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	struct dundee_device *device = data;
	DBusMessageIter iter, var;
	const char *name;

	if (dbus_message_iter_init(msg, &iter) == FALSE)
		return __dundee_error_invalid_args(msg);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
		return __dundee_error_invalid_args(msg);

	dbus_message_iter_get_basic(&iter, &name);
	dbus_message_iter_next(&iter);

	if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
		return __dundee_error_invalid_args(msg);

	dbus_message_iter_recurse(&iter, &var);

	if (g_str_equal(name, "Active"))
		return set_property_active(device, msg, &var);

	return __dundee_error_invalid_args(msg);
}

static const GDBusMethodTable device_methods[] = {
	{ GDBUS_METHOD("GetProperties",
			NULL, GDBUS_ARGS({ "properties", "a{sv}" }),
			device_get_properties) },
	{ GDBUS_ASYNC_METHOD("SetProperty",
			GDBUS_ARGS({ "property", "s" }, { "value", "v" }),
			NULL, device_set_property) },
	{ }
};

static const GDBusSignalTable device_signals[] = {
	{ GDBUS_SIGNAL("PropertyChanged",
			GDBUS_ARGS({ "name", "s" }, { "value", "v" })) },
	{ }
};

static int register_device(struct dundee_device *device)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	DBusMessage *signal;
	DBusMessageIter iter;
	DBusMessageIter dict;

	DBG("%p path %s", device, device->path);

	if (!g_dbus_register_interface(conn, device->path,
					DUNDEE_DEVICE_INTERFACE,
					device_methods, device_signals,
					NULL, device, NULL)) {
		ofono_error("Could not register Device %s", device->path);
		return -EIO;
	}

	signal = dbus_message_new_signal(DUNDEE_MANAGER_PATH,
						DUNDEE_MANAGER_INTERFACE,
						"DeviceAdded");

	if (signal == NULL)
		return -ENOMEM;

	dbus_message_iter_init_append(signal, &iter);

	dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH,
					&device->path);
	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
					OFONO_PROPERTIES_ARRAY_SIGNATURE,
					&dict);
	__dundee_device_append_properties(device, &dict);
	dbus_message_iter_close_container(&iter, &dict);

	g_dbus_send_message(conn, signal);

	return 0;
}

static int unregister_device(struct dundee_device *device)
{
	DBusConnection *conn = ofono_dbus_get_connection();

	DBG("%p path %s", device, device->path);

	g_dbus_unregister_interface(conn, device->path,
					DUNDEE_DEVICE_INTERFACE);

	g_dbus_emit_signal(conn, DUNDEE_MANAGER_PATH,
				DUNDEE_MANAGER_INTERFACE, "DeviceRemoved",
				DBUS_TYPE_OBJECT_PATH, &device->path,
				DBUS_TYPE_INVALID);

	return 0;
}

static void destroy_device(gpointer user)
{
	struct dundee_device *device = user;

	if (device->chat != NULL)
		g_at_chat_unref(device->chat);

	if (device->ppp != NULL)
		g_at_ppp_unref(device->ppp);

	if (device->pending)
		dbus_message_unref(device->pending);

	g_free(device->settings.interface);
	g_free(device->settings.ip);
	g_strfreev(device->settings.nameservers);

	g_free(device->path);
	g_free(device->name);

	g_free(device);
}

struct dundee_device *dundee_device_create(struct dundee_device_driver *d)
{
	struct dundee_device *device;

	device = g_try_new0(struct dundee_device, 1);
	if (device == NULL)
		return NULL;

	device->driver = d;

	device->path = g_strdup_printf("/device%d", next_device_id);
	if (device->path == NULL) {
		g_free(device);
		return NULL;
	}

	next_device_id += 1;

	return device;
}

int dundee_device_register(struct dundee_device *device)
{
	int err;

	err = register_device(device);
	if (err < 0)
		return err;

	device->registered = TRUE;

	g_hash_table_insert(device_hash, g_strdup(device->path), device);

	return 0;
}

void dundee_device_unregister(struct dundee_device *device)
{
	DBG("%p", device);

	unregister_device(device);

	device->registered = FALSE;

	g_hash_table_remove(device_hash, device->path);
}

void dundee_device_set_data(struct dundee_device *device, void *data)
{
	device->data = data;
}

void *dundee_device_get_data(struct dundee_device *device)
{
	return device->data;
}

int dundee_device_set_name(struct dundee_device *device, const char *name)
{
	DBusConnection *conn = ofono_dbus_get_connection();

	DBG("%p name %s", device, name);

	g_free(device->name);
	device->name = g_strdup(name);

	if (device->registered == FALSE)
		return 0;

	ofono_dbus_signal_property_changed(conn, device->path,
					DUNDEE_DEVICE_INTERFACE, "Name",
					DBUS_TYPE_STRING, &device->name);

	return 0;
}

static void device_shutdown(gpointer key, gpointer value, gpointer user_data)
{
	struct dundee_device *device = value;

	unregister_device(device);
}

void __dundee_device_shutdown(void)
{
	g_hash_table_foreach(device_hash, device_shutdown, NULL);

	__dundee_exit();
}

int __dundee_device_init(void)
{
	DBG("");

	device_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
						g_free, destroy_device);

	return 0;
}

void __dundee_device_cleanup(void)
{
	DBG("");

	g_hash_table_destroy(device_hash);
}