blob: f451fb01c233222a7bf3e1c991116f43466628f7 [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,
168 RUIM_NETWORK_PERSONALIZATION = 11
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100169} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -0700170
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800171static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
172static RIL_RadioState currentState();
173static int onSupports (int requestCode);
174static void onCancel (RIL_Token t);
175static const char *getVersion();
176static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -0700177static SIM_Status getSIMStatus();
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700178static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
179static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -0700180static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800181
182extern const char * requestToString(int request);
183
184/*** Static Variables ***/
185static const RIL_RadioFunctions s_callbacks = {
186 RIL_VERSION,
187 onRequest,
188 currentState,
189 onSupports,
190 onCancel,
191 getVersion
192};
193
194#ifdef RIL_SHLIB
195static const struct RIL_Env *s_rilenv;
196
197#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
198#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
199#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
200#endif
201
202static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
203
204static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
205static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
206
207static int s_port = -1;
208static const char * s_device_path = NULL;
209static int s_device_socket = 0;
210
211/* trigger change to this with s_state_cond */
212static int s_closed = 0;
213
214static int sFD; /* file desc of AT channel */
215static char sATBuffer[MAX_AT_RESPONSE+1];
216static char *sATBufferCur = NULL;
217
218static const struct timeval TIMEVAL_SIMPOLL = {1,0};
219static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
220static const struct timeval TIMEVAL_0 = {0,0};
221
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -0700222static int s_ims_registered = 0; // 0==unregistered
223static int s_ims_services = 1; // & 0x1 == sms over ims supported
224static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
225static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
226static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
227static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
228static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
229
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800230#ifdef WORKAROUND_ERRONEOUS_ANSWER
231// Max number of times we'll try to repoll when we think
232// we have a AT+CLCC race condition
233#define REPOLL_CALLS_COUNT_MAX 4
234
235// Line index that was incoming or waiting at last poll, or -1 for none
236static int s_incomingOrWaitingLine = -1;
237// Number of times we've asked for a repoll of AT+CLCC
238static int s_repollCallsCount = 0;
239// Should we expect a call to be answered in the next CLCC?
240static int s_expectAnswer = 0;
241#endif /* WORKAROUND_ERRONEOUS_ANSWER */
242
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200243
Wink Saville8a9e0212013-04-09 12:11:38 -0700244static int s_cell_info_rate_ms = INT_MAX;
245static int s_mcc = 0;
246static int s_mnc = 0;
247static int s_lac = 0;
248static int s_cid = 0;
249
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800250static void pollSIMState (void *param);
251static void setRadioState(RIL_RadioState newState);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700252static void setRadioTechnology(ModemInfo *mdm, int newtech);
253static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
254static int parse_technology_response(const char *response, int *current, int32_t *preferred);
255static int techFromModemType(int mdmtype);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800256
257static int clccStateToRILState(int state, RIL_CallState *p_state)
258
259{
260 switch(state) {
261 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
262 case 1: *p_state = RIL_CALL_HOLDING; return 0;
263 case 2: *p_state = RIL_CALL_DIALING; return 0;
264 case 3: *p_state = RIL_CALL_ALERTING; return 0;
265 case 4: *p_state = RIL_CALL_INCOMING; return 0;
266 case 5: *p_state = RIL_CALL_WAITING; return 0;
267 default: return -1;
268 }
269}
270
271/**
272 * Note: directly modified line and has *p_call point directly into
273 * modified line
274 */
Wink Saville3d54e742009-05-18 18:00:44 -0700275static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800276{
277 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
278 // index,isMT,state,mode,isMpty(,number,TOA)?
279
280 int err;
281 int state;
282 int mode;
283
284 err = at_tok_start(&line);
285 if (err < 0) goto error;
286
287 err = at_tok_nextint(&line, &(p_call->index));
288 if (err < 0) goto error;
289
290 err = at_tok_nextbool(&line, &(p_call->isMT));
291 if (err < 0) goto error;
292
293 err = at_tok_nextint(&line, &state);
294 if (err < 0) goto error;
295
296 err = clccStateToRILState(state, &(p_call->state));
297 if (err < 0) goto error;
298
299 err = at_tok_nextint(&line, &mode);
300 if (err < 0) goto error;
301
302 p_call->isVoice = (mode == 0);
303
304 err = at_tok_nextbool(&line, &(p_call->isMpty));
305 if (err < 0) goto error;
306
307 if (at_tok_hasmore(&line)) {
308 err = at_tok_nextstr(&line, &(p_call->number));
309
310 /* tolerate null here */
311 if (err < 0) return 0;
312
313 // Some lame implementations return strings
314 // like "NOT AVAILABLE" in the CLCC line
315 if (p_call->number != NULL
316 && 0 == strspn(p_call->number, "+0123456789")
317 ) {
318 p_call->number = NULL;
319 }
320
321 err = at_tok_nextint(&line, &p_call->toa);
322 if (err < 0) goto error;
323 }
324
Wink Saville74fa3882009-12-22 15:35:41 -0800325 p_call->uusInfo = NULL;
326
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800327 return 0;
328
329error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800330 RLOGE("invalid CLCC line\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800331 return -1;
332}
333
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -0700334static int parseSimResponseLine(char* line, RIL_SIM_IO_Response* response) {
335 int err;
336
337 err = at_tok_start(&line);
338 if (err < 0) return err;
339 err = at_tok_nextint(&line, &response->sw1);
340 if (err < 0) return err;
341 err = at_tok_nextint(&line, &response->sw2);
342 if (err < 0) return err;
343
344 if (at_tok_hasmore(&line)) {
345 err = at_tok_nextstr(&line, &response->simResponse);
346 if (err < 0) return err;
347 }
348 return 0;
349}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800350
351/** do post-AT+CFUN=1 initialization */
352static void onRadioPowerOn()
353{
354#ifdef USE_TI_COMMANDS
355 /* Must be after CFUN=1 */
356 /* TI specific -- notifications for CPHS things such */
357 /* as CPHS message waiting indicator */
358
359 at_send_command("AT%CPHS=1", NULL);
360
361 /* TI specific -- enable NITZ unsol notifs */
362 at_send_command("AT%CTZV=1", NULL);
363#endif
364
365 pollSIMState(NULL);
366}
367
368/** do post- SIM ready initialization */
369static void onSIMReady()
370{
371 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
372 /*
373 * Always send SMS messages directly to the TE
374 *
375 * mode = 1 // discard when link is reserved (link should never be
376 * reserved)
377 * mt = 2 // most messages routed to TE
378 * bm = 2 // new cell BM's routed to TE
379 * ds = 1 // Status reports routed to TE
380 * bfr = 1 // flush buffer
381 */
382 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
383}
384
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700385static void requestRadioPower(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800386{
387 int onOff;
388
389 int err;
390 ATResponse *p_response = NULL;
391
392 assert (datalen >= sizeof(int *));
393 onOff = ((int *)data)[0];
394
395 if (onOff == 0 && sState != RADIO_STATE_OFF) {
396 err = at_send_command("AT+CFUN=0", &p_response);
397 if (err < 0 || p_response->success == 0) goto error;
398 setRadioState(RADIO_STATE_OFF);
399 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
400 err = at_send_command("AT+CFUN=1", &p_response);
401 if (err < 0|| p_response->success == 0) {
402 // Some stacks return an error when there is no SIM,
403 // but they really turn the RF portion on
404 // So, if we get an error, let's check to see if it
405 // turned on anyway
406
407 if (isRadioOn() != 1) {
408 goto error;
409 }
410 }
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700411 setRadioState(RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800412 }
413
414 at_response_free(p_response);
415 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
416 return;
417error:
418 at_response_free(p_response);
419 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
420}
421
Naveen Kallaa65a16a2014-07-31 16:48:31 -0700422static void requestShutdown(RIL_Token t)
423{
424 int onOff;
425
426 int err;
427 ATResponse *p_response = NULL;
428
429 if (sState != RADIO_STATE_OFF) {
430 err = at_send_command("AT+CFUN=0", &p_response);
431 setRadioState(RADIO_STATE_UNAVAILABLE);
432 }
433
434 at_response_free(p_response);
435 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
436 return;
437}
438
Wink Savillef4c4d362009-04-02 01:37:03 -0700439static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800440
Mark Salyzynba58c202014-03-12 15:20:22 -0700441static void onDataCallListChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800442{
Wink Savillef4c4d362009-04-02 01:37:03 -0700443 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800444}
445
Mark Salyzynba58c202014-03-12 15:20:22 -0700446static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800447{
Wink Savillef4c4d362009-04-02 01:37:03 -0700448 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800449}
450
Wink Savillef4c4d362009-04-02 01:37:03 -0700451static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800452{
453 ATResponse *p_response;
454 ATLine *p_cur;
455 int err;
456 int n = 0;
457 char *out;
458
459 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
460 if (err != 0 || p_response->success == 0) {
461 if (t != NULL)
462 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
463 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700464 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800465 NULL, 0);
466 return;
467 }
468
469 for (p_cur = p_response->p_intermediates; p_cur != NULL;
470 p_cur = p_cur->p_next)
471 n++;
472
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700473 RIL_Data_Call_Response_v11 *responses =
474 alloca(n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800475
476 int i;
477 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800478 responses[i].status = -1;
Wink Saville250eb3c2011-06-22 09:11:34 -0700479 responses[i].suggestedRetryTime = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800480 responses[i].cid = -1;
481 responses[i].active = -1;
482 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800483 responses[i].ifname = "";
484 responses[i].addresses = "";
485 responses[i].dnses = "";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700486 responses[i].gateways = "";
Etan Cohend3652192014-06-20 08:28:44 -0700487 responses[i].pcscf = "";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700488 responses[i].mtu = 0;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800489 }
490
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700491 RIL_Data_Call_Response_v11 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800492 for (p_cur = p_response->p_intermediates; p_cur != NULL;
493 p_cur = p_cur->p_next) {
494 char *line = p_cur->line;
495
496 err = at_tok_start(&line);
497 if (err < 0)
498 goto error;
499
500 err = at_tok_nextint(&line, &response->cid);
501 if (err < 0)
502 goto error;
503
504 err = at_tok_nextint(&line, &response->active);
505 if (err < 0)
506 goto error;
507
508 response++;
509 }
510
511 at_response_free(p_response);
512
513 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
514 if (err != 0 || p_response->success == 0) {
515 if (t != NULL)
516 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
517 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700518 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800519 NULL, 0);
520 return;
521 }
522
523 for (p_cur = p_response->p_intermediates; p_cur != NULL;
524 p_cur = p_cur->p_next) {
525 char *line = p_cur->line;
526 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800527
528 err = at_tok_start(&line);
529 if (err < 0)
530 goto error;
531
532 err = at_tok_nextint(&line, &cid);
533 if (err < 0)
534 goto error;
535
536 for (i = 0; i < n; i++) {
537 if (responses[i].cid == cid)
538 break;
539 }
540
541 if (i >= n) {
542 /* details for a context we didn't hear about in the last request */
543 continue;
544 }
545
Wink Saville43808972011-01-13 17:39:51 -0800546 // Assume no error
547 responses[i].status = 0;
548
549 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800550 err = at_tok_nextstr(&line, &out);
551 if (err < 0)
552 goto error;
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700553
554 int type_size = strlen(out) + 1;
555 responses[i].type = alloca(type_size);
556 strlcpy(responses[i].type, out, type_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800557
Wink Saville43808972011-01-13 17:39:51 -0800558 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800559 err = at_tok_nextstr(&line, &out);
560 if (err < 0)
561 goto error;
562
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700563 int ifname_size = strlen(PPP_TTY_PATH) + 1;
564 responses[i].ifname = alloca(ifname_size);
565 strlcpy(responses[i].ifname, PPP_TTY_PATH, ifname_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800566
567 err = at_tok_nextstr(&line, &out);
568 if (err < 0)
569 goto error;
570
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700571 int addresses_size = strlen(out) + 1;
572 responses[i].addresses = alloca(addresses_size);
573 strlcpy(responses[i].addresses, out, addresses_size);
Wink Saville43808972011-01-13 17:39:51 -0800574
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200575 if (isInEmulator()) {
576 /* We are in the emulator - the dns servers are listed
577 * by the following system properties, setup in
578 * /system/etc/init.goldfish.sh:
579 * - net.eth0.dns1
580 * - net.eth0.dns2
581 * - net.eth0.dns3
582 * - net.eth0.dns4
583 */
584 const int dnslist_sz = 128;
585 char* dnslist = alloca(dnslist_sz);
586 const char* separator = "";
587 int nn;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100588
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200589 dnslist[0] = 0;
590 for (nn = 1; nn <= 4; nn++) {
591 /* Probe net.eth0.dns<n> */
592 char propName[PROP_NAME_MAX];
593 char propValue[PROP_VALUE_MAX];
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100594
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200595 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100596
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200597 /* Ignore if undefined */
598 if (__system_property_get(propName, propValue) == 0) {
599 continue;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100600 }
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700601
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200602 /* Append the DNS IP address */
603 strlcat(dnslist, separator, dnslist_sz);
604 strlcat(dnslist, propValue, dnslist_sz);
605 separator = " ";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100606 }
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200607 responses[i].dnses = dnslist;
608
Tao Wu0c5ad9f2017-05-22 14:06:06 -0700609 responses[i].gateways = "10.0.2.2 fe80::2";
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200610 responses[i].mtu = DEFAULT_MTU;
611 }
612 else {
613 /* I don't know where we are, so use the public Google DNS
614 * servers by default and no gateway.
615 */
616 responses[i].dnses = "8.8.8.8 8.8.4.4";
617 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100618 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800619 }
620
621 at_response_free(p_response);
622
623 if (t != NULL)
624 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700625 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800626 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700627 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800628 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700629 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800630
631 return;
632
633error:
634 if (t != NULL)
635 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
636 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700637 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800638 NULL, 0);
639
640 at_response_free(p_response);
641}
642
643static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700644 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800645{
646 int err;
647 ATResponse *p_response = NULL;
648 int response = 0;
649 char *line;
650
651 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
652
653 if (err < 0 || p_response->success == 0) {
654 goto error;
655 }
656
657 line = p_response->p_intermediates->line;
658
659 err = at_tok_start(&line);
660
661 if (err < 0) {
662 goto error;
663 }
664
665 err = at_tok_nextint(&line, &response);
666
667 if (err < 0) {
668 goto error;
669 }
670
671 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
672 at_response_free(p_response);
673 return;
674error:
675 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800676 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800677 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
678}
679
Mark Salyzynba58c202014-03-12 15:20:22 -0700680static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800681{
682 RIL_onUnsolicitedResponse (
683 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
684 NULL, 0);
685}
686
Mark Salyzynba58c202014-03-12 15:20:22 -0700687static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800688{
689 int err;
690 ATResponse *p_response;
691 ATLine *p_cur;
692 int countCalls;
693 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700694 RIL_Call *p_calls;
695 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800696 int i;
697 int needRepoll = 0;
698
699#ifdef WORKAROUND_ERRONEOUS_ANSWER
700 int prevIncomingOrWaitingLine;
701
702 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
703 s_incomingOrWaitingLine = -1;
704#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
705
706 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
707
708 if (err != 0 || p_response->success == 0) {
709 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
710 return;
711 }
712
713 /* count the calls */
714 for (countCalls = 0, p_cur = p_response->p_intermediates
715 ; p_cur != NULL
716 ; p_cur = p_cur->p_next
717 ) {
718 countCalls++;
719 }
720
721 /* yes, there's an array of pointers and then an array of structures */
722
Wink Saville3d54e742009-05-18 18:00:44 -0700723 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
724 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
725 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800726
727 /* init the pointer array */
728 for(i = 0; i < countCalls ; i++) {
729 pp_calls[i] = &(p_calls[i]);
730 }
731
732 for (countValidCalls = 0, p_cur = p_response->p_intermediates
733 ; p_cur != NULL
734 ; p_cur = p_cur->p_next
735 ) {
736 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
737
738 if (err != 0) {
739 continue;
740 }
741
742#ifdef WORKAROUND_ERRONEOUS_ANSWER
743 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
744 || p_calls[countValidCalls].state == RIL_CALL_WAITING
745 ) {
746 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
747 }
748#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
749
750 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
751 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
752 ) {
753 needRepoll = 1;
754 }
755
756 countValidCalls++;
757 }
758
759#ifdef WORKAROUND_ERRONEOUS_ANSWER
760 // Basically:
761 // A call was incoming or waiting
762 // Now it's marked as active
763 // But we never answered it
764 //
765 // This is probably a bug, and the call will probably
766 // disappear from the call list in the next poll
767 if (prevIncomingOrWaitingLine >= 0
768 && s_incomingOrWaitingLine < 0
769 && s_expectAnswer == 0
770 ) {
771 for (i = 0; i < countValidCalls ; i++) {
772
773 if (p_calls[i].index == prevIncomingOrWaitingLine
774 && p_calls[i].state == RIL_CALL_ACTIVE
775 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
776 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800777 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800778 "Hit WORKAROUND_ERRONOUS_ANSWER case."
779 " Repoll count: %d\n", s_repollCallsCount);
780 s_repollCallsCount++;
781 goto error;
782 }
783 }
784 }
785
786 s_expectAnswer = 0;
787 s_repollCallsCount = 0;
788#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
789
Wink Saville3d54e742009-05-18 18:00:44 -0700790 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
791 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800792
793 at_response_free(p_response);
794
795#ifdef POLL_CALL_STATE
796 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
797 // smd, so we're forced to poll until the call ends.
798#else
799 if (needRepoll) {
800#endif
801 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
802 }
803
804 return;
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700805#ifdef WORKAROUND_ERRONEOUS_ANSWER
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800806error:
807 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
808 at_response_free(p_response);
Tomasz Wasilczyk88961c22017-04-11 09:21:08 -0700809#endif
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800810}
811
Mark Salyzynba58c202014-03-12 15:20:22 -0700812static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800813{
814 RIL_Dial *p_dial;
815 char *cmd;
816 const char *clir;
817 int ret;
818
819 p_dial = (RIL_Dial *)data;
820
821 switch (p_dial->clir) {
822 case 1: clir = "I"; break; /*invocation*/
823 case 2: clir = "i"; break; /*suppression*/
824 default:
825 case 0: clir = ""; break; /*subscription default*/
826 }
827
828 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
829
830 ret = at_send_command(cmd, NULL);
831
832 free(cmd);
833
834 /* success or failure is ignored by the upper layer here.
835 it will call GET_CURRENT_CALLS and determine success that way */
836 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
837}
838
Mark Salyzynba58c202014-03-12 15:20:22 -0700839static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800840{
841 RIL_SMS_WriteArgs *p_args;
842 char *cmd;
843 int length;
844 int err;
845 ATResponse *p_response = NULL;
846
847 p_args = (RIL_SMS_WriteArgs *)data;
848
849 length = strlen(p_args->pdu)/2;
850 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
851
852 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
853
854 if (err != 0 || p_response->success == 0) goto error;
855
856 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
857 at_response_free(p_response);
858
859 return;
860error:
861 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
862 at_response_free(p_response);
863}
864
Mark Salyzynba58c202014-03-12 15:20:22 -0700865static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800866{
867 int *p_line;
868
869 int ret;
870 char *cmd;
871
872 p_line = (int *)data;
873
874 // 3GPP 22.030 6.5.5
875 // "Releases a specific active call X"
876 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
877
878 ret = at_send_command(cmd, NULL);
879
880 free(cmd);
881
882 /* success or failure is ignored by the upper layer here.
883 it will call GET_CURRENT_CALLS and determine success that way */
884 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
885}
886
Mark Salyzynba58c202014-03-12 15:20:22 -0700887static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800888{
889 ATResponse *p_response = NULL;
890 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800891 char *line;
Jim Kayedc9f31b2017-04-03 13:43:31 -0700892 int count = 0;
893 // Accept a response that is at least v6, and up to v10
894 int minNumOfElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
895 int maxNumOfElements=sizeof(RIL_SignalStrength_v10)/sizeof(int);
896 int response[maxNumOfElements];
897
898 memset(response, 0, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800899
900 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
901
902 if (err < 0 || p_response->success == 0) {
903 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
904 goto error;
905 }
906
907 line = p_response->p_intermediates->line;
908
909 err = at_tok_start(&line);
910 if (err < 0) goto error;
911
Jim Kayedc9f31b2017-04-03 13:43:31 -0700912 for (count = 0; count < maxNumOfElements; count++) {
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700913 err = at_tok_nextint(&line, &(response[count]));
Jim Kayedc9f31b2017-04-03 13:43:31 -0700914 if (err < 0 && count < minNumOfElements) goto error;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700915 }
Chih-Wei Huang28059052012-04-30 01:13:27 +0800916
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700917 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800918
919 at_response_free(p_response);
920 return;
921
922error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800923 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800924 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
925 at_response_free(p_response);
926}
927
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700928/**
929 * networkModePossible. Decides whether the network mode is appropriate for the
930 * specified modem
931 */
932static int networkModePossible(ModemInfo *mdm, int nm)
933{
934 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
935 return 1;
936 }
937 return 0;
938}
Mark Salyzynba58c202014-03-12 15:20:22 -0700939static void requestSetPreferredNetworkType( int request __unused, void *data,
940 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700941{
942 ATResponse *p_response = NULL;
943 char *cmd = NULL;
944 int value = *(int *)data;
945 int current, old;
946 int err;
947 int32_t preferred = net2pmask[value];
948
Wink Saville4dcab4f2012-11-19 16:05:13 -0800949 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700950 if (!networkModePossible(sMdmInfo, value)) {
951 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
952 return;
953 }
954 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
955 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
956 return;
957 }
958 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800959 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700960 if (old != preferred) {
961 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800962 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700963 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
964 free(cmd);
965 if (err || !p_response->success) {
966 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
967 return;
968 }
969 PREFERRED_NETWORK(sMdmInfo) = value;
970 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
971 int current;
972 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
973 switch (res) {
974 case -1: // Error or unable to parse
975 break;
976 case 1: // Only able to parse current
977 case 0: // Both current and preferred were parsed
978 setRadioTechnology(sMdmInfo, current);
979 break;
980 }
981 }
982 }
983 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
984}
985
Mark Salyzynba58c202014-03-12 15:20:22 -0700986static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
987 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700988{
989 int preferred;
990 unsigned i;
991
992 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
993 case -1: // Error or unable to parse
994 case 1: // Only able to parse current
995 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
996 break;
997 case 0: // Both current and preferred were parsed
998 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
999 if (preferred == net2pmask[i]) {
1000 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1001 return;
1002 }
1003 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001004 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001005 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1006 break;
1007 }
1008
1009}
1010
Mark Salyzynba58c202014-03-12 15:20:22 -07001011static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1012 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001013{
1014 int err;
1015 char * responseStr;
1016 ATResponse *p_response = NULL;
1017 const char *cmd;
1018 char *line;
1019
1020 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1021 if (err < 0 || !p_response->success) goto error;
1022 line = p_response->p_intermediates->line;
1023 err = at_tok_start(&line);
1024 if (err < 0) goto error;
1025 err = at_tok_nextstr(&line, &responseStr);
1026 if (err < 0 || !responseStr) goto error;
1027 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1028 at_response_free(p_response);
1029 return;
1030error:
1031 at_response_free(p_response);
1032 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1033}
1034
Mark Salyzynba58c202014-03-12 15:20:22 -07001035static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1036 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001037{
1038 int err;
1039 char * responseStr;
1040 ATResponse *p_response = NULL;
1041 const char *cmd;
1042 const char *prefix;
1043 char *line, *p;
1044 int commas;
1045 int skip;
1046 int count = 4;
1047
1048 // Fixed values. TODO: query modem
1049 responseStr = strdup("1.0.0.0");
1050 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1051 free(responseStr);
1052}
1053
Mark Salyzynba58c202014-03-12 15:20:22 -07001054static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1055 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001056{
1057 int err;
1058 int response[4];
1059 char * responseStr[4];
1060 ATResponse *p_response = NULL;
1061 const char *cmd;
1062 const char *prefix;
1063 char *line, *p;
1064 int commas;
1065 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001066 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001067
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001068 // Fixed values. TODO: Query modem
1069 responseStr[0] = "----";
1070 responseStr[1] = "----";
1071 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001072
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001073 err = at_send_command_numeric("AT+CGSN", &p_response);
1074 if (err < 0 || p_response->success == 0) {
1075 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1076 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001077 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001078 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001079 }
1080
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001081 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1082 at_response_free(p_response);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001083}
1084
Mark Salyzynba58c202014-03-12 15:20:22 -07001085static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1086 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001087{
1088 int err;
1089 int *ss = (int *)data;
1090 ATResponse *p_response = NULL;
1091 char *cmd = NULL;
1092 char *line = NULL;
1093 int response;
1094
1095 asprintf(&cmd, "AT+CCSS?");
1096 if (!cmd) goto error;
1097
1098 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1099 if (err < 0 || !p_response->success)
1100 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001101
1102 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001103 err = at_tok_start(&line);
1104 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001105
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001106 err = at_tok_nextint(&line, &response);
1107 free(cmd);
1108 cmd = NULL;
1109
1110 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1111
1112 return;
1113error:
1114 free(cmd);
1115 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1116}
1117
Mark Salyzynba58c202014-03-12 15:20:22 -07001118static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001119 size_t datalen, RIL_Token t)
1120{
1121 int err;
1122 int *ss = (int *)data;
1123 ATResponse *p_response = NULL;
1124 char *cmd = NULL;
1125
1126 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001127 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001128 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1129 return;
1130 }
1131 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1132 if (!cmd) goto error;
1133
1134 err = at_send_command(cmd, &p_response);
1135 if (err < 0 || !p_response->success)
1136 goto error;
1137 free(cmd);
1138 cmd = NULL;
1139
1140 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1141
1142 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1143
1144 return;
1145error:
1146 free(cmd);
1147 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1148}
1149
Mark Salyzynba58c202014-03-12 15:20:22 -07001150static void requestCdmaSubscription(int request __unused, void *data __unused,
1151 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001152{
1153 int err;
1154 int response[5];
1155 char * responseStr[5];
1156 ATResponse *p_response = NULL;
1157 const char *cmd;
1158 const char *prefix;
1159 char *line, *p;
1160 int commas;
1161 int skip;
1162 int count = 5;
1163
1164 // Fixed values. TODO: Query modem
1165 responseStr[0] = "8587777777"; // MDN
1166 responseStr[1] = "1"; // SID
1167 responseStr[2] = "1"; // NID
1168 responseStr[3] = "8587777777"; // MIN
1169 responseStr[4] = "1"; // PRL Version
1170 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001171}
1172
Mark Salyzynba58c202014-03-12 15:20:22 -07001173static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1174 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001175{
1176 int roaming_pref = -1;
1177 ATResponse *p_response = NULL;
1178 char *line;
1179 int res;
1180
1181 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1182 if (res < 0 || !p_response->success) {
1183 goto error;
1184 }
1185 line = p_response->p_intermediates->line;
1186
1187 res = at_tok_start(&line);
1188 if (res < 0) goto error;
1189
1190 res = at_tok_nextint(&line, &roaming_pref);
1191 if (res < 0) goto error;
1192
1193 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1194 return;
1195error:
1196 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1197}
1198
Mark Salyzynba58c202014-03-12 15:20:22 -07001199static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1200 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001201{
1202 int *pref = (int *)data;
1203 ATResponse *p_response = NULL;
1204 char *line;
1205 int res;
1206 char *cmd = NULL;
1207
1208 asprintf(&cmd, "AT+WRMP=%d", *pref);
1209 if (cmd == NULL) goto error;
1210
1211 res = at_send_command(cmd, &p_response);
1212 if (res < 0 || !p_response->success)
1213 goto error;
1214
1215 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1216 free(cmd);
1217 return;
1218error:
1219 free(cmd);
1220 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1221}
1222
1223static int parseRegistrationState(char *str, int *type, int *items, int **response)
1224{
1225 int err;
1226 char *line = str, *p;
1227 int *resp = NULL;
1228 int skip;
1229 int count = 3;
1230 int commas;
1231
Wink Saville4dcab4f2012-11-19 16:05:13 -08001232 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001233 err = at_tok_start(&line);
1234 if (err < 0) goto error;
1235
1236 /* Ok you have to be careful here
1237 * The solicited version of the CREG response is
1238 * +CREG: n, stat, [lac, cid]
1239 * and the unsolicited version is
1240 * +CREG: stat, [lac, cid]
1241 * The <n> parameter is basically "is unsolicited creg on?"
1242 * which it should always be
1243 *
1244 * Now we should normally get the solicited version here,
1245 * but the unsolicited version could have snuck in
1246 * so we have to handle both
1247 *
1248 * Also since the LAC and CID are only reported when registered,
1249 * we can have 1, 2, 3, or 4 arguments here
1250 *
1251 * finally, a +CGREG: answer may have a fifth value that corresponds
1252 * to the network type, as in;
1253 *
1254 * +CGREG: n, stat [,lac, cid [,networkType]]
1255 */
1256
1257 /* count number of commas */
1258 commas = 0;
1259 for (p = line ; *p != '\0' ;p++) {
1260 if (*p == ',') commas++;
1261 }
1262
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001263 resp = (int *)calloc(commas + 1, sizeof(int));
1264 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001265 switch (commas) {
1266 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001267 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001268 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001269 resp[1] = -1;
1270 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001271 break;
1272
1273 case 1: /* +CREG: <n>, <stat> */
1274 err = at_tok_nextint(&line, &skip);
1275 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001276 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001277 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001278 resp[1] = -1;
1279 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001280 if (err < 0) goto error;
1281 break;
1282
1283 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001284 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001285 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001286 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001287 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001288 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001289 if (err < 0) goto error;
1290 break;
1291 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1292 err = at_tok_nextint(&line, &skip);
1293 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001294 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001295 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001296 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001297 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001298 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001299 if (err < 0) goto error;
1300 break;
1301 /* special case for CGREG, there is a fourth parameter
1302 * that is the network type (unknown/gprs/edge/umts)
1303 */
1304 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1305 err = at_tok_nextint(&line, &skip);
1306 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001307 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001308 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001309 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001310 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001311 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001312 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001313 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001314 if (err < 0) goto error;
1315 count = 4;
1316 break;
1317 default:
1318 goto error;
1319 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001320 s_lac = resp[1];
1321 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001322 if (response)
1323 *response = resp;
1324 if (items)
1325 *items = commas + 1;
1326 if (type)
1327 *type = techFromModemType(TECH(sMdmInfo));
1328 return 0;
1329error:
1330 free(resp);
1331 return -1;
1332}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001333
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001334#define REG_STATE_LEN 15
1335#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001336static void requestRegistrationState(int request, void *data __unused,
1337 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001338{
1339 int err;
1340 int *registration;
1341 char **responseStr = NULL;
1342 ATResponse *p_response = NULL;
1343 const char *cmd;
1344 const char *prefix;
1345 char *line;
1346 int i = 0, j, numElements = 0;
1347 int count = 3;
1348 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001349
Wink Saville4dcab4f2012-11-19 16:05:13 -08001350 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001351 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1352 cmd = "AT+CREG?";
1353 prefix = "+CREG:";
1354 numElements = REG_STATE_LEN;
1355 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1356 cmd = "AT+CGREG?";
1357 prefix = "+CGREG:";
1358 numElements = REG_DATA_STATE_LEN;
1359 } else {
1360 assert(0);
1361 goto error;
1362 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001363
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001364 err = at_send_command_singleline(cmd, prefix, &p_response);
1365
1366 if (err != 0) goto error;
1367
1368 line = p_response->p_intermediates->line;
1369
1370 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1371
1372 responseStr = malloc(numElements * sizeof(char *));
1373 if (!responseStr) goto error;
1374 memset(responseStr, 0, numElements * sizeof(char *));
1375 /**
1376 * The first '4' bytes for both registration states remain the same.
1377 * But if the request is 'DATA_REGISTRATION_STATE',
1378 * the 5th and 6th byte(s) are optional.
1379 */
1380 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001381 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001382 // TODO: Query modem
1383 startfrom = 3;
1384 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1385 asprintf(&responseStr[3], "8"); // EvDo revA
1386 asprintf(&responseStr[4], "1"); // BSID
1387 asprintf(&responseStr[5], "123"); // Latitude
1388 asprintf(&responseStr[6], "222"); // Longitude
1389 asprintf(&responseStr[7], "0"); // CSS Indicator
1390 asprintf(&responseStr[8], "4"); // SID
1391 asprintf(&responseStr[9], "65535"); // NID
1392 asprintf(&responseStr[10], "0"); // Roaming indicator
1393 asprintf(&responseStr[11], "1"); // System is in PRL
1394 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1395 asprintf(&responseStr[13], "0"); // Reason for denial
1396 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1397 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1398 asprintf(&responseStr[3], "8"); // Available data radio technology
1399 }
1400 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001401 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001402 startfrom = 0;
1403 asprintf(&responseStr[1], "%x", registration[1]);
1404 asprintf(&responseStr[2], "%x", registration[2]);
1405 if (count > 3)
1406 asprintf(&responseStr[3], "%d", registration[3]);
1407 }
1408 asprintf(&responseStr[0], "%d", registration[0]);
1409
1410 /**
1411 * Optional bytes for DATA_REGISTRATION_STATE request
1412 * 4th byte : Registration denial code
1413 * 5th byte : The max. number of simultaneous Data Calls
1414 */
1415 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1416 // asprintf(&responseStr[4], "3");
1417 // asprintf(&responseStr[5], "1");
1418 }
1419
1420 for (j = startfrom; j < numElements; j++) {
1421 if (!responseStr[i]) goto error;
1422 }
1423 free(registration);
1424 registration = NULL;
1425
1426 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1427 for (j = 0; j < numElements; j++ ) {
1428 free(responseStr[j]);
1429 responseStr[j] = NULL;
1430 }
1431 free(responseStr);
1432 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001433 at_response_free(p_response);
1434
1435 return;
1436error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001437 if (responseStr) {
1438 for (j = 0; j < numElements; j++) {
1439 free(responseStr[j]);
1440 responseStr[j] = NULL;
1441 }
1442 free(responseStr);
1443 responseStr = NULL;
1444 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001445 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001446 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1447 at_response_free(p_response);
1448}
1449
Mark Salyzynba58c202014-03-12 15:20:22 -07001450static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001451{
1452 int err;
1453 int i;
1454 int skip;
1455 ATLine *p_cur;
1456 char *response[3];
1457
1458 memset(response, 0, sizeof(response));
1459
1460 ATResponse *p_response = NULL;
1461
1462 err = at_send_command_multiline(
1463 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1464 "+COPS:", &p_response);
1465
1466 /* we expect 3 lines here:
1467 * +COPS: 0,0,"T - Mobile"
1468 * +COPS: 0,1,"TMO"
1469 * +COPS: 0,2,"310170"
1470 */
1471
1472 if (err != 0) goto error;
1473
1474 for (i = 0, p_cur = p_response->p_intermediates
1475 ; p_cur != NULL
1476 ; p_cur = p_cur->p_next, i++
1477 ) {
1478 char *line = p_cur->line;
1479
1480 err = at_tok_start(&line);
1481 if (err < 0) goto error;
1482
1483 err = at_tok_nextint(&line, &skip);
1484 if (err < 0) goto error;
1485
1486 // If we're unregistered, we may just get
1487 // a "+COPS: 0" response
1488 if (!at_tok_hasmore(&line)) {
1489 response[i] = NULL;
1490 continue;
1491 }
1492
1493 err = at_tok_nextint(&line, &skip);
1494 if (err < 0) goto error;
1495
1496 // a "+COPS: 0, n" response is also possible
1497 if (!at_tok_hasmore(&line)) {
1498 response[i] = NULL;
1499 continue;
1500 }
1501
1502 err = at_tok_nextstr(&line, &(response[i]));
1503 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001504 // Simple assumption that mcc and mnc are 3 digits each
1505 if (strlen(response[i]) == 6) {
1506 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1507 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1508 }
1509 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001510 }
1511
1512 if (i != 3) {
1513 /* expect 3 lines exactly */
1514 goto error;
1515 }
1516
1517 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1518 at_response_free(p_response);
1519
1520 return;
1521error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001522 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001523 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1524 at_response_free(p_response);
1525}
1526
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001527static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1528{
1529 int err = 1; // Set to go to error:
1530 RIL_SMS_Response response;
1531 RIL_CDMA_SMS_Message* rcsm;
1532
Mark Salyzynba58c202014-03-12 15:20:22 -07001533 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001534 datalen, sizeof(RIL_CDMA_SMS_Message));
1535
1536 // verify data content to test marshalling/unmarshalling:
1537 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001538 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001539 uServicecategory=%d, sAddress.digit_mode=%d, \
1540 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1541 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1542 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1543 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1544
1545 if (err != 0) goto error;
1546
1547 // Cdma Send SMS implementation will go here:
1548 // But it is not implemented yet.
1549
1550 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001551 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001552 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1553 return;
1554
1555error:
1556 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001557 response.messageRef = -1;
1558 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001559}
1560
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001561static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1562{
1563 int err;
1564 const char *smsc;
1565 const char *pdu;
1566 int tpLayerLength;
1567 char *cmd1, *cmd2;
1568 RIL_SMS_Response response;
1569 ATResponse *p_response = NULL;
1570
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001571 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001572 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001573
1574 if (s_ims_gsm_fail != 0) goto error;
1575 if (s_ims_gsm_retry != 0) goto error2;
1576
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001577 smsc = ((const char **)data)[0];
1578 pdu = ((const char **)data)[1];
1579
1580 tpLayerLength = strlen(pdu)/2;
1581
1582 // "NULL for default SMSC"
1583 if (smsc == NULL) {
1584 smsc= "00";
1585 }
1586
1587 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1588 asprintf(&cmd2, "%s%s", smsc, pdu);
1589
1590 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1591
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001592 free(cmd1);
1593 free(cmd2);
1594
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001595 if (err != 0 || p_response->success == 0) goto error;
1596
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001597 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001598 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001599 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1600 at_response_free(p_response);
1601
1602 return;
1603error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001604 response.messageRef = -2;
1605 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001606 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001607 return;
1608error2:
1609 // send retry error.
1610 response.messageRef = -1;
1611 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1612 at_response_free(p_response);
1613 return;
1614 }
1615
1616static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1617{
1618 RIL_IMS_SMS_Message *p_args;
1619 RIL_SMS_Response response;
1620
1621 memset(&response, 0, sizeof(response));
1622
Mark Salyzynba58c202014-03-12 15:20:22 -07001623 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001624 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1625 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1626 datalen, s_ims_registered, s_ims_services, s_ims_format,
1627 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1628 s_ims_gsm_retry);
1629
1630 // figure out if this is gsm/cdma format
1631 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1632 p_args = (RIL_IMS_SMS_Message *)data;
1633
1634 if (0 != s_ims_cause_perm_failure ) goto error;
1635
1636 // want to fail over ims and this is first request over ims
1637 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1638
1639 if (RADIO_TECH_3GPP == p_args->tech) {
1640 return requestSendSMS(p_args->message.gsmMessage,
1641 datalen - sizeof(RIL_RadioTechnologyFamily),
1642 t);
1643 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1644 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1645 datalen - sizeof(RIL_RadioTechnologyFamily),
1646 t);
1647 } else {
1648 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1649 }
1650
1651error:
1652 response.messageRef = -2;
1653 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1654 return;
1655
1656error2:
1657 response.messageRef = -1;
1658 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001659}
1660
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001661static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1662{
1663 ATResponse *p_response = NULL;
1664 int32_t session_id;
1665 int err;
1666 char cmd[32];
1667 char dummy;
1668 char *line;
1669
1670 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1671 if (data == NULL || datalen == 0 || datalen > 16) {
1672 ALOGE("Invalid data passed to requestSimOpenChannel");
1673 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1674 return;
1675 }
1676
1677 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1678
1679 err = at_send_command_numeric(cmd, &p_response);
1680 if (err < 0 || p_response == NULL || p_response->success == 0) {
1681 ALOGE("Error %d opening logical channel: %d",
1682 err, p_response ? p_response->success : 0);
1683 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1684 at_response_free(p_response);
1685 return;
1686 }
1687
1688 // Ensure integer only by scanning for an extra char but expect one result
1689 line = p_response->p_intermediates->line;
1690 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1691 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1692 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1693 return;
1694 }
1695
1696 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1697 at_response_free(p_response);
1698}
1699
1700static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1701{
1702 ATResponse *p_response = NULL;
1703 int32_t session_id;
1704 int err;
1705 char cmd[32];
1706
1707 if (data == NULL || datalen != sizeof(session_id)) {
1708 ALOGE("Invalid data passed to requestSimCloseChannel");
1709 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1710 return;
1711 }
1712 session_id = ((int32_t *)data)[0];
1713 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1714 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1715
1716 if (err < 0 || p_response == NULL || p_response->success == 0) {
1717 ALOGE("Error %d closing logical channel %d: %d",
1718 err, session_id, p_response ? p_response->success : 0);
1719 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1720 at_response_free(p_response);
1721 return;
1722 }
1723
1724 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1725
1726 at_response_free(p_response);
1727}
1728
1729static void requestSimTransmitApduChannel(void *data,
1730 size_t datalen,
1731 RIL_Token t)
1732{
1733 ATResponse *p_response = NULL;
1734 int err;
1735 char *cmd;
1736 char *line;
1737 size_t cmd_size;
1738 RIL_SIM_IO_Response sim_response;
1739 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1740
1741 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1742 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1743 return;
1744 }
1745
1746 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
Wei Wang9cec1e02017-02-08 14:37:37 -08001747 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001748 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1749 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1750
1751 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1752 free(cmd);
1753 if (err < 0 || p_response == NULL || p_response->success == 0) {
1754 ALOGE("Error %d transmitting APDU: %d",
1755 err, p_response ? p_response->success : 0);
1756 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1757 at_response_free(p_response);
1758 return;
1759 }
1760
1761 line = p_response->p_intermediates->line;
1762 err = parseSimResponseLine(line, &sim_response);
1763
1764 if (err == 0) {
1765 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1766 &sim_response, sizeof(sim_response));
1767 } else {
1768 ALOGE("Error %d parsing SIM response line: %s", err, line);
1769 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1770 }
1771 at_response_free(p_response);
1772}
1773
Wink Savillef4c4d362009-04-02 01:37:03 -07001774static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001775{
1776 const char *apn;
1777 char *cmd;
1778 int err;
1779 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001780
Wink Savillef4c4d362009-04-02 01:37:03 -07001781 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001782
1783#ifdef USE_TI_COMMANDS
1784 // Config for multislot class 10 (probably default anyway eh?)
1785 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1786 NULL);
1787
1788 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1789#endif /* USE_TI_COMMANDS */
1790
1791 int fd, qmistatus;
1792 size_t cur = 0;
1793 size_t len;
1794 ssize_t written, rlen;
1795 char status[32] = {0};
1796 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001797 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001798
Wink Saville4dcab4f2012-11-19 16:05:13 -08001799 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001800
1801 fd = open ("/dev/qmi", O_RDWR);
1802 if (fd >= 0) { /* the device doesn't exist on the emulator */
1803
Wink Saville4dcab4f2012-11-19 16:05:13 -08001804 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001805 asprintf(&cmd, "up:%s", apn);
1806 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001807
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001808 while (cur < len) {
1809 do {
1810 written = write (fd, cmd + cur, len - cur);
1811 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001812
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001813 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001814 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001815 close(fd);
1816 goto error;
1817 }
1818
1819 cur += written;
1820 }
1821
1822 // wait for interface to come online
1823
1824 do {
1825 sleep(1);
1826 do {
1827 rlen = read(fd, status, 31);
1828 } while (rlen < 0 && errno == EINTR);
1829
1830 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001831 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001832 close(fd);
1833 goto error;
1834 } else {
1835 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001836 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001837 }
1838 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1839
1840 close(fd);
1841
1842 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001843 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001844 goto error;
1845 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001846
1847 qmistatus = system("netcfg rmnet0 dhcp");
1848
Wink Saville4dcab4f2012-11-19 16:05:13 -08001849 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001850
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001851 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001852
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001853 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001854
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001855 if (datalen > 6 * sizeof(char *)) {
1856 pdp_type = ((const char **)data)[6];
1857 } else {
1858 pdp_type = "IP";
1859 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001860
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001861 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001862 //FIXME check for error here
1863 err = at_send_command(cmd, NULL);
1864 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001865
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001866 // Set required QoS params to default
1867 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001868
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001869 // Set minimum QoS params to default
1870 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001871
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001872 // packet-domain event reporting
1873 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001874
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001875 // Hangup anything that's happening there now
1876 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001877
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001878 // Start data on PDP context 1
1879 err = at_send_command("ATD*99***1#", &p_response);
1880
1881 if (err < 0 || p_response->success == 0) {
1882 goto error;
1883 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001884 }
1885
Wink Saville43808972011-01-13 17:39:51 -08001886 requestOrSendDataCallList(&t);
1887
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001888 at_response_free(p_response);
1889
1890 return;
1891error:
1892 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1893 at_response_free(p_response);
1894
1895}
1896
Mark Salyzynba58c202014-03-12 15:20:22 -07001897static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001898{
1899 int ackSuccess;
1900 int err;
1901
1902 ackSuccess = ((int *)data)[0];
1903
1904 if (ackSuccess == 1) {
1905 err = at_send_command("AT+CNMA=1", NULL);
1906 } else if (ackSuccess == 0) {
1907 err = at_send_command("AT+CNMA=2", NULL);
1908 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001909 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001910 goto error;
1911 }
1912
1913 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1914error:
1915 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1916
1917}
1918
Mark Salyzynba58c202014-03-12 15:20:22 -07001919static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001920{
1921 ATResponse *p_response = NULL;
1922 RIL_SIM_IO_Response sr;
1923 int err;
1924 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001925 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001926 char *line;
1927
1928 memset(&sr, 0, sizeof(sr));
1929
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001930 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001931
1932 /* FIXME handle pin2 */
1933
1934 if (p_args->data == NULL) {
1935 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1936 p_args->command, p_args->fileid,
1937 p_args->p1, p_args->p2, p_args->p3);
1938 } else {
1939 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1940 p_args->command, p_args->fileid,
1941 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1942 }
1943
1944 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1945
1946 if (err < 0 || p_response->success == 0) {
1947 goto error;
1948 }
1949
1950 line = p_response->p_intermediates->line;
1951
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001952 err = parseSimResponseLine(line, &sr);
1953 if (err < 0) {
1954 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001955 }
1956
1957 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1958 at_response_free(p_response);
1959 free(cmd);
1960
1961 return;
1962error:
1963 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1964 at_response_free(p_response);
1965 free(cmd);
1966
1967}
1968
1969static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1970{
1971 ATResponse *p_response = NULL;
1972 int err;
1973 char* cmd = NULL;
1974 const char** strings = (const char**)data;;
1975
1976 if ( datalen == sizeof(char*) ) {
1977 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1978 } else if ( datalen == 2*sizeof(char*) ) {
1979 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1980 } else
1981 goto error;
1982
1983 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1984 free(cmd);
1985
1986 if (err < 0 || p_response->success == 0) {
1987error:
1988 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1989 } else {
1990 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1991 }
1992 at_response_free(p_response);
1993}
1994
1995
Mark Salyzynba58c202014-03-12 15:20:22 -07001996static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001997{
1998 const char *ussdRequest;
1999
2000 ussdRequest = (char *)(data);
2001
2002
2003 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2004
2005// @@@ TODO
2006
2007}
2008
Mark Salyzynba58c202014-03-12 15:20:22 -07002009static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002010{
2011 int err;
2012 ATResponse *p_response = NULL;
2013
2014 err = at_send_command("AT+WSOS=0", &p_response);
2015
2016 if (err < 0 || p_response->success == 0) {
2017 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2018 return;
2019 }
2020
2021 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2022}
2023
2024// TODO: Use all radio types
2025static int techFromModemType(int mdmtype)
2026{
2027 int ret = -1;
2028 switch (1 << mdmtype) {
2029 case MDM_CDMA:
2030 ret = RADIO_TECH_1xRTT;
2031 break;
2032 case MDM_EVDO:
2033 ret = RADIO_TECH_EVDO_A;
2034 break;
2035 case MDM_GSM:
2036 ret = RADIO_TECH_GPRS;
2037 break;
2038 case MDM_WCDMA:
2039 ret = RADIO_TECH_HSPA;
2040 break;
2041 case MDM_LTE:
2042 ret = RADIO_TECH_LTE;
2043 break;
2044 }
2045 return ret;
2046}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002047
Mark Salyzynba58c202014-03-12 15:20:22 -07002048static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002049{
2050 uint64_t curTime = ril_nano_time();
2051 RIL_CellInfo ci[1] =
2052 {
2053 { // ci[0]
2054 1, // cellInfoType
2055 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002056 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002057 curTime - 1000, // Fake some time in the past
2058 { // union CellInfo
2059 { // RIL_CellInfoGsm gsm
2060 { // gsm.cellIdneityGsm
2061 s_mcc, // mcc
2062 s_mnc, // mnc
2063 s_lac, // lac
2064 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002065 },
2066 { // gsm.signalStrengthGsm
2067 10, // signalStrength
2068 0 // bitErrorRate
2069 }
2070 }
2071 }
2072 }
2073 };
2074
2075 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2076}
2077
2078
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002079static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002080{
2081 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2082 // will be sent.
2083 assert (datalen == sizeof(int));
2084 s_cell_info_rate_ms = ((int *)data)[0];
2085
2086 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2087}
2088
Etan Cohend3652192014-06-20 08:28:44 -07002089static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2090{
2091 // TODO - hook this up with real query/info from radio.
2092
2093 RIL_HardwareConfig hwCfg;
2094
2095 RIL_UNUSED_PARM(data);
2096 RIL_UNUSED_PARM(datalen);
2097
2098 hwCfg.type = -1;
2099
2100 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2101}
2102
2103
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002104/*** Callback methods from the RIL library to us ***/
2105
2106/**
2107 * Call from RIL to us to make a RIL_REQUEST
2108 *
2109 * Must be completed with a call to RIL_onRequestComplete()
2110 *
2111 * RIL_onRequestComplete() may be called from any thread, before or after
2112 * this function returns.
2113 *
Weilun Du9f471e22017-02-07 10:47:19 -08002114 * Because onRequest function could be called from multiple different thread,
2115 * we must ensure that the underlying at_send_command_* function
2116 * is atomic.
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002117 */
2118static void
2119onRequest (int request, void *data, size_t datalen, RIL_Token t)
2120{
2121 ATResponse *p_response;
2122 int err;
2123
Wink Saville4dcab4f2012-11-19 16:05:13 -08002124 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002125
2126 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2127 * when RADIO_STATE_UNAVAILABLE.
2128 */
2129 if (sState == RADIO_STATE_UNAVAILABLE
2130 && request != RIL_REQUEST_GET_SIM_STATUS
2131 ) {
2132 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2133 return;
2134 }
2135
2136 /* Ignore all non-power requests when RADIO_STATE_OFF
2137 * (except RIL_REQUEST_GET_SIM_STATUS)
2138 */
2139 if (sState == RADIO_STATE_OFF
2140 && !(request == RIL_REQUEST_RADIO_POWER
2141 || request == RIL_REQUEST_GET_SIM_STATUS)
2142 ) {
2143 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2144 return;
2145 }
2146
2147 switch (request) {
2148 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002149 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002150 char *p_buffer;
2151 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002152
Wink Savillef6aa7c12009-04-30 14:20:52 -07002153 int result = getCardStatus(&p_card_status);
2154 if (result == RIL_E_SUCCESS) {
2155 p_buffer = (char *)p_card_status;
2156 buffer_size = sizeof(*p_card_status);
2157 } else {
2158 p_buffer = NULL;
2159 buffer_size = 0;
2160 }
2161 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2162 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002163 break;
2164 }
2165 case RIL_REQUEST_GET_CURRENT_CALLS:
2166 requestGetCurrentCalls(data, datalen, t);
2167 break;
2168 case RIL_REQUEST_DIAL:
2169 requestDial(data, datalen, t);
2170 break;
2171 case RIL_REQUEST_HANGUP:
2172 requestHangup(data, datalen, t);
2173 break;
2174 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
2175 // 3GPP 22.030 6.5.5
2176 // "Releases all held calls or sets User Determined User Busy
2177 // (UDUB) for a waiting call."
2178 at_send_command("AT+CHLD=0", NULL);
2179
2180 /* success or failure is ignored by the upper layer here.
2181 it will call GET_CURRENT_CALLS and determine success that way */
2182 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2183 break;
2184 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
2185 // 3GPP 22.030 6.5.5
2186 // "Releases all active calls (if any exist) and accepts
2187 // the other (held or waiting) call."
2188 at_send_command("AT+CHLD=1", NULL);
2189
2190 /* success or failure is ignored by the upper layer here.
2191 it will call GET_CURRENT_CALLS and determine success that way */
2192 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2193 break;
2194 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
2195 // 3GPP 22.030 6.5.5
2196 // "Places all active calls (if any exist) on hold and accepts
2197 // the other (held or waiting) call."
2198 at_send_command("AT+CHLD=2", NULL);
2199
2200#ifdef WORKAROUND_ERRONEOUS_ANSWER
2201 s_expectAnswer = 1;
2202#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2203
2204 /* success or failure is ignored by the upper layer here.
2205 it will call GET_CURRENT_CALLS and determine success that way */
2206 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2207 break;
2208 case RIL_REQUEST_ANSWER:
2209 at_send_command("ATA", NULL);
2210
2211#ifdef WORKAROUND_ERRONEOUS_ANSWER
2212 s_expectAnswer = 1;
2213#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2214
2215 /* success or failure is ignored by the upper layer here.
2216 it will call GET_CURRENT_CALLS and determine success that way */
2217 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2218 break;
2219 case RIL_REQUEST_CONFERENCE:
2220 // 3GPP 22.030 6.5.5
2221 // "Adds a held call to the conversation"
2222 at_send_command("AT+CHLD=3", NULL);
2223
2224 /* success or failure is ignored by the upper layer here.
2225 it will call GET_CURRENT_CALLS and determine success that way */
2226 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2227 break;
2228 case RIL_REQUEST_UDUB:
2229 /* user determined user busy */
2230 /* sometimes used: ATH */
2231 at_send_command("ATH", NULL);
2232
2233 /* success or failure is ignored by the upper layer here.
2234 it will call GET_CURRENT_CALLS and determine success that way */
2235 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2236 break;
2237
2238 case RIL_REQUEST_SEPARATE_CONNECTION:
2239 {
2240 char cmd[12];
2241 int party = ((int*)data)[0];
2242
2243 // Make sure that party is in a valid range.
2244 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2245 // It's sufficient for us to just make sure it's single digit.)
2246 if (party > 0 && party < 10) {
2247 sprintf(cmd, "AT+CHLD=2%d", party);
2248 at_send_command(cmd, NULL);
2249 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2250 } else {
2251 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2252 }
2253 }
2254 break;
2255
2256 case RIL_REQUEST_SIGNAL_STRENGTH:
2257 requestSignalStrength(data, datalen, t);
2258 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002259 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2260 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002261 requestRegistrationState(request, data, datalen, t);
2262 break;
2263 case RIL_REQUEST_OPERATOR:
2264 requestOperator(data, datalen, t);
2265 break;
2266 case RIL_REQUEST_RADIO_POWER:
2267 requestRadioPower(data, datalen, t);
2268 break;
2269 case RIL_REQUEST_DTMF: {
2270 char c = ((char *)data)[0];
2271 char *cmd;
2272 asprintf(&cmd, "AT+VTS=%c", (int)c);
2273 at_send_command(cmd, NULL);
2274 free(cmd);
2275 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2276 break;
2277 }
2278 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302279 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002280 requestSendSMS(data, datalen, t);
2281 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002282 case RIL_REQUEST_CDMA_SEND_SMS:
2283 requestCdmaSendSMS(data, datalen, t);
2284 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002285 case RIL_REQUEST_IMS_SEND_SMS:
2286 requestImsSendSMS(data, datalen, t);
2287 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002288 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2289 requestSimOpenChannel(data, datalen, t);
2290 break;
2291 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2292 requestSimCloseChannel(data, datalen, t);
2293 break;
2294 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2295 requestSimTransmitApduChannel(data, datalen, t);
2296 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002297 case RIL_REQUEST_SETUP_DATA_CALL:
2298 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002299 break;
2300 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2301 requestSMSAcknowledge(data, datalen, t);
2302 break;
2303
2304 case RIL_REQUEST_GET_IMSI:
2305 p_response = NULL;
2306 err = at_send_command_numeric("AT+CIMI", &p_response);
2307
2308 if (err < 0 || p_response->success == 0) {
2309 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2310 } else {
2311 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2312 p_response->p_intermediates->line, sizeof(char *));
2313 }
2314 at_response_free(p_response);
2315 break;
2316
2317 case RIL_REQUEST_GET_IMEI:
2318 p_response = NULL;
2319 err = at_send_command_numeric("AT+CGSN", &p_response);
2320
2321 if (err < 0 || p_response->success == 0) {
2322 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2323 } else {
2324 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2325 p_response->p_intermediates->line, sizeof(char *));
2326 }
2327 at_response_free(p_response);
2328 break;
2329
2330 case RIL_REQUEST_SIM_IO:
2331 requestSIM_IO(data,datalen,t);
2332 break;
2333
2334 case RIL_REQUEST_SEND_USSD:
2335 requestSendUSSD(data, datalen, t);
2336 break;
2337
2338 case RIL_REQUEST_CANCEL_USSD:
2339 p_response = NULL;
2340 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2341
2342 if (err < 0 || p_response->success == 0) {
2343 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2344 } else {
2345 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2346 p_response->p_intermediates->line, sizeof(char *));
2347 }
2348 at_response_free(p_response);
2349 break;
2350
2351 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2352 at_send_command("AT+COPS=0", NULL);
2353 break;
2354
Wink Savillef4c4d362009-04-02 01:37:03 -07002355 case RIL_REQUEST_DATA_CALL_LIST:
2356 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002357 break;
2358
2359 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2360 requestQueryNetworkSelectionMode(data, datalen, t);
2361 break;
2362
2363 case RIL_REQUEST_OEM_HOOK_RAW:
2364 // echo back data
2365 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2366 break;
2367
2368
2369 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2370 int i;
2371 const char ** cur;
2372
Wink Saville4dcab4f2012-11-19 16:05:13 -08002373 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002374
2375
2376 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2377 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002378 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002379 }
2380
2381 // echo back strings
2382 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2383 break;
2384 }
2385
2386 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2387 requestWriteSmsToSim(data, datalen, t);
2388 break;
2389
2390 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2391 char * cmd;
2392 p_response = NULL;
2393 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2394 err = at_send_command(cmd, &p_response);
2395 free(cmd);
2396 if (err < 0 || p_response->success == 0) {
2397 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2398 } else {
2399 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2400 }
2401 at_response_free(p_response);
2402 break;
2403 }
2404
2405 case RIL_REQUEST_ENTER_SIM_PIN:
2406 case RIL_REQUEST_ENTER_SIM_PUK:
2407 case RIL_REQUEST_ENTER_SIM_PIN2:
2408 case RIL_REQUEST_ENTER_SIM_PUK2:
2409 case RIL_REQUEST_CHANGE_SIM_PIN:
2410 case RIL_REQUEST_CHANGE_SIM_PIN2:
2411 requestEnterSimPin(data, datalen, t);
2412 break;
2413
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002414 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2415 int reply[2];
2416 //0==unregistered, 1==registered
2417 reply[0] = s_ims_registered;
2418
2419 //to be used when changed to include service supporated info
2420 //reply[1] = s_ims_services;
2421
2422 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2423 reply[1] = s_ims_format;
2424
2425 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2426 reply[0], reply[1]);
2427 if (reply[1] != -1) {
2428 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2429 } else {
2430 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2431 }
2432 break;
2433 }
2434
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002435 case RIL_REQUEST_VOICE_RADIO_TECH:
2436 {
2437 int tech = techFromModemType(TECH(sMdmInfo));
2438 if (tech < 0 )
2439 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2440 else
2441 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2442 }
2443 break;
2444 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2445 requestSetPreferredNetworkType(request, data, datalen, t);
2446 break;
2447
2448 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2449 requestGetPreferredNetworkType(request, data, datalen, t);
2450 break;
2451
Jun Tian58027012013-07-30 11:07:22 +08002452 case RIL_REQUEST_GET_CELL_INFO_LIST:
2453 requestGetCellInfoList(data, datalen, t);
2454 break;
2455
2456 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2457 requestSetCellInfoListRate(data, datalen, t);
2458 break;
2459
Etan Cohend3652192014-06-20 08:28:44 -07002460 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2461 requestGetHardwareConfig(data, datalen, t);
2462 break;
2463
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002464 case RIL_REQUEST_SHUTDOWN:
2465 requestShutdown(t);
2466 break;
2467
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002468 /* CDMA Specific Requests */
2469 case RIL_REQUEST_BASEBAND_VERSION:
2470 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2471 requestCdmaBaseBandVersion(request, data, datalen, t);
2472 break;
2473 } // Fall-through if tech is not cdma
2474
2475 case RIL_REQUEST_DEVICE_IDENTITY:
2476 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2477 requestCdmaDeviceIdentity(request, data, datalen, t);
2478 break;
2479 } // Fall-through if tech is not cdma
2480
2481 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2482 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2483 requestCdmaSubscription(request, data, datalen, t);
2484 break;
2485 } // Fall-through if tech is not cdma
2486
2487 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2488 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2489 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2490 break;
2491 } // Fall-through if tech is not cdma
2492
2493 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2494 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2495 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2496 break;
2497 } // Fall-through if tech is not cdma
2498
2499 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2500 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2501 requestCdmaGetRoamingPreference(request, data, datalen, t);
2502 break;
2503 } // Fall-through if tech is not cdma
2504
2505 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2506 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2507 requestCdmaSetRoamingPreference(request, data, datalen, t);
2508 break;
2509 } // Fall-through if tech is not cdma
2510
2511 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2512 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2513 requestExitEmergencyMode(data, datalen, t);
2514 break;
2515 } // Fall-through if tech is not cdma
2516
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002517 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002518 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002519 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2520 break;
2521 }
2522}
2523
2524/**
2525 * Synchronous call from the RIL to us to return current radio state.
2526 * RADIO_STATE_UNAVAILABLE should be the initial state.
2527 */
2528static RIL_RadioState
2529currentState()
2530{
2531 return sState;
2532}
2533/**
2534 * Call from RIL to us to find out whether a specific request code
2535 * is supported by this implementation.
2536 *
2537 * Return 1 for "supported" and 0 for "unsupported"
2538 */
2539
2540static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002541onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002542{
2543 //@@@ todo
2544
2545 return 1;
2546}
2547
Mark Salyzynba58c202014-03-12 15:20:22 -07002548static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002549{
2550 //@@@todo
2551
2552}
2553
2554static const char * getVersion(void)
2555{
2556 return "android reference-ril 1.0";
2557}
2558
2559static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002560setRadioTechnology(ModemInfo *mdm, int newtech)
2561{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002562 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002563
2564 int oldtech = TECH(mdm);
2565
2566 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002567 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002568 TECH(mdm) = newtech;
2569 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2570 int tech = techFromModemType(TECH(sMdmInfo));
2571 if (tech > 0 ) {
2572 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2573 &tech, sizeof(tech));
2574 }
2575 }
2576 }
2577}
2578
2579static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002580setRadioState(RIL_RadioState newState)
2581{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002582 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002583 RIL_RadioState oldState;
2584
2585 pthread_mutex_lock(&s_state_mutex);
2586
2587 oldState = sState;
2588
2589 if (s_closed > 0) {
2590 // If we're closed, the only reasonable state is
2591 // RADIO_STATE_UNAVAILABLE
2592 // This is here because things on the main thread
2593 // may attempt to change the radio state after the closed
2594 // event happened in another thread
2595 newState = RADIO_STATE_UNAVAILABLE;
2596 }
2597
2598 if (sState != newState || s_closed > 0) {
2599 sState = newState;
2600
2601 pthread_cond_broadcast (&s_state_cond);
2602 }
2603
2604 pthread_mutex_unlock(&s_state_mutex);
2605
2606
2607 /* do these outside of the mutex */
2608 if (sState != oldState) {
2609 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2610 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002611 // Sim state can change as result of radio state change
2612 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2613 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002614
2615 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2616 * from the AT reader thread
2617 * Currently, this doesn't happen, but if that changes then these
2618 * will need to be dispatched on the request thread
2619 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002620 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002621 onRadioPowerOn();
2622 }
2623 }
2624}
2625
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002626/** Returns RUIM_NOT_READY on error */
2627static SIM_Status
2628getRUIMStatus()
2629{
2630 ATResponse *p_response = NULL;
2631 int err;
2632 int ret;
2633 char *cpinLine;
2634 char *cpinResult;
2635
2636 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2637 ret = SIM_NOT_READY;
2638 goto done;
2639 }
2640
2641 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2642
2643 if (err != 0) {
2644 ret = SIM_NOT_READY;
2645 goto done;
2646 }
2647
2648 switch (at_get_cme_error(p_response)) {
2649 case CME_SUCCESS:
2650 break;
2651
2652 case CME_SIM_NOT_INSERTED:
2653 ret = SIM_ABSENT;
2654 goto done;
2655
2656 default:
2657 ret = SIM_NOT_READY;
2658 goto done;
2659 }
2660
2661 /* CPIN? has succeeded, now look at the result */
2662
2663 cpinLine = p_response->p_intermediates->line;
2664 err = at_tok_start (&cpinLine);
2665
2666 if (err < 0) {
2667 ret = SIM_NOT_READY;
2668 goto done;
2669 }
2670
2671 err = at_tok_nextstr(&cpinLine, &cpinResult);
2672
2673 if (err < 0) {
2674 ret = SIM_NOT_READY;
2675 goto done;
2676 }
2677
2678 if (0 == strcmp (cpinResult, "SIM PIN")) {
2679 ret = SIM_PIN;
2680 goto done;
2681 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2682 ret = SIM_PUK;
2683 goto done;
2684 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2685 return SIM_NETWORK_PERSONALIZATION;
2686 } else if (0 != strcmp (cpinResult, "READY")) {
2687 /* we're treating unsupported lock types as "sim absent" */
2688 ret = SIM_ABSENT;
2689 goto done;
2690 }
2691
2692 at_response_free(p_response);
2693 p_response = NULL;
2694 cpinResult = NULL;
2695
2696 ret = SIM_READY;
2697
2698done:
2699 at_response_free(p_response);
2700 return ret;
2701}
2702
John Wang309ac292009-07-30 14:53:23 -07002703/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002704static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002705getSIMStatus()
2706{
2707 ATResponse *p_response = NULL;
2708 int err;
2709 int ret;
2710 char *cpinLine;
2711 char *cpinResult;
2712
Wink Saville4dcab4f2012-11-19 16:05:13 -08002713 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002714 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
John Wang309ac292009-07-30 14:53:23 -07002715 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002716 goto done;
2717 }
2718
2719 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2720
2721 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002722 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002723 goto done;
2724 }
2725
2726 switch (at_get_cme_error(p_response)) {
2727 case CME_SUCCESS:
2728 break;
2729
2730 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002731 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002732 goto done;
2733
2734 default:
John Wang309ac292009-07-30 14:53:23 -07002735 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002736 goto done;
2737 }
2738
2739 /* CPIN? has succeeded, now look at the result */
2740
2741 cpinLine = p_response->p_intermediates->line;
2742 err = at_tok_start (&cpinLine);
2743
2744 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002745 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002746 goto done;
2747 }
2748
2749 err = at_tok_nextstr(&cpinLine, &cpinResult);
2750
2751 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002752 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002753 goto done;
2754 }
2755
2756 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002757 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002758 goto done;
2759 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07002760 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002761 goto done;
2762 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002763 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002764 } else if (0 != strcmp (cpinResult, "READY")) {
2765 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07002766 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002767 goto done;
2768 }
2769
2770 at_response_free(p_response);
2771 p_response = NULL;
2772 cpinResult = NULL;
2773
John Wang309ac292009-07-30 14:53:23 -07002774 ret = SIM_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002775
2776done:
2777 at_response_free(p_response);
2778 return ret;
2779}
2780
2781
2782/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07002783 * Get the current card status.
2784 *
2785 * This must be freed using freeCardStatus.
2786 * @return: On success returns RIL_E_SUCCESS
2787 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002788static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002789 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07002790 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07002791 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2792 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002793 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07002794 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2795 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002796 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07002797 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2798 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002799 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07002800 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2801 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002802 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07002803 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2804 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002805 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07002806 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002807 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2808 // RUIM_ABSENT = 6
2809 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2810 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2811 // RUIM_NOT_READY = 7
2812 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2813 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2814 // RUIM_READY = 8
2815 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2816 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2817 // RUIM_PIN = 9
2818 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2819 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2820 // RUIM_PUK = 10
2821 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2822 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2823 // RUIM_NETWORK_PERSONALIZATION = 11
2824 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2825 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
Wink Savillef6aa7c12009-04-30 14:20:52 -07002826 };
2827 RIL_CardState card_state;
2828 int num_apps;
2829
2830 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07002831 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002832 card_state = RIL_CARDSTATE_ABSENT;
2833 num_apps = 0;
2834 } else {
2835 card_state = RIL_CARDSTATE_PRESENT;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002836 num_apps = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002837 }
2838
2839 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002840 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07002841 p_card_status->card_state = card_state;
2842 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
2843 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
2844 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002845 p_card_status->ims_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002846 p_card_status->num_applications = num_apps;
2847
2848 // Initialize application status
2849 int i;
2850 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07002851 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002852 }
2853
2854 // Pickup the appropriate application status
2855 // that reflects sim_status for gsm.
2856 if (num_apps != 0) {
2857 // Only support one app, gsm
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002858 p_card_status->num_applications = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002859 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002860 p_card_status->cdma_subscription_app_index = 1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002861
2862 // Get the correct app status
2863 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002864 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002865 }
2866
2867 *pp_card_status = p_card_status;
2868 return RIL_E_SUCCESS;
2869}
2870
2871/**
2872 * Free the card status returned by getCardStatus
2873 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002874static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002875 free(p_card_status);
2876}
2877
2878/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002879 * SIM ready means any commands that access the SIM will work, including:
2880 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
2881 * (all SMS-related commands)
2882 */
2883
Mark Salyzynba58c202014-03-12 15:20:22 -07002884static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002885{
2886 ATResponse *p_response;
2887 int ret;
2888
Naveen Kalla2baf7232016-10-11 13:49:20 -07002889 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002890 // no longer valid to poll
2891 return;
2892 }
2893
2894 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07002895 case SIM_ABSENT:
2896 case SIM_PIN:
2897 case SIM_PUK:
2898 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002899 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002900 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002901 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002902 return;
2903
John Wang309ac292009-07-30 14:53:23 -07002904 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002905 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
2906 return;
2907
John Wang309ac292009-07-30 14:53:23 -07002908 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002909 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002910 onSIMReady();
2911 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002912 return;
2913 }
2914}
2915
2916/** returns 1 if on, 0 if off, and -1 on error */
2917static int isRadioOn()
2918{
2919 ATResponse *p_response = NULL;
2920 int err;
2921 char *line;
2922 char ret;
2923
2924 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
2925
2926 if (err < 0 || p_response->success == 0) {
2927 // assume radio is off
2928 goto error;
2929 }
2930
2931 line = p_response->p_intermediates->line;
2932
2933 err = at_tok_start(&line);
2934 if (err < 0) goto error;
2935
2936 err = at_tok_nextbool(&line, &ret);
2937 if (err < 0) goto error;
2938
2939 at_response_free(p_response);
2940
2941 return (int)ret;
2942
2943error:
2944
2945 at_response_free(p_response);
2946 return -1;
2947}
2948
2949/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002950 * Parse the response generated by a +CTEC AT command
2951 * The values read from the response are stored in current and preferred.
2952 * Both current and preferred may be null. The corresponding value is ignored in that case.
2953 *
2954 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
2955 * 1 if the response includes the current technology only
2956 * 0 if the response includes both current technology and preferred mode
2957 */
2958int parse_technology_response( const char *response, int *current, int32_t *preferred )
2959{
2960 int err;
2961 char *line, *p;
2962 int ct;
2963 int32_t pt = 0;
2964 char *str_pt;
2965
2966 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08002967 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002968 err = at_tok_start(&p);
2969 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002970 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002971 free(line);
2972 return -1;
2973 }
2974
2975 err = at_tok_nextint(&p, &ct);
2976 if (err) {
2977 free(line);
2978 return -1;
2979 }
2980 if (current) *current = ct;
2981
Wink Saville4dcab4f2012-11-19 16:05:13 -08002982 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002983
2984 err = at_tok_nexthexint(&p, &pt);
2985 if (err) {
2986 free(line);
2987 return 1;
2988 }
2989 if (preferred) {
2990 *preferred = pt;
2991 }
2992 free(line);
2993
2994 return 0;
2995}
2996
Mark Salyzynba58c202014-03-12 15:20:22 -07002997int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002998{
2999 ATResponse *p_response;
3000 int err, val, techs = 0;
3001 char *tok;
3002 char *line;
3003
Wink Saville4dcab4f2012-11-19 16:05:13 -08003004 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003005 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3006 if (err || !p_response->success)
3007 goto error;
3008 line = p_response->p_intermediates->line;
3009 err = at_tok_start(&line);
3010 if (err || !at_tok_hasmore(&line))
3011 goto error;
3012 while (!at_tok_nextint(&line, &val)) {
3013 techs |= ( 1 << val );
3014 }
3015 if (supported) *supported = techs;
3016 return 0;
3017error:
3018 at_response_free(p_response);
3019 return -1;
3020}
3021
3022/**
3023 * query_ctec. Send the +CTEC AT command to the modem to query the current
3024 * and preferred modes. It leaves values in the addresses pointed to by
3025 * current and preferred. If any of those pointers are NULL, the corresponding value
3026 * is ignored, but the return value will still reflect if retreiving and parsing of the
3027 * values suceeded.
3028 *
3029 * @mdm Currently unused
3030 * @current A pointer to store the current mode returned by the modem. May be null.
3031 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3032 * @return -1 on error (or failure to parse)
3033 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3034 * 0 if both current and preferred were returned correctly
3035 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003036int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003037{
3038 ATResponse *response = NULL;
3039 int err;
3040 int res;
3041
Colin Cross5cba4882014-02-05 18:55:42 -08003042 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003043 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3044 if (!err && response->success) {
3045 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3046 at_response_free(response);
3047 return res;
3048 }
Colin Cross5cba4882014-02-05 18:55:42 -08003049 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003050 at_response_free(response);
3051 return -1;
3052}
3053
3054int is_multimode_modem(ModemInfo *mdm)
3055{
3056 ATResponse *response;
3057 int err;
3058 char *line;
3059 int tech;
3060 int32_t preferred;
3061
3062 if (query_ctec(mdm, &tech, &preferred) == 0) {
3063 mdm->currentTech = tech;
3064 mdm->preferredNetworkMode = preferred;
3065 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3066 return 0;
3067 }
3068 return 1;
3069 }
3070 return 0;
3071}
3072
3073/**
3074 * Find out if our modem is GSM, CDMA or both (Multimode)
3075 */
3076static void probeForModemMode(ModemInfo *info)
3077{
3078 ATResponse *response;
3079 int err;
3080 assert (info);
3081 // Currently, our only known multimode modem is qemu's android modem,
3082 // which implements the AT+CTEC command to query and set mode.
3083 // Try that first
3084
3085 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003086 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003087 info->supportedTechs, info->currentTech);
3088 return;
3089 }
3090
3091 /* Being here means that our modem is not multimode */
3092 info->isMultimode = 0;
3093
3094 /* CDMA Modems implement the AT+WNAM command */
3095 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3096 if (!err && response->success) {
3097 at_response_free(response);
3098 // TODO: find out if we really support EvDo
3099 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3100 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003101 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003102 return;
3103 }
3104 if (!err) at_response_free(response);
3105 // TODO: find out if modem really supports WCDMA/LTE
3106 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3107 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003108 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003109}
3110
3111/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003112 * Initialize everything that can be configured while we're still in
3113 * AT+CFUN=0
3114 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003115static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003116{
3117 ATResponse *p_response = NULL;
3118 int err;
3119
3120 setRadioState (RADIO_STATE_OFF);
3121
3122 at_handshake();
3123
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003124 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003125 /* note: we don't check errors here. Everything important will
3126 be handled in onATTimeout and onATReaderClosed */
3127
3128 /* atchannel is tolerant of echo but it must */
3129 /* have verbose result codes */
3130 at_send_command("ATE0Q0V1", NULL);
3131
3132 /* No auto-answer */
3133 at_send_command("ATS0=0", NULL);
3134
3135 /* Extended errors */
3136 at_send_command("AT+CMEE=1", NULL);
3137
3138 /* Network registration events */
3139 err = at_send_command("AT+CREG=2", &p_response);
3140
3141 /* some handsets -- in tethered mode -- don't support CREG=2 */
3142 if (err < 0 || p_response->success == 0) {
3143 at_send_command("AT+CREG=1", NULL);
3144 }
3145
3146 at_response_free(p_response);
3147
3148 /* GPRS registration events */
3149 at_send_command("AT+CGREG=1", NULL);
3150
3151 /* Call Waiting notifications */
3152 at_send_command("AT+CCWA=1", NULL);
3153
3154 /* Alternating voice/data off */
3155 at_send_command("AT+CMOD=0", NULL);
3156
3157 /* Not muted */
3158 at_send_command("AT+CMUT=0", NULL);
3159
3160 /* +CSSU unsolicited supp service notifications */
3161 at_send_command("AT+CSSN=0,1", NULL);
3162
3163 /* no connected line identification */
3164 at_send_command("AT+COLP=0", NULL);
3165
3166 /* HEX character set */
3167 at_send_command("AT+CSCS=\"HEX\"", NULL);
3168
3169 /* USSD unsolicited */
3170 at_send_command("AT+CUSD=1", NULL);
3171
3172 /* Enable +CGEV GPRS event notifications, but don't buffer */
3173 at_send_command("AT+CGEREP=1,0", NULL);
3174
3175 /* SMS PDU mode */
3176 at_send_command("AT+CMGF=0", NULL);
3177
3178#ifdef USE_TI_COMMANDS
3179
3180 at_send_command("AT%CPI=3", NULL);
3181
3182 /* TI specific -- notifications when SMS is ready (currently ignored) */
3183 at_send_command("AT%CSTAT=1", NULL);
3184
3185#endif /* USE_TI_COMMANDS */
3186
3187
3188 /* assume radio is off on error */
3189 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003190 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003191 }
3192}
3193
3194static void waitForClose()
3195{
3196 pthread_mutex_lock(&s_state_mutex);
3197
3198 while (s_closed == 0) {
3199 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3200 }
3201
3202 pthread_mutex_unlock(&s_state_mutex);
3203}
3204
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003205static void sendUnsolImsNetworkStateChanged()
3206{
3207#if 0 // to be used when unsol is changed to return data.
3208 int reply[2];
3209 reply[0] = s_ims_registered;
3210 reply[1] = s_ims_services;
3211 reply[1] = s_ims_format;
3212#endif
3213 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3214 NULL, 0);
3215}
3216
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003217/**
3218 * Called by atchannel when an unsolicited line appears
3219 * This is called on atchannel's reader thread. AT commands may
3220 * not be issued here
3221 */
3222static void onUnsolicited (const char *s, const char *sms_pdu)
3223{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003224 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003225 int err;
3226
3227 /* Ignore unsolicited responses until we're initialized.
3228 * This is OK because the RIL library will poll for initial state
3229 */
3230 if (sState == RADIO_STATE_UNAVAILABLE) {
3231 return;
3232 }
3233
3234 if (strStartsWith(s, "%CTZV:")) {
3235 /* TI specific -- NITZ time */
3236 char *response;
3237
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003238 line = p = strdup(s);
3239 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003240
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003241 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003242
3243 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003244 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003245 } else {
3246 RIL_onUnsolicitedResponse (
3247 RIL_UNSOL_NITZ_TIME_RECEIVED,
3248 response, strlen(response));
3249 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003250 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003251 } else if (strStartsWith(s,"+CRING:")
3252 || strStartsWith(s,"RING")
3253 || strStartsWith(s,"NO CARRIER")
3254 || strStartsWith(s,"+CCWA")
3255 ) {
3256 RIL_onUnsolicitedResponse (
3257 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3258 NULL, 0);
3259#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003260 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003261#endif /* WORKAROUND_FAKE_CGEV */
3262 } else if (strStartsWith(s,"+CREG:")
3263 || strStartsWith(s,"+CGREG:")
3264 ) {
3265 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003266 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003267 NULL, 0);
3268#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003269 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003270#endif /* WORKAROUND_FAKE_CGEV */
3271 } else if (strStartsWith(s, "+CMT:")) {
3272 RIL_onUnsolicitedResponse (
3273 RIL_UNSOL_RESPONSE_NEW_SMS,
3274 sms_pdu, strlen(sms_pdu));
3275 } else if (strStartsWith(s, "+CDS:")) {
3276 RIL_onUnsolicitedResponse (
3277 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3278 sms_pdu, strlen(sms_pdu));
3279 } else if (strStartsWith(s, "+CGEV:")) {
3280 /* Really, we can ignore NW CLASS and ME CLASS events here,
3281 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003282 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003283 */
3284 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003285 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003286#ifdef WORKAROUND_FAKE_CGEV
3287 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003288 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003289#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003290 } else if (strStartsWith(s, "+CTEC: ")) {
3291 int tech, mask;
3292 switch (parse_technology_response(s, &tech, NULL))
3293 {
3294 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003295 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003296 break;
3297 case 1: // current mode correctly parsed
3298 case 0: // preferred mode correctly parsed
3299 mask = 1 << tech;
3300 if (mask != MDM_GSM && mask != MDM_CDMA &&
3301 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003302 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003303 } else {
3304 setRadioTechnology(sMdmInfo, tech);
3305 }
3306 break;
3307 }
3308 } else if (strStartsWith(s, "+CCSS: ")) {
3309 int source = 0;
3310 line = p = strdup(s);
3311 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003312 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003313 return;
3314 }
3315 if (at_tok_start(&p) < 0) {
3316 free(line);
3317 return;
3318 }
3319 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003320 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003321 free(line);
3322 return;
3323 }
3324 SSOURCE(sMdmInfo) = source;
3325 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3326 &source, sizeof(source));
3327 } else if (strStartsWith(s, "+WSOS: ")) {
3328 char state = 0;
3329 int unsol;
3330 line = p = strdup(s);
3331 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003332 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003333 return;
3334 }
3335 if (at_tok_start(&p) < 0) {
3336 free(line);
3337 return;
3338 }
3339 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003340 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003341 free(line);
3342 return;
3343 }
3344 free(line);
3345
3346 unsol = state ?
3347 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3348
3349 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3350
3351 } else if (strStartsWith(s, "+WPRL: ")) {
3352 int version = -1;
3353 line = p = strdup(s);
3354 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003355 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003356 return;
3357 }
3358 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003359 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003360 free(line);
3361 return;
3362 }
3363 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003364 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003365 free(line);
3366 return;
3367 }
3368 free(line);
3369 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3370 } else if (strStartsWith(s, "+CFUN: 0")) {
3371 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003372 }
3373}
3374
3375/* Called on command or reader thread */
3376static void onATReaderClosed()
3377{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003378 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003379 at_close();
3380 s_closed = 1;
3381
3382 setRadioState (RADIO_STATE_UNAVAILABLE);
3383}
3384
3385/* Called on command thread */
3386static void onATTimeout()
3387{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003388 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003389 at_close();
3390
3391 s_closed = 1;
3392
3393 /* FIXME cause a radio reset here */
3394
3395 setRadioState (RADIO_STATE_UNAVAILABLE);
3396}
3397
Etan Cohend3652192014-06-20 08:28:44 -07003398/* Called to pass hardware configuration information to telephony
3399 * framework.
3400 */
3401static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3402{
3403 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3404}
3405
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003406static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003407{
3408#ifdef RIL_SHLIB
3409 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3410#else
3411 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3412 exit(-1);
3413#endif
3414}
3415
3416static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003417mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003418{
3419 int fd;
3420 int ret;
3421
3422 AT_DUMP("== ", "entering mainLoop()", -1 );
3423 at_set_on_reader_closed(onATReaderClosed);
3424 at_set_on_timeout(onATTimeout);
3425
3426 for (;;) {
3427 fd = -1;
3428 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003429 if (isInEmulator()) {
3430 fd = qemu_pipe_open("pipe:qemud:gsm");
3431 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003432 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003433 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003434 fd = socket_local_client(s_device_path,
3435 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3436 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003437 } else if (s_device_path != NULL) {
3438 fd = open (s_device_path, O_RDWR);
3439 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3440 /* disable echo on serial ports */
3441 struct termios ios;
3442 tcgetattr( fd, &ios );
3443 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3444 tcsetattr( fd, TCSANOW, &ios );
3445 }
3446 }
3447
3448 if (fd < 0) {
3449 perror ("opening AT interface. retrying...");
3450 sleep(10);
3451 /* never returns */
3452 }
3453 }
3454
3455 s_closed = 0;
3456 ret = at_open(fd, onUnsolicited);
3457
3458 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003459 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003460 return 0;
3461 }
3462
3463 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3464
3465 // Give initializeCallback a chance to dispatched, since
3466 // we don't presently have a cancellation mechanism
3467 sleep(1);
3468
3469 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003470 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003471 }
3472}
3473
3474#ifdef RIL_SHLIB
3475
3476pthread_t s_tid_mainloop;
3477
3478const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3479{
3480 int ret;
3481 int fd = -1;
3482 int opt;
3483 pthread_attr_t attr;
3484
3485 s_rilenv = env;
3486
Sandeep Gutta11f27942014-07-10 05:00:25 +05303487 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003488 switch (opt) {
3489 case 'p':
3490 s_port = atoi(optarg);
3491 if (s_port == 0) {
3492 usage(argv[0]);
3493 return NULL;
3494 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003495 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003496 break;
3497
3498 case 'd':
3499 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003500 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003501 break;
3502
3503 case 's':
3504 s_device_path = optarg;
3505 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003506 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003507 break;
3508
Sandeep Gutta11f27942014-07-10 05:00:25 +05303509 case 'c':
3510 RLOGI("Client id received %s\n", optarg);
3511 break;
3512
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003513 default:
3514 usage(argv[0]);
3515 return NULL;
3516 }
3517 }
3518
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003519 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003520 usage(argv[0]);
3521 return NULL;
3522 }
3523
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003524 sMdmInfo = calloc(1, sizeof(ModemInfo));
3525 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003526 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003527 return NULL;
3528 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003529 pthread_attr_init (&attr);
3530 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3531 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3532
3533 return &s_callbacks;
3534}
3535#else /* RIL_SHLIB */
3536int main (int argc, char **argv)
3537{
3538 int ret;
3539 int fd = -1;
3540 int opt;
3541
3542 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3543 switch (opt) {
3544 case 'p':
3545 s_port = atoi(optarg);
3546 if (s_port == 0) {
3547 usage(argv[0]);
3548 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003549 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003550 break;
3551
3552 case 'd':
3553 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003554 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003555 break;
3556
3557 case 's':
3558 s_device_path = optarg;
3559 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003560 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003561 break;
3562
3563 default:
3564 usage(argv[0]);
3565 }
3566 }
3567
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003568 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003569 usage(argv[0]);
3570 }
3571
3572 RIL_register(&s_callbacks);
3573
3574 mainLoop(NULL);
3575
3576 return 0;
3577}
3578
3579#endif /* RIL_SHLIB */