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>
39 #include <cutils/properties.h>
41 #define MAX_AT_RESPONSE 0x1000
43 /* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
44 #define PPP_TTY_PATH "/dev/ppp0"
46 #ifdef USE_TI_COMMANDS
48 // Enable a workaround
49 // 1) Make incoming call, do not answer
50 // 2) Hangup remote end
51 // Expected: call should disappear from CLCC line
52 // Actual: Call shows as "ACTIVE" before disappearing
53 #define WORKAROUND_ERRONEOUS_ANSWER 1
55 // Some varients of the TI stack do not support the +CGEV unsolicited
56 // response. However, they seem to send an unsolicited +CME ERROR: 150
57 #define WORKAROUND_FAKE_CGEV 1
63 SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
66 SIM_NETWORK_PERSONALIZATION = 5
69 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
70 static RIL_RadioState currentState();
71 static int onSupports (int requestCode);
72 static void onCancel (RIL_Token t);
73 static const char *getVersion();
74 static int isRadioOn();
75 static SIM_Status getSIMStatus();
76 static int getCardStatus(RIL_CardStatus **pp_card_status);
77 static void freeCardStatus(RIL_CardStatus *p_card_status);
78 static void onDataCallListChanged(void *param);
80 extern const char * requestToString(int request);
82 /*** Static Variables ***/
83 static const RIL_RadioFunctions s_callbacks = {
93 static const struct RIL_Env *s_rilenv;
95 #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
96 #define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
97 #define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
100 static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
102 static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
103 static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
105 static int s_port = -1;
106 static const char * s_device_path = NULL;
107 static int s_device_socket = 0;
109 /* trigger change to this with s_state_cond */
110 static int s_closed = 0;
112 static int sFD; /* file desc of AT channel */
113 static char sATBuffer[MAX_AT_RESPONSE+1];
114 static char *sATBufferCur = NULL;
116 static const struct timeval TIMEVAL_SIMPOLL = {1,0};
117 static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
118 static const struct timeval TIMEVAL_0 = {0,0};
120 #ifdef WORKAROUND_ERRONEOUS_ANSWER
121 // Max number of times we'll try to repoll when we think
122 // we have a AT+CLCC race condition
123 #define REPOLL_CALLS_COUNT_MAX 4
125 // Line index that was incoming or waiting at last poll, or -1 for none
126 static int s_incomingOrWaitingLine = -1;
127 // Number of times we've asked for a repoll of AT+CLCC
128 static int s_repollCallsCount = 0;
129 // Should we expect a call to be answered in the next CLCC?
130 static int s_expectAnswer = 0;
131 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
133 static void pollSIMState (void *param);
134 static void setRadioState(RIL_RadioState newState);
136 static int clccStateToRILState(int state, RIL_CallState *p_state)
140 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
141 case 1: *p_state = RIL_CALL_HOLDING; return 0;
142 case 2: *p_state = RIL_CALL_DIALING; return 0;
143 case 3: *p_state = RIL_CALL_ALERTING; return 0;
144 case 4: *p_state = RIL_CALL_INCOMING; return 0;
145 case 5: *p_state = RIL_CALL_WAITING; return 0;
151 * Note: directly modified line and has *p_call point directly into
154 static int callFromCLCCLine(char *line, RIL_Call *p_call)
156 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
157 // index,isMT,state,mode,isMpty(,number,TOA)?
163 err = at_tok_start(&line);
164 if (err < 0) goto error;
166 err = at_tok_nextint(&line, &(p_call->index));
167 if (err < 0) goto error;
169 err = at_tok_nextbool(&line, &(p_call->isMT));
170 if (err < 0) goto error;
172 err = at_tok_nextint(&line, &state);
173 if (err < 0) goto error;
175 err = clccStateToRILState(state, &(p_call->state));
176 if (err < 0) goto error;
178 err = at_tok_nextint(&line, &mode);
179 if (err < 0) goto error;
181 p_call->isVoice = (mode == 0);
183 err = at_tok_nextbool(&line, &(p_call->isMpty));
184 if (err < 0) goto error;
186 if (at_tok_hasmore(&line)) {
187 err = at_tok_nextstr(&line, &(p_call->number));
189 /* tolerate null here */
190 if (err < 0) return 0;
192 // Some lame implementations return strings
193 // like "NOT AVAILABLE" in the CLCC line
194 if (p_call->number != NULL
195 && 0 == strspn(p_call->number, "+0123456789")
197 p_call->number = NULL;
200 err = at_tok_nextint(&line, &p_call->toa);
201 if (err < 0) goto error;
207 LOGE("invalid CLCC line\n");
212 /** do post-AT+CFUN=1 initialization */
213 static void onRadioPowerOn()
215 #ifdef USE_TI_COMMANDS
216 /* Must be after CFUN=1 */
217 /* TI specific -- notifications for CPHS things such */
218 /* as CPHS message waiting indicator */
220 at_send_command("AT%CPHS=1", NULL);
222 /* TI specific -- enable NITZ unsol notifs */
223 at_send_command("AT%CTZV=1", NULL);
229 /** do post- SIM ready initialization */
230 static void onSIMReady()
232 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
234 * Always send SMS messages directly to the TE
236 * mode = 1 // discard when link is reserved (link should never be
238 * mt = 2 // most messages routed to TE
239 * bm = 2 // new cell BM's routed to TE
240 * ds = 1 // Status reports routed to TE
241 * bfr = 1 // flush buffer
243 LOGI ("######### SIM READY !!\n");
244 at_send_command("AT+CNMI=1,2,2,1,0", 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);
1054 //err = at_send_command("AT+CGACT=1,1", NULL);
1056 // Start data on PDP context 1
1057 //err = at_send_command("ATD*99***1#", &p_response);
1058 //err = at_send_command("ATD*99#", &p_response);
1059 //system("echo test");
1060 //system("pppd call gprs");
1061 //property_set("ctl.start", "pppd_gprs");
1062 //LOGD("ctrl.start err = %d\n", err);
1063 //err = system("pppd call gprs");
1064 //LOGD("pppd err = %d\n", err);
1065 /*if (err < 0 || p_response->success == 0) {
1068 // add pppd init code under this line
1073 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1074 at_response_free(p_response);
1078 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1079 at_response_free(p_response);
1083 static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1088 ackSuccess = ((int *)data)[0];
1090 if (ackSuccess == 1) {
1091 err = at_send_command("AT+CNMA=1", NULL);
1092 } else if (ackSuccess == 0) {
1093 err = at_send_command("AT+CNMA=2", NULL);
1095 LOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1099 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1101 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1105 static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1107 ATResponse *p_response = NULL;
1108 RIL_SIM_IO_Response sr;
1114 memset(&sr, 0, sizeof(sr));
1116 p_args = (RIL_SIM_IO *)data;
1118 /* FIXME handle pin2 */
1120 if (p_args->data == NULL) {
1121 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1122 p_args->command, p_args->fileid,
1123 p_args->p1, p_args->p2, p_args->p3);
1125 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1126 p_args->command, p_args->fileid,
1127 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1130 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1132 if (err < 0 || p_response->success == 0) {
1136 line = p_response->p_intermediates->line;
1138 err = at_tok_start(&line);
1139 if (err < 0) goto error;
1141 err = at_tok_nextint(&line, &(sr.sw1));
1142 if (err < 0) goto error;
1144 err = at_tok_nextint(&line, &(sr.sw2));
1145 if (err < 0) goto error;
1147 if (at_tok_hasmore(&line)) {
1148 err = at_tok_nextstr(&line, &(sr.simResponse));
1149 if (err < 0) goto error;
1152 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1153 at_response_free(p_response);
1158 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1159 at_response_free(p_response);
1164 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1166 ATResponse *p_response = NULL;
1169 const char** strings = (const char**)data;;
1171 if ( datalen == sizeof(char*) ) {
1172 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1173 } else if ( datalen == 2*sizeof(char*) ) {
1174 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1178 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1181 if (err < 0 || p_response->success == 0) {
1183 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1185 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1187 at_response_free(p_response);
1191 static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1193 const char *ussdRequest;
1195 ussdRequest = (char *)(data);
1198 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1205 /*** Callback methods from the RIL library to us ***/
1208 * Call from RIL to us to make a RIL_REQUEST
1210 * Must be completed with a call to RIL_onRequestComplete()
1212 * RIL_onRequestComplete() may be called from any thread, before or after
1213 * this function returns.
1215 * Will always be called from the same thread, so returning here implies
1216 * that the radio is ready to process another command (whether or not
1217 * the previous command has completed).
1220 onRequest (int request, void *data, size_t datalen, RIL_Token t)
1222 ATResponse *p_response;
1225 LOGD("onRequest: %s", requestToString(request));
1227 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1228 * when RADIO_STATE_UNAVAILABLE.
1230 if (sState == RADIO_STATE_UNAVAILABLE
1231 && request != RIL_REQUEST_GET_SIM_STATUS
1233 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1237 /* Ignore all non-power requests when RADIO_STATE_OFF
1238 * (except RIL_REQUEST_GET_SIM_STATUS)
1240 if (sState == RADIO_STATE_OFF
1241 && !(request == RIL_REQUEST_RADIO_POWER
1242 || request == RIL_REQUEST_GET_SIM_STATUS)
1244 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1249 case RIL_REQUEST_GET_SIM_STATUS: {
1250 RIL_CardStatus *p_card_status;
1254 int result = getCardStatus(&p_card_status);
1255 if (result == RIL_E_SUCCESS) {
1256 p_buffer = (char *)p_card_status;
1257 buffer_size = sizeof(*p_card_status);
1262 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
1263 freeCardStatus(p_card_status);
1266 case RIL_REQUEST_GET_CURRENT_CALLS:
1267 requestGetCurrentCalls(data, datalen, t);
1269 case RIL_REQUEST_DIAL:
1270 requestDial(data, datalen, t);
1272 case RIL_REQUEST_HANGUP:
1273 requestHangup(data, datalen, t);
1275 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1276 // 3GPP 22.030 6.5.5
1277 // "Releases all held calls or sets User Determined User Busy
1278 // (UDUB) for a waiting call."
1279 at_send_command("AT+CHLD=0", 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_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1286 // 3GPP 22.030 6.5.5
1287 // "Releases all active calls (if any exist) and accepts
1288 // the other (held or waiting) call."
1289 at_send_command("AT+CHLD=1", NULL);
1291 /* success or failure is ignored by the upper layer here.
1292 it will call GET_CURRENT_CALLS and determine success that way */
1293 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1295 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1296 // 3GPP 22.030 6.5.5
1297 // "Places all active calls (if any exist) on hold and accepts
1298 // the other (held or waiting) call."
1299 at_send_command("AT+CHLD=2", NULL);
1301 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1303 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1305 /* success or failure is ignored by the upper layer here.
1306 it will call GET_CURRENT_CALLS and determine success that way */
1307 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1309 case RIL_REQUEST_ANSWER:
1310 at_send_command("ATA", NULL);
1312 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1314 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1316 /* success or failure is ignored by the upper layer here.
1317 it will call GET_CURRENT_CALLS and determine success that way */
1318 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1320 case RIL_REQUEST_CONFERENCE:
1321 // 3GPP 22.030 6.5.5
1322 // "Adds a held call to the conversation"
1323 at_send_command("AT+CHLD=3", NULL);
1325 /* success or failure is ignored by the upper layer here.
1326 it will call GET_CURRENT_CALLS and determine success that way */
1327 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1329 case RIL_REQUEST_UDUB:
1330 /* user determined user busy */
1331 /* sometimes used: ATH */
1332 at_send_command("ATH", NULL);
1334 /* success or failure is ignored by the upper layer here.
1335 it will call GET_CURRENT_CALLS and determine success that way */
1336 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1339 case RIL_REQUEST_SEPARATE_CONNECTION:
1342 int party = ((int*)data)[0];
1344 // Make sure that party is in a valid range.
1345 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1346 // It's sufficient for us to just make sure it's single digit.)
1347 if (party > 0 && party < 10) {
1348 sprintf(cmd, "AT+CHLD=2%d", party);
1349 at_send_command(cmd, NULL);
1350 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1352 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1357 case RIL_REQUEST_SIGNAL_STRENGTH:
1358 requestSignalStrength(data, datalen, t);
1360 case RIL_REQUEST_REGISTRATION_STATE:
1361 case RIL_REQUEST_GPRS_REGISTRATION_STATE:
1362 requestRegistrationState(request, data, datalen, t);
1364 case RIL_REQUEST_OPERATOR:
1365 requestOperator(data, datalen, t);
1367 case RIL_REQUEST_RADIO_POWER:
1368 requestRadioPower(data, datalen, t);
1370 case RIL_REQUEST_DTMF: {
1371 char c = ((char *)data)[0];
1373 asprintf(&cmd, "AT+VTS=%c", (int)c);
1374 at_send_command(cmd, NULL);
1376 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1379 case RIL_REQUEST_SEND_SMS:
1380 requestSendSMS(data, datalen, t);
1382 case RIL_REQUEST_SETUP_DATA_CALL:
1383 requestSetupDataCall(data, datalen, t);
1385 case RIL_REQUEST_SMS_ACKNOWLEDGE:
1386 requestSMSAcknowledge(data, datalen, t);
1389 case RIL_REQUEST_GET_IMSI:
1391 err = at_send_command_numeric("AT+CIMI", &p_response);
1393 if (err < 0 || p_response->success == 0) {
1394 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1396 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1397 p_response->p_intermediates->line, sizeof(char *));
1399 at_response_free(p_response);
1402 case RIL_REQUEST_GET_IMEI:
1404 err = at_send_command_numeric("AT+CGSN", &p_response);
1406 if (err < 0 || p_response->success == 0) {
1407 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1409 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1410 p_response->p_intermediates->line, sizeof(char *));
1412 at_response_free(p_response);
1415 case RIL_REQUEST_SIM_IO:
1416 requestSIM_IO(data,datalen,t);
1419 case RIL_REQUEST_SEND_USSD:
1420 requestSendUSSD(data, datalen, t);
1423 case RIL_REQUEST_CANCEL_USSD:
1425 err = at_send_command_numeric("AT+CUSD=2", &p_response);
1427 if (err < 0 || p_response->success == 0) {
1428 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1430 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1431 p_response->p_intermediates->line, sizeof(char *));
1433 at_response_free(p_response);
1436 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
1437 at_send_command("AT+COPS=0", NULL);
1440 case RIL_REQUEST_DATA_CALL_LIST:
1441 requestDataCallList(data, datalen, t);
1444 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
1445 requestQueryNetworkSelectionMode(data, datalen, t);
1448 case RIL_REQUEST_OEM_HOOK_RAW:
1450 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1454 case RIL_REQUEST_OEM_HOOK_STRINGS: {
1458 LOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
1461 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
1462 i > 0 ; cur++, i --) {
1463 LOGD("> '%s'", *cur);
1466 // echo back strings
1467 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1471 case RIL_REQUEST_WRITE_SMS_TO_SIM:
1472 requestWriteSmsToSim(data, datalen, t);
1475 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
1478 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
1479 err = at_send_command(cmd, &p_response);
1481 if (err < 0 || p_response->success == 0) {
1482 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1484 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1486 at_response_free(p_response);
1490 case RIL_REQUEST_ENTER_SIM_PIN:
1491 case RIL_REQUEST_ENTER_SIM_PUK:
1492 case RIL_REQUEST_ENTER_SIM_PIN2:
1493 case RIL_REQUEST_ENTER_SIM_PUK2:
1494 case RIL_REQUEST_CHANGE_SIM_PIN:
1495 case RIL_REQUEST_CHANGE_SIM_PIN2:
1496 requestEnterSimPin(data, datalen, t);
1500 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1506 * Synchronous call from the RIL to us to return current radio state.
1507 * RADIO_STATE_UNAVAILABLE should be the initial state.
1509 static RIL_RadioState
1515 * Call from RIL to us to find out whether a specific request code
1516 * is supported by this implementation.
1518 * Return 1 for "supported" and 0 for "unsupported"
1522 onSupports (int requestCode)
1529 static void onCancel (RIL_Token t)
1535 static const char * getVersion(void)
1537 return "android reference-ril 1.0";
1541 setRadioState(RIL_RadioState newState)
1543 RIL_RadioState oldState;
1545 pthread_mutex_lock(&s_state_mutex);
1550 // If we're closed, the only reasonable state is
1551 // RADIO_STATE_UNAVAILABLE
1552 // This is here because things on the main thread
1553 // may attempt to change the radio state after the closed
1554 // event happened in another thread
1555 newState = RADIO_STATE_UNAVAILABLE;
1558 if (sState != newState || s_closed > 0) {
1561 pthread_cond_broadcast (&s_state_cond);
1564 pthread_mutex_unlock(&s_state_mutex);
1567 /* do these outside of the mutex */
1568 if (sState != oldState) {
1569 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1572 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
1573 * from the AT reader thread
1574 * Currently, this doesn't happen, but if that changes then these
1575 * will need to be dispatched on the request thread
1577 if (sState == RADIO_STATE_SIM_READY) {
1579 } else if (sState == RADIO_STATE_SIM_NOT_READY) {
1585 /** Returns SIM_NOT_READY on error */
1589 ATResponse *p_response = NULL;
1595 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
1596 ret = SIM_NOT_READY;
1600 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
1603 ret = SIM_NOT_READY;
1607 switch (at_get_cme_error(p_response)) {
1611 case CME_SIM_NOT_INSERTED:
1616 ret = SIM_NOT_READY;
1620 /* CPIN? has succeeded, now look at the result */
1622 cpinLine = p_response->p_intermediates->line;
1623 err = at_tok_start (&cpinLine);
1626 ret = SIM_NOT_READY;
1630 err = at_tok_nextstr(&cpinLine, &cpinResult);
1633 ret = SIM_NOT_READY;
1637 if (0 == strcmp (cpinResult, "SIM PIN")) {
1640 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
1643 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
1644 return SIM_NETWORK_PERSONALIZATION;
1645 } else if (0 != strcmp (cpinResult, "READY")) {
1646 /* we're treating unsupported lock types as "sim absent" */
1651 at_response_free(p_response);
1658 at_response_free(p_response);
1664 * Get the current card status.
1666 * This must be freed using freeCardStatus.
1667 * @return: On success returns RIL_E_SUCCESS
1669 static int getCardStatus(RIL_CardStatus **pp_card_status) {
1670 static RIL_AppStatus app_status_array[] = {
1672 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
1673 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1674 // SIM_NOT_READY = 1
1675 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
1676 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1678 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
1679 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
1681 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
1682 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
1684 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
1685 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
1686 // SIM_NETWORK_PERSONALIZATION = 5
1687 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
1688 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
1690 RIL_CardState card_state;
1693 int sim_status = getSIMStatus();
1694 if (sim_status == SIM_ABSENT) {
1695 card_state = RIL_CARDSTATE_ABSENT;
1698 card_state = RIL_CARDSTATE_PRESENT;
1702 // Allocate and initialize base card status.
1703 RIL_CardStatus *p_card_status = malloc(sizeof(RIL_CardStatus));
1704 p_card_status->card_state = card_state;
1705 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
1706 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
1707 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
1708 p_card_status->num_applications = num_apps;
1710 // Initialize application status
1712 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
1713 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
1716 // Pickup the appropriate application status
1717 // that reflects sim_status for gsm.
1718 if (num_apps != 0) {
1719 // Only support one app, gsm
1720 p_card_status->num_applications = 1;
1721 p_card_status->gsm_umts_subscription_app_index = 0;
1723 // Get the correct app status
1724 p_card_status->applications[0] = app_status_array[sim_status];
1727 *pp_card_status = p_card_status;
1728 return RIL_E_SUCCESS;
1732 * Free the card status returned by getCardStatus
1734 static void freeCardStatus(RIL_CardStatus *p_card_status) {
1735 free(p_card_status);
1739 * SIM ready means any commands that access the SIM will work, including:
1740 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
1741 * (all SMS-related commands)
1744 static void pollSIMState (void *param)
1746 ATResponse *p_response;
1749 if (sState != RADIO_STATE_SIM_NOT_READY) {
1750 // no longer valid to poll
1754 switch(getSIMStatus()) {
1758 case SIM_NETWORK_PERSONALIZATION:
1760 setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
1764 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
1768 setRadioState(RADIO_STATE_SIM_READY);
1773 /** returns 1 if on, 0 if off, and -1 on error */
1774 static int isRadioOn()
1776 ATResponse *p_response = NULL;
1781 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
1783 if (err < 0 || p_response->success == 0) {
1784 // assume radio is off
1788 line = p_response->p_intermediates->line;
1790 err = at_tok_start(&line);
1791 if (err < 0) goto error;
1793 err = at_tok_nextbool(&line, &ret);
1794 if (err < 0) goto error;
1796 at_response_free(p_response);
1802 at_response_free(p_response);
1807 * Initialize everything that can be configured while we're still in
1810 static void initializeCallback(void *param)
1812 ATResponse *p_response = NULL;
1815 setRadioState (RADIO_STATE_OFF);
1819 /* note: we don't check errors here. Everything important will
1820 be handled in onATTimeout and onATReaderClosed */
1822 /* atchannel is tolerant of echo but it must */
1823 /* have verbose result codes */
1824 at_send_command("ATE0Q0V1", NULL);
1826 /* No auto-answer */
1827 at_send_command("ATS0=0", NULL);
1829 /* Extended errors */
1830 at_send_command("AT+CMEE=1", NULL);
1832 /* Network registration events */
1833 err = at_send_command("AT+CREG=2", &p_response);
1835 /* some handsets -- in tethered mode -- don't support CREG=2 */
1836 if (err < 0 || p_response->success == 0) {
1837 at_send_command("AT+CREG=1", NULL);
1840 at_response_free(p_response);
1842 /* GPRS registration events */
1843 at_send_command("AT+CGREG=1", NULL);
1845 /* Call Waiting notifications */
1846 at_send_command("AT+CCWA=1", NULL);
1848 /* Alternating voice/data off */
1849 at_send_command("AT+CMOD=0", NULL);
1851 /* +CSSU unsolicited supp service notifications */
1852 at_send_command("AT+CSSN=0,1", NULL);
1854 /* no connected line identification */
1855 at_send_command("AT+COLP=0", NULL);
1857 /* HEX character set */
1858 at_send_command("AT+CSCS=\"HEX\"", NULL);
1860 /* USSD unsolicited */
1861 at_send_command("AT+CUSD=1", NULL);
1863 /* Enable +CGEV GPRS event notifications, but don't buffer */
1864 at_send_command("AT+CGEREP=1,0", NULL);
1867 at_send_command("AT+CMGF=0", NULL);
1869 #ifdef USE_TI_COMMANDS
1871 at_send_command("AT%CPI=3", NULL);
1873 /* TI specific -- notifications when SMS is ready (currently ignored) */
1874 at_send_command("AT%CSTAT=1", NULL);
1876 #endif /* USE_TI_COMMANDS */
1879 /* assume radio is off on error */
1880 if (isRadioOn() > 0) {
1881 setRadioState (RADIO_STATE_SIM_NOT_READY);
1885 static void waitForClose()
1887 pthread_mutex_lock(&s_state_mutex);
1889 while (s_closed == 0) {
1890 pthread_cond_wait(&s_state_cond, &s_state_mutex);
1893 pthread_mutex_unlock(&s_state_mutex);
1897 * Called by atchannel when an unsolicited line appears
1898 * This is called on atchannel's reader thread. AT commands may
1899 * not be issued here
1901 static void onUnsolicited (const char *s, const char *sms_pdu)
1905 LOGI("!!!!!!!!!!!!!!!!!!!!!! reference-ril.c onUnsolicited !!!!!!!!!!!!!!!!!!!!!!1\n");
1906 /* Ignore unsolicited responses until we're initialized.
1907 * This is OK because the RIL library will poll for initial state
1909 if (sState == RADIO_STATE_UNAVAILABLE) {
1913 if (strStartsWith(s, "%CTZV:")) {
1914 /* TI specific -- NITZ time */
1918 at_tok_start(&line);
1920 err = at_tok_nextstr(&line, &response);
1923 LOGE("invalid NITZ line %s\n", s);
1925 RIL_onUnsolicitedResponse (
1926 RIL_UNSOL_NITZ_TIME_RECEIVED,
1927 response, strlen(response));
1929 } else if (strStartsWith(s,"+CRING:")
1930 || strStartsWith(s,"RING")
1931 || strStartsWith(s,"NO CARRIER")
1932 || strStartsWith(s,"+CCWA")
1934 RIL_onUnsolicitedResponse (
1935 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
1937 #ifdef WORKAROUND_FAKE_CGEV
1938 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
1939 #endif /* WORKAROUND_FAKE_CGEV */
1940 } else if (strStartsWith(s,"+CREG:")
1941 || strStartsWith(s,"+CGREG:")
1943 RIL_onUnsolicitedResponse (
1944 RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
1946 #ifdef WORKAROUND_FAKE_CGEV
1947 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1948 #endif /* WORKAROUND_FAKE_CGEV */
1949 } else if (strStartsWith(s, "+CMT:")) {
1950 RIL_onUnsolicitedResponse (
1951 RIL_UNSOL_RESPONSE_NEW_SMS,
1952 sms_pdu, strlen(sms_pdu));
1953 } else if (strStartsWith(s, "+CDS:")) {
1954 RIL_onUnsolicitedResponse (
1955 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
1956 sms_pdu, strlen(sms_pdu));
1957 } else if (strStartsWith(s, "+CGEV:")) {
1958 /* Really, we can ignore NW CLASS and ME CLASS events here,
1959 * but right now we don't since extranous
1960 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
1962 /* can't issue AT commands here -- call on main thread */
1963 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1964 #ifdef WORKAROUND_FAKE_CGEV
1965 } else if (strStartsWith(s, "+CME ERROR: 150")) {
1966 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1967 #endif /* WORKAROUND_FAKE_CGEV */
1971 /* Called on command or reader thread */
1972 static void onATReaderClosed()
1974 LOGI("AT channel closed\n");
1978 setRadioState (RADIO_STATE_UNAVAILABLE);
1981 /* Called on command thread */
1982 static void onATTimeout()
1984 LOGI("AT channel timeout; closing\n");
1989 /* FIXME cause a radio reset here */
1991 setRadioState (RADIO_STATE_UNAVAILABLE);
1994 static void usage(char *s)
1997 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
1999 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
2005 mainLoop(void *param)
2010 AT_DUMP("== ", "entering mainLoop()", -1 );
2011 at_set_on_reader_closed(onATReaderClosed);
2012 at_set_on_timeout(onATTimeout);
2018 fd = socket_loopback_client(s_port, SOCK_STREAM);
2019 } else if (s_device_socket) {
2020 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
2021 /* Qemu-specific control socket */
2022 fd = socket_local_client( "qemud",
2023 ANDROID_SOCKET_NAMESPACE_RESERVED,
2028 if ( write(fd, "gsm", 3) != 3 ||
2029 read(fd, answer, 2) != 2 ||
2030 memcmp(answer, "OK", 2) != 0)
2038 fd = socket_local_client( s_device_path,
2039 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
2041 } else if (s_device_path != NULL) {
2042 fd = open (s_device_path, O_RDWR);
2044 /* disable echo on serial ports */
2046 tcgetattr( fd, &ios );
2047 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
2048 tcsetattr( fd, TCSANOW, &ios );
2053 perror ("opening AT interface. retrying...");
2062 LOGI ("xp-debug mainLoop\n");
2063 // signal(SIGIO, SIG_IGN); // the important one.
2065 fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
2066 LOGD ("##############open /dev/ttyUSB0##############\n");
2068 LOGI ("fd < 0 fd:%d\n",fd);
2071 fcntl(fd,F_SETFL,0);
2072 struct termios my_termios;
2073 tcgetattr(fd, &my_termios);
2075 cfsetispeed(&my_termios,B9600); // Baud rate 9600
2076 cfsetospeed(&my_termios,B9600);
2078 my_termios.c_cflag &= ~CSIZE; // 8 data bits
2079 my_termios.c_cflag &= ~PARENB; // no parity
2080 my_termios.c_cflag &= ~CSTOPB; // No hw flow control
2081 my_termios.c_cflag &= ~CRTSCTS;
2082 my_termios.c_cflag |= CS8;
2083 my_termios.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG); // No terminal processing
2084 my_termios.c_oflag &= ~OPOST;
2086 my_termios.c_cc[VMIN] = 0; // Timeout before packet (unused)
2087 my_termios.c_cc[VTIME] = 10; // Timeout between characters
2088 my_termios.c_iflag &= ~(IXON|IXOFF|IXANY); // Disable sw flow control
2090 my_termios.c_cflag |= (CLOCAL|CREAD);
2092 tcsetattr(fd,TCSANOW, &my_termios);
2093 fcntl(fd,F_SETFL,0);
2096 perror ("opening AT interface. retrying...");
2104 ret = at_open(fd, onUnsolicited);
2107 LOGE ("AT error %d on at_open\n", ret);
2111 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
2113 // Give initializeCallback a chance to dispatched, since
2114 // we don't presently have a cancellation mechanism
2118 LOGI("Re-opening after close");
2124 pthread_t s_tid_mainloop;
2126 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
2131 pthread_attr_t attr;
2135 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
2138 s_port = atoi(optarg);
2143 LOGI("Opening loopback port %d\n", s_port);
2147 s_device_path = optarg;
2148 LOGI("Opening tty device %s\n", s_device_path);
2152 s_device_path = optarg;
2153 s_device_socket = 1;
2154 LOGI("Opening socket %s\n", s_device_path);
2163 if (s_port < 0 && s_device_path == NULL) {
2168 pthread_attr_init (&attr);
2169 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2170 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
2172 return &s_callbacks;
2174 #else /* RIL_SHLIB */
2175 int main (int argc, char **argv)
2181 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
2184 s_port = atoi(optarg);
2188 LOGI("Opening loopback port %d\n", s_port);
2192 s_device_path = optarg;
2193 LOGI("Opening tty device %s\n", s_device_path);
2197 s_device_path = optarg;
2198 s_device_socket = 1;
2199 LOGI("Opening socket %s\n", s_device_path);
2207 if (s_port < 0 && s_device_path == NULL) {
2211 RIL_register(&s_callbacks);
2218 #endif /* RIL_SHLIB */