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