blob: b04db97a58dc14ad6c8d2c4f1cc2e13010c45762 [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //device/system/reference-ril/reference-ril.c
2**
3** Copyright 2006, The Android Open Source Project
4**
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
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
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.
16*/
17
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070018#include <telephony/ril_cdma_sms.h>
Wink Saville8a9e0212013-04-09 12:11:38 -070019#include <telephony/librilutils.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080020#include <stdio.h>
21#include <assert.h>
22#include <string.h>
23#include <errno.h>
24#include <unistd.h>
Mark Salyzynba58c202014-03-12 15:20:22 -070025#include <sys/cdefs.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080026#include <sys/types.h>
27#include <sys/stat.h>
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -070028#include <inttypes.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080029#include <fcntl.h>
30#include <pthread.h>
31#include <alloca.h>
32#include "atchannel.h"
33#include "at_tok.h"
34#include "misc.h"
35#include <getopt.h>
36#include <sys/socket.h>
Bjoern Johanssonbc497e72017-07-27 15:41:08 -070037#include <cutils/properties.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080038#include <cutils/sockets.h>
Weilun Du9f471e22017-02-07 10:47:19 -080039#include <termios.h>
bohuba723ce2017-03-29 12:20:46 -070040#include <qemu_pipe.h>
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -070041#include <sys/wait.h>
42#include <stdbool.h>
43#include <net/if.h>
44#include <netinet/in.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080045
Wink Saville9a9fbd22011-02-15 17:13:10 -080046#include "ril.h"
47
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080048#define LOG_TAG "RIL"
49#include <utils/Log.h>
50
Etan Cohend3652192014-06-20 08:28:44 -070051static void *noopRemoveWarning( void *a ) { return a; }
52#define RIL_UNUSED_PARM(a) noopRemoveWarning((void *)&(a));
53
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080054#define MAX_AT_RESPONSE 0x1000
55
Wink Savillef4c4d362009-04-02 01:37:03 -070056/* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
Bjoern Johanssona8bec892017-02-09 22:55:02 -080057// This is used if Wifi is not supported, plain old eth0
58#define PPP_TTY_PATH_ETH0 "eth0"
59// This is used if Wifi is supported to separate radio and wifi interface
60#define PPP_TTY_PATH_RADIO0 "radio0"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080061
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -070062// Default MTU value
63#define DEFAULT_MTU 1500
64
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080065#ifdef USE_TI_COMMANDS
66
67// Enable a workaround
68// 1) Make incoming call, do not answer
69// 2) Hangup remote end
70// Expected: call should disappear from CLCC line
71// Actual: Call shows as "ACTIVE" before disappearing
72#define WORKAROUND_ERRONEOUS_ANSWER 1
73
74// Some varients of the TI stack do not support the +CGEV unsolicited
75// response. However, they seem to send an unsolicited +CME ERROR: 150
76#define WORKAROUND_FAKE_CGEV 1
77#endif
78
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070079/* Modem Technology bits */
80#define MDM_GSM 0x01
81#define MDM_WCDMA 0x02
82#define MDM_CDMA 0x04
83#define MDM_EVDO 0x08
84#define MDM_LTE 0x10
85
86typedef struct {
87 int supportedTechs; // Bitmask of supported Modem Technology bits
88 int currentTech; // Technology the modem is currently using (in the format used by modem)
89 int isMultimode;
90
91 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
92 // in which the byte number from LSB to MSB give the priority.
93 //
94 // |MSB| | |LSB
95 // value: |00 |00 |00 |00
96 // byte #: |3 |2 |1 |0
97 //
98 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
99 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
100 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
101 int32_t preferredNetworkMode;
102 int subscription_source;
103
104} ModemInfo;
105
106static ModemInfo *sMdmInfo;
107// TECH returns the current technology in the format used by the modem.
108// It can be used as an l-value
109#define TECH(mdminfo) ((mdminfo)->currentTech)
110// TECH_BIT returns the bitmask equivalent of the current tech
111#define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
112#define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
113#define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
114#define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
115// CDMA Subscription Source
116#define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
117
118static int net2modem[] = {
119 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
120 MDM_GSM, // 1 - GSM only
121 MDM_WCDMA, // 2 - WCDMA only
122 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
123 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
124 MDM_CDMA, // 5 - CDMA only
125 MDM_EVDO, // 6 - EvDo only
126 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
127 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
128 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
129 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
130 MDM_LTE, // 11 - LTE only
131};
132
133static int32_t net2pmask[] = {
134 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
135 MDM_GSM, // 1 - GSM only
136 MDM_WCDMA, // 2 - WCDMA only
137 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
138 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
139 MDM_CDMA, // 5 - CDMA only
140 MDM_EVDO, // 6 - EvDo only
141 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
142 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
143 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
144 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
145 MDM_LTE, // 11 - LTE only
146};
147
148static int is3gpp2(int radioTech) {
149 switch (radioTech) {
150 case RADIO_TECH_IS95A:
151 case RADIO_TECH_IS95B:
152 case RADIO_TECH_1xRTT:
153 case RADIO_TECH_EVDO_0:
154 case RADIO_TECH_EVDO_A:
155 case RADIO_TECH_EVDO_B:
156 case RADIO_TECH_EHRPD:
157 return 1;
158 default:
159 return 0;
160 }
161}
162
John Wang309ac292009-07-30 14:53:23 -0700163typedef enum {
164 SIM_ABSENT = 0,
165 SIM_NOT_READY = 1,
Naveen Kalla2baf7232016-10-11 13:49:20 -0700166 SIM_READY = 2,
John Wang309ac292009-07-30 14:53:23 -0700167 SIM_PIN = 3,
168 SIM_PUK = 4,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700169 SIM_NETWORK_PERSONALIZATION = 5,
170 RUIM_ABSENT = 6,
171 RUIM_NOT_READY = 7,
172 RUIM_READY = 8,
173 RUIM_PIN = 9,
174 RUIM_PUK = 10,
bohu076e6872017-07-02 21:33:28 -0700175 RUIM_NETWORK_PERSONALIZATION = 11,
176 ISIM_ABSENT = 12,
177 ISIM_NOT_READY = 13,
178 ISIM_READY = 14,
179 ISIM_PIN = 15,
180 ISIM_PUK = 16,
181 ISIM_NETWORK_PERSONALIZATION = 17,
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100182} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -0700183
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800184static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
185static RIL_RadioState currentState();
186static int onSupports (int requestCode);
187static void onCancel (RIL_Token t);
188static const char *getVersion();
189static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -0700190static SIM_Status getSIMStatus();
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700191static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
192static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -0700193static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800194
195extern const char * requestToString(int request);
196
197/*** Static Variables ***/
198static const RIL_RadioFunctions s_callbacks = {
199 RIL_VERSION,
200 onRequest,
201 currentState,
202 onSupports,
203 onCancel,
204 getVersion
205};
206
207#ifdef RIL_SHLIB
208static const struct RIL_Env *s_rilenv;
209
210#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
211#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
212#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
213#endif
214
215static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
216
217static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
218static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
219
220static int s_port = -1;
221static const char * s_device_path = NULL;
222static int s_device_socket = 0;
223
224/* trigger change to this with s_state_cond */
225static int s_closed = 0;
226
227static int sFD; /* file desc of AT channel */
228static char sATBuffer[MAX_AT_RESPONSE+1];
229static char *sATBufferCur = NULL;
230
231static const struct timeval TIMEVAL_SIMPOLL = {1,0};
232static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
233static const struct timeval TIMEVAL_0 = {0,0};
234
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -0700235static int s_ims_registered = 0; // 0==unregistered
236static int s_ims_services = 1; // & 0x1 == sms over ims supported
237static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
238static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
239static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
240static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
241static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
242
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800243#ifdef WORKAROUND_ERRONEOUS_ANSWER
244// Max number of times we'll try to repoll when we think
245// we have a AT+CLCC race condition
246#define REPOLL_CALLS_COUNT_MAX 4
247
248// Line index that was incoming or waiting at last poll, or -1 for none
249static int s_incomingOrWaitingLine = -1;
250// Number of times we've asked for a repoll of AT+CLCC
251static int s_repollCallsCount = 0;
252// Should we expect a call to be answered in the next CLCC?
253static int s_expectAnswer = 0;
254#endif /* WORKAROUND_ERRONEOUS_ANSWER */
255
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200256
Wink Saville8a9e0212013-04-09 12:11:38 -0700257static int s_cell_info_rate_ms = INT_MAX;
258static int s_mcc = 0;
259static int s_mnc = 0;
260static int s_lac = 0;
261static int s_cid = 0;
262
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800263static void pollSIMState (void *param);
264static void setRadioState(RIL_RadioState newState);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700265static void setRadioTechnology(ModemInfo *mdm, int newtech);
266static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
267static int parse_technology_response(const char *response, int *current, int32_t *preferred);
268static int techFromModemType(int mdmtype);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800269
270static int clccStateToRILState(int state, RIL_CallState *p_state)
271
272{
273 switch(state) {
274 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
275 case 1: *p_state = RIL_CALL_HOLDING; return 0;
276 case 2: *p_state = RIL_CALL_DIALING; return 0;
277 case 3: *p_state = RIL_CALL_ALERTING; return 0;
278 case 4: *p_state = RIL_CALL_INCOMING; return 0;
279 case 5: *p_state = RIL_CALL_WAITING; return 0;
280 default: return -1;
281 }
282}
283
284/**
285 * Note: directly modified line and has *p_call point directly into
286 * modified line
287 */
Wink Saville3d54e742009-05-18 18:00:44 -0700288static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800289{
290 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
291 // index,isMT,state,mode,isMpty(,number,TOA)?
292
293 int err;
294 int state;
295 int mode;
296
297 err = at_tok_start(&line);
298 if (err < 0) goto error;
299
300 err = at_tok_nextint(&line, &(p_call->index));
301 if (err < 0) goto error;
302
303 err = at_tok_nextbool(&line, &(p_call->isMT));
304 if (err < 0) goto error;
305
306 err = at_tok_nextint(&line, &state);
307 if (err < 0) goto error;
308
309 err = clccStateToRILState(state, &(p_call->state));
310 if (err < 0) goto error;
311
312 err = at_tok_nextint(&line, &mode);
313 if (err < 0) goto error;
314
315 p_call->isVoice = (mode == 0);
316
317 err = at_tok_nextbool(&line, &(p_call->isMpty));
318 if (err < 0) goto error;
319
320 if (at_tok_hasmore(&line)) {
321 err = at_tok_nextstr(&line, &(p_call->number));
322
323 /* tolerate null here */
324 if (err < 0) return 0;
325
326 // Some lame implementations return strings
327 // like "NOT AVAILABLE" in the CLCC line
328 if (p_call->number != NULL
329 && 0 == strspn(p_call->number, "+0123456789")
330 ) {
331 p_call->number = NULL;
332 }
333
334 err = at_tok_nextint(&line, &p_call->toa);
335 if (err < 0) goto error;
336 }
337
Wink Saville74fa3882009-12-22 15:35:41 -0800338 p_call->uusInfo = NULL;
339
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800340 return 0;
341
342error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800343 RLOGE("invalid CLCC line\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800344 return -1;
345}
346
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -0700347static int parseSimResponseLine(char* line, RIL_SIM_IO_Response* response) {
348 int err;
349
350 err = at_tok_start(&line);
351 if (err < 0) return err;
352 err = at_tok_nextint(&line, &response->sw1);
353 if (err < 0) return err;
354 err = at_tok_nextint(&line, &response->sw2);
355 if (err < 0) return err;
356
357 if (at_tok_hasmore(&line)) {
358 err = at_tok_nextstr(&line, &response->simResponse);
359 if (err < 0) return err;
360 }
361 return 0;
362}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800363
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -0700364enum InterfaceState {
365 kInterfaceUp,
366 kInterfaceDown,
367};
368
369static RIL_Errno setInterfaceState(const char* interfaceName,
370 enum InterfaceState state) {
371 struct ifreq request;
372 int status = 0;
373 int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
374 if (sock == -1) {
375 RLOGE("Failed to open interface socket: %s (%d)",
376 strerror(errno), errno);
377 return RIL_E_GENERIC_FAILURE;
378 }
379
380 memset(&request, 0, sizeof(request));
381 strncpy(request.ifr_name, interfaceName, sizeof(request.ifr_name));
382 request.ifr_name[sizeof(request.ifr_name) - 1] = '\0';
383 status = ioctl(sock, SIOCGIFFLAGS, &request);
384 if (status != 0) {
385 RLOGE("Failed to get interface flags for %s: %s (%d)",
386 interfaceName, strerror(errno), errno);
387 close(sock);
388 return RIL_E_RADIO_NOT_AVAILABLE;
389 }
390
391 bool isUp = (request.ifr_flags & IFF_UP);
392 if ((state == kInterfaceUp && isUp) || (state == kInterfaceDown && !isUp)) {
393 // Interface already in desired state
394 close(sock);
395 return RIL_E_SUCCESS;
396 }
397
398 // Simply toggle the flag since we know it's the opposite of what we want
399 request.ifr_flags ^= IFF_UP;
400
401 status = ioctl(sock, SIOCSIFFLAGS, &request);
402 if (status != 0) {
403 RLOGE("Failed to set interface flags for %s: %s (%d)",
404 interfaceName, strerror(errno), errno);
405 close(sock);
406 return RIL_E_GENERIC_FAILURE;
407 }
408
409 close(sock);
410 return RIL_E_SUCCESS;
411}
412
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800413/** do post-AT+CFUN=1 initialization */
414static void onRadioPowerOn()
415{
416#ifdef USE_TI_COMMANDS
417 /* Must be after CFUN=1 */
418 /* TI specific -- notifications for CPHS things such */
419 /* as CPHS message waiting indicator */
420
421 at_send_command("AT%CPHS=1", NULL);
422
423 /* TI specific -- enable NITZ unsol notifs */
424 at_send_command("AT%CTZV=1", NULL);
425#endif
426
427 pollSIMState(NULL);
428}
429
430/** do post- SIM ready initialization */
431static void onSIMReady()
432{
433 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
434 /*
435 * Always send SMS messages directly to the TE
436 *
437 * mode = 1 // discard when link is reserved (link should never be
438 * reserved)
439 * mt = 2 // most messages routed to TE
440 * bm = 2 // new cell BM's routed to TE
441 * ds = 1 // Status reports routed to TE
442 * bfr = 1 // flush buffer
443 */
444 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
445}
446
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700447static void requestRadioPower(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800448{
449 int onOff;
450
451 int err;
452 ATResponse *p_response = NULL;
453
454 assert (datalen >= sizeof(int *));
455 onOff = ((int *)data)[0];
456
457 if (onOff == 0 && sState != RADIO_STATE_OFF) {
458 err = at_send_command("AT+CFUN=0", &p_response);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800459 if (err < 0 || p_response->success == 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800460 setRadioState(RADIO_STATE_OFF);
461 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
462 err = at_send_command("AT+CFUN=1", &p_response);
463 if (err < 0|| p_response->success == 0) {
464 // Some stacks return an error when there is no SIM,
465 // but they really turn the RF portion on
466 // So, if we get an error, let's check to see if it
467 // turned on anyway
468
469 if (isRadioOn() != 1) {
470 goto error;
471 }
472 }
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700473 setRadioState(RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800474 }
475
476 at_response_free(p_response);
477 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
478 return;
479error:
480 at_response_free(p_response);
481 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
482}
483
Naveen Kallaa65a16a2014-07-31 16:48:31 -0700484static void requestShutdown(RIL_Token t)
485{
486 int onOff;
487
488 int err;
489 ATResponse *p_response = NULL;
490
491 if (sState != RADIO_STATE_OFF) {
492 err = at_send_command("AT+CFUN=0", &p_response);
493 setRadioState(RADIO_STATE_UNAVAILABLE);
494 }
495
496 at_response_free(p_response);
497 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
498 return;
499}
500
Wink Savillef4c4d362009-04-02 01:37:03 -0700501static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800502
Mark Salyzynba58c202014-03-12 15:20:22 -0700503static void onDataCallListChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800504{
Wink Savillef4c4d362009-04-02 01:37:03 -0700505 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800506}
507
Mark Salyzynba58c202014-03-12 15:20:22 -0700508static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800509{
Wink Savillef4c4d362009-04-02 01:37:03 -0700510 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800511}
512
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800513// Hang up, reject, conference, call waiting
514static void requestCallSelection(
515 void *data __unused, size_t datalen __unused, RIL_Token t, int request)
516{
517 // 3GPP 22.030 6.5.5
518 static char hangupWaiting[] = "AT+CHLD=0";
519 static char hangupForeground[] = "AT+CHLD=1";
520 static char switchWaiting[] = "AT+CHLD=2";
521 static char conference[] = "AT+CHLD=3";
522 static char reject[] = "ATH";
523
524 char* atCommand;
525
526 if (getSIMStatus() == SIM_ABSENT) {
527 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
528 return;
529 }
530
531 switch(request) {
532 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
533 // "Releases all held calls or sets User Determined User Busy
534 // (UDUB) for a waiting call."
535 atCommand = hangupWaiting;
536 break;
537 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
538 // "Releases all active calls (if any exist) and accepts
539 // the other (held or waiting) call."
540 atCommand = hangupForeground;
541 break;
542 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
543 // "Places all active calls (if any exist) on hold and accepts
544 // the other (held or waiting) call."
545 atCommand = switchWaiting;
546#ifdef WORKAROUND_ERRONEOUS_ANSWER
547 s_expectAnswer = 1;
548#endif /* WORKAROUND_ERRONEOUS_ANSWER */
549 break;
550 case RIL_REQUEST_CONFERENCE:
551 // "Adds a held call to the conversation"
552 atCommand = conference;
553 break;
554 case RIL_REQUEST_UDUB:
555 // User determined user busy (reject)
556 atCommand = reject;
557 break;
558 default:
559 assert(0);
560 }
561 at_send_command(atCommand, NULL);
562 // Success or failure is ignored by the upper layer here.
563 // It will call GET_CURRENT_CALLS and determine success that way.
564 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
565}
566
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -0700567static bool hasWifiCapability()
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800568{
569 char propValue[PROP_VALUE_MAX];
Bjoern Johanssonbc497e72017-07-27 15:41:08 -0700570 return property_get("ro.kernel.qemu.wifi", propValue, "") > 0 &&
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800571 strcmp("1", propValue) == 0;
572}
573
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -0700574static const char* getRadioInterfaceName(bool hasWifi)
575{
576 return hasWifi ? PPP_TTY_PATH_RADIO0 : PPP_TTY_PATH_ETH0;
577}
578
Wink Savillef4c4d362009-04-02 01:37:03 -0700579static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800580{
581 ATResponse *p_response;
582 ATLine *p_cur;
583 int err;
584 int n = 0;
585 char *out;
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800586 char propValue[PROP_VALUE_MAX];
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -0700587 bool hasWifi = hasWifiCapability();
588 const char* radioInterfaceName = getRadioInterfaceName(hasWifi);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800589
590 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
591 if (err != 0 || p_response->success == 0) {
592 if (t != NULL)
593 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
594 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700595 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800596 NULL, 0);
597 return;
598 }
599
600 for (p_cur = p_response->p_intermediates; p_cur != NULL;
601 p_cur = p_cur->p_next)
602 n++;
603
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700604 RIL_Data_Call_Response_v11 *responses =
605 alloca(n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800606
607 int i;
608 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800609 responses[i].status = -1;
Wink Saville250eb3c2011-06-22 09:11:34 -0700610 responses[i].suggestedRetryTime = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800611 responses[i].cid = -1;
612 responses[i].active = -1;
613 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800614 responses[i].ifname = "";
615 responses[i].addresses = "";
616 responses[i].dnses = "";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700617 responses[i].gateways = "";
Etan Cohend3652192014-06-20 08:28:44 -0700618 responses[i].pcscf = "";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700619 responses[i].mtu = 0;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800620 }
621
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700622 RIL_Data_Call_Response_v11 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800623 for (p_cur = p_response->p_intermediates; p_cur != NULL;
624 p_cur = p_cur->p_next) {
625 char *line = p_cur->line;
626
627 err = at_tok_start(&line);
628 if (err < 0)
629 goto error;
630
631 err = at_tok_nextint(&line, &response->cid);
632 if (err < 0)
633 goto error;
634
635 err = at_tok_nextint(&line, &response->active);
636 if (err < 0)
637 goto error;
638
639 response++;
640 }
641
642 at_response_free(p_response);
643
644 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
645 if (err != 0 || p_response->success == 0) {
646 if (t != NULL)
647 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
648 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700649 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800650 NULL, 0);
651 return;
652 }
653
654 for (p_cur = p_response->p_intermediates; p_cur != NULL;
655 p_cur = p_cur->p_next) {
656 char *line = p_cur->line;
657 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800658
659 err = at_tok_start(&line);
660 if (err < 0)
661 goto error;
662
663 err = at_tok_nextint(&line, &cid);
664 if (err < 0)
665 goto error;
666
667 for (i = 0; i < n; i++) {
668 if (responses[i].cid == cid)
669 break;
670 }
671
672 if (i >= n) {
673 /* details for a context we didn't hear about in the last request */
674 continue;
675 }
676
Wink Saville43808972011-01-13 17:39:51 -0800677 // Assume no error
678 responses[i].status = 0;
679
680 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800681 err = at_tok_nextstr(&line, &out);
682 if (err < 0)
683 goto error;
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700684
685 int type_size = strlen(out) + 1;
686 responses[i].type = alloca(type_size);
687 strlcpy(responses[i].type, out, type_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800688
Wink Saville43808972011-01-13 17:39:51 -0800689 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800690 err = at_tok_nextstr(&line, &out);
691 if (err < 0)
692 goto error;
693
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -0700694 int ifname_size = strlen(radioInterfaceName) + 1;
695 responses[i].ifname = alloca(ifname_size);
696 strlcpy(responses[i].ifname, radioInterfaceName, ifname_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800697
698 err = at_tok_nextstr(&line, &out);
699 if (err < 0)
700 goto error;
701
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700702 int addresses_size = strlen(out) + 1;
703 responses[i].addresses = alloca(addresses_size);
704 strlcpy(responses[i].addresses, out, addresses_size);
Wink Saville43808972011-01-13 17:39:51 -0800705
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200706 if (isInEmulator()) {
707 /* We are in the emulator - the dns servers are listed
708 * by the following system properties, setup in
709 * /system/etc/init.goldfish.sh:
710 * - net.eth0.dns1
711 * - net.eth0.dns2
712 * - net.eth0.dns3
713 * - net.eth0.dns4
714 */
715 const int dnslist_sz = 128;
716 char* dnslist = alloca(dnslist_sz);
717 const char* separator = "";
718 int nn;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100719
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200720 dnslist[0] = 0;
721 for (nn = 1; nn <= 4; nn++) {
722 /* Probe net.eth0.dns<n> */
723 char propName[PROP_NAME_MAX];
724 char propValue[PROP_VALUE_MAX];
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100725
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200726 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100727
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200728 /* Ignore if undefined */
Bjoern Johanssonbc497e72017-07-27 15:41:08 -0700729 if (property_get(propName, propValue, "") <= 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200730 continue;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100731 }
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700732
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200733 /* Append the DNS IP address */
734 strlcat(dnslist, separator, dnslist_sz);
735 strlcat(dnslist, propValue, dnslist_sz);
736 separator = " ";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100737 }
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200738 responses[i].dnses = dnslist;
739
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800740 /* There is only one gateway in the emulator. If WiFi is
741 * configured the interface visible to RIL will be behind a NAT
742 * where the gateway is different. */
Bjoern Johanssonbc497e72017-07-27 15:41:08 -0700743 if (hasWifi) {
744 responses[i].gateways = "192.168.200.1";
745 } else if (property_get("net.eth0.gw", propValue, "") > 0) {
746 responses[i].gateways = propValue;
747 } else {
748 responses[i].gateways = "";
749 }
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200750 responses[i].mtu = DEFAULT_MTU;
751 }
752 else {
753 /* I don't know where we are, so use the public Google DNS
754 * servers by default and no gateway.
755 */
756 responses[i].dnses = "8.8.8.8 8.8.4.4";
757 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100758 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800759 }
760
761 at_response_free(p_response);
762
763 if (t != NULL)
764 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700765 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800766 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700767 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800768 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700769 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800770
771 return;
772
773error:
774 if (t != NULL)
775 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
776 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700777 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800778 NULL, 0);
779
780 at_response_free(p_response);
781}
782
783static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700784 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800785{
786 int err;
787 ATResponse *p_response = NULL;
788 int response = 0;
789 char *line;
790
791 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
792
793 if (err < 0 || p_response->success == 0) {
794 goto error;
795 }
796
797 line = p_response->p_intermediates->line;
798
799 err = at_tok_start(&line);
800
801 if (err < 0) {
802 goto error;
803 }
804
805 err = at_tok_nextint(&line, &response);
806
807 if (err < 0) {
808 goto error;
809 }
810
811 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
812 at_response_free(p_response);
813 return;
814error:
815 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800816 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800817 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
818}
819
Mark Salyzynba58c202014-03-12 15:20:22 -0700820static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800821{
822 RIL_onUnsolicitedResponse (
823 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
824 NULL, 0);
825}
826
Mark Salyzynba58c202014-03-12 15:20:22 -0700827static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800828{
829 int err;
830 ATResponse *p_response;
831 ATLine *p_cur;
832 int countCalls;
833 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700834 RIL_Call *p_calls;
835 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800836 int i;
837 int needRepoll = 0;
838
839#ifdef WORKAROUND_ERRONEOUS_ANSWER
840 int prevIncomingOrWaitingLine;
841
842 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
843 s_incomingOrWaitingLine = -1;
844#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
845
846 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
847
848 if (err != 0 || p_response->success == 0) {
849 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
850 return;
851 }
852
853 /* count the calls */
854 for (countCalls = 0, p_cur = p_response->p_intermediates
855 ; p_cur != NULL
856 ; p_cur = p_cur->p_next
857 ) {
858 countCalls++;
859 }
860
861 /* yes, there's an array of pointers and then an array of structures */
862
Wink Saville3d54e742009-05-18 18:00:44 -0700863 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
864 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
865 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800866
867 /* init the pointer array */
868 for(i = 0; i < countCalls ; i++) {
869 pp_calls[i] = &(p_calls[i]);
870 }
871
872 for (countValidCalls = 0, p_cur = p_response->p_intermediates
873 ; p_cur != NULL
874 ; p_cur = p_cur->p_next
875 ) {
876 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
877
878 if (err != 0) {
879 continue;
880 }
881
882#ifdef WORKAROUND_ERRONEOUS_ANSWER
883 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
884 || p_calls[countValidCalls].state == RIL_CALL_WAITING
885 ) {
886 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
887 }
888#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
889
890 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
891 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
892 ) {
893 needRepoll = 1;
894 }
895
896 countValidCalls++;
897 }
898
899#ifdef WORKAROUND_ERRONEOUS_ANSWER
900 // Basically:
901 // A call was incoming or waiting
902 // Now it's marked as active
903 // But we never answered it
904 //
905 // This is probably a bug, and the call will probably
906 // disappear from the call list in the next poll
907 if (prevIncomingOrWaitingLine >= 0
908 && s_incomingOrWaitingLine < 0
909 && s_expectAnswer == 0
910 ) {
911 for (i = 0; i < countValidCalls ; i++) {
912
913 if (p_calls[i].index == prevIncomingOrWaitingLine
914 && p_calls[i].state == RIL_CALL_ACTIVE
915 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
916 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800917 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800918 "Hit WORKAROUND_ERRONOUS_ANSWER case."
919 " Repoll count: %d\n", s_repollCallsCount);
920 s_repollCallsCount++;
921 goto error;
922 }
923 }
924 }
925
926 s_expectAnswer = 0;
927 s_repollCallsCount = 0;
928#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
929
Wink Saville3d54e742009-05-18 18:00:44 -0700930 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
931 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800932
933 at_response_free(p_response);
934
935#ifdef POLL_CALL_STATE
936 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
937 // smd, so we're forced to poll until the call ends.
938#else
939 if (needRepoll) {
940#endif
941 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
942 }
943
944 return;
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700945#ifdef WORKAROUND_ERRONEOUS_ANSWER
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800946error:
947 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
948 at_response_free(p_response);
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700949#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800950}
951
Mark Salyzynba58c202014-03-12 15:20:22 -0700952static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800953{
954 RIL_Dial *p_dial;
955 char *cmd;
956 const char *clir;
957 int ret;
958
959 p_dial = (RIL_Dial *)data;
960
961 switch (p_dial->clir) {
962 case 1: clir = "I"; break; /*invocation*/
963 case 2: clir = "i"; break; /*suppression*/
964 default:
965 case 0: clir = ""; break; /*subscription default*/
966 }
967
968 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
969
970 ret = at_send_command(cmd, NULL);
971
972 free(cmd);
973
974 /* success or failure is ignored by the upper layer here.
975 it will call GET_CURRENT_CALLS and determine success that way */
976 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
977}
978
Mark Salyzynba58c202014-03-12 15:20:22 -0700979static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800980{
981 RIL_SMS_WriteArgs *p_args;
982 char *cmd;
983 int length;
984 int err;
985 ATResponse *p_response = NULL;
986
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800987 if (getSIMStatus() == SIM_ABSENT) {
988 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
989 return;
990 }
991
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800992 p_args = (RIL_SMS_WriteArgs *)data;
993
994 length = strlen(p_args->pdu)/2;
995 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
996
997 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
998
999 if (err != 0 || p_response->success == 0) goto error;
1000
1001 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1002 at_response_free(p_response);
1003
1004 return;
1005error:
1006 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1007 at_response_free(p_response);
1008}
1009
Mark Salyzynba58c202014-03-12 15:20:22 -07001010static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001011{
1012 int *p_line;
1013
1014 int ret;
1015 char *cmd;
1016
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001017 if (getSIMStatus() == SIM_ABSENT) {
1018 RIL_onRequestComplete(t, RIL_E_MODEM_ERR, NULL, 0);
1019 return;
1020 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001021 p_line = (int *)data;
1022
1023 // 3GPP 22.030 6.5.5
1024 // "Releases a specific active call X"
1025 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
1026
1027 ret = at_send_command(cmd, NULL);
1028
1029 free(cmd);
1030
1031 /* success or failure is ignored by the upper layer here.
1032 it will call GET_CURRENT_CALLS and determine success that way */
1033 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1034}
1035
Mark Salyzynba58c202014-03-12 15:20:22 -07001036static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001037{
1038 ATResponse *p_response = NULL;
1039 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001040 char *line;
Jim Kayedc9f31b2017-04-03 13:43:31 -07001041 int count = 0;
1042 // Accept a response that is at least v6, and up to v10
1043 int minNumOfElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
1044 int maxNumOfElements=sizeof(RIL_SignalStrength_v10)/sizeof(int);
1045 int response[maxNumOfElements];
1046
1047 memset(response, 0, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001048
1049 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
1050
1051 if (err < 0 || p_response->success == 0) {
1052 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1053 goto error;
1054 }
1055
1056 line = p_response->p_intermediates->line;
1057
1058 err = at_tok_start(&line);
1059 if (err < 0) goto error;
1060
Jim Kayedc9f31b2017-04-03 13:43:31 -07001061 for (count = 0; count < maxNumOfElements; count++) {
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -07001062 err = at_tok_nextint(&line, &(response[count]));
Jim Kayedc9f31b2017-04-03 13:43:31 -07001063 if (err < 0 && count < minNumOfElements) goto error;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -07001064 }
Chih-Wei Huang28059052012-04-30 01:13:27 +08001065
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -07001066 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001067
1068 at_response_free(p_response);
1069 return;
1070
1071error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001072 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001073 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1074 at_response_free(p_response);
1075}
1076
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001077/**
1078 * networkModePossible. Decides whether the network mode is appropriate for the
1079 * specified modem
1080 */
1081static int networkModePossible(ModemInfo *mdm, int nm)
1082{
1083 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
1084 return 1;
1085 }
1086 return 0;
1087}
Mark Salyzynba58c202014-03-12 15:20:22 -07001088static void requestSetPreferredNetworkType( int request __unused, void *data,
1089 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001090{
1091 ATResponse *p_response = NULL;
1092 char *cmd = NULL;
1093 int value = *(int *)data;
1094 int current, old;
1095 int err;
1096 int32_t preferred = net2pmask[value];
1097
Wink Saville4dcab4f2012-11-19 16:05:13 -08001098 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001099 if (!networkModePossible(sMdmInfo, value)) {
1100 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
1101 return;
1102 }
1103 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
1104 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1105 return;
1106 }
1107 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -08001108 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001109 if (old != preferred) {
1110 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -08001111 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001112 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
1113 free(cmd);
1114 if (err || !p_response->success) {
1115 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1116 return;
1117 }
1118 PREFERRED_NETWORK(sMdmInfo) = value;
1119 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
1120 int current;
1121 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
1122 switch (res) {
1123 case -1: // Error or unable to parse
1124 break;
1125 case 1: // Only able to parse current
1126 case 0: // Both current and preferred were parsed
1127 setRadioTechnology(sMdmInfo, current);
1128 break;
1129 }
1130 }
1131 }
1132 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1133}
1134
Mark Salyzynba58c202014-03-12 15:20:22 -07001135static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
1136 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001137{
1138 int preferred;
1139 unsigned i;
1140
1141 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
1142 case -1: // Error or unable to parse
1143 case 1: // Only able to parse current
1144 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1145 break;
1146 case 0: // Both current and preferred were parsed
1147 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
1148 if (preferred == net2pmask[i]) {
1149 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1150 return;
1151 }
1152 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001153 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001154 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1155 break;
1156 }
1157
1158}
1159
Mark Salyzynba58c202014-03-12 15:20:22 -07001160static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1161 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001162{
1163 int err;
1164 char * responseStr;
1165 ATResponse *p_response = NULL;
1166 const char *cmd;
1167 char *line;
1168
1169 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1170 if (err < 0 || !p_response->success) goto error;
1171 line = p_response->p_intermediates->line;
1172 err = at_tok_start(&line);
1173 if (err < 0) goto error;
1174 err = at_tok_nextstr(&line, &responseStr);
1175 if (err < 0 || !responseStr) goto error;
1176 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1177 at_response_free(p_response);
1178 return;
1179error:
1180 at_response_free(p_response);
1181 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1182}
1183
Mark Salyzynba58c202014-03-12 15:20:22 -07001184static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1185 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001186{
1187 int err;
1188 char * responseStr;
1189 ATResponse *p_response = NULL;
1190 const char *cmd;
1191 const char *prefix;
1192 char *line, *p;
1193 int commas;
1194 int skip;
1195 int count = 4;
1196
1197 // Fixed values. TODO: query modem
1198 responseStr = strdup("1.0.0.0");
1199 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1200 free(responseStr);
1201}
1202
Mark Salyzynba58c202014-03-12 15:20:22 -07001203static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1204 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001205{
1206 int err;
1207 int response[4];
1208 char * responseStr[4];
1209 ATResponse *p_response = NULL;
1210 const char *cmd;
1211 const char *prefix;
1212 char *line, *p;
1213 int commas;
1214 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001215 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001216
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001217 // Fixed values. TODO: Query modem
1218 responseStr[0] = "----";
1219 responseStr[1] = "----";
1220 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001221
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001222 err = at_send_command_numeric("AT+CGSN", &p_response);
1223 if (err < 0 || p_response->success == 0) {
1224 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1225 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001226 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001227 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001228 }
1229
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001230 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1231 at_response_free(p_response);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001232}
1233
Mark Salyzynba58c202014-03-12 15:20:22 -07001234static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1235 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001236{
1237 int err;
1238 int *ss = (int *)data;
1239 ATResponse *p_response = NULL;
1240 char *cmd = NULL;
1241 char *line = NULL;
1242 int response;
1243
1244 asprintf(&cmd, "AT+CCSS?");
1245 if (!cmd) goto error;
1246
1247 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1248 if (err < 0 || !p_response->success)
1249 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001250
1251 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001252 err = at_tok_start(&line);
1253 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001254
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001255 err = at_tok_nextint(&line, &response);
1256 free(cmd);
1257 cmd = NULL;
1258
1259 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1260
1261 return;
1262error:
1263 free(cmd);
1264 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1265}
1266
Mark Salyzynba58c202014-03-12 15:20:22 -07001267static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001268 size_t datalen, RIL_Token t)
1269{
1270 int err;
1271 int *ss = (int *)data;
1272 ATResponse *p_response = NULL;
1273 char *cmd = NULL;
1274
1275 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001276 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001277 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1278 return;
1279 }
1280 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1281 if (!cmd) goto error;
1282
1283 err = at_send_command(cmd, &p_response);
1284 if (err < 0 || !p_response->success)
1285 goto error;
1286 free(cmd);
1287 cmd = NULL;
1288
1289 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1290
1291 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1292
1293 return;
1294error:
1295 free(cmd);
1296 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1297}
1298
Mark Salyzynba58c202014-03-12 15:20:22 -07001299static void requestCdmaSubscription(int request __unused, void *data __unused,
1300 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001301{
1302 int err;
1303 int response[5];
1304 char * responseStr[5];
1305 ATResponse *p_response = NULL;
1306 const char *cmd;
1307 const char *prefix;
1308 char *line, *p;
1309 int commas;
1310 int skip;
1311 int count = 5;
1312
1313 // Fixed values. TODO: Query modem
1314 responseStr[0] = "8587777777"; // MDN
1315 responseStr[1] = "1"; // SID
1316 responseStr[2] = "1"; // NID
1317 responseStr[3] = "8587777777"; // MIN
1318 responseStr[4] = "1"; // PRL Version
1319 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001320}
1321
Mark Salyzynba58c202014-03-12 15:20:22 -07001322static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1323 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001324{
1325 int roaming_pref = -1;
1326 ATResponse *p_response = NULL;
1327 char *line;
1328 int res;
1329
1330 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1331 if (res < 0 || !p_response->success) {
1332 goto error;
1333 }
1334 line = p_response->p_intermediates->line;
1335
1336 res = at_tok_start(&line);
1337 if (res < 0) goto error;
1338
1339 res = at_tok_nextint(&line, &roaming_pref);
1340 if (res < 0) goto error;
1341
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001342 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001343 return;
1344error:
1345 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1346}
1347
Mark Salyzynba58c202014-03-12 15:20:22 -07001348static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1349 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001350{
1351 int *pref = (int *)data;
1352 ATResponse *p_response = NULL;
1353 char *line;
1354 int res;
1355 char *cmd = NULL;
1356
1357 asprintf(&cmd, "AT+WRMP=%d", *pref);
1358 if (cmd == NULL) goto error;
1359
1360 res = at_send_command(cmd, &p_response);
1361 if (res < 0 || !p_response->success)
1362 goto error;
1363
1364 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1365 free(cmd);
1366 return;
1367error:
1368 free(cmd);
1369 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1370}
1371
1372static int parseRegistrationState(char *str, int *type, int *items, int **response)
1373{
1374 int err;
1375 char *line = str, *p;
1376 int *resp = NULL;
1377 int skip;
1378 int count = 3;
1379 int commas;
1380
Wink Saville4dcab4f2012-11-19 16:05:13 -08001381 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001382 err = at_tok_start(&line);
1383 if (err < 0) goto error;
1384
1385 /* Ok you have to be careful here
1386 * The solicited version of the CREG response is
1387 * +CREG: n, stat, [lac, cid]
1388 * and the unsolicited version is
1389 * +CREG: stat, [lac, cid]
1390 * The <n> parameter is basically "is unsolicited creg on?"
1391 * which it should always be
1392 *
1393 * Now we should normally get the solicited version here,
1394 * but the unsolicited version could have snuck in
1395 * so we have to handle both
1396 *
1397 * Also since the LAC and CID are only reported when registered,
1398 * we can have 1, 2, 3, or 4 arguments here
1399 *
1400 * finally, a +CGREG: answer may have a fifth value that corresponds
1401 * to the network type, as in;
1402 *
1403 * +CGREG: n, stat [,lac, cid [,networkType]]
1404 */
1405
1406 /* count number of commas */
1407 commas = 0;
1408 for (p = line ; *p != '\0' ;p++) {
1409 if (*p == ',') commas++;
1410 }
1411
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001412 resp = (int *)calloc(commas + 1, sizeof(int));
1413 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001414 switch (commas) {
1415 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001416 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001417 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001418 resp[1] = -1;
1419 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001420 break;
1421
1422 case 1: /* +CREG: <n>, <stat> */
1423 err = at_tok_nextint(&line, &skip);
1424 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001425 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001426 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001427 resp[1] = -1;
1428 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001429 if (err < 0) goto error;
1430 break;
1431
1432 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001433 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001434 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001435 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001436 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001437 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001438 if (err < 0) goto error;
1439 break;
1440 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1441 err = at_tok_nextint(&line, &skip);
1442 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001443 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001444 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001445 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001446 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001447 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001448 if (err < 0) goto error;
1449 break;
1450 /* special case for CGREG, there is a fourth parameter
1451 * that is the network type (unknown/gprs/edge/umts)
1452 */
1453 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1454 err = at_tok_nextint(&line, &skip);
1455 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001456 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001457 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001458 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001459 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001460 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001461 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001462 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001463 if (err < 0) goto error;
1464 count = 4;
1465 break;
1466 default:
1467 goto error;
1468 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001469 s_lac = resp[1];
1470 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001471 if (response)
1472 *response = resp;
1473 if (items)
1474 *items = commas + 1;
1475 if (type)
1476 *type = techFromModemType(TECH(sMdmInfo));
1477 return 0;
1478error:
1479 free(resp);
1480 return -1;
1481}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001482
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001483#define REG_STATE_LEN 15
1484#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001485static void requestRegistrationState(int request, void *data __unused,
1486 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001487{
1488 int err;
1489 int *registration;
1490 char **responseStr = NULL;
1491 ATResponse *p_response = NULL;
1492 const char *cmd;
1493 const char *prefix;
1494 char *line;
1495 int i = 0, j, numElements = 0;
1496 int count = 3;
1497 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001498
Wink Saville4dcab4f2012-11-19 16:05:13 -08001499 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001500 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1501 cmd = "AT+CREG?";
1502 prefix = "+CREG:";
1503 numElements = REG_STATE_LEN;
1504 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1505 cmd = "AT+CGREG?";
1506 prefix = "+CGREG:";
1507 numElements = REG_DATA_STATE_LEN;
1508 } else {
1509 assert(0);
1510 goto error;
1511 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001512
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001513 err = at_send_command_singleline(cmd, prefix, &p_response);
1514
1515 if (err != 0) goto error;
1516
1517 line = p_response->p_intermediates->line;
1518
1519 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1520
1521 responseStr = malloc(numElements * sizeof(char *));
1522 if (!responseStr) goto error;
1523 memset(responseStr, 0, numElements * sizeof(char *));
1524 /**
1525 * The first '4' bytes for both registration states remain the same.
1526 * But if the request is 'DATA_REGISTRATION_STATE',
1527 * the 5th and 6th byte(s) are optional.
1528 */
1529 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001530 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001531 // TODO: Query modem
1532 startfrom = 3;
1533 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1534 asprintf(&responseStr[3], "8"); // EvDo revA
1535 asprintf(&responseStr[4], "1"); // BSID
1536 asprintf(&responseStr[5], "123"); // Latitude
1537 asprintf(&responseStr[6], "222"); // Longitude
1538 asprintf(&responseStr[7], "0"); // CSS Indicator
1539 asprintf(&responseStr[8], "4"); // SID
1540 asprintf(&responseStr[9], "65535"); // NID
1541 asprintf(&responseStr[10], "0"); // Roaming indicator
1542 asprintf(&responseStr[11], "1"); // System is in PRL
1543 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1544 asprintf(&responseStr[13], "0"); // Reason for denial
1545 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1546 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1547 asprintf(&responseStr[3], "8"); // Available data radio technology
1548 }
1549 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001550 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001551 startfrom = 0;
1552 asprintf(&responseStr[1], "%x", registration[1]);
1553 asprintf(&responseStr[2], "%x", registration[2]);
1554 if (count > 3)
1555 asprintf(&responseStr[3], "%d", registration[3]);
1556 }
1557 asprintf(&responseStr[0], "%d", registration[0]);
1558
1559 /**
1560 * Optional bytes for DATA_REGISTRATION_STATE request
1561 * 4th byte : Registration denial code
1562 * 5th byte : The max. number of simultaneous Data Calls
1563 */
1564 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1565 // asprintf(&responseStr[4], "3");
1566 // asprintf(&responseStr[5], "1");
1567 }
1568
1569 for (j = startfrom; j < numElements; j++) {
1570 if (!responseStr[i]) goto error;
1571 }
1572 free(registration);
1573 registration = NULL;
1574
1575 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1576 for (j = 0; j < numElements; j++ ) {
1577 free(responseStr[j]);
1578 responseStr[j] = NULL;
1579 }
1580 free(responseStr);
1581 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001582 at_response_free(p_response);
1583
1584 return;
1585error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001586 if (responseStr) {
1587 for (j = 0; j < numElements; j++) {
1588 free(responseStr[j]);
1589 responseStr[j] = NULL;
1590 }
1591 free(responseStr);
1592 responseStr = NULL;
1593 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001594 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001595 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1596 at_response_free(p_response);
1597}
1598
Mark Salyzynba58c202014-03-12 15:20:22 -07001599static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001600{
1601 int err;
1602 int i;
1603 int skip;
1604 ATLine *p_cur;
1605 char *response[3];
1606
1607 memset(response, 0, sizeof(response));
1608
1609 ATResponse *p_response = NULL;
1610
1611 err = at_send_command_multiline(
1612 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1613 "+COPS:", &p_response);
1614
1615 /* we expect 3 lines here:
1616 * +COPS: 0,0,"T - Mobile"
1617 * +COPS: 0,1,"TMO"
1618 * +COPS: 0,2,"310170"
1619 */
1620
1621 if (err != 0) goto error;
1622
1623 for (i = 0, p_cur = p_response->p_intermediates
1624 ; p_cur != NULL
1625 ; p_cur = p_cur->p_next, i++
1626 ) {
1627 char *line = p_cur->line;
1628
1629 err = at_tok_start(&line);
1630 if (err < 0) goto error;
1631
1632 err = at_tok_nextint(&line, &skip);
1633 if (err < 0) goto error;
1634
1635 // If we're unregistered, we may just get
1636 // a "+COPS: 0" response
1637 if (!at_tok_hasmore(&line)) {
1638 response[i] = NULL;
1639 continue;
1640 }
1641
1642 err = at_tok_nextint(&line, &skip);
1643 if (err < 0) goto error;
1644
1645 // a "+COPS: 0, n" response is also possible
1646 if (!at_tok_hasmore(&line)) {
1647 response[i] = NULL;
1648 continue;
1649 }
1650
1651 err = at_tok_nextstr(&line, &(response[i]));
1652 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001653 // Simple assumption that mcc and mnc are 3 digits each
1654 if (strlen(response[i]) == 6) {
1655 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1656 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1657 }
1658 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001659 }
1660
1661 if (i != 3) {
1662 /* expect 3 lines exactly */
1663 goto error;
1664 }
1665
1666 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1667 at_response_free(p_response);
1668
1669 return;
1670error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001671 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001672 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1673 at_response_free(p_response);
1674}
1675
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001676static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1677{
1678 int err = 1; // Set to go to error:
1679 RIL_SMS_Response response;
1680 RIL_CDMA_SMS_Message* rcsm;
1681
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001682 if (getSIMStatus() == SIM_ABSENT) {
1683 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
1684 return;
1685 }
1686
Mark Salyzynba58c202014-03-12 15:20:22 -07001687 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001688 datalen, sizeof(RIL_CDMA_SMS_Message));
1689
1690 // verify data content to test marshalling/unmarshalling:
1691 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001692 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001693 uServicecategory=%d, sAddress.digit_mode=%d, \
1694 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1695 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1696 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1697 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1698
1699 if (err != 0) goto error;
1700
1701 // Cdma Send SMS implementation will go here:
1702 // But it is not implemented yet.
1703
1704 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001705 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001706 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1707 return;
1708
1709error:
1710 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001711 response.messageRef = -1;
1712 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001713}
1714
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001715static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1716{
1717 int err;
1718 const char *smsc;
1719 const char *pdu;
1720 int tpLayerLength;
1721 char *cmd1, *cmd2;
1722 RIL_SMS_Response response;
1723 ATResponse *p_response = NULL;
1724
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001725 if (getSIMStatus() == SIM_ABSENT) {
1726 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
1727 return;
1728 }
1729
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001730 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001731 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001732
1733 if (s_ims_gsm_fail != 0) goto error;
1734 if (s_ims_gsm_retry != 0) goto error2;
1735
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001736 smsc = ((const char **)data)[0];
1737 pdu = ((const char **)data)[1];
1738
1739 tpLayerLength = strlen(pdu)/2;
1740
1741 // "NULL for default SMSC"
1742 if (smsc == NULL) {
1743 smsc= "00";
1744 }
1745
1746 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1747 asprintf(&cmd2, "%s%s", smsc, pdu);
1748
1749 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1750
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001751 free(cmd1);
1752 free(cmd2);
1753
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001754 if (err != 0 || p_response->success == 0) goto error;
1755
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001756 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001757 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001758 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1759 at_response_free(p_response);
1760
1761 return;
1762error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001763 response.messageRef = -2;
1764 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001765 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001766 return;
1767error2:
1768 // send retry error.
1769 response.messageRef = -1;
1770 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1771 at_response_free(p_response);
1772 return;
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001773}
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001774
1775static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1776{
1777 RIL_IMS_SMS_Message *p_args;
1778 RIL_SMS_Response response;
1779
1780 memset(&response, 0, sizeof(response));
1781
Mark Salyzynba58c202014-03-12 15:20:22 -07001782 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001783 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1784 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1785 datalen, s_ims_registered, s_ims_services, s_ims_format,
1786 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1787 s_ims_gsm_retry);
1788
1789 // figure out if this is gsm/cdma format
1790 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1791 p_args = (RIL_IMS_SMS_Message *)data;
1792
1793 if (0 != s_ims_cause_perm_failure ) goto error;
1794
1795 // want to fail over ims and this is first request over ims
1796 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1797
1798 if (RADIO_TECH_3GPP == p_args->tech) {
1799 return requestSendSMS(p_args->message.gsmMessage,
1800 datalen - sizeof(RIL_RadioTechnologyFamily),
1801 t);
1802 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1803 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1804 datalen - sizeof(RIL_RadioTechnologyFamily),
1805 t);
1806 } else {
1807 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1808 }
1809
1810error:
1811 response.messageRef = -2;
1812 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1813 return;
1814
1815error2:
1816 response.messageRef = -1;
1817 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001818}
1819
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001820static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1821{
1822 ATResponse *p_response = NULL;
1823 int32_t session_id;
1824 int err;
1825 char cmd[32];
1826 char dummy;
1827 char *line;
1828
1829 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1830 if (data == NULL || datalen == 0 || datalen > 16) {
1831 ALOGE("Invalid data passed to requestSimOpenChannel");
1832 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1833 return;
1834 }
1835
1836 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1837
1838 err = at_send_command_numeric(cmd, &p_response);
1839 if (err < 0 || p_response == NULL || p_response->success == 0) {
1840 ALOGE("Error %d opening logical channel: %d",
1841 err, p_response ? p_response->success : 0);
1842 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1843 at_response_free(p_response);
1844 return;
1845 }
1846
1847 // Ensure integer only by scanning for an extra char but expect one result
1848 line = p_response->p_intermediates->line;
1849 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1850 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1851 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1852 return;
1853 }
1854
1855 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1856 at_response_free(p_response);
1857}
1858
1859static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1860{
1861 ATResponse *p_response = NULL;
1862 int32_t session_id;
1863 int err;
1864 char cmd[32];
1865
1866 if (data == NULL || datalen != sizeof(session_id)) {
1867 ALOGE("Invalid data passed to requestSimCloseChannel");
1868 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1869 return;
1870 }
1871 session_id = ((int32_t *)data)[0];
1872 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1873 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1874
1875 if (err < 0 || p_response == NULL || p_response->success == 0) {
1876 ALOGE("Error %d closing logical channel %d: %d",
1877 err, session_id, p_response ? p_response->success : 0);
1878 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1879 at_response_free(p_response);
1880 return;
1881 }
1882
1883 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1884
1885 at_response_free(p_response);
1886}
1887
1888static void requestSimTransmitApduChannel(void *data,
1889 size_t datalen,
1890 RIL_Token t)
1891{
1892 ATResponse *p_response = NULL;
1893 int err;
1894 char *cmd;
1895 char *line;
1896 size_t cmd_size;
1897 RIL_SIM_IO_Response sim_response;
1898 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1899
1900 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1901 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1902 return;
1903 }
1904
1905 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
Wei Wang9cec1e02017-02-08 14:37:37 -08001906 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001907 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1908 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1909
1910 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1911 free(cmd);
1912 if (err < 0 || p_response == NULL || p_response->success == 0) {
1913 ALOGE("Error %d transmitting APDU: %d",
1914 err, p_response ? p_response->success : 0);
1915 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1916 at_response_free(p_response);
1917 return;
1918 }
1919
1920 line = p_response->p_intermediates->line;
1921 err = parseSimResponseLine(line, &sim_response);
1922
1923 if (err == 0) {
1924 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1925 &sim_response, sizeof(sim_response));
1926 } else {
1927 ALOGE("Error %d parsing SIM response line: %s", err, line);
1928 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1929 }
1930 at_response_free(p_response);
1931}
1932
Wink Savillef4c4d362009-04-02 01:37:03 -07001933static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001934{
1935 const char *apn;
1936 char *cmd;
1937 int err;
1938 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001939
Wink Savillef4c4d362009-04-02 01:37:03 -07001940 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001941
1942#ifdef USE_TI_COMMANDS
1943 // Config for multislot class 10 (probably default anyway eh?)
1944 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1945 NULL);
1946
1947 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1948#endif /* USE_TI_COMMANDS */
1949
1950 int fd, qmistatus;
1951 size_t cur = 0;
1952 size_t len;
1953 ssize_t written, rlen;
1954 char status[32] = {0};
1955 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001956 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001957
Wink Saville4dcab4f2012-11-19 16:05:13 -08001958 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001959
1960 fd = open ("/dev/qmi", O_RDWR);
1961 if (fd >= 0) { /* the device doesn't exist on the emulator */
1962
Wink Saville4dcab4f2012-11-19 16:05:13 -08001963 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001964 asprintf(&cmd, "up:%s", apn);
1965 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001966
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001967 while (cur < len) {
1968 do {
1969 written = write (fd, cmd + cur, len - cur);
1970 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001971
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001972 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001973 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001974 close(fd);
1975 goto error;
1976 }
1977
1978 cur += written;
1979 }
1980
1981 // wait for interface to come online
1982
1983 do {
1984 sleep(1);
1985 do {
1986 rlen = read(fd, status, 31);
1987 } while (rlen < 0 && errno == EINTR);
1988
1989 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001990 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001991 close(fd);
1992 goto error;
1993 } else {
1994 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001995 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001996 }
1997 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1998
1999 close(fd);
2000
2001 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002002 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002003 goto error;
2004 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002005
2006 qmistatus = system("netcfg rmnet0 dhcp");
2007
Wink Saville4dcab4f2012-11-19 16:05:13 -08002008 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002009
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002010 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002011
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002012 } else {
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -07002013 bool hasWifi = hasWifiCapability();
2014 const char* radioInterfaceName = getRadioInterfaceName(hasWifi);
2015 if (setInterfaceState(radioInterfaceName, kInterfaceUp) != RIL_E_SUCCESS) {
2016 goto error;
2017 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002018
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002019 if (datalen > 6 * sizeof(char *)) {
2020 pdp_type = ((const char **)data)[6];
2021 } else {
2022 pdp_type = "IP";
2023 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002024
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002025 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002026 //FIXME check for error here
2027 err = at_send_command(cmd, NULL);
2028 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002029
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002030 // Set required QoS params to default
2031 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002032
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002033 // Set minimum QoS params to default
2034 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002035
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002036 // packet-domain event reporting
2037 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002038
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002039 // Hangup anything that's happening there now
2040 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002041
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002042 // Start data on PDP context 1
2043 err = at_send_command("ATD*99***1#", &p_response);
2044
2045 if (err < 0 || p_response->success == 0) {
2046 goto error;
2047 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002048 }
2049
Wink Saville43808972011-01-13 17:39:51 -08002050 requestOrSendDataCallList(&t);
2051
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002052 at_response_free(p_response);
2053
2054 return;
2055error:
2056 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2057 at_response_free(p_response);
2058
2059}
2060
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -07002061static void requestDeactivateDataCall(RIL_Token t)
2062{
2063 bool hasWifi = hasWifiCapability();
2064 const char* radioInterfaceName = getRadioInterfaceName(hasWifi);
2065 RIL_Errno rilErrno = setInterfaceState(radioInterfaceName, kInterfaceDown);
2066 RIL_onRequestComplete(t, rilErrno, NULL, 0);
2067}
2068
Mark Salyzynba58c202014-03-12 15:20:22 -07002069static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002070{
2071 int ackSuccess;
2072 int err;
2073
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002074 if (getSIMStatus() == SIM_ABSENT) {
2075 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2076 return;
2077 }
2078
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002079 ackSuccess = ((int *)data)[0];
2080
2081 if (ackSuccess == 1) {
2082 err = at_send_command("AT+CNMA=1", NULL);
2083 } else if (ackSuccess == 0) {
2084 err = at_send_command("AT+CNMA=2", NULL);
2085 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002086 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002087 goto error;
2088 }
2089
2090 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2091error:
2092 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2093
2094}
2095
Mark Salyzynba58c202014-03-12 15:20:22 -07002096static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002097{
2098 ATResponse *p_response = NULL;
2099 RIL_SIM_IO_Response sr;
2100 int err;
2101 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002102 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002103 char *line;
2104
2105 memset(&sr, 0, sizeof(sr));
2106
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002107 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002108
2109 /* FIXME handle pin2 */
2110
2111 if (p_args->data == NULL) {
2112 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
2113 p_args->command, p_args->fileid,
2114 p_args->p1, p_args->p2, p_args->p3);
2115 } else {
2116 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
2117 p_args->command, p_args->fileid,
2118 p_args->p1, p_args->p2, p_args->p3, p_args->data);
2119 }
2120
2121 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
2122
2123 if (err < 0 || p_response->success == 0) {
2124 goto error;
2125 }
2126
2127 line = p_response->p_intermediates->line;
2128
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002129 err = parseSimResponseLine(line, &sr);
2130 if (err < 0) {
2131 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002132 }
2133
2134 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
2135 at_response_free(p_response);
2136 free(cmd);
2137
2138 return;
2139error:
2140 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2141 at_response_free(p_response);
2142 free(cmd);
2143
2144}
2145
2146static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
2147{
2148 ATResponse *p_response = NULL;
2149 int err;
2150 char* cmd = NULL;
2151 const char** strings = (const char**)data;;
2152
2153 if ( datalen == sizeof(char*) ) {
2154 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
2155 } else if ( datalen == 2*sizeof(char*) ) {
2156 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
2157 } else
2158 goto error;
2159
2160 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
2161 free(cmd);
2162
2163 if (err < 0 || p_response->success == 0) {
2164error:
2165 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
2166 } else {
2167 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2168 }
2169 at_response_free(p_response);
2170}
2171
2172
Mark Salyzynba58c202014-03-12 15:20:22 -07002173static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002174{
2175 const char *ussdRequest;
2176
2177 ussdRequest = (char *)(data);
2178
2179
2180 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2181
2182// @@@ TODO
2183
2184}
2185
Mark Salyzynba58c202014-03-12 15:20:22 -07002186static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002187{
2188 int err;
2189 ATResponse *p_response = NULL;
2190
2191 err = at_send_command("AT+WSOS=0", &p_response);
2192
2193 if (err < 0 || p_response->success == 0) {
2194 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2195 return;
2196 }
2197
2198 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2199}
2200
2201// TODO: Use all radio types
2202static int techFromModemType(int mdmtype)
2203{
2204 int ret = -1;
2205 switch (1 << mdmtype) {
2206 case MDM_CDMA:
2207 ret = RADIO_TECH_1xRTT;
2208 break;
2209 case MDM_EVDO:
2210 ret = RADIO_TECH_EVDO_A;
2211 break;
2212 case MDM_GSM:
2213 ret = RADIO_TECH_GPRS;
2214 break;
2215 case MDM_WCDMA:
2216 ret = RADIO_TECH_HSPA;
2217 break;
2218 case MDM_LTE:
2219 ret = RADIO_TECH_LTE;
2220 break;
2221 }
2222 return ret;
2223}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002224
Mark Salyzynba58c202014-03-12 15:20:22 -07002225static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002226{
2227 uint64_t curTime = ril_nano_time();
2228 RIL_CellInfo ci[1] =
2229 {
2230 { // ci[0]
2231 1, // cellInfoType
2232 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002233 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002234 curTime - 1000, // Fake some time in the past
2235 { // union CellInfo
2236 { // RIL_CellInfoGsm gsm
2237 { // gsm.cellIdneityGsm
2238 s_mcc, // mcc
2239 s_mnc, // mnc
2240 s_lac, // lac
2241 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002242 },
2243 { // gsm.signalStrengthGsm
2244 10, // signalStrength
2245 0 // bitErrorRate
2246 }
2247 }
2248 }
2249 }
2250 };
2251
2252 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2253}
2254
2255
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002256static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002257{
2258 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2259 // will be sent.
2260 assert (datalen == sizeof(int));
2261 s_cell_info_rate_ms = ((int *)data)[0];
2262
2263 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2264}
2265
Etan Cohend3652192014-06-20 08:28:44 -07002266static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2267{
2268 // TODO - hook this up with real query/info from radio.
2269
2270 RIL_HardwareConfig hwCfg;
2271
2272 RIL_UNUSED_PARM(data);
2273 RIL_UNUSED_PARM(datalen);
2274
2275 hwCfg.type = -1;
2276
2277 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2278}
2279
Jim Kayed2d82012017-10-06 14:17:47 -07002280static void requestGetTtyMode(void *data, size_t datalen, RIL_Token t)
2281{
2282 int ttyModeResponse;
2283
2284 RIL_UNUSED_PARM(data);
2285 RIL_UNUSED_PARM(datalen);
2286
2287 ttyModeResponse = (getSIMStatus() == SIM_READY) ? 1 // TTY Full
2288 : 0; // TTY Off
2289
2290 RIL_onRequestComplete(t, RIL_E_SUCCESS, &ttyModeResponse, sizeof(ttyModeResponse));
2291}
2292
2293static void requestGetRadioCapability(void *data, size_t datalen, RIL_Token t)
2294{
2295 RIL_RadioCapability radioCapability;
2296
2297 RIL_UNUSED_PARM(data);
2298 RIL_UNUSED_PARM(datalen);
2299
2300 radioCapability.version = RIL_RADIO_CAPABILITY_VERSION;
2301 radioCapability.session = 0;
2302 radioCapability.phase = 0;
2303 radioCapability.rat = 0;
2304 radioCapability.logicalModemUuid[0] = '\0';
2305 radioCapability.status = RC_STATUS_SUCCESS;
2306
2307 RIL_onRequestComplete(t, RIL_E_SUCCESS, &radioCapability, sizeof(radioCapability));
2308}
2309
2310static void requestGetMute(void *data, size_t datalen, RIL_Token t)
2311{
2312 int muteResponse;
2313
2314 RIL_UNUSED_PARM(data);
2315 RIL_UNUSED_PARM(datalen);
2316
2317 muteResponse = 0; // Mute disabled
2318
2319 RIL_onRequestComplete(t, RIL_E_SUCCESS, &muteResponse, sizeof(muteResponse));
2320}
Etan Cohend3652192014-06-20 08:28:44 -07002321
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002322/*** Callback methods from the RIL library to us ***/
2323
2324/**
2325 * Call from RIL to us to make a RIL_REQUEST
2326 *
2327 * Must be completed with a call to RIL_onRequestComplete()
2328 *
2329 * RIL_onRequestComplete() may be called from any thread, before or after
2330 * this function returns.
2331 *
Weilun Du9f471e22017-02-07 10:47:19 -08002332 * Because onRequest function could be called from multiple different thread,
2333 * we must ensure that the underlying at_send_command_* function
2334 * is atomic.
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002335 */
2336static void
2337onRequest (int request, void *data, size_t datalen, RIL_Token t)
2338{
2339 ATResponse *p_response;
2340 int err;
2341
Wink Saville4dcab4f2012-11-19 16:05:13 -08002342 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002343
2344 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2345 * when RADIO_STATE_UNAVAILABLE.
2346 */
2347 if (sState == RADIO_STATE_UNAVAILABLE
2348 && request != RIL_REQUEST_GET_SIM_STATUS
2349 ) {
2350 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2351 return;
2352 }
2353
2354 /* Ignore all non-power requests when RADIO_STATE_OFF
2355 * (except RIL_REQUEST_GET_SIM_STATUS)
2356 */
Jim Kayed2d82012017-10-06 14:17:47 -07002357 if (sState == RADIO_STATE_OFF) {
2358 switch(request) {
2359 case RIL_REQUEST_BASEBAND_VERSION:
2360 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2361 case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:
2362 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2363 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2364 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2365 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2366 case RIL_REQUEST_DEVICE_IDENTITY:
2367 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2368 case RIL_REQUEST_GET_ACTIVITY_INFO:
2369 case RIL_REQUEST_GET_CARRIER_RESTRICTIONS:
2370 case RIL_REQUEST_GET_CURRENT_CALLS:
2371 case RIL_REQUEST_GET_IMEI:
2372 case RIL_REQUEST_GET_MUTE:
2373 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2374 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2375 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2376 case RIL_REQUEST_GET_SIM_STATUS:
2377 case RIL_REQUEST_NV_RESET_CONFIG:
2378 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2379 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2380 case RIL_REQUEST_QUERY_TTY_MODE:
2381 case RIL_REQUEST_RADIO_POWER:
2382 case RIL_REQUEST_SET_BAND_MODE:
2383 case RIL_REQUEST_SET_CARRIER_RESTRICTIONS:
2384 case RIL_REQUEST_SET_LOCATION_UPDATES:
2385 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2386 case RIL_REQUEST_SET_TTY_MODE:
2387 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2388 case RIL_REQUEST_STOP_LCE:
2389 case RIL_REQUEST_VOICE_RADIO_TECH:
2390 // Process all the above, even though the radio is off
2391 break;
2392
2393 default:
2394 // For all others, say NOT_AVAILABLE because the radio is off
2395 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2396 return;
2397 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002398 }
2399
2400 switch (request) {
2401 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002402 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002403 char *p_buffer;
2404 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002405
Wink Savillef6aa7c12009-04-30 14:20:52 -07002406 int result = getCardStatus(&p_card_status);
2407 if (result == RIL_E_SUCCESS) {
2408 p_buffer = (char *)p_card_status;
2409 buffer_size = sizeof(*p_card_status);
2410 } else {
2411 p_buffer = NULL;
2412 buffer_size = 0;
2413 }
2414 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2415 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002416 break;
2417 }
2418 case RIL_REQUEST_GET_CURRENT_CALLS:
2419 requestGetCurrentCalls(data, datalen, t);
2420 break;
2421 case RIL_REQUEST_DIAL:
2422 requestDial(data, datalen, t);
2423 break;
2424 case RIL_REQUEST_HANGUP:
2425 requestHangup(data, datalen, t);
2426 break;
2427 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002428 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002429 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002430 case RIL_REQUEST_CONFERENCE:
2431 case RIL_REQUEST_UDUB:
2432 requestCallSelection(data, datalen, t, request);
2433 break;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002434 case RIL_REQUEST_ANSWER:
2435 at_send_command("ATA", NULL);
2436
2437#ifdef WORKAROUND_ERRONEOUS_ANSWER
2438 s_expectAnswer = 1;
2439#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2440
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002441 if (getSIMStatus() != SIM_READY) {
2442 RIL_onRequestComplete(t, RIL_E_MODEM_ERR, NULL, 0);
2443 } else {
2444 // Success or failure is ignored by the upper layer here.
2445 // It will call GET_CURRENT_CALLS and determine success that way.
2446 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2447 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002448 break;
2449
2450 case RIL_REQUEST_SEPARATE_CONNECTION:
2451 {
2452 char cmd[12];
2453 int party = ((int*)data)[0];
2454
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002455 if (getSIMStatus() == SIM_ABSENT) {
2456 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2457 return;
2458 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002459 // Make sure that party is in a valid range.
2460 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2461 // It's sufficient for us to just make sure it's single digit.)
2462 if (party > 0 && party < 10) {
2463 sprintf(cmd, "AT+CHLD=2%d", party);
2464 at_send_command(cmd, NULL);
2465 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2466 } else {
2467 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2468 }
2469 }
2470 break;
2471
2472 case RIL_REQUEST_SIGNAL_STRENGTH:
2473 requestSignalStrength(data, datalen, t);
2474 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002475 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2476 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002477 requestRegistrationState(request, data, datalen, t);
2478 break;
2479 case RIL_REQUEST_OPERATOR:
2480 requestOperator(data, datalen, t);
2481 break;
2482 case RIL_REQUEST_RADIO_POWER:
2483 requestRadioPower(data, datalen, t);
2484 break;
2485 case RIL_REQUEST_DTMF: {
2486 char c = ((char *)data)[0];
2487 char *cmd;
2488 asprintf(&cmd, "AT+VTS=%c", (int)c);
2489 at_send_command(cmd, NULL);
2490 free(cmd);
2491 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2492 break;
2493 }
2494 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302495 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002496 requestSendSMS(data, datalen, t);
2497 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002498 case RIL_REQUEST_CDMA_SEND_SMS:
2499 requestCdmaSendSMS(data, datalen, t);
2500 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002501 case RIL_REQUEST_IMS_SEND_SMS:
2502 requestImsSendSMS(data, datalen, t);
2503 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002504 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2505 requestSimOpenChannel(data, datalen, t);
2506 break;
2507 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2508 requestSimCloseChannel(data, datalen, t);
2509 break;
2510 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2511 requestSimTransmitApduChannel(data, datalen, t);
2512 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002513 case RIL_REQUEST_SETUP_DATA_CALL:
2514 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002515 break;
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -07002516 case RIL_REQUEST_DEACTIVATE_DATA_CALL:
2517 requestDeactivateDataCall(t);
2518 break;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002519 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2520 requestSMSAcknowledge(data, datalen, t);
2521 break;
2522
2523 case RIL_REQUEST_GET_IMSI:
2524 p_response = NULL;
2525 err = at_send_command_numeric("AT+CIMI", &p_response);
2526
2527 if (err < 0 || p_response->success == 0) {
2528 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2529 } else {
2530 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2531 p_response->p_intermediates->line, sizeof(char *));
2532 }
2533 at_response_free(p_response);
2534 break;
2535
2536 case RIL_REQUEST_GET_IMEI:
2537 p_response = NULL;
2538 err = at_send_command_numeric("AT+CGSN", &p_response);
2539
2540 if (err < 0 || p_response->success == 0) {
2541 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2542 } else {
2543 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2544 p_response->p_intermediates->line, sizeof(char *));
2545 }
2546 at_response_free(p_response);
2547 break;
2548
2549 case RIL_REQUEST_SIM_IO:
2550 requestSIM_IO(data,datalen,t);
2551 break;
2552
2553 case RIL_REQUEST_SEND_USSD:
2554 requestSendUSSD(data, datalen, t);
2555 break;
2556
2557 case RIL_REQUEST_CANCEL_USSD:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002558 if (getSIMStatus() == SIM_ABSENT) {
2559 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2560 return;
2561 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002562 p_response = NULL;
2563 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2564
2565 if (err < 0 || p_response->success == 0) {
2566 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2567 } else {
2568 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2569 p_response->p_intermediates->line, sizeof(char *));
2570 }
2571 at_response_free(p_response);
2572 break;
2573
2574 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002575 if (getSIMStatus() == SIM_ABSENT) {
2576 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2577 } else {
2578 at_send_command("AT+COPS=0", NULL);
2579 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002580 break;
2581
Wink Savillef4c4d362009-04-02 01:37:03 -07002582 case RIL_REQUEST_DATA_CALL_LIST:
2583 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002584 break;
2585
2586 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2587 requestQueryNetworkSelectionMode(data, datalen, t);
2588 break;
2589
2590 case RIL_REQUEST_OEM_HOOK_RAW:
2591 // echo back data
2592 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2593 break;
2594
2595
2596 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2597 int i;
2598 const char ** cur;
2599
Wink Saville4dcab4f2012-11-19 16:05:13 -08002600 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002601
2602
2603 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2604 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002605 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002606 }
2607
2608 // echo back strings
2609 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2610 break;
2611 }
2612
2613 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2614 requestWriteSmsToSim(data, datalen, t);
2615 break;
2616
2617 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2618 char * cmd;
2619 p_response = NULL;
2620 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2621 err = at_send_command(cmd, &p_response);
2622 free(cmd);
2623 if (err < 0 || p_response->success == 0) {
2624 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2625 } else {
2626 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2627 }
2628 at_response_free(p_response);
2629 break;
2630 }
2631
2632 case RIL_REQUEST_ENTER_SIM_PIN:
2633 case RIL_REQUEST_ENTER_SIM_PUK:
2634 case RIL_REQUEST_ENTER_SIM_PIN2:
2635 case RIL_REQUEST_ENTER_SIM_PUK2:
2636 case RIL_REQUEST_CHANGE_SIM_PIN:
2637 case RIL_REQUEST_CHANGE_SIM_PIN2:
2638 requestEnterSimPin(data, datalen, t);
2639 break;
2640
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002641 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2642 int reply[2];
2643 //0==unregistered, 1==registered
2644 reply[0] = s_ims_registered;
2645
2646 //to be used when changed to include service supporated info
2647 //reply[1] = s_ims_services;
2648
2649 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2650 reply[1] = s_ims_format;
2651
2652 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2653 reply[0], reply[1]);
2654 if (reply[1] != -1) {
2655 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2656 } else {
2657 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2658 }
2659 break;
2660 }
2661
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002662 case RIL_REQUEST_VOICE_RADIO_TECH:
2663 {
2664 int tech = techFromModemType(TECH(sMdmInfo));
2665 if (tech < 0 )
2666 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2667 else
2668 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2669 }
2670 break;
2671 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2672 requestSetPreferredNetworkType(request, data, datalen, t);
2673 break;
2674
2675 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2676 requestGetPreferredNetworkType(request, data, datalen, t);
2677 break;
2678
Jun Tian58027012013-07-30 11:07:22 +08002679 case RIL_REQUEST_GET_CELL_INFO_LIST:
2680 requestGetCellInfoList(data, datalen, t);
2681 break;
2682
2683 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2684 requestSetCellInfoListRate(data, datalen, t);
2685 break;
2686
Etan Cohend3652192014-06-20 08:28:44 -07002687 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2688 requestGetHardwareConfig(data, datalen, t);
2689 break;
2690
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002691 case RIL_REQUEST_SHUTDOWN:
2692 requestShutdown(t);
2693 break;
2694
Jim Kayed2d82012017-10-06 14:17:47 -07002695 case RIL_REQUEST_QUERY_TTY_MODE:
2696 requestGetTtyMode(data, datalen, t);
2697 break;
2698
2699 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2700 requestGetRadioCapability(data, datalen, t);
2701 break;
2702
2703 case RIL_REQUEST_GET_MUTE:
2704 requestGetMute(data, datalen, t);
2705 break;
2706
2707 case RIL_REQUEST_SET_INITIAL_ATTACH_APN:
2708 case RIL_REQUEST_ALLOW_DATA:
2709 case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION:
2710 case RIL_REQUEST_SET_CLIR:
2711 case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION:
2712 case RIL_REQUEST_SET_BAND_MODE:
2713 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2714 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2715 case RIL_REQUEST_SET_LOCATION_UPDATES:
2716 case RIL_REQUEST_SET_TTY_MODE:
2717 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2718 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2719 break;
2720
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002721 case RIL_REQUEST_BASEBAND_VERSION:
Jim Kayed2d82012017-10-06 14:17:47 -07002722 requestCdmaBaseBandVersion(request, data, datalen, t);
2723 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002724
2725 case RIL_REQUEST_DEVICE_IDENTITY:
Jim Kayed2d82012017-10-06 14:17:47 -07002726 requestCdmaDeviceIdentity(request, data, datalen, t);
2727 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002728
2729 case RIL_REQUEST_CDMA_SUBSCRIPTION:
Jim Kayed2d82012017-10-06 14:17:47 -07002730 requestCdmaSubscription(request, data, datalen, t);
2731 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002732
Jim Kayed2d82012017-10-06 14:17:47 -07002733 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2734 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2735 break;
2736
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002737 case RIL_REQUEST_START_LCE:
2738 case RIL_REQUEST_STOP_LCE:
2739 case RIL_REQUEST_PULL_LCEDATA:
2740 if (getSIMStatus() == SIM_ABSENT) {
2741 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
2742 } else {
2743 RIL_onRequestComplete(t, RIL_E_LCE_NOT_SUPPORTED, NULL, 0);
2744 }
2745 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002746
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002747 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2748 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2749 requestCdmaGetRoamingPreference(request, data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002750 } else {
2751 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2752 }
2753 break;
2754
2755 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2756 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2757 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2758 } else {
2759 // VTS tests expect us to silently do nothing
2760 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2761 }
2762 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002763
2764 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2765 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2766 requestCdmaSetRoamingPreference(request, data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002767 } else {
2768 // VTS tests expect us to silently do nothing
2769 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2770 }
2771 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002772
2773 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2774 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2775 requestExitEmergencyMode(data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002776 } else {
2777 // VTS tests expect us to silently do nothing
2778 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2779 }
Jim Kayed2d82012017-10-06 14:17:47 -07002780 break;
2781
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002782 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002783 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002784 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2785 break;
2786 }
2787}
2788
2789/**
2790 * Synchronous call from the RIL to us to return current radio state.
2791 * RADIO_STATE_UNAVAILABLE should be the initial state.
2792 */
2793static RIL_RadioState
2794currentState()
2795{
2796 return sState;
2797}
2798/**
2799 * Call from RIL to us to find out whether a specific request code
2800 * is supported by this implementation.
2801 *
2802 * Return 1 for "supported" and 0 for "unsupported"
2803 */
2804
2805static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002806onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002807{
2808 //@@@ todo
2809
2810 return 1;
2811}
2812
Mark Salyzynba58c202014-03-12 15:20:22 -07002813static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002814{
2815 //@@@todo
2816
2817}
2818
2819static const char * getVersion(void)
2820{
2821 return "android reference-ril 1.0";
2822}
2823
2824static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002825setRadioTechnology(ModemInfo *mdm, int newtech)
2826{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002827 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002828
2829 int oldtech = TECH(mdm);
2830
2831 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002832 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002833 TECH(mdm) = newtech;
2834 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2835 int tech = techFromModemType(TECH(sMdmInfo));
2836 if (tech > 0 ) {
2837 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2838 &tech, sizeof(tech));
2839 }
2840 }
2841 }
2842}
2843
2844static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002845setRadioState(RIL_RadioState newState)
2846{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002847 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002848 RIL_RadioState oldState;
2849
2850 pthread_mutex_lock(&s_state_mutex);
2851
2852 oldState = sState;
2853
2854 if (s_closed > 0) {
2855 // If we're closed, the only reasonable state is
2856 // RADIO_STATE_UNAVAILABLE
2857 // This is here because things on the main thread
2858 // may attempt to change the radio state after the closed
2859 // event happened in another thread
2860 newState = RADIO_STATE_UNAVAILABLE;
2861 }
2862
2863 if (sState != newState || s_closed > 0) {
2864 sState = newState;
2865
2866 pthread_cond_broadcast (&s_state_cond);
2867 }
2868
2869 pthread_mutex_unlock(&s_state_mutex);
2870
2871
2872 /* do these outside of the mutex */
2873 if (sState != oldState) {
2874 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2875 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002876 // Sim state can change as result of radio state change
2877 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2878 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002879
2880 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2881 * from the AT reader thread
2882 * Currently, this doesn't happen, but if that changes then these
2883 * will need to be dispatched on the request thread
2884 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002885 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002886 onRadioPowerOn();
2887 }
2888 }
2889}
2890
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002891/** Returns RUIM_NOT_READY on error */
2892static SIM_Status
2893getRUIMStatus()
2894{
2895 ATResponse *p_response = NULL;
2896 int err;
2897 int ret;
2898 char *cpinLine;
2899 char *cpinResult;
2900
2901 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2902 ret = SIM_NOT_READY;
2903 goto done;
2904 }
2905
2906 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2907
2908 if (err != 0) {
2909 ret = SIM_NOT_READY;
2910 goto done;
2911 }
2912
2913 switch (at_get_cme_error(p_response)) {
2914 case CME_SUCCESS:
2915 break;
2916
2917 case CME_SIM_NOT_INSERTED:
2918 ret = SIM_ABSENT;
2919 goto done;
2920
2921 default:
2922 ret = SIM_NOT_READY;
2923 goto done;
2924 }
2925
2926 /* CPIN? has succeeded, now look at the result */
2927
2928 cpinLine = p_response->p_intermediates->line;
2929 err = at_tok_start (&cpinLine);
2930
2931 if (err < 0) {
2932 ret = SIM_NOT_READY;
2933 goto done;
2934 }
2935
2936 err = at_tok_nextstr(&cpinLine, &cpinResult);
2937
2938 if (err < 0) {
2939 ret = SIM_NOT_READY;
2940 goto done;
2941 }
2942
2943 if (0 == strcmp (cpinResult, "SIM PIN")) {
2944 ret = SIM_PIN;
2945 goto done;
2946 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2947 ret = SIM_PUK;
2948 goto done;
2949 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2950 return SIM_NETWORK_PERSONALIZATION;
2951 } else if (0 != strcmp (cpinResult, "READY")) {
2952 /* we're treating unsupported lock types as "sim absent" */
2953 ret = SIM_ABSENT;
2954 goto done;
2955 }
2956
2957 at_response_free(p_response);
2958 p_response = NULL;
2959 cpinResult = NULL;
2960
2961 ret = SIM_READY;
2962
2963done:
2964 at_response_free(p_response);
2965 return ret;
2966}
2967
John Wang309ac292009-07-30 14:53:23 -07002968/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002969static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002970getSIMStatus()
2971{
2972 ATResponse *p_response = NULL;
2973 int err;
2974 int ret;
2975 char *cpinLine;
2976 char *cpinResult;
2977
Wink Saville4dcab4f2012-11-19 16:05:13 -08002978 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002979 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2980
2981 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002982 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002983 goto done;
2984 }
2985
2986 switch (at_get_cme_error(p_response)) {
2987 case CME_SUCCESS:
2988 break;
2989
2990 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002991 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002992 goto done;
2993
2994 default:
John Wang309ac292009-07-30 14:53:23 -07002995 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002996 goto done;
2997 }
2998
2999 /* CPIN? has succeeded, now look at the result */
3000
3001 cpinLine = p_response->p_intermediates->line;
3002 err = at_tok_start (&cpinLine);
3003
3004 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07003005 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003006 goto done;
3007 }
3008
3009 err = at_tok_nextstr(&cpinLine, &cpinResult);
3010
3011 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07003012 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003013 goto done;
3014 }
3015
3016 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07003017 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003018 goto done;
3019 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07003020 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003021 goto done;
3022 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07003023 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003024 } else if (0 != strcmp (cpinResult, "READY")) {
3025 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07003026 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003027 goto done;
3028 }
3029
3030 at_response_free(p_response);
3031 p_response = NULL;
3032 cpinResult = NULL;
3033
Jim Kayed2d82012017-10-06 14:17:47 -07003034 ret = (sState == RADIO_STATE_ON) ? SIM_READY : SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003035
3036done:
3037 at_response_free(p_response);
3038 return ret;
3039}
3040
3041
3042/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07003043 * Get the current card status.
3044 *
3045 * This must be freed using freeCardStatus.
3046 * @return: On success returns RIL_E_SUCCESS
3047 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003048static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003049 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07003050 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07003051 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3052 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003053 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07003054 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3055 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003056 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07003057 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3058 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003059 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07003060 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3061 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003062 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07003063 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3064 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003065 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07003066 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003067 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3068 // RUIM_ABSENT = 6
3069 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3070 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3071 // RUIM_NOT_READY = 7
3072 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3073 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3074 // RUIM_READY = 8
3075 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3076 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3077 // RUIM_PIN = 9
3078 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3079 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3080 // RUIM_PUK = 10
3081 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3082 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
3083 // RUIM_NETWORK_PERSONALIZATION = 11
3084 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
bohu076e6872017-07-02 21:33:28 -07003085 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3086 // ISIM_ABSENT = 12
3087 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3088 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3089 // ISIM_NOT_READY = 13
3090 { RIL_APPTYPE_ISIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3091 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3092 // ISIM_READY = 14
3093 { RIL_APPTYPE_ISIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3094 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3095 // ISIM_PIN = 15
3096 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3097 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3098 // ISIM_PUK = 16
3099 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3100 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
3101 // ISIM_NETWORK_PERSONALIZATION = 17
3102 { RIL_APPTYPE_ISIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
3103 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3104
Wink Savillef6aa7c12009-04-30 14:20:52 -07003105 };
3106 RIL_CardState card_state;
3107 int num_apps;
3108
3109 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07003110 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003111 card_state = RIL_CARDSTATE_ABSENT;
3112 num_apps = 0;
3113 } else {
3114 card_state = RIL_CARDSTATE_PRESENT;
bohu076e6872017-07-02 21:33:28 -07003115 num_apps = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003116 }
3117
3118 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003119 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07003120 p_card_status->card_state = card_state;
3121 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
kun.tang7bb00222017-07-12 11:41:43 +08003122 p_card_status->gsm_umts_subscription_app_index = -1;
3123 p_card_status->cdma_subscription_app_index = -1;
3124 p_card_status->ims_subscription_app_index = -1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003125 p_card_status->num_applications = num_apps;
3126
3127 // Initialize application status
3128 int i;
3129 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07003130 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07003131 }
3132
3133 // Pickup the appropriate application status
3134 // that reflects sim_status for gsm.
3135 if (num_apps != 0) {
bohu076e6872017-07-02 21:33:28 -07003136 p_card_status->num_applications = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003137 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003138 p_card_status->cdma_subscription_app_index = 1;
bohu076e6872017-07-02 21:33:28 -07003139 p_card_status->ims_subscription_app_index = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003140
3141 // Get the correct app status
3142 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003143 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
bohu076e6872017-07-02 21:33:28 -07003144 p_card_status->applications[2] = app_status_array[sim_status + ISIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07003145 }
3146
3147 *pp_card_status = p_card_status;
3148 return RIL_E_SUCCESS;
3149}
3150
3151/**
3152 * Free the card status returned by getCardStatus
3153 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003154static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003155 free(p_card_status);
3156}
3157
3158/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003159 * SIM ready means any commands that access the SIM will work, including:
3160 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
3161 * (all SMS-related commands)
3162 */
3163
Mark Salyzynba58c202014-03-12 15:20:22 -07003164static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003165{
3166 ATResponse *p_response;
3167 int ret;
3168
Naveen Kalla2baf7232016-10-11 13:49:20 -07003169 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003170 // no longer valid to poll
3171 return;
3172 }
3173
3174 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07003175 case SIM_ABSENT:
3176 case SIM_PIN:
3177 case SIM_PUK:
3178 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003179 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003180 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003181 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003182 return;
3183
John Wang309ac292009-07-30 14:53:23 -07003184 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003185 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
3186 return;
3187
John Wang309ac292009-07-30 14:53:23 -07003188 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003189 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003190 onSIMReady();
3191 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003192 return;
3193 }
3194}
3195
3196/** returns 1 if on, 0 if off, and -1 on error */
3197static int isRadioOn()
3198{
3199 ATResponse *p_response = NULL;
3200 int err;
3201 char *line;
3202 char ret;
3203
3204 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
3205
3206 if (err < 0 || p_response->success == 0) {
3207 // assume radio is off
3208 goto error;
3209 }
3210
3211 line = p_response->p_intermediates->line;
3212
3213 err = at_tok_start(&line);
3214 if (err < 0) goto error;
3215
3216 err = at_tok_nextbool(&line, &ret);
3217 if (err < 0) goto error;
3218
3219 at_response_free(p_response);
3220
3221 return (int)ret;
3222
3223error:
3224
3225 at_response_free(p_response);
3226 return -1;
3227}
3228
3229/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003230 * Parse the response generated by a +CTEC AT command
3231 * The values read from the response are stored in current and preferred.
3232 * Both current and preferred may be null. The corresponding value is ignored in that case.
3233 *
3234 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
3235 * 1 if the response includes the current technology only
3236 * 0 if the response includes both current technology and preferred mode
3237 */
3238int parse_technology_response( const char *response, int *current, int32_t *preferred )
3239{
3240 int err;
3241 char *line, *p;
3242 int ct;
3243 int32_t pt = 0;
3244 char *str_pt;
3245
3246 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08003247 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003248 err = at_tok_start(&p);
3249 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003250 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003251 free(line);
3252 return -1;
3253 }
3254
3255 err = at_tok_nextint(&p, &ct);
3256 if (err) {
3257 free(line);
3258 return -1;
3259 }
3260 if (current) *current = ct;
3261
Wink Saville4dcab4f2012-11-19 16:05:13 -08003262 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003263
3264 err = at_tok_nexthexint(&p, &pt);
3265 if (err) {
3266 free(line);
3267 return 1;
3268 }
3269 if (preferred) {
3270 *preferred = pt;
3271 }
3272 free(line);
3273
3274 return 0;
3275}
3276
Mark Salyzynba58c202014-03-12 15:20:22 -07003277int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003278{
3279 ATResponse *p_response;
3280 int err, val, techs = 0;
3281 char *tok;
3282 char *line;
3283
Wink Saville4dcab4f2012-11-19 16:05:13 -08003284 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003285 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3286 if (err || !p_response->success)
3287 goto error;
3288 line = p_response->p_intermediates->line;
3289 err = at_tok_start(&line);
3290 if (err || !at_tok_hasmore(&line))
3291 goto error;
3292 while (!at_tok_nextint(&line, &val)) {
3293 techs |= ( 1 << val );
3294 }
3295 if (supported) *supported = techs;
3296 return 0;
3297error:
3298 at_response_free(p_response);
3299 return -1;
3300}
3301
3302/**
3303 * query_ctec. Send the +CTEC AT command to the modem to query the current
3304 * and preferred modes. It leaves values in the addresses pointed to by
3305 * current and preferred. If any of those pointers are NULL, the corresponding value
3306 * is ignored, but the return value will still reflect if retreiving and parsing of the
3307 * values suceeded.
3308 *
3309 * @mdm Currently unused
3310 * @current A pointer to store the current mode returned by the modem. May be null.
3311 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3312 * @return -1 on error (or failure to parse)
3313 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3314 * 0 if both current and preferred were returned correctly
3315 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003316int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003317{
3318 ATResponse *response = NULL;
3319 int err;
3320 int res;
3321
Colin Cross5cba4882014-02-05 18:55:42 -08003322 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003323 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3324 if (!err && response->success) {
3325 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3326 at_response_free(response);
3327 return res;
3328 }
Colin Cross5cba4882014-02-05 18:55:42 -08003329 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003330 at_response_free(response);
3331 return -1;
3332}
3333
3334int is_multimode_modem(ModemInfo *mdm)
3335{
3336 ATResponse *response;
3337 int err;
3338 char *line;
3339 int tech;
3340 int32_t preferred;
3341
3342 if (query_ctec(mdm, &tech, &preferred) == 0) {
3343 mdm->currentTech = tech;
3344 mdm->preferredNetworkMode = preferred;
3345 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3346 return 0;
3347 }
3348 return 1;
3349 }
3350 return 0;
3351}
3352
3353/**
3354 * Find out if our modem is GSM, CDMA or both (Multimode)
3355 */
3356static void probeForModemMode(ModemInfo *info)
3357{
3358 ATResponse *response;
3359 int err;
3360 assert (info);
3361 // Currently, our only known multimode modem is qemu's android modem,
3362 // which implements the AT+CTEC command to query and set mode.
3363 // Try that first
3364
3365 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003366 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003367 info->supportedTechs, info->currentTech);
3368 return;
3369 }
3370
3371 /* Being here means that our modem is not multimode */
3372 info->isMultimode = 0;
3373
3374 /* CDMA Modems implement the AT+WNAM command */
3375 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3376 if (!err && response->success) {
3377 at_response_free(response);
3378 // TODO: find out if we really support EvDo
3379 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3380 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003381 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003382 return;
3383 }
3384 if (!err) at_response_free(response);
3385 // TODO: find out if modem really supports WCDMA/LTE
3386 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3387 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003388 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003389}
3390
3391/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003392 * Initialize everything that can be configured while we're still in
3393 * AT+CFUN=0
3394 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003395static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003396{
3397 ATResponse *p_response = NULL;
3398 int err;
3399
3400 setRadioState (RADIO_STATE_OFF);
3401
3402 at_handshake();
3403
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003404 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003405 /* note: we don't check errors here. Everything important will
3406 be handled in onATTimeout and onATReaderClosed */
3407
3408 /* atchannel is tolerant of echo but it must */
3409 /* have verbose result codes */
3410 at_send_command("ATE0Q0V1", NULL);
3411
3412 /* No auto-answer */
3413 at_send_command("ATS0=0", NULL);
3414
3415 /* Extended errors */
3416 at_send_command("AT+CMEE=1", NULL);
3417
3418 /* Network registration events */
3419 err = at_send_command("AT+CREG=2", &p_response);
3420
3421 /* some handsets -- in tethered mode -- don't support CREG=2 */
3422 if (err < 0 || p_response->success == 0) {
3423 at_send_command("AT+CREG=1", NULL);
3424 }
3425
3426 at_response_free(p_response);
3427
3428 /* GPRS registration events */
3429 at_send_command("AT+CGREG=1", NULL);
3430
3431 /* Call Waiting notifications */
3432 at_send_command("AT+CCWA=1", NULL);
3433
3434 /* Alternating voice/data off */
3435 at_send_command("AT+CMOD=0", NULL);
3436
3437 /* Not muted */
3438 at_send_command("AT+CMUT=0", NULL);
3439
3440 /* +CSSU unsolicited supp service notifications */
3441 at_send_command("AT+CSSN=0,1", NULL);
3442
3443 /* no connected line identification */
3444 at_send_command("AT+COLP=0", NULL);
3445
3446 /* HEX character set */
3447 at_send_command("AT+CSCS=\"HEX\"", NULL);
3448
3449 /* USSD unsolicited */
3450 at_send_command("AT+CUSD=1", NULL);
3451
3452 /* Enable +CGEV GPRS event notifications, but don't buffer */
3453 at_send_command("AT+CGEREP=1,0", NULL);
3454
3455 /* SMS PDU mode */
3456 at_send_command("AT+CMGF=0", NULL);
3457
3458#ifdef USE_TI_COMMANDS
3459
3460 at_send_command("AT%CPI=3", NULL);
3461
3462 /* TI specific -- notifications when SMS is ready (currently ignored) */
3463 at_send_command("AT%CSTAT=1", NULL);
3464
3465#endif /* USE_TI_COMMANDS */
3466
3467
3468 /* assume radio is off on error */
3469 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003470 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003471 }
3472}
3473
3474static void waitForClose()
3475{
3476 pthread_mutex_lock(&s_state_mutex);
3477
3478 while (s_closed == 0) {
3479 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3480 }
3481
3482 pthread_mutex_unlock(&s_state_mutex);
3483}
3484
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003485static void sendUnsolImsNetworkStateChanged()
3486{
3487#if 0 // to be used when unsol is changed to return data.
3488 int reply[2];
3489 reply[0] = s_ims_registered;
3490 reply[1] = s_ims_services;
3491 reply[1] = s_ims_format;
3492#endif
3493 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3494 NULL, 0);
3495}
3496
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003497/**
3498 * Called by atchannel when an unsolicited line appears
3499 * This is called on atchannel's reader thread. AT commands may
3500 * not be issued here
3501 */
3502static void onUnsolicited (const char *s, const char *sms_pdu)
3503{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003504 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003505 int err;
3506
3507 /* Ignore unsolicited responses until we're initialized.
3508 * This is OK because the RIL library will poll for initial state
3509 */
3510 if (sState == RADIO_STATE_UNAVAILABLE) {
3511 return;
3512 }
3513
3514 if (strStartsWith(s, "%CTZV:")) {
3515 /* TI specific -- NITZ time */
3516 char *response;
3517
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003518 line = p = strdup(s);
3519 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003520
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003521 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003522
3523 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003524 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003525 } else {
3526 RIL_onUnsolicitedResponse (
3527 RIL_UNSOL_NITZ_TIME_RECEIVED,
3528 response, strlen(response));
3529 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003530 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003531 } else if (strStartsWith(s,"+CRING:")
3532 || strStartsWith(s,"RING")
3533 || strStartsWith(s,"NO CARRIER")
3534 || strStartsWith(s,"+CCWA")
3535 ) {
3536 RIL_onUnsolicitedResponse (
3537 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3538 NULL, 0);
3539#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003540 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003541#endif /* WORKAROUND_FAKE_CGEV */
3542 } else if (strStartsWith(s,"+CREG:")
3543 || strStartsWith(s,"+CGREG:")
3544 ) {
3545 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003546 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003547 NULL, 0);
3548#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003549 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003550#endif /* WORKAROUND_FAKE_CGEV */
3551 } else if (strStartsWith(s, "+CMT:")) {
3552 RIL_onUnsolicitedResponse (
3553 RIL_UNSOL_RESPONSE_NEW_SMS,
3554 sms_pdu, strlen(sms_pdu));
3555 } else if (strStartsWith(s, "+CDS:")) {
3556 RIL_onUnsolicitedResponse (
3557 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3558 sms_pdu, strlen(sms_pdu));
3559 } else if (strStartsWith(s, "+CGEV:")) {
3560 /* Really, we can ignore NW CLASS and ME CLASS events here,
3561 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003562 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003563 */
3564 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003565 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003566#ifdef WORKAROUND_FAKE_CGEV
3567 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003568 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003569#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003570 } else if (strStartsWith(s, "+CTEC: ")) {
3571 int tech, mask;
3572 switch (parse_technology_response(s, &tech, NULL))
3573 {
3574 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003575 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003576 break;
3577 case 1: // current mode correctly parsed
3578 case 0: // preferred mode correctly parsed
3579 mask = 1 << tech;
3580 if (mask != MDM_GSM && mask != MDM_CDMA &&
3581 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003582 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003583 } else {
3584 setRadioTechnology(sMdmInfo, tech);
3585 }
3586 break;
3587 }
3588 } else if (strStartsWith(s, "+CCSS: ")) {
3589 int source = 0;
3590 line = p = strdup(s);
3591 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003592 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003593 return;
3594 }
3595 if (at_tok_start(&p) < 0) {
3596 free(line);
3597 return;
3598 }
3599 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003600 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003601 free(line);
3602 return;
3603 }
3604 SSOURCE(sMdmInfo) = source;
3605 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3606 &source, sizeof(source));
3607 } else if (strStartsWith(s, "+WSOS: ")) {
3608 char state = 0;
3609 int unsol;
3610 line = p = strdup(s);
3611 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003612 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003613 return;
3614 }
3615 if (at_tok_start(&p) < 0) {
3616 free(line);
3617 return;
3618 }
3619 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003620 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003621 free(line);
3622 return;
3623 }
3624 free(line);
3625
3626 unsol = state ?
3627 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3628
3629 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3630
3631 } else if (strStartsWith(s, "+WPRL: ")) {
3632 int version = -1;
3633 line = p = strdup(s);
3634 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003635 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003636 return;
3637 }
3638 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003639 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003640 free(line);
3641 return;
3642 }
3643 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003644 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003645 free(line);
3646 return;
3647 }
3648 free(line);
3649 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3650 } else if (strStartsWith(s, "+CFUN: 0")) {
3651 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003652 }
3653}
3654
3655/* Called on command or reader thread */
3656static void onATReaderClosed()
3657{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003658 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003659 at_close();
3660 s_closed = 1;
3661
3662 setRadioState (RADIO_STATE_UNAVAILABLE);
3663}
3664
3665/* Called on command thread */
3666static void onATTimeout()
3667{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003668 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003669 at_close();
3670
3671 s_closed = 1;
3672
3673 /* FIXME cause a radio reset here */
3674
3675 setRadioState (RADIO_STATE_UNAVAILABLE);
3676}
3677
Etan Cohend3652192014-06-20 08:28:44 -07003678/* Called to pass hardware configuration information to telephony
3679 * framework.
3680 */
3681static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3682{
3683 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3684}
3685
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003686static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003687{
3688#ifdef RIL_SHLIB
3689 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3690#else
3691 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3692 exit(-1);
3693#endif
3694}
3695
3696static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003697mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003698{
3699 int fd;
3700 int ret;
3701
3702 AT_DUMP("== ", "entering mainLoop()", -1 );
3703 at_set_on_reader_closed(onATReaderClosed);
3704 at_set_on_timeout(onATTimeout);
3705
3706 for (;;) {
3707 fd = -1;
3708 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003709 if (isInEmulator()) {
3710 fd = qemu_pipe_open("pipe:qemud:gsm");
3711 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003712 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003713 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003714 fd = socket_local_client(s_device_path,
3715 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3716 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003717 } else if (s_device_path != NULL) {
3718 fd = open (s_device_path, O_RDWR);
3719 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3720 /* disable echo on serial ports */
3721 struct termios ios;
3722 tcgetattr( fd, &ios );
3723 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3724 tcsetattr( fd, TCSANOW, &ios );
3725 }
3726 }
3727
3728 if (fd < 0) {
3729 perror ("opening AT interface. retrying...");
3730 sleep(10);
3731 /* never returns */
3732 }
3733 }
3734
3735 s_closed = 0;
3736 ret = at_open(fd, onUnsolicited);
3737
3738 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003739 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003740 return 0;
3741 }
3742
3743 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3744
3745 // Give initializeCallback a chance to dispatched, since
3746 // we don't presently have a cancellation mechanism
3747 sleep(1);
3748
3749 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003750 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003751 }
3752}
3753
3754#ifdef RIL_SHLIB
3755
3756pthread_t s_tid_mainloop;
3757
3758const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3759{
3760 int ret;
3761 int fd = -1;
3762 int opt;
3763 pthread_attr_t attr;
3764
3765 s_rilenv = env;
3766
Sandeep Gutta11f27942014-07-10 05:00:25 +05303767 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003768 switch (opt) {
3769 case 'p':
3770 s_port = atoi(optarg);
3771 if (s_port == 0) {
3772 usage(argv[0]);
3773 return NULL;
3774 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003775 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003776 break;
3777
3778 case 'd':
3779 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003780 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003781 break;
3782
3783 case 's':
3784 s_device_path = optarg;
3785 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003786 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003787 break;
3788
Sandeep Gutta11f27942014-07-10 05:00:25 +05303789 case 'c':
3790 RLOGI("Client id received %s\n", optarg);
3791 break;
3792
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003793 default:
3794 usage(argv[0]);
3795 return NULL;
3796 }
3797 }
3798
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003799 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003800 usage(argv[0]);
3801 return NULL;
3802 }
3803
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003804 sMdmInfo = calloc(1, sizeof(ModemInfo));
3805 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003806 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003807 return NULL;
3808 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003809 pthread_attr_init (&attr);
3810 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3811 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3812
3813 return &s_callbacks;
3814}
3815#else /* RIL_SHLIB */
3816int main (int argc, char **argv)
3817{
3818 int ret;
3819 int fd = -1;
3820 int opt;
3821
3822 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3823 switch (opt) {
3824 case 'p':
3825 s_port = atoi(optarg);
3826 if (s_port == 0) {
3827 usage(argv[0]);
3828 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003829 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003830 break;
3831
3832 case 'd':
3833 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003834 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003835 break;
3836
3837 case 's':
3838 s_device_path = optarg;
3839 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003840 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003841 break;
3842
3843 default:
3844 usage(argv[0]);
3845 }
3846 }
3847
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003848 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003849 usage(argv[0]);
3850 }
3851
3852 RIL_register(&s_callbacks);
3853
3854 mainLoop(NULL);
3855
3856 return 0;
3857}
3858
3859#endif /* RIL_SHLIB */