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 #include <telephony/ril.h>
22 #include <telephony/ril_cdma_sms.h>
23 #include <cutils/sockets.h>
24 #include <cutils/jstring.h>
25 #include <telephony/record_stream.h>
26 #include <utils/Log.h>
27 #include <utils/SystemClock.h>
28 #include <pthread.h>
29 #include <cutils/jstring.h>
30 #include <sys/types.h>
31 #include <sys/limits.h>
32 #include <sys/system_properties.h>
33 #include <pwd.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <time.h>
41 #include <errno.h>
42 #include <assert.h>
43 #include <ctype.h>
44 #include <sys/un.h>
45 #include <assert.h>
46 #include <netinet/in.h>
47 #include <cutils/properties.h>
48 #include <RilSapSocket.h>
49 #include <ril_service.h>
50 #include <sap_service.h>
51 
52 extern "C" void
53 RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen);
54 
55 extern "C" void
56 RIL_onRequestAck(RIL_Token t);
57 namespace android {
58 
59 #define PHONE_PROCESS "radio"
60 #define BLUETOOTH_PROCESS "bluetooth"
61 
62 #define ANDROID_WAKE_LOCK_NAME "radio-interface"
63 
64 #define ANDROID_WAKE_LOCK_SECS 0
65 #define ANDROID_WAKE_LOCK_USECS 200000
66 
67 #define PROPERTY_RIL_IMPL "gsm.version.ril-impl"
68 
69 // match with constant in RIL.java
70 #define MAX_COMMAND_BYTES (8 * 1024)
71 
72 // Basically: memset buffers that the client library
73 // shouldn't be using anymore in an attempt to find
74 // memory usage issues sooner.
75 #define MEMSET_FREED 1
76 
77 #define NUM_ELEMS(a)     (sizeof (a) / sizeof (a)[0])
78 
79 /* Negative values for private RIL errno's */
80 #define RIL_ERRNO_INVALID_RESPONSE (-1)
81 #define RIL_ERRNO_NO_MEMORY (-12)
82 
83 // request, response, and unsolicited msg print macro
84 #define PRINTBUF_SIZE 8096
85 
86 enum WakeType {DONT_WAKE, WAKE_PARTIAL};
87 
88 typedef struct {
89     int requestNumber;
90     int (*responseFunction) (int slotId, int responseType, int token,
91             RIL_Errno e, void *response, size_t responselen);
92     WakeType wakeType;
93 } UnsolResponseInfo;
94 
95 typedef struct UserCallbackInfo {
96     RIL_TimedCallback p_callback;
97     void *userParam;
98     struct ril_event event;
99     struct UserCallbackInfo *p_next;
100 } UserCallbackInfo;
101 
102 extern "C" const char * failCauseToString(RIL_Errno);
103 extern "C" const char * callStateToString(RIL_CallState);
104 extern "C" const char * radioStateToString(RIL_RadioState);
105 extern "C" const char * rilSocketIdToString(RIL_SOCKET_ID socket_id);
106 
107 extern "C"
108 char ril_service_name_base[MAX_SERVICE_NAME_LENGTH] = RIL_SERVICE_NAME_BASE;
109 extern "C"
110 char ril_service_name[MAX_SERVICE_NAME_LENGTH] = RIL1_SERVICE_NAME;
111 /*******************************************************************/
112 
113 RIL_RadioFunctions s_callbacks = {0, NULL, NULL, NULL, NULL, NULL};
114 static int s_registerCalled = 0;
115 
116 static pthread_t s_tid_dispatch;
117 static pthread_t s_tid_reader;
118 static int s_started = 0;
119 
120 static int s_fdDebug = -1;
121 static int s_fdDebug_socket2 = -1;
122 
123 static int s_fdWakeupRead;
124 static int s_fdWakeupWrite;
125 
126 int s_wakelock_count = 0;
127 
128 static struct ril_event s_wakeupfd_event;
129 
130 static pthread_mutex_t s_pendingRequestsMutex = PTHREAD_MUTEX_INITIALIZER;
131 static pthread_mutex_t s_writeMutex = PTHREAD_MUTEX_INITIALIZER;
132 static pthread_mutex_t s_wakeLockCountMutex = PTHREAD_MUTEX_INITIALIZER;
133 static RequestInfo *s_pendingRequests = NULL;
134 
135 #if (SIM_COUNT >= 2)
136 static pthread_mutex_t s_pendingRequestsMutex_socket2  = PTHREAD_MUTEX_INITIALIZER;
137 static pthread_mutex_t s_writeMutex_socket2            = PTHREAD_MUTEX_INITIALIZER;
138 static RequestInfo *s_pendingRequests_socket2          = NULL;
139 #endif
140 
141 #if (SIM_COUNT >= 3)
142 static pthread_mutex_t s_pendingRequestsMutex_socket3  = PTHREAD_MUTEX_INITIALIZER;
143 static pthread_mutex_t s_writeMutex_socket3            = PTHREAD_MUTEX_INITIALIZER;
144 static RequestInfo *s_pendingRequests_socket3          = NULL;
145 #endif
146 
147 #if (SIM_COUNT >= 4)
148 static pthread_mutex_t s_pendingRequestsMutex_socket4  = PTHREAD_MUTEX_INITIALIZER;
149 static pthread_mutex_t s_writeMutex_socket4            = PTHREAD_MUTEX_INITIALIZER;
150 static RequestInfo *s_pendingRequests_socket4          = NULL;
151 #endif
152 
153 static struct ril_event s_wake_timeout_event;
154 static struct ril_event s_debug_event;
155 
156 
157 static const struct timeval TIMEVAL_WAKE_TIMEOUT = {ANDROID_WAKE_LOCK_SECS,ANDROID_WAKE_LOCK_USECS};
158 
159 
160 static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
161 static pthread_cond_t s_startupCond = PTHREAD_COND_INITIALIZER;
162 
163 static pthread_mutex_t s_dispatchMutex = PTHREAD_MUTEX_INITIALIZER;
164 static pthread_cond_t s_dispatchCond = PTHREAD_COND_INITIALIZER;
165 
166 static RequestInfo *s_toDispatchHead = NULL;
167 static RequestInfo *s_toDispatchTail = NULL;
168 
169 static UserCallbackInfo *s_last_wake_timeout_info = NULL;
170 
171 static void *s_lastNITZTimeData = NULL;
172 static size_t s_lastNITZTimeDataSize;
173 
174 #if RILC_LOG
175     static char printBuf[PRINTBUF_SIZE];
176 #endif
177 
178 /*******************************************************************/
179 static void grabPartialWakeLock();
180 void releaseWakeLock();
181 static void wakeTimeoutCallback(void *);
182 
183 #ifdef RIL_SHLIB
184 #if defined(ANDROID_MULTI_SIM)
185 extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
186                                 size_t datalen, RIL_SOCKET_ID socket_id);
187 #else
188 extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
189                                 size_t datalen);
190 #endif
191 #endif
192 
193 #if defined(ANDROID_MULTI_SIM)
194 #define RIL_UNSOL_RESPONSE(a, b, c, d) RIL_onUnsolicitedResponse((a), (b), (c), (d))
195 #else
196 #define RIL_UNSOL_RESPONSE(a, b, c, d) RIL_onUnsolicitedResponse((a), (b), (c))
197 #endif
198 
199 static UserCallbackInfo * internalRequestTimedCallback
200     (RIL_TimedCallback callback, void *param,
201         const struct timeval *relativeTime);
202 
203 /** Index == requestNumber */
204 static CommandInfo s_commands[] = {
205 #include "ril_commands.h"
206 };
207 
208 static UnsolResponseInfo s_unsolResponses[] = {
209 #include "ril_unsol_commands.h"
210 };
211 
RIL_getServiceName()212 char * RIL_getServiceName() {
213     return ril_service_name;
214 }
215 
216 extern "C"
RIL_setServiceName(const char * s)217 void RIL_setServiceName(const char * s) {
218     strncpy(ril_service_name, s, MAX_SERVICE_NAME_LENGTH);
219 }
220 
221 RequestInfo *
addRequestToList(int serial,int slotId,int request)222 addRequestToList(int serial, int slotId, int request) {
223     RequestInfo *pRI;
224     int ret;
225     RIL_SOCKET_ID socket_id = (RIL_SOCKET_ID) slotId;
226     /* Hook for current context */
227     /* pendingRequestsMutextHook refer to &s_pendingRequestsMutex */
228     pthread_mutex_t* pendingRequestsMutexHook = &s_pendingRequestsMutex;
229     /* pendingRequestsHook refer to &s_pendingRequests */
230     RequestInfo**    pendingRequestsHook = &s_pendingRequests;
231 
232 #if (SIM_COUNT >= 2)
233     if (socket_id == RIL_SOCKET_2) {
234         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket2;
235         pendingRequestsHook = &s_pendingRequests_socket2;
236     }
237 #if (SIM_COUNT >= 3)
238     else if (socket_id == RIL_SOCKET_3) {
239         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket3;
240         pendingRequestsHook = &s_pendingRequests_socket3;
241     }
242 #endif
243 #if (SIM_COUNT >= 4)
244     else if (socket_id == RIL_SOCKET_4) {
245         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket4;
246         pendingRequestsHook = &s_pendingRequests_socket4;
247     }
248 #endif
249 #endif
250 
251     pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
252     if (pRI == NULL) {
253         RLOGE("Memory allocation failed for request %s", requestToString(request));
254         return NULL;
255     }
256 
257     pRI->token = serial;
258     pRI->pCI = &(s_commands[request]);
259     pRI->socket_id = socket_id;
260 
261     ret = pthread_mutex_lock(pendingRequestsMutexHook);
262     assert (ret == 0);
263 
264     pRI->p_next = *pendingRequestsHook;
265     *pendingRequestsHook = pRI;
266 
267     ret = pthread_mutex_unlock(pendingRequestsMutexHook);
268     assert (ret == 0);
269 
270     return pRI;
271 }
272 
triggerEvLoop()273 static void triggerEvLoop() {
274     int ret;
275     if (!pthread_equal(pthread_self(), s_tid_dispatch)) {
276         /* trigger event loop to wakeup. No reason to do this,
277          * if we're in the event loop thread */
278          do {
279             ret = write (s_fdWakeupWrite, " ", 1);
280          } while (ret < 0 && errno == EINTR);
281     }
282 }
283 
rilEventAddWakeup(struct ril_event * ev)284 static void rilEventAddWakeup(struct ril_event *ev) {
285     ril_event_add(ev);
286     triggerEvLoop();
287 }
288 
289 /**
290  * A write on the wakeup fd is done just to pop us out of select()
291  * We empty the buffer here and then ril_event will reset the timers on the
292  * way back down
293  */
processWakeupCallback(int fd,short flags,void * param)294 static void processWakeupCallback(int fd, short flags, void *param) {
295     char buff[16];
296     int ret;
297 
298     RLOGV("processWakeupCallback");
299 
300     /* empty our wakeup socket out */
301     do {
302         ret = read(s_fdWakeupRead, &buff, sizeof(buff));
303     } while (ret > 0 || (ret < 0 && errno == EINTR));
304 }
305 
resendLastNITZTimeData(RIL_SOCKET_ID socket_id)306 static void resendLastNITZTimeData(RIL_SOCKET_ID socket_id) {
307     if (s_lastNITZTimeData != NULL) {
308         int responseType = (s_callbacks.version >= 13)
309                            ? RESPONSE_UNSOLICITED_ACK_EXP
310                            : RESPONSE_UNSOLICITED;
311         int ret = radio::nitzTimeReceivedInd(
312             (int)socket_id, responseType, 0,
313             RIL_E_SUCCESS, s_lastNITZTimeData, s_lastNITZTimeDataSize);
314         if (ret == 0) {
315             free(s_lastNITZTimeData);
316             s_lastNITZTimeData = NULL;
317         }
318     }
319 }
320 
onNewCommandConnect(RIL_SOCKET_ID socket_id)321 void onNewCommandConnect(RIL_SOCKET_ID socket_id) {
322     // Inform we are connected and the ril version
323     int rilVer = s_callbacks.version;
324     RIL_UNSOL_RESPONSE(RIL_UNSOL_RIL_CONNECTED,
325                                     &rilVer, sizeof(rilVer), socket_id);
326 
327     // implicit radio state changed
328     RIL_UNSOL_RESPONSE(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
329                                     NULL, 0, socket_id);
330 
331     // Send last NITZ time data, in case it was missed
332     if (s_lastNITZTimeData != NULL) {
333         resendLastNITZTimeData(socket_id);
334     }
335 
336     // Get version string
337     if (s_callbacks.getVersion != NULL) {
338         const char *version;
339         version = s_callbacks.getVersion();
340         RLOGI("RIL Daemon version: %s\n", version);
341 
342         property_set(PROPERTY_RIL_IMPL, version);
343     } else {
344         RLOGI("RIL Daemon version: unavailable\n");
345         property_set(PROPERTY_RIL_IMPL, "unavailable");
346     }
347 
348 }
349 
userTimerCallback(int fd,short flags,void * param)350 static void userTimerCallback (int fd, short flags, void *param) {
351     UserCallbackInfo *p_info;
352 
353     p_info = (UserCallbackInfo *)param;
354 
355     p_info->p_callback(p_info->userParam);
356 
357 
358     // FIXME generalize this...there should be a cancel mechanism
359     if (s_last_wake_timeout_info != NULL && s_last_wake_timeout_info == p_info) {
360         s_last_wake_timeout_info = NULL;
361     }
362 
363     free(p_info);
364 }
365 
366 
367 static void *
eventLoop(void * param)368 eventLoop(void *param) {
369     int ret;
370     int filedes[2];
371 
372     ril_event_init();
373 
374     pthread_mutex_lock(&s_startupMutex);
375 
376     s_started = 1;
377     pthread_cond_broadcast(&s_startupCond);
378 
379     pthread_mutex_unlock(&s_startupMutex);
380 
381     ret = pipe(filedes);
382 
383     if (ret < 0) {
384         RLOGE("Error in pipe() errno:%d", errno);
385         return NULL;
386     }
387 
388     s_fdWakeupRead = filedes[0];
389     s_fdWakeupWrite = filedes[1];
390 
391     fcntl(s_fdWakeupRead, F_SETFL, O_NONBLOCK);
392 
393     ril_event_set (&s_wakeupfd_event, s_fdWakeupRead, true,
394                 processWakeupCallback, NULL);
395 
396     rilEventAddWakeup (&s_wakeupfd_event);
397 
398     // Only returns on error
399     ril_event_loop();
400     RLOGE ("error in event_loop_base errno:%d", errno);
401     // kill self to restart on error
402     kill(0, SIGKILL);
403 
404     return NULL;
405 }
406 
407 extern "C" void
RIL_startEventLoop(void)408 RIL_startEventLoop(void) {
409     /* spin up eventLoop thread and wait for it to get started */
410     s_started = 0;
411     pthread_mutex_lock(&s_startupMutex);
412 
413     pthread_attr_t attr;
414     pthread_attr_init(&attr);
415     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
416 
417     int result = pthread_create(&s_tid_dispatch, &attr, eventLoop, NULL);
418     if (result != 0) {
419         RLOGE("Failed to create dispatch thread: %s", strerror(result));
420         goto done;
421     }
422 
423     while (s_started == 0) {
424         pthread_cond_wait(&s_startupCond, &s_startupMutex);
425     }
426 
427 done:
428     pthread_mutex_unlock(&s_startupMutex);
429 }
430 
431 // Used for testing purpose only.
RIL_setcallbacks(const RIL_RadioFunctions * callbacks)432 extern "C" void RIL_setcallbacks (const RIL_RadioFunctions *callbacks) {
433     memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
434 }
435 
436 extern "C" void
RIL_register(const RIL_RadioFunctions * callbacks)437 RIL_register (const RIL_RadioFunctions *callbacks) {
438     int ret;
439     int flags;
440 
441     RLOGI("SIM_COUNT: %d", SIM_COUNT);
442 
443     if (callbacks == NULL) {
444         RLOGE("RIL_register: RIL_RadioFunctions * null");
445         return;
446     }
447     if (callbacks->version < RIL_VERSION_MIN) {
448         RLOGE("RIL_register: version %d is to old, min version is %d",
449              callbacks->version, RIL_VERSION_MIN);
450         return;
451     }
452 
453     RLOGE("RIL_register: RIL version %d", callbacks->version);
454 
455     if (s_registerCalled > 0) {
456         RLOGE("RIL_register has been called more than once. "
457                 "Subsequent call ignored");
458         return;
459     }
460 
461     memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
462 
463     s_registerCalled = 1;
464 
465     RLOGI("s_registerCalled flag set, %d", s_started);
466     // Little self-check
467 
468     for (int i = 0; i < (int)NUM_ELEMS(s_commands); i++) {
469         assert(i == s_commands[i].requestNumber);
470     }
471 
472     for (int i = 0; i < (int)NUM_ELEMS(s_unsolResponses); i++) {
473         assert(i + RIL_UNSOL_RESPONSE_BASE
474                 == s_unsolResponses[i].requestNumber);
475     }
476 
477     radio::registerService(&s_callbacks, s_commands);
478     RLOGI("RILHIDL called registerService");
479 
480 }
481 
482 extern "C" void
RIL_register_socket(RIL_RadioFunctions * (* Init)(const struct RIL_Env *,int,char **),RIL_SOCKET_TYPE socketType,int argc,char ** argv)483 RIL_register_socket (RIL_RadioFunctions *(*Init)(const struct RIL_Env *, int, char **),
484         RIL_SOCKET_TYPE socketType, int argc, char **argv) {
485 
486     RIL_RadioFunctions* UimFuncs = NULL;
487 
488     if(Init) {
489         UimFuncs = Init(&RilSapSocket::uimRilEnv, argc, argv);
490 
491         switch(socketType) {
492             case RIL_SAP_SOCKET:
493                 RilSapSocket::initSapSocket(RIL1_SERVICE_NAME, UimFuncs);
494 
495 #if (SIM_COUNT >= 2)
496                 RilSapSocket::initSapSocket(RIL2_SERVICE_NAME, UimFuncs);
497 #endif
498 
499 #if (SIM_COUNT >= 3)
500                 RilSapSocket::initSapSocket(RIL3_SERVICE_NAME, UimFuncs);
501 #endif
502 
503 #if (SIM_COUNT >= 4)
504                 RilSapSocket::initSapSocket(RIL4_SERVICE_NAME, UimFuncs);
505 #endif
506                 break;
507             default:;
508         }
509 
510         RLOGI("RIL_register_socket: calling registerService");
511         sap::registerService(UimFuncs);
512     }
513 }
514 
515 // Check and remove RequestInfo if its a response and not just ack sent back
516 static int
checkAndDequeueRequestInfoIfAck(struct RequestInfo * pRI,bool isAck)517 checkAndDequeueRequestInfoIfAck(struct RequestInfo *pRI, bool isAck) {
518     int ret = 0;
519     /* Hook for current context
520        pendingRequestsMutextHook refer to &s_pendingRequestsMutex */
521     pthread_mutex_t* pendingRequestsMutexHook = &s_pendingRequestsMutex;
522     /* pendingRequestsHook refer to &s_pendingRequests */
523     RequestInfo ** pendingRequestsHook = &s_pendingRequests;
524 
525     if (pRI == NULL) {
526         return 0;
527     }
528 
529 #if (SIM_COUNT >= 2)
530     if (pRI->socket_id == RIL_SOCKET_2) {
531         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket2;
532         pendingRequestsHook = &s_pendingRequests_socket2;
533     }
534 #if (SIM_COUNT >= 3)
535         if (pRI->socket_id == RIL_SOCKET_3) {
536             pendingRequestsMutexHook = &s_pendingRequestsMutex_socket3;
537             pendingRequestsHook = &s_pendingRequests_socket3;
538         }
539 #endif
540 #if (SIM_COUNT >= 4)
541     if (pRI->socket_id == RIL_SOCKET_4) {
542         pendingRequestsMutexHook = &s_pendingRequestsMutex_socket4;
543         pendingRequestsHook = &s_pendingRequests_socket4;
544     }
545 #endif
546 #endif
547     pthread_mutex_lock(pendingRequestsMutexHook);
548 
549     for(RequestInfo **ppCur = pendingRequestsHook
550         ; *ppCur != NULL
551         ; ppCur = &((*ppCur)->p_next)
552     ) {
553         if (pRI == *ppCur) {
554             ret = 1;
555             if (isAck) { // Async ack
556                 if (pRI->wasAckSent == 1) {
557                     RLOGD("Ack was already sent for %s", requestToString(pRI->pCI->requestNumber));
558                 } else {
559                     pRI->wasAckSent = 1;
560                 }
561             } else {
562                 *ppCur = (*ppCur)->p_next;
563             }
564             break;
565         }
566     }
567 
568     pthread_mutex_unlock(pendingRequestsMutexHook);
569 
570     return ret;
571 }
572 
573 extern "C" void
RIL_onRequestAck(RIL_Token t)574 RIL_onRequestAck(RIL_Token t) {
575     RequestInfo *pRI;
576     int ret;
577 
578     size_t errorOffset;
579     RIL_SOCKET_ID socket_id = RIL_SOCKET_1;
580 
581     pRI = (RequestInfo *)t;
582 
583     if (!checkAndDequeueRequestInfoIfAck(pRI, true)) {
584         RLOGE ("RIL_onRequestAck: invalid RIL_Token");
585         return;
586     }
587 
588     socket_id = pRI->socket_id;
589 
590 #if VDBG
591     RLOGD("Request Ack, %s", rilSocketIdToString(socket_id));
592 #endif
593 
594     appendPrintBuf("Ack [%04d]< %s", pRI->token, requestToString(pRI->pCI->requestNumber));
595 
596     if (pRI->cancelled == 0) {
597         pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(
598                 (int) socket_id);
599         int rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
600         assert(rwlockRet == 0);
601 
602         radio::acknowledgeRequest((int) socket_id, pRI->token);
603 
604         rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
605         assert(rwlockRet == 0);
606     }
607 }
608 extern "C" void
RIL_onRequestComplete(RIL_Token t,RIL_Errno e,void * response,size_t responselen)609 RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen) {
610     RequestInfo *pRI;
611     int ret;
612     size_t errorOffset;
613     RIL_SOCKET_ID socket_id = RIL_SOCKET_1;
614 
615     pRI = (RequestInfo *)t;
616 
617     if (!checkAndDequeueRequestInfoIfAck(pRI, false)) {
618         RLOGE ("RIL_onRequestComplete: invalid RIL_Token");
619         return;
620     }
621 
622     socket_id = pRI->socket_id;
623 #if VDBG
624     RLOGD("RequestComplete, %s", rilSocketIdToString(socket_id));
625 #endif
626 
627     if (pRI->local > 0) {
628         // Locally issued command...void only!
629         // response does not go back up the command socket
630         RLOGD("C[locl]< %s", requestToString(pRI->pCI->requestNumber));
631 
632         free(pRI);
633         return;
634     }
635 
636     appendPrintBuf("[%04d]< %s",
637         pRI->token, requestToString(pRI->pCI->requestNumber));
638 
639     if (pRI->cancelled == 0) {
640         int responseType;
641         if (s_callbacks.version >= 13 && pRI->wasAckSent == 1) {
642             // If ack was already sent, then this call is an asynchronous response. So we need to
643             // send id indicating that we expect an ack from RIL.java as we acquire wakelock here.
644             responseType = RESPONSE_SOLICITED_ACK_EXP;
645             grabPartialWakeLock();
646         } else {
647             responseType = RESPONSE_SOLICITED;
648         }
649 
650         // there is a response payload, no matter success or not.
651 #if VDBG
652         RLOGE ("Calling responseFunction() for token %d", pRI->token);
653 #endif
654 
655         pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock((int) socket_id);
656         int rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
657         assert(rwlockRet == 0);
658 
659         ret = pRI->pCI->responseFunction((int) socket_id,
660                 responseType, pRI->token, e, response, responselen);
661 
662         rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
663         assert(rwlockRet == 0);
664     }
665     free(pRI);
666 }
667 
668 static void
grabPartialWakeLock()669 grabPartialWakeLock() {
670     if (s_callbacks.version >= 13) {
671         int ret;
672         ret = pthread_mutex_lock(&s_wakeLockCountMutex);
673         assert(ret == 0);
674         acquire_wake_lock(PARTIAL_WAKE_LOCK, ANDROID_WAKE_LOCK_NAME);
675 
676         UserCallbackInfo *p_info =
677                 internalRequestTimedCallback(wakeTimeoutCallback, NULL, &TIMEVAL_WAKE_TIMEOUT);
678         if (p_info == NULL) {
679             release_wake_lock(ANDROID_WAKE_LOCK_NAME);
680         } else {
681             s_wakelock_count++;
682             if (s_last_wake_timeout_info != NULL) {
683                 s_last_wake_timeout_info->userParam = (void *)1;
684             }
685             s_last_wake_timeout_info = p_info;
686         }
687         ret = pthread_mutex_unlock(&s_wakeLockCountMutex);
688         assert(ret == 0);
689     } else {
690         acquire_wake_lock(PARTIAL_WAKE_LOCK, ANDROID_WAKE_LOCK_NAME);
691     }
692 }
693 
694 void
releaseWakeLock()695 releaseWakeLock() {
696     if (s_callbacks.version >= 13) {
697         int ret;
698         ret = pthread_mutex_lock(&s_wakeLockCountMutex);
699         assert(ret == 0);
700 
701         if (s_wakelock_count > 1) {
702             s_wakelock_count--;
703         } else {
704             s_wakelock_count = 0;
705             release_wake_lock(ANDROID_WAKE_LOCK_NAME);
706             if (s_last_wake_timeout_info != NULL) {
707                 s_last_wake_timeout_info->userParam = (void *)1;
708             }
709         }
710 
711         ret = pthread_mutex_unlock(&s_wakeLockCountMutex);
712         assert(ret == 0);
713     } else {
714         release_wake_lock(ANDROID_WAKE_LOCK_NAME);
715     }
716 }
717 
718 /**
719  * Timer callback to put us back to sleep before the default timeout
720  */
721 static void
wakeTimeoutCallback(void * param)722 wakeTimeoutCallback (void *param) {
723     // We're using "param != NULL" as a cancellation mechanism
724     if (s_callbacks.version >= 13) {
725         if (param == NULL) {
726             int ret;
727             ret = pthread_mutex_lock(&s_wakeLockCountMutex);
728             assert(ret == 0);
729             s_wakelock_count = 0;
730             release_wake_lock(ANDROID_WAKE_LOCK_NAME);
731             ret = pthread_mutex_unlock(&s_wakeLockCountMutex);
732             assert(ret == 0);
733         }
734     } else {
735         if (param == NULL) {
736             releaseWakeLock();
737         }
738     }
739 }
740 
741 #if defined(ANDROID_MULTI_SIM)
742 extern "C"
RIL_onUnsolicitedResponse(int unsolResponse,const void * data,size_t datalen,RIL_SOCKET_ID socket_id)743 void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
744                                 size_t datalen, RIL_SOCKET_ID socket_id)
745 #else
746 extern "C"
747 void RIL_onUnsolicitedResponse(int unsolResponse, const void *data,
748                                 size_t datalen)
749 #endif
750 {
751     int unsolResponseIndex;
752     int ret;
753     bool shouldScheduleTimeout = false;
754     RIL_SOCKET_ID soc_id = RIL_SOCKET_1;
755 
756 #if defined(ANDROID_MULTI_SIM)
757     soc_id = socket_id;
758 #endif
759 
760 
761     if (s_registerCalled == 0) {
762         // Ignore RIL_onUnsolicitedResponse before RIL_register
763         RLOGW("RIL_onUnsolicitedResponse called before RIL_register");
764         return;
765     }
766 
767     unsolResponseIndex = unsolResponse - RIL_UNSOL_RESPONSE_BASE;
768 
769     if ((unsolResponseIndex < 0)
770         || (unsolResponseIndex >= (int32_t)NUM_ELEMS(s_unsolResponses))) {
771         RLOGE("unsupported unsolicited response code %d", unsolResponse);
772         return;
773     }
774 
775     // Grab a wake lock if needed for this reponse,
776     // as we exit we'll either release it immediately
777     // or set a timer to release it later.
778     switch (s_unsolResponses[unsolResponseIndex].wakeType) {
779         case WAKE_PARTIAL:
780             grabPartialWakeLock();
781             shouldScheduleTimeout = true;
782         break;
783 
784         case DONT_WAKE:
785         default:
786             // No wake lock is grabed so don't set timeout
787             shouldScheduleTimeout = false;
788             break;
789     }
790 
791     appendPrintBuf("[UNSL]< %s", requestToString(unsolResponse));
792 
793     int responseType;
794     if (s_callbacks.version >= 13
795                 && s_unsolResponses[unsolResponseIndex].wakeType == WAKE_PARTIAL) {
796         responseType = RESPONSE_UNSOLICITED_ACK_EXP;
797     } else {
798         responseType = RESPONSE_UNSOLICITED;
799     }
800 
801     pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock((int) soc_id);
802     int rwlockRet = pthread_rwlock_rdlock(radioServiceRwlockPtr);
803     assert(rwlockRet == 0);
804 
805     ret = s_unsolResponses[unsolResponseIndex].responseFunction(
806             (int) soc_id, responseType, 0, RIL_E_SUCCESS, const_cast<void*>(data),
807             datalen);
808 
809     rwlockRet = pthread_rwlock_unlock(radioServiceRwlockPtr);
810     assert(rwlockRet == 0);
811 
812     if (s_callbacks.version < 13) {
813         if (shouldScheduleTimeout) {
814             UserCallbackInfo *p_info = internalRequestTimedCallback(wakeTimeoutCallback, NULL,
815                     &TIMEVAL_WAKE_TIMEOUT);
816 
817             if (p_info == NULL) {
818                 goto error_exit;
819             } else {
820                 // Cancel the previous request
821                 if (s_last_wake_timeout_info != NULL) {
822                     s_last_wake_timeout_info->userParam = (void *)1;
823                 }
824                 s_last_wake_timeout_info = p_info;
825             }
826         }
827     }
828 
829 #if VDBG
830     RLOGI("%s UNSOLICITED: %s length:%d", rilSocketIdToString(soc_id),
831             requestToString(unsolResponse), datalen);
832 #endif
833 
834     if (ret != 0 && unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
835         // Unfortunately, NITZ time is not poll/update like everything
836         // else in the system. So, if the upstream client isn't connected,
837         // keep a copy of the last NITZ response (with receive time noted
838         // above) around so we can deliver it when it is connected
839 
840         if (s_lastNITZTimeData != NULL) {
841             free(s_lastNITZTimeData);
842             s_lastNITZTimeData = NULL;
843         }
844 
845         s_lastNITZTimeData = calloc(datalen, 1);
846         if (s_lastNITZTimeData == NULL) {
847             RLOGE("Memory allocation failed in RIL_onUnsolicitedResponse");
848             goto error_exit;
849         }
850         s_lastNITZTimeDataSize = datalen;
851         memcpy(s_lastNITZTimeData, data, datalen);
852     }
853 
854     // Normal exit
855     return;
856 
857 error_exit:
858     if (shouldScheduleTimeout) {
859         releaseWakeLock();
860     }
861 }
862 
863 /** FIXME generalize this if you track UserCAllbackInfo, clear it
864     when the callback occurs
865 */
866 static UserCallbackInfo *
internalRequestTimedCallback(RIL_TimedCallback callback,void * param,const struct timeval * relativeTime)867 internalRequestTimedCallback (RIL_TimedCallback callback, void *param,
868                                 const struct timeval *relativeTime)
869 {
870     struct timeval myRelativeTime;
871     UserCallbackInfo *p_info;
872 
873     p_info = (UserCallbackInfo *) calloc(1, sizeof(UserCallbackInfo));
874     if (p_info == NULL) {
875         RLOGE("Memory allocation failed in internalRequestTimedCallback");
876         return p_info;
877 
878     }
879 
880     p_info->p_callback = callback;
881     p_info->userParam = param;
882 
883     if (relativeTime == NULL) {
884         /* treat null parameter as a 0 relative time */
885         memset (&myRelativeTime, 0, sizeof(myRelativeTime));
886     } else {
887         /* FIXME I think event_add's tv param is really const anyway */
888         memcpy (&myRelativeTime, relativeTime, sizeof(myRelativeTime));
889     }
890 
891     ril_event_set(&(p_info->event), -1, false, userTimerCallback, p_info);
892 
893     ril_timer_add(&(p_info->event), &myRelativeTime);
894 
895     triggerEvLoop();
896     return p_info;
897 }
898 
899 
900 extern "C" void
RIL_requestTimedCallback(RIL_TimedCallback callback,void * param,const struct timeval * relativeTime)901 RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
902                                 const struct timeval *relativeTime) {
903     internalRequestTimedCallback (callback, param, relativeTime);
904 }
905 
906 const char *
failCauseToString(RIL_Errno e)907 failCauseToString(RIL_Errno e) {
908     switch(e) {
909         case RIL_E_SUCCESS: return "E_SUCCESS";
910         case RIL_E_RADIO_NOT_AVAILABLE: return "E_RADIO_NOT_AVAILABLE";
911         case RIL_E_GENERIC_FAILURE: return "E_GENERIC_FAILURE";
912         case RIL_E_PASSWORD_INCORRECT: return "E_PASSWORD_INCORRECT";
913         case RIL_E_SIM_PIN2: return "E_SIM_PIN2";
914         case RIL_E_SIM_PUK2: return "E_SIM_PUK2";
915         case RIL_E_REQUEST_NOT_SUPPORTED: return "E_REQUEST_NOT_SUPPORTED";
916         case RIL_E_CANCELLED: return "E_CANCELLED";
917         case RIL_E_OP_NOT_ALLOWED_DURING_VOICE_CALL: return "E_OP_NOT_ALLOWED_DURING_VOICE_CALL";
918         case RIL_E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW: return "E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW";
919         case RIL_E_SMS_SEND_FAIL_RETRY: return "E_SMS_SEND_FAIL_RETRY";
920         case RIL_E_SIM_ABSENT:return "E_SIM_ABSENT";
921         case RIL_E_ILLEGAL_SIM_OR_ME:return "E_ILLEGAL_SIM_OR_ME";
922 #ifdef FEATURE_MULTIMODE_ANDROID
923         case RIL_E_SUBSCRIPTION_NOT_AVAILABLE:return "E_SUBSCRIPTION_NOT_AVAILABLE";
924         case RIL_E_MODE_NOT_SUPPORTED:return "E_MODE_NOT_SUPPORTED";
925 #endif
926         case RIL_E_FDN_CHECK_FAILURE: return "E_FDN_CHECK_FAILURE";
927         case RIL_E_MISSING_RESOURCE: return "E_MISSING_RESOURCE";
928         case RIL_E_NO_SUCH_ELEMENT: return "E_NO_SUCH_ELEMENT";
929         case RIL_E_DIAL_MODIFIED_TO_USSD: return "E_DIAL_MODIFIED_TO_USSD";
930         case RIL_E_DIAL_MODIFIED_TO_SS: return "E_DIAL_MODIFIED_TO_SS";
931         case RIL_E_DIAL_MODIFIED_TO_DIAL: return "E_DIAL_MODIFIED_TO_DIAL";
932         case RIL_E_USSD_MODIFIED_TO_DIAL: return "E_USSD_MODIFIED_TO_DIAL";
933         case RIL_E_USSD_MODIFIED_TO_SS: return "E_USSD_MODIFIED_TO_SS";
934         case RIL_E_USSD_MODIFIED_TO_USSD: return "E_USSD_MODIFIED_TO_USSD";
935         case RIL_E_SS_MODIFIED_TO_DIAL: return "E_SS_MODIFIED_TO_DIAL";
936         case RIL_E_SS_MODIFIED_TO_USSD: return "E_SS_MODIFIED_TO_USSD";
937         case RIL_E_SUBSCRIPTION_NOT_SUPPORTED: return "E_SUBSCRIPTION_NOT_SUPPORTED";
938         case RIL_E_SS_MODIFIED_TO_SS: return "E_SS_MODIFIED_TO_SS";
939         case RIL_E_LCE_NOT_SUPPORTED: return "E_LCE_NOT_SUPPORTED";
940         case RIL_E_NO_MEMORY: return "E_NO_MEMORY";
941         case RIL_E_INTERNAL_ERR: return "E_INTERNAL_ERR";
942         case RIL_E_SYSTEM_ERR: return "E_SYSTEM_ERR";
943         case RIL_E_MODEM_ERR: return "E_MODEM_ERR";
944         case RIL_E_INVALID_STATE: return "E_INVALID_STATE";
945         case RIL_E_NO_RESOURCES: return "E_NO_RESOURCES";
946         case RIL_E_SIM_ERR: return "E_SIM_ERR";
947         case RIL_E_INVALID_ARGUMENTS: return "E_INVALID_ARGUMENTS";
948         case RIL_E_INVALID_SIM_STATE: return "E_INVALID_SIM_STATE";
949         case RIL_E_INVALID_MODEM_STATE: return "E_INVALID_MODEM_STATE";
950         case RIL_E_INVALID_CALL_ID: return "E_INVALID_CALL_ID";
951         case RIL_E_NO_SMS_TO_ACK: return "E_NO_SMS_TO_ACK";
952         case RIL_E_NETWORK_ERR: return "E_NETWORK_ERR";
953         case RIL_E_REQUEST_RATE_LIMITED: return "E_REQUEST_RATE_LIMITED";
954         case RIL_E_SIM_BUSY: return "E_SIM_BUSY";
955         case RIL_E_SIM_FULL: return "E_SIM_FULL";
956         case RIL_E_NETWORK_REJECT: return "E_NETWORK_REJECT";
957         case RIL_E_OPERATION_NOT_ALLOWED: return "E_OPERATION_NOT_ALLOWED";
958         case RIL_E_EMPTY_RECORD: "E_EMPTY_RECORD";
959         case RIL_E_INVALID_SMS_FORMAT: return "E_INVALID_SMS_FORMAT";
960         case RIL_E_ENCODING_ERR: return "E_ENCODING_ERR";
961         case RIL_E_INVALID_SMSC_ADDRESS: return "E_INVALID_SMSC_ADDRESS";
962         case RIL_E_NO_SUCH_ENTRY: return "E_NO_SUCH_ENTRY";
963         case RIL_E_NETWORK_NOT_READY: return "E_NETWORK_NOT_READY";
964         case RIL_E_NOT_PROVISIONED: return "E_NOT_PROVISIONED";
965         case RIL_E_NO_SUBSCRIPTION: return "E_NO_SUBSCRIPTION";
966         case RIL_E_NO_NETWORK_FOUND: return "E_NO_NETWORK_FOUND";
967         case RIL_E_DEVICE_IN_USE: return "E_DEVICE_IN_USE";
968         case RIL_E_ABORTED: return "E_ABORTED";
969         case RIL_E_INVALID_RESPONSE: return "INVALID_RESPONSE";
970         case RIL_E_OEM_ERROR_1: return "E_OEM_ERROR_1";
971         case RIL_E_OEM_ERROR_2: return "E_OEM_ERROR_2";
972         case RIL_E_OEM_ERROR_3: return "E_OEM_ERROR_3";
973         case RIL_E_OEM_ERROR_4: return "E_OEM_ERROR_4";
974         case RIL_E_OEM_ERROR_5: return "E_OEM_ERROR_5";
975         case RIL_E_OEM_ERROR_6: return "E_OEM_ERROR_6";
976         case RIL_E_OEM_ERROR_7: return "E_OEM_ERROR_7";
977         case RIL_E_OEM_ERROR_8: return "E_OEM_ERROR_8";
978         case RIL_E_OEM_ERROR_9: return "E_OEM_ERROR_9";
979         case RIL_E_OEM_ERROR_10: return "E_OEM_ERROR_10";
980         case RIL_E_OEM_ERROR_11: return "E_OEM_ERROR_11";
981         case RIL_E_OEM_ERROR_12: return "E_OEM_ERROR_12";
982         case RIL_E_OEM_ERROR_13: return "E_OEM_ERROR_13";
983         case RIL_E_OEM_ERROR_14: return "E_OEM_ERROR_14";
984         case RIL_E_OEM_ERROR_15: return "E_OEM_ERROR_15";
985         case RIL_E_OEM_ERROR_16: return "E_OEM_ERROR_16";
986         case RIL_E_OEM_ERROR_17: return "E_OEM_ERROR_17";
987         case RIL_E_OEM_ERROR_18: return "E_OEM_ERROR_18";
988         case RIL_E_OEM_ERROR_19: return "E_OEM_ERROR_19";
989         case RIL_E_OEM_ERROR_20: return "E_OEM_ERROR_20";
990         case RIL_E_OEM_ERROR_21: return "E_OEM_ERROR_21";
991         case RIL_E_OEM_ERROR_22: return "E_OEM_ERROR_22";
992         case RIL_E_OEM_ERROR_23: return "E_OEM_ERROR_23";
993         case RIL_E_OEM_ERROR_24: return "E_OEM_ERROR_24";
994         case RIL_E_OEM_ERROR_25: return "E_OEM_ERROR_25";
995         default: return "<unknown error>";
996     }
997 }
998 
999 const char *
radioStateToString(RIL_RadioState s)1000 radioStateToString(RIL_RadioState s) {
1001     switch(s) {
1002         case RADIO_STATE_OFF: return "RADIO_OFF";
1003         case RADIO_STATE_UNAVAILABLE: return "RADIO_UNAVAILABLE";
1004         case RADIO_STATE_ON:return"RADIO_ON";
1005         default: return "<unknown state>";
1006     }
1007 }
1008 
1009 const char *
callStateToString(RIL_CallState s)1010 callStateToString(RIL_CallState s) {
1011     switch(s) {
1012         case RIL_CALL_ACTIVE : return "ACTIVE";
1013         case RIL_CALL_HOLDING: return "HOLDING";
1014         case RIL_CALL_DIALING: return "DIALING";
1015         case RIL_CALL_ALERTING: return "ALERTING";
1016         case RIL_CALL_INCOMING: return "INCOMING";
1017         case RIL_CALL_WAITING: return "WAITING";
1018         default: return "<unknown state>";
1019     }
1020 }
1021 
1022 const char *
requestToString(int request)1023 requestToString(int request) {
1024 /*
1025  cat libs/telephony/ril_commands.h \
1026  | egrep "^ *{RIL_" \
1027  | sed -re 's/\{RIL_([^,]+),[^,]+,([^}]+).+/case RIL_\1: return "\1";/'
1028 
1029 
1030  cat libs/telephony/ril_unsol_commands.h \
1031  | egrep "^ *{RIL_" \
1032  | sed -re 's/\{RIL_([^,]+),([^}]+).+/case RIL_\1: return "\1";/'
1033 
1034 */
1035     switch(request) {
1036         case RIL_REQUEST_GET_SIM_STATUS: return "GET_SIM_STATUS";
1037         case RIL_REQUEST_ENTER_SIM_PIN: return "ENTER_SIM_PIN";
1038         case RIL_REQUEST_ENTER_SIM_PUK: return "ENTER_SIM_PUK";
1039         case RIL_REQUEST_ENTER_SIM_PIN2: return "ENTER_SIM_PIN2";
1040         case RIL_REQUEST_ENTER_SIM_PUK2: return "ENTER_SIM_PUK2";
1041         case RIL_REQUEST_CHANGE_SIM_PIN: return "CHANGE_SIM_PIN";
1042         case RIL_REQUEST_CHANGE_SIM_PIN2: return "CHANGE_SIM_PIN2";
1043         case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION: return "ENTER_NETWORK_DEPERSONALIZATION";
1044         case RIL_REQUEST_GET_CURRENT_CALLS: return "GET_CURRENT_CALLS";
1045         case RIL_REQUEST_DIAL: return "DIAL";
1046         case RIL_REQUEST_GET_IMSI: return "GET_IMSI";
1047         case RIL_REQUEST_HANGUP: return "HANGUP";
1048         case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND: return "HANGUP_WAITING_OR_BACKGROUND";
1049         case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND: return "HANGUP_FOREGROUND_RESUME_BACKGROUND";
1050         case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE: return "SWITCH_WAITING_OR_HOLDING_AND_ACTIVE";
1051         case RIL_REQUEST_CONFERENCE: return "CONFERENCE";
1052         case RIL_REQUEST_UDUB: return "UDUB";
1053         case RIL_REQUEST_LAST_CALL_FAIL_CAUSE: return "LAST_CALL_FAIL_CAUSE";
1054         case RIL_REQUEST_SIGNAL_STRENGTH: return "SIGNAL_STRENGTH";
1055         case RIL_REQUEST_VOICE_REGISTRATION_STATE: return "VOICE_REGISTRATION_STATE";
1056         case RIL_REQUEST_DATA_REGISTRATION_STATE: return "DATA_REGISTRATION_STATE";
1057         case RIL_REQUEST_OPERATOR: return "OPERATOR";
1058         case RIL_REQUEST_RADIO_POWER: return "RADIO_POWER";
1059         case RIL_REQUEST_DTMF: return "DTMF";
1060         case RIL_REQUEST_SEND_SMS: return "SEND_SMS";
1061         case RIL_REQUEST_SEND_SMS_EXPECT_MORE: return "SEND_SMS_EXPECT_MORE";
1062         case RIL_REQUEST_SETUP_DATA_CALL: return "SETUP_DATA_CALL";
1063         case RIL_REQUEST_SIM_IO: return "SIM_IO";
1064         case RIL_REQUEST_SEND_USSD: return "SEND_USSD";
1065         case RIL_REQUEST_CANCEL_USSD: return "CANCEL_USSD";
1066         case RIL_REQUEST_GET_CLIR: return "GET_CLIR";
1067         case RIL_REQUEST_SET_CLIR: return "SET_CLIR";
1068         case RIL_REQUEST_QUERY_CALL_FORWARD_STATUS: return "QUERY_CALL_FORWARD_STATUS";
1069         case RIL_REQUEST_SET_CALL_FORWARD: return "SET_CALL_FORWARD";
1070         case RIL_REQUEST_QUERY_CALL_WAITING: return "QUERY_CALL_WAITING";
1071         case RIL_REQUEST_SET_CALL_WAITING: return "SET_CALL_WAITING";
1072         case RIL_REQUEST_SMS_ACKNOWLEDGE: return "SMS_ACKNOWLEDGE";
1073         case RIL_REQUEST_GET_IMEI: return "GET_IMEI";
1074         case RIL_REQUEST_GET_IMEISV: return "GET_IMEISV";
1075         case RIL_REQUEST_ANSWER: return "ANSWER";
1076         case RIL_REQUEST_DEACTIVATE_DATA_CALL: return "DEACTIVATE_DATA_CALL";
1077         case RIL_REQUEST_QUERY_FACILITY_LOCK: return "QUERY_FACILITY_LOCK";
1078         case RIL_REQUEST_SET_FACILITY_LOCK: return "SET_FACILITY_LOCK";
1079         case RIL_REQUEST_CHANGE_BARRING_PASSWORD: return "CHANGE_BARRING_PASSWORD";
1080         case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE: return "QUERY_NETWORK_SELECTION_MODE";
1081         case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC: return "SET_NETWORK_SELECTION_AUTOMATIC";
1082         case RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL: return "SET_NETWORK_SELECTION_MANUAL";
1083         case RIL_REQUEST_QUERY_AVAILABLE_NETWORKS: return "QUERY_AVAILABLE_NETWORKS";
1084         case RIL_REQUEST_DTMF_START: return "DTMF_START";
1085         case RIL_REQUEST_DTMF_STOP: return "DTMF_STOP";
1086         case RIL_REQUEST_BASEBAND_VERSION: return "BASEBAND_VERSION";
1087         case RIL_REQUEST_SEPARATE_CONNECTION: return "SEPARATE_CONNECTION";
1088         case RIL_REQUEST_SET_MUTE: return "SET_MUTE";
1089         case RIL_REQUEST_GET_MUTE: return "GET_MUTE";
1090         case RIL_REQUEST_QUERY_CLIP: return "QUERY_CLIP";
1091         case RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE: return "LAST_DATA_CALL_FAIL_CAUSE";
1092         case RIL_REQUEST_DATA_CALL_LIST: return "DATA_CALL_LIST";
1093         case RIL_REQUEST_RESET_RADIO: return "RESET_RADIO";
1094         case RIL_REQUEST_OEM_HOOK_RAW: return "OEM_HOOK_RAW";
1095         case RIL_REQUEST_OEM_HOOK_STRINGS: return "OEM_HOOK_STRINGS";
1096         case RIL_REQUEST_SCREEN_STATE: return "SCREEN_STATE";
1097         case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION: return "SET_SUPP_SVC_NOTIFICATION";
1098         case RIL_REQUEST_WRITE_SMS_TO_SIM: return "WRITE_SMS_TO_SIM";
1099         case RIL_REQUEST_DELETE_SMS_ON_SIM: return "DELETE_SMS_ON_SIM";
1100         case RIL_REQUEST_SET_BAND_MODE: return "SET_BAND_MODE";
1101         case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE: return "QUERY_AVAILABLE_BAND_MODE";
1102         case RIL_REQUEST_STK_GET_PROFILE: return "STK_GET_PROFILE";
1103         case RIL_REQUEST_STK_SET_PROFILE: return "STK_SET_PROFILE";
1104         case RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND: return "STK_SEND_ENVELOPE_COMMAND";
1105         case RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE: return "STK_SEND_TERMINAL_RESPONSE";
1106         case RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM: return "STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM";
1107         case RIL_REQUEST_EXPLICIT_CALL_TRANSFER: return "EXPLICIT_CALL_TRANSFER";
1108         case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE: return "SET_PREFERRED_NETWORK_TYPE";
1109         case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE: return "GET_PREFERRED_NETWORK_TYPE";
1110         case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS: return "GET_NEIGHBORING_CELL_IDS";
1111         case RIL_REQUEST_SET_LOCATION_UPDATES: return "SET_LOCATION_UPDATES";
1112         case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE: return "CDMA_SET_SUBSCRIPTION_SOURCE";
1113         case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE: return "CDMA_SET_ROAMING_PREFERENCE";
1114         case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE: return "CDMA_QUERY_ROAMING_PREFERENCE";
1115         case RIL_REQUEST_SET_TTY_MODE: return "SET_TTY_MODE";
1116         case RIL_REQUEST_QUERY_TTY_MODE: return "QUERY_TTY_MODE";
1117         case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE: return "CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE";
1118         case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE: return "CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE";
1119         case RIL_REQUEST_CDMA_FLASH: return "CDMA_FLASH";
1120         case RIL_REQUEST_CDMA_BURST_DTMF: return "CDMA_BURST_DTMF";
1121         case RIL_REQUEST_CDMA_VALIDATE_AND_WRITE_AKEY: return "CDMA_VALIDATE_AND_WRITE_AKEY";
1122         case RIL_REQUEST_CDMA_SEND_SMS: return "CDMA_SEND_SMS";
1123         case RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE: return "CDMA_SMS_ACKNOWLEDGE";
1124         case RIL_REQUEST_GSM_GET_BROADCAST_SMS_CONFIG: return "GSM_GET_BROADCAST_SMS_CONFIG";
1125         case RIL_REQUEST_GSM_SET_BROADCAST_SMS_CONFIG: return "GSM_SET_BROADCAST_SMS_CONFIG";
1126         case RIL_REQUEST_GSM_SMS_BROADCAST_ACTIVATION: return "GSM_SMS_BROADCAST_ACTIVATION";
1127         case RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG: return "CDMA_GET_BROADCAST_SMS_CONFIG";
1128         case RIL_REQUEST_CDMA_SET_BROADCAST_SMS_CONFIG: return "CDMA_SET_BROADCAST_SMS_CONFIG";
1129         case RIL_REQUEST_CDMA_SMS_BROADCAST_ACTIVATION: return "CDMA_SMS_BROADCAST_ACTIVATION";
1130         case RIL_REQUEST_CDMA_SUBSCRIPTION: return "CDMA_SUBSCRIPTION";
1131         case RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM: return "CDMA_WRITE_SMS_TO_RUIM";
1132         case RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM: return "CDMA_DELETE_SMS_ON_RUIM";
1133         case RIL_REQUEST_DEVICE_IDENTITY: return "DEVICE_IDENTITY";
1134         case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE: return "EXIT_EMERGENCY_CALLBACK_MODE";
1135         case RIL_REQUEST_GET_SMSC_ADDRESS: return "GET_SMSC_ADDRESS";
1136         case RIL_REQUEST_SET_SMSC_ADDRESS: return "SET_SMSC_ADDRESS";
1137         case RIL_REQUEST_REPORT_SMS_MEMORY_STATUS: return "REPORT_SMS_MEMORY_STATUS";
1138         case RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING: return "REPORT_STK_SERVICE_IS_RUNNING";
1139         case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE: return "CDMA_GET_SUBSCRIPTION_SOURCE";
1140         case RIL_REQUEST_ISIM_AUTHENTICATION: return "ISIM_AUTHENTICATION";
1141         case RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU: return "ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU";
1142         case RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS: return "STK_SEND_ENVELOPE_WITH_STATUS";
1143         case RIL_REQUEST_VOICE_RADIO_TECH: return "VOICE_RADIO_TECH";
1144         case RIL_REQUEST_GET_CELL_INFO_LIST: return "GET_CELL_INFO_LIST";
1145         case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE: return "SET_UNSOL_CELL_INFO_LIST_RATE";
1146         case RIL_REQUEST_SET_INITIAL_ATTACH_APN: return "SET_INITIAL_ATTACH_APN";
1147         case RIL_REQUEST_IMS_REGISTRATION_STATE: return "IMS_REGISTRATION_STATE";
1148         case RIL_REQUEST_IMS_SEND_SMS: return "IMS_SEND_SMS";
1149         case RIL_REQUEST_SIM_TRANSMIT_APDU_BASIC: return "SIM_TRANSMIT_APDU_BASIC";
1150         case RIL_REQUEST_SIM_OPEN_CHANNEL: return "SIM_OPEN_CHANNEL";
1151         case RIL_REQUEST_SIM_CLOSE_CHANNEL: return "SIM_CLOSE_CHANNEL";
1152         case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL: return "SIM_TRANSMIT_APDU_CHANNEL";
1153         case RIL_REQUEST_NV_READ_ITEM: return "NV_READ_ITEM";
1154         case RIL_REQUEST_NV_WRITE_ITEM: return "NV_WRITE_ITEM";
1155         case RIL_REQUEST_NV_WRITE_CDMA_PRL: return "NV_WRITE_CDMA_PRL";
1156         case RIL_REQUEST_NV_RESET_CONFIG: return "NV_RESET_CONFIG";
1157         case RIL_REQUEST_SET_UICC_SUBSCRIPTION: return "SET_UICC_SUBSCRIPTION";
1158         case RIL_REQUEST_ALLOW_DATA: return "ALLOW_DATA";
1159         case RIL_REQUEST_GET_HARDWARE_CONFIG: return "GET_HARDWARE_CONFIG";
1160         case RIL_REQUEST_SIM_AUTHENTICATION: return "SIM_AUTHENTICATION";
1161         case RIL_REQUEST_GET_DC_RT_INFO: return "GET_DC_RT_INFO";
1162         case RIL_REQUEST_SET_DC_RT_INFO_RATE: return "SET_DC_RT_INFO_RATE";
1163         case RIL_REQUEST_SET_DATA_PROFILE: return "SET_DATA_PROFILE";
1164         case RIL_REQUEST_SHUTDOWN: return "SHUTDOWN";
1165         case RIL_REQUEST_GET_RADIO_CAPABILITY: return "GET_RADIO_CAPABILITY";
1166         case RIL_REQUEST_SET_RADIO_CAPABILITY: return "SET_RADIO_CAPABILITY";
1167         case RIL_REQUEST_START_LCE: return "START_LCE";
1168         case RIL_REQUEST_STOP_LCE: return "STOP_LCE";
1169         case RIL_REQUEST_PULL_LCEDATA: return "PULL_LCEDATA";
1170         case RIL_REQUEST_GET_ACTIVITY_INFO: return "GET_ACTIVITY_INFO";
1171         case RIL_REQUEST_SET_CARRIER_RESTRICTIONS: return "SET_CARRIER_RESTRICTIONS";
1172         case RIL_REQUEST_GET_CARRIER_RESTRICTIONS: return "GET_CARRIER_RESTRICTIONS";
1173         case RIL_RESPONSE_ACKNOWLEDGEMENT: return "RESPONSE_ACKNOWLEDGEMENT";
1174         case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: return "UNSOL_RESPONSE_RADIO_STATE_CHANGED";
1175         case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: return "UNSOL_RESPONSE_CALL_STATE_CHANGED";
1176         case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED: return "UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED";
1177         case RIL_UNSOL_RESPONSE_NEW_SMS: return "UNSOL_RESPONSE_NEW_SMS";
1178         case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT: return "UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT";
1179         case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM: return "UNSOL_RESPONSE_NEW_SMS_ON_SIM";
1180         case RIL_UNSOL_ON_USSD: return "UNSOL_ON_USSD";
1181         case RIL_UNSOL_ON_USSD_REQUEST: return "UNSOL_ON_USSD_REQUEST";
1182         case RIL_UNSOL_NITZ_TIME_RECEIVED: return "UNSOL_NITZ_TIME_RECEIVED";
1183         case RIL_UNSOL_SIGNAL_STRENGTH: return "UNSOL_SIGNAL_STRENGTH";
1184         case RIL_UNSOL_DATA_CALL_LIST_CHANGED: return "UNSOL_DATA_CALL_LIST_CHANGED";
1185         case RIL_UNSOL_SUPP_SVC_NOTIFICATION: return "UNSOL_SUPP_SVC_NOTIFICATION";
1186         case RIL_UNSOL_STK_SESSION_END: return "UNSOL_STK_SESSION_END";
1187         case RIL_UNSOL_STK_PROACTIVE_COMMAND: return "UNSOL_STK_PROACTIVE_COMMAND";
1188         case RIL_UNSOL_STK_EVENT_NOTIFY: return "UNSOL_STK_EVENT_NOTIFY";
1189         case RIL_UNSOL_STK_CALL_SETUP: return "UNSOL_STK_CALL_SETUP";
1190         case RIL_UNSOL_SIM_SMS_STORAGE_FULL: return "UNSOL_SIM_SMS_STORAGE_FULL";
1191         case RIL_UNSOL_SIM_REFRESH: return "UNSOL_SIM_REFRESH";
1192         case RIL_UNSOL_CALL_RING: return "UNSOL_CALL_RING";
1193         case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: return "UNSOL_RESPONSE_SIM_STATUS_CHANGED";
1194         case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS: return "UNSOL_RESPONSE_CDMA_NEW_SMS";
1195         case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: return "UNSOL_RESPONSE_NEW_BROADCAST_SMS";
1196         case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL: return "UNSOL_CDMA_RUIM_SMS_STORAGE_FULL";
1197         case RIL_UNSOL_RESTRICTED_STATE_CHANGED: return "UNSOL_RESTRICTED_STATE_CHANGED";
1198         case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE: return "UNSOL_ENTER_EMERGENCY_CALLBACK_MODE";
1199         case RIL_UNSOL_CDMA_CALL_WAITING: return "UNSOL_CDMA_CALL_WAITING";
1200         case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS: return "UNSOL_CDMA_OTA_PROVISION_STATUS";
1201         case RIL_UNSOL_CDMA_INFO_REC: return "UNSOL_CDMA_INFO_REC";
1202         case RIL_UNSOL_OEM_HOOK_RAW: return "UNSOL_OEM_HOOK_RAW";
1203         case RIL_UNSOL_RINGBACK_TONE: return "UNSOL_RINGBACK_TONE";
1204         case RIL_UNSOL_RESEND_INCALL_MUTE: return "UNSOL_RESEND_INCALL_MUTE";
1205         case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED: return "UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED";
1206         case RIL_UNSOL_CDMA_PRL_CHANGED: return "UNSOL_CDMA_PRL_CHANGED";
1207         case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE: return "UNSOL_EXIT_EMERGENCY_CALLBACK_MODE";
1208         case RIL_UNSOL_RIL_CONNECTED: return "UNSOL_RIL_CONNECTED";
1209         case RIL_UNSOL_VOICE_RADIO_TECH_CHANGED: return "UNSOL_VOICE_RADIO_TECH_CHANGED";
1210         case RIL_UNSOL_CELL_INFO_LIST: return "UNSOL_CELL_INFO_LIST";
1211         case RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED: return "UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED";
1212         case RIL_UNSOL_UICC_SUBSCRIPTION_STATUS_CHANGED: return "UNSOL_UICC_SUBSCRIPTION_STATUS_CHANGED";
1213         case RIL_UNSOL_SRVCC_STATE_NOTIFY: return "UNSOL_SRVCC_STATE_NOTIFY";
1214         case RIL_UNSOL_HARDWARE_CONFIG_CHANGED: return "UNSOL_HARDWARE_CONFIG_CHANGED";
1215         case RIL_UNSOL_DC_RT_INFO_CHANGED: return "UNSOL_DC_RT_INFO_CHANGED";
1216         case RIL_UNSOL_RADIO_CAPABILITY: return "UNSOL_RADIO_CAPABILITY";
1217         case RIL_UNSOL_MODEM_RESTART: return "UNSOL_MODEM_RESTART";
1218         case RIL_UNSOL_ON_SS: return "UNSOL_ON_SS";
1219         case RIL_UNSOL_STK_CC_ALPHA_NOTIFY: return "UNSOL_STK_CC_ALPHA_NOTIFY";
1220         case RIL_UNSOL_LCEDATA_RECV: return "UNSOL_LCEDATA_RECV";
1221         case RIL_UNSOL_PCO_DATA: return "UNSOL_PCO_DATA";
1222         default: return "<unknown request>";
1223     }
1224 }
1225 
1226 const char *
rilSocketIdToString(RIL_SOCKET_ID socket_id)1227 rilSocketIdToString(RIL_SOCKET_ID socket_id)
1228 {
1229     switch(socket_id) {
1230         case RIL_SOCKET_1:
1231             return "RIL_SOCKET_1";
1232 #if (SIM_COUNT >= 2)
1233         case RIL_SOCKET_2:
1234             return "RIL_SOCKET_2";
1235 #endif
1236 #if (SIM_COUNT >= 3)
1237         case RIL_SOCKET_3:
1238             return "RIL_SOCKET_3";
1239 #endif
1240 #if (SIM_COUNT >= 4)
1241         case RIL_SOCKET_4:
1242             return "RIL_SOCKET_4";
1243 #endif
1244         default:
1245             return "not a valid RIL";
1246     }
1247 }
1248 
1249 } /* namespace android */