blob: 87b7bcb5c9a0d30c794a5ec781c144d9886fdd27 [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 */
Robert Greenwaltf838ede2010-07-15 18:54:53 -070053#define PPP_TTY_PATH "eth0"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080054
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -070055// Default MTU value
56#define DEFAULT_MTU 1500
57
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080058#ifdef USE_TI_COMMANDS
59
60// Enable a workaround
61// 1) Make incoming call, do not answer
62// 2) Hangup remote end
63// Expected: call should disappear from CLCC line
64// Actual: Call shows as "ACTIVE" before disappearing
65#define WORKAROUND_ERRONEOUS_ANSWER 1
66
67// Some varients of the TI stack do not support the +CGEV unsolicited
68// response. However, they seem to send an unsolicited +CME ERROR: 150
69#define WORKAROUND_FAKE_CGEV 1
70#endif
71
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070072/* Modem Technology bits */
73#define MDM_GSM 0x01
74#define MDM_WCDMA 0x02
75#define MDM_CDMA 0x04
76#define MDM_EVDO 0x08
77#define MDM_LTE 0x10
78
79typedef struct {
80 int supportedTechs; // Bitmask of supported Modem Technology bits
81 int currentTech; // Technology the modem is currently using (in the format used by modem)
82 int isMultimode;
83
84 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
85 // in which the byte number from LSB to MSB give the priority.
86 //
87 // |MSB| | |LSB
88 // value: |00 |00 |00 |00
89 // byte #: |3 |2 |1 |0
90 //
91 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
92 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
93 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
94 int32_t preferredNetworkMode;
95 int subscription_source;
96
97} ModemInfo;
98
99static ModemInfo *sMdmInfo;
100// TECH returns the current technology in the format used by the modem.
101// It can be used as an l-value
102#define TECH(mdminfo) ((mdminfo)->currentTech)
103// TECH_BIT returns the bitmask equivalent of the current tech
104#define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
105#define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
106#define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
107#define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
108// CDMA Subscription Source
109#define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
110
111static int net2modem[] = {
112 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
113 MDM_GSM, // 1 - GSM only
114 MDM_WCDMA, // 2 - WCDMA only
115 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
116 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
117 MDM_CDMA, // 5 - CDMA only
118 MDM_EVDO, // 6 - EvDo only
119 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
120 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
121 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
122 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
123 MDM_LTE, // 11 - LTE only
124};
125
126static int32_t net2pmask[] = {
127 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
128 MDM_GSM, // 1 - GSM only
129 MDM_WCDMA, // 2 - WCDMA only
130 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
131 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
132 MDM_CDMA, // 5 - CDMA only
133 MDM_EVDO, // 6 - EvDo only
134 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
135 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
136 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
137 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
138 MDM_LTE, // 11 - LTE only
139};
140
141static int is3gpp2(int radioTech) {
142 switch (radioTech) {
143 case RADIO_TECH_IS95A:
144 case RADIO_TECH_IS95B:
145 case RADIO_TECH_1xRTT:
146 case RADIO_TECH_EVDO_0:
147 case RADIO_TECH_EVDO_A:
148 case RADIO_TECH_EVDO_B:
149 case RADIO_TECH_EHRPD:
150 return 1;
151 default:
152 return 0;
153 }
154}
155
John Wang309ac292009-07-30 14:53:23 -0700156typedef enum {
157 SIM_ABSENT = 0,
158 SIM_NOT_READY = 1,
Naveen Kalla2baf7232016-10-11 13:49:20 -0700159 SIM_READY = 2,
John Wang309ac292009-07-30 14:53:23 -0700160 SIM_PIN = 3,
161 SIM_PUK = 4,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700162 SIM_NETWORK_PERSONALIZATION = 5,
163 RUIM_ABSENT = 6,
164 RUIM_NOT_READY = 7,
165 RUIM_READY = 8,
166 RUIM_PIN = 9,
167 RUIM_PUK = 10,
bohu076e6872017-07-02 21:33:28 -0700168 RUIM_NETWORK_PERSONALIZATION = 11,
169 ISIM_ABSENT = 12,
170 ISIM_NOT_READY = 13,
171 ISIM_READY = 14,
172 ISIM_PIN = 15,
173 ISIM_PUK = 16,
174 ISIM_NETWORK_PERSONALIZATION = 17,
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100175} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -0700176
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800177static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
178static RIL_RadioState currentState();
179static int onSupports (int requestCode);
180static void onCancel (RIL_Token t);
181static const char *getVersion();
182static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -0700183static SIM_Status getSIMStatus();
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700184static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
185static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -0700186static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800187
188extern const char * requestToString(int request);
189
190/*** Static Variables ***/
191static const RIL_RadioFunctions s_callbacks = {
192 RIL_VERSION,
193 onRequest,
194 currentState,
195 onSupports,
196 onCancel,
197 getVersion
198};
199
200#ifdef RIL_SHLIB
201static const struct RIL_Env *s_rilenv;
202
203#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
204#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
205#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
206#endif
207
208static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
209
210static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
211static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
212
213static int s_port = -1;
214static const char * s_device_path = NULL;
215static int s_device_socket = 0;
216
217/* trigger change to this with s_state_cond */
218static int s_closed = 0;
219
220static int sFD; /* file desc of AT channel */
221static char sATBuffer[MAX_AT_RESPONSE+1];
222static char *sATBufferCur = NULL;
223
224static const struct timeval TIMEVAL_SIMPOLL = {1,0};
225static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
226static const struct timeval TIMEVAL_0 = {0,0};
227
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -0700228static int s_ims_registered = 0; // 0==unregistered
229static int s_ims_services = 1; // & 0x1 == sms over ims supported
230static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
231static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
232static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
233static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
234static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
235
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800236#ifdef WORKAROUND_ERRONEOUS_ANSWER
237// Max number of times we'll try to repoll when we think
238// we have a AT+CLCC race condition
239#define REPOLL_CALLS_COUNT_MAX 4
240
241// Line index that was incoming or waiting at last poll, or -1 for none
242static int s_incomingOrWaitingLine = -1;
243// Number of times we've asked for a repoll of AT+CLCC
244static int s_repollCallsCount = 0;
245// Should we expect a call to be answered in the next CLCC?
246static int s_expectAnswer = 0;
247#endif /* WORKAROUND_ERRONEOUS_ANSWER */
248
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200249
Wink Saville8a9e0212013-04-09 12:11:38 -0700250static int s_cell_info_rate_ms = INT_MAX;
251static int s_mcc = 0;
252static int s_mnc = 0;
253static int s_lac = 0;
254static int s_cid = 0;
255
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800256static void pollSIMState (void *param);
257static void setRadioState(RIL_RadioState newState);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700258static void setRadioTechnology(ModemInfo *mdm, int newtech);
259static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
260static int parse_technology_response(const char *response, int *current, int32_t *preferred);
261static int techFromModemType(int mdmtype);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800262
263static int clccStateToRILState(int state, RIL_CallState *p_state)
264
265{
266 switch(state) {
267 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
268 case 1: *p_state = RIL_CALL_HOLDING; return 0;
269 case 2: *p_state = RIL_CALL_DIALING; return 0;
270 case 3: *p_state = RIL_CALL_ALERTING; return 0;
271 case 4: *p_state = RIL_CALL_INCOMING; return 0;
272 case 5: *p_state = RIL_CALL_WAITING; return 0;
273 default: return -1;
274 }
275}
276
277/**
278 * Note: directly modified line and has *p_call point directly into
279 * modified line
280 */
Wink Saville3d54e742009-05-18 18:00:44 -0700281static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800282{
283 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
284 // index,isMT,state,mode,isMpty(,number,TOA)?
285
286 int err;
287 int state;
288 int mode;
289
290 err = at_tok_start(&line);
291 if (err < 0) goto error;
292
293 err = at_tok_nextint(&line, &(p_call->index));
294 if (err < 0) goto error;
295
296 err = at_tok_nextbool(&line, &(p_call->isMT));
297 if (err < 0) goto error;
298
299 err = at_tok_nextint(&line, &state);
300 if (err < 0) goto error;
301
302 err = clccStateToRILState(state, &(p_call->state));
303 if (err < 0) goto error;
304
305 err = at_tok_nextint(&line, &mode);
306 if (err < 0) goto error;
307
308 p_call->isVoice = (mode == 0);
309
310 err = at_tok_nextbool(&line, &(p_call->isMpty));
311 if (err < 0) goto error;
312
313 if (at_tok_hasmore(&line)) {
314 err = at_tok_nextstr(&line, &(p_call->number));
315
316 /* tolerate null here */
317 if (err < 0) return 0;
318
319 // Some lame implementations return strings
320 // like "NOT AVAILABLE" in the CLCC line
321 if (p_call->number != NULL
322 && 0 == strspn(p_call->number, "+0123456789")
323 ) {
324 p_call->number = NULL;
325 }
326
327 err = at_tok_nextint(&line, &p_call->toa);
328 if (err < 0) goto error;
329 }
330
Wink Saville74fa3882009-12-22 15:35:41 -0800331 p_call->uusInfo = NULL;
332
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800333 return 0;
334
335error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800336 RLOGE("invalid CLCC line\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800337 return -1;
338}
339
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -0700340static int parseSimResponseLine(char* line, RIL_SIM_IO_Response* response) {
341 int err;
342
343 err = at_tok_start(&line);
344 if (err < 0) return err;
345 err = at_tok_nextint(&line, &response->sw1);
346 if (err < 0) return err;
347 err = at_tok_nextint(&line, &response->sw2);
348 if (err < 0) return err;
349
350 if (at_tok_hasmore(&line)) {
351 err = at_tok_nextstr(&line, &response->simResponse);
352 if (err < 0) return err;
353 }
354 return 0;
355}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800356
357/** do post-AT+CFUN=1 initialization */
358static void onRadioPowerOn()
359{
360#ifdef USE_TI_COMMANDS
361 /* Must be after CFUN=1 */
362 /* TI specific -- notifications for CPHS things such */
363 /* as CPHS message waiting indicator */
364
365 at_send_command("AT%CPHS=1", NULL);
366
367 /* TI specific -- enable NITZ unsol notifs */
368 at_send_command("AT%CTZV=1", NULL);
369#endif
370
371 pollSIMState(NULL);
372}
373
374/** do post- SIM ready initialization */
375static void onSIMReady()
376{
377 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
378 /*
379 * Always send SMS messages directly to the TE
380 *
381 * mode = 1 // discard when link is reserved (link should never be
382 * reserved)
383 * mt = 2 // most messages routed to TE
384 * bm = 2 // new cell BM's routed to TE
385 * ds = 1 // Status reports routed to TE
386 * bfr = 1 // flush buffer
387 */
388 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
389}
390
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700391static void requestRadioPower(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800392{
393 int onOff;
394
395 int err;
396 ATResponse *p_response = NULL;
397
398 assert (datalen >= sizeof(int *));
399 onOff = ((int *)data)[0];
400
401 if (onOff == 0 && sState != RADIO_STATE_OFF) {
402 err = at_send_command("AT+CFUN=0", &p_response);
403 if (err < 0 || p_response->success == 0) goto error;
404 setRadioState(RADIO_STATE_OFF);
405 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
406 err = at_send_command("AT+CFUN=1", &p_response);
407 if (err < 0|| p_response->success == 0) {
408 // Some stacks return an error when there is no SIM,
409 // but they really turn the RF portion on
410 // So, if we get an error, let's check to see if it
411 // turned on anyway
412
413 if (isRadioOn() != 1) {
414 goto error;
415 }
416 }
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700417 setRadioState(RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800418 }
419
420 at_response_free(p_response);
421 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
422 return;
423error:
424 at_response_free(p_response);
425 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
426}
427
Naveen Kallaa65a16a2014-07-31 16:48:31 -0700428static void requestShutdown(RIL_Token t)
429{
430 int onOff;
431
432 int err;
433 ATResponse *p_response = NULL;
434
435 if (sState != RADIO_STATE_OFF) {
436 err = at_send_command("AT+CFUN=0", &p_response);
437 setRadioState(RADIO_STATE_UNAVAILABLE);
438 }
439
440 at_response_free(p_response);
441 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
442 return;
443}
444
Wink Savillef4c4d362009-04-02 01:37:03 -0700445static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800446
Mark Salyzynba58c202014-03-12 15:20:22 -0700447static void onDataCallListChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800448{
Wink Savillef4c4d362009-04-02 01:37:03 -0700449 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800450}
451
Mark Salyzynba58c202014-03-12 15:20:22 -0700452static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800453{
Wink Savillef4c4d362009-04-02 01:37:03 -0700454 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800455}
456
Wink Savillef4c4d362009-04-02 01:37:03 -0700457static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800458{
459 ATResponse *p_response;
460 ATLine *p_cur;
461 int err;
462 int n = 0;
463 char *out;
464
465 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
466 if (err != 0 || p_response->success == 0) {
467 if (t != NULL)
468 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
469 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700470 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800471 NULL, 0);
472 return;
473 }
474
475 for (p_cur = p_response->p_intermediates; p_cur != NULL;
476 p_cur = p_cur->p_next)
477 n++;
478
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700479 RIL_Data_Call_Response_v11 *responses =
480 alloca(n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800481
482 int i;
483 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800484 responses[i].status = -1;
Wink Saville250eb3c2011-06-22 09:11:34 -0700485 responses[i].suggestedRetryTime = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800486 responses[i].cid = -1;
487 responses[i].active = -1;
488 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800489 responses[i].ifname = "";
490 responses[i].addresses = "";
491 responses[i].dnses = "";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700492 responses[i].gateways = "";
Etan Cohend3652192014-06-20 08:28:44 -0700493 responses[i].pcscf = "";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700494 responses[i].mtu = 0;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800495 }
496
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700497 RIL_Data_Call_Response_v11 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800498 for (p_cur = p_response->p_intermediates; p_cur != NULL;
499 p_cur = p_cur->p_next) {
500 char *line = p_cur->line;
501
502 err = at_tok_start(&line);
503 if (err < 0)
504 goto error;
505
506 err = at_tok_nextint(&line, &response->cid);
507 if (err < 0)
508 goto error;
509
510 err = at_tok_nextint(&line, &response->active);
511 if (err < 0)
512 goto error;
513
514 response++;
515 }
516
517 at_response_free(p_response);
518
519 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
520 if (err != 0 || p_response->success == 0) {
521 if (t != NULL)
522 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
523 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700524 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800525 NULL, 0);
526 return;
527 }
528
529 for (p_cur = p_response->p_intermediates; p_cur != NULL;
530 p_cur = p_cur->p_next) {
531 char *line = p_cur->line;
532 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800533
534 err = at_tok_start(&line);
535 if (err < 0)
536 goto error;
537
538 err = at_tok_nextint(&line, &cid);
539 if (err < 0)
540 goto error;
541
542 for (i = 0; i < n; i++) {
543 if (responses[i].cid == cid)
544 break;
545 }
546
547 if (i >= n) {
548 /* details for a context we didn't hear about in the last request */
549 continue;
550 }
551
Wink Saville43808972011-01-13 17:39:51 -0800552 // Assume no error
553 responses[i].status = 0;
554
555 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800556 err = at_tok_nextstr(&line, &out);
557 if (err < 0)
558 goto error;
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700559
560 int type_size = strlen(out) + 1;
561 responses[i].type = alloca(type_size);
562 strlcpy(responses[i].type, out, type_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800563
Wink Saville43808972011-01-13 17:39:51 -0800564 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800565 err = at_tok_nextstr(&line, &out);
566 if (err < 0)
567 goto error;
568
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700569 int ifname_size = strlen(PPP_TTY_PATH) + 1;
570 responses[i].ifname = alloca(ifname_size);
571 strlcpy(responses[i].ifname, PPP_TTY_PATH, ifname_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800572
573 err = at_tok_nextstr(&line, &out);
574 if (err < 0)
575 goto error;
576
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700577 int addresses_size = strlen(out) + 1;
578 responses[i].addresses = alloca(addresses_size);
579 strlcpy(responses[i].addresses, out, addresses_size);
Wink Saville43808972011-01-13 17:39:51 -0800580
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200581 if (isInEmulator()) {
582 /* We are in the emulator - the dns servers are listed
583 * by the following system properties, setup in
584 * /system/etc/init.goldfish.sh:
585 * - net.eth0.dns1
586 * - net.eth0.dns2
587 * - net.eth0.dns3
588 * - net.eth0.dns4
589 */
590 const int dnslist_sz = 128;
591 char* dnslist = alloca(dnslist_sz);
592 const char* separator = "";
593 int nn;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100594
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200595 dnslist[0] = 0;
596 for (nn = 1; nn <= 4; nn++) {
597 /* Probe net.eth0.dns<n> */
598 char propName[PROP_NAME_MAX];
599 char propValue[PROP_VALUE_MAX];
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100600
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200601 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100602
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200603 /* Ignore if undefined */
604 if (__system_property_get(propName, propValue) == 0) {
605 continue;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100606 }
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700607
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200608 /* Append the DNS IP address */
609 strlcat(dnslist, separator, dnslist_sz);
610 strlcat(dnslist, propValue, dnslist_sz);
611 separator = " ";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100612 }
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200613 responses[i].dnses = dnslist;
614
bohu0c389bf2017-10-04 07:16:19 -0700615 responses[i].gateways = "10.0.2.15 fe80::2";
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200616 responses[i].mtu = DEFAULT_MTU;
617 }
618 else {
619 /* I don't know where we are, so use the public Google DNS
620 * servers by default and no gateway.
621 */
622 responses[i].dnses = "8.8.8.8 8.8.4.4";
623 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100624 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800625 }
626
627 at_response_free(p_response);
628
629 if (t != NULL)
630 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700631 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800632 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700633 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800634 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700635 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800636
637 return;
638
639error:
640 if (t != NULL)
641 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
642 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700643 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800644 NULL, 0);
645
646 at_response_free(p_response);
647}
648
649static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700650 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800651{
652 int err;
653 ATResponse *p_response = NULL;
654 int response = 0;
655 char *line;
656
657 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
658
659 if (err < 0 || p_response->success == 0) {
660 goto error;
661 }
662
663 line = p_response->p_intermediates->line;
664
665 err = at_tok_start(&line);
666
667 if (err < 0) {
668 goto error;
669 }
670
671 err = at_tok_nextint(&line, &response);
672
673 if (err < 0) {
674 goto error;
675 }
676
677 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
678 at_response_free(p_response);
679 return;
680error:
681 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800682 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800683 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
684}
685
Mark Salyzynba58c202014-03-12 15:20:22 -0700686static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800687{
688 RIL_onUnsolicitedResponse (
689 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
690 NULL, 0);
691}
692
Mark Salyzynba58c202014-03-12 15:20:22 -0700693static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800694{
695 int err;
696 ATResponse *p_response;
697 ATLine *p_cur;
698 int countCalls;
699 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700700 RIL_Call *p_calls;
701 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800702 int i;
703 int needRepoll = 0;
704
705#ifdef WORKAROUND_ERRONEOUS_ANSWER
706 int prevIncomingOrWaitingLine;
707
708 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
709 s_incomingOrWaitingLine = -1;
710#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
711
712 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
713
714 if (err != 0 || p_response->success == 0) {
715 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
716 return;
717 }
718
719 /* count the calls */
720 for (countCalls = 0, p_cur = p_response->p_intermediates
721 ; p_cur != NULL
722 ; p_cur = p_cur->p_next
723 ) {
724 countCalls++;
725 }
726
727 /* yes, there's an array of pointers and then an array of structures */
728
Wink Saville3d54e742009-05-18 18:00:44 -0700729 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
730 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
731 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800732
733 /* init the pointer array */
734 for(i = 0; i < countCalls ; i++) {
735 pp_calls[i] = &(p_calls[i]);
736 }
737
738 for (countValidCalls = 0, p_cur = p_response->p_intermediates
739 ; p_cur != NULL
740 ; p_cur = p_cur->p_next
741 ) {
742 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
743
744 if (err != 0) {
745 continue;
746 }
747
748#ifdef WORKAROUND_ERRONEOUS_ANSWER
749 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
750 || p_calls[countValidCalls].state == RIL_CALL_WAITING
751 ) {
752 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
753 }
754#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
755
756 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
757 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
758 ) {
759 needRepoll = 1;
760 }
761
762 countValidCalls++;
763 }
764
765#ifdef WORKAROUND_ERRONEOUS_ANSWER
766 // Basically:
767 // A call was incoming or waiting
768 // Now it's marked as active
769 // But we never answered it
770 //
771 // This is probably a bug, and the call will probably
772 // disappear from the call list in the next poll
773 if (prevIncomingOrWaitingLine >= 0
774 && s_incomingOrWaitingLine < 0
775 && s_expectAnswer == 0
776 ) {
777 for (i = 0; i < countValidCalls ; i++) {
778
779 if (p_calls[i].index == prevIncomingOrWaitingLine
780 && p_calls[i].state == RIL_CALL_ACTIVE
781 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
782 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800783 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800784 "Hit WORKAROUND_ERRONOUS_ANSWER case."
785 " Repoll count: %d\n", s_repollCallsCount);
786 s_repollCallsCount++;
787 goto error;
788 }
789 }
790 }
791
792 s_expectAnswer = 0;
793 s_repollCallsCount = 0;
794#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
795
Wink Saville3d54e742009-05-18 18:00:44 -0700796 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
797 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800798
799 at_response_free(p_response);
800
801#ifdef POLL_CALL_STATE
802 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
803 // smd, so we're forced to poll until the call ends.
804#else
805 if (needRepoll) {
806#endif
807 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
808 }
809
810 return;
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700811#ifdef WORKAROUND_ERRONEOUS_ANSWER
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800812error:
813 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
814 at_response_free(p_response);
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700815#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800816}
817
Mark Salyzynba58c202014-03-12 15:20:22 -0700818static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800819{
820 RIL_Dial *p_dial;
821 char *cmd;
822 const char *clir;
823 int ret;
824
825 p_dial = (RIL_Dial *)data;
826
827 switch (p_dial->clir) {
828 case 1: clir = "I"; break; /*invocation*/
829 case 2: clir = "i"; break; /*suppression*/
830 default:
831 case 0: clir = ""; break; /*subscription default*/
832 }
833
834 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
835
836 ret = at_send_command(cmd, NULL);
837
838 free(cmd);
839
840 /* success or failure is ignored by the upper layer here.
841 it will call GET_CURRENT_CALLS and determine success that way */
842 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
843}
844
Mark Salyzynba58c202014-03-12 15:20:22 -0700845static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800846{
847 RIL_SMS_WriteArgs *p_args;
848 char *cmd;
849 int length;
850 int err;
851 ATResponse *p_response = NULL;
852
853 p_args = (RIL_SMS_WriteArgs *)data;
854
855 length = strlen(p_args->pdu)/2;
856 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
857
858 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
859
860 if (err != 0 || p_response->success == 0) goto error;
861
862 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
863 at_response_free(p_response);
864
865 return;
866error:
867 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
868 at_response_free(p_response);
869}
870
Mark Salyzynba58c202014-03-12 15:20:22 -0700871static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800872{
873 int *p_line;
874
875 int ret;
876 char *cmd;
877
878 p_line = (int *)data;
879
880 // 3GPP 22.030 6.5.5
881 // "Releases a specific active call X"
882 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
883
884 ret = at_send_command(cmd, NULL);
885
886 free(cmd);
887
888 /* success or failure is ignored by the upper layer here.
889 it will call GET_CURRENT_CALLS and determine success that way */
890 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
891}
892
Mark Salyzynba58c202014-03-12 15:20:22 -0700893static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800894{
895 ATResponse *p_response = NULL;
896 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800897 char *line;
Jim Kayedc9f31b2017-04-03 13:43:31 -0700898 int count = 0;
899 // Accept a response that is at least v6, and up to v10
900 int minNumOfElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
901 int maxNumOfElements=sizeof(RIL_SignalStrength_v10)/sizeof(int);
902 int response[maxNumOfElements];
903
904 memset(response, 0, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800905
906 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
907
908 if (err < 0 || p_response->success == 0) {
909 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
910 goto error;
911 }
912
913 line = p_response->p_intermediates->line;
914
915 err = at_tok_start(&line);
916 if (err < 0) goto error;
917
Jim Kayedc9f31b2017-04-03 13:43:31 -0700918 for (count = 0; count < maxNumOfElements; count++) {
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700919 err = at_tok_nextint(&line, &(response[count]));
Jim Kayedc9f31b2017-04-03 13:43:31 -0700920 if (err < 0 && count < minNumOfElements) goto error;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700921 }
Chih-Wei Huang28059052012-04-30 01:13:27 +0800922
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700923 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800924
925 at_response_free(p_response);
926 return;
927
928error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800929 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800930 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
931 at_response_free(p_response);
932}
933
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700934/**
935 * networkModePossible. Decides whether the network mode is appropriate for the
936 * specified modem
937 */
938static int networkModePossible(ModemInfo *mdm, int nm)
939{
940 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
941 return 1;
942 }
943 return 0;
944}
Mark Salyzynba58c202014-03-12 15:20:22 -0700945static void requestSetPreferredNetworkType( int request __unused, void *data,
946 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700947{
948 ATResponse *p_response = NULL;
949 char *cmd = NULL;
950 int value = *(int *)data;
951 int current, old;
952 int err;
953 int32_t preferred = net2pmask[value];
954
Wink Saville4dcab4f2012-11-19 16:05:13 -0800955 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700956 if (!networkModePossible(sMdmInfo, value)) {
957 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
958 return;
959 }
960 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
961 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
962 return;
963 }
964 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800965 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700966 if (old != preferred) {
967 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800968 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700969 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
970 free(cmd);
971 if (err || !p_response->success) {
972 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
973 return;
974 }
975 PREFERRED_NETWORK(sMdmInfo) = value;
976 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
977 int current;
978 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
979 switch (res) {
980 case -1: // Error or unable to parse
981 break;
982 case 1: // Only able to parse current
983 case 0: // Both current and preferred were parsed
984 setRadioTechnology(sMdmInfo, current);
985 break;
986 }
987 }
988 }
989 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
990}
991
Mark Salyzynba58c202014-03-12 15:20:22 -0700992static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
993 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700994{
995 int preferred;
996 unsigned i;
997
998 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
999 case -1: // Error or unable to parse
1000 case 1: // Only able to parse current
1001 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1002 break;
1003 case 0: // Both current and preferred were parsed
1004 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
1005 if (preferred == net2pmask[i]) {
1006 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1007 return;
1008 }
1009 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001010 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001011 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1012 break;
1013 }
1014
1015}
1016
Mark Salyzynba58c202014-03-12 15:20:22 -07001017static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1018 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001019{
1020 int err;
1021 char * responseStr;
1022 ATResponse *p_response = NULL;
1023 const char *cmd;
1024 char *line;
1025
1026 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1027 if (err < 0 || !p_response->success) goto error;
1028 line = p_response->p_intermediates->line;
1029 err = at_tok_start(&line);
1030 if (err < 0) goto error;
1031 err = at_tok_nextstr(&line, &responseStr);
1032 if (err < 0 || !responseStr) goto error;
1033 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1034 at_response_free(p_response);
1035 return;
1036error:
1037 at_response_free(p_response);
1038 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1039}
1040
Mark Salyzynba58c202014-03-12 15:20:22 -07001041static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1042 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001043{
1044 int err;
1045 char * responseStr;
1046 ATResponse *p_response = NULL;
1047 const char *cmd;
1048 const char *prefix;
1049 char *line, *p;
1050 int commas;
1051 int skip;
1052 int count = 4;
1053
1054 // Fixed values. TODO: query modem
1055 responseStr = strdup("1.0.0.0");
1056 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1057 free(responseStr);
1058}
1059
Mark Salyzynba58c202014-03-12 15:20:22 -07001060static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1061 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001062{
1063 int err;
1064 int response[4];
1065 char * responseStr[4];
1066 ATResponse *p_response = NULL;
1067 const char *cmd;
1068 const char *prefix;
1069 char *line, *p;
1070 int commas;
1071 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001072 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001073
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001074 // Fixed values. TODO: Query modem
1075 responseStr[0] = "----";
1076 responseStr[1] = "----";
1077 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001078
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001079 err = at_send_command_numeric("AT+CGSN", &p_response);
1080 if (err < 0 || p_response->success == 0) {
1081 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1082 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001083 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001084 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001085 }
1086
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001087 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1088 at_response_free(p_response);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001089}
1090
Mark Salyzynba58c202014-03-12 15:20:22 -07001091static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1092 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001093{
1094 int err;
1095 int *ss = (int *)data;
1096 ATResponse *p_response = NULL;
1097 char *cmd = NULL;
1098 char *line = NULL;
1099 int response;
1100
1101 asprintf(&cmd, "AT+CCSS?");
1102 if (!cmd) goto error;
1103
1104 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1105 if (err < 0 || !p_response->success)
1106 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001107
1108 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001109 err = at_tok_start(&line);
1110 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001111
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001112 err = at_tok_nextint(&line, &response);
1113 free(cmd);
1114 cmd = NULL;
1115
1116 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1117
1118 return;
1119error:
1120 free(cmd);
1121 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1122}
1123
Mark Salyzynba58c202014-03-12 15:20:22 -07001124static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001125 size_t datalen, RIL_Token t)
1126{
1127 int err;
1128 int *ss = (int *)data;
1129 ATResponse *p_response = NULL;
1130 char *cmd = NULL;
1131
1132 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001133 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001134 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1135 return;
1136 }
1137 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1138 if (!cmd) goto error;
1139
1140 err = at_send_command(cmd, &p_response);
1141 if (err < 0 || !p_response->success)
1142 goto error;
1143 free(cmd);
1144 cmd = NULL;
1145
1146 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1147
1148 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1149
1150 return;
1151error:
1152 free(cmd);
1153 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1154}
1155
Mark Salyzynba58c202014-03-12 15:20:22 -07001156static void requestCdmaSubscription(int request __unused, void *data __unused,
1157 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001158{
1159 int err;
1160 int response[5];
1161 char * responseStr[5];
1162 ATResponse *p_response = NULL;
1163 const char *cmd;
1164 const char *prefix;
1165 char *line, *p;
1166 int commas;
1167 int skip;
1168 int count = 5;
1169
1170 // Fixed values. TODO: Query modem
1171 responseStr[0] = "8587777777"; // MDN
1172 responseStr[1] = "1"; // SID
1173 responseStr[2] = "1"; // NID
1174 responseStr[3] = "8587777777"; // MIN
1175 responseStr[4] = "1"; // PRL Version
1176 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001177}
1178
Mark Salyzynba58c202014-03-12 15:20:22 -07001179static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1180 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001181{
1182 int roaming_pref = -1;
1183 ATResponse *p_response = NULL;
1184 char *line;
1185 int res;
1186
1187 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1188 if (res < 0 || !p_response->success) {
1189 goto error;
1190 }
1191 line = p_response->p_intermediates->line;
1192
1193 res = at_tok_start(&line);
1194 if (res < 0) goto error;
1195
1196 res = at_tok_nextint(&line, &roaming_pref);
1197 if (res < 0) goto error;
1198
1199 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1200 return;
1201error:
1202 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1203}
1204
Mark Salyzynba58c202014-03-12 15:20:22 -07001205static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1206 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001207{
1208 int *pref = (int *)data;
1209 ATResponse *p_response = NULL;
1210 char *line;
1211 int res;
1212 char *cmd = NULL;
1213
1214 asprintf(&cmd, "AT+WRMP=%d", *pref);
1215 if (cmd == NULL) goto error;
1216
1217 res = at_send_command(cmd, &p_response);
1218 if (res < 0 || !p_response->success)
1219 goto error;
1220
1221 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1222 free(cmd);
1223 return;
1224error:
1225 free(cmd);
1226 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1227}
1228
1229static int parseRegistrationState(char *str, int *type, int *items, int **response)
1230{
1231 int err;
1232 char *line = str, *p;
1233 int *resp = NULL;
1234 int skip;
1235 int count = 3;
1236 int commas;
1237
Wink Saville4dcab4f2012-11-19 16:05:13 -08001238 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001239 err = at_tok_start(&line);
1240 if (err < 0) goto error;
1241
1242 /* Ok you have to be careful here
1243 * The solicited version of the CREG response is
1244 * +CREG: n, stat, [lac, cid]
1245 * and the unsolicited version is
1246 * +CREG: stat, [lac, cid]
1247 * The <n> parameter is basically "is unsolicited creg on?"
1248 * which it should always be
1249 *
1250 * Now we should normally get the solicited version here,
1251 * but the unsolicited version could have snuck in
1252 * so we have to handle both
1253 *
1254 * Also since the LAC and CID are only reported when registered,
1255 * we can have 1, 2, 3, or 4 arguments here
1256 *
1257 * finally, a +CGREG: answer may have a fifth value that corresponds
1258 * to the network type, as in;
1259 *
1260 * +CGREG: n, stat [,lac, cid [,networkType]]
1261 */
1262
1263 /* count number of commas */
1264 commas = 0;
1265 for (p = line ; *p != '\0' ;p++) {
1266 if (*p == ',') commas++;
1267 }
1268
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001269 resp = (int *)calloc(commas + 1, sizeof(int));
1270 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001271 switch (commas) {
1272 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001273 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001274 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001275 resp[1] = -1;
1276 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001277 break;
1278
1279 case 1: /* +CREG: <n>, <stat> */
1280 err = at_tok_nextint(&line, &skip);
1281 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001282 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001283 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001284 resp[1] = -1;
1285 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001286 if (err < 0) goto error;
1287 break;
1288
1289 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001290 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001291 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001292 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001293 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001294 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001295 if (err < 0) goto error;
1296 break;
1297 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1298 err = at_tok_nextint(&line, &skip);
1299 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001300 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001301 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001302 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001303 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001304 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001305 if (err < 0) goto error;
1306 break;
1307 /* special case for CGREG, there is a fourth parameter
1308 * that is the network type (unknown/gprs/edge/umts)
1309 */
1310 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1311 err = at_tok_nextint(&line, &skip);
1312 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001313 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001314 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001315 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001316 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001317 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001318 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001319 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001320 if (err < 0) goto error;
1321 count = 4;
1322 break;
1323 default:
1324 goto error;
1325 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001326 s_lac = resp[1];
1327 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001328 if (response)
1329 *response = resp;
1330 if (items)
1331 *items = commas + 1;
1332 if (type)
1333 *type = techFromModemType(TECH(sMdmInfo));
1334 return 0;
1335error:
1336 free(resp);
1337 return -1;
1338}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001339
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001340#define REG_STATE_LEN 15
1341#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001342static void requestRegistrationState(int request, void *data __unused,
1343 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001344{
1345 int err;
1346 int *registration;
1347 char **responseStr = NULL;
1348 ATResponse *p_response = NULL;
1349 const char *cmd;
1350 const char *prefix;
1351 char *line;
1352 int i = 0, j, numElements = 0;
1353 int count = 3;
1354 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001355
Wink Saville4dcab4f2012-11-19 16:05:13 -08001356 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001357 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1358 cmd = "AT+CREG?";
1359 prefix = "+CREG:";
1360 numElements = REG_STATE_LEN;
1361 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1362 cmd = "AT+CGREG?";
1363 prefix = "+CGREG:";
1364 numElements = REG_DATA_STATE_LEN;
1365 } else {
1366 assert(0);
1367 goto error;
1368 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001369
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001370 err = at_send_command_singleline(cmd, prefix, &p_response);
1371
1372 if (err != 0) goto error;
1373
1374 line = p_response->p_intermediates->line;
1375
1376 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1377
1378 responseStr = malloc(numElements * sizeof(char *));
1379 if (!responseStr) goto error;
1380 memset(responseStr, 0, numElements * sizeof(char *));
1381 /**
1382 * The first '4' bytes for both registration states remain the same.
1383 * But if the request is 'DATA_REGISTRATION_STATE',
1384 * the 5th and 6th byte(s) are optional.
1385 */
1386 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001387 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001388 // TODO: Query modem
1389 startfrom = 3;
1390 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1391 asprintf(&responseStr[3], "8"); // EvDo revA
1392 asprintf(&responseStr[4], "1"); // BSID
1393 asprintf(&responseStr[5], "123"); // Latitude
1394 asprintf(&responseStr[6], "222"); // Longitude
1395 asprintf(&responseStr[7], "0"); // CSS Indicator
1396 asprintf(&responseStr[8], "4"); // SID
1397 asprintf(&responseStr[9], "65535"); // NID
1398 asprintf(&responseStr[10], "0"); // Roaming indicator
1399 asprintf(&responseStr[11], "1"); // System is in PRL
1400 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1401 asprintf(&responseStr[13], "0"); // Reason for denial
1402 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1403 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1404 asprintf(&responseStr[3], "8"); // Available data radio technology
1405 }
1406 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001407 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001408 startfrom = 0;
1409 asprintf(&responseStr[1], "%x", registration[1]);
1410 asprintf(&responseStr[2], "%x", registration[2]);
1411 if (count > 3)
1412 asprintf(&responseStr[3], "%d", registration[3]);
1413 }
1414 asprintf(&responseStr[0], "%d", registration[0]);
1415
1416 /**
1417 * Optional bytes for DATA_REGISTRATION_STATE request
1418 * 4th byte : Registration denial code
1419 * 5th byte : The max. number of simultaneous Data Calls
1420 */
1421 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1422 // asprintf(&responseStr[4], "3");
1423 // asprintf(&responseStr[5], "1");
1424 }
1425
1426 for (j = startfrom; j < numElements; j++) {
1427 if (!responseStr[i]) goto error;
1428 }
1429 free(registration);
1430 registration = NULL;
1431
1432 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1433 for (j = 0; j < numElements; j++ ) {
1434 free(responseStr[j]);
1435 responseStr[j] = NULL;
1436 }
1437 free(responseStr);
1438 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001439 at_response_free(p_response);
1440
1441 return;
1442error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001443 if (responseStr) {
1444 for (j = 0; j < numElements; j++) {
1445 free(responseStr[j]);
1446 responseStr[j] = NULL;
1447 }
1448 free(responseStr);
1449 responseStr = NULL;
1450 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001451 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001452 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1453 at_response_free(p_response);
1454}
1455
Mark Salyzynba58c202014-03-12 15:20:22 -07001456static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001457{
1458 int err;
1459 int i;
1460 int skip;
1461 ATLine *p_cur;
1462 char *response[3];
1463
1464 memset(response, 0, sizeof(response));
1465
1466 ATResponse *p_response = NULL;
1467
1468 err = at_send_command_multiline(
1469 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1470 "+COPS:", &p_response);
1471
1472 /* we expect 3 lines here:
1473 * +COPS: 0,0,"T - Mobile"
1474 * +COPS: 0,1,"TMO"
1475 * +COPS: 0,2,"310170"
1476 */
1477
1478 if (err != 0) goto error;
1479
1480 for (i = 0, p_cur = p_response->p_intermediates
1481 ; p_cur != NULL
1482 ; p_cur = p_cur->p_next, i++
1483 ) {
1484 char *line = p_cur->line;
1485
1486 err = at_tok_start(&line);
1487 if (err < 0) goto error;
1488
1489 err = at_tok_nextint(&line, &skip);
1490 if (err < 0) goto error;
1491
1492 // If we're unregistered, we may just get
1493 // a "+COPS: 0" response
1494 if (!at_tok_hasmore(&line)) {
1495 response[i] = NULL;
1496 continue;
1497 }
1498
1499 err = at_tok_nextint(&line, &skip);
1500 if (err < 0) goto error;
1501
1502 // a "+COPS: 0, n" response is also possible
1503 if (!at_tok_hasmore(&line)) {
1504 response[i] = NULL;
1505 continue;
1506 }
1507
1508 err = at_tok_nextstr(&line, &(response[i]));
1509 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001510 // Simple assumption that mcc and mnc are 3 digits each
1511 if (strlen(response[i]) == 6) {
1512 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1513 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1514 }
1515 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001516 }
1517
1518 if (i != 3) {
1519 /* expect 3 lines exactly */
1520 goto error;
1521 }
1522
1523 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1524 at_response_free(p_response);
1525
1526 return;
1527error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001528 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001529 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1530 at_response_free(p_response);
1531}
1532
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001533static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1534{
1535 int err = 1; // Set to go to error:
1536 RIL_SMS_Response response;
1537 RIL_CDMA_SMS_Message* rcsm;
1538
Mark Salyzynba58c202014-03-12 15:20:22 -07001539 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001540 datalen, sizeof(RIL_CDMA_SMS_Message));
1541
1542 // verify data content to test marshalling/unmarshalling:
1543 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001544 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001545 uServicecategory=%d, sAddress.digit_mode=%d, \
1546 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1547 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1548 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1549 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1550
1551 if (err != 0) goto error;
1552
1553 // Cdma Send SMS implementation will go here:
1554 // But it is not implemented yet.
1555
1556 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001557 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001558 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1559 return;
1560
1561error:
1562 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001563 response.messageRef = -1;
1564 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001565}
1566
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001567static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1568{
1569 int err;
1570 const char *smsc;
1571 const char *pdu;
1572 int tpLayerLength;
1573 char *cmd1, *cmd2;
1574 RIL_SMS_Response response;
1575 ATResponse *p_response = NULL;
1576
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001577 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001578 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001579
1580 if (s_ims_gsm_fail != 0) goto error;
1581 if (s_ims_gsm_retry != 0) goto error2;
1582
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001583 smsc = ((const char **)data)[0];
1584 pdu = ((const char **)data)[1];
1585
1586 tpLayerLength = strlen(pdu)/2;
1587
1588 // "NULL for default SMSC"
1589 if (smsc == NULL) {
1590 smsc= "00";
1591 }
1592
1593 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1594 asprintf(&cmd2, "%s%s", smsc, pdu);
1595
1596 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1597
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001598 free(cmd1);
1599 free(cmd2);
1600
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001601 if (err != 0 || p_response->success == 0) goto error;
1602
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001603 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001604 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001605 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1606 at_response_free(p_response);
1607
1608 return;
1609error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001610 response.messageRef = -2;
1611 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001612 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001613 return;
1614error2:
1615 // send retry error.
1616 response.messageRef = -1;
1617 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1618 at_response_free(p_response);
1619 return;
1620 }
1621
1622static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1623{
1624 RIL_IMS_SMS_Message *p_args;
1625 RIL_SMS_Response response;
1626
1627 memset(&response, 0, sizeof(response));
1628
Mark Salyzynba58c202014-03-12 15:20:22 -07001629 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001630 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1631 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1632 datalen, s_ims_registered, s_ims_services, s_ims_format,
1633 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1634 s_ims_gsm_retry);
1635
1636 // figure out if this is gsm/cdma format
1637 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1638 p_args = (RIL_IMS_SMS_Message *)data;
1639
1640 if (0 != s_ims_cause_perm_failure ) goto error;
1641
1642 // want to fail over ims and this is first request over ims
1643 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1644
1645 if (RADIO_TECH_3GPP == p_args->tech) {
1646 return requestSendSMS(p_args->message.gsmMessage,
1647 datalen - sizeof(RIL_RadioTechnologyFamily),
1648 t);
1649 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1650 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1651 datalen - sizeof(RIL_RadioTechnologyFamily),
1652 t);
1653 } else {
1654 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1655 }
1656
1657error:
1658 response.messageRef = -2;
1659 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1660 return;
1661
1662error2:
1663 response.messageRef = -1;
1664 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001665}
1666
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001667static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1668{
1669 ATResponse *p_response = NULL;
1670 int32_t session_id;
1671 int err;
1672 char cmd[32];
1673 char dummy;
1674 char *line;
1675
1676 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1677 if (data == NULL || datalen == 0 || datalen > 16) {
1678 ALOGE("Invalid data passed to requestSimOpenChannel");
1679 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1680 return;
1681 }
1682
1683 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1684
1685 err = at_send_command_numeric(cmd, &p_response);
1686 if (err < 0 || p_response == NULL || p_response->success == 0) {
1687 ALOGE("Error %d opening logical channel: %d",
1688 err, p_response ? p_response->success : 0);
1689 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1690 at_response_free(p_response);
1691 return;
1692 }
1693
1694 // Ensure integer only by scanning for an extra char but expect one result
1695 line = p_response->p_intermediates->line;
1696 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1697 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1698 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1699 return;
1700 }
1701
1702 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1703 at_response_free(p_response);
1704}
1705
1706static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1707{
1708 ATResponse *p_response = NULL;
1709 int32_t session_id;
1710 int err;
1711 char cmd[32];
1712
1713 if (data == NULL || datalen != sizeof(session_id)) {
1714 ALOGE("Invalid data passed to requestSimCloseChannel");
1715 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1716 return;
1717 }
1718 session_id = ((int32_t *)data)[0];
1719 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1720 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1721
1722 if (err < 0 || p_response == NULL || p_response->success == 0) {
1723 ALOGE("Error %d closing logical channel %d: %d",
1724 err, session_id, p_response ? p_response->success : 0);
1725 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1726 at_response_free(p_response);
1727 return;
1728 }
1729
1730 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1731
1732 at_response_free(p_response);
1733}
1734
1735static void requestSimTransmitApduChannel(void *data,
1736 size_t datalen,
1737 RIL_Token t)
1738{
1739 ATResponse *p_response = NULL;
1740 int err;
1741 char *cmd;
1742 char *line;
1743 size_t cmd_size;
1744 RIL_SIM_IO_Response sim_response;
1745 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1746
1747 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1748 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1749 return;
1750 }
1751
1752 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
Wei Wang9cec1e02017-02-08 14:37:37 -08001753 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001754 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1755 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1756
1757 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1758 free(cmd);
1759 if (err < 0 || p_response == NULL || p_response->success == 0) {
1760 ALOGE("Error %d transmitting APDU: %d",
1761 err, p_response ? p_response->success : 0);
1762 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1763 at_response_free(p_response);
1764 return;
1765 }
1766
1767 line = p_response->p_intermediates->line;
1768 err = parseSimResponseLine(line, &sim_response);
1769
1770 if (err == 0) {
1771 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1772 &sim_response, sizeof(sim_response));
1773 } else {
1774 ALOGE("Error %d parsing SIM response line: %s", err, line);
1775 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1776 }
1777 at_response_free(p_response);
1778}
1779
Wink Savillef4c4d362009-04-02 01:37:03 -07001780static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001781{
1782 const char *apn;
1783 char *cmd;
1784 int err;
1785 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001786
Wink Savillef4c4d362009-04-02 01:37:03 -07001787 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001788
1789#ifdef USE_TI_COMMANDS
1790 // Config for multislot class 10 (probably default anyway eh?)
1791 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1792 NULL);
1793
1794 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1795#endif /* USE_TI_COMMANDS */
1796
1797 int fd, qmistatus;
1798 size_t cur = 0;
1799 size_t len;
1800 ssize_t written, rlen;
1801 char status[32] = {0};
1802 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001803 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001804
Wink Saville4dcab4f2012-11-19 16:05:13 -08001805 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001806
1807 fd = open ("/dev/qmi", O_RDWR);
1808 if (fd >= 0) { /* the device doesn't exist on the emulator */
1809
Wink Saville4dcab4f2012-11-19 16:05:13 -08001810 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001811 asprintf(&cmd, "up:%s", apn);
1812 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001813
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001814 while (cur < len) {
1815 do {
1816 written = write (fd, cmd + cur, len - cur);
1817 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001818
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001819 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001820 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001821 close(fd);
1822 goto error;
1823 }
1824
1825 cur += written;
1826 }
1827
1828 // wait for interface to come online
1829
1830 do {
1831 sleep(1);
1832 do {
1833 rlen = read(fd, status, 31);
1834 } while (rlen < 0 && errno == EINTR);
1835
1836 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001837 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001838 close(fd);
1839 goto error;
1840 } else {
1841 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001842 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001843 }
1844 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1845
1846 close(fd);
1847
1848 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001849 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001850 goto error;
1851 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001852
1853 qmistatus = system("netcfg rmnet0 dhcp");
1854
Wink Saville4dcab4f2012-11-19 16:05:13 -08001855 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001856
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001857 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001858
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001859 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001860
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001861 if (datalen > 6 * sizeof(char *)) {
1862 pdp_type = ((const char **)data)[6];
1863 } else {
1864 pdp_type = "IP";
1865 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001866
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001867 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001868 //FIXME check for error here
1869 err = at_send_command(cmd, NULL);
1870 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001871
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001872 // Set required QoS params to default
1873 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001874
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001875 // Set minimum QoS params to default
1876 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001877
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001878 // packet-domain event reporting
1879 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001880
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001881 // Hangup anything that's happening there now
1882 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001883
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001884 // Start data on PDP context 1
1885 err = at_send_command("ATD*99***1#", &p_response);
1886
1887 if (err < 0 || p_response->success == 0) {
1888 goto error;
1889 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001890 }
1891
Wink Saville43808972011-01-13 17:39:51 -08001892 requestOrSendDataCallList(&t);
1893
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001894 at_response_free(p_response);
1895
1896 return;
1897error:
1898 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1899 at_response_free(p_response);
1900
1901}
1902
Mark Salyzynba58c202014-03-12 15:20:22 -07001903static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001904{
1905 int ackSuccess;
1906 int err;
1907
1908 ackSuccess = ((int *)data)[0];
1909
1910 if (ackSuccess == 1) {
1911 err = at_send_command("AT+CNMA=1", NULL);
1912 } else if (ackSuccess == 0) {
1913 err = at_send_command("AT+CNMA=2", NULL);
1914 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001915 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001916 goto error;
1917 }
1918
1919 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1920error:
1921 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1922
1923}
1924
Mark Salyzynba58c202014-03-12 15:20:22 -07001925static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001926{
1927 ATResponse *p_response = NULL;
1928 RIL_SIM_IO_Response sr;
1929 int err;
1930 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001931 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001932 char *line;
1933
1934 memset(&sr, 0, sizeof(sr));
1935
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001936 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001937
1938 /* FIXME handle pin2 */
1939
1940 if (p_args->data == NULL) {
1941 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1942 p_args->command, p_args->fileid,
1943 p_args->p1, p_args->p2, p_args->p3);
1944 } else {
1945 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1946 p_args->command, p_args->fileid,
1947 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1948 }
1949
1950 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1951
1952 if (err < 0 || p_response->success == 0) {
1953 goto error;
1954 }
1955
1956 line = p_response->p_intermediates->line;
1957
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001958 err = parseSimResponseLine(line, &sr);
1959 if (err < 0) {
1960 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001961 }
1962
1963 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1964 at_response_free(p_response);
1965 free(cmd);
1966
1967 return;
1968error:
1969 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1970 at_response_free(p_response);
1971 free(cmd);
1972
1973}
1974
1975static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1976{
1977 ATResponse *p_response = NULL;
1978 int err;
1979 char* cmd = NULL;
1980 const char** strings = (const char**)data;;
1981
1982 if ( datalen == sizeof(char*) ) {
1983 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1984 } else if ( datalen == 2*sizeof(char*) ) {
1985 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1986 } else
1987 goto error;
1988
1989 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1990 free(cmd);
1991
1992 if (err < 0 || p_response->success == 0) {
1993error:
1994 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1995 } else {
1996 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1997 }
1998 at_response_free(p_response);
1999}
2000
2001
Mark Salyzynba58c202014-03-12 15:20:22 -07002002static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002003{
2004 const char *ussdRequest;
2005
2006 ussdRequest = (char *)(data);
2007
2008
2009 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2010
2011// @@@ TODO
2012
2013}
2014
Mark Salyzynba58c202014-03-12 15:20:22 -07002015static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002016{
2017 int err;
2018 ATResponse *p_response = NULL;
2019
2020 err = at_send_command("AT+WSOS=0", &p_response);
2021
2022 if (err < 0 || p_response->success == 0) {
2023 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2024 return;
2025 }
2026
2027 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2028}
2029
2030// TODO: Use all radio types
2031static int techFromModemType(int mdmtype)
2032{
2033 int ret = -1;
2034 switch (1 << mdmtype) {
2035 case MDM_CDMA:
2036 ret = RADIO_TECH_1xRTT;
2037 break;
2038 case MDM_EVDO:
2039 ret = RADIO_TECH_EVDO_A;
2040 break;
2041 case MDM_GSM:
2042 ret = RADIO_TECH_GPRS;
2043 break;
2044 case MDM_WCDMA:
2045 ret = RADIO_TECH_HSPA;
2046 break;
2047 case MDM_LTE:
2048 ret = RADIO_TECH_LTE;
2049 break;
2050 }
2051 return ret;
2052}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002053
Mark Salyzynba58c202014-03-12 15:20:22 -07002054static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002055{
2056 uint64_t curTime = ril_nano_time();
2057 RIL_CellInfo ci[1] =
2058 {
2059 { // ci[0]
2060 1, // cellInfoType
2061 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002062 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002063 curTime - 1000, // Fake some time in the past
2064 { // union CellInfo
2065 { // RIL_CellInfoGsm gsm
2066 { // gsm.cellIdneityGsm
2067 s_mcc, // mcc
2068 s_mnc, // mnc
2069 s_lac, // lac
2070 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002071 },
2072 { // gsm.signalStrengthGsm
2073 10, // signalStrength
2074 0 // bitErrorRate
2075 }
2076 }
2077 }
2078 }
2079 };
2080
2081 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2082}
2083
2084
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002085static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002086{
2087 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2088 // will be sent.
2089 assert (datalen == sizeof(int));
2090 s_cell_info_rate_ms = ((int *)data)[0];
2091
2092 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2093}
2094
Etan Cohend3652192014-06-20 08:28:44 -07002095static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2096{
2097 // TODO - hook this up with real query/info from radio.
2098
2099 RIL_HardwareConfig hwCfg;
2100
2101 RIL_UNUSED_PARM(data);
2102 RIL_UNUSED_PARM(datalen);
2103
2104 hwCfg.type = -1;
2105
2106 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2107}
2108
Jim Kayed2d82012017-10-06 14:17:47 -07002109static void requestGetTtyMode(void *data, size_t datalen, RIL_Token t)
2110{
2111 int ttyModeResponse;
2112
2113 RIL_UNUSED_PARM(data);
2114 RIL_UNUSED_PARM(datalen);
2115
2116 ttyModeResponse = (getSIMStatus() == SIM_READY) ? 1 // TTY Full
2117 : 0; // TTY Off
2118
2119 RIL_onRequestComplete(t, RIL_E_SUCCESS, &ttyModeResponse, sizeof(ttyModeResponse));
2120}
2121
2122static void requestGetRadioCapability(void *data, size_t datalen, RIL_Token t)
2123{
2124 RIL_RadioCapability radioCapability;
2125
2126 RIL_UNUSED_PARM(data);
2127 RIL_UNUSED_PARM(datalen);
2128
2129 radioCapability.version = RIL_RADIO_CAPABILITY_VERSION;
2130 radioCapability.session = 0;
2131 radioCapability.phase = 0;
2132 radioCapability.rat = 0;
2133 radioCapability.logicalModemUuid[0] = '\0';
2134 radioCapability.status = RC_STATUS_SUCCESS;
2135
2136 RIL_onRequestComplete(t, RIL_E_SUCCESS, &radioCapability, sizeof(radioCapability));
2137}
2138
2139static void requestGetMute(void *data, size_t datalen, RIL_Token t)
2140{
2141 int muteResponse;
2142
2143 RIL_UNUSED_PARM(data);
2144 RIL_UNUSED_PARM(datalen);
2145
2146 muteResponse = 0; // Mute disabled
2147
2148 RIL_onRequestComplete(t, RIL_E_SUCCESS, &muteResponse, sizeof(muteResponse));
2149}
Etan Cohend3652192014-06-20 08:28:44 -07002150
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002151/*** Callback methods from the RIL library to us ***/
2152
2153/**
2154 * Call from RIL to us to make a RIL_REQUEST
2155 *
2156 * Must be completed with a call to RIL_onRequestComplete()
2157 *
2158 * RIL_onRequestComplete() may be called from any thread, before or after
2159 * this function returns.
2160 *
Weilun Du9f471e22017-02-07 10:47:19 -08002161 * Because onRequest function could be called from multiple different thread,
2162 * we must ensure that the underlying at_send_command_* function
2163 * is atomic.
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002164 */
2165static void
2166onRequest (int request, void *data, size_t datalen, RIL_Token t)
2167{
2168 ATResponse *p_response;
2169 int err;
2170
Wink Saville4dcab4f2012-11-19 16:05:13 -08002171 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002172
2173 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2174 * when RADIO_STATE_UNAVAILABLE.
2175 */
2176 if (sState == RADIO_STATE_UNAVAILABLE
2177 && request != RIL_REQUEST_GET_SIM_STATUS
2178 ) {
2179 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2180 return;
2181 }
2182
2183 /* Ignore all non-power requests when RADIO_STATE_OFF
2184 * (except RIL_REQUEST_GET_SIM_STATUS)
2185 */
Jim Kayed2d82012017-10-06 14:17:47 -07002186 if (sState == RADIO_STATE_OFF) {
2187 switch(request) {
2188 case RIL_REQUEST_BASEBAND_VERSION:
2189 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2190 case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE:
2191 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2192 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2193 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2194 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2195 case RIL_REQUEST_DEVICE_IDENTITY:
2196 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2197 case RIL_REQUEST_GET_ACTIVITY_INFO:
2198 case RIL_REQUEST_GET_CARRIER_RESTRICTIONS:
2199 case RIL_REQUEST_GET_CURRENT_CALLS:
2200 case RIL_REQUEST_GET_IMEI:
2201 case RIL_REQUEST_GET_MUTE:
2202 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2203 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2204 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2205 case RIL_REQUEST_GET_SIM_STATUS:
2206 case RIL_REQUEST_NV_RESET_CONFIG:
2207 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2208 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2209 case RIL_REQUEST_QUERY_TTY_MODE:
2210 case RIL_REQUEST_RADIO_POWER:
2211 case RIL_REQUEST_SET_BAND_MODE:
2212 case RIL_REQUEST_SET_CARRIER_RESTRICTIONS:
2213 case RIL_REQUEST_SET_LOCATION_UPDATES:
2214 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2215 case RIL_REQUEST_SET_TTY_MODE:
2216 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2217 case RIL_REQUEST_STOP_LCE:
2218 case RIL_REQUEST_VOICE_RADIO_TECH:
2219 // Process all the above, even though the radio is off
2220 break;
2221
2222 default:
2223 // For all others, say NOT_AVAILABLE because the radio is off
2224 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2225 return;
2226 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002227 }
2228
2229 switch (request) {
2230 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002231 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002232 char *p_buffer;
2233 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002234
Wink Savillef6aa7c12009-04-30 14:20:52 -07002235 int result = getCardStatus(&p_card_status);
2236 if (result == RIL_E_SUCCESS) {
2237 p_buffer = (char *)p_card_status;
2238 buffer_size = sizeof(*p_card_status);
2239 } else {
2240 p_buffer = NULL;
2241 buffer_size = 0;
2242 }
2243 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2244 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002245 break;
2246 }
2247 case RIL_REQUEST_GET_CURRENT_CALLS:
2248 requestGetCurrentCalls(data, datalen, t);
2249 break;
2250 case RIL_REQUEST_DIAL:
2251 requestDial(data, datalen, t);
2252 break;
2253 case RIL_REQUEST_HANGUP:
2254 requestHangup(data, datalen, t);
2255 break;
2256 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
2257 // 3GPP 22.030 6.5.5
2258 // "Releases all held calls or sets User Determined User Busy
2259 // (UDUB) for a waiting call."
2260 at_send_command("AT+CHLD=0", NULL);
2261
2262 /* success or failure is ignored by the upper layer here.
2263 it will call GET_CURRENT_CALLS and determine success that way */
2264 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2265 break;
2266 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
2267 // 3GPP 22.030 6.5.5
2268 // "Releases all active calls (if any exist) and accepts
2269 // the other (held or waiting) call."
2270 at_send_command("AT+CHLD=1", NULL);
2271
2272 /* success or failure is ignored by the upper layer here.
2273 it will call GET_CURRENT_CALLS and determine success that way */
2274 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2275 break;
2276 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
2277 // 3GPP 22.030 6.5.5
2278 // "Places all active calls (if any exist) on hold and accepts
2279 // the other (held or waiting) call."
2280 at_send_command("AT+CHLD=2", NULL);
2281
2282#ifdef WORKAROUND_ERRONEOUS_ANSWER
2283 s_expectAnswer = 1;
2284#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2285
2286 /* success or failure is ignored by the upper layer here.
2287 it will call GET_CURRENT_CALLS and determine success that way */
2288 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2289 break;
2290 case RIL_REQUEST_ANSWER:
2291 at_send_command("ATA", NULL);
2292
2293#ifdef WORKAROUND_ERRONEOUS_ANSWER
2294 s_expectAnswer = 1;
2295#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2296
2297 /* success or failure is ignored by the upper layer here.
2298 it will call GET_CURRENT_CALLS and determine success that way */
2299 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2300 break;
2301 case RIL_REQUEST_CONFERENCE:
2302 // 3GPP 22.030 6.5.5
2303 // "Adds a held call to the conversation"
2304 at_send_command("AT+CHLD=3", NULL);
2305
2306 /* success or failure is ignored by the upper layer here.
2307 it will call GET_CURRENT_CALLS and determine success that way */
2308 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2309 break;
2310 case RIL_REQUEST_UDUB:
2311 /* user determined user busy */
2312 /* sometimes used: ATH */
2313 at_send_command("ATH", NULL);
2314
2315 /* success or failure is ignored by the upper layer here.
2316 it will call GET_CURRENT_CALLS and determine success that way */
2317 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2318 break;
2319
2320 case RIL_REQUEST_SEPARATE_CONNECTION:
2321 {
2322 char cmd[12];
2323 int party = ((int*)data)[0];
2324
2325 // Make sure that party is in a valid range.
2326 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2327 // It's sufficient for us to just make sure it's single digit.)
2328 if (party > 0 && party < 10) {
2329 sprintf(cmd, "AT+CHLD=2%d", party);
2330 at_send_command(cmd, NULL);
2331 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2332 } else {
2333 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2334 }
2335 }
2336 break;
2337
2338 case RIL_REQUEST_SIGNAL_STRENGTH:
2339 requestSignalStrength(data, datalen, t);
2340 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002341 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2342 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002343 requestRegistrationState(request, data, datalen, t);
2344 break;
2345 case RIL_REQUEST_OPERATOR:
2346 requestOperator(data, datalen, t);
2347 break;
2348 case RIL_REQUEST_RADIO_POWER:
2349 requestRadioPower(data, datalen, t);
2350 break;
2351 case RIL_REQUEST_DTMF: {
2352 char c = ((char *)data)[0];
2353 char *cmd;
2354 asprintf(&cmd, "AT+VTS=%c", (int)c);
2355 at_send_command(cmd, NULL);
2356 free(cmd);
2357 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2358 break;
2359 }
2360 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302361 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002362 requestSendSMS(data, datalen, t);
2363 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002364 case RIL_REQUEST_CDMA_SEND_SMS:
2365 requestCdmaSendSMS(data, datalen, t);
2366 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002367 case RIL_REQUEST_IMS_SEND_SMS:
2368 requestImsSendSMS(data, datalen, t);
2369 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002370 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2371 requestSimOpenChannel(data, datalen, t);
2372 break;
2373 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2374 requestSimCloseChannel(data, datalen, t);
2375 break;
2376 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2377 requestSimTransmitApduChannel(data, datalen, t);
2378 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002379 case RIL_REQUEST_SETUP_DATA_CALL:
2380 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002381 break;
2382 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2383 requestSMSAcknowledge(data, datalen, t);
2384 break;
2385
2386 case RIL_REQUEST_GET_IMSI:
2387 p_response = NULL;
2388 err = at_send_command_numeric("AT+CIMI", &p_response);
2389
2390 if (err < 0 || p_response->success == 0) {
2391 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2392 } else {
2393 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2394 p_response->p_intermediates->line, sizeof(char *));
2395 }
2396 at_response_free(p_response);
2397 break;
2398
2399 case RIL_REQUEST_GET_IMEI:
2400 p_response = NULL;
2401 err = at_send_command_numeric("AT+CGSN", &p_response);
2402
2403 if (err < 0 || p_response->success == 0) {
2404 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2405 } else {
2406 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2407 p_response->p_intermediates->line, sizeof(char *));
2408 }
2409 at_response_free(p_response);
2410 break;
2411
2412 case RIL_REQUEST_SIM_IO:
2413 requestSIM_IO(data,datalen,t);
2414 break;
2415
2416 case RIL_REQUEST_SEND_USSD:
2417 requestSendUSSD(data, datalen, t);
2418 break;
2419
2420 case RIL_REQUEST_CANCEL_USSD:
2421 p_response = NULL;
2422 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2423
2424 if (err < 0 || p_response->success == 0) {
2425 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2426 } else {
2427 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2428 p_response->p_intermediates->line, sizeof(char *));
2429 }
2430 at_response_free(p_response);
2431 break;
2432
2433 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2434 at_send_command("AT+COPS=0", NULL);
2435 break;
2436
Wink Savillef4c4d362009-04-02 01:37:03 -07002437 case RIL_REQUEST_DATA_CALL_LIST:
2438 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002439 break;
2440
2441 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2442 requestQueryNetworkSelectionMode(data, datalen, t);
2443 break;
2444
2445 case RIL_REQUEST_OEM_HOOK_RAW:
2446 // echo back data
2447 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2448 break;
2449
2450
2451 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2452 int i;
2453 const char ** cur;
2454
Wink Saville4dcab4f2012-11-19 16:05:13 -08002455 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002456
2457
2458 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2459 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002460 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002461 }
2462
2463 // echo back strings
2464 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2465 break;
2466 }
2467
2468 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2469 requestWriteSmsToSim(data, datalen, t);
2470 break;
2471
2472 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2473 char * cmd;
2474 p_response = NULL;
2475 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2476 err = at_send_command(cmd, &p_response);
2477 free(cmd);
2478 if (err < 0 || p_response->success == 0) {
2479 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2480 } else {
2481 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2482 }
2483 at_response_free(p_response);
2484 break;
2485 }
2486
2487 case RIL_REQUEST_ENTER_SIM_PIN:
2488 case RIL_REQUEST_ENTER_SIM_PUK:
2489 case RIL_REQUEST_ENTER_SIM_PIN2:
2490 case RIL_REQUEST_ENTER_SIM_PUK2:
2491 case RIL_REQUEST_CHANGE_SIM_PIN:
2492 case RIL_REQUEST_CHANGE_SIM_PIN2:
2493 requestEnterSimPin(data, datalen, t);
2494 break;
2495
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002496 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2497 int reply[2];
2498 //0==unregistered, 1==registered
2499 reply[0] = s_ims_registered;
2500
2501 //to be used when changed to include service supporated info
2502 //reply[1] = s_ims_services;
2503
2504 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2505 reply[1] = s_ims_format;
2506
2507 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2508 reply[0], reply[1]);
2509 if (reply[1] != -1) {
2510 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2511 } else {
2512 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2513 }
2514 break;
2515 }
2516
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002517 case RIL_REQUEST_VOICE_RADIO_TECH:
2518 {
2519 int tech = techFromModemType(TECH(sMdmInfo));
2520 if (tech < 0 )
2521 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2522 else
2523 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2524 }
2525 break;
2526 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2527 requestSetPreferredNetworkType(request, data, datalen, t);
2528 break;
2529
2530 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2531 requestGetPreferredNetworkType(request, data, datalen, t);
2532 break;
2533
Jun Tian58027012013-07-30 11:07:22 +08002534 case RIL_REQUEST_GET_CELL_INFO_LIST:
2535 requestGetCellInfoList(data, datalen, t);
2536 break;
2537
2538 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2539 requestSetCellInfoListRate(data, datalen, t);
2540 break;
2541
Etan Cohend3652192014-06-20 08:28:44 -07002542 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2543 requestGetHardwareConfig(data, datalen, t);
2544 break;
2545
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002546 case RIL_REQUEST_SHUTDOWN:
2547 requestShutdown(t);
2548 break;
2549
Jim Kayed2d82012017-10-06 14:17:47 -07002550 case RIL_REQUEST_QUERY_TTY_MODE:
2551 requestGetTtyMode(data, datalen, t);
2552 break;
2553
2554 case RIL_REQUEST_GET_RADIO_CAPABILITY:
2555 requestGetRadioCapability(data, datalen, t);
2556 break;
2557
2558 case RIL_REQUEST_GET_MUTE:
2559 requestGetMute(data, datalen, t);
2560 break;
2561
2562 case RIL_REQUEST_SET_INITIAL_ATTACH_APN:
2563 case RIL_REQUEST_ALLOW_DATA:
2564 case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION:
2565 case RIL_REQUEST_SET_CLIR:
2566 case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION:
2567 case RIL_REQUEST_SET_BAND_MODE:
2568 case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE:
2569 case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS:
2570 case RIL_REQUEST_SET_LOCATION_UPDATES:
2571 case RIL_REQUEST_SET_TTY_MODE:
2572 case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE:
2573 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2574 break;
2575
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002576 case RIL_REQUEST_BASEBAND_VERSION:
Jim Kayed2d82012017-10-06 14:17:47 -07002577 requestCdmaBaseBandVersion(request, data, datalen, t);
2578 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002579
2580 case RIL_REQUEST_DEVICE_IDENTITY:
Jim Kayed2d82012017-10-06 14:17:47 -07002581 requestCdmaDeviceIdentity(request, data, datalen, t);
2582 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002583
2584 case RIL_REQUEST_CDMA_SUBSCRIPTION:
Jim Kayed2d82012017-10-06 14:17:47 -07002585 requestCdmaSubscription(request, data, datalen, t);
2586 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002587
Jim Kayed2d82012017-10-06 14:17:47 -07002588 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2589 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2590 break;
2591
2592 /* CDMA Specific Requests */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002593 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2594 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2595 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2596 break;
2597 } // Fall-through if tech is not cdma
2598
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002599 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2600 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2601 requestCdmaGetRoamingPreference(request, data, datalen, t);
2602 break;
2603 } // Fall-through if tech is not cdma
2604
2605 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2606 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2607 requestCdmaSetRoamingPreference(request, data, datalen, t);
2608 break;
2609 } // Fall-through if tech is not cdma
2610
2611 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2612 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2613 requestExitEmergencyMode(data, datalen, t);
2614 break;
2615 } // Fall-through if tech is not cdma
2616
Jim Kayed2d82012017-10-06 14:17:47 -07002617 // Fall-through to here when the request is specific to CDMA, but
2618 // our tech is not CDMA. VTS tests expect us to silently do nothing.
2619 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2620 break;
2621
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002622 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002623 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002624 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2625 break;
2626 }
2627}
2628
2629/**
2630 * Synchronous call from the RIL to us to return current radio state.
2631 * RADIO_STATE_UNAVAILABLE should be the initial state.
2632 */
2633static RIL_RadioState
2634currentState()
2635{
2636 return sState;
2637}
2638/**
2639 * Call from RIL to us to find out whether a specific request code
2640 * is supported by this implementation.
2641 *
2642 * Return 1 for "supported" and 0 for "unsupported"
2643 */
2644
2645static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002646onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002647{
2648 //@@@ todo
2649
2650 return 1;
2651}
2652
Mark Salyzynba58c202014-03-12 15:20:22 -07002653static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002654{
2655 //@@@todo
2656
2657}
2658
2659static const char * getVersion(void)
2660{
2661 return "android reference-ril 1.0";
2662}
2663
2664static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002665setRadioTechnology(ModemInfo *mdm, int newtech)
2666{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002667 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002668
2669 int oldtech = TECH(mdm);
2670
2671 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002672 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002673 TECH(mdm) = newtech;
2674 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2675 int tech = techFromModemType(TECH(sMdmInfo));
2676 if (tech > 0 ) {
2677 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2678 &tech, sizeof(tech));
2679 }
2680 }
2681 }
2682}
2683
2684static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002685setRadioState(RIL_RadioState newState)
2686{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002687 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002688 RIL_RadioState oldState;
2689
2690 pthread_mutex_lock(&s_state_mutex);
2691
2692 oldState = sState;
2693
2694 if (s_closed > 0) {
2695 // If we're closed, the only reasonable state is
2696 // RADIO_STATE_UNAVAILABLE
2697 // This is here because things on the main thread
2698 // may attempt to change the radio state after the closed
2699 // event happened in another thread
2700 newState = RADIO_STATE_UNAVAILABLE;
2701 }
2702
2703 if (sState != newState || s_closed > 0) {
2704 sState = newState;
2705
2706 pthread_cond_broadcast (&s_state_cond);
2707 }
2708
2709 pthread_mutex_unlock(&s_state_mutex);
2710
2711
2712 /* do these outside of the mutex */
2713 if (sState != oldState) {
2714 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2715 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002716 // Sim state can change as result of radio state change
2717 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2718 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002719
2720 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2721 * from the AT reader thread
2722 * Currently, this doesn't happen, but if that changes then these
2723 * will need to be dispatched on the request thread
2724 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002725 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002726 onRadioPowerOn();
2727 }
2728 }
2729}
2730
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002731/** Returns RUIM_NOT_READY on error */
2732static SIM_Status
2733getRUIMStatus()
2734{
2735 ATResponse *p_response = NULL;
2736 int err;
2737 int ret;
2738 char *cpinLine;
2739 char *cpinResult;
2740
2741 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2742 ret = SIM_NOT_READY;
2743 goto done;
2744 }
2745
2746 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2747
2748 if (err != 0) {
2749 ret = SIM_NOT_READY;
2750 goto done;
2751 }
2752
2753 switch (at_get_cme_error(p_response)) {
2754 case CME_SUCCESS:
2755 break;
2756
2757 case CME_SIM_NOT_INSERTED:
2758 ret = SIM_ABSENT;
2759 goto done;
2760
2761 default:
2762 ret = SIM_NOT_READY;
2763 goto done;
2764 }
2765
2766 /* CPIN? has succeeded, now look at the result */
2767
2768 cpinLine = p_response->p_intermediates->line;
2769 err = at_tok_start (&cpinLine);
2770
2771 if (err < 0) {
2772 ret = SIM_NOT_READY;
2773 goto done;
2774 }
2775
2776 err = at_tok_nextstr(&cpinLine, &cpinResult);
2777
2778 if (err < 0) {
2779 ret = SIM_NOT_READY;
2780 goto done;
2781 }
2782
2783 if (0 == strcmp (cpinResult, "SIM PIN")) {
2784 ret = SIM_PIN;
2785 goto done;
2786 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2787 ret = SIM_PUK;
2788 goto done;
2789 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2790 return SIM_NETWORK_PERSONALIZATION;
2791 } else if (0 != strcmp (cpinResult, "READY")) {
2792 /* we're treating unsupported lock types as "sim absent" */
2793 ret = SIM_ABSENT;
2794 goto done;
2795 }
2796
2797 at_response_free(p_response);
2798 p_response = NULL;
2799 cpinResult = NULL;
2800
2801 ret = SIM_READY;
2802
2803done:
2804 at_response_free(p_response);
2805 return ret;
2806}
2807
John Wang309ac292009-07-30 14:53:23 -07002808/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002809static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002810getSIMStatus()
2811{
2812 ATResponse *p_response = NULL;
2813 int err;
2814 int ret;
2815 char *cpinLine;
2816 char *cpinResult;
2817
Wink Saville4dcab4f2012-11-19 16:05:13 -08002818 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002819 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2820
2821 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002822 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002823 goto done;
2824 }
2825
2826 switch (at_get_cme_error(p_response)) {
2827 case CME_SUCCESS:
2828 break;
2829
2830 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002831 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002832 goto done;
2833
2834 default:
John Wang309ac292009-07-30 14:53:23 -07002835 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002836 goto done;
2837 }
2838
2839 /* CPIN? has succeeded, now look at the result */
2840
2841 cpinLine = p_response->p_intermediates->line;
2842 err = at_tok_start (&cpinLine);
2843
2844 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002845 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002846 goto done;
2847 }
2848
2849 err = at_tok_nextstr(&cpinLine, &cpinResult);
2850
2851 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002852 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002853 goto done;
2854 }
2855
2856 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002857 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002858 goto done;
2859 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07002860 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002861 goto done;
2862 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002863 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002864 } else if (0 != strcmp (cpinResult, "READY")) {
2865 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07002866 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002867 goto done;
2868 }
2869
2870 at_response_free(p_response);
2871 p_response = NULL;
2872 cpinResult = NULL;
2873
Jim Kayed2d82012017-10-06 14:17:47 -07002874 ret = (sState == RADIO_STATE_ON) ? SIM_READY : SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002875
2876done:
2877 at_response_free(p_response);
2878 return ret;
2879}
2880
2881
2882/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07002883 * Get the current card status.
2884 *
2885 * This must be freed using freeCardStatus.
2886 * @return: On success returns RIL_E_SUCCESS
2887 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002888static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002889 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07002890 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07002891 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2892 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002893 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07002894 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2895 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002896 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07002897 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2898 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002899 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07002900 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2901 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002902 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07002903 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2904 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002905 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07002906 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002907 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2908 // RUIM_ABSENT = 6
2909 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2910 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2911 // RUIM_NOT_READY = 7
2912 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2913 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2914 // RUIM_READY = 8
2915 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2916 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2917 // RUIM_PIN = 9
2918 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2919 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2920 // RUIM_PUK = 10
2921 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2922 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2923 // RUIM_NETWORK_PERSONALIZATION = 11
2924 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
bohu076e6872017-07-02 21:33:28 -07002925 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2926 // ISIM_ABSENT = 12
2927 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2928 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2929 // ISIM_NOT_READY = 13
2930 { RIL_APPTYPE_ISIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2931 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2932 // ISIM_READY = 14
2933 { RIL_APPTYPE_ISIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2934 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2935 // ISIM_PIN = 15
2936 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2937 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2938 // ISIM_PUK = 16
2939 { RIL_APPTYPE_ISIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2940 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2941 // ISIM_NETWORK_PERSONALIZATION = 17
2942 { RIL_APPTYPE_ISIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2943 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2944
Wink Savillef6aa7c12009-04-30 14:20:52 -07002945 };
2946 RIL_CardState card_state;
2947 int num_apps;
2948
2949 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07002950 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002951 card_state = RIL_CARDSTATE_ABSENT;
2952 num_apps = 0;
2953 } else {
2954 card_state = RIL_CARDSTATE_PRESENT;
bohu076e6872017-07-02 21:33:28 -07002955 num_apps = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002956 }
2957
2958 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002959 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07002960 p_card_status->card_state = card_state;
2961 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
kun.tang7bb00222017-07-12 11:41:43 +08002962 p_card_status->gsm_umts_subscription_app_index = -1;
2963 p_card_status->cdma_subscription_app_index = -1;
2964 p_card_status->ims_subscription_app_index = -1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002965 p_card_status->num_applications = num_apps;
2966
2967 // Initialize application status
2968 int i;
2969 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07002970 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002971 }
2972
2973 // Pickup the appropriate application status
2974 // that reflects sim_status for gsm.
2975 if (num_apps != 0) {
bohu076e6872017-07-02 21:33:28 -07002976 p_card_status->num_applications = 3;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002977 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002978 p_card_status->cdma_subscription_app_index = 1;
bohu076e6872017-07-02 21:33:28 -07002979 p_card_status->ims_subscription_app_index = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002980
2981 // Get the correct app status
2982 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002983 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
bohu076e6872017-07-02 21:33:28 -07002984 p_card_status->applications[2] = app_status_array[sim_status + ISIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002985 }
2986
2987 *pp_card_status = p_card_status;
2988 return RIL_E_SUCCESS;
2989}
2990
2991/**
2992 * Free the card status returned by getCardStatus
2993 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002994static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002995 free(p_card_status);
2996}
2997
2998/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002999 * SIM ready means any commands that access the SIM will work, including:
3000 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
3001 * (all SMS-related commands)
3002 */
3003
Mark Salyzynba58c202014-03-12 15:20:22 -07003004static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003005{
3006 ATResponse *p_response;
3007 int ret;
3008
Naveen Kalla2baf7232016-10-11 13:49:20 -07003009 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003010 // no longer valid to poll
3011 return;
3012 }
3013
3014 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07003015 case SIM_ABSENT:
3016 case SIM_PIN:
3017 case SIM_PUK:
3018 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003019 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003020 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003021 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003022 return;
3023
John Wang309ac292009-07-30 14:53:23 -07003024 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003025 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
3026 return;
3027
John Wang309ac292009-07-30 14:53:23 -07003028 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08003029 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003030 onSIMReady();
3031 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003032 return;
3033 }
3034}
3035
3036/** returns 1 if on, 0 if off, and -1 on error */
3037static int isRadioOn()
3038{
3039 ATResponse *p_response = NULL;
3040 int err;
3041 char *line;
3042 char ret;
3043
3044 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
3045
3046 if (err < 0 || p_response->success == 0) {
3047 // assume radio is off
3048 goto error;
3049 }
3050
3051 line = p_response->p_intermediates->line;
3052
3053 err = at_tok_start(&line);
3054 if (err < 0) goto error;
3055
3056 err = at_tok_nextbool(&line, &ret);
3057 if (err < 0) goto error;
3058
3059 at_response_free(p_response);
3060
3061 return (int)ret;
3062
3063error:
3064
3065 at_response_free(p_response);
3066 return -1;
3067}
3068
3069/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003070 * Parse the response generated by a +CTEC AT command
3071 * The values read from the response are stored in current and preferred.
3072 * Both current and preferred may be null. The corresponding value is ignored in that case.
3073 *
3074 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
3075 * 1 if the response includes the current technology only
3076 * 0 if the response includes both current technology and preferred mode
3077 */
3078int parse_technology_response( const char *response, int *current, int32_t *preferred )
3079{
3080 int err;
3081 char *line, *p;
3082 int ct;
3083 int32_t pt = 0;
3084 char *str_pt;
3085
3086 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08003087 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003088 err = at_tok_start(&p);
3089 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003090 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003091 free(line);
3092 return -1;
3093 }
3094
3095 err = at_tok_nextint(&p, &ct);
3096 if (err) {
3097 free(line);
3098 return -1;
3099 }
3100 if (current) *current = ct;
3101
Wink Saville4dcab4f2012-11-19 16:05:13 -08003102 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003103
3104 err = at_tok_nexthexint(&p, &pt);
3105 if (err) {
3106 free(line);
3107 return 1;
3108 }
3109 if (preferred) {
3110 *preferred = pt;
3111 }
3112 free(line);
3113
3114 return 0;
3115}
3116
Mark Salyzynba58c202014-03-12 15:20:22 -07003117int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003118{
3119 ATResponse *p_response;
3120 int err, val, techs = 0;
3121 char *tok;
3122 char *line;
3123
Wink Saville4dcab4f2012-11-19 16:05:13 -08003124 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003125 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3126 if (err || !p_response->success)
3127 goto error;
3128 line = p_response->p_intermediates->line;
3129 err = at_tok_start(&line);
3130 if (err || !at_tok_hasmore(&line))
3131 goto error;
3132 while (!at_tok_nextint(&line, &val)) {
3133 techs |= ( 1 << val );
3134 }
3135 if (supported) *supported = techs;
3136 return 0;
3137error:
3138 at_response_free(p_response);
3139 return -1;
3140}
3141
3142/**
3143 * query_ctec. Send the +CTEC AT command to the modem to query the current
3144 * and preferred modes. It leaves values in the addresses pointed to by
3145 * current and preferred. If any of those pointers are NULL, the corresponding value
3146 * is ignored, but the return value will still reflect if retreiving and parsing of the
3147 * values suceeded.
3148 *
3149 * @mdm Currently unused
3150 * @current A pointer to store the current mode returned by the modem. May be null.
3151 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3152 * @return -1 on error (or failure to parse)
3153 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3154 * 0 if both current and preferred were returned correctly
3155 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003156int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003157{
3158 ATResponse *response = NULL;
3159 int err;
3160 int res;
3161
Colin Cross5cba4882014-02-05 18:55:42 -08003162 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003163 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3164 if (!err && response->success) {
3165 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3166 at_response_free(response);
3167 return res;
3168 }
Colin Cross5cba4882014-02-05 18:55:42 -08003169 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003170 at_response_free(response);
3171 return -1;
3172}
3173
3174int is_multimode_modem(ModemInfo *mdm)
3175{
3176 ATResponse *response;
3177 int err;
3178 char *line;
3179 int tech;
3180 int32_t preferred;
3181
3182 if (query_ctec(mdm, &tech, &preferred) == 0) {
3183 mdm->currentTech = tech;
3184 mdm->preferredNetworkMode = preferred;
3185 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3186 return 0;
3187 }
3188 return 1;
3189 }
3190 return 0;
3191}
3192
3193/**
3194 * Find out if our modem is GSM, CDMA or both (Multimode)
3195 */
3196static void probeForModemMode(ModemInfo *info)
3197{
3198 ATResponse *response;
3199 int err;
3200 assert (info);
3201 // Currently, our only known multimode modem is qemu's android modem,
3202 // which implements the AT+CTEC command to query and set mode.
3203 // Try that first
3204
3205 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003206 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003207 info->supportedTechs, info->currentTech);
3208 return;
3209 }
3210
3211 /* Being here means that our modem is not multimode */
3212 info->isMultimode = 0;
3213
3214 /* CDMA Modems implement the AT+WNAM command */
3215 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3216 if (!err && response->success) {
3217 at_response_free(response);
3218 // TODO: find out if we really support EvDo
3219 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3220 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003221 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003222 return;
3223 }
3224 if (!err) at_response_free(response);
3225 // TODO: find out if modem really supports WCDMA/LTE
3226 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3227 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003228 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003229}
3230
3231/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003232 * Initialize everything that can be configured while we're still in
3233 * AT+CFUN=0
3234 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003235static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003236{
3237 ATResponse *p_response = NULL;
3238 int err;
3239
3240 setRadioState (RADIO_STATE_OFF);
3241
3242 at_handshake();
3243
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003244 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003245 /* note: we don't check errors here. Everything important will
3246 be handled in onATTimeout and onATReaderClosed */
3247
3248 /* atchannel is tolerant of echo but it must */
3249 /* have verbose result codes */
3250 at_send_command("ATE0Q0V1", NULL);
3251
3252 /* No auto-answer */
3253 at_send_command("ATS0=0", NULL);
3254
3255 /* Extended errors */
3256 at_send_command("AT+CMEE=1", NULL);
3257
3258 /* Network registration events */
3259 err = at_send_command("AT+CREG=2", &p_response);
3260
3261 /* some handsets -- in tethered mode -- don't support CREG=2 */
3262 if (err < 0 || p_response->success == 0) {
3263 at_send_command("AT+CREG=1", NULL);
3264 }
3265
3266 at_response_free(p_response);
3267
3268 /* GPRS registration events */
3269 at_send_command("AT+CGREG=1", NULL);
3270
3271 /* Call Waiting notifications */
3272 at_send_command("AT+CCWA=1", NULL);
3273
3274 /* Alternating voice/data off */
3275 at_send_command("AT+CMOD=0", NULL);
3276
3277 /* Not muted */
3278 at_send_command("AT+CMUT=0", NULL);
3279
3280 /* +CSSU unsolicited supp service notifications */
3281 at_send_command("AT+CSSN=0,1", NULL);
3282
3283 /* no connected line identification */
3284 at_send_command("AT+COLP=0", NULL);
3285
3286 /* HEX character set */
3287 at_send_command("AT+CSCS=\"HEX\"", NULL);
3288
3289 /* USSD unsolicited */
3290 at_send_command("AT+CUSD=1", NULL);
3291
3292 /* Enable +CGEV GPRS event notifications, but don't buffer */
3293 at_send_command("AT+CGEREP=1,0", NULL);
3294
3295 /* SMS PDU mode */
3296 at_send_command("AT+CMGF=0", NULL);
3297
3298#ifdef USE_TI_COMMANDS
3299
3300 at_send_command("AT%CPI=3", NULL);
3301
3302 /* TI specific -- notifications when SMS is ready (currently ignored) */
3303 at_send_command("AT%CSTAT=1", NULL);
3304
3305#endif /* USE_TI_COMMANDS */
3306
3307
3308 /* assume radio is off on error */
3309 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003310 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003311 }
3312}
3313
3314static void waitForClose()
3315{
3316 pthread_mutex_lock(&s_state_mutex);
3317
3318 while (s_closed == 0) {
3319 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3320 }
3321
3322 pthread_mutex_unlock(&s_state_mutex);
3323}
3324
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003325static void sendUnsolImsNetworkStateChanged()
3326{
3327#if 0 // to be used when unsol is changed to return data.
3328 int reply[2];
3329 reply[0] = s_ims_registered;
3330 reply[1] = s_ims_services;
3331 reply[1] = s_ims_format;
3332#endif
3333 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3334 NULL, 0);
3335}
3336
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003337/**
3338 * Called by atchannel when an unsolicited line appears
3339 * This is called on atchannel's reader thread. AT commands may
3340 * not be issued here
3341 */
3342static void onUnsolicited (const char *s, const char *sms_pdu)
3343{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003344 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003345 int err;
3346
3347 /* Ignore unsolicited responses until we're initialized.
3348 * This is OK because the RIL library will poll for initial state
3349 */
3350 if (sState == RADIO_STATE_UNAVAILABLE) {
3351 return;
3352 }
3353
3354 if (strStartsWith(s, "%CTZV:")) {
3355 /* TI specific -- NITZ time */
3356 char *response;
3357
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003358 line = p = strdup(s);
3359 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003360
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003361 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003362
3363 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003364 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003365 } else {
3366 RIL_onUnsolicitedResponse (
3367 RIL_UNSOL_NITZ_TIME_RECEIVED,
3368 response, strlen(response));
3369 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003370 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003371 } else if (strStartsWith(s,"+CRING:")
3372 || strStartsWith(s,"RING")
3373 || strStartsWith(s,"NO CARRIER")
3374 || strStartsWith(s,"+CCWA")
3375 ) {
3376 RIL_onUnsolicitedResponse (
3377 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3378 NULL, 0);
3379#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003380 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003381#endif /* WORKAROUND_FAKE_CGEV */
3382 } else if (strStartsWith(s,"+CREG:")
3383 || strStartsWith(s,"+CGREG:")
3384 ) {
3385 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003386 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003387 NULL, 0);
3388#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003389 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003390#endif /* WORKAROUND_FAKE_CGEV */
3391 } else if (strStartsWith(s, "+CMT:")) {
3392 RIL_onUnsolicitedResponse (
3393 RIL_UNSOL_RESPONSE_NEW_SMS,
3394 sms_pdu, strlen(sms_pdu));
3395 } else if (strStartsWith(s, "+CDS:")) {
3396 RIL_onUnsolicitedResponse (
3397 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3398 sms_pdu, strlen(sms_pdu));
3399 } else if (strStartsWith(s, "+CGEV:")) {
3400 /* Really, we can ignore NW CLASS and ME CLASS events here,
3401 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003402 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003403 */
3404 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003405 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003406#ifdef WORKAROUND_FAKE_CGEV
3407 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003408 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003409#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003410 } else if (strStartsWith(s, "+CTEC: ")) {
3411 int tech, mask;
3412 switch (parse_technology_response(s, &tech, NULL))
3413 {
3414 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003415 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003416 break;
3417 case 1: // current mode correctly parsed
3418 case 0: // preferred mode correctly parsed
3419 mask = 1 << tech;
3420 if (mask != MDM_GSM && mask != MDM_CDMA &&
3421 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003422 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003423 } else {
3424 setRadioTechnology(sMdmInfo, tech);
3425 }
3426 break;
3427 }
3428 } else if (strStartsWith(s, "+CCSS: ")) {
3429 int source = 0;
3430 line = p = strdup(s);
3431 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003432 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003433 return;
3434 }
3435 if (at_tok_start(&p) < 0) {
3436 free(line);
3437 return;
3438 }
3439 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003440 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003441 free(line);
3442 return;
3443 }
3444 SSOURCE(sMdmInfo) = source;
3445 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3446 &source, sizeof(source));
3447 } else if (strStartsWith(s, "+WSOS: ")) {
3448 char state = 0;
3449 int unsol;
3450 line = p = strdup(s);
3451 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003452 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003453 return;
3454 }
3455 if (at_tok_start(&p) < 0) {
3456 free(line);
3457 return;
3458 }
3459 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003460 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003461 free(line);
3462 return;
3463 }
3464 free(line);
3465
3466 unsol = state ?
3467 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3468
3469 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3470
3471 } else if (strStartsWith(s, "+WPRL: ")) {
3472 int version = -1;
3473 line = p = strdup(s);
3474 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003475 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003476 return;
3477 }
3478 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003479 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003480 free(line);
3481 return;
3482 }
3483 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003484 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003485 free(line);
3486 return;
3487 }
3488 free(line);
3489 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3490 } else if (strStartsWith(s, "+CFUN: 0")) {
3491 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003492 }
3493}
3494
3495/* Called on command or reader thread */
3496static void onATReaderClosed()
3497{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003498 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003499 at_close();
3500 s_closed = 1;
3501
3502 setRadioState (RADIO_STATE_UNAVAILABLE);
3503}
3504
3505/* Called on command thread */
3506static void onATTimeout()
3507{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003508 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003509 at_close();
3510
3511 s_closed = 1;
3512
3513 /* FIXME cause a radio reset here */
3514
3515 setRadioState (RADIO_STATE_UNAVAILABLE);
3516}
3517
Etan Cohend3652192014-06-20 08:28:44 -07003518/* Called to pass hardware configuration information to telephony
3519 * framework.
3520 */
3521static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3522{
3523 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3524}
3525
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003526static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003527{
3528#ifdef RIL_SHLIB
3529 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3530#else
3531 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3532 exit(-1);
3533#endif
3534}
3535
3536static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003537mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003538{
3539 int fd;
3540 int ret;
3541
3542 AT_DUMP("== ", "entering mainLoop()", -1 );
3543 at_set_on_reader_closed(onATReaderClosed);
3544 at_set_on_timeout(onATTimeout);
3545
3546 for (;;) {
3547 fd = -1;
3548 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003549 if (isInEmulator()) {
3550 fd = qemu_pipe_open("pipe:qemud:gsm");
3551 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003552 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003553 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003554 fd = socket_local_client(s_device_path,
3555 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3556 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003557 } else if (s_device_path != NULL) {
3558 fd = open (s_device_path, O_RDWR);
3559 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3560 /* disable echo on serial ports */
3561 struct termios ios;
3562 tcgetattr( fd, &ios );
3563 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3564 tcsetattr( fd, TCSANOW, &ios );
3565 }
3566 }
3567
3568 if (fd < 0) {
3569 perror ("opening AT interface. retrying...");
3570 sleep(10);
3571 /* never returns */
3572 }
3573 }
3574
3575 s_closed = 0;
3576 ret = at_open(fd, onUnsolicited);
3577
3578 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003579 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003580 return 0;
3581 }
3582
3583 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3584
3585 // Give initializeCallback a chance to dispatched, since
3586 // we don't presently have a cancellation mechanism
3587 sleep(1);
3588
3589 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003590 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003591 }
3592}
3593
3594#ifdef RIL_SHLIB
3595
3596pthread_t s_tid_mainloop;
3597
3598const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3599{
3600 int ret;
3601 int fd = -1;
3602 int opt;
3603 pthread_attr_t attr;
3604
3605 s_rilenv = env;
3606
Sandeep Gutta11f27942014-07-10 05:00:25 +05303607 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003608 switch (opt) {
3609 case 'p':
3610 s_port = atoi(optarg);
3611 if (s_port == 0) {
3612 usage(argv[0]);
3613 return NULL;
3614 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003615 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003616 break;
3617
3618 case 'd':
3619 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003620 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003621 break;
3622
3623 case 's':
3624 s_device_path = optarg;
3625 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003626 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003627 break;
3628
Sandeep Gutta11f27942014-07-10 05:00:25 +05303629 case 'c':
3630 RLOGI("Client id received %s\n", optarg);
3631 break;
3632
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003633 default:
3634 usage(argv[0]);
3635 return NULL;
3636 }
3637 }
3638
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003639 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003640 usage(argv[0]);
3641 return NULL;
3642 }
3643
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003644 sMdmInfo = calloc(1, sizeof(ModemInfo));
3645 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003646 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003647 return NULL;
3648 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003649 pthread_attr_init (&attr);
3650 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3651 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3652
3653 return &s_callbacks;
3654}
3655#else /* RIL_SHLIB */
3656int main (int argc, char **argv)
3657{
3658 int ret;
3659 int fd = -1;
3660 int opt;
3661
3662 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3663 switch (opt) {
3664 case 'p':
3665 s_port = atoi(optarg);
3666 if (s_port == 0) {
3667 usage(argv[0]);
3668 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003669 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003670 break;
3671
3672 case 'd':
3673 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003674 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003675 break;
3676
3677 case 's':
3678 s_device_path = optarg;
3679 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003680 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003681 break;
3682
3683 default:
3684 usage(argv[0]);
3685 }
3686 }
3687
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003688 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003689 usage(argv[0]);
3690 }
3691
3692 RIL_register(&s_callbacks);
3693
3694 mainLoop(NULL);
3695
3696 return 0;
3697}
3698
3699#endif /* RIL_SHLIB */