1 /* //device/system/reference-ril/reference-ril.c
3 ** Copyright 2006, The Android Open Source Project
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
9 ** http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <telephony/ril.h>
24 #include <sys/types.h>
29 #include "atchannel.h"
33 #include <sys/socket.h>
34 #include <cutils/sockets.h>
36 #include "hardware/qemu_pipe.h"
39 #include <utils/Log.h>
41 #define MAX_AT_RESPONSE 0x1000
43 /* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
44 #define PPP_TTY_PATH "/dev/omap_csmi_tty1"
46 #ifdef USE_TI_COMMANDS
48 // Enable a workaround
49 // 1) Make incoming call, do not answer
50 // 2) Hangup remote end
51 // Expected: call should disappear from CLCC line
52 // Actual: Call shows as "ACTIVE" before disappearing
53 #define WORKAROUND_ERRONEOUS_ANSWER 1
55 // Some varients of the TI stack do not support the +CGEV unsolicited
56 // response. However, they seem to send an unsolicited +CME ERROR: 150
57 #define WORKAROUND_FAKE_CGEV 1
63 SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
66 SIM_NETWORK_PERSONALIZATION = 5
69 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
70 static RIL_RadioState currentState();
71 static int onSupports (int requestCode);
72 static void onCancel (RIL_Token t);
73 static const char *getVersion();
74 static int isRadioOn();
75 static SIM_Status getSIMStatus();
76 static int getCardStatus(RIL_CardStatus **pp_card_status);
77 static void freeCardStatus(RIL_CardStatus *p_card_status);
78 static void onDataCallListChanged(void *param);
80 extern const char * requestToString(int request);
82 /*** Static Variables ***/
83 static const RIL_RadioFunctions s_callbacks = {
93 static const struct RIL_Env *s_rilenv;
95 #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
96 #define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
97 #define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
100 static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
102 static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
103 static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
105 static int s_port = -1;
106 static const char * s_device_path = NULL;
107 static int s_device_socket = 0;
109 /* trigger change to this with s_state_cond */
110 static int s_closed = 0;
112 static int sFD; /* file desc of AT channel */
113 static char sATBuffer[MAX_AT_RESPONSE+1];
114 static char *sATBufferCur = NULL;
116 static const struct timeval TIMEVAL_SIMPOLL = {1,0};
117 static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
118 static const struct timeval TIMEVAL_0 = {0,0};
120 #ifdef WORKAROUND_ERRONEOUS_ANSWER
121 // Max number of times we'll try to repoll when we think
122 // we have a AT+CLCC race condition
123 #define REPOLL_CALLS_COUNT_MAX 4
125 // Line index that was incoming or waiting at last poll, or -1 for none
126 static int s_incomingOrWaitingLine = -1;
127 // Number of times we've asked for a repoll of AT+CLCC
128 static int s_repollCallsCount = 0;
129 // Should we expect a call to be answered in the next CLCC?
130 static int s_expectAnswer = 0;
131 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
133 static void pollSIMState (void *param);
134 static void setRadioState(RIL_RadioState newState);
136 static int clccStateToRILState(int state, RIL_CallState *p_state)
140 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
141 case 1: *p_state = RIL_CALL_HOLDING; return 0;
142 case 2: *p_state = RIL_CALL_DIALING; return 0;
143 case 3: *p_state = RIL_CALL_ALERTING; return 0;
144 case 4: *p_state = RIL_CALL_INCOMING; return 0;
145 case 5: *p_state = RIL_CALL_WAITING; return 0;
151 * Note: directly modified line and has *p_call point directly into
154 static int callFromCLCCLine(char *line, RIL_Call *p_call)
156 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
157 // index,isMT,state,mode,isMpty(,number,TOA)?
163 err = at_tok_start(&line);
164 if (err < 0) goto error;
166 err = at_tok_nextint(&line, &(p_call->index));
167 if (err < 0) goto error;
169 err = at_tok_nextbool(&line, &(p_call->isMT));
170 if (err < 0) goto error;
172 err = at_tok_nextint(&line, &state);
173 if (err < 0) goto error;
175 err = clccStateToRILState(state, &(p_call->state));
176 if (err < 0) goto error;
178 err = at_tok_nextint(&line, &mode);
179 if (err < 0) goto error;
181 p_call->isVoice = (mode == 0);
183 err = at_tok_nextbool(&line, &(p_call->isMpty));
184 if (err < 0) goto error;
186 if (at_tok_hasmore(&line)) {
187 err = at_tok_nextstr(&line, &(p_call->number));
189 /* tolerate null here */
190 if (err < 0) return 0;
192 // Some lame implementations return strings
193 // like "NOT AVAILABLE" in the CLCC line
194 if (p_call->number != NULL
195 && 0 == strspn(p_call->number, "+0123456789")
197 p_call->number = NULL;
200 err = at_tok_nextint(&line, &p_call->toa);
201 if (err < 0) goto error;
204 p_call->uusInfo = NULL;
209 LOGE("invalid CLCC line\n");
214 /** do post-AT+CFUN=1 initialization */
215 static void onRadioPowerOn()
217 #ifdef USE_TI_COMMANDS
218 /* Must be after CFUN=1 */
219 /* TI specific -- notifications for CPHS things such */
220 /* as CPHS message waiting indicator */
222 at_send_command("AT%CPHS=1", NULL);
224 /* TI specific -- enable NITZ unsol notifs */
225 at_send_command("AT%CTZV=1", NULL);
231 /** do post- SIM ready initialization */
232 static void onSIMReady()
234 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
236 * Always send SMS messages directly to the TE
238 * mode = 1 // discard when link is reserved (link should never be
240 * mt = 2 // most messages routed to TE
241 * bm = 2 // new cell BM's routed to TE
242 * ds = 1 // Status reports routed to TE
243 * bfr = 1 // flush buffer
245 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
248 static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
253 ATResponse *p_response = NULL;
255 assert (datalen >= sizeof(int *));
256 onOff = ((int *)data)[0];
258 if (onOff == 0 && sState != RADIO_STATE_OFF) {
259 err = at_send_command("AT+CFUN=0", &p_response);
260 if (err < 0 || p_response->success == 0) goto error;
261 setRadioState(RADIO_STATE_OFF);
262 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
263 err = at_send_command("AT+CFUN=1", &p_response);
264 if (err < 0|| p_response->success == 0) {
265 // Some stacks return an error when there is no SIM,
266 // but they really turn the RF portion on
267 // So, if we get an error, let's check to see if it
270 if (isRadioOn() != 1) {
274 setRadioState(RADIO_STATE_SIM_NOT_READY);
277 at_response_free(p_response);
278 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
281 at_response_free(p_response);
282 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
285 static void requestOrSendDataCallList(RIL_Token *t);
287 static void onDataCallListChanged(void *param)
289 requestOrSendDataCallList(NULL);
292 static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
294 requestOrSendDataCallList(&t);
297 static void requestOrSendDataCallList(RIL_Token *t)
299 ATResponse *p_response;
305 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
306 if (err != 0 || p_response->success == 0) {
308 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
310 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
315 for (p_cur = p_response->p_intermediates; p_cur != NULL;
316 p_cur = p_cur->p_next)
319 RIL_Data_Call_Response *responses =
320 alloca(n * sizeof(RIL_Data_Call_Response));
323 for (i = 0; i < n; i++) {
324 responses[i].cid = -1;
325 responses[i].active = -1;
326 responses[i].type = "";
327 responses[i].apn = "";
328 responses[i].address = "";
331 RIL_Data_Call_Response *response = responses;
332 for (p_cur = p_response->p_intermediates; p_cur != NULL;
333 p_cur = p_cur->p_next) {
334 char *line = p_cur->line;
336 err = at_tok_start(&line);
340 err = at_tok_nextint(&line, &response->cid);
344 err = at_tok_nextint(&line, &response->active);
351 at_response_free(p_response);
353 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
354 if (err != 0 || p_response->success == 0) {
356 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
358 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
363 for (p_cur = p_response->p_intermediates; p_cur != NULL;
364 p_cur = p_cur->p_next) {
365 char *line = p_cur->line;
372 err = at_tok_start(&line);
376 err = at_tok_nextint(&line, &cid);
380 for (i = 0; i < n; i++) {
381 if (responses[i].cid == cid)
386 /* details for a context we didn't hear about in the last request */
390 err = at_tok_nextstr(&line, &out);
394 responses[i].type = alloca(strlen(out) + 1);
395 strcpy(responses[i].type, out);
397 err = at_tok_nextstr(&line, &out);
401 responses[i].apn = alloca(strlen(out) + 1);
402 strcpy(responses[i].apn, out);
404 err = at_tok_nextstr(&line, &out);
408 responses[i].address = alloca(strlen(out) + 1);
409 strcpy(responses[i].address, out);
412 at_response_free(p_response);
415 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
416 n * sizeof(RIL_Data_Call_Response));
418 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
420 n * sizeof(RIL_Data_Call_Response));
426 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
428 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
431 at_response_free(p_response);
434 static void requestQueryNetworkSelectionMode(
435 void *data, size_t datalen, RIL_Token t)
438 ATResponse *p_response = NULL;
442 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
444 if (err < 0 || p_response->success == 0) {
448 line = p_response->p_intermediates->line;
450 err = at_tok_start(&line);
456 err = at_tok_nextint(&line, &response);
462 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
463 at_response_free(p_response);
466 at_response_free(p_response);
467 LOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
468 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
471 static void sendCallStateChanged(void *param)
473 RIL_onUnsolicitedResponse (
474 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
478 static void requestGetCurrentCalls(void *data, size_t datalen, RIL_Token t)
481 ATResponse *p_response;
490 #ifdef WORKAROUND_ERRONEOUS_ANSWER
491 int prevIncomingOrWaitingLine;
493 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
494 s_incomingOrWaitingLine = -1;
495 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
497 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
499 if (err != 0 || p_response->success == 0) {
500 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
504 /* count the calls */
505 for (countCalls = 0, p_cur = p_response->p_intermediates
507 ; p_cur = p_cur->p_next
512 /* yes, there's an array of pointers and then an array of structures */
514 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
515 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
516 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
518 /* init the pointer array */
519 for(i = 0; i < countCalls ; i++) {
520 pp_calls[i] = &(p_calls[i]);
523 for (countValidCalls = 0, p_cur = p_response->p_intermediates
525 ; p_cur = p_cur->p_next
527 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
533 #ifdef WORKAROUND_ERRONEOUS_ANSWER
534 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
535 || p_calls[countValidCalls].state == RIL_CALL_WAITING
537 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
539 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
541 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
542 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
550 #ifdef WORKAROUND_ERRONEOUS_ANSWER
552 // A call was incoming or waiting
553 // Now it's marked as active
554 // But we never answered it
556 // This is probably a bug, and the call will probably
557 // disappear from the call list in the next poll
558 if (prevIncomingOrWaitingLine >= 0
559 && s_incomingOrWaitingLine < 0
560 && s_expectAnswer == 0
562 for (i = 0; i < countValidCalls ; i++) {
564 if (p_calls[i].index == prevIncomingOrWaitingLine
565 && p_calls[i].state == RIL_CALL_ACTIVE
566 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
569 "Hit WORKAROUND_ERRONOUS_ANSWER case."
570 " Repoll count: %d\n", s_repollCallsCount);
571 s_repollCallsCount++;
578 s_repollCallsCount = 0;
579 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
581 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
582 countValidCalls * sizeof (RIL_Call *));
584 at_response_free(p_response);
586 #ifdef POLL_CALL_STATE
587 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
588 // smd, so we're forced to poll until the call ends.
592 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
597 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
598 at_response_free(p_response);
601 static void requestDial(void *data, size_t datalen, RIL_Token t)
608 p_dial = (RIL_Dial *)data;
610 switch (p_dial->clir) {
611 case 1: clir = "I"; break; /*invocation*/
612 case 2: clir = "i"; break; /*suppression*/
614 case 0: clir = ""; break; /*subscription default*/
617 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
619 ret = at_send_command(cmd, NULL);
623 /* success or failure is ignored by the upper layer here.
624 it will call GET_CURRENT_CALLS and determine success that way */
625 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
628 static void requestWriteSmsToSim(void *data, size_t datalen, RIL_Token t)
630 RIL_SMS_WriteArgs *p_args;
634 ATResponse *p_response = NULL;
636 p_args = (RIL_SMS_WriteArgs *)data;
638 length = strlen(p_args->pdu)/2;
639 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
641 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
643 if (err != 0 || p_response->success == 0) goto error;
645 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
646 at_response_free(p_response);
650 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
651 at_response_free(p_response);
654 static void requestHangup(void *data, size_t datalen, RIL_Token t)
661 p_line = (int *)data;
664 // "Releases a specific active call X"
665 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
667 ret = at_send_command(cmd, NULL);
671 /* success or failure is ignored by the upper layer here.
672 it will call GET_CURRENT_CALLS and determine success that way */
673 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
676 static void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
678 ATResponse *p_response = NULL;
683 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
685 if (err < 0 || p_response->success == 0) {
686 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
690 line = p_response->p_intermediates->line;
692 err = at_tok_start(&line);
693 if (err < 0) goto error;
695 err = at_tok_nextint(&line, &(response[0]));
696 if (err < 0) goto error;
698 err = at_tok_nextint(&line, &(response[1]));
699 if (err < 0) goto error;
701 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
703 at_response_free(p_response);
707 LOGE("requestSignalStrength must never return an error when radio is on");
708 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
709 at_response_free(p_response);
712 static void requestRegistrationState(int request, void *data,
713 size_t datalen, RIL_Token t)
717 char * responseStr[4];
718 ATResponse *p_response = NULL;
727 if (request == RIL_REQUEST_REGISTRATION_STATE) {
730 } else if (request == RIL_REQUEST_GPRS_REGISTRATION_STATE) {
738 err = at_send_command_singleline(cmd, prefix, &p_response);
740 if (err != 0) goto error;
742 line = p_response->p_intermediates->line;
744 err = at_tok_start(&line);
745 if (err < 0) goto error;
747 /* Ok you have to be careful here
748 * The solicited version of the CREG response is
749 * +CREG: n, stat, [lac, cid]
750 * and the unsolicited version is
751 * +CREG: stat, [lac, cid]
752 * The <n> parameter is basically "is unsolicited creg on?"
753 * which it should always be
755 * Now we should normally get the solicited version here,
756 * but the unsolicited version could have snuck in
757 * so we have to handle both
759 * Also since the LAC and CID are only reported when registered,
760 * we can have 1, 2, 3, or 4 arguments here
762 * finally, a +CGREG: answer may have a fifth value that corresponds
763 * to the network type, as in;
765 * +CGREG: n, stat [,lac, cid [,networkType]]
768 /* count number of commas */
770 for (p = line ; *p != '\0' ;p++) {
771 if (*p == ',') commas++;
775 case 0: /* +CREG: <stat> */
776 err = at_tok_nextint(&line, &response[0]);
777 if (err < 0) goto error;
782 case 1: /* +CREG: <n>, <stat> */
783 err = at_tok_nextint(&line, &skip);
784 if (err < 0) goto error;
785 err = at_tok_nextint(&line, &response[0]);
786 if (err < 0) goto error;
789 if (err < 0) goto error;
792 case 2: /* +CREG: <stat>, <lac>, <cid> */
793 err = at_tok_nextint(&line, &response[0]);
794 if (err < 0) goto error;
795 err = at_tok_nexthexint(&line, &response[1]);
796 if (err < 0) goto error;
797 err = at_tok_nexthexint(&line, &response[2]);
798 if (err < 0) goto error;
800 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
801 err = at_tok_nextint(&line, &skip);
802 if (err < 0) goto error;
803 err = at_tok_nextint(&line, &response[0]);
804 if (err < 0) goto error;
805 err = at_tok_nexthexint(&line, &response[1]);
806 if (err < 0) goto error;
807 err = at_tok_nexthexint(&line, &response[2]);
808 if (err < 0) goto error;
810 /* special case for CGREG, there is a fourth parameter
811 * that is the network type (unknown/gprs/edge/umts)
813 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
814 err = at_tok_nextint(&line, &skip);
815 if (err < 0) goto error;
816 err = at_tok_nextint(&line, &response[0]);
817 if (err < 0) goto error;
818 err = at_tok_nexthexint(&line, &response[1]);
819 if (err < 0) goto error;
820 err = at_tok_nexthexint(&line, &response[2]);
821 if (err < 0) goto error;
822 err = at_tok_nexthexint(&line, &response[3]);
823 if (err < 0) goto error;
830 asprintf(&responseStr[0], "%d", response[0]);
831 asprintf(&responseStr[1], "%x", response[1]);
832 asprintf(&responseStr[2], "%x", response[2]);
835 asprintf(&responseStr[3], "%d", response[3]);
837 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
838 at_response_free(p_response);
842 LOGE("requestRegistrationState must never return an error when radio is on");
843 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
844 at_response_free(p_response);
847 static void requestOperator(void *data, size_t datalen, RIL_Token t)
855 memset(response, 0, sizeof(response));
857 ATResponse *p_response = NULL;
859 err = at_send_command_multiline(
860 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
861 "+COPS:", &p_response);
863 /* we expect 3 lines here:
864 * +COPS: 0,0,"T - Mobile"
866 * +COPS: 0,2,"310170"
869 if (err != 0) goto error;
871 for (i = 0, p_cur = p_response->p_intermediates
873 ; p_cur = p_cur->p_next, i++
875 char *line = p_cur->line;
877 err = at_tok_start(&line);
878 if (err < 0) goto error;
880 err = at_tok_nextint(&line, &skip);
881 if (err < 0) goto error;
883 // If we're unregistered, we may just get
884 // a "+COPS: 0" response
885 if (!at_tok_hasmore(&line)) {
890 err = at_tok_nextint(&line, &skip);
891 if (err < 0) goto error;
893 // a "+COPS: 0, n" response is also possible
894 if (!at_tok_hasmore(&line)) {
899 err = at_tok_nextstr(&line, &(response[i]));
900 if (err < 0) goto error;
904 /* expect 3 lines exactly */
908 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
909 at_response_free(p_response);
913 LOGE("requestOperator must not return error when radio is on");
914 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
915 at_response_free(p_response);
918 static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
925 RIL_SMS_Response response;
926 ATResponse *p_response = NULL;
928 smsc = ((const char **)data)[0];
929 pdu = ((const char **)data)[1];
931 tpLayerLength = strlen(pdu)/2;
933 // "NULL for default SMSC"
938 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
939 asprintf(&cmd2, "%s%s", smsc, pdu);
941 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
943 if (err != 0 || p_response->success == 0) goto error;
945 memset(&response, 0, sizeof(response));
947 /* FIXME fill in messageRef and ackPDU */
949 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
950 at_response_free(p_response);
954 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
955 at_response_free(p_response);
958 static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
963 ATResponse *p_response = NULL;
964 char *response[2] = { "1", PPP_TTY_PATH };
966 apn = ((const char **)data)[2];
968 #ifdef USE_TI_COMMANDS
969 // Config for multislot class 10 (probably default anyway eh?)
970 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
973 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
974 #endif /* USE_TI_COMMANDS */
979 ssize_t written, rlen;
980 char status[32] = {0};
983 LOGD("requesting data connection to APN '%s'", apn);
985 fd = open ("/dev/qmi", O_RDWR);
986 if (fd >= 0) { /* the device doesn't exist on the emulator */
988 LOGD("opened the qmi device\n");
989 asprintf(&cmd, "up:%s", apn);
994 written = write (fd, cmd + cur, len - cur);
995 } while (written < 0 && errno == EINTR);
998 LOGE("### ERROR writing to /dev/qmi");
1006 // wait for interface to come online
1011 rlen = read(fd, status, 31);
1012 } while (rlen < 0 && errno == EINTR);
1015 LOGE("### ERROR reading from /dev/qmi");
1019 status[rlen] = '\0';
1020 LOGD("### status: %s", status);
1022 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1027 LOGE("### Failed to get data connection up\n");
1031 qmistatus = system("netcfg rmnet0 dhcp");
1033 LOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1035 if (qmistatus < 0) goto error;
1039 asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
1040 //FIXME check for error here
1041 err = at_send_command(cmd, NULL);
1044 // Set required QoS params to default
1045 err = at_send_command("AT+CGQREQ=1", NULL);
1047 // Set minimum QoS params to default
1048 err = at_send_command("AT+CGQMIN=1", NULL);
1050 // packet-domain event reporting
1051 err = at_send_command("AT+CGEREP=1,0", NULL);
1053 // Hangup anything that's happening there now
1054 err = at_send_command("AT+CGACT=1,0", NULL);
1056 // Start data on PDP context 1
1057 err = at_send_command("ATD*99***1#", &p_response);
1059 if (err < 0 || p_response->success == 0) {
1064 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1065 at_response_free(p_response);
1069 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1070 at_response_free(p_response);
1074 static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1079 ackSuccess = ((int *)data)[0];
1081 if (ackSuccess == 1) {
1082 err = at_send_command("AT+CNMA=1", NULL);
1083 } else if (ackSuccess == 0) {
1084 err = at_send_command("AT+CNMA=2", NULL);
1086 LOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1090 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1092 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1096 static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1098 ATResponse *p_response = NULL;
1099 RIL_SIM_IO_Response sr;
1105 memset(&sr, 0, sizeof(sr));
1107 p_args = (RIL_SIM_IO *)data;
1109 /* FIXME handle pin2 */
1111 if (p_args->data == NULL) {
1112 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1113 p_args->command, p_args->fileid,
1114 p_args->p1, p_args->p2, p_args->p3);
1116 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1117 p_args->command, p_args->fileid,
1118 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1121 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1123 if (err < 0 || p_response->success == 0) {
1127 line = p_response->p_intermediates->line;
1129 err = at_tok_start(&line);
1130 if (err < 0) goto error;
1132 err = at_tok_nextint(&line, &(sr.sw1));
1133 if (err < 0) goto error;
1135 err = at_tok_nextint(&line, &(sr.sw2));
1136 if (err < 0) goto error;
1138 if (at_tok_hasmore(&line)) {
1139 err = at_tok_nextstr(&line, &(sr.simResponse));
1140 if (err < 0) goto error;
1143 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1144 at_response_free(p_response);
1149 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1150 at_response_free(p_response);
1155 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1157 ATResponse *p_response = NULL;
1160 const char** strings = (const char**)data;;
1162 if ( datalen == sizeof(char*) ) {
1163 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1164 } else if ( datalen == 2*sizeof(char*) ) {
1165 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1169 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1172 if (err < 0 || p_response->success == 0) {
1174 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1176 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1178 at_response_free(p_response);
1182 static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1184 const char *ussdRequest;
1186 ussdRequest = (char *)(data);
1189 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1196 /*** Callback methods from the RIL library to us ***/
1199 * Call from RIL to us to make a RIL_REQUEST
1201 * Must be completed with a call to RIL_onRequestComplete()
1203 * RIL_onRequestComplete() may be called from any thread, before or after
1204 * this function returns.
1206 * Will always be called from the same thread, so returning here implies
1207 * that the radio is ready to process another command (whether or not
1208 * the previous command has completed).
1211 onRequest (int request, void *data, size_t datalen, RIL_Token t)
1213 ATResponse *p_response;
1216 LOGD("onRequest: %s", requestToString(request));
1218 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1219 * when RADIO_STATE_UNAVAILABLE.
1221 if (sState == RADIO_STATE_UNAVAILABLE
1222 && request != RIL_REQUEST_GET_SIM_STATUS
1224 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1228 /* Ignore all non-power requests when RADIO_STATE_OFF
1229 * (except RIL_REQUEST_GET_SIM_STATUS)
1231 if (sState == RADIO_STATE_OFF
1232 && !(request == RIL_REQUEST_RADIO_POWER
1233 || request == RIL_REQUEST_GET_SIM_STATUS)
1235 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1240 case RIL_REQUEST_GET_SIM_STATUS: {
1241 RIL_CardStatus *p_card_status;
1245 int result = getCardStatus(&p_card_status);
1246 if (result == RIL_E_SUCCESS) {
1247 p_buffer = (char *)p_card_status;
1248 buffer_size = sizeof(*p_card_status);
1253 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
1254 freeCardStatus(p_card_status);
1257 case RIL_REQUEST_GET_CURRENT_CALLS:
1258 requestGetCurrentCalls(data, datalen, t);
1260 case RIL_REQUEST_DIAL:
1261 requestDial(data, datalen, t);
1263 case RIL_REQUEST_HANGUP:
1264 requestHangup(data, datalen, t);
1266 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1267 // 3GPP 22.030 6.5.5
1268 // "Releases all held calls or sets User Determined User Busy
1269 // (UDUB) for a waiting call."
1270 at_send_command("AT+CHLD=0", NULL);
1272 /* success or failure is ignored by the upper layer here.
1273 it will call GET_CURRENT_CALLS and determine success that way */
1274 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1276 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1277 // 3GPP 22.030 6.5.5
1278 // "Releases all active calls (if any exist) and accepts
1279 // the other (held or waiting) call."
1280 at_send_command("AT+CHLD=1", NULL);
1282 /* success or failure is ignored by the upper layer here.
1283 it will call GET_CURRENT_CALLS and determine success that way */
1284 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1286 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1287 // 3GPP 22.030 6.5.5
1288 // "Places all active calls (if any exist) on hold and accepts
1289 // the other (held or waiting) call."
1290 at_send_command("AT+CHLD=2", NULL);
1292 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1294 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1296 /* success or failure is ignored by the upper layer here.
1297 it will call GET_CURRENT_CALLS and determine success that way */
1298 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1300 case RIL_REQUEST_ANSWER:
1301 at_send_command("ATA", NULL);
1303 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1305 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1307 /* success or failure is ignored by the upper layer here.
1308 it will call GET_CURRENT_CALLS and determine success that way */
1309 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1311 case RIL_REQUEST_CONFERENCE:
1312 // 3GPP 22.030 6.5.5
1313 // "Adds a held call to the conversation"
1314 at_send_command("AT+CHLD=3", NULL);
1316 /* success or failure is ignored by the upper layer here.
1317 it will call GET_CURRENT_CALLS and determine success that way */
1318 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1320 case RIL_REQUEST_UDUB:
1321 /* user determined user busy */
1322 /* sometimes used: ATH */
1323 at_send_command("ATH", NULL);
1325 /* success or failure is ignored by the upper layer here.
1326 it will call GET_CURRENT_CALLS and determine success that way */
1327 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1330 case RIL_REQUEST_SEPARATE_CONNECTION:
1333 int party = ((int*)data)[0];
1335 // Make sure that party is in a valid range.
1336 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1337 // It's sufficient for us to just make sure it's single digit.)
1338 if (party > 0 && party < 10) {
1339 sprintf(cmd, "AT+CHLD=2%d", party);
1340 at_send_command(cmd, NULL);
1341 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1343 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1348 case RIL_REQUEST_SIGNAL_STRENGTH:
1349 requestSignalStrength(data, datalen, t);
1351 case RIL_REQUEST_REGISTRATION_STATE:
1352 case RIL_REQUEST_GPRS_REGISTRATION_STATE:
1353 requestRegistrationState(request, data, datalen, t);
1355 case RIL_REQUEST_OPERATOR:
1356 requestOperator(data, datalen, t);
1358 case RIL_REQUEST_RADIO_POWER:
1359 requestRadioPower(data, datalen, t);
1361 case RIL_REQUEST_DTMF: {
1362 char c = ((char *)data)[0];
1364 asprintf(&cmd, "AT+VTS=%c", (int)c);
1365 at_send_command(cmd, NULL);
1367 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1370 case RIL_REQUEST_SEND_SMS:
1371 requestSendSMS(data, datalen, t);
1373 case RIL_REQUEST_SETUP_DATA_CALL:
1374 requestSetupDataCall(data, datalen, t);
1376 case RIL_REQUEST_SMS_ACKNOWLEDGE:
1377 requestSMSAcknowledge(data, datalen, t);
1380 case RIL_REQUEST_GET_IMSI:
1382 err = at_send_command_numeric("AT+CIMI", &p_response);
1384 if (err < 0 || p_response->success == 0) {
1385 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1387 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1388 p_response->p_intermediates->line, sizeof(char *));
1390 at_response_free(p_response);
1393 case RIL_REQUEST_GET_IMEI:
1395 err = at_send_command_numeric("AT+CGSN", &p_response);
1397 if (err < 0 || p_response->success == 0) {
1398 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1400 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1401 p_response->p_intermediates->line, sizeof(char *));
1403 at_response_free(p_response);
1406 case RIL_REQUEST_SIM_IO:
1407 requestSIM_IO(data,datalen,t);
1410 case RIL_REQUEST_SEND_USSD:
1411 requestSendUSSD(data, datalen, t);
1414 case RIL_REQUEST_CANCEL_USSD:
1416 err = at_send_command_numeric("AT+CUSD=2", &p_response);
1418 if (err < 0 || p_response->success == 0) {
1419 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1421 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1422 p_response->p_intermediates->line, sizeof(char *));
1424 at_response_free(p_response);
1427 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
1428 at_send_command("AT+COPS=0", NULL);
1431 case RIL_REQUEST_DATA_CALL_LIST:
1432 requestDataCallList(data, datalen, t);
1435 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
1436 requestQueryNetworkSelectionMode(data, datalen, t);
1439 case RIL_REQUEST_OEM_HOOK_RAW:
1441 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1445 case RIL_REQUEST_OEM_HOOK_STRINGS: {
1449 LOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
1452 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
1453 i > 0 ; cur++, i --) {
1454 LOGD("> '%s'", *cur);
1457 // echo back strings
1458 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1462 case RIL_REQUEST_WRITE_SMS_TO_SIM:
1463 requestWriteSmsToSim(data, datalen, t);
1466 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
1469 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
1470 err = at_send_command(cmd, &p_response);
1472 if (err < 0 || p_response->success == 0) {
1473 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1475 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1477 at_response_free(p_response);
1481 case RIL_REQUEST_ENTER_SIM_PIN:
1482 case RIL_REQUEST_ENTER_SIM_PUK:
1483 case RIL_REQUEST_ENTER_SIM_PIN2:
1484 case RIL_REQUEST_ENTER_SIM_PUK2:
1485 case RIL_REQUEST_CHANGE_SIM_PIN:
1486 case RIL_REQUEST_CHANGE_SIM_PIN2:
1487 requestEnterSimPin(data, datalen, t);
1491 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1497 * Synchronous call from the RIL to us to return current radio state.
1498 * RADIO_STATE_UNAVAILABLE should be the initial state.
1500 static RIL_RadioState
1506 * Call from RIL to us to find out whether a specific request code
1507 * is supported by this implementation.
1509 * Return 1 for "supported" and 0 for "unsupported"
1513 onSupports (int requestCode)
1520 static void onCancel (RIL_Token t)
1526 static const char * getVersion(void)
1528 return "android reference-ril 1.0";
1532 setRadioState(RIL_RadioState newState)
1534 RIL_RadioState oldState;
1536 pthread_mutex_lock(&s_state_mutex);
1541 // If we're closed, the only reasonable state is
1542 // RADIO_STATE_UNAVAILABLE
1543 // This is here because things on the main thread
1544 // may attempt to change the radio state after the closed
1545 // event happened in another thread
1546 newState = RADIO_STATE_UNAVAILABLE;
1549 if (sState != newState || s_closed > 0) {
1552 pthread_cond_broadcast (&s_state_cond);
1555 pthread_mutex_unlock(&s_state_mutex);
1558 /* do these outside of the mutex */
1559 if (sState != oldState) {
1560 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1563 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
1564 * from the AT reader thread
1565 * Currently, this doesn't happen, but if that changes then these
1566 * will need to be dispatched on the request thread
1568 if (sState == RADIO_STATE_SIM_READY) {
1570 } else if (sState == RADIO_STATE_SIM_NOT_READY) {
1576 /** Returns SIM_NOT_READY on error */
1580 ATResponse *p_response = NULL;
1586 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
1587 ret = SIM_NOT_READY;
1591 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
1594 ret = SIM_NOT_READY;
1598 switch (at_get_cme_error(p_response)) {
1602 case CME_SIM_NOT_INSERTED:
1607 ret = SIM_NOT_READY;
1611 /* CPIN? has succeeded, now look at the result */
1613 cpinLine = p_response->p_intermediates->line;
1614 err = at_tok_start (&cpinLine);
1617 ret = SIM_NOT_READY;
1621 err = at_tok_nextstr(&cpinLine, &cpinResult);
1624 ret = SIM_NOT_READY;
1628 if (0 == strcmp (cpinResult, "SIM PIN")) {
1631 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
1634 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
1635 return SIM_NETWORK_PERSONALIZATION;
1636 } else if (0 != strcmp (cpinResult, "READY")) {
1637 /* we're treating unsupported lock types as "sim absent" */
1642 at_response_free(p_response);
1649 at_response_free(p_response);
1655 * Get the current card status.
1657 * This must be freed using freeCardStatus.
1658 * @return: On success returns RIL_E_SUCCESS
1660 static int getCardStatus(RIL_CardStatus **pp_card_status) {
1661 static RIL_AppStatus app_status_array[] = {
1663 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
1664 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1665 // SIM_NOT_READY = 1
1666 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
1667 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1669 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
1670 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1672 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
1673 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
1675 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
1676 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
1677 // SIM_NETWORK_PERSONALIZATION = 5
1678 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
1679 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
1681 RIL_CardState card_state;
1684 int sim_status = getSIMStatus();
1685 if (sim_status == SIM_ABSENT) {
1686 card_state = RIL_CARDSTATE_ABSENT;
1689 card_state = RIL_CARDSTATE_PRESENT;
1693 // Allocate and initialize base card status.
1694 RIL_CardStatus *p_card_status = malloc(sizeof(RIL_CardStatus));
1695 p_card_status->card_state = card_state;
1696 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
1697 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
1698 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
1699 p_card_status->num_applications = num_apps;
1701 // Initialize application status
1703 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
1704 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
1707 // Pickup the appropriate application status
1708 // that reflects sim_status for gsm.
1709 if (num_apps != 0) {
1710 // Only support one app, gsm
1711 p_card_status->num_applications = 1;
1712 p_card_status->gsm_umts_subscription_app_index = 0;
1714 // Get the correct app status
1715 p_card_status->applications[0] = app_status_array[sim_status];
1718 *pp_card_status = p_card_status;
1719 return RIL_E_SUCCESS;
1723 * Free the card status returned by getCardStatus
1725 static void freeCardStatus(RIL_CardStatus *p_card_status) {
1726 free(p_card_status);
1730 * SIM ready means any commands that access the SIM will work, including:
1731 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
1732 * (all SMS-related commands)
1735 static void pollSIMState (void *param)
1737 ATResponse *p_response;
1740 if (sState != RADIO_STATE_SIM_NOT_READY) {
1741 // no longer valid to poll
1745 switch(getSIMStatus()) {
1749 case SIM_NETWORK_PERSONALIZATION:
1751 setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
1755 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
1759 setRadioState(RADIO_STATE_SIM_READY);
1764 /** returns 1 if on, 0 if off, and -1 on error */
1765 static int isRadioOn()
1767 ATResponse *p_response = NULL;
1772 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
1774 if (err < 0 || p_response->success == 0) {
1775 // assume radio is off
1779 line = p_response->p_intermediates->line;
1781 err = at_tok_start(&line);
1782 if (err < 0) goto error;
1784 err = at_tok_nextbool(&line, &ret);
1785 if (err < 0) goto error;
1787 at_response_free(p_response);
1793 at_response_free(p_response);
1798 * Initialize everything that can be configured while we're still in
1801 static void initializeCallback(void *param)
1803 ATResponse *p_response = NULL;
1806 setRadioState (RADIO_STATE_OFF);
1810 /* note: we don't check errors here. Everything important will
1811 be handled in onATTimeout and onATReaderClosed */
1813 /* atchannel is tolerant of echo but it must */
1814 /* have verbose result codes */
1815 at_send_command("ATE0Q0V1", NULL);
1817 /* No auto-answer */
1818 at_send_command("ATS0=0", NULL);
1820 /* Extended errors */
1821 at_send_command("AT+CMEE=1", NULL);
1823 /* Network registration events */
1824 err = at_send_command("AT+CREG=2", &p_response);
1826 /* some handsets -- in tethered mode -- don't support CREG=2 */
1827 if (err < 0 || p_response->success == 0) {
1828 at_send_command("AT+CREG=1", NULL);
1831 at_response_free(p_response);
1833 /* GPRS registration events */
1834 at_send_command("AT+CGREG=1", NULL);
1836 /* Call Waiting notifications */
1837 at_send_command("AT+CCWA=1", NULL);
1839 /* Alternating voice/data off */
1840 at_send_command("AT+CMOD=0", NULL);
1843 at_send_command("AT+CMUT=0", NULL);
1845 /* +CSSU unsolicited supp service notifications */
1846 at_send_command("AT+CSSN=0,1", NULL);
1848 /* no connected line identification */
1849 at_send_command("AT+COLP=0", NULL);
1851 /* HEX character set */
1852 at_send_command("AT+CSCS=\"HEX\"", NULL);
1854 /* USSD unsolicited */
1855 at_send_command("AT+CUSD=1", NULL);
1857 /* Enable +CGEV GPRS event notifications, but don't buffer */
1858 at_send_command("AT+CGEREP=1,0", NULL);
1861 at_send_command("AT+CMGF=0", NULL);
1863 #ifdef USE_TI_COMMANDS
1865 at_send_command("AT%CPI=3", NULL);
1867 /* TI specific -- notifications when SMS is ready (currently ignored) */
1868 at_send_command("AT%CSTAT=1", NULL);
1870 #endif /* USE_TI_COMMANDS */
1873 /* assume radio is off on error */
1874 if (isRadioOn() > 0) {
1875 setRadioState (RADIO_STATE_SIM_NOT_READY);
1879 static void waitForClose()
1881 pthread_mutex_lock(&s_state_mutex);
1883 while (s_closed == 0) {
1884 pthread_cond_wait(&s_state_cond, &s_state_mutex);
1887 pthread_mutex_unlock(&s_state_mutex);
1891 * Called by atchannel when an unsolicited line appears
1892 * This is called on atchannel's reader thread. AT commands may
1893 * not be issued here
1895 static void onUnsolicited (const char *s, const char *sms_pdu)
1900 /* Ignore unsolicited responses until we're initialized.
1901 * This is OK because the RIL library will poll for initial state
1903 if (sState == RADIO_STATE_UNAVAILABLE) {
1907 if (strStartsWith(s, "%CTZV:")) {
1908 /* TI specific -- NITZ time */
1912 at_tok_start(&line);
1914 err = at_tok_nextstr(&line, &response);
1917 LOGE("invalid NITZ line %s\n", s);
1919 RIL_onUnsolicitedResponse (
1920 RIL_UNSOL_NITZ_TIME_RECEIVED,
1921 response, strlen(response));
1923 } else if (strStartsWith(s,"+CRING:")
1924 || strStartsWith(s,"RING")
1925 || strStartsWith(s,"NO CARRIER")
1926 || strStartsWith(s,"+CCWA")
1928 RIL_onUnsolicitedResponse (
1929 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
1931 #ifdef WORKAROUND_FAKE_CGEV
1932 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
1933 #endif /* WORKAROUND_FAKE_CGEV */
1934 } else if (strStartsWith(s,"+CREG:")
1935 || strStartsWith(s,"+CGREG:")
1937 RIL_onUnsolicitedResponse (
1938 RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
1940 #ifdef WORKAROUND_FAKE_CGEV
1941 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1942 #endif /* WORKAROUND_FAKE_CGEV */
1943 } else if (strStartsWith(s, "+CMT:")) {
1944 RIL_onUnsolicitedResponse (
1945 RIL_UNSOL_RESPONSE_NEW_SMS,
1946 sms_pdu, strlen(sms_pdu));
1947 } else if (strStartsWith(s, "+CDS:")) {
1948 RIL_onUnsolicitedResponse (
1949 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
1950 sms_pdu, strlen(sms_pdu));
1951 } else if (strStartsWith(s, "+CGEV:")) {
1952 /* Really, we can ignore NW CLASS and ME CLASS events here,
1953 * but right now we don't since extranous
1954 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
1956 /* can't issue AT commands here -- call on main thread */
1957 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1958 #ifdef WORKAROUND_FAKE_CGEV
1959 } else if (strStartsWith(s, "+CME ERROR: 150")) {
1960 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1961 #endif /* WORKAROUND_FAKE_CGEV */
1965 /* Called on command or reader thread */
1966 static void onATReaderClosed()
1968 LOGI("AT channel closed\n");
1972 setRadioState (RADIO_STATE_UNAVAILABLE);
1975 /* Called on command thread */
1976 static void onATTimeout()
1978 LOGI("AT channel timeout; closing\n");
1983 /* FIXME cause a radio reset here */
1985 setRadioState (RADIO_STATE_UNAVAILABLE);
1988 static void usage(char *s)
1991 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
1993 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
1999 mainLoop(void *param)
2004 AT_DUMP("== ", "entering mainLoop()", -1 );
2005 at_set_on_reader_closed(onATReaderClosed);
2006 at_set_on_timeout(onATTimeout);
2012 fd = socket_loopback_client(s_port, SOCK_STREAM);
2013 } else if (s_device_socket) {
2014 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
2015 /* Before trying to connect to /dev/socket/qemud (which is
2016 * now another "legacy" way of communicating with the
2017 * emulator), we will try to connecto to gsm service via
2019 fd = qemu_pipe_open("qemud:gsm");
2021 /* Qemu-specific control socket */
2022 fd = socket_local_client( "qemud",
2023 ANDROID_SOCKET_NAMESPACE_RESERVED,
2028 if ( write(fd, "gsm", 3) != 3 ||
2029 read(fd, answer, 2) != 2 ||
2030 memcmp(answer, "OK", 2) != 0)
2039 fd = socket_local_client( s_device_path,
2040 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
2042 } else if (s_device_path != NULL) {
2043 fd = open (s_device_path, O_RDWR);
2044 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
2045 /* disable echo on serial ports */
2047 tcgetattr( fd, &ios );
2048 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
2049 tcsetattr( fd, TCSANOW, &ios );
2054 perror ("opening AT interface. retrying...");
2061 ret = at_open(fd, onUnsolicited);
2064 LOGE ("AT error %d on at_open\n", ret);
2068 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
2070 // Give initializeCallback a chance to dispatched, since
2071 // we don't presently have a cancellation mechanism
2075 LOGI("Re-opening after close");
2081 pthread_t s_tid_mainloop;
2083 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
2088 pthread_attr_t attr;
2092 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
2095 s_port = atoi(optarg);
2100 LOGI("Opening loopback port %d\n", s_port);
2104 s_device_path = optarg;
2105 LOGI("Opening tty device %s\n", s_device_path);
2109 s_device_path = optarg;
2110 s_device_socket = 1;
2111 LOGI("Opening socket %s\n", s_device_path);
2120 if (s_port < 0 && s_device_path == NULL) {
2125 pthread_attr_init (&attr);
2126 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2127 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
2129 return &s_callbacks;
2131 #else /* RIL_SHLIB */
2132 int main (int argc, char **argv)
2138 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
2141 s_port = atoi(optarg);
2145 LOGI("Opening loopback port %d\n", s_port);
2149 s_device_path = optarg;
2150 LOGI("Opening tty device %s\n", s_device_path);
2154 s_device_path = optarg;
2155 s_device_socket = 1;
2156 LOGI("Opening socket %s\n", s_device_path);
2164 if (s_port < 0 && s_device_path == NULL) {
2168 RIL_register(&s_callbacks);
2175 #endif /* RIL_SHLIB */