3
* oFono - Open Source Telephony
5
* Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License version 2 as
9
* published by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
#include "gatserver.h"
34
#define DUN_SERVER_ADDRESS "192.168.1.1"
35
#define DUN_PEER_ADDRESS "192.168.1.2"
36
#define DUN_DNS_SERVER_1 "10.10.10.10"
37
#define DUN_DNS_SERVER_2 "10.10.10.11"
39
struct ofono_emulator {
40
struct ofono_atom *atom;
41
enum ofono_emulator_type type;
47
static void emulator_debug(const char *str, void *data)
49
ofono_info("%s: %s\n", (char *)data, str);
52
static void emulator_disconnect(gpointer user_data)
54
struct ofono_emulator *em = user_data;
58
ofono_emulator_remove(em);
61
static void ppp_connect(const char *iface, const char *local,
63
const char *dns1, const char *dns2,
66
DBG("Network Device: %s\n", iface);
67
DBG("IP Address: %s\n", local);
68
DBG("Remote IP Address: %s\n", remote);
69
DBG("Primary DNS Server: %s\n", dns1);
70
DBG("Secondary DNS Server: %s\n", dns2);
73
static void ppp_disconnect(GAtPPPDisconnectReason reason, gpointer user_data)
75
struct ofono_emulator *em = user_data;
79
g_at_ppp_unref(em->ppp);
82
if (em->server == NULL)
85
g_at_server_resume(em->server);
88
static gboolean setup_ppp(gpointer user_data)
90
struct ofono_emulator *em = user_data;
97
io = g_at_server_get_io(em->server);
99
g_at_server_suspend(em->server);
101
em->ppp = g_at_ppp_server_new_from_io(io, DUN_SERVER_ADDRESS);
102
if (em->ppp == NULL) {
103
g_at_server_resume(em->server);
107
g_at_ppp_set_server_info(em->ppp, DUN_PEER_ADDRESS,
108
DUN_DNS_SERVER_1, DUN_DNS_SERVER_2);
110
g_at_ppp_set_credentials(em->ppp, "", "");
111
g_at_ppp_set_debug(em->ppp, emulator_debug, "PPP");
113
g_at_ppp_set_connect_function(em->ppp, ppp_connect, em);
114
g_at_ppp_set_disconnect_function(em->ppp, ppp_disconnect, em);
119
static gboolean dial_call(struct ofono_emulator *em, const char *dial_str)
123
DBG("dial call %s", dial_str);
125
if (c == '*' || c == '#' || c == 'T' || c == 't') {
126
g_at_server_send_intermediate(em->server, "CONNECT");
127
em->source = g_idle_add(setup_ppp, em);
133
static void dial_cb(GAtServer *server, GAtServerRequestType type,
134
GAtResult *result, gpointer user_data)
136
struct ofono_emulator *em = user_data;
138
const char *dial_str;
142
if (type != G_AT_SERVER_REQUEST_TYPE_SET)
145
g_at_result_iter_init(&iter, result);
147
if (!g_at_result_iter_next(&iter, ""))
150
dial_str = g_at_result_iter_raw_line(&iter);
157
if (!dial_call(em, dial_str))
163
g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
166
static void emulator_unregister(struct ofono_atom *atom)
168
struct ofono_emulator *em = __ofono_atom_get_data(atom);
173
g_source_remove(em->source);
177
g_at_server_unref(em->server);
181
void ofono_emulator_register(struct ofono_emulator *em, int fd)
185
DBG("%p, %d", em, fd);
190
io = g_io_channel_unix_new(fd);
192
em->server = g_at_server_new(io);
193
if (em->server == NULL)
196
g_io_channel_unref(io);
198
g_at_server_set_debug(em->server, emulator_debug, "Server");
199
g_at_server_set_disconnect_function(em->server,
200
emulator_disconnect, em);
202
__ofono_atom_register(em->atom, emulator_unregister);
204
if (em->type == OFONO_EMULATOR_TYPE_DUN)
205
g_at_server_register(em->server, "D", dial_cb, em, NULL);
208
static void emulator_remove(struct ofono_atom *atom)
210
struct ofono_emulator *em = __ofono_atom_get_data(atom);
212
DBG("atom: %p", atom);
217
struct ofono_emulator *ofono_emulator_create(struct ofono_modem *modem,
218
enum ofono_emulator_type type)
220
struct ofono_emulator *em;
221
enum ofono_atom_type atom_t;
223
DBG("modem: %p, type: %d", modem, type);
225
if (type == OFONO_EMULATOR_TYPE_DUN)
226
atom_t = OFONO_ATOM_TYPE_EMULATOR_DUN;
227
else if (type == OFONO_EMULATOR_TYPE_HFP)
228
atom_t = OFONO_ATOM_TYPE_EMULATOR_HFP;
232
em = g_try_new0(struct ofono_emulator, 1);
239
em->atom = __ofono_modem_add_atom_offline(modem, atom_t,
240
emulator_remove, em);
245
void ofono_emulator_remove(struct ofono_emulator *em)
247
__ofono_atom_free(em->atom);
250
void ofono_emulator_send_final(struct ofono_emulator *em,
251
const struct ofono_error *final)
256
* TODO: Handle various CMEE modes and report error strings from
259
switch (final->type) {
260
case OFONO_ERROR_TYPE_CMS:
261
sprintf(buf, "+CMS ERROR: %d", final->error);
262
g_at_server_send_ext_final(em->server, buf);
265
case OFONO_ERROR_TYPE_CME:
266
sprintf(buf, "+CME ERROR: %d", final->error);
267
g_at_server_send_ext_final(em->server, buf);
270
case OFONO_ERROR_TYPE_NO_ERROR:
271
g_at_server_send_final(em->server, G_AT_SERVER_RESULT_OK);
274
case OFONO_ERROR_TYPE_CEER:
275
case OFONO_ERROR_TYPE_SIM:
276
case OFONO_ERROR_TYPE_FAILURE:
277
g_at_server_send_final(em->server, G_AT_SERVER_RESULT_ERROR);
282
void ofono_emulator_send_unsolicited(struct ofono_emulator *em,
285
g_at_server_send_unsolicited(em->server, result);
288
void ofono_emulator_send_intermediate(struct ofono_emulator *em,
291
g_at_server_send_intermediate(em->server, result);
294
void ofono_emulator_send_info(struct ofono_emulator *em, const char *line,
297
g_at_server_send_info(em->server, line, last);
301
ofono_emulator_request_cb_t cb;
303
ofono_destroy_func destroy;
304
struct ofono_emulator *em;
307
struct ofono_emulator_request {
309
enum ofono_emulator_request_type type;
312
static void handler_proxy(GAtServer *server, GAtServerRequestType type,
313
GAtResult *result, gpointer userdata)
315
struct handler *h = userdata;
316
struct ofono_emulator_request req;
319
case G_AT_SERVER_REQUEST_TYPE_COMMAND_ONLY:
320
req.type = OFONO_EMULATOR_REQUEST_TYPE_COMMAND_ONLY;
322
case G_AT_SERVER_REQUEST_TYPE_SET:
323
req.type = OFONO_EMULATOR_REQUEST_TYPE_SET;
325
case G_AT_SERVER_REQUEST_TYPE_QUERY:
326
req.type = OFONO_EMULATOR_REQUEST_TYPE_QUERY;
328
case G_AT_SERVER_REQUEST_TYPE_SUPPORT:
329
req.type = OFONO_EMULATOR_REQUEST_TYPE_SUPPORT;
332
g_at_result_iter_init(&req.iter, result);
333
g_at_result_iter_next(&req.iter, "");
335
h->cb(h->em, &req, h->data);
338
static void handler_destroy(gpointer userdata)
340
struct handler *h = userdata;
348
ofono_bool_t ofono_emulator_add_handler(struct ofono_emulator *em,
350
ofono_emulator_request_cb_t cb,
351
void *data, ofono_destroy_func destroy)
355
h = g_new0(struct handler, 1);
358
h->destroy = destroy;
361
if (g_at_server_register(em->server, prefix, handler_proxy, h,
362
handler_destroy) == TRUE)
370
ofono_bool_t ofono_emulator_remove_handler(struct ofono_emulator *em,
373
return g_at_server_unregister(em->server, prefix);
376
ofono_bool_t ofono_emulator_request_next_string(
377
struct ofono_emulator_request *req,
380
return g_at_result_iter_next_string(&req->iter, str);
383
ofono_bool_t ofono_emulator_request_next_number(
384
struct ofono_emulator_request *req,
387
return g_at_result_iter_next_number(&req->iter, number);
390
const char *ofono_emulator_request_get_raw(struct ofono_emulator_request *req)
392
return g_at_result_iter_raw_line(&req->iter);
395
enum ofono_emulator_request_type ofono_emulator_request_get_type(
396
struct ofono_emulator_request *req)