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>
38 #include <utils/Log.h>
40 #define MAX_AT_RESPONSE 0x1000
42 /* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
43 #define PPP_TTY_PATH "/dev/omap_csmi_tty1"
45 #ifdef USE_TI_COMMANDS
47 // Enable a workaround
48 // 1) Make incoming call, do not answer
49 // 2) Hangup remote end
50 // Expected: call should disappear from CLCC line
51 // Actual: Call shows as "ACTIVE" before disappearing
52 #define WORKAROUND_ERRONEOUS_ANSWER 1
54 // Some varients of the TI stack do not support the +CGEV unsolicited
55 // response. However, they seem to send an unsolicited +CME ERROR: 150
56 #define WORKAROUND_FAKE_CGEV 1
62 SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
65 SIM_NETWORK_PERSONALIZATION = 5
68 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
69 static RIL_RadioState currentState();
70 static int onSupports (int requestCode);
71 static void onCancel (RIL_Token t);
72 static const char *getVersion();
73 static int isRadioOn();
74 static SIM_Status getSIMStatus();
75 static int getCardStatus(RIL_CardStatus **pp_card_status);
76 static void freeCardStatus(RIL_CardStatus *p_card_status);
77 static void onDataCallListChanged(void *param);
79 extern const char * requestToString(int request);
81 /*** Static Variables ***/
82 static const RIL_RadioFunctions s_callbacks = {
92 static const struct RIL_Env *s_rilenv;
94 #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
95 #define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
96 #define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
99 static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
101 static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
102 static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
104 static int s_port = -1;
105 static const char * s_device_path = NULL;
106 static int s_device_socket = 0;
108 /* trigger change to this with s_state_cond */
109 static int s_closed = 0;
111 static int sFD; /* file desc of AT channel */
112 static char sATBuffer[MAX_AT_RESPONSE+1];
113 static char *sATBufferCur = NULL;
115 static const struct timeval TIMEVAL_SIMPOLL = {1,0};
116 static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
117 static const struct timeval TIMEVAL_0 = {0,0};
119 #ifdef WORKAROUND_ERRONEOUS_ANSWER
120 // Max number of times we'll try to repoll when we think
121 // we have a AT+CLCC race condition
122 #define REPOLL_CALLS_COUNT_MAX 4
124 // Line index that was incoming or waiting at last poll, or -1 for none
125 static int s_incomingOrWaitingLine = -1;
126 // Number of times we've asked for a repoll of AT+CLCC
127 static int s_repollCallsCount = 0;
128 // Should we expect a call to be answered in the next CLCC?
129 static int s_expectAnswer = 0;
130 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
132 static void pollSIMState (void *param);
133 static void setRadioState(RIL_RadioState newState);
135 static int clccStateToRILState(int state, RIL_CallState *p_state)
139 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
140 case 1: *p_state = RIL_CALL_HOLDING; return 0;
141 case 2: *p_state = RIL_CALL_DIALING; return 0;
142 case 3: *p_state = RIL_CALL_ALERTING; return 0;
143 case 4: *p_state = RIL_CALL_INCOMING; return 0;
144 case 5: *p_state = RIL_CALL_WAITING; return 0;
150 * Note: directly modified line and has *p_call point directly into
153 static int callFromCLCCLine(char *line, RIL_Call *p_call)
155 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
156 // index,isMT,state,mode,isMpty(,number,TOA)?
162 err = at_tok_start(&line);
163 if (err < 0) goto error;
165 err = at_tok_nextint(&line, &(p_call->index));
166 if (err < 0) goto error;
168 err = at_tok_nextbool(&line, &(p_call->isMT));
169 if (err < 0) goto error;
171 err = at_tok_nextint(&line, &state);
172 if (err < 0) goto error;
174 err = clccStateToRILState(state, &(p_call->state));
175 if (err < 0) goto error;
177 err = at_tok_nextint(&line, &mode);
178 if (err < 0) goto error;
180 p_call->isVoice = (mode == 0);
182 err = at_tok_nextbool(&line, &(p_call->isMpty));
183 if (err < 0) goto error;
185 if (at_tok_hasmore(&line)) {
186 err = at_tok_nextstr(&line, &(p_call->number));
188 /* tolerate null here */
189 if (err < 0) return 0;
191 // Some lame implementations return strings
192 // like "NOT AVAILABLE" in the CLCC line
193 if (p_call->number != NULL
194 && 0 == strspn(p_call->number, "+0123456789")
196 p_call->number = NULL;
199 err = at_tok_nextint(&line, &p_call->toa);
200 if (err < 0) goto error;
203 p_call->uusInfo = NULL;
208 LOGE("invalid CLCC line\n");
213 /** do post-AT+CFUN=1 initialization */
214 static void onRadioPowerOn()
216 #ifdef USE_TI_COMMANDS
217 /* Must be after CFUN=1 */
218 /* TI specific -- notifications for CPHS things such */
219 /* as CPHS message waiting indicator */
221 at_send_command("AT%CPHS=1", NULL);
223 /* TI specific -- enable NITZ unsol notifs */
224 at_send_command("AT%CTZV=1", NULL);
230 /** do post- SIM ready initialization */
231 static void onSIMReady()
233 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
235 * Always send SMS messages directly to the TE
237 * mode = 1 // discard when link is reserved (link should never be
239 * mt = 2 // most messages routed to TE
240 * bm = 2 // new cell BM's routed to TE
241 * ds = 1 // Status reports routed to TE
242 * bfr = 1 // flush buffer
244 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
247 static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
252 ATResponse *p_response = NULL;
254 assert (datalen >= sizeof(int *));
255 onOff = ((int *)data)[0];
257 if (onOff == 0 && sState != RADIO_STATE_OFF) {
258 err = at_send_command("AT+CFUN=0", &p_response);
259 if (err < 0 || p_response->success == 0) goto error;
260 setRadioState(RADIO_STATE_OFF);
261 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
262 err = at_send_command("AT+CFUN=1", &p_response);
263 if (err < 0|| p_response->success == 0) {
264 // Some stacks return an error when there is no SIM,
265 // but they really turn the RF portion on
266 // So, if we get an error, let's check to see if it
269 if (isRadioOn() != 1) {
273 setRadioState(RADIO_STATE_SIM_NOT_READY);
276 at_response_free(p_response);
277 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
280 at_response_free(p_response);
281 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
284 static void requestOrSendDataCallList(RIL_Token *t);
286 static void onDataCallListChanged(void *param)
288 requestOrSendDataCallList(NULL);
291 static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
293 requestOrSendDataCallList(&t);
296 static void requestOrSendDataCallList(RIL_Token *t)
298 ATResponse *p_response;
304 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
305 if (err != 0 || p_response->success == 0) {
307 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
309 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
314 for (p_cur = p_response->p_intermediates; p_cur != NULL;
315 p_cur = p_cur->p_next)
318 RIL_Data_Call_Response *responses =
319 alloca(n * sizeof(RIL_Data_Call_Response));
322 for (i = 0; i < n; i++) {
323 responses[i].cid = -1;
324 responses[i].active = -1;
325 responses[i].type = "";
326 responses[i].apn = "";
327 responses[i].address = "";
330 RIL_Data_Call_Response *response = responses;
331 for (p_cur = p_response->p_intermediates; p_cur != NULL;
332 p_cur = p_cur->p_next) {
333 char *line = p_cur->line;
335 err = at_tok_start(&line);
339 err = at_tok_nextint(&line, &response->cid);
343 err = at_tok_nextint(&line, &response->active);
350 at_response_free(p_response);
352 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
353 if (err != 0 || p_response->success == 0) {
355 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
357 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
362 for (p_cur = p_response->p_intermediates; p_cur != NULL;
363 p_cur = p_cur->p_next) {
364 char *line = p_cur->line;
371 err = at_tok_start(&line);
375 err = at_tok_nextint(&line, &cid);
379 for (i = 0; i < n; i++) {
380 if (responses[i].cid == cid)
385 /* details for a context we didn't hear about in the last request */
389 err = at_tok_nextstr(&line, &out);
393 responses[i].type = alloca(strlen(out) + 1);
394 strcpy(responses[i].type, out);
396 err = at_tok_nextstr(&line, &out);
400 responses[i].apn = alloca(strlen(out) + 1);
401 strcpy(responses[i].apn, out);
403 err = at_tok_nextstr(&line, &out);
407 responses[i].address = alloca(strlen(out) + 1);
408 strcpy(responses[i].address, out);
411 at_response_free(p_response);
414 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
415 n * sizeof(RIL_Data_Call_Response));
417 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
419 n * sizeof(RIL_Data_Call_Response));
425 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
427 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
430 at_response_free(p_response);
433 static void requestQueryNetworkSelectionMode(
434 void *data, size_t datalen, RIL_Token t)
437 ATResponse *p_response = NULL;
441 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
443 if (err < 0 || p_response->success == 0) {
447 line = p_response->p_intermediates->line;
449 err = at_tok_start(&line);
455 err = at_tok_nextint(&line, &response);
461 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
462 at_response_free(p_response);
465 at_response_free(p_response);
466 LOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
467 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
470 static void sendCallStateChanged(void *param)
472 RIL_onUnsolicitedResponse (
473 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
477 static void requestGetCurrentCalls(void *data, size_t datalen, RIL_Token t)
480 ATResponse *p_response;
489 #ifdef WORKAROUND_ERRONEOUS_ANSWER
490 int prevIncomingOrWaitingLine;
492 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
493 s_incomingOrWaitingLine = -1;
494 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
496 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
498 if (err != 0 || p_response->success == 0) {
499 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
503 /* count the calls */
504 for (countCalls = 0, p_cur = p_response->p_intermediates
506 ; p_cur = p_cur->p_next
511 /* yes, there's an array of pointers and then an array of structures */
513 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
514 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
515 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
517 /* init the pointer array */
518 for(i = 0; i < countCalls ; i++) {
519 pp_calls[i] = &(p_calls[i]);
522 for (countValidCalls = 0, p_cur = p_response->p_intermediates
524 ; p_cur = p_cur->p_next
526 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
532 #ifdef WORKAROUND_ERRONEOUS_ANSWER
533 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
534 || p_calls[countValidCalls].state == RIL_CALL_WAITING
536 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
538 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
540 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
541 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
549 #ifdef WORKAROUND_ERRONEOUS_ANSWER
551 // A call was incoming or waiting
552 // Now it's marked as active
553 // But we never answered it
555 // This is probably a bug, and the call will probably
556 // disappear from the call list in the next poll
557 if (prevIncomingOrWaitingLine >= 0
558 && s_incomingOrWaitingLine < 0
559 && s_expectAnswer == 0
561 for (i = 0; i < countValidCalls ; i++) {
563 if (p_calls[i].index == prevIncomingOrWaitingLine
564 && p_calls[i].state == RIL_CALL_ACTIVE
565 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
568 "Hit WORKAROUND_ERRONOUS_ANSWER case."
569 " Repoll count: %d\n", s_repollCallsCount);
570 s_repollCallsCount++;
577 s_repollCallsCount = 0;
578 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
580 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
581 countValidCalls * sizeof (RIL_Call *));
583 at_response_free(p_response);
585 #ifdef POLL_CALL_STATE
586 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
587 // smd, so we're forced to poll until the call ends.
591 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
596 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
597 at_response_free(p_response);
600 static void requestDial(void *data, size_t datalen, RIL_Token t)
607 p_dial = (RIL_Dial *)data;
609 switch (p_dial->clir) {
610 case 1: clir = "I"; break; /*invocation*/
611 case 2: clir = "i"; break; /*suppression*/
613 case 0: clir = ""; break; /*subscription default*/
616 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
618 ret = at_send_command(cmd, NULL);
622 /* success or failure is ignored by the upper layer here.
623 it will call GET_CURRENT_CALLS and determine success that way */
624 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
627 static void requestWriteSmsToSim(void *data, size_t datalen, RIL_Token t)
629 RIL_SMS_WriteArgs *p_args;
633 ATResponse *p_response = NULL;
635 p_args = (RIL_SMS_WriteArgs *)data;
637 length = strlen(p_args->pdu)/2;
638 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
640 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
642 if (err != 0 || p_response->success == 0) goto error;
644 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
645 at_response_free(p_response);
649 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
650 at_response_free(p_response);
653 static void requestHangup(void *data, size_t datalen, RIL_Token t)
660 p_line = (int *)data;
663 // "Releases a specific active call X"
664 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
666 ret = at_send_command(cmd, NULL);
670 /* success or failure is ignored by the upper layer here.
671 it will call GET_CURRENT_CALLS and determine success that way */
672 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
675 static void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
677 ATResponse *p_response = NULL;
682 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
684 if (err < 0 || p_response->success == 0) {
685 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
689 line = p_response->p_intermediates->line;
691 err = at_tok_start(&line);
692 if (err < 0) goto error;
694 err = at_tok_nextint(&line, &(response[0]));
695 if (err < 0) goto error;
697 err = at_tok_nextint(&line, &(response[1]));
698 if (err < 0) goto error;
700 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
702 at_response_free(p_response);
706 LOGE("requestSignalStrength must never return an error when radio is on");
707 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
708 at_response_free(p_response);
711 static void requestRegistrationState(int request, void *data,
712 size_t datalen, RIL_Token t)
716 char * responseStr[4];
717 ATResponse *p_response = NULL;
726 if (request == RIL_REQUEST_REGISTRATION_STATE) {
729 } else if (request == RIL_REQUEST_GPRS_REGISTRATION_STATE) {
737 err = at_send_command_singleline(cmd, prefix, &p_response);
739 if (err != 0) goto error;
741 line = p_response->p_intermediates->line;
743 err = at_tok_start(&line);
744 if (err < 0) goto error;
746 /* Ok you have to be careful here
747 * The solicited version of the CREG response is
748 * +CREG: n, stat, [lac, cid]
749 * and the unsolicited version is
750 * +CREG: stat, [lac, cid]
751 * The <n> parameter is basically "is unsolicited creg on?"
752 * which it should always be
754 * Now we should normally get the solicited version here,
755 * but the unsolicited version could have snuck in
756 * so we have to handle both
758 * Also since the LAC and CID are only reported when registered,
759 * we can have 1, 2, 3, or 4 arguments here
761 * finally, a +CGREG: answer may have a fifth value that corresponds
762 * to the network type, as in;
764 * +CGREG: n, stat [,lac, cid [,networkType]]
767 /* count number of commas */
769 for (p = line ; *p != '\0' ;p++) {
770 if (*p == ',') commas++;
774 case 0: /* +CREG: <stat> */
775 err = at_tok_nextint(&line, &response[0]);
776 if (err < 0) goto error;
781 case 1: /* +CREG: <n>, <stat> */
782 err = at_tok_nextint(&line, &skip);
783 if (err < 0) goto error;
784 err = at_tok_nextint(&line, &response[0]);
785 if (err < 0) goto error;
788 if (err < 0) goto error;
791 case 2: /* +CREG: <stat>, <lac>, <cid> */
792 err = at_tok_nextint(&line, &response[0]);
793 if (err < 0) goto error;
794 err = at_tok_nexthexint(&line, &response[1]);
795 if (err < 0) goto error;
796 err = at_tok_nexthexint(&line, &response[2]);
797 if (err < 0) goto error;
799 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
800 err = at_tok_nextint(&line, &skip);
801 if (err < 0) goto error;
802 err = at_tok_nextint(&line, &response[0]);
803 if (err < 0) goto error;
804 err = at_tok_nexthexint(&line, &response[1]);
805 if (err < 0) goto error;
806 err = at_tok_nexthexint(&line, &response[2]);
807 if (err < 0) goto error;
809 /* special case for CGREG, there is a fourth parameter
810 * that is the network type (unknown/gprs/edge/umts)
812 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
813 err = at_tok_nextint(&line, &skip);
814 if (err < 0) goto error;
815 err = at_tok_nextint(&line, &response[0]);
816 if (err < 0) goto error;
817 err = at_tok_nexthexint(&line, &response[1]);
818 if (err < 0) goto error;
819 err = at_tok_nexthexint(&line, &response[2]);
820 if (err < 0) goto error;
821 err = at_tok_nexthexint(&line, &response[3]);
822 if (err < 0) goto error;
829 asprintf(&responseStr[0], "%d", response[0]);
830 asprintf(&responseStr[1], "%x", response[1]);
831 asprintf(&responseStr[2], "%x", response[2]);
834 asprintf(&responseStr[3], "%d", response[3]);
836 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
837 at_response_free(p_response);
841 LOGE("requestRegistrationState must never return an error when radio is on");
842 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
843 at_response_free(p_response);
846 static void requestOperator(void *data, size_t datalen, RIL_Token t)
854 memset(response, 0, sizeof(response));
856 ATResponse *p_response = NULL;
858 err = at_send_command_multiline(
859 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
860 "+COPS:", &p_response);
862 /* we expect 3 lines here:
863 * +COPS: 0,0,"T - Mobile"
865 * +COPS: 0,2,"310170"
868 if (err != 0) goto error;
870 for (i = 0, p_cur = p_response->p_intermediates
872 ; p_cur = p_cur->p_next, i++
874 char *line = p_cur->line;
876 err = at_tok_start(&line);
877 if (err < 0) goto error;
879 err = at_tok_nextint(&line, &skip);
880 if (err < 0) goto error;
882 // If we're unregistered, we may just get
883 // a "+COPS: 0" response
884 if (!at_tok_hasmore(&line)) {
889 err = at_tok_nextint(&line, &skip);
890 if (err < 0) goto error;
892 // a "+COPS: 0, n" response is also possible
893 if (!at_tok_hasmore(&line)) {
898 err = at_tok_nextstr(&line, &(response[i]));
899 if (err < 0) goto error;
903 /* expect 3 lines exactly */
907 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
908 at_response_free(p_response);
912 LOGE("requestOperator must not return error when radio is on");
913 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
914 at_response_free(p_response);
917 static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
924 RIL_SMS_Response response;
925 ATResponse *p_response = NULL;
927 smsc = ((const char **)data)[0];
928 pdu = ((const char **)data)[1];
930 tpLayerLength = strlen(pdu)/2;
932 // "NULL for default SMSC"
937 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
938 asprintf(&cmd2, "%s%s", smsc, pdu);
940 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
942 if (err != 0 || p_response->success == 0) goto error;
944 memset(&response, 0, sizeof(response));
946 /* FIXME fill in messageRef and ackPDU */
948 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
949 at_response_free(p_response);
953 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
954 at_response_free(p_response);
957 static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
962 ATResponse *p_response = NULL;
963 char *response[2] = { "1", PPP_TTY_PATH };
965 apn = ((const char **)data)[2];
967 #ifdef USE_TI_COMMANDS
968 // Config for multislot class 10 (probably default anyway eh?)
969 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
972 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
973 #endif /* USE_TI_COMMANDS */
978 ssize_t written, rlen;
979 char status[32] = {0};
982 LOGD("requesting data connection to APN '%s'", apn);
984 fd = open ("/dev/qmi", O_RDWR);
985 if (fd >= 0) { /* the device doesn't exist on the emulator */
987 LOGD("opened the qmi device\n");
988 asprintf(&cmd, "up:%s", apn);
993 written = write (fd, cmd + cur, len - cur);
994 } while (written < 0 && errno == EINTR);
997 LOGE("### ERROR writing to /dev/qmi");
1005 // wait for interface to come online
1010 rlen = read(fd, status, 31);
1011 } while (rlen < 0 && errno == EINTR);
1014 LOGE("### ERROR reading from /dev/qmi");
1018 status[rlen] = '\0';
1019 LOGD("### status: %s", status);
1021 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1026 LOGE("### Failed to get data connection up\n");
1030 qmistatus = system("netcfg rmnet0 dhcp");
1032 LOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1034 if (qmistatus < 0) goto error;
1038 asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
1039 //FIXME check for error here
1040 err = at_send_command(cmd, NULL);
1043 // Set required QoS params to default
1044 err = at_send_command("AT+CGQREQ=1", NULL);
1046 // Set minimum QoS params to default
1047 err = at_send_command("AT+CGQMIN=1", NULL);
1049 // packet-domain event reporting
1050 err = at_send_command("AT+CGEREP=1,0", NULL);
1052 // Hangup anything that's happening there now
1053 err = at_send_command("AT+CGACT=1,0", NULL);
1055 // Start data on PDP context 1
1056 err = at_send_command("ATD*99***1#", &p_response);
1058 if (err < 0 || p_response->success == 0) {
1063 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1064 at_response_free(p_response);
1068 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1069 at_response_free(p_response);
1073 static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1078 ackSuccess = ((int *)data)[0];
1080 if (ackSuccess == 1) {
1081 err = at_send_command("AT+CNMA=1", NULL);
1082 } else if (ackSuccess == 0) {
1083 err = at_send_command("AT+CNMA=2", NULL);
1085 LOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1089 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1091 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1095 static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1097 ATResponse *p_response = NULL;
1098 RIL_SIM_IO_Response sr;
1104 memset(&sr, 0, sizeof(sr));
1106 p_args = (RIL_SIM_IO *)data;
1108 /* FIXME handle pin2 */
1110 if (p_args->data == NULL) {
1111 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1112 p_args->command, p_args->fileid,
1113 p_args->p1, p_args->p2, p_args->p3);
1115 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1116 p_args->command, p_args->fileid,
1117 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1120 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1122 if (err < 0 || p_response->success == 0) {
1126 line = p_response->p_intermediates->line;
1128 err = at_tok_start(&line);
1129 if (err < 0) goto error;
1131 err = at_tok_nextint(&line, &(sr.sw1));
1132 if (err < 0) goto error;
1134 err = at_tok_nextint(&line, &(sr.sw2));
1135 if (err < 0) goto error;
1137 if (at_tok_hasmore(&line)) {
1138 err = at_tok_nextstr(&line, &(sr.simResponse));
1139 if (err < 0) goto error;
1142 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1143 at_response_free(p_response);
1148 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1149 at_response_free(p_response);
1154 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1156 ATResponse *p_response = NULL;
1159 const char** strings = (const char**)data;;
1161 if ( datalen == sizeof(char*) ) {
1162 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1163 } else if ( datalen == 2*sizeof(char*) ) {
1164 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1168 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1171 if (err < 0 || p_response->success == 0) {
1173 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1175 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1177 at_response_free(p_response);
1181 static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1183 const char *ussdRequest;
1185 ussdRequest = (char *)(data);
1188 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1195 /*** Callback methods from the RIL library to us ***/
1198 * Call from RIL to us to make a RIL_REQUEST
1200 * Must be completed with a call to RIL_onRequestComplete()
1202 * RIL_onRequestComplete() may be called from any thread, before or after
1203 * this function returns.
1205 * Will always be called from the same thread, so returning here implies
1206 * that the radio is ready to process another command (whether or not
1207 * the previous command has completed).
1210 onRequest (int request, void *data, size_t datalen, RIL_Token t)
1212 ATResponse *p_response;
1215 LOGD("onRequest: %s", requestToString(request));
1217 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1218 * when RADIO_STATE_UNAVAILABLE.
1220 if (sState == RADIO_STATE_UNAVAILABLE
1221 && request != RIL_REQUEST_GET_SIM_STATUS
1223 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1227 /* Ignore all non-power requests when RADIO_STATE_OFF
1228 * (except RIL_REQUEST_GET_SIM_STATUS)
1230 if (sState == RADIO_STATE_OFF
1231 && !(request == RIL_REQUEST_RADIO_POWER
1232 || request == RIL_REQUEST_GET_SIM_STATUS)
1234 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1239 case RIL_REQUEST_GET_SIM_STATUS: {
1240 RIL_CardStatus *p_card_status;
1244 int result = getCardStatus(&p_card_status);
1245 if (result == RIL_E_SUCCESS) {
1246 p_buffer = (char *)p_card_status;
1247 buffer_size = sizeof(*p_card_status);
1252 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
1253 freeCardStatus(p_card_status);
1256 case RIL_REQUEST_GET_CURRENT_CALLS:
1257 requestGetCurrentCalls(data, datalen, t);
1259 case RIL_REQUEST_DIAL:
1260 requestDial(data, datalen, t);
1262 case RIL_REQUEST_HANGUP:
1263 requestHangup(data, datalen, t);
1265 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1266 // 3GPP 22.030 6.5.5
1267 // "Releases all held calls or sets User Determined User Busy
1268 // (UDUB) for a waiting call."
1269 at_send_command("AT+CHLD=0", NULL);
1271 /* success or failure is ignored by the upper layer here.
1272 it will call GET_CURRENT_CALLS and determine success that way */
1273 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1275 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1276 // 3GPP 22.030 6.5.5
1277 // "Releases all active calls (if any exist) and accepts
1278 // the other (held or waiting) call."
1279 at_send_command("AT+CHLD=1", NULL);
1281 /* success or failure is ignored by the upper layer here.
1282 it will call GET_CURRENT_CALLS and determine success that way */
1283 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1285 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1286 // 3GPP 22.030 6.5.5
1287 // "Places all active calls (if any exist) on hold and accepts
1288 // the other (held or waiting) call."
1289 at_send_command("AT+CHLD=2", NULL);
1291 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1293 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1295 /* success or failure is ignored by the upper layer here.
1296 it will call GET_CURRENT_CALLS and determine success that way */
1297 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1299 case RIL_REQUEST_ANSWER:
1300 at_send_command("ATA", NULL);
1302 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1304 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1306 /* success or failure is ignored by the upper layer here.
1307 it will call GET_CURRENT_CALLS and determine success that way */
1308 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1310 case RIL_REQUEST_CONFERENCE:
1311 // 3GPP 22.030 6.5.5
1312 // "Adds a held call to the conversation"
1313 at_send_command("AT+CHLD=3", NULL);
1315 /* success or failure is ignored by the upper layer here.
1316 it will call GET_CURRENT_CALLS and determine success that way */
1317 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1319 case RIL_REQUEST_UDUB:
1320 /* user determined user busy */
1321 /* sometimes used: ATH */
1322 at_send_command("ATH", NULL);
1324 /* success or failure is ignored by the upper layer here.
1325 it will call GET_CURRENT_CALLS and determine success that way */
1326 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1329 case RIL_REQUEST_SEPARATE_CONNECTION:
1332 int party = ((int*)data)[0];
1334 // Make sure that party is in a valid range.
1335 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1336 // It's sufficient for us to just make sure it's single digit.)
1337 if (party > 0 && party < 10) {
1338 sprintf(cmd, "AT+CHLD=2%d", party);
1339 at_send_command(cmd, NULL);
1340 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1342 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1347 case RIL_REQUEST_SIGNAL_STRENGTH:
1348 requestSignalStrength(data, datalen, t);
1350 case RIL_REQUEST_REGISTRATION_STATE:
1351 case RIL_REQUEST_GPRS_REGISTRATION_STATE:
1352 requestRegistrationState(request, data, datalen, t);
1354 case RIL_REQUEST_OPERATOR:
1355 requestOperator(data, datalen, t);
1357 case RIL_REQUEST_RADIO_POWER:
1358 requestRadioPower(data, datalen, t);
1360 case RIL_REQUEST_DTMF: {
1361 char c = ((char *)data)[0];
1363 asprintf(&cmd, "AT+VTS=%c", (int)c);
1364 at_send_command(cmd, NULL);
1366 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1369 case RIL_REQUEST_SEND_SMS:
1370 requestSendSMS(data, datalen, t);
1372 case RIL_REQUEST_SETUP_DATA_CALL:
1373 requestSetupDataCall(data, datalen, t);
1375 case RIL_REQUEST_SMS_ACKNOWLEDGE:
1376 requestSMSAcknowledge(data, datalen, t);
1379 case RIL_REQUEST_GET_IMSI:
1381 err = at_send_command_numeric("AT+CIMI", &p_response);
1383 if (err < 0 || p_response->success == 0) {
1384 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1386 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1387 p_response->p_intermediates->line, sizeof(char *));
1389 at_response_free(p_response);
1392 case RIL_REQUEST_GET_IMEI:
1394 err = at_send_command_numeric("AT+CGSN", &p_response);
1396 if (err < 0 || p_response->success == 0) {
1397 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1399 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1400 p_response->p_intermediates->line, sizeof(char *));
1402 at_response_free(p_response);
1405 case RIL_REQUEST_SIM_IO:
1406 requestSIM_IO(data,datalen,t);
1409 case RIL_REQUEST_SEND_USSD:
1410 requestSendUSSD(data, datalen, t);
1413 case RIL_REQUEST_CANCEL_USSD:
1415 err = at_send_command_numeric("AT+CUSD=2", &p_response);
1417 if (err < 0 || p_response->success == 0) {
1418 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1420 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1421 p_response->p_intermediates->line, sizeof(char *));
1423 at_response_free(p_response);
1426 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
1427 at_send_command("AT+COPS=0", NULL);
1430 case RIL_REQUEST_DATA_CALL_LIST:
1431 requestDataCallList(data, datalen, t);
1434 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
1435 requestQueryNetworkSelectionMode(data, datalen, t);
1438 case RIL_REQUEST_OEM_HOOK_RAW:
1440 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1444 case RIL_REQUEST_OEM_HOOK_STRINGS: {
1448 LOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
1451 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
1452 i > 0 ; cur++, i --) {
1453 LOGD("> '%s'", *cur);
1456 // echo back strings
1457 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1461 case RIL_REQUEST_WRITE_SMS_TO_SIM:
1462 requestWriteSmsToSim(data, datalen, t);
1465 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
1468 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
1469 err = at_send_command(cmd, &p_response);
1471 if (err < 0 || p_response->success == 0) {
1472 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1474 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1476 at_response_free(p_response);
1480 case RIL_REQUEST_ENTER_SIM_PIN:
1481 case RIL_REQUEST_ENTER_SIM_PUK:
1482 case RIL_REQUEST_ENTER_SIM_PIN2:
1483 case RIL_REQUEST_ENTER_SIM_PUK2:
1484 case RIL_REQUEST_CHANGE_SIM_PIN:
1485 case RIL_REQUEST_CHANGE_SIM_PIN2:
1486 requestEnterSimPin(data, datalen, t);
1490 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1496 * Synchronous call from the RIL to us to return current radio state.
1497 * RADIO_STATE_UNAVAILABLE should be the initial state.
1499 static RIL_RadioState
1505 * Call from RIL to us to find out whether a specific request code
1506 * is supported by this implementation.
1508 * Return 1 for "supported" and 0 for "unsupported"
1512 onSupports (int requestCode)
1519 static void onCancel (RIL_Token t)
1525 static const char * getVersion(void)
1527 return "android reference-ril 1.0";
1531 setRadioState(RIL_RadioState newState)
1533 RIL_RadioState oldState;
1535 pthread_mutex_lock(&s_state_mutex);
1540 // If we're closed, the only reasonable state is
1541 // RADIO_STATE_UNAVAILABLE
1542 // This is here because things on the main thread
1543 // may attempt to change the radio state after the closed
1544 // event happened in another thread
1545 newState = RADIO_STATE_UNAVAILABLE;
1548 if (sState != newState || s_closed > 0) {
1551 pthread_cond_broadcast (&s_state_cond);
1554 pthread_mutex_unlock(&s_state_mutex);
1557 /* do these outside of the mutex */
1558 if (sState != oldState) {
1559 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1562 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
1563 * from the AT reader thread
1564 * Currently, this doesn't happen, but if that changes then these
1565 * will need to be dispatched on the request thread
1567 if (sState == RADIO_STATE_SIM_READY) {
1569 } else if (sState == RADIO_STATE_SIM_NOT_READY) {
1575 /** Returns SIM_NOT_READY on error */
1579 ATResponse *p_response = NULL;
1585 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
1586 ret = SIM_NOT_READY;
1590 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
1593 ret = SIM_NOT_READY;
1597 switch (at_get_cme_error(p_response)) {
1601 case CME_SIM_NOT_INSERTED:
1606 ret = SIM_NOT_READY;
1610 /* CPIN? has succeeded, now look at the result */
1612 cpinLine = p_response->p_intermediates->line;
1613 err = at_tok_start (&cpinLine);
1616 ret = SIM_NOT_READY;
1620 err = at_tok_nextstr(&cpinLine, &cpinResult);
1623 ret = SIM_NOT_READY;
1627 if (0 == strcmp (cpinResult, "SIM PIN")) {
1630 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
1633 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
1634 return SIM_NETWORK_PERSONALIZATION;
1635 } else if (0 != strcmp (cpinResult, "READY")) {
1636 /* we're treating unsupported lock types as "sim absent" */
1641 at_response_free(p_response);
1648 at_response_free(p_response);
1654 * Get the current card status.
1656 * This must be freed using freeCardStatus.
1657 * @return: On success returns RIL_E_SUCCESS
1659 static int getCardStatus(RIL_CardStatus **pp_card_status) {
1660 static RIL_AppStatus app_status_array[] = {
1662 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
1663 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1664 // SIM_NOT_READY = 1
1665 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
1666 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1668 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
1669 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1671 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
1672 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
1674 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
1675 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
1676 // SIM_NETWORK_PERSONALIZATION = 5
1677 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
1678 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
1680 RIL_CardState card_state;
1683 int sim_status = getSIMStatus();
1684 if (sim_status == SIM_ABSENT) {
1685 card_state = RIL_CARDSTATE_ABSENT;
1688 card_state = RIL_CARDSTATE_PRESENT;
1692 // Allocate and initialize base card status.
1693 RIL_CardStatus *p_card_status = malloc(sizeof(RIL_CardStatus));
1694 p_card_status->card_state = card_state;
1695 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
1696 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
1697 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
1698 p_card_status->num_applications = num_apps;
1700 // Initialize application status
1702 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
1703 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
1706 // Pickup the appropriate application status
1707 // that reflects sim_status for gsm.
1708 if (num_apps != 0) {
1709 // Only support one app, gsm
1710 p_card_status->num_applications = 1;
1711 p_card_status->gsm_umts_subscription_app_index = 0;
1713 // Get the correct app status
1714 p_card_status->applications[0] = app_status_array[sim_status];
1717 *pp_card_status = p_card_status;
1718 return RIL_E_SUCCESS;
1722 * Free the card status returned by getCardStatus
1724 static void freeCardStatus(RIL_CardStatus *p_card_status) {
1725 free(p_card_status);
1729 * SIM ready means any commands that access the SIM will work, including:
1730 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
1731 * (all SMS-related commands)
1734 static void pollSIMState (void *param)
1736 ATResponse *p_response;
1739 if (sState != RADIO_STATE_SIM_NOT_READY) {
1740 // no longer valid to poll
1744 switch(getSIMStatus()) {
1748 case SIM_NETWORK_PERSONALIZATION:
1750 setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
1754 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
1758 setRadioState(RADIO_STATE_SIM_READY);
1763 /** returns 1 if on, 0 if off, and -1 on error */
1764 static int isRadioOn()
1766 ATResponse *p_response = NULL;
1771 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
1773 if (err < 0 || p_response->success == 0) {
1774 // assume radio is off
1778 line = p_response->p_intermediates->line;
1780 err = at_tok_start(&line);
1781 if (err < 0) goto error;
1783 err = at_tok_nextbool(&line, &ret);
1784 if (err < 0) goto error;
1786 at_response_free(p_response);
1792 at_response_free(p_response);
1797 * Initialize everything that can be configured while we're still in
1800 static void initializeCallback(void *param)
1802 ATResponse *p_response = NULL;
1805 setRadioState (RADIO_STATE_OFF);
1809 /* note: we don't check errors here. Everything important will
1810 be handled in onATTimeout and onATReaderClosed */
1812 /* atchannel is tolerant of echo but it must */
1813 /* have verbose result codes */
1814 at_send_command("ATE0Q0V1", NULL);
1816 /* No auto-answer */
1817 at_send_command("ATS0=0", NULL);
1819 /* Extended errors */
1820 at_send_command("AT+CMEE=1", NULL);
1822 /* Network registration events */
1823 err = at_send_command("AT+CREG=2", &p_response);
1825 /* some handsets -- in tethered mode -- don't support CREG=2 */
1826 if (err < 0 || p_response->success == 0) {
1827 at_send_command("AT+CREG=1", NULL);
1830 at_response_free(p_response);
1832 /* GPRS registration events */
1833 at_send_command("AT+CGREG=1", NULL);
1835 /* Call Waiting notifications */
1836 at_send_command("AT+CCWA=1", NULL);
1838 /* Alternating voice/data off */
1839 at_send_command("AT+CMOD=0", NULL);
1842 at_send_command("AT+CMUT=0", NULL);
1844 /* +CSSU unsolicited supp service notifications */
1845 at_send_command("AT+CSSN=0,1", NULL);
1847 /* no connected line identification */
1848 at_send_command("AT+COLP=0", NULL);
1850 /* HEX character set */
1851 at_send_command("AT+CSCS=\"HEX\"", NULL);
1853 /* USSD unsolicited */
1854 at_send_command("AT+CUSD=1", NULL);
1856 /* Enable +CGEV GPRS event notifications, but don't buffer */
1857 at_send_command("AT+CGEREP=1,0", NULL);
1860 at_send_command("AT+CMGF=0", NULL);
1862 #ifdef USE_TI_COMMANDS
1864 at_send_command("AT%CPI=3", NULL);
1866 /* TI specific -- notifications when SMS is ready (currently ignored) */
1867 at_send_command("AT%CSTAT=1", NULL);
1869 #endif /* USE_TI_COMMANDS */
1872 /* assume radio is off on error */
1873 if (isRadioOn() > 0) {
1874 setRadioState (RADIO_STATE_SIM_NOT_READY);
1878 static void waitForClose()
1880 pthread_mutex_lock(&s_state_mutex);
1882 while (s_closed == 0) {
1883 pthread_cond_wait(&s_state_cond, &s_state_mutex);
1886 pthread_mutex_unlock(&s_state_mutex);
1890 * Called by atchannel when an unsolicited line appears
1891 * This is called on atchannel's reader thread. AT commands may
1892 * not be issued here
1894 static void onUnsolicited (const char *s, const char *sms_pdu)
1899 /* Ignore unsolicited responses until we're initialized.
1900 * This is OK because the RIL library will poll for initial state
1902 if (sState == RADIO_STATE_UNAVAILABLE) {
1906 if (strStartsWith(s, "%CTZV:")) {
1907 /* TI specific -- NITZ time */
1911 at_tok_start(&line);
1913 err = at_tok_nextstr(&line, &response);
1916 LOGE("invalid NITZ line %s\n", s);
1918 RIL_onUnsolicitedResponse (
1919 RIL_UNSOL_NITZ_TIME_RECEIVED,
1920 response, strlen(response));
1922 } else if (strStartsWith(s,"+CRING:")
1923 || strStartsWith(s,"RING")
1924 || strStartsWith(s,"NO CARRIER")
1925 || strStartsWith(s,"+CCWA")
1927 RIL_onUnsolicitedResponse (
1928 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
1930 #ifdef WORKAROUND_FAKE_CGEV
1931 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
1932 #endif /* WORKAROUND_FAKE_CGEV */
1933 } else if (strStartsWith(s,"+CREG:")
1934 || strStartsWith(s,"+CGREG:")
1936 RIL_onUnsolicitedResponse (
1937 RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
1939 #ifdef WORKAROUND_FAKE_CGEV
1940 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1941 #endif /* WORKAROUND_FAKE_CGEV */
1942 } else if (strStartsWith(s, "+CMT:")) {
1943 RIL_onUnsolicitedResponse (
1944 RIL_UNSOL_RESPONSE_NEW_SMS,
1945 sms_pdu, strlen(sms_pdu));
1946 } else if (strStartsWith(s, "+CDS:")) {
1947 RIL_onUnsolicitedResponse (
1948 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
1949 sms_pdu, strlen(sms_pdu));
1950 } else if (strStartsWith(s, "+CGEV:")) {
1951 /* Really, we can ignore NW CLASS and ME CLASS events here,
1952 * but right now we don't since extranous
1953 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
1955 /* can't issue AT commands here -- call on main thread */
1956 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1957 #ifdef WORKAROUND_FAKE_CGEV
1958 } else if (strStartsWith(s, "+CME ERROR: 150")) {
1959 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1960 #endif /* WORKAROUND_FAKE_CGEV */
1964 /* Called on command or reader thread */
1965 static void onATReaderClosed()
1967 LOGI("AT channel closed\n");
1971 setRadioState (RADIO_STATE_UNAVAILABLE);
1974 /* Called on command thread */
1975 static void onATTimeout()
1977 LOGI("AT channel timeout; closing\n");
1982 /* FIXME cause a radio reset here */
1984 setRadioState (RADIO_STATE_UNAVAILABLE);
1987 static void usage(char *s)
1990 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
1992 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
1998 mainLoop(void *param)
2003 AT_DUMP("== ", "entering mainLoop()", -1 );
2004 at_set_on_reader_closed(onATReaderClosed);
2005 at_set_on_timeout(onATTimeout);
2011 fd = socket_loopback_client(s_port, SOCK_STREAM);
2012 } else if (s_device_socket) {
2013 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
2014 /* Qemu-specific control socket */
2015 fd = socket_local_client( "qemud",
2016 ANDROID_SOCKET_NAMESPACE_RESERVED,
2021 if ( write(fd, "gsm", 3) != 3 ||
2022 read(fd, answer, 2) != 2 ||
2023 memcmp(answer, "OK", 2) != 0)
2031 fd = socket_local_client( s_device_path,
2032 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
2034 } else if (s_device_path != NULL) {
2035 fd = open (s_device_path, O_RDWR);
2036 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
2037 /* disable echo on serial ports */
2039 tcgetattr( fd, &ios );
2040 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
2041 tcsetattr( fd, TCSANOW, &ios );
2046 perror ("opening AT interface. retrying...");
2053 ret = at_open(fd, onUnsolicited);
2056 LOGE ("AT error %d on at_open\n", ret);
2060 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
2062 // Give initializeCallback a chance to dispatched, since
2063 // we don't presently have a cancellation mechanism
2067 LOGI("Re-opening after close");
2073 pthread_t s_tid_mainloop;
2075 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
2080 pthread_attr_t attr;
2084 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
2087 s_port = atoi(optarg);
2092 LOGI("Opening loopback port %d\n", s_port);
2096 s_device_path = optarg;
2097 LOGI("Opening tty device %s\n", s_device_path);
2101 s_device_path = optarg;
2102 s_device_socket = 1;
2103 LOGI("Opening socket %s\n", s_device_path);
2112 if (s_port < 0 && s_device_path == NULL) {
2117 pthread_attr_init (&attr);
2118 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2119 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
2121 return &s_callbacks;
2123 #else /* RIL_SHLIB */
2124 int main (int argc, char **argv)
2130 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
2133 s_port = atoi(optarg);
2137 LOGI("Opening loopback port %d\n", s_port);
2141 s_device_path = optarg;
2142 LOGI("Opening tty device %s\n", s_device_path);
2146 s_device_path = optarg;
2147 s_device_socket = 1;
2148 LOGI("Opening socket %s\n", s_device_path);
2156 if (s_port < 0 && s_device_path == NULL) {
2160 RIL_register(&s_callbacks);
2167 #endif /* RIL_SHLIB */