summaryrefslogtreecommitdiffstats
path: root/plugins/generic_at.c
blob: f500e9caf36bd545817886d13fb678e5dbda83c5 (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
/*
 *
 *  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 <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

#include <glib.h>
#include <gatchat.h>
#include <gatsyntax.h>

#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/plugin.h>
#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/call-barring.h>
#include <ofono/call-forwarding.h>
#include <ofono/call-meter.h>
#include <ofono/call-settings.h>
#include <ofono/devinfo.h>
#include <ofono/message-waiting.h>
#include <ofono/netreg.h>
#include <ofono/phonebook.h>
#include <ofono/sim.h>
#include <ofono/sms.h>
#include <ofono/ssn.h>
#include <ofono/ussd.h>
#include <ofono/voicecall.h>

static GSList *g_modems = NULL;

enum transport_type {
	TRANSPORT_TYPE_TCP = 0,
	TRANSPORT_TYPE_UNIX = 1,
	TRANSPORT_TYPE_TTY = 2,
};

struct generic_at_data {
	enum transport_type type;
	union {
		struct sockaddr_in tcp;
		struct sockaddr_un un;
		char *tty;
	};

	char *timeout_command;
	int timeout_interval;
	char *init_string;
	GAtChat *chat;
	GIOChannel *io;
	unsigned int timeout_watcher;
};

static void generic_at_data_free(struct generic_at_data *d)
{
	if (d->type == TRANSPORT_TYPE_TTY)
		g_free(d->tty);

	g_free(d->timeout_command);
	g_free(d->init_string);

	if (d->chat)
		g_at_chat_unref(d->chat);

	g_free(d);
}

static struct generic_at_data *parse_modem(GKeyFile *keyfile, const char *group)
{
	struct generic_at_data *d;
	char *type;

	d = g_new0(struct generic_at_data, 1);

	type = g_key_file_get_string(keyfile, group, "Type", NULL);

	if (type == NULL)
		goto error;

	if (!strcmp(type, "tcp"))
		d->type = TRANSPORT_TYPE_TCP;
	else if (!strcmp(type, "unix"))
		d->type = TRANSPORT_TYPE_UNIX;
	else if (!strcmp(type, "tty"))
		d->type = TRANSPORT_TYPE_TTY;
	else
		goto error;

	g_free(type);

	switch (d->type) {
	case TRANSPORT_TYPE_TCP:
	{
		in_addr_t inetaddr;
		char *address;
		int port;

		address = g_key_file_get_string(keyfile, group, "Address", NULL);

		if (address == NULL)
			goto error;

		inetaddr = inet_addr(address);

		g_free(address);

		if (inetaddr == INADDR_NONE)
			goto error;

		port = g_key_file_get_integer(keyfile, group, "Port", NULL);

		if (port <= 0 || port > 0xffff)
			goto error;

		d->tcp.sin_family = AF_INET;
		d->tcp.sin_addr.s_addr = inetaddr;
		d->tcp.sin_port = htons(port);

		break;
	}

	case TRANSPORT_TYPE_UNIX:
	{
		char *path;

		path = g_key_file_get_string(keyfile, group, "Address", NULL);

		if (strlen(path) >= 108) {
			g_free(path);
			goto error;
		}

		d->un.sun_family = PF_UNIX;

		if (strncmp("x00", path, 3) == 0)
			strcpy(d->un.sun_path + 1, path + 3);
		else
			strcpy(d->un.sun_path, path);

		g_free(path);

		break;
	}

	case TRANSPORT_TYPE_TTY:
	{
		char *node;

		node = g_key_file_get_string(keyfile, group, "Device", NULL);

		d->tty = node;

		break;
	}

	default:
		break;
	}

	d->init_string = g_key_file_get_string(keyfile, group,
						"InitString", NULL);

	d->timeout_interval = g_key_file_get_integer(keyfile, group,
						"TimeoutInterval", NULL);

	if (d->timeout_interval < 0 || d->timeout_interval > 3600)
		goto error;

	d->timeout_command = g_key_file_get_string(keyfile, group,
							"TimeoutCommand", NULL);

	return d;

error:
	generic_at_data_free(d);
	return NULL;
}

static void parse_config(const char *file)
{
	GError *err = NULL;
	GKeyFile *keyfile;
	char **modems;
	int i;

	keyfile = g_key_file_new();

	g_key_file_set_list_separator(keyfile, ',');

	if (!g_key_file_load_from_file(keyfile, file, 0, &err)) {
		ofono_error("Parsing %s failed: %s", file, err->message);
		g_error_free(err);
		g_key_file_free(keyfile);
		return;
	}

	modems = g_key_file_get_groups(keyfile, NULL);

	for (i = 0; modems[i]; i++) {
		struct generic_at_data *parsed;
		struct ofono_modem *modem;

		DBG("modem: %s", modems[i]);

		parsed = parse_modem(keyfile, modems[i]);

		DBG("parsed: %p", parsed);

		if (parsed == NULL)
			continue;

		modem = ofono_modem_create(modems[i], "generic_at");

		if (modem == NULL)
			continue;

		ofono_modem_set_data(modem, parsed);
		ofono_modem_register(modem);
	}

	g_strfreev(modems);
	g_key_file_free(keyfile);
}

static void connect_destroy(gpointer user)
{
	struct ofono_modem *modem = user;
	struct generic_at_data *d = ofono_modem_get_data(modem);

	if (d->timeout_watcher != 0) {
		g_source_remove(d->timeout_watcher);
		d->timeout_watcher = 0;
	}

	d->io = NULL;
}

static void at_debug(const char *str, void *data)
{
	DBG("%s", str);
}

static gboolean connect_cb(GIOChannel *io, GIOCondition cond, gpointer user)
{
	struct ofono_modem *modem = user;
	struct generic_at_data *d = ofono_modem_get_data(modem);
	int err = 0;
	gboolean success;
	GAtSyntax *syntax;

	if (cond & G_IO_NVAL)
		return FALSE;

	if (cond & G_IO_OUT) {
		int sock = g_io_channel_unix_get_fd(io);
		socklen_t len = sizeof(err);

		if (getsockopt(sock, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
			err = errno == ENOTSOCK ? 0 : errno;
	} else if (cond & (G_IO_HUP | G_IO_ERR))
		err = ECONNRESET;

	success = !err;

	DBG("io ref: %d", io->ref_count);

	if (success == FALSE)
		goto error;

	syntax = g_at_syntax_new_gsmv1();
	d->chat = g_at_chat_new(io, syntax);
	g_at_syntax_unref(syntax);

	DBG("io ref: %d", io->ref_count);

	if (!d->chat)
		goto error;

	if (getenv("OFONO_AT_DEBUG") != NULL)
		g_at_chat_set_debug(d->chat, at_debug, NULL);

	if (d->timeout_command && d->timeout_interval > 0)
		g_at_chat_set_wakeup_command(d->chat, d->timeout_command, 1000,
					d->timeout_interval * 1000);

	DBG("%s", d->init_string);

	if (d->init_string)
		g_at_chat_send(d->chat, d->init_string, NULL, NULL, NULL, NULL);

	ofono_modem_set_powered(modem, TRUE);

	return FALSE;

error:
	ofono_modem_set_powered(modem, FALSE);
	return FALSE;
}

static gboolean connect_timeout(gpointer user)
{
	struct ofono_modem *modem = user;
	struct generic_at_data *d = ofono_modem_get_data(modem);

	d->timeout_watcher = 0;
	g_io_channel_close(d->io);
	ofono_modem_set_powered(modem, FALSE);

	return FALSE;
}

static GIOChannel *tty_connect(const char *tty)
{
	GIOChannel *io;
	int sk;
	struct termios newtio;

	sk = open(tty, O_RDWR | O_NOCTTY);

	if (sk < 0) {
		ofono_error("Can't open TTY %s: %s(%d)",
				tty, strerror(errno), errno);
		return NULL;
	}

	newtio.c_cflag = B115200 | CRTSCTS | CLOCAL | CREAD;
	newtio.c_iflag = IGNPAR;
	newtio.c_oflag = 0;
	newtio.c_lflag = 0;

	newtio.c_cc[VTIME] = 1;
	newtio.c_cc[VMIN] = 5;

	tcflush(sk, TCIFLUSH);
	if (tcsetattr(sk, TCSANOW, &newtio) < 0) {
		ofono_error("Can't change serial settings: %s(%d)",
				strerror(errno), errno);
		close(sk);
		return NULL;
	}

	io = g_io_channel_unix_new(sk);
	g_io_channel_set_close_on_unref(io, TRUE);

	if (g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK,
					NULL) != G_IO_STATUS_NORMAL) {
		g_io_channel_unref(io);
		return NULL;
	}

	return io;
}

static GIOChannel *socket_common(int sk, struct sockaddr *addr,
					socklen_t addrlen)
{
	GIOChannel *io = g_io_channel_unix_new(sk);

	if (io == NULL) {
		close(sk);
		return NULL;
	}

	g_io_channel_set_close_on_unref(io, TRUE);

	if (g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK,
					NULL) != G_IO_STATUS_NORMAL) {
		g_io_channel_unref(io);
		return NULL;
	}

	if (connect(sk, addr, addrlen) < 0) {
		if (errno != EAGAIN && errno != EINPROGRESS) {
			g_io_channel_unref(io);
			return NULL;
		}
	}

	return io;
}

static int generic_at_probe(struct ofono_modem *modem)
{
	return 0;
}

static int generic_at_remove(struct ofono_modem *modem)
{
	return 0;
}

static int generic_at_enable(struct ofono_modem *modem)
{
	struct generic_at_data *d = ofono_modem_get_data(modem);
	int sk;
	GIOChannel *io;
	GIOCondition cond;

	DBG("");

	switch (d->type) {
	case TRANSPORT_TYPE_TCP:
		sk = socket(PF_INET, SOCK_STREAM, 0);

		if (sk < 0)
			return -EAFNOSUPPORT;

		io = socket_common(sk, (struct sockaddr *)&d->tcp,
					sizeof(d->tcp));
		break;

	case TRANSPORT_TYPE_UNIX:
		sk = socket(AF_UNIX, SOCK_STREAM, 0);

		if (sk < 0)
			return -EAFNOSUPPORT;

		io = socket_common(sk, (struct sockaddr *)&d->tcp,
					sizeof(d->tcp));

		break;

	case TRANSPORT_TYPE_TTY:
		io = tty_connect(d->tty);

		break;

	default:
		io = NULL;
	}

	if (io == NULL)
		return -EINVAL;

	DBG("io ref: %d", io->ref_count);

	d->timeout_watcher = g_timeout_add_seconds(10, connect_timeout, modem);

	cond = G_IO_OUT | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
	g_io_add_watch_full(io, G_PRIORITY_DEFAULT, cond, connect_cb,
				modem, connect_destroy);

	DBG("io ref: %d", io->ref_count);

	g_io_channel_unref(io);

	DBG("io ref: %d", io->ref_count);
	d->io = io;

	return -EINPROGRESS;
}

static int generic_at_disable(struct ofono_modem *modem)
{
	struct generic_at_data *d = ofono_modem_get_data(modem);

	if (d->io) {
		g_io_channel_close(d->io);
		d->io = NULL;
	}

	if (d->chat) {
		g_at_chat_unref(d->chat);
		d->chat = NULL;
	}

	return 0;
}

static int generic_at_populate(struct ofono_modem *modem)
{
	struct generic_at_data *d = ofono_modem_get_data(modem);
	GAtChat *chat = d->chat;
	struct ofono_message_waiting *mw;

	ofono_devinfo_create(modem, "generic_at", chat);
	ofono_ussd_create(modem, "generic_at", chat);
	ofono_sim_create(modem, "generic_at", chat);
	ofono_call_forwarding_create(modem, "generic_at", chat);
	ofono_call_settings_create(modem, "generic_at", chat);
	ofono_netreg_create(modem, "generic_at", chat);
	ofono_voicecall_create(modem, "generic_at", chat);
	ofono_call_meter_create(modem, "generic_at", chat);
	ofono_call_barring_create(modem, "generic_at", chat);
	ofono_ssn_create(modem, "generic_at", chat);
	ofono_sms_create(modem, "generic_at", chat);
	ofono_phonebook_create(modem, "generic_at", chat);

	mw = ofono_message_waiting_create(modem);
	if (mw)
		ofono_message_waiting_register(mw);

	return 0;
}

static struct ofono_modem_driver driver = {
	.name = "generic_at",
	.probe = generic_at_probe,
	.remove = generic_at_remove,
	.enable = generic_at_enable,
	.disable = generic_at_disable,
	.populate = generic_at_populate,
};

static int generic_at_init(void)
{
	int err = -EIO;

	err = ofono_modem_driver_register(&driver);

	if (err < 0)
		return err;

	parse_config(CONFIGDIR "/generic_at.conf");

	return 0;
}

static void generic_at_exit(void)
{
	GSList *l;
	struct ofono_modem *modem;

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

		ofono_modem_remove(modem);
	}

	g_slist_free(g_modems);

	ofono_modem_driver_unregister(&driver);
}

OFONO_PLUGIN_DEFINE(generic_at, "Generic AT Modem driver", VERSION,
			OFONO_PLUGIN_PRIORITY_DEFAULT,
			generic_at_init, generic_at_exit)