summaryrefslogtreecommitdiff
path: root/plugins/modem.c
blob: 4c8a3e3d89bea589338fdf565f934d7290036875 (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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-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 <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#include <termios.h>

#include <glib.h>

#include <connman/log.h>

#include "modem.h"

struct modem_data {
	char *device;
	GIOChannel *channel;
	guint watch;
	GSList *callbacks;
	GSList *commands;
	char buf[1024];
	int offset;
};

struct modem_callback {
	char *command;
	modem_cb_t function;
	void *user_data;
};

struct modem_cmd {
	char *cmd;
	char *arg;
	modem_cb_t callback;
	void *user_data;
};

static int send_command(struct modem_data *modem, struct modem_cmd *cmd)
{
	char *buf;
	int fd, err;

	if (cmd->arg == NULL) {
		DBG("AT%s", cmd->cmd);
		buf = g_strdup_printf("AT%s\r\n", cmd->cmd);
	} else {
		DBG("AT%s=%s", cmd->cmd, cmd->arg);
		buf = g_strdup_printf("AT%s=%s\r\n", cmd->cmd, cmd->arg);
	}

	fd = g_io_channel_unix_get_fd(modem->channel);
	err = write(fd, buf, strlen(buf));

	fsync(fd);

	g_free(buf);

	return err;
}

static int queue_command(struct modem_data *modem, struct modem_cmd *cmd)
{
	modem->commands = g_slist_append(modem->commands, cmd);

	if (g_slist_length(modem->commands) > 1)
		return 0;

	return send_command(modem, cmd);
}

struct modem_data *modem_create(const char *device)
{
	struct modem_data *modem;

	DBG("device %s", device);

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

	modem->device = g_strdup(device);

	DBG("modem %p", modem);

	return modem;
}

void modem_destroy(struct modem_data *modem)
{
	DBG("modem %p", modem);

	if (modem == NULL)
		return;

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

static gboolean modem_event(GIOChannel *channel,
				GIOCondition condition, gpointer user_data)
{
	struct modem_data *modem = user_data;
	struct modem_cmd *cmd;
	GSList *list;
	gsize len;
	GIOError err;

	if (condition & (G_IO_NVAL | G_IO_HUP | G_IO_ERR))
		return FALSE;

	err = g_io_channel_read(channel, modem->buf + modem->offset,
				sizeof(modem->buf) - modem->offset, &len);
	if (err) {
		if (err == G_IO_ERROR_AGAIN)
			return TRUE;
		return FALSE;
	}

	DBG("Read %zu bytes (offset %d)", len, modem->offset);

	if (g_str_has_suffix(modem->buf, "\r\n") == TRUE) {
		for (list = modem->callbacks; list; list = list->next) {
			struct modem_callback *callback = list->data;

			if (callback->function == NULL)
				continue;

			if (g_strrstr(modem->buf, callback->command) != NULL)
				callback->function(modem->buf,
							callback->user_data);
		}
	}

	if (g_strrstr(modem->buf, "\r\nERROR\r\n") == NULL &&
				g_strrstr(modem->buf, "\r\nOK\r\n") == NULL) {
		modem->offset += len;
		return TRUE;
	}

	memset(modem->buf, 0, sizeof(modem->buf));
	modem->offset = 0;

	cmd = g_slist_nth_data(modem->commands, 0);
	if (cmd == NULL)
		return TRUE;

	modem->commands = g_slist_remove(modem->commands, cmd);

	DBG("AT%s", cmd->cmd);

	if (cmd->callback)
		cmd->callback(modem->buf, cmd->user_data);

	g_free(cmd->arg);
	g_free(cmd->cmd);
	g_free(cmd);

	cmd = g_slist_nth_data(modem->commands, 0);
	if (cmd == NULL)
		return TRUE;

	send_command(modem, cmd);

	return TRUE;
}

static int open_device(const char *device)
{
	struct termios ti;
	int fd;

	fd = open(device, O_RDWR | O_NOCTTY);
	if (fd < 0)
		return -1;

	tcflush(fd, TCIOFLUSH);

	/* Switch TTY to raw mode */
	memset(&ti, 0, sizeof(ti));
	cfmakeraw(&ti);

	tcsetattr(fd, TCSANOW, &ti);

	return fd;
}

int modem_open(struct modem_data *modem)
{
	int fd, try = 5;

	DBG("modem %p", modem);

	if (modem == NULL)
		return -ENOENT;

	while (try-- > 0) {
		fd = open_device(modem->device);
		if (fd < 0) {
			sleep(1);
			continue;
		}
		try = 0;
	}

	if (fd < 0) {
		connman_error("Can't open %s device", modem->device);
		return -EIO;
	}

	modem->channel = g_io_channel_unix_new(fd);
	g_io_channel_set_close_on_unref(modem->channel, TRUE);

	modem->watch = g_io_add_watch(modem->channel,
				G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
							modem_event, modem);

	return 0;
}

int modem_close(struct modem_data *modem)
{
	DBG("modem %p", modem);

	if (modem == NULL)
		return -ENOENT;

	g_source_remove(modem->watch);
	modem->watch = 0;

	g_io_channel_unref(modem->channel);
	modem->channel = NULL;

	return 0;
}

int modem_add_callback(struct modem_data *modem, const char *command,
					modem_cb_t function, void *user_data)
{
	struct modem_callback *callback;

	callback = g_try_new0(struct modem_callback, 1);
	if (callback == NULL)
		return -ENOMEM;

	callback->command   = g_strdup(command);
	callback->function  = function;
	callback->user_data = user_data;

	modem->callbacks = g_slist_append(modem->callbacks, callback);

	return 0;
}

static int modem_command_valist(struct modem_data *modem, modem_cb_t callback,
					void *user_data, const char *command,
					const char *format, va_list var_args)
{
	struct modem_cmd *cmd;

	cmd = g_try_new0(struct modem_cmd, 1);
	if (cmd == NULL)
		return -ENOMEM;

	cmd->cmd = g_strdup(command);
	if (format != NULL)
		cmd->arg = g_strdup_vprintf(format, var_args);

	cmd->callback  = callback;
	cmd->user_data = user_data;

	return queue_command(modem, cmd);
}

int modem_command(struct modem_data *modem,
				modem_cb_t callback, void *user_data,
				const char *command, const char *format, ...)
{
	va_list args;
	int err;

	DBG("modem %p", modem);

	if (modem == NULL)
		return -ENOENT;

	va_start(args, format);
	err = modem_command_valist(modem, callback, user_data,
						command, format, args);
	va_end(args);

	return err;
}