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

« back to all changes in this revision

Viewing changes to gril/grilunsol.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
 
 *  RIL library with GLib integration
4
 
 *
5
 
 *  Copyright (C) 2008-2011  Intel Corporation. All rights reserved.
6
 
 *  Copyright (C) 2012-2013  Canonical Ltd.
7
 
 *
8
 
 *  This program is free software; you can redistribute it and/or modify
9
 
 *  it under the terms of the GNU General Public License version 2 as
10
 
 *  published by the Free Software Foundation.
11
 
 *
12
 
 *  This program is distributed in the hope that it will be useful,
13
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 *  GNU General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU General Public License
18
 
 *  along with this program; if not, write to the Free Software
19
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 
 *
21
 
 */
22
 
 
23
 
#ifdef HAVE_CONFIG_H
24
 
#include <config.h>
25
 
#endif
26
 
 
27
 
#include <stdio.h>
28
 
#include <ctype.h>
29
 
#include <errno.h>
30
 
#include <string.h>
31
 
 
32
 
#include <glib.h>
33
 
 
34
 
#include <ofono/log.h>
35
 
#include <ofono/modem.h>
36
 
#include <ofono/gprs-context.h>
37
 
#include "util.h"
38
 
 
39
 
#include "common.h"
40
 
#include "grilunsol.h"
41
 
 
42
 
/* Minimum size is two int32s version/number of calls */
43
 
#define MIN_DATA_CALL_LIST_SIZE 8
44
 
 
45
 
/*
46
 
 * Minimum NITZ is: 'yy/mm/dd,hh:mm:ss'
47
 
 * TZ '(+/-)tz,dt' are optional
48
 
 */
49
 
#define MIN_NITZ_SIZE 17
50
 
 
51
 
static gint data_call_compare(gconstpointer a, gconstpointer b)
52
 
{
53
 
        const struct ril_data_call *ca = a;
54
 
        const struct ril_data_call *cb = b;
55
 
 
56
 
        if (ca->cid < cb->cid)
57
 
                return -1;
58
 
 
59
 
        if (ca->cid > cb->cid)
60
 
                return 1;
61
 
 
62
 
        return 0;
63
 
}
64
 
 
65
 
static void free_data_call(gpointer data, gpointer user_data)
66
 
{
67
 
        struct ril_data_call *call = data;
68
 
 
69
 
        if (call) {
70
 
                g_free(call->ifname);
71
 
                g_free(call->ip_addr);
72
 
                g_free(call->dns_addrs);
73
 
                g_free(call->gateways);
74
 
                g_free(call);
75
 
        }
76
 
}
77
 
 
78
 
void g_ril_unsol_free_data_call_list(struct ril_data_call_list *call_list)
79
 
{
80
 
        if (call_list) {
81
 
                g_slist_foreach(call_list->calls, (GFunc) free_data_call, NULL);
82
 
                g_slist_free(call_list->calls);
83
 
                g_free(call_list);
84
 
        }
85
 
}
86
 
 
87
 
static gboolean handle_settings(struct ril_data_call *call, char *type,
88
 
                                char *ifname, char *raw_ip_addrs,
89
 
                                char *raw_dns, char *raw_gws)
90
 
{
91
 
        gboolean result = FALSE;
92
 
        int protocol;
93
 
        char **dns_addrs = NULL, **gateways = NULL;
94
 
        char **ip_addrs = NULL, **split_ip_addr = NULL;
95
 
 
96
 
        protocol = ril_protocol_string_to_ofono_protocol(type);
97
 
        if (protocol < 0) {
98
 
                ofono_error("%s: invalid type(protocol) specified: %s",
99
 
                                __func__, type);
100
 
                goto done;
101
 
        }
102
 
 
103
 
        if (ifname == NULL || strlen(ifname) == 0) {
104
 
                ofono_error("%s: no interface specified: %s",
105
 
                                __func__, ifname);
106
 
                goto done;
107
 
        }
108
 
 
109
 
        /* Split DNS addresses */
110
 
        if (raw_dns)
111
 
                dns_addrs = g_strsplit(raw_dns, " ", 3);
112
 
 
113
 
        if (dns_addrs == NULL || g_strv_length(dns_addrs) == 0) {
114
 
                ofono_error("%s: no DNS: %s", __func__, raw_dns);
115
 
                goto done;
116
 
        }
117
 
 
118
 
        /*
119
 
         * RILD can return multiple addresses; oFono only supports
120
 
         * setting a single IPv4 gateway.
121
 
         */
122
 
        if (raw_gws)
123
 
                gateways = g_strsplit(raw_gws, " ", 3);
124
 
 
125
 
        if (gateways == NULL || g_strv_length(gateways) == 0) {
126
 
                ofono_error("%s: no gateways: %s", __func__, raw_gws);
127
 
                goto done;
128
 
        }
129
 
 
130
 
        /* TODO:
131
 
         * RILD can return multiple addresses; oFono only supports
132
 
         * setting a single IPv4 address.  At this time, we only
133
 
         * use the first address.  It's possible that a RIL may
134
 
         * just specify the end-points of the point-to-point
135
 
         * connection, in which case this code will need to
136
 
         * changed to handle such a device.
137
 
         *
138
 
         * For now split into a maximum of three, and only use
139
 
         * the first address for the remaining operations.
140
 
         */
141
 
        if (raw_ip_addrs)
142
 
                ip_addrs = g_strsplit(raw_ip_addrs, " ", 3);
143
 
 
144
 
        if (ip_addrs == NULL || g_strv_length(ip_addrs) == 0) {
145
 
                ofono_error("%s: no IP address: %s", __func__, raw_ip_addrs);
146
 
                goto done;
147
 
        }
148
 
 
149
 
        DBG("num ip addrs is: %d", g_strv_length(ip_addrs));
150
 
 
151
 
        if (g_strv_length(ip_addrs) > 1)
152
 
                ofono_warn("%s: more than one IP addr returned: %s", __func__,
153
 
                                raw_ip_addrs);
154
 
        /*
155
 
         * Note - the address may optionally include a prefix size
156
 
         * ( Eg. "/30" ).  As this confuses NetworkManager, we
157
 
         * explicitly strip any prefix after calculating the netmask.
158
 
         */
159
 
        split_ip_addr = g_strsplit(ip_addrs[0], "/", 2);
160
 
 
161
 
        if (split_ip_addr == NULL || g_strv_length(split_ip_addr) == 0) {
162
 
                ofono_error("%s: invalid IP address field returned: %s",
163
 
                                __func__, ip_addrs[0]);
164
 
                goto done;
165
 
        }
166
 
 
167
 
        call->protocol = protocol;
168
 
        call->ifname = g_strdup(ifname);
169
 
        call->ip_addr = g_strdup(split_ip_addr[0]);
170
 
        call->dns_addrs = g_strdupv(dns_addrs);
171
 
        call->gateways = g_strdupv(gateways);
172
 
 
173
 
        result = TRUE;
174
 
 
175
 
done:
176
 
        if (dns_addrs)
177
 
                g_strfreev(dns_addrs);
178
 
 
179
 
        if (gateways)
180
 
                g_strfreev(gateways);
181
 
 
182
 
        if (ip_addrs)
183
 
                g_strfreev(ip_addrs);
184
 
 
185
 
        if (split_ip_addr)
186
 
                g_strfreev(split_ip_addr);
187
 
 
188
 
 
189
 
        return result;
190
 
}
191
 
 
192
 
/*
193
 
 * This function handles RIL_UNSOL_DATA_CALL_LIST_CHANGED messages,
194
 
 * as well as RIL_REQUEST_DATA_CALL_LIST/SETUP_DATA_CALL replies, as
195
 
 * all have the same payload.
196
 
 */
197
 
struct ril_data_call_list *g_ril_unsol_parse_data_call_list(GRil *gril,
198
 
                                                const struct ril_msg *message)
199
 
{
200
 
        struct ril_data_call *call;
201
 
        struct parcel rilp;
202
 
        struct ril_data_call_list *reply = NULL;
203
 
        unsigned int active, cid, i, num_calls, retry, status;
204
 
        char *type = NULL, *ifname = NULL, *raw_addrs = NULL;
205
 
        char *raw_dns = NULL, *raw_gws = NULL;
206
 
 
207
 
        DBG("");
208
 
 
209
 
        /* Can happen for RIL_REQUEST_DATA_CALL_LIST replies */
210
 
        if (message->buf_len < MIN_DATA_CALL_LIST_SIZE) {
211
 
                if (message->req == RIL_REQUEST_SETUP_DATA_CALL) {
212
 
                        ofono_error("%s: message too small: %d",
213
 
                                        __func__,
214
 
                                        (int) message->buf_len);
215
 
                        goto error;
216
 
                } else {
217
 
                        g_ril_append_print_buf(gril, "{");
218
 
                        goto done;
219
 
                }
220
 
        }
221
 
 
222
 
        reply = g_try_new0(struct ril_data_call_list, 1);
223
 
        if (reply == NULL) {
224
 
                ofono_error("%s: out of memory", __func__);
225
 
                goto error;
226
 
        }
227
 
 
228
 
        g_ril_init_parcel(message, &rilp);
229
 
 
230
 
        /*
231
 
         * ril.h documents the reply to a RIL_REQUEST_DATA_CALL_LIST
232
 
         * as being an array of  RIL_Data_Call_Response_v6 structs,
233
 
         * however in reality, the response also includes a version
234
 
         * to start.
235
 
         */
236
 
        reply->version = parcel_r_int32(&rilp);
237
 
        num_calls = parcel_r_int32(&rilp);
238
 
 
239
 
        g_ril_append_print_buf(gril,
240
 
                                "{version=%d,num=%d",
241
 
                                reply->version,
242
 
                                num_calls);
243
 
 
244
 
        for (i = 0; i < num_calls; i++) {
245
 
                status = parcel_r_int32(&rilp);
246
 
                retry = parcel_r_int32(&rilp);          /* ignore */
247
 
                cid = parcel_r_int32(&rilp);
248
 
                active = parcel_r_int32(&rilp);
249
 
                type = parcel_r_string(&rilp);
250
 
                ifname = parcel_r_string(&rilp);
251
 
                raw_addrs = parcel_r_string(&rilp);
252
 
                raw_dns = parcel_r_string(&rilp);
253
 
                raw_gws = parcel_r_string(&rilp);
254
 
 
255
 
                /* malformed check */
256
 
                if (rilp.malformed) {
257
 
                        ofono_error("%s: malformed parcel received", __func__);
258
 
                        goto error;
259
 
                }
260
 
 
261
 
                g_ril_append_print_buf(gril,
262
 
                                        "%s [status=%d,retry=%d,cid=%d,"
263
 
                                        "active=%d,type=%s,ifname=%s,"
264
 
                                        "address=%s,dns=%s,gateways=%s]",
265
 
                                        print_buf,
266
 
                                        status,
267
 
                                        retry,
268
 
                                        cid,
269
 
                                        active,
270
 
                                        type,
271
 
                                        ifname,
272
 
                                        raw_addrs,
273
 
                                        raw_dns,
274
 
                                        raw_gws);
275
 
 
276
 
                call = g_try_new0(struct ril_data_call, 1);
277
 
                if (call == NULL) {
278
 
                        ofono_error("%s: out of memory", __func__);
279
 
                        goto error;
280
 
                }
281
 
 
282
 
                call->status = status;
283
 
                call->cid = cid;
284
 
                call->active = active;
285
 
 
286
 
                if (message->req == RIL_REQUEST_SETUP_DATA_CALL &&
287
 
                        status == PDP_FAIL_NONE &&
288
 
                        handle_settings(call, type, ifname, raw_addrs,
289
 
                                        raw_dns, raw_gws) == FALSE)
290
 
                        goto error;
291
 
 
292
 
                g_free(type);
293
 
                g_free(ifname);
294
 
                g_free(raw_addrs);
295
 
                g_free(raw_dns);
296
 
                g_free(raw_gws);
297
 
 
298
 
                reply->calls =
299
 
                        g_slist_insert_sorted(reply->calls, call,
300
 
                                                data_call_compare);
301
 
        }
302
 
 
303
 
done:
304
 
        g_ril_append_print_buf(gril, "%s}", print_buf);
305
 
 
306
 
        if (message->unsolicited)
307
 
                g_ril_print_unsol(gril, message);
308
 
        else
309
 
                g_ril_print_response(gril, message);
310
 
 
311
 
        return reply;
312
 
 
313
 
error:
314
 
        g_free(type);
315
 
        g_free(ifname);
316
 
        g_free(raw_addrs);
317
 
        g_free(raw_dns);
318
 
        g_free(raw_gws);
319
 
        g_ril_unsol_free_data_call_list(reply);
320
 
 
321
 
        return NULL;
322
 
}
323
 
 
324
 
char *g_ril_unsol_parse_nitz(GRil *gril, const struct ril_msg *message)
325
 
{
326
 
        struct parcel rilp;
327
 
        gchar *nitz = NULL;
328
 
 
329
 
        DBG("");
330
 
 
331
 
        if (message->buf_len < MIN_NITZ_SIZE) {
332
 
                ofono_error("%s: NITZ too small: %d",
333
 
                                __func__,
334
 
                                (int) message->buf_len);
335
 
                goto error;
336
 
        }
337
 
 
338
 
        g_ril_init_parcel(message, &rilp);
339
 
 
340
 
        nitz = parcel_r_string(&rilp);
341
 
 
342
 
        g_ril_append_print_buf(gril, "(%s)", nitz);
343
 
        g_ril_print_unsol(gril, message);
344
 
 
345
 
error:
346
 
        return nitz;
347
 
}
348
 
 
349
 
void g_ril_unsol_free_sms_data(struct unsol_sms_data *unsol)
350
 
{
351
 
        if (unsol != NULL) {
352
 
                g_free(unsol->data);
353
 
                g_free(unsol);
354
 
        }
355
 
}
356
 
 
357
 
struct unsol_sms_data *g_ril_unsol_parse_new_sms(GRil *gril,
358
 
                                                const struct ril_msg *message)
359
 
{
360
 
        struct parcel rilp;
361
 
        char *ril_pdu;
362
 
        size_t ril_pdu_len;
363
 
        struct unsol_sms_data *sms_data;
364
 
 
365
 
        sms_data = g_new0(struct unsol_sms_data, 1);
366
 
        if (sms_data == NULL) {
367
 
                ofono_error("%s out of memory", __func__);
368
 
                goto error;
369
 
        }
370
 
 
371
 
        g_ril_init_parcel(message, &rilp);
372
 
 
373
 
        ril_pdu = parcel_r_string(&rilp);
374
 
        if (ril_pdu == NULL) {
375
 
                ofono_error("%s Unable to parse notification", __func__);
376
 
                goto error;
377
 
        }
378
 
 
379
 
        ril_pdu_len = strlen(ril_pdu);
380
 
 
381
 
        sms_data->data = decode_hex(ril_pdu, ril_pdu_len,
382
 
                                        &sms_data->length, -1);
383
 
        if (sms_data->data == NULL) {
384
 
                ofono_error("%s Unable to decode notification", __func__);
385
 
                goto error_dec;
386
 
        }
387
 
 
388
 
        g_ril_append_print_buf(gril, "{%s}", ril_pdu);
389
 
        g_ril_print_unsol(gril, message);
390
 
 
391
 
        g_free(ril_pdu);
392
 
 
393
 
        return sms_data;
394
 
 
395
 
error_dec:
396
 
        g_free(ril_pdu);
397
 
error:
398
 
        g_ril_unsol_free_sms_data(sms_data);
399
 
        return NULL;
400
 
}
401
 
 
402
 
int g_ril_unsol_parse_radio_state_changed(GRil *gril, const struct ril_msg *message)
403
 
{
404
 
        struct parcel rilp;
405
 
        int radio_state;
406
 
 
407
 
        g_ril_init_parcel(message, &rilp);
408
 
 
409
 
        radio_state = parcel_r_int32(&rilp);
410
 
 
411
 
        if (rilp.malformed) {
412
 
                ofono_error("%s: malformed parcel received", __func__);
413
 
                radio_state = -1;
414
 
        }
415
 
 
416
 
        g_ril_append_print_buf(gril, "(state: %s)",
417
 
                                ril_radio_state_to_string(radio_state));
418
 
 
419
 
        g_ril_print_unsol(gril, message);
420
 
 
421
 
        return radio_state;
422
 
}
423
 
 
424
 
int g_ril_unsol_parse_signal_strength(GRil *gril, const struct ril_msg *message)
425
 
{
426
 
        struct parcel rilp;
427
 
        int gw_signal, cdma_dbm, evdo_dbm, lte_signal;
428
 
 
429
 
        g_ril_init_parcel(message, &rilp);
430
 
 
431
 
        /* RIL_SignalStrength_v5 */
432
 
        /* GW_SignalStrength */
433
 
        gw_signal = parcel_r_int32(&rilp);
434
 
        parcel_r_int32(&rilp); /* bitErrorRate */
435
 
 
436
 
        /* CDMA_SignalStrength */
437
 
        cdma_dbm = parcel_r_int32(&rilp);
438
 
        parcel_r_int32(&rilp); /* ecio */
439
 
 
440
 
        /* EVDO_SignalStrength */
441
 
        evdo_dbm = parcel_r_int32(&rilp);
442
 
        parcel_r_int32(&rilp); /* ecio */
443
 
        parcel_r_int32(&rilp); /* signalNoiseRatio */
444
 
 
445
 
        /* Present only for RIL_SignalStrength_v6 or newer */
446
 
        if (parcel_data_avail(&rilp) > 0) {
447
 
                /* LTE_SignalStrength */
448
 
                lte_signal = parcel_r_int32(&rilp);
449
 
                parcel_r_int32(&rilp); /* rsrp */
450
 
                parcel_r_int32(&rilp); /* rsrq */
451
 
                parcel_r_int32(&rilp); /* rssnr */
452
 
                parcel_r_int32(&rilp); /* cqi */
453
 
        } else {
454
 
                lte_signal = -1;
455
 
        }
456
 
 
457
 
        g_ril_append_print_buf(gril, "(gw: %d, cdma: %d, evdo: %d, lte: %d)",
458
 
                                gw_signal, cdma_dbm, evdo_dbm, lte_signal);
459
 
 
460
 
        if (message->unsolicited)
461
 
                g_ril_print_unsol(gril, message);
462
 
        else
463
 
                g_ril_print_response(gril, message);
464
 
 
465
 
        /* Return the first valid one */
466
 
        if ((gw_signal != 99) && (gw_signal != -1))
467
 
                return (gw_signal * 100) / 31;
468
 
        if ((lte_signal != 99) && (lte_signal != -1))
469
 
                return (lte_signal * 100) / 31;
470
 
 
471
 
        /* In case of dbm, return the value directly */
472
 
        if (cdma_dbm != -1) {
473
 
                if (cdma_dbm > 100)
474
 
                        cdma_dbm = 100;
475
 
                return cdma_dbm;
476
 
        }
477
 
        if (evdo_dbm != -1) {
478
 
                if (evdo_dbm > 100)
479
 
                        evdo_dbm = 100;
480
 
                return evdo_dbm;
481
 
        }
482
 
 
483
 
        return -1;
484
 
}
485
 
 
486
 
void g_ril_unsol_free_supp_svc_notif(struct unsol_supp_svc_notif *unsol)
487
 
{
488
 
        g_free(unsol);
489
 
}
490
 
 
491
 
struct unsol_supp_svc_notif *g_ril_unsol_parse_supp_svc_notif(GRil *gril,
492
 
                                                struct ril_msg *message)
493
 
{
494
 
        struct parcel rilp;
495
 
        char *tmp_number;
496
 
        int type;
497
 
        struct unsol_supp_svc_notif *unsol =
498
 
                g_new0(struct unsol_supp_svc_notif, 1);
499
 
 
500
 
        g_ril_init_parcel(message, &rilp);
501
 
 
502
 
        unsol->notif_type = parcel_r_int32(&rilp);
503
 
        unsol->code = parcel_r_int32(&rilp);
504
 
        unsol->index = parcel_r_int32(&rilp);
505
 
        type = parcel_r_int32(&rilp);
506
 
        tmp_number = parcel_r_string(&rilp);
507
 
 
508
 
        if (tmp_number != NULL) {
509
 
                strncpy(unsol->number.number, tmp_number,
510
 
                        OFONO_MAX_PHONE_NUMBER_LENGTH);
511
 
                unsol->number.type = type;
512
 
                g_free(tmp_number);
513
 
        }
514
 
 
515
 
        g_ril_append_print_buf(gril, "{%d,%d,%d,%d,%s}",
516
 
                                unsol->notif_type, unsol->code, unsol->index,
517
 
                                type, tmp_number);
518
 
        g_ril_print_unsol(gril, message);
519
 
 
520
 
        return unsol;
521
 
}
522
 
 
523
 
void g_ril_unsol_free_ussd(struct unsol_ussd *unsol)
524
 
{
525
 
        if (unsol != NULL) {
526
 
                g_free(unsol->message);
527
 
                g_free(unsol);
528
 
        }
529
 
}
530
 
 
531
 
struct unsol_ussd *g_ril_unsol_parse_ussd(GRil *gril, struct ril_msg *message)
532
 
{
533
 
        struct parcel rilp;
534
 
        struct unsol_ussd *ussd;
535
 
        char *typestr = NULL;
536
 
        int numstr;
537
 
 
538
 
        ussd = g_try_malloc0(sizeof(*ussd));
539
 
        if (ussd == NULL) {
540
 
                ofono_error("%s out of memory", __func__);
541
 
                goto error;
542
 
        }
543
 
 
544
 
        g_ril_init_parcel(message, &rilp);
545
 
 
546
 
        numstr = parcel_r_int32(&rilp);
547
 
        if (numstr < 1) {
548
 
                ofono_error("%s malformed parcel", __func__);
549
 
                goto error;
550
 
        }
551
 
 
552
 
        typestr = parcel_r_string(&rilp);
553
 
        if (typestr == NULL || *typestr == '\0') {
554
 
                ofono_error("%s wrong type", __func__);
555
 
                goto error;
556
 
        }
557
 
 
558
 
        ussd->type = *typestr - '0';
559
 
 
560
 
        g_free(typestr);
561
 
 
562
 
        if (numstr > 1)
563
 
                ussd->message = parcel_r_string(&rilp);
564
 
 
565
 
        g_ril_append_print_buf(gril, "{%d,%s}", ussd->type, ussd->message);
566
 
 
567
 
        g_ril_print_unsol(gril, message);
568
 
 
569
 
        return ussd;
570
 
 
571
 
error:
572
 
        g_free(typestr);
573
 
        g_free(ussd);
574
 
 
575
 
        return NULL;
576
 
}