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;
206 LOGE("invalid CLCC line\n");
211 /** do post-AT+CFUN=1 initialization */
212 static void onRadioPowerOn()
214 #ifdef USE_TI_COMMANDS
215 /* Must be after CFUN=1 */
216 /* TI specific -- notifications for CPHS things such */
217 /* as CPHS message waiting indicator */
219 at_send_command("AT%CPHS=1", NULL);
221 /* TI specific -- enable NITZ unsol notifs */
222 at_send_command("AT%CTZV=1", NULL);
228 /** do post- SIM ready initialization */
229 static void onSIMReady()
231 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
233 * Always send SMS messages directly to the TE
235 * mode = 1 // discard when link is reserved (link should never be
237 * mt = 2 // most messages routed to TE
238 * bm = 2 // new cell BM's routed to TE
239 * ds = 1 // Status reports routed to TE
240 * bfr = 1 // flush buffer
242 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
245 static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
250 ATResponse *p_response = NULL;
252 assert (datalen >= sizeof(int *));
253 onOff = ((int *)data)[0];
255 if (onOff == 0 && sState != RADIO_STATE_OFF) {
256 err = at_send_command("AT+CFUN=0", &p_response);
257 if (err < 0 || p_response->success == 0) goto error;
258 setRadioState(RADIO_STATE_OFF);
259 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
260 err = at_send_command("AT+CFUN=1", &p_response);
261 if (err < 0|| p_response->success == 0) {
262 // Some stacks return an error when there is no SIM,
263 // but they really turn the RF portion on
264 // So, if we get an error, let's check to see if it
267 if (isRadioOn() != 1) {
271 setRadioState(RADIO_STATE_SIM_NOT_READY);
274 at_response_free(p_response);
275 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
278 at_response_free(p_response);
279 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
282 static void requestOrSendDataCallList(RIL_Token *t);
284 static void onDataCallListChanged(void *param)
286 requestOrSendDataCallList(NULL);
289 static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
291 requestOrSendDataCallList(&t);
294 static void requestOrSendDataCallList(RIL_Token *t)
296 ATResponse *p_response;
302 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
303 if (err != 0 || p_response->success == 0) {
305 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
307 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
312 for (p_cur = p_response->p_intermediates; p_cur != NULL;
313 p_cur = p_cur->p_next)
316 RIL_Data_Call_Response *responses =
317 alloca(n * sizeof(RIL_Data_Call_Response));
320 for (i = 0; i < n; i++) {
321 responses[i].cid = -1;
322 responses[i].active = -1;
323 responses[i].type = "";
324 responses[i].apn = "";
325 responses[i].address = "";
328 RIL_Data_Call_Response *response = responses;
329 for (p_cur = p_response->p_intermediates; p_cur != NULL;
330 p_cur = p_cur->p_next) {
331 char *line = p_cur->line;
333 err = at_tok_start(&line);
337 err = at_tok_nextint(&line, &response->cid);
341 err = at_tok_nextint(&line, &response->active);
348 at_response_free(p_response);
350 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
351 if (err != 0 || p_response->success == 0) {
353 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
355 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
360 for (p_cur = p_response->p_intermediates; p_cur != NULL;
361 p_cur = p_cur->p_next) {
362 char *line = p_cur->line;
369 err = at_tok_start(&line);
373 err = at_tok_nextint(&line, &cid);
377 for (i = 0; i < n; i++) {
378 if (responses[i].cid == cid)
383 /* details for a context we didn't hear about in the last request */
387 err = at_tok_nextstr(&line, &out);
391 responses[i].type = alloca(strlen(out) + 1);
392 strcpy(responses[i].type, out);
394 err = at_tok_nextstr(&line, &out);
398 responses[i].apn = alloca(strlen(out) + 1);
399 strcpy(responses[i].apn, out);
401 err = at_tok_nextstr(&line, &out);
405 responses[i].address = alloca(strlen(out) + 1);
406 strcpy(responses[i].address, out);
409 at_response_free(p_response);
412 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
413 n * sizeof(RIL_Data_Call_Response));
415 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
417 n * sizeof(RIL_Data_Call_Response));
423 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
425 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
428 at_response_free(p_response);
431 static void requestQueryNetworkSelectionMode(
432 void *data, size_t datalen, RIL_Token t)
435 ATResponse *p_response = NULL;
439 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
441 if (err < 0 || p_response->success == 0) {
445 line = p_response->p_intermediates->line;
447 err = at_tok_start(&line);
453 err = at_tok_nextint(&line, &response);
459 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
460 at_response_free(p_response);
463 at_response_free(p_response);
464 LOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
465 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
468 static void sendCallStateChanged(void *param)
470 RIL_onUnsolicitedResponse (
471 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
475 static void requestGetCurrentCalls(void *data, size_t datalen, RIL_Token t)
478 ATResponse *p_response;
487 #ifdef WORKAROUND_ERRONEOUS_ANSWER
488 int prevIncomingOrWaitingLine;
490 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
491 s_incomingOrWaitingLine = -1;
492 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
494 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
496 if (err != 0 || p_response->success == 0) {
497 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
501 /* count the calls */
502 for (countCalls = 0, p_cur = p_response->p_intermediates
504 ; p_cur = p_cur->p_next
509 /* yes, there's an array of pointers and then an array of structures */
511 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
512 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
513 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
515 /* init the pointer array */
516 for(i = 0; i < countCalls ; i++) {
517 pp_calls[i] = &(p_calls[i]);
520 for (countValidCalls = 0, p_cur = p_response->p_intermediates
522 ; p_cur = p_cur->p_next
524 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
530 #ifdef WORKAROUND_ERRONEOUS_ANSWER
531 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
532 || p_calls[countValidCalls].state == RIL_CALL_WAITING
534 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
536 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
538 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
539 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
547 #ifdef WORKAROUND_ERRONEOUS_ANSWER
549 // A call was incoming or waiting
550 // Now it's marked as active
551 // But we never answered it
553 // This is probably a bug, and the call will probably
554 // disappear from the call list in the next poll
555 if (prevIncomingOrWaitingLine >= 0
556 && s_incomingOrWaitingLine < 0
557 && s_expectAnswer == 0
559 for (i = 0; i < countValidCalls ; i++) {
561 if (p_calls[i].index == prevIncomingOrWaitingLine
562 && p_calls[i].state == RIL_CALL_ACTIVE
563 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
566 "Hit WORKAROUND_ERRONOUS_ANSWER case."
567 " Repoll count: %d\n", s_repollCallsCount);
568 s_repollCallsCount++;
575 s_repollCallsCount = 0;
576 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
578 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
579 countValidCalls * sizeof (RIL_Call *));
581 at_response_free(p_response);
583 #ifdef POLL_CALL_STATE
584 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
585 // smd, so we're forced to poll until the call ends.
589 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
594 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
595 at_response_free(p_response);
598 static void requestDial(void *data, size_t datalen, RIL_Token t)
605 p_dial = (RIL_Dial *)data;
607 switch (p_dial->clir) {
608 case 1: clir = "I"; break; /*invocation*/
609 case 2: clir = "i"; break; /*suppression*/
611 case 0: clir = ""; break; /*subscription default*/
614 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
616 ret = at_send_command(cmd, NULL);
620 /* success or failure is ignored by the upper layer here.
621 it will call GET_CURRENT_CALLS and determine success that way */
622 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
625 static void requestWriteSmsToSim(void *data, size_t datalen, RIL_Token t)
627 RIL_SMS_WriteArgs *p_args;
631 ATResponse *p_response = NULL;
633 p_args = (RIL_SMS_WriteArgs *)data;
635 length = strlen(p_args->pdu)/2;
636 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
638 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
640 if (err != 0 || p_response->success == 0) goto error;
642 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
643 at_response_free(p_response);
647 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
648 at_response_free(p_response);
651 static void requestHangup(void *data, size_t datalen, RIL_Token t)
658 p_line = (int *)data;
661 // "Releases a specific active call X"
662 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
664 ret = at_send_command(cmd, NULL);
668 /* success or failure is ignored by the upper layer here.
669 it will call GET_CURRENT_CALLS and determine success that way */
670 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
673 static void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
675 ATResponse *p_response = NULL;
680 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
682 if (err < 0 || p_response->success == 0) {
683 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
687 line = p_response->p_intermediates->line;
689 err = at_tok_start(&line);
690 if (err < 0) goto error;
692 err = at_tok_nextint(&line, &(response[0]));
693 if (err < 0) goto error;
695 err = at_tok_nextint(&line, &(response[1]));
696 if (err < 0) goto error;
698 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
700 at_response_free(p_response);
704 LOGE("requestSignalStrength must never return an error when radio is on");
705 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
706 at_response_free(p_response);
709 static void requestRegistrationState(int request, void *data,
710 size_t datalen, RIL_Token t)
714 char * responseStr[4];
715 ATResponse *p_response = NULL;
724 if (request == RIL_REQUEST_REGISTRATION_STATE) {
727 } else if (request == RIL_REQUEST_GPRS_REGISTRATION_STATE) {
735 err = at_send_command_singleline(cmd, prefix, &p_response);
737 if (err != 0) goto error;
739 line = p_response->p_intermediates->line;
741 err = at_tok_start(&line);
742 if (err < 0) goto error;
744 /* Ok you have to be careful here
745 * The solicited version of the CREG response is
746 * +CREG: n, stat, [lac, cid]
747 * and the unsolicited version is
748 * +CREG: stat, [lac, cid]
749 * The <n> parameter is basically "is unsolicited creg on?"
750 * which it should always be
752 * Now we should normally get the solicited version here,
753 * but the unsolicited version could have snuck in
754 * so we have to handle both
756 * Also since the LAC and CID are only reported when registered,
757 * we can have 1, 2, 3, or 4 arguments here
759 * finally, a +CGREG: answer may have a fifth value that corresponds
760 * to the network type, as in;
762 * +CGREG: n, stat [,lac, cid [,networkType]]
765 /* count number of commas */
767 for (p = line ; *p != '\0' ;p++) {
768 if (*p == ',') commas++;
772 case 0: /* +CREG: <stat> */
773 err = at_tok_nextint(&line, &response[0]);
774 if (err < 0) goto error;
779 case 1: /* +CREG: <n>, <stat> */
780 err = at_tok_nextint(&line, &skip);
781 if (err < 0) goto error;
782 err = at_tok_nextint(&line, &response[0]);
783 if (err < 0) goto error;
786 if (err < 0) goto error;
789 case 2: /* +CREG: <stat>, <lac>, <cid> */
790 err = at_tok_nextint(&line, &response[0]);
791 if (err < 0) goto error;
792 err = at_tok_nexthexint(&line, &response[1]);
793 if (err < 0) goto error;
794 err = at_tok_nexthexint(&line, &response[2]);
795 if (err < 0) goto error;
797 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
798 err = at_tok_nextint(&line, &skip);
799 if (err < 0) goto error;
800 err = at_tok_nextint(&line, &response[0]);
801 if (err < 0) goto error;
802 err = at_tok_nexthexint(&line, &response[1]);
803 if (err < 0) goto error;
804 err = at_tok_nexthexint(&line, &response[2]);
805 if (err < 0) goto error;
807 /* special case for CGREG, there is a fourth parameter
808 * that is the network type (unknown/gprs/edge/umts)
810 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
811 err = at_tok_nextint(&line, &skip);
812 if (err < 0) goto error;
813 err = at_tok_nextint(&line, &response[0]);
814 if (err < 0) goto error;
815 err = at_tok_nexthexint(&line, &response[1]);
816 if (err < 0) goto error;
817 err = at_tok_nexthexint(&line, &response[2]);
818 if (err < 0) goto error;
819 err = at_tok_nexthexint(&line, &response[3]);
820 if (err < 0) goto error;
827 asprintf(&responseStr[0], "%d", response[0]);
828 asprintf(&responseStr[1], "%x", response[1]);
829 asprintf(&responseStr[2], "%x", response[2]);
832 asprintf(&responseStr[3], "%d", response[3]);
834 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
835 at_response_free(p_response);
839 LOGE("requestRegistrationState must never return an error when radio is on");
840 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
841 at_response_free(p_response);
844 static void requestOperator(void *data, size_t datalen, RIL_Token t)
852 memset(response, 0, sizeof(response));
854 ATResponse *p_response = NULL;
856 err = at_send_command_multiline(
857 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
858 "+COPS:", &p_response);
860 /* we expect 3 lines here:
861 * +COPS: 0,0,"T - Mobile"
863 * +COPS: 0,2,"310170"
866 if (err != 0) goto error;
868 for (i = 0, p_cur = p_response->p_intermediates
870 ; p_cur = p_cur->p_next, i++
872 char *line = p_cur->line;
874 err = at_tok_start(&line);
875 if (err < 0) goto error;
877 err = at_tok_nextint(&line, &skip);
878 if (err < 0) goto error;
880 // If we're unregistered, we may just get
881 // a "+COPS: 0" response
882 if (!at_tok_hasmore(&line)) {
887 err = at_tok_nextint(&line, &skip);
888 if (err < 0) goto error;
890 // a "+COPS: 0, n" response is also possible
891 if (!at_tok_hasmore(&line)) {
896 err = at_tok_nextstr(&line, &(response[i]));
897 if (err < 0) goto error;
901 /* expect 3 lines exactly */
905 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
906 at_response_free(p_response);
910 LOGE("requestOperator must not return error when radio is on");
911 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
912 at_response_free(p_response);
915 static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
922 RIL_SMS_Response response;
923 ATResponse *p_response = NULL;
925 smsc = ((const char **)data)[0];
926 pdu = ((const char **)data)[1];
928 tpLayerLength = strlen(pdu)/2;
930 // "NULL for default SMSC"
935 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
936 asprintf(&cmd2, "%s%s", smsc, pdu);
938 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
940 if (err != 0 || p_response->success == 0) goto error;
942 memset(&response, 0, sizeof(response));
944 /* FIXME fill in messageRef and ackPDU */
946 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
947 at_response_free(p_response);
951 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
952 at_response_free(p_response);
955 static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
960 ATResponse *p_response = NULL;
961 char *response[2] = { "1", PPP_TTY_PATH };
963 apn = ((const char **)data)[2];
965 #ifdef USE_TI_COMMANDS
966 // Config for multislot class 10 (probably default anyway eh?)
967 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
970 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
971 #endif /* USE_TI_COMMANDS */
976 ssize_t written, rlen;
977 char status[32] = {0};
980 LOGD("requesting data connection to APN '%s'", apn);
982 fd = open ("/dev/qmi", O_RDWR);
983 if (fd >= 0) { /* the device doesn't exist on the emulator */
985 LOGD("opened the qmi device\n");
986 asprintf(&cmd, "up:%s", apn);
991 written = write (fd, cmd + cur, len - cur);
992 } while (written < 0 && errno == EINTR);
995 LOGE("### ERROR writing to /dev/qmi");
1003 // wait for interface to come online
1008 rlen = read(fd, status, 31);
1009 } while (rlen < 0 && errno == EINTR);
1012 LOGE("### ERROR reading from /dev/qmi");
1016 status[rlen] = '\0';
1017 LOGD("### status: %s", status);
1019 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1024 LOGE("### Failed to get data connection up\n");
1028 qmistatus = system("netcfg rmnet0 dhcp");
1030 LOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1032 if (qmistatus < 0) goto error;
1036 asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
1037 //FIXME check for error here
1038 err = at_send_command(cmd, NULL);
1041 // Set required QoS params to default
1042 err = at_send_command("AT+CGQREQ=1", NULL);
1044 // Set minimum QoS params to default
1045 err = at_send_command("AT+CGQMIN=1", NULL);
1047 // packet-domain event reporting
1048 err = at_send_command("AT+CGEREP=1,0", NULL);
1050 // Hangup anything that's happening there now
1051 err = at_send_command("AT+CGACT=1,0", NULL);
1053 // Start data on PDP context 1
1054 err = at_send_command("ATD*99***1#", &p_response);
1056 if (err < 0 || p_response->success == 0) {
1061 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1062 at_response_free(p_response);
1066 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1067 at_response_free(p_response);
1071 static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1076 ackSuccess = ((int *)data)[0];
1078 if (ackSuccess == 1) {
1079 err = at_send_command("AT+CNMA=1", NULL);
1080 } else if (ackSuccess == 0) {
1081 err = at_send_command("AT+CNMA=2", NULL);
1083 LOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1087 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1089 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1093 static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1095 ATResponse *p_response = NULL;
1096 RIL_SIM_IO_Response sr;
1102 memset(&sr, 0, sizeof(sr));
1104 p_args = (RIL_SIM_IO *)data;
1106 /* FIXME handle pin2 */
1108 if (p_args->data == NULL) {
1109 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1110 p_args->command, p_args->fileid,
1111 p_args->p1, p_args->p2, p_args->p3);
1113 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1114 p_args->command, p_args->fileid,
1115 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1118 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1120 if (err < 0 || p_response->success == 0) {
1124 line = p_response->p_intermediates->line;
1126 err = at_tok_start(&line);
1127 if (err < 0) goto error;
1129 err = at_tok_nextint(&line, &(sr.sw1));
1130 if (err < 0) goto error;
1132 err = at_tok_nextint(&line, &(sr.sw2));
1133 if (err < 0) goto error;
1135 if (at_tok_hasmore(&line)) {
1136 err = at_tok_nextstr(&line, &(sr.simResponse));
1137 if (err < 0) goto error;
1140 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1141 at_response_free(p_response);
1146 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1147 at_response_free(p_response);
1152 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1154 ATResponse *p_response = NULL;
1157 const char** strings = (const char**)data;;
1159 if ( datalen == sizeof(char*) ) {
1160 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1161 } else if ( datalen == 2*sizeof(char*) ) {
1162 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1166 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1169 if (err < 0 || p_response->success == 0) {
1171 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1173 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1175 at_response_free(p_response);
1179 static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1181 const char *ussdRequest;
1183 ussdRequest = (char *)(data);
1186 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1193 /*** Callback methods from the RIL library to us ***/
1196 * Call from RIL to us to make a RIL_REQUEST
1198 * Must be completed with a call to RIL_onRequestComplete()
1200 * RIL_onRequestComplete() may be called from any thread, before or after
1201 * this function returns.
1203 * Will always be called from the same thread, so returning here implies
1204 * that the radio is ready to process another command (whether or not
1205 * the previous command has completed).
1208 onRequest (int request, void *data, size_t datalen, RIL_Token t)
1210 ATResponse *p_response;
1213 LOGD("onRequest: %s", requestToString(request));
1215 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1216 * when RADIO_STATE_UNAVAILABLE.
1218 if (sState == RADIO_STATE_UNAVAILABLE
1219 && request != RIL_REQUEST_GET_SIM_STATUS
1221 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1225 /* Ignore all non-power requests when RADIO_STATE_OFF
1226 * (except RIL_REQUEST_GET_SIM_STATUS)
1228 if (sState == RADIO_STATE_OFF
1229 && !(request == RIL_REQUEST_RADIO_POWER
1230 || request == RIL_REQUEST_GET_SIM_STATUS)
1232 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1237 case RIL_REQUEST_GET_SIM_STATUS: {
1238 RIL_CardStatus *p_card_status;
1242 int result = getCardStatus(&p_card_status);
1243 if (result == RIL_E_SUCCESS) {
1244 p_buffer = (char *)p_card_status;
1245 buffer_size = sizeof(*p_card_status);
1250 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
1251 freeCardStatus(p_card_status);
1254 case RIL_REQUEST_GET_CURRENT_CALLS:
1255 requestGetCurrentCalls(data, datalen, t);
1257 case RIL_REQUEST_DIAL:
1258 requestDial(data, datalen, t);
1260 case RIL_REQUEST_HANGUP:
1261 requestHangup(data, datalen, t);
1263 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1264 // 3GPP 22.030 6.5.5
1265 // "Releases all held calls or sets User Determined User Busy
1266 // (UDUB) for a waiting call."
1267 at_send_command("AT+CHLD=0", NULL);
1269 /* success or failure is ignored by the upper layer here.
1270 it will call GET_CURRENT_CALLS and determine success that way */
1271 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1273 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1274 // 3GPP 22.030 6.5.5
1275 // "Releases all active calls (if any exist) and accepts
1276 // the other (held or waiting) call."
1277 at_send_command("AT+CHLD=1", NULL);
1279 /* success or failure is ignored by the upper layer here.
1280 it will call GET_CURRENT_CALLS and determine success that way */
1281 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1283 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1284 // 3GPP 22.030 6.5.5
1285 // "Places all active calls (if any exist) on hold and accepts
1286 // the other (held or waiting) call."
1287 at_send_command("AT+CHLD=2", NULL);
1289 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1291 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1293 /* success or failure is ignored by the upper layer here.
1294 it will call GET_CURRENT_CALLS and determine success that way */
1295 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1297 case RIL_REQUEST_ANSWER:
1298 at_send_command("ATA", NULL);
1300 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1302 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1304 /* success or failure is ignored by the upper layer here.
1305 it will call GET_CURRENT_CALLS and determine success that way */
1306 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1308 case RIL_REQUEST_CONFERENCE:
1309 // 3GPP 22.030 6.5.5
1310 // "Adds a held call to the conversation"
1311 at_send_command("AT+CHLD=3", NULL);
1313 /* success or failure is ignored by the upper layer here.
1314 it will call GET_CURRENT_CALLS and determine success that way */
1315 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1317 case RIL_REQUEST_UDUB:
1318 /* user determined user busy */
1319 /* sometimes used: ATH */
1320 at_send_command("ATH", NULL);
1322 /* success or failure is ignored by the upper layer here.
1323 it will call GET_CURRENT_CALLS and determine success that way */
1324 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1327 case RIL_REQUEST_SEPARATE_CONNECTION:
1330 int party = ((int*)data)[0];
1332 // Make sure that party is in a valid range.
1333 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1334 // It's sufficient for us to just make sure it's single digit.)
1335 if (party > 0 && party < 10) {
1336 sprintf(cmd, "AT+CHLD=2%d", party);
1337 at_send_command(cmd, NULL);
1338 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1340 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1345 case RIL_REQUEST_SIGNAL_STRENGTH:
1346 requestSignalStrength(data, datalen, t);
1348 case RIL_REQUEST_REGISTRATION_STATE:
1349 case RIL_REQUEST_GPRS_REGISTRATION_STATE:
1350 requestRegistrationState(request, data, datalen, t);
1352 case RIL_REQUEST_OPERATOR:
1353 requestOperator(data, datalen, t);
1355 case RIL_REQUEST_RADIO_POWER:
1356 requestRadioPower(data, datalen, t);
1358 case RIL_REQUEST_DTMF: {
1359 char c = ((char *)data)[0];
1361 asprintf(&cmd, "AT+VTS=%c", (int)c);
1362 at_send_command(cmd, NULL);
1364 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1367 case RIL_REQUEST_SEND_SMS:
1368 requestSendSMS(data, datalen, t);
1370 case RIL_REQUEST_SETUP_DATA_CALL:
1371 requestSetupDataCall(data, datalen, t);
1373 case RIL_REQUEST_SMS_ACKNOWLEDGE:
1374 requestSMSAcknowledge(data, datalen, t);
1377 case RIL_REQUEST_GET_IMSI:
1379 err = at_send_command_numeric("AT+CIMI", &p_response);
1381 if (err < 0 || p_response->success == 0) {
1382 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1384 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1385 p_response->p_intermediates->line, sizeof(char *));
1387 at_response_free(p_response);
1390 case RIL_REQUEST_GET_IMEI:
1392 err = at_send_command_numeric("AT+CGSN", &p_response);
1394 if (err < 0 || p_response->success == 0) {
1395 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1397 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1398 p_response->p_intermediates->line, sizeof(char *));
1400 at_response_free(p_response);
1403 case RIL_REQUEST_SIM_IO:
1404 requestSIM_IO(data,datalen,t);
1407 case RIL_REQUEST_SEND_USSD:
1408 requestSendUSSD(data, datalen, t);
1411 case RIL_REQUEST_CANCEL_USSD:
1413 err = at_send_command_numeric("AT+CUSD=2", &p_response);
1415 if (err < 0 || p_response->success == 0) {
1416 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1418 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1419 p_response->p_intermediates->line, sizeof(char *));
1421 at_response_free(p_response);
1424 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
1425 at_send_command("AT+COPS=0", NULL);
1428 case RIL_REQUEST_DATA_CALL_LIST:
1429 requestDataCallList(data, datalen, t);
1432 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
1433 requestQueryNetworkSelectionMode(data, datalen, t);
1436 case RIL_REQUEST_OEM_HOOK_RAW:
1438 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1442 case RIL_REQUEST_OEM_HOOK_STRINGS: {
1446 LOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
1449 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
1450 i > 0 ; cur++, i --) {
1451 LOGD("> '%s'", *cur);
1454 // echo back strings
1455 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1459 case RIL_REQUEST_WRITE_SMS_TO_SIM:
1460 requestWriteSmsToSim(data, datalen, t);
1463 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
1466 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
1467 err = at_send_command(cmd, &p_response);
1469 if (err < 0 || p_response->success == 0) {
1470 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1472 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1474 at_response_free(p_response);
1478 case RIL_REQUEST_ENTER_SIM_PIN:
1479 case RIL_REQUEST_ENTER_SIM_PUK:
1480 case RIL_REQUEST_ENTER_SIM_PIN2:
1481 case RIL_REQUEST_ENTER_SIM_PUK2:
1482 case RIL_REQUEST_CHANGE_SIM_PIN:
1483 case RIL_REQUEST_CHANGE_SIM_PIN2:
1484 requestEnterSimPin(data, datalen, t);
1488 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1494 * Synchronous call from the RIL to us to return current radio state.
1495 * RADIO_STATE_UNAVAILABLE should be the initial state.
1497 static RIL_RadioState
1503 * Call from RIL to us to find out whether a specific request code
1504 * is supported by this implementation.
1506 * Return 1 for "supported" and 0 for "unsupported"
1510 onSupports (int requestCode)
1517 static void onCancel (RIL_Token t)
1523 static const char * getVersion(void)
1525 return "android reference-ril 1.0";
1529 setRadioState(RIL_RadioState newState)
1531 RIL_RadioState oldState;
1533 pthread_mutex_lock(&s_state_mutex);
1538 // If we're closed, the only reasonable state is
1539 // RADIO_STATE_UNAVAILABLE
1540 // This is here because things on the main thread
1541 // may attempt to change the radio state after the closed
1542 // event happened in another thread
1543 newState = RADIO_STATE_UNAVAILABLE;
1546 if (sState != newState || s_closed > 0) {
1549 pthread_cond_broadcast (&s_state_cond);
1552 pthread_mutex_unlock(&s_state_mutex);
1555 /* do these outside of the mutex */
1556 if (sState != oldState) {
1557 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1560 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
1561 * from the AT reader thread
1562 * Currently, this doesn't happen, but if that changes then these
1563 * will need to be dispatched on the request thread
1565 if (sState == RADIO_STATE_SIM_READY) {
1567 } else if (sState == RADIO_STATE_SIM_NOT_READY) {
1573 /** Returns SIM_NOT_READY on error */
1577 ATResponse *p_response = NULL;
1583 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
1584 ret = SIM_NOT_READY;
1588 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
1591 ret = SIM_NOT_READY;
1595 switch (at_get_cme_error(p_response)) {
1599 case CME_SIM_NOT_INSERTED:
1604 ret = SIM_NOT_READY;
1608 /* CPIN? has succeeded, now look at the result */
1610 cpinLine = p_response->p_intermediates->line;
1611 err = at_tok_start (&cpinLine);
1614 ret = SIM_NOT_READY;
1618 err = at_tok_nextstr(&cpinLine, &cpinResult);
1621 ret = SIM_NOT_READY;
1625 if (0 == strcmp (cpinResult, "SIM PIN")) {
1628 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
1631 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
1632 return SIM_NETWORK_PERSONALIZATION;
1633 } else if (0 != strcmp (cpinResult, "READY")) {
1634 /* we're treating unsupported lock types as "sim absent" */
1639 at_response_free(p_response);
1646 at_response_free(p_response);
1652 * Get the current card status.
1654 * This must be freed using freeCardStatus.
1655 * @return: On success returns RIL_E_SUCCESS
1657 static int getCardStatus(RIL_CardStatus **pp_card_status) {
1658 static RIL_AppStatus app_status_array[] = {
1660 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
1661 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1662 // SIM_NOT_READY = 1
1663 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
1664 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1666 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
1667 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1669 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
1670 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
1672 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
1673 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
1674 // SIM_NETWORK_PERSONALIZATION = 5
1675 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
1676 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
1678 RIL_CardState card_state;
1681 int sim_status = getSIMStatus();
1682 if (sim_status == SIM_ABSENT) {
1683 card_state = RIL_CARDSTATE_ABSENT;
1686 card_state = RIL_CARDSTATE_PRESENT;
1690 // Allocate and initialize base card status.
1691 RIL_CardStatus *p_card_status = malloc(sizeof(RIL_CardStatus));
1692 p_card_status->card_state = card_state;
1693 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
1694 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
1695 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
1696 p_card_status->num_applications = num_apps;
1698 // Initialize application status
1700 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
1701 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
1704 // Pickup the appropriate application status
1705 // that reflects sim_status for gsm.
1706 if (num_apps != 0) {
1707 // Only support one app, gsm
1708 p_card_status->num_applications = 1;
1709 p_card_status->gsm_umts_subscription_app_index = 0;
1711 // Get the correct app status
1712 p_card_status->applications[0] = app_status_array[sim_status];
1715 *pp_card_status = p_card_status;
1716 return RIL_E_SUCCESS;
1720 * Free the card status returned by getCardStatus
1722 static void freeCardStatus(RIL_CardStatus *p_card_status) {
1723 free(p_card_status);
1727 * SIM ready means any commands that access the SIM will work, including:
1728 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
1729 * (all SMS-related commands)
1732 static void pollSIMState (void *param)
1734 ATResponse *p_response;
1737 if (sState != RADIO_STATE_SIM_NOT_READY) {
1738 // no longer valid to poll
1742 switch(getSIMStatus()) {
1746 case SIM_NETWORK_PERSONALIZATION:
1748 setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
1752 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
1756 setRadioState(RADIO_STATE_SIM_READY);
1761 /** returns 1 if on, 0 if off, and -1 on error */
1762 static int isRadioOn()
1764 ATResponse *p_response = NULL;
1769 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
1771 if (err < 0 || p_response->success == 0) {
1772 // assume radio is off
1776 line = p_response->p_intermediates->line;
1778 err = at_tok_start(&line);
1779 if (err < 0) goto error;
1781 err = at_tok_nextbool(&line, &ret);
1782 if (err < 0) goto error;
1784 at_response_free(p_response);
1790 at_response_free(p_response);
1795 * Initialize everything that can be configured while we're still in
1798 static void initializeCallback(void *param)
1800 ATResponse *p_response = NULL;
1803 setRadioState (RADIO_STATE_OFF);
1807 /* note: we don't check errors here. Everything important will
1808 be handled in onATTimeout and onATReaderClosed */
1810 /* atchannel is tolerant of echo but it must */
1811 /* have verbose result codes */
1812 at_send_command("ATE0Q0V1", NULL);
1814 /* No auto-answer */
1815 at_send_command("ATS0=0", NULL);
1817 /* Extended errors */
1818 at_send_command("AT+CMEE=1", NULL);
1820 /* Network registration events */
1821 err = at_send_command("AT+CREG=2", &p_response);
1823 /* some handsets -- in tethered mode -- don't support CREG=2 */
1824 if (err < 0 || p_response->success == 0) {
1825 at_send_command("AT+CREG=1", NULL);
1828 at_response_free(p_response);
1830 /* GPRS registration events */
1831 at_send_command("AT+CGREG=1", NULL);
1833 /* Call Waiting notifications */
1834 at_send_command("AT+CCWA=1", NULL);
1836 /* Alternating voice/data off */
1837 at_send_command("AT+CMOD=0", NULL);
1840 at_send_command("AT+CMUT=0", NULL);
1842 /* +CSSU unsolicited supp service notifications */
1843 at_send_command("AT+CSSN=0,1", NULL);
1845 /* no connected line identification */
1846 at_send_command("AT+COLP=0", NULL);
1848 /* HEX character set */
1849 at_send_command("AT+CSCS=\"HEX\"", NULL);
1851 /* USSD unsolicited */
1852 at_send_command("AT+CUSD=1", NULL);
1854 /* Enable +CGEV GPRS event notifications, but don't buffer */
1855 at_send_command("AT+CGEREP=1,0", NULL);
1858 at_send_command("AT+CMGF=0", NULL);
1860 #ifdef USE_TI_COMMANDS
1862 at_send_command("AT%CPI=3", NULL);
1864 /* TI specific -- notifications when SMS is ready (currently ignored) */
1865 at_send_command("AT%CSTAT=1", NULL);
1867 #endif /* USE_TI_COMMANDS */
1870 /* assume radio is off on error */
1871 if (isRadioOn() > 0) {
1872 setRadioState (RADIO_STATE_SIM_NOT_READY);
1876 static void waitForClose()
1878 pthread_mutex_lock(&s_state_mutex);
1880 while (s_closed == 0) {
1881 pthread_cond_wait(&s_state_cond, &s_state_mutex);
1884 pthread_mutex_unlock(&s_state_mutex);
1888 * Called by atchannel when an unsolicited line appears
1889 * This is called on atchannel's reader thread. AT commands may
1890 * not be issued here
1892 static void onUnsolicited (const char *s, const char *sms_pdu)
1897 /* Ignore unsolicited responses until we're initialized.
1898 * This is OK because the RIL library will poll for initial state
1900 if (sState == RADIO_STATE_UNAVAILABLE) {
1904 if (strStartsWith(s, "%CTZV:")) {
1905 /* TI specific -- NITZ time */
1909 at_tok_start(&line);
1911 err = at_tok_nextstr(&line, &response);
1914 LOGE("invalid NITZ line %s\n", s);
1916 RIL_onUnsolicitedResponse (
1917 RIL_UNSOL_NITZ_TIME_RECEIVED,
1918 response, strlen(response));
1920 } else if (strStartsWith(s,"+CRING:")
1921 || strStartsWith(s,"RING")
1922 || strStartsWith(s,"NO CARRIER")
1923 || strStartsWith(s,"+CCWA")
1925 RIL_onUnsolicitedResponse (
1926 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
1928 #ifdef WORKAROUND_FAKE_CGEV
1929 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
1930 #endif /* WORKAROUND_FAKE_CGEV */
1931 } else if (strStartsWith(s,"+CREG:")
1932 || strStartsWith(s,"+CGREG:")
1934 RIL_onUnsolicitedResponse (
1935 RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
1937 #ifdef WORKAROUND_FAKE_CGEV
1938 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1939 #endif /* WORKAROUND_FAKE_CGEV */
1940 } else if (strStartsWith(s, "+CMT:")) {
1941 RIL_onUnsolicitedResponse (
1942 RIL_UNSOL_RESPONSE_NEW_SMS,
1943 sms_pdu, strlen(sms_pdu));
1944 } else if (strStartsWith(s, "+CDS:")) {
1945 RIL_onUnsolicitedResponse (
1946 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
1947 sms_pdu, strlen(sms_pdu));
1948 } else if (strStartsWith(s, "+CGEV:")) {
1949 /* Really, we can ignore NW CLASS and ME CLASS events here,
1950 * but right now we don't since extranous
1951 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
1953 /* can't issue AT commands here -- call on main thread */
1954 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1955 #ifdef WORKAROUND_FAKE_CGEV
1956 } else if (strStartsWith(s, "+CME ERROR: 150")) {
1957 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1958 #endif /* WORKAROUND_FAKE_CGEV */
1962 /* Called on command or reader thread */
1963 static void onATReaderClosed()
1965 LOGI("AT channel closed\n");
1969 setRadioState (RADIO_STATE_UNAVAILABLE);
1972 /* Called on command thread */
1973 static void onATTimeout()
1975 LOGI("AT channel timeout; closing\n");
1980 /* FIXME cause a radio reset here */
1982 setRadioState (RADIO_STATE_UNAVAILABLE);
1985 static void usage(char *s)
1988 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
1990 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
1996 mainLoop(void *param)
2001 AT_DUMP("== ", "entering mainLoop()", -1 );
2002 at_set_on_reader_closed(onATReaderClosed);
2003 at_set_on_timeout(onATTimeout);
2009 fd = socket_loopback_client(s_port, SOCK_STREAM);
2010 } else if (s_device_socket) {
2011 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
2012 /* Qemu-specific control socket */
2013 fd = socket_local_client( "qemud",
2014 ANDROID_SOCKET_NAMESPACE_RESERVED,
2019 if ( write(fd, "gsm", 3) != 3 ||
2020 read(fd, answer, 2) != 2 ||
2021 memcmp(answer, "OK", 2) != 0)
2029 fd = socket_local_client( s_device_path,
2030 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
2032 } else if (s_device_path != NULL) {
2033 fd = open (s_device_path, O_RDWR);
2034 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
2035 /* disable echo on serial ports */
2037 tcgetattr( fd, &ios );
2038 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
2039 tcsetattr( fd, TCSANOW, &ios );
2044 perror ("opening AT interface. retrying...");
2051 ret = at_open(fd, onUnsolicited);
2054 LOGE ("AT error %d on at_open\n", ret);
2058 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
2060 // Give initializeCallback a chance to dispatched, since
2061 // we don't presently have a cancellation mechanism
2065 LOGI("Re-opening after close");
2071 pthread_t s_tid_mainloop;
2073 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
2078 pthread_attr_t attr;
2082 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
2085 s_port = atoi(optarg);
2090 LOGI("Opening loopback port %d\n", s_port);
2094 s_device_path = optarg;
2095 LOGI("Opening tty device %s\n", s_device_path);
2099 s_device_path = optarg;
2100 s_device_socket = 1;
2101 LOGI("Opening socket %s\n", s_device_path);
2110 if (s_port < 0 && s_device_path == NULL) {
2115 pthread_attr_init (&attr);
2116 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2117 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
2119 return &s_callbacks;
2121 #else /* RIL_SHLIB */
2122 int main (int argc, char **argv)
2128 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
2131 s_port = atoi(optarg);
2135 LOGI("Opening loopback port %d\n", s_port);
2139 s_device_path = optarg;
2140 LOGI("Opening tty device %s\n", s_device_path);
2144 s_device_path = optarg;
2145 s_device_socket = 1;
2146 LOGI("Opening socket %s\n", s_device_path);
2154 if (s_port < 0 && s_device_path == NULL) {
2158 RIL_register(&s_callbacks);
2165 #endif /* RIL_SHLIB */