summaryrefslogtreecommitdiffstats
path: root/src/modem.c
blob: 209033f2fb1e413d7dd463d2bc7876ae4b76a6fd (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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2009  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 <stdio.h>

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

#include "ofono.h"

#include "driver.h"
#include "common.h"
#include "sim.h"

#define MODEM_FLAG_INITIALIZING_ATTRS 1

static GSList *g_modem_list = NULL;
static int g_next_modem_id = 1;

struct ofono_modem_data {
	char      			*manufacturer;
	char      			*model;
	char      			*revision;
	char      			*serial;
	GSList          		*interface_list;
	int				flags;
	unsigned int			idlist;
	struct ofono_modem_attribute_ops	*ops;
	DBusMessage			*pending;
	guint				interface_update;
};

struct ofono_atom {
	enum ofono_atom_type type;
	void (*destruct)(struct ofono_atom *atom);
	void (*unregister)(struct ofono_atom *atom);
	void *data;
	struct ofono_modem *modem;
};

struct ofono_atom_watch {
	enum ofono_atom_type type;
	int id;
	ofono_atom_watch_func notify;
	ofono_destroy_func destroy;
	void *notify_data;
};

unsigned int __ofono_modem_alloc_callid(struct ofono_modem *modem)
{
	struct ofono_modem_data *d = modem->modem_info;
	unsigned int i;

	for (i = 1; i < sizeof(d->idlist) * 8; i++) {
		if (d->idlist & (0x1 << i))
			continue;

		d->idlist |= (0x1 << i);
		return i;
	}

	return 0;
}

void __ofono_modem_release_callid(struct ofono_modem *modem, int id)
{
	struct ofono_modem_data *d = modem->modem_info;

	d->idlist &= ~(0x1 << id);
}

void ofono_modem_set_userdata(struct ofono_modem *modem, void *userdata)
{
	if (modem)
		modem->userdata = userdata;
}

void *ofono_modem_get_userdata(struct ofono_modem *modem)
{
	if (modem)
		return modem->userdata;

	return NULL;
}

const char *ofono_modem_get_path(struct ofono_modem *modem)
{
	if (modem)
		return modem->path;

	return NULL;
}

struct ofono_atom *__ofono_modem_add_atom(struct ofono_modem *modem,
					enum ofono_atom_type type,
					void (*destruct)(struct ofono_atom *),
					void *data)
{
	struct ofono_atom *atom;

	if (modem == NULL)
		return NULL;

	atom = g_new0(struct ofono_atom, 1);

	atom->type = type;
	atom->destruct = destruct;
	atom->data = data;
	atom->modem = modem;

	modem->atoms = g_slist_prepend(modem->atoms, atom);

	return atom;
}

void *__ofono_atom_get_data(struct ofono_atom *atom)
{
	return atom->data;
}

const char *__ofono_atom_get_path(struct ofono_atom *atom)
{
	return atom->modem->path;
}

struct ofono_modem *__ofono_atom_get_modem(struct ofono_atom *atom)
{
	return atom->modem;
}

static void call_watches(struct ofono_atom *atom,
				enum ofono_atom_watch_condition cond)
{
	struct ofono_modem *modem = atom->modem;
	GSList *l;
	struct ofono_atom_watch *watch;

	for (l = modem->atom_watches; l; l = l->next) {
		watch = l->data;

		if (watch->type != atom->type)
			continue;

		watch->notify(atom, cond, watch->notify_data);
	}
}

void __ofono_atom_register(struct ofono_atom *atom,
			void (*unregister)(struct ofono_atom *))
{
	if (unregister == NULL)
		return;

	atom->unregister = unregister;

	call_watches(atom, OFONO_ATOM_WATCH_CONDITION_REGISTERED);
}

void __ofono_atom_unregister(struct ofono_atom *atom)
{
	if (atom->unregister == NULL)
		return;

	call_watches(atom, OFONO_ATOM_WATCH_CONDITION_UNREGISTERED);

	atom->unregister(atom);
}

gboolean __ofono_atom_get_registered(struct ofono_atom *atom)
{
	return atom->unregister ? TRUE : FALSE;
}

int __ofono_modem_add_atom_watch(struct ofono_modem *modem,
					enum ofono_atom_type type,
					ofono_atom_watch_func notify,
					void *data, ofono_destroy_func destroy)
{
	struct ofono_atom_watch *watch;
	
	if (notify == NULL)
		return 0;
		
	watch = g_new0(struct ofono_atom_watch, 1);

	watch->type = type;
	watch->id = ++modem->next_atom_watch_id;
	watch->notify = notify;
	watch->destroy = destroy;
	watch->notify_data = data;

	modem->atom_watches = g_slist_prepend(modem->atom_watches, watch);

	return watch->id;
}

gboolean __ofono_modem_remove_atom_watch(struct ofono_modem *modem, int id)
{
	struct ofono_atom_watch *watch;
	GSList *p;
	GSList *c;

	p = NULL;
	c = modem->atom_watches;

	while (c) {
		watch = c->data;

		if (watch->id != id) {
			p = c;
			c = c->next;
			continue;
		}

		if (p)
			p->next = c->next;
		else
			modem->atom_watches = c->next;

		if (watch->destroy)
			watch->destroy(watch->notify_data);

		g_free(watch);
		g_slist_free_1(c);

		return TRUE;
	}

	return FALSE;
}

static void remove_all_watches(struct ofono_modem *modem)
{
	struct ofono_atom_watch *watch;
	GSList *l;

	for (l = modem->atom_watches; l; l = l->next) {
		watch = l->data;

		if (watch->destroy)
			watch->destroy(watch->notify_data);

		g_free(watch);
	}

	g_slist_free(modem->atom_watches);
	modem->atom_watches = NULL;
}

struct ofono_atom *__ofono_modem_find_atom(struct ofono_modem *modem,
						enum ofono_atom_type type)
{
	GSList *l;
	struct ofono_atom *atom;

	if (modem == NULL)
		return NULL;

	for (l = modem->atoms; l; l = l->next) {
		atom = l->data;

		if (atom->type == type)
			return atom;
	}

	return NULL;
}

void __ofono_atom_free(struct ofono_atom *atom)
{
	struct ofono_modem *modem = atom->modem;

	modem->atoms = g_slist_remove(modem->atoms, atom);

	__ofono_atom_unregister(atom);

	if (atom->destruct)
		atom->destruct(atom);

	g_free(atom);
}

static void remove_all_atoms(struct ofono_modem *modem)
{
	GSList *l;
	struct ofono_atom *atom;

	if (modem == NULL)
		return;

	for (l = modem->atoms; l; l = l->next) {
		atom = l->data;

		__ofono_atom_unregister(atom);

		if (atom->destruct)
			atom->destruct(atom);

		g_free(atom);
	}

	g_slist_free(modem->atoms);
	modem->atoms = NULL;
}

static void modem_free(gpointer data)
{
	struct ofono_modem *modem = data;
	GSList *l;

	if (modem == NULL)
		return;

	for (l = modem->modem_info->interface_list; l; l = l->next)
		g_free(l->data);

	g_slist_free(modem->modem_info->interface_list);

	g_free(modem->modem_info->manufacturer);
	g_free(modem->modem_info->serial);
	g_free(modem->modem_info->revision);
	g_free(modem->modem_info->model);

	if (modem->modem_info->pending)
		dbus_message_unref(modem->modem_info->pending);

	if (modem->modem_info->interface_update)
		g_source_remove(modem->modem_info->interface_update);

	g_free(modem->modem_info);

	g_free(modem->path);
	g_free(modem);
}

static DBusMessage *generate_properties_reply(struct ofono_modem *modem,
				DBusConnection *conn, DBusMessage *msg)
{
	struct ofono_modem_data *info = modem->modem_info;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter dict;

	char **interfaces;
	int i;
	GSList *l;

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

	dbus_message_iter_init_append(reply, &iter);

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

	if (info->manufacturer)
		ofono_dbus_dict_append(&dict, "Manufacturer", DBUS_TYPE_STRING,
					&info->manufacturer);

	if (info->model)
		ofono_dbus_dict_append(&dict, "Model", DBUS_TYPE_STRING,
					&info->model);

	if (info->revision)
		ofono_dbus_dict_append(&dict, "Revision", DBUS_TYPE_STRING,
					&info->revision);

	if (info->serial)
		ofono_dbus_dict_append(&dict, "Serial", DBUS_TYPE_STRING,
					&info->serial);

	interfaces = g_new0(char *, g_slist_length(info->interface_list) + 1);
	for (i = 0, l = info->interface_list; l; l = l->next, i++)
		interfaces[i] = l->data;

	ofono_dbus_dict_append_array(&dict, "Interfaces", DBUS_TYPE_STRING,
					&interfaces);

	g_free(interfaces);

	dbus_message_iter_close_container(&iter, &dict);

	return reply;
}

static DBusMessage *modem_get_properties(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	struct ofono_modem *modem = data;

	if (modem->modem_info->flags & MODEM_FLAG_INITIALIZING_ATTRS) {
		modem->modem_info->pending = dbus_message_ref(msg);
		return NULL;
	}

	return generate_properties_reply(modem, conn, msg);
}

static GDBusMethodTable modem_methods[] = {
	{ "GetProperties",	"",	"a{sv}",	modem_get_properties,
							G_DBUS_METHOD_FLAG_ASYNC },
	{ }
};

static GDBusSignalTable modem_signals[] = {
	{ "PropertyChanged",	"sv" },
	{ }
};

static gboolean trigger_interface_update(void *data)
{
	struct ofono_modem *modem = data;
	struct ofono_modem_data *info = modem->modem_info;
	DBusConnection *conn = ofono_dbus_get_connection();

	char **interfaces;
	GSList *l;
	int i;

	interfaces = g_new0(char *, g_slist_length(info->interface_list) + 1);
	for (i = 0, l = info->interface_list; l; l = l->next, i++)
		interfaces[i] = l->data;

	ofono_dbus_signal_array_property_changed(conn, modem->path,
						OFONO_MODEM_INTERFACE,
						"Interfaces", DBUS_TYPE_STRING,
						&interfaces);

	g_free(interfaces);

	info->interface_update = 0;

	return FALSE;
}

void ofono_modem_add_interface(struct ofono_modem *modem,
				const char *interface)
{
	struct ofono_modem_data *info = modem->modem_info;

	info->interface_list =
		g_slist_prepend(info->interface_list, g_strdup(interface));

	if (info->interface_update == 0)
		info->interface_update =
			g_timeout_add(0, trigger_interface_update, modem);
}

void ofono_modem_remove_interface(struct ofono_modem *modem,
				const char *interface)
{
	struct ofono_modem_data *info = modem->modem_info;

	GSList *found = g_slist_find_custom(info->interface_list,
						interface,
						(GCompareFunc) strcmp);

	if (!found) {
		ofono_error("Interface %s not found on the interface_list",
				interface);
		return;
	}

	g_free(found->data);

	info->interface_list =
		g_slist_remove(info->interface_list, found->data);

	if (info->interface_update == 0)
		info->interface_update =
			g_timeout_add(0, trigger_interface_update, modem);
}

static void finish_attr_query(struct ofono_modem *modem)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	DBusMessage *reply;

	modem->modem_info->flags &= ~MODEM_FLAG_INITIALIZING_ATTRS;

	if (!modem->modem_info->pending)
		return;

	reply = generate_properties_reply(modem, conn,
						modem->modem_info->pending);

	if (reply)
		g_dbus_send_message(conn, reply);

	dbus_message_unref(modem->modem_info->pending);
	modem->modem_info->pending = NULL;
}

static void query_serial_cb(const struct ofono_error *error,
				const char *serial, void *user)
{
	struct ofono_modem *modem = user;

	if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
		modem->modem_info->serial = g_strdup(serial);

	finish_attr_query(modem);
}

static void query_serial(struct ofono_modem *modem)
{
	if (!modem->modem_info->ops->query_serial) {
		finish_attr_query(modem);
		return;
	}

	modem->modem_info->ops->query_serial(modem, query_serial_cb, modem);
}

static void query_revision_cb(const struct ofono_error *error,
				const char *revision, void *user)
{
	struct ofono_modem *modem = user;

	if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
		modem->modem_info->revision = g_strdup(revision);

	query_serial(modem);
}

static void query_revision(struct ofono_modem *modem)
{
	if (!modem->modem_info->ops->query_revision) {
		query_serial(modem);
		return;
	}

	modem->modem_info->ops->query_revision(modem, query_revision_cb, modem);
}

static void query_model_cb(const struct ofono_error *error,
				const char *model, void *user)
{
	struct ofono_modem *modem = user;

	if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
		modem->modem_info->model = g_strdup(model);

	query_revision(modem);
}

static void query_model(struct ofono_modem *modem)
{
	if (!modem->modem_info->ops->query_model) {
		/* If model is not supported, don't bother querying revision */
		query_serial(modem);
		return;
	}

	modem->modem_info->ops->query_model(modem, query_model_cb, modem);
}

static void query_manufacturer_cb(const struct ofono_error *error,
					const char *manufacturer, void *user)
{
	struct ofono_modem *modem = user;

	if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
		modem->modem_info->manufacturer = g_strdup(manufacturer);

	query_model(modem);
}

static gboolean query_manufacturer(gpointer user)
{
	struct ofono_modem *modem = user;

	if (!modem->modem_info->ops->query_manufacturer) {
		query_model(modem);
		return FALSE;
	}

	modem->modem_info->ops->query_manufacturer(modem, query_manufacturer_cb,
							modem);

	return FALSE;
}

static struct ofono_modem *modem_create(int id,
					struct ofono_modem_attribute_ops *ops)
{
	char path[128];
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_modem *modem;

	modem = g_try_new0(struct ofono_modem, 1);
	if (modem == NULL)
		return modem;

	modem->modem_info = g_try_new0(struct ofono_modem_data, 1);
	if (modem->modem_info == NULL) {
		g_free(modem);
		return NULL;
	}

	modem->id = id;
	modem->modem_info->ops = ops;

	snprintf(path, sizeof(path), "/modem%d", modem->id);
	modem->path = g_strdup(path);

	if (!g_dbus_register_interface(conn, path, OFONO_MODEM_INTERFACE,
			modem_methods, modem_signals, NULL,
			modem, modem_free)) {
		ofono_error("Modem interface init failed on path %s", path);
		modem_free(modem);
		return NULL;
	}

	ofono_sim_manager_init(modem);

	modem->modem_info->flags |= MODEM_FLAG_INITIALIZING_ATTRS;
	g_timeout_add(0, query_manufacturer, modem);

	return modem;
}

static void modem_remove(struct ofono_modem *modem)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	/* Need to make a copy to keep gdbus happy */
	char *path = g_strdup(modem->path);

	ofono_debug("Removing modem: %s", modem->path);

	remove_all_atoms(modem);
	remove_all_watches(modem);

	ofono_sim_manager_exit(modem);

	g_dbus_unregister_interface(conn, path, OFONO_MODEM_INTERFACE);

	g_free(path);
}

/* Clients only need to free *modems */
const char **__ofono_modem_get_list()
{
	GSList *l;
	int i;
	struct ofono_modem *modem;
	const char **modems;

	modems = g_new0(const char *, g_slist_length(g_modem_list) + 1);

	for (l = g_modem_list, i = 0; l; l = l->next, i++) {
		modem = l->data;

		modems[i] = modem->path;
	}

	return modems;
}

struct ofono_modem *ofono_modem_register(struct ofono_modem_attribute_ops *ops)
{
	struct ofono_modem *modem;
	DBusConnection *conn = ofono_dbus_get_connection();
	const char **modems;

	modem = modem_create(g_next_modem_id, ops);

	if (modem == NULL)
		return 0;

	++g_next_modem_id;

	__ofono_history_probe_drivers(modem);
	g_modem_list = g_slist_prepend(g_modem_list, modem);

	modems = __ofono_modem_get_list();

	if (modems) {
		ofono_dbus_signal_array_property_changed(conn,
				OFONO_MANAGER_PATH,
				OFONO_MANAGER_INTERFACE, "Modems",
				DBUS_TYPE_OBJECT_PATH, &modems);

		g_free(modems);
	}

	return modem;
}

int ofono_modem_unregister(struct ofono_modem *m)
{
	struct ofono_modem *modem = m;
	DBusConnection *conn = ofono_dbus_get_connection();
	const char **modems;

	if (modem == NULL)
		return -1;

	__ofono_history_remove_drivers(modem);
	modem_remove(modem);

	g_modem_list = g_slist_remove(g_modem_list, modem);

	modems = __ofono_modem_get_list();

	if (modems) {
		ofono_dbus_signal_array_property_changed(conn,
				OFONO_MANAGER_PATH,
				OFONO_MANAGER_INTERFACE, "Modems",
				DBUS_TYPE_OBJECT_PATH, &modems);

		g_free(modems);
	}

	return 0;
}