blob: ebc2f2de88c3407a0c85794578203a9a68988120 [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //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>
Wink Savillef4c4d362009-04-02 01:37:03 -070023#include <telephony/ril_cdma_sms.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080024#include <cutils/sockets.h>
25#include <cutils/jstring.h>
26#include <cutils/record_stream.h>
27#include <utils/Log.h>
28#include <utils/SystemClock.h>
29#include <pthread.h>
30#include <utils/Parcel.h>
31#include <cutils/jstring.h>
32
33#include <sys/types.h>
34#include <pwd.h>
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <stdarg.h>
39#include <string.h>
40#include <unistd.h>
41#include <fcntl.h>
42#include <time.h>
43#include <errno.h>
44#include <assert.h>
45#include <ctype.h>
46#include <alloca.h>
47#include <sys/un.h>
48#include <assert.h>
49#include <netinet/in.h>
50#include <cutils/properties.h>
51
52#include <ril_event.h>
53
54namespace android {
55
56#define PHONE_PROCESS "radio"
57
58#define SOCKET_NAME_RIL "rild"
59#define SOCKET_NAME_RIL_DEBUG "rild-debug"
60
61#define ANDROID_WAKE_LOCK_NAME "radio-interface"
62
63
64#define PROPERTY_RIL_IMPL "gsm.version.ril-impl"
65
66// match with constant in RIL.java
67#define MAX_COMMAND_BYTES (8 * 1024)
68
69// Basically: memset buffers that the client library
70// shouldn't be using anymore in an attempt to find
71// memory usage issues sooner.
72#define MEMSET_FREED 1
73
74#define NUM_ELEMS(a) (sizeof (a) / sizeof (a)[0])
75
Wink Savillef4c4d362009-04-02 01:37:03 -070076#define MIN(a,b) ((a)<(b) ? (a) : (b))
77
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080078/* Constants for response types */
79#define RESPONSE_SOLICITED 0
80#define RESPONSE_UNSOLICITED 1
81
82/* Negative values for private RIL errno's */
83#define RIL_ERRNO_INVALID_RESPONSE -1
84
85// request, response, and unsolicited msg print macro
86#define PRINTBUF_SIZE 8096
87
88// Enable RILC log
89#define RILC_LOG 0
90
91#if RILC_LOG
92 #define startRequest sprintf(printBuf, "(")
93 #define closeRequest sprintf(printBuf, "%s)", printBuf)
94 #define printRequest(token, req) \
95 LOGD("[%04d]> %s %s", token, requestToString(req), printBuf)
96
97 #define startResponse sprintf(printBuf, "%s {", printBuf)
98 #define closeResponse sprintf(printBuf, "%s}", printBuf)
99 #define printResponse LOGD("%s", printBuf)
100
101 #define clearPrintBuf printBuf[0] = 0
102 #define removeLastChar printBuf[strlen(printBuf)-1] = 0
103 #define appendPrintBuf(x...) sprintf(printBuf, x)
104#else
105 #define startRequest
106 #define closeRequest
107 #define printRequest(token, req)
108 #define startResponse
109 #define closeResponse
110 #define printResponse
111 #define clearPrintBuf
112 #define removeLastChar
113 #define appendPrintBuf(x...)
114#endif
115
116enum WakeType {DONT_WAKE, WAKE_PARTIAL};
117
118typedef struct {
119 int requestNumber;
120 void (*dispatchFunction) (Parcel &p, struct RequestInfo *pRI);
121 int(*responseFunction) (Parcel &p, void *response, size_t responselen);
122} CommandInfo;
123
124typedef struct {
125 int requestNumber;
126 int (*responseFunction) (Parcel &p, void *response, size_t responselen);
127 WakeType wakeType;
128} UnsolResponseInfo;
129
130typedef struct RequestInfo {
131 int32_t token; //this is not RIL_Token
132 CommandInfo *pCI;
133 struct RequestInfo *p_next;
134 char cancelled;
135 char local; // responses to local commands do not go back to command process
136} RequestInfo;
137
138typedef struct UserCallbackInfo{
139 RIL_TimedCallback p_callback;
140 void *userParam;
141 struct ril_event event;
142 struct UserCallbackInfo *p_next;
143} UserCallbackInfo;
144
145
146/*******************************************************************/
147
148RIL_RadioFunctions s_callbacks = {0, NULL, NULL, NULL, NULL, NULL};
149static int s_registerCalled = 0;
150
151static pthread_t s_tid_dispatch;
152static pthread_t s_tid_reader;
153static int s_started = 0;
154
155static int s_fdListen = -1;
156static int s_fdCommand = -1;
157static int s_fdDebug = -1;
158
159static int s_fdWakeupRead;
160static int s_fdWakeupWrite;
161
162static struct ril_event s_commands_event;
163static struct ril_event s_wakeupfd_event;
164static struct ril_event s_listen_event;
165static struct ril_event s_wake_timeout_event;
166static struct ril_event s_debug_event;
167
168
169static const struct timeval TIMEVAL_WAKE_TIMEOUT = {1,0};
170
171static pthread_mutex_t s_pendingRequestsMutex = PTHREAD_MUTEX_INITIALIZER;
172static pthread_mutex_t s_writeMutex = PTHREAD_MUTEX_INITIALIZER;
173static pthread_mutex_t s_startupMutex = PTHREAD_MUTEX_INITIALIZER;
174static pthread_cond_t s_startupCond = PTHREAD_COND_INITIALIZER;
175
176static pthread_mutex_t s_dispatchMutex = PTHREAD_MUTEX_INITIALIZER;
177static pthread_cond_t s_dispatchCond = PTHREAD_COND_INITIALIZER;
178
179static RequestInfo *s_pendingRequests = NULL;
180
181static RequestInfo *s_toDispatchHead = NULL;
182static RequestInfo *s_toDispatchTail = NULL;
183
184static UserCallbackInfo *s_last_wake_timeout_info = NULL;
185
186static void *s_lastNITZTimeData = NULL;
187static size_t s_lastNITZTimeDataSize;
188
189#if RILC_LOG
190 static char printBuf[PRINTBUF_SIZE];
191#endif
192
193/*******************************************************************/
194
195static void dispatchVoid (Parcel& p, RequestInfo *pRI);
196static void dispatchString (Parcel& p, RequestInfo *pRI);
197static void dispatchStrings (Parcel& p, RequestInfo *pRI);
198static void dispatchInts (Parcel& p, RequestInfo *pRI);
199static void dispatchDial (Parcel& p, RequestInfo *pRI);
200static void dispatchSIM_IO (Parcel& p, RequestInfo *pRI);
201static void dispatchCallForward(Parcel& p, RequestInfo *pRI);
202static void dispatchRaw(Parcel& p, RequestInfo *pRI);
203static void dispatchSmsWrite (Parcel &p, RequestInfo *pRI);
204
Wink Savillef4c4d362009-04-02 01:37:03 -0700205static void dispatchCdmaSms(Parcel &p, RequestInfo *pRI);
206static void dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI);
207static void dispatchBrSmsCnf(Parcel &p, RequestInfo *pRI);
208static void dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI);
209static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800210static int responseInts(Parcel &p, void *response, size_t responselen);
211static int responseStrings(Parcel &p, void *response, size_t responselen);
212static int responseString(Parcel &p, void *response, size_t responselen);
213static int responseVoid(Parcel &p, void *response, size_t responselen);
214static int responseCallList(Parcel &p, void *response, size_t responselen);
215static int responseSMS(Parcel &p, void *response, size_t responselen);
216static int responseSIM_IO(Parcel &p, void *response, size_t responselen);
217static int responseCallForwards(Parcel &p, void *response, size_t responselen);
Wink Savillef4c4d362009-04-02 01:37:03 -0700218static int responseDataCallList(Parcel &p, void *response, size_t responselen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800219static int responseRaw(Parcel &p, void *response, size_t responselen);
220static int responseSsn(Parcel &p, void *response, size_t responselen);
Wink Savillef4c4d362009-04-02 01:37:03 -0700221static int responseSimStatus(Parcel &p, void *response, size_t responselen);
222static int responseBrSmsCnf(Parcel &p, void *response, size_t responselen);
223static int responseCdmaBrCnf(Parcel &p, void *response, size_t responselen);
224static int responseCdmaSms(Parcel &p, void *response, size_t responselen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800225static int responseCellList(Parcel &p, void *response, size_t responselen);
226
227extern "C" const char * requestToString(int request);
228extern "C" const char * failCauseToString(RIL_Errno);
229extern "C" const char * callStateToString(RIL_CallState);
230extern "C" const char * radioStateToString(RIL_RadioState);
231
232#ifdef RIL_SHLIB
233extern "C" void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
234 size_t datalen);
235#endif
236
237static UserCallbackInfo * internalRequestTimedCallback
238 (RIL_TimedCallback callback, void *param,
239 const struct timeval *relativeTime);
240
241/** Index == requestNumber */
242static CommandInfo s_commands[] = {
243#include "ril_commands.h"
244};
245
246static UnsolResponseInfo s_unsolResponses[] = {
247#include "ril_unsol_commands.h"
248};
249
250
251static char *
Wink Savillef4c4d362009-04-02 01:37:03 -0700252strdupReadString(Parcel &p) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800253 size_t stringlen;
254 const char16_t *s16;
255
256 s16 = p.readString16Inplace(&stringlen);
257
258 return strndup16to8(s16, stringlen);
259}
260
Wink Savillef4c4d362009-04-02 01:37:03 -0700261static void writeStringToParcel(Parcel &p, const char *s) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800262 char16_t *s16;
263 size_t s16_len;
264 s16 = strdup8to16(s, &s16_len);
265 p.writeString16(s16, s16_len);
266 free(s16);
267}
268
269
270static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700271memsetString (char *s) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800272 if (s != NULL) {
273 memset (s, 0, strlen(s));
274 }
275}
276
277void nullParcelReleaseFunction (const uint8_t* data, size_t dataSize,
278 const size_t* objects, size_t objectsSize,
Wink Savillef4c4d362009-04-02 01:37:03 -0700279 void* cookie) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800280 // do nothing -- the data reference lives longer than the Parcel object
281}
282
283/**
284 * To be called from dispatch thread
285 * Issue a single local request, ensuring that the response
286 * is not sent back up to the command process
287 */
288static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700289issueLocalRequest(int request, void *data, int len) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800290 RequestInfo *pRI;
291 int ret;
292
293 pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
294
295 pRI->local = 1;
296 pRI->token = 0xffffffff; // token is not used in this context
297 pRI->pCI = &(s_commands[request]);
298
299 ret = pthread_mutex_lock(&s_pendingRequestsMutex);
300 assert (ret == 0);
301
302 pRI->p_next = s_pendingRequests;
303 s_pendingRequests = pRI;
304
305 ret = pthread_mutex_unlock(&s_pendingRequestsMutex);
306 assert (ret == 0);
307
308 LOGD("C[locl]> %s", requestToString(request));
309
310 s_callbacks.onRequest(request, data, len, pRI);
311}
312
313
314
315static int
Wink Savillef4c4d362009-04-02 01:37:03 -0700316processCommandBuffer(void *buffer, size_t buflen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800317 Parcel p;
318 status_t status;
319 int32_t request;
320 int32_t token;
321 RequestInfo *pRI;
322 int ret;
323
324 p.setData((uint8_t *) buffer, buflen);
325
326 // status checked at end
327 status = p.readInt32(&request);
328 status = p.readInt32 (&token);
329
330 if (status != NO_ERROR) {
331 LOGE("invalid request block");
332 return 0;
333 }
334
335 if (request < 1 || request >= (int32_t)NUM_ELEMS(s_commands)) {
336 LOGE("unsupported request code %d token %d", request, token);
337 // FIXME this should perhaps return a response
338 return 0;
339 }
340
341
342 pRI = (RequestInfo *)calloc(1, sizeof(RequestInfo));
343
344 pRI->token = token;
345 pRI->pCI = &(s_commands[request]);
346
347 ret = pthread_mutex_lock(&s_pendingRequestsMutex);
348 assert (ret == 0);
349
350 pRI->p_next = s_pendingRequests;
351 s_pendingRequests = pRI;
352
353 ret = pthread_mutex_unlock(&s_pendingRequestsMutex);
354 assert (ret == 0);
355
356/* sLastDispatchedToken = token; */
357
358 pRI->pCI->dispatchFunction(p, pRI);
359
360 return 0;
361}
362
363static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700364invalidCommandBlock (RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800365 LOGE("invalid command block for token %d request %s",
366 pRI->token, requestToString(pRI->pCI->requestNumber));
367}
368
369/** Callee expects NULL */
370static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700371dispatchVoid (Parcel& p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800372 clearPrintBuf;
373 printRequest(pRI->token, pRI->pCI->requestNumber);
374 s_callbacks.onRequest(pRI->pCI->requestNumber, NULL, 0, pRI);
375}
376
377/** Callee expects const char * */
378static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700379dispatchString (Parcel& p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800380 status_t status;
381 size_t datalen;
382 size_t stringlen;
383 char *string8 = NULL;
384
385 string8 = strdupReadString(p);
386
387 startRequest;
388 appendPrintBuf("%s%s", printBuf, string8);
389 closeRequest;
390 printRequest(pRI->token, pRI->pCI->requestNumber);
391
392 s_callbacks.onRequest(pRI->pCI->requestNumber, string8,
393 sizeof(char *), pRI);
394
395#ifdef MEMSET_FREED
396 memsetString(string8);
397#endif
398
399 free(string8);
400 return;
401invalid:
402 invalidCommandBlock(pRI);
403 return;
404}
405
406/** Callee expects const char ** */
407static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700408dispatchStrings (Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800409 int32_t countStrings;
410 status_t status;
411 size_t datalen;
412 char **pStrings;
413
414 status = p.readInt32 (&countStrings);
415
416 if (status != NO_ERROR) {
417 goto invalid;
418 }
419
420 startRequest;
421 if (countStrings == 0) {
422 // just some non-null pointer
423 pStrings = (char **)alloca(sizeof(char *));
424 datalen = 0;
425 } else if (((int)countStrings) == -1) {
426 pStrings = NULL;
427 datalen = 0;
428 } else {
429 datalen = sizeof(char *) * countStrings;
430
431 pStrings = (char **)alloca(datalen);
432
433 for (int i = 0 ; i < countStrings ; i++) {
434 pStrings[i] = strdupReadString(p);
435 appendPrintBuf("%s%s,", printBuf, pStrings[i]);
436 }
437 }
438 removeLastChar;
439 closeRequest;
440 printRequest(pRI->token, pRI->pCI->requestNumber);
441
442 s_callbacks.onRequest(pRI->pCI->requestNumber, pStrings, datalen, pRI);
443
444 if (pStrings != NULL) {
445 for (int i = 0 ; i < countStrings ; i++) {
446#ifdef MEMSET_FREED
447 memsetString (pStrings[i]);
448#endif
449 free(pStrings[i]);
450 }
451
452#ifdef MEMSET_FREED
453 memset(pStrings, 0, datalen);
454#endif
455 }
456
457 return;
458invalid:
459 invalidCommandBlock(pRI);
460 return;
461}
462
463/** Callee expects const int * */
464static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700465dispatchInts (Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800466 int32_t count;
467 status_t status;
468 size_t datalen;
469 int *pInts;
470
471 status = p.readInt32 (&count);
472
473 if (status != NO_ERROR || count == 0) {
474 goto invalid;
475 }
476
477 datalen = sizeof(int) * count;
478 pInts = (int *)alloca(datalen);
479
480 startRequest;
481 for (int i = 0 ; i < count ; i++) {
482 int32_t t;
483
484 status = p.readInt32(&t);
485 pInts[i] = (int)t;
486 appendPrintBuf("%s%d,", printBuf, t);
487
488 if (status != NO_ERROR) {
489 goto invalid;
490 }
491 }
492 removeLastChar;
493 closeRequest;
494 printRequest(pRI->token, pRI->pCI->requestNumber);
495
496 s_callbacks.onRequest(pRI->pCI->requestNumber, const_cast<int *>(pInts),
497 datalen, pRI);
498
499#ifdef MEMSET_FREED
500 memset(pInts, 0, datalen);
501#endif
502
503 return;
504invalid:
505 invalidCommandBlock(pRI);
506 return;
507}
508
509
510/**
511 * Callee expects const RIL_SMS_WriteArgs *
512 * Payload is:
513 * int32_t status
514 * String pdu
515 */
516static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700517dispatchSmsWrite (Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800518 RIL_SMS_WriteArgs args;
519 int32_t t;
520 status_t status;
521
522 memset (&args, 0, sizeof(args));
523
524 status = p.readInt32(&t);
525 args.status = (int)t;
526
527 args.pdu = strdupReadString(p);
528
529 if (status != NO_ERROR || args.pdu == NULL) {
530 goto invalid;
531 }
532
533 args.smsc = strdupReadString(p);
534
535 startRequest;
536 appendPrintBuf("%s%d,%s,smsc=%s", printBuf, args.status,
537 (char*)args.pdu, (char*)args.smsc);
538 closeRequest;
539 printRequest(pRI->token, pRI->pCI->requestNumber);
540
541 s_callbacks.onRequest(pRI->pCI->requestNumber, &args, sizeof(args), pRI);
542
543#ifdef MEMSET_FREED
544 memsetString (args.pdu);
545#endif
546
547 free (args.pdu);
548
549#ifdef MEMSET_FREED
550 memset(&args, 0, sizeof(args));
551#endif
552
553 return;
554invalid:
555 invalidCommandBlock(pRI);
556 return;
557}
558
559/**
560 * Callee expects const RIL_Dial *
561 * Payload is:
562 * String address
563 * int32_t clir
564 */
565static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700566dispatchDial (Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800567 RIL_Dial dial;
568 int32_t t;
569 status_t status;
570
571 memset (&dial, 0, sizeof(dial));
572
573 dial.address = strdupReadString(p);
574
575 status = p.readInt32(&t);
576 dial.clir = (int)t;
577
578 if (status != NO_ERROR || dial.address == NULL) {
579 goto invalid;
580 }
581
582 startRequest;
583 appendPrintBuf("%snum=%s,clir=%d", printBuf, dial.address, dial.clir);
584 closeRequest;
585 printRequest(pRI->token, pRI->pCI->requestNumber);
586
587 s_callbacks.onRequest(pRI->pCI->requestNumber, &dial, sizeof(dial), pRI);
588
589#ifdef MEMSET_FREED
590 memsetString (dial.address);
591#endif
592
593 free (dial.address);
594
595#ifdef MEMSET_FREED
596 memset(&dial, 0, sizeof(dial));
597#endif
598
599 return;
600invalid:
601 invalidCommandBlock(pRI);
602 return;
603}
604
605/**
606 * Callee expects const RIL_SIM_IO *
607 * Payload is:
608 * int32_t command
609 * int32_t fileid
610 * String path
611 * int32_t p1, p2, p3
612 * String data
613 * String pin2
614 */
615static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700616dispatchSIM_IO (Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800617 RIL_SIM_IO simIO;
618 int32_t t;
619 status_t status;
620
621 memset (&simIO, 0, sizeof(simIO));
622
623 // note we only check status at the end
624
625 status = p.readInt32(&t);
626 simIO.command = (int)t;
627
628 status = p.readInt32(&t);
629 simIO.fileid = (int)t;
630
631 simIO.path = strdupReadString(p);
632
633 status = p.readInt32(&t);
634 simIO.p1 = (int)t;
635
636 status = p.readInt32(&t);
637 simIO.p2 = (int)t;
638
639 status = p.readInt32(&t);
640 simIO.p3 = (int)t;
641
642 simIO.data = strdupReadString(p);
643 simIO.pin2 = strdupReadString(p);
644
645 startRequest;
646 appendPrintBuf("%scmd=0x%X,efid=0x%X,path=%s,%d,%d,%d,%s,pin2=%s", printBuf,
647 simIO.command, simIO.fileid, (char*)simIO.path,
648 simIO.p1, simIO.p2, simIO.p3,
649 (char*)simIO.data, (char*)simIO.pin2);
650 closeRequest;
651 printRequest(pRI->token, pRI->pCI->requestNumber);
652
653 if (status != NO_ERROR) {
654 goto invalid;
655 }
656
657 s_callbacks.onRequest(pRI->pCI->requestNumber, &simIO, sizeof(simIO), pRI);
658
659#ifdef MEMSET_FREED
660 memsetString (simIO.path);
661 memsetString (simIO.data);
662 memsetString (simIO.pin2);
663#endif
664
665 free (simIO.path);
666 free (simIO.data);
667 free (simIO.pin2);
668
669#ifdef MEMSET_FREED
670 memset(&simIO, 0, sizeof(simIO));
671#endif
672
673 return;
674invalid:
675 invalidCommandBlock(pRI);
676 return;
677}
678
679/**
680 * Callee expects const RIL_CallForwardInfo *
681 * Payload is:
682 * int32_t status/action
683 * int32_t reason
684 * int32_t serviceCode
685 * int32_t toa
686 * String number (0 length -> null)
687 * int32_t timeSeconds
688 */
689static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700690dispatchCallForward(Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800691 RIL_CallForwardInfo cff;
692 int32_t t;
693 status_t status;
694
695 memset (&cff, 0, sizeof(cff));
696
697 // note we only check status at the end
698
699 status = p.readInt32(&t);
700 cff.status = (int)t;
701
702 status = p.readInt32(&t);
703 cff.reason = (int)t;
704
705 status = p.readInt32(&t);
706 cff.serviceClass = (int)t;
707
708 status = p.readInt32(&t);
709 cff.toa = (int)t;
710
711 cff.number = strdupReadString(p);
712
713 status = p.readInt32(&t);
714 cff.timeSeconds = (int)t;
715
716 if (status != NO_ERROR) {
717 goto invalid;
718 }
719
720 // special case: number 0-length fields is null
721
722 if (cff.number != NULL && strlen (cff.number) == 0) {
723 cff.number = NULL;
724 }
725
726 startRequest;
727 appendPrintBuf("%sstat=%d,reason=%d,serv=%d,toa=%d,%s,tout=%d", printBuf,
728 cff.status, cff.reason, cff.serviceClass, cff.toa,
729 (char*)cff.number, cff.timeSeconds);
730 closeRequest;
731 printRequest(pRI->token, pRI->pCI->requestNumber);
732
733 s_callbacks.onRequest(pRI->pCI->requestNumber, &cff, sizeof(cff), pRI);
734
735#ifdef MEMSET_FREED
736 memsetString(cff.number);
737#endif
738
739 free (cff.number);
740
741#ifdef MEMSET_FREED
742 memset(&cff, 0, sizeof(cff));
743#endif
744
745 return;
746invalid:
747 invalidCommandBlock(pRI);
748 return;
749}
750
751
752static void
Wink Savillef4c4d362009-04-02 01:37:03 -0700753dispatchRaw(Parcel &p, RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800754 int32_t len;
755 status_t status;
756 const void *data;
757
758 status = p.readInt32(&len);
759
760 if (status != NO_ERROR) {
761 goto invalid;
762 }
763
764 // The java code writes -1 for null arrays
765 if (((int)len) == -1) {
766 data = NULL;
767 len = 0;
768 }
769
770 data = p.readInplace(len);
771
772 startRequest;
773 appendPrintBuf("%sraw_size=%d", printBuf, len);
774 closeRequest;
775 printRequest(pRI->token, pRI->pCI->requestNumber);
776
777 s_callbacks.onRequest(pRI->pCI->requestNumber, const_cast<void *>(data), len, pRI);
778
779 return;
780invalid:
781 invalidCommandBlock(pRI);
782 return;
783}
784
Wink Savillef4c4d362009-04-02 01:37:03 -0700785static void
786dispatchCdmaSms(Parcel &p, RequestInfo *pRI) {
787 RIL_CDMA_SMS_Message rcsm;
788 int32_t t;
789 uint8_t ut;
790 status_t status;
791 int32_t digitCount;
792 int digitLimit;
793
794 memset(&rcsm, 0, sizeof(rcsm));
795
796 status = p.readInt32(&t);
797 rcsm.uTeleserviceID = (int) t;
798
799 status = p.read(&ut,sizeof(ut));
800 rcsm.bIsServicePresent = (uint8_t) ut;
801
802 status = p.readInt32(&t);
803 rcsm.uServicecategory = (int) t;
804
805 status = p.readInt32(&t);
806 rcsm.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) t;
807
808 status = p.readInt32(&t);
809 rcsm.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) t;
810
811 status = p.readInt32(&t);
812 rcsm.sAddress.number_type = (RIL_CDMA_SMS_NumberType) t;
813
814 status = p.readInt32(&t);
815 rcsm.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) t;
816
817 status = p.read(&ut,sizeof(ut));
818 rcsm.sAddress.number_of_digits= (uint8_t) ut;
819
820 digitLimit= MIN((rcsm.sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
821 for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
822 status = p.read(&ut,sizeof(ut));
823 rcsm.sAddress.digits[digitCount] = (uint8_t) ut;
824 }
825
826 status = p.readInt32(&t);
827 rcsm.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) t;
828
829 status = p.read(&ut,sizeof(ut));
830 rcsm.sSubAddress.odd = (uint8_t) ut;
831
832 status = p.read(&ut,sizeof(ut));
833 rcsm.sSubAddress.number_of_digits = (uint8_t) ut;
834
835 digitLimit= MIN((rcsm.sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
836 for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
837 status = p.read(&ut,sizeof(ut));
838 rcsm.sSubAddress.digits[digitCount] = (uint8_t) ut;
839 }
840
841 status = p.readInt32(&t);
842 rcsm.uBearerDataLen = (int) t;
843
844 digitLimit= MIN((rcsm.uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
845 for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
846 status = p.read(&ut, sizeof(ut));
847 rcsm.aBearerData[digitCount] = (uint8_t) ut;
848 }
849
850 if (status != NO_ERROR) {
851 goto invalid;
852 }
853
854 startRequest;
855 appendPrintBuf("%suTeleserviceID=%d, bIsServicePresent=%d, uServicecategory=%d, \
Wink Saville1b5fd232009-04-22 14:50:00 -0700856 sAddress.digit_mode=%d, sAddress.Number_mode=%d, sAddress.number_type=%d, ",
Wink Savillef4c4d362009-04-02 01:37:03 -0700857 printBuf, rcsm.uTeleserviceID,rcsm.bIsServicePresent,rcsm.uServicecategory,
Wink Saville1b5fd232009-04-22 14:50:00 -0700858 rcsm.sAddress.digit_mode, rcsm.sAddress.number_mode,rcsm.sAddress.number_type);
Wink Savillef4c4d362009-04-02 01:37:03 -0700859 closeRequest;
860
861 printRequest(pRI->token, pRI->pCI->requestNumber);
862
863 s_callbacks.onRequest(pRI->pCI->requestNumber, &rcsm, sizeof(rcsm),pRI);
864
865#ifdef MEMSET_FREED
866 memset(&rcsm, 0, sizeof(rcsm));
867#endif
868
869 return;
870
871invalid:
872 invalidCommandBlock(pRI);
873 return;
874}
875
876static void
877dispatchCdmaSmsAck(Parcel &p, RequestInfo *pRI) {
878 RIL_CDMA_SMS_Ack rcsa;
879 int32_t t;
880 status_t status;
881 int32_t digitCount;
882
883 memset(&rcsa, 0, sizeof(rcsa));
884
885 status = p.readInt32(&t);
886 rcsa.uErrorClass = (RIL_CDMA_SMS_ErrorClass) t;
887
888 status = p.readInt32(&t);
889 rcsa.uSMSCauseCode = (int) t;
890
891 if (status != NO_ERROR) {
892 goto invalid;
893 }
894
895 startRequest;
Wink Saville1b5fd232009-04-22 14:50:00 -0700896 appendPrintBuf("%suErrorClass=%d, uTLStatus=%d, ",
897 printBuf, rcsa.uErrorClass, rcsa.uSMSCauseCode);
Wink Savillef4c4d362009-04-02 01:37:03 -0700898 closeRequest;
899
900 printRequest(pRI->token, pRI->pCI->requestNumber);
901
902 s_callbacks.onRequest(pRI->pCI->requestNumber, &rcsa, sizeof(rcsa),pRI);
903
904#ifdef MEMSET_FREED
905 memset(&rcsa, 0, sizeof(rcsa));
906#endif
907
908 return;
909
910invalid:
911 invalidCommandBlock(pRI);
912 return;
913}
914
915static void
916dispatchBrSmsCnf(Parcel &p, RequestInfo *pRI) {
917 RIL_BroadcastSMSConfig rbsc;
918 int32_t t;
919 uint8_t ut;
920 status_t status;
921 int32_t digitCount;
922
923 memset(&rbsc, 0, sizeof(rbsc));
924
925 status = p.readInt32(&t);
926 rbsc.size = (int) t;
927
928 status = p.readInt32(&t);
929 rbsc.entries->uFromServiceID = (int) t;
930
931 status = p.readInt32(&t);
932 rbsc.entries->uToserviceID = (int) t;
933
934 //usage of read function on assumption that it reads any length given as 2nd argument
935 status = p.read(&ut,sizeof(ut));
936 rbsc.entries->bSelected = (uint8_t) ut;
937
938 if (status != NO_ERROR) {
939 goto invalid;
940 }
941
942 startRequest;
Wink Saville1b5fd232009-04-22 14:50:00 -0700943 appendPrintBuf("%ssize=%d, entries.uFromServiceID=%d, \
Wink Savillef4c4d362009-04-02 01:37:03 -0700944 entries.uToserviceID=%d, entries.bSelected =%d, ", printBuf,
945 rbsc.size,rbsc.entries->uFromServiceID, rbsc.entries->uToserviceID,
946 rbsc.entries->bSelected);
947 closeRequest;
948
949 printRequest(pRI->token, pRI->pCI->requestNumber);
950
951 s_callbacks.onRequest(pRI->pCI->requestNumber, &rbsc, sizeof(rbsc),pRI);
952
953#ifdef MEMSET_FREED
954 memset(&rbsc, 0, sizeof(rbsc));
955#endif
956
957 return;
958
959invalid:
960 invalidCommandBlock(pRI);
961 return;
962
963}
964
965static void
966dispatchCdmaBrSmsCnf(Parcel &p, RequestInfo *pRI) {
967 RIL_CDMA_BroadcastSMSConfig rcbsc;
968 int32_t t;
969 uint8_t ut;
970 status_t status;
971 int32_t digitCount;
972
973 memset(&rcbsc, 0, sizeof(rcbsc));
974
975 status = p.readInt32(&t);
976 rcbsc.size = (int) t;
977
978 status = p.readInt32(&t);
979 rcbsc.entries->uServiceCategory = (int) t;
980
981 status = p.readInt32(&t);
982 rcbsc.entries->uLanguage = (int) t;
983
984 status = p.read(&ut, sizeof(ut));
985 rcbsc.entries->bSelected = (uint8_t) ut;
986
987 if (status != NO_ERROR) {
988 goto invalid;
989 }
990
991 startRequest;
Wink Saville1b5fd232009-04-22 14:50:00 -0700992 appendPrintBuf("%ssize=%d, entries.uServicecategory=%d, \
993 entries.uLanguage =%d, entries.bSelected =%d, ", printBuf, rcbsc.size,
Wink Savillef4c4d362009-04-02 01:37:03 -0700994 rcbsc.entries->uServiceCategory,rcbsc.entries->uLanguage, rcbsc.entries->bSelected);
995 closeRequest;
996
997 printRequest(pRI->token, pRI->pCI->requestNumber);
998
999 s_callbacks.onRequest(pRI->pCI->requestNumber, &rcbsc, sizeof(rcbsc),pRI);
1000
1001#ifdef MEMSET_FREED
1002 memset(&rcbsc, 0, sizeof(rcbsc));
1003#endif
1004
1005 return;
1006
1007invalid:
1008 invalidCommandBlock(pRI);
1009 return;
1010
1011}
1012
1013static void dispatchRilCdmaSmsWriteArgs(Parcel &p, RequestInfo *pRI) {
1014 RIL_CDMA_SMS_WriteArgs rcsw;
1015 int32_t t;
1016 uint32_t ut;
1017 uint8_t uct;
1018 status_t status;
1019 int32_t digitCount;
1020
1021 memset(&rcsw, 0, sizeof(rcsw));
1022
1023 status = p.readInt32(&t);
1024 rcsw.status = t;
1025
1026 status = p.readInt32(&t);
1027 rcsw.message.uTeleserviceID = (int) t;
1028
1029 status = p.read(&uct,sizeof(uct));
1030 rcsw.message.bIsServicePresent = (uint8_t) uct;
1031
1032 status = p.readInt32(&t);
1033 rcsw.message.uServicecategory = (int) t;
1034
1035 status = p.readInt32(&t);
1036 rcsw.message.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) t;
1037
1038 status = p.readInt32(&t);
1039 rcsw.message.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) t;
1040
1041 status = p.readInt32(&t);
1042 rcsw.message.sAddress.number_type = (RIL_CDMA_SMS_NumberType) t;
1043
1044 status = p.readInt32(&t);
1045 rcsw.message.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) t;
1046
1047 status = p.read(&uct,sizeof(uct));
1048 rcsw.message.sAddress.number_of_digits = (uint8_t) uct;
1049
1050 for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_ADDRESS_MAX; digitCount ++) {
1051 status = p.read(&uct,sizeof(uct));
1052 rcsw.message.sAddress.digits[digitCount] = (uint8_t) uct;
1053 }
1054
1055 status = p.readInt32(&t);
1056 rcsw.message.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) t;
1057
1058 status = p.read(&uct,sizeof(uct));
1059 rcsw.message.sSubAddress.odd = (uint8_t) uct;
1060
1061 status = p.read(&uct,sizeof(uct));
1062 rcsw.message.sSubAddress.number_of_digits = (uint8_t) uct;
1063
1064 for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_SUBADDRESS_MAX; digitCount ++) {
1065 status = p.read(&uct,sizeof(uct));
1066 rcsw.message.sSubAddress.digits[digitCount] = (uint8_t) uct;
1067 }
1068
1069 status = p.readInt32(&t);
1070 rcsw.message.uBearerDataLen = (int) t;
1071
1072 for(digitCount = 0 ; digitCount < RIL_CDMA_SMS_BEARER_DATA_MAX; digitCount ++) {
1073 status = p.read(&uct, sizeof(uct));
1074 rcsw.message.aBearerData[digitCount] = (uint8_t) uct;
1075 }
1076
1077 if (status != NO_ERROR) {
1078 goto invalid;
1079 }
1080
1081 startRequest;
Wink Saville1b5fd232009-04-22 14:50:00 -07001082 appendPrintBuf("%sstatus=%d, message.uTeleserviceID=%d, message.bIsServicePresent=%d, \
1083 message.uServicecategory=%d, message.sAddress.digit_mode=%d, \
1084 message.sAddress.number_mode=%d, \
1085 message.sAddress.number_type=%d, ",
Wink Savillef4c4d362009-04-02 01:37:03 -07001086 printBuf, rcsw.status, rcsw.message.uTeleserviceID, rcsw.message.bIsServicePresent,
Wink Saville1b5fd232009-04-22 14:50:00 -07001087 rcsw.message.uServicecategory, rcsw.message.sAddress.digit_mode,
1088 rcsw.message.sAddress.number_mode,
1089 rcsw.message.sAddress.number_type);
Wink Savillef4c4d362009-04-02 01:37:03 -07001090 closeRequest;
1091
1092 printRequest(pRI->token, pRI->pCI->requestNumber);
1093
1094 s_callbacks.onRequest(pRI->pCI->requestNumber, &rcsw, sizeof(rcsw),pRI);
1095
1096#ifdef MEMSET_FREED
1097 memset(&rcsw, 0, sizeof(rcsw));
1098#endif
1099
1100 return;
1101
1102invalid:
1103 invalidCommandBlock(pRI);
1104 return;
1105
1106}
1107
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001108static int
Wink Savillef4c4d362009-04-02 01:37:03 -07001109blockingWrite(int fd, const void *buffer, size_t len) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001110 size_t writeOffset = 0;
1111 const uint8_t *toWrite;
1112
1113 toWrite = (const uint8_t *)buffer;
1114
1115 while (writeOffset < len) {
1116 ssize_t written;
1117 do {
1118 written = write (fd, toWrite + writeOffset,
1119 len - writeOffset);
1120 } while (written < 0 && errno == EINTR);
1121
1122 if (written >= 0) {
1123 writeOffset += written;
1124 } else { // written < 0
1125 LOGE ("RIL Response: unexpected error on write errno:%d", errno);
1126 close(fd);
1127 return -1;
1128 }
1129 }
1130
1131 return 0;
1132}
1133
1134static int
Wink Savillef4c4d362009-04-02 01:37:03 -07001135sendResponseRaw (const void *data, size_t dataSize) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001136 int fd = s_fdCommand;
1137 int ret;
1138 uint32_t header;
1139
1140 if (s_fdCommand < 0) {
1141 return -1;
1142 }
1143
1144 if (dataSize > MAX_COMMAND_BYTES) {
1145 LOGE("RIL: packet larger than %u (%u)",
1146 MAX_COMMAND_BYTES, (unsigned int )dataSize);
1147
1148 return -1;
1149 }
1150
1151
1152 // FIXME is blocking here ok? issue #550970
1153
1154 pthread_mutex_lock(&s_writeMutex);
1155
1156 header = htonl(dataSize);
1157
1158 ret = blockingWrite(fd, (void *)&header, sizeof(header));
1159
1160 if (ret < 0) {
1161 return ret;
1162 }
1163
1164 blockingWrite(fd, data, dataSize);
1165
1166 if (ret < 0) {
1167 return ret;
1168 }
1169
1170 pthread_mutex_unlock(&s_writeMutex);
1171
1172 return 0;
1173}
1174
1175static int
Wink Savillef4c4d362009-04-02 01:37:03 -07001176sendResponse (Parcel &p) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001177 printResponse;
1178 return sendResponseRaw(p.data(), p.dataSize());
1179}
1180
1181/** response is an int* pointing to an array of ints*/
1182
1183static int
Wink Savillef4c4d362009-04-02 01:37:03 -07001184responseInts(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001185 int numInts;
1186
1187 if (response == NULL && responselen != 0) {
1188 LOGE("invalid response: NULL");
1189 return RIL_ERRNO_INVALID_RESPONSE;
1190 }
1191 if (responselen % sizeof(int) != 0) {
1192 LOGE("invalid response length %d expected multiple of %d\n",
1193 (int)responselen, (int)sizeof(int));
1194 return RIL_ERRNO_INVALID_RESPONSE;
1195 }
1196
1197 int *p_int = (int *) response;
1198
1199 numInts = responselen / sizeof(int *);
1200 p.writeInt32 (numInts);
1201
1202 /* each int*/
1203 startResponse;
1204 for (int i = 0 ; i < numInts ; i++) {
1205 appendPrintBuf("%s%d,", printBuf, p_int[i]);
1206 p.writeInt32(p_int[i]);
1207 }
1208 removeLastChar;
1209 closeResponse;
1210
1211 return 0;
1212}
1213
1214/** response is a char **, pointing to an array of char *'s */
Wink Savillef4c4d362009-04-02 01:37:03 -07001215static int responseStrings(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001216 int numStrings;
1217
1218 if (response == NULL && responselen != 0) {
1219 LOGE("invalid response: NULL");
1220 return RIL_ERRNO_INVALID_RESPONSE;
1221 }
1222 if (responselen % sizeof(char *) != 0) {
1223 LOGE("invalid response length %d expected multiple of %d\n",
1224 (int)responselen, (int)sizeof(char *));
1225 return RIL_ERRNO_INVALID_RESPONSE;
1226 }
1227
1228 if (response == NULL) {
1229 p.writeInt32 (0);
1230 } else {
1231 char **p_cur = (char **) response;
1232
1233 numStrings = responselen / sizeof(char *);
1234 p.writeInt32 (numStrings);
1235
1236 /* each string*/
1237 startResponse;
1238 for (int i = 0 ; i < numStrings ; i++) {
1239 appendPrintBuf("%s%s,", printBuf, (char*)p_cur[i]);
1240 writeStringToParcel (p, p_cur[i]);
1241 }
1242 removeLastChar;
1243 closeResponse;
1244 }
1245 return 0;
1246}
1247
1248
1249/**
1250 * NULL strings are accepted
1251 * FIXME currently ignores responselen
1252 */
Wink Savillef4c4d362009-04-02 01:37:03 -07001253static int responseString(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001254 /* one string only */
1255 startResponse;
1256 appendPrintBuf("%s%s", printBuf, (char*)response);
1257 closeResponse;
1258
1259 writeStringToParcel(p, (const char *)response);
1260
1261 return 0;
1262}
1263
Wink Savillef4c4d362009-04-02 01:37:03 -07001264static int responseVoid(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001265 startResponse;
1266 removeLastChar;
1267 return 0;
1268}
1269
Wink Savillef4c4d362009-04-02 01:37:03 -07001270static int responseCallList(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001271 int num;
1272
1273 if (response == NULL && responselen != 0) {
1274 LOGE("invalid response: NULL");
1275 return RIL_ERRNO_INVALID_RESPONSE;
1276 }
1277
1278 if (responselen % sizeof (RIL_Call *) != 0) {
1279 LOGE("invalid response length %d expected multiple of %d\n",
1280 (int)responselen, (int)sizeof (RIL_Call *));
1281 return RIL_ERRNO_INVALID_RESPONSE;
1282 }
1283
1284 startResponse;
1285 /* number of call info's */
1286 num = responselen / sizeof(RIL_Call *);
1287 p.writeInt32(num);
1288
1289 for (int i = 0 ; i < num ; i++) {
Wink Saville1b5fd232009-04-22 14:50:00 -07001290 /* NEWRIL:TODO Remove this conditional and the else clause when we have the new ril */
1291#if NEWRIL
1292 LOGD("Compilied for NEWRIL"); // NEWRIL:TODO remove when we have the new ril
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001293 RIL_Call *p_cur = ((RIL_Call **) response)[i];
1294 /* each call info */
1295 p.writeInt32(p_cur->state);
1296 p.writeInt32(p_cur->index);
1297 p.writeInt32(p_cur->toa);
1298 p.writeInt32(p_cur->isMpty);
1299 p.writeInt32(p_cur->isMT);
1300 p.writeInt32(p_cur->als);
1301 p.writeInt32(p_cur->isVoice);
Wink Saville1b5fd232009-04-22 14:50:00 -07001302 p.writeInt32(p_cur->isVoicePrivacy);
1303 writeStringToParcel(p, p_cur->number);
John Wangff368742009-03-24 17:56:29 -07001304 p.writeInt32(p_cur->numberPresentation);
Wink Saville1b5fd232009-04-22 14:50:00 -07001305 writeStringToParcel(p, p_cur->name);
1306 p.writeInt32(p_cur->namePresentation);
1307 appendPrintBuf("%s[id=%d,%s,toa=%d,%s,%s,als=%d,%s,%s,%s,cli=%d,name='%s',%d],",
John Wangff368742009-03-24 17:56:29 -07001308 printBuf,
Wink Saville1b5fd232009-04-22 14:50:00 -07001309 p_cur->index,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001310 callStateToString(p_cur->state),
Wink Saville1b5fd232009-04-22 14:50:00 -07001311 p_cur->toa,
1312 (p_cur->isMpty)?"conf":"norm",
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001313 (p_cur->isMT)?"mt":"mo",
1314 p_cur->als,
1315 (p_cur->isVoice)?"voc":"nonvoc",
Wink Saville1b5fd232009-04-22 14:50:00 -07001316 (p_cur->isVoicePrivacy)?"evp":"noevp",
1317 p_cur->number,
1318 p_cur->numberPresentation,
1319 p_cur->name,
1320 p_cur->namePresentation);
1321#else
1322 LOGD("Old RIL");
1323 RIL_CallOld *p_cur = ((RIL_CallOld **) response)[i];
1324 /* each call info */
1325 p.writeInt32(p_cur->state);
1326 p.writeInt32(p_cur->index);
1327 p.writeInt32(p_cur->toa);
1328 p.writeInt32(p_cur->isMpty);
1329 p.writeInt32(p_cur->isMT);
1330 p.writeInt32(p_cur->als);
1331 p.writeInt32(p_cur->isVoice);
1332 p.writeInt32(0); // p_cur->isVoicePrivacy);
1333 writeStringToParcel (p, p_cur->number);
1334 p.writeInt32(p_cur->numberPresentation);
1335 writeStringToParcel (p, "a-person");
1336 p.writeInt32(2); // p_cur->namePresentation);
1337 appendPrintBuf("%s[id=%d,%s,toa=%d,%s,%s,als=%d,%s,%s,%s,cli=%d,name='%s',%d],",
1338 printBuf,
1339 p_cur->index,
1340 callStateToString(p_cur->state),
1341 p_cur->toa,
1342 (p_cur->isMpty)?"conf":"norm",
1343 (p_cur->isMT)?"mt":"mo",
1344 p_cur->als,
1345 (p_cur->isVoice)?"voc":"nonvoc",
1346 (p_cur->isVoicePrivacy)?"evp":"noevp",
1347 p_cur->number,
1348 p_cur->numberPresentation,
1349 p_cur->name,
1350 p_cur->namePresentation);
1351#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001352 }
1353 removeLastChar;
1354 closeResponse;
1355
1356 return 0;
1357}
1358
Wink Savillef4c4d362009-04-02 01:37:03 -07001359static int responseSMS(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001360 if (response == NULL) {
1361 LOGE("invalid response: NULL");
1362 return RIL_ERRNO_INVALID_RESPONSE;
1363 }
1364
1365 if (responselen != sizeof (RIL_SMS_Response) ) {
1366 LOGE("invalid response length %d expected %d",
1367 (int)responselen, (int)sizeof (RIL_SMS_Response));
1368 return RIL_ERRNO_INVALID_RESPONSE;
1369 }
1370
1371 RIL_SMS_Response *p_cur = (RIL_SMS_Response *) response;
1372
1373 p.writeInt32(p_cur->messageRef);
1374 writeStringToParcel(p, p_cur->ackPDU);
1375
1376 startResponse;
1377 appendPrintBuf("%s%d,%s", printBuf, p_cur->messageRef,
1378 (char*)p_cur->ackPDU);
1379 closeResponse;
1380
1381 return 0;
1382}
1383
Wink Savillef4c4d362009-04-02 01:37:03 -07001384static int responseDataCallList(Parcel &p, void *response, size_t responselen)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001385{
1386 if (response == NULL && responselen != 0) {
1387 LOGE("invalid response: NULL");
1388 return RIL_ERRNO_INVALID_RESPONSE;
1389 }
1390
Wink Savillef4c4d362009-04-02 01:37:03 -07001391 if (responselen % sizeof(RIL_Data_Call_Response) != 0) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001392 LOGE("invalid response length %d expected multiple of %d",
Wink Savillef4c4d362009-04-02 01:37:03 -07001393 (int)responselen, (int)sizeof(RIL_Data_Call_Response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001394 return RIL_ERRNO_INVALID_RESPONSE;
1395 }
1396
Wink Savillef4c4d362009-04-02 01:37:03 -07001397 int num = responselen / sizeof(RIL_Data_Call_Response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001398 p.writeInt32(num);
1399
Wink Savillef4c4d362009-04-02 01:37:03 -07001400 RIL_Data_Call_Response *p_cur = (RIL_Data_Call_Response *) response;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001401 startResponse;
1402 int i;
1403 for (i = 0; i < num; i++) {
1404 p.writeInt32(p_cur[i].cid);
1405 p.writeInt32(p_cur[i].active);
1406 writeStringToParcel(p, p_cur[i].type);
1407 writeStringToParcel(p, p_cur[i].apn);
1408 writeStringToParcel(p, p_cur[i].address);
1409 appendPrintBuf("%s[cid=%d,%s,%s,%s,%s],", printBuf,
1410 p_cur[i].cid,
1411 (p_cur[i].active==0)?"down":"up",
1412 (char*)p_cur[i].type,
1413 (char*)p_cur[i].apn,
1414 (char*)p_cur[i].address);
1415 }
1416 removeLastChar;
1417 closeResponse;
1418
1419 return 0;
1420}
1421
Wink Savillef4c4d362009-04-02 01:37:03 -07001422static int responseRaw(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001423 if (response == NULL && responselen != 0) {
1424 LOGE("invalid response: NULL with responselen != 0");
1425 return RIL_ERRNO_INVALID_RESPONSE;
1426 }
1427
1428 // The java code reads -1 size as null byte array
1429 if (response == NULL) {
1430 p.writeInt32(-1);
1431 } else {
1432 p.writeInt32(responselen);
1433 p.write(response, responselen);
1434 }
1435
1436 return 0;
1437}
1438
1439
Wink Savillef4c4d362009-04-02 01:37:03 -07001440static int responseSIM_IO(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001441 if (response == NULL) {
1442 LOGE("invalid response: NULL");
1443 return RIL_ERRNO_INVALID_RESPONSE;
1444 }
1445
1446 if (responselen != sizeof (RIL_SIM_IO_Response) ) {
1447 LOGE("invalid response length was %d expected %d",
1448 (int)responselen, (int)sizeof (RIL_SIM_IO_Response));
1449 return RIL_ERRNO_INVALID_RESPONSE;
1450 }
1451
1452 RIL_SIM_IO_Response *p_cur = (RIL_SIM_IO_Response *) response;
1453 p.writeInt32(p_cur->sw1);
1454 p.writeInt32(p_cur->sw2);
1455 writeStringToParcel(p, p_cur->simResponse);
1456
1457 startResponse;
1458 appendPrintBuf("%ssw1=0x%X,sw2=0x%X,%s", printBuf, p_cur->sw1, p_cur->sw2,
1459 (char*)p_cur->simResponse);
1460 closeResponse;
1461
1462
1463 return 0;
1464}
1465
Wink Savillef4c4d362009-04-02 01:37:03 -07001466static int responseCallForwards(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001467 int num;
1468
1469 if (response == NULL && responselen != 0) {
1470 LOGE("invalid response: NULL");
1471 return RIL_ERRNO_INVALID_RESPONSE;
1472 }
1473
1474 if (responselen % sizeof(RIL_CallForwardInfo *) != 0) {
1475 LOGE("invalid response length %d expected multiple of %d",
1476 (int)responselen, (int)sizeof(RIL_CallForwardInfo *));
1477 return RIL_ERRNO_INVALID_RESPONSE;
1478 }
1479
1480 /* number of call info's */
1481 num = responselen / sizeof(RIL_CallForwardInfo *);
1482 p.writeInt32(num);
1483
1484 startResponse;
1485 for (int i = 0 ; i < num ; i++) {
1486 RIL_CallForwardInfo *p_cur = ((RIL_CallForwardInfo **) response)[i];
1487
1488 p.writeInt32(p_cur->status);
1489 p.writeInt32(p_cur->reason);
1490 p.writeInt32(p_cur->serviceClass);
1491 p.writeInt32(p_cur->toa);
1492 writeStringToParcel(p, p_cur->number);
1493 p.writeInt32(p_cur->timeSeconds);
1494 appendPrintBuf("%s[%s,reason=%d,cls=%d,toa=%d,%s,tout=%d],", printBuf,
1495 (p_cur->status==1)?"enable":"disable",
1496 p_cur->reason, p_cur->serviceClass, p_cur->toa,
1497 (char*)p_cur->number,
1498 p_cur->timeSeconds);
1499 }
1500 removeLastChar;
1501 closeResponse;
1502
1503 return 0;
1504}
1505
Wink Savillef4c4d362009-04-02 01:37:03 -07001506static int responseSsn(Parcel &p, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001507 if (response == NULL) {
1508 LOGE("invalid response: NULL");
1509 return RIL_ERRNO_INVALID_RESPONSE;
1510 }
1511
1512 if (responselen != sizeof(RIL_SuppSvcNotification)) {
1513 LOGE("invalid response length was %d expected %d",
1514 (int)responselen, (int)sizeof (RIL_SuppSvcNotification));
1515 return RIL_ERRNO_INVALID_RESPONSE;
1516 }
1517
1518 RIL_SuppSvcNotification *p_cur = (RIL_SuppSvcNotification *) response;
1519 p.writeInt32(p_cur->notificationType);
1520 p.writeInt32(p_cur->code);
1521 p.writeInt32(p_cur->index);
1522 p.writeInt32(p_cur->type);
1523 writeStringToParcel(p, p_cur->number);
1524
1525 startResponse;
1526 appendPrintBuf("%s%s,code=%d,id=%d,type=%d,%s", printBuf,
1527 (p_cur->notificationType==0)?"mo":"mt",
1528 p_cur->code, p_cur->index, p_cur->type,
1529 (char*)p_cur->number);
1530 closeResponse;
1531
1532 return 0;
1533}
1534
1535static int responseCellList(Parcel &p, void *response, size_t responselen)
1536{
1537 int num;
1538
1539 if (response == NULL && responselen != 0) {
1540 LOGE("invalid response: NULL");
1541 return RIL_ERRNO_INVALID_RESPONSE;
1542 }
1543
1544 if (responselen % sizeof (RIL_NeighboringCell *) != 0) {
1545 LOGE("invalid response length %d expected multiple of %d\n",
1546 (int)responselen, (int)sizeof (RIL_NeighboringCell *));
1547 return RIL_ERRNO_INVALID_RESPONSE;
1548 }
1549
1550 startResponse;
1551 /* number of cell info's */
1552 num = responselen / sizeof(RIL_NeighboringCell *);
1553 p.writeInt32(num);
1554
1555 for (int i = 0 ; i < num ; i++) {
1556 RIL_NeighboringCell *p_cur = ((RIL_NeighboringCell **) response)[i];
1557
1558 /* each cell info */
1559 p.writeInt32(p_cur->rssi);
1560 writeStringToParcel (p, p_cur->cid);
1561
1562 appendPrintBuf("%s[cid=%s,rssi=%d],", printBuf,
1563 p_cur->cid, p_cur->rssi);
1564 }
1565 removeLastChar;
1566 closeResponse;
1567
1568 return 0;
1569}
1570
1571static void triggerEvLoop()
1572{
1573 int ret;
1574 if (!pthread_equal(pthread_self(), s_tid_dispatch)) {
1575 /* trigger event loop to wakeup. No reason to do this,
1576 * if we're in the event loop thread */
1577 do {
1578 ret = write (s_fdWakeupWrite, " ", 1);
1579 } while (ret < 0 && errno == EINTR);
1580 }
1581}
1582
1583static void rilEventAddWakeup(struct ril_event *ev)
1584{
1585 ril_event_add(ev);
1586 triggerEvLoop();
1587}
1588
Wink Savillef4c4d362009-04-02 01:37:03 -07001589static int responseSimStatus(Parcel &p, void *response, size_t responselen) {
1590 int i;
1591
1592 if (response == NULL && responselen != 0) {
1593 LOGE("invalid response: NULL");
1594 return RIL_ERRNO_INVALID_RESPONSE;
1595 }
1596
1597 if (responselen % sizeof (RIL_CardStatus *) != 0) {
1598 LOGE("invalid response length %d expected multiple of %d\n",
1599 (int)responselen, (int)sizeof (RIL_CardStatus *));
1600 return RIL_ERRNO_INVALID_RESPONSE;
1601 }
1602
1603 RIL_CardStatus *p_cur = ((RIL_CardStatus *) response);
1604
1605 p.writeInt32(p_cur->card_state);
1606 p.writeInt32(p_cur->universal_pin_state);
1607 p.writeInt32(p_cur->gsm_umts_subscription_app_index);
1608 p.writeInt32(p_cur->cdma_subscription_app_index);
1609 p.writeInt32(p_cur->num_applications);
1610
1611 startResponse;
1612 for (i = 0; i < p_cur->num_applications; i++) {
1613 p.writeInt32(p_cur->applications[i].app_type);
1614 p.writeInt32(p_cur->applications[i].app_state);
1615 p.writeInt32(p_cur->applications[i].perso_substate);
1616 writeStringToParcel (p, (const char*)(p_cur->applications[i].aid_ptr));
1617 writeStringToParcel (p, (const char*)(p_cur->applications[i].app_label_ptr));
1618 p.writeInt32(p_cur->applications[i].pin1_replaced);
1619 p.writeInt32(p_cur->applications[i].pin1);
1620 p.writeInt32(p_cur->applications[i].pin2);
1621 appendPrintBuf("%s[app_type=%d,app_state=%d,perso_substate=%d,aid_ptr=%s,\
1622 app_label_ptr=%s,pin1_replaced=%d,pin1=%d,pin2=%d],",
1623 printBuf,
1624 p_cur->applications[i].app_type,
1625 p_cur->applications[i].app_state,
1626 p_cur->applications[i].perso_substate,
1627 p_cur->applications[i].aid_ptr,
1628 p_cur->applications[i].app_label_ptr,
1629 p_cur->applications[i].pin1_replaced,
1630 p_cur->applications[i].pin1,
1631 p_cur->applications[i].pin2);
1632 }
1633 closeResponse;
1634
1635 return 0;
1636}
1637
1638static int responseBrSmsCnf(Parcel &p, void *response, size_t responselen) {
1639 int num;
1640
1641 if (response == NULL && responselen != 0) {
1642 LOGE("invalid response: NULL");
1643 return RIL_ERRNO_INVALID_RESPONSE;
1644 }
1645
1646 if (responselen % sizeof(RIL_BroadcastSMSConfig *) != 0) {
1647 LOGE("invalid response length %d expected multiple of %d",
1648 (int)responselen, (int)sizeof(RIL_BroadcastSMSConfig *));
1649 return RIL_ERRNO_INVALID_RESPONSE;
1650 }
1651
1652 /* number of call info's */
1653 num = responselen / sizeof(RIL_BroadcastSMSConfig *);
1654 p.writeInt32(num);
1655
1656 RIL_BroadcastSMSConfig *p_cur = (RIL_BroadcastSMSConfig *) response;
1657 p.writeInt32(p_cur->size);
1658 p.writeInt32(p_cur->entries->uFromServiceID);
1659 p.writeInt32(p_cur->entries->uToserviceID);
1660 p.write(&(p_cur->entries->bSelected),sizeof(p_cur->entries->bSelected));
1661
1662 startResponse;
Wink Saville1b5fd232009-04-22 14:50:00 -07001663 appendPrintBuf("%s size=%d, entries.uFromServiceID=%d, \
Wink Savillef4c4d362009-04-02 01:37:03 -07001664 entries.uToserviceID=%d, entries.bSelected =%d, ",
1665 printBuf, p_cur->size,p_cur->entries->uFromServiceID,
Wink Saville1b5fd232009-04-22 14:50:00 -07001666 p_cur->entries->uToserviceID, p_cur->entries->bSelected);
Wink Savillef4c4d362009-04-02 01:37:03 -07001667 closeResponse;
1668
1669 return 0;
1670}
1671
1672static int responseCdmaBrCnf(Parcel &p, void *response, size_t responselen) {
1673 int num;
1674
1675 if (response == NULL && responselen != 0) {
1676 LOGE("invalid response: NULL");
1677 return RIL_ERRNO_INVALID_RESPONSE;
1678 }
1679
1680 if (responselen % sizeof(RIL_CDMA_BroadcastSMSConfig*) != 0) {
1681 LOGE("invalid response length %d expected multiple of %d",
1682 (int)responselen, (int)sizeof(RIL_CDMA_BroadcastSMSConfig *));
1683 return RIL_ERRNO_INVALID_RESPONSE;
1684 }
1685
1686 /* number of call info's */
1687 num = responselen / sizeof(RIL_CDMA_BroadcastSMSConfig *);
1688 p.writeInt32(num);
1689
1690 RIL_CDMA_BroadcastSMSConfig *p_cur = (RIL_CDMA_BroadcastSMSConfig * ) response;
1691 p.writeInt32(p_cur->size);
1692 p.writeInt32(p_cur->entries->uServiceCategory);
1693 p.writeInt32(p_cur->entries->uLanguage);
1694 p.write(&(p_cur->entries->bSelected),sizeof(p_cur->entries->bSelected));
1695
1696 startResponse;
1697 appendPrintBuf("%ssize=%d, entries.uServicecategory=%d, entries.uLanguage =%d, \
1698 entries.bSelected =%d, ", printBuf,p_cur->size, p_cur->entries->uServiceCategory,
1699 p_cur->entries->uLanguage, p_cur->entries->bSelected);
1700 closeResponse;
1701
1702 return 0;
1703}
1704
1705static int responseCdmaSms(Parcel &p, void *response, size_t responselen) {
1706 int num;
1707 int digitCount;
1708 int digitLimit;
1709 uint8_t uct;
1710 void* dest;
1711
1712 if (response == NULL && responselen != 0) {
1713 LOGE("invalid response: NULL");
1714 return RIL_ERRNO_INVALID_RESPONSE;
1715 }
1716
1717 if (responselen != sizeof(RIL_CDMA_SMS_Message*)) {
1718 LOGE("invalid response length was %d expected %d",
1719 (int)responselen, (int)sizeof(RIL_CDMA_SMS_Message *));
1720 return RIL_ERRNO_INVALID_RESPONSE;
1721 }
1722
1723 RIL_CDMA_SMS_Message *p_cur = (RIL_CDMA_SMS_Message *) response;
1724 p.writeInt32(p_cur->uTeleserviceID);
1725 p.write(&(p_cur->bIsServicePresent),sizeof(uct));
1726 p.writeInt32(p_cur->uServicecategory);
1727 p.writeInt32(p_cur->sAddress.digit_mode);
1728 p.writeInt32(p_cur->sAddress.number_mode);
1729 p.writeInt32(p_cur->sAddress.number_type);
1730 p.writeInt32(p_cur->sAddress.number_plan);
1731 p.write(&(p_cur->sAddress.number_of_digits), sizeof(uct));
1732 digitLimit= MIN((p_cur->sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
1733 for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
1734 p.write(&(p_cur->sAddress.digits[digitCount]),sizeof(uct));
1735 }
1736
1737 p.writeInt32(p_cur->sSubAddress.subaddressType);
1738 p.write(&(p_cur->sSubAddress.odd),sizeof(uct));
1739 p.write(&(p_cur->sSubAddress.number_of_digits),sizeof(uct));
1740 digitLimit= MIN((p_cur->sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
1741 for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
1742 p.write(&(p_cur->sSubAddress.digits[digitCount]),sizeof(uct));
1743 }
1744
1745 digitLimit= MIN((p_cur->uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
1746 p.writeInt32(p_cur->uBearerDataLen);
1747 for(digitCount =0 ; digitCount < digitLimit; digitCount ++) {
1748 p.write(&(p_cur->aBearerData[digitCount]), sizeof(uct));
1749 }
1750
1751 startResponse;
1752 appendPrintBuf("%suTeleserviceID=%d, bIsServicePresent=%d, uServicecategory=%d, \
Wink Saville1b5fd232009-04-22 14:50:00 -07001753 sAddress.digit_mode=%d, sAddress.number_mode=%d, sAddress.number_type=%d, ",
Wink Savillef4c4d362009-04-02 01:37:03 -07001754 printBuf, p_cur->uTeleserviceID,p_cur->bIsServicePresent,p_cur->uServicecategory,
1755 p_cur->sAddress.digit_mode, p_cur->sAddress.number_mode,p_cur->sAddress.number_type);
1756 closeResponse;
1757
1758 return 0;
1759}
1760
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001761/**
1762 * A write on the wakeup fd is done just to pop us out of select()
1763 * We empty the buffer here and then ril_event will reset the timers on the
1764 * way back down
1765 */
Wink Savillef4c4d362009-04-02 01:37:03 -07001766static void processWakeupCallback(int fd, short flags, void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001767 char buff[16];
1768 int ret;
1769
1770 LOGV("processWakeupCallback");
1771
1772 /* empty our wakeup socket out */
1773 do {
1774 ret = read(s_fdWakeupRead, &buff, sizeof(buff));
1775 } while (ret > 0 || (ret < 0 && errno == EINTR));
1776}
1777
Wink Savillef4c4d362009-04-02 01:37:03 -07001778static void onCommandsSocketClosed() {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001779 int ret;
1780 RequestInfo *p_cur;
1781
1782 /* mark pending requests as "cancelled" so we dont report responses */
1783
1784 ret = pthread_mutex_lock(&s_pendingRequestsMutex);
1785 assert (ret == 0);
1786
1787 p_cur = s_pendingRequests;
1788
1789 for (p_cur = s_pendingRequests
1790 ; p_cur != NULL
1791 ; p_cur = p_cur->p_next
1792 ) {
1793 p_cur->cancelled = 1;
1794 }
1795
1796 ret = pthread_mutex_unlock(&s_pendingRequestsMutex);
1797 assert (ret == 0);
1798}
1799
Wink Savillef4c4d362009-04-02 01:37:03 -07001800static void processCommandsCallback(int fd, short flags, void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001801 RecordStream *p_rs;
1802 void *p_record;
1803 size_t recordlen;
1804 int ret;
1805
1806 assert(fd == s_fdCommand);
1807
1808 p_rs = (RecordStream *)param;
1809
1810 for (;;) {
1811 /* loop until EAGAIN/EINTR, end of stream, or other error */
1812 ret = record_stream_get_next(p_rs, &p_record, &recordlen);
1813
1814 if (ret == 0 && p_record == NULL) {
1815 /* end-of-stream */
1816 break;
1817 } else if (ret < 0) {
1818 break;
1819 } else if (ret == 0) { /* && p_record != NULL */
1820 processCommandBuffer(p_record, recordlen);
1821 }
1822 }
1823
1824 if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) {
1825 /* fatal error or end-of-stream */
1826 if (ret != 0) {
1827 LOGE("error on reading command socket errno:%d\n", errno);
1828 } else {
1829 LOGW("EOS. Closing command socket.");
1830 }
1831
1832 close(s_fdCommand);
1833 s_fdCommand = -1;
1834
1835 ril_event_del(&s_commands_event);
1836
1837 record_stream_free(p_rs);
1838
1839 /* start listening for new connections again */
1840 rilEventAddWakeup(&s_listen_event);
1841
1842 onCommandsSocketClosed();
1843 }
1844}
1845
1846
Wink Savillef4c4d362009-04-02 01:37:03 -07001847static void onNewCommandConnect() {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001848 // implicit radio state changed
1849 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1850 NULL, 0);
1851
1852 // Send last NITZ time data, in case it was missed
1853 if (s_lastNITZTimeData != NULL) {
1854 sendResponseRaw(s_lastNITZTimeData, s_lastNITZTimeDataSize);
1855
1856 free(s_lastNITZTimeData);
1857 s_lastNITZTimeData = NULL;
1858 }
1859
1860 // Get version string
1861 if (s_callbacks.getVersion != NULL) {
1862 const char *version;
1863 version = s_callbacks.getVersion();
1864 LOGI("RIL Daemon version: %s\n", version);
1865
1866 property_set(PROPERTY_RIL_IMPL, version);
1867 } else {
1868 LOGI("RIL Daemon version: unavailable\n");
1869 property_set(PROPERTY_RIL_IMPL, "unavailable");
1870 }
1871
1872}
1873
Wink Savillef4c4d362009-04-02 01:37:03 -07001874static void listenCallback (int fd, short flags, void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001875 int ret;
1876 int err;
1877 int is_phone_socket;
1878 RecordStream *p_rs;
1879
1880 struct sockaddr_un peeraddr;
1881 socklen_t socklen = sizeof (peeraddr);
1882
1883 struct ucred creds;
1884 socklen_t szCreds = sizeof(creds);
1885
1886 struct passwd *pwd = NULL;
1887
1888 assert (s_fdCommand < 0);
1889 assert (fd == s_fdListen);
1890
1891 s_fdCommand = accept(s_fdListen, (sockaddr *) &peeraddr, &socklen);
1892
1893 if (s_fdCommand < 0 ) {
1894 LOGE("Error on accept() errno:%d", errno);
1895 /* start listening for new connections again */
1896 rilEventAddWakeup(&s_listen_event);
Wink Savillef4c4d362009-04-02 01:37:03 -07001897 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001898 }
1899
1900 /* check the credential of the other side and only accept socket from
1901 * phone process
1902 */
1903 errno = 0;
1904 is_phone_socket = 0;
Wink Savillef4c4d362009-04-02 01:37:03 -07001905
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001906 err = getsockopt(s_fdCommand, SOL_SOCKET, SO_PEERCRED, &creds, &szCreds);
Wink Savillef4c4d362009-04-02 01:37:03 -07001907
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001908 if (err == 0 && szCreds > 0) {
Wink Savillef4c4d362009-04-02 01:37:03 -07001909 errno = 0;
1910 pwd = getpwuid(creds.uid);
1911 if (pwd != NULL) {
1912 if (strcmp(pwd->pw_name, PHONE_PROCESS) == 0) {
1913 is_phone_socket = 1;
1914 } else {
1915 LOGE("RILD can't accept socket from process %s", pwd->pw_name);
1916 }
1917 } else {
1918 LOGE("Error on getpwuid() errno: %d", errno);
1919 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001920 } else {
Wink Savillef4c4d362009-04-02 01:37:03 -07001921 LOGD("Error on getsockopt() errno: %d", errno);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001922 }
1923
1924 if ( !is_phone_socket ) {
1925 LOGE("RILD must accept socket from %s", PHONE_PROCESS);
1926
1927 close(s_fdCommand);
1928 s_fdCommand = -1;
1929
1930 onCommandsSocketClosed();
1931
1932 /* start listening for new connections again */
1933 rilEventAddWakeup(&s_listen_event);
1934
1935 return;
1936 }
1937
1938 ret = fcntl(s_fdCommand, F_SETFL, O_NONBLOCK);
1939
1940 if (ret < 0) {
1941 LOGE ("Error setting O_NONBLOCK errno:%d", errno);
1942 }
1943
1944 LOGI("libril: new connection");
1945
1946 p_rs = record_stream_new(s_fdCommand, MAX_COMMAND_BYTES);
1947
1948 ril_event_set (&s_commands_event, s_fdCommand, 1,
1949 processCommandsCallback, p_rs);
1950
1951 rilEventAddWakeup (&s_commands_event);
1952
1953 onNewCommandConnect();
1954}
1955
1956static void freeDebugCallbackArgs(int number, char **args) {
1957 for (int i = 0; i < number; i++) {
1958 if (args[i] != NULL) {
1959 free(args[i]);
1960 }
1961 }
1962 free(args);
1963}
1964
Wink Savillef4c4d362009-04-02 01:37:03 -07001965static void debugCallback (int fd, short flags, void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001966 int acceptFD, option;
1967 struct sockaddr_un peeraddr;
1968 socklen_t socklen = sizeof (peeraddr);
1969 int data;
1970 unsigned int qxdm_data[6];
1971 const char *deactData[1] = {"1"};
1972 char *actData[1];
1973 RIL_Dial dialData;
1974 int hangupData[1] = {1};
1975 int number;
1976 char **args;
1977
1978 acceptFD = accept (fd, (sockaddr *) &peeraddr, &socklen);
1979
1980 if (acceptFD < 0) {
1981 LOGE ("error accepting on debug port: %d\n", errno);
1982 return;
1983 }
1984
1985 if (recv(acceptFD, &number, sizeof(int), 0) != sizeof(int)) {
1986 LOGE ("error reading on socket: number of Args: \n");
1987 return;
1988 }
1989 args = (char **) malloc(sizeof(char*) * number);
1990
1991 for (int i = 0; i < number; i++) {
1992 int len;
1993 if (recv(acceptFD, &len, sizeof(int), 0) != sizeof(int)) {
1994 LOGE ("error reading on socket: Len of Args: \n");
1995 freeDebugCallbackArgs(i, args);
1996 return;
1997 }
1998 // +1 for null-term
1999 args[i] = (char *) malloc((sizeof(char) * len) + 1);
2000 if (recv(acceptFD, args[i], sizeof(char) * len, 0)
Wink Saville1b5fd232009-04-22 14:50:00 -07002001 != (int)sizeof(char) * len) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002002 LOGE ("error reading on socket: Args[%d] \n", i);
2003 freeDebugCallbackArgs(i, args);
2004 return;
2005 }
2006 char * buf = args[i];
2007 buf[len] = 0;
2008 }
2009
2010 switch (atoi(args[0])) {
2011 case 0:
2012 LOGI ("Connection on debug port: issuing reset.");
2013 issueLocalRequest(RIL_REQUEST_RESET_RADIO, NULL, 0);
2014 break;
2015 case 1:
2016 LOGI ("Connection on debug port: issuing radio power off.");
2017 data = 0;
2018 issueLocalRequest(RIL_REQUEST_RADIO_POWER, &data, sizeof(int));
2019 // Close the socket
2020 close(s_fdCommand);
2021 s_fdCommand = -1;
2022 break;
2023 case 2:
2024 LOGI ("Debug port: issuing unsolicited network change.");
2025 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
2026 NULL, 0);
2027 break;
2028 case 3:
2029 LOGI ("Debug port: QXDM log enable.");
2030 qxdm_data[0] = 65536;
2031 qxdm_data[1] = 16;
2032 qxdm_data[2] = 1;
2033 qxdm_data[3] = 32;
2034 qxdm_data[4] = 0;
2035 qxdm_data[4] = 8;
2036 issueLocalRequest(RIL_REQUEST_OEM_HOOK_RAW, qxdm_data,
2037 6 * sizeof(int));
2038 break;
2039 case 4:
2040 LOGI ("Debug port: QXDM log disable.");
2041 qxdm_data[0] = 65536;
2042 qxdm_data[1] = 16;
2043 qxdm_data[2] = 0;
2044 qxdm_data[3] = 32;
2045 qxdm_data[4] = 0;
2046 qxdm_data[4] = 8;
2047 issueLocalRequest(RIL_REQUEST_OEM_HOOK_RAW, qxdm_data,
2048 6 * sizeof(int));
2049 break;
2050 case 5:
2051 LOGI("Debug port: Radio On");
2052 data = 1;
2053 issueLocalRequest(RIL_REQUEST_RADIO_POWER, &data, sizeof(int));
2054 sleep(2);
2055 // Set network selection automatic.
2056 issueLocalRequest(RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC, NULL, 0);
2057 break;
2058 case 6:
Wink Savillef4c4d362009-04-02 01:37:03 -07002059 LOGI("Debug port: Setup Data Call, Apn :%s\n", args[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002060 actData[0] = args[1];
Wink Savillef4c4d362009-04-02 01:37:03 -07002061 issueLocalRequest(RIL_REQUEST_SETUP_DATA_CALL, &actData,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002062 sizeof(actData));
2063 break;
2064 case 7:
Wink Savillef4c4d362009-04-02 01:37:03 -07002065 LOGI("Debug port: Deactivate Data Call");
2066 issueLocalRequest(RIL_REQUEST_DEACTIVATE_DATA_CALL, &deactData,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002067 sizeof(deactData));
2068 break;
2069 case 8:
2070 LOGI("Debug port: Dial Call");
2071 dialData.clir = 0;
2072 dialData.address = args[1];
2073 issueLocalRequest(RIL_REQUEST_DIAL, &dialData, sizeof(dialData));
2074 break;
2075 case 9:
2076 LOGI("Debug port: Answer Call");
2077 issueLocalRequest(RIL_REQUEST_ANSWER, NULL, 0);
2078 break;
2079 case 10:
2080 LOGI("Debug port: End Call");
2081 issueLocalRequest(RIL_REQUEST_HANGUP, &hangupData,
2082 sizeof(hangupData));
2083 break;
2084 default:
2085 LOGE ("Invalid request");
2086 break;
2087 }
2088 freeDebugCallbackArgs(number, args);
2089 close(acceptFD);
2090}
2091
2092
Wink Savillef4c4d362009-04-02 01:37:03 -07002093static void userTimerCallback (int fd, short flags, void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002094 UserCallbackInfo *p_info;
2095
2096 p_info = (UserCallbackInfo *)param;
2097
2098 p_info->p_callback(p_info->userParam);
2099
2100
2101 // FIXME generalize this...there should be a cancel mechanism
2102 if (s_last_wake_timeout_info != NULL && s_last_wake_timeout_info == p_info) {
2103 s_last_wake_timeout_info = NULL;
2104 }
2105
2106 free(p_info);
2107}
2108
2109
2110static void *
Wink Savillef4c4d362009-04-02 01:37:03 -07002111eventLoop(void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002112 int ret;
2113 int filedes[2];
2114
2115 ril_event_init();
2116
2117 pthread_mutex_lock(&s_startupMutex);
2118
2119 s_started = 1;
2120 pthread_cond_broadcast(&s_startupCond);
2121
2122 pthread_mutex_unlock(&s_startupMutex);
2123
2124 ret = pipe(filedes);
2125
2126 if (ret < 0) {
2127 LOGE("Error in pipe() errno:%d", errno);
2128 return NULL;
2129 }
2130
2131 s_fdWakeupRead = filedes[0];
2132 s_fdWakeupWrite = filedes[1];
2133
2134 fcntl(s_fdWakeupRead, F_SETFL, O_NONBLOCK);
2135
2136 ril_event_set (&s_wakeupfd_event, s_fdWakeupRead, true,
2137 processWakeupCallback, NULL);
2138
2139 rilEventAddWakeup (&s_wakeupfd_event);
2140
2141 // Only returns on error
2142 ril_event_loop();
2143 LOGE ("error in event_loop_base errno:%d", errno);
2144
2145 return NULL;
2146}
2147
2148extern "C" void
Wink Savillef4c4d362009-04-02 01:37:03 -07002149RIL_startEventLoop(void) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002150 int ret;
2151 pthread_attr_t attr;
2152
2153 /* spin up eventLoop thread and wait for it to get started */
2154 s_started = 0;
2155 pthread_mutex_lock(&s_startupMutex);
2156
2157 pthread_attr_init (&attr);
2158 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2159 ret = pthread_create(&s_tid_dispatch, &attr, eventLoop, NULL);
2160
2161 while (s_started == 0) {
2162 pthread_cond_wait(&s_startupCond, &s_startupMutex);
2163 }
2164
2165 pthread_mutex_unlock(&s_startupMutex);
2166
2167 if (ret < 0) {
2168 LOGE("Failed to create dispatch thread errno:%d", errno);
2169 return;
2170 }
2171}
2172
2173// Used for testing purpose only.
2174extern "C" void RIL_setcallbacks (const RIL_RadioFunctions *callbacks) {
2175 memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
2176}
2177
2178extern "C" void
Wink Savillef4c4d362009-04-02 01:37:03 -07002179RIL_register (const RIL_RadioFunctions *callbacks) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002180 int ret;
2181 int flags;
2182
2183 if (callbacks == NULL
2184 || ! (callbacks->version == RIL_VERSION || callbacks->version == 1)
2185 ) {
2186 LOGE(
2187 "RIL_register: RIL_RadioFunctions * null or invalid version"
2188 " (expected %d)", RIL_VERSION);
2189 return;
2190 }
2191
2192 if (s_registerCalled > 0) {
2193 LOGE("RIL_register has been called more than once. "
2194 "Subsequent call ignored");
2195 return;
2196 }
2197
2198 memcpy(&s_callbacks, callbacks, sizeof (RIL_RadioFunctions));
2199
2200 s_registerCalled = 1;
2201
2202 // Little self-check
2203
Wink Savillef4c4d362009-04-02 01:37:03 -07002204 for (int i = 0; i < (int)NUM_ELEMS(s_commands); i++) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002205 assert(i == s_commands[i].requestNumber);
2206 }
2207
Wink Savillef4c4d362009-04-02 01:37:03 -07002208 for (int i = 0; i < (int)NUM_ELEMS(s_unsolResponses); i++) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002209 assert(i + RIL_UNSOL_RESPONSE_BASE
2210 == s_unsolResponses[i].requestNumber);
2211 }
2212
2213 // New rild impl calls RIL_startEventLoop() first
2214 // old standalone impl wants it here.
2215
2216 if (s_started == 0) {
2217 RIL_startEventLoop();
2218 }
2219
2220 // start listen socket
2221
2222#if 0
2223 ret = socket_local_server (SOCKET_NAME_RIL,
2224 ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);
2225
2226 if (ret < 0) {
2227 LOGE("Unable to bind socket errno:%d", errno);
2228 exit (-1);
2229 }
2230 s_fdListen = ret;
2231
2232#else
2233 s_fdListen = android_get_control_socket(SOCKET_NAME_RIL);
2234 if (s_fdListen < 0) {
2235 LOGE("Failed to get socket '" SOCKET_NAME_RIL "'");
2236 exit(-1);
2237 }
2238
2239 ret = listen(s_fdListen, 4);
2240
2241 if (ret < 0) {
2242 LOGE("Failed to listen on control socket '%d': %s",
2243 s_fdListen, strerror(errno));
2244 exit(-1);
2245 }
2246#endif
2247
2248
2249 /* note: non-persistent so we can accept only one connection at a time */
2250 ril_event_set (&s_listen_event, s_fdListen, false,
2251 listenCallback, NULL);
2252
2253 rilEventAddWakeup (&s_listen_event);
2254
2255#if 1
2256 // start debug interface socket
2257
2258 s_fdDebug = android_get_control_socket(SOCKET_NAME_RIL_DEBUG);
2259 if (s_fdDebug < 0) {
2260 LOGE("Failed to get socket '" SOCKET_NAME_RIL_DEBUG "' errno:%d", errno);
2261 exit(-1);
2262 }
2263
2264 ret = listen(s_fdDebug, 4);
2265
2266 if (ret < 0) {
2267 LOGE("Failed to listen on ril debug socket '%d': %s",
2268 s_fdDebug, strerror(errno));
2269 exit(-1);
2270 }
2271
2272 ril_event_set (&s_debug_event, s_fdDebug, true,
2273 debugCallback, NULL);
2274
2275 rilEventAddWakeup (&s_debug_event);
2276#endif
2277
2278}
2279
2280static int
Wink Savillef4c4d362009-04-02 01:37:03 -07002281checkAndDequeueRequestInfo(struct RequestInfo *pRI) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002282 int ret = 0;
2283
2284 if (pRI == NULL) {
2285 return 0;
2286 }
2287
2288 pthread_mutex_lock(&s_pendingRequestsMutex);
2289
2290 for(RequestInfo **ppCur = &s_pendingRequests
2291 ; *ppCur != NULL
2292 ; ppCur = &((*ppCur)->p_next)
2293 ) {
2294 if (pRI == *ppCur) {
2295 ret = 1;
2296
2297 *ppCur = (*ppCur)->p_next;
2298 break;
2299 }
2300 }
2301
2302 pthread_mutex_unlock(&s_pendingRequestsMutex);
2303
2304 return ret;
2305}
2306
2307
2308extern "C" void
Wink Savillef4c4d362009-04-02 01:37:03 -07002309RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002310 RequestInfo *pRI;
2311 int ret;
2312 size_t errorOffset;
2313
2314 pRI = (RequestInfo *)t;
2315
2316 if (!checkAndDequeueRequestInfo(pRI)) {
2317 LOGE ("RIL_onRequestComplete: invalid RIL_Token");
2318 return;
2319 }
2320
2321 if (pRI->local > 0) {
2322 // Locally issued command...void only!
2323 // response does not go back up the command socket
2324 LOGD("C[locl]< %s", requestToString(pRI->pCI->requestNumber));
2325
2326 goto done;
2327 }
2328
2329 appendPrintBuf("[%04d]< %s",
2330 pRI->token, requestToString(pRI->pCI->requestNumber));
2331
2332 if (pRI->cancelled == 0) {
2333 Parcel p;
2334
2335 p.writeInt32 (RESPONSE_SOLICITED);
2336 p.writeInt32 (pRI->token);
2337 errorOffset = p.dataPosition();
2338
2339 p.writeInt32 (e);
2340
2341 if (e == RIL_E_SUCCESS) {
2342 /* process response on success */
2343 ret = pRI->pCI->responseFunction(p, response, responselen);
2344
2345 /* if an error occurred, rewind and mark it */
2346 if (ret != 0) {
2347 p.setDataPosition(errorOffset);
2348 p.writeInt32 (ret);
2349 }
2350 } else {
2351 appendPrintBuf("%s returns %s", printBuf, failCauseToString(e));
2352 }
2353
2354 if (s_fdCommand < 0) {
2355 LOGD ("RIL onRequestComplete: Command channel closed");
2356 }
2357 sendResponse(p);
2358 }
2359
2360done:
2361 free(pRI);
2362}
2363
2364
2365static void
Wink Savillef4c4d362009-04-02 01:37:03 -07002366grabPartialWakeLock() {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002367 acquire_wake_lock(PARTIAL_WAKE_LOCK, ANDROID_WAKE_LOCK_NAME);
2368}
2369
2370static void
Wink Savillef4c4d362009-04-02 01:37:03 -07002371releaseWakeLock() {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002372 release_wake_lock(ANDROID_WAKE_LOCK_NAME);
2373}
2374
2375/**
2376 * Timer callback to put us back to sleep before the default timeout
2377 */
2378static void
Wink Savillef4c4d362009-04-02 01:37:03 -07002379wakeTimeoutCallback (void *param) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002380 // We're using "param != NULL" as a cancellation mechanism
2381 if (param == NULL) {
2382 //LOGD("wakeTimeout: releasing wake lock");
2383
2384 releaseWakeLock();
2385 } else {
2386 //LOGD("wakeTimeout: releasing wake lock CANCELLED");
2387 }
2388}
2389
2390extern "C"
2391void RIL_onUnsolicitedResponse(int unsolResponse, void *data,
2392 size_t datalen)
2393{
2394 int unsolResponseIndex;
2395 int ret;
2396 int64_t timeReceived = 0;
2397 bool shouldScheduleTimeout = false;
2398
2399 if (s_registerCalled == 0) {
2400 // Ignore RIL_onUnsolicitedResponse before RIL_register
2401 LOGW("RIL_onUnsolicitedResponse called before RIL_register");
2402 return;
2403 }
The Android Open Source Project34a51082009-03-05 14:34:37 -08002404
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002405 unsolResponseIndex = unsolResponse - RIL_UNSOL_RESPONSE_BASE;
2406
2407 if ((unsolResponseIndex < 0)
2408 || (unsolResponseIndex >= (int32_t)NUM_ELEMS(s_unsolResponses))) {
2409 LOGE("unsupported unsolicited response code %d", unsolResponse);
2410 return;
2411 }
2412
2413 // Grab a wake lock if needed for this reponse,
2414 // as we exit we'll either release it immediately
2415 // or set a timer to release it later.
2416 switch (s_unsolResponses[unsolResponseIndex].wakeType) {
2417 case WAKE_PARTIAL:
2418 grabPartialWakeLock();
2419 shouldScheduleTimeout = true;
2420 break;
2421
2422 case DONT_WAKE:
2423 default:
2424 // No wake lock is grabed so don't set timeout
2425 shouldScheduleTimeout = false;
2426 break;
2427 }
2428
2429 // Mark the time this was received, doing this
2430 // after grabing the wakelock incase getting
2431 // the elapsedRealTime might cause us to goto
2432 // sleep.
2433 if (unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
2434 timeReceived = elapsedRealtime();
2435 }
2436
2437 appendPrintBuf("[UNSL]< %s", requestToString(unsolResponse));
2438
2439 Parcel p;
2440
2441 p.writeInt32 (RESPONSE_UNSOLICITED);
2442 p.writeInt32 (unsolResponse);
2443
2444 ret = s_unsolResponses[unsolResponseIndex]
2445 .responseFunction(p, data, datalen);
2446 if (ret != 0) {
2447 // Problem with the response. Don't continue;
2448 goto error_exit;
2449 }
2450
2451 // some things get more payload
2452 switch(unsolResponse) {
2453 case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
2454 p.writeInt32(s_callbacks.onStateRequest());
2455 appendPrintBuf("%s {%s}", printBuf,
2456 radioStateToString(s_callbacks.onStateRequest()));
2457 break;
2458
2459
2460 case RIL_UNSOL_NITZ_TIME_RECEIVED:
2461 // Store the time that this was received so the
2462 // handler of this message can account for
2463 // the time it takes to arrive and process. In
2464 // particular the system has been known to sleep
2465 // before this message can be processed.
2466 p.writeInt64(timeReceived);
2467 break;
2468 }
2469
2470 ret = sendResponse(p);
2471 if (ret != 0 && unsolResponse == RIL_UNSOL_NITZ_TIME_RECEIVED) {
2472
2473 // Unfortunately, NITZ time is not poll/update like everything
2474 // else in the system. So, if the upstream client isn't connected,
2475 // keep a copy of the last NITZ response (with receive time noted
2476 // above) around so we can deliver it when it is connected
2477
2478 if (s_lastNITZTimeData != NULL) {
2479 free (s_lastNITZTimeData);
2480 s_lastNITZTimeData = NULL;
2481 }
2482
2483 s_lastNITZTimeData = malloc(p.dataSize());
2484 s_lastNITZTimeDataSize = p.dataSize();
2485 memcpy(s_lastNITZTimeData, p.data(), p.dataSize());
2486 }
2487
2488 // For now, we automatically go back to sleep after TIMEVAL_WAKE_TIMEOUT
2489 // FIXME The java code should handshake here to release wake lock
2490
2491 if (shouldScheduleTimeout) {
2492 // Cancel the previous request
2493 if (s_last_wake_timeout_info != NULL) {
2494 s_last_wake_timeout_info->userParam = (void *)1;
2495 }
2496
2497 s_last_wake_timeout_info
2498 = internalRequestTimedCallback(wakeTimeoutCallback, NULL,
2499 &TIMEVAL_WAKE_TIMEOUT);
2500 }
2501
2502 // Normal exit
2503 return;
2504
2505error_exit:
2506 // There was an error and we've got the wake lock so release it.
2507 if (shouldScheduleTimeout) {
2508 releaseWakeLock();
2509 }
2510}
2511
2512/** FIXME generalize this if you track UserCAllbackInfo, clear it
2513 when the callback occurs
2514*/
2515static UserCallbackInfo *
2516internalRequestTimedCallback (RIL_TimedCallback callback, void *param,
2517 const struct timeval *relativeTime)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002518{
2519 struct timeval myRelativeTime;
2520 UserCallbackInfo *p_info;
2521
2522 p_info = (UserCallbackInfo *) malloc (sizeof(UserCallbackInfo));
2523
2524 p_info->p_callback = callback;
2525 p_info->userParam = param;
2526
2527 if (relativeTime == NULL) {
2528 /* treat null parameter as a 0 relative time */
2529 memset (&myRelativeTime, 0, sizeof(myRelativeTime));
2530 } else {
2531 /* FIXME I think event_add's tv param is really const anyway */
2532 memcpy (&myRelativeTime, relativeTime, sizeof(myRelativeTime));
2533 }
2534
2535 ril_event_set(&(p_info->event), -1, false, userTimerCallback, p_info);
2536
2537 ril_timer_add(&(p_info->event), &myRelativeTime);
2538
2539 triggerEvLoop();
2540 return p_info;
2541}
2542
2543
2544extern "C" void
2545RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
Wink Savillef4c4d362009-04-02 01:37:03 -07002546 const struct timeval *relativeTime) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002547 internalRequestTimedCallback (callback, param, relativeTime);
2548}
2549
2550const char *
Wink Savillef4c4d362009-04-02 01:37:03 -07002551failCauseToString(RIL_Errno e) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002552 switch(e) {
2553 case RIL_E_SUCCESS: return "E_SUCCESS";
2554 case RIL_E_RADIO_NOT_AVAILABLE: return "E_RAIDO_NOT_AVAILABLE";
2555 case RIL_E_GENERIC_FAILURE: return "E_GENERIC_FAILURE";
2556 case RIL_E_PASSWORD_INCORRECT: return "E_PASSWORD_INCORRECT";
2557 case RIL_E_SIM_PIN2: return "E_SIM_PIN2";
2558 case RIL_E_SIM_PUK2: return "E_SIM_PUK2";
2559 case RIL_E_REQUEST_NOT_SUPPORTED: return "E_REQUEST_NOT_SUPPORTED";
2560 case RIL_E_CANCELLED: return "E_CANCELLED";
2561 case RIL_E_OP_NOT_ALLOWED_DURING_VOICE_CALL: return "E_OP_NOT_ALLOWED_DURING_VOICE_CALL";
2562 case RIL_E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW: return "E_OP_NOT_ALLOWED_BEFORE_REG_TO_NW";
2563 case RIL_E_SMS_SEND_FAIL_RETRY: return "E_SMS_SEND_FAIL_RETRY";
Wink Savillef4c4d362009-04-02 01:37:03 -07002564 case RIL_E_SIM_ABSENT:return "E_SIM_ABSENT";
2565#ifdef FEATURE_MULTIMODE_ANDROID
2566 case RIL_E_SUBSCRIPTION_NOT_AVAILABLE:return "E_SUBSCRIPTION_NOT_AVAILABLE";
2567 case RIL_E_MODE_NOT_SUPPORTED:return "E_MODE_NOT_SUPPORTED";
2568#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002569 default: return "<unknown error>";
2570 }
2571}
2572
2573const char *
Wink Savillef4c4d362009-04-02 01:37:03 -07002574radioStateToString(RIL_RadioState s) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002575 switch(s) {
2576 case RADIO_STATE_OFF: return "RADIO_OFF";
2577 case RADIO_STATE_UNAVAILABLE: return "RADIO_UNAVAILABLE";
2578 case RADIO_STATE_SIM_NOT_READY: return "RADIO_SIM_NOT_READY";
2579 case RADIO_STATE_SIM_LOCKED_OR_ABSENT: return "RADIO_SIM_LOCKED_OR_ABSENT";
2580 case RADIO_STATE_SIM_READY: return "RADIO_SIM_READY";
Wink Savillef4c4d362009-04-02 01:37:03 -07002581 case RADIO_STATE_RUIM_NOT_READY:return"RADIO_RUIM_NOT_READY";
2582 case RADIO_STATE_RUIM_READY:return"RADIO_RUIM_READY";
2583 case RADIO_STATE_RUIM_LOCKED_OR_ABSENT:return"RADIO_RUIM_LOCKED_OR_ABSENT";
2584 case RADIO_STATE_NV_NOT_READY:return"RADIO_NV_NOT_READY";
2585 case RADIO_STATE_NV_READY:return"RADIO_NV_READY";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002586 default: return "<unknown state>";
2587 }
2588}
2589
2590const char *
Wink Savillef4c4d362009-04-02 01:37:03 -07002591callStateToString(RIL_CallState s) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002592 switch(s) {
2593 case RIL_CALL_ACTIVE : return "ACTIVE";
2594 case RIL_CALL_HOLDING: return "HOLDING";
2595 case RIL_CALL_DIALING: return "DIALING";
2596 case RIL_CALL_ALERTING: return "ALERTING";
2597 case RIL_CALL_INCOMING: return "INCOMING";
2598 case RIL_CALL_WAITING: return "WAITING";
2599 default: return "<unknown state>";
2600 }
2601}
2602
2603const char *
Wink Savillef4c4d362009-04-02 01:37:03 -07002604requestToString(int request) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002605/*
2606 cat libs/telephony/ril_commands.h \
2607 | egrep "^ *{RIL_" \
2608 | sed -re 's/\{RIL_([^,]+),[^,]+,([^}]+).+/case RIL_\1: return "\1";/'
2609
2610
2611 cat libs/telephony/ril_unsol_commands.h \
2612 | egrep "^ *{RIL_" \
2613 | sed -re 's/\{RIL_([^,]+),([^}]+).+/case RIL_\1: return "\1";/'
2614
2615*/
2616 switch(request) {
2617 case RIL_REQUEST_GET_SIM_STATUS: return "GET_SIM_STATUS";
2618 case RIL_REQUEST_ENTER_SIM_PIN: return "ENTER_SIM_PIN";
2619 case RIL_REQUEST_ENTER_SIM_PUK: return "ENTER_SIM_PUK";
2620 case RIL_REQUEST_ENTER_SIM_PIN2: return "ENTER_SIM_PIN2";
2621 case RIL_REQUEST_ENTER_SIM_PUK2: return "ENTER_SIM_PUK2";
2622 case RIL_REQUEST_CHANGE_SIM_PIN: return "CHANGE_SIM_PIN";
2623 case RIL_REQUEST_CHANGE_SIM_PIN2: return "CHANGE_SIM_PIN2";
2624 case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION: return "ENTER_NETWORK_DEPERSONALIZATION";
2625 case RIL_REQUEST_GET_CURRENT_CALLS: return "GET_CURRENT_CALLS";
2626 case RIL_REQUEST_DIAL: return "DIAL";
2627 case RIL_REQUEST_GET_IMSI: return "GET_IMSI";
2628 case RIL_REQUEST_HANGUP: return "HANGUP";
2629 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND: return "HANGUP_WAITING_OR_BACKGROUND";
2630 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND: return "HANGUP_FOREGROUND_RESUME_BACKGROUND";
2631 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE: return "SWITCH_WAITING_OR_HOLDING_AND_ACTIVE";
2632 case RIL_REQUEST_CONFERENCE: return "CONFERENCE";
2633 case RIL_REQUEST_UDUB: return "UDUB";
2634 case RIL_REQUEST_LAST_CALL_FAIL_CAUSE: return "LAST_CALL_FAIL_CAUSE";
2635 case RIL_REQUEST_SIGNAL_STRENGTH: return "SIGNAL_STRENGTH";
2636 case RIL_REQUEST_REGISTRATION_STATE: return "REGISTRATION_STATE";
2637 case RIL_REQUEST_GPRS_REGISTRATION_STATE: return "GPRS_REGISTRATION_STATE";
2638 case RIL_REQUEST_OPERATOR: return "OPERATOR";
2639 case RIL_REQUEST_RADIO_POWER: return "RADIO_POWER";
2640 case RIL_REQUEST_DTMF: return "DTMF";
2641 case RIL_REQUEST_SEND_SMS: return "SEND_SMS";
2642 case RIL_REQUEST_SEND_SMS_EXPECT_MORE: return "SEND_SMS_EXPECT_MORE";
Wink Savillef4c4d362009-04-02 01:37:03 -07002643 case RIL_REQUEST_SETUP_DATA_CALL: return "SETUP_DATA_CALL";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002644 case RIL_REQUEST_SIM_IO: return "SIM_IO";
2645 case RIL_REQUEST_SEND_USSD: return "SEND_USSD";
2646 case RIL_REQUEST_CANCEL_USSD: return "CANCEL_USSD";
2647 case RIL_REQUEST_GET_CLIR: return "GET_CLIR";
2648 case RIL_REQUEST_SET_CLIR: return "SET_CLIR";
2649 case RIL_REQUEST_QUERY_CALL_FORWARD_STATUS: return "QUERY_CALL_FORWARD_STATUS";
2650 case RIL_REQUEST_SET_CALL_FORWARD: return "SET_CALL_FORWARD";
2651 case RIL_REQUEST_QUERY_CALL_WAITING: return "QUERY_CALL_WAITING";
2652 case RIL_REQUEST_SET_CALL_WAITING: return "SET_CALL_WAITING";
2653 case RIL_REQUEST_SMS_ACKNOWLEDGE: return "SMS_ACKNOWLEDGE";
2654 case RIL_REQUEST_GET_IMEI: return "GET_IMEI";
2655 case RIL_REQUEST_GET_IMEISV: return "GET_IMEISV";
2656 case RIL_REQUEST_ANSWER: return "ANSWER";
Wink Savillef4c4d362009-04-02 01:37:03 -07002657 case RIL_REQUEST_DEACTIVATE_DATA_CALL: return "DEACTIVATE_DATA_CALL";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002658 case RIL_REQUEST_QUERY_FACILITY_LOCK: return "QUERY_FACILITY_LOCK";
2659 case RIL_REQUEST_SET_FACILITY_LOCK: return "SET_FACILITY_LOCK";
2660 case RIL_REQUEST_CHANGE_BARRING_PASSWORD: return "CHANGE_BARRING_PASSWORD";
2661 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE: return "QUERY_NETWORK_SELECTION_MODE";
2662 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC: return "SET_NETWORK_SELECTION_AUTOMATIC";
2663 case RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL: return "SET_NETWORK_SELECTION_MANUAL";
2664 case RIL_REQUEST_QUERY_AVAILABLE_NETWORKS : return "QUERY_AVAILABLE_NETWORKS ";
2665 case RIL_REQUEST_DTMF_START: return "DTMF_START";
2666 case RIL_REQUEST_DTMF_STOP: return "DTMF_STOP";
2667 case RIL_REQUEST_BASEBAND_VERSION: return "BASEBAND_VERSION";
2668 case RIL_REQUEST_SEPARATE_CONNECTION: return "SEPARATE_CONNECTION";
2669 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE: return "SET_PREFERRED_NETWORK_TYPE";
2670 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE: return "GET_PREFERRED_NETWORK_TYPE";
2671 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS: return "GET_NEIGHBORING_CELL_IDS";
2672 case RIL_REQUEST_SET_MUTE: return "SET_MUTE";
2673 case RIL_REQUEST_GET_MUTE: return "GET_MUTE";
2674 case RIL_REQUEST_QUERY_CLIP: return "QUERY_CLIP";
Wink Savillef4c4d362009-04-02 01:37:03 -07002675 case RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE: return "LAST_DATA_CALL_FAIL_CAUSE";
2676 case RIL_REQUEST_DATA_CALL_LIST: return "DATA_CALL_LIST";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002677 case RIL_REQUEST_RESET_RADIO: return "RESET_RADIO";
2678 case RIL_REQUEST_OEM_HOOK_RAW: return "OEM_HOOK_RAW";
2679 case RIL_REQUEST_OEM_HOOK_STRINGS: return "OEM_HOOK_STRINGS";
Wink Savillef4c4d362009-04-02 01:37:03 -07002680 case RIL_REQUEST_SET_BAND_MODE: return "SET_BAND_MODE";
2681 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE: return "QUERY_AVAILABLE_BAND_MODE";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002682 case RIL_REQUEST_STK_GET_PROFILE: return "STK_GET_PROFILE";
2683 case RIL_REQUEST_STK_SET_PROFILE: return "STK_SET_PROFILE";
2684 case RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND: return "STK_SEND_ENVELOPE_COMMAND";
2685 case RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE: return "STK_SEND_TERMINAL_RESPONSE";
2686 case RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM: return "STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM";
2687 case RIL_REQUEST_SCREEN_STATE: return "SCREEN_STATE";
2688 case RIL_REQUEST_EXPLICIT_CALL_TRANSFER: return "EXPLICIT_CALL_TRANSFER";
2689 case RIL_REQUEST_SET_LOCATION_UPDATES: return "SET_LOCATION_UPDATES";
Wink Savillef4c4d362009-04-02 01:37:03 -07002690 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION:return"CDMA_SET_SUBSCRIPTION";
2691 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:return"CDMA_SET_ROAMING_PREFERENCE";
2692 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:return"CDMA_QUERY_ROAMING_PREFERENCE";
2693 case RIL_REQUEST_SET_TTY_MODE:return"SET_TTY_MODE";
2694 case RIL_REQUEST_QUERY_TTY_MODE:return"QUERY_TTY_MODE";
2695 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:return"CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE";
2696 case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:return"CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE";
2697 case RIL_REQUEST_CDMA_FLASH:return"CDMA_FLASH";
2698 case RIL_REQUEST_CDMA_BURST_DTMF:return"CDMA_BURST_DTMF";
2699 case RIL_REQUEST_CDMA_SEND_SMS:return"CDMA_SEND_SMS";
2700 case RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE:return"CDMA_SMS_ACKNOWLEDGE";
2701 case RIL_REQUEST_GET_BROADCAST_CONFIG:return"GET_BROADCAST_CONFIG";
2702 case RIL_REQUEST_SET_BROADCAST_CONFIG:return"SET_BROADCAST_CONFIG";
2703 case RIL_REQUEST_CDMA_GET_BROADCAST_CONFIG:return "CDMA_GET_BROADCAST_CONFIG";
2704 case RIL_REQUEST_CDMA_SET_BROADCAST_CONFIG:return "SET_CDMA_BROADCAST_CONFIG";
2705 case RIL_REQUEST_BROADCAST_ACTIVATION:return "BROADCAST_ACTIVATION";
2706 case RIL_REQUEST_CDMA_VALIDATE_AKEY: return"CDMA_VALIDATE_AKEY";
2707 case RIL_REQUEST_CDMA_SUBSCRIPTION: return"CDMA_SUBSCRIPTION";
2708 case RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM: return "CDMA_WRITE_SMS_TO_RUIM";
2709 case RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM: return "CDMA_DELETE_SMS_ON_RUIM";
2710 case RIL_REQUEST_DEVICE_IDENTITY: return "DEVICE_IDENTITY";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002711 case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: return "UNSOL_RESPONSE_RADIO_STATE_CHANGED";
2712 case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: return "UNSOL_RESPONSE_CALL_STATE_CHANGED";
2713 case RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED: return "UNSOL_RESPONSE_NETWORK_STATE_CHANGED";
2714 case RIL_UNSOL_RESPONSE_NEW_SMS: return "UNSOL_RESPONSE_NEW_SMS";
2715 case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT: return "UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT";
2716 case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM: return "UNSOL_RESPONSE_NEW_SMS_ON_SIM";
2717 case RIL_UNSOL_ON_USSD: return "UNSOL_ON_USSD";
2718 case RIL_UNSOL_ON_USSD_REQUEST: return "UNSOL_ON_USSD_REQUEST(obsolete)";
2719 case RIL_UNSOL_NITZ_TIME_RECEIVED: return "UNSOL_NITZ_TIME_RECEIVED";
2720 case RIL_UNSOL_SIGNAL_STRENGTH: return "UNSOL_SIGNAL_STRENGTH";
2721 case RIL_UNSOL_STK_SESSION_END: return "UNSOL_STK_SESSION_END";
2722 case RIL_UNSOL_STK_PROACTIVE_COMMAND: return "UNSOL_STK_PROACTIVE_COMMAND";
2723 case RIL_UNSOL_STK_EVENT_NOTIFY: return "UNSOL_STK_EVENT_NOTIFY";
2724 case RIL_UNSOL_STK_CALL_SETUP: return "UNSOL_STK_CALL_SETUP";
2725 case RIL_UNSOL_SIM_SMS_STORAGE_FULL: return "UNSOL_SIM_SMS_STORAGE_FUL";
2726 case RIL_UNSOL_SIM_REFRESH: return "UNSOL_SIM_REFRESH";
Wink Savillef4c4d362009-04-02 01:37:03 -07002727 case RIL_UNSOL_DATA_CALL_LIST_CHANGED: return "UNSOL_DATA_CALL_LIST_CHANGED";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002728 case RIL_UNSOL_CALL_RING: return "UNSOL_CALL_RING";
The Android Open Source Project34a51082009-03-05 14:34:37 -08002729 case RIL_UNSOL_RESTRICTED_STATE_CHANGED: return "UNSOL_RESTRICTED_STATE_CHANGED";
Wink Savillef4c4d362009-04-02 01:37:03 -07002730 case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: return "UNSOL_RESPONSE_SIM_STATUS_CHANGED";
2731 case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS: return "UNSOL_NEW_CDMA_SMS";
2732 case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: return "UNSOL_NEW_BROADCAST_SMS";
2733 case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL: return "UNSOL_CDMA_RUIM_SMS_STORAGE_FULL";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002734 default: return "<unknown request>";
2735 }
2736}
2737
2738} /* namespace android */