summaryrefslogtreecommitdiffstats
path: root/src/modem.c
blob: 0b8b36ef9a077a7be561b170c0c2c674aa8ddad1 (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
/*
 *
 *  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 <dbus/dbus.h>
#include <glib.h>
#include <gdbus.h>

#include "ofono.h"

#include "dbus-gsm.h"
#include "modem.h"
#include "driver.h"

#define MODEM_INTERFACE "org.ofono.Modem"

#define MODEM_FLAG_INITIALIZING_ATTRS 1

#define ATTRIBUTE_QUERY_DELAY 0

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;
};

unsigned int 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 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_userdata(struct ofono_modem *modem)
{
	if (modem)
		return modem->userdata;

	return 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,
						PROPERTIES_ARRAY_SIGNATURE,
						&dict);

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

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

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

	if (info->serial)
		dbus_gsm_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;

	dbus_gsm_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 = dbus_gsm_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;

	dbus_gsm_signal_array_property_changed(conn, modem->path,
						MODEM_INTERFACE,
						"Interfaces", DBUS_TYPE_STRING,
						&interfaces);

	g_free(interfaces);

	info->interface_update = 0;

	return FALSE;
}

void 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 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)
{
	//struct ofono_modem_data *info = modem->modem_info;
	DBusConnection *conn = dbus_gsm_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 gboolean query_serial(gpointer user)
{
	struct ofono_modem *modem = user;

	if (!modem->modem_info->ops->query_serial) {
		finish_attr_query(modem);
		return FALSE;
	}

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

	return FALSE;
}

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);

	g_timeout_add(0, query_serial, modem);
}

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

	if (!modem->modem_info->ops->query_revision) {
		g_timeout_add(0, query_serial, modem);
		return FALSE;
	}

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

	return FALSE;
}

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);

	g_timeout_add(0, query_revision, modem);
}

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

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

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

	return FALSE;
}

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);

	g_timeout_add(0, query_model, modem);
}

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

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

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

	return FALSE;
}

struct ofono_modem *modem_create(int id, struct ofono_modem_attribute_ops *ops)
{
	char path[MAX_DBUS_PATH_LEN];
	DBusConnection *conn = dbus_gsm_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, MAX_DBUS_PATH_LEN, "/modem%d", modem->id);
	modem->path = g_strdup(path);

	if (!g_dbus_register_interface(conn, path, 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;
	}

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

	return modem;
}

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

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

	g_dbus_unregister_interface(conn, path, MODEM_INTERFACE);

	g_free(path);
}