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
59 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
60 static RIL_RadioState currentState();
61 static int onSupports (int requestCode);
62 static void onCancel (RIL_Token t);
63 static const char *getVersion();
64 static int isRadioOn();
65 static int getSIMStatus();
66 static void onDataCallListChanged(void *param);
68 extern const char * requestToString(int request);
70 /*** Static Variables ***/
71 static const RIL_RadioFunctions s_callbacks = {
81 static const struct RIL_Env *s_rilenv;
83 #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
84 #define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
85 #define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
88 static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
90 static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
91 static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
93 static int s_port = -1;
94 static const char * s_device_path = NULL;
95 static int s_device_socket = 0;
97 /* trigger change to this with s_state_cond */
98 static int s_closed = 0;
100 static int sFD; /* file desc of AT channel */
101 static char sATBuffer[MAX_AT_RESPONSE+1];
102 static char *sATBufferCur = NULL;
104 static const struct timeval TIMEVAL_SIMPOLL = {1,0};
105 static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
106 static const struct timeval TIMEVAL_0 = {0,0};
108 #ifdef WORKAROUND_ERRONEOUS_ANSWER
109 // Max number of times we'll try to repoll when we think
110 // we have a AT+CLCC race condition
111 #define REPOLL_CALLS_COUNT_MAX 4
113 // Line index that was incoming or waiting at last poll, or -1 for none
114 static int s_incomingOrWaitingLine = -1;
115 // Number of times we've asked for a repoll of AT+CLCC
116 static int s_repollCallsCount = 0;
117 // Should we expect a call to be answered in the next CLCC?
118 static int s_expectAnswer = 0;
119 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
121 static void pollSIMState (void *param);
122 static void setRadioState(RIL_RadioState newState);
124 static int clccStateToRILState(int state, RIL_CallState *p_state)
128 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
129 case 1: *p_state = RIL_CALL_HOLDING; return 0;
130 case 2: *p_state = RIL_CALL_DIALING; return 0;
131 case 3: *p_state = RIL_CALL_ALERTING; return 0;
132 case 4: *p_state = RIL_CALL_INCOMING; return 0;
133 case 5: *p_state = RIL_CALL_WAITING; return 0;
139 * Note: directly modified line and has *p_call point directly into
142 static int callFromCLCCLine(char *line, RIL_CallOld *p_call)
144 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
145 // index,isMT,state,mode,isMpty(,number,TOA)?
151 err = at_tok_start(&line);
152 if (err < 0) goto error;
154 err = at_tok_nextint(&line, &(p_call->index));
155 if (err < 0) goto error;
157 err = at_tok_nextbool(&line, &(p_call->isMT));
158 if (err < 0) goto error;
160 err = at_tok_nextint(&line, &state);
161 if (err < 0) goto error;
163 err = clccStateToRILState(state, &(p_call->state));
164 if (err < 0) goto error;
166 err = at_tok_nextint(&line, &mode);
167 if (err < 0) goto error;
169 p_call->isVoice = (mode == 0);
171 err = at_tok_nextbool(&line, &(p_call->isMpty));
172 if (err < 0) goto error;
174 if (at_tok_hasmore(&line)) {
175 err = at_tok_nextstr(&line, &(p_call->number));
177 /* tolerate null here */
178 if (err < 0) return 0;
180 // Some lame implementations return strings
181 // like "NOT AVAILABLE" in the CLCC line
182 if (p_call->number != NULL
183 && 0 == strspn(p_call->number, "+0123456789")
185 p_call->number = NULL;
188 err = at_tok_nextint(&line, &p_call->toa);
189 if (err < 0) goto error;
195 LOGE("invalid CLCC line\n");
200 /** do post-AT+CFUN=1 initialization */
201 static void onRadioPowerOn()
203 #ifdef USE_TI_COMMANDS
204 /* Must be after CFUN=1 */
205 /* TI specific -- notifications for CPHS things such */
206 /* as CPHS message waiting indicator */
208 at_send_command("AT%CPHS=1", NULL);
210 /* TI specific -- enable NITZ unsol notifs */
211 at_send_command("AT%CTZV=1", NULL);
217 /** do post- SIM ready initialization */
218 static void onSIMReady()
220 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
222 * Always send SMS messages directly to the TE
224 * mode = 1 // discard when link is reserved (link should never be
226 * mt = 2 // most messages routed to TE
227 * bm = 2 // new cell BM's routed to TE
228 * ds = 1 // Status reports routed to TE
229 * bfr = 1 // flush buffer
231 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
234 static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
239 ATResponse *p_response = NULL;
241 assert (datalen >= sizeof(int *));
242 onOff = ((int *)data)[0];
244 if (onOff == 0 && sState != RADIO_STATE_OFF) {
245 err = at_send_command("AT+CFUN=0", &p_response);
246 if (err < 0 || p_response->success == 0) goto error;
247 setRadioState(RADIO_STATE_OFF);
248 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
249 err = at_send_command("AT+CFUN=1", &p_response);
250 if (err < 0|| p_response->success == 0) {
251 // Some stacks return an error when there is no SIM,
252 // but they really turn the RF portion on
253 // So, if we get an error, let's check to see if it
256 if (isRadioOn() != 1) {
260 setRadioState(RADIO_STATE_SIM_NOT_READY);
263 at_response_free(p_response);
264 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
267 at_response_free(p_response);
268 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
271 static void requestOrSendDataCallList(RIL_Token *t);
273 static void onDataCallListChanged(void *param)
275 requestOrSendDataCallList(NULL);
278 static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
280 requestOrSendDataCallList(&t);
283 static void requestOrSendDataCallList(RIL_Token *t)
285 ATResponse *p_response;
291 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
292 if (err != 0 || p_response->success == 0) {
294 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
296 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
301 for (p_cur = p_response->p_intermediates; p_cur != NULL;
302 p_cur = p_cur->p_next)
305 RIL_Data_Call_Response *responses =
306 alloca(n * sizeof(RIL_Data_Call_Response));
309 for (i = 0; i < n; i++) {
310 responses[i].cid = -1;
311 responses[i].active = -1;
312 responses[i].type = "";
313 responses[i].apn = "";
314 responses[i].address = "";
317 RIL_Data_Call_Response *response = responses;
318 for (p_cur = p_response->p_intermediates; p_cur != NULL;
319 p_cur = p_cur->p_next) {
320 char *line = p_cur->line;
322 err = at_tok_start(&line);
326 err = at_tok_nextint(&line, &response->cid);
330 err = at_tok_nextint(&line, &response->active);
337 at_response_free(p_response);
339 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
340 if (err != 0 || p_response->success == 0) {
342 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
344 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
349 for (p_cur = p_response->p_intermediates; p_cur != NULL;
350 p_cur = p_cur->p_next) {
351 char *line = p_cur->line;
358 err = at_tok_start(&line);
362 err = at_tok_nextint(&line, &cid);
366 for (i = 0; i < n; i++) {
367 if (responses[i].cid == cid)
372 /* details for a context we didn't hear about in the last request */
376 err = at_tok_nextstr(&line, &out);
380 responses[i].type = alloca(strlen(out) + 1);
381 strcpy(responses[i].type, out);
383 err = at_tok_nextstr(&line, &out);
387 responses[i].apn = alloca(strlen(out) + 1);
388 strcpy(responses[i].apn, out);
390 err = at_tok_nextstr(&line, &out);
394 responses[i].address = alloca(strlen(out) + 1);
395 strcpy(responses[i].address, out);
398 at_response_free(p_response);
401 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
402 n * sizeof(RIL_Data_Call_Response));
404 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
406 n * sizeof(RIL_Data_Call_Response));
412 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
414 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
417 at_response_free(p_response);
420 static void requestQueryNetworkSelectionMode(
421 void *data, size_t datalen, RIL_Token t)
424 ATResponse *p_response = NULL;
428 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
430 if (err < 0 || p_response->success == 0) {
434 line = p_response->p_intermediates->line;
436 err = at_tok_start(&line);
442 err = at_tok_nextint(&line, &response);
448 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
449 at_response_free(p_response);
452 at_response_free(p_response);
453 LOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
454 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
457 static void sendCallStateChanged(void *param)
459 RIL_onUnsolicitedResponse (
460 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
464 static void requestGetCurrentCalls(void *data, size_t datalen, RIL_Token t)
467 ATResponse *p_response;
471 RIL_CallOld *p_calls;
472 RIL_CallOld **pp_calls;
476 #ifdef WORKAROUND_ERRONEOUS_ANSWER
477 int prevIncomingOrWaitingLine;
479 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
480 s_incomingOrWaitingLine = -1;
481 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
483 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
485 if (err != 0 || p_response->success == 0) {
486 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
490 /* count the calls */
491 for (countCalls = 0, p_cur = p_response->p_intermediates
493 ; p_cur = p_cur->p_next
498 /* yes, there's an array of pointers and then an array of structures */
500 pp_calls = (RIL_CallOld **)alloca(countCalls * sizeof(RIL_CallOld *));
501 p_calls = (RIL_CallOld *)alloca(countCalls * sizeof(RIL_CallOld));
502 memset (p_calls, 0, countCalls * sizeof(RIL_CallOld));
504 /* init the pointer array */
505 for(i = 0; i < countCalls ; i++) {
506 pp_calls[i] = &(p_calls[i]);
509 for (countValidCalls = 0, p_cur = p_response->p_intermediates
511 ; p_cur = p_cur->p_next
513 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
519 #ifdef WORKAROUND_ERRONEOUS_ANSWER
520 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
521 || p_calls[countValidCalls].state == RIL_CALL_WAITING
523 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
525 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
527 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
528 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
536 #ifdef WORKAROUND_ERRONEOUS_ANSWER
538 // A call was incoming or waiting
539 // Now it's marked as active
540 // But we never answered it
542 // This is probably a bug, and the call will probably
543 // disappear from the call list in the next poll
544 if (prevIncomingOrWaitingLine >= 0
545 && s_incomingOrWaitingLine < 0
546 && s_expectAnswer == 0
548 for (i = 0; i < countValidCalls ; i++) {
550 if (p_calls[i].index == prevIncomingOrWaitingLine
551 && p_calls[i].state == RIL_CALL_ACTIVE
552 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
555 "Hit WORKAROUND_ERRONOUS_ANSWER case."
556 " Repoll count: %d\n", s_repollCallsCount);
557 s_repollCallsCount++;
564 s_repollCallsCount = 0;
565 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
567 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
568 countValidCalls * sizeof (RIL_CallOld *));
570 at_response_free(p_response);
572 #ifdef POLL_CALL_STATE
573 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
574 // smd, so we're forced to poll until the call ends.
578 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
583 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
584 at_response_free(p_response);
587 static void requestDial(void *data, size_t datalen, RIL_Token t)
594 p_dial = (RIL_Dial *)data;
596 switch (p_dial->clir) {
597 case 1: clir = "I"; break; /*invocation*/
598 case 2: clir = "i"; break; /*suppression*/
600 case 0: clir = ""; break; /*subscription default*/
603 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
605 ret = at_send_command(cmd, NULL);
609 /* success or failure is ignored by the upper layer here.
610 it will call GET_CURRENT_CALLS and determine success that way */
611 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
614 static void requestWriteSmsToSim(void *data, size_t datalen, RIL_Token t)
616 RIL_SMS_WriteArgs *p_args;
620 ATResponse *p_response = NULL;
622 p_args = (RIL_SMS_WriteArgs *)data;
624 length = strlen(p_args->pdu)/2;
625 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
627 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
629 if (err != 0 || p_response->success == 0) goto error;
631 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
632 at_response_free(p_response);
636 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
637 at_response_free(p_response);
640 static void requestHangup(void *data, size_t datalen, RIL_Token t)
647 p_line = (int *)data;
650 // "Releases a specific active call X"
651 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
653 ret = at_send_command(cmd, NULL);
657 /* success or failure is ignored by the upper layer here.
658 it will call GET_CURRENT_CALLS and determine success that way */
659 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
662 static void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
664 ATResponse *p_response = NULL;
669 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
671 if (err < 0 || p_response->success == 0) {
672 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
676 line = p_response->p_intermediates->line;
678 err = at_tok_start(&line);
679 if (err < 0) goto error;
681 err = at_tok_nextint(&line, &(response[0]));
682 if (err < 0) goto error;
684 err = at_tok_nextint(&line, &(response[1]));
685 if (err < 0) goto error;
687 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
689 at_response_free(p_response);
693 LOGE("requestSignalStrength must never return an error when radio is on");
694 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
695 at_response_free(p_response);
698 static void requestRegistrationState(int request, void *data,
699 size_t datalen, RIL_Token t)
703 char * responseStr[4];
704 ATResponse *p_response = NULL;
713 if (request == RIL_REQUEST_REGISTRATION_STATE) {
716 } else if (request == RIL_REQUEST_GPRS_REGISTRATION_STATE) {
724 err = at_send_command_singleline(cmd, prefix, &p_response);
726 if (err != 0) goto error;
728 line = p_response->p_intermediates->line;
730 err = at_tok_start(&line);
731 if (err < 0) goto error;
733 /* Ok you have to be careful here
734 * The solicited version of the CREG response is
735 * +CREG: n, stat, [lac, cid]
736 * and the unsolicited version is
737 * +CREG: stat, [lac, cid]
738 * The <n> parameter is basically "is unsolicited creg on?"
739 * which it should always be
741 * Now we should normally get the solicited version here,
742 * but the unsolicited version could have snuck in
743 * so we have to handle both
745 * Also since the LAC and CID are only reported when registered,
746 * we can have 1, 2, 3, or 4 arguments here
748 * finally, a +CGREG: answer may have a fifth value that corresponds
749 * to the network type, as in;
751 * +CGREG: n, stat [,lac, cid [,networkType]]
754 /* count number of commas */
756 for (p = line ; *p != '\0' ;p++) {
757 if (*p == ',') commas++;
761 case 0: /* +CREG: <stat> */
762 err = at_tok_nextint(&line, &response[0]);
763 if (err < 0) goto error;
768 case 1: /* +CREG: <n>, <stat> */
769 err = at_tok_nextint(&line, &skip);
770 if (err < 0) goto error;
771 err = at_tok_nextint(&line, &response[0]);
772 if (err < 0) goto error;
775 if (err < 0) goto error;
778 case 2: /* +CREG: <stat>, <lac>, <cid> */
779 err = at_tok_nextint(&line, &response[0]);
780 if (err < 0) goto error;
781 err = at_tok_nexthexint(&line, &response[1]);
782 if (err < 0) goto error;
783 err = at_tok_nexthexint(&line, &response[2]);
784 if (err < 0) goto error;
786 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
787 err = at_tok_nextint(&line, &skip);
788 if (err < 0) goto error;
789 err = at_tok_nextint(&line, &response[0]);
790 if (err < 0) goto error;
791 err = at_tok_nexthexint(&line, &response[1]);
792 if (err < 0) goto error;
793 err = at_tok_nexthexint(&line, &response[2]);
794 if (err < 0) goto error;
796 /* special case for CGREG, there is a fourth parameter
797 * that is the network type (unknown/gprs/edge/umts)
799 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
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;
808 err = at_tok_nexthexint(&line, &response[3]);
809 if (err < 0) goto error;
816 asprintf(&responseStr[0], "%d", response[0]);
817 asprintf(&responseStr[1], "%d", response[1]);
818 asprintf(&responseStr[2], "%d", response[2]);
821 asprintf(&responseStr[3], "%d", response[3]);
823 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
824 at_response_free(p_response);
828 LOGE("requestRegistrationState must never return an error when radio is on");
829 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
830 at_response_free(p_response);
833 static void requestOperator(void *data, size_t datalen, RIL_Token t)
841 memset(response, 0, sizeof(response));
843 ATResponse *p_response = NULL;
845 err = at_send_command_multiline(
846 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
847 "+COPS:", &p_response);
849 /* we expect 3 lines here:
850 * +COPS: 0,0,"T - Mobile"
852 * +COPS: 0,2,"310170"
855 if (err != 0) goto error;
857 for (i = 0, p_cur = p_response->p_intermediates
859 ; p_cur = p_cur->p_next, i++
861 char *line = p_cur->line;
863 err = at_tok_start(&line);
864 if (err < 0) goto error;
866 err = at_tok_nextint(&line, &skip);
867 if (err < 0) goto error;
869 // If we're unregistered, we may just get
870 // a "+COPS: 0" response
871 if (!at_tok_hasmore(&line)) {
876 err = at_tok_nextint(&line, &skip);
877 if (err < 0) goto error;
879 // a "+COPS: 0, n" response is also possible
880 if (!at_tok_hasmore(&line)) {
885 err = at_tok_nextstr(&line, &(response[i]));
886 if (err < 0) goto error;
890 /* expect 3 lines exactly */
894 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
895 at_response_free(p_response);
899 LOGE("requestOperator must not return error when radio is on");
900 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
901 at_response_free(p_response);
904 static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
911 RIL_SMS_Response response;
912 ATResponse *p_response = NULL;
914 smsc = ((const char **)data)[0];
915 pdu = ((const char **)data)[1];
917 tpLayerLength = strlen(pdu)/2;
919 // "NULL for default SMSC"
924 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
925 asprintf(&cmd2, "%s%s", smsc, pdu);
927 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
929 if (err != 0 || p_response->success == 0) goto error;
931 memset(&response, 0, sizeof(response));
933 /* FIXME fill in messageRef and ackPDU */
935 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
936 at_response_free(p_response);
940 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
941 at_response_free(p_response);
944 static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
949 ATResponse *p_response = NULL;
950 char *response[2] = { "1", PPP_TTY_PATH };
952 apn = ((const char **)data)[2];
954 #ifdef USE_TI_COMMANDS
955 // Config for multislot class 10 (probably default anyway eh?)
956 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
959 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
960 #endif /* USE_TI_COMMANDS */
965 ssize_t written, rlen;
966 char status[32] = {0};
969 LOGD("requesting data connection to APN '%s'", apn);
971 fd = open ("/dev/qmi", O_RDWR);
972 if (fd >= 0) { /* the device doesn't exist on the emulator */
974 LOGD("opened the qmi device\n");
975 asprintf(&cmd, "up:%s", apn);
980 written = write (fd, cmd + cur, len - cur);
981 } while (written < 0 && errno == EINTR);
984 LOGE("### ERROR writing to /dev/qmi");
992 // wait for interface to come online
997 rlen = read(fd, status, 31);
998 } while (rlen < 0 && errno == EINTR);
1001 LOGE("### ERROR reading from /dev/qmi");
1005 status[rlen] = '\0';
1006 LOGD("### status: %s", status);
1008 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1013 LOGE("### Failed to get data connection up\n");
1017 qmistatus = system("netcfg rmnet0 dhcp");
1019 LOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1021 if (qmistatus < 0) goto error;
1025 asprintf(&cmd, "AT+CGDCONT=1,\"IP\",\"%s\",,0,0", apn);
1026 //FIXME check for error here
1027 err = at_send_command(cmd, NULL);
1030 // Set required QoS params to default
1031 err = at_send_command("AT+CGQREQ=1", NULL);
1033 // Set minimum QoS params to default
1034 err = at_send_command("AT+CGQMIN=1", NULL);
1036 // packet-domain event reporting
1037 err = at_send_command("AT+CGEREP=1,0", NULL);
1039 // Hangup anything that's happening there now
1040 err = at_send_command("AT+CGACT=1,0", NULL);
1042 // Start data on PDP context 1
1043 err = at_send_command("ATD*99***1#", &p_response);
1045 if (err < 0 || p_response->success == 0) {
1050 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1051 at_response_free(p_response);
1055 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1056 at_response_free(p_response);
1060 static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1065 ackSuccess = ((int *)data)[0];
1067 if (ackSuccess == 1) {
1068 err = at_send_command("AT+CNMA=1", NULL);
1069 } else if (ackSuccess == 0) {
1070 err = at_send_command("AT+CNMA=2", NULL);
1072 LOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1076 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1078 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1082 static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1084 ATResponse *p_response = NULL;
1085 RIL_SIM_IO_Response sr;
1091 memset(&sr, 0, sizeof(sr));
1093 p_args = (RIL_SIM_IO *)data;
1095 /* FIXME handle pin2 */
1097 if (p_args->data == NULL) {
1098 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1099 p_args->command, p_args->fileid,
1100 p_args->p1, p_args->p2, p_args->p3);
1102 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1103 p_args->command, p_args->fileid,
1104 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1107 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1109 if (err < 0 || p_response->success == 0) {
1113 line = p_response->p_intermediates->line;
1115 err = at_tok_start(&line);
1116 if (err < 0) goto error;
1118 err = at_tok_nextint(&line, &(sr.sw1));
1119 if (err < 0) goto error;
1121 err = at_tok_nextint(&line, &(sr.sw2));
1122 if (err < 0) goto error;
1124 if (at_tok_hasmore(&line)) {
1125 err = at_tok_nextstr(&line, &(sr.simResponse));
1126 if (err < 0) goto error;
1129 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1130 at_response_free(p_response);
1135 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1136 at_response_free(p_response);
1141 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1143 ATResponse *p_response = NULL;
1146 const char** strings = (const char**)data;;
1148 if ( datalen == sizeof(char*) ) {
1149 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1150 } else if ( datalen == 2*sizeof(char*) ) {
1151 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1155 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1158 if (err < 0 || p_response->success == 0) {
1160 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1162 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1164 at_response_free(p_response);
1168 static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1170 const char *ussdRequest;
1172 ussdRequest = (char *)(data);
1175 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1182 /*** Callback methods from the RIL library to us ***/
1185 * Call from RIL to us to make a RIL_REQUEST
1187 * Must be completed with a call to RIL_onRequestComplete()
1189 * RIL_onRequestComplete() may be called from any thread, before or after
1190 * this function returns.
1192 * Will always be called from the same thread, so returning here implies
1193 * that the radio is ready to process another command (whether or not
1194 * the previous command has completed).
1197 onRequest (int request, void *data, size_t datalen, RIL_Token t)
1199 ATResponse *p_response;
1202 LOGD("onRequest: %s", requestToString(request));
1204 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1205 * when RADIO_STATE_UNAVAILABLE.
1207 if (sState == RADIO_STATE_UNAVAILABLE
1208 && request != RIL_REQUEST_GET_SIM_STATUS
1210 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1214 /* Ignore all non-power requests when RADIO_STATE_OFF
1215 * (except RIL_REQUEST_GET_SIM_STATUS)
1217 if (sState == RADIO_STATE_OFF
1218 && !(request == RIL_REQUEST_RADIO_POWER
1219 || request == RIL_REQUEST_GET_SIM_STATUS)
1221 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1226 case RIL_REQUEST_GET_SIM_STATUS: {
1229 simStatus = getSIMStatus();
1231 RIL_onRequestComplete(t, RIL_E_SUCCESS, &simStatus, sizeof(simStatus));
1234 case RIL_REQUEST_GET_CURRENT_CALLS:
1235 requestGetCurrentCalls(data, datalen, t);
1237 case RIL_REQUEST_DIAL:
1238 requestDial(data, datalen, t);
1240 case RIL_REQUEST_HANGUP:
1241 requestHangup(data, datalen, t);
1243 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1244 // 3GPP 22.030 6.5.5
1245 // "Releases all held calls or sets User Determined User Busy
1246 // (UDUB) for a waiting call."
1247 at_send_command("AT+CHLD=0", NULL);
1249 /* success or failure is ignored by the upper layer here.
1250 it will call GET_CURRENT_CALLS and determine success that way */
1251 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1253 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1254 // 3GPP 22.030 6.5.5
1255 // "Releases all active calls (if any exist) and accepts
1256 // the other (held or waiting) call."
1257 at_send_command("AT+CHLD=1", NULL);
1259 /* success or failure is ignored by the upper layer here.
1260 it will call GET_CURRENT_CALLS and determine success that way */
1261 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1263 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1264 // 3GPP 22.030 6.5.5
1265 // "Places all active calls (if any exist) on hold and accepts
1266 // the other (held or waiting) call."
1267 at_send_command("AT+CHLD=2", NULL);
1269 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1271 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1273 /* success or failure is ignored by the upper layer here.
1274 it will call GET_CURRENT_CALLS and determine success that way */
1275 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1277 case RIL_REQUEST_ANSWER:
1278 at_send_command("ATA", NULL);
1280 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1282 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1284 /* success or failure is ignored by the upper layer here.
1285 it will call GET_CURRENT_CALLS and determine success that way */
1286 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1288 case RIL_REQUEST_CONFERENCE:
1289 // 3GPP 22.030 6.5.5
1290 // "Adds a held call to the conversation"
1291 at_send_command("AT+CHLD=3", NULL);
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_UDUB:
1298 /* user determined user busy */
1299 /* sometimes used: ATH */
1300 at_send_command("ATH", NULL);
1302 /* success or failure is ignored by the upper layer here.
1303 it will call GET_CURRENT_CALLS and determine success that way */
1304 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1307 case RIL_REQUEST_SEPARATE_CONNECTION:
1310 int party = ((int*)data)[0];
1312 // Make sure that party is in a valid range.
1313 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1314 // It's sufficient for us to just make sure it's single digit.)
1315 if (party > 0 && party < 10) {
1316 sprintf(cmd, "AT+CHLD=2%d", party);
1317 at_send_command(cmd, NULL);
1318 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1320 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1325 case RIL_REQUEST_SIGNAL_STRENGTH:
1326 requestSignalStrength(data, datalen, t);
1328 case RIL_REQUEST_REGISTRATION_STATE:
1329 case RIL_REQUEST_GPRS_REGISTRATION_STATE:
1330 requestRegistrationState(request, data, datalen, t);
1332 case RIL_REQUEST_OPERATOR:
1333 requestOperator(data, datalen, t);
1335 case RIL_REQUEST_RADIO_POWER:
1336 requestRadioPower(data, datalen, t);
1338 case RIL_REQUEST_DTMF: {
1339 char c = ((char *)data)[0];
1341 asprintf(&cmd, "AT+VTS=%c", (int)c);
1342 at_send_command(cmd, NULL);
1344 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1347 case RIL_REQUEST_SEND_SMS:
1348 requestSendSMS(data, datalen, t);
1350 case RIL_REQUEST_SETUP_DATA_CALL:
1351 requestSetupDataCall(data, datalen, t);
1353 case RIL_REQUEST_SMS_ACKNOWLEDGE:
1354 requestSMSAcknowledge(data, datalen, t);
1357 case RIL_REQUEST_GET_IMSI:
1359 err = at_send_command_numeric("AT+CIMI", &p_response);
1361 if (err < 0 || p_response->success == 0) {
1362 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1364 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1365 p_response->p_intermediates->line, sizeof(char *));
1367 at_response_free(p_response);
1370 case RIL_REQUEST_GET_IMEI:
1372 err = at_send_command_numeric("AT+CGSN", &p_response);
1374 if (err < 0 || p_response->success == 0) {
1375 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1377 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1378 p_response->p_intermediates->line, sizeof(char *));
1380 at_response_free(p_response);
1383 case RIL_REQUEST_SIM_IO:
1384 requestSIM_IO(data,datalen,t);
1387 case RIL_REQUEST_SEND_USSD:
1388 requestSendUSSD(data, datalen, t);
1391 case RIL_REQUEST_CANCEL_USSD:
1393 err = at_send_command_numeric("AT+CUSD=2", &p_response);
1395 if (err < 0 || p_response->success == 0) {
1396 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1398 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1399 p_response->p_intermediates->line, sizeof(char *));
1401 at_response_free(p_response);
1404 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
1405 at_send_command("AT+COPS=0", NULL);
1408 case RIL_REQUEST_DATA_CALL_LIST:
1409 requestDataCallList(data, datalen, t);
1412 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
1413 requestQueryNetworkSelectionMode(data, datalen, t);
1416 case RIL_REQUEST_OEM_HOOK_RAW:
1418 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1422 case RIL_REQUEST_OEM_HOOK_STRINGS: {
1426 LOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
1429 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
1430 i > 0 ; cur++, i --) {
1431 LOGD("> '%s'", *cur);
1434 // echo back strings
1435 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1439 case RIL_REQUEST_WRITE_SMS_TO_SIM:
1440 requestWriteSmsToSim(data, datalen, t);
1443 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
1446 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
1447 err = at_send_command(cmd, &p_response);
1449 if (err < 0 || p_response->success == 0) {
1450 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1452 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1454 at_response_free(p_response);
1458 case RIL_REQUEST_ENTER_SIM_PIN:
1459 case RIL_REQUEST_ENTER_SIM_PUK:
1460 case RIL_REQUEST_ENTER_SIM_PIN2:
1461 case RIL_REQUEST_ENTER_SIM_PUK2:
1462 case RIL_REQUEST_CHANGE_SIM_PIN:
1463 case RIL_REQUEST_CHANGE_SIM_PIN2:
1464 requestEnterSimPin(data, datalen, t);
1468 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1474 * Synchronous call from the RIL to us to return current radio state.
1475 * RADIO_STATE_UNAVAILABLE should be the initial state.
1477 static RIL_RadioState
1483 * Call from RIL to us to find out whether a specific request code
1484 * is supported by this implementation.
1486 * Return 1 for "supported" and 0 for "unsupported"
1490 onSupports (int requestCode)
1497 static void onCancel (RIL_Token t)
1503 static const char * getVersion(void)
1505 return "android reference-ril 1.0";
1509 setRadioState(RIL_RadioState newState)
1511 RIL_RadioState oldState;
1513 pthread_mutex_lock(&s_state_mutex);
1518 // If we're closed, the only reasonable state is
1519 // RADIO_STATE_UNAVAILABLE
1520 // This is here because things on the main thread
1521 // may attempt to change the radio state after the closed
1522 // event happened in another thread
1523 newState = RADIO_STATE_UNAVAILABLE;
1526 if (sState != newState || s_closed > 0) {
1529 pthread_cond_broadcast (&s_state_cond);
1532 pthread_mutex_unlock(&s_state_mutex);
1535 /* do these outside of the mutex */
1536 if (sState != oldState) {
1537 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1540 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
1541 * from the AT reader thread
1542 * Currently, this doesn't happen, but if that changes then these
1543 * will need to be dispatched on the request thread
1545 if (sState == RADIO_STATE_SIM_READY) {
1547 } else if (sState == RADIO_STATE_SIM_NOT_READY) {
1553 /** returns one of RIM_SIM_*. Returns RIL_SIM_NOT_READY on error */
1557 ATResponse *p_response = NULL;
1563 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
1564 ret = RIL_SIM_NOT_READY;
1568 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
1571 ret = RIL_SIM_NOT_READY;
1575 switch (at_get_cme_error(p_response)) {
1579 case CME_SIM_NOT_INSERTED:
1580 ret = RIL_SIM_ABSENT;
1584 ret = RIL_SIM_NOT_READY;
1588 /* CPIN? has succeeded, now look at the result */
1590 cpinLine = p_response->p_intermediates->line;
1591 err = at_tok_start (&cpinLine);
1594 ret = RIL_SIM_NOT_READY;
1598 err = at_tok_nextstr(&cpinLine, &cpinResult);
1601 ret = RIL_SIM_NOT_READY;
1605 if (0 == strcmp (cpinResult, "SIM PIN")) {
1608 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
1611 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
1612 return RIL_SIM_NETWORK_PERSONALIZATION;
1613 } else if (0 != strcmp (cpinResult, "READY")) {
1614 /* we're treating unsupported lock types as "sim absent" */
1615 ret = RIL_SIM_ABSENT;
1619 at_response_free(p_response);
1623 ret = RIL_SIM_READY;
1626 at_response_free(p_response);
1632 * SIM ready means any commands that access the SIM will work, including:
1633 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
1634 * (all SMS-related commands)
1637 static void pollSIMState (void *param)
1639 ATResponse *p_response;
1642 if (sState != RADIO_STATE_SIM_NOT_READY) {
1643 // no longer valid to poll
1647 switch(getSIMStatus()) {
1648 case RIL_SIM_ABSENT:
1651 case RIL_SIM_NETWORK_PERSONALIZATION:
1653 setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
1656 case RIL_SIM_NOT_READY:
1657 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
1661 setRadioState(RADIO_STATE_SIM_READY);
1666 /** returns 1 if on, 0 if off, and -1 on error */
1667 static int isRadioOn()
1669 ATResponse *p_response = NULL;
1674 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
1676 if (err < 0 || p_response->success == 0) {
1677 // assume radio is off
1681 line = p_response->p_intermediates->line;
1683 err = at_tok_start(&line);
1684 if (err < 0) goto error;
1686 err = at_tok_nextbool(&line, &ret);
1687 if (err < 0) goto error;
1689 at_response_free(p_response);
1695 at_response_free(p_response);
1700 * Initialize everything that can be configured while we're still in
1703 static void initializeCallback(void *param)
1705 ATResponse *p_response = NULL;
1708 setRadioState (RADIO_STATE_OFF);
1712 /* note: we don't check errors here. Everything important will
1713 be handled in onATTimeout and onATReaderClosed */
1715 /* atchannel is tolerant of echo but it must */
1716 /* have verbose result codes */
1717 at_send_command("ATE0Q0V1", NULL);
1719 /* No auto-answer */
1720 at_send_command("ATS0=0", NULL);
1722 /* Extended errors */
1723 at_send_command("AT+CMEE=1", NULL);
1725 /* Network registration events */
1726 err = at_send_command("AT+CREG=2", &p_response);
1728 /* some handsets -- in tethered mode -- don't support CREG=2 */
1729 if (err < 0 || p_response->success == 0) {
1730 at_send_command("AT+CREG=1", NULL);
1733 at_response_free(p_response);
1735 /* GPRS registration events */
1736 at_send_command("AT+CGREG=1", NULL);
1738 /* Call Waiting notifications */
1739 at_send_command("AT+CCWA=1", NULL);
1741 /* Alternating voice/data off */
1742 at_send_command("AT+CMOD=0", NULL);
1745 at_send_command("AT+CMUT=0", NULL);
1747 /* +CSSU unsolicited supp service notifications */
1748 at_send_command("AT+CSSN=0,1", NULL);
1750 /* no connected line identification */
1751 at_send_command("AT+COLP=0", NULL);
1753 /* HEX character set */
1754 at_send_command("AT+CSCS=\"HEX\"", NULL);
1756 /* USSD unsolicited */
1757 at_send_command("AT+CUSD=1", NULL);
1759 /* Enable +CGEV GPRS event notifications, but don't buffer */
1760 at_send_command("AT+CGEREP=1,0", NULL);
1763 at_send_command("AT+CMGF=0", NULL);
1765 #ifdef USE_TI_COMMANDS
1767 at_send_command("AT%CPI=3", NULL);
1769 /* TI specific -- notifications when SMS is ready (currently ignored) */
1770 at_send_command("AT%CSTAT=1", NULL);
1772 #endif /* USE_TI_COMMANDS */
1775 /* assume radio is off on error */
1776 if (isRadioOn() > 0) {
1777 setRadioState (RADIO_STATE_SIM_NOT_READY);
1781 static void waitForClose()
1783 pthread_mutex_lock(&s_state_mutex);
1785 while (s_closed == 0) {
1786 pthread_cond_wait(&s_state_cond, &s_state_mutex);
1789 pthread_mutex_unlock(&s_state_mutex);
1793 * Called by atchannel when an unsolicited line appears
1794 * This is called on atchannel's reader thread. AT commands may
1795 * not be issued here
1797 static void onUnsolicited (const char *s, const char *sms_pdu)
1802 /* Ignore unsolicited responses until we're initialized.
1803 * This is OK because the RIL library will poll for initial state
1805 if (sState == RADIO_STATE_UNAVAILABLE) {
1809 if (strStartsWith(s, "%CTZV:")) {
1810 /* TI specific -- NITZ time */
1814 at_tok_start(&line);
1816 err = at_tok_nextstr(&line, &response);
1819 LOGE("invalid NITZ line %s\n", s);
1821 RIL_onUnsolicitedResponse (
1822 RIL_UNSOL_NITZ_TIME_RECEIVED,
1823 response, strlen(response));
1825 } else if (strStartsWith(s,"+CRING:")
1826 || strStartsWith(s,"RING")
1827 || strStartsWith(s,"NO CARRIER")
1828 || strStartsWith(s,"+CCWA")
1830 RIL_onUnsolicitedResponse (
1831 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
1833 #ifdef WORKAROUND_FAKE_CGEV
1834 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
1835 #endif /* WORKAROUND_FAKE_CGEV */
1836 } else if (strStartsWith(s,"+CREG:")
1837 || strStartsWith(s,"+CGREG:")
1839 RIL_onUnsolicitedResponse (
1840 RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
1842 #ifdef WORKAROUND_FAKE_CGEV
1843 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1844 #endif /* WORKAROUND_FAKE_CGEV */
1845 } else if (strStartsWith(s, "+CMT:")) {
1846 RIL_onUnsolicitedResponse (
1847 RIL_UNSOL_RESPONSE_NEW_SMS,
1848 sms_pdu, strlen(sms_pdu));
1849 } else if (strStartsWith(s, "+CDS:")) {
1850 RIL_onUnsolicitedResponse (
1851 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
1852 sms_pdu, strlen(sms_pdu));
1853 } else if (strStartsWith(s, "+CGEV:")) {
1854 /* Really, we can ignore NW CLASS and ME CLASS events here,
1855 * but right now we don't since extranous
1856 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
1858 /* can't issue AT commands here -- call on main thread */
1859 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1860 #ifdef WORKAROUND_FAKE_CGEV
1861 } else if (strStartsWith(s, "+CME ERROR: 150")) {
1862 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
1863 #endif /* WORKAROUND_FAKE_CGEV */
1867 /* Called on command or reader thread */
1868 static void onATReaderClosed()
1870 LOGI("AT channel closed\n");
1874 setRadioState (RADIO_STATE_UNAVAILABLE);
1877 /* Called on command thread */
1878 static void onATTimeout()
1880 LOGI("AT channel timeout; closing\n");
1885 /* FIXME cause a radio reset here */
1887 setRadioState (RADIO_STATE_UNAVAILABLE);
1890 static void usage(char *s)
1893 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
1895 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
1901 mainLoop(void *param)
1906 AT_DUMP("== ", "entering mainLoop()", -1 );
1907 at_set_on_reader_closed(onATReaderClosed);
1908 at_set_on_timeout(onATTimeout);
1914 fd = socket_loopback_client(s_port, SOCK_STREAM);
1915 } else if (s_device_socket) {
1916 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
1917 /* Qemu-specific control socket */
1918 fd = socket_local_client( "qemud",
1919 ANDROID_SOCKET_NAMESPACE_RESERVED,
1924 if ( write(fd, "gsm", 3) != 3 ||
1925 read(fd, answer, 2) != 2 ||
1926 memcmp(answer, "OK", 2) != 0)
1934 fd = socket_local_client( s_device_path,
1935 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
1937 } else if (s_device_path != NULL) {
1938 fd = open (s_device_path, O_RDWR);
1939 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
1940 /* disable echo on serial ports */
1942 tcgetattr( fd, &ios );
1943 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
1944 tcsetattr( fd, TCSANOW, &ios );
1949 perror ("opening AT interface. retrying...");
1956 ret = at_open(fd, onUnsolicited);
1959 LOGE ("AT error %d on at_open\n", ret);
1963 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
1965 // Give initializeCallback a chance to dispatched, since
1966 // we don't presently have a cancellation mechanism
1970 LOGI("Re-opening after close");
1976 pthread_t s_tid_mainloop;
1978 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
1983 pthread_attr_t attr;
1987 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
1990 s_port = atoi(optarg);
1995 LOGI("Opening loopback port %d\n", s_port);
1999 s_device_path = optarg;
2000 LOGI("Opening tty device %s\n", s_device_path);
2004 s_device_path = optarg;
2005 s_device_socket = 1;
2006 LOGI("Opening socket %s\n", s_device_path);
2015 if (s_port < 0 && s_device_path == NULL) {
2020 pthread_attr_init (&attr);
2021 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2022 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
2024 return &s_callbacks;
2026 #else /* RIL_SHLIB */
2027 int main (int argc, char **argv)
2033 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
2036 s_port = atoi(optarg);
2040 LOGI("Opening loopback port %d\n", s_port);
2044 s_device_path = optarg;
2045 LOGI("Opening tty device %s\n", s_device_path);
2049 s_device_path = optarg;
2050 s_device_socket = 1;
2051 LOGI("Opening socket %s\n", s_device_path);
2059 if (s_port < 0 && s_device_path == NULL) {
2063 RIL_register(&s_callbacks);
2070 #endif /* RIL_SHLIB */