~ubuntu-branches/debian/sid/ofono/sid

« back to all changes in this revision

Viewing changes to drivers/mbmmodem/gprs-context.c

  • Committer: Bazaar Package Importer
  • Author(s): Andres Salomon
  • Date: 2009-11-02 18:46:37 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20091102184637-rh8ik6v1lcdgdh10
Tags: 0.9-1
* New upstream release.
* Since it runs w/out it, change the udev Depends to a Recommends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 *  oFono - Open Source Telephony
 
4
 *
 
5
 *  Copyright (C) 2008-2009  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
#define _GNU_SOURCE
 
27
#include <string.h>
 
28
#include <stdlib.h>
 
29
#include <stdio.h>
 
30
 
 
31
#include <glib.h>
 
32
 
 
33
#include <ofono/log.h>
 
34
#include <ofono/modem.h>
 
35
#include <ofono/gprs-context.h>
 
36
 
 
37
#include "gatchat.h"
 
38
#include "gatresult.h"
 
39
 
 
40
#include "mbmmodem.h"
 
41
 
 
42
#define MBM_E2NAP_DISCONNECTED 0
 
43
#define MBM_E2NAP_CONNECTED 1
 
44
#define MBM_E2NAP_CONNECTING 2
 
45
 
 
46
static const char *none_prefix[] = { NULL };
 
47
 
 
48
struct gprs_context_data {
 
49
        GAtChat *chat;
 
50
        unsigned active_context;
 
51
};
 
52
 
 
53
static void at_enap_down_cb(gboolean ok, GAtResult *result, gpointer user_data)
 
54
{
 
55
        struct cb_data *cbd = user_data;
 
56
        ofono_gprs_context_cb_t cb = cbd->cb;
 
57
        struct ofono_gprs_context *gc = cbd->user;
 
58
        struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 
59
        struct ofono_error error;
 
60
 
 
61
        if (ok)
 
62
                gcd->active_context = 0;
 
63
 
 
64
        dump_response("enap_down_cb", ok, result);
 
65
        decode_at_error(&error, g_at_result_final_response(result));
 
66
 
 
67
        cb(&error, cbd->data);
 
68
}
 
69
 
 
70
static void mbm_enap_up_cb(gboolean ok, GAtResult *result, gpointer user_data)
 
71
{
 
72
        struct cb_data *cbd = user_data;
 
73
        ofono_gprs_context_cb_t cb = cbd->cb;
 
74
        struct ofono_error error;
 
75
 
 
76
        dump_response("enap_up_cb", ok, result);
 
77
        decode_at_error(&error, g_at_result_final_response(result));
 
78
 
 
79
        cb(&error, cbd->data);
 
80
}
 
81
 
 
82
static void mbm_cgdcont_cb(gboolean ok, GAtResult *result, gpointer user_data)
 
83
{
 
84
        struct cb_data *cbd = user_data;
 
85
        ofono_gprs_context_cb_t cb = cbd->cb;
 
86
        struct ofono_gprs_context *gc = cbd->user;
 
87
        struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 
88
        struct cb_data *ncbd;
 
89
        char buf[64];
 
90
 
 
91
        dump_response("cgdcont_cb", ok, result);
 
92
 
 
93
        if (!ok) {
 
94
                struct ofono_error error;
 
95
 
 
96
                gcd->active_context = 0;
 
97
 
 
98
                decode_at_error(&error, g_at_result_final_response(result));
 
99
                cb(&error, cbd->data);
 
100
                return;
 
101
        }
 
102
 
 
103
        ncbd = g_memdup(cbd, sizeof(struct cb_data));
 
104
 
 
105
        sprintf(buf, "AT*ENAP=1,%u", gcd->active_context);
 
106
 
 
107
        if (g_at_chat_send(gcd->chat, buf, none_prefix,
 
108
                                mbm_enap_up_cb, ncbd, g_free) > 0)
 
109
                return;
 
110
 
 
111
        if (ncbd)
 
112
                g_free(ncbd);
 
113
 
 
114
        gcd->active_context = 0;
 
115
 
 
116
        CALLBACK_WITH_FAILURE(cb, cbd->data);
 
117
}
 
118
 
 
119
static void mbm_gprs_activate_primary(struct ofono_gprs_context *gc,
 
120
                                const struct ofono_gprs_primary_context *ctx,
 
121
                                ofono_gprs_context_cb_t cb, void *data)
 
122
{
 
123
        struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 
124
        struct cb_data *cbd = cb_data_new(cb, data);
 
125
        char buf[OFONO_GPRS_MAX_APN_LENGTH + 128];
 
126
        int len;
 
127
 
 
128
        if (!cbd)
 
129
                goto error;
 
130
 
 
131
        gcd->active_context = ctx->cid;
 
132
 
 
133
        cbd->user = gc;
 
134
 
 
135
        /* TODO: Handle username / password fields */
 
136
        len = sprintf(buf, "AT+CGDCONT=%u,\"IP\"", ctx->cid);
 
137
 
 
138
        if (ctx->apn)
 
139
                snprintf(buf + len, sizeof(buf) - len - 3, ",\"%s\"",
 
140
                                ctx->apn);
 
141
 
 
142
        if (g_at_chat_send(gcd->chat, buf, none_prefix,
 
143
                                mbm_cgdcont_cb, cbd, g_free) > 0)
 
144
                return;
 
145
error:
 
146
        if (cbd)
 
147
                g_free(cbd);
 
148
 
 
149
        CALLBACK_WITH_FAILURE(cb, data);
 
150
}
 
151
 
 
152
static void mbm_gprs_deactivate_primary(struct ofono_gprs_context *gc,
 
153
                                        unsigned int cid,
 
154
                                        ofono_gprs_context_cb_t cb, void *data)
 
155
{
 
156
        struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 
157
        struct cb_data *cbd = cb_data_new(cb, data);
 
158
 
 
159
        if (!cbd)
 
160
                goto error;
 
161
 
 
162
        cbd->user = gc;
 
163
 
 
164
        if (g_at_chat_send(gcd->chat, "AT*ENAP=0", none_prefix,
 
165
                                at_enap_down_cb, cbd, g_free) > 0)
 
166
                return;
 
167
 
 
168
error:
 
169
        if (cbd)
 
170
                g_free(cbd);
 
171
 
 
172
        CALLBACK_WITH_FAILURE(cb, data);
 
173
}
 
174
 
 
175
static void e2nap_notifier(GAtResult *result, gpointer user_data)
 
176
{
 
177
        struct ofono_gprs_context *gc = user_data;
 
178
        struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 
179
        GAtResultIter iter;
 
180
        int state;
 
181
 
 
182
        if (gcd->active_context == 0)
 
183
                return;
 
184
 
 
185
        g_at_result_iter_init(&iter, result);
 
186
 
 
187
        if (g_at_result_iter_next(&iter, "*E2NAP:") == FALSE)
 
188
                return;
 
189
 
 
190
        g_at_result_iter_next_number(&iter, &state);
 
191
 
 
192
        switch (state) {
 
193
        case MBM_E2NAP_DISCONNECTED:
 
194
                ofono_gprs_context_deactivated(gc, gcd->active_context);
 
195
                gcd->active_context = 0;
 
196
                break;
 
197
        case MBM_E2NAP_CONNECTED:
 
198
                ofono_debug("MBM Context: connected");
 
199
                break;
 
200
        case MBM_E2NAP_CONNECTING:
 
201
                ofono_debug("MBM Context: connecting");
 
202
                break;
 
203
        default:
 
204
                break;
 
205
        };
 
206
}
 
207
 
 
208
static int mbm_gprs_context_probe(struct ofono_gprs_context *gc,
 
209
                                        unsigned int vendor, void *data)
 
210
{
 
211
        GAtChat *chat = data;
 
212
        struct gprs_context_data *gcd;
 
213
 
 
214
        gcd = g_new0(struct gprs_context_data, 1);
 
215
        gcd->chat = chat;
 
216
 
 
217
        g_at_chat_register(chat, "*E2NAP:", e2nap_notifier, FALSE, gc, NULL);
 
218
 
 
219
        g_at_chat_send(chat, "AT*E2NAP=1", NULL, NULL, NULL, NULL);
 
220
 
 
221
        ofono_gprs_context_set_data(gc, gcd);
 
222
 
 
223
        return 0;
 
224
}
 
225
 
 
226
static void mbm_gprs_context_remove(struct ofono_gprs_context *gc)
 
227
{
 
228
        struct gprs_context_data *gcd = ofono_gprs_context_get_data(gc);
 
229
 
 
230
        ofono_gprs_context_set_data(gc, NULL);
 
231
        g_free(gcd);
 
232
}
 
233
 
 
234
static struct ofono_gprs_context_driver driver = {
 
235
        .name                   = "mbm",
 
236
        .probe                  = mbm_gprs_context_probe,
 
237
        .remove                 = mbm_gprs_context_remove,
 
238
        .activate_primary       = mbm_gprs_activate_primary,
 
239
        .deactivate_primary     = mbm_gprs_deactivate_primary,
 
240
};
 
241
 
 
242
void mbm_gprs_context_init()
 
243
{
 
244
        ofono_gprs_context_driver_register(&driver);
 
245
}
 
246
 
 
247
void mbm_gprs_context_exit()
 
248
{
 
249
        ofono_gprs_context_driver_unregister(&driver);
 
250
}