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
30
#include <sys/socket.h>
31
#include <netinet/in.h>
32
#include <arpa/inet.h>
39
#define OFONO_API_SUBJECT_TO_CHANGE
40
#include <ofono/plugin.h>
41
#include <ofono/log.h>
42
#include <ofono/modem.h>
43
#include <ofono/stk.h>
45
#include <drivers/atmodem/atutil.h>
46
#include <drivers/atmodem/vendor.h>
50
static struct ofono_modem *stktest;
52
static const char *none_prefix[] = { NULL };
58
static int stktest_probe(struct ofono_modem *modem)
60
struct stktest_data *data;
64
data = g_try_new0(struct stktest_data, 1);
68
ofono_modem_set_data(modem, data);
73
static void stktest_remove(struct ofono_modem *modem)
75
struct stktest_data *data = ofono_modem_get_data(modem);
80
ofono_modem_set_data(modem, NULL);
83
static void stktest_debug(const char *str, void *prefix)
85
ofono_info("%s%s", (const char *) prefix, str);
88
static void stktest_disconnected(gpointer user_data)
90
struct ofono_modem *modem = user_data;
91
struct stktest_data *data = ofono_modem_get_data(modem);
95
ofono_modem_set_powered(modem, FALSE);
97
g_at_chat_unref(data->chat);
101
static int connect_socket(const char *address, int port)
103
struct sockaddr_in addr;
107
sk = socket(PF_INET, SOCK_STREAM, 0);
111
memset(&addr, 0, sizeof(addr));
112
addr.sin_family = AF_INET;
113
addr.sin_addr.s_addr = inet_addr(address);
114
addr.sin_port = htons(port);
116
err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
125
static int stktest_enable(struct ofono_modem *modem)
127
struct stktest_data *data = ofono_modem_get_data(modem);
134
sk = connect_socket("127.0.0.1", 12765);
138
io = g_io_channel_unix_new(sk);
144
syntax = g_at_syntax_new_gsmv1();
145
data->chat = g_at_chat_new(io, syntax);
146
g_at_syntax_unref(syntax);
147
g_io_channel_unref(io);
149
if (data->chat == NULL)
152
if (getenv("OFONO_AT_DEBUG"))
153
g_at_chat_set_debug(data->chat, stktest_debug, "");
155
g_at_chat_set_disconnect_function(data->chat,
156
stktest_disconnected, modem);
161
static void set_online_cb(gboolean ok, GAtResult *result, gpointer user_data)
163
struct cb_data *cbd = user_data;
164
ofono_modem_online_cb_t callback = cbd->cb;
165
struct ofono_error error;
167
decode_at_error(&error, g_at_result_final_response(result));
169
callback(&error, cbd->data);
172
static void stktest_set_online(struct ofono_modem *modem, ofono_bool_t online,
173
ofono_modem_online_cb_t cb, void *user_data)
175
struct stktest_data *data = ofono_modem_get_data(modem);
176
struct cb_data *cbd = cb_data_new(cb, user_data);
181
snprintf(buf, sizeof(buf), "AT+CFUN=%d", online ? 1 : 4);
183
if (g_at_chat_send(data->chat, buf, none_prefix,
184
set_online_cb, cbd, g_free) > 0)
187
CALLBACK_WITH_FAILURE(cb, user_data);
190
static int stktest_disable(struct ofono_modem *modem)
192
struct stktest_data *data = ofono_modem_get_data(modem);
196
g_at_chat_unref(data->chat);
202
static void stktest_pre_sim(struct ofono_modem *modem)
207
static void stktest_post_sim(struct ofono_modem *modem)
209
struct stktest_data *data = ofono_modem_get_data(modem);
213
ofono_stk_create(modem, OFONO_VENDOR_PHONESIM, "atmodem", data->chat);
216
static void stktest_post_online(struct ofono_modem *modem)
220
static struct ofono_modem_driver stktest_driver = {
221
.modem_type = OFONO_MODEM_TYPE_TEST,
223
.probe = stktest_probe,
224
.remove = stktest_remove,
225
.enable = stktest_enable,
226
.disable = stktest_disable,
227
.set_online = stktest_set_online,
228
.pre_sim = stktest_pre_sim,
229
.post_sim = stktest_post_sim,
230
.post_online = stktest_post_online,
233
static int stktest_init(void)
237
err = ofono_modem_driver_register(&stktest_driver);
241
stktest = ofono_modem_create("stktest", "stktest");
242
ofono_modem_register(stktest);
247
static void stktest_exit(void)
249
ofono_modem_remove(stktest);
250
ofono_modem_driver_unregister(&stktest_driver);
253
OFONO_PLUGIN_DEFINE(stktest, "STK End-to-End tester driver", VERSION,
254
OFONO_PLUGIN_PRIORITY_DEFAULT, stktest_init, stktest_exit)