3
* oFono - Open Source Telephony
5
* Copyright (C) 2008-2011 Intel Corporation. All rights reserved.
6
* Copyright (C) 2010 ST-Ericsson AB.
7
* Copyright (C) 2013 Canonical Ltd.
8
* Copyright (C) 2013 Jolla Ltd.
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License version 2 as
12
* published by the Free Software Foundation.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
37
#include <ofono/log.h>
38
#include <ofono/modem.h>
39
#include <ofono/gprs.h>
40
#include <ofono/types.h>
47
#include "grilreply.h"
48
#include "grilrequest.h"
49
#include "grilunsol.h"
52
/* Time between get data status retries */
53
#define GET_STATUS_TIMER_MS 5000
56
* This module is the ofono_gprs_driver implementation for rilmodem.
60
* 1. ofono_gprs_suspend/resume() are not used by this module, as
61
* the concept of suspended GPRS is not exposed by RILD.
64
static int ril_tech_to_bearer_tech(int ril_tech)
67
* This code handles the mapping between the RIL_RadioTechnology
68
* and packet bearer values ( see <curr_bearer> values - 27.007
74
case RADIO_TECH_UNKNOWN:
75
return PACKET_BEARER_NONE;
77
return PACKET_BEARER_GPRS;
79
return PACKET_BEARER_EGPRS;
81
return PACKET_BEARER_UMTS;
82
case RADIO_TECH_HSDPA:
83
return PACKET_BEARER_HSDPA;
84
case RADIO_TECH_HSUPA:
85
return PACKET_BEARER_HSUPA;
86
case RADIO_TECH_HSPAP:
89
* HSPAP is HSPA+; which ofono doesn't define;
90
* so, if differentiating HSPA and HSPA+ is
91
* important, then ofono needs to be patched,
92
* and we probably also need to introduce a
95
return PACKET_BEARER_HSUPA_HSDPA;
97
return PACKET_BEARER_EPS;
99
return PACKET_BEARER_NONE;
103
static void ril_gprs_state_change(struct ril_msg *message, gpointer user_data)
105
struct ofono_gprs *gprs = user_data;
106
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
108
g_ril_print_unsol_no_args(gd->ril, message);
111
* We just want to track network data status if ofono
112
* itself is attached, so we avoid unnecessary data state requests.
114
if (gd->ofono_attached == TRUE)
115
ril_gprs_registration_status(gprs, NULL, NULL);
118
gboolean ril_gprs_set_attached_cb(gpointer user_data)
120
struct cb_data *cbd = user_data;
121
ofono_gprs_cb_t cb = cbd->cb;
125
CALLBACK_WITH_SUCCESS(cb, cbd->data);
128
/* Run once per g_idle_add() call */
132
static void ril_gprs_set_attached(struct ofono_gprs *gprs, int attached,
133
ofono_gprs_cb_t cb, void *data)
135
struct cb_data *cbd = cb_data_new(cb, data, NULL);
136
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
138
DBG("attached: %d", attached);
141
* As RIL offers no actual control over the GPRS 'attached'
142
* state, we save the desired state, and use it to override
143
* the actual modem's state in the 'attached_status' function.
144
* This is similar to the way the core ofono gprs code handles
145
* data roaming ( see src/gprs.c gprs_netreg_update().
147
* The core gprs code calls driver->set_attached() when a netreg
148
* notificaiton is received and any configured roaming conditions
151
gd->ofono_attached = attached;
154
* Call from idle loop, so core can set driver_attached before
155
* the callback is invoked.
157
g_idle_add(ril_gprs_set_attached_cb, cbd);
160
static gboolean ril_get_status_retry(gpointer user_data)
162
struct ofono_gprs *gprs = user_data;
164
ril_gprs_registration_status(gprs, NULL, NULL);
169
static void ril_data_reg_cb(struct ril_msg *message, gpointer user_data)
171
struct cb_data *cbd = user_data;
172
ofono_gprs_status_cb_t cb = cbd->cb;
173
struct ofono_gprs *gprs = cbd->user;
174
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
175
struct reply_reg_state *reply;
176
gboolean attached = FALSE;
177
gboolean notify_status = FALSE;
180
old_status = gd->rild_status;
182
if (message->error != RIL_E_SUCCESS) {
183
ofono_error("%s: DATA_REGISTRATION_STATE reply failure: %s",
185
ril_error_to_string(message->error));
189
if ((reply = g_ril_reply_parse_reg_state(gd->ril, message)) == NULL)
193
* There are three cases that can result in this callback
196
* 1) The driver's probe() method was called, and thus an
197
* internal call to ril_gprs_registration_status() is
198
* generated. No ofono cb exists.
200
* 2) ril_gprs_state_change() is called due to an unsolicited
201
* event from RILD. No ofono cb exists.
203
* 3) The ofono code code calls the driver's attached_status()
204
* function. A valid ofono cb exists.
207
if (gd->rild_status != reply->status) {
208
gd->rild_status = reply->status;
211
notify_status = TRUE;
215
* Override the actual status based upon the desired
216
* attached status set by the core GPRS code ( controlled
217
* by the ConnnectionManager's 'Powered' property ).
219
attached = (reply->status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
220
reply->status == NETWORK_REGISTRATION_STATUS_ROAMING);
222
if (attached && gd->ofono_attached == FALSE) {
223
DBG("attached=true; ofono_attached=false; return !REGISTERED");
224
reply->status = NETWORK_REGISTRATION_STATUS_NOT_REGISTERED;
227
* Further optimization so that if ril_status ==
228
* NOT_REGISTERED, ofono_attached == false, and status ==
229
* ROAMING | REGISTERED, then notify gets cleared...
231
* As is, this results in unecessary status notify calls
232
* when nothing has changed.
234
if (notify_status && reply->status == old_status)
235
notify_status = FALSE;
238
if (old_status == -1) {
239
ofono_gprs_register(gprs);
241
/* Different rild implementations use different events here */
242
g_ril_register(gd->ril,
243
gd->state_changed_unsol,
244
ril_gprs_state_change, gprs);
246
if (reply->max_cids == 0)
247
gd->max_cids = RIL_MAX_NUM_ACTIVE_DATA_CALLS;
248
else if (reply->max_cids < RIL_MAX_NUM_ACTIVE_DATA_CALLS)
249
gd->max_cids = reply->max_cids;
251
gd->max_cids = RIL_MAX_NUM_ACTIVE_DATA_CALLS;
253
DBG("Setting max cids to %d", gd->max_cids);
254
ofono_gprs_set_cid_range(gprs, 1, gd->max_cids);
257
* This callback is a result of the inital call
258
* to probe(), so should return after registration.
265
/* Just need to notify ofono if it's already attached */
269
* If network disconnect has occurred, call detached_notify()
270
* instead of status_notify().
273
(old_status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
275
NETWORK_REGISTRATION_STATUS_ROAMING)) {
276
DBG("calling ofono_gprs_detached_notify()");
277
ofono_gprs_detached_notify(gprs);
278
reply->tech = RADIO_TECH_UNKNOWN;
280
DBG("calling ofono_gprs_status_notify()");
281
ofono_gprs_status_notify(gprs, reply->status);
285
if (gd->tech != reply->tech) {
286
gd->tech = reply->tech;
288
ofono_gprs_bearer_notify(gprs,
289
ril_tech_to_bearer_tech(reply->tech));
293
CALLBACK_WITH_SUCCESS(cb, reply->status, cbd->data);
301
* For some modems DATA_REGISTRATION_STATE will return an error until we
302
* are registered in the voice network.
304
if (old_status == -1 && message->error == RIL_E_GENERIC_FAILURE)
305
g_timeout_add(GET_STATUS_TIMER_MS, ril_get_status_retry, gprs);
308
CALLBACK_WITH_FAILURE(cb, -1, cbd->data);
311
void ril_gprs_registration_status(struct ofono_gprs *gprs,
312
ofono_gprs_status_cb_t cb, void *data)
314
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
315
struct cb_data *cbd = cb_data_new(cb, data, gprs);
319
if (g_ril_send(gd->ril, RIL_REQUEST_DATA_REGISTRATION_STATE, NULL,
320
ril_data_reg_cb, cbd, g_free) == 0) {
321
ofono_error("%s: send "
322
"RIL_REQUEST_DATA_REGISTRATION_STATE failed",
327
CALLBACK_WITH_FAILURE(cb, -1, data);
331
static void drop_data_call_cb(struct ril_msg *message, gpointer user_data)
333
struct ofono_gprs *gprs = user_data;
334
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
336
if (message->error == RIL_E_SUCCESS)
337
g_ril_print_response_no_args(gd->ril, message);
339
ofono_error("%s: RIL error %s", __func__,
340
ril_error_to_string(message->error));
342
if (--(gd->pending_deact_req) == 0)
343
ril_gprs_registration_status(gprs, NULL, NULL);
346
static int drop_data_call(struct ofono_gprs *gprs, int cid)
348
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
349
struct req_deactivate_data_call request;
351
struct ofono_error error;
354
request.reason = RIL_DEACTIVATE_DATA_CALL_NO_REASON;
356
g_ril_request_deactivate_data_call(gd->ril, &request, &rilp, &error);
358
if (g_ril_send(gd->ril, RIL_REQUEST_DEACTIVATE_DATA_CALL,
359
&rilp, drop_data_call_cb, gprs, NULL) == 0) {
360
ofono_error("%s: send failed", __func__);
367
static void get_active_data_calls_cb(struct ril_msg *message,
370
struct ofono_gprs *gprs = user_data;
371
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
372
struct ril_data_call_list *call_list = NULL;
374
struct ril_data_call *call;
376
if (message->error != RIL_E_SUCCESS) {
377
ofono_error("%s: RIL error %s", __func__,
378
ril_error_to_string(message->error));
382
/* reply can be NULL when there are no existing data calls */
383
call_list = g_ril_unsol_parse_data_call_list(gd->ril, message);
384
if (call_list == NULL)
388
* We disconnect from previous calls here, which might be needed
389
* because of a previous ofono abort, as some rild implementations do
390
* not disconnect the calls even after the ril socket is closed.
392
for (iterator = call_list->calls; iterator; iterator = iterator->next) {
393
call = iterator->data;
394
DBG("Standing data call with cid %d", call->cid);
395
if (drop_data_call(gprs, call->cid) == 0)
396
++(gd->pending_deact_req);
399
g_ril_unsol_free_data_call_list(call_list);
402
if (gd->pending_deact_req == 0)
403
ril_gprs_registration_status(gprs, NULL, NULL);
406
static void get_active_data_calls(struct ofono_gprs *gprs)
408
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
410
if (g_ril_send(gd->ril, RIL_REQUEST_DATA_CALL_LIST, NULL,
411
get_active_data_calls_cb, gprs, NULL) == 0)
412
ofono_error("%s: send failed", __func__);
415
void ril_gprs_start(GRil *ril, struct ofono_gprs *gprs,
416
struct ril_gprs_data *gd)
418
gd->ril = g_ril_clone(ril);
419
gd->ofono_attached = FALSE;
421
gd->rild_status = -1;
422
gd->tech = RADIO_TECH_UNKNOWN;
423
/* AOSP RILD tracks data network state together with voice */
424
gd->state_changed_unsol =
425
RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED;
427
ofono_gprs_set_data(gprs, gd);
429
get_active_data_calls(gprs);
432
int ril_gprs_probe(struct ofono_gprs *gprs, unsigned int vendor, void *data)
435
struct ril_gprs_data *gd;
437
gd = g_try_new0(struct ril_gprs_data, 1);
441
ril_gprs_start(ril, gprs, gd);
446
void ril_gprs_remove(struct ofono_gprs *gprs)
448
struct ril_gprs_data *gd = ofono_gprs_get_data(gprs);
452
ofono_gprs_set_data(gprs, NULL);
454
g_ril_unref(gd->ril);
458
static struct ofono_gprs_driver driver = {
460
.probe = ril_gprs_probe,
461
.remove = ril_gprs_remove,
462
.set_attached = ril_gprs_set_attached,
463
.attached_status = ril_gprs_registration_status,
466
void ril_gprs_init(void)
468
ofono_gprs_driver_register(&driver);
471
void ril_gprs_exit(void)
473
ofono_gprs_driver_unregister(&driver);