Home | History | Annotate | Download | only in libril
      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 <telephony/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 <sys/limits.h>
     35 #include <pwd.h>
     36 
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <stdarg.h>
     40 #include <string.h>
     41 #include <unistd.h>
     42 #include <fcntl.h>
     43 #include <time.h>
     44 #include <errno.h>
     45 #include <assert.h>
     46 #include <ctype.h>
     47 #include <alloca.h>
     48 #include <sys/un.h>
     49 #include <assert.h>
     50 #include <netinet/in.h>
     51 #include <cutils/properties.h>
     52 
     53 #include <ril_event.h>
     54 
     55 namespace android {
     56 
     57 #define PHONE_PROCESS "radio"
     58 
     59 #define SOCKET_NAME_RIL "rild"
     60 #define SOCKET2_NAME_RIL "rild2"
     61 #define SOCKET3_NAME_RIL "rild3"
     62 #define SOCKET4_NAME_RIL "rild4"
     63 
     64 #define SOCKET_NAME_RIL_DEBUG "rild-debug"
     65 
     66 #define ANDROID_WAKE_LOCK_NAME "radio-interface"
     67 
     68 
     69 #define PROPERTY_RIL_IMPL "gsm.version.ril-impl"
     70 
     71 // match with constant in RIL.java
     72 #define MAX_COMMAND_BYTES (8 * 1024)
     73 
     74 // Basically: memset buffers that the client library
     75 // shouldn't be using anymore in an attempt to find
     76 // memory usage issues sooner.
     77 #define MEMSET_FREED 1
     78 
     79 #define NUM_ELEMS(a)     (sizeof (a) / sizeof (a)[0])
     80 
     81 #define MIN(a,b) ((a)<(b) ? (a) : (b))
     82 
     83 /* Constants for response types */
     84 #define RESPONSE_SOLICITED 0
     85 #define RESPONSE_UNSOLICITED 1
     86 
     87 /* Negative values for private RIL errno's */
     88 #define RIL_ERRNO_INVALID_RESPONSE -1
     89 
     90 // request, response, and unsolicited msg print macro
     91 #define PRINTBUF_SIZE 8096
     92 
     93 // Enable RILC log
     94 #define RILC_LOG 0
     95 
     96 #if RILC_LOG
     97     #define startRequest           sprintf(printBuf, "(")
     98     #define closeRequest           sprintf(printBuf, "%s)", printBuf)
     99     #define printRequest(token, req)           \
    100             RLOGD("[%04d]> %s %s", token, requestToString(req), printBuf)
    101 
    102     #define startResponse           sprintf(printBuf, "%s {", printBuf)
    103     #define closeResponse           sprintf(printBuf, "%s}", printBuf)
    104     #define printResponse           RLOGD("%s", printBuf)
    105 
    106     #define clearPrintBuf           printBuf[0] = 0
    107     #define removeLastChar          printBuf[strlen(printBuf)-1] = 0
    108     #define appendPrintBuf(x...)    sprintf(printBuf, x)
    109 #else
    110     #define startRequest
    111     #define closeRequest
    112     #define printRequest(token, req)
    113     #define startResponse
    114     #define closeResponse
    115     #define printResponse
    116     #define clearPrintBuf
    117     #define removeLastChar
    118     #define appendPrintBuf(x...)
    119 #endif
    120 
    121 enum WakeType {DONT_WAKE, WAKE_PARTIAL};
    122 
    123 typedef struct {
    124     int requestNumber;
    125     void (*dispatchFunction) (Parcel &p, struct RequestInfo *pRI);
    126     int(*responseFunction) (Parcel &p, void *response, size_t responselen);
    127 } CommandInfo;
    128 
    129 typedef struct {
    130     int requestNumber;
    131     int (*responseFunction) (Parcel &p, void *response, size_t responselen);
    132     WakeType wakeType;
    133 } UnsolResponseInfo;
    134 
    135 typedef struct RequestInfo {
    136     int32_t token;      //this is not RIL_Token
    137     CommandInfo *pCI;
    138     struct RequestInfo *p_next;
    139     char cancelled;
    140     char local;         // responses to local commands do not go back to command process
    141     RIL_SOCKET_ID socket_id;
    142 } RequestInfo;
    143 
    144 typedef struct UserCallbackInfo {
    145     RIL_TimedCallback p_callback;
    146     void *userParam;
    147     struct ril_event event;
    148     struct UserCallbackInfo *p_next;
    149 } UserCallbackInfo;
    150 
    151 typedef struct SocketListenParam {
    152     RIL_SOCKET_ID socket_id;
    153     int fdListen;
    154     int fdCommand;
    155     char* processName;
    156     struct ril_event* commands_event;
    157     struct ril_event* listen_event;
    158     void (*processCommandsCallback)(int fd, short flags, void *param);
    159     RecordStream *p_rs;
    160 } SocketListenParam;
    161 
    162 extern "C" const char * requestToString(int request);
    163 extern "C" const char * failCauseToString(RIL_Errno);
    164 extern "C" const char * callStateToString(RIL_CallState);
    165 extern "C" const char * radioStateToString(RIL_RadioState);
    166 extern "C" const char * rilSocketIdToString(RIL_SOCKET_ID socket_id);
    167 
    168 extern "C"
    169 char rild[MAX_SOCKET_NAME_LENGTH] = SOCKET_NAME_RIL;
    170 /*******************************************************************/
    171 
    172 RIL_RadioFunctions s_callbacks = {0, NULL, NULL, NULL, NULL, NULL};
    173 static int s_registerCalled = 0;
    174 
    175 static pthread_t s_tid_dispatch;
    176 static pthread_t s_tid_reader;
    177 static int s_started = 0;
    178 
    179 static int s_fdDebug = -1;
    180 static int s_fdDebug_socket2 = -1;
    181 
    182 static int s_fdWakeupRead;
    183 static int s_fdWakeupWrite;
    184 
    185 static struct ril_event s_commands_event;
    186 static struct ril_event s_wakeupfd_event;
    187 static struct ril_event s_listen_event;
    188 static SocketListenParam s_ril_param_socket;
    189 
    190 static pthread_mutex_t s_pendingRequestsMutex = PTHREAD_MUTEX_INITIALIZER;
    191 static pthread_mutex_t s_writeMutex = PTHREAD_MUTEX_INITIALIZER;
    192 static RequestInfo *s_pendingRequests = NULL;
    193 
    194 #if (SIM_COUNT >= 2)
    195 static struct ril_event s_commands_event_socket2;
    196 static struct ril_event s_listen_event_socket2;
    197 static SocketListenParam s_ril_param_socket2;
    198 
    199 static pthread_mutex_t s_pendingRequestsMutex_socket2  = PTHREAD_MUTEX_INITIALIZER;
    200 static pthread_mutex_t s_writeMutex_socket2            = PTHREAD_MUTEX_INITIALIZER;
    201 static RequestInfo *s_pendingRequests_socket2          = NULL;
    202 #endif
    203 
    204 #if (SIM_COUNT >= 3)
    205 static struct ril_event s_commands_event_socket3;
    206 static struct ril_event s_listen_event_socket3;
    207 static SocketListenParam s_ril_param_socket3;
    208 
    209 static pthread_mutex_t s_pendingRequestsMutex_socket3  = PTHREAD_MUTEX_INITIALIZER;
    210 static pthread_mutex_t s_writeMutex_socket3            = PTHREAD_MUTEX_INITIALIZER;
    211 static RequestInfo *s_pendingRequests_socket3          = NULL;
    212 #endif
    213 
    214 #if (SIM_COUNT >= 4)
    215 static struct ril_event s_commands_event_socket4;
    216 static struct ril_event s_listen_event_socket4;
    217 static SocketListenParam s_ril_param_socket4;
    218 
    219 static pthread_mutex_t s_pendingRequestsMutex_socket4  = PTHREAD_MUTEX_INITIALIZER;
    220 static pthread_mutex_t s_writeMutex_socket4            = PTHREAD_MUTEX_INITIALIZER;
    221 static RequestInfo *s_pendingRequests_socket4          = NULL;
    222 #endif
    223 
    224 static struct ril_event s_wake_timeout_event;
    225 static struct ril_event s_debug_event;
    226 
    227 
    228 static const struct timeval TIMEVAL_WAKE_TIMEOUT = {1,0};
    229 
    230 
    231 static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
    232 static pthread_cond_t s_startupCond = PTHREAD_COND_INITIALIZER;
    233 
    234 static pthread_mutex_t s_dispatchMutex = PTHREAD_MUTEX_INITIALIZER;
    235 static pthread_cond_t s_dispatchCond = PTHREAD_COND_INITIALIZER;
    236 
    237 static RequestInfo *s_toDispatchHead = NULL;
    238 static RequestInfo *s_toDispatchTail = NULL;
    239 
    240 static UserCallbackInfo *s_last_wake_timeout_info = NULL;
    241 
    242 static void *s_lastNITZTimeData = NULL;
    243 static size_t s_lastNITZTimeDataSize;
    244 
    245 #if RILC_LOG
    246     static char printBuf[PRINTBUF_SIZE];
    247 #endif
    248 
    249 /*******************************************************************/
    250 static int sendResponse (Parcel &p, RIL_SOCKET_ID socket_id);
    251 
    252 static void dispatchVoid (Parcel& p, RequestInfo *pRI);
    253 static void dispatchString (Parcel& p, RequestInfo *pRI);
    254 static void dispatchStrings (Parcel& p, RequestInfo *pRI);
    255 static void dispatchInts (Parcel& p, RequestInfo *pRI);
    256 static void dispatchDial (Parcel& p, RequestInfo *pRI);
    257 static void dispatchSIM_IO (Parcel& p, RequestInfo *pRI);
    258 static void dispatchSIM_APDU (Parcel& p, RequestInfo *pRI);
    259 static void dispatchCallForward(Parcel& p, RequestInfo *pRI);
    260 static void dispatchRaw(Parcel& p, RequestInfo *pRI);
    261 static void dispatchSmsWrite (Parcel &p, RequestInfo *pRI);
    262 static void dispatchDataCall (Parcel& p, RequestInfo *pRI);
    263 static void dispatchVoiceRadioTech (Parcel& p, RequestInfo *pRI);
    264 static void dispatchSetInitialAttachApn (Parcel& p, RequestInfo *pRI);
    265 static void dispatchCdmaSubscriptionSource (Parcel& p, RequestInfo *pRI);
    266 
    267 static void dispatchCdmaSms(Parcel &p, RequestInfo *pRI);
    268 static void dispatchImsSms(Parcel &p, RequestInfo *pRI);
    269 static void dispatchImsCdmaSms(Parcel &p, RequestInfo *pRI, uint8_t retry, int32_t messageRef);
    270 static void dispatchImsGsmSms(Parcel &p, RequestInfo *pRI, uint8_t retry, int32_t messageRef);
    271 static void dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI);
    272 static void dispatchGsmBrSmsCnf(Parcel &p, RequestInfo *pRI);
    273 static void dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI);
    274 static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI);
    275 static void dispatchNVReadItem(Parcel &p, RequestInfo *pRI);
    276 static void dispatchNVWriteItem(Parcel &p, RequestInfo *pRI);
    277 static void dispatchUiccSubscripton(Parcel &p, RequestInfo *pRI);
    278 static void dispatchSimAuthentication(Parcel &p, RequestInfo *pRI);
    279 static void dispatchDataProfile(Parcel &p, RequestInfo *pRI);
    280 static int responseInts(Parcel &p, void *response, size_t responselen);
    281 static int responseStrings(Parcel &p, void *response, size_t responselen);
    282 static int responseString(Parcel &p, void *response, size_t responselen);
    283 static int responseVoid(Parcel &p, void *response, size_t responselen);
    284 static int responseCallList(Parcel &p, void *response, size_t responselen);
    285 static int responseSMS(Parcel &p, void *response, size_t responselen);
    286 static int responseSIM_IO(Parcel &p, void *response, size_t responselen);
    287 static int responseCallForwards(Parcel &p, void *response, size_t responselen);
    288 static int responseDataCallList(Parcel &p, void *response, size_t responselen);
    289 static int responseSetupDataCall(Parcel &p, void *response, size_t responselen);
    290 static int responseRaw(Parcel &p, void *response, size_t responselen);
    291 static int responseSsn(Parcel &p, void *response, size_t responselen);
    292 static int responseSimStatus(Parcel &p, void *response, size_t responselen);
    293 static int responseGsmBrSmsCnf(Parcel &p, void *response, size_t responselen);
    294 static int responseCdmaBrSmsCnf(Parcel &p, void *response, size_t responselen);
    295 static int responseCdmaSms(Parcel &p, void *response, size_t responselen);
    296 static int responseCellList(Parcel &p, void *response, size_t responselen);
    297 static int responseCdmaInformationRecords(Parcel &p,void *response, size_t responselen);
    298 static int responseRilSignalStrength(Parcel &p,void *response, size_t responselen);
    299 static int responseCallRing(Parcel &p, void *response, size_t responselen);
    300 static int responseCdmaSignalInfoRecord(Parcel &p,void *response, size_t responselen);
    301 static int responseCdmaCallWaiting(Parcel &p,void *response, size_t responselen);
    302 static int responseSimRefresh(Parcel &p, void *response, size_t responselen);
    303 static int responseCellInfoList(Parcel &p, void *response, size_t responselen);
    304 static int responseHardwareConfig(Parcel &p, void *response, size_t responselen);
    305 static int responseDcRtInfo(Parcel &p, void *response, size_t responselen);
    306 
    307 static int decodeVoiceRadioTechnology (RIL_RadioState radioState);
    308 static int decodeCdmaSubscriptionSource (RIL_RadioState radioState);
    309 static RIL_RadioState processRadioState(RIL_RadioState newRadioState);
    310 
    311 #ifdef RIL_SHLIB
    312 #if defined(ANDROID_MULTI_SIM)
    313 extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
    314                                 size_t datalen, RIL_SOCKET_ID socket_id);
    315 #else
    316 extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
    317                                 size_t datalen);
    318 #endif
    319 #endif
    320 
    321 #if defined(ANDROID_MULTI_SIM)
    322 #define RIL_UNSOL_RESPONSE(a, b, c, d) RIL_onUnsolicitedResponse((a), (b), (c), (d))
    323 #define CALL_ONREQUEST(a, b, c, d, e) s_callbacks.onRequest((a), (b), (c), (d), (e))
    324 #define CALL_ONSTATEREQUEST(a) s_callbacks.onStateRequest(a)
    325 #else
    326 #define RIL_UNSOL_RESPONSE(a, b, c, d) RIL_onUnsolicitedResponse((a), (b), (c))
    327 #define CALL_ONREQUEST(a, b, c, d, e) s_callbacks.onRequest((a), (b), (c), (d))
    328 #define CALL_ONSTATEREQUEST(a) s_callbacks.onStateRequest()
    329 #endif
    330 
    331 static UserCallbackInfo * internalRequestTimedCallback
    332     (RIL_TimedCallback callback, void *param,
    333         const struct timeval *relativeTime);
    334 
    335 /** Index == requestNumber */
    336 static CommandInfo s_commands[] = {
    337 #include "ril_commands.h"
    338 };
    339 
    340 static UnsolResponseInfo s_unsolResponses[] = {
    341 #include "ril_unsol_commands.h"
    342 };
    343 
    344 /* For older RILs that do not support new commands RIL_REQUEST_VOICE_RADIO_TECH and
    345    RIL_UNSOL_VOICE_RADIO_TECH_CHANGED messages, decode the voice radio tech from
    346    radio state message and store it. Every time there is a change in Radio State
    347    check to see if voice radio tech changes and notify telephony
    348  */
    349 int voiceRadioTech = -1;
    350 
    351 /* For older RILs that do not support new commands RIL_REQUEST_GET_CDMA_SUBSCRIPTION_SOURCE
    352    and RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED messages, decode the subscription
    353    source from radio state and store it. Every time there is a change in Radio State
    354    check to see if subscription source changed and notify telephony
    355  */
    356 int cdmaSubscriptionSource = -1;
    357 
    358 /* For older RILs that do not send RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, decode the
    359    SIM/RUIM state from radio state and store it. Every time there is a change in Radio State,
    360    check to see if SIM/RUIM status changed and notify telephony
    361  */
    362 int simRuimStatus = -1;
    363 
    364 static char * RIL_getRilSocketName() {
    365     return rild;
    366 }
    367 
    368 extern "C"
    369 void RIL_setRilSocketName(char * s) {
    370     strncpy(rild, s, MAX_SOCKET_NAME_LENGTH);
    371 }
    372 
    373 static char *
    374 strdupReadString(Parcel &p) {
    375     size_t stringlen;
    376     const char16_t *s16;
    377 
    378     s16 = p.readString16Inplace(&stringlen);
    379 
    380     return strndup16to8(s16, stringlen);
    381 }
    382 
    383 static void writeStringToParcel(Parcel &p, const char *s) {
    384     char16_t *s16;
    385     size_t s16_len;
    386     s16 = strdup8to16(s, &s16_len);
    387     p.writeString16(s16, s16_len);
    388     free(s16);
    389 }
    390 
    391 
    392 static void
    393 memsetString (char *s) {
    394     if (s != NULL) {
    395         memset (s, 0, strlen(s));
    396     }
    397 }
    398 
    399 void   nullParcelReleaseFunction (const uint8_t* data, size_t dataSize,
    400                                     const size_t* objects, size_t objectsSize,
    401                                         void* cookie) {
    402     // do nothing -- the data reference lives longer than the Parcel object
    403 }
    404 
    405 /**
    406  * To be called from dispatch thread
    407  * Issue a single local request, ensuring that the response
    408  * is not sent back up to the command process
    409  */
    410 static void
    411 issueLocalRequest(int request, void *data, int len, RIL_SOCKET_ID socket_id) {
    412     RequestInfo *pRI;
    413     int ret;
    414     /* Hook for current context */
    415     /* pendingRequestsMutextHook refer to &s_pendingRequestsMutex */
    416     pthread_mutex_t* pendingRequestsMutexHook = &s_pendingRequestsMutex;
    417     /* pendingRequestsHook refer to &s_pendingRequests */
    418     RequestInfo**    pendingRequestsHook = &s_pendingRequests;
    419 
    420 #if (SIM_COUNT == 2)
    421     if (socket_id == RIL_SOCKET_2) {
    422         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket2;
    423         pendingRequestsHook = &s_pendingRequests_socket2;
    424     }
    425 #endif
    426 
    427     pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
    428 
    429     pRI->local = 1;
    430     pRI->token = 0xffffffff;        // token is not used in this context
    431     pRI->pCI = &(s_commands[request]);
    432     pRI->socket_id = socket_id;
    433 
    434     ret = pthread_mutex_lock(pendingRequestsMutexHook);
    435     assert (ret == 0);
    436 
    437     pRI->p_next = *pendingRequestsHook;
    438     *pendingRequestsHook = pRI;
    439 
    440     ret = pthread_mutex_unlock(pendingRequestsMutexHook);
    441     assert (ret == 0);
    442 
    443     RLOGD("C[locl]> %s", requestToString(request));
    444 
    445     CALL_ONREQUEST(request, data, len, pRI, pRI->socket_id);
    446 }
    447 
    448 
    449 
    450 static int
    451 processCommandBuffer(void *buffer, size_t buflen, RIL_SOCKET_ID socket_id) {
    452     Parcel p;
    453     status_t status;
    454     int32_t request;
    455     int32_t token;
    456     RequestInfo *pRI;
    457     int ret;
    458     /* Hook for current context */
    459     /* pendingRequestsMutextHook refer to &s_pendingRequestsMutex */
    460     pthread_mutex_t* pendingRequestsMutexHook = &s_pendingRequestsMutex;
    461     /* pendingRequestsHook refer to &s_pendingRequests */
    462     RequestInfo**    pendingRequestsHook = &s_pendingRequests;
    463 
    464     p.setData((uint8_t *) buffer, buflen);
    465 
    466     // status checked at end
    467     status = p.readInt32(&request);
    468     status = p.readInt32 (&token);
    469 
    470     RLOGD("SOCKET %s REQUEST: %s length:%d", rilSocketIdToString(socket_id), requestToString(request), buflen);
    471 
    472 #if (SIM_COUNT >= 2)
    473     if (socket_id == RIL_SOCKET_2) {
    474         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket2;
    475         pendingRequestsHook = &s_pendingRequests_socket2;
    476     }
    477 #if (SIM_COUNT >= 3)
    478     else if (socket_id == RIL_SOCKET_3) {
    479         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket3;
    480         pendingRequestsHook = &s_pendingRequests_socket3;
    481     }
    482 #endif
    483 #if (SIM_COUNT >= 4)
    484     else if (socket_id == RIL_SOCKET_4) {
    485         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket4;
    486         pendingRequestsHook = &s_pendingRequests_socket4;
    487     }
    488 #endif
    489 #endif
    490 
    491     if (status != NO_ERROR) {
    492         RLOGE("invalid request block");
    493         return 0;
    494     }
    495 
    496     if (request < 1 || request >= (int32_t)NUM_ELEMS(s_commands)) {
    497         Parcel pErr;
    498         RLOGE("unsupported request code %d token %d", request, token);
    499         // FIXME this should perhaps return a response
    500         pErr.writeInt32 (RESPONSE_SOLICITED);
    501         pErr.writeInt32 (token);
    502         pErr.writeInt32 (RIL_E_GENERIC_FAILURE);
    503 
    504         sendResponse(pErr, socket_id);
    505         return 0;
    506     }
    507 
    508 
    509     pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
    510 
    511     pRI->token = token;
    512     pRI->pCI = &(s_commands[request]);
    513     pRI->socket_id = socket_id;
    514 
    515     ret = pthread_mutex_lock(pendingRequestsMutexHook);
    516     assert (ret == 0);
    517 
    518     pRI->p_next = *pendingRequestsHook;
    519     *pendingRequestsHook = pRI;
    520 
    521     ret = pthread_mutex_unlock(pendingRequestsMutexHook);
    522     assert (ret == 0);
    523 
    524 /*    sLastDispatchedToken = token; */
    525 
    526     pRI->pCI->dispatchFunction(p, pRI);
    527 
    528     return 0;
    529 }
    530 
    531 static void
    532 invalidCommandBlock (RequestInfo *pRI) {
    533     RLOGE("invalid command block for token %d request %s",
    534                 pRI->token, requestToString(pRI->pCI->requestNumber));
    535 }
    536 
    537 /** Callee expects NULL */
    538 static void
    539 dispatchVoid (Parcel& p, RequestInfo *pRI) {
    540     clearPrintBuf;
    541     printRequest(pRI->token, pRI->pCI->requestNumber);
    542     CALL_ONREQUEST(pRI->pCI->requestNumber, NULL, 0, pRI, pRI->socket_id);
    543 }
    544 
    545 /** Callee expects const char * */
    546 static void
    547 dispatchString (Parcel& p, RequestInfo *pRI) {
    548     status_t status;
    549     size_t datalen;
    550     size_t stringlen;
    551     char *string8 = NULL;
    552 
    553     string8 = strdupReadString(p);
    554 
    555     startRequest;
    556     appendPrintBuf("%s%s", printBuf, string8);
    557     closeRequest;
    558     printRequest(pRI->token, pRI->pCI->requestNumber);
    559 
    560     CALL_ONREQUEST(pRI->pCI->requestNumber, string8,
    561                        sizeof(char *), pRI, pRI->socket_id);
    562 
    563 #ifdef MEMSET_FREED
    564     memsetString(string8);
    565 #endif
    566 
    567     free(string8);
    568     return;
    569 invalid:
    570     invalidCommandBlock(pRI);
    571     return;
    572 }
    573 
    574 /** Callee expects const char ** */
    575 static void
    576 dispatchStrings (Parcel &p, RequestInfo *pRI) {
    577     int32_t countStrings;
    578     status_t status;
    579     size_t datalen;
    580     char **pStrings;
    581 
    582     status = p.readInt32 (&countStrings);
    583 
    584     if (status != NO_ERROR) {
    585         goto invalid;
    586     }
    587 
    588     startRequest;
    589     if (countStrings == 0) {
    590         // just some non-null pointer
    591         pStrings = (char **)alloca(sizeof(char *));
    592         datalen = 0;
    593     } else if (((int)countStrings) == -1) {
    594         pStrings = NULL;
    595         datalen = 0;
    596     } else {
    597         datalen = sizeof(char *) * countStrings;
    598 
    599         pStrings = (char **)alloca(datalen);
    600 
    601         for (int i = 0 ; i < countStrings ; i++) {
    602             pStrings[i] = strdupReadString(p);
    603             appendPrintBuf("%s%s,", printBuf, pStrings[i]);
    604         }
    605     }
    606     removeLastChar;
    607     closeRequest;
    608     printRequest(pRI->token, pRI->pCI->requestNumber);
    609 
    610     CALL_ONREQUEST(pRI->pCI->requestNumber, pStrings, datalen, pRI, pRI->socket_id);
    611 
    612     if (pStrings != NULL) {
    613         for (int i = 0 ; i < countStrings ; i++) {
    614 #ifdef MEMSET_FREED
    615             memsetString (pStrings[i]);
    616 #endif
    617             free(pStrings[i]);
    618         }
    619 
    620 #ifdef MEMSET_FREED
    621         memset(pStrings, 0, datalen);
    622 #endif
    623     }
    624 
    625     return;
    626 invalid:
    627     invalidCommandBlock(pRI);
    628     return;
    629 }
    630 
    631 /** Callee expects const int * */
    632 static void
    633 dispatchInts (Parcel &p, RequestInfo *pRI) {
    634     int32_t count;
    635     status_t status;
    636     size_t datalen;
    637     int *pInts;
    638 
    639     status = p.readInt32 (&count);
    640 
    641     if (status != NO_ERROR || count == 0) {
    642         goto invalid;
    643     }
    644 
    645     datalen = sizeof(int) * count;
    646     pInts = (int *)alloca(datalen);
    647 
    648     startRequest;
    649     for (int i = 0 ; i < count ; i++) {
    650         int32_t t;
    651 
    652         status = p.readInt32(&t);
    653         pInts[i] = (int)t;
    654         appendPrintBuf("%s%d,", printBuf, t);
    655 
    656         if (status != NO_ERROR) {
    657             goto invalid;
    658         }
    659    }
    660    removeLastChar;
    661    closeRequest;
    662    printRequest(pRI->token, pRI->pCI->requestNumber);
    663 
    664    CALL_ONREQUEST(pRI->pCI->requestNumber, const_cast<int *>(pInts),
    665                        datalen, pRI, pRI->socket_id);
    666 
    667 #ifdef MEMSET_FREED
    668     memset(pInts, 0, datalen);
    669 #endif
    670 
    671     return;
    672 invalid:
    673     invalidCommandBlock(pRI);
    674     return;
    675 }
    676 
    677 
    678 /**
    679  * Callee expects const RIL_SMS_WriteArgs *
    680  * Payload is:
    681  *   int32_t status
    682  *   String pdu
    683  */
    684 static void
    685 dispatchSmsWrite (Parcel &p, RequestInfo *pRI) {
    686     RIL_SMS_WriteArgs args;
    687     int32_t t;
    688     status_t status;
    689 
    690     memset (&args, 0, sizeof(args));
    691 
    692     status = p.readInt32(&t);
    693     args.status = (int)t;
    694 
    695     args.pdu = strdupReadString(p);
    696 
    697     if (status != NO_ERROR || args.pdu == NULL) {
    698         goto invalid;
    699     }
    700 
    701     args.smsc = strdupReadString(p);
    702 
    703     startRequest;
    704     appendPrintBuf("%s%d,%s,smsc=%s", printBuf, args.status,
    705         (char*)args.pdu,  (char*)args.smsc);
    706     closeRequest;
    707     printRequest(pRI->token, pRI->pCI->requestNumber);
    708 
    709     CALL_ONREQUEST(pRI->pCI->requestNumber, &args, sizeof(args), pRI, pRI->socket_id);
    710 
    711 #ifdef MEMSET_FREED
    712     memsetString (args.pdu);
    713 #endif
    714 
    715     free (args.pdu);
    716 
    717 #ifdef MEMSET_FREED
    718     memset(&args, 0, sizeof(args));
    719 #endif
    720 
    721     return;
    722 invalid:
    723     invalidCommandBlock(pRI);
    724     return;
    725 }
    726 
    727 /**
    728  * Callee expects const RIL_Dial *
    729  * Payload is:
    730  *   String address
    731  *   int32_t clir
    732  */
    733 static void
    734 dispatchDial (Parcel &p, RequestInfo *pRI) {
    735     RIL_Dial dial;
    736     RIL_UUS_Info uusInfo;
    737     int32_t sizeOfDial;
    738     int32_t t;
    739     int32_t uusPresent;
    740     status_t status;
    741 
    742     memset (&dial, 0, sizeof(dial));
    743 
    744     dial.address = strdupReadString(p);
    745 
    746     status = p.readInt32(&t);
    747     dial.clir = (int)t;
    748 
    749     if (status != NO_ERROR || dial.address == NULL) {
    750         goto invalid;
    751     }
    752 
    753     if (s_callbacks.version < 3) { // Remove when partners upgrade to version 3
    754         uusPresent = 0;
    755         sizeOfDial = sizeof(dial) - sizeof(RIL_UUS_Info *);
    756     } else {
    757         status = p.readInt32(&uusPresent);
    758 
    759         if (status != NO_ERROR) {
    760             goto invalid;
    761         }
    762 
    763         if (uusPresent == 0) {
    764             dial.uusInfo = NULL;
    765         } else {
    766             int32_t len;
    767 
    768             memset(&uusInfo, 0, sizeof(RIL_UUS_Info));
    769 
    770             status = p.readInt32(&t);
    771             uusInfo.uusType = (RIL_UUS_Type) t;
    772 
    773             status = p.readInt32(&t);
    774             uusInfo.uusDcs = (RIL_UUS_DCS) t;
    775 
    776             status = p.readInt32(&len);
    777             if (status != NO_ERROR) {
    778                 goto invalid;
    779             }
    780 
    781             // The java code writes -1 for null arrays
    782             if (((int) len) == -1) {
    783                 uusInfo.uusData = NULL;
    784                 len = 0;
    785             } else {
    786                 uusInfo.uusData = (char*) p.readInplace(len);
    787             }
    788 
    789             uusInfo.uusLength = len;
    790             dial.uusInfo = &uusInfo;
    791         }
    792         sizeOfDial = sizeof(dial);
    793     }
    794 
    795     startRequest;
    796     appendPrintBuf("%snum=%s,clir=%d", printBuf, dial.address, dial.clir);
    797     if (uusPresent) {
    798         appendPrintBuf("%s,uusType=%d,uusDcs=%d,uusLen=%d", printBuf,
    799                 dial.uusInfo->uusType, dial.uusInfo->uusDcs,
    800                 dial.uusInfo->uusLength);
    801     }
    802     closeRequest;
    803     printRequest(pRI->token, pRI->pCI->requestNumber);
    804 
    805     CALL_ONREQUEST(pRI->pCI->requestNumber, &dial, sizeOfDial, pRI, pRI->socket_id);
    806 
    807 #ifdef MEMSET_FREED
    808     memsetString (dial.address);
    809 #endif
    810 
    811     free (dial.address);
    812 
    813 #ifdef MEMSET_FREED
    814     memset(&uusInfo, 0, sizeof(RIL_UUS_Info));
    815     memset(&dial, 0, sizeof(dial));
    816 #endif
    817 
    818     return;
    819 invalid:
    820     invalidCommandBlock(pRI);
    821     return;
    822 }
    823 
    824 /**
    825  * Callee expects const RIL_SIM_IO *
    826  * Payload is:
    827  *   int32_t command
    828  *   int32_t fileid
    829  *   String path
    830  *   int32_t p1, p2, p3
    831  *   String data
    832  *   String pin2
    833  *   String aidPtr
    834  */
    835 static void
    836 dispatchSIM_IO (Parcel &p, RequestInfo *pRI) {
    837     union RIL_SIM_IO {
    838         RIL_SIM_IO_v6 v6;
    839         RIL_SIM_IO_v5 v5;
    840     } simIO;
    841 
    842     int32_t t;
    843     int size;
    844     status_t status;
    845 
    846     memset (&simIO, 0, sizeof(simIO));
    847 
    848     // note we only check status at the end
    849 
    850     status = p.readInt32(&t);
    851     simIO.v6.command = (int)t;
    852 
    853     status = p.readInt32(&t);
    854     simIO.v6.fileid = (int)t;
    855 
    856     simIO.v6.path = strdupReadString(p);
    857 
    858     status = p.readInt32(&t);
    859     simIO.v6.p1 = (int)t;
    860 
    861     status = p.readInt32(&t);
    862     simIO.v6.p2 = (int)t;
    863 
    864     status = p.readInt32(&t);
    865     simIO.v6.p3 = (int)t;
    866 
    867     simIO.v6.data = strdupReadString(p);
    868     simIO.v6.pin2 = strdupReadString(p);
    869     simIO.v6.aidPtr = strdupReadString(p);
    870 
    871     startRequest;
    872     appendPrintBuf("%scmd=0x%X,efid=0x%X,path=%s,%d,%d,%d,%s,pin2=%s,aid=%s", printBuf,
    873         simIO.v6.command, simIO.v6.fileid, (char*)simIO.v6.path,
    874         simIO.v6.p1, simIO.v6.p2, simIO.v6.p3,
    875         (char*)simIO.v6.data,  (char*)simIO.v6.pin2, simIO.v6.aidPtr);
    876     closeRequest;
    877     printRequest(pRI->token, pRI->pCI->requestNumber);
    878 
    879     if (status != NO_ERROR) {
    880         goto invalid;
    881     }
    882 
    883     size = (s_callbacks.version < 6) ? sizeof(simIO.v5) : sizeof(simIO.v6);
    884     CALL_ONREQUEST(pRI->pCI->requestNumber, &simIO, size, pRI, pRI->socket_id);
    885 
    886 #ifdef MEMSET_FREED
    887     memsetString (simIO.v6.path);
    888     memsetString (simIO.v6.data);
    889     memsetString (simIO.v6.pin2);
    890     memsetString (simIO.v6.aidPtr);
    891 #endif
    892 
    893     free (simIO.v6.path);
    894     free (simIO.v6.data);
    895     free (simIO.v6.pin2);
    896     free (simIO.v6.aidPtr);
    897 
    898 #ifdef MEMSET_FREED
    899     memset(&simIO, 0, sizeof(simIO));
    900 #endif
    901 
    902     return;
    903 invalid:
    904     invalidCommandBlock(pRI);
    905     return;
    906 }
    907 
    908 /**
    909  * Callee expects const RIL_SIM_APDU *
    910  * Payload is:
    911  *   int32_t sessionid
    912  *   int32_t cla
    913  *   int32_t instruction
    914  *   int32_t p1, p2, p3
    915  *   String data
    916  */
    917 static void
    918 dispatchSIM_APDU (Parcel &p, RequestInfo *pRI) {
    919     int32_t t;
    920     status_t status;
    921     RIL_SIM_APDU apdu;
    922 
    923     memset (&apdu, 0, sizeof(RIL_SIM_APDU));
    924 
    925     // Note we only check status at the end. Any single failure leads to
    926     // subsequent reads filing.
    927     status = p.readInt32(&t);
    928     apdu.sessionid = (int)t;
    929 
    930     status = p.readInt32(&t);
    931     apdu.cla = (int)t;
    932 
    933     status = p.readInt32(&t);
    934     apdu.instruction = (int)t;
    935 
    936     status = p.readInt32(&t);
    937     apdu.p1 = (int)t;
    938 
    939     status = p.readInt32(&t);
    940     apdu.p2 = (int)t;
    941 
    942     status = p.readInt32(&t);
    943     apdu.p3 = (int)t;
    944 
    945     apdu.data = strdupReadString(p);
    946 
    947     startRequest;
    948     appendPrintBuf("%ssessionid=%d,cla=%d,ins=%d,p1=%d,p2=%d,p3=%d,data=%s",
    949         printBuf, apdu.sessionid, apdu.cla, apdu.instruction, apdu.p1, apdu.p2,
    950         apdu.p3, (char*)apdu.data);
    951     closeRequest;
    952     printRequest(pRI->token, pRI->pCI->requestNumber);
    953 
    954     if (status != NO_ERROR) {
    955         goto invalid;
    956     }
    957 
    958     CALL_ONREQUEST(pRI->pCI->requestNumber, &apdu, sizeof(RIL_SIM_APDU), pRI, pRI->socket_id);
    959 
    960 #ifdef MEMSET_FREED
    961     memsetString(apdu.data);
    962 #endif
    963     free(apdu.data);
    964 
    965 #ifdef MEMSET_FREED
    966     memset(&apdu, 0, sizeof(RIL_SIM_APDU));
    967 #endif
    968 
    969     return;
    970 invalid:
    971     invalidCommandBlock(pRI);
    972     return;
    973 }
    974 
    975 
    976 /**
    977  * Callee expects const RIL_CallForwardInfo *
    978  * Payload is:
    979  *  int32_t status/action
    980  *  int32_t reason
    981  *  int32_t serviceCode
    982  *  int32_t toa
    983  *  String number  (0 length -> null)
    984  *  int32_t timeSeconds
    985  */
    986 static void
    987 dispatchCallForward(Parcel &p, RequestInfo *pRI) {
    988     RIL_CallForwardInfo cff;
    989     int32_t t;
    990     status_t status;
    991 
    992     memset (&cff, 0, sizeof(cff));
    993 
    994     // note we only check status at the end
    995 
    996     status = p.readInt32(&t);
    997     cff.status = (int)t;
    998 
    999     status = p.readInt32(&t);
   1000     cff.reason = (int)t;
   1001 
   1002     status = p.readInt32(&t);
   1003     cff.serviceClass = (int)t;
   1004 
   1005     status = p.readInt32(&t);
   1006     cff.toa = (int)t;
   1007 
   1008     cff.number = strdupReadString(p);
   1009 
   1010     status = p.readInt32(&t);
   1011     cff.timeSeconds = (int)t;
   1012 
   1013     if (status != NO_ERROR) {
   1014         goto invalid;
   1015     }
   1016 
   1017     // special case: number 0-length fields is null
   1018 
   1019     if (cff.number != NULL && strlen (cff.number) == 0) {
   1020         cff.number = NULL;
   1021     }
   1022 
   1023     startRequest;
   1024     appendPrintBuf("%sstat=%d,reason=%d,serv=%d,toa=%d,%s,tout=%d", printBuf,
   1025         cff.status, cff.reason, cff.serviceClass, cff.toa,
   1026         (char*)cff.number, cff.timeSeconds);
   1027     closeRequest;
   1028     printRequest(pRI->token, pRI->pCI->requestNumber);
   1029 
   1030     CALL_ONREQUEST(pRI->pCI->requestNumber, &cff, sizeof(cff), pRI, pRI->socket_id);
   1031 
   1032 #ifdef MEMSET_FREED
   1033     memsetString(cff.number);
   1034 #endif
   1035 
   1036     free (cff.number);
   1037 
   1038 #ifdef MEMSET_FREED
   1039     memset(&cff, 0, sizeof(cff));
   1040 #endif
   1041 
   1042     return;
   1043 invalid:
   1044     invalidCommandBlock(pRI);
   1045     return;
   1046 }
   1047 
   1048 
   1049 static void
   1050 dispatchRaw(Parcel &p, RequestInfo *pRI) {
   1051     int32_t len;
   1052     status_t status;
   1053     const void *data;
   1054 
   1055     status = p.readInt32(&len);
   1056 
   1057     if (status != NO_ERROR) {
   1058         goto invalid;
   1059     }
   1060 
   1061     // The java code writes -1 for null arrays
   1062     if (((int)len) == -1) {
   1063         data = NULL;
   1064         len = 0;
   1065     }
   1066 
   1067     data = p.readInplace(len);
   1068 
   1069     startRequest;
   1070     appendPrintBuf("%sraw_size=%d", printBuf, len);
   1071     closeRequest;
   1072     printRequest(pRI->token, pRI->pCI->requestNumber);
   1073 
   1074     CALL_ONREQUEST(pRI->pCI->requestNumber, const_cast<void *>(data), len, pRI, pRI->socket_id);
   1075 
   1076     return;
   1077 invalid:
   1078     invalidCommandBlock(pRI);
   1079     return;
   1080 }
   1081 
   1082 static status_t
   1083 constructCdmaSms(Parcel &p, RequestInfo *pRI, RIL_CDMA_SMS_Message& rcsm) {
   1084     int32_t  t;
   1085     uint8_t ut;
   1086     status_t status;
   1087     int32_t digitCount;
   1088     int digitLimit;
   1089 
   1090     memset(&rcsm, 0, sizeof(rcsm));
   1091 
   1092     status = p.readInt32(&t);
   1093     rcsm.uTeleserviceID = (int) t;
   1094 
   1095     status = p.read(&ut,sizeof(ut));
   1096     rcsm.bIsServicePresent = (uint8_t) ut;
   1097 
   1098     status = p.readInt32(&t);
   1099     rcsm.uServicecategory = (int) t;
   1100 
   1101     status = p.readInt32(&t);
   1102     rcsm.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) t;
   1103 
   1104     status = p.readInt32(&t);
   1105     rcsm.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) t;
   1106 
   1107     status = p.readInt32(&t);
   1108     rcsm.sAddress.number_type = (RIL_CDMA_SMS_NumberType) t;
   1109 
   1110     status = p.readInt32(&t);
   1111     rcsm.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) t;
   1112 
   1113     status = p.read(&ut,sizeof(ut));
   1114     rcsm.sAddress.number_of_digits= (uint8_t) ut;
   1115 
   1116     digitLimit= MIN((rcsm.sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
   1117     for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
   1118         status = p.read(&ut,sizeof(ut));
   1119         rcsm.sAddress.digits[digitCount] = (uint8_t) ut;
   1120     }
   1121 
   1122     status = p.readInt32(&t);
   1123     rcsm.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) t;
   1124 
   1125     status = p.read(&ut,sizeof(ut));
   1126     rcsm.sSubAddress.odd = (uint8_t) ut;
   1127 
   1128     status = p.read(&ut,sizeof(ut));
   1129     rcsm.sSubAddress.number_of_digits = (uint8_t) ut;
   1130 
   1131     digitLimit= MIN((rcsm.sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
   1132     for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
   1133         status = p.read(&ut,sizeof(ut));
   1134         rcsm.sSubAddress.digits[digitCount] = (uint8_t) ut;
   1135     }
   1136 
   1137     status = p.readInt32(&t);
   1138     rcsm.uBearerDataLen = (int) t;
   1139 
   1140     digitLimit= MIN((rcsm.uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
   1141     for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
   1142         status = p.read(&ut, sizeof(ut));
   1143         rcsm.aBearerData[digitCount] = (uint8_t) ut;
   1144     }
   1145 
   1146     if (status != NO_ERROR) {
   1147         return status;
   1148     }
   1149 
   1150     startRequest;
   1151     appendPrintBuf("%suTeleserviceID=%d, bIsServicePresent=%d, uServicecategory=%d, \
   1152             sAddress.digit_mode=%d, sAddress.Number_mode=%d, sAddress.number_type=%d, ",
   1153             printBuf, rcsm.uTeleserviceID,rcsm.bIsServicePresent,rcsm.uServicecategory,
   1154             rcsm.sAddress.digit_mode, rcsm.sAddress.number_mode,rcsm.sAddress.number_type);
   1155     closeRequest;
   1156 
   1157     printRequest(pRI->token, pRI->pCI->requestNumber);
   1158 
   1159     return status;
   1160 }
   1161 
   1162 static void
   1163 dispatchCdmaSms(Parcel &p, RequestInfo *pRI) {
   1164     RIL_CDMA_SMS_Message rcsm;
   1165 
   1166     ALOGD("dispatchCdmaSms");
   1167     if (NO_ERROR != constructCdmaSms(p, pRI, rcsm)) {
   1168         goto invalid;
   1169     }
   1170 
   1171     CALL_ONREQUEST(pRI->pCI->requestNumber, &rcsm, sizeof(rcsm),pRI, pRI->socket_id);
   1172 
   1173 #ifdef MEMSET_FREED
   1174     memset(&rcsm, 0, sizeof(rcsm));
   1175 #endif
   1176 
   1177     return;
   1178 
   1179 invalid:
   1180     invalidCommandBlock(pRI);
   1181     return;
   1182 }
   1183 
   1184 static void
   1185 dispatchImsCdmaSms(Parcel &p, RequestInfo *pRI, uint8_t retry, int32_t messageRef) {
   1186     RIL_IMS_SMS_Message rism;
   1187     RIL_CDMA_SMS_Message rcsm;
   1188 
   1189     ALOGD("dispatchImsCdmaSms: retry=%d, messageRef=%d", retry, messageRef);
   1190 
   1191     if (NO_ERROR != constructCdmaSms(p, pRI, rcsm)) {
   1192         goto invalid;
   1193     }
   1194     memset(&rism, 0, sizeof(rism));
   1195     rism.tech = RADIO_TECH_3GPP2;
   1196     rism.retry = retry;
   1197     rism.messageRef = messageRef;
   1198     rism.message.cdmaMessage = &rcsm;
   1199 
   1200     CALL_ONREQUEST(pRI->pCI->requestNumber, &rism,
   1201             sizeof(RIL_RadioTechnologyFamily)+sizeof(uint8_t)+sizeof(int32_t)
   1202             +sizeof(rcsm),pRI, pRI->socket_id);
   1203 
   1204 #ifdef MEMSET_FREED
   1205     memset(&rcsm, 0, sizeof(rcsm));
   1206     memset(&rism, 0, sizeof(rism));
   1207 #endif
   1208 
   1209     return;
   1210 
   1211 invalid:
   1212     invalidCommandBlock(pRI);
   1213     return;
   1214 }
   1215 
   1216 static void
   1217 dispatchImsGsmSms(Parcel &p, RequestInfo *pRI, uint8_t retry, int32_t messageRef) {
   1218     RIL_IMS_SMS_Message rism;
   1219     int32_t countStrings;
   1220     status_t status;
   1221     size_t datalen;
   1222     char **pStrings;
   1223     ALOGD("dispatchImsGsmSms: retry=%d, messageRef=%d", retry, messageRef);
   1224 
   1225     status = p.readInt32 (&countStrings);
   1226 
   1227     if (status != NO_ERROR) {
   1228         goto invalid;
   1229     }
   1230 
   1231     memset(&rism, 0, sizeof(rism));
   1232     rism.tech = RADIO_TECH_3GPP;
   1233     rism.retry = retry;
   1234     rism.messageRef = messageRef;
   1235 
   1236     startRequest;
   1237     appendPrintBuf("%stech=%d, retry=%d, messageRef=%d, ", printBuf,
   1238                     (int)rism.tech, (int)rism.retry, rism.messageRef);
   1239     if (countStrings == 0) {
   1240         // just some non-null pointer
   1241         pStrings = (char **)alloca(sizeof(char *));
   1242         datalen = 0;
   1243     } else if (((int)countStrings) == -1) {
   1244         pStrings = NULL;
   1245         datalen = 0;
   1246     } else {
   1247         datalen = sizeof(char *) * countStrings;
   1248 
   1249         pStrings = (char **)alloca(datalen);
   1250 
   1251         for (int i = 0 ; i < countStrings ; i++) {
   1252             pStrings[i] = strdupReadString(p);
   1253             appendPrintBuf("%s%s,", printBuf, pStrings[i]);
   1254         }
   1255     }
   1256     removeLastChar;
   1257     closeRequest;
   1258     printRequest(pRI->token, pRI->pCI->requestNumber);
   1259 
   1260     rism.message.gsmMessage = pStrings;
   1261     CALL_ONREQUEST(pRI->pCI->requestNumber, &rism,
   1262             sizeof(RIL_RadioTechnologyFamily)+sizeof(uint8_t)+sizeof(int32_t)
   1263             +datalen, pRI, pRI->socket_id);
   1264 
   1265     if (pStrings != NULL) {
   1266         for (int i = 0 ; i < countStrings ; i++) {
   1267 #ifdef MEMSET_FREED
   1268             memsetString (pStrings[i]);
   1269 #endif
   1270             free(pStrings[i]);
   1271         }
   1272 
   1273 #ifdef MEMSET_FREED
   1274         memset(pStrings, 0, datalen);
   1275 #endif
   1276     }
   1277 
   1278 #ifdef MEMSET_FREED
   1279     memset(&rism, 0, sizeof(rism));
   1280 #endif
   1281     return;
   1282 invalid:
   1283     ALOGE("dispatchImsGsmSms invalid block");
   1284     invalidCommandBlock(pRI);
   1285     return;
   1286 }
   1287 
   1288 static void
   1289 dispatchImsSms(Parcel &p, RequestInfo *pRI) {
   1290     int32_t  t;
   1291     status_t status = p.readInt32(&t);
   1292     RIL_RadioTechnologyFamily format;
   1293     uint8_t retry;
   1294     int32_t messageRef;
   1295 
   1296     ALOGD("dispatchImsSms");
   1297     if (status != NO_ERROR) {
   1298         goto invalid;
   1299     }
   1300     format = (RIL_RadioTechnologyFamily) t;
   1301 
   1302     // read retry field
   1303     status = p.read(&retry,sizeof(retry));
   1304     if (status != NO_ERROR) {
   1305         goto invalid;
   1306     }
   1307     // read messageRef field
   1308     status = p.read(&messageRef,sizeof(messageRef));
   1309     if (status != NO_ERROR) {
   1310         goto invalid;
   1311     }
   1312 
   1313     if (RADIO_TECH_3GPP == format) {
   1314         dispatchImsGsmSms(p, pRI, retry, messageRef);
   1315     } else if (RADIO_TECH_3GPP2 == format) {
   1316         dispatchImsCdmaSms(p, pRI, retry, messageRef);
   1317     } else {
   1318         ALOGE("requestImsSendSMS invalid format value =%d", format);
   1319     }
   1320 
   1321     return;
   1322 
   1323 invalid:
   1324     invalidCommandBlock(pRI);
   1325     return;
   1326 }
   1327 
   1328 static void
   1329 dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI) {
   1330     RIL_CDMA_SMS_Ack rcsa;
   1331     int32_t  t;
   1332     status_t status;
   1333     int32_t digitCount;
   1334 
   1335     memset(&rcsa, 0, sizeof(rcsa));
   1336 
   1337     status = p.readInt32(&t);
   1338     rcsa.uErrorClass = (RIL_CDMA_SMS_ErrorClass) t;
   1339 
   1340     status = p.readInt32(&t);
   1341     rcsa.uSMSCauseCode = (int) t;
   1342 
   1343     if (status != NO_ERROR) {
   1344         goto invalid;
   1345     }
   1346 
   1347     startRequest;
   1348     appendPrintBuf("%suErrorClass=%d, uTLStatus=%d, ",
   1349             printBuf, rcsa.uErrorClass, rcsa.uSMSCauseCode);
   1350     closeRequest;
   1351 
   1352     printRequest(pRI->token, pRI->pCI->requestNumber);
   1353 
   1354     CALL_ONREQUEST(pRI->pCI->requestNumber, &rcsa, sizeof(rcsa),pRI, pRI->socket_id);
   1355 
   1356 #ifdef MEMSET_FREED
   1357     memset(&rcsa, 0, sizeof(rcsa));
   1358 #endif
   1359 
   1360     return;
   1361 
   1362 invalid:
   1363     invalidCommandBlock(pRI);
   1364     return;
   1365 }
   1366 
   1367 static void
   1368 dispatchGsmBrSmsCnf(Parcel &p, RequestInfo *pRI) {
   1369     int32_t t;
   1370     status_t status;
   1371     int32_t num;
   1372 
   1373     status = p.readInt32(&num);
   1374     if (status != NO_ERROR) {
   1375         goto invalid;
   1376     }
   1377 
   1378     {
   1379         RIL_GSM_BroadcastSmsConfigInfo gsmBci[num];
   1380         RIL_GSM_BroadcastSmsConfigInfo *gsmBciPtrs[num];
   1381 
   1382         startRequest;
   1383         for (int i = 0 ; i < num ; i++ ) {
   1384             gsmBciPtrs[i] = &gsmBci[i];
   1385 
   1386             status = p.readInt32(&t);
   1387             gsmBci[i].fromServiceId = (int) t;
   1388 
   1389             status = p.readInt32(&t);
   1390             gsmBci[i].toServiceId = (int) t;
   1391 
   1392             status = p.readInt32(&t);
   1393             gsmBci[i].fromCodeScheme = (int) t;
   1394 
   1395             status = p.readInt32(&t);
   1396             gsmBci[i].toCodeScheme = (int) t;
   1397 
   1398             status = p.readInt32(&t);
   1399             gsmBci[i].selected = (uint8_t) t;
   1400 
   1401             appendPrintBuf("%s [%d: fromServiceId=%d, toServiceId =%d, \
   1402                   fromCodeScheme=%d, toCodeScheme=%d, selected =%d]", printBuf, i,
   1403                   gsmBci[i].fromServiceId, gsmBci[i].toServiceId,
   1404                   gsmBci[i].fromCodeScheme, gsmBci[i].toCodeScheme,
   1405                   gsmBci[i].selected);
   1406         }
   1407         closeRequest;
   1408 
   1409         if (status != NO_ERROR) {
   1410             goto invalid;
   1411         }
   1412 
   1413         CALL_ONREQUEST(pRI->pCI->requestNumber,
   1414                               gsmBciPtrs,
   1415                               num * sizeof(RIL_GSM_BroadcastSmsConfigInfo *),
   1416                               pRI, pRI->socket_id);
   1417 
   1418 #ifdef MEMSET_FREED
   1419         memset(gsmBci, 0, num * sizeof(RIL_GSM_BroadcastSmsConfigInfo));
   1420         memset(gsmBciPtrs, 0, num * sizeof(RIL_GSM_BroadcastSmsConfigInfo *));
   1421 #endif
   1422     }
   1423 
   1424     return;
   1425 
   1426 invalid:
   1427     invalidCommandBlock(pRI);
   1428     return;
   1429 }
   1430 
   1431 static void
   1432 dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI) {
   1433     int32_t t;
   1434     status_t status;
   1435     int32_t num;
   1436 
   1437     status = p.readInt32(&num);
   1438     if (status != NO_ERROR) {
   1439         goto invalid;
   1440     }
   1441 
   1442     {
   1443         RIL_CDMA_BroadcastSmsConfigInfo cdmaBci[num];
   1444         RIL_CDMA_BroadcastSmsConfigInfo *cdmaBciPtrs[num];
   1445 
   1446         startRequest;
   1447         for (int i = 0 ; i < num ; i++ ) {
   1448             cdmaBciPtrs[i] = &cdmaBci[i];
   1449 
   1450             status = p.readInt32(&t);
   1451             cdmaBci[i].service_category = (int) t;
   1452 
   1453             status = p.readInt32(&t);
   1454             cdmaBci[i].language = (int) t;
   1455 
   1456             status = p.readInt32(&t);
   1457             cdmaBci[i].selected = (uint8_t) t;
   1458 
   1459             appendPrintBuf("%s [%d: service_category=%d, language =%d, \
   1460                   entries.bSelected =%d]", printBuf, i, cdmaBci[i].service_category,
   1461                   cdmaBci[i].language, cdmaBci[i].selected);
   1462         }
   1463         closeRequest;
   1464 
   1465         if (status != NO_ERROR) {
   1466             goto invalid;
   1467         }
   1468 
   1469         CALL_ONREQUEST(pRI->pCI->requestNumber,
   1470                               cdmaBciPtrs,
   1471                               num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo *),
   1472                               pRI, pRI->socket_id);
   1473 
   1474 #ifdef MEMSET_FREED
   1475         memset(cdmaBci, 0, num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo));
   1476         memset(cdmaBciPtrs, 0, num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo *));
   1477 #endif
   1478     }
   1479 
   1480     return;
   1481 
   1482 invalid:
   1483     invalidCommandBlock(pRI);
   1484     return;
   1485 }
   1486 
   1487 static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI) {
   1488     RIL_CDMA_SMS_WriteArgs rcsw;
   1489     int32_t  t;
   1490     uint32_t ut;
   1491     uint8_t  uct;
   1492     status_t status;
   1493     int32_t  digitCount;
   1494 
   1495     memset(&rcsw, 0, sizeof(rcsw));
   1496 
   1497     status = p.readInt32(&t);
   1498     rcsw.status = t;
   1499 
   1500     status = p.readInt32(&t);
   1501     rcsw.message.uTeleserviceID = (int) t;
   1502 
   1503     status = p.read(&uct,sizeof(uct));
   1504     rcsw.message.bIsServicePresent = (uint8_t) uct;
   1505 
   1506     status = p.readInt32(&t);
   1507     rcsw.message.uServicecategory = (int) t;
   1508 
   1509     status = p.readInt32(&t);
   1510     rcsw.message.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) t;
   1511 
   1512     status = p.readInt32(&t);
   1513     rcsw.message.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) t;
   1514 
   1515     status = p.readInt32(&t);
   1516     rcsw.message.sAddress.number_type = (RIL_CDMA_SMS_NumberType) t;
   1517 
   1518     status = p.readInt32(&t);
   1519     rcsw.message.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) t;
   1520 
   1521     status = p.read(&uct,sizeof(uct));
   1522     rcsw.message.sAddress.number_of_digits = (uint8_t) uct;
   1523 
   1524     for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_ADDRESS_MAX; digitCount ++) {
   1525         status = p.read(&uct,sizeof(uct));
   1526         rcsw.message.sAddress.digits[digitCount] = (uint8_t) uct;
   1527     }
   1528 
   1529     status = p.readInt32(&t);
   1530     rcsw.message.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) t;
   1531 
   1532     status = p.read(&uct,sizeof(uct));
   1533     rcsw.message.sSubAddress.odd = (uint8_t) uct;
   1534 
   1535     status = p.read(&uct,sizeof(uct));
   1536     rcsw.message.sSubAddress.number_of_digits = (uint8_t) uct;
   1537 
   1538     for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_SUBADDRESS_MAX; digitCount ++) {
   1539         status = p.read(&uct,sizeof(uct));
   1540         rcsw.message.sSubAddress.digits[digitCount] = (uint8_t) uct;
   1541     }
   1542 
   1543     status = p.readInt32(&t);
   1544     rcsw.message.uBearerDataLen = (int) t;
   1545 
   1546     for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_BEARER_DATA_MAX; digitCount ++) {
   1547         status = p.read(&uct, sizeof(uct));
   1548         rcsw.message.aBearerData[digitCount] = (uint8_t) uct;
   1549     }
   1550 
   1551     if (status != NO_ERROR) {
   1552         goto invalid;
   1553     }
   1554 
   1555     startRequest;
   1556     appendPrintBuf("%sstatus=%d, message.uTeleserviceID=%d, message.bIsServicePresent=%d, \
   1557             message.uServicecategory=%d, message.sAddress.digit_mode=%d, \
   1558             message.sAddress.number_mode=%d, \
   1559             message.sAddress.number_type=%d, ",
   1560             printBuf, rcsw.status, rcsw.message.uTeleserviceID, rcsw.message.bIsServicePresent,
   1561             rcsw.message.uServicecategory, rcsw.message.sAddress.digit_mode,
   1562             rcsw.message.sAddress.number_mode,
   1563             rcsw.message.sAddress.number_type);
   1564     closeRequest;
   1565 
   1566     printRequest(pRI->token, pRI->pCI->requestNumber);
   1567 
   1568     CALL_ONREQUEST(pRI->pCI->requestNumber, &rcsw, sizeof(rcsw),pRI, pRI->socket_id);
   1569 
   1570 #ifdef MEMSET_FREED
   1571     memset(&rcsw, 0, sizeof(rcsw));
   1572 #endif
   1573 
   1574     return;
   1575 
   1576 invalid:
   1577     invalidCommandBlock(pRI);
   1578     return;
   1579 
   1580 }
   1581 
   1582 // For backwards compatibility in RIL_REQUEST_SETUP_DATA_CALL.
   1583 // Version 4 of the RIL interface adds a new PDP type parameter to support
   1584 // IPv6 and dual-stack PDP contexts. When dealing with a previous version of
   1585 // RIL, remove the parameter from the request.
   1586 static void dispatchDataCall(Parcel& p, RequestInfo *pRI) {
   1587     // In RIL v3, REQUEST_SETUP_DATA_CALL takes 6 parameters.
   1588     const int numParamsRilV3 = 6;
   1589 
   1590     // The first bytes of the RIL parcel contain the request number and the
   1591     // serial number - see processCommandBuffer(). Copy them over too.
   1592     int pos = p.dataPosition();
   1593 
   1594     int numParams = p.readInt32();
   1595     if (s_callbacks.version < 4 && numParams > numParamsRilV3) {
   1596       Parcel p2;
   1597       p2.appendFrom(&p, 0, pos);
   1598       p2.writeInt32(numParamsRilV3);
   1599       for(int i = 0; i < numParamsRilV3; i++) {
   1600         p2.writeString16(p.readString16());
   1601       }
   1602       p2.setDataPosition(pos);
   1603       dispatchStrings(p2, pRI);
   1604     } else {
   1605       p.setDataPosition(pos);
   1606       dispatchStrings(p, pRI);
   1607     }
   1608 }
   1609 
   1610 // For backwards compatibility with RILs that dont support RIL_REQUEST_VOICE_RADIO_TECH.
   1611 // When all RILs handle this request, this function can be removed and
   1612 // the request can be sent directly to the RIL using dispatchVoid.
   1613 static void dispatchVoiceRadioTech(Parcel& p, RequestInfo *pRI) {
   1614     RIL_RadioState state = CALL_ONSTATEREQUEST((RIL_SOCKET_ID)pRI->socket_id);
   1615 
   1616     if ((RADIO_STATE_UNAVAILABLE == state) || (RADIO_STATE_OFF == state)) {
   1617         RIL_onRequestComplete(pRI, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
   1618     }
   1619 
   1620     // RILs that support RADIO_STATE_ON should support this request.
   1621     if (RADIO_STATE_ON == state) {
   1622         dispatchVoid(p, pRI);
   1623         return;
   1624     }
   1625 
   1626     // For Older RILs, that do not support RADIO_STATE_ON, assume that they
   1627     // will not support this new request either and decode Voice Radio Technology
   1628     // from Radio State
   1629     voiceRadioTech = decodeVoiceRadioTechnology(state);
   1630 
   1631     if (voiceRadioTech < 0)
   1632         RIL_onRequestComplete(pRI, RIL_E_GENERIC_FAILURE, NULL, 0);
   1633     else
   1634         RIL_onRequestComplete(pRI, RIL_E_SUCCESS, &voiceRadioTech, sizeof(int));
   1635 }
   1636 
   1637 // For backwards compatibility in RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:.
   1638 // When all RILs handle this request, this function can be removed and
   1639 // the request can be sent directly to the RIL using dispatchVoid.
   1640 static void dispatchCdmaSubscriptionSource(Parcel& p, RequestInfo *pRI) {
   1641     RIL_RadioState state = CALL_ONSTATEREQUEST((RIL_SOCKET_ID)pRI->socket_id);
   1642 
   1643     if ((RADIO_STATE_UNAVAILABLE == state) || (RADIO_STATE_OFF == state)) {
   1644         RIL_onRequestComplete(pRI, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
   1645     }
   1646 
   1647     // RILs that support RADIO_STATE_ON should support this request.
   1648     if (RADIO_STATE_ON == state) {
   1649         dispatchVoid(p, pRI);
   1650         return;
   1651     }
   1652 
   1653     // For Older RILs, that do not support RADIO_STATE_ON, assume that they
   1654     // will not support this new request either and decode CDMA Subscription Source
   1655     // from Radio State
   1656     cdmaSubscriptionSource = decodeCdmaSubscriptionSource(state);
   1657 
   1658     if (cdmaSubscriptionSource < 0)
   1659         RIL_onRequestComplete(pRI, RIL_E_GENERIC_FAILURE, NULL, 0);
   1660     else
   1661         RIL_onRequestComplete(pRI, RIL_E_SUCCESS, &cdmaSubscriptionSource, sizeof(int));
   1662 }
   1663 
   1664 static void dispatchSetInitialAttachApn(Parcel &p, RequestInfo *pRI)
   1665 {
   1666     RIL_InitialAttachApn pf;
   1667     int32_t  t;
   1668     status_t status;
   1669 
   1670     memset(&pf, 0, sizeof(pf));
   1671 
   1672     pf.apn = strdupReadString(p);
   1673     pf.protocol = strdupReadString(p);
   1674 
   1675     status = p.readInt32(&t);
   1676     pf.authtype = (int) t;
   1677 
   1678     pf.username = strdupReadString(p);
   1679     pf.password = strdupReadString(p);
   1680 
   1681     startRequest;
   1682     appendPrintBuf("%sapn=%s, protocol=%s, authtype=%d, username=%s, password=%s",
   1683             printBuf, pf.apn, pf.protocol, pf.authtype, pf.username, pf.password);
   1684     closeRequest;
   1685     printRequest(pRI->token, pRI->pCI->requestNumber);
   1686 
   1687     if (status != NO_ERROR) {
   1688         goto invalid;
   1689     }
   1690     CALL_ONREQUEST(pRI->pCI->requestNumber, &pf, sizeof(pf), pRI, pRI->socket_id);
   1691 
   1692 #ifdef MEMSET_FREED
   1693     memsetString(pf.apn);
   1694     memsetString(pf.protocol);
   1695     memsetString(pf.username);
   1696     memsetString(pf.password);
   1697 #endif
   1698 
   1699     free(pf.apn);
   1700     free(pf.protocol);
   1701     free(pf.username);
   1702     free(pf.password);
   1703 
   1704 #ifdef MEMSET_FREED
   1705     memset(&pf, 0, sizeof(pf));
   1706 #endif
   1707 
   1708     return;
   1709 invalid:
   1710     invalidCommandBlock(pRI);
   1711     return;
   1712 }
   1713 
   1714 static void dispatchNVReadItem(Parcel &p, RequestInfo *pRI) {
   1715     RIL_NV_ReadItem nvri;
   1716     int32_t  t;
   1717     status_t status;
   1718 
   1719     memset(&nvri, 0, sizeof(nvri));
   1720 
   1721     status = p.readInt32(&t);
   1722     nvri.itemID = (RIL_NV_Item) t;
   1723 
   1724     if (status != NO_ERROR) {
   1725         goto invalid;
   1726     }
   1727 
   1728     startRequest;
   1729     appendPrintBuf("%snvri.itemID=%d, ", printBuf, nvri.itemID);
   1730     closeRequest;
   1731 
   1732     printRequest(pRI->token, pRI->pCI->requestNumber);
   1733 
   1734     CALL_ONREQUEST(pRI->pCI->requestNumber, &nvri, sizeof(nvri), pRI, pRI->socket_id);
   1735 
   1736 #ifdef MEMSET_FREED
   1737     memset(&nvri, 0, sizeof(nvri));
   1738 #endif
   1739 
   1740     return;
   1741 
   1742 invalid:
   1743     invalidCommandBlock(pRI);
   1744     return;
   1745 }
   1746 
   1747 static void dispatchNVWriteItem(Parcel &p, RequestInfo *pRI) {
   1748     RIL_NV_WriteItem nvwi;
   1749     int32_t  t;
   1750     status_t status;
   1751 
   1752     memset(&nvwi, 0, sizeof(nvwi));
   1753 
   1754     status = p.readInt32(&t);
   1755     nvwi.itemID = (RIL_NV_Item) t;
   1756 
   1757     nvwi.value = strdupReadString(p);
   1758 
   1759     if (status != NO_ERROR || nvwi.value == NULL) {
   1760         goto invalid;
   1761     }
   1762 
   1763     startRequest;
   1764     appendPrintBuf("%snvwi.itemID=%d, value=%s, ", printBuf, nvwi.itemID,
   1765             nvwi.value);
   1766     closeRequest;
   1767 
   1768     printRequest(pRI->token, pRI->pCI->requestNumber);
   1769 
   1770     CALL_ONREQUEST(pRI->pCI->requestNumber, &nvwi, sizeof(nvwi), pRI, pRI->socket_id);
   1771 
   1772 #ifdef MEMSET_FREED
   1773     memsetString(nvwi.value);
   1774 #endif
   1775 
   1776     free(nvwi.value);
   1777 
   1778 #ifdef MEMSET_FREED
   1779     memset(&nvwi, 0, sizeof(nvwi));
   1780 #endif
   1781 
   1782     return;
   1783 
   1784 invalid:
   1785     invalidCommandBlock(pRI);
   1786     return;
   1787 }
   1788 
   1789 
   1790 static void dispatchUiccSubscripton(Parcel &p, RequestInfo *pRI) {
   1791     RIL_SelectUiccSub uicc_sub;
   1792     status_t status;
   1793     int32_t  t;
   1794     memset(&uicc_sub, 0, sizeof(uicc_sub));
   1795 
   1796     status = p.readInt32(&t);
   1797     if (status != NO_ERROR) {
   1798         goto invalid;
   1799     }
   1800     uicc_sub.slot = (int) t;
   1801 
   1802     status = p.readInt32(&t);
   1803     if (status != NO_ERROR) {
   1804         goto invalid;
   1805     }
   1806     uicc_sub.app_index = (int) t;
   1807 
   1808     status = p.readInt32(&t);
   1809     if (status != NO_ERROR) {
   1810         goto invalid;
   1811     }
   1812     uicc_sub.sub_type = (RIL_SubscriptionType) t;
   1813 
   1814     status = p.readInt32(&t);
   1815     if (status != NO_ERROR) {
   1816         goto invalid;
   1817     }
   1818     uicc_sub.act_status = (RIL_UiccSubActStatus) t;
   1819 
   1820     startRequest;
   1821     appendPrintBuf("slot=%d, app_index=%d, act_status = %d", uicc_sub.slot, uicc_sub.app_index,
   1822             uicc_sub.act_status);
   1823     RLOGD("dispatchUiccSubscription, slot=%d, app_index=%d, act_status = %d", uicc_sub.slot,
   1824             uicc_sub.app_index, uicc_sub.act_status);
   1825     closeRequest;
   1826     printRequest(pRI->token, pRI->pCI->requestNumber);
   1827 
   1828     CALL_ONREQUEST(pRI->pCI->requestNumber, &uicc_sub, sizeof(uicc_sub), pRI, pRI->socket_id);
   1829 
   1830 #ifdef MEMSET_FREED
   1831     memset(&uicc_sub, 0, sizeof(uicc_sub));
   1832 #endif
   1833     return;
   1834 
   1835 invalid:
   1836     invalidCommandBlock(pRI);
   1837     return;
   1838 }
   1839 
   1840 static void dispatchSimAuthentication(Parcel &p, RequestInfo *pRI)
   1841 {
   1842     RIL_SimAuthentication pf;
   1843     int32_t  t;
   1844     status_t status;
   1845 
   1846     memset(&pf, 0, sizeof(pf));
   1847 
   1848     status = p.readInt32(&t);
   1849     pf.authContext = (int) t;
   1850     pf.authData = strdupReadString(p);
   1851     pf.aid = strdupReadString(p);
   1852 
   1853     startRequest;
   1854     appendPrintBuf("authContext=%s, authData=%s, aid=%s", pf.authContext, pf.authData, pf.aid);
   1855     closeRequest;
   1856     printRequest(pRI->token, pRI->pCI->requestNumber);
   1857 
   1858     if (status != NO_ERROR) {
   1859         goto invalid;
   1860     }
   1861     CALL_ONREQUEST(pRI->pCI->requestNumber, &pf, sizeof(pf), pRI, pRI->socket_id);
   1862 
   1863 #ifdef MEMSET_FREED
   1864     memsetString(pf.authData);
   1865     memsetString(pf.aid);
   1866 #endif
   1867 
   1868     free(pf.authData);
   1869     free(pf.aid);
   1870 
   1871 #ifdef MEMSET_FREED
   1872     memset(&pf, 0, sizeof(pf));
   1873 #endif
   1874 
   1875     return;
   1876 invalid:
   1877     invalidCommandBlock(pRI);
   1878     return;
   1879 }
   1880 
   1881 static void dispatchDataProfile(Parcel &p, RequestInfo *pRI) {
   1882     int32_t t;
   1883     status_t status;
   1884     int32_t num;
   1885 
   1886     status = p.readInt32(&num);
   1887     if (status != NO_ERROR) {
   1888         goto invalid;
   1889     }
   1890 
   1891     {
   1892         RIL_DataProfileInfo dataProfiles[num];
   1893         RIL_DataProfileInfo *dataProfilePtrs[num];
   1894 
   1895         startRequest;
   1896         for (int i = 0 ; i < num ; i++ ) {
   1897             dataProfilePtrs[i] = &dataProfiles[i];
   1898 
   1899             status = p.readInt32(&t);
   1900             dataProfiles[i].profileId = (int) t;
   1901 
   1902             dataProfiles[i].apn = strdupReadString(p);
   1903             dataProfiles[i].protocol = strdupReadString(p);
   1904             status = p.readInt32(&t);
   1905             dataProfiles[i].authType = (int) t;
   1906 
   1907             dataProfiles[i].user = strdupReadString(p);
   1908             dataProfiles[i].password = strdupReadString(p);
   1909 
   1910             status = p.readInt32(&t);
   1911             dataProfiles[i].type = (int) t;
   1912 
   1913             status = p.readInt32(&t);
   1914             dataProfiles[i].maxConnsTime = (int) t;
   1915             status = p.readInt32(&t);
   1916             dataProfiles[i].maxConns = (int) t;
   1917             status = p.readInt32(&t);
   1918             dataProfiles[i].waitTime = (int) t;
   1919 
   1920             status = p.readInt32(&t);
   1921             dataProfiles[i].enabled = (int) t;
   1922 
   1923             appendPrintBuf("%s [%d: profileId=%d, apn =%s, protocol =%s, authType =%d, \
   1924                   user =%s, password =%s, type =%d, maxConnsTime =%d, maxConns =%d, \
   1925                   waitTime =%d, enabled =%d]", printBuf, i, dataProfiles[i].profileId,
   1926                   dataProfiles[i].apn, dataProfiles[i].protocol, dataProfiles[i].authType,
   1927                   dataProfiles[i].user, dataProfiles[i].password, dataProfiles[i].type,
   1928                   dataProfiles[i].maxConnsTime, dataProfiles[i].maxConns,
   1929                   dataProfiles[i].waitTime, dataProfiles[i].enabled);
   1930         }
   1931         closeRequest;
   1932         printRequest(pRI->token, pRI->pCI->requestNumber);
   1933 
   1934         if (status != NO_ERROR) {
   1935             goto invalid;
   1936         }
   1937         CALL_ONREQUEST(pRI->pCI->requestNumber,
   1938                               dataProfilePtrs,
   1939                               num * sizeof(RIL_DataProfileInfo *),
   1940                               pRI, pRI->socket_id);
   1941 
   1942 #ifdef MEMSET_FREED
   1943         memset(dataProfiles, 0, num * sizeof(RIL_DataProfileInfo));
   1944         memset(dataProfilePtrs, 0, num * sizeof(RIL_DataProfileInfo *));
   1945 #endif
   1946     }
   1947 
   1948     return;
   1949 
   1950 invalid:
   1951     invalidCommandBlock(pRI);
   1952     return;
   1953 }
   1954 
   1955 static int
   1956 blockingWrite(int fd, const void *buffer, size_t len) {
   1957     size_t writeOffset = 0;
   1958     const uint8_t *toWrite;
   1959 
   1960     toWrite = (const uint8_t *)buffer;
   1961 
   1962     while (writeOffset < len) {
   1963         ssize_t written;
   1964         do {
   1965             written = write (fd, toWrite + writeOffset,
   1966                                 len - writeOffset);
   1967         } while (written < 0 && ((errno == EINTR) || (errno == EAGAIN)));
   1968 
   1969         if (written >= 0) {
   1970             writeOffset += written;
   1971         } else {   // written < 0
   1972             RLOGE ("RIL Response: unexpected error on write errno:%d", errno);
   1973             close(fd);
   1974             return -1;
   1975         }
   1976     }
   1977 
   1978     return 0;
   1979 }
   1980 
   1981 static int
   1982 sendResponseRaw (const void *data, size_t dataSize, RIL_SOCKET_ID socket_id) {
   1983     int fd = s_ril_param_socket.fdCommand;
   1984     int ret;
   1985     uint32_t header;
   1986     pthread_mutex_t * writeMutexHook = &s_writeMutex;
   1987 
   1988     RLOGE("Send Response to %s", rilSocketIdToString(socket_id));
   1989 
   1990 #if (SIM_COUNT >= 2)
   1991     if (socket_id == RIL_SOCKET_2) {
   1992         fd = s_ril_param_socket2.fdCommand;
   1993         writeMutexHook = &s_writeMutex_socket2;
   1994     }
   1995 #if (SIM_COUNT >= 3)
   1996     else if (socket_id == RIL_SOCKET_3) {
   1997         fd = s_ril_param_socket3.fdCommand;
   1998         writeMutexHook = &s_writeMutex_socket3;
   1999     }
   2000 #endif
   2001 #if (SIM_COUNT >= 4)
   2002     else if (socket_id == RIL_SOCKET_4) {
   2003         fd = s_ril_param_socket4.fdCommand;
   2004         writeMutexHook = &s_writeMutex_socket4;
   2005     }
   2006 #endif
   2007 #endif
   2008     if (fd < 0) {
   2009         return -1;
   2010     }
   2011 
   2012     if (dataSize > MAX_COMMAND_BYTES) {
   2013         RLOGE("RIL: packet larger than %u (%u)",
   2014                 MAX_COMMAND_BYTES, (unsigned int )dataSize);
   2015 
   2016         return -1;
   2017     }
   2018 
   2019     pthread_mutex_lock(writeMutexHook);
   2020 
   2021     header = htonl(dataSize);
   2022 
   2023     ret = blockingWrite(fd, (void *)&header, sizeof(header));
   2024 
   2025     if (ret < 0) {
   2026         pthread_mutex_unlock(writeMutexHook);
   2027         return ret;
   2028     }
   2029 
   2030     ret = blockingWrite(fd, data, dataSize);
   2031 
   2032     if (ret < 0) {
   2033         pthread_mutex_unlock(writeMutexHook);
   2034         return ret;
   2035     }
   2036 
   2037     pthread_mutex_unlock(writeMutexHook);
   2038 
   2039     return 0;
   2040 }
   2041 
   2042 static int
   2043 sendResponse (Parcel &p, RIL_SOCKET_ID socket_id) {
   2044     printResponse;
   2045     return sendResponseRaw(p.data(), p.dataSize(), socket_id);
   2046 }
   2047 
   2048 /** response is an int* pointing to an array of ints */
   2049 
   2050 static int
   2051 responseInts(Parcel &p, void *response, size_t responselen) {
   2052     int numInts;
   2053 
   2054     if (response == NULL && responselen != 0) {
   2055         RLOGE("invalid response: NULL");
   2056         return RIL_ERRNO_INVALID_RESPONSE;
   2057     }
   2058     if (responselen % sizeof(int) != 0) {
   2059         RLOGE("responseInts: invalid response length %d expected multiple of %d\n",
   2060             (int)responselen, (int)sizeof(int));
   2061         return RIL_ERRNO_INVALID_RESPONSE;
   2062     }
   2063 
   2064     int *p_int = (int *) response;
   2065 
   2066     numInts = responselen / sizeof(int);
   2067     p.writeInt32 (numInts);
   2068 
   2069     /* each int*/
   2070     startResponse;
   2071     for (int i = 0 ; i < numInts ; i++) {
   2072         appendPrintBuf("%s%d,", printBuf, p_int[i]);
   2073         p.writeInt32(p_int[i]);
   2074     }
   2075     removeLastChar;
   2076     closeResponse;
   2077 
   2078     return 0;
   2079 }
   2080 
   2081 /** response is a char **, pointing to an array of char *'s
   2082     The parcel will begin with the version */
   2083 static int responseStringsWithVersion(int version, Parcel &p, void *response, size_t responselen) {
   2084     p.writeInt32(version);
   2085     return responseStrings(p, response, responselen);
   2086 }
   2087 
   2088 /** response is a char **, pointing to an array of char *'s */
   2089 static int responseStrings(Parcel &p, void *response, size_t responselen) {
   2090     int numStrings;
   2091 
   2092     if (response == NULL && responselen != 0) {
   2093         RLOGE("invalid response: NULL");
   2094         return RIL_ERRNO_INVALID_RESPONSE;
   2095     }
   2096     if (responselen % sizeof(char *) != 0) {
   2097         RLOGE("responseStrings: invalid response length %d expected multiple of %d\n",
   2098             (int)responselen, (int)sizeof(char *));
   2099         return RIL_ERRNO_INVALID_RESPONSE;
   2100     }
   2101 
   2102     if (response == NULL) {
   2103         p.writeInt32 (0);
   2104     } else {
   2105         char **p_cur = (char **) response;
   2106 
   2107         numStrings = responselen / sizeof(char *);
   2108         p.writeInt32 (numStrings);
   2109 
   2110         /* each string*/
   2111         startResponse;
   2112         for (int i = 0 ; i < numStrings ; i++) {
   2113             appendPrintBuf("%s%s,", printBuf, (char*)p_cur[i]);
   2114             writeStringToParcel (p, p_cur[i]);
   2115         }
   2116         removeLastChar;
   2117         closeResponse;
   2118     }
   2119     return 0;
   2120 }
   2121 
   2122 
   2123 /**
   2124  * NULL strings are accepted
   2125  * FIXME currently ignores responselen
   2126  */
   2127 static int responseString(Parcel &p, void *response, size_t responselen) {
   2128     /* one string only */
   2129     startResponse;
   2130     appendPrintBuf("%s%s", printBuf, (char*)response);
   2131     closeResponse;
   2132 
   2133     writeStringToParcel(p, (const char *)response);
   2134 
   2135     return 0;
   2136 }
   2137 
   2138 static int responseVoid(Parcel &p, void *response, size_t responselen) {
   2139     startResponse;
   2140     removeLastChar;
   2141     return 0;
   2142 }
   2143 
   2144 static int responseCallList(Parcel &p, void *response, size_t responselen) {
   2145     int num;
   2146 
   2147     if (response == NULL && responselen != 0) {
   2148         RLOGE("invalid response: NULL");
   2149         return RIL_ERRNO_INVALID_RESPONSE;
   2150     }
   2151 
   2152     if (responselen % sizeof (RIL_Call *) != 0) {
   2153         RLOGE("responseCallList: invalid response length %d expected multiple of %d\n",
   2154             (int)responselen, (int)sizeof (RIL_Call *));
   2155         return RIL_ERRNO_INVALID_RESPONSE;
   2156     }
   2157 
   2158     startResponse;
   2159     /* number of call info's */
   2160     num = responselen / sizeof(RIL_Call *);
   2161     p.writeInt32(num);
   2162 
   2163     for (int i = 0 ; i < num ; i++) {
   2164         RIL_Call *p_cur = ((RIL_Call **) response)[i];
   2165         /* each call info */
   2166         p.writeInt32(p_cur->state);
   2167         p.writeInt32(p_cur->index);
   2168         p.writeInt32(p_cur->toa);
   2169         p.writeInt32(p_cur->isMpty);
   2170         p.writeInt32(p_cur->isMT);
   2171         p.writeInt32(p_cur->als);
   2172         p.writeInt32(p_cur->isVoice);
   2173         p.writeInt32(p_cur->isVoicePrivacy);
   2174         writeStringToParcel(p, p_cur->number);
   2175         p.writeInt32(p_cur->numberPresentation);
   2176         writeStringToParcel(p, p_cur->name);
   2177         p.writeInt32(p_cur->namePresentation);
   2178         // Remove when partners upgrade to version 3
   2179         if ((s_callbacks.version < 3) || (p_cur->uusInfo == NULL || p_cur->uusInfo->uusData == NULL)) {
   2180             p.writeInt32(0); /* UUS Information is absent */
   2181         } else {
   2182             RIL_UUS_Info *uusInfo = p_cur->uusInfo;
   2183             p.writeInt32(1); /* UUS Information is present */
   2184             p.writeInt32(uusInfo->uusType);
   2185             p.writeInt32(uusInfo->uusDcs);
   2186             p.writeInt32(uusInfo->uusLength);
   2187             p.write(uusInfo->uusData, uusInfo->uusLength);
   2188         }
   2189         appendPrintBuf("%s[id=%d,%s,toa=%d,",
   2190             printBuf,
   2191             p_cur->index,
   2192             callStateToString(p_cur->state),
   2193             p_cur->toa);
   2194         appendPrintBuf("%s%s,%s,als=%d,%s,%s,",
   2195             printBuf,
   2196             (p_cur->isMpty)?"conf":"norm",
   2197             (p_cur->isMT)?"mt":"mo",
   2198             p_cur->als,
   2199             (p_cur->isVoice)?"voc":"nonvoc",
   2200             (p_cur->isVoicePrivacy)?"evp":"noevp");
   2201         appendPrintBuf("%s%s,cli=%d,name='%s',%d]",
   2202             printBuf,
   2203             p_cur->number,
   2204             p_cur->numberPresentation,
   2205             p_cur->name,
   2206             p_cur->namePresentation);
   2207     }
   2208     removeLastChar;
   2209     closeResponse;
   2210 
   2211     return 0;
   2212 }
   2213 
   2214 static int responseSMS(Parcel &p, void *response, size_t responselen) {
   2215     if (response == NULL) {
   2216         RLOGE("invalid response: NULL");
   2217         return RIL_ERRNO_INVALID_RESPONSE;
   2218     }
   2219 
   2220     if (responselen != sizeof (RIL_SMS_Response) ) {
   2221         RLOGE("invalid response length %d expected %d",
   2222                 (int)responselen, (int)sizeof (RIL_SMS_Response));
   2223         return RIL_ERRNO_INVALID_RESPONSE;
   2224     }
   2225 
   2226     RIL_SMS_Response *p_cur = (RIL_SMS_Response *) response;
   2227 
   2228     p.writeInt32(p_cur->messageRef);
   2229     writeStringToParcel(p, p_cur->ackPDU);
   2230     p.writeInt32(p_cur->errorCode);
   2231 
   2232     startResponse;
   2233     appendPrintBuf("%s%d,%s,%d", printBuf, p_cur->messageRef,
   2234         (char*)p_cur->ackPDU, p_cur->errorCode);
   2235     closeResponse;
   2236 
   2237     return 0;
   2238 }
   2239 
   2240 static int responseDataCallListV4(Parcel &p, void *response, size_t responselen)
   2241 {
   2242     if (response == NULL && responselen != 0) {
   2243         RLOGE("invalid response: NULL");
   2244         return RIL_ERRNO_INVALID_RESPONSE;
   2245     }
   2246 
   2247     if (responselen % sizeof(RIL_Data_Call_Response_v4) != 0) {
   2248         RLOGE("responseDataCallListV4: invalid response length %d expected multiple of %d",
   2249                 (int)responselen, (int)sizeof(RIL_Data_Call_Response_v4));
   2250         return RIL_ERRNO_INVALID_RESPONSE;
   2251     }
   2252 
   2253     // Write version
   2254     p.writeInt32(4);
   2255 
   2256     int num = responselen / sizeof(RIL_Data_Call_Response_v4);
   2257     p.writeInt32(num);
   2258 
   2259     RIL_Data_Call_Response_v4 *p_cur = (RIL_Data_Call_Response_v4 *) response;
   2260     startResponse;
   2261     int i;
   2262     for (i = 0; i < num; i++) {
   2263         p.writeInt32(p_cur[i].cid);
   2264         p.writeInt32(p_cur[i].active);
   2265         writeStringToParcel(p, p_cur[i].type);
   2266         // apn is not used, so don't send.
   2267         writeStringToParcel(p, p_cur[i].address);
   2268         appendPrintBuf("%s[cid=%d,%s,%s,%s],", printBuf,
   2269             p_cur[i].cid,
   2270             (p_cur[i].active==0)?"down":"up",
   2271             (char*)p_cur[i].type,
   2272             (char*)p_cur[i].address);
   2273     }
   2274     removeLastChar;
   2275     closeResponse;
   2276 
   2277     return 0;
   2278 }
   2279 
   2280 static int responseDataCallListV6(Parcel &p, void *response, size_t responselen)
   2281 {
   2282     if (response == NULL && responselen != 0) {
   2283         RLOGE("invalid response: NULL");
   2284         return RIL_ERRNO_INVALID_RESPONSE;
   2285     }
   2286 
   2287     if (responselen % sizeof(RIL_Data_Call_Response_v6) != 0) {
   2288         RLOGE("responseDataCallListV6: invalid response length %d expected multiple of %d",
   2289                 (int)responselen, (int)sizeof(RIL_Data_Call_Response_v6));
   2290         return RIL_ERRNO_INVALID_RESPONSE;
   2291     }
   2292 
   2293     // Write version
   2294     p.writeInt32(6);
   2295 
   2296     int num = responselen / sizeof(RIL_Data_Call_Response_v6);
   2297     p.writeInt32(num);
   2298 
   2299     RIL_Data_Call_Response_v6 *p_cur = (RIL_Data_Call_Response_v6 *) response;
   2300     startResponse;
   2301     int i;
   2302     for (i = 0; i < num; i++) {
   2303         p.writeInt32((int)p_cur[i].status);
   2304         p.writeInt32(p_cur[i].suggestedRetryTime);
   2305         p.writeInt32(p_cur[i].cid);
   2306         p.writeInt32(p_cur[i].active);
   2307         writeStringToParcel(p, p_cur[i].type);
   2308         writeStringToParcel(p, p_cur[i].ifname);
   2309         writeStringToParcel(p, p_cur[i].addresses);
   2310         writeStringToParcel(p, p_cur[i].dnses);
   2311         writeStringToParcel(p, p_cur[i].gateways);
   2312         appendPrintBuf("%s[status=%d,retry=%d,cid=%d,%s,%s,%s,%s,%s,%s],", printBuf,
   2313             p_cur[i].status,
   2314             p_cur[i].suggestedRetryTime,
   2315             p_cur[i].cid,
   2316             (p_cur[i].active==0)?"down":"up",
   2317             (char*)p_cur[i].type,
   2318             (char*)p_cur[i].ifname,
   2319             (char*)p_cur[i].addresses,
   2320             (char*)p_cur[i].dnses,
   2321             (char*)p_cur[i].gateways);
   2322     }
   2323     removeLastChar;
   2324     closeResponse;
   2325 
   2326     return 0;
   2327 }
   2328 
   2329 static int responseDataCallList(Parcel &p, void *response, size_t responselen)
   2330 {
   2331     if (s_callbacks.version < 5) {
   2332         RLOGD("responseDataCallList: v4");
   2333         return responseDataCallListV4(p, response, responselen);
   2334     } else {
   2335         if (response == NULL && responselen != 0) {
   2336             RLOGE("invalid response: NULL");
   2337             return RIL_ERRNO_INVALID_RESPONSE;
   2338         }
   2339 
   2340         // Support v6 or v9 with new rils
   2341         if (responselen % sizeof(RIL_Data_Call_Response_v6) == 0) {
   2342             RLOGD("responseDataCallList: v6");
   2343             return responseDataCallListV6(p, response, responselen);
   2344         }
   2345 
   2346         if (responselen % sizeof(RIL_Data_Call_Response_v9) != 0) {
   2347             RLOGE("responseDataCallList: invalid response length %d expected multiple of %d",
   2348                     (int)responselen, (int)sizeof(RIL_Data_Call_Response_v9));
   2349             return RIL_ERRNO_INVALID_RESPONSE;
   2350         }
   2351 
   2352         // Write version
   2353         p.writeInt32(10);
   2354 
   2355         int num = responselen / sizeof(RIL_Data_Call_Response_v9);
   2356         p.writeInt32(num);
   2357 
   2358         RIL_Data_Call_Response_v9 *p_cur = (RIL_Data_Call_Response_v9 *) response;
   2359         startResponse;
   2360         int i;
   2361         for (i = 0; i < num; i++) {
   2362             p.writeInt32((int)p_cur[i].status);
   2363             p.writeInt32(p_cur[i].suggestedRetryTime);
   2364             p.writeInt32(p_cur[i].cid);
   2365             p.writeInt32(p_cur[i].active);
   2366             writeStringToParcel(p, p_cur[i].type);
   2367             writeStringToParcel(p, p_cur[i].ifname);
   2368             writeStringToParcel(p, p_cur[i].addresses);
   2369             writeStringToParcel(p, p_cur[i].dnses);
   2370             writeStringToParcel(p, p_cur[i].gateways);
   2371             writeStringToParcel(p, p_cur[i].pcscf);
   2372             appendPrintBuf("%s[status=%d,retry=%d,cid=%d,%s,%s,%s,%s,%s,%s,%s],", printBuf,
   2373                 p_cur[i].status,
   2374                 p_cur[i].suggestedRetryTime,
   2375                 p_cur[i].cid,
   2376                 (p_cur[i].active==0)?"down":"up",
   2377                 (char*)p_cur[i].type,
   2378                 (char*)p_cur[i].ifname,
   2379                 (char*)p_cur[i].addresses,
   2380                 (char*)p_cur[i].dnses,
   2381                 (char*)p_cur[i].gateways,
   2382                 (char*)p_cur[i].pcscf);
   2383         }
   2384         removeLastChar;
   2385         closeResponse;
   2386     }
   2387 
   2388     return 0;
   2389 }
   2390 
   2391 static int responseSetupDataCall(Parcel &p, void *response, size_t responselen)
   2392 {
   2393     if (s_callbacks.version < 5) {
   2394         return responseStringsWithVersion(s_callbacks.version, p, response, responselen);
   2395     } else {
   2396         return responseDataCallList(p, response, responselen);
   2397     }
   2398 }
   2399 
   2400 static int responseRaw(Parcel &p, void *response, size_t responselen) {
   2401     if (response == NULL && responselen != 0) {
   2402         RLOGE("invalid response: NULL with responselen != 0");
   2403         return RIL_ERRNO_INVALID_RESPONSE;
   2404     }
   2405 
   2406     // The java code reads -1 size as null byte array
   2407     if (response == NULL) {
   2408         p.writeInt32(-1);
   2409     } else {
   2410         p.writeInt32(responselen);
   2411         p.write(response, responselen);
   2412     }
   2413 
   2414     return 0;
   2415 }
   2416 
   2417 
   2418 static int responseSIM_IO(Parcel &p, void *response, size_t responselen) {
   2419     if (response == NULL) {
   2420         RLOGE("invalid response: NULL");
   2421         return RIL_ERRNO_INVALID_RESPONSE;
   2422     }
   2423 
   2424     if (responselen != sizeof (RIL_SIM_IO_Response) ) {
   2425         RLOGE("invalid response length was %d expected %d",
   2426                 (int)responselen, (int)sizeof (RIL_SIM_IO_Response));
   2427         return RIL_ERRNO_INVALID_RESPONSE;
   2428     }
   2429 
   2430     RIL_SIM_IO_Response *p_cur = (RIL_SIM_IO_Response *) response;
   2431     p.writeInt32(p_cur->sw1);
   2432     p.writeInt32(p_cur->sw2);
   2433     writeStringToParcel(p, p_cur->simResponse);
   2434 
   2435     startResponse;
   2436     appendPrintBuf("%ssw1=0x%X,sw2=0x%X,%s", printBuf, p_cur->sw1, p_cur->sw2,
   2437         (char*)p_cur->simResponse);
   2438     closeResponse;
   2439 
   2440 
   2441     return 0;
   2442 }
   2443 
   2444 static int responseCallForwards(Parcel &p, void *response, size_t responselen) {
   2445     int num;
   2446 
   2447     if (response == NULL && responselen != 0) {
   2448         RLOGE("invalid response: NULL");
   2449         return RIL_ERRNO_INVALID_RESPONSE;
   2450     }
   2451 
   2452     if (responselen % sizeof(RIL_CallForwardInfo *) != 0) {
   2453         RLOGE("responseCallForwards: invalid response length %d expected multiple of %d",
   2454                 (int)responselen, (int)sizeof(RIL_CallForwardInfo *));
   2455         return RIL_ERRNO_INVALID_RESPONSE;
   2456     }
   2457 
   2458     /* number of call info's */
   2459     num = responselen / sizeof(RIL_CallForwardInfo *);
   2460     p.writeInt32(num);
   2461 
   2462     startResponse;
   2463     for (int i = 0 ; i < num ; i++) {
   2464         RIL_CallForwardInfo *p_cur = ((RIL_CallForwardInfo **) response)[i];
   2465 
   2466         p.writeInt32(p_cur->status);
   2467         p.writeInt32(p_cur->reason);
   2468         p.writeInt32(p_cur->serviceClass);
   2469         p.writeInt32(p_cur->toa);
   2470         writeStringToParcel(p, p_cur->number);
   2471         p.writeInt32(p_cur->timeSeconds);
   2472         appendPrintBuf("%s[%s,reason=%d,cls=%d,toa=%d,%s,tout=%d],", printBuf,
   2473             (p_cur->status==1)?"enable":"disable",
   2474             p_cur->reason, p_cur->serviceClass, p_cur->toa,
   2475             (char*)p_cur->number,
   2476             p_cur->timeSeconds);
   2477     }
   2478     removeLastChar;
   2479     closeResponse;
   2480 
   2481     return 0;
   2482 }
   2483 
   2484 static int responseSsn(Parcel &p, void *response, size_t responselen) {
   2485     if (response == NULL) {
   2486         RLOGE("invalid response: NULL");
   2487         return RIL_ERRNO_INVALID_RESPONSE;
   2488     }
   2489 
   2490     if (responselen != sizeof(RIL_SuppSvcNotification)) {
   2491         RLOGE("invalid response length was %d expected %d",
   2492                 (int)responselen, (int)sizeof (RIL_SuppSvcNotification));
   2493         return RIL_ERRNO_INVALID_RESPONSE;
   2494     }
   2495 
   2496     RIL_SuppSvcNotification *p_cur = (RIL_SuppSvcNotification *) response;
   2497     p.writeInt32(p_cur->notificationType);
   2498     p.writeInt32(p_cur->code);
   2499     p.writeInt32(p_cur->index);
   2500     p.writeInt32(p_cur->type);
   2501     writeStringToParcel(p, p_cur->number);
   2502 
   2503     startResponse;
   2504     appendPrintBuf("%s%s,code=%d,id=%d,type=%d,%s", printBuf,
   2505         (p_cur->notificationType==0)?"mo":"mt",
   2506          p_cur->code, p_cur->index, p_cur->type,
   2507         (char*)p_cur->number);
   2508     closeResponse;
   2509 
   2510     return 0;
   2511 }
   2512 
   2513 static int responseCellList(Parcel &p, void *response, size_t responselen) {
   2514     int num;
   2515 
   2516     if (response == NULL && responselen != 0) {
   2517         RLOGE("invalid response: NULL");
   2518         return RIL_ERRNO_INVALID_RESPONSE;
   2519     }
   2520 
   2521     if (responselen % sizeof (RIL_NeighboringCell *) != 0) {
   2522         RLOGE("responseCellList: invalid response length %d expected multiple of %d\n",
   2523             (int)responselen, (int)sizeof (RIL_NeighboringCell *));
   2524         return RIL_ERRNO_INVALID_RESPONSE;
   2525     }
   2526 
   2527     startResponse;
   2528     /* number of records */
   2529     num = responselen / sizeof(RIL_NeighboringCell *);
   2530     p.writeInt32(num);
   2531 
   2532     for (int i = 0 ; i < num ; i++) {
   2533         RIL_NeighboringCell *p_cur = ((RIL_NeighboringCell **) response)[i];
   2534 
   2535         p.writeInt32(p_cur->rssi);
   2536         writeStringToParcel (p, p_cur->cid);
   2537 
   2538         appendPrintBuf("%s[cid=%s,rssi=%d],", printBuf,
   2539             p_cur->cid, p_cur->rssi);
   2540     }
   2541     removeLastChar;
   2542     closeResponse;
   2543 
   2544     return 0;
   2545 }
   2546 
   2547 /**
   2548  * Marshall the signalInfoRecord into the parcel if it exists.
   2549  */
   2550 static void marshallSignalInfoRecord(Parcel &p,
   2551             RIL_CDMA_SignalInfoRecord &p_signalInfoRecord) {
   2552     p.writeInt32(p_signalInfoRecord.isPresent);
   2553     p.writeInt32(p_signalInfoRecord.signalType);
   2554     p.writeInt32(p_signalInfoRecord.alertPitch);
   2555     p.writeInt32(p_signalInfoRecord.signal);
   2556 }
   2557 
   2558 static int responseCdmaInformationRecords(Parcel &p,
   2559             void *response, size_t responselen) {
   2560     int num;
   2561     char* string8 = NULL;
   2562     int buffer_lenght;
   2563     RIL_CDMA_InformationRecord *infoRec;
   2564 
   2565     if (response == NULL && responselen != 0) {
   2566         RLOGE("invalid response: NULL");
   2567         return RIL_ERRNO_INVALID_RESPONSE;
   2568     }
   2569 
   2570     if (responselen != sizeof (RIL_CDMA_InformationRecords)) {
   2571         RLOGE("responseCdmaInformationRecords: invalid response length %d expected multiple of %d\n",
   2572             (int)responselen, (int)sizeof (RIL_CDMA_InformationRecords *));
   2573         return RIL_ERRNO_INVALID_RESPONSE;
   2574     }
   2575 
   2576     RIL_CDMA_InformationRecords *p_cur =
   2577                              (RIL_CDMA_InformationRecords *) response;
   2578     num = MIN(p_cur->numberOfInfoRecs, RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
   2579 
   2580     startResponse;
   2581     p.writeInt32(num);
   2582 
   2583     for (int i = 0 ; i < num ; i++) {
   2584         infoRec = &p_cur->infoRec[i];
   2585         p.writeInt32(infoRec->name);
   2586         switch (infoRec->name) {
   2587             case RIL_CDMA_DISPLAY_INFO_REC:
   2588             case RIL_CDMA_EXTENDED_DISPLAY_INFO_REC:
   2589                 if (infoRec->rec.display.alpha_len >
   2590                                          CDMA_ALPHA_INFO_BUFFER_LENGTH) {
   2591                     RLOGE("invalid display info response length %d \
   2592                           expected not more than %d\n",
   2593                          (int)infoRec->rec.display.alpha_len,
   2594                          CDMA_ALPHA_INFO_BUFFER_LENGTH);
   2595                     return RIL_ERRNO_INVALID_RESPONSE;
   2596                 }
   2597                 string8 = (char*) malloc((infoRec->rec.display.alpha_len + 1)
   2598                                                              * sizeof(char) );
   2599                 for (int i = 0 ; i < infoRec->rec.display.alpha_len ; i++) {
   2600                     string8[i] = infoRec->rec.display.alpha_buf[i];
   2601                 }
   2602                 string8[(int)infoRec->rec.display.alpha_len] = '\0';
   2603                 writeStringToParcel(p, (const char*)string8);
   2604                 free(string8);
   2605                 string8 = NULL;
   2606                 break;
   2607             case RIL_CDMA_CALLED_PARTY_NUMBER_INFO_REC:
   2608             case RIL_CDMA_CALLING_PARTY_NUMBER_INFO_REC:
   2609             case RIL_CDMA_CONNECTED_NUMBER_INFO_REC:
   2610                 if (infoRec->rec.number.len > CDMA_NUMBER_INFO_BUFFER_LENGTH) {
   2611                     RLOGE("invalid display info response length %d \
   2612                           expected not more than %d\n",
   2613                          (int)infoRec->rec.number.len,
   2614                          CDMA_NUMBER_INFO_BUFFER_LENGTH);
   2615                     return RIL_ERRNO_INVALID_RESPONSE;
   2616                 }
   2617                 string8 = (char*) malloc((infoRec->rec.number.len + 1)
   2618                                                              * sizeof(char) );
   2619                 for (int i = 0 ; i < infoRec->rec.number.len; i++) {
   2620                     string8[i] = infoRec->rec.number.buf[i];
   2621                 }
   2622                 string8[(int)infoRec->rec.number.len] = '\0';
   2623                 writeStringToParcel(p, (const char*)string8);
   2624                 free(string8);
   2625                 string8 = NULL;
   2626                 p.writeInt32(infoRec->rec.number.number_type);
   2627                 p.writeInt32(infoRec->rec.number.number_plan);
   2628                 p.writeInt32(infoRec->rec.number.pi);
   2629                 p.writeInt32(infoRec->rec.number.si);
   2630                 break;
   2631             case RIL_CDMA_SIGNAL_INFO_REC:
   2632                 p.writeInt32(infoRec->rec.signal.isPresent);
   2633                 p.writeInt32(infoRec->rec.signal.signalType);
   2634                 p.writeInt32(infoRec->rec.signal.alertPitch);
   2635                 p.writeInt32(infoRec->rec.signal.signal);
   2636 
   2637                 appendPrintBuf("%sisPresent=%X, signalType=%X, \
   2638                                 alertPitch=%X, signal=%X, ",
   2639                    printBuf, (int)infoRec->rec.signal.isPresent,
   2640                    (int)infoRec->rec.signal.signalType,
   2641                    (int)infoRec->rec.signal.alertPitch,
   2642                    (int)infoRec->rec.signal.signal);
   2643                 removeLastChar;
   2644                 break;
   2645             case RIL_CDMA_REDIRECTING_NUMBER_INFO_REC:
   2646                 if (infoRec->rec.redir.redirectingNumber.len >
   2647                                               CDMA_NUMBER_INFO_BUFFER_LENGTH) {
   2648                     RLOGE("invalid display info response length %d \
   2649                           expected not more than %d\n",
   2650                          (int)infoRec->rec.redir.redirectingNumber.len,
   2651                          CDMA_NUMBER_INFO_BUFFER_LENGTH);
   2652                     return RIL_ERRNO_INVALID_RESPONSE;
   2653                 }
   2654                 string8 = (char*) malloc((infoRec->rec.redir.redirectingNumber
   2655                                           .len + 1) * sizeof(char) );
   2656                 for (int i = 0;
   2657                          i < infoRec->rec.redir.redirectingNumber.len;
   2658                          i++) {
   2659                     string8[i] = infoRec->rec.redir.redirectingNumber.buf[i];
   2660                 }
   2661                 string8[(int)infoRec->rec.redir.redirectingNumber.len] = '\0';
   2662                 writeStringToParcel(p, (const char*)string8);
   2663                 free(string8);
   2664                 string8 = NULL;
   2665                 p.writeInt32(infoRec->rec.redir.redirectingNumber.number_type);
   2666                 p.writeInt32(infoRec->rec.redir.redirectingNumber.number_plan);
   2667                 p.writeInt32(infoRec->rec.redir.redirectingNumber.pi);
   2668                 p.writeInt32(infoRec->rec.redir.redirectingNumber.si);
   2669                 p.writeInt32(infoRec->rec.redir.redirectingReason);
   2670                 break;
   2671             case RIL_CDMA_LINE_CONTROL_INFO_REC:
   2672                 p.writeInt32(infoRec->rec.lineCtrl.lineCtrlPolarityIncluded);
   2673                 p.writeInt32(infoRec->rec.lineCtrl.lineCtrlToggle);
   2674                 p.writeInt32(infoRec->rec.lineCtrl.lineCtrlReverse);
   2675                 p.writeInt32(infoRec->rec.lineCtrl.lineCtrlPowerDenial);
   2676 
   2677                 appendPrintBuf("%slineCtrlPolarityIncluded=%d, \
   2678                                 lineCtrlToggle=%d, lineCtrlReverse=%d, \
   2679                                 lineCtrlPowerDenial=%d, ", printBuf,
   2680                        (int)infoRec->rec.lineCtrl.lineCtrlPolarityIncluded,
   2681                        (int)infoRec->rec.lineCtrl.lineCtrlToggle,
   2682                        (int)infoRec->rec.lineCtrl.lineCtrlReverse,
   2683                        (int)infoRec->rec.lineCtrl.lineCtrlPowerDenial);
   2684                 removeLastChar;
   2685                 break;
   2686             case RIL_CDMA_T53_CLIR_INFO_REC:
   2687                 p.writeInt32((int)(infoRec->rec.clir.cause));
   2688 
   2689                 appendPrintBuf("%scause%d", printBuf, infoRec->rec.clir.cause);
   2690                 removeLastChar;
   2691                 break;
   2692             case RIL_CDMA_T53_AUDIO_CONTROL_INFO_REC:
   2693                 p.writeInt32(infoRec->rec.audioCtrl.upLink);
   2694                 p.writeInt32(infoRec->rec.audioCtrl.downLink);
   2695 
   2696                 appendPrintBuf("%supLink=%d, downLink=%d, ", printBuf,
   2697                         infoRec->rec.audioCtrl.upLink,
   2698                         infoRec->rec.audioCtrl.downLink);
   2699                 removeLastChar;
   2700                 break;
   2701             case RIL_CDMA_T53_RELEASE_INFO_REC:
   2702                 // TODO(Moto): See David Krause, he has the answer:)
   2703                 RLOGE("RIL_CDMA_T53_RELEASE_INFO_REC: return INVALID_RESPONSE");
   2704                 return RIL_ERRNO_INVALID_RESPONSE;
   2705             default:
   2706                 RLOGE("Incorrect name value");
   2707                 return RIL_ERRNO_INVALID_RESPONSE;
   2708         }
   2709     }
   2710     closeResponse;
   2711 
   2712     return 0;
   2713 }
   2714 
   2715 static int responseRilSignalStrength(Parcel &p,
   2716                     void *response, size_t responselen) {
   2717     if (response == NULL && responselen != 0) {
   2718         RLOGE("invalid response: NULL");
   2719         return RIL_ERRNO_INVALID_RESPONSE;
   2720     }
   2721 
   2722     if (responselen >= sizeof (RIL_SignalStrength_v5)) {
   2723         RIL_SignalStrength_v10 *p_cur = ((RIL_SignalStrength_v10 *) response);
   2724 
   2725         p.writeInt32(p_cur->GW_SignalStrength.signalStrength);
   2726         p.writeInt32(p_cur->GW_SignalStrength.bitErrorRate);
   2727         p.writeInt32(p_cur->CDMA_SignalStrength.dbm);
   2728         p.writeInt32(p_cur->CDMA_SignalStrength.ecio);
   2729         p.writeInt32(p_cur->EVDO_SignalStrength.dbm);
   2730         p.writeInt32(p_cur->EVDO_SignalStrength.ecio);
   2731         p.writeInt32(p_cur->EVDO_SignalStrength.signalNoiseRatio);
   2732         if (responselen >= sizeof (RIL_SignalStrength_v6)) {
   2733             /*
   2734              * Fixup LTE for backwards compatibility
   2735              */
   2736             if (s_callbacks.version <= 6) {
   2737                 // signalStrength: -1 -> 99
   2738                 if (p_cur->LTE_SignalStrength.signalStrength == -1) {
   2739                     p_cur->LTE_SignalStrength.signalStrength = 99;
   2740                 }
   2741                 // rsrp: -1 -> INT_MAX all other negative value to positive.
   2742                 // So remap here
   2743                 if (p_cur->LTE_SignalStrength.rsrp == -1) {
   2744                     p_cur->LTE_SignalStrength.rsrp = INT_MAX;
   2745                 } else if (p_cur->LTE_SignalStrength.rsrp < -1) {
   2746                     p_cur->LTE_SignalStrength.rsrp = -p_cur->LTE_SignalStrength.rsrp;
   2747                 }
   2748                 // rsrq: -1 -> INT_MAX
   2749                 if (p_cur->LTE_SignalStrength.rsrq == -1) {
   2750                     p_cur->LTE_SignalStrength.rsrq = INT_MAX;
   2751                 }
   2752                 // Not remapping rssnr is already using INT_MAX
   2753 
   2754                 // cqi: -1 -> INT_MAX
   2755                 if (p_cur->LTE_SignalStrength.cqi == -1) {
   2756                     p_cur->LTE_SignalStrength.cqi = INT_MAX;
   2757                 }
   2758             }
   2759             p.writeInt32(p_cur->LTE_SignalStrength.signalStrength);
   2760             p.writeInt32(p_cur->LTE_SignalStrength.rsrp);
   2761             p.writeInt32(p_cur->LTE_SignalStrength.rsrq);
   2762             p.writeInt32(p_cur->LTE_SignalStrength.rssnr);
   2763             p.writeInt32(p_cur->LTE_SignalStrength.cqi);
   2764             if (responselen >= sizeof (RIL_SignalStrength_v10)) {
   2765                 p.writeInt32(p_cur->TD_SCDMA_SignalStrength.rscp);
   2766             } else {
   2767                 p.writeInt32(INT_MAX);
   2768             }
   2769         } else {
   2770             p.writeInt32(99);
   2771             p.writeInt32(INT_MAX);
   2772             p.writeInt32(INT_MAX);
   2773             p.writeInt32(INT_MAX);
   2774             p.writeInt32(INT_MAX);
   2775             p.writeInt32(INT_MAX);
   2776         }
   2777 
   2778         startResponse;
   2779         appendPrintBuf("%s[signalStrength=%d,bitErrorRate=%d,\
   2780                 CDMA_SS.dbm=%d,CDMA_SSecio=%d,\
   2781                 EVDO_SS.dbm=%d,EVDO_SS.ecio=%d,\
   2782                 EVDO_SS.signalNoiseRatio=%d,\
   2783                 LTE_SS.signalStrength=%d,LTE_SS.rsrp=%d,LTE_SS.rsrq=%d,\
   2784                 LTE_SS.rssnr=%d,LTE_SS.cqi=%d,TDSCDMA_SS.rscp=%d]",
   2785                 printBuf,
   2786                 p_cur->GW_SignalStrength.signalStrength,
   2787                 p_cur->GW_SignalStrength.bitErrorRate,
   2788                 p_cur->CDMA_SignalStrength.dbm,
   2789                 p_cur->CDMA_SignalStrength.ecio,
   2790                 p_cur->EVDO_SignalStrength.dbm,
   2791                 p_cur->EVDO_SignalStrength.ecio,
   2792                 p_cur->EVDO_SignalStrength.signalNoiseRatio,
   2793                 p_cur->LTE_SignalStrength.signalStrength,
   2794                 p_cur->LTE_SignalStrength.rsrp,
   2795                 p_cur->LTE_SignalStrength.rsrq,
   2796                 p_cur->LTE_SignalStrength.rssnr,
   2797                 p_cur->LTE_SignalStrength.cqi,
   2798                 p_cur->TD_SCDMA_SignalStrength.rscp);
   2799         closeResponse;
   2800 
   2801     } else {
   2802         RLOGE("invalid response length");
   2803         return RIL_ERRNO_INVALID_RESPONSE;
   2804     }
   2805 
   2806     return 0;
   2807 }
   2808 
   2809 static int responseCallRing(Parcel &p, void *response, size_t responselen) {
   2810     if ((response == NULL) || (responselen == 0)) {
   2811         return responseVoid(p, response, responselen);
   2812     } else {
   2813         return responseCdmaSignalInfoRecord(p, response, responselen);
   2814     }
   2815 }
   2816 
   2817 static int responseCdmaSignalInfoRecord(Parcel &p, void *response, size_t responselen) {
   2818     if (response == NULL || responselen == 0) {
   2819         RLOGE("invalid response: NULL");
   2820         return RIL_ERRNO_INVALID_RESPONSE;
   2821     }
   2822 
   2823     if (responselen != sizeof (RIL_CDMA_SignalInfoRecord)) {
   2824         RLOGE("invalid response length %d expected sizeof (RIL_CDMA_SignalInfoRecord) of %d\n",
   2825             (int)responselen, (int)sizeof (RIL_CDMA_SignalInfoRecord));
   2826         return RIL_ERRNO_INVALID_RESPONSE;
   2827     }
   2828 
   2829     startResponse;
   2830 
   2831     RIL_CDMA_SignalInfoRecord *p_cur = ((RIL_CDMA_SignalInfoRecord *) response);
   2832     marshallSignalInfoRecord(p, *p_cur);
   2833 
   2834     appendPrintBuf("%s[isPresent=%d,signalType=%d,alertPitch=%d\
   2835               signal=%d]",
   2836               printBuf,
   2837               p_cur->isPresent,
   2838               p_cur->signalType,
   2839               p_cur->alertPitch,
   2840               p_cur->signal);
   2841 
   2842     closeResponse;
   2843     return 0;
   2844 }
   2845 
   2846 static int responseCdmaCallWaiting(Parcel &p, void *response,
   2847             size_t responselen) {
   2848     if (response == NULL && responselen != 0) {
   2849         RLOGE("invalid response: NULL");
   2850         return RIL_ERRNO_INVALID_RESPONSE;
   2851     }
   2852 
   2853     if (responselen < sizeof(RIL_CDMA_CallWaiting_v6)) {
   2854         RLOGW("Upgrade to ril version %d\n", RIL_VERSION);
   2855     }
   2856 
   2857     RIL_CDMA_CallWaiting_v6 *p_cur = ((RIL_CDMA_CallWaiting_v6 *) response);
   2858 
   2859     writeStringToParcel(p, p_cur->number);
   2860     p.writeInt32(p_cur->numberPresentation);
   2861     writeStringToParcel(p, p_cur->name);
   2862     marshallSignalInfoRecord(p, p_cur->signalInfoRecord);
   2863 
   2864     if (responselen >= sizeof(RIL_CDMA_CallWaiting_v6)) {
   2865         p.writeInt32(p_cur->number_type);
   2866         p.writeInt32(p_cur->number_plan);
   2867     } else {
   2868         p.writeInt32(0);
   2869         p.writeInt32(0);
   2870     }
   2871 
   2872     startResponse;
   2873     appendPrintBuf("%snumber=%s,numberPresentation=%d, name=%s,\
   2874             signalInfoRecord[isPresent=%d,signalType=%d,alertPitch=%d\
   2875             signal=%d,number_type=%d,number_plan=%d]",
   2876             printBuf,
   2877             p_cur->number,
   2878             p_cur->numberPresentation,
   2879             p_cur->name,
   2880             p_cur->signalInfoRecord.isPresent,
   2881             p_cur->signalInfoRecord.signalType,
   2882             p_cur->signalInfoRecord.alertPitch,
   2883             p_cur->signalInfoRecord.signal,
   2884             p_cur->number_type,
   2885             p_cur->number_plan);
   2886     closeResponse;
   2887 
   2888     return 0;
   2889 }
   2890 
   2891 static int responseSimRefresh(Parcel &p, void *response, size_t responselen) {
   2892     if (response == NULL && responselen != 0) {
   2893         RLOGE("responseSimRefresh: invalid response: NULL");
   2894         return RIL_ERRNO_INVALID_RESPONSE;
   2895     }
   2896 
   2897     startResponse;
   2898     if (s_callbacks.version == 7) {
   2899         RIL_SimRefreshResponse_v7 *p_cur = ((RIL_SimRefreshResponse_v7 *) response);
   2900         p.writeInt32(p_cur->result);
   2901         p.writeInt32(p_cur->ef_id);
   2902         writeStringToParcel(p, p_cur->aid);
   2903 
   2904         appendPrintBuf("%sresult=%d, ef_id=%d, aid=%s",
   2905                 printBuf,
   2906                 p_cur->result,
   2907                 p_cur->ef_id,
   2908                 p_cur->aid);
   2909     } else {
   2910         int *p_cur = ((int *) response);
   2911         p.writeInt32(p_cur[0]);
   2912         p.writeInt32(p_cur[1]);
   2913         writeStringToParcel(p, NULL);
   2914 
   2915         appendPrintBuf("%sresult=%d, ef_id=%d",
   2916                 printBuf,
   2917                 p_cur[0],
   2918                 p_cur[1]);
   2919     }
   2920     closeResponse;
   2921 
   2922     return 0;
   2923 }
   2924 
   2925 static int responseCellInfoList(Parcel &p, void *response, size_t responselen)
   2926 {
   2927     if (response == NULL && responselen != 0) {
   2928         RLOGE("invalid response: NULL");
   2929         return RIL_ERRNO_INVALID_RESPONSE;
   2930     }
   2931 
   2932     if (responselen % sizeof(RIL_CellInfo) != 0) {
   2933         RLOGE("responseCellInfoList: invalid response length %d expected multiple of %d",
   2934                 (int)responselen, (int)sizeof(RIL_CellInfo));
   2935         return RIL_ERRNO_INVALID_RESPONSE;
   2936     }
   2937 
   2938     int num = responselen / sizeof(RIL_CellInfo);
   2939     p.writeInt32(num);
   2940 
   2941     RIL_CellInfo *p_cur = (RIL_CellInfo *) response;
   2942     startResponse;
   2943     int i;
   2944     for (i = 0; i < num; i++) {
   2945         appendPrintBuf("%s[%d: type=%d,registered=%d,timeStampType=%d,timeStamp=%lld", printBuf, i,
   2946             p_cur->cellInfoType, p_cur->registered, p_cur->timeStampType, p_cur->timeStamp);
   2947         p.writeInt32((int)p_cur->cellInfoType);
   2948         p.writeInt32(p_cur->registered);
   2949         p.writeInt32(p_cur->timeStampType);
   2950         p.writeInt64(p_cur->timeStamp);
   2951         switch(p_cur->cellInfoType) {
   2952             case RIL_CELL_INFO_TYPE_GSM: {
   2953                 appendPrintBuf("%s GSM id: mcc=%d,mnc=%d,lac=%d,cid=%d,", printBuf,
   2954                     p_cur->CellInfo.gsm.cellIdentityGsm.mcc,
   2955                     p_cur->CellInfo.gsm.cellIdentityGsm.mnc,
   2956                     p_cur->CellInfo.gsm.cellIdentityGsm.lac,
   2957                     p_cur->CellInfo.gsm.cellIdentityGsm.cid);
   2958                 appendPrintBuf("%s gsmSS: ss=%d,ber=%d],", printBuf,
   2959                     p_cur->CellInfo.gsm.signalStrengthGsm.signalStrength,
   2960                     p_cur->CellInfo.gsm.signalStrengthGsm.bitErrorRate);
   2961 
   2962                 p.writeInt32(p_cur->CellInfo.gsm.cellIdentityGsm.mcc);
   2963                 p.writeInt32(p_cur->CellInfo.gsm.cellIdentityGsm.mnc);
   2964                 p.writeInt32(p_cur->CellInfo.gsm.cellIdentityGsm.lac);
   2965                 p.writeInt32(p_cur->CellInfo.gsm.cellIdentityGsm.cid);
   2966                 p.writeInt32(p_cur->CellInfo.gsm.signalStrengthGsm.signalStrength);
   2967                 p.writeInt32(p_cur->CellInfo.gsm.signalStrengthGsm.bitErrorRate);
   2968                 break;
   2969             }
   2970             case RIL_CELL_INFO_TYPE_WCDMA: {
   2971                 appendPrintBuf("%s WCDMA id: mcc=%d,mnc=%d,lac=%d,cid=%d,psc=%d,", printBuf,
   2972                     p_cur->CellInfo.wcdma.cellIdentityWcdma.mcc,
   2973                     p_cur->CellInfo.wcdma.cellIdentityWcdma.mnc,
   2974                     p_cur->CellInfo.wcdma.cellIdentityWcdma.lac,
   2975                     p_cur->CellInfo.wcdma.cellIdentityWcdma.cid,
   2976                     p_cur->CellInfo.wcdma.cellIdentityWcdma.psc);
   2977                 appendPrintBuf("%s wcdmaSS: ss=%d,ber=%d],", printBuf,
   2978                     p_cur->CellInfo.wcdma.signalStrengthWcdma.signalStrength,
   2979                     p_cur->CellInfo.wcdma.signalStrengthWcdma.bitErrorRate);
   2980 
   2981                 p.writeInt32(p_cur->CellInfo.wcdma.cellIdentityWcdma.mcc);
   2982                 p.writeInt32(p_cur->CellInfo.wcdma.cellIdentityWcdma.mnc);
   2983                 p.writeInt32(p_cur->CellInfo.wcdma.cellIdentityWcdma.lac);
   2984                 p.writeInt32(p_cur->CellInfo.wcdma.cellIdentityWcdma.cid);
   2985                 p.writeInt32(p_cur->CellInfo.wcdma.cellIdentityWcdma.psc);
   2986                 p.writeInt32(p_cur->CellInfo.wcdma.signalStrengthWcdma.signalStrength);
   2987                 p.writeInt32(p_cur->CellInfo.wcdma.signalStrengthWcdma.bitErrorRate);
   2988                 break;
   2989             }
   2990             case RIL_CELL_INFO_TYPE_CDMA: {
   2991                 appendPrintBuf("%s CDMA id: nId=%d,sId=%d,bsId=%d,long=%d,lat=%d", printBuf,
   2992                     p_cur->CellInfo.cdma.cellIdentityCdma.networkId,
   2993                     p_cur->CellInfo.cdma.cellIdentityCdma.systemId,
   2994                     p_cur->CellInfo.cdma.cellIdentityCdma.basestationId,
   2995                     p_cur->CellInfo.cdma.cellIdentityCdma.longitude,
   2996                     p_cur->CellInfo.cdma.cellIdentityCdma.latitude);
   2997 
   2998                 p.writeInt32(p_cur->CellInfo.cdma.cellIdentityCdma.networkId);
   2999                 p.writeInt32(p_cur->CellInfo.cdma.cellIdentityCdma.systemId);
   3000                 p.writeInt32(p_cur->CellInfo.cdma.cellIdentityCdma.basestationId);
   3001                 p.writeInt32(p_cur->CellInfo.cdma.cellIdentityCdma.longitude);
   3002                 p.writeInt32(p_cur->CellInfo.cdma.cellIdentityCdma.latitude);
   3003 
   3004                 appendPrintBuf("%s cdmaSS: dbm=%d ecio=%d evdoSS: dbm=%d,ecio=%d,snr=%d", printBuf,
   3005                     p_cur->CellInfo.cdma.signalStrengthCdma.dbm,
   3006                     p_cur->CellInfo.cdma.signalStrengthCdma.ecio,
   3007                     p_cur->CellInfo.cdma.signalStrengthEvdo.dbm,
   3008                     p_cur->CellInfo.cdma.signalStrengthEvdo.ecio,
   3009                     p_cur->CellInfo.cdma.signalStrengthEvdo.signalNoiseRatio);
   3010 
   3011                 p.writeInt32(p_cur->CellInfo.cdma.signalStrengthCdma.dbm);
   3012                 p.writeInt32(p_cur->CellInfo.cdma.signalStrengthCdma.ecio);
   3013                 p.writeInt32(p_cur->CellInfo.cdma.signalStrengthEvdo.dbm);
   3014                 p.writeInt32(p_cur->CellInfo.cdma.signalStrengthEvdo.ecio);
   3015                 p.writeInt32(p_cur->CellInfo.cdma.signalStrengthEvdo.signalNoiseRatio);
   3016                 break;
   3017             }
   3018             case RIL_CELL_INFO_TYPE_LTE: {
   3019                 appendPrintBuf("%s LTE id: mcc=%d,mnc=%d,ci=%d,pci=%d,tac=%d", printBuf,
   3020                     p_cur->CellInfo.lte.cellIdentityLte.mcc,
   3021                     p_cur->CellInfo.lte.cellIdentityLte.mnc,
   3022                     p_cur->CellInfo.lte.cellIdentityLte.ci,
   3023                     p_cur->CellInfo.lte.cellIdentityLte.pci,
   3024                     p_cur->CellInfo.lte.cellIdentityLte.tac);
   3025 
   3026                 p.writeInt32(p_cur->CellInfo.lte.cellIdentityLte.mcc);
   3027                 p.writeInt32(p_cur->CellInfo.lte.cellIdentityLte.mnc);
   3028                 p.writeInt32(p_cur->CellInfo.lte.cellIdentityLte.ci);
   3029                 p.writeInt32(p_cur->CellInfo.lte.cellIdentityLte.pci);
   3030                 p.writeInt32(p_cur->CellInfo.lte.cellIdentityLte.tac);
   3031 
   3032                 appendPrintBuf("%s lteSS: ss=%d,rsrp=%d,rsrq=%d,rssnr=%d,cqi=%d,ta=%d", printBuf,
   3033                     p_cur->CellInfo.lte.signalStrengthLte.signalStrength,
   3034                     p_cur->CellInfo.lte.signalStrengthLte.rsrp,
   3035                     p_cur->CellInfo.lte.signalStrengthLte.rsrq,
   3036                     p_cur->CellInfo.lte.signalStrengthLte.rssnr,
   3037                     p_cur->CellInfo.lte.signalStrengthLte.cqi,
   3038                     p_cur->CellInfo.lte.signalStrengthLte.timingAdvance);
   3039                 p.writeInt32(p_cur->CellInfo.lte.signalStrengthLte.signalStrength);
   3040                 p.writeInt32(p_cur->CellInfo.lte.signalStrengthLte.rsrp);
   3041                 p.writeInt32(p_cur->CellInfo.lte.signalStrengthLte.rsrq);
   3042                 p.writeInt32(p_cur->CellInfo.lte.signalStrengthLte.rssnr);
   3043                 p.writeInt32(p_cur->CellInfo.lte.signalStrengthLte.cqi);
   3044                 p.writeInt32(p_cur->CellInfo.lte.signalStrengthLte.timingAdvance);
   3045                 break;
   3046             }
   3047             case RIL_CELL_INFO_TYPE_TD_SCDMA: {
   3048                 appendPrintBuf("%s TDSCDMA id: mcc=%d,mnc=%d,lac=%d,cid=%d,cpid=%d,", printBuf,
   3049                     p_cur->CellInfo.tdscdma.cellIdentityTdscdma.mcc,
   3050                     p_cur->CellInfo.tdscdma.cellIdentityTdscdma.mnc,
   3051                     p_cur->CellInfo.tdscdma.cellIdentityTdscdma.lac,
   3052                     p_cur->CellInfo.tdscdma.cellIdentityTdscdma.cid,
   3053                     p_cur->CellInfo.tdscdma.cellIdentityTdscdma.cpid);
   3054                 appendPrintBuf("%s tdscdmaSS: rscp=%d],", printBuf,
   3055                     p_cur->CellInfo.tdscdma.signalStrengthTdscdma.rscp);
   3056 
   3057                 p.writeInt32(p_cur->CellInfo.tdscdma.cellIdentityTdscdma.mcc);
   3058                 p.writeInt32(p_cur->CellInfo.tdscdma.cellIdentityTdscdma.mnc);
   3059                 p.writeInt32(p_cur->CellInfo.tdscdma.cellIdentityTdscdma.lac);
   3060                 p.writeInt32(p_cur->CellInfo.tdscdma.cellIdentityTdscdma.cid);
   3061                 p.writeInt32(p_cur->CellInfo.tdscdma.cellIdentityTdscdma.cpid);
   3062                 p.writeInt32(p_cur->CellInfo.tdscdma.signalStrengthTdscdma.rscp);
   3063                 break;
   3064             }
   3065         }
   3066         p_cur += 1;
   3067     }
   3068     removeLastChar;
   3069     closeResponse;
   3070 
   3071     return 0;
   3072 }
   3073 
   3074 static int responseHardwareConfig(Parcel &p, void *response, size_t responselen)
   3075 {
   3076    if (response == NULL && responselen != 0) {
   3077        RLOGE("invalid response: NULL");
   3078        return RIL_ERRNO_INVALID_RESPONSE;
   3079    }
   3080 
   3081    if (responselen % sizeof(RIL_HardwareConfig) != 0) {
   3082        RLOGE("responseHardwareConfig: invalid response length %d expected multiple of %d",
   3083           (int)responselen, (int)sizeof(RIL_HardwareConfig));
   3084        return RIL_ERRNO_INVALID_RESPONSE;
   3085    }
   3086 
   3087    int num = responselen / sizeof(RIL_HardwareConfig);
   3088    int i;
   3089    RIL_HardwareConfig *p_cur = (RIL_HardwareConfig *) response;
   3090 
   3091    p.writeInt32(num);
   3092 
   3093    startResponse;
   3094    for (i = 0; i < num; i++) {
   3095       switch (p_cur[i].type) {
   3096          case RIL_HARDWARE_CONFIG_MODEM: {
   3097             writeStringToParcel(p, p_cur[i].uuid);
   3098             p.writeInt32((int)p_cur[i].state);
   3099             p.writeInt32(p_cur[i].cfg.modem.rat);
   3100             p.writeInt32(p_cur[i].cfg.modem.maxVoice);
   3101             p.writeInt32(p_cur[i].cfg.modem.maxData);
   3102             p.writeInt32(p_cur[i].cfg.modem.maxStandby);
   3103 
   3104             appendPrintBuf("%s modem: uuid=%s,state=%d,rat=%08x,maxV=%d,maxD=%d,maxS=%d", printBuf,
   3105                p_cur[i].uuid, (int)p_cur[i].state, p_cur[i].cfg.modem.rat,
   3106                p_cur[i].cfg.modem.maxVoice, p_cur[i].cfg.modem.maxData, p_cur[i].cfg.modem.maxStandby);
   3107             break;
   3108          }
   3109          case RIL_HARDWARE_CONFIG_SIM: {
   3110             writeStringToParcel(p, p_cur[i].uuid);
   3111             p.writeInt32((int)p_cur[i].state);
   3112             writeStringToParcel(p, p_cur[i].cfg.sim.modemUuid);
   3113 
   3114             appendPrintBuf("%s sim: uuid=%s,state=%d,modem-uuid=%s", printBuf,
   3115                p_cur[i].uuid, (int)p_cur[i].state, p_cur[i].cfg.sim.modemUuid);
   3116             break;
   3117          }
   3118       }
   3119    }
   3120    removeLastChar;
   3121    closeResponse;
   3122    return 0;
   3123 }
   3124 
   3125 static void triggerEvLoop() {
   3126     int ret;
   3127     if (!pthread_equal(pthread_self(), s_tid_dispatch)) {
   3128         /* trigger event loop to wakeup. No reason to do this,
   3129          * if we're in the event loop thread */
   3130          do {
   3131             ret = write (s_fdWakeupWrite, " ", 1);
   3132          } while (ret < 0 && errno == EINTR);
   3133     }
   3134 }
   3135 
   3136 static void rilEventAddWakeup(struct ril_event *ev) {
   3137     ril_event_add(ev);
   3138     triggerEvLoop();
   3139 }
   3140 
   3141 static void sendSimStatusAppInfo(Parcel &p, int num_apps, RIL_AppStatus appStatus[]) {
   3142         p.writeInt32(num_apps);
   3143         startResponse;
   3144         for (int i = 0; i < num_apps; i++) {
   3145             p.writeInt32(appStatus[i].app_type);
   3146             p.writeInt32(appStatus[i].app_state);
   3147             p.writeInt32(appStatus[i].perso_substate);
   3148             writeStringToParcel(p, (const char*)(appStatus[i].aid_ptr));
   3149             writeStringToParcel(p, (const char*)
   3150                                           (appStatus[i].app_label_ptr));
   3151             p.writeInt32(appStatus[i].pin1_replaced);
   3152             p.writeInt32(appStatus[i].pin1);
   3153             p.writeInt32(appStatus[i].pin2);
   3154             appendPrintBuf("%s[app_type=%d,app_state=%d,perso_substate=%d,\
   3155                     aid_ptr=%s,app_label_ptr=%s,pin1_replaced=%d,pin1=%d,pin2=%d],",
   3156                     printBuf,
   3157                     appStatus[i].app_type,
   3158                     appStatus[i].app_state,
   3159                     appStatus[i].perso_substate,
   3160                     appStatus[i].aid_ptr,
   3161                     appStatus[i].app_label_ptr,
   3162                     appStatus[i].pin1_replaced,
   3163                     appStatus[i].pin1,
   3164                     appStatus[i].pin2);
   3165         }
   3166         closeResponse;
   3167 }
   3168 
   3169 static int responseSimStatus(Parcel &p, void *response, size_t responselen) {
   3170     int i;
   3171 
   3172     if (response == NULL && responselen != 0) {
   3173         RLOGE("invalid response: NULL");
   3174         return RIL_ERRNO_INVALID_RESPONSE;
   3175     }
   3176 
   3177     if (responselen == sizeof (RIL_CardStatus_v6)) {
   3178         RIL_CardStatus_v6 *p_cur = ((RIL_CardStatus_v6 *) response);
   3179 
   3180         p.writeInt32(p_cur->card_state);
   3181         p.writeInt32(p_cur->universal_pin_state);
   3182         p.writeInt32(p_cur->gsm_umts_subscription_app_index);
   3183         p.writeInt32(p_cur->cdma_subscription_app_index);
   3184         p.writeInt32(p_cur->ims_subscription_app_index);
   3185 
   3186         sendSimStatusAppInfo(p, p_cur->num_applications, p_cur->applications);
   3187     } else if (responselen == sizeof (RIL_CardStatus_v5)) {
   3188         RIL_CardStatus_v5 *p_cur = ((RIL_CardStatus_v5 *) response);
   3189 
   3190         p.writeInt32(p_cur->card_state);
   3191         p.writeInt32(p_cur->universal_pin_state);
   3192         p.writeInt32(p_cur->gsm_umts_subscription_app_index);
   3193         p.writeInt32(p_cur->cdma_subscription_app_index);
   3194         p.writeInt32(-1);
   3195 
   3196         sendSimStatusAppInfo(p, p_cur->num_applications, p_cur->applications);
   3197     } else {
   3198         RLOGE("responseSimStatus: A RilCardStatus_v6 or _v5 expected\n");
   3199         return RIL_ERRNO_INVALID_RESPONSE;
   3200     }
   3201 
   3202     return 0;
   3203 }
   3204 
   3205 static int responseGsmBrSmsCnf(Parcel &p, void *response, size_t responselen) {
   3206     int num = responselen / sizeof(RIL_GSM_BroadcastSmsConfigInfo *);
   3207     p.writeInt32(num);
   3208 
   3209     startResponse;
   3210     RIL_GSM_BroadcastSmsConfigInfo **p_cur =
   3211                 (RIL_GSM_BroadcastSmsConfigInfo **) response;
   3212     for (int i = 0; i < num; i++) {
   3213         p.writeInt32(p_cur[i]->fromServiceId);
   3214         p.writeInt32(p_cur[i]->toServiceId);
   3215         p.writeInt32(p_cur[i]->fromCodeScheme);
   3216         p.writeInt32(p_cur[i]->toCodeScheme);
   3217         p.writeInt32(p_cur[i]->selected);
   3218 
   3219         appendPrintBuf("%s [%d: fromServiceId=%d, toServiceId=%d, \
   3220                 fromCodeScheme=%d, toCodeScheme=%d, selected =%d]",
   3221                 printBuf, i, p_cur[i]->fromServiceId, p_cur[i]->toServiceId,
   3222                 p_cur[i]->fromCodeScheme, p_cur[i]->toCodeScheme,
   3223                 p_cur[i]->selected);
   3224     }
   3225     closeResponse;
   3226 
   3227     return 0;
   3228 }
   3229 
   3230 static int responseCdmaBrSmsCnf(Parcel &p, void *response, size_t responselen) {
   3231     RIL_CDMA_BroadcastSmsConfigInfo **p_cur =
   3232                (RIL_CDMA_BroadcastSmsConfigInfo **) response;
   3233 
   3234     int num = responselen / sizeof (RIL_CDMA_BroadcastSmsConfigInfo *);
   3235     p.writeInt32(num);
   3236 
   3237     startResponse;
   3238     for (int i = 0 ; i < num ; i++ ) {
   3239         p.writeInt32(p_cur[i]->service_category);
   3240         p.writeInt32(p_cur[i]->language);
   3241         p.writeInt32(p_cur[i]->selected);
   3242 
   3243         appendPrintBuf("%s [%d: srvice_category=%d, language =%d, \
   3244               selected =%d], ",
   3245               printBuf, i, p_cur[i]->service_category, p_cur[i]->language,
   3246               p_cur[i]->selected);
   3247     }
   3248     closeResponse;
   3249 
   3250     return 0;
   3251 }
   3252 
   3253 static int responseCdmaSms(Parcel &p, void *response, size_t responselen) {
   3254     int num;
   3255     int digitCount;
   3256     int digitLimit;
   3257     uint8_t uct;
   3258     void* dest;
   3259 
   3260     RLOGD("Inside responseCdmaSms");
   3261 
   3262     if (response == NULL && responselen != 0) {
   3263         RLOGE("invalid response: NULL");
   3264         return RIL_ERRNO_INVALID_RESPONSE;
   3265     }
   3266 
   3267     if (responselen != sizeof(RIL_CDMA_SMS_Message)) {
   3268         RLOGE("invalid response length was %d expected %d",
   3269                 (int)responselen, (int)sizeof(RIL_CDMA_SMS_Message));
   3270         return RIL_ERRNO_INVALID_RESPONSE;
   3271     }
   3272 
   3273     RIL_CDMA_SMS_Message *p_cur = (RIL_CDMA_SMS_Message *) response;
   3274     p.writeInt32(p_cur->uTeleserviceID);
   3275     p.write(&(p_cur->bIsServicePresent),sizeof(uct));
   3276     p.writeInt32(p_cur->uServicecategory);
   3277     p.writeInt32(p_cur->sAddress.digit_mode);
   3278     p.writeInt32(p_cur->sAddress.number_mode);
   3279     p.writeInt32(p_cur->sAddress.number_type);
   3280     p.writeInt32(p_cur->sAddress.number_plan);
   3281     p.write(&(p_cur->sAddress.number_of_digits), sizeof(uct));
   3282     digitLimit= MIN((p_cur->sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
   3283     for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
   3284         p.write(&(p_cur->sAddress.digits[digitCount]),sizeof(uct));
   3285     }
   3286 
   3287     p.writeInt32(p_cur->sSubAddress.subaddressType);
   3288     p.write(&(p_cur->sSubAddress.odd),sizeof(uct));
   3289     p.write(&(p_cur->sSubAddress.number_of_digits),sizeof(uct));
   3290     digitLimit= MIN((p_cur->sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
   3291     for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
   3292         p.write(&(p_cur->sSubAddress.digits[digitCount]),sizeof(uct));
   3293     }
   3294 
   3295     digitLimit= MIN((p_cur->uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
   3296     p.writeInt32(p_cur->uBearerDataLen);
   3297     for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
   3298        p.write(&(p_cur->aBearerData[digitCount]), sizeof(uct));
   3299     }
   3300 
   3301     startResponse;
   3302     appendPrintBuf("%suTeleserviceID=%d, bIsServicePresent=%d, uServicecategory=%d, \
   3303             sAddress.digit_mode=%d, sAddress.number_mode=%d, sAddress.number_type=%d, ",
   3304             printBuf, p_cur->uTeleserviceID,p_cur->bIsServicePresent,p_cur->uServicecategory,
   3305             p_cur->sAddress.digit_mode, p_cur->sAddress.number_mode,p_cur->sAddress.number_type);
   3306     closeResponse;
   3307 
   3308     return 0;
   3309 }
   3310 
   3311 static int responseDcRtInfo(Parcel &p, void *response, size_t responselen)
   3312 {
   3313     int num = responselen / sizeof(RIL_DcRtInfo);
   3314     if ((responselen % sizeof(RIL_DcRtInfo) != 0) || (num != 1)) {
   3315         RLOGE("responseDcRtInfo: invalid response length %d expected multiple of %d",
   3316                 (int)responselen, (int)sizeof(RIL_DcRtInfo));
   3317         return RIL_ERRNO_INVALID_RESPONSE;
   3318     }
   3319 
   3320     startResponse;
   3321     RIL_DcRtInfo *pDcRtInfo = (RIL_DcRtInfo *)response;
   3322     p.writeInt64(pDcRtInfo->time);
   3323     p.writeInt32(pDcRtInfo->powerState);
   3324     appendPrintBuf("%s[time=%d,powerState=%d]", printBuf,
   3325         pDcRtInfo->time,
   3326         pDcRtInfo->powerState);
   3327     closeResponse;
   3328 
   3329     return 0;
   3330 }
   3331 
   3332 /**
   3333  * A write on the wakeup fd is done just to pop us out of select()
   3334  * We empty the buffer here and then ril_event will reset the timers on the
   3335  * way back down
   3336  */
   3337 static void processWakeupCallback(int fd, short flags, void *param) {
   3338     char buff[16];
   3339     int ret;
   3340 
   3341     RLOGV("processWakeupCallback");
   3342 
   3343     /* empty our wakeup socket out */
   3344     do {
   3345         ret = read(s_fdWakeupRead, &buff, sizeof(buff));
   3346     } while (ret > 0 || (ret < 0 && errno == EINTR));
   3347 }
   3348 
   3349 static void onCommandsSocketClosed(RIL_SOCKET_ID socket_id) {
   3350     int ret;
   3351     RequestInfo *p_cur;
   3352     /* Hook for current context
   3353        pendingRequestsMutextHook refer to &s_pendingRequestsMutex */
   3354     pthread_mutex_t * pendingRequestsMutexHook = &s_pendingRequestsMutex;
   3355     /* pendingRequestsHook refer to &s_pendingRequests */
   3356     RequestInfo **    pendingRequestsHook = &s_pendingRequests;
   3357 
   3358 #if (SIM_COUNT >= 2)
   3359     if (socket_id == RIL_SOCKET_2) {
   3360         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket2;
   3361         pendingRequestsHook = &s_pendingRequests_socket2;
   3362     }
   3363 #if (SIM_COUNT >= 3)
   3364     else if (socket_id == RIL_SOCKET_3) {
   3365         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket3;
   3366         pendingRequestsHook = &s_pendingRequests_socket3;
   3367     }
   3368 #endif
   3369 #if (SIM_COUNT >= 4)
   3370     else if (socket_id == RIL_SOCKET_4) {
   3371         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket4;
   3372         pendingRequestsHook = &s_pendingRequests_socket4;
   3373     }
   3374 #endif
   3375 #endif
   3376     /* mark pending requests as "cancelled" so we dont report responses */
   3377     ret = pthread_mutex_lock(pendingRequestsMutexHook);
   3378     assert (ret == 0);
   3379 
   3380     p_cur = *pendingRequestsHook;
   3381 
   3382     for (p_cur = *pendingRequestsHook
   3383             ; p_cur != NULL
   3384             ; p_cur  = p_cur->p_next
   3385     ) {
   3386         p_cur->cancelled = 1;
   3387     }
   3388 
   3389     ret = pthread_mutex_unlock(pendingRequestsMutexHook);
   3390     assert (ret == 0);
   3391 }
   3392 
   3393 static void processCommandsCallback(int fd, short flags, void *param) {
   3394     RecordStream *p_rs;
   3395     void *p_record;
   3396     size_t recordlen;
   3397     int ret;
   3398     SocketListenParam *p_info = (SocketListenParam *)param;
   3399 
   3400     assert(fd == p_info->fdCommand);
   3401 
   3402     p_rs = p_info->p_rs;
   3403 
   3404     for (;;) {
   3405         /* loop until EAGAIN/EINTR, end of stream, or other error */
   3406         ret = record_stream_get_next(p_rs, &p_record, &recordlen);
   3407 
   3408         if (ret == 0 && p_record == NULL) {
   3409             /* end-of-stream */
   3410             break;
   3411         } else if (ret < 0) {
   3412             break;
   3413         } else if (ret == 0) { /* && p_record != NULL */
   3414             processCommandBuffer(p_record, recordlen, p_info->socket_id);
   3415         }
   3416     }
   3417 
   3418     if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) {
   3419         /* fatal error or end-of-stream */
   3420         if (ret != 0) {
   3421             RLOGE("error on reading command socket errno:%d\n", errno);
   3422         } else {
   3423             RLOGW("EOS.  Closing command socket.");
   3424         }
   3425 
   3426         close(fd);
   3427         p_info->fdCommand = -1;
   3428 
   3429         ril_event_del(p_info->commands_event);
   3430 
   3431         record_stream_free(p_rs);
   3432 
   3433         /* start listening for new connections again */
   3434         rilEventAddWakeup(&s_listen_event);
   3435 
   3436         onCommandsSocketClosed(p_info->socket_id);
   3437     }
   3438 }
   3439 
   3440 
   3441 static void onNewCommandConnect(RIL_SOCKET_ID socket_id) {
   3442     // Inform we are connected and the ril version
   3443     int rilVer = s_callbacks.version;
   3444     RIL_UNSOL_RESPONSE(RIL_UNSOL_RIL_CONNECTED,
   3445                                     &rilVer, sizeof(rilVer), socket_id);
   3446 
   3447     // implicit radio state changed
   3448     RIL_UNSOL_RESPONSE(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
   3449                                     NULL, 0, socket_id);
   3450 
   3451     // Send last NITZ time data, in case it was missed
   3452     if (s_lastNITZTimeData != NULL) {
   3453         sendResponseRaw(s_lastNITZTimeData, s_lastNITZTimeDataSize, socket_id);
   3454 
   3455         free(s_lastNITZTimeData);
   3456         s_lastNITZTimeData = NULL;
   3457     }
   3458 
   3459     // Get version string
   3460     if (s_callbacks.getVersion != NULL) {
   3461         const char *version;
   3462         version = s_callbacks.getVersion();
   3463         RLOGI("RIL Daemon version: %s\n", version);
   3464 
   3465         property_set(PROPERTY_RIL_IMPL, version);
   3466     } else {
   3467         RLOGI("RIL Daemon version: unavailable\n");
   3468         property_set(PROPERTY_RIL_IMPL, "unavailable");
   3469     }
   3470 
   3471 }
   3472 
   3473 static void listenCallback (int fd, short flags, void *param) {
   3474     int ret;
   3475     int err;
   3476     int is_phone_socket;
   3477     int fdCommand = -1;
   3478     RecordStream *p_rs;
   3479     SocketListenParam *p_info = (SocketListenParam *)param;
   3480 
   3481     struct sockaddr_un peeraddr;
   3482     socklen_t socklen = sizeof (peeraddr);
   3483 
   3484     struct ucred creds;
   3485     socklen_t szCreds = sizeof(creds);
   3486 
   3487     struct passwd *pwd = NULL;
   3488 
   3489     assert (*p_info->fdCommand < 0);
   3490     assert (fd == *p_info->fdListen);
   3491 
   3492     fdCommand = accept(fd, (sockaddr *) &peeraddr, &socklen);
   3493 
   3494     if (fdCommand < 0 ) {
   3495         RLOGE("Error on accept() errno:%d", errno);
   3496         /* start listening for new connections again */
   3497         rilEventAddWakeup(p_info->listen_event);
   3498         return;
   3499     }
   3500 
   3501     /* check the credential of the other side and only accept socket from
   3502      * phone process
   3503      */
   3504     errno = 0;
   3505     is_phone_socket = 0;
   3506 
   3507     err = getsockopt(fdCommand, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
   3508 
   3509     if (err == 0 && szCreds > 0) {
   3510         errno = 0;
   3511         pwd = getpwuid(creds.uid);
   3512         if (pwd != NULL) {
   3513             if (strcmp(pwd->pw_name, p_info->processName) == 0) {
   3514                 is_phone_socket = 1;
   3515             } else {
   3516                 RLOGE("RILD can't accept socket from process %s", pwd->pw_name);
   3517             }
   3518         } else {
   3519             RLOGE("Error on getpwuid() errno: %d", errno);
   3520         }
   3521     } else {
   3522         RLOGD("Error on getsockopt() errno: %d", errno);
   3523     }
   3524 
   3525     if (!is_phone_socket) {
   3526       RLOGE("RILD must accept socket from %s", p_info->processName);
   3527 
   3528       close(fdCommand);
   3529       fdCommand = -1;
   3530 
   3531       onCommandsSocketClosed(p_info->socket_id);
   3532 
   3533       /* start listening for new connections again */
   3534       rilEventAddWakeup(p_info->listen_event);
   3535 
   3536       return;
   3537     }
   3538 
   3539     ret = fcntl(fdCommand, F_SETFL, O_NONBLOCK);
   3540 
   3541     if (ret < 0) {
   3542         RLOGE ("Error setting O_NONBLOCK errno:%d", errno);
   3543     }
   3544 
   3545     RLOGI("libril: new connection to %s", rilSocketIdToString(p_info->socket_id));
   3546 
   3547     p_info->fdCommand = fdCommand;
   3548 
   3549     p_rs = record_stream_new(p_info->fdCommand, MAX_COMMAND_BYTES);
   3550 
   3551     p_info->p_rs = p_rs;
   3552 
   3553     ril_event_set (p_info->commands_event, p_info->fdCommand, 1,
   3554         p_info->processCommandsCallback, p_info);
   3555 
   3556     rilEventAddWakeup (p_info->commands_event);
   3557 
   3558     onNewCommandConnect(p_info->socket_id);
   3559 }
   3560 
   3561 static void freeDebugCallbackArgs(int number, char **args) {
   3562     for (int i = 0; i < number; i++) {
   3563         if (args[i] != NULL) {
   3564             free(args[i]);
   3565         }
   3566     }
   3567     free(args);
   3568 }
   3569 
   3570 static void debugCallback (int fd, short flags, void *param) {
   3571     int acceptFD, option;
   3572     struct sockaddr_un peeraddr;
   3573     socklen_t socklen = sizeof (peeraddr);
   3574     int data;
   3575     unsigned int qxdm_data[6];
   3576     const char *deactData[1] = {"1"};
   3577     char *actData[1];
   3578     RIL_Dial dialData;
   3579     int hangupData[1] = {1};
   3580     int number;
   3581     char **args;
   3582     RIL_SOCKET_ID socket_id = RIL_SOCKET_1;
   3583     int sim_id = 0;
   3584 
   3585     RLOGI("debugCallback for socket %s", rilSocketIdToString(socket_id));
   3586 
   3587     acceptFD = accept (fd,  (sockaddr *) &peeraddr, &socklen);
   3588 
   3589     if (acceptFD < 0) {
   3590         RLOGE ("error accepting on debug port: %d\n", errno);
   3591         return;
   3592     }
   3593 
   3594     if (recv(acceptFD, &number, sizeof(int), 0) != sizeof(int)) {
   3595         RLOGE ("error reading on socket: number of Args: \n");
   3596         return;
   3597     }
   3598     args = (char **) malloc(sizeof(char*) * number);
   3599 
   3600     for (int i = 0; i < number; i++) {
   3601         int len;
   3602         if (recv(acceptFD, &len, sizeof(int), 0) != sizeof(int)) {
   3603             RLOGE ("error reading on socket: Len of Args: \n");
   3604             freeDebugCallbackArgs(i, args);
   3605             return;
   3606         }
   3607         // +1 for null-term
   3608         args[i] = (char *) malloc((sizeof(char) * len) + 1);
   3609         if (recv(acceptFD, args[i], sizeof(char) * len, 0)
   3610             != (int)sizeof(char) * len) {
   3611             RLOGE ("error reading on socket: Args[%d] \n", i);
   3612             freeDebugCallbackArgs(i, args);
   3613             return;
   3614         }
   3615         char * buf = args[i];
   3616         buf[len] = 0;
   3617         if ((i+1) == number) {
   3618             /* The last argument should be sim id 0(SIM1)~3(SIM4) */
   3619             sim_id = atoi(args[i]);
   3620             switch (sim_id) {
   3621                 case 0:
   3622                     socket_id = RIL_SOCKET_1;
   3623                     break;
   3624             #if (SIM_COUNT >= 2)
   3625                 case 1:
   3626                     socket_id = RIL_SOCKET_2;
   3627                     break;
   3628             #endif
   3629             #if (SIM_COUNT >= 3)
   3630                 case 2:
   3631                     socket_id = RIL_SOCKET_3;
   3632                     break;
   3633             #endif
   3634             #if (SIM_COUNT >= 4)
   3635                 case 3:
   3636                     socket_id = RIL_SOCKET_4;
   3637                     break;
   3638             #endif
   3639                 default:
   3640                     socket_id = RIL_SOCKET_1;
   3641                     break;
   3642             }
   3643         }
   3644     }
   3645 
   3646     switch (atoi(args[0])) {
   3647         case 0:
   3648             RLOGI ("Connection on debug port: issuing reset.");
   3649             issueLocalRequest(RIL_REQUEST_RESET_RADIO, NULL, 0, socket_id);
   3650             break;
   3651         case 1:
   3652             RLOGI ("Connection on debug port: issuing radio power off.");
   3653             data = 0;
   3654             issueLocalRequest(RIL_REQUEST_RADIO_POWER, &data, sizeof(int), socket_id);
   3655             // Close the socket
   3656             if (socket_id == RIL_SOCKET_1 && s_ril_param_socket.fdCommand > 0) {
   3657                 close(s_ril_param_socket.fdCommand);
   3658                 s_ril_param_socket.fdCommand = -1;
   3659             }
   3660         #if (SIM_COUNT == 2)
   3661             else if (socket_id == RIL_SOCKET_2 && s_ril_param_socket2.fdCommand > 0) {
   3662                 close(s_ril_param_socket2.fdCommand);
   3663                 s_ril_param_socket2.fdCommand = -1;
   3664             }
   3665         #endif
   3666             break;
   3667         case 2:
   3668             RLOGI ("Debug port: issuing unsolicited voice network change.");
   3669             RIL_UNSOL_RESPONSE(RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED, NULL, 0, socket_id);
   3670             break;
   3671         case 3:
   3672             RLOGI ("Debug port: QXDM log enable.");
   3673             qxdm_data[0] = 65536;     // head.func_tag
   3674             qxdm_data[1] = 16;        // head.len
   3675             qxdm_data[2] = 1;         // mode: 1 for 'start logging'
   3676             qxdm_data[3] = 32;        // log_file_size: 32megabytes
   3677             qxdm_data[4] = 0;         // log_mask
   3678             qxdm_data[5] = 8;         // log_max_fileindex
   3679             issueLocalRequest(RIL_REQUEST_OEM_HOOK_RAW, qxdm_data,
   3680                               6 * sizeof(int), socket_id);
   3681             break;
   3682         case 4:
   3683             RLOGI ("Debug port: QXDM log disable.");
   3684             qxdm_data[0] = 65536;
   3685             qxdm_data[1] = 16;
   3686             qxdm_data[2] = 0;          // mode: 0 for 'stop logging'
   3687             qxdm_data[3] = 32;
   3688             qxdm_data[4] = 0;
   3689             qxdm_data[5] = 8;
   3690             issueLocalRequest(RIL_REQUEST_OEM_HOOK_RAW, qxdm_data,
   3691                               6 * sizeof(int), socket_id);
   3692             break;
   3693         case 5:
   3694             RLOGI("Debug port: Radio On");
   3695             data = 1;
   3696             issueLocalRequest(RIL_REQUEST_RADIO_POWER, &data, sizeof(int), socket_id);
   3697             sleep(2);
   3698             // Set network selection automatic.
   3699             issueLocalRequest(RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC, NULL, 0, socket_id);
   3700             break;
   3701         case 6:
   3702             RLOGI("Debug port: Setup Data Call, Apn :%s\n", args[1]);
   3703             actData[0] = args[1];
   3704             issueLocalRequest(RIL_REQUEST_SETUP_DATA_CALL, &actData,
   3705                               sizeof(actData), socket_id);
   3706             break;
   3707         case 7:
   3708             RLOGI("Debug port: Deactivate Data Call");
   3709             issueLocalRequest(RIL_REQUEST_DEACTIVATE_DATA_CALL, &deactData,
   3710                               sizeof(deactData), socket_id);
   3711             break;
   3712         case 8:
   3713             RLOGI("Debug port: Dial Call");
   3714             dialData.clir = 0;
   3715             dialData.address = args[1];
   3716             issueLocalRequest(RIL_REQUEST_DIAL, &dialData, sizeof(dialData), socket_id);
   3717             break;
   3718         case 9:
   3719             RLOGI("Debug port: Answer Call");
   3720             issueLocalRequest(RIL_REQUEST_ANSWER, NULL, 0, socket_id);
   3721             break;
   3722         case 10:
   3723             RLOGI("Debug port: End Call");
   3724             issueLocalRequest(RIL_REQUEST_HANGUP, &hangupData,
   3725                               sizeof(hangupData), socket_id);
   3726             break;
   3727         default:
   3728             RLOGE ("Invalid request");
   3729             break;
   3730     }
   3731     freeDebugCallbackArgs(number, args);
   3732     close(acceptFD);
   3733 }
   3734 
   3735 
   3736 static void userTimerCallback (int fd, short flags, void *param) {
   3737     UserCallbackInfo *p_info;
   3738 
   3739     p_info = (UserCallbackInfo *)param;
   3740 
   3741     p_info->p_callback(p_info->userParam);
   3742 
   3743 
   3744     // FIXME generalize this...there should be a cancel mechanism
   3745     if (s_last_wake_timeout_info != NULL && s_last_wake_timeout_info == p_info) {
   3746         s_last_wake_timeout_info = NULL;
   3747     }
   3748 
   3749     free(p_info);
   3750 }
   3751 
   3752 
   3753 static void *
   3754 eventLoop(void *param) {
   3755     int ret;
   3756     int filedes[2];
   3757 
   3758     ril_event_init();
   3759 
   3760     pthread_mutex_lock(&s_startupMutex);
   3761 
   3762     s_started = 1;
   3763     pthread_cond_broadcast(&s_startupCond);
   3764 
   3765     pthread_mutex_unlock(&s_startupMutex);
   3766 
   3767     ret = pipe(filedes);
   3768 
   3769     if (ret < 0) {
   3770         RLOGE("Error in pipe() errno:%d", errno);
   3771         return NULL;
   3772     }
   3773 
   3774     s_fdWakeupRead = filedes[0];
   3775     s_fdWakeupWrite = filedes[1];
   3776 
   3777     fcntl(s_fdWakeupRead, F_SETFL, O_NONBLOCK);
   3778 
   3779     ril_event_set (&s_wakeupfd_event, s_fdWakeupRead, true,
   3780                 processWakeupCallback, NULL);
   3781 
   3782     rilEventAddWakeup (&s_wakeupfd_event);
   3783 
   3784     // Only returns on error
   3785     ril_event_loop();
   3786     RLOGE ("error in event_loop_base errno:%d", errno);
   3787     // kill self to restart on error
   3788     kill(0, SIGKILL);
   3789 
   3790     return NULL;
   3791 }
   3792 
   3793 extern "C" void
   3794 RIL_startEventLoop(void) {
   3795     /* spin up eventLoop thread and wait for it to get started */
   3796     s_started = 0;
   3797     pthread_mutex_lock(&s_startupMutex);
   3798 
   3799     pthread_attr_t attr;
   3800     pthread_attr_init(&attr);
   3801     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
   3802 
   3803     int result = pthread_create(&s_tid_dispatch, &attr, eventLoop, NULL);
   3804     if (result != 0) {
   3805         RLOGE("Failed to create dispatch thread: %s", strerror(result));
   3806         goto done;
   3807     }
   3808 
   3809     while (s_started == 0) {
   3810         pthread_cond_wait(&s_startupCond, &s_startupMutex);
   3811     }
   3812 
   3813 done:
   3814     pthread_mutex_unlock(&s_startupMutex);
   3815 }
   3816 
   3817 // Used for testing purpose only.
   3818 extern "C" void RIL_setcallbacks (const RIL_RadioFunctions *callbacks) {
   3819     memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
   3820 }
   3821 
   3822 static void startListen(RIL_SOCKET_ID socket_id, SocketListenParam* socket_listen_p) {
   3823     int fdListen = -1;
   3824     int ret;
   3825     char socket_name[10];
   3826 
   3827     memset(socket_name, 0, sizeof(char)*10);
   3828 
   3829     switch(socket_id) {
   3830         case RIL_SOCKET_1:
   3831             strncpy(socket_name, RIL_getRilSocketName(), 9);
   3832             break;
   3833     #if (SIM_COUNT >= 2)
   3834         case RIL_SOCKET_2:
   3835             strncpy(socket_name, SOCKET2_NAME_RIL, 9);
   3836             break;
   3837     #endif
   3838     #if (SIM_COUNT >= 3)
   3839         case RIL_SOCKET_3:
   3840             strncpy(socket_name, SOCKET3_NAME_RIL, 9);
   3841             break;
   3842     #endif
   3843     #if (SIM_COUNT >= 4)
   3844         case RIL_SOCKET_4:
   3845             strncpy(socket_name, SOCKET4_NAME_RIL, 9);
   3846             break;
   3847     #endif
   3848         default:
   3849             RLOGE("Socket id is wrong!!");
   3850             return;
   3851     }
   3852 
   3853     RLOGI("Start to listen %s", rilSocketIdToString(socket_id));
   3854 
   3855     fdListen = android_get_control_socket(socket_name);
   3856     if (fdListen < 0) {
   3857         RLOGE("Failed to get socket %s", socket_name);
   3858         exit(-1);
   3859     }
   3860 
   3861     ret = listen(fdListen, 4);
   3862 
   3863     if (ret < 0) {
   3864         RLOGE("Failed to listen on control socket '%d': %s",
   3865              fdListen, strerror(errno));
   3866         exit(-1);
   3867     }
   3868     socket_listen_p->fdListen = fdListen;
   3869 
   3870     /* note: non-persistent so we can accept only one connection at a time */
   3871     ril_event_set (socket_listen_p->listen_event, fdListen, false,
   3872                 listenCallback, socket_listen_p);
   3873 
   3874     rilEventAddWakeup (socket_listen_p->listen_event);
   3875 }
   3876 
   3877 extern "C" void
   3878 RIL_register (const RIL_RadioFunctions *callbacks) {
   3879     int ret;
   3880     int flags;
   3881 
   3882     RLOGI("SIM_COUNT: %d", SIM_COUNT);
   3883 
   3884     if (callbacks == NULL) {
   3885         RLOGE("RIL_register: RIL_RadioFunctions * null");
   3886         return;
   3887     }
   3888     if (callbacks->version < RIL_VERSION_MIN) {
   3889         RLOGE("RIL_register: version %d is to old, min version is %d",
   3890              callbacks->version, RIL_VERSION_MIN);
   3891         return;
   3892     }
   3893     if (callbacks->version > RIL_VERSION) {
   3894         RLOGE("RIL_register: version %d is too new, max version is %d",
   3895              callbacks->version, RIL_VERSION);
   3896         return;
   3897     }
   3898     RLOGE("RIL_register: RIL version %d", callbacks->version);
   3899 
   3900     if (s_registerCalled > 0) {
   3901         RLOGE("RIL_register has been called more than once. "
   3902                 "Subsequent call ignored");
   3903         return;
   3904     }
   3905 
   3906     memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
   3907 
   3908     /* Initialize socket1 parameters */
   3909     s_ril_param_socket = {
   3910                         RIL_SOCKET_1,             /* socket_id */
   3911                         -1,                       /* fdListen */
   3912                         -1,                       /* fdCommand */
   3913                         PHONE_PROCESS,            /* processName */
   3914                         &s_commands_event,        /* commands_event */
   3915                         &s_listen_event,          /* listen_event */
   3916                         processCommandsCallback,  /* processCommandsCallback */
   3917                         NULL                      /* p_rs */
   3918                         };
   3919 
   3920 #if (SIM_COUNT >= 2)
   3921     s_ril_param_socket2 = {
   3922                         RIL_SOCKET_2,               /* socket_id */
   3923                         -1,                         /* fdListen */
   3924                         -1,                         /* fdCommand */
   3925                         PHONE_PROCESS,              /* processName */
   3926                         &s_commands_event_socket2,  /* commands_event */
   3927                         &s_listen_event_socket2,    /* listen_event */
   3928                         processCommandsCallback,    /* processCommandsCallback */
   3929                         NULL                        /* p_rs */
   3930                         };
   3931 #endif
   3932 
   3933 #if (SIM_COUNT >= 3)
   3934     s_ril_param_socket3 = {
   3935                         RIL_SOCKET_3,               /* socket_id */
   3936                         -1,                         /* fdListen */
   3937                         -1,                         /* fdCommand */
   3938                         PHONE_PROCESS,              /* processName */
   3939                         &s_commands_event_socket3,  /* commands_event */
   3940                         &s_listen_event_socket3,    /* listen_event */
   3941                         processCommandsCallback,    /* processCommandsCallback */
   3942                         NULL                        /* p_rs */
   3943                         };
   3944 #endif
   3945 
   3946 #if (SIM_COUNT >= 4)
   3947     s_ril_param_socket4 = {
   3948                         RIL_SOCKET_4,               /* socket_id */
   3949                         -1,                         /* fdListen */
   3950                         -1,                         /* fdCommand */
   3951                         PHONE_PROCESS,              /* processName */
   3952                         &s_commands_event_socket4,  /* commands_event */
   3953                         &s_listen_event_socket4,    /* listen_event */
   3954                         processCommandsCallback,    /* processCommandsCallback */
   3955                         NULL                        /* p_rs */
   3956                         };
   3957 #endif
   3958 
   3959 
   3960     s_registerCalled = 1;
   3961 
   3962     RLOGI("s_registerCalled flag set, %d", s_started);
   3963     // Little self-check
   3964 
   3965     for (int i = 0; i < (int)NUM_ELEMS(s_commands); i++) {
   3966         assert(i == s_commands[i].requestNumber);
   3967     }
   3968 
   3969     for (int i = 0; i < (int)NUM_ELEMS(s_unsolResponses); i++) {
   3970         assert(i + RIL_UNSOL_RESPONSE_BASE
   3971                 == s_unsolResponses[i].requestNumber);
   3972     }
   3973 
   3974     // New rild impl calls RIL_startEventLoop() first
   3975     // old standalone impl wants it here.
   3976 
   3977     if (s_started == 0) {
   3978         RIL_startEventLoop();
   3979     }
   3980 
   3981     // start listen socket1
   3982     startListen(RIL_SOCKET_1, &s_ril_param_socket);
   3983 
   3984 #if (SIM_COUNT >= 2)
   3985     // start listen socket2
   3986     startListen(RIL_SOCKET_2, &s_ril_param_socket2);
   3987 #endif /* (SIM_COUNT == 2) */
   3988 
   3989 #if (SIM_COUNT >= 3)
   3990     // start listen socket3
   3991     startListen(RIL_SOCKET_3, &s_ril_param_socket3);
   3992 #endif /* (SIM_COUNT == 3) */
   3993 
   3994 #if (SIM_COUNT >= 4)
   3995     // start listen socket4
   3996     startListen(RIL_SOCKET_4, &s_ril_param_socket4);
   3997 #endif /* (SIM_COUNT == 4) */
   3998 
   3999 
   4000 #if 1
   4001     // start debug interface socket
   4002 
   4003     char *inst = NULL;
   4004     if (strlen(RIL_getRilSocketName()) >= strlen(SOCKET_NAME_RIL)) {
   4005         inst = RIL_getRilSocketName() + strlen(SOCKET_NAME_RIL);
   4006     }
   4007 
   4008     char rildebug[MAX_DEBUG_SOCKET_NAME_LENGTH] = SOCKET_NAME_RIL_DEBUG;
   4009     if (inst != NULL) {
   4010         strncat(rildebug, inst, MAX_DEBUG_SOCKET_NAME_LENGTH);
   4011     }
   4012 
   4013     s_fdDebug = android_get_control_socket(rildebug);
   4014     if (s_fdDebug < 0) {
   4015         RLOGE("Failed to get socket : %s errno:%d", rildebug, errno);
   4016         exit(-1);
   4017     }
   4018 
   4019     ret = listen(s_fdDebug, 4);
   4020 
   4021     if (ret < 0) {
   4022         RLOGE("Failed to listen on ril debug socket '%d': %s",
   4023              s_fdDebug, strerror(errno));
   4024         exit(-1);
   4025     }
   4026 
   4027     ril_event_set (&s_debug_event, s_fdDebug, true,
   4028                 debugCallback, NULL);
   4029 
   4030     rilEventAddWakeup (&s_debug_event);
   4031 #endif
   4032 
   4033 }
   4034 
   4035 static int
   4036 checkAndDequeueRequestInfo(struct RequestInfo *pRI) {
   4037     int ret = 0;
   4038     /* Hook for current context
   4039        pendingRequestsMutextHook refer to &s_pendingRequestsMutex */
   4040     pthread_mutex_t* pendingRequestsMutexHook = &s_pendingRequestsMutex;
   4041     /* pendingRequestsHook refer to &s_pendingRequests */
   4042     RequestInfo ** pendingRequestsHook = &s_pendingRequests;
   4043 
   4044     if (pRI == NULL) {
   4045         return 0;
   4046     }
   4047 
   4048 #if (SIM_COUNT >= 2)
   4049     if (pRI->socket_id == RIL_SOCKET_2) {
   4050         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket2;
   4051         pendingRequestsHook = &s_pendingRequests_socket2;
   4052     }
   4053 #if (SIM_COUNT >= 3)
   4054         if (pRI->socket_id == RIL_SOCKET_3) {
   4055             pendingRequestsMutexHook = &s_pendingRequestsMutex_socket3;
   4056             pendingRequestsHook = &s_pendingRequests_socket3;
   4057         }
   4058 #endif
   4059 #if (SIM_COUNT >= 4)
   4060     if (pRI->socket_id == RIL_SOCKET_4) {
   4061         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket4;
   4062         pendingRequestsHook = &s_pendingRequests_socket4;
   4063     }
   4064 #endif
   4065 #endif
   4066     pthread_mutex_lock(pendingRequestsMutexHook);
   4067 
   4068     for(RequestInfo **ppCur = pendingRequestsHook
   4069         ; *ppCur != NULL
   4070         ; ppCur = &((*ppCur)->p_next)
   4071     ) {
   4072         if (pRI == *ppCur) {
   4073             ret = 1;
   4074 
   4075             *ppCur = (*ppCur)->p_next;
   4076             break;
   4077         }
   4078     }
   4079 
   4080     pthread_mutex_unlock(pendingRequestsMutexHook);
   4081 
   4082     return ret;
   4083 }
   4084 
   4085 
   4086 extern "C" void
   4087 RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen) {
   4088     RequestInfo *pRI;
   4089     int ret;
   4090     int fd = s_ril_param_socket.fdCommand;
   4091     size_t errorOffset;
   4092     RIL_SOCKET_ID socket_id = RIL_SOCKET_1;
   4093 
   4094     pRI = (RequestInfo *)t;
   4095 
   4096     if (!checkAndDequeueRequestInfo(pRI)) {
   4097         RLOGE ("RIL_onRequestComplete: invalid RIL_Token");
   4098         return;
   4099     }
   4100 
   4101     socket_id = pRI->socket_id;
   4102 #if (SIM_COUNT >= 2)
   4103     if (socket_id == RIL_SOCKET_2) {
   4104         fd = s_ril_param_socket2.fdCommand;
   4105     }
   4106 #if (SIM_COUNT >= 3)
   4107         if (socket_id == RIL_SOCKET_3) {
   4108             fd = s_ril_param_socket3.fdCommand;
   4109         }
   4110 #endif
   4111 #if (SIM_COUNT >= 4)
   4112     if (socket_id == RIL_SOCKET_4) {
   4113         fd = s_ril_param_socket4.fdCommand;
   4114     }
   4115 #endif
   4116 #endif
   4117     RLOGD("RequestComplete, %s", rilSocketIdToString(socket_id));
   4118 
   4119     if (pRI->local > 0) {
   4120         // Locally issued command...void only!
   4121         // response does not go back up the command socket
   4122         RLOGD("C[locl]< %s", requestToString(pRI->pCI->requestNumber));
   4123 
   4124         goto done;
   4125     }
   4126 
   4127     appendPrintBuf("[%04d]< %s",
   4128         pRI->token, requestToString(pRI->pCI->requestNumber));
   4129 
   4130     if (pRI->cancelled == 0) {
   4131         Parcel p;
   4132 
   4133         p.writeInt32 (RESPONSE_SOLICITED);
   4134         p.writeInt32 (pRI->token);
   4135         errorOffset = p.dataPosition();
   4136 
   4137         p.writeInt32 (e);
   4138 
   4139         if (response != NULL) {
   4140             // there is a response payload, no matter success or not.
   4141             ret = pRI->pCI->responseFunction(p, response, responselen);
   4142 
   4143             /* if an error occurred, rewind and mark it */
   4144             if (ret != 0) {
   4145                 RLOGE ("responseFunction error, ret %d", ret);
   4146                 p.setDataPosition(errorOffset);
   4147                 p.writeInt32 (ret);
   4148             }
   4149         }
   4150 
   4151         if (e != RIL_E_SUCCESS) {
   4152             appendPrintBuf("%s fails by %s", printBuf, failCauseToString(e));
   4153         }
   4154 
   4155         if (fd < 0) {
   4156             RLOGD ("RIL onRequestComplete: Command channel closed");
   4157         }
   4158         sendResponse(p, socket_id);
   4159     }
   4160 
   4161 done:
   4162     free(pRI);
   4163 }
   4164 
   4165 
   4166 static void
   4167 grabPartialWakeLock() {
   4168     acquire_wake_lock(PARTIAL_WAKE_LOCK, ANDROID_WAKE_LOCK_NAME);
   4169 }
   4170 
   4171 static void
   4172 releaseWakeLock() {
   4173     release_wake_lock(ANDROID_WAKE_LOCK_NAME);
   4174 }
   4175 
   4176 /**
   4177  * Timer callback to put us back to sleep before the default timeout
   4178  */
   4179 static void
   4180 wakeTimeoutCallback (void *param) {
   4181     // We're using "param != NULL" as a cancellation mechanism
   4182     if (param == NULL) {
   4183         //RLOGD("wakeTimeout: releasing wake lock");
   4184 
   4185         releaseWakeLock();
   4186     } else {
   4187         //RLOGD("wakeTimeout: releasing wake lock CANCELLED");
   4188     }
   4189 }
   4190 
   4191 static int
   4192 decodeVoiceRadioTechnology (RIL_RadioState radioState) {
   4193     switch (radioState) {
   4194         case RADIO_STATE_SIM_NOT_READY:
   4195         case RADIO_STATE_SIM_LOCKED_OR_ABSENT:
   4196         case RADIO_STATE_SIM_READY:
   4197             return RADIO_TECH_UMTS;
   4198 
   4199         case RADIO_STATE_RUIM_NOT_READY:
   4200         case RADIO_STATE_RUIM_READY:
   4201         case RADIO_STATE_RUIM_LOCKED_OR_ABSENT:
   4202         case RADIO_STATE_NV_NOT_READY:
   4203         case RADIO_STATE_NV_READY:
   4204             return RADIO_TECH_1xRTT;
   4205 
   4206         default:
   4207             RLOGD("decodeVoiceRadioTechnology: Invoked with incorrect RadioState");
   4208             return -1;
   4209     }
   4210 }
   4211 
   4212 static int
   4213 decodeCdmaSubscriptionSource (RIL_RadioState radioState) {
   4214     switch (radioState) {
   4215         case RADIO_STATE_SIM_NOT_READY:
   4216         case RADIO_STATE_SIM_LOCKED_OR_ABSENT:
   4217         case RADIO_STATE_SIM_READY:
   4218         case RADIO_STATE_RUIM_NOT_READY:
   4219         case RADIO_STATE_RUIM_READY:
   4220         case RADIO_STATE_RUIM_LOCKED_OR_ABSENT:
   4221             return CDMA_SUBSCRIPTION_SOURCE_RUIM_SIM;
   4222 
   4223         case RADIO_STATE_NV_NOT_READY:
   4224         case RADIO_STATE_NV_READY:
   4225             return CDMA_SUBSCRIPTION_SOURCE_NV;
   4226 
   4227         default:
   4228             RLOGD("decodeCdmaSubscriptionSource: Invoked with incorrect RadioState");
   4229             return -1;
   4230     }
   4231 }
   4232 
   4233 static int
   4234 decodeSimStatus (RIL_RadioState radioState) {
   4235    switch (radioState) {
   4236        case RADIO_STATE_SIM_NOT_READY:
   4237        case RADIO_STATE_RUIM_NOT_READY:
   4238        case RADIO_STATE_NV_NOT_READY:
   4239        case RADIO_STATE_NV_READY:
   4240            return -1;
   4241        case RADIO_STATE_SIM_LOCKED_OR_ABSENT:
   4242        case RADIO_STATE_SIM_READY:
   4243        case RADIO_STATE_RUIM_READY:
   4244        case RADIO_STATE_RUIM_LOCKED_OR_ABSENT:
   4245            return radioState;
   4246        default:
   4247            RLOGD("decodeSimStatus: Invoked with incorrect RadioState");
   4248            return -1;
   4249    }
   4250 }
   4251 
   4252 static bool is3gpp2(int radioTech) {
   4253     switch (radioTech) {
   4254         case RADIO_TECH_IS95A:
   4255         case RADIO_TECH_IS95B:
   4256         case RADIO_TECH_1xRTT:
   4257         case RADIO_TECH_EVDO_0:
   4258         case RADIO_TECH_EVDO_A:
   4259         case RADIO_TECH_EVDO_B:
   4260         case RADIO_TECH_EHRPD:
   4261             return true;
   4262         default:
   4263             return false;
   4264     }
   4265 }
   4266 
   4267 /* If RIL sends SIM states or RUIM states, store the voice radio
   4268  * technology and subscription source information so that they can be
   4269  * returned when telephony framework requests them
   4270  */
   4271 static RIL_RadioState
   4272 processRadioState(RIL_RadioState newRadioState, RIL_SOCKET_ID socket_id) {
   4273 
   4274     if((newRadioState > RADIO_STATE_UNAVAILABLE) && (newRadioState < RADIO_STATE_ON)) {
   4275         int newVoiceRadioTech;
   4276         int newCdmaSubscriptionSource;
   4277         int newSimStatus;
   4278 
   4279         /* This is old RIL. Decode Subscription source and Voice Radio Technology
   4280            from Radio State and send change notifications if there has been a change */
   4281         newVoiceRadioTech = decodeVoiceRadioTechnology(newRadioState);
   4282         if(newVoiceRadioTech != voiceRadioTech) {
   4283             voiceRadioTech = newVoiceRadioTech;
   4284             RIL_UNSOL_RESPONSE(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
   4285                         &voiceRadioTech, sizeof(voiceRadioTech), socket_id);
   4286         }
   4287         if(is3gpp2(newVoiceRadioTech)) {
   4288             newCdmaSubscriptionSource = decodeCdmaSubscriptionSource(newRadioState);
   4289             if(newCdmaSubscriptionSource != cdmaSubscriptionSource) {
   4290                 cdmaSubscriptionSource = newCdmaSubscriptionSource;
   4291                 RIL_UNSOL_RESPONSE(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
   4292                         &cdmaSubscriptionSource, sizeof(cdmaSubscriptionSource), socket_id);
   4293             }
   4294         }
   4295         newSimStatus = decodeSimStatus(newRadioState);
   4296         if(newSimStatus != simRuimStatus) {
   4297             simRuimStatus = newSimStatus;
   4298             RIL_UNSOL_RESPONSE(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0, socket_id);
   4299         }
   4300 
   4301         /* Send RADIO_ON to telephony */
   4302         newRadioState = RADIO_STATE_ON;
   4303     }
   4304 
   4305     return newRadioState;
   4306 }
   4307 
   4308 
   4309 #if defined(ANDROID_MULTI_SIM)
   4310 extern "C"
   4311 void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
   4312                                 size_t datalen, RIL_SOCKET_ID socket_id)
   4313 #else
   4314 extern "C"
   4315 void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
   4316                                 size_t datalen)
   4317 #endif
   4318 {
   4319     int unsolResponseIndex;
   4320     int ret;
   4321     int64_t timeReceived = 0;
   4322     bool shouldScheduleTimeout = false;
   4323     RIL_RadioState newState;
   4324     RIL_SOCKET_ID soc_id = RIL_SOCKET_1;
   4325 
   4326 #if defined(ANDROID_MULTI_SIM)
   4327     soc_id = socket_id;
   4328 #endif
   4329 
   4330 
   4331     if (s_registerCalled == 0) {
   4332         // Ignore RIL_onUnsolicitedResponse before RIL_register
   4333         RLOGW("RIL_onUnsolicitedResponse called before RIL_register");
   4334         return;
   4335     }
   4336 
   4337     unsolResponseIndex = unsolResponse - RIL_UNSOL_RESPONSE_BASE;
   4338 
   4339     if ((unsolResponseIndex < 0)
   4340         || (unsolResponseIndex >= (int32_t)NUM_ELEMS(s_unsolResponses))) {
   4341         RLOGE("unsupported unsolicited response code %d", unsolResponse);
   4342         return;
   4343     }
   4344 
   4345     // Grab a wake lock if needed for this reponse,
   4346     // as we exit we'll either release it immediately
   4347     // or set a timer to release it later.
   4348     switch (s_unsolResponses[unsolResponseIndex].wakeType) {
   4349         case WAKE_PARTIAL:
   4350             grabPartialWakeLock();
   4351             shouldScheduleTimeout = true;
   4352         break;
   4353 
   4354         case DONT_WAKE:
   4355         default:
   4356             // No wake lock is grabed so don't set timeout
   4357             shouldScheduleTimeout = false;
   4358             break;
   4359     }
   4360 
   4361     // Mark the time this was received, doing this
   4362     // after grabing the wakelock incase getting
   4363     // the elapsedRealTime might cause us to goto
   4364     // sleep.
   4365     if (unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
   4366         timeReceived = elapsedRealtime();
   4367     }
   4368 
   4369     appendPrintBuf("[UNSL]< %s", requestToString(unsolResponse));
   4370 
   4371     Parcel p;
   4372 
   4373     p.writeInt32 (RESPONSE_UNSOLICITED);
   4374     p.writeInt32 (unsolResponse);
   4375 
   4376     ret = s_unsolResponses[unsolResponseIndex]
   4377                 .responseFunction(p, const_cast<void*>(data), datalen);
   4378     if (ret != 0) {
   4379         // Problem with the response. Don't continue;
   4380         goto error_exit;
   4381     }
   4382 
   4383     // some things get more payload
   4384     switch(unsolResponse) {
   4385         case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
   4386             newState = processRadioState(CALL_ONSTATEREQUEST(soc_id), soc_id);
   4387             p.writeInt32(newState);
   4388             appendPrintBuf("%s {%s}", printBuf,
   4389                 radioStateToString(CALL_ONSTATEREQUEST(soc_id)));
   4390         break;
   4391 
   4392 
   4393         case RIL_UNSOL_NITZ_TIME_RECEIVED:
   4394             // Store the time that this was received so the
   4395             // handler of this message can account for
   4396             // the time it takes to arrive and process. In
   4397             // particular the system has been known to sleep
   4398             // before this message can be processed.
   4399             p.writeInt64(timeReceived);
   4400         break;
   4401     }
   4402 
   4403     RLOGI("%s UNSOLICITED: %s length:%d", rilSocketIdToString(soc_id), requestToString(unsolResponse), p.dataSize());
   4404     ret = sendResponse(p, soc_id);
   4405     if (ret != 0 && unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
   4406 
   4407         // Unfortunately, NITZ time is not poll/update like everything
   4408         // else in the system. So, if the upstream client isn't connected,
   4409         // keep a copy of the last NITZ response (with receive time noted
   4410         // above) around so we can deliver it when it is connected
   4411 
   4412         if (s_lastNITZTimeData != NULL) {
   4413             free (s_lastNITZTimeData);
   4414             s_lastNITZTimeData = NULL;
   4415         }
   4416 
   4417         s_lastNITZTimeData = malloc(p.dataSize());
   4418         s_lastNITZTimeDataSize = p.dataSize();
   4419         memcpy(s_lastNITZTimeData, p.data(), p.dataSize());
   4420     }
   4421 
   4422     // For now, we automatically go back to sleep after TIMEVAL_WAKE_TIMEOUT
   4423     // FIXME The java code should handshake here to release wake lock
   4424 
   4425     if (shouldScheduleTimeout) {
   4426         // Cancel the previous request
   4427         if (s_last_wake_timeout_info != NULL) {
   4428             s_last_wake_timeout_info->userParam = (void *)1;
   4429         }
   4430 
   4431         s_last_wake_timeout_info
   4432             = internalRequestTimedCallback(wakeTimeoutCallback, NULL,
   4433                                             &TIMEVAL_WAKE_TIMEOUT);
   4434     }
   4435 
   4436     // Normal exit
   4437     return;
   4438 
   4439 error_exit:
   4440     if (shouldScheduleTimeout) {
   4441         releaseWakeLock();
   4442     }
   4443 }
   4444 
   4445 /** FIXME generalize this if you track UserCAllbackInfo, clear it
   4446     when the callback occurs
   4447 */
   4448 static UserCallbackInfo *
   4449 internalRequestTimedCallback (RIL_TimedCallback callback, void *param,
   4450                                 const struct timeval *relativeTime)
   4451 {
   4452     struct timeval myRelativeTime;
   4453     UserCallbackInfo *p_info;
   4454 
   4455     p_info = (UserCallbackInfo *) malloc (sizeof(UserCallbackInfo));
   4456 
   4457     p_info->p_callback = callback;
   4458     p_info->userParam = param;
   4459 
   4460     if (relativeTime == NULL) {
   4461         /* treat null parameter as a 0 relative time */
   4462         memset (&myRelativeTime, 0, sizeof(myRelativeTime));
   4463     } else {
   4464         /* FIXME I think event_add's tv param is really const anyway */
   4465         memcpy (&myRelativeTime, relativeTime, sizeof(myRelativeTime));
   4466     }
   4467 
   4468     ril_event_set(&(p_info->event), -1, false, userTimerCallback, p_info);
   4469 
   4470     ril_timer_add(&(p_info->event), &myRelativeTime);
   4471 
   4472     triggerEvLoop();
   4473     return p_info;
   4474 }
   4475 
   4476 
   4477 extern "C" void
   4478 RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
   4479                                 const struct timeval *relativeTime) {
   4480     internalRequestTimedCallback (callback, param, relativeTime);
   4481 }
   4482 
   4483 const char *
   4484 failCauseToString(RIL_Errno e) {
   4485     switch(e) {
   4486         case RIL_E_SUCCESS: return "E_SUCCESS";
   4487         case RIL_E_RADIO_NOT_AVAILABLE: return "E_RADIO_NOT_AVAILABLE";
   4488         case RIL_E_GENERIC_FAILURE: return "E_GENERIC_FAILURE";
   4489         case RIL_E_PASSWORD_INCORRECT: return "E_PASSWORD_INCORRECT";
   4490         case RIL_E_SIM_PIN2: return "E_SIM_PIN2";
   4491         case RIL_E_SIM_PUK2: return "E_SIM_PUK2";
   4492         case RIL_E_REQUEST_NOT_SUPPORTED: return "E_REQUEST_NOT_SUPPORTED";
   4493         case RIL_E_CANCELLED: return "E_CANCELLED";
   4494         case RIL_E_OP_NOT_ALLOWED_DURING_VOICE_CALL: return "E_OP_NOT_ALLOWED_DURING_VOICE_CALL";
   4495         case RIL_E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW: return "E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW";
   4496         case RIL_E_SMS_SEND_FAIL_RETRY: return "E_SMS_SEND_FAIL_RETRY";
   4497         case RIL_E_SIM_ABSENT:return "E_SIM_ABSENT";
   4498         case RIL_E_ILLEGAL_SIM_OR_ME:return "E_ILLEGAL_SIM_OR_ME";
   4499 #ifdef FEATURE_MULTIMODE_ANDROID
   4500         case RIL_E_SUBSCRIPTION_NOT_AVAILABLE:return "E_SUBSCRIPTION_NOT_AVAILABLE";
   4501         case RIL_E_MODE_NOT_SUPPORTED:return "E_MODE_NOT_SUPPORTED";
   4502 #endif
   4503         default: return "<unknown error>";
   4504     }
   4505 }
   4506 
   4507 const char *
   4508 radioStateToString(RIL_RadioState s) {
   4509     switch(s) {
   4510         case RADIO_STATE_OFF: return "RADIO_OFF";
   4511         case RADIO_STATE_UNAVAILABLE: return "RADIO_UNAVAILABLE";
   4512         case RADIO_STATE_SIM_NOT_READY: return "RADIO_SIM_NOT_READY";
   4513         case RADIO_STATE_SIM_LOCKED_OR_ABSENT: return "RADIO_SIM_LOCKED_OR_ABSENT";
   4514         case RADIO_STATE_SIM_READY: return "RADIO_SIM_READY";
   4515         case RADIO_STATE_RUIM_NOT_READY:return"RADIO_RUIM_NOT_READY";
   4516         case RADIO_STATE_RUIM_READY:return"RADIO_RUIM_READY";
   4517         case RADIO_STATE_RUIM_LOCKED_OR_ABSENT:return"RADIO_RUIM_LOCKED_OR_ABSENT";
   4518         case RADIO_STATE_NV_NOT_READY:return"RADIO_NV_NOT_READY";
   4519         case RADIO_STATE_NV_READY:return"RADIO_NV_READY";
   4520         case RADIO_STATE_ON:return"RADIO_ON";
   4521         default: return "<unknown state>";
   4522     }
   4523 }
   4524 
   4525 const char *
   4526 callStateToString(RIL_CallState s) {
   4527     switch(s) {
   4528         case RIL_CALL_ACTIVE : return "ACTIVE";
   4529         case RIL_CALL_HOLDING: return "HOLDING";
   4530         case RIL_CALL_DIALING: return "DIALING";
   4531         case RIL_CALL_ALERTING: return "ALERTING";
   4532         case RIL_CALL_INCOMING: return "INCOMING";
   4533         case RIL_CALL_WAITING: return "WAITING";
   4534         default: return "<unknown state>";
   4535     }
   4536 }
   4537 
   4538 const char *
   4539 requestToString(int request) {
   4540 /*
   4541  cat libs/telephony/ril_commands.h \
   4542  | egrep "^ *{RIL_" \
   4543  | sed -re 's/\{RIL_([^,]+),[^,]+,([^}]+).+/case RIL_\1: return "\1";/'
   4544 
   4545 
   4546  cat libs/telephony/ril_unsol_commands.h \
   4547  | egrep "^ *{RIL_" \
   4548  | sed -re 's/\{RIL_([^,]+),([^}]+).+/case RIL_\1: return "\1";/'
   4549 
   4550 */
   4551     switch(request) {
   4552         case RIL_REQUEST_GET_SIM_STATUS: return "GET_SIM_STATUS";
   4553         case RIL_REQUEST_ENTER_SIM_PIN: return "ENTER_SIM_PIN";
   4554         case RIL_REQUEST_ENTER_SIM_PUK: return "ENTER_SIM_PUK";
   4555         case RIL_REQUEST_ENTER_SIM_PIN2: return "ENTER_SIM_PIN2";
   4556         case RIL_REQUEST_ENTER_SIM_PUK2: return "ENTER_SIM_PUK2";
   4557         case RIL_REQUEST_CHANGE_SIM_PIN: return "CHANGE_SIM_PIN";
   4558         case RIL_REQUEST_CHANGE_SIM_PIN2: return "CHANGE_SIM_PIN2";
   4559         case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION: return "ENTER_NETWORK_DEPERSONALIZATION";
   4560         case RIL_REQUEST_GET_CURRENT_CALLS: return "GET_CURRENT_CALLS";
   4561         case RIL_REQUEST_DIAL: return "DIAL";
   4562         case RIL_REQUEST_GET_IMSI: return "GET_IMSI";
   4563         case RIL_REQUEST_HANGUP: return "HANGUP";
   4564         case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND: return "HANGUP_WAITING_OR_BACKGROUND";
   4565         case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND: return "HANGUP_FOREGROUND_RESUME_BACKGROUND";
   4566         case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE: return "SWITCH_WAITING_OR_HOLDING_AND_ACTIVE";
   4567         case RIL_REQUEST_CONFERENCE: return "CONFERENCE";
   4568         case RIL_REQUEST_UDUB: return "UDUB";
   4569         case RIL_REQUEST_LAST_CALL_FAIL_CAUSE: return "LAST_CALL_FAIL_CAUSE";
   4570         case RIL_REQUEST_SIGNAL_STRENGTH: return "SIGNAL_STRENGTH";
   4571         case RIL_REQUEST_VOICE_REGISTRATION_STATE: return "VOICE_REGISTRATION_STATE";
   4572         case RIL_REQUEST_DATA_REGISTRATION_STATE: return "DATA_REGISTRATION_STATE";
   4573         case RIL_REQUEST_OPERATOR: return "OPERATOR";
   4574         case RIL_REQUEST_RADIO_POWER: return "RADIO_POWER";
   4575         case RIL_REQUEST_DTMF: return "DTMF";
   4576         case RIL_REQUEST_SEND_SMS: return "SEND_SMS";
   4577         case RIL_REQUEST_SEND_SMS_EXPECT_MORE: return "SEND_SMS_EXPECT_MORE";
   4578         case RIL_REQUEST_SETUP_DATA_CALL: return "SETUP_DATA_CALL";
   4579         case RIL_REQUEST_SIM_IO: return "SIM_IO";
   4580         case RIL_REQUEST_SEND_USSD: return "SEND_USSD";
   4581         case RIL_REQUEST_CANCEL_USSD: return "CANCEL_USSD";
   4582         case RIL_REQUEST_GET_CLIR: return "GET_CLIR";
   4583         case RIL_REQUEST_SET_CLIR: return "SET_CLIR";
   4584         case RIL_REQUEST_QUERY_CALL_FORWARD_STATUS: return "QUERY_CALL_FORWARD_STATUS";
   4585         case RIL_REQUEST_SET_CALL_FORWARD: return "SET_CALL_FORWARD";
   4586         case RIL_REQUEST_QUERY_CALL_WAITING: return "QUERY_CALL_WAITING";
   4587         case RIL_REQUEST_SET_CALL_WAITING: return "SET_CALL_WAITING";
   4588         case RIL_REQUEST_SMS_ACKNOWLEDGE: return "SMS_ACKNOWLEDGE";
   4589         case RIL_REQUEST_GET_IMEI: return "GET_IMEI";
   4590         case RIL_REQUEST_GET_IMEISV: return "GET_IMEISV";
   4591         case RIL_REQUEST_ANSWER: return "ANSWER";
   4592         case RIL_REQUEST_DEACTIVATE_DATA_CALL: return "DEACTIVATE_DATA_CALL";
   4593         case RIL_REQUEST_QUERY_FACILITY_LOCK: return "QUERY_FACILITY_LOCK";
   4594         case RIL_REQUEST_SET_FACILITY_LOCK: return "SET_FACILITY_LOCK";
   4595         case RIL_REQUEST_CHANGE_BARRING_PASSWORD: return "CHANGE_BARRING_PASSWORD";
   4596         case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE: return "QUERY_NETWORK_SELECTION_MODE";
   4597         case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC: return "SET_NETWORK_SELECTION_AUTOMATIC";
   4598         case RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL: return "SET_NETWORK_SELECTION_MANUAL";
   4599         case RIL_REQUEST_QUERY_AVAILABLE_NETWORKS : return "QUERY_AVAILABLE_NETWORKS ";
   4600         case RIL_REQUEST_DTMF_START: return "DTMF_START";
   4601         case RIL_REQUEST_DTMF_STOP: return "DTMF_STOP";
   4602         case RIL_REQUEST_BASEBAND_VERSION: return "BASEBAND_VERSION";
   4603         case RIL_REQUEST_SEPARATE_CONNECTION: return "SEPARATE_CONNECTION";
   4604         case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE: return "SET_PREFERRED_NETWORK_TYPE";
   4605         case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE: return "GET_PREFERRED_NETWORK_TYPE";
   4606         case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS: return "GET_NEIGHBORING_CELL_IDS";
   4607         case RIL_REQUEST_SET_MUTE: return "SET_MUTE";
   4608         case RIL_REQUEST_GET_MUTE: return "GET_MUTE";
   4609         case RIL_REQUEST_QUERY_CLIP: return "QUERY_CLIP";
   4610         case RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE: return "LAST_DATA_CALL_FAIL_CAUSE";
   4611         case RIL_REQUEST_DATA_CALL_LIST: return "DATA_CALL_LIST";
   4612         case RIL_REQUEST_RESET_RADIO: return "RESET_RADIO";
   4613         case RIL_REQUEST_OEM_HOOK_RAW: return "OEM_HOOK_RAW";
   4614         case RIL_REQUEST_OEM_HOOK_STRINGS: return "OEM_HOOK_STRINGS";
   4615         case RIL_REQUEST_SET_BAND_MODE: return "SET_BAND_MODE";
   4616         case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE: return "QUERY_AVAILABLE_BAND_MODE";
   4617         case RIL_REQUEST_STK_GET_PROFILE: return "STK_GET_PROFILE";
   4618         case RIL_REQUEST_STK_SET_PROFILE: return "STK_SET_PROFILE";
   4619         case RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND: return "STK_SEND_ENVELOPE_COMMAND";
   4620         case RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE: return "STK_SEND_TERMINAL_RESPONSE";
   4621         case RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM: return "STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM";
   4622         case RIL_REQUEST_SCREEN_STATE: return "SCREEN_STATE";
   4623         case RIL_REQUEST_EXPLICIT_CALL_TRANSFER: return "EXPLICIT_CALL_TRANSFER";
   4624         case RIL_REQUEST_SET_LOCATION_UPDATES: return "SET_LOCATION_UPDATES";
   4625         case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:return"CDMA_SET_SUBSCRIPTION_SOURCE";
   4626         case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:return"CDMA_SET_ROAMING_PREFERENCE";
   4627         case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:return"CDMA_QUERY_ROAMING_PREFERENCE";
   4628         case RIL_REQUEST_SET_TTY_MODE:return"SET_TTY_MODE";
   4629         case RIL_REQUEST_QUERY_TTY_MODE:return"QUERY_TTY_MODE";
   4630         case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:return"CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE";
   4631         case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:return"CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE";
   4632         case RIL_REQUEST_CDMA_FLASH:return"CDMA_FLASH";
   4633         case RIL_REQUEST_CDMA_BURST_DTMF:return"CDMA_BURST_DTMF";
   4634         case RIL_REQUEST_CDMA_SEND_SMS:return"CDMA_SEND_SMS";
   4635         case RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE:return"CDMA_SMS_ACKNOWLEDGE";
   4636         case RIL_REQUEST_GSM_GET_BROADCAST_SMS_CONFIG:return"GSM_GET_BROADCAST_SMS_CONFIG";
   4637         case RIL_REQUEST_GSM_SET_BROADCAST_SMS_CONFIG:return"GSM_SET_BROADCAST_SMS_CONFIG";
   4638         case RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG:return "CDMA_GET_BROADCAST_SMS_CONFIG";
   4639         case RIL_REQUEST_CDMA_SET_BROADCAST_SMS_CONFIG:return "CDMA_SET_BROADCAST_SMS_CONFIG";
   4640         case RIL_REQUEST_CDMA_SMS_BROADCAST_ACTIVATION:return "CDMA_SMS_BROADCAST_ACTIVATION";
   4641         case RIL_REQUEST_CDMA_VALIDATE_AND_WRITE_AKEY: return"CDMA_VALIDATE_AND_WRITE_AKEY";
   4642         case RIL_REQUEST_CDMA_SUBSCRIPTION: return"CDMA_SUBSCRIPTION";
   4643         case RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM: return "CDMA_WRITE_SMS_TO_RUIM";
   4644         case RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM: return "CDMA_DELETE_SMS_ON_RUIM";
   4645         case RIL_REQUEST_DEVICE_IDENTITY: return "DEVICE_IDENTITY";
   4646         case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE: return "EXIT_EMERGENCY_CALLBACK_MODE";
   4647         case RIL_REQUEST_GET_SMSC_ADDRESS: return "GET_SMSC_ADDRESS";
   4648         case RIL_REQUEST_SET_SMSC_ADDRESS: return "SET_SMSC_ADDRESS";
   4649         case RIL_REQUEST_REPORT_SMS_MEMORY_STATUS: return "REPORT_SMS_MEMORY_STATUS";
   4650         case RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING: return "REPORT_STK_SERVICE_IS_RUNNING";
   4651         case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE: return "CDMA_GET_SUBSCRIPTION_SOURCE";
   4652         case RIL_REQUEST_ISIM_AUTHENTICATION: return "ISIM_AUTHENTICATION";
   4653         case RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU: return "RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU";
   4654         case RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS: return "RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS";
   4655         case RIL_REQUEST_VOICE_RADIO_TECH: return "VOICE_RADIO_TECH";
   4656         case RIL_REQUEST_GET_CELL_INFO_LIST: return"GET_CELL_INFO_LIST";
   4657         case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE: return"SET_UNSOL_CELL_INFO_LIST_RATE";
   4658         case RIL_REQUEST_SET_INITIAL_ATTACH_APN: return "RIL_REQUEST_SET_INITIAL_ATTACH_APN";
   4659         case RIL_REQUEST_IMS_REGISTRATION_STATE: return "IMS_REGISTRATION_STATE";
   4660         case RIL_REQUEST_IMS_SEND_SMS: return "IMS_SEND_SMS";
   4661         case RIL_REQUEST_SIM_TRANSMIT_APDU_BASIC: return "SIM_TRANSMIT_APDU_BASIC";
   4662         case RIL_REQUEST_SIM_OPEN_CHANNEL: return "SIM_OPEN_CHANNEL";
   4663         case RIL_REQUEST_SIM_CLOSE_CHANNEL: return "SIM_CLOSE_CHANNEL";
   4664         case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL: return "SIM_TRANSMIT_APDU_CHANNEL";
   4665         case RIL_REQUEST_SET_UICC_SUBSCRIPTION: return "SET_UICC_SUBSCRIPTION";
   4666         case RIL_REQUEST_ALLOW_DATA: return "ALLOW_DATA";
   4667         case RIL_REQUEST_GET_HARDWARE_CONFIG: return "GET_HARDWARE_CONFIG";
   4668         case RIL_REQUEST_SIM_AUTHENTICATION: return "SIM_AUTHENTICATION";
   4669         case RIL_REQUEST_GET_DC_RT_INFO: return "GET_DC_RT_INFO";
   4670         case RIL_REQUEST_SET_DC_RT_INFO_RATE: return "SET_DC_RT_INFO_RATE";
   4671         case RIL_REQUEST_SET_DATA_PROFILE: return "SET_DATA_PROFILE";
   4672         case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: return "UNSOL_RESPONSE_RADIO_STATE_CHANGED";
   4673         case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: return "UNSOL_RESPONSE_CALL_STATE_CHANGED";
   4674         case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED: return "UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED";
   4675         case RIL_UNSOL_RESPONSE_NEW_SMS: return "UNSOL_RESPONSE_NEW_SMS";
   4676         case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT: return "UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT";
   4677         case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM: return "UNSOL_RESPONSE_NEW_SMS_ON_SIM";
   4678         case RIL_UNSOL_ON_USSD: return "UNSOL_ON_USSD";
   4679         case RIL_UNSOL_ON_USSD_REQUEST: return "UNSOL_ON_USSD_REQUEST(obsolete)";
   4680         case RIL_UNSOL_NITZ_TIME_RECEIVED: return "UNSOL_NITZ_TIME_RECEIVED";
   4681         case RIL_UNSOL_SIGNAL_STRENGTH: return "UNSOL_SIGNAL_STRENGTH";
   4682         case RIL_UNSOL_STK_SESSION_END: return "UNSOL_STK_SESSION_END";
   4683         case RIL_UNSOL_STK_PROACTIVE_COMMAND: return "UNSOL_STK_PROACTIVE_COMMAND";
   4684         case RIL_UNSOL_STK_EVENT_NOTIFY: return "UNSOL_STK_EVENT_NOTIFY";
   4685         case RIL_UNSOL_STK_CALL_SETUP: return "UNSOL_STK_CALL_SETUP";
   4686         case RIL_UNSOL_SIM_SMS_STORAGE_FULL: return "UNSOL_SIM_SMS_STORAGE_FUL";
   4687         case RIL_UNSOL_SIM_REFRESH: return "UNSOL_SIM_REFRESH";
   4688         case RIL_UNSOL_DATA_CALL_LIST_CHANGED: return "UNSOL_DATA_CALL_LIST_CHANGED";
   4689         case RIL_UNSOL_CALL_RING: return "UNSOL_CALL_RING";
   4690         case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: return "UNSOL_RESPONSE_SIM_STATUS_CHANGED";
   4691         case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS: return "UNSOL_NEW_CDMA_SMS";
   4692         case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: return "UNSOL_NEW_BROADCAST_SMS";
   4693         case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL: return "UNSOL_CDMA_RUIM_SMS_STORAGE_FULL";
   4694         case RIL_UNSOL_RESTRICTED_STATE_CHANGED: return "UNSOL_RESTRICTED_STATE_CHANGED";
   4695         case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE: return "UNSOL_ENTER_EMERGENCY_CALLBACK_MODE";
   4696         case RIL_UNSOL_CDMA_CALL_WAITING: return "UNSOL_CDMA_CALL_WAITING";
   4697         case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS: return "UNSOL_CDMA_OTA_PROVISION_STATUS";
   4698         case RIL_UNSOL_CDMA_INFO_REC: return "UNSOL_CDMA_INFO_REC";
   4699         case RIL_UNSOL_OEM_HOOK_RAW: return "UNSOL_OEM_HOOK_RAW";
   4700         case RIL_UNSOL_RINGBACK_TONE: return "UNSOL_RINGBACK_TONE";
   4701         case RIL_UNSOL_RESEND_INCALL_MUTE: return "UNSOL_RESEND_INCALL_MUTE";
   4702         case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED: return "UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED";
   4703         case RIL_UNSOL_CDMA_PRL_CHANGED: return "UNSOL_CDMA_PRL_CHANGED";
   4704         case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE: return "UNSOL_EXIT_EMERGENCY_CALLBACK_MODE";
   4705         case RIL_UNSOL_RIL_CONNECTED: return "UNSOL_RIL_CONNECTED";
   4706         case RIL_UNSOL_VOICE_RADIO_TECH_CHANGED: return "UNSOL_VOICE_RADIO_TECH_CHANGED";
   4707         case RIL_UNSOL_CELL_INFO_LIST: return "UNSOL_CELL_INFO_LIST";
   4708         case RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED: return "RESPONSE_IMS_NETWORK_STATE_CHANGED";
   4709         case RIL_UNSOL_UICC_SUBSCRIPTION_STATUS_CHANGED: return "UNSOL_UICC_SUBSCRIPTION_STATUS_CHANGED";
   4710         case RIL_UNSOL_SRVCC_STATE_NOTIFY: return "UNSOL_SRVCC_STATE_NOTIFY";
   4711         case RIL_UNSOL_HARDWARE_CONFIG_CHANGED: return "HARDWARE_CONFIG_CHANGED";
   4712         case RIL_UNSOL_DC_RT_INFO_CHANGED: return "UNSOL_DC_RT_INFO_CHANGED";
   4713         case RIL_REQUEST_SHUTDOWN: return "SHUTDOWN";
   4714         default: return "<unknown request>";
   4715     }
   4716 }
   4717 
   4718 const char *
   4719 rilSocketIdToString(RIL_SOCKET_ID socket_id)
   4720 {
   4721     switch(socket_id) {
   4722         case RIL_SOCKET_1:
   4723             return "RIL_SOCKET_1";
   4724 #if (SIM_COUNT >= 2)
   4725         case RIL_SOCKET_2:
   4726             return "RIL_SOCKET_2";
   4727 #endif
   4728 #if (SIM_COUNT >= 3)
   4729         case RIL_SOCKET_3:
   4730             return "RIL_SOCKET_3";
   4731 #endif
   4732 #if (SIM_COUNT >= 4)
   4733         case RIL_SOCKET_4:
   4734             return "RIL_SOCKET_4";
   4735 #endif
   4736         default:
   4737             return "not a valid RIL";
   4738     }
   4739 }
   4740 
   4741 } /* namespace android */
   4742