blob: 0e84d692cb42f639dbd340bf25f14c6e95fe92c5 [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
bohuefa34012017-05-01 14:07:22 -07001203static void requestDeviceIdentity(int request __unused, void *data __unused,
Mark Salyzynba58c202014-03-12 15:20:22 -07001204 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";
bohu8c1da5b2017-05-01 14:38:13 -07001221 responseStr[3] = ""; // default empty for non-CDMA
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001222
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001223 err = at_send_command_numeric("AT+CGSN", &p_response);
1224 if (err < 0 || p_response->success == 0) {
1225 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1226 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001227 } else {
bohuefa34012017-05-01 14:07:22 -07001228 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
1229 responseStr[3] = p_response->p_intermediates->line;
1230 } else {
1231 responseStr[0] = p_response->p_intermediates->line;
1232 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001233 }
1234
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001235 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1236 at_response_free(p_response);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001237}
1238
Mark Salyzynba58c202014-03-12 15:20:22 -07001239static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1240 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001241{
1242 int err;
1243 int *ss = (int *)data;
1244 ATResponse *p_response = NULL;
1245 char *cmd = NULL;
1246 char *line = NULL;
1247 int response;
1248
1249 asprintf(&cmd, "AT+CCSS?");
1250 if (!cmd) goto error;
1251
1252 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1253 if (err < 0 || !p_response->success)
1254 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001255
1256 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001257 err = at_tok_start(&line);
1258 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001259
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001260 err = at_tok_nextint(&line, &response);
1261 free(cmd);
1262 cmd = NULL;
1263
1264 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1265
1266 return;
1267error:
1268 free(cmd);
1269 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1270}
1271
Mark Salyzynba58c202014-03-12 15:20:22 -07001272static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001273 size_t datalen, RIL_Token t)
1274{
1275 int err;
1276 int *ss = (int *)data;
1277 ATResponse *p_response = NULL;
1278 char *cmd = NULL;
1279
1280 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001281 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001282 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1283 return;
1284 }
1285 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1286 if (!cmd) goto error;
1287
1288 err = at_send_command(cmd, &p_response);
1289 if (err < 0 || !p_response->success)
1290 goto error;
1291 free(cmd);
1292 cmd = NULL;
1293
1294 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1295
1296 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1297
1298 return;
1299error:
1300 free(cmd);
1301 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1302}
1303
Mark Salyzynba58c202014-03-12 15:20:22 -07001304static void requestCdmaSubscription(int request __unused, void *data __unused,
1305 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001306{
1307 int err;
1308 int response[5];
1309 char * responseStr[5];
1310 ATResponse *p_response = NULL;
1311 const char *cmd;
1312 const char *prefix;
1313 char *line, *p;
1314 int commas;
1315 int skip;
1316 int count = 5;
1317
1318 // Fixed values. TODO: Query modem
1319 responseStr[0] = "8587777777"; // MDN
1320 responseStr[1] = "1"; // SID
1321 responseStr[2] = "1"; // NID
1322 responseStr[3] = "8587777777"; // MIN
1323 responseStr[4] = "1"; // PRL Version
1324 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001325}
1326
Mark Salyzynba58c202014-03-12 15:20:22 -07001327static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1328 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001329{
1330 int roaming_pref = -1;
1331 ATResponse *p_response = NULL;
1332 char *line;
1333 int res;
1334
1335 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1336 if (res < 0 || !p_response->success) {
1337 goto error;
1338 }
1339 line = p_response->p_intermediates->line;
1340
1341 res = at_tok_start(&line);
1342 if (res < 0) goto error;
1343
1344 res = at_tok_nextint(&line, &roaming_pref);
1345 if (res < 0) goto error;
1346
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001347 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001348 return;
1349error:
1350 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1351}
1352
Mark Salyzynba58c202014-03-12 15:20:22 -07001353static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1354 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001355{
1356 int *pref = (int *)data;
1357 ATResponse *p_response = NULL;
1358 char *line;
1359 int res;
1360 char *cmd = NULL;
1361
1362 asprintf(&cmd, "AT+WRMP=%d", *pref);
1363 if (cmd == NULL) goto error;
1364
1365 res = at_send_command(cmd, &p_response);
1366 if (res < 0 || !p_response->success)
1367 goto error;
1368
1369 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1370 free(cmd);
1371 return;
1372error:
1373 free(cmd);
1374 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1375}
1376
1377static int parseRegistrationState(char *str, int *type, int *items, int **response)
1378{
1379 int err;
1380 char *line = str, *p;
1381 int *resp = NULL;
1382 int skip;
1383 int count = 3;
1384 int commas;
1385
Wink Saville4dcab4f2012-11-19 16:05:13 -08001386 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001387 err = at_tok_start(&line);
1388 if (err < 0) goto error;
1389
1390 /* Ok you have to be careful here
1391 * The solicited version of the CREG response is
1392 * +CREG: n, stat, [lac, cid]
1393 * and the unsolicited version is
1394 * +CREG: stat, [lac, cid]
1395 * The <n> parameter is basically "is unsolicited creg on?"
1396 * which it should always be
1397 *
1398 * Now we should normally get the solicited version here,
1399 * but the unsolicited version could have snuck in
1400 * so we have to handle both
1401 *
1402 * Also since the LAC and CID are only reported when registered,
1403 * we can have 1, 2, 3, or 4 arguments here
1404 *
1405 * finally, a +CGREG: answer may have a fifth value that corresponds
1406 * to the network type, as in;
1407 *
1408 * +CGREG: n, stat [,lac, cid [,networkType]]
1409 */
1410
1411 /* count number of commas */
1412 commas = 0;
1413 for (p = line ; *p != '\0' ;p++) {
1414 if (*p == ',') commas++;
1415 }
1416
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001417 resp = (int *)calloc(commas + 1, sizeof(int));
1418 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001419 switch (commas) {
1420 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001421 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001422 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001423 resp[1] = -1;
1424 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001425 break;
1426
1427 case 1: /* +CREG: <n>, <stat> */
1428 err = at_tok_nextint(&line, &skip);
1429 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001430 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001431 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001432 resp[1] = -1;
1433 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001434 if (err < 0) goto error;
1435 break;
1436
1437 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001438 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001439 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001440 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001441 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001442 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001443 if (err < 0) goto error;
1444 break;
1445 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1446 err = at_tok_nextint(&line, &skip);
1447 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001448 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001449 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001450 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001451 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001452 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001453 if (err < 0) goto error;
1454 break;
1455 /* special case for CGREG, there is a fourth parameter
1456 * that is the network type (unknown/gprs/edge/umts)
1457 */
1458 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1459 err = at_tok_nextint(&line, &skip);
1460 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001461 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001462 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001463 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001464 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001465 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001466 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001467 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001468 if (err < 0) goto error;
1469 count = 4;
1470 break;
1471 default:
1472 goto error;
1473 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001474 s_lac = resp[1];
1475 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001476 if (response)
1477 *response = resp;
1478 if (items)
1479 *items = commas + 1;
1480 if (type)
1481 *type = techFromModemType(TECH(sMdmInfo));
1482 return 0;
1483error:
1484 free(resp);
1485 return -1;
1486}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001487
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001488#define REG_STATE_LEN 15
1489#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001490static void requestRegistrationState(int request, void *data __unused,
1491 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001492{
1493 int err;
1494 int *registration;
1495 char **responseStr = NULL;
1496 ATResponse *p_response = NULL;
1497 const char *cmd;
1498 const char *prefix;
1499 char *line;
1500 int i = 0, j, numElements = 0;
1501 int count = 3;
1502 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001503
Wink Saville4dcab4f2012-11-19 16:05:13 -08001504 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001505 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1506 cmd = "AT+CREG?";
1507 prefix = "+CREG:";
1508 numElements = REG_STATE_LEN;
1509 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1510 cmd = "AT+CGREG?";
1511 prefix = "+CGREG:";
1512 numElements = REG_DATA_STATE_LEN;
1513 } else {
1514 assert(0);
1515 goto error;
1516 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001517
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001518 err = at_send_command_singleline(cmd, prefix, &p_response);
1519
1520 if (err != 0) goto error;
1521
1522 line = p_response->p_intermediates->line;
1523
1524 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1525
1526 responseStr = malloc(numElements * sizeof(char *));
1527 if (!responseStr) goto error;
1528 memset(responseStr, 0, numElements * sizeof(char *));
1529 /**
1530 * The first '4' bytes for both registration states remain the same.
1531 * But if the request is 'DATA_REGISTRATION_STATE',
1532 * the 5th and 6th byte(s) are optional.
1533 */
1534 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001535 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001536 // TODO: Query modem
1537 startfrom = 3;
1538 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1539 asprintf(&responseStr[3], "8"); // EvDo revA
1540 asprintf(&responseStr[4], "1"); // BSID
1541 asprintf(&responseStr[5], "123"); // Latitude
1542 asprintf(&responseStr[6], "222"); // Longitude
1543 asprintf(&responseStr[7], "0"); // CSS Indicator
1544 asprintf(&responseStr[8], "4"); // SID
1545 asprintf(&responseStr[9], "65535"); // NID
1546 asprintf(&responseStr[10], "0"); // Roaming indicator
1547 asprintf(&responseStr[11], "1"); // System is in PRL
1548 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1549 asprintf(&responseStr[13], "0"); // Reason for denial
1550 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1551 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1552 asprintf(&responseStr[3], "8"); // Available data radio technology
1553 }
1554 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001555 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001556 startfrom = 0;
1557 asprintf(&responseStr[1], "%x", registration[1]);
1558 asprintf(&responseStr[2], "%x", registration[2]);
1559 if (count > 3)
1560 asprintf(&responseStr[3], "%d", registration[3]);
1561 }
1562 asprintf(&responseStr[0], "%d", registration[0]);
1563
1564 /**
1565 * Optional bytes for DATA_REGISTRATION_STATE request
1566 * 4th byte : Registration denial code
1567 * 5th byte : The max. number of simultaneous Data Calls
1568 */
1569 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1570 // asprintf(&responseStr[4], "3");
1571 // asprintf(&responseStr[5], "1");
1572 }
1573
1574 for (j = startfrom; j < numElements; j++) {
1575 if (!responseStr[i]) goto error;
1576 }
1577 free(registration);
1578 registration = NULL;
1579
1580 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1581 for (j = 0; j < numElements; j++ ) {
1582 free(responseStr[j]);
1583 responseStr[j] = NULL;
1584 }
1585 free(responseStr);
1586 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001587 at_response_free(p_response);
1588
1589 return;
1590error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001591 if (responseStr) {
1592 for (j = 0; j < numElements; j++) {
1593 free(responseStr[j]);
1594 responseStr[j] = NULL;
1595 }
1596 free(responseStr);
1597 responseStr = NULL;
1598 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001599 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001600 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1601 at_response_free(p_response);
1602}
1603
Mark Salyzynba58c202014-03-12 15:20:22 -07001604static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001605{
1606 int err;
1607 int i;
1608 int skip;
1609 ATLine *p_cur;
1610 char *response[3];
1611
1612 memset(response, 0, sizeof(response));
1613
1614 ATResponse *p_response = NULL;
1615
1616 err = at_send_command_multiline(
1617 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1618 "+COPS:", &p_response);
1619
1620 /* we expect 3 lines here:
1621 * +COPS: 0,0,"T - Mobile"
1622 * +COPS: 0,1,"TMO"
1623 * +COPS: 0,2,"310170"
1624 */
1625
1626 if (err != 0) goto error;
1627
1628 for (i = 0, p_cur = p_response->p_intermediates
1629 ; p_cur != NULL
1630 ; p_cur = p_cur->p_next, i++
1631 ) {
1632 char *line = p_cur->line;
1633
1634 err = at_tok_start(&line);
1635 if (err < 0) goto error;
1636
1637 err = at_tok_nextint(&line, &skip);
1638 if (err < 0) goto error;
1639
1640 // If we're unregistered, we may just get
1641 // a "+COPS: 0" response
1642 if (!at_tok_hasmore(&line)) {
1643 response[i] = NULL;
1644 continue;
1645 }
1646
1647 err = at_tok_nextint(&line, &skip);
1648 if (err < 0) goto error;
1649
1650 // a "+COPS: 0, n" response is also possible
1651 if (!at_tok_hasmore(&line)) {
1652 response[i] = NULL;
1653 continue;
1654 }
1655
1656 err = at_tok_nextstr(&line, &(response[i]));
1657 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001658 // Simple assumption that mcc and mnc are 3 digits each
1659 if (strlen(response[i]) == 6) {
1660 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1661 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1662 }
1663 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001664 }
1665
1666 if (i != 3) {
1667 /* expect 3 lines exactly */
1668 goto error;
1669 }
1670
1671 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1672 at_response_free(p_response);
1673
1674 return;
1675error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001676 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001677 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1678 at_response_free(p_response);
1679}
1680
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001681static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1682{
1683 int err = 1; // Set to go to error:
1684 RIL_SMS_Response response;
1685 RIL_CDMA_SMS_Message* rcsm;
1686
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001687 if (getSIMStatus() == SIM_ABSENT) {
1688 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
1689 return;
1690 }
1691
Mark Salyzynba58c202014-03-12 15:20:22 -07001692 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001693 datalen, sizeof(RIL_CDMA_SMS_Message));
1694
1695 // verify data content to test marshalling/unmarshalling:
1696 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001697 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001698 uServicecategory=%d, sAddress.digit_mode=%d, \
1699 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1700 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1701 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1702 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1703
1704 if (err != 0) goto error;
1705
1706 // Cdma Send SMS implementation will go here:
1707 // But it is not implemented yet.
1708
1709 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001710 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001711 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1712 return;
1713
1714error:
1715 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001716 response.messageRef = -1;
1717 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001718}
1719
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001720static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1721{
1722 int err;
1723 const char *smsc;
1724 const char *pdu;
1725 int tpLayerLength;
1726 char *cmd1, *cmd2;
1727 RIL_SMS_Response response;
1728 ATResponse *p_response = NULL;
1729
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001730 if (getSIMStatus() == SIM_ABSENT) {
1731 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
1732 return;
1733 }
1734
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001735 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001736 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001737
1738 if (s_ims_gsm_fail != 0) goto error;
1739 if (s_ims_gsm_retry != 0) goto error2;
1740
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001741 smsc = ((const char **)data)[0];
1742 pdu = ((const char **)data)[1];
1743
1744 tpLayerLength = strlen(pdu)/2;
1745
1746 // "NULL for default SMSC"
1747 if (smsc == NULL) {
1748 smsc= "00";
1749 }
1750
1751 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1752 asprintf(&cmd2, "%s%s", smsc, pdu);
1753
1754 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1755
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001756 free(cmd1);
1757 free(cmd2);
1758
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001759 if (err != 0 || p_response->success == 0) goto error;
1760
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001761 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001762 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001763 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1764 at_response_free(p_response);
1765
1766 return;
1767error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001768 response.messageRef = -2;
1769 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001770 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001771 return;
1772error2:
1773 // send retry error.
1774 response.messageRef = -1;
1775 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1776 at_response_free(p_response);
1777 return;
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001778}
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001779
1780static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1781{
1782 RIL_IMS_SMS_Message *p_args;
1783 RIL_SMS_Response response;
1784
1785 memset(&response, 0, sizeof(response));
1786
Mark Salyzynba58c202014-03-12 15:20:22 -07001787 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001788 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1789 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1790 datalen, s_ims_registered, s_ims_services, s_ims_format,
1791 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1792 s_ims_gsm_retry);
1793
1794 // figure out if this is gsm/cdma format
1795 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1796 p_args = (RIL_IMS_SMS_Message *)data;
1797
1798 if (0 != s_ims_cause_perm_failure ) goto error;
1799
1800 // want to fail over ims and this is first request over ims
1801 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1802
1803 if (RADIO_TECH_3GPP == p_args->tech) {
1804 return requestSendSMS(p_args->message.gsmMessage,
1805 datalen - sizeof(RIL_RadioTechnologyFamily),
1806 t);
1807 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1808 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1809 datalen - sizeof(RIL_RadioTechnologyFamily),
1810 t);
1811 } else {
1812 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1813 }
1814
1815error:
1816 response.messageRef = -2;
1817 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1818 return;
1819
1820error2:
1821 response.messageRef = -1;
1822 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001823}
1824
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001825static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1826{
1827 ATResponse *p_response = NULL;
1828 int32_t session_id;
1829 int err;
1830 char cmd[32];
1831 char dummy;
1832 char *line;
1833
1834 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1835 if (data == NULL || datalen == 0 || datalen > 16) {
1836 ALOGE("Invalid data passed to requestSimOpenChannel");
1837 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1838 return;
1839 }
1840
1841 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1842
1843 err = at_send_command_numeric(cmd, &p_response);
1844 if (err < 0 || p_response == NULL || p_response->success == 0) {
1845 ALOGE("Error %d opening logical channel: %d",
1846 err, p_response ? p_response->success : 0);
1847 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1848 at_response_free(p_response);
1849 return;
1850 }
1851
1852 // Ensure integer only by scanning for an extra char but expect one result
1853 line = p_response->p_intermediates->line;
1854 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1855 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1856 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1857 return;
1858 }
1859
1860 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1861 at_response_free(p_response);
1862}
1863
1864static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1865{
1866 ATResponse *p_response = NULL;
1867 int32_t session_id;
1868 int err;
1869 char cmd[32];
1870
1871 if (data == NULL || datalen != sizeof(session_id)) {
1872 ALOGE("Invalid data passed to requestSimCloseChannel");
1873 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1874 return;
1875 }
1876 session_id = ((int32_t *)data)[0];
1877 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1878 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1879
1880 if (err < 0 || p_response == NULL || p_response->success == 0) {
1881 ALOGE("Error %d closing logical channel %d: %d",
1882 err, session_id, p_response ? p_response->success : 0);
1883 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1884 at_response_free(p_response);
1885 return;
1886 }
1887
1888 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1889
1890 at_response_free(p_response);
1891}
1892
1893static void requestSimTransmitApduChannel(void *data,
1894 size_t datalen,
1895 RIL_Token t)
1896{
1897 ATResponse *p_response = NULL;
1898 int err;
1899 char *cmd;
1900 char *line;
1901 size_t cmd_size;
1902 RIL_SIM_IO_Response sim_response;
1903 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1904
1905 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1906 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1907 return;
1908 }
1909
1910 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
Wei Wang9cec1e02017-02-08 14:37:37 -08001911 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001912 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1913 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1914
1915 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1916 free(cmd);
1917 if (err < 0 || p_response == NULL || p_response->success == 0) {
1918 ALOGE("Error %d transmitting APDU: %d",
1919 err, p_response ? p_response->success : 0);
1920 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1921 at_response_free(p_response);
1922 return;
1923 }
1924
1925 line = p_response->p_intermediates->line;
1926 err = parseSimResponseLine(line, &sim_response);
1927
1928 if (err == 0) {
1929 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1930 &sim_response, sizeof(sim_response));
1931 } else {
1932 ALOGE("Error %d parsing SIM response line: %s", err, line);
1933 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1934 }
1935 at_response_free(p_response);
1936}
1937
Wink Savillef4c4d362009-04-02 01:37:03 -07001938static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001939{
1940 const char *apn;
1941 char *cmd;
1942 int err;
1943 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001944
Wink Savillef4c4d362009-04-02 01:37:03 -07001945 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001946
1947#ifdef USE_TI_COMMANDS
1948 // Config for multislot class 10 (probably default anyway eh?)
1949 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1950 NULL);
1951
1952 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1953#endif /* USE_TI_COMMANDS */
1954
1955 int fd, qmistatus;
1956 size_t cur = 0;
1957 size_t len;
1958 ssize_t written, rlen;
1959 char status[32] = {0};
1960 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001961 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001962
Wink Saville4dcab4f2012-11-19 16:05:13 -08001963 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001964
1965 fd = open ("/dev/qmi", O_RDWR);
1966 if (fd >= 0) { /* the device doesn't exist on the emulator */
1967
Wink Saville4dcab4f2012-11-19 16:05:13 -08001968 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001969 asprintf(&cmd, "up:%s", apn);
1970 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001971
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001972 while (cur < len) {
1973 do {
1974 written = write (fd, cmd + cur, len - cur);
1975 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001976
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001977 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001978 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001979 close(fd);
1980 goto error;
1981 }
1982
1983 cur += written;
1984 }
1985
1986 // wait for interface to come online
1987
1988 do {
1989 sleep(1);
1990 do {
1991 rlen = read(fd, status, 31);
1992 } while (rlen < 0 && errno == EINTR);
1993
1994 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001995 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001996 close(fd);
1997 goto error;
1998 } else {
1999 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08002000 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002001 }
2002 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
2003
2004 close(fd);
2005
2006 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002007 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002008 goto error;
2009 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002010
2011 qmistatus = system("netcfg rmnet0 dhcp");
2012
Wink Saville4dcab4f2012-11-19 16:05:13 -08002013 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002014
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002015 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002016
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002017 } else {
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -07002018 bool hasWifi = hasWifiCapability();
2019 const char* radioInterfaceName = getRadioInterfaceName(hasWifi);
2020 if (setInterfaceState(radioInterfaceName, kInterfaceUp) != RIL_E_SUCCESS) {
2021 goto error;
2022 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002023
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002024 if (datalen > 6 * sizeof(char *)) {
2025 pdp_type = ((const char **)data)[6];
2026 } else {
2027 pdp_type = "IP";
2028 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002029
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002030 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002031 //FIXME check for error here
2032 err = at_send_command(cmd, NULL);
2033 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002034
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002035 // Set required QoS params to default
2036 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002037
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002038 // Set minimum QoS params to default
2039 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002040
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002041 // packet-domain event reporting
2042 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002043
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002044 // Hangup anything that's happening there now
2045 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002046
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07002047 // Start data on PDP context 1
2048 err = at_send_command("ATD*99***1#", &p_response);
2049
2050 if (err < 0 || p_response->success == 0) {
2051 goto error;
2052 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002053 }
2054
Wink Saville43808972011-01-13 17:39:51 -08002055 requestOrSendDataCallList(&t);
2056
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002057 at_response_free(p_response);
2058
2059 return;
2060error:
2061 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2062 at_response_free(p_response);
2063
2064}
2065
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -07002066static void requestDeactivateDataCall(RIL_Token t)
2067{
2068 bool hasWifi = hasWifiCapability();
2069 const char* radioInterfaceName = getRadioInterfaceName(hasWifi);
2070 RIL_Errno rilErrno = setInterfaceState(radioInterfaceName, kInterfaceDown);
2071 RIL_onRequestComplete(t, rilErrno, NULL, 0);
2072}
2073
Mark Salyzynba58c202014-03-12 15:20:22 -07002074static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002075{
2076 int ackSuccess;
2077 int err;
2078
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002079 if (getSIMStatus() == SIM_ABSENT) {
2080 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2081 return;
2082 }
2083
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002084 ackSuccess = ((int *)data)[0];
2085
2086 if (ackSuccess == 1) {
2087 err = at_send_command("AT+CNMA=1", NULL);
2088 } else if (ackSuccess == 0) {
2089 err = at_send_command("AT+CNMA=2", NULL);
2090 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002091 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002092 goto error;
2093 }
2094
2095 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2096error:
2097 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2098
2099}
2100
Mark Salyzynba58c202014-03-12 15:20:22 -07002101static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002102{
2103 ATResponse *p_response = NULL;
2104 RIL_SIM_IO_Response sr;
2105 int err;
2106 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002107 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002108 char *line;
2109
2110 memset(&sr, 0, sizeof(sr));
2111
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002112 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002113
2114 /* FIXME handle pin2 */
2115
2116 if (p_args->data == NULL) {
2117 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
2118 p_args->command, p_args->fileid,
2119 p_args->p1, p_args->p2, p_args->p3);
2120 } else {
2121 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
2122 p_args->command, p_args->fileid,
2123 p_args->p1, p_args->p2, p_args->p3, p_args->data);
2124 }
2125
2126 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
2127
2128 if (err < 0 || p_response->success == 0) {
2129 goto error;
2130 }
2131
2132 line = p_response->p_intermediates->line;
2133
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002134 err = parseSimResponseLine(line, &sr);
2135 if (err < 0) {
2136 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002137 }
2138
2139 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
2140 at_response_free(p_response);
2141 free(cmd);
2142
2143 return;
2144error:
2145 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2146 at_response_free(p_response);
2147 free(cmd);
2148
2149}
2150
2151static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
2152{
2153 ATResponse *p_response = NULL;
2154 int err;
2155 char* cmd = NULL;
2156 const char** strings = (const char**)data;;
2157
2158 if ( datalen == sizeof(char*) ) {
2159 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
2160 } else if ( datalen == 2*sizeof(char*) ) {
2161 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
2162 } else
2163 goto error;
2164
2165 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
2166 free(cmd);
2167
2168 if (err < 0 || p_response->success == 0) {
2169error:
2170 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
2171 } else {
2172 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2173 }
2174 at_response_free(p_response);
2175}
2176
2177
Mark Salyzynba58c202014-03-12 15:20:22 -07002178static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002179{
2180 const char *ussdRequest;
2181
2182 ussdRequest = (char *)(data);
2183
2184
2185 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2186
2187// @@@ TODO
2188
2189}
2190
Mark Salyzynba58c202014-03-12 15:20:22 -07002191static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002192{
2193 int err;
2194 ATResponse *p_response = NULL;
2195
2196 err = at_send_command("AT+WSOS=0", &p_response);
2197
2198 if (err < 0 || p_response->success == 0) {
2199 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2200 return;
2201 }
2202
2203 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2204}
2205
2206// TODO: Use all radio types
2207static int techFromModemType(int mdmtype)
2208{
2209 int ret = -1;
2210 switch (1 << mdmtype) {
2211 case MDM_CDMA:
2212 ret = RADIO_TECH_1xRTT;
2213 break;
2214 case MDM_EVDO:
2215 ret = RADIO_TECH_EVDO_A;
2216 break;
2217 case MDM_GSM:
2218 ret = RADIO_TECH_GPRS;
2219 break;
2220 case MDM_WCDMA:
2221 ret = RADIO_TECH_HSPA;
2222 break;
2223 case MDM_LTE:
2224 ret = RADIO_TECH_LTE;
2225 break;
2226 }
2227 return ret;
2228}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002229
Mark Salyzynba58c202014-03-12 15:20:22 -07002230static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002231{
2232 uint64_t curTime = ril_nano_time();
2233 RIL_CellInfo ci[1] =
2234 {
2235 { // ci[0]
2236 1, // cellInfoType
2237 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002238 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002239 curTime - 1000, // Fake some time in the past
2240 { // union CellInfo
2241 { // RIL_CellInfoGsm gsm
2242 { // gsm.cellIdneityGsm
2243 s_mcc, // mcc
2244 s_mnc, // mnc
2245 s_lac, // lac
2246 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002247 },
2248 { // gsm.signalStrengthGsm
2249 10, // signalStrength
2250 0 // bitErrorRate
2251 }
2252 }
2253 }
2254 }
2255 };
2256
2257 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2258}
2259
2260
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002261static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002262{
2263 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2264 // will be sent.
2265 assert (datalen == sizeof(int));
2266 s_cell_info_rate_ms = ((int *)data)[0];
2267
2268 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2269}
2270
Etan Cohend3652192014-06-20 08:28:44 -07002271static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2272{
2273 // TODO - hook this up with real query/info from radio.
2274
2275 RIL_HardwareConfig hwCfg;
2276
2277 RIL_UNUSED_PARM(data);
2278 RIL_UNUSED_PARM(datalen);
2279
2280 hwCfg.type = -1;
2281
2282 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2283}
2284
Jim Kayed2d82012017-10-06 14:17:47 -07002285static void requestGetTtyMode(void *data, size_t datalen, RIL_Token t)
2286{
2287 int ttyModeResponse;
2288
2289 RIL_UNUSED_PARM(data);
2290 RIL_UNUSED_PARM(datalen);
2291
2292 ttyModeResponse = (getSIMStatus() == SIM_READY) ? 1 // TTY Full
2293 : 0; // TTY Off
2294
2295 RIL_onRequestComplete(t, RIL_E_SUCCESS, &ttyModeResponse, sizeof(ttyModeResponse));
2296}
2297
2298static void requestGetRadioCapability(void *data, size_t datalen, RIL_Token t)
2299{
2300 RIL_RadioCapability radioCapability;
2301
2302 RIL_UNUSED_PARM(data);
2303 RIL_UNUSED_PARM(datalen);
2304
2305 radioCapability.version = RIL_RADIO_CAPABILITY_VERSION;
2306 radioCapability.session = 0;
2307 radioCapability.phase = 0;
2308 radioCapability.rat = 0;
2309 radioCapability.logicalModemUuid[0] = '\0';
2310 radioCapability.status = RC_STATUS_SUCCESS;
2311
2312 RIL_onRequestComplete(t, RIL_E_SUCCESS, &radioCapability, sizeof(radioCapability));
2313}
2314
2315static void requestGetMute(void *data, size_t datalen, RIL_Token t)
2316{
2317 int muteResponse;
2318
2319 RIL_UNUSED_PARM(data);
2320 RIL_UNUSED_PARM(datalen);
2321
2322 muteResponse = 0; // Mute disabled
2323
2324 RIL_onRequestComplete(t, RIL_E_SUCCESS, &muteResponse, sizeof(muteResponse));
2325}
Etan Cohend3652192014-06-20 08:28:44 -07002326
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002327/*** Callback methods from the RIL library to us ***/
2328
2329/**
2330 * Call from RIL to us to make a RIL_REQUEST
2331 *
2332 * Must be completed with a call to RIL_onRequestComplete()
2333 *
2334 * RIL_onRequestComplete() may be called from any thread, before or after
2335 * this function returns.
2336 *
Weilun Du9f471e22017-02-07 10:47:19 -08002337 * Because onRequest function could be called from multiple different thread,
2338 * we must ensure that the underlying at_send_command_* function
2339 * is atomic.
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002340 */
2341static void
2342onRequest (int request, void *data, size_t datalen, RIL_Token t)
2343{
2344 ATResponse *p_response;
2345 int err;
2346
Wink Saville4dcab4f2012-11-19 16:05:13 -08002347 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002348
2349 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2350 * when RADIO_STATE_UNAVAILABLE.
2351 */
2352 if (sState == RADIO_STATE_UNAVAILABLE
2353 && request != RIL_REQUEST_GET_SIM_STATUS
2354 ) {
2355 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2356 return;
2357 }
2358
2359 /* Ignore all non-power requests when RADIO_STATE_OFF
2360 * (except RIL_REQUEST_GET_SIM_STATUS)
2361 */
Jim Kayed2d82012017-10-06 14:17:47 -07002362 if (sState == RADIO_STATE_OFF) {
2363 switch(request) {
2364 case RIL_REQUEST_BASEBAND_VERSION:
2365 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2366 case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:
2367 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2368 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2369 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2370 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2371 case RIL_REQUEST_DEVICE_IDENTITY:
2372 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2373 case RIL_REQUEST_GET_ACTIVITY_INFO:
2374 case RIL_REQUEST_GET_CARRIER_RESTRICTIONS:
2375 case RIL_REQUEST_GET_CURRENT_CALLS:
2376 case RIL_REQUEST_GET_IMEI:
2377 case RIL_REQUEST_GET_MUTE:
2378 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2379 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2380 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2381 case RIL_REQUEST_GET_SIM_STATUS:
2382 case RIL_REQUEST_NV_RESET_CONFIG:
2383 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2384 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2385 case RIL_REQUEST_QUERY_TTY_MODE:
2386 case RIL_REQUEST_RADIO_POWER:
2387 case RIL_REQUEST_SET_BAND_MODE:
2388 case RIL_REQUEST_SET_CARRIER_RESTRICTIONS:
2389 case RIL_REQUEST_SET_LOCATION_UPDATES:
2390 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2391 case RIL_REQUEST_SET_TTY_MODE:
2392 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2393 case RIL_REQUEST_STOP_LCE:
2394 case RIL_REQUEST_VOICE_RADIO_TECH:
2395 // Process all the above, even though the radio is off
2396 break;
2397
2398 default:
2399 // For all others, say NOT_AVAILABLE because the radio is off
2400 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2401 return;
2402 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002403 }
2404
2405 switch (request) {
2406 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002407 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002408 char *p_buffer;
2409 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002410
Wink Savillef6aa7c12009-04-30 14:20:52 -07002411 int result = getCardStatus(&p_card_status);
2412 if (result == RIL_E_SUCCESS) {
2413 p_buffer = (char *)p_card_status;
2414 buffer_size = sizeof(*p_card_status);
2415 } else {
2416 p_buffer = NULL;
2417 buffer_size = 0;
2418 }
2419 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2420 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002421 break;
2422 }
2423 case RIL_REQUEST_GET_CURRENT_CALLS:
2424 requestGetCurrentCalls(data, datalen, t);
2425 break;
2426 case RIL_REQUEST_DIAL:
2427 requestDial(data, datalen, t);
2428 break;
2429 case RIL_REQUEST_HANGUP:
2430 requestHangup(data, datalen, t);
2431 break;
2432 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002433 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002434 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002435 case RIL_REQUEST_CONFERENCE:
2436 case RIL_REQUEST_UDUB:
2437 requestCallSelection(data, datalen, t, request);
2438 break;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002439 case RIL_REQUEST_ANSWER:
2440 at_send_command("ATA", NULL);
2441
2442#ifdef WORKAROUND_ERRONEOUS_ANSWER
2443 s_expectAnswer = 1;
2444#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2445
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002446 if (getSIMStatus() != SIM_READY) {
2447 RIL_onRequestComplete(t, RIL_E_MODEM_ERR, NULL, 0);
2448 } else {
2449 // Success or failure is ignored by the upper layer here.
2450 // It will call GET_CURRENT_CALLS and determine success that way.
2451 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2452 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002453 break;
2454
2455 case RIL_REQUEST_SEPARATE_CONNECTION:
2456 {
2457 char cmd[12];
2458 int party = ((int*)data)[0];
2459
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002460 if (getSIMStatus() == SIM_ABSENT) {
2461 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2462 return;
2463 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002464 // Make sure that party is in a valid range.
2465 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2466 // It's sufficient for us to just make sure it's single digit.)
2467 if (party > 0 && party < 10) {
2468 sprintf(cmd, "AT+CHLD=2%d", party);
2469 at_send_command(cmd, NULL);
2470 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2471 } else {
2472 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2473 }
2474 }
2475 break;
2476
2477 case RIL_REQUEST_SIGNAL_STRENGTH:
2478 requestSignalStrength(data, datalen, t);
2479 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002480 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2481 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002482 requestRegistrationState(request, data, datalen, t);
2483 break;
2484 case RIL_REQUEST_OPERATOR:
2485 requestOperator(data, datalen, t);
2486 break;
2487 case RIL_REQUEST_RADIO_POWER:
2488 requestRadioPower(data, datalen, t);
2489 break;
2490 case RIL_REQUEST_DTMF: {
2491 char c = ((char *)data)[0];
2492 char *cmd;
2493 asprintf(&cmd, "AT+VTS=%c", (int)c);
2494 at_send_command(cmd, NULL);
2495 free(cmd);
2496 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2497 break;
2498 }
2499 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302500 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002501 requestSendSMS(data, datalen, t);
2502 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002503 case RIL_REQUEST_CDMA_SEND_SMS:
2504 requestCdmaSendSMS(data, datalen, t);
2505 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002506 case RIL_REQUEST_IMS_SEND_SMS:
2507 requestImsSendSMS(data, datalen, t);
2508 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002509 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2510 requestSimOpenChannel(data, datalen, t);
2511 break;
2512 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2513 requestSimCloseChannel(data, datalen, t);
2514 break;
2515 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2516 requestSimTransmitApduChannel(data, datalen, t);
2517 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002518 case RIL_REQUEST_SETUP_DATA_CALL:
2519 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002520 break;
Bjoern Johanssone1ac3f22017-03-22 14:54:00 -07002521 case RIL_REQUEST_DEACTIVATE_DATA_CALL:
2522 requestDeactivateDataCall(t);
2523 break;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002524 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2525 requestSMSAcknowledge(data, datalen, t);
2526 break;
2527
2528 case RIL_REQUEST_GET_IMSI:
2529 p_response = NULL;
2530 err = at_send_command_numeric("AT+CIMI", &p_response);
2531
2532 if (err < 0 || p_response->success == 0) {
2533 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2534 } else {
2535 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2536 p_response->p_intermediates->line, sizeof(char *));
2537 }
2538 at_response_free(p_response);
2539 break;
2540
2541 case RIL_REQUEST_GET_IMEI:
2542 p_response = NULL;
2543 err = at_send_command_numeric("AT+CGSN", &p_response);
2544
2545 if (err < 0 || p_response->success == 0) {
2546 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2547 } else {
2548 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2549 p_response->p_intermediates->line, sizeof(char *));
2550 }
2551 at_response_free(p_response);
2552 break;
2553
2554 case RIL_REQUEST_SIM_IO:
2555 requestSIM_IO(data,datalen,t);
2556 break;
2557
2558 case RIL_REQUEST_SEND_USSD:
2559 requestSendUSSD(data, datalen, t);
2560 break;
2561
2562 case RIL_REQUEST_CANCEL_USSD:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002563 if (getSIMStatus() == SIM_ABSENT) {
2564 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2565 return;
2566 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002567 p_response = NULL;
2568 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2569
2570 if (err < 0 || p_response->success == 0) {
2571 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2572 } else {
2573 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2574 p_response->p_intermediates->line, sizeof(char *));
2575 }
2576 at_response_free(p_response);
2577 break;
2578
2579 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002580 if (getSIMStatus() == SIM_ABSENT) {
2581 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2582 } else {
2583 at_send_command("AT+COPS=0", NULL);
2584 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002585 break;
2586
Wink Savillef4c4d362009-04-02 01:37:03 -07002587 case RIL_REQUEST_DATA_CALL_LIST:
2588 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002589 break;
2590
2591 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2592 requestQueryNetworkSelectionMode(data, datalen, t);
2593 break;
2594
2595 case RIL_REQUEST_OEM_HOOK_RAW:
2596 // echo back data
2597 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2598 break;
2599
2600
2601 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2602 int i;
2603 const char ** cur;
2604
Wink Saville4dcab4f2012-11-19 16:05:13 -08002605 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002606
2607
2608 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2609 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002610 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002611 }
2612
2613 // echo back strings
2614 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2615 break;
2616 }
2617
2618 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2619 requestWriteSmsToSim(data, datalen, t);
2620 break;
2621
2622 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2623 char * cmd;
2624 p_response = NULL;
2625 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2626 err = at_send_command(cmd, &p_response);
2627 free(cmd);
2628 if (err < 0 || p_response->success == 0) {
2629 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2630 } else {
2631 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2632 }
2633 at_response_free(p_response);
2634 break;
2635 }
2636
2637 case RIL_REQUEST_ENTER_SIM_PIN:
2638 case RIL_REQUEST_ENTER_SIM_PUK:
2639 case RIL_REQUEST_ENTER_SIM_PIN2:
2640 case RIL_REQUEST_ENTER_SIM_PUK2:
2641 case RIL_REQUEST_CHANGE_SIM_PIN:
2642 case RIL_REQUEST_CHANGE_SIM_PIN2:
2643 requestEnterSimPin(data, datalen, t);
2644 break;
2645
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002646 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2647 int reply[2];
2648 //0==unregistered, 1==registered
2649 reply[0] = s_ims_registered;
2650
2651 //to be used when changed to include service supporated info
2652 //reply[1] = s_ims_services;
2653
2654 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2655 reply[1] = s_ims_format;
2656
2657 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2658 reply[0], reply[1]);
2659 if (reply[1] != -1) {
2660 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2661 } else {
2662 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2663 }
2664 break;
2665 }
2666
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002667 case RIL_REQUEST_VOICE_RADIO_TECH:
2668 {
2669 int tech = techFromModemType(TECH(sMdmInfo));
2670 if (tech < 0 )
2671 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2672 else
2673 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2674 }
2675 break;
2676 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2677 requestSetPreferredNetworkType(request, data, datalen, t);
2678 break;
2679
2680 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2681 requestGetPreferredNetworkType(request, data, datalen, t);
2682 break;
2683
Jun Tian58027012013-07-30 11:07:22 +08002684 case RIL_REQUEST_GET_CELL_INFO_LIST:
2685 requestGetCellInfoList(data, datalen, t);
2686 break;
2687
2688 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2689 requestSetCellInfoListRate(data, datalen, t);
2690 break;
2691
Etan Cohend3652192014-06-20 08:28:44 -07002692 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2693 requestGetHardwareConfig(data, datalen, t);
2694 break;
2695
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002696 case RIL_REQUEST_SHUTDOWN:
2697 requestShutdown(t);
2698 break;
2699
Jim Kayed2d82012017-10-06 14:17:47 -07002700 case RIL_REQUEST_QUERY_TTY_MODE:
2701 requestGetTtyMode(data, datalen, t);
2702 break;
2703
2704 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2705 requestGetRadioCapability(data, datalen, t);
2706 break;
2707
2708 case RIL_REQUEST_GET_MUTE:
2709 requestGetMute(data, datalen, t);
2710 break;
2711
2712 case RIL_REQUEST_SET_INITIAL_ATTACH_APN:
2713 case RIL_REQUEST_ALLOW_DATA:
2714 case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION:
2715 case RIL_REQUEST_SET_CLIR:
2716 case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION:
2717 case RIL_REQUEST_SET_BAND_MODE:
2718 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2719 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2720 case RIL_REQUEST_SET_LOCATION_UPDATES:
2721 case RIL_REQUEST_SET_TTY_MODE:
2722 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2723 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2724 break;
2725
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002726 case RIL_REQUEST_BASEBAND_VERSION:
Jim Kayed2d82012017-10-06 14:17:47 -07002727 requestCdmaBaseBandVersion(request, data, datalen, t);
2728 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002729
2730 case RIL_REQUEST_DEVICE_IDENTITY:
bohuefa34012017-05-01 14:07:22 -07002731 requestDeviceIdentity(request, data, datalen, t);
Jim Kayed2d82012017-10-06 14:17:47 -07002732 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002733
2734 case RIL_REQUEST_CDMA_SUBSCRIPTION:
Jim Kayed2d82012017-10-06 14:17:47 -07002735 requestCdmaSubscription(request, data, datalen, t);
2736 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002737
Jim Kayed2d82012017-10-06 14:17:47 -07002738 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2739 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2740 break;
2741
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002742 case RIL_REQUEST_START_LCE:
2743 case RIL_REQUEST_STOP_LCE:
2744 case RIL_REQUEST_PULL_LCEDATA:
2745 if (getSIMStatus() == SIM_ABSENT) {
2746 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
2747 } else {
2748 RIL_onRequestComplete(t, RIL_E_LCE_NOT_SUPPORTED, NULL, 0);
2749 }
2750 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002751
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002752 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2753 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2754 requestCdmaGetRoamingPreference(request, data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002755 } else {
2756 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2757 }
2758 break;
2759
2760 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2761 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2762 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2763 } else {
2764 // VTS tests expect us to silently do nothing
2765 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2766 }
2767 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002768
2769 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2770 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2771 requestCdmaSetRoamingPreference(request, data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002772 } else {
2773 // VTS tests expect us to silently do nothing
2774 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2775 }
2776 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002777
2778 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2779 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2780 requestExitEmergencyMode(data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002781 } else {
2782 // VTS tests expect us to silently do nothing
2783 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2784 }
Jim Kayed2d82012017-10-06 14:17:47 -07002785 break;
2786
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002787 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002788 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002789 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2790 break;
2791 }
2792}
2793
2794/**
2795 * Synchronous call from the RIL to us to return current radio state.
2796 * RADIO_STATE_UNAVAILABLE should be the initial state.
2797 */
2798static RIL_RadioState
2799currentState()
2800{
2801 return sState;
2802}
2803/**
2804 * Call from RIL to us to find out whether a specific request code
2805 * is supported by this implementation.
2806 *
2807 * Return 1 for "supported" and 0 for "unsupported"
2808 */
2809
2810static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002811onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002812{
2813 //@@@ todo
2814
2815 return 1;
2816}
2817
Mark Salyzynba58c202014-03-12 15:20:22 -07002818static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002819{
2820 //@@@todo
2821
2822}
2823
2824static const char * getVersion(void)
2825{
2826 return "android reference-ril 1.0";
2827}
2828
2829static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002830setRadioTechnology(ModemInfo *mdm, int newtech)
2831{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002832 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002833
2834 int oldtech = TECH(mdm);
2835
2836 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002837 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002838 TECH(mdm) = newtech;
2839 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2840 int tech = techFromModemType(TECH(sMdmInfo));
2841 if (tech > 0 ) {
2842 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2843 &tech, sizeof(tech));
2844 }
2845 }
2846 }
2847}
2848
2849static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002850setRadioState(RIL_RadioState newState)
2851{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002852 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002853 RIL_RadioState oldState;
2854
2855 pthread_mutex_lock(&s_state_mutex);
2856
2857 oldState = sState;
2858
2859 if (s_closed > 0) {
2860 // If we're closed, the only reasonable state is
2861 // RADIO_STATE_UNAVAILABLE
2862 // This is here because things on the main thread
2863 // may attempt to change the radio state after the closed
2864 // event happened in another thread
2865 newState = RADIO_STATE_UNAVAILABLE;
2866 }
2867
2868 if (sState != newState || s_closed > 0) {
2869 sState = newState;
2870
2871 pthread_cond_broadcast (&s_state_cond);
2872 }
2873
2874 pthread_mutex_unlock(&s_state_mutex);
2875
2876
2877 /* do these outside of the mutex */
2878 if (sState != oldState) {
2879 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2880 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002881 // Sim state can change as result of radio state change
2882 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2883 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002884
2885 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2886 * from the AT reader thread
2887 * Currently, this doesn't happen, but if that changes then these
2888 * will need to be dispatched on the request thread
2889 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002890 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002891 onRadioPowerOn();
2892 }
2893 }
2894}
2895
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002896/** Returns RUIM_NOT_READY on error */
2897static SIM_Status
2898getRUIMStatus()
2899{
2900 ATResponse *p_response = NULL;
2901 int err;
2902 int ret;
2903 char *cpinLine;
2904 char *cpinResult;
2905
2906 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2907 ret = SIM_NOT_READY;
2908 goto done;
2909 }
2910
2911 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2912
2913 if (err != 0) {
2914 ret = SIM_NOT_READY;
2915 goto done;
2916 }
2917
2918 switch (at_get_cme_error(p_response)) {
2919 case CME_SUCCESS:
2920 break;
2921
2922 case CME_SIM_NOT_INSERTED:
2923 ret = SIM_ABSENT;
2924 goto done;
2925
2926 default:
2927 ret = SIM_NOT_READY;
2928 goto done;
2929 }
2930
2931 /* CPIN? has succeeded, now look at the result */
2932
2933 cpinLine = p_response->p_intermediates->line;
2934 err = at_tok_start (&cpinLine);
2935
2936 if (err < 0) {
2937 ret = SIM_NOT_READY;
2938 goto done;
2939 }
2940
2941 err = at_tok_nextstr(&cpinLine, &cpinResult);
2942
2943 if (err < 0) {
2944 ret = SIM_NOT_READY;
2945 goto done;
2946 }
2947
2948 if (0 == strcmp (cpinResult, "SIM PIN")) {
2949 ret = SIM_PIN;
2950 goto done;
2951 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2952 ret = SIM_PUK;
2953 goto done;
2954 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2955 return SIM_NETWORK_PERSONALIZATION;
2956 } else if (0 != strcmp (cpinResult, "READY")) {
2957 /* we're treating unsupported lock types as "sim absent" */
2958 ret = SIM_ABSENT;
2959 goto done;
2960 }
2961
2962 at_response_free(p_response);
2963 p_response = NULL;
2964 cpinResult = NULL;
2965
2966 ret = SIM_READY;
2967
2968done:
2969 at_response_free(p_response);
2970 return ret;
2971}
2972
John Wang309ac292009-07-30 14:53:23 -07002973/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002974static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002975getSIMStatus()
2976{
2977 ATResponse *p_response = NULL;
2978 int err;
2979 int ret;
2980 char *cpinLine;
2981 char *cpinResult;
2982
Wink Saville4dcab4f2012-11-19 16:05:13 -08002983 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002984 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2985
2986 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002987 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002988 goto done;
2989 }
2990
2991 switch (at_get_cme_error(p_response)) {
2992 case CME_SUCCESS:
2993 break;
2994
2995 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002996 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002997 goto done;
2998
2999 default:
John Wang309ac292009-07-30 14:53:23 -07003000 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003001 goto done;
3002 }
3003
3004 /* CPIN? has succeeded, now look at the result */
3005
3006 cpinLine = p_response->p_intermediates->line;
3007 err = at_tok_start (&cpinLine);
3008
3009 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07003010 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003011 goto done;
3012 }
3013
3014 err = at_tok_nextstr(&cpinLine, &cpinResult);
3015
3016 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07003017 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003018 goto done;
3019 }
3020
3021 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07003022 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003023 goto done;
3024 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07003025 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003026 goto done;
3027 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07003028 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003029 } else if (0 != strcmp (cpinResult, "READY")) {
3030 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07003031 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003032 goto done;
3033 }
3034
3035 at_response_free(p_response);
3036 p_response = NULL;
3037 cpinResult = NULL;
3038
Jim Kayed2d82012017-10-06 14:17:47 -07003039 ret = (sState == RADIO_STATE_ON) ? SIM_READY : SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003040
3041done:
3042 at_response_free(p_response);
3043 return ret;
3044}
3045
3046
3047/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07003048 * Get the current card status.
3049 *
3050 * This must be freed using freeCardStatus.
3051 * @return: On success returns RIL_E_SUCCESS
3052 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003053static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003054 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07003055 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07003056 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3057 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003058 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07003059 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3060 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003061 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07003062 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3063 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003064 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07003065 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3066 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003067 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07003068 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3069 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07003070 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07003071 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003072 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3073 // RUIM_ABSENT = 6
3074 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3075 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3076 // RUIM_NOT_READY = 7
3077 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3078 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3079 // RUIM_READY = 8
3080 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3081 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3082 // RUIM_PIN = 9
3083 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3084 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3085 // RUIM_PUK = 10
3086 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3087 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
3088 // RUIM_NETWORK_PERSONALIZATION = 11
3089 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
bohu076e6872017-07-02 21:33:28 -07003090 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3091 // ISIM_ABSENT = 12
3092 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3093 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3094 // ISIM_NOT_READY = 13
3095 { RIL_APPTYPE_ISIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3096 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3097 // ISIM_READY = 14
3098 { RIL_APPTYPE_ISIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3099 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3100 // ISIM_PIN = 15
3101 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3102 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3103 // ISIM_PUK = 16
3104 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3105 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
3106 // ISIM_NETWORK_PERSONALIZATION = 17
3107 { RIL_APPTYPE_ISIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
3108 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3109
Wink Savillef6aa7c12009-04-30 14:20:52 -07003110 };
3111 RIL_CardState card_state;
3112 int num_apps;
3113
3114 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07003115 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003116 card_state = RIL_CARDSTATE_ABSENT;
3117 num_apps = 0;
3118 } else {
3119 card_state = RIL_CARDSTATE_PRESENT;
bohu076e6872017-07-02 21:33:28 -07003120 num_apps = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003121 }
3122
3123 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003124 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07003125 p_card_status->card_state = card_state;
3126 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
kun.tang7bb00222017-07-12 11:41:43 +08003127 p_card_status->gsm_umts_subscription_app_index = -1;
3128 p_card_status->cdma_subscription_app_index = -1;
3129 p_card_status->ims_subscription_app_index = -1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003130 p_card_status->num_applications = num_apps;
3131
3132 // Initialize application status
3133 int i;
3134 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07003135 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07003136 }
3137
3138 // Pickup the appropriate application status
3139 // that reflects sim_status for gsm.
3140 if (num_apps != 0) {
bohu076e6872017-07-02 21:33:28 -07003141 p_card_status->num_applications = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003142 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003143 p_card_status->cdma_subscription_app_index = 1;
bohu076e6872017-07-02 21:33:28 -07003144 p_card_status->ims_subscription_app_index = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003145
3146 // Get the correct app status
3147 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003148 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
bohu076e6872017-07-02 21:33:28 -07003149 p_card_status->applications[2] = app_status_array[sim_status + ISIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07003150 }
3151
3152 *pp_card_status = p_card_status;
3153 return RIL_E_SUCCESS;
3154}
3155
3156/**
3157 * Free the card status returned by getCardStatus
3158 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003159static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003160 free(p_card_status);
3161}
3162
3163/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003164 * SIM ready means any commands that access the SIM will work, including:
3165 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
3166 * (all SMS-related commands)
3167 */
3168
Mark Salyzynba58c202014-03-12 15:20:22 -07003169static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003170{
3171 ATResponse *p_response;
3172 int ret;
3173
Naveen Kalla2baf7232016-10-11 13:49:20 -07003174 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003175 // no longer valid to poll
3176 return;
3177 }
3178
3179 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07003180 case SIM_ABSENT:
3181 case SIM_PIN:
3182 case SIM_PUK:
3183 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003184 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003185 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003186 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003187 return;
3188
John Wang309ac292009-07-30 14:53:23 -07003189 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003190 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
3191 return;
3192
John Wang309ac292009-07-30 14:53:23 -07003193 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003194 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003195 onSIMReady();
3196 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003197 return;
3198 }
3199}
3200
3201/** returns 1 if on, 0 if off, and -1 on error */
3202static int isRadioOn()
3203{
3204 ATResponse *p_response = NULL;
3205 int err;
3206 char *line;
3207 char ret;
3208
3209 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
3210
3211 if (err < 0 || p_response->success == 0) {
3212 // assume radio is off
3213 goto error;
3214 }
3215
3216 line = p_response->p_intermediates->line;
3217
3218 err = at_tok_start(&line);
3219 if (err < 0) goto error;
3220
3221 err = at_tok_nextbool(&line, &ret);
3222 if (err < 0) goto error;
3223
3224 at_response_free(p_response);
3225
3226 return (int)ret;
3227
3228error:
3229
3230 at_response_free(p_response);
3231 return -1;
3232}
3233
3234/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003235 * Parse the response generated by a +CTEC AT command
3236 * The values read from the response are stored in current and preferred.
3237 * Both current and preferred may be null. The corresponding value is ignored in that case.
3238 *
3239 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
3240 * 1 if the response includes the current technology only
3241 * 0 if the response includes both current technology and preferred mode
3242 */
3243int parse_technology_response( const char *response, int *current, int32_t *preferred )
3244{
3245 int err;
3246 char *line, *p;
3247 int ct;
3248 int32_t pt = 0;
3249 char *str_pt;
3250
3251 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08003252 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003253 err = at_tok_start(&p);
3254 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003255 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003256 free(line);
3257 return -1;
3258 }
3259
3260 err = at_tok_nextint(&p, &ct);
3261 if (err) {
3262 free(line);
3263 return -1;
3264 }
3265 if (current) *current = ct;
3266
Wink Saville4dcab4f2012-11-19 16:05:13 -08003267 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003268
3269 err = at_tok_nexthexint(&p, &pt);
3270 if (err) {
3271 free(line);
3272 return 1;
3273 }
3274 if (preferred) {
3275 *preferred = pt;
3276 }
3277 free(line);
3278
3279 return 0;
3280}
3281
Mark Salyzynba58c202014-03-12 15:20:22 -07003282int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003283{
3284 ATResponse *p_response;
3285 int err, val, techs = 0;
3286 char *tok;
3287 char *line;
3288
Wink Saville4dcab4f2012-11-19 16:05:13 -08003289 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003290 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3291 if (err || !p_response->success)
3292 goto error;
3293 line = p_response->p_intermediates->line;
3294 err = at_tok_start(&line);
3295 if (err || !at_tok_hasmore(&line))
3296 goto error;
3297 while (!at_tok_nextint(&line, &val)) {
3298 techs |= ( 1 << val );
3299 }
3300 if (supported) *supported = techs;
3301 return 0;
3302error:
3303 at_response_free(p_response);
3304 return -1;
3305}
3306
3307/**
3308 * query_ctec. Send the +CTEC AT command to the modem to query the current
3309 * and preferred modes. It leaves values in the addresses pointed to by
3310 * current and preferred. If any of those pointers are NULL, the corresponding value
3311 * is ignored, but the return value will still reflect if retreiving and parsing of the
3312 * values suceeded.
3313 *
3314 * @mdm Currently unused
3315 * @current A pointer to store the current mode returned by the modem. May be null.
3316 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3317 * @return -1 on error (or failure to parse)
3318 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3319 * 0 if both current and preferred were returned correctly
3320 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003321int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003322{
3323 ATResponse *response = NULL;
3324 int err;
3325 int res;
3326
Colin Cross5cba4882014-02-05 18:55:42 -08003327 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003328 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3329 if (!err && response->success) {
3330 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3331 at_response_free(response);
3332 return res;
3333 }
Colin Cross5cba4882014-02-05 18:55:42 -08003334 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003335 at_response_free(response);
3336 return -1;
3337}
3338
3339int is_multimode_modem(ModemInfo *mdm)
3340{
3341 ATResponse *response;
3342 int err;
3343 char *line;
3344 int tech;
3345 int32_t preferred;
3346
3347 if (query_ctec(mdm, &tech, &preferred) == 0) {
3348 mdm->currentTech = tech;
3349 mdm->preferredNetworkMode = preferred;
3350 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3351 return 0;
3352 }
3353 return 1;
3354 }
3355 return 0;
3356}
3357
3358/**
3359 * Find out if our modem is GSM, CDMA or both (Multimode)
3360 */
3361static void probeForModemMode(ModemInfo *info)
3362{
3363 ATResponse *response;
3364 int err;
3365 assert (info);
3366 // Currently, our only known multimode modem is qemu's android modem,
3367 // which implements the AT+CTEC command to query and set mode.
3368 // Try that first
3369
3370 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003371 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003372 info->supportedTechs, info->currentTech);
3373 return;
3374 }
3375
3376 /* Being here means that our modem is not multimode */
3377 info->isMultimode = 0;
3378
3379 /* CDMA Modems implement the AT+WNAM command */
3380 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3381 if (!err && response->success) {
3382 at_response_free(response);
3383 // TODO: find out if we really support EvDo
3384 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3385 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003386 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003387 return;
3388 }
3389 if (!err) at_response_free(response);
3390 // TODO: find out if modem really supports WCDMA/LTE
3391 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3392 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003393 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003394}
3395
3396/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003397 * Initialize everything that can be configured while we're still in
3398 * AT+CFUN=0
3399 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003400static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003401{
3402 ATResponse *p_response = NULL;
3403 int err;
3404
3405 setRadioState (RADIO_STATE_OFF);
3406
3407 at_handshake();
3408
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003409 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003410 /* note: we don't check errors here. Everything important will
3411 be handled in onATTimeout and onATReaderClosed */
3412
3413 /* atchannel is tolerant of echo but it must */
3414 /* have verbose result codes */
3415 at_send_command("ATE0Q0V1", NULL);
3416
3417 /* No auto-answer */
3418 at_send_command("ATS0=0", NULL);
3419
3420 /* Extended errors */
3421 at_send_command("AT+CMEE=1", NULL);
3422
3423 /* Network registration events */
3424 err = at_send_command("AT+CREG=2", &p_response);
3425
3426 /* some handsets -- in tethered mode -- don't support CREG=2 */
3427 if (err < 0 || p_response->success == 0) {
3428 at_send_command("AT+CREG=1", NULL);
3429 }
3430
3431 at_response_free(p_response);
3432
3433 /* GPRS registration events */
3434 at_send_command("AT+CGREG=1", NULL);
3435
3436 /* Call Waiting notifications */
3437 at_send_command("AT+CCWA=1", NULL);
3438
3439 /* Alternating voice/data off */
3440 at_send_command("AT+CMOD=0", NULL);
3441
3442 /* Not muted */
3443 at_send_command("AT+CMUT=0", NULL);
3444
3445 /* +CSSU unsolicited supp service notifications */
3446 at_send_command("AT+CSSN=0,1", NULL);
3447
3448 /* no connected line identification */
3449 at_send_command("AT+COLP=0", NULL);
3450
3451 /* HEX character set */
3452 at_send_command("AT+CSCS=\"HEX\"", NULL);
3453
3454 /* USSD unsolicited */
3455 at_send_command("AT+CUSD=1", NULL);
3456
3457 /* Enable +CGEV GPRS event notifications, but don't buffer */
3458 at_send_command("AT+CGEREP=1,0", NULL);
3459
3460 /* SMS PDU mode */
3461 at_send_command("AT+CMGF=0", NULL);
3462
3463#ifdef USE_TI_COMMANDS
3464
3465 at_send_command("AT%CPI=3", NULL);
3466
3467 /* TI specific -- notifications when SMS is ready (currently ignored) */
3468 at_send_command("AT%CSTAT=1", NULL);
3469
3470#endif /* USE_TI_COMMANDS */
3471
3472
3473 /* assume radio is off on error */
3474 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003475 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003476 }
3477}
3478
3479static void waitForClose()
3480{
3481 pthread_mutex_lock(&s_state_mutex);
3482
3483 while (s_closed == 0) {
3484 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3485 }
3486
3487 pthread_mutex_unlock(&s_state_mutex);
3488}
3489
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003490static void sendUnsolImsNetworkStateChanged()
3491{
3492#if 0 // to be used when unsol is changed to return data.
3493 int reply[2];
3494 reply[0] = s_ims_registered;
3495 reply[1] = s_ims_services;
3496 reply[1] = s_ims_format;
3497#endif
3498 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3499 NULL, 0);
3500}
3501
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003502/**
3503 * Called by atchannel when an unsolicited line appears
3504 * This is called on atchannel's reader thread. AT commands may
3505 * not be issued here
3506 */
3507static void onUnsolicited (const char *s, const char *sms_pdu)
3508{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003509 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003510 int err;
3511
3512 /* Ignore unsolicited responses until we're initialized.
3513 * This is OK because the RIL library will poll for initial state
3514 */
3515 if (sState == RADIO_STATE_UNAVAILABLE) {
3516 return;
3517 }
3518
3519 if (strStartsWith(s, "%CTZV:")) {
3520 /* TI specific -- NITZ time */
3521 char *response;
3522
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003523 line = p = strdup(s);
3524 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003525
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003526 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003527
3528 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003529 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003530 } else {
3531 RIL_onUnsolicitedResponse (
3532 RIL_UNSOL_NITZ_TIME_RECEIVED,
3533 response, strlen(response));
3534 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003535 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003536 } else if (strStartsWith(s,"+CRING:")
3537 || strStartsWith(s,"RING")
3538 || strStartsWith(s,"NO CARRIER")
3539 || strStartsWith(s,"+CCWA")
3540 ) {
3541 RIL_onUnsolicitedResponse (
3542 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3543 NULL, 0);
3544#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003545 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003546#endif /* WORKAROUND_FAKE_CGEV */
3547 } else if (strStartsWith(s,"+CREG:")
3548 || strStartsWith(s,"+CGREG:")
3549 ) {
3550 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003551 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003552 NULL, 0);
3553#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003554 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003555#endif /* WORKAROUND_FAKE_CGEV */
3556 } else if (strStartsWith(s, "+CMT:")) {
3557 RIL_onUnsolicitedResponse (
3558 RIL_UNSOL_RESPONSE_NEW_SMS,
3559 sms_pdu, strlen(sms_pdu));
3560 } else if (strStartsWith(s, "+CDS:")) {
3561 RIL_onUnsolicitedResponse (
3562 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3563 sms_pdu, strlen(sms_pdu));
3564 } else if (strStartsWith(s, "+CGEV:")) {
3565 /* Really, we can ignore NW CLASS and ME CLASS events here,
3566 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003567 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003568 */
3569 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003570 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003571#ifdef WORKAROUND_FAKE_CGEV
3572 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003573 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003574#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003575 } else if (strStartsWith(s, "+CTEC: ")) {
3576 int tech, mask;
3577 switch (parse_technology_response(s, &tech, NULL))
3578 {
3579 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003580 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003581 break;
3582 case 1: // current mode correctly parsed
3583 case 0: // preferred mode correctly parsed
3584 mask = 1 << tech;
3585 if (mask != MDM_GSM && mask != MDM_CDMA &&
3586 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003587 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003588 } else {
3589 setRadioTechnology(sMdmInfo, tech);
3590 }
3591 break;
3592 }
3593 } else if (strStartsWith(s, "+CCSS: ")) {
3594 int source = 0;
3595 line = p = strdup(s);
3596 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003597 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003598 return;
3599 }
3600 if (at_tok_start(&p) < 0) {
3601 free(line);
3602 return;
3603 }
3604 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003605 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003606 free(line);
3607 return;
3608 }
3609 SSOURCE(sMdmInfo) = source;
3610 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3611 &source, sizeof(source));
3612 } else if (strStartsWith(s, "+WSOS: ")) {
3613 char state = 0;
3614 int unsol;
3615 line = p = strdup(s);
3616 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003617 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003618 return;
3619 }
3620 if (at_tok_start(&p) < 0) {
3621 free(line);
3622 return;
3623 }
3624 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003625 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003626 free(line);
3627 return;
3628 }
3629 free(line);
3630
3631 unsol = state ?
3632 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3633
3634 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3635
3636 } else if (strStartsWith(s, "+WPRL: ")) {
3637 int version = -1;
3638 line = p = strdup(s);
3639 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003640 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003641 return;
3642 }
3643 if (at_tok_start(&p) < 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 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003649 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003650 free(line);
3651 return;
3652 }
3653 free(line);
3654 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3655 } else if (strStartsWith(s, "+CFUN: 0")) {
3656 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003657 }
3658}
3659
3660/* Called on command or reader thread */
3661static void onATReaderClosed()
3662{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003663 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003664 at_close();
3665 s_closed = 1;
3666
3667 setRadioState (RADIO_STATE_UNAVAILABLE);
3668}
3669
3670/* Called on command thread */
3671static void onATTimeout()
3672{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003673 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003674 at_close();
3675
3676 s_closed = 1;
3677
3678 /* FIXME cause a radio reset here */
3679
3680 setRadioState (RADIO_STATE_UNAVAILABLE);
3681}
3682
Etan Cohend3652192014-06-20 08:28:44 -07003683/* Called to pass hardware configuration information to telephony
3684 * framework.
3685 */
3686static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3687{
3688 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3689}
3690
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003691static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003692{
3693#ifdef RIL_SHLIB
3694 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3695#else
3696 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3697 exit(-1);
3698#endif
3699}
3700
3701static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003702mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003703{
3704 int fd;
3705 int ret;
3706
3707 AT_DUMP("== ", "entering mainLoop()", -1 );
3708 at_set_on_reader_closed(onATReaderClosed);
3709 at_set_on_timeout(onATTimeout);
3710
3711 for (;;) {
3712 fd = -1;
3713 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003714 if (isInEmulator()) {
3715 fd = qemu_pipe_open("pipe:qemud:gsm");
3716 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003717 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003718 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003719 fd = socket_local_client(s_device_path,
3720 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3721 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003722 } else if (s_device_path != NULL) {
3723 fd = open (s_device_path, O_RDWR);
3724 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3725 /* disable echo on serial ports */
3726 struct termios ios;
3727 tcgetattr( fd, &ios );
3728 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3729 tcsetattr( fd, TCSANOW, &ios );
3730 }
3731 }
3732
3733 if (fd < 0) {
3734 perror ("opening AT interface. retrying...");
3735 sleep(10);
3736 /* never returns */
3737 }
3738 }
3739
3740 s_closed = 0;
3741 ret = at_open(fd, onUnsolicited);
3742
3743 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003744 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003745 return 0;
3746 }
3747
3748 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3749
3750 // Give initializeCallback a chance to dispatched, since
3751 // we don't presently have a cancellation mechanism
3752 sleep(1);
3753
3754 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003755 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003756 }
3757}
3758
3759#ifdef RIL_SHLIB
3760
3761pthread_t s_tid_mainloop;
3762
3763const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3764{
3765 int ret;
3766 int fd = -1;
3767 int opt;
3768 pthread_attr_t attr;
3769
3770 s_rilenv = env;
3771
Sandeep Gutta11f27942014-07-10 05:00:25 +05303772 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003773 switch (opt) {
3774 case 'p':
3775 s_port = atoi(optarg);
3776 if (s_port == 0) {
3777 usage(argv[0]);
3778 return NULL;
3779 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003780 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003781 break;
3782
3783 case 'd':
3784 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003785 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003786 break;
3787
3788 case 's':
3789 s_device_path = optarg;
3790 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003791 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003792 break;
3793
Sandeep Gutta11f27942014-07-10 05:00:25 +05303794 case 'c':
3795 RLOGI("Client id received %s\n", optarg);
3796 break;
3797
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003798 default:
3799 usage(argv[0]);
3800 return NULL;
3801 }
3802 }
3803
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003804 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003805 usage(argv[0]);
3806 return NULL;
3807 }
3808
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003809 sMdmInfo = calloc(1, sizeof(ModemInfo));
3810 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003811 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003812 return NULL;
3813 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003814 pthread_attr_init (&attr);
3815 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3816 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3817
3818 return &s_callbacks;
3819}
3820#else /* RIL_SHLIB */
3821int main (int argc, char **argv)
3822{
3823 int ret;
3824 int fd = -1;
3825 int opt;
3826
3827 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3828 switch (opt) {
3829 case 'p':
3830 s_port = atoi(optarg);
3831 if (s_port == 0) {
3832 usage(argv[0]);
3833 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003834 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003835 break;
3836
3837 case 'd':
3838 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003839 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003840 break;
3841
3842 case 's':
3843 s_device_path = optarg;
3844 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003845 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003846 break;
3847
3848 default:
3849 usage(argv[0]);
3850 }
3851 }
3852
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003853 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003854 usage(argv[0]);
3855 }
3856
3857 RIL_register(&s_callbacks);
3858
3859 mainLoop(NULL);
3860
3861 return 0;
3862}
3863
3864#endif /* RIL_SHLIB */