blob: 1ba25088e9ccb216ac29fc967920405b60a1d17e [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>
37#include <cutils/sockets.h>
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +010038#include <sys/system_properties.h>
Weilun Du9f471e22017-02-07 10:47:19 -080039#include <termios.h>
bohuba723ce2017-03-29 12:20:46 -070040#include <qemu_pipe.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080041
Wink Saville9a9fbd22011-02-15 17:13:10 -080042#include "ril.h"
43
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080044#define LOG_TAG "RIL"
45#include <utils/Log.h>
46
Etan Cohend3652192014-06-20 08:28:44 -070047static void *noopRemoveWarning( void *a ) { return a; }
48#define RIL_UNUSED_PARM(a) noopRemoveWarning((void *)&(a));
49
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080050#define MAX_AT_RESPONSE 0x1000
51
Wink Savillef4c4d362009-04-02 01:37:03 -070052/* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
Bjoern Johanssona8bec892017-02-09 22:55:02 -080053// This is used if Wifi is not supported, plain old eth0
54#define PPP_TTY_PATH_ETH0 "eth0"
55// This is used if Wifi is supported to separate radio and wifi interface
56#define PPP_TTY_PATH_RADIO0 "radio0"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080057
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -070058// Default MTU value
59#define DEFAULT_MTU 1500
60
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080061#ifdef USE_TI_COMMANDS
62
63// Enable a workaround
64// 1) Make incoming call, do not answer
65// 2) Hangup remote end
66// Expected: call should disappear from CLCC line
67// Actual: Call shows as "ACTIVE" before disappearing
68#define WORKAROUND_ERRONEOUS_ANSWER 1
69
70// Some varients of the TI stack do not support the +CGEV unsolicited
71// response. However, they seem to send an unsolicited +CME ERROR: 150
72#define WORKAROUND_FAKE_CGEV 1
73#endif
74
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070075/* Modem Technology bits */
76#define MDM_GSM 0x01
77#define MDM_WCDMA 0x02
78#define MDM_CDMA 0x04
79#define MDM_EVDO 0x08
80#define MDM_LTE 0x10
81
82typedef struct {
83 int supportedTechs; // Bitmask of supported Modem Technology bits
84 int currentTech; // Technology the modem is currently using (in the format used by modem)
85 int isMultimode;
86
87 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
88 // in which the byte number from LSB to MSB give the priority.
89 //
90 // |MSB| | |LSB
91 // value: |00 |00 |00 |00
92 // byte #: |3 |2 |1 |0
93 //
94 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
95 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
96 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
97 int32_t preferredNetworkMode;
98 int subscription_source;
99
100} ModemInfo;
101
102static ModemInfo *sMdmInfo;
103// TECH returns the current technology in the format used by the modem.
104// It can be used as an l-value
105#define TECH(mdminfo) ((mdminfo)->currentTech)
106// TECH_BIT returns the bitmask equivalent of the current tech
107#define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
108#define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
109#define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
110#define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
111// CDMA Subscription Source
112#define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
113
114static int net2modem[] = {
115 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
116 MDM_GSM, // 1 - GSM only
117 MDM_WCDMA, // 2 - WCDMA only
118 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
119 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
120 MDM_CDMA, // 5 - CDMA only
121 MDM_EVDO, // 6 - EvDo only
122 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
123 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
124 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
125 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
126 MDM_LTE, // 11 - LTE only
127};
128
129static int32_t net2pmask[] = {
130 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
131 MDM_GSM, // 1 - GSM only
132 MDM_WCDMA, // 2 - WCDMA only
133 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
134 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
135 MDM_CDMA, // 5 - CDMA only
136 MDM_EVDO, // 6 - EvDo only
137 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
138 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
139 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
140 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
141 MDM_LTE, // 11 - LTE only
142};
143
144static int is3gpp2(int radioTech) {
145 switch (radioTech) {
146 case RADIO_TECH_IS95A:
147 case RADIO_TECH_IS95B:
148 case RADIO_TECH_1xRTT:
149 case RADIO_TECH_EVDO_0:
150 case RADIO_TECH_EVDO_A:
151 case RADIO_TECH_EVDO_B:
152 case RADIO_TECH_EHRPD:
153 return 1;
154 default:
155 return 0;
156 }
157}
158
John Wang309ac292009-07-30 14:53:23 -0700159typedef enum {
160 SIM_ABSENT = 0,
161 SIM_NOT_READY = 1,
Naveen Kalla2baf7232016-10-11 13:49:20 -0700162 SIM_READY = 2,
John Wang309ac292009-07-30 14:53:23 -0700163 SIM_PIN = 3,
164 SIM_PUK = 4,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700165 SIM_NETWORK_PERSONALIZATION = 5,
166 RUIM_ABSENT = 6,
167 RUIM_NOT_READY = 7,
168 RUIM_READY = 8,
169 RUIM_PIN = 9,
170 RUIM_PUK = 10,
bohu076e6872017-07-02 21:33:28 -0700171 RUIM_NETWORK_PERSONALIZATION = 11,
172 ISIM_ABSENT = 12,
173 ISIM_NOT_READY = 13,
174 ISIM_READY = 14,
175 ISIM_PIN = 15,
176 ISIM_PUK = 16,
177 ISIM_NETWORK_PERSONALIZATION = 17,
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100178} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -0700179
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800180static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
181static RIL_RadioState currentState();
182static int onSupports (int requestCode);
183static void onCancel (RIL_Token t);
184static const char *getVersion();
185static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -0700186static SIM_Status getSIMStatus();
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700187static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
188static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -0700189static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800190
191extern const char * requestToString(int request);
192
193/*** Static Variables ***/
194static const RIL_RadioFunctions s_callbacks = {
195 RIL_VERSION,
196 onRequest,
197 currentState,
198 onSupports,
199 onCancel,
200 getVersion
201};
202
203#ifdef RIL_SHLIB
204static const struct RIL_Env *s_rilenv;
205
206#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
207#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
208#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
209#endif
210
211static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
212
213static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
214static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
215
216static int s_port = -1;
217static const char * s_device_path = NULL;
218static int s_device_socket = 0;
219
220/* trigger change to this with s_state_cond */
221static int s_closed = 0;
222
223static int sFD; /* file desc of AT channel */
224static char sATBuffer[MAX_AT_RESPONSE+1];
225static char *sATBufferCur = NULL;
226
227static const struct timeval TIMEVAL_SIMPOLL = {1,0};
228static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
229static const struct timeval TIMEVAL_0 = {0,0};
230
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -0700231static int s_ims_registered = 0; // 0==unregistered
232static int s_ims_services = 1; // & 0x1 == sms over ims supported
233static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
234static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
235static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
236static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
237static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
238
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800239#ifdef WORKAROUND_ERRONEOUS_ANSWER
240// Max number of times we'll try to repoll when we think
241// we have a AT+CLCC race condition
242#define REPOLL_CALLS_COUNT_MAX 4
243
244// Line index that was incoming or waiting at last poll, or -1 for none
245static int s_incomingOrWaitingLine = -1;
246// Number of times we've asked for a repoll of AT+CLCC
247static int s_repollCallsCount = 0;
248// Should we expect a call to be answered in the next CLCC?
249static int s_expectAnswer = 0;
250#endif /* WORKAROUND_ERRONEOUS_ANSWER */
251
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200252
Wink Saville8a9e0212013-04-09 12:11:38 -0700253static int s_cell_info_rate_ms = INT_MAX;
254static int s_mcc = 0;
255static int s_mnc = 0;
256static int s_lac = 0;
257static int s_cid = 0;
258
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800259static void pollSIMState (void *param);
260static void setRadioState(RIL_RadioState newState);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700261static void setRadioTechnology(ModemInfo *mdm, int newtech);
262static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
263static int parse_technology_response(const char *response, int *current, int32_t *preferred);
264static int techFromModemType(int mdmtype);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800265
266static int clccStateToRILState(int state, RIL_CallState *p_state)
267
268{
269 switch(state) {
270 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
271 case 1: *p_state = RIL_CALL_HOLDING; return 0;
272 case 2: *p_state = RIL_CALL_DIALING; return 0;
273 case 3: *p_state = RIL_CALL_ALERTING; return 0;
274 case 4: *p_state = RIL_CALL_INCOMING; return 0;
275 case 5: *p_state = RIL_CALL_WAITING; return 0;
276 default: return -1;
277 }
278}
279
280/**
281 * Note: directly modified line and has *p_call point directly into
282 * modified line
283 */
Wink Saville3d54e742009-05-18 18:00:44 -0700284static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800285{
286 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
287 // index,isMT,state,mode,isMpty(,number,TOA)?
288
289 int err;
290 int state;
291 int mode;
292
293 err = at_tok_start(&line);
294 if (err < 0) goto error;
295
296 err = at_tok_nextint(&line, &(p_call->index));
297 if (err < 0) goto error;
298
299 err = at_tok_nextbool(&line, &(p_call->isMT));
300 if (err < 0) goto error;
301
302 err = at_tok_nextint(&line, &state);
303 if (err < 0) goto error;
304
305 err = clccStateToRILState(state, &(p_call->state));
306 if (err < 0) goto error;
307
308 err = at_tok_nextint(&line, &mode);
309 if (err < 0) goto error;
310
311 p_call->isVoice = (mode == 0);
312
313 err = at_tok_nextbool(&line, &(p_call->isMpty));
314 if (err < 0) goto error;
315
316 if (at_tok_hasmore(&line)) {
317 err = at_tok_nextstr(&line, &(p_call->number));
318
319 /* tolerate null here */
320 if (err < 0) return 0;
321
322 // Some lame implementations return strings
323 // like "NOT AVAILABLE" in the CLCC line
324 if (p_call->number != NULL
325 && 0 == strspn(p_call->number, "+0123456789")
326 ) {
327 p_call->number = NULL;
328 }
329
330 err = at_tok_nextint(&line, &p_call->toa);
331 if (err < 0) goto error;
332 }
333
Wink Saville74fa3882009-12-22 15:35:41 -0800334 p_call->uusInfo = NULL;
335
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800336 return 0;
337
338error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800339 RLOGE("invalid CLCC line\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800340 return -1;
341}
342
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -0700343static int parseSimResponseLine(char* line, RIL_SIM_IO_Response* response) {
344 int err;
345
346 err = at_tok_start(&line);
347 if (err < 0) return err;
348 err = at_tok_nextint(&line, &response->sw1);
349 if (err < 0) return err;
350 err = at_tok_nextint(&line, &response->sw2);
351 if (err < 0) return err;
352
353 if (at_tok_hasmore(&line)) {
354 err = at_tok_nextstr(&line, &response->simResponse);
355 if (err < 0) return err;
356 }
357 return 0;
358}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800359
360/** do post-AT+CFUN=1 initialization */
361static void onRadioPowerOn()
362{
363#ifdef USE_TI_COMMANDS
364 /* Must be after CFUN=1 */
365 /* TI specific -- notifications for CPHS things such */
366 /* as CPHS message waiting indicator */
367
368 at_send_command("AT%CPHS=1", NULL);
369
370 /* TI specific -- enable NITZ unsol notifs */
371 at_send_command("AT%CTZV=1", NULL);
372#endif
373
374 pollSIMState(NULL);
375}
376
377/** do post- SIM ready initialization */
378static void onSIMReady()
379{
380 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
381 /*
382 * Always send SMS messages directly to the TE
383 *
384 * mode = 1 // discard when link is reserved (link should never be
385 * reserved)
386 * mt = 2 // most messages routed to TE
387 * bm = 2 // new cell BM's routed to TE
388 * ds = 1 // Status reports routed to TE
389 * bfr = 1 // flush buffer
390 */
391 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
392}
393
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700394static void requestRadioPower(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800395{
396 int onOff;
397
398 int err;
399 ATResponse *p_response = NULL;
400
401 assert (datalen >= sizeof(int *));
402 onOff = ((int *)data)[0];
403
404 if (onOff == 0 && sState != RADIO_STATE_OFF) {
405 err = at_send_command("AT+CFUN=0", &p_response);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800406 if (err < 0 || p_response->success == 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800407 setRadioState(RADIO_STATE_OFF);
408 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
409 err = at_send_command("AT+CFUN=1", &p_response);
410 if (err < 0|| p_response->success == 0) {
411 // Some stacks return an error when there is no SIM,
412 // but they really turn the RF portion on
413 // So, if we get an error, let's check to see if it
414 // turned on anyway
415
416 if (isRadioOn() != 1) {
417 goto error;
418 }
419 }
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700420 setRadioState(RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800421 }
422
423 at_response_free(p_response);
424 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
425 return;
426error:
427 at_response_free(p_response);
428 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
429}
430
Naveen Kallaa65a16a2014-07-31 16:48:31 -0700431static void requestShutdown(RIL_Token t)
432{
433 int onOff;
434
435 int err;
436 ATResponse *p_response = NULL;
437
438 if (sState != RADIO_STATE_OFF) {
439 err = at_send_command("AT+CFUN=0", &p_response);
440 setRadioState(RADIO_STATE_UNAVAILABLE);
441 }
442
443 at_response_free(p_response);
444 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
445 return;
446}
447
Wink Savillef4c4d362009-04-02 01:37:03 -0700448static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800449
Mark Salyzynba58c202014-03-12 15:20:22 -0700450static void onDataCallListChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800451{
Wink Savillef4c4d362009-04-02 01:37:03 -0700452 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800453}
454
Mark Salyzynba58c202014-03-12 15:20:22 -0700455static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800456{
Wink Savillef4c4d362009-04-02 01:37:03 -0700457 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800458}
459
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800460// Hang up, reject, conference, call waiting
461static void requestCallSelection(
462 void *data __unused, size_t datalen __unused, RIL_Token t, int request)
463{
464 // 3GPP 22.030 6.5.5
465 static char hangupWaiting[] = "AT+CHLD=0";
466 static char hangupForeground[] = "AT+CHLD=1";
467 static char switchWaiting[] = "AT+CHLD=2";
468 static char conference[] = "AT+CHLD=3";
469 static char reject[] = "ATH";
470
471 char* atCommand;
472
473 if (getSIMStatus() == SIM_ABSENT) {
474 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
475 return;
476 }
477
478 switch(request) {
479 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
480 // "Releases all held calls or sets User Determined User Busy
481 // (UDUB) for a waiting call."
482 atCommand = hangupWaiting;
483 break;
484 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
485 // "Releases all active calls (if any exist) and accepts
486 // the other (held or waiting) call."
487 atCommand = hangupForeground;
488 break;
489 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
490 // "Places all active calls (if any exist) on hold and accepts
491 // the other (held or waiting) call."
492 atCommand = switchWaiting;
493#ifdef WORKAROUND_ERRONEOUS_ANSWER
494 s_expectAnswer = 1;
495#endif /* WORKAROUND_ERRONEOUS_ANSWER */
496 break;
497 case RIL_REQUEST_CONFERENCE:
498 // "Adds a held call to the conversation"
499 atCommand = conference;
500 break;
501 case RIL_REQUEST_UDUB:
502 // User determined user busy (reject)
503 atCommand = reject;
504 break;
505 default:
506 assert(0);
507 }
508 at_send_command(atCommand, NULL);
509 // Success or failure is ignored by the upper layer here.
510 // It will call GET_CURRENT_CALLS and determine success that way.
511 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
512}
513
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800514static bool hasWifi()
515{
516 char propValue[PROP_VALUE_MAX];
517 return __system_property_get("ro.kernel.qemu.wifi", propValue) != 0 &&
518 strcmp("1", propValue) == 0;
519}
520
Wink Savillef4c4d362009-04-02 01:37:03 -0700521static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800522{
523 ATResponse *p_response;
524 ATLine *p_cur;
525 int err;
526 int n = 0;
527 char *out;
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800528 char propValue[PROP_VALUE_MAX];
529 bool has_wifi = hasWifi();
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800530
531 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
532 if (err != 0 || p_response->success == 0) {
533 if (t != NULL)
534 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
535 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700536 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800537 NULL, 0);
538 return;
539 }
540
541 for (p_cur = p_response->p_intermediates; p_cur != NULL;
542 p_cur = p_cur->p_next)
543 n++;
544
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700545 RIL_Data_Call_Response_v11 *responses =
546 alloca(n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800547
548 int i;
549 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800550 responses[i].status = -1;
Wink Saville250eb3c2011-06-22 09:11:34 -0700551 responses[i].suggestedRetryTime = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800552 responses[i].cid = -1;
553 responses[i].active = -1;
554 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800555 responses[i].ifname = "";
556 responses[i].addresses = "";
557 responses[i].dnses = "";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700558 responses[i].gateways = "";
Etan Cohend3652192014-06-20 08:28:44 -0700559 responses[i].pcscf = "";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700560 responses[i].mtu = 0;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800561 }
562
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700563 RIL_Data_Call_Response_v11 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800564 for (p_cur = p_response->p_intermediates; p_cur != NULL;
565 p_cur = p_cur->p_next) {
566 char *line = p_cur->line;
567
568 err = at_tok_start(&line);
569 if (err < 0)
570 goto error;
571
572 err = at_tok_nextint(&line, &response->cid);
573 if (err < 0)
574 goto error;
575
576 err = at_tok_nextint(&line, &response->active);
577 if (err < 0)
578 goto error;
579
580 response++;
581 }
582
583 at_response_free(p_response);
584
585 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
586 if (err != 0 || p_response->success == 0) {
587 if (t != NULL)
588 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
589 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700590 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800591 NULL, 0);
592 return;
593 }
594
595 for (p_cur = p_response->p_intermediates; p_cur != NULL;
596 p_cur = p_cur->p_next) {
597 char *line = p_cur->line;
598 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800599
600 err = at_tok_start(&line);
601 if (err < 0)
602 goto error;
603
604 err = at_tok_nextint(&line, &cid);
605 if (err < 0)
606 goto error;
607
608 for (i = 0; i < n; i++) {
609 if (responses[i].cid == cid)
610 break;
611 }
612
613 if (i >= n) {
614 /* details for a context we didn't hear about in the last request */
615 continue;
616 }
617
Wink Saville43808972011-01-13 17:39:51 -0800618 // Assume no error
619 responses[i].status = 0;
620
621 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800622 err = at_tok_nextstr(&line, &out);
623 if (err < 0)
624 goto error;
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700625
626 int type_size = strlen(out) + 1;
627 responses[i].type = alloca(type_size);
628 strlcpy(responses[i].type, out, type_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800629
Wink Saville43808972011-01-13 17:39:51 -0800630 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800631 err = at_tok_nextstr(&line, &out);
632 if (err < 0)
633 goto error;
634
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800635 if (has_wifi) {
636 int ifname_size = strlen(PPP_TTY_PATH_RADIO0) + 1;
637 responses[i].ifname = alloca(ifname_size);
638 strlcpy(responses[i].ifname, PPP_TTY_PATH_RADIO0, ifname_size);
639 } else {
640 int ifname_size = strlen(PPP_TTY_PATH_ETH0) + 1;
641 responses[i].ifname = alloca(ifname_size);
642 strlcpy(responses[i].ifname, PPP_TTY_PATH_ETH0, ifname_size);
643 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800644
645 err = at_tok_nextstr(&line, &out);
646 if (err < 0)
647 goto error;
648
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700649 int addresses_size = strlen(out) + 1;
650 responses[i].addresses = alloca(addresses_size);
651 strlcpy(responses[i].addresses, out, addresses_size);
Wink Saville43808972011-01-13 17:39:51 -0800652
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200653 if (isInEmulator()) {
654 /* We are in the emulator - the dns servers are listed
655 * by the following system properties, setup in
656 * /system/etc/init.goldfish.sh:
657 * - net.eth0.dns1
658 * - net.eth0.dns2
659 * - net.eth0.dns3
660 * - net.eth0.dns4
661 */
662 const int dnslist_sz = 128;
663 char* dnslist = alloca(dnslist_sz);
664 const char* separator = "";
665 int nn;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100666
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200667 dnslist[0] = 0;
668 for (nn = 1; nn <= 4; nn++) {
669 /* Probe net.eth0.dns<n> */
670 char propName[PROP_NAME_MAX];
671 char propValue[PROP_VALUE_MAX];
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100672
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200673 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100674
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200675 /* Ignore if undefined */
676 if (__system_property_get(propName, propValue) == 0) {
677 continue;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100678 }
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700679
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200680 /* Append the DNS IP address */
681 strlcat(dnslist, separator, dnslist_sz);
682 strlcat(dnslist, propValue, dnslist_sz);
683 separator = " ";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100684 }
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200685 responses[i].dnses = dnslist;
686
Bjoern Johanssona8bec892017-02-09 22:55:02 -0800687 /* There is only one gateway in the emulator. If WiFi is
688 * configured the interface visible to RIL will be behind a NAT
689 * where the gateway is different. */
690 responses[i].gateways = has_wifi ? "192.168.200.1" : "10.0.2.2";
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200691 responses[i].mtu = DEFAULT_MTU;
692 }
693 else {
694 /* I don't know where we are, so use the public Google DNS
695 * servers by default and no gateway.
696 */
697 responses[i].dnses = "8.8.8.8 8.8.4.4";
698 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100699 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800700 }
701
702 at_response_free(p_response);
703
704 if (t != NULL)
705 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700706 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800707 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700708 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800709 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700710 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800711
712 return;
713
714error:
715 if (t != NULL)
716 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
717 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700718 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800719 NULL, 0);
720
721 at_response_free(p_response);
722}
723
724static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700725 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800726{
727 int err;
728 ATResponse *p_response = NULL;
729 int response = 0;
730 char *line;
731
732 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
733
734 if (err < 0 || p_response->success == 0) {
735 goto error;
736 }
737
738 line = p_response->p_intermediates->line;
739
740 err = at_tok_start(&line);
741
742 if (err < 0) {
743 goto error;
744 }
745
746 err = at_tok_nextint(&line, &response);
747
748 if (err < 0) {
749 goto error;
750 }
751
752 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
753 at_response_free(p_response);
754 return;
755error:
756 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800757 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800758 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
759}
760
Mark Salyzynba58c202014-03-12 15:20:22 -0700761static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800762{
763 RIL_onUnsolicitedResponse (
764 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
765 NULL, 0);
766}
767
Mark Salyzynba58c202014-03-12 15:20:22 -0700768static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800769{
770 int err;
771 ATResponse *p_response;
772 ATLine *p_cur;
773 int countCalls;
774 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700775 RIL_Call *p_calls;
776 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800777 int i;
778 int needRepoll = 0;
779
780#ifdef WORKAROUND_ERRONEOUS_ANSWER
781 int prevIncomingOrWaitingLine;
782
783 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
784 s_incomingOrWaitingLine = -1;
785#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
786
787 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
788
789 if (err != 0 || p_response->success == 0) {
790 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
791 return;
792 }
793
794 /* count the calls */
795 for (countCalls = 0, p_cur = p_response->p_intermediates
796 ; p_cur != NULL
797 ; p_cur = p_cur->p_next
798 ) {
799 countCalls++;
800 }
801
802 /* yes, there's an array of pointers and then an array of structures */
803
Wink Saville3d54e742009-05-18 18:00:44 -0700804 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
805 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
806 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800807
808 /* init the pointer array */
809 for(i = 0; i < countCalls ; i++) {
810 pp_calls[i] = &(p_calls[i]);
811 }
812
813 for (countValidCalls = 0, p_cur = p_response->p_intermediates
814 ; p_cur != NULL
815 ; p_cur = p_cur->p_next
816 ) {
817 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
818
819 if (err != 0) {
820 continue;
821 }
822
823#ifdef WORKAROUND_ERRONEOUS_ANSWER
824 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
825 || p_calls[countValidCalls].state == RIL_CALL_WAITING
826 ) {
827 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
828 }
829#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
830
831 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
832 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
833 ) {
834 needRepoll = 1;
835 }
836
837 countValidCalls++;
838 }
839
840#ifdef WORKAROUND_ERRONEOUS_ANSWER
841 // Basically:
842 // A call was incoming or waiting
843 // Now it's marked as active
844 // But we never answered it
845 //
846 // This is probably a bug, and the call will probably
847 // disappear from the call list in the next poll
848 if (prevIncomingOrWaitingLine >= 0
849 && s_incomingOrWaitingLine < 0
850 && s_expectAnswer == 0
851 ) {
852 for (i = 0; i < countValidCalls ; i++) {
853
854 if (p_calls[i].index == prevIncomingOrWaitingLine
855 && p_calls[i].state == RIL_CALL_ACTIVE
856 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
857 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800858 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800859 "Hit WORKAROUND_ERRONOUS_ANSWER case."
860 " Repoll count: %d\n", s_repollCallsCount);
861 s_repollCallsCount++;
862 goto error;
863 }
864 }
865 }
866
867 s_expectAnswer = 0;
868 s_repollCallsCount = 0;
869#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
870
Wink Saville3d54e742009-05-18 18:00:44 -0700871 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
872 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800873
874 at_response_free(p_response);
875
876#ifdef POLL_CALL_STATE
877 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
878 // smd, so we're forced to poll until the call ends.
879#else
880 if (needRepoll) {
881#endif
882 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
883 }
884
885 return;
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700886#ifdef WORKAROUND_ERRONEOUS_ANSWER
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800887error:
888 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
889 at_response_free(p_response);
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700890#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800891}
892
Mark Salyzynba58c202014-03-12 15:20:22 -0700893static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800894{
895 RIL_Dial *p_dial;
896 char *cmd;
897 const char *clir;
898 int ret;
899
900 p_dial = (RIL_Dial *)data;
901
902 switch (p_dial->clir) {
903 case 1: clir = "I"; break; /*invocation*/
904 case 2: clir = "i"; break; /*suppression*/
905 default:
906 case 0: clir = ""; break; /*subscription default*/
907 }
908
909 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
910
911 ret = at_send_command(cmd, NULL);
912
913 free(cmd);
914
915 /* success or failure is ignored by the upper layer here.
916 it will call GET_CURRENT_CALLS and determine success that way */
917 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
918}
919
Mark Salyzynba58c202014-03-12 15:20:22 -0700920static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800921{
922 RIL_SMS_WriteArgs *p_args;
923 char *cmd;
924 int length;
925 int err;
926 ATResponse *p_response = NULL;
927
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800928 if (getSIMStatus() == SIM_ABSENT) {
929 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
930 return;
931 }
932
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800933 p_args = (RIL_SMS_WriteArgs *)data;
934
935 length = strlen(p_args->pdu)/2;
936 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
937
938 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
939
940 if (err != 0 || p_response->success == 0) goto error;
941
942 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
943 at_response_free(p_response);
944
945 return;
946error:
947 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
948 at_response_free(p_response);
949}
950
Mark Salyzynba58c202014-03-12 15:20:22 -0700951static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800952{
953 int *p_line;
954
955 int ret;
956 char *cmd;
957
Jim Kayeb6f3f7e2017-12-07 14:06:22 -0800958 if (getSIMStatus() == SIM_ABSENT) {
959 RIL_onRequestComplete(t, RIL_E_MODEM_ERR, NULL, 0);
960 return;
961 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800962 p_line = (int *)data;
963
964 // 3GPP 22.030 6.5.5
965 // "Releases a specific active call X"
966 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
967
968 ret = at_send_command(cmd, NULL);
969
970 free(cmd);
971
972 /* success or failure is ignored by the upper layer here.
973 it will call GET_CURRENT_CALLS and determine success that way */
974 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
975}
976
Mark Salyzynba58c202014-03-12 15:20:22 -0700977static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800978{
979 ATResponse *p_response = NULL;
980 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800981 char *line;
Jim Kayedc9f31b2017-04-03 13:43:31 -0700982 int count = 0;
983 // Accept a response that is at least v6, and up to v10
984 int minNumOfElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
985 int maxNumOfElements=sizeof(RIL_SignalStrength_v10)/sizeof(int);
986 int response[maxNumOfElements];
987
988 memset(response, 0, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800989
990 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
991
992 if (err < 0 || p_response->success == 0) {
993 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
994 goto error;
995 }
996
997 line = p_response->p_intermediates->line;
998
999 err = at_tok_start(&line);
1000 if (err < 0) goto error;
1001
Jim Kayedc9f31b2017-04-03 13:43:31 -07001002 for (count = 0; count < maxNumOfElements; count++) {
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -07001003 err = at_tok_nextint(&line, &(response[count]));
Jim Kayedc9f31b2017-04-03 13:43:31 -07001004 if (err < 0 && count < minNumOfElements) goto error;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -07001005 }
Chih-Wei Huang28059052012-04-30 01:13:27 +08001006
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -07001007 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001008
1009 at_response_free(p_response);
1010 return;
1011
1012error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001013 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001014 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1015 at_response_free(p_response);
1016}
1017
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001018/**
1019 * networkModePossible. Decides whether the network mode is appropriate for the
1020 * specified modem
1021 */
1022static int networkModePossible(ModemInfo *mdm, int nm)
1023{
1024 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
1025 return 1;
1026 }
1027 return 0;
1028}
Mark Salyzynba58c202014-03-12 15:20:22 -07001029static void requestSetPreferredNetworkType( int request __unused, void *data,
1030 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001031{
1032 ATResponse *p_response = NULL;
1033 char *cmd = NULL;
1034 int value = *(int *)data;
1035 int current, old;
1036 int err;
1037 int32_t preferred = net2pmask[value];
1038
Wink Saville4dcab4f2012-11-19 16:05:13 -08001039 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001040 if (!networkModePossible(sMdmInfo, value)) {
1041 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
1042 return;
1043 }
1044 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
1045 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1046 return;
1047 }
1048 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -08001049 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001050 if (old != preferred) {
1051 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -08001052 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001053 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
1054 free(cmd);
1055 if (err || !p_response->success) {
1056 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1057 return;
1058 }
1059 PREFERRED_NETWORK(sMdmInfo) = value;
1060 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
1061 int current;
1062 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
1063 switch (res) {
1064 case -1: // Error or unable to parse
1065 break;
1066 case 1: // Only able to parse current
1067 case 0: // Both current and preferred were parsed
1068 setRadioTechnology(sMdmInfo, current);
1069 break;
1070 }
1071 }
1072 }
1073 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1074}
1075
Mark Salyzynba58c202014-03-12 15:20:22 -07001076static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
1077 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001078{
1079 int preferred;
1080 unsigned i;
1081
1082 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
1083 case -1: // Error or unable to parse
1084 case 1: // Only able to parse current
1085 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1086 break;
1087 case 0: // Both current and preferred were parsed
1088 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
1089 if (preferred == net2pmask[i]) {
1090 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1091 return;
1092 }
1093 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001094 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001095 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1096 break;
1097 }
1098
1099}
1100
Mark Salyzynba58c202014-03-12 15:20:22 -07001101static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1102 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001103{
1104 int err;
1105 char * responseStr;
1106 ATResponse *p_response = NULL;
1107 const char *cmd;
1108 char *line;
1109
1110 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1111 if (err < 0 || !p_response->success) goto error;
1112 line = p_response->p_intermediates->line;
1113 err = at_tok_start(&line);
1114 if (err < 0) goto error;
1115 err = at_tok_nextstr(&line, &responseStr);
1116 if (err < 0 || !responseStr) goto error;
1117 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1118 at_response_free(p_response);
1119 return;
1120error:
1121 at_response_free(p_response);
1122 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1123}
1124
Mark Salyzynba58c202014-03-12 15:20:22 -07001125static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1126 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001127{
1128 int err;
1129 char * responseStr;
1130 ATResponse *p_response = NULL;
1131 const char *cmd;
1132 const char *prefix;
1133 char *line, *p;
1134 int commas;
1135 int skip;
1136 int count = 4;
1137
1138 // Fixed values. TODO: query modem
1139 responseStr = strdup("1.0.0.0");
1140 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1141 free(responseStr);
1142}
1143
Mark Salyzynba58c202014-03-12 15:20:22 -07001144static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1145 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001146{
1147 int err;
1148 int response[4];
1149 char * responseStr[4];
1150 ATResponse *p_response = NULL;
1151 const char *cmd;
1152 const char *prefix;
1153 char *line, *p;
1154 int commas;
1155 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001156 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001157
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001158 // Fixed values. TODO: Query modem
1159 responseStr[0] = "----";
1160 responseStr[1] = "----";
1161 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001162
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001163 err = at_send_command_numeric("AT+CGSN", &p_response);
1164 if (err < 0 || p_response->success == 0) {
1165 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1166 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001167 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001168 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001169 }
1170
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001171 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1172 at_response_free(p_response);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001173}
1174
Mark Salyzynba58c202014-03-12 15:20:22 -07001175static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1176 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001177{
1178 int err;
1179 int *ss = (int *)data;
1180 ATResponse *p_response = NULL;
1181 char *cmd = NULL;
1182 char *line = NULL;
1183 int response;
1184
1185 asprintf(&cmd, "AT+CCSS?");
1186 if (!cmd) goto error;
1187
1188 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1189 if (err < 0 || !p_response->success)
1190 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001191
1192 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001193 err = at_tok_start(&line);
1194 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001195
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001196 err = at_tok_nextint(&line, &response);
1197 free(cmd);
1198 cmd = NULL;
1199
1200 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1201
1202 return;
1203error:
1204 free(cmd);
1205 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1206}
1207
Mark Salyzynba58c202014-03-12 15:20:22 -07001208static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001209 size_t datalen, RIL_Token t)
1210{
1211 int err;
1212 int *ss = (int *)data;
1213 ATResponse *p_response = NULL;
1214 char *cmd = NULL;
1215
1216 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001217 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001218 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1219 return;
1220 }
1221 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1222 if (!cmd) goto error;
1223
1224 err = at_send_command(cmd, &p_response);
1225 if (err < 0 || !p_response->success)
1226 goto error;
1227 free(cmd);
1228 cmd = NULL;
1229
1230 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1231
1232 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1233
1234 return;
1235error:
1236 free(cmd);
1237 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1238}
1239
Mark Salyzynba58c202014-03-12 15:20:22 -07001240static void requestCdmaSubscription(int request __unused, void *data __unused,
1241 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001242{
1243 int err;
1244 int response[5];
1245 char * responseStr[5];
1246 ATResponse *p_response = NULL;
1247 const char *cmd;
1248 const char *prefix;
1249 char *line, *p;
1250 int commas;
1251 int skip;
1252 int count = 5;
1253
1254 // Fixed values. TODO: Query modem
1255 responseStr[0] = "8587777777"; // MDN
1256 responseStr[1] = "1"; // SID
1257 responseStr[2] = "1"; // NID
1258 responseStr[3] = "8587777777"; // MIN
1259 responseStr[4] = "1"; // PRL Version
1260 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001261}
1262
Mark Salyzynba58c202014-03-12 15:20:22 -07001263static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1264 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001265{
1266 int roaming_pref = -1;
1267 ATResponse *p_response = NULL;
1268 char *line;
1269 int res;
1270
1271 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1272 if (res < 0 || !p_response->success) {
1273 goto error;
1274 }
1275 line = p_response->p_intermediates->line;
1276
1277 res = at_tok_start(&line);
1278 if (res < 0) goto error;
1279
1280 res = at_tok_nextint(&line, &roaming_pref);
1281 if (res < 0) goto error;
1282
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001283 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001284 return;
1285error:
1286 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1287}
1288
Mark Salyzynba58c202014-03-12 15:20:22 -07001289static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1290 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001291{
1292 int *pref = (int *)data;
1293 ATResponse *p_response = NULL;
1294 char *line;
1295 int res;
1296 char *cmd = NULL;
1297
1298 asprintf(&cmd, "AT+WRMP=%d", *pref);
1299 if (cmd == NULL) goto error;
1300
1301 res = at_send_command(cmd, &p_response);
1302 if (res < 0 || !p_response->success)
1303 goto error;
1304
1305 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1306 free(cmd);
1307 return;
1308error:
1309 free(cmd);
1310 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1311}
1312
1313static int parseRegistrationState(char *str, int *type, int *items, int **response)
1314{
1315 int err;
1316 char *line = str, *p;
1317 int *resp = NULL;
1318 int skip;
1319 int count = 3;
1320 int commas;
1321
Wink Saville4dcab4f2012-11-19 16:05:13 -08001322 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001323 err = at_tok_start(&line);
1324 if (err < 0) goto error;
1325
1326 /* Ok you have to be careful here
1327 * The solicited version of the CREG response is
1328 * +CREG: n, stat, [lac, cid]
1329 * and the unsolicited version is
1330 * +CREG: stat, [lac, cid]
1331 * The <n> parameter is basically "is unsolicited creg on?"
1332 * which it should always be
1333 *
1334 * Now we should normally get the solicited version here,
1335 * but the unsolicited version could have snuck in
1336 * so we have to handle both
1337 *
1338 * Also since the LAC and CID are only reported when registered,
1339 * we can have 1, 2, 3, or 4 arguments here
1340 *
1341 * finally, a +CGREG: answer may have a fifth value that corresponds
1342 * to the network type, as in;
1343 *
1344 * +CGREG: n, stat [,lac, cid [,networkType]]
1345 */
1346
1347 /* count number of commas */
1348 commas = 0;
1349 for (p = line ; *p != '\0' ;p++) {
1350 if (*p == ',') commas++;
1351 }
1352
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001353 resp = (int *)calloc(commas + 1, sizeof(int));
1354 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001355 switch (commas) {
1356 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001357 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001358 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001359 resp[1] = -1;
1360 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001361 break;
1362
1363 case 1: /* +CREG: <n>, <stat> */
1364 err = at_tok_nextint(&line, &skip);
1365 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001366 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001367 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001368 resp[1] = -1;
1369 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001370 if (err < 0) goto error;
1371 break;
1372
1373 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001374 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001375 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001376 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001377 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001378 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001379 if (err < 0) goto error;
1380 break;
1381 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1382 err = at_tok_nextint(&line, &skip);
1383 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001384 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001385 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001386 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001387 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001388 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001389 if (err < 0) goto error;
1390 break;
1391 /* special case for CGREG, there is a fourth parameter
1392 * that is the network type (unknown/gprs/edge/umts)
1393 */
1394 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1395 err = at_tok_nextint(&line, &skip);
1396 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001397 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001398 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001399 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001400 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001401 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001402 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001403 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001404 if (err < 0) goto error;
1405 count = 4;
1406 break;
1407 default:
1408 goto error;
1409 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001410 s_lac = resp[1];
1411 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001412 if (response)
1413 *response = resp;
1414 if (items)
1415 *items = commas + 1;
1416 if (type)
1417 *type = techFromModemType(TECH(sMdmInfo));
1418 return 0;
1419error:
1420 free(resp);
1421 return -1;
1422}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001423
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001424#define REG_STATE_LEN 15
1425#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001426static void requestRegistrationState(int request, void *data __unused,
1427 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001428{
1429 int err;
1430 int *registration;
1431 char **responseStr = NULL;
1432 ATResponse *p_response = NULL;
1433 const char *cmd;
1434 const char *prefix;
1435 char *line;
1436 int i = 0, j, numElements = 0;
1437 int count = 3;
1438 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001439
Wink Saville4dcab4f2012-11-19 16:05:13 -08001440 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001441 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1442 cmd = "AT+CREG?";
1443 prefix = "+CREG:";
1444 numElements = REG_STATE_LEN;
1445 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1446 cmd = "AT+CGREG?";
1447 prefix = "+CGREG:";
1448 numElements = REG_DATA_STATE_LEN;
1449 } else {
1450 assert(0);
1451 goto error;
1452 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001453
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001454 err = at_send_command_singleline(cmd, prefix, &p_response);
1455
1456 if (err != 0) goto error;
1457
1458 line = p_response->p_intermediates->line;
1459
1460 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1461
1462 responseStr = malloc(numElements * sizeof(char *));
1463 if (!responseStr) goto error;
1464 memset(responseStr, 0, numElements * sizeof(char *));
1465 /**
1466 * The first '4' bytes for both registration states remain the same.
1467 * But if the request is 'DATA_REGISTRATION_STATE',
1468 * the 5th and 6th byte(s) are optional.
1469 */
1470 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001471 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001472 // TODO: Query modem
1473 startfrom = 3;
1474 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1475 asprintf(&responseStr[3], "8"); // EvDo revA
1476 asprintf(&responseStr[4], "1"); // BSID
1477 asprintf(&responseStr[5], "123"); // Latitude
1478 asprintf(&responseStr[6], "222"); // Longitude
1479 asprintf(&responseStr[7], "0"); // CSS Indicator
1480 asprintf(&responseStr[8], "4"); // SID
1481 asprintf(&responseStr[9], "65535"); // NID
1482 asprintf(&responseStr[10], "0"); // Roaming indicator
1483 asprintf(&responseStr[11], "1"); // System is in PRL
1484 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1485 asprintf(&responseStr[13], "0"); // Reason for denial
1486 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1487 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1488 asprintf(&responseStr[3], "8"); // Available data radio technology
1489 }
1490 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001491 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001492 startfrom = 0;
1493 asprintf(&responseStr[1], "%x", registration[1]);
1494 asprintf(&responseStr[2], "%x", registration[2]);
1495 if (count > 3)
1496 asprintf(&responseStr[3], "%d", registration[3]);
1497 }
1498 asprintf(&responseStr[0], "%d", registration[0]);
1499
1500 /**
1501 * Optional bytes for DATA_REGISTRATION_STATE request
1502 * 4th byte : Registration denial code
1503 * 5th byte : The max. number of simultaneous Data Calls
1504 */
1505 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1506 // asprintf(&responseStr[4], "3");
1507 // asprintf(&responseStr[5], "1");
1508 }
1509
1510 for (j = startfrom; j < numElements; j++) {
1511 if (!responseStr[i]) goto error;
1512 }
1513 free(registration);
1514 registration = NULL;
1515
1516 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1517 for (j = 0; j < numElements; j++ ) {
1518 free(responseStr[j]);
1519 responseStr[j] = NULL;
1520 }
1521 free(responseStr);
1522 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001523 at_response_free(p_response);
1524
1525 return;
1526error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001527 if (responseStr) {
1528 for (j = 0; j < numElements; j++) {
1529 free(responseStr[j]);
1530 responseStr[j] = NULL;
1531 }
1532 free(responseStr);
1533 responseStr = NULL;
1534 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001535 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001536 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1537 at_response_free(p_response);
1538}
1539
Mark Salyzynba58c202014-03-12 15:20:22 -07001540static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001541{
1542 int err;
1543 int i;
1544 int skip;
1545 ATLine *p_cur;
1546 char *response[3];
1547
1548 memset(response, 0, sizeof(response));
1549
1550 ATResponse *p_response = NULL;
1551
1552 err = at_send_command_multiline(
1553 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1554 "+COPS:", &p_response);
1555
1556 /* we expect 3 lines here:
1557 * +COPS: 0,0,"T - Mobile"
1558 * +COPS: 0,1,"TMO"
1559 * +COPS: 0,2,"310170"
1560 */
1561
1562 if (err != 0) goto error;
1563
1564 for (i = 0, p_cur = p_response->p_intermediates
1565 ; p_cur != NULL
1566 ; p_cur = p_cur->p_next, i++
1567 ) {
1568 char *line = p_cur->line;
1569
1570 err = at_tok_start(&line);
1571 if (err < 0) goto error;
1572
1573 err = at_tok_nextint(&line, &skip);
1574 if (err < 0) goto error;
1575
1576 // If we're unregistered, we may just get
1577 // a "+COPS: 0" response
1578 if (!at_tok_hasmore(&line)) {
1579 response[i] = NULL;
1580 continue;
1581 }
1582
1583 err = at_tok_nextint(&line, &skip);
1584 if (err < 0) goto error;
1585
1586 // a "+COPS: 0, n" response is also possible
1587 if (!at_tok_hasmore(&line)) {
1588 response[i] = NULL;
1589 continue;
1590 }
1591
1592 err = at_tok_nextstr(&line, &(response[i]));
1593 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001594 // Simple assumption that mcc and mnc are 3 digits each
1595 if (strlen(response[i]) == 6) {
1596 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1597 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1598 }
1599 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001600 }
1601
1602 if (i != 3) {
1603 /* expect 3 lines exactly */
1604 goto error;
1605 }
1606
1607 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1608 at_response_free(p_response);
1609
1610 return;
1611error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001612 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001613 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1614 at_response_free(p_response);
1615}
1616
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001617static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1618{
1619 int err = 1; // Set to go to error:
1620 RIL_SMS_Response response;
1621 RIL_CDMA_SMS_Message* rcsm;
1622
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001623 if (getSIMStatus() == SIM_ABSENT) {
1624 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
1625 return;
1626 }
1627
Mark Salyzynba58c202014-03-12 15:20:22 -07001628 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001629 datalen, sizeof(RIL_CDMA_SMS_Message));
1630
1631 // verify data content to test marshalling/unmarshalling:
1632 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001633 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001634 uServicecategory=%d, sAddress.digit_mode=%d, \
1635 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1636 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1637 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1638 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1639
1640 if (err != 0) goto error;
1641
1642 // Cdma Send SMS implementation will go here:
1643 // But it is not implemented yet.
1644
1645 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001646 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001647 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1648 return;
1649
1650error:
1651 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001652 response.messageRef = -1;
1653 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001654}
1655
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001656static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1657{
1658 int err;
1659 const char *smsc;
1660 const char *pdu;
1661 int tpLayerLength;
1662 char *cmd1, *cmd2;
1663 RIL_SMS_Response response;
1664 ATResponse *p_response = NULL;
1665
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001666 if (getSIMStatus() == SIM_ABSENT) {
1667 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
1668 return;
1669 }
1670
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001671 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001672 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001673
1674 if (s_ims_gsm_fail != 0) goto error;
1675 if (s_ims_gsm_retry != 0) goto error2;
1676
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001677 smsc = ((const char **)data)[0];
1678 pdu = ((const char **)data)[1];
1679
1680 tpLayerLength = strlen(pdu)/2;
1681
1682 // "NULL for default SMSC"
1683 if (smsc == NULL) {
1684 smsc= "00";
1685 }
1686
1687 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1688 asprintf(&cmd2, "%s%s", smsc, pdu);
1689
1690 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1691
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001692 free(cmd1);
1693 free(cmd2);
1694
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001695 if (err != 0 || p_response->success == 0) goto error;
1696
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001697 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001698 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001699 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1700 at_response_free(p_response);
1701
1702 return;
1703error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001704 response.messageRef = -2;
1705 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001706 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001707 return;
1708error2:
1709 // send retry error.
1710 response.messageRef = -1;
1711 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1712 at_response_free(p_response);
1713 return;
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08001714}
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001715
1716static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1717{
1718 RIL_IMS_SMS_Message *p_args;
1719 RIL_SMS_Response response;
1720
1721 memset(&response, 0, sizeof(response));
1722
Mark Salyzynba58c202014-03-12 15:20:22 -07001723 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001724 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1725 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1726 datalen, s_ims_registered, s_ims_services, s_ims_format,
1727 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1728 s_ims_gsm_retry);
1729
1730 // figure out if this is gsm/cdma format
1731 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1732 p_args = (RIL_IMS_SMS_Message *)data;
1733
1734 if (0 != s_ims_cause_perm_failure ) goto error;
1735
1736 // want to fail over ims and this is first request over ims
1737 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1738
1739 if (RADIO_TECH_3GPP == p_args->tech) {
1740 return requestSendSMS(p_args->message.gsmMessage,
1741 datalen - sizeof(RIL_RadioTechnologyFamily),
1742 t);
1743 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1744 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1745 datalen - sizeof(RIL_RadioTechnologyFamily),
1746 t);
1747 } else {
1748 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1749 }
1750
1751error:
1752 response.messageRef = -2;
1753 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1754 return;
1755
1756error2:
1757 response.messageRef = -1;
1758 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001759}
1760
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001761static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1762{
1763 ATResponse *p_response = NULL;
1764 int32_t session_id;
1765 int err;
1766 char cmd[32];
1767 char dummy;
1768 char *line;
1769
1770 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1771 if (data == NULL || datalen == 0 || datalen > 16) {
1772 ALOGE("Invalid data passed to requestSimOpenChannel");
1773 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1774 return;
1775 }
1776
1777 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1778
1779 err = at_send_command_numeric(cmd, &p_response);
1780 if (err < 0 || p_response == NULL || p_response->success == 0) {
1781 ALOGE("Error %d opening logical channel: %d",
1782 err, p_response ? p_response->success : 0);
1783 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1784 at_response_free(p_response);
1785 return;
1786 }
1787
1788 // Ensure integer only by scanning for an extra char but expect one result
1789 line = p_response->p_intermediates->line;
1790 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1791 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1792 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1793 return;
1794 }
1795
1796 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1797 at_response_free(p_response);
1798}
1799
1800static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1801{
1802 ATResponse *p_response = NULL;
1803 int32_t session_id;
1804 int err;
1805 char cmd[32];
1806
1807 if (data == NULL || datalen != sizeof(session_id)) {
1808 ALOGE("Invalid data passed to requestSimCloseChannel");
1809 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1810 return;
1811 }
1812 session_id = ((int32_t *)data)[0];
1813 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1814 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1815
1816 if (err < 0 || p_response == NULL || p_response->success == 0) {
1817 ALOGE("Error %d closing logical channel %d: %d",
1818 err, session_id, p_response ? p_response->success : 0);
1819 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1820 at_response_free(p_response);
1821 return;
1822 }
1823
1824 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1825
1826 at_response_free(p_response);
1827}
1828
1829static void requestSimTransmitApduChannel(void *data,
1830 size_t datalen,
1831 RIL_Token t)
1832{
1833 ATResponse *p_response = NULL;
1834 int err;
1835 char *cmd;
1836 char *line;
1837 size_t cmd_size;
1838 RIL_SIM_IO_Response sim_response;
1839 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1840
1841 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1842 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1843 return;
1844 }
1845
1846 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
Wei Wang9cec1e02017-02-08 14:37:37 -08001847 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001848 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1849 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1850
1851 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1852 free(cmd);
1853 if (err < 0 || p_response == NULL || p_response->success == 0) {
1854 ALOGE("Error %d transmitting APDU: %d",
1855 err, p_response ? p_response->success : 0);
1856 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1857 at_response_free(p_response);
1858 return;
1859 }
1860
1861 line = p_response->p_intermediates->line;
1862 err = parseSimResponseLine(line, &sim_response);
1863
1864 if (err == 0) {
1865 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1866 &sim_response, sizeof(sim_response));
1867 } else {
1868 ALOGE("Error %d parsing SIM response line: %s", err, line);
1869 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1870 }
1871 at_response_free(p_response);
1872}
1873
Wink Savillef4c4d362009-04-02 01:37:03 -07001874static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001875{
1876 const char *apn;
1877 char *cmd;
1878 int err;
1879 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001880
Wink Savillef4c4d362009-04-02 01:37:03 -07001881 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001882
1883#ifdef USE_TI_COMMANDS
1884 // Config for multislot class 10 (probably default anyway eh?)
1885 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1886 NULL);
1887
1888 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1889#endif /* USE_TI_COMMANDS */
1890
1891 int fd, qmistatus;
1892 size_t cur = 0;
1893 size_t len;
1894 ssize_t written, rlen;
1895 char status[32] = {0};
1896 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001897 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001898
Wink Saville4dcab4f2012-11-19 16:05:13 -08001899 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001900
1901 fd = open ("/dev/qmi", O_RDWR);
1902 if (fd >= 0) { /* the device doesn't exist on the emulator */
1903
Wink Saville4dcab4f2012-11-19 16:05:13 -08001904 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001905 asprintf(&cmd, "up:%s", apn);
1906 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001907
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001908 while (cur < len) {
1909 do {
1910 written = write (fd, cmd + cur, len - cur);
1911 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001912
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001913 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001914 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001915 close(fd);
1916 goto error;
1917 }
1918
1919 cur += written;
1920 }
1921
1922 // wait for interface to come online
1923
1924 do {
1925 sleep(1);
1926 do {
1927 rlen = read(fd, status, 31);
1928 } while (rlen < 0 && errno == EINTR);
1929
1930 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001931 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001932 close(fd);
1933 goto error;
1934 } else {
1935 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001936 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001937 }
1938 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1939
1940 close(fd);
1941
1942 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001943 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001944 goto error;
1945 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001946
1947 qmistatus = system("netcfg rmnet0 dhcp");
1948
Wink Saville4dcab4f2012-11-19 16:05:13 -08001949 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001950
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001951 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001952
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001953 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001954
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001955 if (datalen > 6 * sizeof(char *)) {
1956 pdp_type = ((const char **)data)[6];
1957 } else {
1958 pdp_type = "IP";
1959 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001960
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001961 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001962 //FIXME check for error here
1963 err = at_send_command(cmd, NULL);
1964 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001965
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001966 // Set required QoS params to default
1967 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001968
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001969 // Set minimum QoS params to default
1970 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001971
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001972 // packet-domain event reporting
1973 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001974
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001975 // Hangup anything that's happening there now
1976 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001977
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001978 // Start data on PDP context 1
1979 err = at_send_command("ATD*99***1#", &p_response);
1980
1981 if (err < 0 || p_response->success == 0) {
1982 goto error;
1983 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001984 }
1985
Wink Saville43808972011-01-13 17:39:51 -08001986 requestOrSendDataCallList(&t);
1987
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001988 at_response_free(p_response);
1989
1990 return;
1991error:
1992 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1993 at_response_free(p_response);
1994
1995}
1996
Mark Salyzynba58c202014-03-12 15:20:22 -07001997static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001998{
1999 int ackSuccess;
2000 int err;
2001
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002002 if (getSIMStatus() == SIM_ABSENT) {
2003 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2004 return;
2005 }
2006
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002007 ackSuccess = ((int *)data)[0];
2008
2009 if (ackSuccess == 1) {
2010 err = at_send_command("AT+CNMA=1", NULL);
2011 } else if (ackSuccess == 0) {
2012 err = at_send_command("AT+CNMA=2", NULL);
2013 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002014 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002015 goto error;
2016 }
2017
2018 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2019error:
2020 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2021
2022}
2023
Mark Salyzynba58c202014-03-12 15:20:22 -07002024static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002025{
2026 ATResponse *p_response = NULL;
2027 RIL_SIM_IO_Response sr;
2028 int err;
2029 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002030 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002031 char *line;
2032
2033 memset(&sr, 0, sizeof(sr));
2034
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002035 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002036
2037 /* FIXME handle pin2 */
2038
2039 if (p_args->data == NULL) {
2040 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
2041 p_args->command, p_args->fileid,
2042 p_args->p1, p_args->p2, p_args->p3);
2043 } else {
2044 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
2045 p_args->command, p_args->fileid,
2046 p_args->p1, p_args->p2, p_args->p3, p_args->data);
2047 }
2048
2049 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
2050
2051 if (err < 0 || p_response->success == 0) {
2052 goto error;
2053 }
2054
2055 line = p_response->p_intermediates->line;
2056
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002057 err = parseSimResponseLine(line, &sr);
2058 if (err < 0) {
2059 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002060 }
2061
2062 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
2063 at_response_free(p_response);
2064 free(cmd);
2065
2066 return;
2067error:
2068 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2069 at_response_free(p_response);
2070 free(cmd);
2071
2072}
2073
2074static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
2075{
2076 ATResponse *p_response = NULL;
2077 int err;
2078 char* cmd = NULL;
2079 const char** strings = (const char**)data;;
2080
2081 if ( datalen == sizeof(char*) ) {
2082 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
2083 } else if ( datalen == 2*sizeof(char*) ) {
2084 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
2085 } else
2086 goto error;
2087
2088 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
2089 free(cmd);
2090
2091 if (err < 0 || p_response->success == 0) {
2092error:
2093 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
2094 } else {
2095 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2096 }
2097 at_response_free(p_response);
2098}
2099
2100
Mark Salyzynba58c202014-03-12 15:20:22 -07002101static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002102{
2103 const char *ussdRequest;
2104
2105 ussdRequest = (char *)(data);
2106
2107
2108 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2109
2110// @@@ TODO
2111
2112}
2113
Mark Salyzynba58c202014-03-12 15:20:22 -07002114static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002115{
2116 int err;
2117 ATResponse *p_response = NULL;
2118
2119 err = at_send_command("AT+WSOS=0", &p_response);
2120
2121 if (err < 0 || p_response->success == 0) {
2122 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2123 return;
2124 }
2125
2126 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2127}
2128
2129// TODO: Use all radio types
2130static int techFromModemType(int mdmtype)
2131{
2132 int ret = -1;
2133 switch (1 << mdmtype) {
2134 case MDM_CDMA:
2135 ret = RADIO_TECH_1xRTT;
2136 break;
2137 case MDM_EVDO:
2138 ret = RADIO_TECH_EVDO_A;
2139 break;
2140 case MDM_GSM:
2141 ret = RADIO_TECH_GPRS;
2142 break;
2143 case MDM_WCDMA:
2144 ret = RADIO_TECH_HSPA;
2145 break;
2146 case MDM_LTE:
2147 ret = RADIO_TECH_LTE;
2148 break;
2149 }
2150 return ret;
2151}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002152
Mark Salyzynba58c202014-03-12 15:20:22 -07002153static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002154{
2155 uint64_t curTime = ril_nano_time();
2156 RIL_CellInfo ci[1] =
2157 {
2158 { // ci[0]
2159 1, // cellInfoType
2160 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002161 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002162 curTime - 1000, // Fake some time in the past
2163 { // union CellInfo
2164 { // RIL_CellInfoGsm gsm
2165 { // gsm.cellIdneityGsm
2166 s_mcc, // mcc
2167 s_mnc, // mnc
2168 s_lac, // lac
2169 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002170 },
2171 { // gsm.signalStrengthGsm
2172 10, // signalStrength
2173 0 // bitErrorRate
2174 }
2175 }
2176 }
2177 }
2178 };
2179
2180 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2181}
2182
2183
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002184static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002185{
2186 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2187 // will be sent.
2188 assert (datalen == sizeof(int));
2189 s_cell_info_rate_ms = ((int *)data)[0];
2190
2191 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2192}
2193
Etan Cohend3652192014-06-20 08:28:44 -07002194static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2195{
2196 // TODO - hook this up with real query/info from radio.
2197
2198 RIL_HardwareConfig hwCfg;
2199
2200 RIL_UNUSED_PARM(data);
2201 RIL_UNUSED_PARM(datalen);
2202
2203 hwCfg.type = -1;
2204
2205 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2206}
2207
Jim Kayed2d82012017-10-06 14:17:47 -07002208static void requestGetTtyMode(void *data, size_t datalen, RIL_Token t)
2209{
2210 int ttyModeResponse;
2211
2212 RIL_UNUSED_PARM(data);
2213 RIL_UNUSED_PARM(datalen);
2214
2215 ttyModeResponse = (getSIMStatus() == SIM_READY) ? 1 // TTY Full
2216 : 0; // TTY Off
2217
2218 RIL_onRequestComplete(t, RIL_E_SUCCESS, &ttyModeResponse, sizeof(ttyModeResponse));
2219}
2220
2221static void requestGetRadioCapability(void *data, size_t datalen, RIL_Token t)
2222{
2223 RIL_RadioCapability radioCapability;
2224
2225 RIL_UNUSED_PARM(data);
2226 RIL_UNUSED_PARM(datalen);
2227
2228 radioCapability.version = RIL_RADIO_CAPABILITY_VERSION;
2229 radioCapability.session = 0;
2230 radioCapability.phase = 0;
2231 radioCapability.rat = 0;
2232 radioCapability.logicalModemUuid[0] = '\0';
2233 radioCapability.status = RC_STATUS_SUCCESS;
2234
2235 RIL_onRequestComplete(t, RIL_E_SUCCESS, &radioCapability, sizeof(radioCapability));
2236}
2237
2238static void requestGetMute(void *data, size_t datalen, RIL_Token t)
2239{
2240 int muteResponse;
2241
2242 RIL_UNUSED_PARM(data);
2243 RIL_UNUSED_PARM(datalen);
2244
2245 muteResponse = 0; // Mute disabled
2246
2247 RIL_onRequestComplete(t, RIL_E_SUCCESS, &muteResponse, sizeof(muteResponse));
2248}
Etan Cohend3652192014-06-20 08:28:44 -07002249
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002250/*** Callback methods from the RIL library to us ***/
2251
2252/**
2253 * Call from RIL to us to make a RIL_REQUEST
2254 *
2255 * Must be completed with a call to RIL_onRequestComplete()
2256 *
2257 * RIL_onRequestComplete() may be called from any thread, before or after
2258 * this function returns.
2259 *
Weilun Du9f471e22017-02-07 10:47:19 -08002260 * Because onRequest function could be called from multiple different thread,
2261 * we must ensure that the underlying at_send_command_* function
2262 * is atomic.
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002263 */
2264static void
2265onRequest (int request, void *data, size_t datalen, RIL_Token t)
2266{
2267 ATResponse *p_response;
2268 int err;
2269
Wink Saville4dcab4f2012-11-19 16:05:13 -08002270 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002271
2272 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2273 * when RADIO_STATE_UNAVAILABLE.
2274 */
2275 if (sState == RADIO_STATE_UNAVAILABLE
2276 && request != RIL_REQUEST_GET_SIM_STATUS
2277 ) {
2278 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2279 return;
2280 }
2281
2282 /* Ignore all non-power requests when RADIO_STATE_OFF
2283 * (except RIL_REQUEST_GET_SIM_STATUS)
2284 */
Jim Kayed2d82012017-10-06 14:17:47 -07002285 if (sState == RADIO_STATE_OFF) {
2286 switch(request) {
2287 case RIL_REQUEST_BASEBAND_VERSION:
2288 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2289 case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:
2290 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2291 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2292 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2293 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2294 case RIL_REQUEST_DEVICE_IDENTITY:
2295 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2296 case RIL_REQUEST_GET_ACTIVITY_INFO:
2297 case RIL_REQUEST_GET_CARRIER_RESTRICTIONS:
2298 case RIL_REQUEST_GET_CURRENT_CALLS:
2299 case RIL_REQUEST_GET_IMEI:
2300 case RIL_REQUEST_GET_MUTE:
2301 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2302 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2303 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2304 case RIL_REQUEST_GET_SIM_STATUS:
2305 case RIL_REQUEST_NV_RESET_CONFIG:
2306 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2307 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2308 case RIL_REQUEST_QUERY_TTY_MODE:
2309 case RIL_REQUEST_RADIO_POWER:
2310 case RIL_REQUEST_SET_BAND_MODE:
2311 case RIL_REQUEST_SET_CARRIER_RESTRICTIONS:
2312 case RIL_REQUEST_SET_LOCATION_UPDATES:
2313 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2314 case RIL_REQUEST_SET_TTY_MODE:
2315 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2316 case RIL_REQUEST_STOP_LCE:
2317 case RIL_REQUEST_VOICE_RADIO_TECH:
2318 // Process all the above, even though the radio is off
2319 break;
2320
2321 default:
2322 // For all others, say NOT_AVAILABLE because the radio is off
2323 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2324 return;
2325 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002326 }
2327
2328 switch (request) {
2329 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002330 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002331 char *p_buffer;
2332 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002333
Wink Savillef6aa7c12009-04-30 14:20:52 -07002334 int result = getCardStatus(&p_card_status);
2335 if (result == RIL_E_SUCCESS) {
2336 p_buffer = (char *)p_card_status;
2337 buffer_size = sizeof(*p_card_status);
2338 } else {
2339 p_buffer = NULL;
2340 buffer_size = 0;
2341 }
2342 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2343 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002344 break;
2345 }
2346 case RIL_REQUEST_GET_CURRENT_CALLS:
2347 requestGetCurrentCalls(data, datalen, t);
2348 break;
2349 case RIL_REQUEST_DIAL:
2350 requestDial(data, datalen, t);
2351 break;
2352 case RIL_REQUEST_HANGUP:
2353 requestHangup(data, datalen, t);
2354 break;
2355 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002356 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002357 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002358 case RIL_REQUEST_CONFERENCE:
2359 case RIL_REQUEST_UDUB:
2360 requestCallSelection(data, datalen, t, request);
2361 break;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002362 case RIL_REQUEST_ANSWER:
2363 at_send_command("ATA", NULL);
2364
2365#ifdef WORKAROUND_ERRONEOUS_ANSWER
2366 s_expectAnswer = 1;
2367#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2368
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002369 if (getSIMStatus() != SIM_READY) {
2370 RIL_onRequestComplete(t, RIL_E_MODEM_ERR, NULL, 0);
2371 } else {
2372 // Success or failure is ignored by the upper layer here.
2373 // It will call GET_CURRENT_CALLS and determine success that way.
2374 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2375 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002376 break;
2377
2378 case RIL_REQUEST_SEPARATE_CONNECTION:
2379 {
2380 char cmd[12];
2381 int party = ((int*)data)[0];
2382
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002383 if (getSIMStatus() == SIM_ABSENT) {
2384 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2385 return;
2386 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002387 // Make sure that party is in a valid range.
2388 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2389 // It's sufficient for us to just make sure it's single digit.)
2390 if (party > 0 && party < 10) {
2391 sprintf(cmd, "AT+CHLD=2%d", party);
2392 at_send_command(cmd, NULL);
2393 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2394 } else {
2395 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2396 }
2397 }
2398 break;
2399
2400 case RIL_REQUEST_SIGNAL_STRENGTH:
2401 requestSignalStrength(data, datalen, t);
2402 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002403 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2404 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002405 requestRegistrationState(request, data, datalen, t);
2406 break;
2407 case RIL_REQUEST_OPERATOR:
2408 requestOperator(data, datalen, t);
2409 break;
2410 case RIL_REQUEST_RADIO_POWER:
2411 requestRadioPower(data, datalen, t);
2412 break;
2413 case RIL_REQUEST_DTMF: {
2414 char c = ((char *)data)[0];
2415 char *cmd;
2416 asprintf(&cmd, "AT+VTS=%c", (int)c);
2417 at_send_command(cmd, NULL);
2418 free(cmd);
2419 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2420 break;
2421 }
2422 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302423 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002424 requestSendSMS(data, datalen, t);
2425 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002426 case RIL_REQUEST_CDMA_SEND_SMS:
2427 requestCdmaSendSMS(data, datalen, t);
2428 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002429 case RIL_REQUEST_IMS_SEND_SMS:
2430 requestImsSendSMS(data, datalen, t);
2431 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002432 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2433 requestSimOpenChannel(data, datalen, t);
2434 break;
2435 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2436 requestSimCloseChannel(data, datalen, t);
2437 break;
2438 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2439 requestSimTransmitApduChannel(data, datalen, t);
2440 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002441 case RIL_REQUEST_SETUP_DATA_CALL:
2442 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002443 break;
2444 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2445 requestSMSAcknowledge(data, datalen, t);
2446 break;
2447
2448 case RIL_REQUEST_GET_IMSI:
2449 p_response = NULL;
2450 err = at_send_command_numeric("AT+CIMI", &p_response);
2451
2452 if (err < 0 || p_response->success == 0) {
2453 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2454 } else {
2455 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2456 p_response->p_intermediates->line, sizeof(char *));
2457 }
2458 at_response_free(p_response);
2459 break;
2460
2461 case RIL_REQUEST_GET_IMEI:
2462 p_response = NULL;
2463 err = at_send_command_numeric("AT+CGSN", &p_response);
2464
2465 if (err < 0 || p_response->success == 0) {
2466 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2467 } else {
2468 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2469 p_response->p_intermediates->line, sizeof(char *));
2470 }
2471 at_response_free(p_response);
2472 break;
2473
2474 case RIL_REQUEST_SIM_IO:
2475 requestSIM_IO(data,datalen,t);
2476 break;
2477
2478 case RIL_REQUEST_SEND_USSD:
2479 requestSendUSSD(data, datalen, t);
2480 break;
2481
2482 case RIL_REQUEST_CANCEL_USSD:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002483 if (getSIMStatus() == SIM_ABSENT) {
2484 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2485 return;
2486 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002487 p_response = NULL;
2488 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2489
2490 if (err < 0 || p_response->success == 0) {
2491 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2492 } else {
2493 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2494 p_response->p_intermediates->line, sizeof(char *));
2495 }
2496 at_response_free(p_response);
2497 break;
2498
2499 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002500 if (getSIMStatus() == SIM_ABSENT) {
2501 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2502 } else {
2503 at_send_command("AT+COPS=0", NULL);
2504 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002505 break;
2506
Wink Savillef4c4d362009-04-02 01:37:03 -07002507 case RIL_REQUEST_DATA_CALL_LIST:
2508 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002509 break;
2510
2511 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2512 requestQueryNetworkSelectionMode(data, datalen, t);
2513 break;
2514
2515 case RIL_REQUEST_OEM_HOOK_RAW:
2516 // echo back data
2517 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2518 break;
2519
2520
2521 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2522 int i;
2523 const char ** cur;
2524
Wink Saville4dcab4f2012-11-19 16:05:13 -08002525 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002526
2527
2528 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2529 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002530 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002531 }
2532
2533 // echo back strings
2534 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2535 break;
2536 }
2537
2538 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2539 requestWriteSmsToSim(data, datalen, t);
2540 break;
2541
2542 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2543 char * cmd;
2544 p_response = NULL;
2545 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2546 err = at_send_command(cmd, &p_response);
2547 free(cmd);
2548 if (err < 0 || p_response->success == 0) {
2549 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2550 } else {
2551 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2552 }
2553 at_response_free(p_response);
2554 break;
2555 }
2556
2557 case RIL_REQUEST_ENTER_SIM_PIN:
2558 case RIL_REQUEST_ENTER_SIM_PUK:
2559 case RIL_REQUEST_ENTER_SIM_PIN2:
2560 case RIL_REQUEST_ENTER_SIM_PUK2:
2561 case RIL_REQUEST_CHANGE_SIM_PIN:
2562 case RIL_REQUEST_CHANGE_SIM_PIN2:
2563 requestEnterSimPin(data, datalen, t);
2564 break;
2565
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002566 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2567 int reply[2];
2568 //0==unregistered, 1==registered
2569 reply[0] = s_ims_registered;
2570
2571 //to be used when changed to include service supporated info
2572 //reply[1] = s_ims_services;
2573
2574 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2575 reply[1] = s_ims_format;
2576
2577 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2578 reply[0], reply[1]);
2579 if (reply[1] != -1) {
2580 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2581 } else {
2582 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2583 }
2584 break;
2585 }
2586
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002587 case RIL_REQUEST_VOICE_RADIO_TECH:
2588 {
2589 int tech = techFromModemType(TECH(sMdmInfo));
2590 if (tech < 0 )
2591 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2592 else
2593 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2594 }
2595 break;
2596 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2597 requestSetPreferredNetworkType(request, data, datalen, t);
2598 break;
2599
2600 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2601 requestGetPreferredNetworkType(request, data, datalen, t);
2602 break;
2603
Jun Tian58027012013-07-30 11:07:22 +08002604 case RIL_REQUEST_GET_CELL_INFO_LIST:
2605 requestGetCellInfoList(data, datalen, t);
2606 break;
2607
2608 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2609 requestSetCellInfoListRate(data, datalen, t);
2610 break;
2611
Etan Cohend3652192014-06-20 08:28:44 -07002612 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2613 requestGetHardwareConfig(data, datalen, t);
2614 break;
2615
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002616 case RIL_REQUEST_SHUTDOWN:
2617 requestShutdown(t);
2618 break;
2619
Jim Kayed2d82012017-10-06 14:17:47 -07002620 case RIL_REQUEST_QUERY_TTY_MODE:
2621 requestGetTtyMode(data, datalen, t);
2622 break;
2623
2624 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2625 requestGetRadioCapability(data, datalen, t);
2626 break;
2627
2628 case RIL_REQUEST_GET_MUTE:
2629 requestGetMute(data, datalen, t);
2630 break;
2631
2632 case RIL_REQUEST_SET_INITIAL_ATTACH_APN:
2633 case RIL_REQUEST_ALLOW_DATA:
2634 case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION:
2635 case RIL_REQUEST_SET_CLIR:
2636 case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION:
2637 case RIL_REQUEST_SET_BAND_MODE:
2638 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2639 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2640 case RIL_REQUEST_SET_LOCATION_UPDATES:
2641 case RIL_REQUEST_SET_TTY_MODE:
2642 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2643 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2644 break;
2645
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002646 case RIL_REQUEST_BASEBAND_VERSION:
Jim Kayed2d82012017-10-06 14:17:47 -07002647 requestCdmaBaseBandVersion(request, data, datalen, t);
2648 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002649
2650 case RIL_REQUEST_DEVICE_IDENTITY:
Jim Kayed2d82012017-10-06 14:17:47 -07002651 requestCdmaDeviceIdentity(request, data, datalen, t);
2652 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002653
2654 case RIL_REQUEST_CDMA_SUBSCRIPTION:
Jim Kayed2d82012017-10-06 14:17:47 -07002655 requestCdmaSubscription(request, data, datalen, t);
2656 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002657
Jim Kayed2d82012017-10-06 14:17:47 -07002658 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2659 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2660 break;
2661
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002662 case RIL_REQUEST_START_LCE:
2663 case RIL_REQUEST_STOP_LCE:
2664 case RIL_REQUEST_PULL_LCEDATA:
2665 if (getSIMStatus() == SIM_ABSENT) {
2666 RIL_onRequestComplete(t, RIL_E_SIM_ABSENT, NULL, 0);
2667 } else {
2668 RIL_onRequestComplete(t, RIL_E_LCE_NOT_SUPPORTED, NULL, 0);
2669 }
2670 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002671
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002672 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2673 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2674 requestCdmaGetRoamingPreference(request, data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002675 } else {
2676 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2677 }
2678 break;
2679
2680 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2681 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2682 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2683 } else {
2684 // VTS tests expect us to silently do nothing
2685 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2686 }
2687 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002688
2689 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2690 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2691 requestCdmaSetRoamingPreference(request, data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002692 } else {
2693 // VTS tests expect us to silently do nothing
2694 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2695 }
2696 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002697
2698 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2699 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2700 requestExitEmergencyMode(data, datalen, t);
Jim Kayeb6f3f7e2017-12-07 14:06:22 -08002701 } else {
2702 // VTS tests expect us to silently do nothing
2703 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2704 }
Jim Kayed2d82012017-10-06 14:17:47 -07002705 break;
2706
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002707 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002708 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002709 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2710 break;
2711 }
2712}
2713
2714/**
2715 * Synchronous call from the RIL to us to return current radio state.
2716 * RADIO_STATE_UNAVAILABLE should be the initial state.
2717 */
2718static RIL_RadioState
2719currentState()
2720{
2721 return sState;
2722}
2723/**
2724 * Call from RIL to us to find out whether a specific request code
2725 * is supported by this implementation.
2726 *
2727 * Return 1 for "supported" and 0 for "unsupported"
2728 */
2729
2730static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002731onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002732{
2733 //@@@ todo
2734
2735 return 1;
2736}
2737
Mark Salyzynba58c202014-03-12 15:20:22 -07002738static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002739{
2740 //@@@todo
2741
2742}
2743
2744static const char * getVersion(void)
2745{
2746 return "android reference-ril 1.0";
2747}
2748
2749static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002750setRadioTechnology(ModemInfo *mdm, int newtech)
2751{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002752 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002753
2754 int oldtech = TECH(mdm);
2755
2756 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002757 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002758 TECH(mdm) = newtech;
2759 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2760 int tech = techFromModemType(TECH(sMdmInfo));
2761 if (tech > 0 ) {
2762 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2763 &tech, sizeof(tech));
2764 }
2765 }
2766 }
2767}
2768
2769static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002770setRadioState(RIL_RadioState newState)
2771{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002772 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002773 RIL_RadioState oldState;
2774
2775 pthread_mutex_lock(&s_state_mutex);
2776
2777 oldState = sState;
2778
2779 if (s_closed > 0) {
2780 // If we're closed, the only reasonable state is
2781 // RADIO_STATE_UNAVAILABLE
2782 // This is here because things on the main thread
2783 // may attempt to change the radio state after the closed
2784 // event happened in another thread
2785 newState = RADIO_STATE_UNAVAILABLE;
2786 }
2787
2788 if (sState != newState || s_closed > 0) {
2789 sState = newState;
2790
2791 pthread_cond_broadcast (&s_state_cond);
2792 }
2793
2794 pthread_mutex_unlock(&s_state_mutex);
2795
2796
2797 /* do these outside of the mutex */
2798 if (sState != oldState) {
2799 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2800 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002801 // Sim state can change as result of radio state change
2802 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2803 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002804
2805 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2806 * from the AT reader thread
2807 * Currently, this doesn't happen, but if that changes then these
2808 * will need to be dispatched on the request thread
2809 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002810 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002811 onRadioPowerOn();
2812 }
2813 }
2814}
2815
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002816/** Returns RUIM_NOT_READY on error */
2817static SIM_Status
2818getRUIMStatus()
2819{
2820 ATResponse *p_response = NULL;
2821 int err;
2822 int ret;
2823 char *cpinLine;
2824 char *cpinResult;
2825
2826 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2827 ret = SIM_NOT_READY;
2828 goto done;
2829 }
2830
2831 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2832
2833 if (err != 0) {
2834 ret = SIM_NOT_READY;
2835 goto done;
2836 }
2837
2838 switch (at_get_cme_error(p_response)) {
2839 case CME_SUCCESS:
2840 break;
2841
2842 case CME_SIM_NOT_INSERTED:
2843 ret = SIM_ABSENT;
2844 goto done;
2845
2846 default:
2847 ret = SIM_NOT_READY;
2848 goto done;
2849 }
2850
2851 /* CPIN? has succeeded, now look at the result */
2852
2853 cpinLine = p_response->p_intermediates->line;
2854 err = at_tok_start (&cpinLine);
2855
2856 if (err < 0) {
2857 ret = SIM_NOT_READY;
2858 goto done;
2859 }
2860
2861 err = at_tok_nextstr(&cpinLine, &cpinResult);
2862
2863 if (err < 0) {
2864 ret = SIM_NOT_READY;
2865 goto done;
2866 }
2867
2868 if (0 == strcmp (cpinResult, "SIM PIN")) {
2869 ret = SIM_PIN;
2870 goto done;
2871 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2872 ret = SIM_PUK;
2873 goto done;
2874 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2875 return SIM_NETWORK_PERSONALIZATION;
2876 } else if (0 != strcmp (cpinResult, "READY")) {
2877 /* we're treating unsupported lock types as "sim absent" */
2878 ret = SIM_ABSENT;
2879 goto done;
2880 }
2881
2882 at_response_free(p_response);
2883 p_response = NULL;
2884 cpinResult = NULL;
2885
2886 ret = SIM_READY;
2887
2888done:
2889 at_response_free(p_response);
2890 return ret;
2891}
2892
John Wang309ac292009-07-30 14:53:23 -07002893/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002894static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002895getSIMStatus()
2896{
2897 ATResponse *p_response = NULL;
2898 int err;
2899 int ret;
2900 char *cpinLine;
2901 char *cpinResult;
2902
Wink Saville4dcab4f2012-11-19 16:05:13 -08002903 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002904 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2905
2906 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002907 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002908 goto done;
2909 }
2910
2911 switch (at_get_cme_error(p_response)) {
2912 case CME_SUCCESS:
2913 break;
2914
2915 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002916 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002917 goto done;
2918
2919 default:
John Wang309ac292009-07-30 14:53:23 -07002920 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002921 goto done;
2922 }
2923
2924 /* CPIN? has succeeded, now look at the result */
2925
2926 cpinLine = p_response->p_intermediates->line;
2927 err = at_tok_start (&cpinLine);
2928
2929 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002930 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002931 goto done;
2932 }
2933
2934 err = at_tok_nextstr(&cpinLine, &cpinResult);
2935
2936 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002937 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002938 goto done;
2939 }
2940
2941 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002942 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002943 goto done;
2944 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07002945 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002946 goto done;
2947 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002948 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002949 } else if (0 != strcmp (cpinResult, "READY")) {
2950 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07002951 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002952 goto done;
2953 }
2954
2955 at_response_free(p_response);
2956 p_response = NULL;
2957 cpinResult = NULL;
2958
Jim Kayed2d82012017-10-06 14:17:47 -07002959 ret = (sState == RADIO_STATE_ON) ? SIM_READY : SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002960
2961done:
2962 at_response_free(p_response);
2963 return ret;
2964}
2965
2966
2967/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07002968 * Get the current card status.
2969 *
2970 * This must be freed using freeCardStatus.
2971 * @return: On success returns RIL_E_SUCCESS
2972 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002973static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002974 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07002975 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07002976 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2977 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002978 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07002979 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2980 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002981 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07002982 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2983 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002984 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07002985 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2986 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002987 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07002988 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2989 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002990 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07002991 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002992 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2993 // RUIM_ABSENT = 6
2994 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2995 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2996 // RUIM_NOT_READY = 7
2997 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2998 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2999 // RUIM_READY = 8
3000 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3001 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3002 // RUIM_PIN = 9
3003 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3004 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3005 // RUIM_PUK = 10
3006 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3007 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
3008 // RUIM_NETWORK_PERSONALIZATION = 11
3009 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
bohu076e6872017-07-02 21:33:28 -07003010 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3011 // ISIM_ABSENT = 12
3012 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
3013 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3014 // ISIM_NOT_READY = 13
3015 { RIL_APPTYPE_ISIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
3016 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3017 // ISIM_READY = 14
3018 { RIL_APPTYPE_ISIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
3019 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
3020 // ISIM_PIN = 15
3021 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
3022 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3023 // ISIM_PUK = 16
3024 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
3025 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
3026 // ISIM_NETWORK_PERSONALIZATION = 17
3027 { RIL_APPTYPE_ISIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
3028 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
3029
Wink Savillef6aa7c12009-04-30 14:20:52 -07003030 };
3031 RIL_CardState card_state;
3032 int num_apps;
3033
3034 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07003035 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003036 card_state = RIL_CARDSTATE_ABSENT;
3037 num_apps = 0;
3038 } else {
3039 card_state = RIL_CARDSTATE_PRESENT;
bohu076e6872017-07-02 21:33:28 -07003040 num_apps = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003041 }
3042
3043 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003044 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07003045 p_card_status->card_state = card_state;
3046 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
kun.tang7bb00222017-07-12 11:41:43 +08003047 p_card_status->gsm_umts_subscription_app_index = -1;
3048 p_card_status->cdma_subscription_app_index = -1;
3049 p_card_status->ims_subscription_app_index = -1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003050 p_card_status->num_applications = num_apps;
3051
3052 // Initialize application status
3053 int i;
3054 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07003055 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07003056 }
3057
3058 // Pickup the appropriate application status
3059 // that reflects sim_status for gsm.
3060 if (num_apps != 0) {
bohu076e6872017-07-02 21:33:28 -07003061 p_card_status->num_applications = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003062 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003063 p_card_status->cdma_subscription_app_index = 1;
bohu076e6872017-07-02 21:33:28 -07003064 p_card_status->ims_subscription_app_index = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07003065
3066 // Get the correct app status
3067 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003068 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
bohu076e6872017-07-02 21:33:28 -07003069 p_card_status->applications[2] = app_status_array[sim_status + ISIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07003070 }
3071
3072 *pp_card_status = p_card_status;
3073 return RIL_E_SUCCESS;
3074}
3075
3076/**
3077 * Free the card status returned by getCardStatus
3078 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003079static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07003080 free(p_card_status);
3081}
3082
3083/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003084 * SIM ready means any commands that access the SIM will work, including:
3085 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
3086 * (all SMS-related commands)
3087 */
3088
Mark Salyzynba58c202014-03-12 15:20:22 -07003089static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003090{
3091 ATResponse *p_response;
3092 int ret;
3093
Naveen Kalla2baf7232016-10-11 13:49:20 -07003094 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003095 // no longer valid to poll
3096 return;
3097 }
3098
3099 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07003100 case SIM_ABSENT:
3101 case SIM_PIN:
3102 case SIM_PUK:
3103 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003104 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003105 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003106 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003107 return;
3108
John Wang309ac292009-07-30 14:53:23 -07003109 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003110 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
3111 return;
3112
John Wang309ac292009-07-30 14:53:23 -07003113 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003114 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003115 onSIMReady();
3116 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003117 return;
3118 }
3119}
3120
3121/** returns 1 if on, 0 if off, and -1 on error */
3122static int isRadioOn()
3123{
3124 ATResponse *p_response = NULL;
3125 int err;
3126 char *line;
3127 char ret;
3128
3129 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
3130
3131 if (err < 0 || p_response->success == 0) {
3132 // assume radio is off
3133 goto error;
3134 }
3135
3136 line = p_response->p_intermediates->line;
3137
3138 err = at_tok_start(&line);
3139 if (err < 0) goto error;
3140
3141 err = at_tok_nextbool(&line, &ret);
3142 if (err < 0) goto error;
3143
3144 at_response_free(p_response);
3145
3146 return (int)ret;
3147
3148error:
3149
3150 at_response_free(p_response);
3151 return -1;
3152}
3153
3154/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003155 * Parse the response generated by a +CTEC AT command
3156 * The values read from the response are stored in current and preferred.
3157 * Both current and preferred may be null. The corresponding value is ignored in that case.
3158 *
3159 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
3160 * 1 if the response includes the current technology only
3161 * 0 if the response includes both current technology and preferred mode
3162 */
3163int parse_technology_response( const char *response, int *current, int32_t *preferred )
3164{
3165 int err;
3166 char *line, *p;
3167 int ct;
3168 int32_t pt = 0;
3169 char *str_pt;
3170
3171 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08003172 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003173 err = at_tok_start(&p);
3174 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003175 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003176 free(line);
3177 return -1;
3178 }
3179
3180 err = at_tok_nextint(&p, &ct);
3181 if (err) {
3182 free(line);
3183 return -1;
3184 }
3185 if (current) *current = ct;
3186
Wink Saville4dcab4f2012-11-19 16:05:13 -08003187 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003188
3189 err = at_tok_nexthexint(&p, &pt);
3190 if (err) {
3191 free(line);
3192 return 1;
3193 }
3194 if (preferred) {
3195 *preferred = pt;
3196 }
3197 free(line);
3198
3199 return 0;
3200}
3201
Mark Salyzynba58c202014-03-12 15:20:22 -07003202int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003203{
3204 ATResponse *p_response;
3205 int err, val, techs = 0;
3206 char *tok;
3207 char *line;
3208
Wink Saville4dcab4f2012-11-19 16:05:13 -08003209 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003210 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3211 if (err || !p_response->success)
3212 goto error;
3213 line = p_response->p_intermediates->line;
3214 err = at_tok_start(&line);
3215 if (err || !at_tok_hasmore(&line))
3216 goto error;
3217 while (!at_tok_nextint(&line, &val)) {
3218 techs |= ( 1 << val );
3219 }
3220 if (supported) *supported = techs;
3221 return 0;
3222error:
3223 at_response_free(p_response);
3224 return -1;
3225}
3226
3227/**
3228 * query_ctec. Send the +CTEC AT command to the modem to query the current
3229 * and preferred modes. It leaves values in the addresses pointed to by
3230 * current and preferred. If any of those pointers are NULL, the corresponding value
3231 * is ignored, but the return value will still reflect if retreiving and parsing of the
3232 * values suceeded.
3233 *
3234 * @mdm Currently unused
3235 * @current A pointer to store the current mode returned by the modem. May be null.
3236 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3237 * @return -1 on error (or failure to parse)
3238 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3239 * 0 if both current and preferred were returned correctly
3240 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003241int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003242{
3243 ATResponse *response = NULL;
3244 int err;
3245 int res;
3246
Colin Cross5cba4882014-02-05 18:55:42 -08003247 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003248 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3249 if (!err && response->success) {
3250 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3251 at_response_free(response);
3252 return res;
3253 }
Colin Cross5cba4882014-02-05 18:55:42 -08003254 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003255 at_response_free(response);
3256 return -1;
3257}
3258
3259int is_multimode_modem(ModemInfo *mdm)
3260{
3261 ATResponse *response;
3262 int err;
3263 char *line;
3264 int tech;
3265 int32_t preferred;
3266
3267 if (query_ctec(mdm, &tech, &preferred) == 0) {
3268 mdm->currentTech = tech;
3269 mdm->preferredNetworkMode = preferred;
3270 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3271 return 0;
3272 }
3273 return 1;
3274 }
3275 return 0;
3276}
3277
3278/**
3279 * Find out if our modem is GSM, CDMA or both (Multimode)
3280 */
3281static void probeForModemMode(ModemInfo *info)
3282{
3283 ATResponse *response;
3284 int err;
3285 assert (info);
3286 // Currently, our only known multimode modem is qemu's android modem,
3287 // which implements the AT+CTEC command to query and set mode.
3288 // Try that first
3289
3290 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003291 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003292 info->supportedTechs, info->currentTech);
3293 return;
3294 }
3295
3296 /* Being here means that our modem is not multimode */
3297 info->isMultimode = 0;
3298
3299 /* CDMA Modems implement the AT+WNAM command */
3300 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3301 if (!err && response->success) {
3302 at_response_free(response);
3303 // TODO: find out if we really support EvDo
3304 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3305 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003306 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003307 return;
3308 }
3309 if (!err) at_response_free(response);
3310 // TODO: find out if modem really supports WCDMA/LTE
3311 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3312 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003313 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003314}
3315
3316/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003317 * Initialize everything that can be configured while we're still in
3318 * AT+CFUN=0
3319 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003320static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003321{
3322 ATResponse *p_response = NULL;
3323 int err;
3324
3325 setRadioState (RADIO_STATE_OFF);
3326
3327 at_handshake();
3328
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003329 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003330 /* note: we don't check errors here. Everything important will
3331 be handled in onATTimeout and onATReaderClosed */
3332
3333 /* atchannel is tolerant of echo but it must */
3334 /* have verbose result codes */
3335 at_send_command("ATE0Q0V1", NULL);
3336
3337 /* No auto-answer */
3338 at_send_command("ATS0=0", NULL);
3339
3340 /* Extended errors */
3341 at_send_command("AT+CMEE=1", NULL);
3342
3343 /* Network registration events */
3344 err = at_send_command("AT+CREG=2", &p_response);
3345
3346 /* some handsets -- in tethered mode -- don't support CREG=2 */
3347 if (err < 0 || p_response->success == 0) {
3348 at_send_command("AT+CREG=1", NULL);
3349 }
3350
3351 at_response_free(p_response);
3352
3353 /* GPRS registration events */
3354 at_send_command("AT+CGREG=1", NULL);
3355
3356 /* Call Waiting notifications */
3357 at_send_command("AT+CCWA=1", NULL);
3358
3359 /* Alternating voice/data off */
3360 at_send_command("AT+CMOD=0", NULL);
3361
3362 /* Not muted */
3363 at_send_command("AT+CMUT=0", NULL);
3364
3365 /* +CSSU unsolicited supp service notifications */
3366 at_send_command("AT+CSSN=0,1", NULL);
3367
3368 /* no connected line identification */
3369 at_send_command("AT+COLP=0", NULL);
3370
3371 /* HEX character set */
3372 at_send_command("AT+CSCS=\"HEX\"", NULL);
3373
3374 /* USSD unsolicited */
3375 at_send_command("AT+CUSD=1", NULL);
3376
3377 /* Enable +CGEV GPRS event notifications, but don't buffer */
3378 at_send_command("AT+CGEREP=1,0", NULL);
3379
3380 /* SMS PDU mode */
3381 at_send_command("AT+CMGF=0", NULL);
3382
3383#ifdef USE_TI_COMMANDS
3384
3385 at_send_command("AT%CPI=3", NULL);
3386
3387 /* TI specific -- notifications when SMS is ready (currently ignored) */
3388 at_send_command("AT%CSTAT=1", NULL);
3389
3390#endif /* USE_TI_COMMANDS */
3391
3392
3393 /* assume radio is off on error */
3394 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003395 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003396 }
3397}
3398
3399static void waitForClose()
3400{
3401 pthread_mutex_lock(&s_state_mutex);
3402
3403 while (s_closed == 0) {
3404 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3405 }
3406
3407 pthread_mutex_unlock(&s_state_mutex);
3408}
3409
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003410static void sendUnsolImsNetworkStateChanged()
3411{
3412#if 0 // to be used when unsol is changed to return data.
3413 int reply[2];
3414 reply[0] = s_ims_registered;
3415 reply[1] = s_ims_services;
3416 reply[1] = s_ims_format;
3417#endif
3418 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3419 NULL, 0);
3420}
3421
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003422/**
3423 * Called by atchannel when an unsolicited line appears
3424 * This is called on atchannel's reader thread. AT commands may
3425 * not be issued here
3426 */
3427static void onUnsolicited (const char *s, const char *sms_pdu)
3428{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003429 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003430 int err;
3431
3432 /* Ignore unsolicited responses until we're initialized.
3433 * This is OK because the RIL library will poll for initial state
3434 */
3435 if (sState == RADIO_STATE_UNAVAILABLE) {
3436 return;
3437 }
3438
3439 if (strStartsWith(s, "%CTZV:")) {
3440 /* TI specific -- NITZ time */
3441 char *response;
3442
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003443 line = p = strdup(s);
3444 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003445
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003446 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003447
3448 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003449 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003450 } else {
3451 RIL_onUnsolicitedResponse (
3452 RIL_UNSOL_NITZ_TIME_RECEIVED,
3453 response, strlen(response));
3454 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003455 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003456 } else if (strStartsWith(s,"+CRING:")
3457 || strStartsWith(s,"RING")
3458 || strStartsWith(s,"NO CARRIER")
3459 || strStartsWith(s,"+CCWA")
3460 ) {
3461 RIL_onUnsolicitedResponse (
3462 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3463 NULL, 0);
3464#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003465 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003466#endif /* WORKAROUND_FAKE_CGEV */
3467 } else if (strStartsWith(s,"+CREG:")
3468 || strStartsWith(s,"+CGREG:")
3469 ) {
3470 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003471 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003472 NULL, 0);
3473#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003474 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003475#endif /* WORKAROUND_FAKE_CGEV */
3476 } else if (strStartsWith(s, "+CMT:")) {
3477 RIL_onUnsolicitedResponse (
3478 RIL_UNSOL_RESPONSE_NEW_SMS,
3479 sms_pdu, strlen(sms_pdu));
3480 } else if (strStartsWith(s, "+CDS:")) {
3481 RIL_onUnsolicitedResponse (
3482 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3483 sms_pdu, strlen(sms_pdu));
3484 } else if (strStartsWith(s, "+CGEV:")) {
3485 /* Really, we can ignore NW CLASS and ME CLASS events here,
3486 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003487 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003488 */
3489 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003490 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003491#ifdef WORKAROUND_FAKE_CGEV
3492 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003493 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003494#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003495 } else if (strStartsWith(s, "+CTEC: ")) {
3496 int tech, mask;
3497 switch (parse_technology_response(s, &tech, NULL))
3498 {
3499 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003500 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003501 break;
3502 case 1: // current mode correctly parsed
3503 case 0: // preferred mode correctly parsed
3504 mask = 1 << tech;
3505 if (mask != MDM_GSM && mask != MDM_CDMA &&
3506 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003507 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003508 } else {
3509 setRadioTechnology(sMdmInfo, tech);
3510 }
3511 break;
3512 }
3513 } else if (strStartsWith(s, "+CCSS: ")) {
3514 int source = 0;
3515 line = p = strdup(s);
3516 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003517 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003518 return;
3519 }
3520 if (at_tok_start(&p) < 0) {
3521 free(line);
3522 return;
3523 }
3524 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003525 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003526 free(line);
3527 return;
3528 }
3529 SSOURCE(sMdmInfo) = source;
3530 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3531 &source, sizeof(source));
3532 } else if (strStartsWith(s, "+WSOS: ")) {
3533 char state = 0;
3534 int unsol;
3535 line = p = strdup(s);
3536 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003537 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003538 return;
3539 }
3540 if (at_tok_start(&p) < 0) {
3541 free(line);
3542 return;
3543 }
3544 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003545 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003546 free(line);
3547 return;
3548 }
3549 free(line);
3550
3551 unsol = state ?
3552 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3553
3554 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3555
3556 } else if (strStartsWith(s, "+WPRL: ")) {
3557 int version = -1;
3558 line = p = strdup(s);
3559 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003560 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003561 return;
3562 }
3563 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003564 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003565 free(line);
3566 return;
3567 }
3568 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003569 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003570 free(line);
3571 return;
3572 }
3573 free(line);
3574 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3575 } else if (strStartsWith(s, "+CFUN: 0")) {
3576 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003577 }
3578}
3579
3580/* Called on command or reader thread */
3581static void onATReaderClosed()
3582{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003583 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003584 at_close();
3585 s_closed = 1;
3586
3587 setRadioState (RADIO_STATE_UNAVAILABLE);
3588}
3589
3590/* Called on command thread */
3591static void onATTimeout()
3592{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003593 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003594 at_close();
3595
3596 s_closed = 1;
3597
3598 /* FIXME cause a radio reset here */
3599
3600 setRadioState (RADIO_STATE_UNAVAILABLE);
3601}
3602
Etan Cohend3652192014-06-20 08:28:44 -07003603/* Called to pass hardware configuration information to telephony
3604 * framework.
3605 */
3606static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3607{
3608 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3609}
3610
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003611static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003612{
3613#ifdef RIL_SHLIB
3614 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3615#else
3616 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3617 exit(-1);
3618#endif
3619}
3620
3621static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003622mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003623{
3624 int fd;
3625 int ret;
3626
3627 AT_DUMP("== ", "entering mainLoop()", -1 );
3628 at_set_on_reader_closed(onATReaderClosed);
3629 at_set_on_timeout(onATTimeout);
3630
3631 for (;;) {
3632 fd = -1;
3633 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003634 if (isInEmulator()) {
3635 fd = qemu_pipe_open("pipe:qemud:gsm");
3636 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003637 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003638 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003639 fd = socket_local_client(s_device_path,
3640 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3641 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003642 } else if (s_device_path != NULL) {
3643 fd = open (s_device_path, O_RDWR);
3644 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3645 /* disable echo on serial ports */
3646 struct termios ios;
3647 tcgetattr( fd, &ios );
3648 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3649 tcsetattr( fd, TCSANOW, &ios );
3650 }
3651 }
3652
3653 if (fd < 0) {
3654 perror ("opening AT interface. retrying...");
3655 sleep(10);
3656 /* never returns */
3657 }
3658 }
3659
3660 s_closed = 0;
3661 ret = at_open(fd, onUnsolicited);
3662
3663 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003664 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003665 return 0;
3666 }
3667
3668 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3669
3670 // Give initializeCallback a chance to dispatched, since
3671 // we don't presently have a cancellation mechanism
3672 sleep(1);
3673
3674 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003675 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003676 }
3677}
3678
3679#ifdef RIL_SHLIB
3680
3681pthread_t s_tid_mainloop;
3682
3683const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3684{
3685 int ret;
3686 int fd = -1;
3687 int opt;
3688 pthread_attr_t attr;
3689
3690 s_rilenv = env;
3691
Sandeep Gutta11f27942014-07-10 05:00:25 +05303692 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003693 switch (opt) {
3694 case 'p':
3695 s_port = atoi(optarg);
3696 if (s_port == 0) {
3697 usage(argv[0]);
3698 return NULL;
3699 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003700 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003701 break;
3702
3703 case 'd':
3704 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003705 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003706 break;
3707
3708 case 's':
3709 s_device_path = optarg;
3710 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003711 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003712 break;
3713
Sandeep Gutta11f27942014-07-10 05:00:25 +05303714 case 'c':
3715 RLOGI("Client id received %s\n", optarg);
3716 break;
3717
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003718 default:
3719 usage(argv[0]);
3720 return NULL;
3721 }
3722 }
3723
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003724 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003725 usage(argv[0]);
3726 return NULL;
3727 }
3728
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003729 sMdmInfo = calloc(1, sizeof(ModemInfo));
3730 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003731 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003732 return NULL;
3733 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003734 pthread_attr_init (&attr);
3735 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3736 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3737
3738 return &s_callbacks;
3739}
3740#else /* RIL_SHLIB */
3741int main (int argc, char **argv)
3742{
3743 int ret;
3744 int fd = -1;
3745 int opt;
3746
3747 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3748 switch (opt) {
3749 case 'p':
3750 s_port = atoi(optarg);
3751 if (s_port == 0) {
3752 usage(argv[0]);
3753 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003754 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003755 break;
3756
3757 case 'd':
3758 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003759 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003760 break;
3761
3762 case 's':
3763 s_device_path = optarg;
3764 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003765 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003766 break;
3767
3768 default:
3769 usage(argv[0]);
3770 }
3771 }
3772
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003773 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003774 usage(argv[0]);
3775 }
3776
3777 RIL_register(&s_callbacks);
3778
3779 mainLoop(NULL);
3780
3781 return 0;
3782}
3783
3784#endif /* RIL_SHLIB */