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
35
#include <ofono/log.h>
36
#include <ofono/modem.h>
37
#include <ofono/gprs-context.h>
40
#include "gatresult.h"
45
static const char *none_prefix[] = { NULL };
47
struct gprs_context_data {
49
unsigned int active_context;
50
ofono_gprs_context_cb_t cb;
54
static void at_scact_down_cb(gboolean ok, GAtResult *result,
57
struct cb_data *cbd = user_data;
58
ofono_gprs_context_cb_t cb = cbd->cb;
59
struct ofono_gprs_context *gc = cbd->user;
60
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
61
struct ofono_error error;
67
gcd->cb_data = cbd->data;
70
decode_at_error(&error, g_at_result_final_response(result));
71
cb(&error, cbd->data);
74
static void at_scact_up_cb(gboolean ok, GAtResult *result,
77
struct cb_data *cbd = user_data;
78
ofono_gprs_context_cb_t cb = cbd->cb;
79
struct ofono_gprs_context *gc = cbd->user;
80
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
81
struct ofono_modem *modem;
82
const char *interface;
88
struct ofono_error error;
90
gcd->active_context = 0;
92
decode_at_error(&error, g_at_result_final_response(result));
93
cb(&error, cbd->data);
98
gcd->cb_data = cbd->data;
100
snprintf(buf, sizeof(buf), "AT!SCPADDR=%u", gcd->active_context);
101
g_at_chat_send(gcd->chat, buf, none_prefix, NULL, NULL, NULL);
103
snprintf(buf, sizeof(buf), "AT+CGCONTRDP=%u", gcd->active_context);
104
g_at_chat_send(gcd->chat, buf, none_prefix, NULL, NULL, NULL);
106
modem = ofono_gprs_context_get_modem(gc);
107
interface = ofono_modem_get_string(modem, "NetworkInterface");
109
ofono_gprs_context_set_interface(gc, interface);
110
ofono_gprs_context_set_ipv4_address(gc, NULL, FALSE);
112
CALLBACK_WITH_SUCCESS(gcd->cb, gcd->cb_data);
115
static void at_cgdcont_cb(gboolean ok, GAtResult *result, gpointer user_data)
117
struct cb_data *cbd = user_data;
118
ofono_gprs_context_cb_t cb = cbd->cb;
119
struct ofono_gprs_context *gc = cbd->user;
120
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
121
struct cb_data *ncbd;
127
struct ofono_error error;
129
gcd->active_context = 0;
131
decode_at_error(&error, g_at_result_final_response(result));
132
cb(&error, cbd->data);
136
ncbd = g_memdup(cbd, sizeof(struct cb_data));
138
snprintf(buf, sizeof(buf), "AT!SCACT=1,%u", gcd->active_context);
140
if (g_at_chat_send(gcd->chat, buf, none_prefix,
141
at_scact_up_cb, ncbd, g_free) > 0)
146
gcd->active_context = 0;
148
CALLBACK_WITH_FAILURE(cb, cbd->data);
151
static void sw_gprs_activate_primary(struct ofono_gprs_context *gc,
152
const struct ofono_gprs_primary_context *ctx,
153
ofono_gprs_context_cb_t cb, void *data)
155
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
156
struct cb_data *cbd = cb_data_new(cb, data);
157
char buf[OFONO_GPRS_MAX_APN_LENGTH + 128];
160
DBG("cid %u", ctx->cid);
162
gcd->active_context = ctx->cid;
166
switch (ctx->proto) {
167
case OFONO_GPRS_PROTO_IP:
168
len = snprintf(buf, sizeof(buf), "AT+CGDCONT=%u,\"IP\"",
171
case OFONO_GPRS_PROTO_IPV6:
172
len = snprintf(buf, sizeof(buf), "AT+CGDCONT=%u,\"IPV6\"",
175
case OFONO_GPRS_PROTO_IPV4V6:
176
len = snprintf(buf, sizeof(buf), "AT+CGDCONT=%u,\"IPV4V6\"",
182
snprintf(buf + len, sizeof(buf) - len - 3,
183
",\"%s\"", ctx->apn);
185
if (g_at_chat_send(gcd->chat, buf, none_prefix,
186
at_cgdcont_cb, cbd, g_free) > 0)
191
CALLBACK_WITH_FAILURE(cb, data);
194
static void sw_gprs_deactivate_primary(struct ofono_gprs_context *gc,
196
ofono_gprs_context_cb_t cb, void *data)
198
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
199
struct cb_data *cbd = cb_data_new(cb, data);
206
snprintf(buf, sizeof(buf), "AT!SCACT=0,%u", cid);
208
if (g_at_chat_send(gcd->chat, buf, none_prefix,
209
at_scact_down_cb, cbd, g_free) > 0)
214
CALLBACK_WITH_FAILURE(cb, data);
217
static int sw_gprs_context_probe(struct ofono_gprs_context *gc,
218
unsigned int vendor, void *data)
220
GAtChat *chat = data;
221
struct gprs_context_data *gcd;
225
gcd = g_try_new0(struct gprs_context_data, 1);
229
gcd->chat = g_at_chat_clone(chat);
231
ofono_gprs_context_set_data(gc, gcd);
236
static void sw_gprs_context_remove(struct ofono_gprs_context *gc)
238
struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
242
ofono_gprs_context_set_data(gc, NULL);
244
g_at_chat_unref(gcd->chat);
248
static struct ofono_gprs_context_driver driver = {
250
.probe = sw_gprs_context_probe,
251
.remove = sw_gprs_context_remove,
252
.activate_primary = sw_gprs_activate_primary,
253
.deactivate_primary = sw_gprs_deactivate_primary,
256
void sw_gprs_context_init(void)
258
ofono_gprs_context_driver_register(&driver);
261
void sw_gprs_context_exit(void)
263
ofono_gprs_context_driver_unregister(&driver);