blob: 997195a6f0f27300477f4ab18ae9ef2f76ca1375 [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>
David 'Digit' Turner834eca82016-06-22 12:10:01 +020020#include <stdbool.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080021#include <stdio.h>
22#include <assert.h>
23#include <string.h>
24#include <errno.h>
25#include <unistd.h>
Mark Salyzynba58c202014-03-12 15:20:22 -070026#include <sys/cdefs.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080027#include <sys/types.h>
28#include <sys/stat.h>
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -070029#include <inttypes.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080030#include <fcntl.h>
31#include <pthread.h>
32#include <alloca.h>
33#include "atchannel.h"
34#include "at_tok.h"
35#include "misc.h"
36#include <getopt.h>
37#include <sys/socket.h>
38#include <cutils/sockets.h>
39#include <termios.h>
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +010040#include <sys/system_properties.h>
David 'Digit' Turner2e98d892016-06-16 19:00:05 +020041#include <system/qemu_pipe.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080042
Wink Saville9a9fbd22011-02-15 17:13:10 -080043#include "ril.h"
44
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080045#define LOG_TAG "RIL"
46#include <utils/Log.h>
47
Etan Cohend3652192014-06-20 08:28:44 -070048static void *noopRemoveWarning( void *a ) { return a; }
49#define RIL_UNUSED_PARM(a) noopRemoveWarning((void *)&(a));
50
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080051#define MAX_AT_RESPONSE 0x1000
52
Wink Savillef4c4d362009-04-02 01:37:03 -070053/* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
Robert Greenwaltf838ede2010-07-15 18:54:53 -070054#define PPP_TTY_PATH "eth0"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080055
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -070056// Default MTU value
57#define DEFAULT_MTU 1500
58
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080059#ifdef USE_TI_COMMANDS
60
61// Enable a workaround
62// 1) Make incoming call, do not answer
63// 2) Hangup remote end
64// Expected: call should disappear from CLCC line
65// Actual: Call shows as "ACTIVE" before disappearing
66#define WORKAROUND_ERRONEOUS_ANSWER 1
67
68// Some varients of the TI stack do not support the +CGEV unsolicited
69// response. However, they seem to send an unsolicited +CME ERROR: 150
70#define WORKAROUND_FAKE_CGEV 1
71#endif
72
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070073/* Modem Technology bits */
74#define MDM_GSM 0x01
75#define MDM_WCDMA 0x02
76#define MDM_CDMA 0x04
77#define MDM_EVDO 0x08
78#define MDM_LTE 0x10
79
80typedef struct {
81 int supportedTechs; // Bitmask of supported Modem Technology bits
82 int currentTech; // Technology the modem is currently using (in the format used by modem)
83 int isMultimode;
84
85 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
86 // in which the byte number from LSB to MSB give the priority.
87 //
88 // |MSB| | |LSB
89 // value: |00 |00 |00 |00
90 // byte #: |3 |2 |1 |0
91 //
92 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
93 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
94 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
95 int32_t preferredNetworkMode;
96 int subscription_source;
97
98} ModemInfo;
99
100static ModemInfo *sMdmInfo;
101// TECH returns the current technology in the format used by the modem.
102// It can be used as an l-value
103#define TECH(mdminfo) ((mdminfo)->currentTech)
104// TECH_BIT returns the bitmask equivalent of the current tech
105#define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
106#define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
107#define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
108#define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
109// CDMA Subscription Source
110#define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
111
112static int net2modem[] = {
113 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
114 MDM_GSM, // 1 - GSM only
115 MDM_WCDMA, // 2 - WCDMA only
116 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
117 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
118 MDM_CDMA, // 5 - CDMA only
119 MDM_EVDO, // 6 - EvDo only
120 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
121 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
122 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
123 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
124 MDM_LTE, // 11 - LTE only
125};
126
127static int32_t net2pmask[] = {
128 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
129 MDM_GSM, // 1 - GSM only
130 MDM_WCDMA, // 2 - WCDMA only
131 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
132 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
133 MDM_CDMA, // 5 - CDMA only
134 MDM_EVDO, // 6 - EvDo only
135 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
136 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
137 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
138 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
139 MDM_LTE, // 11 - LTE only
140};
141
142static int is3gpp2(int radioTech) {
143 switch (radioTech) {
144 case RADIO_TECH_IS95A:
145 case RADIO_TECH_IS95B:
146 case RADIO_TECH_1xRTT:
147 case RADIO_TECH_EVDO_0:
148 case RADIO_TECH_EVDO_A:
149 case RADIO_TECH_EVDO_B:
150 case RADIO_TECH_EHRPD:
151 return 1;
152 default:
153 return 0;
154 }
155}
156
John Wang309ac292009-07-30 14:53:23 -0700157typedef enum {
158 SIM_ABSENT = 0,
159 SIM_NOT_READY = 1,
Naveen Kalla2baf7232016-10-11 13:49:20 -0700160 SIM_READY = 2,
John Wang309ac292009-07-30 14:53:23 -0700161 SIM_PIN = 3,
162 SIM_PUK = 4,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700163 SIM_NETWORK_PERSONALIZATION = 5,
164 RUIM_ABSENT = 6,
165 RUIM_NOT_READY = 7,
166 RUIM_READY = 8,
167 RUIM_PIN = 9,
168 RUIM_PUK = 10,
169 RUIM_NETWORK_PERSONALIZATION = 11
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100170} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -0700171
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800172static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
173static RIL_RadioState currentState();
174static int onSupports (int requestCode);
175static void onCancel (RIL_Token t);
176static const char *getVersion();
177static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -0700178static SIM_Status getSIMStatus();
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700179static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
180static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -0700181static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800182
183extern const char * requestToString(int request);
184
185/*** Static Variables ***/
186static const RIL_RadioFunctions s_callbacks = {
187 RIL_VERSION,
188 onRequest,
189 currentState,
190 onSupports,
191 onCancel,
192 getVersion
193};
194
195#ifdef RIL_SHLIB
196static const struct RIL_Env *s_rilenv;
197
198#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
199#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
200#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
201#endif
202
203static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
204
205static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
206static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
207
208static int s_port = -1;
209static const char * s_device_path = NULL;
210static int s_device_socket = 0;
211
212/* trigger change to this with s_state_cond */
213static int s_closed = 0;
214
215static int sFD; /* file desc of AT channel */
216static char sATBuffer[MAX_AT_RESPONSE+1];
217static char *sATBufferCur = NULL;
218
219static const struct timeval TIMEVAL_SIMPOLL = {1,0};
220static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
221static const struct timeval TIMEVAL_0 = {0,0};
222
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -0700223static int s_ims_registered = 0; // 0==unregistered
224static int s_ims_services = 1; // & 0x1 == sms over ims supported
225static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
226static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
227static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
228static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
229static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
230
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800231#ifdef WORKAROUND_ERRONEOUS_ANSWER
232// Max number of times we'll try to repoll when we think
233// we have a AT+CLCC race condition
234#define REPOLL_CALLS_COUNT_MAX 4
235
236// Line index that was incoming or waiting at last poll, or -1 for none
237static int s_incomingOrWaitingLine = -1;
238// Number of times we've asked for a repoll of AT+CLCC
239static int s_repollCallsCount = 0;
240// Should we expect a call to be answered in the next CLCC?
241static int s_expectAnswer = 0;
242#endif /* WORKAROUND_ERRONEOUS_ANSWER */
243
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200244// Returns true iff running this process in an emulator VM
245static bool isInEmulator(void) {
246 static int inQemu = -1;
247 if (inQemu < 0) {
248 char propValue[PROP_VALUE_MAX];
249 inQemu = (__system_property_get("ro.kernel.qemu", propValue) != 0);
250 }
251 return inQemu == 1;
252}
253
Wink Saville8a9e0212013-04-09 12:11:38 -0700254static int s_cell_info_rate_ms = INT_MAX;
255static int s_mcc = 0;
256static int s_mnc = 0;
257static int s_lac = 0;
258static int s_cid = 0;
259
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800260static void pollSIMState (void *param);
261static void setRadioState(RIL_RadioState newState);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700262static void setRadioTechnology(ModemInfo *mdm, int newtech);
263static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
264static int parse_technology_response(const char *response, int *current, int32_t *preferred);
265static int techFromModemType(int mdmtype);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800266
267static int clccStateToRILState(int state, RIL_CallState *p_state)
268
269{
270 switch(state) {
271 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
272 case 1: *p_state = RIL_CALL_HOLDING; return 0;
273 case 2: *p_state = RIL_CALL_DIALING; return 0;
274 case 3: *p_state = RIL_CALL_ALERTING; return 0;
275 case 4: *p_state = RIL_CALL_INCOMING; return 0;
276 case 5: *p_state = RIL_CALL_WAITING; return 0;
277 default: return -1;
278 }
279}
280
281/**
282 * Note: directly modified line and has *p_call point directly into
283 * modified line
284 */
Wink Saville3d54e742009-05-18 18:00:44 -0700285static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800286{
287 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
288 // index,isMT,state,mode,isMpty(,number,TOA)?
289
290 int err;
291 int state;
292 int mode;
293
294 err = at_tok_start(&line);
295 if (err < 0) goto error;
296
297 err = at_tok_nextint(&line, &(p_call->index));
298 if (err < 0) goto error;
299
300 err = at_tok_nextbool(&line, &(p_call->isMT));
301 if (err < 0) goto error;
302
303 err = at_tok_nextint(&line, &state);
304 if (err < 0) goto error;
305
306 err = clccStateToRILState(state, &(p_call->state));
307 if (err < 0) goto error;
308
309 err = at_tok_nextint(&line, &mode);
310 if (err < 0) goto error;
311
312 p_call->isVoice = (mode == 0);
313
314 err = at_tok_nextbool(&line, &(p_call->isMpty));
315 if (err < 0) goto error;
316
317 if (at_tok_hasmore(&line)) {
318 err = at_tok_nextstr(&line, &(p_call->number));
319
320 /* tolerate null here */
321 if (err < 0) return 0;
322
323 // Some lame implementations return strings
324 // like "NOT AVAILABLE" in the CLCC line
325 if (p_call->number != NULL
326 && 0 == strspn(p_call->number, "+0123456789")
327 ) {
328 p_call->number = NULL;
329 }
330
331 err = at_tok_nextint(&line, &p_call->toa);
332 if (err < 0) goto error;
333 }
334
Wink Saville74fa3882009-12-22 15:35:41 -0800335 p_call->uusInfo = NULL;
336
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800337 return 0;
338
339error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800340 RLOGE("invalid CLCC line\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800341 return -1;
342}
343
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -0700344static int parseSimResponseLine(char* line, RIL_SIM_IO_Response* response) {
345 int err;
346
347 err = at_tok_start(&line);
348 if (err < 0) return err;
349 err = at_tok_nextint(&line, &response->sw1);
350 if (err < 0) return err;
351 err = at_tok_nextint(&line, &response->sw2);
352 if (err < 0) return err;
353
354 if (at_tok_hasmore(&line)) {
355 err = at_tok_nextstr(&line, &response->simResponse);
356 if (err < 0) return err;
357 }
358 return 0;
359}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800360
361/** do post-AT+CFUN=1 initialization */
362static void onRadioPowerOn()
363{
364#ifdef USE_TI_COMMANDS
365 /* Must be after CFUN=1 */
366 /* TI specific -- notifications for CPHS things such */
367 /* as CPHS message waiting indicator */
368
369 at_send_command("AT%CPHS=1", NULL);
370
371 /* TI specific -- enable NITZ unsol notifs */
372 at_send_command("AT%CTZV=1", NULL);
373#endif
374
375 pollSIMState(NULL);
376}
377
378/** do post- SIM ready initialization */
379static void onSIMReady()
380{
381 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
382 /*
383 * Always send SMS messages directly to the TE
384 *
385 * mode = 1 // discard when link is reserved (link should never be
386 * reserved)
387 * mt = 2 // most messages routed to TE
388 * bm = 2 // new cell BM's routed to TE
389 * ds = 1 // Status reports routed to TE
390 * bfr = 1 // flush buffer
391 */
392 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
393}
394
Sanket Padawef0c8ca72016-06-30 15:01:08 -0700395static void requestRadioPower(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800396{
397 int onOff;
398
399 int err;
400 ATResponse *p_response = NULL;
401
402 assert (datalen >= sizeof(int *));
403 onOff = ((int *)data)[0];
404
405 if (onOff == 0 && sState != RADIO_STATE_OFF) {
406 err = at_send_command("AT+CFUN=0", &p_response);
407 if (err < 0 || p_response->success == 0) goto error;
408 setRadioState(RADIO_STATE_OFF);
409 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
410 err = at_send_command("AT+CFUN=1", &p_response);
411 if (err < 0|| p_response->success == 0) {
412 // Some stacks return an error when there is no SIM,
413 // but they really turn the RF portion on
414 // So, if we get an error, let's check to see if it
415 // turned on anyway
416
417 if (isRadioOn() != 1) {
418 goto error;
419 }
420 }
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700421 setRadioState(RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800422 }
423
424 at_response_free(p_response);
425 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
426 return;
427error:
428 at_response_free(p_response);
429 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
430}
431
Naveen Kallaa65a16a2014-07-31 16:48:31 -0700432static void requestShutdown(RIL_Token t)
433{
434 int onOff;
435
436 int err;
437 ATResponse *p_response = NULL;
438
439 if (sState != RADIO_STATE_OFF) {
440 err = at_send_command("AT+CFUN=0", &p_response);
441 setRadioState(RADIO_STATE_UNAVAILABLE);
442 }
443
444 at_response_free(p_response);
445 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
446 return;
447}
448
Wink Savillef4c4d362009-04-02 01:37:03 -0700449static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800450
Mark Salyzynba58c202014-03-12 15:20:22 -0700451static void onDataCallListChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800452{
Wink Savillef4c4d362009-04-02 01:37:03 -0700453 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800454}
455
Mark Salyzynba58c202014-03-12 15:20:22 -0700456static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800457{
Wink Savillef4c4d362009-04-02 01:37:03 -0700458 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800459}
460
Wink Savillef4c4d362009-04-02 01:37:03 -0700461static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800462{
463 ATResponse *p_response;
464 ATLine *p_cur;
465 int err;
466 int n = 0;
467 char *out;
468
469 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
470 if (err != 0 || p_response->success == 0) {
471 if (t != NULL)
472 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
473 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700474 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800475 NULL, 0);
476 return;
477 }
478
479 for (p_cur = p_response->p_intermediates; p_cur != NULL;
480 p_cur = p_cur->p_next)
481 n++;
482
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700483 RIL_Data_Call_Response_v11 *responses =
484 alloca(n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800485
486 int i;
487 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800488 responses[i].status = -1;
Wink Saville250eb3c2011-06-22 09:11:34 -0700489 responses[i].suggestedRetryTime = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800490 responses[i].cid = -1;
491 responses[i].active = -1;
492 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800493 responses[i].ifname = "";
494 responses[i].addresses = "";
495 responses[i].dnses = "";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700496 responses[i].gateways = "";
Etan Cohend3652192014-06-20 08:28:44 -0700497 responses[i].pcscf = "";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700498 responses[i].mtu = 0;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800499 }
500
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700501 RIL_Data_Call_Response_v11 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800502 for (p_cur = p_response->p_intermediates; p_cur != NULL;
503 p_cur = p_cur->p_next) {
504 char *line = p_cur->line;
505
506 err = at_tok_start(&line);
507 if (err < 0)
508 goto error;
509
510 err = at_tok_nextint(&line, &response->cid);
511 if (err < 0)
512 goto error;
513
514 err = at_tok_nextint(&line, &response->active);
515 if (err < 0)
516 goto error;
517
518 response++;
519 }
520
521 at_response_free(p_response);
522
523 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
524 if (err != 0 || p_response->success == 0) {
525 if (t != NULL)
526 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
527 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700528 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800529 NULL, 0);
530 return;
531 }
532
533 for (p_cur = p_response->p_intermediates; p_cur != NULL;
534 p_cur = p_cur->p_next) {
535 char *line = p_cur->line;
536 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800537
538 err = at_tok_start(&line);
539 if (err < 0)
540 goto error;
541
542 err = at_tok_nextint(&line, &cid);
543 if (err < 0)
544 goto error;
545
546 for (i = 0; i < n; i++) {
547 if (responses[i].cid == cid)
548 break;
549 }
550
551 if (i >= n) {
552 /* details for a context we didn't hear about in the last request */
553 continue;
554 }
555
Wink Saville43808972011-01-13 17:39:51 -0800556 // Assume no error
557 responses[i].status = 0;
558
559 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800560 err = at_tok_nextstr(&line, &out);
561 if (err < 0)
562 goto error;
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700563
564 int type_size = strlen(out) + 1;
565 responses[i].type = alloca(type_size);
566 strlcpy(responses[i].type, out, type_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800567
Wink Saville43808972011-01-13 17:39:51 -0800568 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800569 err = at_tok_nextstr(&line, &out);
570 if (err < 0)
571 goto error;
572
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700573 int ifname_size = strlen(PPP_TTY_PATH) + 1;
574 responses[i].ifname = alloca(ifname_size);
575 strlcpy(responses[i].ifname, PPP_TTY_PATH, ifname_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800576
577 err = at_tok_nextstr(&line, &out);
578 if (err < 0)
579 goto error;
580
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700581 int addresses_size = strlen(out) + 1;
582 responses[i].addresses = alloca(addresses_size);
583 strlcpy(responses[i].addresses, out, addresses_size);
Wink Saville43808972011-01-13 17:39:51 -0800584
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200585 if (isInEmulator()) {
586 /* We are in the emulator - the dns servers are listed
587 * by the following system properties, setup in
588 * /system/etc/init.goldfish.sh:
589 * - net.eth0.dns1
590 * - net.eth0.dns2
591 * - net.eth0.dns3
592 * - net.eth0.dns4
593 */
594 const int dnslist_sz = 128;
595 char* dnslist = alloca(dnslist_sz);
596 const char* separator = "";
597 int nn;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100598
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200599 dnslist[0] = 0;
600 for (nn = 1; nn <= 4; nn++) {
601 /* Probe net.eth0.dns<n> */
602 char propName[PROP_NAME_MAX];
603 char propValue[PROP_VALUE_MAX];
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100604
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200605 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100606
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200607 /* Ignore if undefined */
608 if (__system_property_get(propName, propValue) == 0) {
609 continue;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100610 }
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700611
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200612 /* Append the DNS IP address */
613 strlcat(dnslist, separator, dnslist_sz);
614 strlcat(dnslist, propValue, dnslist_sz);
615 separator = " ";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100616 }
David 'Digit' Turner834eca82016-06-22 12:10:01 +0200617 responses[i].dnses = dnslist;
618
619 /* There is only on gateway in the emulator */
620 responses[i].gateways = "10.0.2.2";
621 responses[i].mtu = DEFAULT_MTU;
622 }
623 else {
624 /* I don't know where we are, so use the public Google DNS
625 * servers by default and no gateway.
626 */
627 responses[i].dnses = "8.8.8.8 8.8.4.4";
628 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100629 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800630 }
631
632 at_response_free(p_response);
633
634 if (t != NULL)
635 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700636 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800637 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 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700640 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800641
642 return;
643
644error:
645 if (t != NULL)
646 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
647 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700648 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800649 NULL, 0);
650
651 at_response_free(p_response);
652}
653
654static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700655 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800656{
657 int err;
658 ATResponse *p_response = NULL;
659 int response = 0;
660 char *line;
661
662 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
663
664 if (err < 0 || p_response->success == 0) {
665 goto error;
666 }
667
668 line = p_response->p_intermediates->line;
669
670 err = at_tok_start(&line);
671
672 if (err < 0) {
673 goto error;
674 }
675
676 err = at_tok_nextint(&line, &response);
677
678 if (err < 0) {
679 goto error;
680 }
681
682 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
683 at_response_free(p_response);
684 return;
685error:
686 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800687 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800688 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
689}
690
Mark Salyzynba58c202014-03-12 15:20:22 -0700691static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800692{
693 RIL_onUnsolicitedResponse (
694 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
695 NULL, 0);
696}
697
Mark Salyzynba58c202014-03-12 15:20:22 -0700698static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800699{
700 int err;
701 ATResponse *p_response;
702 ATLine *p_cur;
703 int countCalls;
704 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700705 RIL_Call *p_calls;
706 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800707 int i;
708 int needRepoll = 0;
709
710#ifdef WORKAROUND_ERRONEOUS_ANSWER
711 int prevIncomingOrWaitingLine;
712
713 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
714 s_incomingOrWaitingLine = -1;
715#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
716
717 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
718
719 if (err != 0 || p_response->success == 0) {
720 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
721 return;
722 }
723
724 /* count the calls */
725 for (countCalls = 0, p_cur = p_response->p_intermediates
726 ; p_cur != NULL
727 ; p_cur = p_cur->p_next
728 ) {
729 countCalls++;
730 }
731
732 /* yes, there's an array of pointers and then an array of structures */
733
Wink Saville3d54e742009-05-18 18:00:44 -0700734 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
735 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
736 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800737
738 /* init the pointer array */
739 for(i = 0; i < countCalls ; i++) {
740 pp_calls[i] = &(p_calls[i]);
741 }
742
743 for (countValidCalls = 0, p_cur = p_response->p_intermediates
744 ; p_cur != NULL
745 ; p_cur = p_cur->p_next
746 ) {
747 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
748
749 if (err != 0) {
750 continue;
751 }
752
753#ifdef WORKAROUND_ERRONEOUS_ANSWER
754 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
755 || p_calls[countValidCalls].state == RIL_CALL_WAITING
756 ) {
757 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
758 }
759#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
760
761 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
762 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
763 ) {
764 needRepoll = 1;
765 }
766
767 countValidCalls++;
768 }
769
770#ifdef WORKAROUND_ERRONEOUS_ANSWER
771 // Basically:
772 // A call was incoming or waiting
773 // Now it's marked as active
774 // But we never answered it
775 //
776 // This is probably a bug, and the call will probably
777 // disappear from the call list in the next poll
778 if (prevIncomingOrWaitingLine >= 0
779 && s_incomingOrWaitingLine < 0
780 && s_expectAnswer == 0
781 ) {
782 for (i = 0; i < countValidCalls ; i++) {
783
784 if (p_calls[i].index == prevIncomingOrWaitingLine
785 && p_calls[i].state == RIL_CALL_ACTIVE
786 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
787 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800788 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800789 "Hit WORKAROUND_ERRONOUS_ANSWER case."
790 " Repoll count: %d\n", s_repollCallsCount);
791 s_repollCallsCount++;
792 goto error;
793 }
794 }
795 }
796
797 s_expectAnswer = 0;
798 s_repollCallsCount = 0;
799#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
800
Wink Saville3d54e742009-05-18 18:00:44 -0700801 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
802 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800803
804 at_response_free(p_response);
805
806#ifdef POLL_CALL_STATE
807 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
808 // smd, so we're forced to poll until the call ends.
809#else
810 if (needRepoll) {
811#endif
812 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
813 }
814
815 return;
816error:
817 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
818 at_response_free(p_response);
819}
820
Mark Salyzynba58c202014-03-12 15:20:22 -0700821static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800822{
823 RIL_Dial *p_dial;
824 char *cmd;
825 const char *clir;
826 int ret;
827
828 p_dial = (RIL_Dial *)data;
829
830 switch (p_dial->clir) {
831 case 1: clir = "I"; break; /*invocation*/
832 case 2: clir = "i"; break; /*suppression*/
833 default:
834 case 0: clir = ""; break; /*subscription default*/
835 }
836
837 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
838
839 ret = at_send_command(cmd, NULL);
840
841 free(cmd);
842
843 /* success or failure is ignored by the upper layer here.
844 it will call GET_CURRENT_CALLS and determine success that way */
845 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
846}
847
Mark Salyzynba58c202014-03-12 15:20:22 -0700848static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800849{
850 RIL_SMS_WriteArgs *p_args;
851 char *cmd;
852 int length;
853 int err;
854 ATResponse *p_response = NULL;
855
856 p_args = (RIL_SMS_WriteArgs *)data;
857
858 length = strlen(p_args->pdu)/2;
859 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
860
861 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
862
863 if (err != 0 || p_response->success == 0) goto error;
864
865 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
866 at_response_free(p_response);
867
868 return;
869error:
870 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
871 at_response_free(p_response);
872}
873
Mark Salyzynba58c202014-03-12 15:20:22 -0700874static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800875{
876 int *p_line;
877
878 int ret;
879 char *cmd;
880
881 p_line = (int *)data;
882
883 // 3GPP 22.030 6.5.5
884 // "Releases a specific active call X"
885 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
886
887 ret = at_send_command(cmd, NULL);
888
889 free(cmd);
890
891 /* success or failure is ignored by the upper layer here.
892 it will call GET_CURRENT_CALLS and determine success that way */
893 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
894}
895
Mark Salyzynba58c202014-03-12 15:20:22 -0700896static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800897{
898 ATResponse *p_response = NULL;
899 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800900 char *line;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700901 int count =0;
902 int numofElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
903 int response[numofElements];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800904
905 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
906
907 if (err < 0 || p_response->success == 0) {
908 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
909 goto error;
910 }
911
912 line = p_response->p_intermediates->line;
913
914 err = at_tok_start(&line);
915 if (err < 0) goto error;
916
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700917 for (count =0; count < numofElements; count ++) {
918 err = at_tok_nextint(&line, &(response[count]));
919 if (err < 0) goto error;
920 }
Chih-Wei Huang28059052012-04-30 01:13:27 +0800921
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700922 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800923
924 at_response_free(p_response);
925 return;
926
927error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800928 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800929 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
930 at_response_free(p_response);
931}
932
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700933/**
934 * networkModePossible. Decides whether the network mode is appropriate for the
935 * specified modem
936 */
937static int networkModePossible(ModemInfo *mdm, int nm)
938{
939 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
940 return 1;
941 }
942 return 0;
943}
Mark Salyzynba58c202014-03-12 15:20:22 -0700944static void requestSetPreferredNetworkType( int request __unused, void *data,
945 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700946{
947 ATResponse *p_response = NULL;
948 char *cmd = NULL;
949 int value = *(int *)data;
950 int current, old;
951 int err;
952 int32_t preferred = net2pmask[value];
953
Wink Saville4dcab4f2012-11-19 16:05:13 -0800954 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700955 if (!networkModePossible(sMdmInfo, value)) {
956 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
957 return;
958 }
959 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
960 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
961 return;
962 }
963 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800964 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700965 if (old != preferred) {
966 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800967 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700968 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
969 free(cmd);
970 if (err || !p_response->success) {
971 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
972 return;
973 }
974 PREFERRED_NETWORK(sMdmInfo) = value;
975 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
976 int current;
977 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
978 switch (res) {
979 case -1: // Error or unable to parse
980 break;
981 case 1: // Only able to parse current
982 case 0: // Both current and preferred were parsed
983 setRadioTechnology(sMdmInfo, current);
984 break;
985 }
986 }
987 }
988 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
989}
990
Mark Salyzynba58c202014-03-12 15:20:22 -0700991static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
992 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700993{
994 int preferred;
995 unsigned i;
996
997 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
998 case -1: // Error or unable to parse
999 case 1: // Only able to parse current
1000 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1001 break;
1002 case 0: // Both current and preferred were parsed
1003 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
1004 if (preferred == net2pmask[i]) {
1005 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
1006 return;
1007 }
1008 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001009 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001010 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1011 break;
1012 }
1013
1014}
1015
Mark Salyzynba58c202014-03-12 15:20:22 -07001016static void requestCdmaPrlVersion(int request __unused, void *data __unused,
1017 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001018{
1019 int err;
1020 char * responseStr;
1021 ATResponse *p_response = NULL;
1022 const char *cmd;
1023 char *line;
1024
1025 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1026 if (err < 0 || !p_response->success) goto error;
1027 line = p_response->p_intermediates->line;
1028 err = at_tok_start(&line);
1029 if (err < 0) goto error;
1030 err = at_tok_nextstr(&line, &responseStr);
1031 if (err < 0 || !responseStr) goto error;
1032 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1033 at_response_free(p_response);
1034 return;
1035error:
1036 at_response_free(p_response);
1037 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1038}
1039
Mark Salyzynba58c202014-03-12 15:20:22 -07001040static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1041 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001042{
1043 int err;
1044 char * responseStr;
1045 ATResponse *p_response = NULL;
1046 const char *cmd;
1047 const char *prefix;
1048 char *line, *p;
1049 int commas;
1050 int skip;
1051 int count = 4;
1052
1053 // Fixed values. TODO: query modem
1054 responseStr = strdup("1.0.0.0");
1055 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1056 free(responseStr);
1057}
1058
Mark Salyzynba58c202014-03-12 15:20:22 -07001059static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1060 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001061{
1062 int err;
1063 int response[4];
1064 char * responseStr[4];
1065 ATResponse *p_response = NULL;
1066 const char *cmd;
1067 const char *prefix;
1068 char *line, *p;
1069 int commas;
1070 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001071 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001072
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001073 // Fixed values. TODO: Query modem
1074 responseStr[0] = "----";
1075 responseStr[1] = "----";
1076 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001077
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001078 err = at_send_command_numeric("AT+CGSN", &p_response);
1079 if (err < 0 || p_response->success == 0) {
1080 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1081 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001082 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001083 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001084 }
1085
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001086 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1087 at_response_free(p_response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001088
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001089 return;
1090error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001091 RLOGE("requestCdmaDeviceIdentity must never return an error when radio is on");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001092 at_response_free(p_response);
1093 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1094}
1095
Mark Salyzynba58c202014-03-12 15:20:22 -07001096static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1097 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001098{
1099 int err;
1100 int *ss = (int *)data;
1101 ATResponse *p_response = NULL;
1102 char *cmd = NULL;
1103 char *line = NULL;
1104 int response;
1105
1106 asprintf(&cmd, "AT+CCSS?");
1107 if (!cmd) goto error;
1108
1109 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1110 if (err < 0 || !p_response->success)
1111 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001112
1113 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001114 err = at_tok_start(&line);
1115 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001116
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001117 err = at_tok_nextint(&line, &response);
1118 free(cmd);
1119 cmd = NULL;
1120
1121 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1122
1123 return;
1124error:
1125 free(cmd);
1126 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1127}
1128
Mark Salyzynba58c202014-03-12 15:20:22 -07001129static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001130 size_t datalen, RIL_Token t)
1131{
1132 int err;
1133 int *ss = (int *)data;
1134 ATResponse *p_response = NULL;
1135 char *cmd = NULL;
1136
1137 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001138 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001139 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1140 return;
1141 }
1142 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1143 if (!cmd) goto error;
1144
1145 err = at_send_command(cmd, &p_response);
1146 if (err < 0 || !p_response->success)
1147 goto error;
1148 free(cmd);
1149 cmd = NULL;
1150
1151 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1152
1153 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1154
1155 return;
1156error:
1157 free(cmd);
1158 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1159}
1160
Mark Salyzynba58c202014-03-12 15:20:22 -07001161static void requestCdmaSubscription(int request __unused, void *data __unused,
1162 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001163{
1164 int err;
1165 int response[5];
1166 char * responseStr[5];
1167 ATResponse *p_response = NULL;
1168 const char *cmd;
1169 const char *prefix;
1170 char *line, *p;
1171 int commas;
1172 int skip;
1173 int count = 5;
1174
1175 // Fixed values. TODO: Query modem
1176 responseStr[0] = "8587777777"; // MDN
1177 responseStr[1] = "1"; // SID
1178 responseStr[2] = "1"; // NID
1179 responseStr[3] = "8587777777"; // MIN
1180 responseStr[4] = "1"; // PRL Version
1181 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1182
1183 return;
1184error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001185 RLOGE("requestRegistrationState must never return an error when radio is on");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001186 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1187}
1188
Mark Salyzynba58c202014-03-12 15:20:22 -07001189static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1190 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001191{
1192 int roaming_pref = -1;
1193 ATResponse *p_response = NULL;
1194 char *line;
1195 int res;
1196
1197 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1198 if (res < 0 || !p_response->success) {
1199 goto error;
1200 }
1201 line = p_response->p_intermediates->line;
1202
1203 res = at_tok_start(&line);
1204 if (res < 0) goto error;
1205
1206 res = at_tok_nextint(&line, &roaming_pref);
1207 if (res < 0) goto error;
1208
1209 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1210 return;
1211error:
1212 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1213}
1214
Mark Salyzynba58c202014-03-12 15:20:22 -07001215static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1216 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001217{
1218 int *pref = (int *)data;
1219 ATResponse *p_response = NULL;
1220 char *line;
1221 int res;
1222 char *cmd = NULL;
1223
1224 asprintf(&cmd, "AT+WRMP=%d", *pref);
1225 if (cmd == NULL) goto error;
1226
1227 res = at_send_command(cmd, &p_response);
1228 if (res < 0 || !p_response->success)
1229 goto error;
1230
1231 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1232 free(cmd);
1233 return;
1234error:
1235 free(cmd);
1236 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1237}
1238
1239static int parseRegistrationState(char *str, int *type, int *items, int **response)
1240{
1241 int err;
1242 char *line = str, *p;
1243 int *resp = NULL;
1244 int skip;
1245 int count = 3;
1246 int commas;
1247
Wink Saville4dcab4f2012-11-19 16:05:13 -08001248 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001249 err = at_tok_start(&line);
1250 if (err < 0) goto error;
1251
1252 /* Ok you have to be careful here
1253 * The solicited version of the CREG response is
1254 * +CREG: n, stat, [lac, cid]
1255 * and the unsolicited version is
1256 * +CREG: stat, [lac, cid]
1257 * The <n> parameter is basically "is unsolicited creg on?"
1258 * which it should always be
1259 *
1260 * Now we should normally get the solicited version here,
1261 * but the unsolicited version could have snuck in
1262 * so we have to handle both
1263 *
1264 * Also since the LAC and CID are only reported when registered,
1265 * we can have 1, 2, 3, or 4 arguments here
1266 *
1267 * finally, a +CGREG: answer may have a fifth value that corresponds
1268 * to the network type, as in;
1269 *
1270 * +CGREG: n, stat [,lac, cid [,networkType]]
1271 */
1272
1273 /* count number of commas */
1274 commas = 0;
1275 for (p = line ; *p != '\0' ;p++) {
1276 if (*p == ',') commas++;
1277 }
1278
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001279 resp = (int *)calloc(commas + 1, sizeof(int));
1280 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001281 switch (commas) {
1282 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001283 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001284 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001285 resp[1] = -1;
1286 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001287 break;
1288
1289 case 1: /* +CREG: <n>, <stat> */
1290 err = at_tok_nextint(&line, &skip);
1291 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001292 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001293 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001294 resp[1] = -1;
1295 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001296 if (err < 0) goto error;
1297 break;
1298
1299 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001300 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001301 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001302 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001303 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001304 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001305 if (err < 0) goto error;
1306 break;
1307 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1308 err = at_tok_nextint(&line, &skip);
1309 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001310 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001311 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001312 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001313 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001314 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001315 if (err < 0) goto error;
1316 break;
1317 /* special case for CGREG, there is a fourth parameter
1318 * that is the network type (unknown/gprs/edge/umts)
1319 */
1320 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1321 err = at_tok_nextint(&line, &skip);
1322 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001323 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001324 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001325 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001326 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001327 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001328 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001329 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001330 if (err < 0) goto error;
1331 count = 4;
1332 break;
1333 default:
1334 goto error;
1335 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001336 s_lac = resp[1];
1337 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001338 if (response)
1339 *response = resp;
1340 if (items)
1341 *items = commas + 1;
1342 if (type)
1343 *type = techFromModemType(TECH(sMdmInfo));
1344 return 0;
1345error:
1346 free(resp);
1347 return -1;
1348}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001349
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001350#define REG_STATE_LEN 15
1351#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001352static void requestRegistrationState(int request, void *data __unused,
1353 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001354{
1355 int err;
1356 int *registration;
1357 char **responseStr = NULL;
1358 ATResponse *p_response = NULL;
1359 const char *cmd;
1360 const char *prefix;
1361 char *line;
1362 int i = 0, j, numElements = 0;
1363 int count = 3;
1364 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001365
Wink Saville4dcab4f2012-11-19 16:05:13 -08001366 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001367 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1368 cmd = "AT+CREG?";
1369 prefix = "+CREG:";
1370 numElements = REG_STATE_LEN;
1371 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1372 cmd = "AT+CGREG?";
1373 prefix = "+CGREG:";
1374 numElements = REG_DATA_STATE_LEN;
1375 } else {
1376 assert(0);
1377 goto error;
1378 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001379
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001380 err = at_send_command_singleline(cmd, prefix, &p_response);
1381
1382 if (err != 0) goto error;
1383
1384 line = p_response->p_intermediates->line;
1385
1386 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1387
1388 responseStr = malloc(numElements * sizeof(char *));
1389 if (!responseStr) goto error;
1390 memset(responseStr, 0, numElements * sizeof(char *));
1391 /**
1392 * The first '4' bytes for both registration states remain the same.
1393 * But if the request is 'DATA_REGISTRATION_STATE',
1394 * the 5th and 6th byte(s) are optional.
1395 */
1396 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001397 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001398 // TODO: Query modem
1399 startfrom = 3;
1400 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1401 asprintf(&responseStr[3], "8"); // EvDo revA
1402 asprintf(&responseStr[4], "1"); // BSID
1403 asprintf(&responseStr[5], "123"); // Latitude
1404 asprintf(&responseStr[6], "222"); // Longitude
1405 asprintf(&responseStr[7], "0"); // CSS Indicator
1406 asprintf(&responseStr[8], "4"); // SID
1407 asprintf(&responseStr[9], "65535"); // NID
1408 asprintf(&responseStr[10], "0"); // Roaming indicator
1409 asprintf(&responseStr[11], "1"); // System is in PRL
1410 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1411 asprintf(&responseStr[13], "0"); // Reason for denial
1412 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1413 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1414 asprintf(&responseStr[3], "8"); // Available data radio technology
1415 }
1416 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001417 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001418 startfrom = 0;
1419 asprintf(&responseStr[1], "%x", registration[1]);
1420 asprintf(&responseStr[2], "%x", registration[2]);
1421 if (count > 3)
1422 asprintf(&responseStr[3], "%d", registration[3]);
1423 }
1424 asprintf(&responseStr[0], "%d", registration[0]);
1425
1426 /**
1427 * Optional bytes for DATA_REGISTRATION_STATE request
1428 * 4th byte : Registration denial code
1429 * 5th byte : The max. number of simultaneous Data Calls
1430 */
1431 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1432 // asprintf(&responseStr[4], "3");
1433 // asprintf(&responseStr[5], "1");
1434 }
1435
1436 for (j = startfrom; j < numElements; j++) {
1437 if (!responseStr[i]) goto error;
1438 }
1439 free(registration);
1440 registration = NULL;
1441
1442 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1443 for (j = 0; j < numElements; j++ ) {
1444 free(responseStr[j]);
1445 responseStr[j] = NULL;
1446 }
1447 free(responseStr);
1448 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001449 at_response_free(p_response);
1450
1451 return;
1452error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001453 if (responseStr) {
1454 for (j = 0; j < numElements; j++) {
1455 free(responseStr[j]);
1456 responseStr[j] = NULL;
1457 }
1458 free(responseStr);
1459 responseStr = NULL;
1460 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001461 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001462 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1463 at_response_free(p_response);
1464}
1465
Mark Salyzynba58c202014-03-12 15:20:22 -07001466static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001467{
1468 int err;
1469 int i;
1470 int skip;
1471 ATLine *p_cur;
1472 char *response[3];
1473
1474 memset(response, 0, sizeof(response));
1475
1476 ATResponse *p_response = NULL;
1477
1478 err = at_send_command_multiline(
1479 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1480 "+COPS:", &p_response);
1481
1482 /* we expect 3 lines here:
1483 * +COPS: 0,0,"T - Mobile"
1484 * +COPS: 0,1,"TMO"
1485 * +COPS: 0,2,"310170"
1486 */
1487
1488 if (err != 0) goto error;
1489
1490 for (i = 0, p_cur = p_response->p_intermediates
1491 ; p_cur != NULL
1492 ; p_cur = p_cur->p_next, i++
1493 ) {
1494 char *line = p_cur->line;
1495
1496 err = at_tok_start(&line);
1497 if (err < 0) goto error;
1498
1499 err = at_tok_nextint(&line, &skip);
1500 if (err < 0) goto error;
1501
1502 // If we're unregistered, we may just get
1503 // a "+COPS: 0" response
1504 if (!at_tok_hasmore(&line)) {
1505 response[i] = NULL;
1506 continue;
1507 }
1508
1509 err = at_tok_nextint(&line, &skip);
1510 if (err < 0) goto error;
1511
1512 // a "+COPS: 0, n" response is also possible
1513 if (!at_tok_hasmore(&line)) {
1514 response[i] = NULL;
1515 continue;
1516 }
1517
1518 err = at_tok_nextstr(&line, &(response[i]));
1519 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001520 // Simple assumption that mcc and mnc are 3 digits each
1521 if (strlen(response[i]) == 6) {
1522 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1523 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1524 }
1525 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001526 }
1527
1528 if (i != 3) {
1529 /* expect 3 lines exactly */
1530 goto error;
1531 }
1532
1533 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1534 at_response_free(p_response);
1535
1536 return;
1537error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001538 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001539 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1540 at_response_free(p_response);
1541}
1542
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001543static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1544{
1545 int err = 1; // Set to go to error:
1546 RIL_SMS_Response response;
1547 RIL_CDMA_SMS_Message* rcsm;
1548
Mark Salyzynba58c202014-03-12 15:20:22 -07001549 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001550 datalen, sizeof(RIL_CDMA_SMS_Message));
1551
1552 // verify data content to test marshalling/unmarshalling:
1553 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001554 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001555 uServicecategory=%d, sAddress.digit_mode=%d, \
1556 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1557 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1558 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1559 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1560
1561 if (err != 0) goto error;
1562
1563 // Cdma Send SMS implementation will go here:
1564 // But it is not implemented yet.
1565
1566 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001567 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001568 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1569 return;
1570
1571error:
1572 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001573 response.messageRef = -1;
1574 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001575}
1576
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001577static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1578{
1579 int err;
1580 const char *smsc;
1581 const char *pdu;
1582 int tpLayerLength;
1583 char *cmd1, *cmd2;
1584 RIL_SMS_Response response;
1585 ATResponse *p_response = NULL;
1586
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001587 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001588 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001589
1590 if (s_ims_gsm_fail != 0) goto error;
1591 if (s_ims_gsm_retry != 0) goto error2;
1592
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001593 smsc = ((const char **)data)[0];
1594 pdu = ((const char **)data)[1];
1595
1596 tpLayerLength = strlen(pdu)/2;
1597
1598 // "NULL for default SMSC"
1599 if (smsc == NULL) {
1600 smsc= "00";
1601 }
1602
1603 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1604 asprintf(&cmd2, "%s%s", smsc, pdu);
1605
1606 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1607
Daniele Palmasa5c743e2015-05-06 11:47:59 +02001608 free(cmd1);
1609 free(cmd2);
1610
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001611 if (err != 0 || p_response->success == 0) goto error;
1612
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001613 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001614 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001615 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1616 at_response_free(p_response);
1617
1618 return;
1619error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001620 response.messageRef = -2;
1621 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001622 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001623 return;
1624error2:
1625 // send retry error.
1626 response.messageRef = -1;
1627 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1628 at_response_free(p_response);
1629 return;
1630 }
1631
1632static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1633{
1634 RIL_IMS_SMS_Message *p_args;
1635 RIL_SMS_Response response;
1636
1637 memset(&response, 0, sizeof(response));
1638
Mark Salyzynba58c202014-03-12 15:20:22 -07001639 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001640 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1641 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1642 datalen, s_ims_registered, s_ims_services, s_ims_format,
1643 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1644 s_ims_gsm_retry);
1645
1646 // figure out if this is gsm/cdma format
1647 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1648 p_args = (RIL_IMS_SMS_Message *)data;
1649
1650 if (0 != s_ims_cause_perm_failure ) goto error;
1651
1652 // want to fail over ims and this is first request over ims
1653 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1654
1655 if (RADIO_TECH_3GPP == p_args->tech) {
1656 return requestSendSMS(p_args->message.gsmMessage,
1657 datalen - sizeof(RIL_RadioTechnologyFamily),
1658 t);
1659 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1660 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1661 datalen - sizeof(RIL_RadioTechnologyFamily),
1662 t);
1663 } else {
1664 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1665 }
1666
1667error:
1668 response.messageRef = -2;
1669 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1670 return;
1671
1672error2:
1673 response.messageRef = -1;
1674 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001675}
1676
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001677static void requestSimOpenChannel(void *data, size_t datalen, RIL_Token t)
1678{
1679 ATResponse *p_response = NULL;
1680 int32_t session_id;
1681 int err;
1682 char cmd[32];
1683 char dummy;
1684 char *line;
1685
1686 // Max length is 16 bytes according to 3GPP spec 27.007 section 8.45
1687 if (data == NULL || datalen == 0 || datalen > 16) {
1688 ALOGE("Invalid data passed to requestSimOpenChannel");
1689 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1690 return;
1691 }
1692
1693 snprintf(cmd, sizeof(cmd), "AT+CCHO=%s", data);
1694
1695 err = at_send_command_numeric(cmd, &p_response);
1696 if (err < 0 || p_response == NULL || p_response->success == 0) {
1697 ALOGE("Error %d opening logical channel: %d",
1698 err, p_response ? p_response->success : 0);
1699 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1700 at_response_free(p_response);
1701 return;
1702 }
1703
1704 // Ensure integer only by scanning for an extra char but expect one result
1705 line = p_response->p_intermediates->line;
1706 if (sscanf(line, "%" SCNd32 "%c", &session_id, &dummy) != 1) {
1707 ALOGE("Invalid AT response, expected integer, was '%s'", line);
1708 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1709 return;
1710 }
1711
1712 RIL_onRequestComplete(t, RIL_E_SUCCESS, &session_id, sizeof(&session_id));
1713 at_response_free(p_response);
1714}
1715
1716static void requestSimCloseChannel(void *data, size_t datalen, RIL_Token t)
1717{
1718 ATResponse *p_response = NULL;
1719 int32_t session_id;
1720 int err;
1721 char cmd[32];
1722
1723 if (data == NULL || datalen != sizeof(session_id)) {
1724 ALOGE("Invalid data passed to requestSimCloseChannel");
1725 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1726 return;
1727 }
1728 session_id = ((int32_t *)data)[0];
1729 snprintf(cmd, sizeof(cmd), "AT+CCHC=%" PRId32, session_id);
1730 err = at_send_command_singleline(cmd, "+CCHC", &p_response);
1731
1732 if (err < 0 || p_response == NULL || p_response->success == 0) {
1733 ALOGE("Error %d closing logical channel %d: %d",
1734 err, session_id, p_response ? p_response->success : 0);
1735 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1736 at_response_free(p_response);
1737 return;
1738 }
1739
1740 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1741
1742 at_response_free(p_response);
1743}
1744
1745static void requestSimTransmitApduChannel(void *data,
1746 size_t datalen,
1747 RIL_Token t)
1748{
1749 ATResponse *p_response = NULL;
1750 int err;
1751 char *cmd;
1752 char *line;
1753 size_t cmd_size;
1754 RIL_SIM_IO_Response sim_response;
1755 RIL_SIM_APDU *apdu = (RIL_SIM_APDU *)data;
1756
1757 if (apdu == NULL || datalen != sizeof(RIL_SIM_APDU)) {
1758 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1759 return;
1760 }
1761
1762 cmd_size = 10 + (apdu->data ? strlen(apdu->data) : 0);
1763 asprintf(&cmd, "AT+CGLA=%d,%d,%02x%02x%02x%02x%02x%s",
1764 apdu->sessionid, cmd_size, apdu->cla, apdu->instruction,
1765 apdu->p1, apdu->p2, apdu->p3, apdu->data ? apdu->data : "");
1766
1767 err = at_send_command_singleline(cmd, "+CGLA", &p_response);
1768 free(cmd);
1769 if (err < 0 || p_response == NULL || p_response->success == 0) {
1770 ALOGE("Error %d transmitting APDU: %d",
1771 err, p_response ? p_response->success : 0);
1772 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1773 at_response_free(p_response);
1774 return;
1775 }
1776
1777 line = p_response->p_intermediates->line;
1778 err = parseSimResponseLine(line, &sim_response);
1779
1780 if (err == 0) {
1781 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1782 &sim_response, sizeof(sim_response));
1783 } else {
1784 ALOGE("Error %d parsing SIM response line: %s", err, line);
1785 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1786 }
1787 at_response_free(p_response);
1788}
1789
Wink Savillef4c4d362009-04-02 01:37:03 -07001790static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001791{
1792 const char *apn;
1793 char *cmd;
1794 int err;
1795 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001796
Wink Savillef4c4d362009-04-02 01:37:03 -07001797 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001798
1799#ifdef USE_TI_COMMANDS
1800 // Config for multislot class 10 (probably default anyway eh?)
1801 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1802 NULL);
1803
1804 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1805#endif /* USE_TI_COMMANDS */
1806
1807 int fd, qmistatus;
1808 size_t cur = 0;
1809 size_t len;
1810 ssize_t written, rlen;
1811 char status[32] = {0};
1812 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001813 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001814
Wink Saville4dcab4f2012-11-19 16:05:13 -08001815 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001816
1817 fd = open ("/dev/qmi", O_RDWR);
1818 if (fd >= 0) { /* the device doesn't exist on the emulator */
1819
Wink Saville4dcab4f2012-11-19 16:05:13 -08001820 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001821 asprintf(&cmd, "up:%s", apn);
1822 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001823
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001824 while (cur < len) {
1825 do {
1826 written = write (fd, cmd + cur, len - cur);
1827 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001828
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001829 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001830 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001831 close(fd);
1832 goto error;
1833 }
1834
1835 cur += written;
1836 }
1837
1838 // wait for interface to come online
1839
1840 do {
1841 sleep(1);
1842 do {
1843 rlen = read(fd, status, 31);
1844 } while (rlen < 0 && errno == EINTR);
1845
1846 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001847 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001848 close(fd);
1849 goto error;
1850 } else {
1851 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001852 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001853 }
1854 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1855
1856 close(fd);
1857
1858 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001859 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001860 goto error;
1861 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001862
1863 qmistatus = system("netcfg rmnet0 dhcp");
1864
Wink Saville4dcab4f2012-11-19 16:05:13 -08001865 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001866
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001867 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001868
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001869 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001870
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001871 if (datalen > 6 * sizeof(char *)) {
1872 pdp_type = ((const char **)data)[6];
1873 } else {
1874 pdp_type = "IP";
1875 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001876
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001877 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001878 //FIXME check for error here
1879 err = at_send_command(cmd, NULL);
1880 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001881
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001882 // Set required QoS params to default
1883 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001884
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001885 // Set minimum QoS params to default
1886 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001887
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001888 // packet-domain event reporting
1889 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001890
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001891 // Hangup anything that's happening there now
1892 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001893
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001894 // Start data on PDP context 1
1895 err = at_send_command("ATD*99***1#", &p_response);
1896
1897 if (err < 0 || p_response->success == 0) {
1898 goto error;
1899 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001900 }
1901
Wink Saville43808972011-01-13 17:39:51 -08001902 requestOrSendDataCallList(&t);
1903
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001904 at_response_free(p_response);
1905
1906 return;
1907error:
1908 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1909 at_response_free(p_response);
1910
1911}
1912
Mark Salyzynba58c202014-03-12 15:20:22 -07001913static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001914{
1915 int ackSuccess;
1916 int err;
1917
1918 ackSuccess = ((int *)data)[0];
1919
1920 if (ackSuccess == 1) {
1921 err = at_send_command("AT+CNMA=1", NULL);
1922 } else if (ackSuccess == 0) {
1923 err = at_send_command("AT+CNMA=2", NULL);
1924 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001925 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001926 goto error;
1927 }
1928
1929 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1930error:
1931 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1932
1933}
1934
Mark Salyzynba58c202014-03-12 15:20:22 -07001935static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001936{
1937 ATResponse *p_response = NULL;
1938 RIL_SIM_IO_Response sr;
1939 int err;
1940 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001941 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001942 char *line;
1943
1944 memset(&sr, 0, sizeof(sr));
1945
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001946 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001947
1948 /* FIXME handle pin2 */
1949
1950 if (p_args->data == NULL) {
1951 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1952 p_args->command, p_args->fileid,
1953 p_args->p1, p_args->p2, p_args->p3);
1954 } else {
1955 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1956 p_args->command, p_args->fileid,
1957 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1958 }
1959
1960 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1961
1962 if (err < 0 || p_response->success == 0) {
1963 goto error;
1964 }
1965
1966 line = p_response->p_intermediates->line;
1967
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07001968 err = parseSimResponseLine(line, &sr);
1969 if (err < 0) {
1970 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001971 }
1972
1973 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1974 at_response_free(p_response);
1975 free(cmd);
1976
1977 return;
1978error:
1979 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1980 at_response_free(p_response);
1981 free(cmd);
1982
1983}
1984
1985static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1986{
1987 ATResponse *p_response = NULL;
1988 int err;
1989 char* cmd = NULL;
1990 const char** strings = (const char**)data;;
1991
1992 if ( datalen == sizeof(char*) ) {
1993 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1994 } else if ( datalen == 2*sizeof(char*) ) {
1995 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1996 } else
1997 goto error;
1998
1999 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
2000 free(cmd);
2001
2002 if (err < 0 || p_response->success == 0) {
2003error:
2004 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
2005 } else {
2006 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2007 }
2008 at_response_free(p_response);
2009}
2010
2011
Mark Salyzynba58c202014-03-12 15:20:22 -07002012static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002013{
2014 const char *ussdRequest;
2015
2016 ussdRequest = (char *)(data);
2017
2018
2019 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2020
2021// @@@ TODO
2022
2023}
2024
Mark Salyzynba58c202014-03-12 15:20:22 -07002025static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002026{
2027 int err;
2028 ATResponse *p_response = NULL;
2029
2030 err = at_send_command("AT+WSOS=0", &p_response);
2031
2032 if (err < 0 || p_response->success == 0) {
2033 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2034 return;
2035 }
2036
2037 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2038}
2039
2040// TODO: Use all radio types
2041static int techFromModemType(int mdmtype)
2042{
2043 int ret = -1;
2044 switch (1 << mdmtype) {
2045 case MDM_CDMA:
2046 ret = RADIO_TECH_1xRTT;
2047 break;
2048 case MDM_EVDO:
2049 ret = RADIO_TECH_EVDO_A;
2050 break;
2051 case MDM_GSM:
2052 ret = RADIO_TECH_GPRS;
2053 break;
2054 case MDM_WCDMA:
2055 ret = RADIO_TECH_HSPA;
2056 break;
2057 case MDM_LTE:
2058 ret = RADIO_TECH_LTE;
2059 break;
2060 }
2061 return ret;
2062}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002063
Mark Salyzynba58c202014-03-12 15:20:22 -07002064static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002065{
2066 uint64_t curTime = ril_nano_time();
2067 RIL_CellInfo ci[1] =
2068 {
2069 { // ci[0]
2070 1, // cellInfoType
2071 1, // registered
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002072 RIL_TIMESTAMP_TYPE_MODEM,
Wink Saville8a9e0212013-04-09 12:11:38 -07002073 curTime - 1000, // Fake some time in the past
2074 { // union CellInfo
2075 { // RIL_CellInfoGsm gsm
2076 { // gsm.cellIdneityGsm
2077 s_mcc, // mcc
2078 s_mnc, // mnc
2079 s_lac, // lac
2080 s_cid, // cid
Wink Saville8a9e0212013-04-09 12:11:38 -07002081 },
2082 { // gsm.signalStrengthGsm
2083 10, // signalStrength
2084 0 // bitErrorRate
2085 }
2086 }
2087 }
2088 }
2089 };
2090
2091 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
2092}
2093
2094
Sanket Padawef0c8ca72016-06-30 15:01:08 -07002095static void requestSetCellInfoListRate(void *data, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07002096{
2097 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
2098 // will be sent.
2099 assert (datalen == sizeof(int));
2100 s_cell_info_rate_ms = ((int *)data)[0];
2101
2102 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2103}
2104
Etan Cohend3652192014-06-20 08:28:44 -07002105static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
2106{
2107 // TODO - hook this up with real query/info from radio.
2108
2109 RIL_HardwareConfig hwCfg;
2110
2111 RIL_UNUSED_PARM(data);
2112 RIL_UNUSED_PARM(datalen);
2113
2114 hwCfg.type = -1;
2115
2116 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
2117}
2118
2119
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002120/*** Callback methods from the RIL library to us ***/
2121
2122/**
2123 * Call from RIL to us to make a RIL_REQUEST
2124 *
2125 * Must be completed with a call to RIL_onRequestComplete()
2126 *
2127 * RIL_onRequestComplete() may be called from any thread, before or after
2128 * this function returns.
2129 *
2130 * Will always be called from the same thread, so returning here implies
2131 * that the radio is ready to process another command (whether or not
2132 * the previous command has completed).
2133 */
2134static void
2135onRequest (int request, void *data, size_t datalen, RIL_Token t)
2136{
2137 ATResponse *p_response;
2138 int err;
2139
Wink Saville4dcab4f2012-11-19 16:05:13 -08002140 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002141
2142 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2143 * when RADIO_STATE_UNAVAILABLE.
2144 */
2145 if (sState == RADIO_STATE_UNAVAILABLE
2146 && request != RIL_REQUEST_GET_SIM_STATUS
2147 ) {
2148 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2149 return;
2150 }
2151
2152 /* Ignore all non-power requests when RADIO_STATE_OFF
2153 * (except RIL_REQUEST_GET_SIM_STATUS)
2154 */
2155 if (sState == RADIO_STATE_OFF
2156 && !(request == RIL_REQUEST_RADIO_POWER
2157 || request == RIL_REQUEST_GET_SIM_STATUS)
2158 ) {
2159 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2160 return;
2161 }
2162
2163 switch (request) {
2164 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002165 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002166 char *p_buffer;
2167 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002168
Wink Savillef6aa7c12009-04-30 14:20:52 -07002169 int result = getCardStatus(&p_card_status);
2170 if (result == RIL_E_SUCCESS) {
2171 p_buffer = (char *)p_card_status;
2172 buffer_size = sizeof(*p_card_status);
2173 } else {
2174 p_buffer = NULL;
2175 buffer_size = 0;
2176 }
2177 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2178 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002179 break;
2180 }
2181 case RIL_REQUEST_GET_CURRENT_CALLS:
2182 requestGetCurrentCalls(data, datalen, t);
2183 break;
2184 case RIL_REQUEST_DIAL:
2185 requestDial(data, datalen, t);
2186 break;
2187 case RIL_REQUEST_HANGUP:
2188 requestHangup(data, datalen, t);
2189 break;
2190 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
2191 // 3GPP 22.030 6.5.5
2192 // "Releases all held calls or sets User Determined User Busy
2193 // (UDUB) for a waiting call."
2194 at_send_command("AT+CHLD=0", NULL);
2195
2196 /* success or failure is ignored by the upper layer here.
2197 it will call GET_CURRENT_CALLS and determine success that way */
2198 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2199 break;
2200 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
2201 // 3GPP 22.030 6.5.5
2202 // "Releases all active calls (if any exist) and accepts
2203 // the other (held or waiting) call."
2204 at_send_command("AT+CHLD=1", NULL);
2205
2206 /* success or failure is ignored by the upper layer here.
2207 it will call GET_CURRENT_CALLS and determine success that way */
2208 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2209 break;
2210 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
2211 // 3GPP 22.030 6.5.5
2212 // "Places all active calls (if any exist) on hold and accepts
2213 // the other (held or waiting) call."
2214 at_send_command("AT+CHLD=2", NULL);
2215
2216#ifdef WORKAROUND_ERRONEOUS_ANSWER
2217 s_expectAnswer = 1;
2218#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2219
2220 /* success or failure is ignored by the upper layer here.
2221 it will call GET_CURRENT_CALLS and determine success that way */
2222 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2223 break;
2224 case RIL_REQUEST_ANSWER:
2225 at_send_command("ATA", NULL);
2226
2227#ifdef WORKAROUND_ERRONEOUS_ANSWER
2228 s_expectAnswer = 1;
2229#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2230
2231 /* success or failure is ignored by the upper layer here.
2232 it will call GET_CURRENT_CALLS and determine success that way */
2233 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2234 break;
2235 case RIL_REQUEST_CONFERENCE:
2236 // 3GPP 22.030 6.5.5
2237 // "Adds a held call to the conversation"
2238 at_send_command("AT+CHLD=3", NULL);
2239
2240 /* success or failure is ignored by the upper layer here.
2241 it will call GET_CURRENT_CALLS and determine success that way */
2242 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2243 break;
2244 case RIL_REQUEST_UDUB:
2245 /* user determined user busy */
2246 /* sometimes used: ATH */
2247 at_send_command("ATH", NULL);
2248
2249 /* success or failure is ignored by the upper layer here.
2250 it will call GET_CURRENT_CALLS and determine success that way */
2251 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2252 break;
2253
2254 case RIL_REQUEST_SEPARATE_CONNECTION:
2255 {
2256 char cmd[12];
2257 int party = ((int*)data)[0];
2258
2259 // Make sure that party is in a valid range.
2260 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2261 // It's sufficient for us to just make sure it's single digit.)
2262 if (party > 0 && party < 10) {
2263 sprintf(cmd, "AT+CHLD=2%d", party);
2264 at_send_command(cmd, NULL);
2265 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2266 } else {
2267 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2268 }
2269 }
2270 break;
2271
2272 case RIL_REQUEST_SIGNAL_STRENGTH:
2273 requestSignalStrength(data, datalen, t);
2274 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002275 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2276 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002277 requestRegistrationState(request, data, datalen, t);
2278 break;
2279 case RIL_REQUEST_OPERATOR:
2280 requestOperator(data, datalen, t);
2281 break;
2282 case RIL_REQUEST_RADIO_POWER:
2283 requestRadioPower(data, datalen, t);
2284 break;
2285 case RIL_REQUEST_DTMF: {
2286 char c = ((char *)data)[0];
2287 char *cmd;
2288 asprintf(&cmd, "AT+VTS=%c", (int)c);
2289 at_send_command(cmd, NULL);
2290 free(cmd);
2291 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2292 break;
2293 }
2294 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302295 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002296 requestSendSMS(data, datalen, t);
2297 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002298 case RIL_REQUEST_CDMA_SEND_SMS:
2299 requestCdmaSendSMS(data, datalen, t);
2300 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002301 case RIL_REQUEST_IMS_SEND_SMS:
2302 requestImsSendSMS(data, datalen, t);
2303 break;
Bjoern Johansson1fdedbd2016-10-27 16:36:29 -07002304 case RIL_REQUEST_SIM_OPEN_CHANNEL:
2305 requestSimOpenChannel(data, datalen, t);
2306 break;
2307 case RIL_REQUEST_SIM_CLOSE_CHANNEL:
2308 requestSimCloseChannel(data, datalen, t);
2309 break;
2310 case RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL:
2311 requestSimTransmitApduChannel(data, datalen, t);
2312 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002313 case RIL_REQUEST_SETUP_DATA_CALL:
2314 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002315 break;
2316 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2317 requestSMSAcknowledge(data, datalen, t);
2318 break;
2319
2320 case RIL_REQUEST_GET_IMSI:
2321 p_response = NULL;
2322 err = at_send_command_numeric("AT+CIMI", &p_response);
2323
2324 if (err < 0 || p_response->success == 0) {
2325 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2326 } else {
2327 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2328 p_response->p_intermediates->line, sizeof(char *));
2329 }
2330 at_response_free(p_response);
2331 break;
2332
2333 case RIL_REQUEST_GET_IMEI:
2334 p_response = NULL;
2335 err = at_send_command_numeric("AT+CGSN", &p_response);
2336
2337 if (err < 0 || p_response->success == 0) {
2338 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2339 } else {
2340 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2341 p_response->p_intermediates->line, sizeof(char *));
2342 }
2343 at_response_free(p_response);
2344 break;
2345
2346 case RIL_REQUEST_SIM_IO:
2347 requestSIM_IO(data,datalen,t);
2348 break;
2349
2350 case RIL_REQUEST_SEND_USSD:
2351 requestSendUSSD(data, datalen, t);
2352 break;
2353
2354 case RIL_REQUEST_CANCEL_USSD:
2355 p_response = NULL;
2356 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2357
2358 if (err < 0 || p_response->success == 0) {
2359 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2360 } else {
2361 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2362 p_response->p_intermediates->line, sizeof(char *));
2363 }
2364 at_response_free(p_response);
2365 break;
2366
2367 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2368 at_send_command("AT+COPS=0", NULL);
2369 break;
2370
Wink Savillef4c4d362009-04-02 01:37:03 -07002371 case RIL_REQUEST_DATA_CALL_LIST:
2372 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002373 break;
2374
2375 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2376 requestQueryNetworkSelectionMode(data, datalen, t);
2377 break;
2378
2379 case RIL_REQUEST_OEM_HOOK_RAW:
2380 // echo back data
2381 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2382 break;
2383
2384
2385 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2386 int i;
2387 const char ** cur;
2388
Wink Saville4dcab4f2012-11-19 16:05:13 -08002389 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002390
2391
2392 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2393 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002394 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002395 }
2396
2397 // echo back strings
2398 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2399 break;
2400 }
2401
2402 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2403 requestWriteSmsToSim(data, datalen, t);
2404 break;
2405
2406 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2407 char * cmd;
2408 p_response = NULL;
2409 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2410 err = at_send_command(cmd, &p_response);
2411 free(cmd);
2412 if (err < 0 || p_response->success == 0) {
2413 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2414 } else {
2415 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2416 }
2417 at_response_free(p_response);
2418 break;
2419 }
2420
2421 case RIL_REQUEST_ENTER_SIM_PIN:
2422 case RIL_REQUEST_ENTER_SIM_PUK:
2423 case RIL_REQUEST_ENTER_SIM_PIN2:
2424 case RIL_REQUEST_ENTER_SIM_PUK2:
2425 case RIL_REQUEST_CHANGE_SIM_PIN:
2426 case RIL_REQUEST_CHANGE_SIM_PIN2:
2427 requestEnterSimPin(data, datalen, t);
2428 break;
2429
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002430 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2431 int reply[2];
2432 //0==unregistered, 1==registered
2433 reply[0] = s_ims_registered;
2434
2435 //to be used when changed to include service supporated info
2436 //reply[1] = s_ims_services;
2437
2438 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2439 reply[1] = s_ims_format;
2440
2441 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2442 reply[0], reply[1]);
2443 if (reply[1] != -1) {
2444 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2445 } else {
2446 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2447 }
2448 break;
2449 }
2450
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002451 case RIL_REQUEST_VOICE_RADIO_TECH:
2452 {
2453 int tech = techFromModemType(TECH(sMdmInfo));
2454 if (tech < 0 )
2455 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2456 else
2457 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2458 }
2459 break;
2460 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2461 requestSetPreferredNetworkType(request, data, datalen, t);
2462 break;
2463
2464 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2465 requestGetPreferredNetworkType(request, data, datalen, t);
2466 break;
2467
Jun Tian58027012013-07-30 11:07:22 +08002468 case RIL_REQUEST_GET_CELL_INFO_LIST:
2469 requestGetCellInfoList(data, datalen, t);
2470 break;
2471
2472 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2473 requestSetCellInfoListRate(data, datalen, t);
2474 break;
2475
Etan Cohend3652192014-06-20 08:28:44 -07002476 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2477 requestGetHardwareConfig(data, datalen, t);
2478 break;
2479
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002480 case RIL_REQUEST_SHUTDOWN:
2481 requestShutdown(t);
2482 break;
2483
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002484 /* CDMA Specific Requests */
2485 case RIL_REQUEST_BASEBAND_VERSION:
2486 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2487 requestCdmaBaseBandVersion(request, data, datalen, t);
2488 break;
2489 } // Fall-through if tech is not cdma
2490
2491 case RIL_REQUEST_DEVICE_IDENTITY:
2492 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2493 requestCdmaDeviceIdentity(request, data, datalen, t);
2494 break;
2495 } // Fall-through if tech is not cdma
2496
2497 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2498 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2499 requestCdmaSubscription(request, data, datalen, t);
2500 break;
2501 } // Fall-through if tech is not cdma
2502
2503 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2504 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2505 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2506 break;
2507 } // Fall-through if tech is not cdma
2508
2509 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2510 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2511 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2512 break;
2513 } // Fall-through if tech is not cdma
2514
2515 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2516 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2517 requestCdmaGetRoamingPreference(request, data, datalen, t);
2518 break;
2519 } // Fall-through if tech is not cdma
2520
2521 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2522 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2523 requestCdmaSetRoamingPreference(request, data, datalen, t);
2524 break;
2525 } // Fall-through if tech is not cdma
2526
2527 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2528 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2529 requestExitEmergencyMode(data, datalen, t);
2530 break;
2531 } // Fall-through if tech is not cdma
2532
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002533 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002534 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002535 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2536 break;
2537 }
2538}
2539
2540/**
2541 * Synchronous call from the RIL to us to return current radio state.
2542 * RADIO_STATE_UNAVAILABLE should be the initial state.
2543 */
2544static RIL_RadioState
2545currentState()
2546{
2547 return sState;
2548}
2549/**
2550 * Call from RIL to us to find out whether a specific request code
2551 * is supported by this implementation.
2552 *
2553 * Return 1 for "supported" and 0 for "unsupported"
2554 */
2555
2556static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002557onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002558{
2559 //@@@ todo
2560
2561 return 1;
2562}
2563
Mark Salyzynba58c202014-03-12 15:20:22 -07002564static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002565{
2566 //@@@todo
2567
2568}
2569
2570static const char * getVersion(void)
2571{
2572 return "android reference-ril 1.0";
2573}
2574
2575static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002576setRadioTechnology(ModemInfo *mdm, int newtech)
2577{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002578 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002579
2580 int oldtech = TECH(mdm);
2581
2582 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002583 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002584 TECH(mdm) = newtech;
2585 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2586 int tech = techFromModemType(TECH(sMdmInfo));
2587 if (tech > 0 ) {
2588 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2589 &tech, sizeof(tech));
2590 }
2591 }
2592 }
2593}
2594
2595static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002596setRadioState(RIL_RadioState newState)
2597{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002598 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002599 RIL_RadioState oldState;
2600
2601 pthread_mutex_lock(&s_state_mutex);
2602
2603 oldState = sState;
2604
2605 if (s_closed > 0) {
2606 // If we're closed, the only reasonable state is
2607 // RADIO_STATE_UNAVAILABLE
2608 // This is here because things on the main thread
2609 // may attempt to change the radio state after the closed
2610 // event happened in another thread
2611 newState = RADIO_STATE_UNAVAILABLE;
2612 }
2613
2614 if (sState != newState || s_closed > 0) {
2615 sState = newState;
2616
2617 pthread_cond_broadcast (&s_state_cond);
2618 }
2619
2620 pthread_mutex_unlock(&s_state_mutex);
2621
2622
2623 /* do these outside of the mutex */
2624 if (sState != oldState) {
2625 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2626 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002627 // Sim state can change as result of radio state change
2628 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2629 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002630
2631 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2632 * from the AT reader thread
2633 * Currently, this doesn't happen, but if that changes then these
2634 * will need to be dispatched on the request thread
2635 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002636 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002637 onRadioPowerOn();
2638 }
2639 }
2640}
2641
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002642/** Returns RUIM_NOT_READY on error */
2643static SIM_Status
2644getRUIMStatus()
2645{
2646 ATResponse *p_response = NULL;
2647 int err;
2648 int ret;
2649 char *cpinLine;
2650 char *cpinResult;
2651
2652 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2653 ret = SIM_NOT_READY;
2654 goto done;
2655 }
2656
2657 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2658
2659 if (err != 0) {
2660 ret = SIM_NOT_READY;
2661 goto done;
2662 }
2663
2664 switch (at_get_cme_error(p_response)) {
2665 case CME_SUCCESS:
2666 break;
2667
2668 case CME_SIM_NOT_INSERTED:
2669 ret = SIM_ABSENT;
2670 goto done;
2671
2672 default:
2673 ret = SIM_NOT_READY;
2674 goto done;
2675 }
2676
2677 /* CPIN? has succeeded, now look at the result */
2678
2679 cpinLine = p_response->p_intermediates->line;
2680 err = at_tok_start (&cpinLine);
2681
2682 if (err < 0) {
2683 ret = SIM_NOT_READY;
2684 goto done;
2685 }
2686
2687 err = at_tok_nextstr(&cpinLine, &cpinResult);
2688
2689 if (err < 0) {
2690 ret = SIM_NOT_READY;
2691 goto done;
2692 }
2693
2694 if (0 == strcmp (cpinResult, "SIM PIN")) {
2695 ret = SIM_PIN;
2696 goto done;
2697 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2698 ret = SIM_PUK;
2699 goto done;
2700 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2701 return SIM_NETWORK_PERSONALIZATION;
2702 } else if (0 != strcmp (cpinResult, "READY")) {
2703 /* we're treating unsupported lock types as "sim absent" */
2704 ret = SIM_ABSENT;
2705 goto done;
2706 }
2707
2708 at_response_free(p_response);
2709 p_response = NULL;
2710 cpinResult = NULL;
2711
2712 ret = SIM_READY;
2713
2714done:
2715 at_response_free(p_response);
2716 return ret;
2717}
2718
John Wang309ac292009-07-30 14:53:23 -07002719/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002720static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002721getSIMStatus()
2722{
2723 ATResponse *p_response = NULL;
2724 int err;
2725 int ret;
2726 char *cpinLine;
2727 char *cpinResult;
2728
Wink Saville4dcab4f2012-11-19 16:05:13 -08002729 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002730 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
John Wang309ac292009-07-30 14:53:23 -07002731 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002732 goto done;
2733 }
2734
2735 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2736
2737 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002738 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002739 goto done;
2740 }
2741
2742 switch (at_get_cme_error(p_response)) {
2743 case CME_SUCCESS:
2744 break;
2745
2746 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002747 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002748 goto done;
2749
2750 default:
John Wang309ac292009-07-30 14:53:23 -07002751 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002752 goto done;
2753 }
2754
2755 /* CPIN? has succeeded, now look at the result */
2756
2757 cpinLine = p_response->p_intermediates->line;
2758 err = at_tok_start (&cpinLine);
2759
2760 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002761 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002762 goto done;
2763 }
2764
2765 err = at_tok_nextstr(&cpinLine, &cpinResult);
2766
2767 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002768 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002769 goto done;
2770 }
2771
2772 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002773 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002774 goto done;
2775 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07002776 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002777 goto done;
2778 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002779 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002780 } else if (0 != strcmp (cpinResult, "READY")) {
2781 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07002782 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002783 goto done;
2784 }
2785
2786 at_response_free(p_response);
2787 p_response = NULL;
2788 cpinResult = NULL;
2789
John Wang309ac292009-07-30 14:53:23 -07002790 ret = SIM_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002791
2792done:
2793 at_response_free(p_response);
2794 return ret;
2795}
2796
2797
2798/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07002799 * Get the current card status.
2800 *
2801 * This must be freed using freeCardStatus.
2802 * @return: On success returns RIL_E_SUCCESS
2803 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002804static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002805 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07002806 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07002807 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2808 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002809 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07002810 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2811 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002812 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07002813 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2814 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002815 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07002816 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2817 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002818 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07002819 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2820 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002821 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07002822 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002823 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2824 // RUIM_ABSENT = 6
2825 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2826 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2827 // RUIM_NOT_READY = 7
2828 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2829 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2830 // RUIM_READY = 8
2831 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2832 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2833 // RUIM_PIN = 9
2834 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2835 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2836 // RUIM_PUK = 10
2837 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2838 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2839 // RUIM_NETWORK_PERSONALIZATION = 11
2840 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2841 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
Wink Savillef6aa7c12009-04-30 14:20:52 -07002842 };
2843 RIL_CardState card_state;
2844 int num_apps;
2845
2846 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07002847 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002848 card_state = RIL_CARDSTATE_ABSENT;
2849 num_apps = 0;
2850 } else {
2851 card_state = RIL_CARDSTATE_PRESENT;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002852 num_apps = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002853 }
2854
2855 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002856 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07002857 p_card_status->card_state = card_state;
2858 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
2859 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
2860 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002861 p_card_status->ims_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002862 p_card_status->num_applications = num_apps;
2863
2864 // Initialize application status
2865 int i;
2866 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07002867 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002868 }
2869
2870 // Pickup the appropriate application status
2871 // that reflects sim_status for gsm.
2872 if (num_apps != 0) {
2873 // Only support one app, gsm
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002874 p_card_status->num_applications = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002875 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002876 p_card_status->cdma_subscription_app_index = 1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002877
2878 // Get the correct app status
2879 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002880 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002881 }
2882
2883 *pp_card_status = p_card_status;
2884 return RIL_E_SUCCESS;
2885}
2886
2887/**
2888 * Free the card status returned by getCardStatus
2889 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002890static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002891 free(p_card_status);
2892}
2893
2894/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002895 * SIM ready means any commands that access the SIM will work, including:
2896 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
2897 * (all SMS-related commands)
2898 */
2899
Mark Salyzynba58c202014-03-12 15:20:22 -07002900static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002901{
2902 ATResponse *p_response;
2903 int ret;
2904
Naveen Kalla2baf7232016-10-11 13:49:20 -07002905 if (sState != RADIO_STATE_UNAVAILABLE) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002906 // no longer valid to poll
2907 return;
2908 }
2909
2910 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07002911 case SIM_ABSENT:
2912 case SIM_PIN:
2913 case SIM_PUK:
2914 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002915 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002916 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002917 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002918 return;
2919
John Wang309ac292009-07-30 14:53:23 -07002920 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002921 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
2922 return;
2923
John Wang309ac292009-07-30 14:53:23 -07002924 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002925 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002926 onSIMReady();
2927 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002928 return;
2929 }
2930}
2931
2932/** returns 1 if on, 0 if off, and -1 on error */
2933static int isRadioOn()
2934{
2935 ATResponse *p_response = NULL;
2936 int err;
2937 char *line;
2938 char ret;
2939
2940 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
2941
2942 if (err < 0 || p_response->success == 0) {
2943 // assume radio is off
2944 goto error;
2945 }
2946
2947 line = p_response->p_intermediates->line;
2948
2949 err = at_tok_start(&line);
2950 if (err < 0) goto error;
2951
2952 err = at_tok_nextbool(&line, &ret);
2953 if (err < 0) goto error;
2954
2955 at_response_free(p_response);
2956
2957 return (int)ret;
2958
2959error:
2960
2961 at_response_free(p_response);
2962 return -1;
2963}
2964
2965/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002966 * Parse the response generated by a +CTEC AT command
2967 * The values read from the response are stored in current and preferred.
2968 * Both current and preferred may be null. The corresponding value is ignored in that case.
2969 *
2970 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
2971 * 1 if the response includes the current technology only
2972 * 0 if the response includes both current technology and preferred mode
2973 */
2974int parse_technology_response( const char *response, int *current, int32_t *preferred )
2975{
2976 int err;
2977 char *line, *p;
2978 int ct;
2979 int32_t pt = 0;
2980 char *str_pt;
2981
2982 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08002983 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002984 err = at_tok_start(&p);
2985 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002986 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002987 free(line);
2988 return -1;
2989 }
2990
2991 err = at_tok_nextint(&p, &ct);
2992 if (err) {
2993 free(line);
2994 return -1;
2995 }
2996 if (current) *current = ct;
2997
Wink Saville4dcab4f2012-11-19 16:05:13 -08002998 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002999
3000 err = at_tok_nexthexint(&p, &pt);
3001 if (err) {
3002 free(line);
3003 return 1;
3004 }
3005 if (preferred) {
3006 *preferred = pt;
3007 }
3008 free(line);
3009
3010 return 0;
3011}
3012
Mark Salyzynba58c202014-03-12 15:20:22 -07003013int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003014{
3015 ATResponse *p_response;
3016 int err, val, techs = 0;
3017 char *tok;
3018 char *line;
3019
Wink Saville4dcab4f2012-11-19 16:05:13 -08003020 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003021 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
3022 if (err || !p_response->success)
3023 goto error;
3024 line = p_response->p_intermediates->line;
3025 err = at_tok_start(&line);
3026 if (err || !at_tok_hasmore(&line))
3027 goto error;
3028 while (!at_tok_nextint(&line, &val)) {
3029 techs |= ( 1 << val );
3030 }
3031 if (supported) *supported = techs;
3032 return 0;
3033error:
3034 at_response_free(p_response);
3035 return -1;
3036}
3037
3038/**
3039 * query_ctec. Send the +CTEC AT command to the modem to query the current
3040 * and preferred modes. It leaves values in the addresses pointed to by
3041 * current and preferred. If any of those pointers are NULL, the corresponding value
3042 * is ignored, but the return value will still reflect if retreiving and parsing of the
3043 * values suceeded.
3044 *
3045 * @mdm Currently unused
3046 * @current A pointer to store the current mode returned by the modem. May be null.
3047 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
3048 * @return -1 on error (or failure to parse)
3049 * 1 if only the current mode was returned by modem (or failed to parse preferred)
3050 * 0 if both current and preferred were returned correctly
3051 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003052int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003053{
3054 ATResponse *response = NULL;
3055 int err;
3056 int res;
3057
Colin Cross5cba4882014-02-05 18:55:42 -08003058 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003059 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
3060 if (!err && response->success) {
3061 res = parse_technology_response(response->p_intermediates->line, current, preferred);
3062 at_response_free(response);
3063 return res;
3064 }
Colin Cross5cba4882014-02-05 18:55:42 -08003065 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003066 at_response_free(response);
3067 return -1;
3068}
3069
3070int is_multimode_modem(ModemInfo *mdm)
3071{
3072 ATResponse *response;
3073 int err;
3074 char *line;
3075 int tech;
3076 int32_t preferred;
3077
3078 if (query_ctec(mdm, &tech, &preferred) == 0) {
3079 mdm->currentTech = tech;
3080 mdm->preferredNetworkMode = preferred;
3081 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
3082 return 0;
3083 }
3084 return 1;
3085 }
3086 return 0;
3087}
3088
3089/**
3090 * Find out if our modem is GSM, CDMA or both (Multimode)
3091 */
3092static void probeForModemMode(ModemInfo *info)
3093{
3094 ATResponse *response;
3095 int err;
3096 assert (info);
3097 // Currently, our only known multimode modem is qemu's android modem,
3098 // which implements the AT+CTEC command to query and set mode.
3099 // Try that first
3100
3101 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003102 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003103 info->supportedTechs, info->currentTech);
3104 return;
3105 }
3106
3107 /* Being here means that our modem is not multimode */
3108 info->isMultimode = 0;
3109
3110 /* CDMA Modems implement the AT+WNAM command */
3111 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
3112 if (!err && response->success) {
3113 at_response_free(response);
3114 // TODO: find out if we really support EvDo
3115 info->supportedTechs = MDM_CDMA | MDM_EVDO;
3116 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003117 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003118 return;
3119 }
3120 if (!err) at_response_free(response);
3121 // TODO: find out if modem really supports WCDMA/LTE
3122 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
3123 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003124 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003125}
3126
3127/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003128 * Initialize everything that can be configured while we're still in
3129 * AT+CFUN=0
3130 */
Mark Salyzynba58c202014-03-12 15:20:22 -07003131static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003132{
3133 ATResponse *p_response = NULL;
3134 int err;
3135
3136 setRadioState (RADIO_STATE_OFF);
3137
3138 at_handshake();
3139
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003140 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003141 /* note: we don't check errors here. Everything important will
3142 be handled in onATTimeout and onATReaderClosed */
3143
3144 /* atchannel is tolerant of echo but it must */
3145 /* have verbose result codes */
3146 at_send_command("ATE0Q0V1", NULL);
3147
3148 /* No auto-answer */
3149 at_send_command("ATS0=0", NULL);
3150
3151 /* Extended errors */
3152 at_send_command("AT+CMEE=1", NULL);
3153
3154 /* Network registration events */
3155 err = at_send_command("AT+CREG=2", &p_response);
3156
3157 /* some handsets -- in tethered mode -- don't support CREG=2 */
3158 if (err < 0 || p_response->success == 0) {
3159 at_send_command("AT+CREG=1", NULL);
3160 }
3161
3162 at_response_free(p_response);
3163
3164 /* GPRS registration events */
3165 at_send_command("AT+CGREG=1", NULL);
3166
3167 /* Call Waiting notifications */
3168 at_send_command("AT+CCWA=1", NULL);
3169
3170 /* Alternating voice/data off */
3171 at_send_command("AT+CMOD=0", NULL);
3172
3173 /* Not muted */
3174 at_send_command("AT+CMUT=0", NULL);
3175
3176 /* +CSSU unsolicited supp service notifications */
3177 at_send_command("AT+CSSN=0,1", NULL);
3178
3179 /* no connected line identification */
3180 at_send_command("AT+COLP=0", NULL);
3181
3182 /* HEX character set */
3183 at_send_command("AT+CSCS=\"HEX\"", NULL);
3184
3185 /* USSD unsolicited */
3186 at_send_command("AT+CUSD=1", NULL);
3187
3188 /* Enable +CGEV GPRS event notifications, but don't buffer */
3189 at_send_command("AT+CGEREP=1,0", NULL);
3190
3191 /* SMS PDU mode */
3192 at_send_command("AT+CMGF=0", NULL);
3193
3194#ifdef USE_TI_COMMANDS
3195
3196 at_send_command("AT%CPI=3", NULL);
3197
3198 /* TI specific -- notifications when SMS is ready (currently ignored) */
3199 at_send_command("AT%CSTAT=1", NULL);
3200
3201#endif /* USE_TI_COMMANDS */
3202
3203
3204 /* assume radio is off on error */
3205 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003206 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003207 }
3208}
3209
3210static void waitForClose()
3211{
3212 pthread_mutex_lock(&s_state_mutex);
3213
3214 while (s_closed == 0) {
3215 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3216 }
3217
3218 pthread_mutex_unlock(&s_state_mutex);
3219}
3220
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003221static void sendUnsolImsNetworkStateChanged()
3222{
3223#if 0 // to be used when unsol is changed to return data.
3224 int reply[2];
3225 reply[0] = s_ims_registered;
3226 reply[1] = s_ims_services;
3227 reply[1] = s_ims_format;
3228#endif
3229 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3230 NULL, 0);
3231}
3232
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003233/**
3234 * Called by atchannel when an unsolicited line appears
3235 * This is called on atchannel's reader thread. AT commands may
3236 * not be issued here
3237 */
3238static void onUnsolicited (const char *s, const char *sms_pdu)
3239{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003240 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003241 int err;
3242
3243 /* Ignore unsolicited responses until we're initialized.
3244 * This is OK because the RIL library will poll for initial state
3245 */
3246 if (sState == RADIO_STATE_UNAVAILABLE) {
3247 return;
3248 }
3249
3250 if (strStartsWith(s, "%CTZV:")) {
3251 /* TI specific -- NITZ time */
3252 char *response;
3253
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003254 line = p = strdup(s);
3255 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003256
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003257 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003258
3259 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003260 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003261 } else {
3262 RIL_onUnsolicitedResponse (
3263 RIL_UNSOL_NITZ_TIME_RECEIVED,
3264 response, strlen(response));
3265 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003266 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003267 } else if (strStartsWith(s,"+CRING:")
3268 || strStartsWith(s,"RING")
3269 || strStartsWith(s,"NO CARRIER")
3270 || strStartsWith(s,"+CCWA")
3271 ) {
3272 RIL_onUnsolicitedResponse (
3273 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3274 NULL, 0);
3275#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003276 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003277#endif /* WORKAROUND_FAKE_CGEV */
3278 } else if (strStartsWith(s,"+CREG:")
3279 || strStartsWith(s,"+CGREG:")
3280 ) {
3281 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003282 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003283 NULL, 0);
3284#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003285 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003286#endif /* WORKAROUND_FAKE_CGEV */
3287 } else if (strStartsWith(s, "+CMT:")) {
3288 RIL_onUnsolicitedResponse (
3289 RIL_UNSOL_RESPONSE_NEW_SMS,
3290 sms_pdu, strlen(sms_pdu));
3291 } else if (strStartsWith(s, "+CDS:")) {
3292 RIL_onUnsolicitedResponse (
3293 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3294 sms_pdu, strlen(sms_pdu));
3295 } else if (strStartsWith(s, "+CGEV:")) {
3296 /* Really, we can ignore NW CLASS and ME CLASS events here,
3297 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003298 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003299 */
3300 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003301 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003302#ifdef WORKAROUND_FAKE_CGEV
3303 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003304 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003305#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003306 } else if (strStartsWith(s, "+CTEC: ")) {
3307 int tech, mask;
3308 switch (parse_technology_response(s, &tech, NULL))
3309 {
3310 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003311 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003312 break;
3313 case 1: // current mode correctly parsed
3314 case 0: // preferred mode correctly parsed
3315 mask = 1 << tech;
3316 if (mask != MDM_GSM && mask != MDM_CDMA &&
3317 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003318 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003319 } else {
3320 setRadioTechnology(sMdmInfo, tech);
3321 }
3322 break;
3323 }
3324 } else if (strStartsWith(s, "+CCSS: ")) {
3325 int source = 0;
3326 line = p = strdup(s);
3327 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003328 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003329 return;
3330 }
3331 if (at_tok_start(&p) < 0) {
3332 free(line);
3333 return;
3334 }
3335 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003336 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003337 free(line);
3338 return;
3339 }
3340 SSOURCE(sMdmInfo) = source;
3341 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3342 &source, sizeof(source));
3343 } else if (strStartsWith(s, "+WSOS: ")) {
3344 char state = 0;
3345 int unsol;
3346 line = p = strdup(s);
3347 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003348 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003349 return;
3350 }
3351 if (at_tok_start(&p) < 0) {
3352 free(line);
3353 return;
3354 }
3355 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003356 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003357 free(line);
3358 return;
3359 }
3360 free(line);
3361
3362 unsol = state ?
3363 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3364
3365 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3366
3367 } else if (strStartsWith(s, "+WPRL: ")) {
3368 int version = -1;
3369 line = p = strdup(s);
3370 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003371 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003372 return;
3373 }
3374 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003375 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003376 free(line);
3377 return;
3378 }
3379 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003380 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003381 free(line);
3382 return;
3383 }
3384 free(line);
3385 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3386 } else if (strStartsWith(s, "+CFUN: 0")) {
3387 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003388 }
3389}
3390
3391/* Called on command or reader thread */
3392static void onATReaderClosed()
3393{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003394 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003395 at_close();
3396 s_closed = 1;
3397
3398 setRadioState (RADIO_STATE_UNAVAILABLE);
3399}
3400
3401/* Called on command thread */
3402static void onATTimeout()
3403{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003404 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003405 at_close();
3406
3407 s_closed = 1;
3408
3409 /* FIXME cause a radio reset here */
3410
3411 setRadioState (RADIO_STATE_UNAVAILABLE);
3412}
3413
Etan Cohend3652192014-06-20 08:28:44 -07003414/* Called to pass hardware configuration information to telephony
3415 * framework.
3416 */
3417static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3418{
3419 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3420}
3421
Sanket Padawef0c8ca72016-06-30 15:01:08 -07003422static void usage(char *s __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003423{
3424#ifdef RIL_SHLIB
3425 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3426#else
3427 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3428 exit(-1);
3429#endif
3430}
3431
3432static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003433mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003434{
3435 int fd;
3436 int ret;
3437
3438 AT_DUMP("== ", "entering mainLoop()", -1 );
3439 at_set_on_reader_closed(onATReaderClosed);
3440 at_set_on_timeout(onATTimeout);
3441
3442 for (;;) {
3443 fd = -1;
3444 while (fd < 0) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003445 if (isInEmulator()) {
3446 fd = qemu_pipe_open("pipe:qemud:gsm");
3447 } else if (s_port > 0) {
Elliott Hughes7e3bbd42016-10-11 13:50:06 -07003448 fd = socket_network_client("localhost", s_port, SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003449 } else if (s_device_socket) {
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003450 fd = socket_local_client(s_device_path,
3451 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3452 SOCK_STREAM);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003453 } else if (s_device_path != NULL) {
3454 fd = open (s_device_path, O_RDWR);
3455 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3456 /* disable echo on serial ports */
3457 struct termios ios;
3458 tcgetattr( fd, &ios );
3459 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3460 tcsetattr( fd, TCSANOW, &ios );
3461 }
3462 }
3463
3464 if (fd < 0) {
3465 perror ("opening AT interface. retrying...");
3466 sleep(10);
3467 /* never returns */
3468 }
3469 }
3470
3471 s_closed = 0;
3472 ret = at_open(fd, onUnsolicited);
3473
3474 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003475 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003476 return 0;
3477 }
3478
3479 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3480
3481 // Give initializeCallback a chance to dispatched, since
3482 // we don't presently have a cancellation mechanism
3483 sleep(1);
3484
3485 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003486 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003487 }
3488}
3489
3490#ifdef RIL_SHLIB
3491
3492pthread_t s_tid_mainloop;
3493
3494const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3495{
3496 int ret;
3497 int fd = -1;
3498 int opt;
3499 pthread_attr_t attr;
3500
3501 s_rilenv = env;
3502
Sandeep Gutta11f27942014-07-10 05:00:25 +05303503 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003504 switch (opt) {
3505 case 'p':
3506 s_port = atoi(optarg);
3507 if (s_port == 0) {
3508 usage(argv[0]);
3509 return NULL;
3510 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003511 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003512 break;
3513
3514 case 'd':
3515 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003516 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003517 break;
3518
3519 case 's':
3520 s_device_path = optarg;
3521 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003522 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003523 break;
3524
Sandeep Gutta11f27942014-07-10 05:00:25 +05303525 case 'c':
3526 RLOGI("Client id received %s\n", optarg);
3527 break;
3528
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003529 default:
3530 usage(argv[0]);
3531 return NULL;
3532 }
3533 }
3534
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003535 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003536 usage(argv[0]);
3537 return NULL;
3538 }
3539
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003540 sMdmInfo = calloc(1, sizeof(ModemInfo));
3541 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003542 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003543 return NULL;
3544 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003545 pthread_attr_init (&attr);
3546 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3547 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3548
3549 return &s_callbacks;
3550}
3551#else /* RIL_SHLIB */
3552int main (int argc, char **argv)
3553{
3554 int ret;
3555 int fd = -1;
3556 int opt;
3557
3558 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3559 switch (opt) {
3560 case 'p':
3561 s_port = atoi(optarg);
3562 if (s_port == 0) {
3563 usage(argv[0]);
3564 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003565 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003566 break;
3567
3568 case 'd':
3569 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003570 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003571 break;
3572
3573 case 's':
3574 s_device_path = optarg;
3575 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003576 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003577 break;
3578
3579 default:
3580 usage(argv[0]);
3581 }
3582 }
3583
David 'Digit' Turner834eca82016-06-22 12:10:01 +02003584 if (s_port < 0 && s_device_path == NULL && !isInEmulator()) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003585 usage(argv[0]);
3586 }
3587
3588 RIL_register(&s_callbacks);
3589
3590 mainLoop(NULL);
3591
3592 return 0;
3593}
3594
3595#endif /* RIL_SHLIB */