blob: 42fa254d438f5ccdefd640ad3dd5f2911764531d [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
609 /* There is only on gateway in the emulator */
610 responses[i].gateways = "10.0.2.2";
611 responses[i].mtu = DEFAULT_MTU;
612 }
613 else {
614 /* I don't know where we are, so use the public Google DNS
615 * servers by default and no gateway.
616 */
617 responses[i].dnses = "8.8.8.8 8.8.4.4";
618 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100619 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800620 }
621
622 at_response_free(p_response);
623
624 if (t != NULL)
625 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700626 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800627 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700628 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800629 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700630 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800631
632 return;
633
634error:
635 if (t != NULL)
636 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
637 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700638 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800639 NULL, 0);
640
641 at_response_free(p_response);
642}
643
644static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700645 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800646{
647 int err;
648 ATResponse *p_response = NULL;
649 int response = 0;
650 char *line;
651
652 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
653
654 if (err < 0 || p_response->success == 0) {
655 goto error;
656 }
657
658 line = p_response->p_intermediates->line;
659
660 err = at_tok_start(&line);
661
662 if (err < 0) {
663 goto error;
664 }
665
666 err = at_tok_nextint(&line, &response);
667
668 if (err < 0) {
669 goto error;
670 }
671
672 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
673 at_response_free(p_response);
674 return;
675error:
676 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800677 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800678 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
679}
680
Mark Salyzynba58c202014-03-12 15:20:22 -0700681static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800682{
683 RIL_onUnsolicitedResponse (
684 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
685 NULL, 0);
686}
687
Mark Salyzynba58c202014-03-12 15:20:22 -0700688static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800689{
690 int err;
691 ATResponse *p_response;
692 ATLine *p_cur;
693 int countCalls;
694 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700695 RIL_Call *p_calls;
696 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800697 int i;
698 int needRepoll = 0;
699
700#ifdef WORKAROUND_ERRONEOUS_ANSWER
701 int prevIncomingOrWaitingLine;
702
703 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
704 s_incomingOrWaitingLine = -1;
705#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
706
707 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
708
709 if (err != 0 || p_response->success == 0) {
710 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
711 return;
712 }
713
714 /* count the calls */
715 for (countCalls = 0, p_cur = p_response->p_intermediates
716 ; p_cur != NULL
717 ; p_cur = p_cur->p_next
718 ) {
719 countCalls++;
720 }
721
722 /* yes, there's an array of pointers and then an array of structures */
723
Wink Saville3d54e742009-05-18 18:00:44 -0700724 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
725 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
726 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800727
728 /* init the pointer array */
729 for(i = 0; i < countCalls ; i++) {
730 pp_calls[i] = &(p_calls[i]);
731 }
732
733 for (countValidCalls = 0, p_cur = p_response->p_intermediates
734 ; p_cur != NULL
735 ; p_cur = p_cur->p_next
736 ) {
737 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
738
739 if (err != 0) {
740 continue;
741 }
742
743#ifdef WORKAROUND_ERRONEOUS_ANSWER
744 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
745 || p_calls[countValidCalls].state == RIL_CALL_WAITING
746 ) {
747 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
748 }
749#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
750
751 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
752 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
753 ) {
754 needRepoll = 1;
755 }
756
757 countValidCalls++;
758 }
759
760#ifdef WORKAROUND_ERRONEOUS_ANSWER
761 // Basically:
762 // A call was incoming or waiting
763 // Now it's marked as active
764 // But we never answered it
765 //
766 // This is probably a bug, and the call will probably
767 // disappear from the call list in the next poll
768 if (prevIncomingOrWaitingLine >= 0
769 && s_incomingOrWaitingLine < 0
770 && s_expectAnswer == 0
771 ) {
772 for (i = 0; i < countValidCalls ; i++) {
773
774 if (p_calls[i].index == prevIncomingOrWaitingLine
775 && p_calls[i].state == RIL_CALL_ACTIVE
776 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
777 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800778 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800779 "Hit WORKAROUND_ERRONOUS_ANSWER case."
780 " Repoll count: %d\n", s_repollCallsCount);
781 s_repollCallsCount++;
782 goto error;
783 }
784 }
785 }
786
787 s_expectAnswer = 0;
788 s_repollCallsCount = 0;
789#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
790
Wink Saville3d54e742009-05-18 18:00:44 -0700791 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
792 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800793
794 at_response_free(p_response);
795
796#ifdef POLL_CALL_STATE
797 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
798 // smd, so we're forced to poll until the call ends.
799#else
800 if (needRepoll) {
801#endif
802 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
803 }
804
805 return;
806error:
807 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
808 at_response_free(p_response);
809}
810
Mark Salyzynba58c202014-03-12 15:20:22 -0700811static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800812{
813 RIL_Dial *p_dial;
814 char *cmd;
815 const char *clir;
816 int ret;
817
818 p_dial = (RIL_Dial *)data;
819
820 switch (p_dial->clir) {
821 case 1: clir = "I"; break; /*invocation*/
822 case 2: clir = "i"; break; /*suppression*/
823 default:
824 case 0: clir = ""; break; /*subscription default*/
825 }
826
827 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
828
829 ret = at_send_command(cmd, NULL);
830
831 free(cmd);
832
833 /* success or failure is ignored by the upper layer here.
834 it will call GET_CURRENT_CALLS and determine success that way */
835 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
836}
837
Mark Salyzynba58c202014-03-12 15:20:22 -0700838static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800839{
840 RIL_SMS_WriteArgs *p_args;
841 char *cmd;
842 int length;
843 int err;
844 ATResponse *p_response = NULL;
845
846 p_args = (RIL_SMS_WriteArgs *)data;
847
848 length = strlen(p_args->pdu)/2;
849 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
850
851 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
852
853 if (err != 0 || p_response->success == 0) goto error;
854
855 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
856 at_response_free(p_response);
857
858 return;
859error:
860 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
861 at_response_free(p_response);
862}
863
Mark Salyzynba58c202014-03-12 15:20:22 -0700864static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800865{
866 int *p_line;
867
868 int ret;
869 char *cmd;
870
871 p_line = (int *)data;
872
873 // 3GPP 22.030 6.5.5
874 // "Releases a specific active call X"
875 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
876
877 ret = at_send_command(cmd, NULL);
878
879 free(cmd);
880
881 /* success or failure is ignored by the upper layer here.
882 it will call GET_CURRENT_CALLS and determine success that way */
883 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
884}
885
Mark Salyzynba58c202014-03-12 15:20:22 -0700886static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800887{
888 ATResponse *p_response = NULL;
889 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800890 char *line;
Jim Kayedc9f31b2017-04-03 13:43:31 -0700891 int count = 0;
892 // Accept a response that is at least v6, and up to v10
893 int minNumOfElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
894 int maxNumOfElements=sizeof(RIL_SignalStrength_v10)/sizeof(int);
895 int response[maxNumOfElements];
896
897 memset(response, 0, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800898
899 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
900
901 if (err < 0 || p_response->success == 0) {
902 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
903 goto error;
904 }
905
906 line = p_response->p_intermediates->line;
907
908 err = at_tok_start(&line);
909 if (err < 0) goto error;
910
Jim Kayedc9f31b2017-04-03 13:43:31 -0700911 for (count = 0; count < maxNumOfElements; count++) {
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700912 err = at_tok_nextint(&line, &(response[count]));
Jim Kayedc9f31b2017-04-03 13:43:31 -0700913 if (err < 0 && count < minNumOfElements) goto error;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700914 }
Chih-Wei Huang28059052012-04-30 01:13:27 +0800915
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700916 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800917
918 at_response_free(p_response);
919 return;
920
921error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800922 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800923 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
924 at_response_free(p_response);
925}
926
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700927/**
928 * networkModePossible. Decides whether the network mode is appropriate for the
929 * specified modem
930 */
931static int networkModePossible(ModemInfo *mdm, int nm)
932{
933 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
934 return 1;
935 }
936 return 0;
937}
Mark Salyzynba58c202014-03-12 15:20:22 -0700938static void requestSetPreferredNetworkType( int request __unused, void *data,
939 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700940{
941 ATResponse *p_response = NULL;
942 char *cmd = NULL;
943 int value = *(int *)data;
944 int current, old;
945 int err;
946 int32_t preferred = net2pmask[value];
947
Wink Saville4dcab4f2012-11-19 16:05:13 -0800948 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700949 if (!networkModePossible(sMdmInfo, value)) {
950 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
951 return;
952 }
953 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
954 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
955 return;
956 }
957 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800958 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700959 if (old != preferred) {
960 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800961 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700962 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
963 free(cmd);
964 if (err || !p_response->success) {
965 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
966 return;
967 }
968 PREFERRED_NETWORK(sMdmInfo) = value;
969 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
970 int current;
971 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
972 switch (res) {
973 case -1: // Error or unable to parse
974 break;
975 case 1: // Only able to parse current
976 case 0: // Both current and preferred were parsed
977 setRadioTechnology(sMdmInfo, current);
978 break;
979 }
980 }
981 }
982 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
983}
984
Mark Salyzynba58c202014-03-12 15:20:22 -0700985static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
986 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700987{
988 int preferred;
989 unsigned i;
990
991 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
992 case -1: // Error or unable to parse
993 case 1: // Only able to parse current
994 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
995 break;
996 case 0: // Both current and preferred were parsed
997 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
998 if (preferred == net2pmask[i]) {
999 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1000 return;
1001 }
1002 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001003 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001004 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1005 break;
1006 }
1007
1008}
1009
Mark Salyzynba58c202014-03-12 15:20:22 -07001010static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1011 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001012{
1013 int err;
1014 char * responseStr;
1015 ATResponse *p_response = NULL;
1016 const char *cmd;
1017 char *line;
1018
1019 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1020 if (err < 0 || !p_response->success) goto error;
1021 line = p_response->p_intermediates->line;
1022 err = at_tok_start(&line);
1023 if (err < 0) goto error;
1024 err = at_tok_nextstr(&line, &responseStr);
1025 if (err < 0 || !responseStr) goto error;
1026 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1027 at_response_free(p_response);
1028 return;
1029error:
1030 at_response_free(p_response);
1031 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1032}
1033
Mark Salyzynba58c202014-03-12 15:20:22 -07001034static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1035 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001036{
1037 int err;
1038 char * responseStr;
1039 ATResponse *p_response = NULL;
1040 const char *cmd;
1041 const char *prefix;
1042 char *line, *p;
1043 int commas;
1044 int skip;
1045 int count = 4;
1046
1047 // Fixed values. TODO: query modem
1048 responseStr = strdup("1.0.0.0");
1049 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1050 free(responseStr);
1051}
1052
Mark Salyzynba58c202014-03-12 15:20:22 -07001053static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1054 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001055{
1056 int err;
1057 int response[4];
1058 char * responseStr[4];
1059 ATResponse *p_response = NULL;
1060 const char *cmd;
1061 const char *prefix;
1062 char *line, *p;
1063 int commas;
1064 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001065 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001066
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001067 // Fixed values. TODO: Query modem
1068 responseStr[0] = "----";
1069 responseStr[1] = "----";
1070 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001071
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001072 err = at_send_command_numeric("AT+CGSN", &p_response);
1073 if (err < 0 || p_response->success == 0) {
1074 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1075 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001076 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001077 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001078 }
1079
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001080 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1081 at_response_free(p_response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001082
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001083 return;
1084error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001085 RLOGE("requestCdmaDeviceIdentity must never return an error when radio is on");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001086 at_response_free(p_response);
1087 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1088}
1089
Mark Salyzynba58c202014-03-12 15:20:22 -07001090static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1091 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001092{
1093 int err;
1094 int *ss = (int *)data;
1095 ATResponse *p_response = NULL;
1096 char *cmd = NULL;
1097 char *line = NULL;
1098 int response;
1099
1100 asprintf(&cmd, "AT+CCSS?");
1101 if (!cmd) goto error;
1102
1103 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1104 if (err < 0 || !p_response->success)
1105 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001106
1107 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001108 err = at_tok_start(&line);
1109 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001110
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001111 err = at_tok_nextint(&line, &response);
1112 free(cmd);
1113 cmd = NULL;
1114
1115 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1116
1117 return;
1118error:
1119 free(cmd);
1120 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1121}
1122
Mark Salyzynba58c202014-03-12 15:20:22 -07001123static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001124 size_t datalen, RIL_Token t)
1125{
1126 int err;
1127 int *ss = (int *)data;
1128 ATResponse *p_response = NULL;
1129 char *cmd = NULL;
1130
1131 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001132 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001133 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1134 return;
1135 }
1136 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1137 if (!cmd) goto error;
1138
1139 err = at_send_command(cmd, &p_response);
1140 if (err < 0 || !p_response->success)
1141 goto error;
1142 free(cmd);
1143 cmd = NULL;
1144
1145 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1146
1147 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1148
1149 return;
1150error:
1151 free(cmd);
1152 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1153}
1154
Mark Salyzynba58c202014-03-12 15:20:22 -07001155static void requestCdmaSubscription(int request __unused, void *data __unused,
1156 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001157{
1158 int err;
1159 int response[5];
1160 char * responseStr[5];
1161 ATResponse *p_response = NULL;
1162 const char *cmd;
1163 const char *prefix;
1164 char *line, *p;
1165 int commas;
1166 int skip;
1167 int count = 5;
1168
1169 // Fixed values. TODO: Query modem
1170 responseStr[0] = "8587777777"; // MDN
1171 responseStr[1] = "1"; // SID
1172 responseStr[2] = "1"; // NID
1173 responseStr[3] = "8587777777"; // MIN
1174 responseStr[4] = "1"; // PRL Version
1175 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1176
1177 return;
1178error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001179 RLOGE("requestRegistrationState must never return an error when radio is on");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001180 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1181}
1182
Mark Salyzynba58c202014-03-12 15:20:22 -07001183static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1184 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001185{
1186 int roaming_pref = -1;
1187 ATResponse *p_response = NULL;
1188 char *line;
1189 int res;
1190
1191 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1192 if (res < 0 || !p_response->success) {
1193 goto error;
1194 }
1195 line = p_response->p_intermediates->line;
1196
1197 res = at_tok_start(&line);
1198 if (res < 0) goto error;
1199
1200 res = at_tok_nextint(&line, &roaming_pref);
1201 if (res < 0) goto error;
1202
1203 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1204 return;
1205error:
1206 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1207}
1208
Mark Salyzynba58c202014-03-12 15:20:22 -07001209static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1210 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001211{
1212 int *pref = (int *)data;
1213 ATResponse *p_response = NULL;
1214 char *line;
1215 int res;
1216 char *cmd = NULL;
1217
1218 asprintf(&cmd, "AT+WRMP=%d", *pref);
1219 if (cmd == NULL) goto error;
1220
1221 res = at_send_command(cmd, &p_response);
1222 if (res < 0 || !p_response->success)
1223 goto error;
1224
1225 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1226 free(cmd);
1227 return;
1228error:
1229 free(cmd);
1230 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1231}
1232
1233static int parseRegistrationState(char *str, int *type, int *items, int **response)
1234{
1235 int err;
1236 char *line = str, *p;
1237 int *resp = NULL;
1238 int skip;
1239 int count = 3;
1240 int commas;
1241
Wink Saville4dcab4f2012-11-19 16:05:13 -08001242 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001243 err = at_tok_start(&line);
1244 if (err < 0) goto error;
1245
1246 /* Ok you have to be careful here
1247 * The solicited version of the CREG response is
1248 * +CREG: n, stat, [lac, cid]
1249 * and the unsolicited version is
1250 * +CREG: stat, [lac, cid]
1251 * The <n> parameter is basically "is unsolicited creg on?"
1252 * which it should always be
1253 *
1254 * Now we should normally get the solicited version here,
1255 * but the unsolicited version could have snuck in
1256 * so we have to handle both
1257 *
1258 * Also since the LAC and CID are only reported when registered,
1259 * we can have 1, 2, 3, or 4 arguments here
1260 *
1261 * finally, a +CGREG: answer may have a fifth value that corresponds
1262 * to the network type, as in;
1263 *
1264 * +CGREG: n, stat [,lac, cid [,networkType]]
1265 */
1266
1267 /* count number of commas */
1268 commas = 0;
1269 for (p = line ; *p != '\0' ;p++) {
1270 if (*p == ',') commas++;
1271 }
1272
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001273 resp = (int *)calloc(commas + 1, sizeof(int));
1274 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001275 switch (commas) {
1276 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001277 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001278 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001279 resp[1] = -1;
1280 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001281 break;
1282
1283 case 1: /* +CREG: <n>, <stat> */
1284 err = at_tok_nextint(&line, &skip);
1285 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001286 err = at_tok_nextint(&line, &resp[0]);
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 resp[1] = -1;
1289 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001290 if (err < 0) goto error;
1291 break;
1292
1293 case 2: /* +CREG: <stat>, <lac>, <cid> */
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 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1302 err = at_tok_nextint(&line, &skip);
1303 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001304 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001305 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001306 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001307 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001308 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001309 if (err < 0) goto error;
1310 break;
1311 /* special case for CGREG, there is a fourth parameter
1312 * that is the network type (unknown/gprs/edge/umts)
1313 */
1314 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1315 err = at_tok_nextint(&line, &skip);
1316 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001317 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001318 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001319 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001320 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001321 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001322 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001323 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001324 if (err < 0) goto error;
1325 count = 4;
1326 break;
1327 default:
1328 goto error;
1329 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001330 s_lac = resp[1];
1331 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001332 if (response)
1333 *response = resp;
1334 if (items)
1335 *items = commas + 1;
1336 if (type)
1337 *type = techFromModemType(TECH(sMdmInfo));
1338 return 0;
1339error:
1340 free(resp);
1341 return -1;
1342}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001343
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001344#define REG_STATE_LEN 15
1345#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001346static void requestRegistrationState(int request, void *data __unused,
1347 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001348{
1349 int err;
1350 int *registration;
1351 char **responseStr = NULL;
1352 ATResponse *p_response = NULL;
1353 const char *cmd;
1354 const char *prefix;
1355 char *line;
1356 int i = 0, j, numElements = 0;
1357 int count = 3;
1358 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001359
Wink Saville4dcab4f2012-11-19 16:05:13 -08001360 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001361 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1362 cmd = "AT+CREG?";
1363 prefix = "+CREG:";
1364 numElements = REG_STATE_LEN;
1365 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1366 cmd = "AT+CGREG?";
1367 prefix = "+CGREG:";
1368 numElements = REG_DATA_STATE_LEN;
1369 } else {
1370 assert(0);
1371 goto error;
1372 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001373
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001374 err = at_send_command_singleline(cmd, prefix, &p_response);
1375
1376 if (err != 0) goto error;
1377
1378 line = p_response->p_intermediates->line;
1379
1380 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1381
1382 responseStr = malloc(numElements * sizeof(char *));
1383 if (!responseStr) goto error;
1384 memset(responseStr, 0, numElements * sizeof(char *));
1385 /**
1386 * The first '4' bytes for both registration states remain the same.
1387 * But if the request is 'DATA_REGISTRATION_STATE',
1388 * the 5th and 6th byte(s) are optional.
1389 */
1390 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001391 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001392 // TODO: Query modem
1393 startfrom = 3;
1394 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1395 asprintf(&responseStr[3], "8"); // EvDo revA
1396 asprintf(&responseStr[4], "1"); // BSID
1397 asprintf(&responseStr[5], "123"); // Latitude
1398 asprintf(&responseStr[6], "222"); // Longitude
1399 asprintf(&responseStr[7], "0"); // CSS Indicator
1400 asprintf(&responseStr[8], "4"); // SID
1401 asprintf(&responseStr[9], "65535"); // NID
1402 asprintf(&responseStr[10], "0"); // Roaming indicator
1403 asprintf(&responseStr[11], "1"); // System is in PRL
1404 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1405 asprintf(&responseStr[13], "0"); // Reason for denial
1406 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1407 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1408 asprintf(&responseStr[3], "8"); // Available data radio technology
1409 }
1410 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001411 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001412 startfrom = 0;
1413 asprintf(&responseStr[1], "%x", registration[1]);
1414 asprintf(&responseStr[2], "%x", registration[2]);
1415 if (count > 3)
1416 asprintf(&responseStr[3], "%d", registration[3]);
1417 }
1418 asprintf(&responseStr[0], "%d", registration[0]);
1419
1420 /**
1421 * Optional bytes for DATA_REGISTRATION_STATE request
1422 * 4th byte : Registration denial code
1423 * 5th byte : The max. number of simultaneous Data Calls
1424 */
1425 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1426 // asprintf(&responseStr[4], "3");
1427 // asprintf(&responseStr[5], "1");
1428 }
1429
1430 for (j = startfrom; j < numElements; j++) {
1431 if (!responseStr[i]) goto error;
1432 }
1433 free(registration);
1434 registration = NULL;
1435
1436 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1437 for (j = 0; j < numElements; j++ ) {
1438 free(responseStr[j]);
1439 responseStr[j] = NULL;
1440 }
1441 free(responseStr);
1442 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001443 at_response_free(p_response);
1444
1445 return;
1446error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001447 if (responseStr) {
1448 for (j = 0; j < numElements; j++) {
1449 free(responseStr[j]);
1450 responseStr[j] = NULL;
1451 }
1452 free(responseStr);
1453 responseStr = NULL;
1454 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001455 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001456 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1457 at_response_free(p_response);
1458}
1459
Mark Salyzynba58c202014-03-12 15:20:22 -07001460static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001461{
1462 int err;
1463 int i;
1464 int skip;
1465 ATLine *p_cur;
1466 char *response[3];
1467
1468 memset(response, 0, sizeof(response));
1469
1470 ATResponse *p_response = NULL;
1471
1472 err = at_send_command_multiline(
1473 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1474 "+COPS:", &p_response);
1475
1476 /* we expect 3 lines here:
1477 * +COPS: 0,0,"T - Mobile"
1478 * +COPS: 0,1,"TMO"
1479 * +COPS: 0,2,"310170"
1480 */
1481
1482 if (err != 0) goto error;
1483
1484 for (i = 0, p_cur = p_response->p_intermediates
1485 ; p_cur != NULL
1486 ; p_cur = p_cur->p_next, i++
1487 ) {
1488 char *line = p_cur->line;
1489
1490 err = at_tok_start(&line);
1491 if (err < 0) goto error;
1492
1493 err = at_tok_nextint(&line, &skip);
1494 if (err < 0) goto error;
1495
1496 // If we're unregistered, we may just get
1497 // a "+COPS: 0" response
1498 if (!at_tok_hasmore(&line)) {
1499 response[i] = NULL;
1500 continue;
1501 }
1502
1503 err = at_tok_nextint(&line, &skip);
1504 if (err < 0) goto error;
1505
1506 // a "+COPS: 0, n" response is also possible
1507 if (!at_tok_hasmore(&line)) {
1508 response[i] = NULL;
1509 continue;
1510 }
1511
1512 err = at_tok_nextstr(&line, &(response[i]));
1513 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001514 // Simple assumption that mcc and mnc are 3 digits each
1515 if (strlen(response[i]) == 6) {
1516 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1517 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1518 }
1519 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001520 }
1521
1522 if (i != 3) {
1523 /* expect 3 lines exactly */
1524 goto error;
1525 }
1526
1527 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1528 at_response_free(p_response);
1529
1530 return;
1531error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001532 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001533 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1534 at_response_free(p_response);
1535}
1536
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001537static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1538{
1539 int err = 1; // Set to go to error:
1540 RIL_SMS_Response response;
1541 RIL_CDMA_SMS_Message* rcsm;
1542
Mark Salyzynba58c202014-03-12 15:20:22 -07001543 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001544 datalen, sizeof(RIL_CDMA_SMS_Message));
1545
1546 // verify data content to test marshalling/unmarshalling:
1547 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001548 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001549 uServicecategory=%d, sAddress.digit_mode=%d, \
1550 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1551 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1552 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1553 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1554
1555 if (err != 0) goto error;
1556
1557 // Cdma Send SMS implementation will go here:
1558 // But it is not implemented yet.
1559
1560 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001561 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001562 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1563 return;
1564
1565error:
1566 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001567 response.messageRef = -1;
1568 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001569}
1570
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001571static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1572{
1573 int err;
1574 const char *smsc;
1575 const char *pdu;
1576 int tpLayerLength;
1577 char *cmd1, *cmd2;
1578 RIL_SMS_Response response;
1579 ATResponse *p_response = NULL;
1580
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001581 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001582 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001583
1584 if (s_ims_gsm_fail != 0) goto error;
1585 if (s_ims_gsm_retry != 0) goto error2;
1586
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001587 smsc = ((const char **)data)[0];
1588 pdu = ((const char **)data)[1];
1589
1590 tpLayerLength = strlen(pdu)/2;
1591
1592 // "NULL for default SMSC"
1593 if (smsc == NULL) {
1594 smsc= "00";
1595 }
1596
1597 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1598 asprintf(&cmd2, "%s%s", smsc, pdu);
1599
1600 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1601
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001602 free(cmd1);
1603 free(cmd2);
1604
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001605 if (err != 0 || p_response->success == 0) goto error;
1606
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001607 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001608 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001609 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1610 at_response_free(p_response);
1611
1612 return;
1613error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001614 response.messageRef = -2;
1615 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001616 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001617 return;
1618error2:
1619 // send retry error.
1620 response.messageRef = -1;
1621 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1622 at_response_free(p_response);
1623 return;
1624 }
1625
1626static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1627{
1628 RIL_IMS_SMS_Message *p_args;
1629 RIL_SMS_Response response;
1630
1631 memset(&response, 0, sizeof(response));
1632
Mark Salyzynba58c202014-03-12 15:20:22 -07001633 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001634 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1635 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1636 datalen, s_ims_registered, s_ims_services, s_ims_format,
1637 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1638 s_ims_gsm_retry);
1639
1640 // figure out if this is gsm/cdma format
1641 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1642 p_args = (RIL_IMS_SMS_Message *)data;
1643
1644 if (0 != s_ims_cause_perm_failure ) goto error;
1645
1646 // want to fail over ims and this is first request over ims
1647 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1648
1649 if (RADIO_TECH_3GPP == p_args->tech) {
1650 return requestSendSMS(p_args->message.gsmMessage,
1651 datalen - sizeof(RIL_RadioTechnologyFamily),
1652 t);
1653 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1654 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1655 datalen - sizeof(RIL_RadioTechnologyFamily),
1656 t);
1657 } else {
1658 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1659 }
1660
1661error:
1662 response.messageRef = -2;
1663 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1664 return;
1665
1666error2:
1667 response.messageRef = -1;
1668 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001669}
1670
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001671static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1672{
1673 ATResponse *p_response = NULL;
1674 int32_t session_id;
1675 int err;
1676 char cmd[32];
1677 char dummy;
1678 char *line;
1679
1680 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1681 if (data == NULL || datalen == 0 || datalen > 16) {
1682 ALOGE("Invalid data passed to requestSimOpenChannel");
1683 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1684 return;
1685 }
1686
1687 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1688
1689 err = at_send_command_numeric(cmd, &p_response);
1690 if (err < 0 || p_response == NULL || p_response->success == 0) {
1691 ALOGE("Error %d opening logical channel: %d",
1692 err, p_response ? p_response->success : 0);
1693 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1694 at_response_free(p_response);
1695 return;
1696 }
1697
1698 // Ensure integer only by scanning for an extra char but expect one result
1699 line = p_response->p_intermediates->line;
1700 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1701 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1702 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1703 return;
1704 }
1705
1706 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1707 at_response_free(p_response);
1708}
1709
1710static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1711{
1712 ATResponse *p_response = NULL;
1713 int32_t session_id;
1714 int err;
1715 char cmd[32];
1716
1717 if (data == NULL || datalen != sizeof(session_id)) {
1718 ALOGE("Invalid data passed to requestSimCloseChannel");
1719 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1720 return;
1721 }
1722 session_id = ((int32_t *)data)[0];
1723 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1724 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1725
1726 if (err < 0 || p_response == NULL || p_response->success == 0) {
1727 ALOGE("Error %d closing logical channel %d: %d",
1728 err, session_id, p_response ? p_response->success : 0);
1729 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1730 at_response_free(p_response);
1731 return;
1732 }
1733
1734 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1735
1736 at_response_free(p_response);
1737}
1738
1739static void requestSimTransmitApduChannel(void *data,
1740 size_t datalen,
1741 RIL_Token t)
1742{
1743 ATResponse *p_response = NULL;
1744 int err;
1745 char *cmd;
1746 char *line;
1747 size_t cmd_size;
1748 RIL_SIM_IO_Response sim_response;
1749 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1750
1751 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1752 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1753 return;
1754 }
1755
1756 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
Wei Wang9cec1e02017-02-08 14:37:37 -08001757 asprintf(&cmd, "AT+CGLA=%d,%zu,%02x%02x%02x%02x%02x%s",
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001758 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1759 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1760
1761 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1762 free(cmd);
1763 if (err < 0 || p_response == NULL || p_response->success == 0) {
1764 ALOGE("Error %d transmitting APDU: %d",
1765 err, p_response ? p_response->success : 0);
1766 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1767 at_response_free(p_response);
1768 return;
1769 }
1770
1771 line = p_response->p_intermediates->line;
1772 err = parseSimResponseLine(line, &sim_response);
1773
1774 if (err == 0) {
1775 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1776 &sim_response, sizeof(sim_response));
1777 } else {
1778 ALOGE("Error %d parsing SIM response line: %s", err, line);
1779 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1780 }
1781 at_response_free(p_response);
1782}
1783
Wink Savillef4c4d362009-04-02 01:37:03 -07001784static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001785{
1786 const char *apn;
1787 char *cmd;
1788 int err;
1789 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001790
Wink Savillef4c4d362009-04-02 01:37:03 -07001791 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001792
1793#ifdef USE_TI_COMMANDS
1794 // Config for multislot class 10 (probably default anyway eh?)
1795 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1796 NULL);
1797
1798 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1799#endif /* USE_TI_COMMANDS */
1800
1801 int fd, qmistatus;
1802 size_t cur = 0;
1803 size_t len;
1804 ssize_t written, rlen;
1805 char status[32] = {0};
1806 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001807 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001808
Wink Saville4dcab4f2012-11-19 16:05:13 -08001809 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001810
1811 fd = open ("/dev/qmi", O_RDWR);
1812 if (fd >= 0) { /* the device doesn't exist on the emulator */
1813
Wink Saville4dcab4f2012-11-19 16:05:13 -08001814 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001815 asprintf(&cmd, "up:%s", apn);
1816 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001817
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001818 while (cur < len) {
1819 do {
1820 written = write (fd, cmd + cur, len - cur);
1821 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001822
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001823 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001824 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001825 close(fd);
1826 goto error;
1827 }
1828
1829 cur += written;
1830 }
1831
1832 // wait for interface to come online
1833
1834 do {
1835 sleep(1);
1836 do {
1837 rlen = read(fd, status, 31);
1838 } while (rlen < 0 && errno == EINTR);
1839
1840 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001841 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001842 close(fd);
1843 goto error;
1844 } else {
1845 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001846 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001847 }
1848 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1849
1850 close(fd);
1851
1852 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001853 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001854 goto error;
1855 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001856
1857 qmistatus = system("netcfg rmnet0 dhcp");
1858
Wink Saville4dcab4f2012-11-19 16:05:13 -08001859 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001860
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001861 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001862
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001863 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001864
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001865 if (datalen > 6 * sizeof(char *)) {
1866 pdp_type = ((const char **)data)[6];
1867 } else {
1868 pdp_type = "IP";
1869 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001870
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001871 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001872 //FIXME check for error here
1873 err = at_send_command(cmd, NULL);
1874 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001875
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001876 // Set required QoS params to default
1877 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001878
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001879 // Set minimum QoS params to default
1880 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001881
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001882 // packet-domain event reporting
1883 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001884
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001885 // Hangup anything that's happening there now
1886 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001887
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001888 // Start data on PDP context 1
1889 err = at_send_command("ATD*99***1#", &p_response);
1890
1891 if (err < 0 || p_response->success == 0) {
1892 goto error;
1893 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001894 }
1895
Wink Saville43808972011-01-13 17:39:51 -08001896 requestOrSendDataCallList(&t);
1897
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001898 at_response_free(p_response);
1899
1900 return;
1901error:
1902 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1903 at_response_free(p_response);
1904
1905}
1906
Mark Salyzynba58c202014-03-12 15:20:22 -07001907static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001908{
1909 int ackSuccess;
1910 int err;
1911
1912 ackSuccess = ((int *)data)[0];
1913
1914 if (ackSuccess == 1) {
1915 err = at_send_command("AT+CNMA=1", NULL);
1916 } else if (ackSuccess == 0) {
1917 err = at_send_command("AT+CNMA=2", NULL);
1918 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001919 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001920 goto error;
1921 }
1922
1923 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1924error:
1925 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1926
1927}
1928
Mark Salyzynba58c202014-03-12 15:20:22 -07001929static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001930{
1931 ATResponse *p_response = NULL;
1932 RIL_SIM_IO_Response sr;
1933 int err;
1934 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001935 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001936 char *line;
1937
1938 memset(&sr, 0, sizeof(sr));
1939
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001940 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001941
1942 /* FIXME handle pin2 */
1943
1944 if (p_args->data == NULL) {
1945 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1946 p_args->command, p_args->fileid,
1947 p_args->p1, p_args->p2, p_args->p3);
1948 } else {
1949 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1950 p_args->command, p_args->fileid,
1951 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1952 }
1953
1954 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1955
1956 if (err < 0 || p_response->success == 0) {
1957 goto error;
1958 }
1959
1960 line = p_response->p_intermediates->line;
1961
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001962 err = parseSimResponseLine(line, &sr);
1963 if (err < 0) {
1964 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001965 }
1966
1967 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1968 at_response_free(p_response);
1969 free(cmd);
1970
1971 return;
1972error:
1973 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1974 at_response_free(p_response);
1975 free(cmd);
1976
1977}
1978
1979static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1980{
1981 ATResponse *p_response = NULL;
1982 int err;
1983 char* cmd = NULL;
1984 const char** strings = (const char**)data;;
1985
1986 if ( datalen == sizeof(char*) ) {
1987 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1988 } else if ( datalen == 2*sizeof(char*) ) {
1989 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1990 } else
1991 goto error;
1992
1993 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1994 free(cmd);
1995
1996 if (err < 0 || p_response->success == 0) {
1997error:
1998 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1999 } else {
2000 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2001 }
2002 at_response_free(p_response);
2003}
2004
2005
Mark Salyzynba58c202014-03-12 15:20:22 -07002006static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002007{
2008 const char *ussdRequest;
2009
2010 ussdRequest = (char *)(data);
2011
2012
2013 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2014
2015// @@@ TODO
2016
2017}
2018
Mark Salyzynba58c202014-03-12 15:20:22 -07002019static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002020{
2021 int err;
2022 ATResponse *p_response = NULL;
2023
2024 err = at_send_command("AT+WSOS=0", &p_response);
2025
2026 if (err < 0 || p_response->success == 0) {
2027 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2028 return;
2029 }
2030
2031 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2032}
2033
2034// TODO: Use all radio types
2035static int techFromModemType(int mdmtype)
2036{
2037 int ret = -1;
2038 switch (1 << mdmtype) {
2039 case MDM_CDMA:
2040 ret = RADIO_TECH_1xRTT;
2041 break;
2042 case MDM_EVDO:
2043 ret = RADIO_TECH_EVDO_A;
2044 break;
2045 case MDM_GSM:
2046 ret = RADIO_TECH_GPRS;
2047 break;
2048 case MDM_WCDMA:
2049 ret = RADIO_TECH_HSPA;
2050 break;
2051 case MDM_LTE:
2052 ret = RADIO_TECH_LTE;
2053 break;
2054 }
2055 return ret;
2056}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002057
Mark Salyzynba58c202014-03-12 15:20:22 -07002058static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002059{
2060 uint64_t curTime = ril_nano_time();
2061 RIL_CellInfo ci[1] =
2062 {
2063 { // ci[0]
2064 1, // cellInfoType
2065 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002066 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002067 curTime - 1000, // Fake some time in the past
2068 { // union CellInfo
2069 { // RIL_CellInfoGsm gsm
2070 { // gsm.cellIdneityGsm
2071 s_mcc, // mcc
2072 s_mnc, // mnc
2073 s_lac, // lac
2074 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002075 },
2076 { // gsm.signalStrengthGsm
2077 10, // signalStrength
2078 0 // bitErrorRate
2079 }
2080 }
2081 }
2082 }
2083 };
2084
2085 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2086}
2087
2088
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002089static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002090{
2091 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2092 // will be sent.
2093 assert (datalen == sizeof(int));
2094 s_cell_info_rate_ms = ((int *)data)[0];
2095
2096 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2097}
2098
Etan Cohend3652192014-06-20 08:28:44 -07002099static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2100{
2101 // TODO - hook this up with real query/info from radio.
2102
2103 RIL_HardwareConfig hwCfg;
2104
2105 RIL_UNUSED_PARM(data);
2106 RIL_UNUSED_PARM(datalen);
2107
2108 hwCfg.type = -1;
2109
2110 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2111}
2112
2113
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002114/*** Callback methods from the RIL library to us ***/
2115
2116/**
2117 * Call from RIL to us to make a RIL_REQUEST
2118 *
2119 * Must be completed with a call to RIL_onRequestComplete()
2120 *
2121 * RIL_onRequestComplete() may be called from any thread, before or after
2122 * this function returns.
2123 *
Weilun Du9f471e22017-02-07 10:47:19 -08002124 * Because onRequest function could be called from multiple different thread,
2125 * we must ensure that the underlying at_send_command_* function
2126 * is atomic.
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002127 */
2128static void
2129onRequest (int request, void *data, size_t datalen, RIL_Token t)
2130{
2131 ATResponse *p_response;
2132 int err;
2133
Wink Saville4dcab4f2012-11-19 16:05:13 -08002134 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002135
2136 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2137 * when RADIO_STATE_UNAVAILABLE.
2138 */
2139 if (sState == RADIO_STATE_UNAVAILABLE
2140 && request != RIL_REQUEST_GET_SIM_STATUS
2141 ) {
2142 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2143 return;
2144 }
2145
2146 /* Ignore all non-power requests when RADIO_STATE_OFF
2147 * (except RIL_REQUEST_GET_SIM_STATUS)
2148 */
2149 if (sState == RADIO_STATE_OFF
2150 && !(request == RIL_REQUEST_RADIO_POWER
2151 || request == RIL_REQUEST_GET_SIM_STATUS)
2152 ) {
2153 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2154 return;
2155 }
2156
2157 switch (request) {
2158 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002159 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002160 char *p_buffer;
2161 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002162
Wink Savillef6aa7c12009-04-30 14:20:52 -07002163 int result = getCardStatus(&p_card_status);
2164 if (result == RIL_E_SUCCESS) {
2165 p_buffer = (char *)p_card_status;
2166 buffer_size = sizeof(*p_card_status);
2167 } else {
2168 p_buffer = NULL;
2169 buffer_size = 0;
2170 }
2171 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2172 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002173 break;
2174 }
2175 case RIL_REQUEST_GET_CURRENT_CALLS:
2176 requestGetCurrentCalls(data, datalen, t);
2177 break;
2178 case RIL_REQUEST_DIAL:
2179 requestDial(data, datalen, t);
2180 break;
2181 case RIL_REQUEST_HANGUP:
2182 requestHangup(data, datalen, t);
2183 break;
2184 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
2185 // 3GPP 22.030 6.5.5
2186 // "Releases all held calls or sets User Determined User Busy
2187 // (UDUB) for a waiting call."
2188 at_send_command("AT+CHLD=0", 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_HANGUP_FOREGROUND_RESUME_BACKGROUND:
2195 // 3GPP 22.030 6.5.5
2196 // "Releases all active calls (if any exist) and accepts
2197 // the other (held or waiting) call."
2198 at_send_command("AT+CHLD=1", NULL);
2199
2200 /* success or failure is ignored by the upper layer here.
2201 it will call GET_CURRENT_CALLS and determine success that way */
2202 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2203 break;
2204 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
2205 // 3GPP 22.030 6.5.5
2206 // "Places all active calls (if any exist) on hold and accepts
2207 // the other (held or waiting) call."
2208 at_send_command("AT+CHLD=2", NULL);
2209
2210#ifdef WORKAROUND_ERRONEOUS_ANSWER
2211 s_expectAnswer = 1;
2212#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2213
2214 /* success or failure is ignored by the upper layer here.
2215 it will call GET_CURRENT_CALLS and determine success that way */
2216 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2217 break;
2218 case RIL_REQUEST_ANSWER:
2219 at_send_command("ATA", NULL);
2220
2221#ifdef WORKAROUND_ERRONEOUS_ANSWER
2222 s_expectAnswer = 1;
2223#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2224
2225 /* success or failure is ignored by the upper layer here.
2226 it will call GET_CURRENT_CALLS and determine success that way */
2227 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2228 break;
2229 case RIL_REQUEST_CONFERENCE:
2230 // 3GPP 22.030 6.5.5
2231 // "Adds a held call to the conversation"
2232 at_send_command("AT+CHLD=3", NULL);
2233
2234 /* success or failure is ignored by the upper layer here.
2235 it will call GET_CURRENT_CALLS and determine success that way */
2236 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2237 break;
2238 case RIL_REQUEST_UDUB:
2239 /* user determined user busy */
2240 /* sometimes used: ATH */
2241 at_send_command("ATH", NULL);
2242
2243 /* success or failure is ignored by the upper layer here.
2244 it will call GET_CURRENT_CALLS and determine success that way */
2245 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2246 break;
2247
2248 case RIL_REQUEST_SEPARATE_CONNECTION:
2249 {
2250 char cmd[12];
2251 int party = ((int*)data)[0];
2252
2253 // Make sure that party is in a valid range.
2254 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2255 // It's sufficient for us to just make sure it's single digit.)
2256 if (party > 0 && party < 10) {
2257 sprintf(cmd, "AT+CHLD=2%d", party);
2258 at_send_command(cmd, NULL);
2259 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2260 } else {
2261 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2262 }
2263 }
2264 break;
2265
2266 case RIL_REQUEST_SIGNAL_STRENGTH:
2267 requestSignalStrength(data, datalen, t);
2268 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002269 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2270 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002271 requestRegistrationState(request, data, datalen, t);
2272 break;
2273 case RIL_REQUEST_OPERATOR:
2274 requestOperator(data, datalen, t);
2275 break;
2276 case RIL_REQUEST_RADIO_POWER:
2277 requestRadioPower(data, datalen, t);
2278 break;
2279 case RIL_REQUEST_DTMF: {
2280 char c = ((char *)data)[0];
2281 char *cmd;
2282 asprintf(&cmd, "AT+VTS=%c", (int)c);
2283 at_send_command(cmd, NULL);
2284 free(cmd);
2285 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2286 break;
2287 }
2288 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302289 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002290 requestSendSMS(data, datalen, t);
2291 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002292 case RIL_REQUEST_CDMA_SEND_SMS:
2293 requestCdmaSendSMS(data, datalen, t);
2294 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002295 case RIL_REQUEST_IMS_SEND_SMS:
2296 requestImsSendSMS(data, datalen, t);
2297 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002298 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2299 requestSimOpenChannel(data, datalen, t);
2300 break;
2301 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2302 requestSimCloseChannel(data, datalen, t);
2303 break;
2304 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2305 requestSimTransmitApduChannel(data, datalen, t);
2306 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002307 case RIL_REQUEST_SETUP_DATA_CALL:
2308 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002309 break;
2310 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2311 requestSMSAcknowledge(data, datalen, t);
2312 break;
2313
2314 case RIL_REQUEST_GET_IMSI:
2315 p_response = NULL;
2316 err = at_send_command_numeric("AT+CIMI", &p_response);
2317
2318 if (err < 0 || p_response->success == 0) {
2319 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2320 } else {
2321 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2322 p_response->p_intermediates->line, sizeof(char *));
2323 }
2324 at_response_free(p_response);
2325 break;
2326
2327 case RIL_REQUEST_GET_IMEI:
2328 p_response = NULL;
2329 err = at_send_command_numeric("AT+CGSN", &p_response);
2330
2331 if (err < 0 || p_response->success == 0) {
2332 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2333 } else {
2334 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2335 p_response->p_intermediates->line, sizeof(char *));
2336 }
2337 at_response_free(p_response);
2338 break;
2339
2340 case RIL_REQUEST_SIM_IO:
2341 requestSIM_IO(data,datalen,t);
2342 break;
2343
2344 case RIL_REQUEST_SEND_USSD:
2345 requestSendUSSD(data, datalen, t);
2346 break;
2347
2348 case RIL_REQUEST_CANCEL_USSD:
2349 p_response = NULL;
2350 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2351
2352 if (err < 0 || p_response->success == 0) {
2353 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2354 } else {
2355 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2356 p_response->p_intermediates->line, sizeof(char *));
2357 }
2358 at_response_free(p_response);
2359 break;
2360
2361 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2362 at_send_command("AT+COPS=0", NULL);
2363 break;
2364
Wink Savillef4c4d362009-04-02 01:37:03 -07002365 case RIL_REQUEST_DATA_CALL_LIST:
2366 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002367 break;
2368
2369 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2370 requestQueryNetworkSelectionMode(data, datalen, t);
2371 break;
2372
2373 case RIL_REQUEST_OEM_HOOK_RAW:
2374 // echo back data
2375 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2376 break;
2377
2378
2379 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2380 int i;
2381 const char ** cur;
2382
Wink Saville4dcab4f2012-11-19 16:05:13 -08002383 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002384
2385
2386 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2387 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002388 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002389 }
2390
2391 // echo back strings
2392 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2393 break;
2394 }
2395
2396 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2397 requestWriteSmsToSim(data, datalen, t);
2398 break;
2399
2400 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2401 char * cmd;
2402 p_response = NULL;
2403 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2404 err = at_send_command(cmd, &p_response);
2405 free(cmd);
2406 if (err < 0 || p_response->success == 0) {
2407 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2408 } else {
2409 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2410 }
2411 at_response_free(p_response);
2412 break;
2413 }
2414
2415 case RIL_REQUEST_ENTER_SIM_PIN:
2416 case RIL_REQUEST_ENTER_SIM_PUK:
2417 case RIL_REQUEST_ENTER_SIM_PIN2:
2418 case RIL_REQUEST_ENTER_SIM_PUK2:
2419 case RIL_REQUEST_CHANGE_SIM_PIN:
2420 case RIL_REQUEST_CHANGE_SIM_PIN2:
2421 requestEnterSimPin(data, datalen, t);
2422 break;
2423
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002424 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2425 int reply[2];
2426 //0==unregistered, 1==registered
2427 reply[0] = s_ims_registered;
2428
2429 //to be used when changed to include service supporated info
2430 //reply[1] = s_ims_services;
2431
2432 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2433 reply[1] = s_ims_format;
2434
2435 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2436 reply[0], reply[1]);
2437 if (reply[1] != -1) {
2438 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2439 } else {
2440 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2441 }
2442 break;
2443 }
2444
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002445 case RIL_REQUEST_VOICE_RADIO_TECH:
2446 {
2447 int tech = techFromModemType(TECH(sMdmInfo));
2448 if (tech < 0 )
2449 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2450 else
2451 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2452 }
2453 break;
2454 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2455 requestSetPreferredNetworkType(request, data, datalen, t);
2456 break;
2457
2458 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2459 requestGetPreferredNetworkType(request, data, datalen, t);
2460 break;
2461
Jun Tian58027012013-07-30 11:07:22 +08002462 case RIL_REQUEST_GET_CELL_INFO_LIST:
2463 requestGetCellInfoList(data, datalen, t);
2464 break;
2465
2466 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2467 requestSetCellInfoListRate(data, datalen, t);
2468 break;
2469
Etan Cohend3652192014-06-20 08:28:44 -07002470 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2471 requestGetHardwareConfig(data, datalen, t);
2472 break;
2473
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002474 case RIL_REQUEST_SHUTDOWN:
2475 requestShutdown(t);
2476 break;
2477
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002478 /* CDMA Specific Requests */
2479 case RIL_REQUEST_BASEBAND_VERSION:
2480 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2481 requestCdmaBaseBandVersion(request, data, datalen, t);
2482 break;
2483 } // Fall-through if tech is not cdma
2484
2485 case RIL_REQUEST_DEVICE_IDENTITY:
2486 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2487 requestCdmaDeviceIdentity(request, data, datalen, t);
2488 break;
2489 } // Fall-through if tech is not cdma
2490
2491 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2492 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2493 requestCdmaSubscription(request, data, datalen, t);
2494 break;
2495 } // Fall-through if tech is not cdma
2496
2497 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2498 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2499 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2500 break;
2501 } // Fall-through if tech is not cdma
2502
2503 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2504 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2505 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2506 break;
2507 } // Fall-through if tech is not cdma
2508
2509 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2510 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2511 requestCdmaGetRoamingPreference(request, data, datalen, t);
2512 break;
2513 } // Fall-through if tech is not cdma
2514
2515 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2516 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2517 requestCdmaSetRoamingPreference(request, data, datalen, t);
2518 break;
2519 } // Fall-through if tech is not cdma
2520
2521 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2522 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2523 requestExitEmergencyMode(data, datalen, t);
2524 break;
2525 } // Fall-through if tech is not cdma
2526
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002527 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002528 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002529 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2530 break;
2531 }
2532}
2533
2534/**
2535 * Synchronous call from the RIL to us to return current radio state.
2536 * RADIO_STATE_UNAVAILABLE should be the initial state.
2537 */
2538static RIL_RadioState
2539currentState()
2540{
2541 return sState;
2542}
2543/**
2544 * Call from RIL to us to find out whether a specific request code
2545 * is supported by this implementation.
2546 *
2547 * Return 1 for "supported" and 0 for "unsupported"
2548 */
2549
2550static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002551onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002552{
2553 //@@@ todo
2554
2555 return 1;
2556}
2557
Mark Salyzynba58c202014-03-12 15:20:22 -07002558static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002559{
2560 //@@@todo
2561
2562}
2563
2564static const char * getVersion(void)
2565{
2566 return "android reference-ril 1.0";
2567}
2568
2569static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002570setRadioTechnology(ModemInfo *mdm, int newtech)
2571{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002572 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002573
2574 int oldtech = TECH(mdm);
2575
2576 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002577 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002578 TECH(mdm) = newtech;
2579 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2580 int tech = techFromModemType(TECH(sMdmInfo));
2581 if (tech > 0 ) {
2582 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2583 &tech, sizeof(tech));
2584 }
2585 }
2586 }
2587}
2588
2589static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002590setRadioState(RIL_RadioState newState)
2591{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002592 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002593 RIL_RadioState oldState;
2594
2595 pthread_mutex_lock(&s_state_mutex);
2596
2597 oldState = sState;
2598
2599 if (s_closed > 0) {
2600 // If we're closed, the only reasonable state is
2601 // RADIO_STATE_UNAVAILABLE
2602 // This is here because things on the main thread
2603 // may attempt to change the radio state after the closed
2604 // event happened in another thread
2605 newState = RADIO_STATE_UNAVAILABLE;
2606 }
2607
2608 if (sState != newState || s_closed > 0) {
2609 sState = newState;
2610
2611 pthread_cond_broadcast (&s_state_cond);
2612 }
2613
2614 pthread_mutex_unlock(&s_state_mutex);
2615
2616
2617 /* do these outside of the mutex */
2618 if (sState != oldState) {
2619 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2620 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002621 // Sim state can change as result of radio state change
2622 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2623 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002624
2625 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2626 * from the AT reader thread
2627 * Currently, this doesn't happen, but if that changes then these
2628 * will need to be dispatched on the request thread
2629 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002630 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002631 onRadioPowerOn();
2632 }
2633 }
2634}
2635
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002636/** Returns RUIM_NOT_READY on error */
2637static SIM_Status
2638getRUIMStatus()
2639{
2640 ATResponse *p_response = NULL;
2641 int err;
2642 int ret;
2643 char *cpinLine;
2644 char *cpinResult;
2645
2646 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2647 ret = SIM_NOT_READY;
2648 goto done;
2649 }
2650
2651 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2652
2653 if (err != 0) {
2654 ret = SIM_NOT_READY;
2655 goto done;
2656 }
2657
2658 switch (at_get_cme_error(p_response)) {
2659 case CME_SUCCESS:
2660 break;
2661
2662 case CME_SIM_NOT_INSERTED:
2663 ret = SIM_ABSENT;
2664 goto done;
2665
2666 default:
2667 ret = SIM_NOT_READY;
2668 goto done;
2669 }
2670
2671 /* CPIN? has succeeded, now look at the result */
2672
2673 cpinLine = p_response->p_intermediates->line;
2674 err = at_tok_start (&cpinLine);
2675
2676 if (err < 0) {
2677 ret = SIM_NOT_READY;
2678 goto done;
2679 }
2680
2681 err = at_tok_nextstr(&cpinLine, &cpinResult);
2682
2683 if (err < 0) {
2684 ret = SIM_NOT_READY;
2685 goto done;
2686 }
2687
2688 if (0 == strcmp (cpinResult, "SIM PIN")) {
2689 ret = SIM_PIN;
2690 goto done;
2691 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2692 ret = SIM_PUK;
2693 goto done;
2694 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2695 return SIM_NETWORK_PERSONALIZATION;
2696 } else if (0 != strcmp (cpinResult, "READY")) {
2697 /* we're treating unsupported lock types as "sim absent" */
2698 ret = SIM_ABSENT;
2699 goto done;
2700 }
2701
2702 at_response_free(p_response);
2703 p_response = NULL;
2704 cpinResult = NULL;
2705
2706 ret = SIM_READY;
2707
2708done:
2709 at_response_free(p_response);
2710 return ret;
2711}
2712
John Wang309ac292009-07-30 14:53:23 -07002713/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002714static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002715getSIMStatus()
2716{
2717 ATResponse *p_response = NULL;
2718 int err;
2719 int ret;
2720 char *cpinLine;
2721 char *cpinResult;
2722
Wink Saville4dcab4f2012-11-19 16:05:13 -08002723 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002724 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
John Wang309ac292009-07-30 14:53:23 -07002725 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002726 goto done;
2727 }
2728
2729 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2730
2731 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002732 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002733 goto done;
2734 }
2735
2736 switch (at_get_cme_error(p_response)) {
2737 case CME_SUCCESS:
2738 break;
2739
2740 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002741 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002742 goto done;
2743
2744 default:
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 /* CPIN? has succeeded, now look at the result */
2750
2751 cpinLine = p_response->p_intermediates->line;
2752 err = at_tok_start (&cpinLine);
2753
2754 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002755 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002756 goto done;
2757 }
2758
2759 err = at_tok_nextstr(&cpinLine, &cpinResult);
2760
2761 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002762 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002763 goto done;
2764 }
2765
2766 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002767 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002768 goto done;
2769 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07002770 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002771 goto done;
2772 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002773 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002774 } else if (0 != strcmp (cpinResult, "READY")) {
2775 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07002776 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002777 goto done;
2778 }
2779
2780 at_response_free(p_response);
2781 p_response = NULL;
2782 cpinResult = NULL;
2783
John Wang309ac292009-07-30 14:53:23 -07002784 ret = SIM_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002785
2786done:
2787 at_response_free(p_response);
2788 return ret;
2789}
2790
2791
2792/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07002793 * Get the current card status.
2794 *
2795 * This must be freed using freeCardStatus.
2796 * @return: On success returns RIL_E_SUCCESS
2797 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002798static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002799 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07002800 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07002801 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2802 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002803 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07002804 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2805 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002806 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07002807 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2808 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002809 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07002810 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2811 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002812 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07002813 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2814 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002815 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07002816 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002817 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2818 // RUIM_ABSENT = 6
2819 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2820 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2821 // RUIM_NOT_READY = 7
2822 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2823 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2824 // RUIM_READY = 8
2825 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2826 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2827 // RUIM_PIN = 9
2828 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2829 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2830 // RUIM_PUK = 10
2831 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2832 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2833 // RUIM_NETWORK_PERSONALIZATION = 11
2834 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2835 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
Wink Savillef6aa7c12009-04-30 14:20:52 -07002836 };
2837 RIL_CardState card_state;
2838 int num_apps;
2839
2840 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07002841 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002842 card_state = RIL_CARDSTATE_ABSENT;
2843 num_apps = 0;
2844 } else {
2845 card_state = RIL_CARDSTATE_PRESENT;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002846 num_apps = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002847 }
2848
2849 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002850 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07002851 p_card_status->card_state = card_state;
2852 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
2853 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
2854 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002855 p_card_status->ims_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002856 p_card_status->num_applications = num_apps;
2857
2858 // Initialize application status
2859 int i;
2860 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07002861 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002862 }
2863
2864 // Pickup the appropriate application status
2865 // that reflects sim_status for gsm.
2866 if (num_apps != 0) {
2867 // Only support one app, gsm
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002868 p_card_status->num_applications = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002869 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002870 p_card_status->cdma_subscription_app_index = 1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002871
2872 // Get the correct app status
2873 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002874 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002875 }
2876
2877 *pp_card_status = p_card_status;
2878 return RIL_E_SUCCESS;
2879}
2880
2881/**
2882 * Free the card status returned by getCardStatus
2883 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002884static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002885 free(p_card_status);
2886}
2887
2888/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002889 * SIM ready means any commands that access the SIM will work, including:
2890 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
2891 * (all SMS-related commands)
2892 */
2893
Mark Salyzynba58c202014-03-12 15:20:22 -07002894static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002895{
2896 ATResponse *p_response;
2897 int ret;
2898
Naveen Kalla2baf7232016-10-11 13:49:20 -07002899 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002900 // no longer valid to poll
2901 return;
2902 }
2903
2904 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07002905 case SIM_ABSENT:
2906 case SIM_PIN:
2907 case SIM_PUK:
2908 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002909 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002910 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002911 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002912 return;
2913
John Wang309ac292009-07-30 14:53:23 -07002914 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002915 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
2916 return;
2917
John Wang309ac292009-07-30 14:53:23 -07002918 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002919 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002920 onSIMReady();
2921 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002922 return;
2923 }
2924}
2925
2926/** returns 1 if on, 0 if off, and -1 on error */
2927static int isRadioOn()
2928{
2929 ATResponse *p_response = NULL;
2930 int err;
2931 char *line;
2932 char ret;
2933
2934 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
2935
2936 if (err < 0 || p_response->success == 0) {
2937 // assume radio is off
2938 goto error;
2939 }
2940
2941 line = p_response->p_intermediates->line;
2942
2943 err = at_tok_start(&line);
2944 if (err < 0) goto error;
2945
2946 err = at_tok_nextbool(&line, &ret);
2947 if (err < 0) goto error;
2948
2949 at_response_free(p_response);
2950
2951 return (int)ret;
2952
2953error:
2954
2955 at_response_free(p_response);
2956 return -1;
2957}
2958
2959/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002960 * Parse the response generated by a +CTEC AT command
2961 * The values read from the response are stored in current and preferred.
2962 * Both current and preferred may be null. The corresponding value is ignored in that case.
2963 *
2964 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
2965 * 1 if the response includes the current technology only
2966 * 0 if the response includes both current technology and preferred mode
2967 */
2968int parse_technology_response( const char *response, int *current, int32_t *preferred )
2969{
2970 int err;
2971 char *line, *p;
2972 int ct;
2973 int32_t pt = 0;
2974 char *str_pt;
2975
2976 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08002977 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002978 err = at_tok_start(&p);
2979 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002980 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002981 free(line);
2982 return -1;
2983 }
2984
2985 err = at_tok_nextint(&p, &ct);
2986 if (err) {
2987 free(line);
2988 return -1;
2989 }
2990 if (current) *current = ct;
2991
Wink Saville4dcab4f2012-11-19 16:05:13 -08002992 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002993
2994 err = at_tok_nexthexint(&p, &pt);
2995 if (err) {
2996 free(line);
2997 return 1;
2998 }
2999 if (preferred) {
3000 *preferred = pt;
3001 }
3002 free(line);
3003
3004 return 0;
3005}
3006
Mark Salyzynba58c202014-03-12 15:20:22 -07003007int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003008{
3009 ATResponse *p_response;
3010 int err, val, techs = 0;
3011 char *tok;
3012 char *line;
3013
Wink Saville4dcab4f2012-11-19 16:05:13 -08003014 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003015 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3016 if (err || !p_response->success)
3017 goto error;
3018 line = p_response->p_intermediates->line;
3019 err = at_tok_start(&line);
3020 if (err || !at_tok_hasmore(&line))
3021 goto error;
3022 while (!at_tok_nextint(&line, &val)) {
3023 techs |= ( 1 << val );
3024 }
3025 if (supported) *supported = techs;
3026 return 0;
3027error:
3028 at_response_free(p_response);
3029 return -1;
3030}
3031
3032/**
3033 * query_ctec. Send the +CTEC AT command to the modem to query the current
3034 * and preferred modes. It leaves values in the addresses pointed to by
3035 * current and preferred. If any of those pointers are NULL, the corresponding value
3036 * is ignored, but the return value will still reflect if retreiving and parsing of the
3037 * values suceeded.
3038 *
3039 * @mdm Currently unused
3040 * @current A pointer to store the current mode returned by the modem. May be null.
3041 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3042 * @return -1 on error (or failure to parse)
3043 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3044 * 0 if both current and preferred were returned correctly
3045 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003046int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003047{
3048 ATResponse *response = NULL;
3049 int err;
3050 int res;
3051
Colin Cross5cba4882014-02-05 18:55:42 -08003052 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003053 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3054 if (!err && response->success) {
3055 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3056 at_response_free(response);
3057 return res;
3058 }
Colin Cross5cba4882014-02-05 18:55:42 -08003059 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003060 at_response_free(response);
3061 return -1;
3062}
3063
3064int is_multimode_modem(ModemInfo *mdm)
3065{
3066 ATResponse *response;
3067 int err;
3068 char *line;
3069 int tech;
3070 int32_t preferred;
3071
3072 if (query_ctec(mdm, &tech, &preferred) == 0) {
3073 mdm->currentTech = tech;
3074 mdm->preferredNetworkMode = preferred;
3075 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3076 return 0;
3077 }
3078 return 1;
3079 }
3080 return 0;
3081}
3082
3083/**
3084 * Find out if our modem is GSM, CDMA or both (Multimode)
3085 */
3086static void probeForModemMode(ModemInfo *info)
3087{
3088 ATResponse *response;
3089 int err;
3090 assert (info);
3091 // Currently, our only known multimode modem is qemu's android modem,
3092 // which implements the AT+CTEC command to query and set mode.
3093 // Try that first
3094
3095 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003096 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003097 info->supportedTechs, info->currentTech);
3098 return;
3099 }
3100
3101 /* Being here means that our modem is not multimode */
3102 info->isMultimode = 0;
3103
3104 /* CDMA Modems implement the AT+WNAM command */
3105 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3106 if (!err && response->success) {
3107 at_response_free(response);
3108 // TODO: find out if we really support EvDo
3109 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3110 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003111 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003112 return;
3113 }
3114 if (!err) at_response_free(response);
3115 // TODO: find out if modem really supports WCDMA/LTE
3116 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3117 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003118 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003119}
3120
3121/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003122 * Initialize everything that can be configured while we're still in
3123 * AT+CFUN=0
3124 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003125static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003126{
3127 ATResponse *p_response = NULL;
3128 int err;
3129
3130 setRadioState (RADIO_STATE_OFF);
3131
3132 at_handshake();
3133
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003134 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003135 /* note: we don't check errors here. Everything important will
3136 be handled in onATTimeout and onATReaderClosed */
3137
3138 /* atchannel is tolerant of echo but it must */
3139 /* have verbose result codes */
3140 at_send_command("ATE0Q0V1", NULL);
3141
3142 /* No auto-answer */
3143 at_send_command("ATS0=0", NULL);
3144
3145 /* Extended errors */
3146 at_send_command("AT+CMEE=1", NULL);
3147
3148 /* Network registration events */
3149 err = at_send_command("AT+CREG=2", &p_response);
3150
3151 /* some handsets -- in tethered mode -- don't support CREG=2 */
3152 if (err < 0 || p_response->success == 0) {
3153 at_send_command("AT+CREG=1", NULL);
3154 }
3155
3156 at_response_free(p_response);
3157
3158 /* GPRS registration events */
3159 at_send_command("AT+CGREG=1", NULL);
3160
3161 /* Call Waiting notifications */
3162 at_send_command("AT+CCWA=1", NULL);
3163
3164 /* Alternating voice/data off */
3165 at_send_command("AT+CMOD=0", NULL);
3166
3167 /* Not muted */
3168 at_send_command("AT+CMUT=0", NULL);
3169
3170 /* +CSSU unsolicited supp service notifications */
3171 at_send_command("AT+CSSN=0,1", NULL);
3172
3173 /* no connected line identification */
3174 at_send_command("AT+COLP=0", NULL);
3175
3176 /* HEX character set */
3177 at_send_command("AT+CSCS=\"HEX\"", NULL);
3178
3179 /* USSD unsolicited */
3180 at_send_command("AT+CUSD=1", NULL);
3181
3182 /* Enable +CGEV GPRS event notifications, but don't buffer */
3183 at_send_command("AT+CGEREP=1,0", NULL);
3184
3185 /* SMS PDU mode */
3186 at_send_command("AT+CMGF=0", NULL);
3187
3188#ifdef USE_TI_COMMANDS
3189
3190 at_send_command("AT%CPI=3", NULL);
3191
3192 /* TI specific -- notifications when SMS is ready (currently ignored) */
3193 at_send_command("AT%CSTAT=1", NULL);
3194
3195#endif /* USE_TI_COMMANDS */
3196
3197
3198 /* assume radio is off on error */
3199 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003200 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003201 }
3202}
3203
3204static void waitForClose()
3205{
3206 pthread_mutex_lock(&s_state_mutex);
3207
3208 while (s_closed == 0) {
3209 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3210 }
3211
3212 pthread_mutex_unlock(&s_state_mutex);
3213}
3214
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003215static void sendUnsolImsNetworkStateChanged()
3216{
3217#if 0 // to be used when unsol is changed to return data.
3218 int reply[2];
3219 reply[0] = s_ims_registered;
3220 reply[1] = s_ims_services;
3221 reply[1] = s_ims_format;
3222#endif
3223 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3224 NULL, 0);
3225}
3226
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003227/**
3228 * Called by atchannel when an unsolicited line appears
3229 * This is called on atchannel's reader thread. AT commands may
3230 * not be issued here
3231 */
3232static void onUnsolicited (const char *s, const char *sms_pdu)
3233{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003234 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003235 int err;
3236
3237 /* Ignore unsolicited responses until we're initialized.
3238 * This is OK because the RIL library will poll for initial state
3239 */
3240 if (sState == RADIO_STATE_UNAVAILABLE) {
3241 return;
3242 }
3243
3244 if (strStartsWith(s, "%CTZV:")) {
3245 /* TI specific -- NITZ time */
3246 char *response;
3247
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003248 line = p = strdup(s);
3249 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003250
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003251 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003252
3253 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003254 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003255 } else {
3256 RIL_onUnsolicitedResponse (
3257 RIL_UNSOL_NITZ_TIME_RECEIVED,
3258 response, strlen(response));
3259 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003260 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003261 } else if (strStartsWith(s,"+CRING:")
3262 || strStartsWith(s,"RING")
3263 || strStartsWith(s,"NO CARRIER")
3264 || strStartsWith(s,"+CCWA")
3265 ) {
3266 RIL_onUnsolicitedResponse (
3267 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3268 NULL, 0);
3269#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003270 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003271#endif /* WORKAROUND_FAKE_CGEV */
3272 } else if (strStartsWith(s,"+CREG:")
3273 || strStartsWith(s,"+CGREG:")
3274 ) {
3275 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003276 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003277 NULL, 0);
3278#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003279 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003280#endif /* WORKAROUND_FAKE_CGEV */
3281 } else if (strStartsWith(s, "+CMT:")) {
3282 RIL_onUnsolicitedResponse (
3283 RIL_UNSOL_RESPONSE_NEW_SMS,
3284 sms_pdu, strlen(sms_pdu));
3285 } else if (strStartsWith(s, "+CDS:")) {
3286 RIL_onUnsolicitedResponse (
3287 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3288 sms_pdu, strlen(sms_pdu));
3289 } else if (strStartsWith(s, "+CGEV:")) {
3290 /* Really, we can ignore NW CLASS and ME CLASS events here,
3291 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003292 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003293 */
3294 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003295 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003296#ifdef WORKAROUND_FAKE_CGEV
3297 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003298 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003299#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003300 } else if (strStartsWith(s, "+CTEC: ")) {
3301 int tech, mask;
3302 switch (parse_technology_response(s, &tech, NULL))
3303 {
3304 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003305 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003306 break;
3307 case 1: // current mode correctly parsed
3308 case 0: // preferred mode correctly parsed
3309 mask = 1 << tech;
3310 if (mask != MDM_GSM && mask != MDM_CDMA &&
3311 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003312 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003313 } else {
3314 setRadioTechnology(sMdmInfo, tech);
3315 }
3316 break;
3317 }
3318 } else if (strStartsWith(s, "+CCSS: ")) {
3319 int source = 0;
3320 line = p = strdup(s);
3321 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003322 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003323 return;
3324 }
3325 if (at_tok_start(&p) < 0) {
3326 free(line);
3327 return;
3328 }
3329 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003330 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003331 free(line);
3332 return;
3333 }
3334 SSOURCE(sMdmInfo) = source;
3335 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3336 &source, sizeof(source));
3337 } else if (strStartsWith(s, "+WSOS: ")) {
3338 char state = 0;
3339 int unsol;
3340 line = p = strdup(s);
3341 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003342 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003343 return;
3344 }
3345 if (at_tok_start(&p) < 0) {
3346 free(line);
3347 return;
3348 }
3349 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003350 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003351 free(line);
3352 return;
3353 }
3354 free(line);
3355
3356 unsol = state ?
3357 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3358
3359 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3360
3361 } else if (strStartsWith(s, "+WPRL: ")) {
3362 int version = -1;
3363 line = p = strdup(s);
3364 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003365 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003366 return;
3367 }
3368 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003369 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003370 free(line);
3371 return;
3372 }
3373 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003374 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003375 free(line);
3376 return;
3377 }
3378 free(line);
3379 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3380 } else if (strStartsWith(s, "+CFUN: 0")) {
3381 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003382 }
3383}
3384
3385/* Called on command or reader thread */
3386static void onATReaderClosed()
3387{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003388 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003389 at_close();
3390 s_closed = 1;
3391
3392 setRadioState (RADIO_STATE_UNAVAILABLE);
3393}
3394
3395/* Called on command thread */
3396static void onATTimeout()
3397{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003398 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003399 at_close();
3400
3401 s_closed = 1;
3402
3403 /* FIXME cause a radio reset here */
3404
3405 setRadioState (RADIO_STATE_UNAVAILABLE);
3406}
3407
Etan Cohend3652192014-06-20 08:28:44 -07003408/* Called to pass hardware configuration information to telephony
3409 * framework.
3410 */
3411static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3412{
3413 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3414}
3415
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003416static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003417{
3418#ifdef RIL_SHLIB
3419 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3420#else
3421 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3422 exit(-1);
3423#endif
3424}
3425
3426static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003427mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003428{
3429 int fd;
3430 int ret;
3431
3432 AT_DUMP("== ", "entering mainLoop()", -1 );
3433 at_set_on_reader_closed(onATReaderClosed);
3434 at_set_on_timeout(onATTimeout);
3435
3436 for (;;) {
3437 fd = -1;
3438 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003439 if (isInEmulator()) {
3440 fd = qemu_pipe_open("pipe:qemud:gsm");
3441 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003442 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003443 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003444 fd = socket_local_client(s_device_path,
3445 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3446 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003447 } else if (s_device_path != NULL) {
3448 fd = open (s_device_path, O_RDWR);
3449 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3450 /* disable echo on serial ports */
3451 struct termios ios;
3452 tcgetattr( fd, &ios );
3453 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3454 tcsetattr( fd, TCSANOW, &ios );
3455 }
3456 }
3457
3458 if (fd < 0) {
3459 perror ("opening AT interface. retrying...");
3460 sleep(10);
3461 /* never returns */
3462 }
3463 }
3464
3465 s_closed = 0;
3466 ret = at_open(fd, onUnsolicited);
3467
3468 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003469 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003470 return 0;
3471 }
3472
3473 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3474
3475 // Give initializeCallback a chance to dispatched, since
3476 // we don't presently have a cancellation mechanism
3477 sleep(1);
3478
3479 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003480 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003481 }
3482}
3483
3484#ifdef RIL_SHLIB
3485
3486pthread_t s_tid_mainloop;
3487
3488const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3489{
3490 int ret;
3491 int fd = -1;
3492 int opt;
3493 pthread_attr_t attr;
3494
3495 s_rilenv = env;
3496
Sandeep Gutta11f27942014-07-10 05:00:25 +05303497 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003498 switch (opt) {
3499 case 'p':
3500 s_port = atoi(optarg);
3501 if (s_port == 0) {
3502 usage(argv[0]);
3503 return NULL;
3504 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003505 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003506 break;
3507
3508 case 'd':
3509 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003510 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003511 break;
3512
3513 case 's':
3514 s_device_path = optarg;
3515 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003516 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003517 break;
3518
Sandeep Gutta11f27942014-07-10 05:00:25 +05303519 case 'c':
3520 RLOGI("Client id received %s\n", optarg);
3521 break;
3522
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003523 default:
3524 usage(argv[0]);
3525 return NULL;
3526 }
3527 }
3528
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003529 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003530 usage(argv[0]);
3531 return NULL;
3532 }
3533
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003534 sMdmInfo = calloc(1, sizeof(ModemInfo));
3535 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003536 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003537 return NULL;
3538 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003539 pthread_attr_init (&attr);
3540 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3541 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3542
3543 return &s_callbacks;
3544}
3545#else /* RIL_SHLIB */
3546int main (int argc, char **argv)
3547{
3548 int ret;
3549 int fd = -1;
3550 int opt;
3551
3552 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3553 switch (opt) {
3554 case 'p':
3555 s_port = atoi(optarg);
3556 if (s_port == 0) {
3557 usage(argv[0]);
3558 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003559 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003560 break;
3561
3562 case 'd':
3563 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003564 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003565 break;
3566
3567 case 's':
3568 s_device_path = optarg;
3569 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003570 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003571 break;
3572
3573 default:
3574 usage(argv[0]);
3575 }
3576 }
3577
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003578 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003579 usage(argv[0]);
3580 }
3581
3582 RIL_register(&s_callbacks);
3583
3584 mainLoop(NULL);
3585
3586 return 0;
3587}
3588
3589#endif /* RIL_SHLIB */