File: emulator.c

package info (click to toggle)
ofono 2.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,064 kB
  • sloc: ansic: 224,979; sh: 5,012; python: 4,040; makefile: 956
file content (264 lines) | stat: -rw-r--r-- 5,638 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2011  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 <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <glib.h>

#define OFONO_API_SUBJECT_TO_CHANGE
#include <ofono/plugin.h>
#include <ofono/log.h>
#include <ofono/modem.h>
#include <ofono/emulator.h>

#include "ofono.h"

#define DUN_PORT 12346
#define HFP_PORT 12347

static unsigned int modemwatch_id;
static guint dun_watch;
static guint hfp_watch;
static struct l_queue *modem_infos;
static unsigned int n_powered;

struct modem_info {
	struct ofono_modem *modem;
	unsigned int watch_id;
};

static bool modem_matches(const void *data, const void *user_data)
{
	const struct modem_info *info = data;

	return info->modem == user_data;
}

static void modem_info_free(struct modem_info *info)
{
	if (!info)
		return;

	if (info->watch_id)
		__ofono_modem_remove_powered_watch(info->modem, info->watch_id);

	l_free(info);
}

static struct ofono_modem *find_first_powered(void)
{
	const struct l_queue_entry *entry;

	for (entry = l_queue_get_entries(modem_infos); entry;
							entry = entry->next) {
		struct ofono_modem *modem = entry->data;

		if (ofono_modem_get_powered(modem))
			return modem;
	}

	return NULL;
}

static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
							gpointer user)
{
	struct sockaddr saddr;
	unsigned int len = sizeof(saddr);
	int fd;
	struct ofono_emulator *em;
	struct ofono_modem *modem;

	if (cond != G_IO_IN)
		return FALSE;

	fd = accept(g_io_channel_unix_get_fd(chan), &saddr, &len);
	if (fd == -1)
		return FALSE;

	/* Pick the first powered modem */
	modem = find_first_powered();
	if (!modem)
		goto error;

	DBG("Picked modem %p for emulator", modem);

	em = ofono_emulator_create(modem, GPOINTER_TO_INT(user));
	if (!em)
		goto error;

	ofono_emulator_register(em, fd);
	return TRUE;

error:
	close(fd);
	return TRUE;
}

static guint create_tcp(short port, enum ofono_emulator_type type)
{
	struct sockaddr_in addr;
	int sk;
	int reuseaddr = 1;
	GIOChannel *server;
	guint server_watch;

	sk = socket(PF_INET, SOCK_STREAM, 0);
	if (sk < 0)
		return 0;

	memset(&addr, 0, sizeof(addr));

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = INADDR_ANY;
	addr.sin_port = htons(port);

	setsockopt(sk, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));

	if (bind(sk, (struct sockaddr *) &addr, sizeof(struct sockaddr)) < 0)
		goto err;

	if (listen(sk, 1) < 0)
		goto err;

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

	server_watch = g_io_add_watch_full(server, G_PRIORITY_DEFAULT,
				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
				on_socket_connected, GINT_TO_POINTER(type),
				NULL);
	g_io_channel_unref(server);

	DBG("Created server_watch: %u", server_watch);
	return server_watch;

err:
	close(sk);
	return 0;
}

static void powered_watch_destroy(void *user)
{
	struct modem_info *info = user;

	DBG("");

	info->watch_id = 0;
}

static void powered_watch(struct ofono_modem *modem, gboolean powered,
				void *user)
{
	DBG("powered: %d", powered);

	if (powered == FALSE) {
		n_powered -= 1;

		if (n_powered)
			return;

		g_source_remove(dun_watch);
		dun_watch = 0;

		g_source_remove(hfp_watch);
		hfp_watch = 0;

		return;
	}

	n_powered += 1;

	if (!dun_watch)
		dun_watch = create_tcp(DUN_PORT, OFONO_EMULATOR_TYPE_DUN);

	if (!hfp_watch)
		hfp_watch = create_tcp(HFP_PORT, OFONO_EMULATOR_TYPE_HFP);
}

static void modem_watch(struct ofono_modem *modem, gboolean added, void *user)
{
	struct modem_info *info;

	DBG("modem: %p, added: %d", modem, added);

	if (added == FALSE) {
		info = l_queue_remove_if(modem_infos, modem_matches, modem);
		DBG("Removing modem %p, info %p, from the list", modem, info);
		modem_info_free(info);
		return;
	}

	info = l_new(struct modem_info, 1);
	info->modem = modem;
	info->watch_id = __ofono_modem_add_powered_watch(modem, powered_watch,
							info,
							powered_watch_destroy);

	l_queue_push_tail(modem_infos, info);

	if (ofono_modem_get_powered(modem) == TRUE)
		powered_watch(modem, TRUE, NULL);
}

static void call_modemwatch(struct ofono_modem *modem, void *user)
{
	modem_watch(modem, TRUE, user);
}

static int example_emulator_init(void)
{
	DBG("");

	modem_infos = l_queue_new();
	modemwatch_id = __ofono_modemwatch_add(modem_watch, NULL, NULL);

	__ofono_modem_foreach(call_modemwatch, NULL);

	return 0;
}

static void example_emulator_exit(void)
{
	DBG("");

	__ofono_modemwatch_remove(modemwatch_id);
	l_queue_destroy(modem_infos, (l_queue_destroy_func_t) modem_info_free);

	if (dun_watch)
		g_source_remove(dun_watch);

	if (hfp_watch)
		g_source_remove(hfp_watch);
}

OFONO_PLUGIN_DEFINE(example_emulator, "Example AT Modem Emulator Plugin",
			VERSION, OFONO_PLUGIN_PRIORITY_DEFAULT,
			example_emulator_init, example_emulator_exit)