ril.cpp revision b589a1171e6c4a9524c7bac473514954571b5da1
1/* //device/libs/telephony/ril.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "RILC"
19
20#include <hardware_legacy/power.h>
21
22#include <telephony/ril.h>
23#include <telephony/ril_cdma_sms.h>
24#include <cutils/sockets.h>
25#include <cutils/jstring.h>
26#include <cutils/record_stream.h>
27#include <utils/Log.h>
28#include <utils/SystemClock.h>
29#include <pthread.h>
30#include <binder/Parcel.h>
31#include <cutils/jstring.h>
32
33#include <sys/types.h>
34#include <pwd.h>
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <stdarg.h>
39#include <string.h>
40#include <unistd.h>
41#include <fcntl.h>
42#include <time.h>
43#include <errno.h>
44#include <assert.h>
45#include <ctype.h>
46#include <alloca.h>
47#include <sys/un.h>
48#include <assert.h>
49#include <netinet/in.h>
50#include <cutils/properties.h>
51
52#include <ril_event.h>
53
54namespace android {
55
56#define PHONE_PROCESS "radio"
57
58#define SOCKET_NAME_RIL "rild"
59#define SOCKET_NAME_RIL_DEBUG "rild-debug"
60
61#define ANDROID_WAKE_LOCK_NAME "radio-interface"
62
63
64#define PROPERTY_RIL_IMPL "gsm.version.ril-impl"
65
66// match with constant in RIL.java
67#define MAX_COMMAND_BYTES (8 * 1024)
68
69// Basically: memset buffers that the client library
70// shouldn't be using anymore in an attempt to find
71// memory usage issues sooner.
72#define MEMSET_FREED 1
73
74#define NUM_ELEMS(a)     (sizeof (a) / sizeof (a)[0])
75
76#define MIN(a,b) ((a)<(b) ? (a) : (b))
77
78/* Constants for response types */
79#define RESPONSE_SOLICITED 0
80#define RESPONSE_UNSOLICITED 1
81
82/* Negative values for private RIL errno's */
83#define RIL_ERRNO_INVALID_RESPONSE -1
84
85// request, response, and unsolicited msg print macro
86#define PRINTBUF_SIZE 8096
87
88// Enable RILC log
89#define RILC_LOG 0
90
91#if RILC_LOG
92    #define startRequest           sprintf(printBuf, "(")
93    #define closeRequest           sprintf(printBuf, "%s)", printBuf)
94    #define printRequest(token, req)           \
95            LOGD("[%04d]> %s %s", token, requestToString(req), printBuf)
96
97    #define startResponse           sprintf(printBuf, "%s {", printBuf)
98    #define closeResponse           sprintf(printBuf, "%s}", printBuf)
99    #define printResponse           LOGD("%s", printBuf)
100
101    #define clearPrintBuf           printBuf[0] = 0
102    #define removeLastChar          printBuf[strlen(printBuf)-1] = 0
103    #define appendPrintBuf(x...)    sprintf(printBuf, x)
104#else
105    #define startRequest
106    #define closeRequest
107    #define printRequest(token, req)
108    #define startResponse
109    #define closeResponse
110    #define printResponse
111    #define clearPrintBuf
112    #define removeLastChar
113    #define appendPrintBuf(x...)
114#endif
115
116enum WakeType {DONT_WAKE, WAKE_PARTIAL};
117
118typedef struct {
119    int requestNumber;
120    void (*dispatchFunction) (Parcel &p, struct RequestInfo *pRI);
121    int(*responseFunction) (Parcel &p, void *response, size_t responselen);
122} CommandInfo;
123
124typedef struct {
125    int requestNumber;
126    int (*responseFunction) (Parcel &p, void *response, size_t responselen);
127    WakeType wakeType;
128} UnsolResponseInfo;
129
130typedef struct RequestInfo {
131    int32_t token;      //this is not RIL_Token
132    CommandInfo *pCI;
133    struct RequestInfo *p_next;
134    char cancelled;
135    char local;         // responses to local commands do not go back to command process
136} RequestInfo;
137
138typedef struct UserCallbackInfo {
139    RIL_TimedCallback p_callback;
140    void *userParam;
141    struct ril_event event;
142    struct UserCallbackInfo *p_next;
143} UserCallbackInfo;
144
145
146/*******************************************************************/
147
148RIL_RadioFunctions s_callbacks = {0, NULL, NULL, NULL, NULL, NULL};
149static int s_registerCalled = 0;
150
151static pthread_t s_tid_dispatch;
152static pthread_t s_tid_reader;
153static int s_started = 0;
154
155static int s_fdListen = -1;
156static int s_fdCommand = -1;
157static int s_fdDebug = -1;
158
159static int s_fdWakeupRead;
160static int s_fdWakeupWrite;
161
162static struct ril_event s_commands_event;
163static struct ril_event s_wakeupfd_event;
164static struct ril_event s_listen_event;
165static struct ril_event s_wake_timeout_event;
166static struct ril_event s_debug_event;
167
168
169static const struct timeval TIMEVAL_WAKE_TIMEOUT = {1,0};
170
171static pthread_mutex_t s_pendingRequestsMutex = PTHREAD_MUTEX_INITIALIZER;
172static pthread_mutex_t s_writeMutex = PTHREAD_MUTEX_INITIALIZER;
173static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
174static pthread_cond_t s_startupCond = PTHREAD_COND_INITIALIZER;
175
176static pthread_mutex_t s_dispatchMutex = PTHREAD_MUTEX_INITIALIZER;
177static pthread_cond_t s_dispatchCond = PTHREAD_COND_INITIALIZER;
178
179static RequestInfo *s_pendingRequests = NULL;
180
181static RequestInfo *s_toDispatchHead = NULL;
182static RequestInfo *s_toDispatchTail = NULL;
183
184static UserCallbackInfo *s_last_wake_timeout_info = NULL;
185
186static void *s_lastNITZTimeData = NULL;
187static size_t s_lastNITZTimeDataSize;
188
189#if RILC_LOG
190    static char printBuf[PRINTBUF_SIZE];
191#endif
192
193/*******************************************************************/
194
195static void dispatchVoid (Parcel& p, RequestInfo *pRI);
196static void dispatchString (Parcel& p, RequestInfo *pRI);
197static void dispatchStrings (Parcel& p, RequestInfo *pRI);
198static void dispatchInts (Parcel& p, RequestInfo *pRI);
199static void dispatchDial (Parcel& p, RequestInfo *pRI);
200static void dispatchSIM_IO (Parcel& p, RequestInfo *pRI);
201static void dispatchCallForward(Parcel& p, RequestInfo *pRI);
202static void dispatchRaw(Parcel& p, RequestInfo *pRI);
203static void dispatchSmsWrite (Parcel &p, RequestInfo *pRI);
204
205static void dispatchCdmaSms(Parcel &p, RequestInfo *pRI);
206static void dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI);
207static void dispatchGsmBrSmsCnf(Parcel &p, RequestInfo *pRI);
208static void dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI);
209static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI);
210static int responseInts(Parcel &p, void *response, size_t responselen);
211static int responseStrings(Parcel &p, void *response, size_t responselen);
212static int responseString(Parcel &p, void *response, size_t responselen);
213static int responseVoid(Parcel &p, void *response, size_t responselen);
214static int responseCallList(Parcel &p, void *response, size_t responselen);
215static int responseSMS(Parcel &p, void *response, size_t responselen);
216static int responseSIM_IO(Parcel &p, void *response, size_t responselen);
217static int responseCallForwards(Parcel &p, void *response, size_t responselen);
218static int responseDataCallList(Parcel &p, void *response, size_t responselen);
219static int responseRaw(Parcel &p, void *response, size_t responselen);
220static int responseSsn(Parcel &p, void *response, size_t responselen);
221static int responseSimStatus(Parcel &p, void *response, size_t responselen);
222static int responseGsmBrSmsCnf(Parcel &p, void *response, size_t responselen);
223static int responseCdmaBrSmsCnf(Parcel &p, void *response, size_t responselen);
224static int responseCdmaSms(Parcel &p, void *response, size_t responselen);
225static int responseCellList(Parcel &p, void *response, size_t responselen);
226static int responseCdmaInformationRecords(Parcel &p,void *response, size_t responselen);
227static int responseRilSignalStrength(Parcel &p,void *response, size_t responselen);
228static int responseCallRing(Parcel &p, void *response, size_t responselen);
229static int responseCdmaSignalInfoRecord(Parcel &p,void *response, size_t responselen);
230static int responseCdmaCallWaiting(Parcel &p,void *response, size_t responselen);
231
232extern "C" const char * requestToString(int request);
233extern "C" const char * failCauseToString(RIL_Errno);
234extern "C" const char * callStateToString(RIL_CallState);
235extern "C" const char * radioStateToString(RIL_RadioState);
236
237#ifdef RIL_SHLIB
238extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
239                                size_t datalen);
240#endif
241
242static UserCallbackInfo * internalRequestTimedCallback
243    (RIL_TimedCallback callback, void *param,
244        const struct timeval *relativeTime);
245
246/** Index == requestNumber */
247static CommandInfo s_commands[] = {
248#include "ril_commands.h"
249};
250
251static UnsolResponseInfo s_unsolResponses[] = {
252#include "ril_unsol_commands.h"
253};
254
255
256static char *
257strdupReadString(Parcel &p) {
258    size_t stringlen;
259    const char16_t *s16;
260
261    s16 = p.readString16Inplace(&stringlen);
262
263    return strndup16to8(s16, stringlen);
264}
265
266static void writeStringToParcel(Parcel &p, const char *s) {
267    char16_t *s16;
268    size_t s16_len;
269    s16 = strdup8to16(s, &s16_len);
270    p.writeString16(s16, s16_len);
271    free(s16);
272}
273
274
275static void
276memsetString (char *s) {
277    if (s != NULL) {
278        memset (s, 0, strlen(s));
279    }
280}
281
282void   nullParcelReleaseFunction (const uint8_t* data, size_t dataSize,
283                                    const size_t* objects, size_t objectsSize,
284                                        void* cookie) {
285    // do nothing -- the data reference lives longer than the Parcel object
286}
287
288/**
289 * To be called from dispatch thread
290 * Issue a single local request, ensuring that the response
291 * is not sent back up to the command process
292 */
293static void
294issueLocalRequest(int request, void *data, int len) {
295    RequestInfo *pRI;
296    int ret;
297
298    pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
299
300    pRI->local = 1;
301    pRI->token = 0xffffffff;        // token is not used in this context
302    pRI->pCI = &(s_commands[request]);
303
304    ret = pthread_mutex_lock(&s_pendingRequestsMutex);
305    assert (ret == 0);
306
307    pRI->p_next = s_pendingRequests;
308    s_pendingRequests = pRI;
309
310    ret = pthread_mutex_unlock(&s_pendingRequestsMutex);
311    assert (ret == 0);
312
313    LOGD("C[locl]> %s", requestToString(request));
314
315    s_callbacks.onRequest(request, data, len, pRI);
316}
317
318
319
320static int
321processCommandBuffer(void *buffer, size_t buflen) {
322    Parcel p;
323    status_t status;
324    int32_t request;
325    int32_t token;
326    RequestInfo *pRI;
327    int ret;
328
329    p.setData((uint8_t *) buffer, buflen);
330
331    // status checked at end
332    status = p.readInt32(&request);
333    status = p.readInt32 (&token);
334
335    if (status != NO_ERROR) {
336        LOGE("invalid request block");
337        return 0;
338    }
339
340    if (request < 1 || request >= (int32_t)NUM_ELEMS(s_commands)) {
341        LOGE("unsupported request code %d token %d", request, token);
342        // FIXME this should perhaps return a response
343        return 0;
344    }
345
346
347    pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
348
349    pRI->token = token;
350    pRI->pCI = &(s_commands[request]);
351
352    ret = pthread_mutex_lock(&s_pendingRequestsMutex);
353    assert (ret == 0);
354
355    pRI->p_next = s_pendingRequests;
356    s_pendingRequests = pRI;
357
358    ret = pthread_mutex_unlock(&s_pendingRequestsMutex);
359    assert (ret == 0);
360
361/*    sLastDispatchedToken = token; */
362
363    pRI->pCI->dispatchFunction(p, pRI);
364
365    return 0;
366}
367
368static void
369invalidCommandBlock (RequestInfo *pRI) {
370    LOGE("invalid command block for token %d request %s",
371                pRI->token, requestToString(pRI->pCI->requestNumber));
372}
373
374/** Callee expects NULL */
375static void
376dispatchVoid (Parcel& p, RequestInfo *pRI) {
377    clearPrintBuf;
378    printRequest(pRI->token, pRI->pCI->requestNumber);
379    s_callbacks.onRequest(pRI->pCI->requestNumber, NULL, 0, pRI);
380}
381
382/** Callee expects const char * */
383static void
384dispatchString (Parcel& p, RequestInfo *pRI) {
385    status_t status;
386    size_t datalen;
387    size_t stringlen;
388    char *string8 = NULL;
389
390    string8 = strdupReadString(p);
391
392    startRequest;
393    appendPrintBuf("%s%s", printBuf, string8);
394    closeRequest;
395    printRequest(pRI->token, pRI->pCI->requestNumber);
396
397    s_callbacks.onRequest(pRI->pCI->requestNumber, string8,
398                       sizeof(char *), pRI);
399
400#ifdef MEMSET_FREED
401    memsetString(string8);
402#endif
403
404    free(string8);
405    return;
406invalid:
407    invalidCommandBlock(pRI);
408    return;
409}
410
411/** Callee expects const char ** */
412static void
413dispatchStrings (Parcel &p, RequestInfo *pRI) {
414    int32_t countStrings;
415    status_t status;
416    size_t datalen;
417    char **pStrings;
418
419    status = p.readInt32 (&countStrings);
420
421    if (status != NO_ERROR) {
422        goto invalid;
423    }
424
425    startRequest;
426    if (countStrings == 0) {
427        // just some non-null pointer
428        pStrings = (char **)alloca(sizeof(char *));
429        datalen = 0;
430    } else if (((int)countStrings) == -1) {
431        pStrings = NULL;
432        datalen = 0;
433    } else {
434        datalen = sizeof(char *) * countStrings;
435
436        pStrings = (char **)alloca(datalen);
437
438        for (int i = 0 ; i < countStrings ; i++) {
439            pStrings[i] = strdupReadString(p);
440            appendPrintBuf("%s%s,", printBuf, pStrings[i]);
441        }
442    }
443    removeLastChar;
444    closeRequest;
445    printRequest(pRI->token, pRI->pCI->requestNumber);
446
447    s_callbacks.onRequest(pRI->pCI->requestNumber, pStrings, datalen, pRI);
448
449    if (pStrings != NULL) {
450        for (int i = 0 ; i < countStrings ; i++) {
451#ifdef MEMSET_FREED
452            memsetString (pStrings[i]);
453#endif
454            free(pStrings[i]);
455        }
456
457#ifdef MEMSET_FREED
458        memset(pStrings, 0, datalen);
459#endif
460    }
461
462    return;
463invalid:
464    invalidCommandBlock(pRI);
465    return;
466}
467
468/** Callee expects const int * */
469static void
470dispatchInts (Parcel &p, RequestInfo *pRI) {
471    int32_t count;
472    status_t status;
473    size_t datalen;
474    int *pInts;
475
476    status = p.readInt32 (&count);
477
478    if (status != NO_ERROR || count == 0) {
479        goto invalid;
480    }
481
482    datalen = sizeof(int) * count;
483    pInts = (int *)alloca(datalen);
484
485    startRequest;
486    for (int i = 0 ; i < count ; i++) {
487        int32_t t;
488
489        status = p.readInt32(&t);
490        pInts[i] = (int)t;
491        appendPrintBuf("%s%d,", printBuf, t);
492
493        if (status != NO_ERROR) {
494            goto invalid;
495        }
496   }
497   removeLastChar;
498   closeRequest;
499   printRequest(pRI->token, pRI->pCI->requestNumber);
500
501   s_callbacks.onRequest(pRI->pCI->requestNumber, const_cast<int *>(pInts),
502                       datalen, pRI);
503
504#ifdef MEMSET_FREED
505    memset(pInts, 0, datalen);
506#endif
507
508    return;
509invalid:
510    invalidCommandBlock(pRI);
511    return;
512}
513
514
515/**
516 * Callee expects const RIL_SMS_WriteArgs *
517 * Payload is:
518 *   int32_t status
519 *   String pdu
520 */
521static void
522dispatchSmsWrite (Parcel &p, RequestInfo *pRI) {
523    RIL_SMS_WriteArgs args;
524    int32_t t;
525    status_t status;
526
527    memset (&args, 0, sizeof(args));
528
529    status = p.readInt32(&t);
530    args.status = (int)t;
531
532    args.pdu = strdupReadString(p);
533
534    if (status != NO_ERROR || args.pdu == NULL) {
535        goto invalid;
536    }
537
538    args.smsc = strdupReadString(p);
539
540    startRequest;
541    appendPrintBuf("%s%d,%s,smsc=%s", printBuf, args.status,
542        (char*)args.pdu,  (char*)args.smsc);
543    closeRequest;
544    printRequest(pRI->token, pRI->pCI->requestNumber);
545
546    s_callbacks.onRequest(pRI->pCI->requestNumber, &args, sizeof(args), pRI);
547
548#ifdef MEMSET_FREED
549    memsetString (args.pdu);
550#endif
551
552    free (args.pdu);
553
554#ifdef MEMSET_FREED
555    memset(&args, 0, sizeof(args));
556#endif
557
558    return;
559invalid:
560    invalidCommandBlock(pRI);
561    return;
562}
563
564/**
565 * Callee expects const RIL_Dial *
566 * Payload is:
567 *   String address
568 *   int32_t clir
569 */
570static void
571dispatchDial (Parcel &p, RequestInfo *pRI) {
572    RIL_Dial dial;
573    int32_t t;
574    status_t status;
575
576    memset (&dial, 0, sizeof(dial));
577
578    dial.address = strdupReadString(p);
579
580    status = p.readInt32(&t);
581    dial.clir = (int)t;
582
583    if (status != NO_ERROR || dial.address == NULL) {
584        goto invalid;
585    }
586
587    startRequest;
588    appendPrintBuf("%snum=%s,clir=%d", printBuf, dial.address, dial.clir);
589    closeRequest;
590    printRequest(pRI->token, pRI->pCI->requestNumber);
591
592    s_callbacks.onRequest(pRI->pCI->requestNumber, &dial, sizeof(dial), pRI);
593
594#ifdef MEMSET_FREED
595    memsetString (dial.address);
596#endif
597
598    free (dial.address);
599
600#ifdef MEMSET_FREED
601    memset(&dial, 0, sizeof(dial));
602#endif
603
604    return;
605invalid:
606    invalidCommandBlock(pRI);
607    return;
608}
609
610/**
611 * Callee expects const RIL_SIM_IO *
612 * Payload is:
613 *   int32_t command
614 *   int32_t fileid
615 *   String path
616 *   int32_t p1, p2, p3
617 *   String data
618 *   String pin2
619 */
620static void
621dispatchSIM_IO (Parcel &p, RequestInfo *pRI) {
622    RIL_SIM_IO simIO;
623    int32_t t;
624    status_t status;
625
626    memset (&simIO, 0, sizeof(simIO));
627
628    // note we only check status at the end
629
630    status = p.readInt32(&t);
631    simIO.command = (int)t;
632
633    status = p.readInt32(&t);
634    simIO.fileid = (int)t;
635
636    simIO.path = strdupReadString(p);
637
638    status = p.readInt32(&t);
639    simIO.p1 = (int)t;
640
641    status = p.readInt32(&t);
642    simIO.p2 = (int)t;
643
644    status = p.readInt32(&t);
645    simIO.p3 = (int)t;
646
647    simIO.data = strdupReadString(p);
648    simIO.pin2 = strdupReadString(p);
649
650    startRequest;
651    appendPrintBuf("%scmd=0x%X,efid=0x%X,path=%s,%d,%d,%d,%s,pin2=%s", printBuf,
652        simIO.command, simIO.fileid, (char*)simIO.path,
653        simIO.p1, simIO.p2, simIO.p3,
654        (char*)simIO.data,  (char*)simIO.pin2);
655    closeRequest;
656    printRequest(pRI->token, pRI->pCI->requestNumber);
657
658    if (status != NO_ERROR) {
659        goto invalid;
660    }
661
662       s_callbacks.onRequest(pRI->pCI->requestNumber, &simIO, sizeof(simIO), pRI);
663
664#ifdef MEMSET_FREED
665    memsetString (simIO.path);
666    memsetString (simIO.data);
667    memsetString (simIO.pin2);
668#endif
669
670    free (simIO.path);
671    free (simIO.data);
672    free (simIO.pin2);
673
674#ifdef MEMSET_FREED
675    memset(&simIO, 0, sizeof(simIO));
676#endif
677
678    return;
679invalid:
680    invalidCommandBlock(pRI);
681    return;
682}
683
684/**
685 * Callee expects const RIL_CallForwardInfo *
686 * Payload is:
687 *  int32_t status/action
688 *  int32_t reason
689 *  int32_t serviceCode
690 *  int32_t toa
691 *  String number  (0 length -> null)
692 *  int32_t timeSeconds
693 */
694static void
695dispatchCallForward(Parcel &p, RequestInfo *pRI) {
696    RIL_CallForwardInfo cff;
697    int32_t t;
698    status_t status;
699
700    memset (&cff, 0, sizeof(cff));
701
702    // note we only check status at the end
703
704    status = p.readInt32(&t);
705    cff.status = (int)t;
706
707    status = p.readInt32(&t);
708    cff.reason = (int)t;
709
710    status = p.readInt32(&t);
711    cff.serviceClass = (int)t;
712
713    status = p.readInt32(&t);
714    cff.toa = (int)t;
715
716    cff.number = strdupReadString(p);
717
718    status = p.readInt32(&t);
719    cff.timeSeconds = (int)t;
720
721    if (status != NO_ERROR) {
722        goto invalid;
723    }
724
725    // special case: number 0-length fields is null
726
727    if (cff.number != NULL && strlen (cff.number) == 0) {
728        cff.number = NULL;
729    }
730
731    startRequest;
732    appendPrintBuf("%sstat=%d,reason=%d,serv=%d,toa=%d,%s,tout=%d", printBuf,
733        cff.status, cff.reason, cff.serviceClass, cff.toa,
734        (char*)cff.number, cff.timeSeconds);
735    closeRequest;
736    printRequest(pRI->token, pRI->pCI->requestNumber);
737
738    s_callbacks.onRequest(pRI->pCI->requestNumber, &cff, sizeof(cff), pRI);
739
740#ifdef MEMSET_FREED
741    memsetString(cff.number);
742#endif
743
744    free (cff.number);
745
746#ifdef MEMSET_FREED
747    memset(&cff, 0, sizeof(cff));
748#endif
749
750    return;
751invalid:
752    invalidCommandBlock(pRI);
753    return;
754}
755
756
757static void
758dispatchRaw(Parcel &p, RequestInfo *pRI) {
759    int32_t len;
760    status_t status;
761    const void *data;
762
763    status = p.readInt32(&len);
764
765    if (status != NO_ERROR) {
766        goto invalid;
767    }
768
769    // The java code writes -1 for null arrays
770    if (((int)len) == -1) {
771        data = NULL;
772        len = 0;
773    }
774
775    data = p.readInplace(len);
776
777    startRequest;
778    appendPrintBuf("%sraw_size=%d", printBuf, len);
779    closeRequest;
780    printRequest(pRI->token, pRI->pCI->requestNumber);
781
782    s_callbacks.onRequest(pRI->pCI->requestNumber, const_cast<void *>(data), len, pRI);
783
784    return;
785invalid:
786    invalidCommandBlock(pRI);
787    return;
788}
789
790static void
791dispatchCdmaSms(Parcel &p, RequestInfo *pRI) {
792    RIL_CDMA_SMS_Message rcsm;
793    int32_t  t;
794    uint8_t ut;
795    status_t status;
796    int32_t digitCount;
797    int digitLimit;
798
799    memset(&rcsm, 0, sizeof(rcsm));
800
801    status = p.readInt32(&t);
802    rcsm.uTeleserviceID = (int) t;
803
804    status = p.read(&ut,sizeof(ut));
805    rcsm.bIsServicePresent = (uint8_t) ut;
806
807    status = p.readInt32(&t);
808    rcsm.uServicecategory = (int) t;
809
810    status = p.readInt32(&t);
811    rcsm.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) t;
812
813    status = p.readInt32(&t);
814    rcsm.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) t;
815
816    status = p.readInt32(&t);
817    rcsm.sAddress.number_type = (RIL_CDMA_SMS_NumberType) t;
818
819    status = p.readInt32(&t);
820    rcsm.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) t;
821
822    status = p.read(&ut,sizeof(ut));
823    rcsm.sAddress.number_of_digits= (uint8_t) ut;
824
825    digitLimit= MIN((rcsm.sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
826    for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
827        status = p.read(&ut,sizeof(ut));
828        rcsm.sAddress.digits[digitCount] = (uint8_t) ut;
829    }
830
831    status = p.readInt32(&t);
832    rcsm.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) t;
833
834    status = p.read(&ut,sizeof(ut));
835    rcsm.sSubAddress.odd = (uint8_t) ut;
836
837    status = p.read(&ut,sizeof(ut));
838    rcsm.sSubAddress.number_of_digits = (uint8_t) ut;
839
840    digitLimit= MIN((rcsm.sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
841    for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
842        status = p.read(&ut,sizeof(ut));
843        rcsm.sSubAddress.digits[digitCount] = (uint8_t) ut;
844    }
845
846    status = p.readInt32(&t);
847    rcsm.uBearerDataLen = (int) t;
848
849    digitLimit= MIN((rcsm.uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
850    for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
851        status = p.read(&ut, sizeof(ut));
852        rcsm.aBearerData[digitCount] = (uint8_t) ut;
853    }
854
855    if (status != NO_ERROR) {
856        goto invalid;
857    }
858
859    startRequest;
860    appendPrintBuf("%suTeleserviceID=%d, bIsServicePresent=%d, uServicecategory=%d, \
861            sAddress.digit_mode=%d, sAddress.Number_mode=%d, sAddress.number_type=%d, ",
862            printBuf, rcsm.uTeleserviceID,rcsm.bIsServicePresent,rcsm.uServicecategory,
863            rcsm.sAddress.digit_mode, rcsm.sAddress.number_mode,rcsm.sAddress.number_type);
864    closeRequest;
865
866    printRequest(pRI->token, pRI->pCI->requestNumber);
867
868    s_callbacks.onRequest(pRI->pCI->requestNumber, &rcsm, sizeof(rcsm),pRI);
869
870#ifdef MEMSET_FREED
871    memset(&rcsm, 0, sizeof(rcsm));
872#endif
873
874    return;
875
876invalid:
877    invalidCommandBlock(pRI);
878    return;
879}
880
881static void
882dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI) {
883    RIL_CDMA_SMS_Ack rcsa;
884    int32_t  t;
885    status_t status;
886    int32_t digitCount;
887
888    memset(&rcsa, 0, sizeof(rcsa));
889
890    status = p.readInt32(&t);
891    rcsa.uErrorClass = (RIL_CDMA_SMS_ErrorClass) t;
892
893    status = p.readInt32(&t);
894    rcsa.uSMSCauseCode = (int) t;
895
896    if (status != NO_ERROR) {
897        goto invalid;
898    }
899
900    startRequest;
901    appendPrintBuf("%suErrorClass=%d, uTLStatus=%d, ",
902            printBuf, rcsa.uErrorClass, rcsa.uSMSCauseCode);
903    closeRequest;
904
905    printRequest(pRI->token, pRI->pCI->requestNumber);
906
907    s_callbacks.onRequest(pRI->pCI->requestNumber, &rcsa, sizeof(rcsa),pRI);
908
909#ifdef MEMSET_FREED
910    memset(&rcsa, 0, sizeof(rcsa));
911#endif
912
913    return;
914
915invalid:
916    invalidCommandBlock(pRI);
917    return;
918}
919
920static void
921dispatchGsmBrSmsCnf(Parcel &p, RequestInfo *pRI) {
922    int32_t t;
923    status_t status;
924    int32_t num;
925
926    status = p.readInt32(&num);
927    if (status != NO_ERROR) {
928        goto invalid;
929    }
930
931    RIL_GSM_BroadcastSmsConfigInfo gsmBci[num];
932    RIL_GSM_BroadcastSmsConfigInfo *gsmBciPtrs[num];
933
934    startRequest;
935    for (int i = 0 ; i < num ; i++ ) {
936        gsmBciPtrs[i] = &gsmBci[i];
937
938        status = p.readInt32(&t);
939        gsmBci[i].fromServiceId = (int) t;
940
941        status = p.readInt32(&t);
942        gsmBci[i].toServiceId = (int) t;
943
944        status = p.readInt32(&t);
945        gsmBci[i].fromCodeScheme = (int) t;
946
947        status = p.readInt32(&t);
948        gsmBci[i].toCodeScheme = (int) t;
949
950        status = p.readInt32(&t);
951        gsmBci[i].selected = (uint8_t) t;
952
953        appendPrintBuf("%s [%d: fromServiceId=%d, toServiceId =%d, \
954              fromCodeScheme=%d, toCodeScheme=%d, selected =%d]", printBuf, i,
955              gsmBci[i].fromServiceId, gsmBci[i].toServiceId,
956              gsmBci[i].fromCodeScheme, gsmBci[i].toCodeScheme,
957              gsmBci[i].selected);
958    }
959    closeRequest;
960
961    if (status != NO_ERROR) {
962        goto invalid;
963    }
964
965    s_callbacks.onRequest(pRI->pCI->requestNumber,
966                          gsmBciPtrs,
967                          num * sizeof(RIL_GSM_BroadcastSmsConfigInfo *),
968                          pRI);
969
970#ifdef MEMSET_FREED
971    memset(gsmBci, 0, num * sizeof(RIL_GSM_BroadcastSmsConfigInfo));
972    memset(gsmBciPtrs, 0, num * sizeof(RIL_GSM_BroadcastSmsConfigInfo *));
973#endif
974
975    return;
976
977invalid:
978    invalidCommandBlock(pRI);
979    return;
980}
981
982static void
983dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI) {
984    int32_t t;
985    status_t status;
986    int32_t num;
987
988    status = p.readInt32(&num);
989    if (status != NO_ERROR) {
990        goto invalid;
991    }
992
993    RIL_CDMA_BroadcastSmsConfigInfo cdmaBci[num];
994    RIL_CDMA_BroadcastSmsConfigInfo *cdmaBciPtrs[num];
995
996    startRequest;
997    for (int i = 0 ; i < num ; i++ ) {
998        cdmaBciPtrs[i] = &cdmaBci[i];
999
1000        status = p.readInt32(&t);
1001        cdmaBci[i].service_category = (int) t;
1002
1003        status = p.readInt32(&t);
1004        cdmaBci[i].language = (int) t;
1005
1006        status = p.readInt32(&t);
1007        cdmaBci[i].selected = (uint8_t) t;
1008
1009        appendPrintBuf("%s [%d: service_category=%d, language =%d, \
1010              entries.bSelected =%d]", printBuf, i, cdmaBci[i].service_category,
1011              cdmaBci[i].language, cdmaBci[i].selected);
1012    }
1013    closeRequest;
1014
1015    if (status != NO_ERROR) {
1016        goto invalid;
1017    }
1018
1019    s_callbacks.onRequest(pRI->pCI->requestNumber,
1020                          cdmaBciPtrs,
1021                          num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo *),
1022                          pRI);
1023
1024#ifdef MEMSET_FREED
1025    memset(cdmaBci, 0, num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo));
1026    memset(cdmaBciPtrs, 0, num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo *));
1027#endif
1028
1029    return;
1030
1031invalid:
1032    invalidCommandBlock(pRI);
1033    return;
1034}
1035
1036static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI) {
1037    RIL_CDMA_SMS_WriteArgs rcsw;
1038    int32_t  t;
1039    uint32_t ut;
1040    uint8_t  uct;
1041    status_t status;
1042    int32_t  digitCount;
1043
1044    memset(&rcsw, 0, sizeof(rcsw));
1045
1046    status = p.readInt32(&t);
1047    rcsw.status = t;
1048
1049    status = p.readInt32(&t);
1050    rcsw.message.uTeleserviceID = (int) t;
1051
1052    status = p.read(&uct,sizeof(uct));
1053    rcsw.message.bIsServicePresent = (uint8_t) uct;
1054
1055    status = p.readInt32(&t);
1056    rcsw.message.uServicecategory = (int) t;
1057
1058    status = p.readInt32(&t);
1059    rcsw.message.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) t;
1060
1061    status = p.readInt32(&t);
1062    rcsw.message.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) t;
1063
1064    status = p.readInt32(&t);
1065    rcsw.message.sAddress.number_type = (RIL_CDMA_SMS_NumberType) t;
1066
1067    status = p.readInt32(&t);
1068    rcsw.message.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) t;
1069
1070    status = p.read(&uct,sizeof(uct));
1071    rcsw.message.sAddress.number_of_digits = (uint8_t) uct;
1072
1073    for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_ADDRESS_MAX; digitCount ++) {
1074        status = p.read(&uct,sizeof(uct));
1075        rcsw.message.sAddress.digits[digitCount] = (uint8_t) uct;
1076    }
1077
1078    status = p.readInt32(&t);
1079    rcsw.message.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) t;
1080
1081    status = p.read(&uct,sizeof(uct));
1082    rcsw.message.sSubAddress.odd = (uint8_t) uct;
1083
1084    status = p.read(&uct,sizeof(uct));
1085    rcsw.message.sSubAddress.number_of_digits = (uint8_t) uct;
1086
1087    for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_SUBADDRESS_MAX; digitCount ++) {
1088        status = p.read(&uct,sizeof(uct));
1089        rcsw.message.sSubAddress.digits[digitCount] = (uint8_t) uct;
1090    }
1091
1092    status = p.readInt32(&t);
1093    rcsw.message.uBearerDataLen = (int) t;
1094
1095    for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_BEARER_DATA_MAX; digitCount ++) {
1096        status = p.read(&uct, sizeof(uct));
1097        rcsw.message.aBearerData[digitCount] = (uint8_t) uct;
1098    }
1099
1100    if (status != NO_ERROR) {
1101        goto invalid;
1102    }
1103
1104    startRequest;
1105    appendPrintBuf("%sstatus=%d, message.uTeleserviceID=%d, message.bIsServicePresent=%d, \
1106            message.uServicecategory=%d, message.sAddress.digit_mode=%d, \
1107            message.sAddress.number_mode=%d, \
1108            message.sAddress.number_type=%d, ",
1109            printBuf, rcsw.status, rcsw.message.uTeleserviceID, rcsw.message.bIsServicePresent,
1110            rcsw.message.uServicecategory, rcsw.message.sAddress.digit_mode,
1111            rcsw.message.sAddress.number_mode,
1112            rcsw.message.sAddress.number_type);
1113    closeRequest;
1114
1115    printRequest(pRI->token, pRI->pCI->requestNumber);
1116
1117    s_callbacks.onRequest(pRI->pCI->requestNumber, &rcsw, sizeof(rcsw),pRI);
1118
1119#ifdef MEMSET_FREED
1120    memset(&rcsw, 0, sizeof(rcsw));
1121#endif
1122
1123    return;
1124
1125invalid:
1126    invalidCommandBlock(pRI);
1127    return;
1128
1129}
1130
1131static int
1132blockingWrite(int fd, const void *buffer, size_t len) {
1133    size_t writeOffset = 0;
1134    const uint8_t *toWrite;
1135
1136    toWrite = (const uint8_t *)buffer;
1137
1138    while (writeOffset < len) {
1139        ssize_t written;
1140        do {
1141            written = write (fd, toWrite + writeOffset,
1142                                len - writeOffset);
1143        } while (written < 0 && errno == EINTR);
1144
1145        if (written >= 0) {
1146            writeOffset += written;
1147        } else {   // written < 0
1148            LOGE ("RIL Response: unexpected error on write errno:%d", errno);
1149            close(fd);
1150            return -1;
1151        }
1152    }
1153
1154    return 0;
1155}
1156
1157static int
1158sendResponseRaw (const void *data, size_t dataSize) {
1159    int fd = s_fdCommand;
1160    int ret;
1161    uint32_t header;
1162
1163    if (s_fdCommand < 0) {
1164        return -1;
1165    }
1166
1167    if (dataSize > MAX_COMMAND_BYTES) {
1168        LOGE("RIL: packet larger than %u (%u)",
1169                MAX_COMMAND_BYTES, (unsigned int )dataSize);
1170
1171        return -1;
1172    }
1173
1174
1175    // FIXME is blocking here ok? issue #550970
1176
1177    pthread_mutex_lock(&s_writeMutex);
1178
1179    header = htonl(dataSize);
1180
1181    ret = blockingWrite(fd, (void *)&header, sizeof(header));
1182
1183    if (ret < 0) {
1184        return ret;
1185    }
1186
1187    blockingWrite(fd, data, dataSize);
1188
1189    if (ret < 0) {
1190        return ret;
1191    }
1192
1193    pthread_mutex_unlock(&s_writeMutex);
1194
1195    return 0;
1196}
1197
1198static int
1199sendResponse (Parcel &p) {
1200    printResponse;
1201    return sendResponseRaw(p.data(), p.dataSize());
1202}
1203
1204/** response is an int* pointing to an array of ints*/
1205
1206static int
1207responseInts(Parcel &p, void *response, size_t responselen) {
1208    int numInts;
1209
1210    if (response == NULL && responselen != 0) {
1211        LOGE("invalid response: NULL");
1212        return RIL_ERRNO_INVALID_RESPONSE;
1213    }
1214    if (responselen % sizeof(int) != 0) {
1215        LOGE("invalid response length %d expected multiple of %d\n",
1216            (int)responselen, (int)sizeof(int));
1217        return RIL_ERRNO_INVALID_RESPONSE;
1218    }
1219
1220    int *p_int = (int *) response;
1221
1222    numInts = responselen / sizeof(int *);
1223    p.writeInt32 (numInts);
1224
1225    /* each int*/
1226    startResponse;
1227    for (int i = 0 ; i < numInts ; i++) {
1228        appendPrintBuf("%s%d,", printBuf, p_int[i]);
1229        p.writeInt32(p_int[i]);
1230    }
1231    removeLastChar;
1232    closeResponse;
1233
1234    return 0;
1235}
1236
1237/** response is a char **, pointing to an array of char *'s */
1238static int responseStrings(Parcel &p, void *response, size_t responselen) {
1239    int numStrings;
1240
1241    if (response == NULL && responselen != 0) {
1242        LOGE("invalid response: NULL");
1243        return RIL_ERRNO_INVALID_RESPONSE;
1244    }
1245    if (responselen % sizeof(char *) != 0) {
1246        LOGE("invalid response length %d expected multiple of %d\n",
1247            (int)responselen, (int)sizeof(char *));
1248        return RIL_ERRNO_INVALID_RESPONSE;
1249    }
1250
1251    if (response == NULL) {
1252        p.writeInt32 (0);
1253    } else {
1254        char **p_cur = (char **) response;
1255
1256        numStrings = responselen / sizeof(char *);
1257        p.writeInt32 (numStrings);
1258
1259        /* each string*/
1260        startResponse;
1261        for (int i = 0 ; i < numStrings ; i++) {
1262            appendPrintBuf("%s%s,", printBuf, (char*)p_cur[i]);
1263            writeStringToParcel (p, p_cur[i]);
1264        }
1265        removeLastChar;
1266        closeResponse;
1267    }
1268    return 0;
1269}
1270
1271
1272/**
1273 * NULL strings are accepted
1274 * FIXME currently ignores responselen
1275 */
1276static int responseString(Parcel &p, void *response, size_t responselen) {
1277    /* one string only */
1278    startResponse;
1279    appendPrintBuf("%s%s", printBuf, (char*)response);
1280    closeResponse;
1281
1282    writeStringToParcel(p, (const char *)response);
1283
1284    return 0;
1285}
1286
1287static int responseVoid(Parcel &p, void *response, size_t responselen) {
1288    startResponse;
1289    removeLastChar;
1290    return 0;
1291}
1292
1293static int responseCallList(Parcel &p, void *response, size_t responselen) {
1294    int num;
1295
1296    if (response == NULL && responselen != 0) {
1297        LOGE("invalid response: NULL");
1298        return RIL_ERRNO_INVALID_RESPONSE;
1299    }
1300
1301    if (responselen % sizeof (RIL_Call *) != 0) {
1302        LOGE("invalid response length %d expected multiple of %d\n",
1303            (int)responselen, (int)sizeof (RIL_Call *));
1304        return RIL_ERRNO_INVALID_RESPONSE;
1305    }
1306
1307    startResponse;
1308    /* number of call info's */
1309    num = responselen / sizeof(RIL_Call *);
1310    p.writeInt32(num);
1311
1312    for (int i = 0 ; i < num ; i++) {
1313        RIL_Call *p_cur = ((RIL_Call **) response)[i];
1314        /* each call info */
1315        p.writeInt32(p_cur->state);
1316        p.writeInt32(p_cur->index);
1317        p.writeInt32(p_cur->toa);
1318        p.writeInt32(p_cur->isMpty);
1319        p.writeInt32(p_cur->isMT);
1320        p.writeInt32(p_cur->als);
1321        p.writeInt32(p_cur->isVoice);
1322        p.writeInt32(p_cur->isVoicePrivacy);
1323        writeStringToParcel(p, p_cur->number);
1324        p.writeInt32(p_cur->numberPresentation);
1325        writeStringToParcel(p, p_cur->name);
1326        p.writeInt32(p_cur->namePresentation);
1327        appendPrintBuf("%s[id=%d,%s,toa=%d,",
1328            printBuf,
1329            p_cur->index,
1330            callStateToString(p_cur->state),
1331            p_cur->toa);
1332        appendPrintBuf("%s%s,%s,als=%d,%s,%s,",
1333            printBuf,
1334            (p_cur->isMpty)?"conf":"norm",
1335            (p_cur->isMT)?"mt":"mo",
1336            p_cur->als,
1337            (p_cur->isVoice)?"voc":"nonvoc",
1338            (p_cur->isVoicePrivacy)?"evp":"noevp");
1339        appendPrintBuf("%s%s,cli=%d,name='%s',%d]",
1340            printBuf,
1341            p_cur->number,
1342            p_cur->numberPresentation,
1343            p_cur->name,
1344            p_cur->namePresentation);
1345    }
1346    removeLastChar;
1347    closeResponse;
1348
1349    return 0;
1350}
1351
1352static int responseSMS(Parcel &p, void *response, size_t responselen) {
1353    if (response == NULL) {
1354        LOGE("invalid response: NULL");
1355        return RIL_ERRNO_INVALID_RESPONSE;
1356    }
1357
1358    if (responselen != sizeof (RIL_SMS_Response) ) {
1359        LOGE("invalid response length %d expected %d",
1360                (int)responselen, (int)sizeof (RIL_SMS_Response));
1361        return RIL_ERRNO_INVALID_RESPONSE;
1362    }
1363
1364    RIL_SMS_Response *p_cur = (RIL_SMS_Response *) response;
1365
1366    p.writeInt32(p_cur->messageRef);
1367    writeStringToParcel(p, p_cur->ackPDU);
1368    p.writeInt32(p_cur->errorCode);
1369
1370    startResponse;
1371    appendPrintBuf("%s%d,%s,%d", printBuf, p_cur->messageRef,
1372        (char*)p_cur->ackPDU, p_cur->errorCode);
1373    closeResponse;
1374
1375    return 0;
1376}
1377
1378static int responseDataCallList(Parcel &p, void *response, size_t responselen)
1379{
1380    if (response == NULL && responselen != 0) {
1381        LOGE("invalid response: NULL");
1382        return RIL_ERRNO_INVALID_RESPONSE;
1383    }
1384
1385    if (responselen % sizeof(RIL_Data_Call_Response) != 0) {
1386        LOGE("invalid response length %d expected multiple of %d",
1387                (int)responselen, (int)sizeof(RIL_Data_Call_Response));
1388        return RIL_ERRNO_INVALID_RESPONSE;
1389    }
1390
1391    int num = responselen / sizeof(RIL_Data_Call_Response);
1392    p.writeInt32(num);
1393
1394    RIL_Data_Call_Response *p_cur = (RIL_Data_Call_Response *) response;
1395    startResponse;
1396    int i;
1397    for (i = 0; i < num; i++) {
1398        p.writeInt32(p_cur[i].cid);
1399        p.writeInt32(p_cur[i].active);
1400        writeStringToParcel(p, p_cur[i].type);
1401        writeStringToParcel(p, p_cur[i].apn);
1402        writeStringToParcel(p, p_cur[i].address);
1403        appendPrintBuf("%s[cid=%d,%s,%s,%s,%s],", printBuf,
1404            p_cur[i].cid,
1405            (p_cur[i].active==0)?"down":"up",
1406            (char*)p_cur[i].type,
1407            (char*)p_cur[i].apn,
1408            (char*)p_cur[i].address);
1409    }
1410    removeLastChar;
1411    closeResponse;
1412
1413    return 0;
1414}
1415
1416static int responseRaw(Parcel &p, void *response, size_t responselen) {
1417    if (response == NULL && responselen != 0) {
1418        LOGE("invalid response: NULL with responselen != 0");
1419        return RIL_ERRNO_INVALID_RESPONSE;
1420    }
1421
1422    // The java code reads -1 size as null byte array
1423    if (response == NULL) {
1424        p.writeInt32(-1);
1425    } else {
1426        p.writeInt32(responselen);
1427        p.write(response, responselen);
1428    }
1429
1430    return 0;
1431}
1432
1433
1434static int responseSIM_IO(Parcel &p, void *response, size_t responselen) {
1435    if (response == NULL) {
1436        LOGE("invalid response: NULL");
1437        return RIL_ERRNO_INVALID_RESPONSE;
1438    }
1439
1440    if (responselen != sizeof (RIL_SIM_IO_Response) ) {
1441        LOGE("invalid response length was %d expected %d",
1442                (int)responselen, (int)sizeof (RIL_SIM_IO_Response));
1443        return RIL_ERRNO_INVALID_RESPONSE;
1444    }
1445
1446    RIL_SIM_IO_Response *p_cur = (RIL_SIM_IO_Response *) response;
1447    p.writeInt32(p_cur->sw1);
1448    p.writeInt32(p_cur->sw2);
1449    writeStringToParcel(p, p_cur->simResponse);
1450
1451    startResponse;
1452    appendPrintBuf("%ssw1=0x%X,sw2=0x%X,%s", printBuf, p_cur->sw1, p_cur->sw2,
1453        (char*)p_cur->simResponse);
1454    closeResponse;
1455
1456
1457    return 0;
1458}
1459
1460static int responseCallForwards(Parcel &p, void *response, size_t responselen) {
1461    int num;
1462
1463    if (response == NULL && responselen != 0) {
1464        LOGE("invalid response: NULL");
1465        return RIL_ERRNO_INVALID_RESPONSE;
1466    }
1467
1468    if (responselen % sizeof(RIL_CallForwardInfo *) != 0) {
1469        LOGE("invalid response length %d expected multiple of %d",
1470                (int)responselen, (int)sizeof(RIL_CallForwardInfo *));
1471        return RIL_ERRNO_INVALID_RESPONSE;
1472    }
1473
1474    /* number of call info's */
1475    num = responselen / sizeof(RIL_CallForwardInfo *);
1476    p.writeInt32(num);
1477
1478    startResponse;
1479    for (int i = 0 ; i < num ; i++) {
1480        RIL_CallForwardInfo *p_cur = ((RIL_CallForwardInfo **) response)[i];
1481
1482        p.writeInt32(p_cur->status);
1483        p.writeInt32(p_cur->reason);
1484        p.writeInt32(p_cur->serviceClass);
1485        p.writeInt32(p_cur->toa);
1486        writeStringToParcel(p, p_cur->number);
1487        p.writeInt32(p_cur->timeSeconds);
1488        appendPrintBuf("%s[%s,reason=%d,cls=%d,toa=%d,%s,tout=%d],", printBuf,
1489            (p_cur->status==1)?"enable":"disable",
1490            p_cur->reason, p_cur->serviceClass, p_cur->toa,
1491            (char*)p_cur->number,
1492            p_cur->timeSeconds);
1493    }
1494    removeLastChar;
1495    closeResponse;
1496
1497    return 0;
1498}
1499
1500static int responseSsn(Parcel &p, void *response, size_t responselen) {
1501    if (response == NULL) {
1502        LOGE("invalid response: NULL");
1503        return RIL_ERRNO_INVALID_RESPONSE;
1504    }
1505
1506    if (responselen != sizeof(RIL_SuppSvcNotification)) {
1507        LOGE("invalid response length was %d expected %d",
1508                (int)responselen, (int)sizeof (RIL_SuppSvcNotification));
1509        return RIL_ERRNO_INVALID_RESPONSE;
1510    }
1511
1512    RIL_SuppSvcNotification *p_cur = (RIL_SuppSvcNotification *) response;
1513    p.writeInt32(p_cur->notificationType);
1514    p.writeInt32(p_cur->code);
1515    p.writeInt32(p_cur->index);
1516    p.writeInt32(p_cur->type);
1517    writeStringToParcel(p, p_cur->number);
1518
1519    startResponse;
1520    appendPrintBuf("%s%s,code=%d,id=%d,type=%d,%s", printBuf,
1521        (p_cur->notificationType==0)?"mo":"mt",
1522         p_cur->code, p_cur->index, p_cur->type,
1523        (char*)p_cur->number);
1524    closeResponse;
1525
1526    return 0;
1527}
1528
1529static int responseCellList(Parcel &p, void *response, size_t responselen) {
1530    int num;
1531
1532    if (response == NULL && responselen != 0) {
1533        LOGE("invalid response: NULL");
1534        return RIL_ERRNO_INVALID_RESPONSE;
1535    }
1536
1537    if (responselen % sizeof (RIL_NeighboringCell *) != 0) {
1538        LOGE("invalid response length %d expected multiple of %d\n",
1539            (int)responselen, (int)sizeof (RIL_NeighboringCell *));
1540        return RIL_ERRNO_INVALID_RESPONSE;
1541    }
1542
1543    startResponse;
1544    /* number of records */
1545    num = responselen / sizeof(RIL_NeighboringCell *);
1546    p.writeInt32(num);
1547
1548    for (int i = 0 ; i < num ; i++) {
1549        RIL_NeighboringCell *p_cur = ((RIL_NeighboringCell **) response)[i];
1550
1551        p.writeInt32(p_cur->rssi);
1552        writeStringToParcel (p, p_cur->cid);
1553
1554        appendPrintBuf("%s[cid=%s,rssi=%d],", printBuf,
1555            p_cur->cid, p_cur->rssi);
1556    }
1557    removeLastChar;
1558    closeResponse;
1559
1560    return 0;
1561}
1562
1563/**
1564 * Marshall the signalInfoRecord into the parcel if it exists.
1565 */
1566static void marshallSignalInfoRecord(Parcel &p,
1567            RIL_CDMA_SignalInfoRecord &p_signalInfoRecord) {
1568    p.writeInt32(p_signalInfoRecord.isPresent);
1569    p.writeInt32(p_signalInfoRecord.signalType);
1570    p.writeInt32(p_signalInfoRecord.alertPitch);
1571    p.writeInt32(p_signalInfoRecord.signal);
1572}
1573
1574static int responseCdmaInformationRecords(Parcel &p,
1575            void *response, size_t responselen) {
1576    int num;
1577    char* string8 = NULL;
1578    int buffer_lenght;
1579    RIL_CDMA_InformationRecord *infoRec;
1580
1581    if (response == NULL && responselen != 0) {
1582        LOGE("invalid response: NULL");
1583        return RIL_ERRNO_INVALID_RESPONSE;
1584    }
1585
1586    if (responselen != sizeof (RIL_CDMA_InformationRecords)) {
1587        LOGE("invalid response length %d expected multiple of %d\n",
1588            (int)responselen, (int)sizeof (RIL_CDMA_InformationRecords *));
1589        return RIL_ERRNO_INVALID_RESPONSE;
1590    }
1591
1592    RIL_CDMA_InformationRecords *p_cur =
1593                             (RIL_CDMA_InformationRecords *) response;
1594    num = MIN(p_cur->numberOfInfoRecs, RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
1595
1596    startResponse;
1597    p.writeInt32(num);
1598
1599    for (int i = 0 ; i < num ; i++) {
1600        infoRec = &p_cur->infoRec[i];
1601        p.writeInt32(infoRec->name);
1602        switch (infoRec->name) {
1603            case RIL_CDMA_DISPLAY_INFO_REC:
1604            case RIL_CDMA_EXTENDED_DISPLAY_INFO_REC:
1605                if (infoRec->rec.display.alpha_len >
1606                                         CDMA_ALPHA_INFO_BUFFER_LENGTH) {
1607                    LOGE("invalid display info response length %d \
1608                          expected not more than %d\n",
1609                         (int)infoRec->rec.display.alpha_len,
1610                         CDMA_ALPHA_INFO_BUFFER_LENGTH);
1611                    return RIL_ERRNO_INVALID_RESPONSE;
1612                }
1613                string8 = (char*) malloc((infoRec->rec.display.alpha_len + 1)
1614                                                             * sizeof(char) );
1615                for (int i = 0 ; i < infoRec->rec.display.alpha_len ; i++) {
1616                    string8[i] = infoRec->rec.display.alpha_buf[i];
1617                }
1618                string8[infoRec->rec.display.alpha_len] = '\0';
1619                writeStringToParcel(p, (const char*)string8);
1620                free(string8);
1621                string8 = NULL;
1622                break;
1623            case RIL_CDMA_CALLED_PARTY_NUMBER_INFO_REC:
1624            case RIL_CDMA_CALLING_PARTY_NUMBER_INFO_REC:
1625            case RIL_CDMA_CONNECTED_NUMBER_INFO_REC:
1626                if (infoRec->rec.number.len > CDMA_NUMBER_INFO_BUFFER_LENGTH) {
1627                    LOGE("invalid display info response length %d \
1628                          expected not more than %d\n",
1629                         (int)infoRec->rec.number.len,
1630                         CDMA_NUMBER_INFO_BUFFER_LENGTH);
1631                    return RIL_ERRNO_INVALID_RESPONSE;
1632                }
1633                string8 = (char*) malloc((infoRec->rec.number.len + 1)
1634                                                             * sizeof(char) );
1635                for (int i = 0 ; i < infoRec->rec.number.len; i++) {
1636                    string8[i] = infoRec->rec.number.buf[i];
1637                }
1638                string8[infoRec->rec.number.len] = '\0';
1639                writeStringToParcel(p, (const char*)string8);
1640                free(string8);
1641                string8 = NULL;
1642                p.writeInt32(infoRec->rec.number.number_type);
1643                p.writeInt32(infoRec->rec.number.number_plan);
1644                p.writeInt32(infoRec->rec.number.pi);
1645                p.writeInt32(infoRec->rec.number.si);
1646                break;
1647            case RIL_CDMA_SIGNAL_INFO_REC:
1648                p.writeInt32(infoRec->rec.signal.isPresent);
1649                p.writeInt32(infoRec->rec.signal.signalType);
1650                p.writeInt32(infoRec->rec.signal.alertPitch);
1651                p.writeInt32(infoRec->rec.signal.signal);
1652
1653                appendPrintBuf("%sisPresent=%X, signalType=%X, \
1654                                alertPitch=%X, signal=%X, ",
1655                   printBuf, (int)infoRec->rec.signal.isPresent,
1656                   (int)infoRec->rec.signal.signalType,
1657                   (int)infoRec->rec.signal.alertPitch,
1658                   (int)infoRec->rec.signal.signal);
1659                removeLastChar;
1660                break;
1661            case RIL_CDMA_REDIRECTING_NUMBER_INFO_REC:
1662                if (infoRec->rec.redir.redirectingNumber.len >
1663                                              CDMA_NUMBER_INFO_BUFFER_LENGTH) {
1664                    LOGE("invalid display info response length %d \
1665                          expected not more than %d\n",
1666                         (int)infoRec->rec.redir.redirectingNumber.len,
1667                         CDMA_NUMBER_INFO_BUFFER_LENGTH);
1668                    return RIL_ERRNO_INVALID_RESPONSE;
1669                }
1670                string8 = (char*) malloc((infoRec->rec.redir.redirectingNumber
1671                                          .len + 1) * sizeof(char) );
1672                for (int i = 0;
1673                         i < infoRec->rec.redir.redirectingNumber.len;
1674                         i++) {
1675                    string8[i] = infoRec->rec.redir.redirectingNumber.buf[i];
1676                }
1677                string8[infoRec->rec.redir.redirectingNumber.len] = '\0';
1678                writeStringToParcel(p, (const char*)string8);
1679                free(string8);
1680                string8 = NULL;
1681                p.writeInt32(infoRec->rec.redir.redirectingNumber.number_type);
1682                p.writeInt32(infoRec->rec.redir.redirectingNumber.number_plan);
1683                p.writeInt32(infoRec->rec.redir.redirectingNumber.pi);
1684                p.writeInt32(infoRec->rec.redir.redirectingNumber.si);
1685                p.writeInt32(infoRec->rec.redir.redirectingReason);
1686                break;
1687            case RIL_CDMA_LINE_CONTROL_INFO_REC:
1688                p.writeInt32(infoRec->rec.lineCtrl.lineCtrlPolarityIncluded);
1689                p.writeInt32(infoRec->rec.lineCtrl.lineCtrlToggle);
1690                p.writeInt32(infoRec->rec.lineCtrl.lineCtrlReverse);
1691                p.writeInt32(infoRec->rec.lineCtrl.lineCtrlPowerDenial);
1692
1693                appendPrintBuf("%slineCtrlPolarityIncluded=%d, \
1694                                lineCtrlToggle=%d, lineCtrlReverse=%d, \
1695                                lineCtrlPowerDenial=%d, ", printBuf,
1696                       (int)infoRec->rec.lineCtrl.lineCtrlPolarityIncluded,
1697                       (int)infoRec->rec.lineCtrl.lineCtrlToggle,
1698                       (int)infoRec->rec.lineCtrl.lineCtrlReverse,
1699                       (int)infoRec->rec.lineCtrl.lineCtrlPowerDenial);
1700                removeLastChar;
1701                break;
1702            case RIL_CDMA_T53_CLIR_INFO_REC:
1703                p.writeInt32((int)(infoRec->rec.clir.cause));
1704
1705                appendPrintBuf("%scause%d", printBuf, infoRec->rec.clir.cause);
1706                removeLastChar;
1707                break;
1708            case RIL_CDMA_T53_AUDIO_CONTROL_INFO_REC:
1709                p.writeInt32(infoRec->rec.audioCtrl.upLink);
1710                p.writeInt32(infoRec->rec.audioCtrl.downLink);
1711
1712                appendPrintBuf("%supLink=%d, downLink=%d, ", printBuf,
1713                        infoRec->rec.audioCtrl.upLink,
1714                        infoRec->rec.audioCtrl.downLink);
1715                removeLastChar;
1716                break;
1717            case RIL_CDMA_T53_RELEASE_INFO_REC:
1718                // TODO(Moto): See David Krause, he has the answer:)
1719                LOGE("RIL_CDMA_T53_RELEASE_INFO_REC: return INVALID_RESPONSE");
1720                return RIL_ERRNO_INVALID_RESPONSE;
1721            default:
1722                LOGE("Incorrect name value");
1723                return RIL_ERRNO_INVALID_RESPONSE;
1724        }
1725    }
1726    closeResponse;
1727
1728    return 0;
1729}
1730
1731static int responseRilSignalStrength(Parcel &p,
1732                    void *response, size_t responselen) {
1733    if (response == NULL && responselen != 0) {
1734        LOGE("invalid response: NULL");
1735        return RIL_ERRNO_INVALID_RESPONSE;
1736    }
1737
1738    if (responselen == sizeof (RIL_SignalStrength)) {
1739        // New RIL
1740        RIL_SignalStrength *p_cur = ((RIL_SignalStrength *) response);
1741
1742        p.writeInt32(p_cur->GW_SignalStrength.signalStrength);
1743        p.writeInt32(p_cur->GW_SignalStrength.bitErrorRate);
1744        p.writeInt32(p_cur->CDMA_SignalStrength.dbm);
1745        p.writeInt32(p_cur->CDMA_SignalStrength.ecio);
1746        p.writeInt32(p_cur->EVDO_SignalStrength.dbm);
1747        p.writeInt32(p_cur->EVDO_SignalStrength.ecio);
1748        p.writeInt32(p_cur->EVDO_SignalStrength.signalNoiseRatio);
1749
1750        startResponse;
1751        appendPrintBuf("%s[signalStrength=%d,bitErrorRate=%d,\
1752                CDMA_SignalStrength.dbm=%d,CDMA_SignalStrength.ecio=%d,\
1753                EVDO_SignalStrength.dbm =%d,EVDO_SignalStrength.ecio=%d,\
1754                EVDO_SignalStrength.signalNoiseRatio=%d]",
1755                printBuf,
1756                p_cur->GW_SignalStrength.signalStrength,
1757                p_cur->GW_SignalStrength.bitErrorRate,
1758                p_cur->CDMA_SignalStrength.dbm,
1759                p_cur->CDMA_SignalStrength.ecio,
1760                p_cur->EVDO_SignalStrength.dbm,
1761                p_cur->EVDO_SignalStrength.ecio,
1762                p_cur->EVDO_SignalStrength.signalNoiseRatio);
1763
1764        closeResponse;
1765
1766    } else if (responselen % sizeof (int) == 0) {
1767        // Old RIL deprecated
1768        int *p_cur = (int *) response;
1769
1770        startResponse;
1771
1772        // With the Old RIL we see one or 2 integers.
1773        size_t num = responselen / sizeof (int); // Number of integers from ril
1774        size_t totalIntegers = 7; // Number of integers in RIL_SignalStrength
1775        size_t i;
1776
1777        appendPrintBuf("%s[", printBuf);
1778        for (i = 0; i < num; i++) {
1779            appendPrintBuf("%s %d", printBuf, *p_cur);
1780            p.writeInt32(*p_cur++);
1781        }
1782        appendPrintBuf("%s]", printBuf);
1783
1784        // Fill the remainder with zero's.
1785        for (; i < totalIntegers; i++) {
1786            p.writeInt32(0);
1787        }
1788
1789        closeResponse;
1790    } else {
1791        LOGE("invalid response length");
1792        return RIL_ERRNO_INVALID_RESPONSE;
1793    }
1794
1795    return 0;
1796}
1797
1798static int responseCallRing(Parcel &p, void *response, size_t responselen) {
1799    if ((response == NULL) || (responselen == 0)) {
1800        return responseVoid(p, response, responselen);
1801    } else {
1802        return responseCdmaSignalInfoRecord(p, response, responselen);
1803    }
1804}
1805
1806static int responseCdmaSignalInfoRecord(Parcel &p, void *response, size_t responselen) {
1807    if (response == NULL || responselen == 0) {
1808        LOGE("invalid response: NULL");
1809        return RIL_ERRNO_INVALID_RESPONSE;
1810    }
1811
1812    if (responselen != sizeof (RIL_CDMA_SignalInfoRecord)) {
1813        LOGE("invalid response length %d expected sizeof (RIL_CDMA_SignalInfoRecord) of %d\n",
1814            (int)responselen, (int)sizeof (RIL_CDMA_SignalInfoRecord));
1815        return RIL_ERRNO_INVALID_RESPONSE;
1816    }
1817
1818    startResponse;
1819
1820    RIL_CDMA_SignalInfoRecord *p_cur = ((RIL_CDMA_SignalInfoRecord *) response);
1821    marshallSignalInfoRecord(p, *p_cur);
1822
1823    appendPrintBuf("%s[isPresent=%d,signalType=%d,alertPitch=%d\
1824              signal=%d]",
1825              printBuf,
1826              p_cur->isPresent,
1827              p_cur->signalType,
1828              p_cur->alertPitch,
1829              p_cur->signal);
1830
1831    closeResponse;
1832    return 0;
1833}
1834
1835static int responseCdmaCallWaiting(Parcel &p, void *response,
1836            size_t responselen) {
1837    if (response == NULL && responselen != 0) {
1838        LOGE("invalid response: NULL");
1839        return RIL_ERRNO_INVALID_RESPONSE;
1840    }
1841
1842    if (responselen != sizeof(RIL_CDMA_CallWaiting)) {
1843        LOGE("invalid response length %d expected %d\n",
1844            (int)responselen, (int)sizeof(RIL_CDMA_CallWaiting));
1845        return RIL_ERRNO_INVALID_RESPONSE;
1846    }
1847
1848    startResponse;
1849    RIL_CDMA_CallWaiting *p_cur = ((RIL_CDMA_CallWaiting *) response);
1850
1851    writeStringToParcel (p, p_cur->number);
1852    p.writeInt32(p_cur->numberPresentation);
1853    writeStringToParcel (p, p_cur->name);
1854    marshallSignalInfoRecord(p, p_cur->signalInfoRecord);
1855
1856    appendPrintBuf("%snumber=%s,numberPresentation=%d, name=%s,\
1857            signalInfoRecord[isPresent=%d,signalType=%d,alertPitch=%d\
1858            signal=%d]",
1859            printBuf,
1860            p_cur->number,
1861            p_cur->numberPresentation,
1862            p_cur->name,
1863            p_cur->signalInfoRecord.isPresent,
1864            p_cur->signalInfoRecord.signalType,
1865            p_cur->signalInfoRecord.alertPitch,
1866            p_cur->signalInfoRecord.signal);
1867
1868    closeResponse;
1869
1870    return 0;
1871}
1872
1873static void triggerEvLoop() {
1874    int ret;
1875    if (!pthread_equal(pthread_self(), s_tid_dispatch)) {
1876        /* trigger event loop to wakeup. No reason to do this,
1877         * if we're in the event loop thread */
1878         do {
1879            ret = write (s_fdWakeupWrite, " ", 1);
1880         } while (ret < 0 && errno == EINTR);
1881    }
1882}
1883
1884static void rilEventAddWakeup(struct ril_event *ev) {
1885    ril_event_add(ev);
1886    triggerEvLoop();
1887}
1888
1889static int responseSimStatus(Parcel &p, void *response, size_t responselen) {
1890    int i;
1891
1892    if (response == NULL && responselen != 0) {
1893        LOGE("invalid response: NULL");
1894        return RIL_ERRNO_INVALID_RESPONSE;
1895    }
1896
1897    if (responselen % sizeof (RIL_CardStatus *) != 0) {
1898        LOGE("invalid response length %d expected multiple of %d\n",
1899            (int)responselen, (int)sizeof (RIL_CardStatus *));
1900        return RIL_ERRNO_INVALID_RESPONSE;
1901    }
1902
1903    RIL_CardStatus *p_cur = ((RIL_CardStatus *) response);
1904
1905    p.writeInt32(p_cur->card_state);
1906    p.writeInt32(p_cur->universal_pin_state);
1907    p.writeInt32(p_cur->gsm_umts_subscription_app_index);
1908    p.writeInt32(p_cur->cdma_subscription_app_index);
1909    p.writeInt32(p_cur->num_applications);
1910
1911    startResponse;
1912    for (i = 0; i < p_cur->num_applications; i++) {
1913        p.writeInt32(p_cur->applications[i].app_type);
1914        p.writeInt32(p_cur->applications[i].app_state);
1915        p.writeInt32(p_cur->applications[i].perso_substate);
1916        writeStringToParcel(p, (const char*)(p_cur->applications[i].aid_ptr));
1917        writeStringToParcel(p, (const char*)
1918                                      (p_cur->applications[i].app_label_ptr));
1919        p.writeInt32(p_cur->applications[i].pin1_replaced);
1920        p.writeInt32(p_cur->applications[i].pin1);
1921        p.writeInt32(p_cur->applications[i].pin2);
1922        appendPrintBuf("%s[app_type=%d,app_state=%d,perso_substate=%d,\
1923                aid_ptr=%s,app_label_ptr=%s,pin1_replaced=%d,pin1=%d,pin2=%d],",
1924                printBuf,
1925                p_cur->applications[i].app_type,
1926                p_cur->applications[i].app_state,
1927                p_cur->applications[i].perso_substate,
1928                p_cur->applications[i].aid_ptr,
1929                p_cur->applications[i].app_label_ptr,
1930                p_cur->applications[i].pin1_replaced,
1931                p_cur->applications[i].pin1,
1932                p_cur->applications[i].pin2);
1933    }
1934    closeResponse;
1935
1936    return 0;
1937}
1938
1939static int responseGsmBrSmsCnf(Parcel &p, void *response, size_t responselen) {
1940    int num = responselen / sizeof(RIL_GSM_BroadcastSmsConfigInfo *);
1941    p.writeInt32(num);
1942
1943    startResponse;
1944    RIL_GSM_BroadcastSmsConfigInfo **p_cur =
1945                (RIL_GSM_BroadcastSmsConfigInfo **) response;
1946    for (int i = 0; i < num; i++) {
1947        p.writeInt32(p_cur[i]->fromServiceId);
1948        p.writeInt32(p_cur[i]->toServiceId);
1949        p.writeInt32(p_cur[i]->fromCodeScheme);
1950        p.writeInt32(p_cur[i]->toCodeScheme);
1951        p.writeInt32(p_cur[i]->selected);
1952
1953        appendPrintBuf("%s [%d: fromServiceId=%d, toServiceId=%d, \
1954                fromCodeScheme=%d, toCodeScheme=%d, selected =%d]",
1955                printBuf, i, p_cur[i]->fromServiceId, p_cur[i]->toServiceId,
1956                p_cur[i]->fromCodeScheme, p_cur[i]->toCodeScheme,
1957                p_cur[i]->selected);
1958    }
1959    closeResponse;
1960
1961    return 0;
1962}
1963
1964static int responseCdmaBrSmsCnf(Parcel &p, void *response, size_t responselen) {
1965    RIL_CDMA_BroadcastSmsConfigInfo **p_cur =
1966               (RIL_CDMA_BroadcastSmsConfigInfo **) response;
1967
1968    int num = responselen / sizeof (RIL_CDMA_BroadcastSmsConfigInfo *);
1969    p.writeInt32(num);
1970
1971    startResponse;
1972    for (int i = 0 ; i < num ; i++ ) {
1973        p.writeInt32(p_cur[i]->service_category);
1974        p.writeInt32(p_cur[i]->language);
1975        p.writeInt32(p_cur[i]->selected);
1976
1977        appendPrintBuf("%s [%d: srvice_category=%d, language =%d, \
1978              selected =%d], ",
1979              printBuf, i, p_cur[i]->service_category, p_cur[i]->language,
1980              p_cur[i]->selected);
1981    }
1982    closeResponse;
1983
1984    return 0;
1985}
1986
1987static int responseCdmaSms(Parcel &p, void *response, size_t responselen) {
1988    int num;
1989    int digitCount;
1990    int digitLimit;
1991    uint8_t uct;
1992    void* dest;
1993
1994    LOGD("Inside responseCdmaSms");
1995
1996    if (response == NULL && responselen != 0) {
1997        LOGE("invalid response: NULL");
1998        return RIL_ERRNO_INVALID_RESPONSE;
1999    }
2000
2001    if (responselen != sizeof(RIL_CDMA_SMS_Message)) {
2002        LOGE("invalid response length was %d expected %d",
2003                (int)responselen, (int)sizeof(RIL_CDMA_SMS_Message));
2004        return RIL_ERRNO_INVALID_RESPONSE;
2005    }
2006
2007    RIL_CDMA_SMS_Message *p_cur = (RIL_CDMA_SMS_Message *) response;
2008    p.writeInt32(p_cur->uTeleserviceID);
2009    p.write(&(p_cur->bIsServicePresent),sizeof(uct));
2010    p.writeInt32(p_cur->uServicecategory);
2011    p.writeInt32(p_cur->sAddress.digit_mode);
2012    p.writeInt32(p_cur->sAddress.number_mode);
2013    p.writeInt32(p_cur->sAddress.number_type);
2014    p.writeInt32(p_cur->sAddress.number_plan);
2015    p.write(&(p_cur->sAddress.number_of_digits), sizeof(uct));
2016    digitLimit= MIN((p_cur->sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
2017    for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
2018        p.write(&(p_cur->sAddress.digits[digitCount]),sizeof(uct));
2019    }
2020
2021    p.writeInt32(p_cur->sSubAddress.subaddressType);
2022    p.write(&(p_cur->sSubAddress.odd),sizeof(uct));
2023    p.write(&(p_cur->sSubAddress.number_of_digits),sizeof(uct));
2024    digitLimit= MIN((p_cur->sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
2025    for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
2026        p.write(&(p_cur->sSubAddress.digits[digitCount]),sizeof(uct));
2027    }
2028
2029    digitLimit= MIN((p_cur->uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
2030    p.writeInt32(p_cur->uBearerDataLen);
2031    for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
2032       p.write(&(p_cur->aBearerData[digitCount]), sizeof(uct));
2033    }
2034
2035    startResponse;
2036    appendPrintBuf("%suTeleserviceID=%d, bIsServicePresent=%d, uServicecategory=%d, \
2037            sAddress.digit_mode=%d, sAddress.number_mode=%d, sAddress.number_type=%d, ",
2038            printBuf, p_cur->uTeleserviceID,p_cur->bIsServicePresent,p_cur->uServicecategory,
2039            p_cur->sAddress.digit_mode, p_cur->sAddress.number_mode,p_cur->sAddress.number_type);
2040    closeResponse;
2041
2042    return 0;
2043}
2044
2045/**
2046 * A write on the wakeup fd is done just to pop us out of select()
2047 * We empty the buffer here and then ril_event will reset the timers on the
2048 * way back down
2049 */
2050static void processWakeupCallback(int fd, short flags, void *param) {
2051    char buff[16];
2052    int ret;
2053
2054    LOGV("processWakeupCallback");
2055
2056    /* empty our wakeup socket out */
2057    do {
2058        ret = read(s_fdWakeupRead, &buff, sizeof(buff));
2059    } while (ret > 0 || (ret < 0 && errno == EINTR));
2060}
2061
2062static void onCommandsSocketClosed() {
2063    int ret;
2064    RequestInfo *p_cur;
2065
2066    /* mark pending requests as "cancelled" so we dont report responses */
2067
2068    ret = pthread_mutex_lock(&s_pendingRequestsMutex);
2069    assert (ret == 0);
2070
2071    p_cur = s_pendingRequests;
2072
2073    for (p_cur = s_pendingRequests
2074            ; p_cur != NULL
2075            ; p_cur  = p_cur->p_next
2076    ) {
2077        p_cur->cancelled = 1;
2078    }
2079
2080    ret = pthread_mutex_unlock(&s_pendingRequestsMutex);
2081    assert (ret == 0);
2082}
2083
2084static void processCommandsCallback(int fd, short flags, void *param) {
2085    RecordStream *p_rs;
2086    void *p_record;
2087    size_t recordlen;
2088    int ret;
2089
2090    assert(fd == s_fdCommand);
2091
2092    p_rs = (RecordStream *)param;
2093
2094    for (;;) {
2095        /* loop until EAGAIN/EINTR, end of stream, or other error */
2096        ret = record_stream_get_next(p_rs, &p_record, &recordlen);
2097
2098        if (ret == 0 && p_record == NULL) {
2099            /* end-of-stream */
2100            break;
2101        } else if (ret < 0) {
2102            break;
2103        } else if (ret == 0) { /* && p_record != NULL */
2104            processCommandBuffer(p_record, recordlen);
2105        }
2106    }
2107
2108    if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) {
2109        /* fatal error or end-of-stream */
2110        if (ret != 0) {
2111            LOGE("error on reading command socket errno:%d\n", errno);
2112        } else {
2113            LOGW("EOS.  Closing command socket.");
2114        }
2115
2116        close(s_fdCommand);
2117        s_fdCommand = -1;
2118
2119        ril_event_del(&s_commands_event);
2120
2121        record_stream_free(p_rs);
2122
2123        /* start listening for new connections again */
2124        rilEventAddWakeup(&s_listen_event);
2125
2126        onCommandsSocketClosed();
2127    }
2128}
2129
2130
2131static void onNewCommandConnect() {
2132    // implicit radio state changed
2133    RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2134                                    NULL, 0);
2135
2136    // Send last NITZ time data, in case it was missed
2137    if (s_lastNITZTimeData != NULL) {
2138        sendResponseRaw(s_lastNITZTimeData, s_lastNITZTimeDataSize);
2139
2140        free(s_lastNITZTimeData);
2141        s_lastNITZTimeData = NULL;
2142    }
2143
2144    // Get version string
2145    if (s_callbacks.getVersion != NULL) {
2146        const char *version;
2147        version = s_callbacks.getVersion();
2148        LOGI("RIL Daemon version: %s\n", version);
2149
2150        property_set(PROPERTY_RIL_IMPL, version);
2151    } else {
2152        LOGI("RIL Daemon version: unavailable\n");
2153        property_set(PROPERTY_RIL_IMPL, "unavailable");
2154    }
2155
2156}
2157
2158static void listenCallback (int fd, short flags, void *param) {
2159    int ret;
2160    int err;
2161    int is_phone_socket;
2162    RecordStream *p_rs;
2163
2164    struct sockaddr_un peeraddr;
2165    socklen_t socklen = sizeof (peeraddr);
2166
2167    struct ucred creds;
2168    socklen_t szCreds = sizeof(creds);
2169
2170    struct passwd *pwd = NULL;
2171
2172    assert (s_fdCommand < 0);
2173    assert (fd == s_fdListen);
2174
2175    s_fdCommand = accept(s_fdListen, (sockaddr *) &peeraddr, &socklen);
2176
2177    if (s_fdCommand < 0 ) {
2178        LOGE("Error on accept() errno:%d", errno);
2179        /* start listening for new connections again */
2180        rilEventAddWakeup(&s_listen_event);
2181	      return;
2182    }
2183
2184    /* check the credential of the other side and only accept socket from
2185     * phone process
2186     */
2187    errno = 0;
2188    is_phone_socket = 0;
2189
2190    err = getsockopt(s_fdCommand, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
2191
2192    if (err == 0 && szCreds > 0) {
2193        errno = 0;
2194        pwd = getpwuid(creds.uid);
2195        if (pwd != NULL) {
2196            if (strcmp(pwd->pw_name, PHONE_PROCESS) == 0) {
2197                is_phone_socket = 1;
2198            } else {
2199                LOGE("RILD can't accept socket from process %s", pwd->pw_name);
2200            }
2201        } else {
2202            LOGE("Error on getpwuid() errno: %d", errno);
2203        }
2204    } else {
2205        LOGD("Error on getsockopt() errno: %d", errno);
2206    }
2207
2208    if ( !is_phone_socket ) {
2209      LOGE("RILD must accept socket from %s", PHONE_PROCESS);
2210
2211      close(s_fdCommand);
2212      s_fdCommand = -1;
2213
2214      onCommandsSocketClosed();
2215
2216      /* start listening for new connections again */
2217      rilEventAddWakeup(&s_listen_event);
2218
2219      return;
2220    }
2221
2222    ret = fcntl(s_fdCommand, F_SETFL, O_NONBLOCK);
2223
2224    if (ret < 0) {
2225        LOGE ("Error setting O_NONBLOCK errno:%d", errno);
2226    }
2227
2228    LOGI("libril: new connection");
2229
2230    p_rs = record_stream_new(s_fdCommand, MAX_COMMAND_BYTES);
2231
2232    ril_event_set (&s_commands_event, s_fdCommand, 1,
2233        processCommandsCallback, p_rs);
2234
2235    rilEventAddWakeup (&s_commands_event);
2236
2237    onNewCommandConnect();
2238}
2239
2240static void freeDebugCallbackArgs(int number, char **args) {
2241    for (int i = 0; i < number; i++) {
2242        if (args[i] != NULL) {
2243            free(args[i]);
2244        }
2245    }
2246    free(args);
2247}
2248
2249static void debugCallback (int fd, short flags, void *param) {
2250    int acceptFD, option;
2251    struct sockaddr_un peeraddr;
2252    socklen_t socklen = sizeof (peeraddr);
2253    int data;
2254    unsigned int qxdm_data[6];
2255    const char *deactData[1] = {"1"};
2256    char *actData[1];
2257    RIL_Dial dialData;
2258    int hangupData[1] = {1};
2259    int number;
2260    char **args;
2261
2262    acceptFD = accept (fd,  (sockaddr *) &peeraddr, &socklen);
2263
2264    if (acceptFD < 0) {
2265        LOGE ("error accepting on debug port: %d\n", errno);
2266        return;
2267    }
2268
2269    if (recv(acceptFD, &number, sizeof(int), 0) != sizeof(int)) {
2270        LOGE ("error reading on socket: number of Args: \n");
2271        return;
2272    }
2273    args = (char **) malloc(sizeof(char*) * number);
2274
2275    for (int i = 0; i < number; i++) {
2276        int len;
2277        if (recv(acceptFD, &len, sizeof(int), 0) != sizeof(int)) {
2278            LOGE ("error reading on socket: Len of Args: \n");
2279            freeDebugCallbackArgs(i, args);
2280            return;
2281        }
2282        // +1 for null-term
2283        args[i] = (char *) malloc((sizeof(char) * len) + 1);
2284        if (recv(acceptFD, args[i], sizeof(char) * len, 0)
2285            != (int)sizeof(char) * len) {
2286            LOGE ("error reading on socket: Args[%d] \n", i);
2287            freeDebugCallbackArgs(i, args);
2288            return;
2289        }
2290        char * buf = args[i];
2291        buf[len] = 0;
2292    }
2293
2294    switch (atoi(args[0])) {
2295        case 0:
2296            LOGI ("Connection on debug port: issuing reset.");
2297            issueLocalRequest(RIL_REQUEST_RESET_RADIO, NULL, 0);
2298            break;
2299        case 1:
2300            LOGI ("Connection on debug port: issuing radio power off.");
2301            data = 0;
2302            issueLocalRequest(RIL_REQUEST_RADIO_POWER, &data, sizeof(int));
2303            // Close the socket
2304            close(s_fdCommand);
2305            s_fdCommand = -1;
2306            break;
2307        case 2:
2308            LOGI ("Debug port: issuing unsolicited network change.");
2309            RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
2310                                      NULL, 0);
2311            break;
2312        case 3:
2313            LOGI ("Debug port: QXDM log enable.");
2314            qxdm_data[0] = 65536;
2315            qxdm_data[1] = 16;
2316            qxdm_data[2] = 1;
2317            qxdm_data[3] = 32;
2318            qxdm_data[4] = 0;
2319            qxdm_data[4] = 8;
2320            issueLocalRequest(RIL_REQUEST_OEM_HOOK_RAW, qxdm_data,
2321                              6 * sizeof(int));
2322            break;
2323        case 4:
2324            LOGI ("Debug port: QXDM log disable.");
2325            qxdm_data[0] = 65536;
2326            qxdm_data[1] = 16;
2327            qxdm_data[2] = 0;
2328            qxdm_data[3] = 32;
2329            qxdm_data[4] = 0;
2330            qxdm_data[4] = 8;
2331            issueLocalRequest(RIL_REQUEST_OEM_HOOK_RAW, qxdm_data,
2332                              6 * sizeof(int));
2333            break;
2334        case 5:
2335            LOGI("Debug port: Radio On");
2336            data = 1;
2337            issueLocalRequest(RIL_REQUEST_RADIO_POWER, &data, sizeof(int));
2338            sleep(2);
2339            // Set network selection automatic.
2340            issueLocalRequest(RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC, NULL, 0);
2341            break;
2342        case 6:
2343            LOGI("Debug port: Setup Data Call, Apn :%s\n", args[1]);
2344            actData[0] = args[1];
2345            issueLocalRequest(RIL_REQUEST_SETUP_DATA_CALL, &actData,
2346                              sizeof(actData));
2347            break;
2348        case 7:
2349            LOGI("Debug port: Deactivate Data Call");
2350            issueLocalRequest(RIL_REQUEST_DEACTIVATE_DATA_CALL, &deactData,
2351                              sizeof(deactData));
2352            break;
2353        case 8:
2354            LOGI("Debug port: Dial Call");
2355            dialData.clir = 0;
2356            dialData.address = args[1];
2357            issueLocalRequest(RIL_REQUEST_DIAL, &dialData, sizeof(dialData));
2358            break;
2359        case 9:
2360            LOGI("Debug port: Answer Call");
2361            issueLocalRequest(RIL_REQUEST_ANSWER, NULL, 0);
2362            break;
2363        case 10:
2364            LOGI("Debug port: End Call");
2365            issueLocalRequest(RIL_REQUEST_HANGUP, &hangupData,
2366                              sizeof(hangupData));
2367            break;
2368        default:
2369            LOGE ("Invalid request");
2370            break;
2371    }
2372    freeDebugCallbackArgs(number, args);
2373    close(acceptFD);
2374}
2375
2376
2377static void userTimerCallback (int fd, short flags, void *param) {
2378    UserCallbackInfo *p_info;
2379
2380    p_info = (UserCallbackInfo *)param;
2381
2382    p_info->p_callback(p_info->userParam);
2383
2384
2385    // FIXME generalize this...there should be a cancel mechanism
2386    if (s_last_wake_timeout_info != NULL && s_last_wake_timeout_info == p_info) {
2387        s_last_wake_timeout_info = NULL;
2388    }
2389
2390    free(p_info);
2391}
2392
2393
2394static void *
2395eventLoop(void *param) {
2396    int ret;
2397    int filedes[2];
2398
2399    ril_event_init();
2400
2401    pthread_mutex_lock(&s_startupMutex);
2402
2403    s_started = 1;
2404    pthread_cond_broadcast(&s_startupCond);
2405
2406    pthread_mutex_unlock(&s_startupMutex);
2407
2408    ret = pipe(filedes);
2409
2410    if (ret < 0) {
2411        LOGE("Error in pipe() errno:%d", errno);
2412        return NULL;
2413    }
2414
2415    s_fdWakeupRead = filedes[0];
2416    s_fdWakeupWrite = filedes[1];
2417
2418    fcntl(s_fdWakeupRead, F_SETFL, O_NONBLOCK);
2419
2420    ril_event_set (&s_wakeupfd_event, s_fdWakeupRead, true,
2421                processWakeupCallback, NULL);
2422
2423    rilEventAddWakeup (&s_wakeupfd_event);
2424
2425    // Only returns on error
2426    ril_event_loop();
2427    LOGE ("error in event_loop_base errno:%d", errno);
2428
2429    return NULL;
2430}
2431
2432extern "C" void
2433RIL_startEventLoop(void) {
2434    int ret;
2435    pthread_attr_t attr;
2436
2437    /* spin up eventLoop thread and wait for it to get started */
2438    s_started = 0;
2439    pthread_mutex_lock(&s_startupMutex);
2440
2441    pthread_attr_init (&attr);
2442    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2443    ret = pthread_create(&s_tid_dispatch, &attr, eventLoop, NULL);
2444
2445    while (s_started == 0) {
2446        pthread_cond_wait(&s_startupCond, &s_startupMutex);
2447    }
2448
2449    pthread_mutex_unlock(&s_startupMutex);
2450
2451    if (ret < 0) {
2452        LOGE("Failed to create dispatch thread errno:%d", errno);
2453        return;
2454    }
2455}
2456
2457// Used for testing purpose only.
2458extern "C" void RIL_setcallbacks (const RIL_RadioFunctions *callbacks) {
2459    memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
2460}
2461
2462extern "C" void
2463RIL_register (const RIL_RadioFunctions *callbacks) {
2464    int ret;
2465    int flags;
2466
2467    if (callbacks == NULL
2468        || ! (callbacks->version == RIL_VERSION || callbacks->version == 1)
2469    ) {
2470        LOGE(
2471            "RIL_register: RIL_RadioFunctions * null or invalid version"
2472            " (expected %d)", RIL_VERSION);
2473        return;
2474    }
2475
2476    if (s_registerCalled > 0) {
2477        LOGE("RIL_register has been called more than once. "
2478                "Subsequent call ignored");
2479        return;
2480    }
2481
2482    memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
2483
2484    s_registerCalled = 1;
2485
2486    // Little self-check
2487
2488    for (int i = 0; i < (int)NUM_ELEMS(s_commands); i++) {
2489        assert(i == s_commands[i].requestNumber);
2490    }
2491
2492    for (int i = 0; i < (int)NUM_ELEMS(s_unsolResponses); i++) {
2493        assert(i + RIL_UNSOL_RESPONSE_BASE
2494                == s_unsolResponses[i].requestNumber);
2495    }
2496
2497    // New rild impl calls RIL_startEventLoop() first
2498    // old standalone impl wants it here.
2499
2500    if (s_started == 0) {
2501        RIL_startEventLoop();
2502    }
2503
2504    // start listen socket
2505
2506#if 0
2507    ret = socket_local_server (SOCKET_NAME_RIL,
2508            ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
2509
2510    if (ret < 0) {
2511        LOGE("Unable to bind socket errno:%d", errno);
2512        exit (-1);
2513    }
2514    s_fdListen = ret;
2515
2516#else
2517    s_fdListen = android_get_control_socket(SOCKET_NAME_RIL);
2518    if (s_fdListen < 0) {
2519        LOGE("Failed to get socket '" SOCKET_NAME_RIL "'");
2520        exit(-1);
2521    }
2522
2523    ret = listen(s_fdListen, 4);
2524
2525    if (ret < 0) {
2526        LOGE("Failed to listen on control socket '%d': %s",
2527             s_fdListen, strerror(errno));
2528        exit(-1);
2529    }
2530#endif
2531
2532
2533    /* note: non-persistent so we can accept only one connection at a time */
2534    ril_event_set (&s_listen_event, s_fdListen, false,
2535                listenCallback, NULL);
2536
2537    rilEventAddWakeup (&s_listen_event);
2538
2539#if 1
2540    // start debug interface socket
2541
2542    s_fdDebug = android_get_control_socket(SOCKET_NAME_RIL_DEBUG);
2543    if (s_fdDebug < 0) {
2544        LOGE("Failed to get socket '" SOCKET_NAME_RIL_DEBUG "' errno:%d", errno);
2545        exit(-1);
2546    }
2547
2548    ret = listen(s_fdDebug, 4);
2549
2550    if (ret < 0) {
2551        LOGE("Failed to listen on ril debug socket '%d': %s",
2552             s_fdDebug, strerror(errno));
2553        exit(-1);
2554    }
2555
2556    ril_event_set (&s_debug_event, s_fdDebug, true,
2557                debugCallback, NULL);
2558
2559    rilEventAddWakeup (&s_debug_event);
2560#endif
2561
2562}
2563
2564static int
2565checkAndDequeueRequestInfo(struct RequestInfo *pRI) {
2566    int ret = 0;
2567
2568    if (pRI == NULL) {
2569        return 0;
2570    }
2571
2572    pthread_mutex_lock(&s_pendingRequestsMutex);
2573
2574    for(RequestInfo **ppCur = &s_pendingRequests
2575        ; *ppCur != NULL
2576        ; ppCur = &((*ppCur)->p_next)
2577    ) {
2578        if (pRI == *ppCur) {
2579            ret = 1;
2580
2581            *ppCur = (*ppCur)->p_next;
2582            break;
2583        }
2584    }
2585
2586    pthread_mutex_unlock(&s_pendingRequestsMutex);
2587
2588    return ret;
2589}
2590
2591
2592extern "C" void
2593RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen) {
2594    RequestInfo *pRI;
2595    int ret;
2596    size_t errorOffset;
2597
2598    pRI = (RequestInfo *)t;
2599
2600    if (!checkAndDequeueRequestInfo(pRI)) {
2601        LOGE ("RIL_onRequestComplete: invalid RIL_Token");
2602        return;
2603    }
2604
2605    if (pRI->local > 0) {
2606        // Locally issued command...void only!
2607        // response does not go back up the command socket
2608        LOGD("C[locl]< %s", requestToString(pRI->pCI->requestNumber));
2609
2610        goto done;
2611    }
2612
2613    appendPrintBuf("[%04d]< %s",
2614        pRI->token, requestToString(pRI->pCI->requestNumber));
2615
2616    if (pRI->cancelled == 0) {
2617        Parcel p;
2618
2619        p.writeInt32 (RESPONSE_SOLICITED);
2620        p.writeInt32 (pRI->token);
2621        errorOffset = p.dataPosition();
2622
2623        p.writeInt32 (e);
2624
2625        if (response != NULL) {
2626            // there is a response payload, no matter success or not.
2627            ret = pRI->pCI->responseFunction(p, response, responselen);
2628
2629            /* if an error occurred, rewind and mark it */
2630            if (ret != 0) {
2631                p.setDataPosition(errorOffset);
2632                p.writeInt32 (ret);
2633            }
2634        }
2635
2636        if (e != RIL_E_SUCCESS) {
2637            appendPrintBuf("%s fails by %s", printBuf, failCauseToString(e));
2638        }
2639
2640        if (s_fdCommand < 0) {
2641            LOGD ("RIL onRequestComplete: Command channel closed");
2642        }
2643        sendResponse(p);
2644    }
2645
2646done:
2647    free(pRI);
2648}
2649
2650
2651static void
2652grabPartialWakeLock() {
2653    acquire_wake_lock(PARTIAL_WAKE_LOCK, ANDROID_WAKE_LOCK_NAME);
2654}
2655
2656static void
2657releaseWakeLock() {
2658    release_wake_lock(ANDROID_WAKE_LOCK_NAME);
2659}
2660
2661/**
2662 * Timer callback to put us back to sleep before the default timeout
2663 */
2664static void
2665wakeTimeoutCallback (void *param) {
2666    // We're using "param != NULL" as a cancellation mechanism
2667    if (param == NULL) {
2668        //LOGD("wakeTimeout: releasing wake lock");
2669
2670        releaseWakeLock();
2671    } else {
2672        //LOGD("wakeTimeout: releasing wake lock CANCELLED");
2673    }
2674}
2675
2676extern "C"
2677void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
2678                                size_t datalen)
2679{
2680    int unsolResponseIndex;
2681    int ret;
2682    int64_t timeReceived = 0;
2683    bool shouldScheduleTimeout = false;
2684
2685    if (s_registerCalled == 0) {
2686        // Ignore RIL_onUnsolicitedResponse before RIL_register
2687        LOGW("RIL_onUnsolicitedResponse called before RIL_register");
2688        return;
2689    }
2690
2691    unsolResponseIndex = unsolResponse - RIL_UNSOL_RESPONSE_BASE;
2692
2693    if ((unsolResponseIndex < 0)
2694        || (unsolResponseIndex >= (int32_t)NUM_ELEMS(s_unsolResponses))) {
2695        LOGE("unsupported unsolicited response code %d", unsolResponse);
2696        return;
2697    }
2698
2699    // Grab a wake lock if needed for this reponse,
2700    // as we exit we'll either release it immediately
2701    // or set a timer to release it later.
2702    switch (s_unsolResponses[unsolResponseIndex].wakeType) {
2703        case WAKE_PARTIAL:
2704            grabPartialWakeLock();
2705            shouldScheduleTimeout = true;
2706        break;
2707
2708        case DONT_WAKE:
2709        default:
2710            // No wake lock is grabed so don't set timeout
2711            shouldScheduleTimeout = false;
2712            break;
2713    }
2714
2715    // Mark the time this was received, doing this
2716    // after grabing the wakelock incase getting
2717    // the elapsedRealTime might cause us to goto
2718    // sleep.
2719    if (unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
2720        timeReceived = elapsedRealtime();
2721    }
2722
2723    appendPrintBuf("[UNSL]< %s", requestToString(unsolResponse));
2724
2725    Parcel p;
2726
2727    p.writeInt32 (RESPONSE_UNSOLICITED);
2728    p.writeInt32 (unsolResponse);
2729
2730    ret = s_unsolResponses[unsolResponseIndex]
2731                .responseFunction(p, data, datalen);
2732    if (ret != 0) {
2733        // Problem with the response. Don't continue;
2734        goto error_exit;
2735    }
2736
2737    // some things get more payload
2738    switch(unsolResponse) {
2739        case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
2740            p.writeInt32(s_callbacks.onStateRequest());
2741            appendPrintBuf("%s {%s}", printBuf,
2742                radioStateToString(s_callbacks.onStateRequest()));
2743        break;
2744
2745
2746        case RIL_UNSOL_NITZ_TIME_RECEIVED:
2747            // Store the time that this was received so the
2748            // handler of this message can account for
2749            // the time it takes to arrive and process. In
2750            // particular the system has been known to sleep
2751            // before this message can be processed.
2752            p.writeInt64(timeReceived);
2753        break;
2754    }
2755
2756    ret = sendResponse(p);
2757    if (ret != 0 && unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
2758
2759        // Unfortunately, NITZ time is not poll/update like everything
2760        // else in the system. So, if the upstream client isn't connected,
2761        // keep a copy of the last NITZ response (with receive time noted
2762        // above) around so we can deliver it when it is connected
2763
2764        if (s_lastNITZTimeData != NULL) {
2765            free (s_lastNITZTimeData);
2766            s_lastNITZTimeData = NULL;
2767        }
2768
2769        s_lastNITZTimeData = malloc(p.dataSize());
2770        s_lastNITZTimeDataSize = p.dataSize();
2771        memcpy(s_lastNITZTimeData, p.data(), p.dataSize());
2772    }
2773
2774    // For now, we automatically go back to sleep after TIMEVAL_WAKE_TIMEOUT
2775    // FIXME The java code should handshake here to release wake lock
2776
2777    if (shouldScheduleTimeout) {
2778        // Cancel the previous request
2779        if (s_last_wake_timeout_info != NULL) {
2780            s_last_wake_timeout_info->userParam = (void *)1;
2781        }
2782
2783        s_last_wake_timeout_info
2784            = internalRequestTimedCallback(wakeTimeoutCallback, NULL,
2785                                            &TIMEVAL_WAKE_TIMEOUT);
2786    }
2787
2788    // Normal exit
2789    return;
2790
2791error_exit:
2792    if (shouldScheduleTimeout) {
2793        releaseWakeLock();
2794    }
2795}
2796
2797/** FIXME generalize this if you track UserCAllbackInfo, clear it
2798    when the callback occurs
2799*/
2800static UserCallbackInfo *
2801internalRequestTimedCallback (RIL_TimedCallback callback, void *param,
2802                                const struct timeval *relativeTime)
2803{
2804    struct timeval myRelativeTime;
2805    UserCallbackInfo *p_info;
2806
2807    p_info = (UserCallbackInfo *) malloc (sizeof(UserCallbackInfo));
2808
2809    p_info->p_callback = callback;
2810    p_info->userParam = param;
2811
2812    if (relativeTime == NULL) {
2813        /* treat null parameter as a 0 relative time */
2814        memset (&myRelativeTime, 0, sizeof(myRelativeTime));
2815    } else {
2816        /* FIXME I think event_add's tv param is really const anyway */
2817        memcpy (&myRelativeTime, relativeTime, sizeof(myRelativeTime));
2818    }
2819
2820    ril_event_set(&(p_info->event), -1, false, userTimerCallback, p_info);
2821
2822    ril_timer_add(&(p_info->event), &myRelativeTime);
2823
2824    triggerEvLoop();
2825    return p_info;
2826}
2827
2828
2829extern "C" void
2830RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
2831                                const struct timeval *relativeTime) {
2832    internalRequestTimedCallback (callback, param, relativeTime);
2833}
2834
2835const char *
2836failCauseToString(RIL_Errno e) {
2837    switch(e) {
2838        case RIL_E_SUCCESS: return "E_SUCCESS";
2839        case RIL_E_RADIO_NOT_AVAILABLE: return "E_RAIDO_NOT_AVAILABLE";
2840        case RIL_E_GENERIC_FAILURE: return "E_GENERIC_FAILURE";
2841        case RIL_E_PASSWORD_INCORRECT: return "E_PASSWORD_INCORRECT";
2842        case RIL_E_SIM_PIN2: return "E_SIM_PIN2";
2843        case RIL_E_SIM_PUK2: return "E_SIM_PUK2";
2844        case RIL_E_REQUEST_NOT_SUPPORTED: return "E_REQUEST_NOT_SUPPORTED";
2845        case RIL_E_CANCELLED: return "E_CANCELLED";
2846        case RIL_E_OP_NOT_ALLOWED_DURING_VOICE_CALL: return "E_OP_NOT_ALLOWED_DURING_VOICE_CALL";
2847        case RIL_E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW: return "E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW";
2848        case RIL_E_SMS_SEND_FAIL_RETRY: return "E_SMS_SEND_FAIL_RETRY";
2849        case RIL_E_SIM_ABSENT:return "E_SIM_ABSENT";
2850#ifdef FEATURE_MULTIMODE_ANDROID
2851        case RIL_E_SUBSCRIPTION_NOT_AVAILABLE:return "E_SUBSCRIPTION_NOT_AVAILABLE";
2852        case RIL_E_MODE_NOT_SUPPORTED:return "E_MODE_NOT_SUPPORTED";
2853#endif
2854        default: return "<unknown error>";
2855    }
2856}
2857
2858const char *
2859radioStateToString(RIL_RadioState s) {
2860    switch(s) {
2861        case RADIO_STATE_OFF: return "RADIO_OFF";
2862        case RADIO_STATE_UNAVAILABLE: return "RADIO_UNAVAILABLE";
2863        case RADIO_STATE_SIM_NOT_READY: return "RADIO_SIM_NOT_READY";
2864        case RADIO_STATE_SIM_LOCKED_OR_ABSENT: return "RADIO_SIM_LOCKED_OR_ABSENT";
2865        case RADIO_STATE_SIM_READY: return "RADIO_SIM_READY";
2866        case RADIO_STATE_RUIM_NOT_READY:return"RADIO_RUIM_NOT_READY";
2867        case RADIO_STATE_RUIM_READY:return"RADIO_RUIM_READY";
2868        case RADIO_STATE_RUIM_LOCKED_OR_ABSENT:return"RADIO_RUIM_LOCKED_OR_ABSENT";
2869        case RADIO_STATE_NV_NOT_READY:return"RADIO_NV_NOT_READY";
2870        case RADIO_STATE_NV_READY:return"RADIO_NV_READY";
2871        default: return "<unknown state>";
2872    }
2873}
2874
2875const char *
2876callStateToString(RIL_CallState s) {
2877    switch(s) {
2878        case RIL_CALL_ACTIVE : return "ACTIVE";
2879        case RIL_CALL_HOLDING: return "HOLDING";
2880        case RIL_CALL_DIALING: return "DIALING";
2881        case RIL_CALL_ALERTING: return "ALERTING";
2882        case RIL_CALL_INCOMING: return "INCOMING";
2883        case RIL_CALL_WAITING: return "WAITING";
2884        default: return "<unknown state>";
2885    }
2886}
2887
2888const char *
2889requestToString(int request) {
2890/*
2891 cat libs/telephony/ril_commands.h \
2892 | egrep "^ *{RIL_" \
2893 | sed -re 's/\{RIL_([^,]+),[^,]+,([^}]+).+/case RIL_\1: return "\1";/'
2894
2895
2896 cat libs/telephony/ril_unsol_commands.h \
2897 | egrep "^ *{RIL_" \
2898 | sed -re 's/\{RIL_([^,]+),([^}]+).+/case RIL_\1: return "\1";/'
2899
2900*/
2901    switch(request) {
2902        case RIL_REQUEST_GET_SIM_STATUS: return "GET_SIM_STATUS";
2903        case RIL_REQUEST_ENTER_SIM_PIN: return "ENTER_SIM_PIN";
2904        case RIL_REQUEST_ENTER_SIM_PUK: return "ENTER_SIM_PUK";
2905        case RIL_REQUEST_ENTER_SIM_PIN2: return "ENTER_SIM_PIN2";
2906        case RIL_REQUEST_ENTER_SIM_PUK2: return "ENTER_SIM_PUK2";
2907        case RIL_REQUEST_CHANGE_SIM_PIN: return "CHANGE_SIM_PIN";
2908        case RIL_REQUEST_CHANGE_SIM_PIN2: return "CHANGE_SIM_PIN2";
2909        case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION: return "ENTER_NETWORK_DEPERSONALIZATION";
2910        case RIL_REQUEST_GET_CURRENT_CALLS: return "GET_CURRENT_CALLS";
2911        case RIL_REQUEST_DIAL: return "DIAL";
2912        case RIL_REQUEST_GET_IMSI: return "GET_IMSI";
2913        case RIL_REQUEST_HANGUP: return "HANGUP";
2914        case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND: return "HANGUP_WAITING_OR_BACKGROUND";
2915        case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND: return "HANGUP_FOREGROUND_RESUME_BACKGROUND";
2916        case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE: return "SWITCH_WAITING_OR_HOLDING_AND_ACTIVE";
2917        case RIL_REQUEST_CONFERENCE: return "CONFERENCE";
2918        case RIL_REQUEST_UDUB: return "UDUB";
2919        case RIL_REQUEST_LAST_CALL_FAIL_CAUSE: return "LAST_CALL_FAIL_CAUSE";
2920        case RIL_REQUEST_SIGNAL_STRENGTH: return "SIGNAL_STRENGTH";
2921        case RIL_REQUEST_REGISTRATION_STATE: return "REGISTRATION_STATE";
2922        case RIL_REQUEST_GPRS_REGISTRATION_STATE: return "GPRS_REGISTRATION_STATE";
2923        case RIL_REQUEST_OPERATOR: return "OPERATOR";
2924        case RIL_REQUEST_RADIO_POWER: return "RADIO_POWER";
2925        case RIL_REQUEST_DTMF: return "DTMF";
2926        case RIL_REQUEST_SEND_SMS: return "SEND_SMS";
2927        case RIL_REQUEST_SEND_SMS_EXPECT_MORE: return "SEND_SMS_EXPECT_MORE";
2928        case RIL_REQUEST_SETUP_DATA_CALL: return "SETUP_DATA_CALL";
2929        case RIL_REQUEST_SIM_IO: return "SIM_IO";
2930        case RIL_REQUEST_SEND_USSD: return "SEND_USSD";
2931        case RIL_REQUEST_CANCEL_USSD: return "CANCEL_USSD";
2932        case RIL_REQUEST_GET_CLIR: return "GET_CLIR";
2933        case RIL_REQUEST_SET_CLIR: return "SET_CLIR";
2934        case RIL_REQUEST_QUERY_CALL_FORWARD_STATUS: return "QUERY_CALL_FORWARD_STATUS";
2935        case RIL_REQUEST_SET_CALL_FORWARD: return "SET_CALL_FORWARD";
2936        case RIL_REQUEST_QUERY_CALL_WAITING: return "QUERY_CALL_WAITING";
2937        case RIL_REQUEST_SET_CALL_WAITING: return "SET_CALL_WAITING";
2938        case RIL_REQUEST_SMS_ACKNOWLEDGE: return "SMS_ACKNOWLEDGE";
2939        case RIL_REQUEST_GET_IMEI: return "GET_IMEI";
2940        case RIL_REQUEST_GET_IMEISV: return "GET_IMEISV";
2941        case RIL_REQUEST_ANSWER: return "ANSWER";
2942        case RIL_REQUEST_DEACTIVATE_DATA_CALL: return "DEACTIVATE_DATA_CALL";
2943        case RIL_REQUEST_QUERY_FACILITY_LOCK: return "QUERY_FACILITY_LOCK";
2944        case RIL_REQUEST_SET_FACILITY_LOCK: return "SET_FACILITY_LOCK";
2945        case RIL_REQUEST_CHANGE_BARRING_PASSWORD: return "CHANGE_BARRING_PASSWORD";
2946        case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE: return "QUERY_NETWORK_SELECTION_MODE";
2947        case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC: return "SET_NETWORK_SELECTION_AUTOMATIC";
2948        case RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL: return "SET_NETWORK_SELECTION_MANUAL";
2949        case RIL_REQUEST_QUERY_AVAILABLE_NETWORKS : return "QUERY_AVAILABLE_NETWORKS ";
2950        case RIL_REQUEST_DTMF_START: return "DTMF_START";
2951        case RIL_REQUEST_DTMF_STOP: return "DTMF_STOP";
2952        case RIL_REQUEST_BASEBAND_VERSION: return "BASEBAND_VERSION";
2953        case RIL_REQUEST_SEPARATE_CONNECTION: return "SEPARATE_CONNECTION";
2954        case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE: return "SET_PREFERRED_NETWORK_TYPE";
2955        case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE: return "GET_PREFERRED_NETWORK_TYPE";
2956        case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS: return "GET_NEIGHBORING_CELL_IDS";
2957        case RIL_REQUEST_SET_MUTE: return "SET_MUTE";
2958        case RIL_REQUEST_GET_MUTE: return "GET_MUTE";
2959        case RIL_REQUEST_QUERY_CLIP: return "QUERY_CLIP";
2960        case RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE: return "LAST_DATA_CALL_FAIL_CAUSE";
2961        case RIL_REQUEST_DATA_CALL_LIST: return "DATA_CALL_LIST";
2962        case RIL_REQUEST_RESET_RADIO: return "RESET_RADIO";
2963        case RIL_REQUEST_OEM_HOOK_RAW: return "OEM_HOOK_RAW";
2964        case RIL_REQUEST_OEM_HOOK_STRINGS: return "OEM_HOOK_STRINGS";
2965        case RIL_REQUEST_SET_BAND_MODE: return "SET_BAND_MODE";
2966        case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE: return "QUERY_AVAILABLE_BAND_MODE";
2967        case RIL_REQUEST_STK_GET_PROFILE: return "STK_GET_PROFILE";
2968        case RIL_REQUEST_STK_SET_PROFILE: return "STK_SET_PROFILE";
2969        case RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND: return "STK_SEND_ENVELOPE_COMMAND";
2970        case RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE: return "STK_SEND_TERMINAL_RESPONSE";
2971        case RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM: return "STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM";
2972        case RIL_REQUEST_SCREEN_STATE: return "SCREEN_STATE";
2973        case RIL_REQUEST_EXPLICIT_CALL_TRANSFER: return "EXPLICIT_CALL_TRANSFER";
2974        case RIL_REQUEST_SET_LOCATION_UPDATES: return "SET_LOCATION_UPDATES";
2975        case RIL_REQUEST_CDMA_SET_SUBSCRIPTION:return"CDMA_SET_SUBSCRIPTION";
2976        case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:return"CDMA_SET_ROAMING_PREFERENCE";
2977        case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:return"CDMA_QUERY_ROAMING_PREFERENCE";
2978        case RIL_REQUEST_SET_TTY_MODE:return"SET_TTY_MODE";
2979        case RIL_REQUEST_QUERY_TTY_MODE:return"QUERY_TTY_MODE";
2980        case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:return"CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE";
2981        case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:return"CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE";
2982        case RIL_REQUEST_CDMA_FLASH:return"CDMA_FLASH";
2983        case RIL_REQUEST_CDMA_BURST_DTMF:return"CDMA_BURST_DTMF";
2984        case RIL_REQUEST_CDMA_SEND_SMS:return"CDMA_SEND_SMS";
2985        case RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE:return"CDMA_SMS_ACKNOWLEDGE";
2986        case RIL_REQUEST_GSM_GET_BROADCAST_SMS_CONFIG:return"GSM_GET_BROADCAST_SMS_CONFIG";
2987        case RIL_REQUEST_GSM_SET_BROADCAST_SMS_CONFIG:return"GSM_SET_BROADCAST_SMS_CONFIG";
2988        case RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG:return "CDMA_GET_BROADCAST_SMS_CONFIG";
2989        case RIL_REQUEST_CDMA_SET_BROADCAST_SMS_CONFIG:return "CDMA_SET_BROADCAST_SMS_CONFIG";
2990        case RIL_REQUEST_CDMA_SMS_BROADCAST_ACTIVATION:return "CDMA_SMS_BROADCAST_ACTIVATION";
2991        case RIL_REQUEST_CDMA_VALIDATE_AKEY: return"CDMA_VALIDATE_AKEY";
2992        case RIL_REQUEST_CDMA_SUBSCRIPTION: return"CDMA_SUBSCRIPTION";
2993        case RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM: return "CDMA_WRITE_SMS_TO_RUIM";
2994        case RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM: return "CDMA_DELETE_SMS_ON_RUIM";
2995        case RIL_REQUEST_DEVICE_IDENTITY: return "DEVICE_IDENTITY";
2996        case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE: return "EXIT_EMERGENCY_CALLBACK_MODE";
2997        case RIL_REQUEST_GET_SMSC_ADDRESS: return "GET_SMSC_ADDRESS";
2998        case RIL_REQUEST_SET_SMSC_ADDRESS: return "SET_SMSC_ADDRESS";
2999        case RIL_REQUEST_REPORT_SMS_MEMORY_STATUS: return "REPORT_SMS_MEMORY_STATUS";
3000        case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: return "UNSOL_RESPONSE_RADIO_STATE_CHANGED";
3001        case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: return "UNSOL_RESPONSE_CALL_STATE_CHANGED";
3002        case RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED: return "UNSOL_RESPONSE_NETWORK_STATE_CHANGED";
3003        case RIL_UNSOL_RESPONSE_NEW_SMS: return "UNSOL_RESPONSE_NEW_SMS";
3004        case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT: return "UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT";
3005        case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM: return "UNSOL_RESPONSE_NEW_SMS_ON_SIM";
3006        case RIL_UNSOL_ON_USSD: return "UNSOL_ON_USSD";
3007        case RIL_UNSOL_ON_USSD_REQUEST: return "UNSOL_ON_USSD_REQUEST(obsolete)";
3008        case RIL_UNSOL_NITZ_TIME_RECEIVED: return "UNSOL_NITZ_TIME_RECEIVED";
3009        case RIL_UNSOL_SIGNAL_STRENGTH: return "UNSOL_SIGNAL_STRENGTH";
3010        case RIL_UNSOL_STK_SESSION_END: return "UNSOL_STK_SESSION_END";
3011        case RIL_UNSOL_STK_PROACTIVE_COMMAND: return "UNSOL_STK_PROACTIVE_COMMAND";
3012        case RIL_UNSOL_STK_EVENT_NOTIFY: return "UNSOL_STK_EVENT_NOTIFY";
3013        case RIL_UNSOL_STK_CALL_SETUP: return "UNSOL_STK_CALL_SETUP";
3014        case RIL_UNSOL_SIM_SMS_STORAGE_FULL: return "UNSOL_SIM_SMS_STORAGE_FUL";
3015        case RIL_UNSOL_SIM_REFRESH: return "UNSOL_SIM_REFRESH";
3016        case RIL_UNSOL_DATA_CALL_LIST_CHANGED: return "UNSOL_DATA_CALL_LIST_CHANGED";
3017        case RIL_UNSOL_CALL_RING: return "UNSOL_CALL_RING";
3018        case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: return "UNSOL_RESPONSE_SIM_STATUS_CHANGED";
3019        case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS: return "UNSOL_NEW_CDMA_SMS";
3020        case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: return "UNSOL_NEW_BROADCAST_SMS";
3021        case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL: return "UNSOL_CDMA_RUIM_SMS_STORAGE_FULL";
3022        case RIL_UNSOL_RESTRICTED_STATE_CHANGED: return "UNSOL_RESTRICTED_STATE_CHANGED";
3023        case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE: return "UNSOL_ENTER_EMERGENCY_CALLBACK_MODE";
3024        case RIL_UNSOL_CDMA_CALL_WAITING: return "UNSOL_CDMA_CALL_WAITING";
3025        case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS: return "UNSOL_CDMA_OTA_PROVISION_STATUS";
3026        case RIL_UNSOL_CDMA_INFO_REC: return "UNSOL_CDMA_INFO_REC";
3027        case RIL_UNSOL_OEM_HOOK_RAW: return "UNSOL_OEM_HOOK_RAW";
3028        default: return "<unknown request>";
3029    }
3030}
3031
3032} /* namespace android */
3033