~phablet-team/ofono/ofono-bug-updates

« back to all changes in this revision

Viewing changes to plugins/stktest.c

  • Committer: Denis Kenzior
  • Author(s): Lucas De Marchi
  • Date: 2011-03-18 23:31:14 UTC
  • Revision ID: git-v1:888e07863b24026413bac8f449de377c879e1044
message: add cancelled state

Based on patch from Yang Gu <gyagp0@gmail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 *  oFono - Open Source Telephony
4
 
 *
5
 
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6
 
 *
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.
10
 
 *
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.
15
 
 *
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
19
 
 *
20
 
 */
21
 
 
22
 
#ifdef HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
 
26
 
#include <errno.h>
27
 
#include <unistd.h>
28
 
#include <stdlib.h>
29
 
#include <string.h>
30
 
#include <sys/socket.h>
31
 
#include <netinet/in.h>
32
 
#include <arpa/inet.h>
33
 
#include <stdio.h>
34
 
 
35
 
#include <glib.h>
36
 
#include <gatmux.h>
37
 
#include <gatchat.h>
38
 
 
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>
44
 
 
45
 
#include <drivers/atmodem/atutil.h>
46
 
#include <drivers/atmodem/vendor.h>
47
 
 
48
 
#include "ofono.h"
49
 
 
50
 
static struct ofono_modem *stktest;
51
 
 
52
 
static const char *none_prefix[] = { NULL };
53
 
 
54
 
struct stktest_data {
55
 
        GAtChat *chat;
56
 
};
57
 
 
58
 
static int stktest_probe(struct ofono_modem *modem)
59
 
{
60
 
        struct stktest_data *data;
61
 
 
62
 
        DBG("%p", modem);
63
 
 
64
 
        data = g_try_new0(struct stktest_data, 1);
65
 
        if (data == NULL)
66
 
                return -ENOMEM;
67
 
 
68
 
        ofono_modem_set_data(modem, data);
69
 
 
70
 
        return 0;
71
 
}
72
 
 
73
 
static void stktest_remove(struct ofono_modem *modem)
74
 
{
75
 
        struct stktest_data *data = ofono_modem_get_data(modem);
76
 
 
77
 
        DBG("%p", modem);
78
 
 
79
 
        g_free(data);
80
 
        ofono_modem_set_data(modem, NULL);
81
 
}
82
 
 
83
 
static void stktest_debug(const char *str, void *prefix)
84
 
{
85
 
        ofono_info("%s%s", (const char *) prefix, str);
86
 
}
87
 
 
88
 
static void stktest_disconnected(gpointer user_data)
89
 
{
90
 
        struct ofono_modem *modem = user_data;
91
 
        struct stktest_data *data = ofono_modem_get_data(modem);
92
 
 
93
 
        DBG("");
94
 
 
95
 
        ofono_modem_set_powered(modem, FALSE);
96
 
 
97
 
        g_at_chat_unref(data->chat);
98
 
        data->chat = NULL;
99
 
}
100
 
 
101
 
static int connect_socket(const char *address, int port)
102
 
{
103
 
        struct sockaddr_in addr;
104
 
        int sk;
105
 
        int err;
106
 
 
107
 
        sk = socket(PF_INET, SOCK_STREAM, 0);
108
 
        if (sk < 0)
109
 
                return -EINVAL;
110
 
 
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);
115
 
 
116
 
        err = connect(sk, (struct sockaddr *) &addr, sizeof(addr));
117
 
        if (err < 0) {
118
 
                close(sk);
119
 
                return -errno;
120
 
        }
121
 
 
122
 
        return sk;
123
 
}
124
 
 
125
 
static int stktest_enable(struct ofono_modem *modem)
126
 
{
127
 
        struct stktest_data *data = ofono_modem_get_data(modem);
128
 
        GIOChannel *io;
129
 
        GAtSyntax *syntax;
130
 
        int sk;
131
 
 
132
 
        DBG("%p", modem);
133
 
 
134
 
        sk = connect_socket("127.0.0.1", 12765);
135
 
        if (sk < 0)
136
 
                return sk;
137
 
 
138
 
        io = g_io_channel_unix_new(sk);
139
 
        if (io == NULL) {
140
 
                close(sk);
141
 
                return -ENOMEM;
142
 
        }
143
 
 
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);
148
 
 
149
 
        if (data->chat == NULL)
150
 
                return -ENOMEM;
151
 
 
152
 
        if (getenv("OFONO_AT_DEBUG"))
153
 
                g_at_chat_set_debug(data->chat, stktest_debug, "");
154
 
 
155
 
        g_at_chat_set_disconnect_function(data->chat,
156
 
                                                stktest_disconnected, modem);
157
 
 
158
 
        return 0;
159
 
}
160
 
 
161
 
static void set_online_cb(gboolean ok, GAtResult *result, gpointer user_data)
162
 
{
163
 
        struct cb_data *cbd = user_data;
164
 
        ofono_modem_online_cb_t callback = cbd->cb;
165
 
        struct ofono_error error;
166
 
 
167
 
        decode_at_error(&error, g_at_result_final_response(result));
168
 
 
169
 
        callback(&error, cbd->data);
170
 
}
171
 
 
172
 
static void stktest_set_online(struct ofono_modem *modem, ofono_bool_t online,
173
 
                                ofono_modem_online_cb_t cb, void *user_data)
174
 
{
175
 
        struct stktest_data *data = ofono_modem_get_data(modem);
176
 
        struct cb_data *cbd = cb_data_new(cb, user_data);
177
 
        char buf[64];
178
 
 
179
 
        DBG("%p", modem);
180
 
 
181
 
        snprintf(buf, sizeof(buf), "AT+CFUN=%d", online ? 1 : 4);
182
 
 
183
 
        if (g_at_chat_send(data->chat, buf, none_prefix,
184
 
                                set_online_cb, cbd, g_free) > 0)
185
 
                return;
186
 
 
187
 
        CALLBACK_WITH_FAILURE(cb, user_data);
188
 
}
189
 
 
190
 
static int stktest_disable(struct ofono_modem *modem)
191
 
{
192
 
        struct stktest_data *data = ofono_modem_get_data(modem);
193
 
 
194
 
        DBG("%p", modem);
195
 
 
196
 
        g_at_chat_unref(data->chat);
197
 
        data->chat = NULL;
198
 
 
199
 
        return 0;
200
 
}
201
 
 
202
 
static void stktest_pre_sim(struct ofono_modem *modem)
203
 
{
204
 
        DBG("%p", modem);
205
 
}
206
 
 
207
 
static void stktest_post_sim(struct ofono_modem *modem)
208
 
{
209
 
        struct stktest_data *data = ofono_modem_get_data(modem);
210
 
 
211
 
        DBG("%p", modem);
212
 
 
213
 
        ofono_stk_create(modem, OFONO_VENDOR_PHONESIM, "atmodem", data->chat);
214
 
}
215
 
 
216
 
static void stktest_post_online(struct ofono_modem *modem)
217
 
{
218
 
}
219
 
 
220
 
static struct ofono_modem_driver stktest_driver = {
221
 
        .modem_type     = OFONO_MODEM_TYPE_TEST,
222
 
        .name           = "stktest",
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,
231
 
};
232
 
 
233
 
static int stktest_init(void)
234
 
{
235
 
        int err;
236
 
 
237
 
        err = ofono_modem_driver_register(&stktest_driver);
238
 
        if (err < 0)
239
 
                return err;
240
 
 
241
 
        stktest = ofono_modem_create("stktest", "stktest");
242
 
        ofono_modem_register(stktest);
243
 
 
244
 
        return 0;
245
 
}
246
 
 
247
 
static void stktest_exit(void)
248
 
{
249
 
        ofono_modem_remove(stktest);
250
 
        ofono_modem_driver_unregister(&stktest_driver);
251
 
}
252
 
 
253
 
OFONO_PLUGIN_DEFINE(stktest, "STK End-to-End tester driver", VERSION,
254
 
                OFONO_PLUGIN_PRIORITY_DEFAULT, stktest_init, stktest_exit)