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>
19 #include <telephony/librilutils.h>
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
32 #include "atchannel.h"
36 #include <sys/socket.h>
37 #include <cutils/sockets.h>
38 #include <sys/system_properties.h>
40 #include <qemu_pipe.h>
45 #include <utils/Log.h>
47 static void *noopRemoveWarning( void *a ) { return a; }
48 #define RIL_UNUSED_PARM(a) noopRemoveWarning((void *)&(a));
50 #define MAX_AT_RESPONSE 0x1000
52 /* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
53 #define PPP_TTY_PATH "eth0"
56 #define DEFAULT_MTU 1500
58 #ifdef USE_TI_COMMANDS
60 // Enable a workaround
61 // 1) Make incoming call, do not answer
62 // 2) Hangup remote end
63 // Expected: call should disappear from CLCC line
64 // Actual: Call shows as "ACTIVE" before disappearing
65 #define WORKAROUND_ERRONEOUS_ANSWER 1
67 // Some varients of the TI stack do not support the +CGEV unsolicited
68 // response. However, they seem to send an unsolicited +CME ERROR: 150
69 #define WORKAROUND_FAKE_CGEV 1
72 /* Modem Technology bits */
74 #define MDM_WCDMA 0x02
80 int supportedTechs; // Bitmask of supported Modem Technology bits
81 int currentTech; // Technology the modem is currently using (in the format used by modem)
84 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
85 // in which the byte number from LSB to MSB give the priority.
88 // value: |00 |00 |00 |00
89 // byte #: |3 |2 |1 |0
91 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
92 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
93 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
94 int32_t preferredNetworkMode;
95 int subscription_source;
99 static ModemInfo *sMdmInfo;
100 // TECH returns the current technology in the format used by the modem.
101 // It can be used as an l-value
102 #define TECH(mdminfo) ((mdminfo)->currentTech)
103 // TECH_BIT returns the bitmask equivalent of the current tech
104 #define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
105 #define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
106 #define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
107 #define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
108 // CDMA Subscription Source
109 #define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
111 static int net2modem[] = {
112 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
113 MDM_GSM, // 1 - GSM only
114 MDM_WCDMA, // 2 - WCDMA only
115 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
116 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
117 MDM_CDMA, // 5 - CDMA only
118 MDM_EVDO, // 6 - EvDo only
119 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
120 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
121 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
122 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
123 MDM_LTE, // 11 - LTE only
126 static int32_t net2pmask[] = {
127 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
128 MDM_GSM, // 1 - GSM only
129 MDM_WCDMA, // 2 - WCDMA only
130 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
131 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
132 MDM_CDMA, // 5 - CDMA only
133 MDM_EVDO, // 6 - EvDo only
134 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
135 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
136 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
137 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
138 MDM_LTE, // 11 - LTE only
141 static int is3gpp2(int radioTech) {
143 case RADIO_TECH_IS95A:
144 case RADIO_TECH_IS95B:
145 case RADIO_TECH_1xRTT:
146 case RADIO_TECH_EVDO_0:
147 case RADIO_TECH_EVDO_A:
148 case RADIO_TECH_EVDO_B:
149 case RADIO_TECH_EHRPD:
162 SIM_NETWORK_PERSONALIZATION = 5,
168 RUIM_NETWORK_PERSONALIZATION = 11
171 static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
172 static RIL_RadioState currentState();
173 static int onSupports (int requestCode);
174 static void onCancel (RIL_Token t);
175 static const char *getVersion();
176 static int isRadioOn();
177 static SIM_Status getSIMStatus();
178 static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
179 static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
180 static void onDataCallListChanged(void *param);
182 extern const char * requestToString(int request);
184 /*** Static Variables ***/
185 static const RIL_RadioFunctions s_callbacks = {
195 static const struct RIL_Env *s_rilenv;
197 #define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
198 #define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
199 #define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
202 static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
204 static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
205 static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
207 static int s_port = -1;
208 static const char * s_device_path = NULL;
209 static int s_device_socket = 0;
211 /* trigger change to this with s_state_cond */
212 static int s_closed = 0;
214 static int sFD; /* file desc of AT channel */
215 static char sATBuffer[MAX_AT_RESPONSE+1];
216 static char *sATBufferCur = NULL;
218 static const struct timeval TIMEVAL_SIMPOLL = {1,0};
219 static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
220 static const struct timeval TIMEVAL_0 = {0,0};
222 static int s_ims_registered = 0; // 0==unregistered
223 static int s_ims_services = 1; // & 0x1 == sms over ims supported
224 static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
225 static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
226 static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
227 static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
228 static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
230 #ifdef WORKAROUND_ERRONEOUS_ANSWER
231 // Max number of times we'll try to repoll when we think
232 // we have a AT+CLCC race condition
233 #define REPOLL_CALLS_COUNT_MAX 4
235 // Line index that was incoming or waiting at last poll, or -1 for none
236 static int s_incomingOrWaitingLine = -1;
237 // Number of times we've asked for a repoll of AT+CLCC
238 static int s_repollCallsCount = 0;
239 // Should we expect a call to be answered in the next CLCC?
240 static int s_expectAnswer = 0;
241 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
244 static int s_cell_info_rate_ms = INT_MAX;
245 static int s_mcc = 0;
246 static int s_mnc = 0;
247 static int s_lac = 0;
248 static int s_cid = 0;
250 static void pollSIMState (void *param);
251 static void setRadioState(RIL_RadioState newState);
252 static void setRadioTechnology(ModemInfo *mdm, int newtech);
253 static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
254 static int parse_technology_response(const char *response, int *current, int32_t *preferred);
255 static int techFromModemType(int mdmtype);
257 static int clccStateToRILState(int state, RIL_CallState *p_state)
261 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
262 case 1: *p_state = RIL_CALL_HOLDING; return 0;
263 case 2: *p_state = RIL_CALL_DIALING; return 0;
264 case 3: *p_state = RIL_CALL_ALERTING; return 0;
265 case 4: *p_state = RIL_CALL_INCOMING; return 0;
266 case 5: *p_state = RIL_CALL_WAITING; return 0;
272 * Note: directly modified line and has *p_call point directly into
275 static int callFromCLCCLine(char *line, RIL_Call *p_call)
277 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
278 // index,isMT,state,mode,isMpty(,number,TOA)?
284 err = at_tok_start(&line);
285 if (err < 0) goto error;
287 err = at_tok_nextint(&line, &(p_call->index));
288 if (err < 0) goto error;
290 err = at_tok_nextbool(&line, &(p_call->isMT));
291 if (err < 0) goto error;
293 err = at_tok_nextint(&line, &state);
294 if (err < 0) goto error;
296 err = clccStateToRILState(state, &(p_call->state));
297 if (err < 0) goto error;
299 err = at_tok_nextint(&line, &mode);
300 if (err < 0) goto error;
302 p_call->isVoice = (mode == 0);
304 err = at_tok_nextbool(&line, &(p_call->isMpty));
305 if (err < 0) goto error;
307 if (at_tok_hasmore(&line)) {
308 err = at_tok_nextstr(&line, &(p_call->number));
310 /* tolerate null here */
311 if (err < 0) return 0;
313 // Some lame implementations return strings
314 // like "NOT AVAILABLE" in the CLCC line
315 if (p_call->number != NULL
316 && 0 == strspn(p_call->number, "+0123456789")
318 p_call->number = NULL;
321 err = at_tok_nextint(&line, &p_call->toa);
322 if (err < 0) goto error;
325 p_call->uusInfo = NULL;
330 RLOGE("invalid CLCC line\n");
334 static int parseSimResponseLine(char* line, RIL_SIM_IO_Response* response) {
337 err = at_tok_start(&line);
338 if (err < 0) return err;
339 err = at_tok_nextint(&line, &response->sw1);
340 if (err < 0) return err;
341 err = at_tok_nextint(&line, &response->sw2);
342 if (err < 0) return err;
344 if (at_tok_hasmore(&line)) {
345 err = at_tok_nextstr(&line, &response->simResponse);
346 if (err < 0) return err;
351 /** do post-AT+CFUN=1 initialization */
352 static void onRadioPowerOn()
354 #ifdef USE_TI_COMMANDS
355 /* Must be after CFUN=1 */
356 /* TI specific -- notifications for CPHS things such */
357 /* as CPHS message waiting indicator */
359 at_send_command("AT%CPHS=1", NULL);
361 /* TI specific -- enable NITZ unsol notifs */
362 at_send_command("AT%CTZV=1", NULL);
368 /** do post- SIM ready initialization */
369 static void onSIMReady()
371 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
373 * Always send SMS messages directly to the TE
375 * mode = 1 // discard when link is reserved (link should never be
377 * mt = 2 // most messages routed to TE
378 * bm = 2 // new cell BM's routed to TE
379 * ds = 1 // Status reports routed to TE
380 * bfr = 1 // flush buffer
382 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
385 static void requestRadioPower(void *data, size_t datalen __unused, RIL_Token t)
390 ATResponse *p_response = NULL;
392 assert (datalen >= sizeof(int *));
393 onOff = ((int *)data)[0];
395 if (onOff == 0 && sState != RADIO_STATE_OFF) {
396 err = at_send_command("AT+CFUN=0", &p_response);
397 if (err < 0 || p_response->success == 0) goto error;
398 setRadioState(RADIO_STATE_OFF);
399 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
400 err = at_send_command("AT+CFUN=1", &p_response);
401 if (err < 0|| p_response->success == 0) {
402 // Some stacks return an error when there is no SIM,
403 // but they really turn the RF portion on
404 // So, if we get an error, let's check to see if it
407 if (isRadioOn() != 1) {
411 setRadioState(RADIO_STATE_ON);
414 at_response_free(p_response);
415 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
418 at_response_free(p_response);
419 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
422 static void requestShutdown(RIL_Token t)
427 ATResponse *p_response = NULL;
429 if (sState != RADIO_STATE_OFF) {
430 err = at_send_command("AT+CFUN=0", &p_response);
431 setRadioState(RADIO_STATE_UNAVAILABLE);
434 at_response_free(p_response);
435 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
439 static void requestOrSendDataCallList(RIL_Token *t);
441 static void onDataCallListChanged(void *param __unused)
443 requestOrSendDataCallList(NULL);
446 static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
448 requestOrSendDataCallList(&t);
451 static void requestOrSendDataCallList(RIL_Token *t)
453 ATResponse *p_response;
459 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
460 if (err != 0 || p_response->success == 0) {
462 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
464 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
469 for (p_cur = p_response->p_intermediates; p_cur != NULL;
470 p_cur = p_cur->p_next)
473 RIL_Data_Call_Response_v11 *responses =
474 alloca(n * sizeof(RIL_Data_Call_Response_v11));
477 for (i = 0; i < n; i++) {
478 responses[i].status = -1;
479 responses[i].suggestedRetryTime = -1;
480 responses[i].cid = -1;
481 responses[i].active = -1;
482 responses[i].type = "";
483 responses[i].ifname = "";
484 responses[i].addresses = "";
485 responses[i].dnses = "";
486 responses[i].gateways = "";
487 responses[i].pcscf = "";
488 responses[i].mtu = 0;
491 RIL_Data_Call_Response_v11 *response = responses;
492 for (p_cur = p_response->p_intermediates; p_cur != NULL;
493 p_cur = p_cur->p_next) {
494 char *line = p_cur->line;
496 err = at_tok_start(&line);
500 err = at_tok_nextint(&line, &response->cid);
504 err = at_tok_nextint(&line, &response->active);
511 at_response_free(p_response);
513 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
514 if (err != 0 || p_response->success == 0) {
516 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
518 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
523 for (p_cur = p_response->p_intermediates; p_cur != NULL;
524 p_cur = p_cur->p_next) {
525 char *line = p_cur->line;
528 err = at_tok_start(&line);
532 err = at_tok_nextint(&line, &cid);
536 for (i = 0; i < n; i++) {
537 if (responses[i].cid == cid)
542 /* details for a context we didn't hear about in the last request */
547 responses[i].status = 0;
550 err = at_tok_nextstr(&line, &out);
554 int type_size = strlen(out) + 1;
555 responses[i].type = alloca(type_size);
556 strlcpy(responses[i].type, out, type_size);
558 // APN ignored for v5
559 err = at_tok_nextstr(&line, &out);
563 int ifname_size = strlen(PPP_TTY_PATH) + 1;
564 responses[i].ifname = alloca(ifname_size);
565 strlcpy(responses[i].ifname, PPP_TTY_PATH, ifname_size);
567 err = at_tok_nextstr(&line, &out);
571 int addresses_size = strlen(out) + 1;
572 responses[i].addresses = alloca(addresses_size);
573 strlcpy(responses[i].addresses, out, addresses_size);
575 if (isInEmulator()) {
576 /* We are in the emulator - the dns servers are listed
577 * by the following system properties, setup in
578 * /system/etc/init.goldfish.sh:
584 const int dnslist_sz = 128;
585 char* dnslist = alloca(dnslist_sz);
586 const char* separator = "";
590 for (nn = 1; nn <= 4; nn++) {
591 /* Probe net.eth0.dns<n> */
592 char propName[PROP_NAME_MAX];
593 char propValue[PROP_VALUE_MAX];
595 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
597 /* Ignore if undefined */
598 if (__system_property_get(propName, propValue) == 0) {
602 /* Append the DNS IP address */
603 strlcat(dnslist, separator, dnslist_sz);
604 strlcat(dnslist, propValue, dnslist_sz);
607 responses[i].dnses = dnslist;
609 responses[i].gateways = "10.0.2.15 fe80::2";
610 responses[i].mtu = DEFAULT_MTU;
613 /* I don't know where we are, so use the public Google DNS
614 * servers by default and no gateway.
616 responses[i].dnses = "8.8.8.8 8.8.4.4";
617 responses[i].gateways = "";
621 at_response_free(p_response);
624 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
625 n * sizeof(RIL_Data_Call_Response_v11));
627 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
629 n * sizeof(RIL_Data_Call_Response_v11));
635 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
637 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
640 at_response_free(p_response);
643 static void requestQueryNetworkSelectionMode(
644 void *data __unused, size_t datalen __unused, RIL_Token t)
647 ATResponse *p_response = NULL;
651 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
653 if (err < 0 || p_response->success == 0) {
657 line = p_response->p_intermediates->line;
659 err = at_tok_start(&line);
665 err = at_tok_nextint(&line, &response);
671 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
672 at_response_free(p_response);
675 at_response_free(p_response);
676 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
677 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
680 static void sendCallStateChanged(void *param __unused)
682 RIL_onUnsolicitedResponse (
683 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
687 static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
690 ATResponse *p_response;
699 #ifdef WORKAROUND_ERRONEOUS_ANSWER
700 int prevIncomingOrWaitingLine;
702 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
703 s_incomingOrWaitingLine = -1;
704 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
706 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
708 if (err != 0 || p_response->success == 0) {
709 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
713 /* count the calls */
714 for (countCalls = 0, p_cur = p_response->p_intermediates
716 ; p_cur = p_cur->p_next
721 /* yes, there's an array of pointers and then an array of structures */
723 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
724 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
725 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
727 /* init the pointer array */
728 for(i = 0; i < countCalls ; i++) {
729 pp_calls[i] = &(p_calls[i]);
732 for (countValidCalls = 0, p_cur = p_response->p_intermediates
734 ; p_cur = p_cur->p_next
736 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
742 #ifdef WORKAROUND_ERRONEOUS_ANSWER
743 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
744 || p_calls[countValidCalls].state == RIL_CALL_WAITING
746 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
748 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
750 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
751 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
759 #ifdef WORKAROUND_ERRONEOUS_ANSWER
761 // A call was incoming or waiting
762 // Now it's marked as active
763 // But we never answered it
765 // This is probably a bug, and the call will probably
766 // disappear from the call list in the next poll
767 if (prevIncomingOrWaitingLine >= 0
768 && s_incomingOrWaitingLine < 0
769 && s_expectAnswer == 0
771 for (i = 0; i < countValidCalls ; i++) {
773 if (p_calls[i].index == prevIncomingOrWaitingLine
774 && p_calls[i].state == RIL_CALL_ACTIVE
775 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
778 "Hit WORKAROUND_ERRONOUS_ANSWER case."
779 " Repoll count: %d\n", s_repollCallsCount);
780 s_repollCallsCount++;
787 s_repollCallsCount = 0;
788 #endif /*WORKAROUND_ERRONEOUS_ANSWER*/
790 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
791 countValidCalls * sizeof (RIL_Call *));
793 at_response_free(p_response);
795 #ifdef POLL_CALL_STATE
796 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
797 // smd, so we're forced to poll until the call ends.
801 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
805 #ifdef WORKAROUND_ERRONEOUS_ANSWER
807 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
808 at_response_free(p_response);
812 static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
819 p_dial = (RIL_Dial *)data;
821 switch (p_dial->clir) {
822 case 1: clir = "I"; break; /*invocation*/
823 case 2: clir = "i"; break; /*suppression*/
825 case 0: clir = ""; break; /*subscription default*/
828 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
830 ret = at_send_command(cmd, NULL);
834 /* success or failure is ignored by the upper layer here.
835 it will call GET_CURRENT_CALLS and determine success that way */
836 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
839 static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
841 RIL_SMS_WriteArgs *p_args;
845 ATResponse *p_response = NULL;
847 p_args = (RIL_SMS_WriteArgs *)data;
849 length = strlen(p_args->pdu)/2;
850 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
852 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
854 if (err != 0 || p_response->success == 0) goto error;
856 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
857 at_response_free(p_response);
861 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
862 at_response_free(p_response);
865 static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
872 p_line = (int *)data;
875 // "Releases a specific active call X"
876 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
878 ret = at_send_command(cmd, NULL);
882 /* success or failure is ignored by the upper layer here.
883 it will call GET_CURRENT_CALLS and determine success that way */
884 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
887 static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
889 ATResponse *p_response = NULL;
893 // Accept a response that is at least v6, and up to v10
894 int minNumOfElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
895 int maxNumOfElements=sizeof(RIL_SignalStrength_v10)/sizeof(int);
896 int response[maxNumOfElements];
898 memset(response, 0, sizeof(response));
900 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
902 if (err < 0 || p_response->success == 0) {
903 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
907 line = p_response->p_intermediates->line;
909 err = at_tok_start(&line);
910 if (err < 0) goto error;
912 for (count = 0; count < maxNumOfElements; count++) {
913 err = at_tok_nextint(&line, &(response[count]));
914 if (err < 0 && count < minNumOfElements) goto error;
917 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
919 at_response_free(p_response);
923 RLOGE("requestSignalStrength must never return an error when radio is on");
924 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
925 at_response_free(p_response);
929 * networkModePossible. Decides whether the network mode is appropriate for the
932 static int networkModePossible(ModemInfo *mdm, int nm)
934 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
939 static void requestSetPreferredNetworkType( int request __unused, void *data,
940 size_t datalen __unused, RIL_Token t )
942 ATResponse *p_response = NULL;
944 int value = *(int *)data;
947 int32_t preferred = net2pmask[value];
949 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
950 if (!networkModePossible(sMdmInfo, value)) {
951 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
954 if (query_ctec(sMdmInfo, ¤t, NULL) < 0) {
955 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
958 old = PREFERRED_NETWORK(sMdmInfo);
959 RLOGD("old != preferred: %d", old != preferred);
960 if (old != preferred) {
961 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
962 RLOGD("Sending command: <%s>", cmd);
963 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
965 if (err || !p_response->success) {
966 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
969 PREFERRED_NETWORK(sMdmInfo) = value;
970 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
972 int res = parse_technology_response(p_response->p_intermediates->line, ¤t, NULL);
974 case -1: // Error or unable to parse
976 case 1: // Only able to parse current
977 case 0: // Both current and preferred were parsed
978 setRadioTechnology(sMdmInfo, current);
983 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
986 static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
987 size_t datalen __unused, RIL_Token t)
992 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
993 case -1: // Error or unable to parse
994 case 1: // Only able to parse current
995 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
997 case 0: // Both current and preferred were parsed
998 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
999 if (preferred == net2pmask[i]) {
1000 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1004 RLOGE("Unknown preferred mode received from modem: %d", preferred);
1005 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1011 static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1012 size_t datalen __unused, RIL_Token t)
1016 ATResponse *p_response = NULL;
1020 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1021 if (err < 0 || !p_response->success) goto error;
1022 line = p_response->p_intermediates->line;
1023 err = at_tok_start(&line);
1024 if (err < 0) goto error;
1025 err = at_tok_nextstr(&line, &responseStr);
1026 if (err < 0 || !responseStr) goto error;
1027 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1028 at_response_free(p_response);
1031 at_response_free(p_response);
1032 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1035 static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1036 size_t datalen __unused, RIL_Token t)
1040 ATResponse *p_response = NULL;
1048 // Fixed values. TODO: query modem
1049 responseStr = strdup("1.0.0.0");
1050 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1054 static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1055 size_t datalen __unused, RIL_Token t)
1059 char * responseStr[4];
1060 ATResponse *p_response = NULL;
1068 // Fixed values. TODO: Query modem
1069 responseStr[0] = "----";
1070 responseStr[1] = "----";
1071 responseStr[2] = "77777777";
1073 err = at_send_command_numeric("AT+CGSN", &p_response);
1074 if (err < 0 || p_response->success == 0) {
1075 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1078 responseStr[3] = p_response->p_intermediates->line;
1081 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1082 at_response_free(p_response);
1085 static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1086 size_t datalen __unused, RIL_Token t)
1089 int *ss = (int *)data;
1090 ATResponse *p_response = NULL;
1095 asprintf(&cmd, "AT+CCSS?");
1096 if (!cmd) goto error;
1098 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1099 if (err < 0 || !p_response->success)
1102 line = p_response->p_intermediates->line;
1103 err = at_tok_start(&line);
1104 if (err < 0) goto error;
1106 err = at_tok_nextint(&line, &response);
1110 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1115 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1118 static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
1119 size_t datalen, RIL_Token t)
1122 int *ss = (int *)data;
1123 ATResponse *p_response = NULL;
1126 if (!ss || !datalen) {
1127 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
1128 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1131 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1132 if (!cmd) goto error;
1134 err = at_send_command(cmd, &p_response);
1135 if (err < 0 || !p_response->success)
1140 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1142 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1147 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1150 static void requestCdmaSubscription(int request __unused, void *data __unused,
1151 size_t datalen __unused, RIL_Token t)
1155 char * responseStr[5];
1156 ATResponse *p_response = NULL;
1164 // Fixed values. TODO: Query modem
1165 responseStr[0] = "8587777777"; // MDN
1166 responseStr[1] = "1"; // SID
1167 responseStr[2] = "1"; // NID
1168 responseStr[3] = "8587777777"; // MIN
1169 responseStr[4] = "1"; // PRL Version
1170 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1173 static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1174 size_t datalen __unused, RIL_Token t)
1176 int roaming_pref = -1;
1177 ATResponse *p_response = NULL;
1181 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1182 if (res < 0 || !p_response->success) {
1185 line = p_response->p_intermediates->line;
1187 res = at_tok_start(&line);
1188 if (res < 0) goto error;
1190 res = at_tok_nextint(&line, &roaming_pref);
1191 if (res < 0) goto error;
1193 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1196 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1199 static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1200 size_t datalen __unused, RIL_Token t)
1202 int *pref = (int *)data;
1203 ATResponse *p_response = NULL;
1208 asprintf(&cmd, "AT+WRMP=%d", *pref);
1209 if (cmd == NULL) goto error;
1211 res = at_send_command(cmd, &p_response);
1212 if (res < 0 || !p_response->success)
1215 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1220 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1223 static int parseRegistrationState(char *str, int *type, int *items, int **response)
1226 char *line = str, *p;
1232 RLOGD("parseRegistrationState. Parsing: %s",str);
1233 err = at_tok_start(&line);
1234 if (err < 0) goto error;
1236 /* Ok you have to be careful here
1237 * The solicited version of the CREG response is
1238 * +CREG: n, stat, [lac, cid]
1239 * and the unsolicited version is
1240 * +CREG: stat, [lac, cid]
1241 * The <n> parameter is basically "is unsolicited creg on?"
1242 * which it should always be
1244 * Now we should normally get the solicited version here,
1245 * but the unsolicited version could have snuck in
1246 * so we have to handle both
1248 * Also since the LAC and CID are only reported when registered,
1249 * we can have 1, 2, 3, or 4 arguments here
1251 * finally, a +CGREG: answer may have a fifth value that corresponds
1252 * to the network type, as in;
1254 * +CGREG: n, stat [,lac, cid [,networkType]]
1257 /* count number of commas */
1259 for (p = line ; *p != '\0' ;p++) {
1260 if (*p == ',') commas++;
1263 resp = (int *)calloc(commas + 1, sizeof(int));
1264 if (!resp) goto error;
1266 case 0: /* +CREG: <stat> */
1267 err = at_tok_nextint(&line, &resp[0]);
1268 if (err < 0) goto error;
1273 case 1: /* +CREG: <n>, <stat> */
1274 err = at_tok_nextint(&line, &skip);
1275 if (err < 0) goto error;
1276 err = at_tok_nextint(&line, &resp[0]);
1277 if (err < 0) goto error;
1280 if (err < 0) goto error;
1283 case 2: /* +CREG: <stat>, <lac>, <cid> */
1284 err = at_tok_nextint(&line, &resp[0]);
1285 if (err < 0) goto error;
1286 err = at_tok_nexthexint(&line, &resp[1]);
1287 if (err < 0) goto error;
1288 err = at_tok_nexthexint(&line, &resp[2]);
1289 if (err < 0) goto error;
1291 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1292 err = at_tok_nextint(&line, &skip);
1293 if (err < 0) goto error;
1294 err = at_tok_nextint(&line, &resp[0]);
1295 if (err < 0) goto error;
1296 err = at_tok_nexthexint(&line, &resp[1]);
1297 if (err < 0) goto error;
1298 err = at_tok_nexthexint(&line, &resp[2]);
1299 if (err < 0) goto error;
1301 /* special case for CGREG, there is a fourth parameter
1302 * that is the network type (unknown/gprs/edge/umts)
1304 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1305 err = at_tok_nextint(&line, &skip);
1306 if (err < 0) goto error;
1307 err = at_tok_nextint(&line, &resp[0]);
1308 if (err < 0) goto error;
1309 err = at_tok_nexthexint(&line, &resp[1]);
1310 if (err < 0) goto error;
1311 err = at_tok_nexthexint(&line, &resp[2]);
1312 if (err < 0) goto error;
1313 err = at_tok_nexthexint(&line, &resp[3]);
1314 if (err < 0) goto error;
1325 *items = commas + 1;
1327 *type = techFromModemType(TECH(sMdmInfo));
1334 #define REG_STATE_LEN 15
1335 #define REG_DATA_STATE_LEN 6
1336 static void requestRegistrationState(int request, void *data __unused,
1337 size_t datalen __unused, RIL_Token t)
1341 char **responseStr = NULL;
1342 ATResponse *p_response = NULL;
1346 int i = 0, j, numElements = 0;
1348 int type, startfrom;
1350 RLOGD("requestRegistrationState");
1351 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1354 numElements = REG_STATE_LEN;
1355 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1358 numElements = REG_DATA_STATE_LEN;
1364 err = at_send_command_singleline(cmd, prefix, &p_response);
1366 if (err != 0) goto error;
1368 line = p_response->p_intermediates->line;
1370 if (parseRegistrationState(line, &type, &count, ®istration)) goto error;
1372 responseStr = malloc(numElements * sizeof(char *));
1373 if (!responseStr) goto error;
1374 memset(responseStr, 0, numElements * sizeof(char *));
1376 * The first '4' bytes for both registration states remain the same.
1377 * But if the request is 'DATA_REGISTRATION_STATE',
1378 * the 5th and 6th byte(s) are optional.
1380 if (is3gpp2(type) == 1) {
1381 RLOGD("registration state type: 3GPP2");
1382 // TODO: Query modem
1384 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1385 asprintf(&responseStr[3], "8"); // EvDo revA
1386 asprintf(&responseStr[4], "1"); // BSID
1387 asprintf(&responseStr[5], "123"); // Latitude
1388 asprintf(&responseStr[6], "222"); // Longitude
1389 asprintf(&responseStr[7], "0"); // CSS Indicator
1390 asprintf(&responseStr[8], "4"); // SID
1391 asprintf(&responseStr[9], "65535"); // NID
1392 asprintf(&responseStr[10], "0"); // Roaming indicator
1393 asprintf(&responseStr[11], "1"); // System is in PRL
1394 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1395 asprintf(&responseStr[13], "0"); // Reason for denial
1396 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1397 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1398 asprintf(&responseStr[3], "8"); // Available data radio technology
1400 } else { // type == RADIO_TECH_3GPP
1401 RLOGD("registration state type: 3GPP");
1403 asprintf(&responseStr[1], "%x", registration[1]);
1404 asprintf(&responseStr[2], "%x", registration[2]);
1406 asprintf(&responseStr[3], "%d", registration[3]);
1408 asprintf(&responseStr[0], "%d", registration[0]);
1411 * Optional bytes for DATA_REGISTRATION_STATE request
1412 * 4th byte : Registration denial code
1413 * 5th byte : The max. number of simultaneous Data Calls
1415 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1416 // asprintf(&responseStr[4], "3");
1417 // asprintf(&responseStr[5], "1");
1420 for (j = startfrom; j < numElements; j++) {
1421 if (!responseStr[i]) goto error;
1424 registration = NULL;
1426 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1427 for (j = 0; j < numElements; j++ ) {
1428 free(responseStr[j]);
1429 responseStr[j] = NULL;
1433 at_response_free(p_response);
1438 for (j = 0; j < numElements; j++) {
1439 free(responseStr[j]);
1440 responseStr[j] = NULL;
1445 RLOGE("requestRegistrationState must never return an error when radio is on");
1446 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1447 at_response_free(p_response);
1450 static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
1458 memset(response, 0, sizeof(response));
1460 ATResponse *p_response = NULL;
1462 err = at_send_command_multiline(
1463 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1464 "+COPS:", &p_response);
1466 /* we expect 3 lines here:
1467 * +COPS: 0,0,"T - Mobile"
1469 * +COPS: 0,2,"310170"
1472 if (err != 0) goto error;
1474 for (i = 0, p_cur = p_response->p_intermediates
1476 ; p_cur = p_cur->p_next, i++
1478 char *line = p_cur->line;
1480 err = at_tok_start(&line);
1481 if (err < 0) goto error;
1483 err = at_tok_nextint(&line, &skip);
1484 if (err < 0) goto error;
1486 // If we're unregistered, we may just get
1487 // a "+COPS: 0" response
1488 if (!at_tok_hasmore(&line)) {
1493 err = at_tok_nextint(&line, &skip);
1494 if (err < 0) goto error;
1496 // a "+COPS: 0, n" response is also possible
1497 if (!at_tok_hasmore(&line)) {
1502 err = at_tok_nextstr(&line, &(response[i]));
1503 if (err < 0) goto error;
1504 // Simple assumption that mcc and mnc are 3 digits each
1505 if (strlen(response[i]) == 6) {
1506 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1507 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1513 /* expect 3 lines exactly */
1517 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1518 at_response_free(p_response);
1522 RLOGE("requestOperator must not return error when radio is on");
1523 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1524 at_response_free(p_response);
1527 static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1529 int err = 1; // Set to go to error:
1530 RIL_SMS_Response response;
1531 RIL_CDMA_SMS_Message* rcsm;
1533 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
1534 datalen, sizeof(RIL_CDMA_SMS_Message));
1536 // verify data content to test marshalling/unmarshalling:
1537 rcsm = (RIL_CDMA_SMS_Message*)data;
1538 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
1539 uServicecategory=%d, sAddress.digit_mode=%d, \
1540 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1541 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1542 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1543 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1545 if (err != 0) goto error;
1547 // Cdma Send SMS implementation will go here:
1548 // But it is not implemented yet.
1550 memset(&response, 0, sizeof(response));
1551 response.messageRef = 1;
1552 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1556 // Cdma Send SMS will always cause send retry error.
1557 response.messageRef = -1;
1558 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1561 static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1568 RIL_SMS_Response response;
1569 ATResponse *p_response = NULL;
1571 memset(&response, 0, sizeof(response));
1572 RLOGD("requestSendSMS datalen =%zu", datalen);
1574 if (s_ims_gsm_fail != 0) goto error;
1575 if (s_ims_gsm_retry != 0) goto error2;
1577 smsc = ((const char **)data)[0];
1578 pdu = ((const char **)data)[1];
1580 tpLayerLength = strlen(pdu)/2;
1582 // "NULL for default SMSC"
1587 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1588 asprintf(&cmd2, "%s%s", smsc, pdu);
1590 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1595 if (err != 0 || p_response->success == 0) goto error;
1597 /* FIXME fill in messageRef and ackPDU */
1598 response.messageRef = 1;
1599 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1600 at_response_free(p_response);
1604 response.messageRef = -2;
1605 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1606 at_response_free(p_response);
1609 // send retry error.
1610 response.messageRef = -1;
1611 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1612 at_response_free(p_response);
1616 static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1618 RIL_IMS_SMS_Message *p_args;
1619 RIL_SMS_Response response;
1621 memset(&response, 0, sizeof(response));
1623 RLOGD("requestImsSendSMS: datalen=%zu, "
1624 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1625 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1626 datalen, s_ims_registered, s_ims_services, s_ims_format,
1627 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1630 // figure out if this is gsm/cdma format
1631 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1632 p_args = (RIL_IMS_SMS_Message *)data;
1634 if (0 != s_ims_cause_perm_failure ) goto error;
1636 // want to fail over ims and this is first request over ims
1637 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1639 if (RADIO_TECH_3GPP == p_args->tech) {
1640 return requestSendSMS(p_args->message.gsmMessage,
1641 datalen - sizeof(RIL_RadioTechnologyFamily),
1643 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1644 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1645 datalen - sizeof(RIL_RadioTechnologyFamily),
1648 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1652 response.messageRef = -2;
1653 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1657 response.messageRef = -1;
1658 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1661 static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1663 ATResponse *p_response = NULL;
1670 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1671 if (data == NULL || datalen == 0 || datalen > 16) {
1672 ALOGE("Invalid data passed to requestSimOpenChannel");
1673 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1677 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1679 err = at_send_command_numeric(cmd, &p_response);
1680 if (err < 0 || p_response == NULL || p_response->success == 0) {
1681 ALOGE("Error %d opening logical channel: %d",
1682 err, p_response ? p_response->success : 0);
1683 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1684 at_response_free(p_response);
1688 // Ensure integer only by scanning for an extra char but expect one result
1689 line = p_response->p_intermediates->line;
1690 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1691 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1692 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1696 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1697 at_response_free(p_response);
1700 static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1702 ATResponse *p_response = NULL;
1707 if (data == NULL || datalen != sizeof(session_id)) {
1708 ALOGE("Invalid data passed to requestSimCloseChannel");
1709 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1712 session_id = ((int32_t *)data)[0];
1713 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1714 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1716 if (err < 0 || p_response == NULL || p_response->success == 0) {
1717 ALOGE("Error %d closing logical channel %d: %d",
1718 err, session_id, p_response ? p_response->success : 0);
1719 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1720 at_response_free(p_response);
1724 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1726 at_response_free(p_response);
1729 static void requestSimTransmitApduChannel(void *data,
1733 ATResponse *p_response = NULL;
1738 RIL_SIM_IO_Response sim_response;
1739 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1741 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1742 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1746 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
1747 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
1748 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1749 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1751 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1753 if (err < 0 || p_response == NULL || p_response->success == 0) {
1754 ALOGE("Error %d transmitting APDU: %d",
1755 err, p_response ? p_response->success : 0);
1756 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1757 at_response_free(p_response);
1761 line = p_response->p_intermediates->line;
1762 err = parseSimResponseLine(line, &sim_response);
1765 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1766 &sim_response, sizeof(sim_response));
1768 ALOGE("Error %d parsing SIM response line: %s", err, line);
1769 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1771 at_response_free(p_response);
1774 static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
1779 ATResponse *p_response = NULL;
1781 apn = ((const char **)data)[2];
1783 #ifdef USE_TI_COMMANDS
1784 // Config for multislot class 10 (probably default anyway eh?)
1785 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1788 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1789 #endif /* USE_TI_COMMANDS */
1794 ssize_t written, rlen;
1795 char status[32] = {0};
1797 const char *pdp_type;
1799 RLOGD("requesting data connection to APN '%s'", apn);
1801 fd = open ("/dev/qmi", O_RDWR);
1802 if (fd >= 0) { /* the device doesn't exist on the emulator */
1804 RLOGD("opened the qmi device\n");
1805 asprintf(&cmd, "up:%s", apn);
1810 written = write (fd, cmd + cur, len - cur);
1811 } while (written < 0 && errno == EINTR);
1814 RLOGE("### ERROR writing to /dev/qmi");
1822 // wait for interface to come online
1827 rlen = read(fd, status, 31);
1828 } while (rlen < 0 && errno == EINTR);
1831 RLOGE("### ERROR reading from /dev/qmi");
1835 status[rlen] = '\0';
1836 RLOGD("### status: %s", status);
1838 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1843 RLOGE("### Failed to get data connection up\n");
1847 qmistatus = system("netcfg rmnet0 dhcp");
1849 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1851 if (qmistatus < 0) goto error;
1855 if (datalen > 6 * sizeof(char *)) {
1856 pdp_type = ((const char **)data)[6];
1861 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
1862 //FIXME check for error here
1863 err = at_send_command(cmd, NULL);
1866 // Set required QoS params to default
1867 err = at_send_command("AT+CGQREQ=1", NULL);
1869 // Set minimum QoS params to default
1870 err = at_send_command("AT+CGQMIN=1", NULL);
1872 // packet-domain event reporting
1873 err = at_send_command("AT+CGEREP=1,0", NULL);
1875 // Hangup anything that's happening there now
1876 err = at_send_command("AT+CGACT=1,0", NULL);
1878 // Start data on PDP context 1
1879 err = at_send_command("ATD*99***1#", &p_response);
1881 if (err < 0 || p_response->success == 0) {
1886 requestOrSendDataCallList(&t);
1888 at_response_free(p_response);
1892 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1893 at_response_free(p_response);
1897 static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
1902 ackSuccess = ((int *)data)[0];
1904 if (ackSuccess == 1) {
1905 err = at_send_command("AT+CNMA=1", NULL);
1906 } else if (ackSuccess == 0) {
1907 err = at_send_command("AT+CNMA=2", NULL);
1909 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1913 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1915 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1919 static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
1921 ATResponse *p_response = NULL;
1922 RIL_SIM_IO_Response sr;
1925 RIL_SIM_IO_v6 *p_args;
1928 memset(&sr, 0, sizeof(sr));
1930 p_args = (RIL_SIM_IO_v6 *)data;
1932 /* FIXME handle pin2 */
1934 if (p_args->data == NULL) {
1935 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1936 p_args->command, p_args->fileid,
1937 p_args->p1, p_args->p2, p_args->p3);
1939 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1940 p_args->command, p_args->fileid,
1941 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1944 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1946 if (err < 0 || p_response->success == 0) {
1950 line = p_response->p_intermediates->line;
1952 err = parseSimResponseLine(line, &sr);
1957 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1958 at_response_free(p_response);
1963 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1964 at_response_free(p_response);
1969 static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1971 ATResponse *p_response = NULL;
1974 const char** strings = (const char**)data;;
1976 if ( datalen == sizeof(char*) ) {
1977 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1978 } else if ( datalen == 2*sizeof(char*) ) {
1979 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1983 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1986 if (err < 0 || p_response->success == 0) {
1988 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1990 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1992 at_response_free(p_response);
1996 static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
1998 const char *ussdRequest;
2000 ussdRequest = (char *)(data);
2003 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2009 static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
2012 ATResponse *p_response = NULL;
2014 err = at_send_command("AT+WSOS=0", &p_response);
2016 if (err < 0 || p_response->success == 0) {
2017 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2021 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2024 // TODO: Use all radio types
2025 static int techFromModemType(int mdmtype)
2028 switch (1 << mdmtype) {
2030 ret = RADIO_TECH_1xRTT;
2033 ret = RADIO_TECH_EVDO_A;
2036 ret = RADIO_TECH_GPRS;
2039 ret = RADIO_TECH_HSPA;
2042 ret = RADIO_TECH_LTE;
2048 static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
2050 uint64_t curTime = ril_nano_time();
2051 RIL_CellInfo ci[1] =
2056 RIL_TIMESTAMP_TYPE_MODEM,
2057 curTime - 1000, // Fake some time in the past
2059 { // RIL_CellInfoGsm gsm
2060 { // gsm.cellIdneityGsm
2066 { // gsm.signalStrengthGsm
2067 10, // signalStrength
2075 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2079 static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
2081 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2083 assert (datalen == sizeof(int));
2084 s_cell_info_rate_ms = ((int *)data)[0];
2086 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2089 static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2091 // TODO - hook this up with real query/info from radio.
2093 RIL_HardwareConfig hwCfg;
2095 RIL_UNUSED_PARM(data);
2096 RIL_UNUSED_PARM(datalen);
2100 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2103 static void requestGetTtyMode(void *data, size_t datalen, RIL_Token t)
2105 int ttyModeResponse;
2107 RIL_UNUSED_PARM(data);
2108 RIL_UNUSED_PARM(datalen);
2110 ttyModeResponse = (getSIMStatus() == SIM_READY) ? 1 // TTY Full
2113 RIL_onRequestComplete(t, RIL_E_SUCCESS, &ttyModeResponse, sizeof(ttyModeResponse));
2116 static void requestGetRadioCapability(void *data, size_t datalen, RIL_Token t)
2118 RIL_RadioCapability radioCapability;
2120 RIL_UNUSED_PARM(data);
2121 RIL_UNUSED_PARM(datalen);
2123 radioCapability.version = RIL_RADIO_CAPABILITY_VERSION;
2124 radioCapability.session = 0;
2125 radioCapability.phase = 0;
2126 radioCapability.rat = 0;
2127 radioCapability.logicalModemUuid[0] = '\0';
2128 radioCapability.status = RC_STATUS_SUCCESS;
2130 RIL_onRequestComplete(t, RIL_E_SUCCESS, &radioCapability, sizeof(radioCapability));
2133 static void requestGetMute(void *data, size_t datalen, RIL_Token t)
2137 RIL_UNUSED_PARM(data);
2138 RIL_UNUSED_PARM(datalen);
2140 muteResponse = 0; // Mute disabled
2142 RIL_onRequestComplete(t, RIL_E_SUCCESS, &muteResponse, sizeof(muteResponse));
2145 /*** Callback methods from the RIL library to us ***/
2148 * Call from RIL to us to make a RIL_REQUEST
2150 * Must be completed with a call to RIL_onRequestComplete()
2152 * RIL_onRequestComplete() may be called from any thread, before or after
2153 * this function returns.
2155 * Because onRequest function could be called from multiple different thread,
2156 * we must ensure that the underlying at_send_command_* function
2160 onRequest (int request, void *data, size_t datalen, RIL_Token t)
2162 ATResponse *p_response;
2165 RLOGD("onRequest: %s", requestToString(request));
2167 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2168 * when RADIO_STATE_UNAVAILABLE.
2170 if (sState == RADIO_STATE_UNAVAILABLE
2171 && request != RIL_REQUEST_GET_SIM_STATUS
2173 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2177 /* Ignore all non-power requests when RADIO_STATE_OFF
2178 * (except RIL_REQUEST_GET_SIM_STATUS)
2180 if (sState == RADIO_STATE_OFF) {
2182 case RIL_REQUEST_BASEBAND_VERSION:
2183 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2184 case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:
2185 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2186 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2187 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2188 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2189 case RIL_REQUEST_DEVICE_IDENTITY:
2190 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2191 case RIL_REQUEST_GET_ACTIVITY_INFO:
2192 case RIL_REQUEST_GET_CARRIER_RESTRICTIONS:
2193 case RIL_REQUEST_GET_CURRENT_CALLS:
2194 case RIL_REQUEST_GET_IMEI:
2195 case RIL_REQUEST_GET_MUTE:
2196 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2197 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2198 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2199 case RIL_REQUEST_GET_SIM_STATUS:
2200 case RIL_REQUEST_NV_RESET_CONFIG:
2201 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2202 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2203 case RIL_REQUEST_QUERY_TTY_MODE:
2204 case RIL_REQUEST_RADIO_POWER:
2205 case RIL_REQUEST_SET_BAND_MODE:
2206 case RIL_REQUEST_SET_CARRIER_RESTRICTIONS:
2207 case RIL_REQUEST_SET_LOCATION_UPDATES:
2208 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2209 case RIL_REQUEST_SET_TTY_MODE:
2210 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2211 case RIL_REQUEST_STOP_LCE:
2212 case RIL_REQUEST_VOICE_RADIO_TECH:
2213 // Process all the above, even though the radio is off
2217 // For all others, say NOT_AVAILABLE because the radio is off
2218 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2224 case RIL_REQUEST_GET_SIM_STATUS: {
2225 RIL_CardStatus_v6 *p_card_status;
2229 int result = getCardStatus(&p_card_status);
2230 if (result == RIL_E_SUCCESS) {
2231 p_buffer = (char *)p_card_status;
2232 buffer_size = sizeof(*p_card_status);
2237 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2238 freeCardStatus(p_card_status);
2241 case RIL_REQUEST_GET_CURRENT_CALLS:
2242 requestGetCurrentCalls(data, datalen, t);
2244 case RIL_REQUEST_DIAL:
2245 requestDial(data, datalen, t);
2247 case RIL_REQUEST_HANGUP:
2248 requestHangup(data, datalen, t);
2250 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
2251 // 3GPP 22.030 6.5.5
2252 // "Releases all held calls or sets User Determined User Busy
2253 // (UDUB) for a waiting call."
2254 at_send_command("AT+CHLD=0", NULL);
2256 /* success or failure is ignored by the upper layer here.
2257 it will call GET_CURRENT_CALLS and determine success that way */
2258 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2260 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
2261 // 3GPP 22.030 6.5.5
2262 // "Releases all active calls (if any exist) and accepts
2263 // the other (held or waiting) call."
2264 at_send_command("AT+CHLD=1", NULL);
2266 /* success or failure is ignored by the upper layer here.
2267 it will call GET_CURRENT_CALLS and determine success that way */
2268 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2270 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
2271 // 3GPP 22.030 6.5.5
2272 // "Places all active calls (if any exist) on hold and accepts
2273 // the other (held or waiting) call."
2274 at_send_command("AT+CHLD=2", NULL);
2276 #ifdef WORKAROUND_ERRONEOUS_ANSWER
2278 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
2280 /* success or failure is ignored by the upper layer here.
2281 it will call GET_CURRENT_CALLS and determine success that way */
2282 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2284 case RIL_REQUEST_ANSWER:
2285 at_send_command("ATA", NULL);
2287 #ifdef WORKAROUND_ERRONEOUS_ANSWER
2289 #endif /* WORKAROUND_ERRONEOUS_ANSWER */
2291 /* success or failure is ignored by the upper layer here.
2292 it will call GET_CURRENT_CALLS and determine success that way */
2293 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2295 case RIL_REQUEST_CONFERENCE:
2296 // 3GPP 22.030 6.5.5
2297 // "Adds a held call to the conversation"
2298 at_send_command("AT+CHLD=3", NULL);
2300 /* success or failure is ignored by the upper layer here.
2301 it will call GET_CURRENT_CALLS and determine success that way */
2302 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2304 case RIL_REQUEST_UDUB:
2305 /* user determined user busy */
2306 /* sometimes used: ATH */
2307 at_send_command("ATH", NULL);
2309 /* success or failure is ignored by the upper layer here.
2310 it will call GET_CURRENT_CALLS and determine success that way */
2311 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2314 case RIL_REQUEST_SEPARATE_CONNECTION:
2317 int party = ((int*)data)[0];
2319 // Make sure that party is in a valid range.
2320 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2321 // It's sufficient for us to just make sure it's single digit.)
2322 if (party > 0 && party < 10) {
2323 sprintf(cmd, "AT+CHLD=2%d", party);
2324 at_send_command(cmd, NULL);
2325 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2327 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2332 case RIL_REQUEST_SIGNAL_STRENGTH:
2333 requestSignalStrength(data, datalen, t);
2335 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2336 case RIL_REQUEST_DATA_REGISTRATION_STATE:
2337 requestRegistrationState(request, data, datalen, t);
2339 case RIL_REQUEST_OPERATOR:
2340 requestOperator(data, datalen, t);
2342 case RIL_REQUEST_RADIO_POWER:
2343 requestRadioPower(data, datalen, t);
2345 case RIL_REQUEST_DTMF: {
2346 char c = ((char *)data)[0];
2348 asprintf(&cmd, "AT+VTS=%c", (int)c);
2349 at_send_command(cmd, NULL);
2351 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2354 case RIL_REQUEST_SEND_SMS:
2355 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
2356 requestSendSMS(data, datalen, t);
2358 case RIL_REQUEST_CDMA_SEND_SMS:
2359 requestCdmaSendSMS(data, datalen, t);
2361 case RIL_REQUEST_IMS_SEND_SMS:
2362 requestImsSendSMS(data, datalen, t);
2364 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2365 requestSimOpenChannel(data, datalen, t);
2367 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2368 requestSimCloseChannel(data, datalen, t);
2370 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2371 requestSimTransmitApduChannel(data, datalen, t);
2373 case RIL_REQUEST_SETUP_DATA_CALL:
2374 requestSetupDataCall(data, datalen, t);
2376 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2377 requestSMSAcknowledge(data, datalen, t);
2380 case RIL_REQUEST_GET_IMSI:
2382 err = at_send_command_numeric("AT+CIMI", &p_response);
2384 if (err < 0 || p_response->success == 0) {
2385 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2387 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2388 p_response->p_intermediates->line, sizeof(char *));
2390 at_response_free(p_response);
2393 case RIL_REQUEST_GET_IMEI:
2395 err = at_send_command_numeric("AT+CGSN", &p_response);
2397 if (err < 0 || p_response->success == 0) {
2398 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2400 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2401 p_response->p_intermediates->line, sizeof(char *));
2403 at_response_free(p_response);
2406 case RIL_REQUEST_SIM_IO:
2407 requestSIM_IO(data,datalen,t);
2410 case RIL_REQUEST_SEND_USSD:
2411 requestSendUSSD(data, datalen, t);
2414 case RIL_REQUEST_CANCEL_USSD:
2416 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2418 if (err < 0 || p_response->success == 0) {
2419 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2421 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2422 p_response->p_intermediates->line, sizeof(char *));
2424 at_response_free(p_response);
2427 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2428 at_send_command("AT+COPS=0", NULL);
2431 case RIL_REQUEST_DATA_CALL_LIST:
2432 requestDataCallList(data, datalen, t);
2435 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2436 requestQueryNetworkSelectionMode(data, datalen, t);
2439 case RIL_REQUEST_OEM_HOOK_RAW:
2441 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2445 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2449 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
2452 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2453 i > 0 ; cur++, i --) {
2454 RLOGD("> '%s'", *cur);
2457 // echo back strings
2458 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2462 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2463 requestWriteSmsToSim(data, datalen, t);
2466 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2469 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2470 err = at_send_command(cmd, &p_response);
2472 if (err < 0 || p_response->success == 0) {
2473 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2475 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2477 at_response_free(p_response);
2481 case RIL_REQUEST_ENTER_SIM_PIN:
2482 case RIL_REQUEST_ENTER_SIM_PUK:
2483 case RIL_REQUEST_ENTER_SIM_PIN2:
2484 case RIL_REQUEST_ENTER_SIM_PUK2:
2485 case RIL_REQUEST_CHANGE_SIM_PIN:
2486 case RIL_REQUEST_CHANGE_SIM_PIN2:
2487 requestEnterSimPin(data, datalen, t);
2490 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2492 //0==unregistered, 1==registered
2493 reply[0] = s_ims_registered;
2495 //to be used when changed to include service supporated info
2496 //reply[1] = s_ims_services;
2498 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2499 reply[1] = s_ims_format;
2501 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2502 reply[0], reply[1]);
2503 if (reply[1] != -1) {
2504 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2506 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2511 case RIL_REQUEST_VOICE_RADIO_TECH:
2513 int tech = techFromModemType(TECH(sMdmInfo));
2515 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2517 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2520 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2521 requestSetPreferredNetworkType(request, data, datalen, t);
2524 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2525 requestGetPreferredNetworkType(request, data, datalen, t);
2528 case RIL_REQUEST_GET_CELL_INFO_LIST:
2529 requestGetCellInfoList(data, datalen, t);
2532 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2533 requestSetCellInfoListRate(data, datalen, t);
2536 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2537 requestGetHardwareConfig(data, datalen, t);
2540 case RIL_REQUEST_SHUTDOWN:
2544 case RIL_REQUEST_QUERY_TTY_MODE:
2545 requestGetTtyMode(data, datalen, t);
2548 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2549 requestGetRadioCapability(data, datalen, t);
2552 case RIL_REQUEST_GET_MUTE:
2553 requestGetMute(data, datalen, t);
2556 case RIL_REQUEST_SET_INITIAL_ATTACH_APN:
2557 case RIL_REQUEST_ALLOW_DATA:
2558 case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION:
2559 case RIL_REQUEST_SET_CLIR:
2560 case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION:
2561 case RIL_REQUEST_SET_BAND_MODE:
2562 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2563 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2564 case RIL_REQUEST_SET_LOCATION_UPDATES:
2565 case RIL_REQUEST_SET_TTY_MODE:
2566 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2567 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2570 case RIL_REQUEST_BASEBAND_VERSION:
2571 requestCdmaBaseBandVersion(request, data, datalen, t);
2574 case RIL_REQUEST_DEVICE_IDENTITY:
2575 requestCdmaDeviceIdentity(request, data, datalen, t);
2578 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2579 requestCdmaSubscription(request, data, datalen, t);
2582 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2583 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2586 /* CDMA Specific Requests */
2587 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2588 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2589 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2591 } // Fall-through if tech is not cdma
2593 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2594 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2595 requestCdmaGetRoamingPreference(request, data, datalen, t);
2597 } // Fall-through if tech is not cdma
2599 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2600 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2601 requestCdmaSetRoamingPreference(request, data, datalen, t);
2603 } // Fall-through if tech is not cdma
2605 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2606 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2607 requestExitEmergencyMode(data, datalen, t);
2609 } // Fall-through if tech is not cdma
2611 // Fall-through to here when the request is specific to CDMA, but
2612 // our tech is not CDMA. VTS tests expect us to silently do nothing.
2613 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2617 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
2618 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2624 * Synchronous call from the RIL to us to return current radio state.
2625 * RADIO_STATE_UNAVAILABLE should be the initial state.
2627 static RIL_RadioState
2633 * Call from RIL to us to find out whether a specific request code
2634 * is supported by this implementation.
2636 * Return 1 for "supported" and 0 for "unsupported"
2640 onSupports (int requestCode __unused)
2647 static void onCancel (RIL_Token t __unused)
2653 static const char * getVersion(void)
2655 return "android reference-ril 1.0";
2659 setRadioTechnology(ModemInfo *mdm, int newtech)
2661 RLOGD("setRadioTechnology(%d)", newtech);
2663 int oldtech = TECH(mdm);
2665 if (newtech != oldtech) {
2666 RLOGD("Tech change (%d => %d)", oldtech, newtech);
2667 TECH(mdm) = newtech;
2668 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2669 int tech = techFromModemType(TECH(sMdmInfo));
2671 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2672 &tech, sizeof(tech));
2679 setRadioState(RIL_RadioState newState)
2681 RLOGD("setRadioState(%d)", newState);
2682 RIL_RadioState oldState;
2684 pthread_mutex_lock(&s_state_mutex);
2689 // If we're closed, the only reasonable state is
2690 // RADIO_STATE_UNAVAILABLE
2691 // This is here because things on the main thread
2692 // may attempt to change the radio state after the closed
2693 // event happened in another thread
2694 newState = RADIO_STATE_UNAVAILABLE;
2697 if (sState != newState || s_closed > 0) {
2700 pthread_cond_broadcast (&s_state_cond);
2703 pthread_mutex_unlock(&s_state_mutex);
2706 /* do these outside of the mutex */
2707 if (sState != oldState) {
2708 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2710 // Sim state can change as result of radio state change
2711 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2714 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2715 * from the AT reader thread
2716 * Currently, this doesn't happen, but if that changes then these
2717 * will need to be dispatched on the request thread
2719 if (sState == RADIO_STATE_ON) {
2725 /** Returns RUIM_NOT_READY on error */
2729 ATResponse *p_response = NULL;
2735 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2736 ret = SIM_NOT_READY;
2740 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2743 ret = SIM_NOT_READY;
2747 switch (at_get_cme_error(p_response)) {
2751 case CME_SIM_NOT_INSERTED:
2756 ret = SIM_NOT_READY;
2760 /* CPIN? has succeeded, now look at the result */
2762 cpinLine = p_response->p_intermediates->line;
2763 err = at_tok_start (&cpinLine);
2766 ret = SIM_NOT_READY;
2770 err = at_tok_nextstr(&cpinLine, &cpinResult);
2773 ret = SIM_NOT_READY;
2777 if (0 == strcmp (cpinResult, "SIM PIN")) {
2780 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2783 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2784 return SIM_NETWORK_PERSONALIZATION;
2785 } else if (0 != strcmp (cpinResult, "READY")) {
2786 /* we're treating unsupported lock types as "sim absent" */
2791 at_response_free(p_response);
2798 at_response_free(p_response);
2802 /** Returns SIM_NOT_READY on error */
2806 ATResponse *p_response = NULL;
2812 RLOGD("getSIMStatus(). sState: %d",sState);
2813 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2816 ret = SIM_NOT_READY;
2820 switch (at_get_cme_error(p_response)) {
2824 case CME_SIM_NOT_INSERTED:
2829 ret = SIM_NOT_READY;
2833 /* CPIN? has succeeded, now look at the result */
2835 cpinLine = p_response->p_intermediates->line;
2836 err = at_tok_start (&cpinLine);
2839 ret = SIM_NOT_READY;
2843 err = at_tok_nextstr(&cpinLine, &cpinResult);
2846 ret = SIM_NOT_READY;
2850 if (0 == strcmp (cpinResult, "SIM PIN")) {
2853 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2856 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2857 return SIM_NETWORK_PERSONALIZATION;
2858 } else if (0 != strcmp (cpinResult, "READY")) {
2859 /* we're treating unsupported lock types as "sim absent" */
2864 at_response_free(p_response);
2868 ret = (sState == RADIO_STATE_ON) ? SIM_READY : SIM_NOT_READY;
2871 at_response_free(p_response);
2877 * Get the current card status.
2879 * This must be freed using freeCardStatus.
2880 * @return: On success returns RIL_E_SUCCESS
2882 static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
2883 static RIL_AppStatus app_status_array[] = {
2885 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2886 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2887 // SIM_NOT_READY = 1
2888 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2889 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2891 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2892 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2894 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2895 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2897 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2898 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2899 // SIM_NETWORK_PERSONALIZATION = 5
2900 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2901 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2903 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2904 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2905 // RUIM_NOT_READY = 7
2906 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2907 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2909 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2910 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2912 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2913 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2915 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2916 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2917 // RUIM_NETWORK_PERSONALIZATION = 11
2918 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2919 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
2921 RIL_CardState card_state;
2924 int sim_status = getSIMStatus();
2925 if (sim_status == SIM_ABSENT) {
2926 card_state = RIL_CARDSTATE_ABSENT;
2929 card_state = RIL_CARDSTATE_PRESENT;
2933 // Allocate and initialize base card status.
2934 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
2935 p_card_status->card_state = card_state;
2936 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
2937 p_card_status->gsm_umts_subscription_app_index = -1;
2938 p_card_status->cdma_subscription_app_index = -1;
2939 p_card_status->ims_subscription_app_index = -1;
2940 p_card_status->num_applications = num_apps;
2942 // Initialize application status
2944 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
2945 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
2948 // Pickup the appropriate application status
2949 // that reflects sim_status for gsm.
2950 if (num_apps != 0) {
2951 // Only support one app, gsm
2952 p_card_status->num_applications = 2;
2953 p_card_status->gsm_umts_subscription_app_index = 0;
2954 p_card_status->cdma_subscription_app_index = 1;
2956 // Get the correct app status
2957 p_card_status->applications[0] = app_status_array[sim_status];
2958 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
2961 *pp_card_status = p_card_status;
2962 return RIL_E_SUCCESS;
2966 * Free the card status returned by getCardStatus
2968 static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
2969 free(p_card_status);
2973 * SIM ready means any commands that access the SIM will work, including:
2974 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
2975 * (all SMS-related commands)
2978 static void pollSIMState (void *param __unused)
2980 ATResponse *p_response;
2983 if (sState != RADIO_STATE_UNAVAILABLE) {
2984 // no longer valid to poll
2988 switch(getSIMStatus()) {
2992 case SIM_NETWORK_PERSONALIZATION:
2994 RLOGI("SIM ABSENT or LOCKED");
2995 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
2999 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
3005 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
3010 /** returns 1 if on, 0 if off, and -1 on error */
3011 static int isRadioOn()
3013 ATResponse *p_response = NULL;
3018 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
3020 if (err < 0 || p_response->success == 0) {
3021 // assume radio is off
3025 line = p_response->p_intermediates->line;
3027 err = at_tok_start(&line);
3028 if (err < 0) goto error;
3030 err = at_tok_nextbool(&line, &ret);
3031 if (err < 0) goto error;
3033 at_response_free(p_response);
3039 at_response_free(p_response);
3044 * Parse the response generated by a +CTEC AT command
3045 * The values read from the response are stored in current and preferred.
3046 * Both current and preferred may be null. The corresponding value is ignored in that case.
3048 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
3049 * 1 if the response includes the current technology only
3050 * 0 if the response includes both current technology and preferred mode
3052 int parse_technology_response( const char *response, int *current, int32_t *preferred )
3060 line = p = strdup(response);
3061 RLOGD("Response: %s", line);
3062 err = at_tok_start(&p);
3063 if (err || !at_tok_hasmore(&p)) {
3064 RLOGD("err: %d. p: %s", err, p);
3069 err = at_tok_nextint(&p, &ct);
3074 if (current) *current = ct;
3076 RLOGD("line remaining after int: %s", p);
3078 err = at_tok_nexthexint(&p, &pt);
3091 int query_supported_techs( ModemInfo *mdm __unused, int *supported )
3093 ATResponse *p_response;
3094 int err, val, techs = 0;
3098 RLOGD("query_supported_techs");
3099 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3100 if (err || !p_response->success)
3102 line = p_response->p_intermediates->line;
3103 err = at_tok_start(&line);
3104 if (err || !at_tok_hasmore(&line))
3106 while (!at_tok_nextint(&line, &val)) {
3107 techs |= ( 1 << val );
3109 if (supported) *supported = techs;
3112 at_response_free(p_response);
3117 * query_ctec. Send the +CTEC AT command to the modem to query the current
3118 * and preferred modes. It leaves values in the addresses pointed to by
3119 * current and preferred. If any of those pointers are NULL, the corresponding value
3120 * is ignored, but the return value will still reflect if retreiving and parsing of the
3123 * @mdm Currently unused
3124 * @current A pointer to store the current mode returned by the modem. May be null.
3125 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3126 * @return -1 on error (or failure to parse)
3127 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3128 * 0 if both current and preferred were returned correctly
3130 int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
3132 ATResponse *response = NULL;
3136 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
3137 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3138 if (!err && response->success) {
3139 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3140 at_response_free(response);
3143 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
3144 at_response_free(response);
3148 int is_multimode_modem(ModemInfo *mdm)
3150 ATResponse *response;
3156 if (query_ctec(mdm, &tech, &preferred) == 0) {
3157 mdm->currentTech = tech;
3158 mdm->preferredNetworkMode = preferred;
3159 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3168 * Find out if our modem is GSM, CDMA or both (Multimode)
3170 static void probeForModemMode(ModemInfo *info)
3172 ATResponse *response;
3175 // Currently, our only known multimode modem is qemu's android modem,
3176 // which implements the AT+CTEC command to query and set mode.
3179 if (is_multimode_modem(info)) {
3180 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
3181 info->supportedTechs, info->currentTech);
3185 /* Being here means that our modem is not multimode */
3186 info->isMultimode = 0;
3188 /* CDMA Modems implement the AT+WNAM command */
3189 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3190 if (!err && response->success) {
3191 at_response_free(response);
3192 // TODO: find out if we really support EvDo
3193 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3194 info->currentTech = MDM_CDMA;
3195 RLOGI("Found CDMA Modem");
3198 if (!err) at_response_free(response);
3199 // TODO: find out if modem really supports WCDMA/LTE
3200 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3201 info->currentTech = MDM_GSM;
3202 RLOGI("Found GSM Modem");
3206 * Initialize everything that can be configured while we're still in
3209 static void initializeCallback(void *param __unused)
3211 ATResponse *p_response = NULL;
3214 setRadioState (RADIO_STATE_OFF);
3218 probeForModemMode(sMdmInfo);
3219 /* note: we don't check errors here. Everything important will
3220 be handled in onATTimeout and onATReaderClosed */
3222 /* atchannel is tolerant of echo but it must */
3223 /* have verbose result codes */
3224 at_send_command("ATE0Q0V1", NULL);
3226 /* No auto-answer */
3227 at_send_command("ATS0=0", NULL);
3229 /* Extended errors */
3230 at_send_command("AT+CMEE=1", NULL);
3232 /* Network registration events */
3233 err = at_send_command("AT+CREG=2", &p_response);
3235 /* some handsets -- in tethered mode -- don't support CREG=2 */
3236 if (err < 0 || p_response->success == 0) {
3237 at_send_command("AT+CREG=1", NULL);
3240 at_response_free(p_response);
3242 /* GPRS registration events */
3243 at_send_command("AT+CGREG=1", NULL);
3245 /* Call Waiting notifications */
3246 at_send_command("AT+CCWA=1", NULL);
3248 /* Alternating voice/data off */
3249 at_send_command("AT+CMOD=0", NULL);
3252 at_send_command("AT+CMUT=0", NULL);
3254 /* +CSSU unsolicited supp service notifications */
3255 at_send_command("AT+CSSN=0,1", NULL);
3257 /* no connected line identification */
3258 at_send_command("AT+COLP=0", NULL);
3260 /* HEX character set */
3261 at_send_command("AT+CSCS=\"HEX\"", NULL);
3263 /* USSD unsolicited */
3264 at_send_command("AT+CUSD=1", NULL);
3266 /* Enable +CGEV GPRS event notifications, but don't buffer */
3267 at_send_command("AT+CGEREP=1,0", NULL);
3270 at_send_command("AT+CMGF=0", NULL);
3272 #ifdef USE_TI_COMMANDS
3274 at_send_command("AT%CPI=3", NULL);
3276 /* TI specific -- notifications when SMS is ready (currently ignored) */
3277 at_send_command("AT%CSTAT=1", NULL);
3279 #endif /* USE_TI_COMMANDS */
3282 /* assume radio is off on error */
3283 if (isRadioOn() > 0) {
3284 setRadioState (RADIO_STATE_ON);
3288 static void waitForClose()
3290 pthread_mutex_lock(&s_state_mutex);
3292 while (s_closed == 0) {
3293 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3296 pthread_mutex_unlock(&s_state_mutex);
3299 static void sendUnsolImsNetworkStateChanged()
3301 #if 0 // to be used when unsol is changed to return data.
3303 reply[0] = s_ims_registered;
3304 reply[1] = s_ims_services;
3305 reply[1] = s_ims_format;
3307 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3312 * Called by atchannel when an unsolicited line appears
3313 * This is called on atchannel's reader thread. AT commands may
3314 * not be issued here
3316 static void onUnsolicited (const char *s, const char *sms_pdu)
3318 char *line = NULL, *p;
3321 /* Ignore unsolicited responses until we're initialized.
3322 * This is OK because the RIL library will poll for initial state
3324 if (sState == RADIO_STATE_UNAVAILABLE) {
3328 if (strStartsWith(s, "%CTZV:")) {
3329 /* TI specific -- NITZ time */
3332 line = p = strdup(s);
3335 err = at_tok_nextstr(&p, &response);
3338 RLOGE("invalid NITZ line %s\n", s);
3340 RIL_onUnsolicitedResponse (
3341 RIL_UNSOL_NITZ_TIME_RECEIVED,
3342 response, strlen(response));
3345 } else if (strStartsWith(s,"+CRING:")
3346 || strStartsWith(s,"RING")
3347 || strStartsWith(s,"NO CARRIER")
3348 || strStartsWith(s,"+CCWA")
3350 RIL_onUnsolicitedResponse (
3351 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3353 #ifdef WORKAROUND_FAKE_CGEV
3354 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
3355 #endif /* WORKAROUND_FAKE_CGEV */
3356 } else if (strStartsWith(s,"+CREG:")
3357 || strStartsWith(s,"+CGREG:")
3359 RIL_onUnsolicitedResponse (
3360 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
3362 #ifdef WORKAROUND_FAKE_CGEV
3363 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
3364 #endif /* WORKAROUND_FAKE_CGEV */
3365 } else if (strStartsWith(s, "+CMT:")) {
3366 RIL_onUnsolicitedResponse (
3367 RIL_UNSOL_RESPONSE_NEW_SMS,
3368 sms_pdu, strlen(sms_pdu));
3369 } else if (strStartsWith(s, "+CDS:")) {
3370 RIL_onUnsolicitedResponse (
3371 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3372 sms_pdu, strlen(sms_pdu));
3373 } else if (strStartsWith(s, "+CGEV:")) {
3374 /* Really, we can ignore NW CLASS and ME CLASS events here,
3375 * but right now we don't since extranous
3376 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
3378 /* can't issue AT commands here -- call on main thread */
3379 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
3380 #ifdef WORKAROUND_FAKE_CGEV
3381 } else if (strStartsWith(s, "+CME ERROR: 150")) {
3382 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
3383 #endif /* WORKAROUND_FAKE_CGEV */
3384 } else if (strStartsWith(s, "+CTEC: ")) {
3386 switch (parse_technology_response(s, &tech, NULL))
3388 case -1: // no argument could be parsed.
3389 RLOGE("invalid CTEC line %s\n", s);
3391 case 1: // current mode correctly parsed
3392 case 0: // preferred mode correctly parsed
3394 if (mask != MDM_GSM && mask != MDM_CDMA &&
3395 mask != MDM_WCDMA && mask != MDM_LTE) {
3396 RLOGE("Unknown technology %d\n", tech);
3398 setRadioTechnology(sMdmInfo, tech);
3402 } else if (strStartsWith(s, "+CCSS: ")) {
3404 line = p = strdup(s);
3406 RLOGE("+CCSS: Unable to allocate memory");
3409 if (at_tok_start(&p) < 0) {
3413 if (at_tok_nextint(&p, &source) < 0) {
3414 RLOGE("invalid +CCSS response: %s", line);
3418 SSOURCE(sMdmInfo) = source;
3419 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3420 &source, sizeof(source));
3421 } else if (strStartsWith(s, "+WSOS: ")) {
3424 line = p = strdup(s);
3426 RLOGE("+WSOS: Unable to allocate memory");
3429 if (at_tok_start(&p) < 0) {
3433 if (at_tok_nextbool(&p, &state) < 0) {
3434 RLOGE("invalid +WSOS response: %s", line);
3441 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3443 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3445 } else if (strStartsWith(s, "+WPRL: ")) {
3447 line = p = strdup(s);
3449 RLOGE("+WPRL: Unable to allocate memory");
3452 if (at_tok_start(&p) < 0) {
3453 RLOGE("invalid +WPRL response: %s", s);
3457 if (at_tok_nextint(&p, &version) < 0) {
3458 RLOGE("invalid +WPRL response: %s", s);
3463 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3464 } else if (strStartsWith(s, "+CFUN: 0")) {
3465 setRadioState(RADIO_STATE_OFF);
3469 /* Called on command or reader thread */
3470 static void onATReaderClosed()
3472 RLOGI("AT channel closed\n");
3476 setRadioState (RADIO_STATE_UNAVAILABLE);
3479 /* Called on command thread */
3480 static void onATTimeout()
3482 RLOGI("AT channel timeout; closing\n");
3487 /* FIXME cause a radio reset here */
3489 setRadioState (RADIO_STATE_UNAVAILABLE);
3492 /* Called to pass hardware configuration information to telephony
3495 static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3497 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3500 static void usage(char *s __unused)
3503 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3505 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3511 mainLoop(void *param __unused)
3516 AT_DUMP("== ", "entering mainLoop()", -1 );
3517 at_set_on_reader_closed(onATReaderClosed);
3518 at_set_on_timeout(onATTimeout);
3523 if (isInEmulator()) {
3524 fd = qemu_pipe_open("pipe:qemud:gsm");
3525 } else if (s_port > 0) {
3526 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
3527 } else if (s_device_socket) {
3528 fd = socket_local_client(s_device_path,
3529 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3531 } else if (s_device_path != NULL) {
3532 fd = open (s_device_path, O_RDWR);
3533 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3534 /* disable echo on serial ports */
3536 tcgetattr( fd, &ios );
3537 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3538 tcsetattr( fd, TCSANOW, &ios );
3543 perror ("opening AT interface. retrying...");
3550 ret = at_open(fd, onUnsolicited);
3553 RLOGE ("AT error %d on at_open\n", ret);
3557 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3559 // Give initializeCallback a chance to dispatched, since
3560 // we don't presently have a cancellation mechanism
3564 RLOGI("Re-opening after close");
3570 pthread_t s_tid_mainloop;
3572 const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3577 pthread_attr_t attr;
3581 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
3584 s_port = atoi(optarg);
3589 RLOGI("Opening loopback port %d\n", s_port);
3593 s_device_path = optarg;
3594 RLOGI("Opening tty device %s\n", s_device_path);
3598 s_device_path = optarg;
3599 s_device_socket = 1;
3600 RLOGI("Opening socket %s\n", s_device_path);
3604 RLOGI("Client id received %s\n", optarg);
3613 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
3618 sMdmInfo = calloc(1, sizeof(ModemInfo));
3620 RLOGE("Unable to alloc memory for ModemInfo");
3623 pthread_attr_init (&attr);
3624 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3625 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3627 return &s_callbacks;
3629 #else /* RIL_SHLIB */
3630 int main (int argc, char **argv)
3636 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3639 s_port = atoi(optarg);
3643 RLOGI("Opening loopback port %d\n", s_port);
3647 s_device_path = optarg;
3648 RLOGI("Opening tty device %s\n", s_device_path);
3652 s_device_path = optarg;
3653 s_device_socket = 1;
3654 RLOGI("Opening socket %s\n", s_device_path);
3662 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
3666 RIL_register(&s_callbacks);
3673 #endif /* RIL_SHLIB */