aboutsummaryrefslogtreecommitdiff
path: root/u300-ril-oem.cpp
blob: 8c033157ebbd6ed978f703a04cae4fa6e5e13b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/* ST-Ericsson U300 RIL
**
** Copyright (C) ST-Ericsson AB 2008-2010
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** Based on reference-ril by The Android Open Source Project.
**
** Heavily modified for ST-Ericsson U300 modems.
** Author: Christian Bejram <christian.bejram@stericsson.com>
*/

#include <stdio.h>
#include <string.h>
#include <telephony/ril.h>
#include <utils/Errors.h>
#include <utils/String8.h>

#define LOG_TAG "RILV"
#include <utils/Log.h>

#include "u300-ril.h"
#include "u300-ril-oem.h"
#include "u300-ril-oem-parser.h"
#include "atchannel.h"
#include "at_tok.h"
#include "misc.h"
#include <stdlib.h>

using android::NO_ERROR;
using android::BAD_VALUE;
using android::NAME_NOT_FOUND;
using android::String8;

enum FrequencySubscriptionType
{
    FREQUENCY_SUBSCRIPTION_OFF,
    FREQUENCY_SUBSCRIPTION_ON,
    FREQUENCY_SUBSCRIPTION_QUERY,
};

static android::status_t handleOemPing(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemNetworkSearchAndSet(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemRequestFrequencyReport(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemUpdateFrequencySubscription(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemRequestOpenLogicalChannel(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemRequestCloseLogicalChannel (
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemRequestUiccLogicalChannelAccess (
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemRequestSimAccess(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);
static android::status_t handleOemRequestFastDormancy(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno);

static void onFrequencyNotification(const char *str);

static android::status_t updateFrequencySubscription(ATResponse **atresponse,
                         enum FrequencySubscriptionType status);
static int parseFrequencyNotification(const char *str,
        u300_ril::OemRilParser::pairFrequencyReportItem_t &frequencyReportItem);

/**
 * RIL_REQUEST_OEM_HOOK_RAW
 *
 * This request reserved for OEM-specific uses. It passes raw byte arrays
 * back and forth.
 */
void requestOEMHookRaw(void *data, size_t datalen, RIL_Token t)
{
    u300_ril::OemRilParser parser;
    android::status_t status;
    RIL_Errno ril_errno = RIL_E_SUCCESS;
    uint32_t msg_id;

    do {
        status = parser.setData(static_cast<uint8_t*>(data), datalen);
        if (status != NO_ERROR)
            break;

        status = parser.parseHeader(&msg_id);
        if (status != NO_ERROR)
            break;

        switch (msg_id) {
#ifdef U300_RIL_OEM_MSG_SELFTEST
        case U300_RIL_OEM_MSG_PING:
            status = handleOemPing(parser, &ril_errno);
            break;
#endif /* U300_RIL_OEM_MSG_SELFTEST */
        case U300_RIL_OEM_MSG_NETWORK_SEARCH_AND_SET:
            status = handleOemNetworkSearchAndSet(parser, &ril_errno);
            break;

        case U300_RIL_OEM_MSG_REQUEST_FREQUENCY_REPORT:
            status = handleOemRequestFrequencyReport(parser, &ril_errno);
            break;

        case U300_RIL_OEM_MSG_UPDATE_FREQUENCY_SUBSCRIPTION:
            status = handleOemUpdateFrequencySubscription(parser, &ril_errno);
            break;

        case U300_RIL_OEM_MSG_OPEN_LOGICAL_CHANNEL:
            status = handleOemRequestOpenLogicalChannel(parser, &ril_errno);
            break;

        case U300_RIL_OEM_MSG_CLOSE_LOGICAL_CHANNEL:
            status = handleOemRequestCloseLogicalChannel(parser, &ril_errno);
            break;
        case U300_RIL_OEM_MSG_UICC_LOGICAL_CHANNEL_ACCESS:
            status = handleOemRequestUiccLogicalChannelAccess(parser,
                                                               &ril_errno);
            break;
        case U300_RIL_OEM_MSG_SIM_ACCESS:
            status = handleOemRequestSimAccess(parser, &ril_errno);
            break;
        case U300_RIL_OEM_MSG_FAST_DORMANCY:
            status = handleOemRequestFastDormancy(parser, &ril_errno);
            break;
        default:
            status = NAME_NOT_FOUND;
            break;
        }
    } while (false);

    if (status == NO_ERROR) {
        RIL_onRequestComplete(t, ril_errno, const_cast<uint8_t*>(parser.data()),
                              parser.dataSize());
    } else {
        RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
    }
    return;
}

/**
 * RIL_REQUEST_OEM_HOOK_STRINGS
 *
 * This request reserved for OEM-specific uses. It passes strings
 * back and forth.
 */
void requestOEMHookStrings(void *data, size_t datalen, RIL_Token t)
{
    int i;
    const char **cur;

    for (i = (datalen / sizeof(char *)), cur = (const char **) data;
         i > 0; cur++, i--)
        LOGD("> '%s'", *cur);

    /* Echo back strings. */
    RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
    return;
}

/**
 * Hook for unsolicited responses processing.
 */
void onOemUnsolHook(const char *s)
{
    if (strStartsWith(s, "*EFBR:")) {
        onFrequencyNotification(s);
    }
    // TODO: add your unsolicited handler calls here.
}

/**
 * Handler for *EFBR unsolicited response.
 */
static void onFrequencyNotification(const char *str)
{
    u300_ril::OemRilParser parser;
    parser.writeUnsolFrequencyNotification();
    RIL_onUnsolicitedResponse(RIL_UNSOL_OEM_HOOK_RAW,
                              const_cast<uint8_t*>(parser.data()),
                              parser.dataSize());
    return;
}

// TODO: add your unsolicited handlers here.



#ifdef U300_RIL_OEM_MSG_SELFTEST
/**
 * OEM PING handler.
 *
 * This function is an example of OEM RIL command handler. It expects "PING" in
 * u300_ril_oem_ping_request::val_string and returns "PONG" in
 * u300_ril_oem_ping_response::val_string. Also it negates value of the val_i32
 * field.
 */
static android::status_t handleOemPing(u300_ril::OemRilParser &parser,
                                       RIL_Errno *ril_errno)
{
    android::status_t status;
    static String8 strPing("PING");
    static String8 strPong("PONG");
    struct u300_ril_oem_ping_request req;
    struct u300_ril_oem_ping_response resp;

    status = parser.parsePing(&req);
    if (status != NO_ERROR)
        return status;

    if (req.val_string != strPing)
        *ril_errno = RIL_E_MODE_NOT_SUPPORTED;

    resp.val_string = strPong;
    resp.val_i32 = -req.val_i32;

    status = parser.writePingResponse(&resp);

    return status;
}
#endif /* U300_RIL_OEM_MSG_SELFTEST */

/**
 * OEM NETWORK_SEARCH_AND_SET handler
 */
static android::status_t handleOemNetworkSearchAndSet(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status;
    ATResponse *atresponse = NULL;

    status = at_send_command("AT*EICS", &atresponse);
    if (status < 0 || atresponse->success == 0)
        *ril_errno = RIL_E_GENERIC_FAILURE;
    at_response_free(atresponse);

    return parser.writeNetworkSearchAndSetResponse();
}

/**
 * *EFBRS / *EFBRN parser helper function.
 *
 * \param str: [in] *EFBR[SN] response line to be parsed.
 * \param frequencyReportItem: [out] frequency report item struct to be filled
 *                             by parsed data.
 * \return status of the parsing. Negative value indicates failure.
 */
static int parseFrequencyNotification(const char *str,
        u300_ril::OemRilParser::pairFrequencyReportItem_t &frequencyReportItem)
{
    char *tok, *line;
    int channel, frequency, strength;
    int status;

    line = tok = strdup(str);

    /* +EFBRS: <channel>,<frequency>,<strength> */
    status = at_tok_start(&tok);
    if (status < 0)
        goto out;

    status = at_tok_nextint(&tok, &channel);
    if (status < 0)
        goto out;
    status = at_tok_nextint(&tok, &frequency);
    if (status < 0)
        goto out;
    status = at_tok_nextint(&tok, &strength);
    if (status < 0)
        goto out;

    frequencyReportItem.frequency = frequency;
    frequencyReportItem.strength = strength;
out:
    free(line);
    return status;
}

/**
 * OEM REQUEST_FREQUENCY_REPORT handler
 */
static android::status_t handleOemRequestFrequencyReport(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    u300_ril::OemRilParser::pairFrequencyReportItem_t pairCurrent;
    u300_ril::OemRilParser::vecFrequencyReport_t vecNeighbors;
    ATResponse *atresponse = NULL;
    ATLine *p_line;
    int status;

    pairCurrent.frequency = 0;
    pairCurrent.strength = 0;

    status = at_send_command_multiline("AT*EFBR?", "*EFBR", &atresponse);
    if (status < 0 || atresponse->success == 0) {
        *ril_errno = RIL_E_GENERIC_FAILURE;
    } else {
        for (p_line = atresponse->p_intermediates;
             p_line; p_line = p_line->p_next) {
            if (strStartsWith(p_line->line, "*EFBRS:")) {
                status = parseFrequencyNotification(p_line->line, pairCurrent);
            } else if (strStartsWith(p_line->line, "*EFBRN:")) {
                u300_ril::OemRilParser::pairFrequencyReportItem_t pairTemp;
                status = parseFrequencyNotification(p_line->line, pairTemp);
                vecNeighbors.push_back(pairTemp);
            }
            if (status < 0) {
                *ril_errno = RIL_E_GENERIC_FAILURE;
                break;
            }
        }
    }

    at_response_free(atresponse);
    return parser.writeRequestFrequencyReportResponse(pairCurrent, vecNeighbors);
}

/**
 * OEM UPDATE_FREQUENCY_SUBSCRIPTION handler
 */
static android::status_t handleOemUpdateFrequencySubscription(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status;
    const char *cmd;
    u300_ril_oem_frequency_subscription_request req;
    ATResponse *atresponse = NULL;

    status = parser.parseUpdateFrequencySubscription(&req);
    if (status != NO_ERROR)
        return status;

    if (req.enabled)
        cmd = "AT*EFBR=1";
    else
        cmd = "AT*EFBR=0";

    status = at_send_command_multiline(cmd, "*EFBR", &atresponse);
    if (status < 0 || atresponse->success == 0)
        *ril_errno = RIL_E_GENERIC_FAILURE;
    at_response_free(atresponse);

    return parser.writeUpdateFrequencySubscriptionResponse();
}

/**
 * OEM OPEN_LOGICAL_CHANNEL handler
 */
static android::status_t handleOemRequestOpenLogicalChannel(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status = 0;
    char *cmd = NULL;
    char *line = NULL;
    int session_id = 0;
    u300_ril_oem_open_logical_channel_response response;
    u300_ril_oem_open_logical_channel_request req;
    ATResponse *atresponse = NULL;
    char *application_id_string_as_hex;
    char *curr;
    int i;

    status = parser.parseOpenLogicalChannelRequest(&req);
    if (status != NO_ERROR)
        return status;

    asprintf(&cmd,"AT+CCHO=\"%s\"", req.application_id_string.string());
    status = at_send_command_singleline(cmd, "+CCHO:", &atresponse);
    if (status < 0 || atresponse->success == 0) {
        goto error;
    }

    line = atresponse->p_intermediates->line;
    status = at_tok_start(&line);
    if (status < 0)
        goto error;

    status = at_tok_nextint(&line, &session_id);
    if (status < 0)
        goto error;

    goto exit;
error:
    *ril_errno = RIL_E_GENERIC_FAILURE;
exit:
    response.session_id = session_id;
    at_response_free(atresponse);
    free(cmd);

    return parser.writeOpenLogicalChannelResponse(&response);
}

/**
 * OEM CLOSE_LOGICAL_CHANNEL handler
 */
static android::status_t handleOemRequestCloseLogicalChannel(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status = 0;
    char *cmd = NULL;
    ATResponse *atresponse = NULL;
    u300_ril_oem_close_logical_channel_request req;

    status = parser.parseCloseLogicalChannelRequest(&req);
    if (status != NO_ERROR)
        return status;

    asprintf(&cmd, "AT+CCHC=%d", req.channel_session_id);
    status = at_send_command(cmd, &atresponse);
    if (status < 0 || atresponse->success == 0)
        *ril_errno = RIL_E_GENERIC_FAILURE;

    at_response_free(atresponse);
    free(cmd);

    return parser.writeCloseLogicalChannelResponse();
}

/**
 * OEM UICC_LOGICAL_CHANNEL_ACCESS handler
 */
static android::status_t handleOemRequestUiccLogicalChannelAccess(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status = 0;
    int resplen = 0;
    char *resp = NULL;
    char *cmd = NULL;
    char *line = NULL;
    unsigned char sw1, sw2;
    ATResponse *atresponse = NULL;
    ATCmeError cmeError;
    u300_ril_oem_uicc_logical_channel_access_request req;
    u300_ril_oem_uicc_logical_channel_access_response *response = NULL;

    status = parser.parseUiccLogicalChannelAccessRequest(&req);
    if (status != NO_ERROR)
        return status;

    asprintf(&cmd, "AT+CGLA=%d, %d, \"%s\"",
             req.channel_session_id_val_i32,
             req.command_val_string.length(),
             req.command_val_string.string());
    status = at_send_command_singleline(cmd, "+CGLA:", &atresponse);
    if (status < 0)
        goto error;

    if (atresponse->success == 0) {
        if (at_get_cme_error(atresponse, &cmeError)) {
            if (cmeError == CME_SIM_FAILURE)
                /*
                 * Modem returns CME_SIM_FAILURE if channel session id is not
                 * valid, this happens when modem has been restarted silently
                 * without application noticed. In this case, application is
                 * supposed to re-open the channel to start over.
                 */
                LOGI("Sim command failed probably due to session id is "
                     "invalid, the modem may have been restarted silently.\n");
        }
        goto error;
    }

    line = atresponse->p_intermediates->line;

    status = at_tok_start(&line);
    if (status < 0)
        goto error;

    status = at_tok_nextint(&line, &resplen);
    if (status < 0)
        goto error;

    status = at_tok_nextstr(&line, &resp);
    if (status < 0)
        goto error;

    if ((resplen < 4) || ((size_t)resplen != strlen(resp))) {
        status = -1;
        goto error;
    }
    goto exit;
error:
    *ril_errno = RIL_E_GENERIC_FAILURE;
exit:
    at_response_free(atresponse);
    free(cmd);
    return parser.writeUiccLogicalChannelAccessResponse(response);
}
/**
 * OEM SIM_ACCESS handler
 */
static android::status_t handleOemRequestSimAccess(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status = 0;
    int resplen = 0;
    char *resp = NULL;
    char *cmd = NULL;
    char *line = NULL;
    unsigned char sw1, sw2;
    ATResponse *atresponse = NULL;
    ATCmeError cmeError;
    u300_ril_oem_sim_access_request req;
    u300_ril_oem_sim_access_response *response = NULL;

    status = parser.parseSimAccessRequest(&req);
    if (status != NO_ERROR)
        return status;

    asprintf(&cmd, "AT+CSIM=%d, \"%s\"",
             req.command_val_string.length(),
             req.command_val_string.string());
    status = at_send_command_singleline(cmd, "+CSIM:", &atresponse);

    if (status < 0 || atresponse->success == 0)
        goto error;

    line = atresponse->p_intermediates->line;

    status = at_tok_start(&line);
    if (status < 0)
        goto error;

    status = at_tok_nextint(&line, &resplen);
    if (status < 0)
        goto error;

    status = at_tok_nextstr(&line, &resp);
    if (status < 0)
        goto error;

    if ((resplen < 4) || ((size_t)resplen != strlen(resp))) {
        status = -1;
        goto error;
    }
    goto exit;
error:
    *ril_errno = RIL_E_GENERIC_FAILURE;
exit:
    at_response_free(atresponse);
    free(cmd);
    return parser.writeSimAccessResponse(response);
}

/**
 * OEM FAST DORMANCY handler
 */
static android::status_t handleOemRequestFastDormancy(
                         u300_ril::OemRilParser &parser, RIL_Errno *ril_errno)
{
    int status;
    ATResponse *atresponse = NULL;

    status = at_send_command("AT*EFDORM", &atresponse);
    if (status < 0 || atresponse->success == 0)
        *ril_errno = RIL_E_GENERIC_FAILURE;
    at_response_free(atresponse);

    return parser.writeFastDormancyResponse();
}