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_cdma_sms.h>
24 #include <sys/types.h>
29 #include "atchannel.h"
33 #include <sys/socket.h>
34 #include <cutils/sockets.h>
36 #include <sys/system_properties.h>
39 #include "hardware/qemu_pipe.h"
42 #include <utils/Log.h>
44 #define MAX_AT_RESPONSE 0x1000
46 /* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
47 #define PPP_TTY_PATH "eth0"
49 #ifdef USE_TI_COMMANDS
51 // Enable a workaround
52 // 1) Make incoming call, do not answer
53 // 2) Hangup remote end
54 // Expected: call should disappear from CLCC line
55 // Actual: Call shows as "ACTIVE" before disappearing
56 #define WORKAROUND_ERRONEOUS_ANSWER 1
58 // Some varients of the TI stack do not support the +CGEV unsolicited
59 // response. However, they seem to send an unsolicited +CME ERROR: 150
60 #define WORKAROUND_FAKE_CGEV 1
63 /* Modem Technology bits */
65 #define MDM_WCDMA 0x02
71 int supportedTechs; // Bitmask of supported Modem Technology bits
72 int currentTech; // Technology the modem is currently using (in the format used by modem)
75 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
76 // in which the byte number from LSB to MSB give the priority.
79 // value: |00 |00 |00 |00
80 // byte #: |3 |2 |1 |0
82 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
83 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
84 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
85 int32_t preferredNetworkMode;
86 int subscription_source;
90 static ModemInfo *sMdmInfo;
91 // TECH returns the current technology in the format used by the modem.
92 // It can be used as an l-value
93 #define TECH(mdminfo) ((mdminfo)->currentTech)
94 // TECH_BIT returns the bitmask equivalent of the current tech
95 #define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
96 #define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
97 #define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
98 #define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
99 // CDMA Subscription Source
100 #define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
102 static int net2modem[] = {
103 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
104 MDM_GSM, // 1 - GSM only
105 MDM_WCDMA, // 2 - WCDMA only
106 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
107 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
108 MDM_CDMA, // 5 - CDMA only
109 MDM_EVDO, // 6 - EvDo only
110 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
111 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
112 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
113 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
114 MDM_LTE, // 11 - LTE only
117 static int32_t net2pmask[] = {
118 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
119 MDM_GSM, // 1 - GSM only
120 MDM_WCDMA, // 2 - WCDMA only
121 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
122 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
123 MDM_CDMA, // 5 - CDMA only
124 MDM_EVDO, // 6 - EvDo only
125 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
126 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
127 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
128 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
129 MDM_LTE, // 11 - LTE only
132 static int is3gpp2(int radioTech) {
134 case RADIO_TECH_IS95A:
135 case RADIO_TECH_IS95B:
136 case RADIO_TECH_1xRTT:
137 case RADIO_TECH_EVDO_0:
138 case RADIO_TECH_EVDO_A:
139 case RADIO_TECH_EVDO_B:
140 case RADIO_TECH_EHRPD:
150 SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
153 SIM_NETWORK_PERSONALIZATION = 5,
159 RUIM_NETWORK_PERSONALIZATION = 11
162 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
163 static RIL_RadioState currentState();
164 static int onSupports (int requestCode);
165 static void onCancel (RIL_Token t);
166 static const char *getVersion();
167 static int isRadioOn();
168 static SIM_Status getSIMStatus();
169 static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
170 static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
171 static void onDataCallListChanged(void *param);
173 extern const char * requestToString(int request);
175 /*** Static Variables ***/
176 static const RIL_RadioFunctions s_callbacks = {
186 static const struct RIL_Env *s_rilenv;
188 #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
189 #define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
190 #define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
193 static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
195 static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
196 static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
198 static int s_port = -1;
199 static const char * s_device_path = NULL;
200 static int s_device_socket = 0;
202 /* trigger change to this with s_state_cond */
203 static int s_closed = 0;
205 static int sFD; /* file desc of AT channel */
206 static char sATBuffer[MAX_AT_RESPONSE+1];
207 static char *sATBufferCur = NULL;
209 static const struct timeval TIMEVAL_SIMPOLL = {1,0};
210 static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
211 static const struct timeval TIMEVAL_0 = {0,0};
213 #ifdef WORKAROUND_ERRONEOUS_ANSWER
214 // Max number of times we'll try to repoll when we think
215 // we have a AT+CLCC race condition
216 #define REPOLL_CALLS_COUNT_MAX 4
218 // Line index that was incoming or waiting at last poll, or -1 for none
219 static int s_incomingOrWaitingLine = -1;
220 // Number of times we've asked for a repoll of AT+CLCC
221 static int s_repollCallsCount = 0;
222 // Should we expect a call to be answered in the next CLCC?
223 static int s_expectAnswer = 0;
224 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
226 static void pollSIMState (void *param);
227 static void setRadioState(RIL_RadioState newState);
228 static void setRadioTechnology(ModemInfo *mdm, int newtech);
229 static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
230 static int parse_technology_response(const char *response, int *current, int32_t *preferred);
231 static int techFromModemType(int mdmtype);
233 static int clccStateToRILState(int state, RIL_CallState *p_state)
237 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
238 case 1: *p_state = RIL_CALL_HOLDING; return 0;
239 case 2: *p_state = RIL_CALL_DIALING; return 0;
240 case 3: *p_state = RIL_CALL_ALERTING; return 0;
241 case 4: *p_state = RIL_CALL_INCOMING; return 0;
242 case 5: *p_state = RIL_CALL_WAITING; return 0;
248 * Note: directly modified line and has *p_call point directly into
251 static int callFromCLCCLine(char *line, RIL_Call *p_call)
253 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
254 // index,isMT,state,mode,isMpty(,number,TOA)?
260 err = at_tok_start(&line);
261 if (err < 0) goto error;
263 err = at_tok_nextint(&line, &(p_call->index));
264 if (err < 0) goto error;
266 err = at_tok_nextbool(&line, &(p_call->isMT));
267 if (err < 0) goto error;
269 err = at_tok_nextint(&line, &state);
270 if (err < 0) goto error;
272 err = clccStateToRILState(state, &(p_call->state));
273 if (err < 0) goto error;
275 err = at_tok_nextint(&line, &mode);
276 if (err < 0) goto error;
278 p_call->isVoice = (mode == 0);
280 err = at_tok_nextbool(&line, &(p_call->isMpty));
281 if (err < 0) goto error;
283 if (at_tok_hasmore(&line)) {
284 err = at_tok_nextstr(&line, &(p_call->number));
286 /* tolerate null here */
287 if (err < 0) return 0;
289 // Some lame implementations return strings
290 // like "NOT AVAILABLE" in the CLCC line
291 if (p_call->number != NULL
292 && 0 == strspn(p_call->number, "+0123456789")
294 p_call->number = NULL;
297 err = at_tok_nextint(&line, &p_call->toa);
298 if (err < 0) goto error;
301 p_call->uusInfo = NULL;
306 RLOGE("invalid CLCC line\n");
311 /** do post-AT+CFUN=1 initialization */
312 static void onRadioPowerOn()
314 #ifdef USE_TI_COMMANDS
315 /* Must be after CFUN=1 */
316 /* TI specific -- notifications for CPHS things such */
317 /* as CPHS message waiting indicator */
319 at_send_command("AT%CPHS=1", NULL);
321 /* TI specific -- enable NITZ unsol notifs */
322 at_send_command("AT%CTZV=1", NULL);
328 /** do post- SIM ready initialization */
329 static void onSIMReady()
331 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
333 * Always send SMS messages directly to the TE
335 * mode = 1 // discard when link is reserved (link should never be
337 * mt = 2 // most messages routed to TE
338 * bm = 2 // new cell BM's routed to TE
339 * ds = 1 // Status reports routed to TE
340 * bfr = 1 // flush buffer
342 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
345 static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
350 ATResponse *p_response = NULL;
352 assert (datalen >= sizeof(int *));
353 onOff = ((int *)data)[0];
355 if (onOff == 0 && sState != RADIO_STATE_OFF) {
356 err = at_send_command("AT+CFUN=0", &p_response);
357 if (err < 0 || p_response->success == 0) goto error;
358 setRadioState(RADIO_STATE_OFF);
359 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
360 err = at_send_command("AT+CFUN=1", &p_response);
361 if (err < 0|| p_response->success == 0) {
362 // Some stacks return an error when there is no SIM,
363 // but they really turn the RF portion on
364 // So, if we get an error, let's check to see if it
367 if (isRadioOn() != 1) {
371 setRadioState(RADIO_STATE_ON);
374 at_response_free(p_response);
375 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
378 at_response_free(p_response);
379 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
382 static void requestOrSendDataCallList(RIL_Token *t);
384 static void onDataCallListChanged(void *param)
386 requestOrSendDataCallList(NULL);
389 static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
391 requestOrSendDataCallList(&t);
394 static void requestOrSendDataCallList(RIL_Token *t)
396 ATResponse *p_response;
402 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
403 if (err != 0 || p_response->success == 0) {
405 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
407 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
412 for (p_cur = p_response->p_intermediates; p_cur != NULL;
413 p_cur = p_cur->p_next)
416 RIL_Data_Call_Response_v6 *responses =
417 alloca(n * sizeof(RIL_Data_Call_Response_v6));
420 for (i = 0; i < n; i++) {
421 responses[i].status = -1;
422 responses[i].suggestedRetryTime = -1;
423 responses[i].cid = -1;
424 responses[i].active = -1;
425 responses[i].type = "";
426 responses[i].ifname = "";
427 responses[i].addresses = "";
428 responses[i].dnses = "";
429 responses[i].gateways = "";
432 RIL_Data_Call_Response_v6 *response = responses;
433 for (p_cur = p_response->p_intermediates; p_cur != NULL;
434 p_cur = p_cur->p_next) {
435 char *line = p_cur->line;
437 err = at_tok_start(&line);
441 err = at_tok_nextint(&line, &response->cid);
445 err = at_tok_nextint(&line, &response->active);
452 at_response_free(p_response);
454 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
455 if (err != 0 || p_response->success == 0) {
457 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
459 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
464 for (p_cur = p_response->p_intermediates; p_cur != NULL;
465 p_cur = p_cur->p_next) {
466 char *line = p_cur->line;
469 err = at_tok_start(&line);
473 err = at_tok_nextint(&line, &cid);
477 for (i = 0; i < n; i++) {
478 if (responses[i].cid == cid)
483 /* details for a context we didn't hear about in the last request */
488 responses[i].status = 0;
491 err = at_tok_nextstr(&line, &out);
494 responses[i].type = alloca(strlen(out) + 1);
495 strcpy(responses[i].type, out);
497 // APN ignored for v5
498 err = at_tok_nextstr(&line, &out);
502 responses[i].ifname = alloca(strlen(PPP_TTY_PATH) + 1);
503 strcpy(responses[i].ifname, PPP_TTY_PATH);
505 err = at_tok_nextstr(&line, &out);
509 responses[i].addresses = alloca(strlen(out) + 1);
510 strcpy(responses[i].addresses, out);
513 char propValue[PROP_VALUE_MAX];
515 if (__system_property_get("ro.kernel.qemu", propValue) != 0) {
516 /* We are in the emulator - the dns servers are listed
517 * by the following system properties, setup in
518 * /system/etc/init.goldfish.sh:
524 const int dnslist_sz = 128;
525 char* dnslist = alloca(dnslist_sz);
526 const char* separator = "";
530 for (nn = 1; nn <= 4; nn++) {
531 /* Probe net.eth0.dns<n> */
532 char propName[PROP_NAME_MAX];
533 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
535 /* Ignore if undefined */
536 if (__system_property_get(propName, propValue) == 0) {
540 /* Append the DNS IP address */
541 strlcat(dnslist, separator, dnslist_sz);
542 strlcat(dnslist, propValue, dnslist_sz);
545 responses[i].dnses = dnslist;
547 /* There is only on gateway in the emulator */
548 responses[i].gateways = "10.0.2.2";
551 /* I don't know where we are, so use the public Google DNS
552 * servers by default and no gateway.
554 responses[i].dnses = "8.8.8.8 8.8.4.4";
555 responses[i].gateways = "";
560 at_response_free(p_response);
563 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
564 n * sizeof(RIL_Data_Call_Response_v6));
566 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
568 n * sizeof(RIL_Data_Call_Response_v6));
574 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
576 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
579 at_response_free(p_response);
582 static void requestQueryNetworkSelectionMode(
583 void *data, size_t datalen, RIL_Token t)
586 ATResponse *p_response = NULL;
590 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
592 if (err < 0 || p_response->success == 0) {
596 line = p_response->p_intermediates->line;
598 err = at_tok_start(&line);
604 err = at_tok_nextint(&line, &response);
610 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
611 at_response_free(p_response);
614 at_response_free(p_response);
615 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
616 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
619 static void sendCallStateChanged(void *param)
621 RIL_onUnsolicitedResponse (
622 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
626 static void requestGetCurrentCalls(void *data, size_t datalen, RIL_Token t)
629 ATResponse *p_response;
638 #ifdef WORKAROUND_ERRONEOUS_ANSWER
639 int prevIncomingOrWaitingLine;
641 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
642 s_incomingOrWaitingLine = -1;
643 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
645 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
647 if (err != 0 || p_response->success == 0) {
648 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
652 /* count the calls */
653 for (countCalls = 0, p_cur = p_response->p_intermediates
655 ; p_cur = p_cur->p_next
660 /* yes, there's an array of pointers and then an array of structures */
662 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
663 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
664 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
666 /* init the pointer array */
667 for(i = 0; i < countCalls ; i++) {
668 pp_calls[i] = &(p_calls[i]);
671 for (countValidCalls = 0, p_cur = p_response->p_intermediates
673 ; p_cur = p_cur->p_next
675 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
681 #ifdef WORKAROUND_ERRONEOUS_ANSWER
682 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
683 || p_calls[countValidCalls].state == RIL_CALL_WAITING
685 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
687 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
689 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
690 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
698 #ifdef WORKAROUND_ERRONEOUS_ANSWER
700 // A call was incoming or waiting
701 // Now it's marked as active
702 // But we never answered it
704 // This is probably a bug, and the call will probably
705 // disappear from the call list in the next poll
706 if (prevIncomingOrWaitingLine >= 0
707 && s_incomingOrWaitingLine < 0
708 && s_expectAnswer == 0
710 for (i = 0; i < countValidCalls ; i++) {
712 if (p_calls[i].index == prevIncomingOrWaitingLine
713 && p_calls[i].state == RIL_CALL_ACTIVE
714 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
717 "Hit WORKAROUND_ERRONOUS_ANSWER case."
718 " Repoll count: %d\n", s_repollCallsCount);
719 s_repollCallsCount++;
726 s_repollCallsCount = 0;
727 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
729 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
730 countValidCalls * sizeof (RIL_Call *));
732 at_response_free(p_response);
734 #ifdef POLL_CALL_STATE
735 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
736 // smd, so we're forced to poll until the call ends.
740 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
745 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
746 at_response_free(p_response);
749 static void requestDial(void *data, size_t datalen, RIL_Token t)
756 p_dial = (RIL_Dial *)data;
758 switch (p_dial->clir) {
759 case 1: clir = "I"; break; /*invocation*/
760 case 2: clir = "i"; break; /*suppression*/
762 case 0: clir = ""; break; /*subscription default*/
765 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
767 ret = at_send_command(cmd, NULL);
771 /* success or failure is ignored by the upper layer here.
772 it will call GET_CURRENT_CALLS and determine success that way */
773 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
776 static void requestWriteSmsToSim(void *data, size_t datalen, RIL_Token t)
778 RIL_SMS_WriteArgs *p_args;
782 ATResponse *p_response = NULL;
784 p_args = (RIL_SMS_WriteArgs *)data;
786 length = strlen(p_args->pdu)/2;
787 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
789 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
791 if (err != 0 || p_response->success == 0) goto error;
793 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
794 at_response_free(p_response);
798 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
799 at_response_free(p_response);
802 static void requestHangup(void *data, size_t datalen, RIL_Token t)
809 p_line = (int *)data;
812 // "Releases a specific active call X"
813 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
815 ret = at_send_command(cmd, NULL);
819 /* success or failure is ignored by the upper layer here.
820 it will call GET_CURRENT_CALLS and determine success that way */
821 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
824 static void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
826 ATResponse *p_response = NULL;
830 int numofElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
831 int response[numofElements];
833 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
835 if (err < 0 || p_response->success == 0) {
836 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
840 line = p_response->p_intermediates->line;
842 err = at_tok_start(&line);
843 if (err < 0) goto error;
845 for (count =0; count < numofElements; count ++) {
846 err = at_tok_nextint(&line, &(response[count]));
847 if (err < 0) goto error;
850 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
852 at_response_free(p_response);
856 RLOGE("requestSignalStrength must never return an error when radio is on");
857 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
858 at_response_free(p_response);
862 * networkModePossible. Decides whether the network mode is appropriate for the
865 static int networkModePossible(ModemInfo *mdm, int nm)
867 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
872 static void requestSetPreferredNetworkType( int request, void *data,
873 size_t datalen, RIL_Token t )
875 ATResponse *p_response = NULL;
877 int value = *(int *)data;
880 int32_t preferred = net2pmask[value];
882 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
883 if (!networkModePossible(sMdmInfo, value)) {
884 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
887 if (query_ctec(sMdmInfo, ¤t, NULL) < 0) {
888 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
891 old = PREFERRED_NETWORK(sMdmInfo);
892 RLOGD("old != preferred: %d", old != preferred);
893 if (old != preferred) {
894 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
895 RLOGD("Sending command: <%s>", cmd);
896 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
898 if (err || !p_response->success) {
899 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
902 PREFERRED_NETWORK(sMdmInfo) = value;
903 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
905 int res = parse_technology_response(p_response->p_intermediates->line, ¤t, NULL);
907 case -1: // Error or unable to parse
909 case 1: // Only able to parse current
910 case 0: // Both current and preferred were parsed
911 setRadioTechnology(sMdmInfo, current);
916 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
919 static void requestGetPreferredNetworkType(int request, void *data,
920 size_t datalen, RIL_Token t)
925 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
926 case -1: // Error or unable to parse
927 case 1: // Only able to parse current
928 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
930 case 0: // Both current and preferred were parsed
931 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
932 if (preferred == net2pmask[i]) {
933 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
937 RLOGE("Unknown preferred mode received from modem: %d", preferred);
938 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
944 static void requestCdmaPrlVersion(int request, void *data,
945 size_t datalen, RIL_Token t)
949 ATResponse *p_response = NULL;
953 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
954 if (err < 0 || !p_response->success) goto error;
955 line = p_response->p_intermediates->line;
956 err = at_tok_start(&line);
957 if (err < 0) goto error;
958 err = at_tok_nextstr(&line, &responseStr);
959 if (err < 0 || !responseStr) goto error;
960 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
961 at_response_free(p_response);
964 at_response_free(p_response);
965 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
968 static void requestCdmaBaseBandVersion(int request, void *data,
969 size_t datalen, RIL_Token t)
973 ATResponse *p_response = NULL;
981 // Fixed values. TODO: query modem
982 responseStr = strdup("1.0.0.0");
983 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
987 static void requestCdmaDeviceIdentity(int request, void *data,
988 size_t datalen, RIL_Token t)
992 char * responseStr[4];
993 ATResponse *p_response = NULL;
1001 // Fixed values. TODO: Query modem
1002 responseStr[0] = "----";
1003 responseStr[1] = "----";
1004 responseStr[2] = "77777777";
1006 err = at_send_command_numeric("AT+CGSN", &p_response);
1007 if (err < 0 || p_response->success == 0) {
1008 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1011 responseStr[3] = p_response->p_intermediates->line;
1014 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1015 at_response_free(p_response);
1019 RLOGE("requestCdmaDeviceIdentity must never return an error when radio is on");
1020 at_response_free(p_response);
1021 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1024 static void requestCdmaGetSubscriptionSource(int request, void *data,
1025 size_t datalen, RIL_Token t)
1028 int *ss = (int *)data;
1029 ATResponse *p_response = NULL;
1034 asprintf(&cmd, "AT+CCSS?");
1035 if (!cmd) goto error;
1037 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1038 if (err < 0 || !p_response->success)
1041 line = p_response->p_intermediates->line;
1042 err = at_tok_start(&line);
1043 if (err < 0) goto error;
1045 err = at_tok_nextint(&line, &response);
1049 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1054 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1057 static void requestCdmaSetSubscriptionSource(int request, void *data,
1058 size_t datalen, RIL_Token t)
1061 int *ss = (int *)data;
1062 ATResponse *p_response = NULL;
1065 if (!ss || !datalen) {
1066 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
1067 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1070 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1071 if (!cmd) goto error;
1073 err = at_send_command(cmd, &p_response);
1074 if (err < 0 || !p_response->success)
1079 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1081 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1086 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1089 static void requestCdmaSubscription(int request, void *data,
1090 size_t datalen, RIL_Token t)
1094 char * responseStr[5];
1095 ATResponse *p_response = NULL;
1103 // Fixed values. TODO: Query modem
1104 responseStr[0] = "8587777777"; // MDN
1105 responseStr[1] = "1"; // SID
1106 responseStr[2] = "1"; // NID
1107 responseStr[3] = "8587777777"; // MIN
1108 responseStr[4] = "1"; // PRL Version
1109 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1113 RLOGE("requestRegistrationState must never return an error when radio is on");
1114 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1117 static void requestCdmaGetRoamingPreference(int request, void *data,
1118 size_t datalen, RIL_Token t)
1120 int roaming_pref = -1;
1121 ATResponse *p_response = NULL;
1125 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1126 if (res < 0 || !p_response->success) {
1129 line = p_response->p_intermediates->line;
1131 res = at_tok_start(&line);
1132 if (res < 0) goto error;
1134 res = at_tok_nextint(&line, &roaming_pref);
1135 if (res < 0) goto error;
1137 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1140 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1143 static void requestCdmaSetRoamingPreference(int request, void *data,
1144 size_t datalen, RIL_Token t)
1146 int *pref = (int *)data;
1147 ATResponse *p_response = NULL;
1152 asprintf(&cmd, "AT+WRMP=%d", *pref);
1153 if (cmd == NULL) goto error;
1155 res = at_send_command(cmd, &p_response);
1156 if (res < 0 || !p_response->success)
1159 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1164 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1167 static int parseRegistrationState(char *str, int *type, int *items, int **response)
1170 char *line = str, *p;
1176 RLOGD("parseRegistrationState. Parsing: %s",str);
1177 err = at_tok_start(&line);
1178 if (err < 0) goto error;
1180 /* Ok you have to be careful here
1181 * The solicited version of the CREG response is
1182 * +CREG: n, stat, [lac, cid]
1183 * and the unsolicited version is
1184 * +CREG: stat, [lac, cid]
1185 * The <n> parameter is basically "is unsolicited creg on?"
1186 * which it should always be
1188 * Now we should normally get the solicited version here,
1189 * but the unsolicited version could have snuck in
1190 * so we have to handle both
1192 * Also since the LAC and CID are only reported when registered,
1193 * we can have 1, 2, 3, or 4 arguments here
1195 * finally, a +CGREG: answer may have a fifth value that corresponds
1196 * to the network type, as in;
1198 * +CGREG: n, stat [,lac, cid [,networkType]]
1201 /* count number of commas */
1203 for (p = line ; *p != '\0' ;p++) {
1204 if (*p == ',') commas++;
1207 resp = (int *)calloc(commas + 1, sizeof(int));
1208 if (!resp) goto error;
1210 case 0: /* +CREG: <stat> */
1211 err = at_tok_nextint(&line, &resp[0]);
1212 if (err < 0) goto error;
1217 case 1: /* +CREG: <n>, <stat> */
1218 err = at_tok_nextint(&line, &skip);
1219 if (err < 0) goto error;
1220 err = at_tok_nextint(&line, &resp[0]);
1221 if (err < 0) goto error;
1224 if (err < 0) goto error;
1227 case 2: /* +CREG: <stat>, <lac>, <cid> */
1228 err = at_tok_nextint(&line, &resp[0]);
1229 if (err < 0) goto error;
1230 err = at_tok_nexthexint(&line, &resp[1]);
1231 if (err < 0) goto error;
1232 err = at_tok_nexthexint(&line, &resp[2]);
1233 if (err < 0) goto error;
1235 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1236 err = at_tok_nextint(&line, &skip);
1237 if (err < 0) goto error;
1238 err = at_tok_nextint(&line, &resp[0]);
1239 if (err < 0) goto error;
1240 err = at_tok_nexthexint(&line, &resp[1]);
1241 if (err < 0) goto error;
1242 err = at_tok_nexthexint(&line, &resp[2]);
1243 if (err < 0) goto error;
1245 /* special case for CGREG, there is a fourth parameter
1246 * that is the network type (unknown/gprs/edge/umts)
1248 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1249 err = at_tok_nextint(&line, &skip);
1250 if (err < 0) goto error;
1251 err = at_tok_nextint(&line, &resp[0]);
1252 if (err < 0) goto error;
1253 err = at_tok_nexthexint(&line, &resp[1]);
1254 if (err < 0) goto error;
1255 err = at_tok_nexthexint(&line, &resp[2]);
1256 if (err < 0) goto error;
1257 err = at_tok_nexthexint(&line, &resp[3]);
1258 if (err < 0) goto error;
1267 *items = commas + 1;
1269 *type = techFromModemType(TECH(sMdmInfo));
1276 #define REG_STATE_LEN 15
1277 #define REG_DATA_STATE_LEN 6
1278 static void requestRegistrationState(int request, void *data,
1279 size_t datalen, RIL_Token t)
1283 char **responseStr = NULL;
1284 ATResponse *p_response = NULL;
1288 int i = 0, j, numElements = 0;
1290 int type, startfrom;
1292 RLOGD("requestRegistrationState");
1293 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1296 numElements = REG_STATE_LEN;
1297 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1300 numElements = REG_DATA_STATE_LEN;
1306 err = at_send_command_singleline(cmd, prefix, &p_response);
1308 if (err != 0) goto error;
1310 line = p_response->p_intermediates->line;
1312 if (parseRegistrationState(line, &type, &count, ®istration)) goto error;
1314 responseStr = malloc(numElements * sizeof(char *));
1315 if (!responseStr) goto error;
1316 memset(responseStr, 0, numElements * sizeof(char *));
1318 * The first '4' bytes for both registration states remain the same.
1319 * But if the request is 'DATA_REGISTRATION_STATE',
1320 * the 5th and 6th byte(s) are optional.
1322 if (is3gpp2(type) == 1) {
1323 RLOGD("registration state type: 3GPP2");
1324 // TODO: Query modem
1326 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1327 asprintf(&responseStr[3], "8"); // EvDo revA
1328 asprintf(&responseStr[4], "1"); // BSID
1329 asprintf(&responseStr[5], "123"); // Latitude
1330 asprintf(&responseStr[6], "222"); // Longitude
1331 asprintf(&responseStr[7], "0"); // CSS Indicator
1332 asprintf(&responseStr[8], "4"); // SID
1333 asprintf(&responseStr[9], "65535"); // NID
1334 asprintf(&responseStr[10], "0"); // Roaming indicator
1335 asprintf(&responseStr[11], "1"); // System is in PRL
1336 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1337 asprintf(&responseStr[13], "0"); // Reason for denial
1338 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1339 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1340 asprintf(&responseStr[3], "8"); // Available data radio technology
1342 } else { // type == RADIO_TECH_3GPP
1343 RLOGD("registration state type: 3GPP");
1345 asprintf(&responseStr[1], "%x", registration[1]);
1346 asprintf(&responseStr[2], "%x", registration[2]);
1348 asprintf(&responseStr[3], "%d", registration[3]);
1350 asprintf(&responseStr[0], "%d", registration[0]);
1353 * Optional bytes for DATA_REGISTRATION_STATE request
1354 * 4th byte : Registration denial code
1355 * 5th byte : The max. number of simultaneous Data Calls
1357 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1358 // asprintf(&responseStr[4], "3");
1359 // asprintf(&responseStr[5], "1");
1362 for (j = startfrom; j < numElements; j++) {
1363 if (!responseStr[i]) goto error;
1366 registration = NULL;
1368 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1369 for (j = 0; j < numElements; j++ ) {
1370 free(responseStr[j]);
1371 responseStr[j] = NULL;
1375 at_response_free(p_response);
1380 for (j = 0; j < numElements; j++) {
1381 free(responseStr[j]);
1382 responseStr[j] = NULL;
1387 RLOGE("requestRegistrationState must never return an error when radio is on");
1388 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1389 at_response_free(p_response);
1392 static void requestOperator(void *data, size_t datalen, RIL_Token t)
1400 memset(response, 0, sizeof(response));
1402 ATResponse *p_response = NULL;
1404 err = at_send_command_multiline(
1405 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1406 "+COPS:", &p_response);
1408 /* we expect 3 lines here:
1409 * +COPS: 0,0,"T - Mobile"
1411 * +COPS: 0,2,"310170"
1414 if (err != 0) goto error;
1416 for (i = 0, p_cur = p_response->p_intermediates
1418 ; p_cur = p_cur->p_next, i++
1420 char *line = p_cur->line;
1422 err = at_tok_start(&line);
1423 if (err < 0) goto error;
1425 err = at_tok_nextint(&line, &skip);
1426 if (err < 0) goto error;
1428 // If we're unregistered, we may just get
1429 // a "+COPS: 0" response
1430 if (!at_tok_hasmore(&line)) {
1435 err = at_tok_nextint(&line, &skip);
1436 if (err < 0) goto error;
1438 // a "+COPS: 0, n" response is also possible
1439 if (!at_tok_hasmore(&line)) {
1444 err = at_tok_nextstr(&line, &(response[i]));
1445 if (err < 0) goto error;
1449 /* expect 3 lines exactly */
1453 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1454 at_response_free(p_response);
1458 RLOGE("requestOperator must not return error when radio is on");
1459 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1460 at_response_free(p_response);
1463 static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1465 int err = 1; // Set to go to error:
1466 RIL_SMS_Response response;
1467 RIL_CDMA_SMS_Message* rcsm;
1469 RLOGD("requestCdmaSendSMS datalen=%d, sizeof(RIL_CDMA_SMS_Message)=%d",
1470 datalen, sizeof(RIL_CDMA_SMS_Message));
1472 // verify data content to test marshalling/unmarshalling:
1473 rcsm = (RIL_CDMA_SMS_Message*)data;
1474 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
1475 uServicecategory=%d, sAddress.digit_mode=%d, \
1476 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1477 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1478 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1479 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1481 if (err != 0) goto error;
1483 // Cdma Send SMS implementation will go here:
1484 // But it is not implemented yet.
1486 memset(&response, 0, sizeof(response));
1487 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1491 // Cdma Send SMS will always cause send retry error.
1492 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, NULL, 0);
1495 static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1502 RIL_SMS_Response response;
1503 ATResponse *p_response = NULL;
1505 smsc = ((const char **)data)[0];
1506 pdu = ((const char **)data)[1];
1508 tpLayerLength = strlen(pdu)/2;
1510 // "NULL for default SMSC"
1515 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1516 asprintf(&cmd2, "%s%s", smsc, pdu);
1518 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1520 if (err != 0 || p_response->success == 0) goto error;
1522 memset(&response, 0, sizeof(response));
1524 /* FIXME fill in messageRef and ackPDU */
1526 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1527 at_response_free(p_response);
1531 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1532 at_response_free(p_response);
1535 static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
1540 ATResponse *p_response = NULL;
1542 apn = ((const char **)data)[2];
1544 #ifdef USE_TI_COMMANDS
1545 // Config for multislot class 10 (probably default anyway eh?)
1546 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1549 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1550 #endif /* USE_TI_COMMANDS */
1555 ssize_t written, rlen;
1556 char status[32] = {0};
1558 const char *pdp_type;
1560 RLOGD("requesting data connection to APN '%s'", apn);
1562 fd = open ("/dev/qmi", O_RDWR);
1563 if (fd >= 0) { /* the device doesn't exist on the emulator */
1565 RLOGD("opened the qmi device\n");
1566 asprintf(&cmd, "up:%s", apn);
1571 written = write (fd, cmd + cur, len - cur);
1572 } while (written < 0 && errno == EINTR);
1575 RLOGE("### ERROR writing to /dev/qmi");
1583 // wait for interface to come online
1588 rlen = read(fd, status, 31);
1589 } while (rlen < 0 && errno == EINTR);
1592 RLOGE("### ERROR reading from /dev/qmi");
1596 status[rlen] = '\0';
1597 RLOGD("### status: %s", status);
1599 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1604 RLOGE("### Failed to get data connection up\n");
1608 qmistatus = system("netcfg rmnet0 dhcp");
1610 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1612 if (qmistatus < 0) goto error;
1616 if (datalen > 6 * sizeof(char *)) {
1617 pdp_type = ((const char **)data)[6];
1622 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
1623 //FIXME check for error here
1624 err = at_send_command(cmd, NULL);
1627 // Set required QoS params to default
1628 err = at_send_command("AT+CGQREQ=1", NULL);
1630 // Set minimum QoS params to default
1631 err = at_send_command("AT+CGQMIN=1", NULL);
1633 // packet-domain event reporting
1634 err = at_send_command("AT+CGEREP=1,0", NULL);
1636 // Hangup anything that's happening there now
1637 err = at_send_command("AT+CGACT=1,0", NULL);
1639 // Start data on PDP context 1
1640 err = at_send_command("ATD*99***1#", &p_response);
1642 if (err < 0 || p_response->success == 0) {
1647 requestOrSendDataCallList(&t);
1649 at_response_free(p_response);
1653 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1654 at_response_free(p_response);
1658 static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1663 ackSuccess = ((int *)data)[0];
1665 if (ackSuccess == 1) {
1666 err = at_send_command("AT+CNMA=1", NULL);
1667 } else if (ackSuccess == 0) {
1668 err = at_send_command("AT+CNMA=2", NULL);
1670 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1674 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1676 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1680 static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1682 ATResponse *p_response = NULL;
1683 RIL_SIM_IO_Response sr;
1686 RIL_SIM_IO_v6 *p_args;
1689 memset(&sr, 0, sizeof(sr));
1691 p_args = (RIL_SIM_IO_v6 *)data;
1693 /* FIXME handle pin2 */
1695 if (p_args->data == NULL) {
1696 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1697 p_args->command, p_args->fileid,
1698 p_args->p1, p_args->p2, p_args->p3);
1700 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1701 p_args->command, p_args->fileid,
1702 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1705 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1707 if (err < 0 || p_response->success == 0) {
1711 line = p_response->p_intermediates->line;
1713 err = at_tok_start(&line);
1714 if (err < 0) goto error;
1716 err = at_tok_nextint(&line, &(sr.sw1));
1717 if (err < 0) goto error;
1719 err = at_tok_nextint(&line, &(sr.sw2));
1720 if (err < 0) goto error;
1722 if (at_tok_hasmore(&line)) {
1723 err = at_tok_nextstr(&line, &(sr.simResponse));
1724 if (err < 0) goto error;
1727 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1728 at_response_free(p_response);
1733 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1734 at_response_free(p_response);
1739 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1741 ATResponse *p_response = NULL;
1744 const char** strings = (const char**)data;;
1746 if ( datalen == sizeof(char*) ) {
1747 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1748 } else if ( datalen == 2*sizeof(char*) ) {
1749 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1753 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1756 if (err < 0 || p_response->success == 0) {
1758 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1760 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1762 at_response_free(p_response);
1766 static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1768 const char *ussdRequest;
1770 ussdRequest = (char *)(data);
1773 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1779 static void requestExitEmergencyMode(void *data, size_t datalen, RIL_Token t)
1782 ATResponse *p_response = NULL;
1784 err = at_send_command("AT+WSOS=0", &p_response);
1786 if (err < 0 || p_response->success == 0) {
1787 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1791 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1794 // TODO: Use all radio types
1795 static int techFromModemType(int mdmtype)
1798 switch (1 << mdmtype) {
1800 ret = RADIO_TECH_1xRTT;
1803 ret = RADIO_TECH_EVDO_A;
1806 ret = RADIO_TECH_GPRS;
1809 ret = RADIO_TECH_HSPA;
1812 ret = RADIO_TECH_LTE;
1818 /*** Callback methods from the RIL library to us ***/
1821 * Call from RIL to us to make a RIL_REQUEST
1823 * Must be completed with a call to RIL_onRequestComplete()
1825 * RIL_onRequestComplete() may be called from any thread, before or after
1826 * this function returns.
1828 * Will always be called from the same thread, so returning here implies
1829 * that the radio is ready to process another command (whether or not
1830 * the previous command has completed).
1833 onRequest (int request, void *data, size_t datalen, RIL_Token t)
1835 ATResponse *p_response;
1838 RLOGD("onRequest: %s", requestToString(request));
1840 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1841 * when RADIO_STATE_UNAVAILABLE.
1843 if (sState == RADIO_STATE_UNAVAILABLE
1844 && request != RIL_REQUEST_GET_SIM_STATUS
1846 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1850 /* Ignore all non-power requests when RADIO_STATE_OFF
1851 * (except RIL_REQUEST_GET_SIM_STATUS)
1853 if (sState == RADIO_STATE_OFF
1854 && !(request == RIL_REQUEST_RADIO_POWER
1855 || request == RIL_REQUEST_GET_SIM_STATUS)
1857 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1862 case RIL_REQUEST_GET_SIM_STATUS: {
1863 RIL_CardStatus_v6 *p_card_status;
1867 int result = getCardStatus(&p_card_status);
1868 if (result == RIL_E_SUCCESS) {
1869 p_buffer = (char *)p_card_status;
1870 buffer_size = sizeof(*p_card_status);
1875 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
1876 freeCardStatus(p_card_status);
1879 case RIL_REQUEST_GET_CURRENT_CALLS:
1880 requestGetCurrentCalls(data, datalen, t);
1882 case RIL_REQUEST_DIAL:
1883 requestDial(data, datalen, t);
1885 case RIL_REQUEST_HANGUP:
1886 requestHangup(data, datalen, t);
1888 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1889 // 3GPP 22.030 6.5.5
1890 // "Releases all held calls or sets User Determined User Busy
1891 // (UDUB) for a waiting call."
1892 at_send_command("AT+CHLD=0", NULL);
1894 /* success or failure is ignored by the upper layer here.
1895 it will call GET_CURRENT_CALLS and determine success that way */
1896 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1898 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1899 // 3GPP 22.030 6.5.5
1900 // "Releases all active calls (if any exist) and accepts
1901 // the other (held or waiting) call."
1902 at_send_command("AT+CHLD=1", NULL);
1904 /* success or failure is ignored by the upper layer here.
1905 it will call GET_CURRENT_CALLS and determine success that way */
1906 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1908 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1909 // 3GPP 22.030 6.5.5
1910 // "Places all active calls (if any exist) on hold and accepts
1911 // the other (held or waiting) call."
1912 at_send_command("AT+CHLD=2", NULL);
1914 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1916 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1918 /* success or failure is ignored by the upper layer here.
1919 it will call GET_CURRENT_CALLS and determine success that way */
1920 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1922 case RIL_REQUEST_ANSWER:
1923 at_send_command("ATA", NULL);
1925 #ifdef WORKAROUND_ERRONEOUS_ANSWER
1927 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
1929 /* success or failure is ignored by the upper layer here.
1930 it will call GET_CURRENT_CALLS and determine success that way */
1931 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1933 case RIL_REQUEST_CONFERENCE:
1934 // 3GPP 22.030 6.5.5
1935 // "Adds a held call to the conversation"
1936 at_send_command("AT+CHLD=3", NULL);
1938 /* success or failure is ignored by the upper layer here.
1939 it will call GET_CURRENT_CALLS and determine success that way */
1940 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1942 case RIL_REQUEST_UDUB:
1943 /* user determined user busy */
1944 /* sometimes used: ATH */
1945 at_send_command("ATH", NULL);
1947 /* success or failure is ignored by the upper layer here.
1948 it will call GET_CURRENT_CALLS and determine success that way */
1949 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1952 case RIL_REQUEST_SEPARATE_CONNECTION:
1955 int party = ((int*)data)[0];
1957 // Make sure that party is in a valid range.
1958 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1959 // It's sufficient for us to just make sure it's single digit.)
1960 if (party > 0 && party < 10) {
1961 sprintf(cmd, "AT+CHLD=2%d", party);
1962 at_send_command(cmd, NULL);
1963 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1965 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1970 case RIL_REQUEST_SIGNAL_STRENGTH:
1971 requestSignalStrength(data, datalen, t);
1973 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
1974 case RIL_REQUEST_DATA_REGISTRATION_STATE:
1975 requestRegistrationState(request, data, datalen, t);
1977 case RIL_REQUEST_OPERATOR:
1978 requestOperator(data, datalen, t);
1980 case RIL_REQUEST_RADIO_POWER:
1981 requestRadioPower(data, datalen, t);
1983 case RIL_REQUEST_DTMF: {
1984 char c = ((char *)data)[0];
1986 asprintf(&cmd, "AT+VTS=%c", (int)c);
1987 at_send_command(cmd, NULL);
1989 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1992 case RIL_REQUEST_SEND_SMS:
1993 requestSendSMS(data, datalen, t);
1995 case RIL_REQUEST_CDMA_SEND_SMS:
1996 requestCdmaSendSMS(data, datalen, t);
1998 case RIL_REQUEST_SETUP_DATA_CALL:
1999 requestSetupDataCall(data, datalen, t);
2001 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2002 requestSMSAcknowledge(data, datalen, t);
2005 case RIL_REQUEST_GET_IMSI:
2007 err = at_send_command_numeric("AT+CIMI", &p_response);
2009 if (err < 0 || p_response->success == 0) {
2010 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2012 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2013 p_response->p_intermediates->line, sizeof(char *));
2015 at_response_free(p_response);
2018 case RIL_REQUEST_GET_IMEI:
2020 err = at_send_command_numeric("AT+CGSN", &p_response);
2022 if (err < 0 || p_response->success == 0) {
2023 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2025 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2026 p_response->p_intermediates->line, sizeof(char *));
2028 at_response_free(p_response);
2031 case RIL_REQUEST_SIM_IO:
2032 requestSIM_IO(data,datalen,t);
2035 case RIL_REQUEST_SEND_USSD:
2036 requestSendUSSD(data, datalen, t);
2039 case RIL_REQUEST_CANCEL_USSD:
2041 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2043 if (err < 0 || p_response->success == 0) {
2044 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2046 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2047 p_response->p_intermediates->line, sizeof(char *));
2049 at_response_free(p_response);
2052 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2053 at_send_command("AT+COPS=0", NULL);
2056 case RIL_REQUEST_DATA_CALL_LIST:
2057 requestDataCallList(data, datalen, t);
2060 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2061 requestQueryNetworkSelectionMode(data, datalen, t);
2064 case RIL_REQUEST_OEM_HOOK_RAW:
2066 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2070 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2074 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
2077 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2078 i > 0 ; cur++, i --) {
2079 RLOGD("> '%s'", *cur);
2082 // echo back strings
2083 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2087 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2088 requestWriteSmsToSim(data, datalen, t);
2091 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2094 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2095 err = at_send_command(cmd, &p_response);
2097 if (err < 0 || p_response->success == 0) {
2098 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2100 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2102 at_response_free(p_response);
2106 case RIL_REQUEST_ENTER_SIM_PIN:
2107 case RIL_REQUEST_ENTER_SIM_PUK:
2108 case RIL_REQUEST_ENTER_SIM_PIN2:
2109 case RIL_REQUEST_ENTER_SIM_PUK2:
2110 case RIL_REQUEST_CHANGE_SIM_PIN:
2111 case RIL_REQUEST_CHANGE_SIM_PIN2:
2112 requestEnterSimPin(data, datalen, t);
2115 case RIL_REQUEST_VOICE_RADIO_TECH:
2117 int tech = techFromModemType(TECH(sMdmInfo));
2119 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2121 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2124 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2125 requestSetPreferredNetworkType(request, data, datalen, t);
2128 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2129 requestGetPreferredNetworkType(request, data, datalen, t);
2132 /* CDMA Specific Requests */
2133 case RIL_REQUEST_BASEBAND_VERSION:
2134 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2135 requestCdmaBaseBandVersion(request, data, datalen, t);
2137 } // Fall-through if tech is not cdma
2139 case RIL_REQUEST_DEVICE_IDENTITY:
2140 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2141 requestCdmaDeviceIdentity(request, data, datalen, t);
2143 } // Fall-through if tech is not cdma
2145 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2146 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2147 requestCdmaSubscription(request, data, datalen, t);
2149 } // Fall-through if tech is not cdma
2151 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2152 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2153 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2155 } // Fall-through if tech is not cdma
2157 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2158 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2159 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2161 } // Fall-through if tech is not cdma
2163 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2164 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2165 requestCdmaGetRoamingPreference(request, data, datalen, t);
2167 } // Fall-through if tech is not cdma
2169 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2170 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2171 requestCdmaSetRoamingPreference(request, data, datalen, t);
2173 } // Fall-through if tech is not cdma
2175 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2176 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2177 requestExitEmergencyMode(data, datalen, t);
2179 } // Fall-through if tech is not cdma
2182 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
2183 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2189 * Synchronous call from the RIL to us to return current radio state.
2190 * RADIO_STATE_UNAVAILABLE should be the initial state.
2192 static RIL_RadioState
2198 * Call from RIL to us to find out whether a specific request code
2199 * is supported by this implementation.
2201 * Return 1 for "supported" and 0 for "unsupported"
2205 onSupports (int requestCode)
2212 static void onCancel (RIL_Token t)
2218 static const char * getVersion(void)
2220 return "android reference-ril 1.0";
2224 setRadioTechnology(ModemInfo *mdm, int newtech)
2226 RLOGD("setRadioTechnology(%d)", newtech);
2228 int oldtech = TECH(mdm);
2230 if (newtech != oldtech) {
2231 RLOGD("Tech change (%d => %d)", oldtech, newtech);
2232 TECH(mdm) = newtech;
2233 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2234 int tech = techFromModemType(TECH(sMdmInfo));
2236 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2237 &tech, sizeof(tech));
2244 setRadioState(RIL_RadioState newState)
2246 RLOGD("setRadioState(%d)", newState);
2247 RIL_RadioState oldState;
2249 pthread_mutex_lock(&s_state_mutex);
2254 // If we're closed, the only reasonable state is
2255 // RADIO_STATE_UNAVAILABLE
2256 // This is here because things on the main thread
2257 // may attempt to change the radio state after the closed
2258 // event happened in another thread
2259 newState = RADIO_STATE_UNAVAILABLE;
2262 if (sState != newState || s_closed > 0) {
2265 pthread_cond_broadcast (&s_state_cond);
2268 pthread_mutex_unlock(&s_state_mutex);
2271 /* do these outside of the mutex */
2272 if (sState != oldState) {
2273 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2276 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2277 * from the AT reader thread
2278 * Currently, this doesn't happen, but if that changes then these
2279 * will need to be dispatched on the request thread
2281 if (sState == RADIO_STATE_ON) {
2287 /** Returns RUIM_NOT_READY on error */
2291 ATResponse *p_response = NULL;
2297 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2298 ret = SIM_NOT_READY;
2302 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2305 ret = SIM_NOT_READY;
2309 switch (at_get_cme_error(p_response)) {
2313 case CME_SIM_NOT_INSERTED:
2318 ret = SIM_NOT_READY;
2322 /* CPIN? has succeeded, now look at the result */
2324 cpinLine = p_response->p_intermediates->line;
2325 err = at_tok_start (&cpinLine);
2328 ret = SIM_NOT_READY;
2332 err = at_tok_nextstr(&cpinLine, &cpinResult);
2335 ret = SIM_NOT_READY;
2339 if (0 == strcmp (cpinResult, "SIM PIN")) {
2342 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2345 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2346 return SIM_NETWORK_PERSONALIZATION;
2347 } else if (0 != strcmp (cpinResult, "READY")) {
2348 /* we're treating unsupported lock types as "sim absent" */
2353 at_response_free(p_response);
2360 at_response_free(p_response);
2364 /** Returns SIM_NOT_READY on error */
2368 ATResponse *p_response = NULL;
2374 RLOGD("getSIMStatus(). sState: %d",sState);
2375 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2376 ret = SIM_NOT_READY;
2380 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2383 ret = SIM_NOT_READY;
2387 switch (at_get_cme_error(p_response)) {
2391 case CME_SIM_NOT_INSERTED:
2396 ret = SIM_NOT_READY;
2400 /* CPIN? has succeeded, now look at the result */
2402 cpinLine = p_response->p_intermediates->line;
2403 err = at_tok_start (&cpinLine);
2406 ret = SIM_NOT_READY;
2410 err = at_tok_nextstr(&cpinLine, &cpinResult);
2413 ret = SIM_NOT_READY;
2417 if (0 == strcmp (cpinResult, "SIM PIN")) {
2420 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2423 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2424 return SIM_NETWORK_PERSONALIZATION;
2425 } else if (0 != strcmp (cpinResult, "READY")) {
2426 /* we're treating unsupported lock types as "sim absent" */
2431 at_response_free(p_response);
2438 at_response_free(p_response);
2444 * Get the current card status.
2446 * This must be freed using freeCardStatus.
2447 * @return: On success returns RIL_E_SUCCESS
2449 static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
2450 static RIL_AppStatus app_status_array[] = {
2452 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2453 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2454 // SIM_NOT_READY = 1
2455 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2456 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2458 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2459 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2461 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2462 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2464 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2465 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2466 // SIM_NETWORK_PERSONALIZATION = 5
2467 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2468 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2470 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2471 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2472 // RUIM_NOT_READY = 7
2473 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2474 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2476 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2477 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2479 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2480 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2482 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2483 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2484 // RUIM_NETWORK_PERSONALIZATION = 11
2485 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2486 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
2488 RIL_CardState card_state;
2491 int sim_status = getSIMStatus();
2492 if (sim_status == SIM_ABSENT) {
2493 card_state = RIL_CARDSTATE_ABSENT;
2496 card_state = RIL_CARDSTATE_PRESENT;
2500 // Allocate and initialize base card status.
2501 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
2502 p_card_status->card_state = card_state;
2503 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
2504 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
2505 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
2506 p_card_status->ims_subscription_app_index = RIL_CARD_MAX_APPS;
2507 p_card_status->num_applications = num_apps;
2509 // Initialize application status
2511 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
2512 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
2515 // Pickup the appropriate application status
2516 // that reflects sim_status for gsm.
2517 if (num_apps != 0) {
2518 // Only support one app, gsm
2519 p_card_status->num_applications = 2;
2520 p_card_status->gsm_umts_subscription_app_index = 0;
2521 p_card_status->cdma_subscription_app_index = 1;
2523 // Get the correct app status
2524 p_card_status->applications[0] = app_status_array[sim_status];
2525 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
2528 *pp_card_status = p_card_status;
2529 return RIL_E_SUCCESS;
2533 * Free the card status returned by getCardStatus
2535 static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
2536 free(p_card_status);
2540 * SIM ready means any commands that access the SIM will work, including:
2541 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
2542 * (all SMS-related commands)
2545 static void pollSIMState (void *param)
2547 ATResponse *p_response;
2550 if (sState != RADIO_STATE_SIM_NOT_READY) {
2551 // no longer valid to poll
2555 switch(getSIMStatus()) {
2559 case SIM_NETWORK_PERSONALIZATION:
2561 RLOGI("SIM ABSENT or LOCKED");
2562 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
2566 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
2572 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
2577 /** returns 1 if on, 0 if off, and -1 on error */
2578 static int isRadioOn()
2580 ATResponse *p_response = NULL;
2585 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
2587 if (err < 0 || p_response->success == 0) {
2588 // assume radio is off
2592 line = p_response->p_intermediates->line;
2594 err = at_tok_start(&line);
2595 if (err < 0) goto error;
2597 err = at_tok_nextbool(&line, &ret);
2598 if (err < 0) goto error;
2600 at_response_free(p_response);
2606 at_response_free(p_response);
2611 * Parse the response generated by a +CTEC AT command
2612 * The values read from the response are stored in current and preferred.
2613 * Both current and preferred may be null. The corresponding value is ignored in that case.
2615 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
2616 * 1 if the response includes the current technology only
2617 * 0 if the response includes both current technology and preferred mode
2619 int parse_technology_response( const char *response, int *current, int32_t *preferred )
2627 line = p = strdup(response);
2628 RLOGD("Response: %s", line);
2629 err = at_tok_start(&p);
2630 if (err || !at_tok_hasmore(&p)) {
2631 RLOGD("err: %d. p: %s", err, p);
2636 err = at_tok_nextint(&p, &ct);
2641 if (current) *current = ct;
2643 RLOGD("line remaining after int: %s", p);
2645 err = at_tok_nexthexint(&p, &pt);
2658 int query_supported_techs( ModemInfo *mdm, int *supported )
2660 ATResponse *p_response;
2661 int err, val, techs = 0;
2665 RLOGD("query_supported_techs");
2666 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
2667 if (err || !p_response->success)
2669 line = p_response->p_intermediates->line;
2670 err = at_tok_start(&line);
2671 if (err || !at_tok_hasmore(&line))
2673 while (!at_tok_nextint(&line, &val)) {
2674 techs |= ( 1 << val );
2676 if (supported) *supported = techs;
2679 at_response_free(p_response);
2684 * query_ctec. Send the +CTEC AT command to the modem to query the current
2685 * and preferred modes. It leaves values in the addresses pointed to by
2686 * current and preferred. If any of those pointers are NULL, the corresponding value
2687 * is ignored, but the return value will still reflect if retreiving and parsing of the
2690 * @mdm Currently unused
2691 * @current A pointer to store the current mode returned by the modem. May be null.
2692 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
2693 * @return -1 on error (or failure to parse)
2694 * 1 if only the current mode was returned by modem (or failed to parse preferred)
2695 * 0 if both current and preferred were returned correctly
2697 int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred)
2699 ATResponse *response = NULL;
2703 RLOGD("query_ctec. current: %d, preferred: %d", (int)current, (int) preferred);
2704 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
2705 if (!err && response->success) {
2706 res = parse_technology_response(response->p_intermediates->line, current, preferred);
2707 at_response_free(response);
2710 RLOGE("Error executing command: %d. response: %x. status: %d", err, (int)response, response? response->success : -1);
2711 at_response_free(response);
2715 int is_multimode_modem(ModemInfo *mdm)
2717 ATResponse *response;
2723 if (query_ctec(mdm, &tech, &preferred) == 0) {
2724 mdm->currentTech = tech;
2725 mdm->preferredNetworkMode = preferred;
2726 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
2735 * Find out if our modem is GSM, CDMA or both (Multimode)
2737 static void probeForModemMode(ModemInfo *info)
2739 ATResponse *response;
2742 // Currently, our only known multimode modem is qemu's android modem,
2743 // which implements the AT+CTEC command to query and set mode.
2746 if (is_multimode_modem(info)) {
2747 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
2748 info->supportedTechs, info->currentTech);
2752 /* Being here means that our modem is not multimode */
2753 info->isMultimode = 0;
2755 /* CDMA Modems implement the AT+WNAM command */
2756 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
2757 if (!err && response->success) {
2758 at_response_free(response);
2759 // TODO: find out if we really support EvDo
2760 info->supportedTechs = MDM_CDMA | MDM_EVDO;
2761 info->currentTech = MDM_CDMA;
2762 RLOGI("Found CDMA Modem");
2765 if (!err) at_response_free(response);
2766 // TODO: find out if modem really supports WCDMA/LTE
2767 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
2768 info->currentTech = MDM_GSM;
2769 RLOGI("Found GSM Modem");
2773 * Initialize everything that can be configured while we're still in
2776 static void initializeCallback(void *param)
2778 ATResponse *p_response = NULL;
2781 setRadioState (RADIO_STATE_OFF);
2785 probeForModemMode(sMdmInfo);
2786 /* note: we don't check errors here. Everything important will
2787 be handled in onATTimeout and onATReaderClosed */
2789 /* atchannel is tolerant of echo but it must */
2790 /* have verbose result codes */
2791 at_send_command("ATE0Q0V1", NULL);
2793 /* No auto-answer */
2794 at_send_command("ATS0=0", NULL);
2796 /* Extended errors */
2797 at_send_command("AT+CMEE=1", NULL);
2799 /* Network registration events */
2800 err = at_send_command("AT+CREG=2", &p_response);
2802 /* some handsets -- in tethered mode -- don't support CREG=2 */
2803 if (err < 0 || p_response->success == 0) {
2804 at_send_command("AT+CREG=1", NULL);
2807 at_response_free(p_response);
2809 /* GPRS registration events */
2810 at_send_command("AT+CGREG=1", NULL);
2812 /* Call Waiting notifications */
2813 at_send_command("AT+CCWA=1", NULL);
2815 /* Alternating voice/data off */
2816 at_send_command("AT+CMOD=0", NULL);
2819 at_send_command("AT+CMUT=0", NULL);
2821 /* +CSSU unsolicited supp service notifications */
2822 at_send_command("AT+CSSN=0,1", NULL);
2824 /* no connected line identification */
2825 at_send_command("AT+COLP=0", NULL);
2827 /* HEX character set */
2828 at_send_command("AT+CSCS=\"HEX\"", NULL);
2830 /* USSD unsolicited */
2831 at_send_command("AT+CUSD=1", NULL);
2833 /* Enable +CGEV GPRS event notifications, but don't buffer */
2834 at_send_command("AT+CGEREP=1,0", NULL);
2837 at_send_command("AT+CMGF=0", NULL);
2839 #ifdef USE_TI_COMMANDS
2841 at_send_command("AT%CPI=3", NULL);
2843 /* TI specific -- notifications when SMS is ready (currently ignored) */
2844 at_send_command("AT%CSTAT=1", NULL);
2846 #endif /* USE_TI_COMMANDS */
2849 /* assume radio is off on error */
2850 if (isRadioOn() > 0) {
2851 setRadioState (RADIO_STATE_ON);
2855 static void waitForClose()
2857 pthread_mutex_lock(&s_state_mutex);
2859 while (s_closed == 0) {
2860 pthread_cond_wait(&s_state_cond, &s_state_mutex);
2863 pthread_mutex_unlock(&s_state_mutex);
2867 * Called by atchannel when an unsolicited line appears
2868 * This is called on atchannel's reader thread. AT commands may
2869 * not be issued here
2871 static void onUnsolicited (const char *s, const char *sms_pdu)
2873 char *line = NULL, *p;
2876 /* Ignore unsolicited responses until we're initialized.
2877 * This is OK because the RIL library will poll for initial state
2879 if (sState == RADIO_STATE_UNAVAILABLE) {
2883 if (strStartsWith(s, "%CTZV:")) {
2884 /* TI specific -- NITZ time */
2887 line = p = strdup(s);
2890 err = at_tok_nextstr(&p, &response);
2894 RLOGE("invalid NITZ line %s\n", s);
2896 RIL_onUnsolicitedResponse (
2897 RIL_UNSOL_NITZ_TIME_RECEIVED,
2898 response, strlen(response));
2900 } else if (strStartsWith(s,"+CRING:")
2901 || strStartsWith(s,"RING")
2902 || strStartsWith(s,"NO CARRIER")
2903 || strStartsWith(s,"+CCWA")
2905 RIL_onUnsolicitedResponse (
2906 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
2908 #ifdef WORKAROUND_FAKE_CGEV
2909 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
2910 #endif /* WORKAROUND_FAKE_CGEV */
2911 } else if (strStartsWith(s,"+CREG:")
2912 || strStartsWith(s,"+CGREG:")
2914 RIL_onUnsolicitedResponse (
2915 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
2917 #ifdef WORKAROUND_FAKE_CGEV
2918 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
2919 #endif /* WORKAROUND_FAKE_CGEV */
2920 } else if (strStartsWith(s, "+CMT:")) {
2921 RIL_onUnsolicitedResponse (
2922 RIL_UNSOL_RESPONSE_NEW_SMS,
2923 sms_pdu, strlen(sms_pdu));
2924 } else if (strStartsWith(s, "+CDS:")) {
2925 RIL_onUnsolicitedResponse (
2926 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
2927 sms_pdu, strlen(sms_pdu));
2928 } else if (strStartsWith(s, "+CGEV:")) {
2929 /* Really, we can ignore NW CLASS and ME CLASS events here,
2930 * but right now we don't since extranous
2931 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
2933 /* can't issue AT commands here -- call on main thread */
2934 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
2935 #ifdef WORKAROUND_FAKE_CGEV
2936 } else if (strStartsWith(s, "+CME ERROR: 150")) {
2937 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
2938 #endif /* WORKAROUND_FAKE_CGEV */
2939 } else if (strStartsWith(s, "+CTEC: ")) {
2941 switch (parse_technology_response(s, &tech, NULL))
2943 case -1: // no argument could be parsed.
2944 RLOGE("invalid CTEC line %s\n", s);
2946 case 1: // current mode correctly parsed
2947 case 0: // preferred mode correctly parsed
2949 if (mask != MDM_GSM && mask != MDM_CDMA &&
2950 mask != MDM_WCDMA && mask != MDM_LTE) {
2951 RLOGE("Unknown technology %d\n", tech);
2953 setRadioTechnology(sMdmInfo, tech);
2957 } else if (strStartsWith(s, "+CCSS: ")) {
2959 line = p = strdup(s);
2961 RLOGE("+CCSS: Unable to allocate memory");
2964 if (at_tok_start(&p) < 0) {
2968 if (at_tok_nextint(&p, &source) < 0) {
2969 RLOGE("invalid +CCSS response: %s", line);
2973 SSOURCE(sMdmInfo) = source;
2974 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
2975 &source, sizeof(source));
2976 } else if (strStartsWith(s, "+WSOS: ")) {
2979 line = p = strdup(s);
2981 RLOGE("+WSOS: Unable to allocate memory");
2984 if (at_tok_start(&p) < 0) {
2988 if (at_tok_nextbool(&p, &state) < 0) {
2989 RLOGE("invalid +WSOS response: %s", line);
2996 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
2998 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3000 } else if (strStartsWith(s, "+WPRL: ")) {
3002 line = p = strdup(s);
3004 RLOGE("+WPRL: Unable to allocate memory");
3007 if (at_tok_start(&p) < 0) {
3008 RLOGE("invalid +WPRL response: %s", s);
3012 if (at_tok_nextint(&p, &version) < 0) {
3013 RLOGE("invalid +WPRL response: %s", s);
3018 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3019 } else if (strStartsWith(s, "+CFUN: 0")) {
3020 setRadioState(RADIO_STATE_OFF);
3024 /* Called on command or reader thread */
3025 static void onATReaderClosed()
3027 RLOGI("AT channel closed\n");
3031 setRadioState (RADIO_STATE_UNAVAILABLE);
3034 /* Called on command thread */
3035 static void onATTimeout()
3037 RLOGI("AT channel timeout; closing\n");
3042 /* FIXME cause a radio reset here */
3044 setRadioState (RADIO_STATE_UNAVAILABLE);
3047 static void usage(char *s)
3050 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3052 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3058 mainLoop(void *param)
3063 AT_DUMP("== ", "entering mainLoop()", -1 );
3064 at_set_on_reader_closed(onATReaderClosed);
3065 at_set_on_timeout(onATTimeout);
3071 fd = socket_loopback_client(s_port, SOCK_STREAM);
3072 } else if (s_device_socket) {
3073 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
3074 /* Before trying to connect to /dev/socket/qemud (which is
3075 * now another "legacy" way of communicating with the
3076 * emulator), we will try to connecto to gsm service via
3078 fd = qemu_pipe_open("qemud:gsm");
3080 /* Qemu-specific control socket */
3081 fd = socket_local_client( "qemud",
3082 ANDROID_SOCKET_NAMESPACE_RESERVED,
3087 if ( write(fd, "gsm", 3) != 3 ||
3088 read(fd, answer, 2) != 2 ||
3089 memcmp(answer, "OK", 2) != 0)
3098 fd = socket_local_client( s_device_path,
3099 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3101 } else if (s_device_path != NULL) {
3102 fd = open (s_device_path, O_RDWR);
3103 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3104 /* disable echo on serial ports */
3106 tcgetattr( fd, &ios );
3107 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3108 tcsetattr( fd, TCSANOW, &ios );
3113 perror ("opening AT interface. retrying...");
3120 ret = at_open(fd, onUnsolicited);
3123 RLOGE ("AT error %d on at_open\n", ret);
3127 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3129 // Give initializeCallback a chance to dispatched, since
3130 // we don't presently have a cancellation mechanism
3134 RLOGI("Re-opening after close");
3140 pthread_t s_tid_mainloop;
3142 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3147 pthread_attr_t attr;
3151 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
3154 s_port = atoi(optarg);
3159 RLOGI("Opening loopback port %d\n", s_port);
3163 s_device_path = optarg;
3164 RLOGI("Opening tty device %s\n", s_device_path);
3168 s_device_path = optarg;
3169 s_device_socket = 1;
3170 RLOGI("Opening socket %s\n", s_device_path);
3179 if (s_port < 0 && s_device_path == NULL) {
3184 sMdmInfo = calloc(1, sizeof(ModemInfo));
3186 RLOGE("Unable to alloc memory for ModemInfo");
3189 pthread_attr_init (&attr);
3190 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3191 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3193 return &s_callbacks;
3195 #else /* RIL_SHLIB */
3196 int main (int argc, char **argv)
3202 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3205 s_port = atoi(optarg);
3209 RLOGI("Opening loopback port %d\n", s_port);
3213 s_device_path = optarg;
3214 RLOGI("Opening tty device %s\n", s_device_path);
3218 s_device_path = optarg;
3219 s_device_socket = 1;
3220 RLOGI("Opening socket %s\n", s_device_path);
3228 if (s_port < 0 && s_device_path == NULL) {
3232 RIL_register(&s_callbacks);
3239 #endif /* RIL_SHLIB */