blob: 557109ba3565ff8075ccf56cabdac18198770667 [file] [log] [blame]
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001/* //device/system/reference-ril/reference-ril.c
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070018#include <telephony/ril_cdma_sms.h>
Wink Saville8a9e0212013-04-09 12:11:38 -070019#include <telephony/librilutils.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080020#include <stdio.h>
21#include <assert.h>
22#include <string.h>
23#include <errno.h>
24#include <unistd.h>
Mark Salyzynba58c202014-03-12 15:20:22 -070025#include <sys/cdefs.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080026#include <sys/types.h>
27#include <sys/stat.h>
28#include <fcntl.h>
29#include <pthread.h>
30#include <alloca.h>
31#include "atchannel.h"
32#include "at_tok.h"
33#include "misc.h"
34#include <getopt.h>
35#include <sys/socket.h>
36#include <cutils/sockets.h>
37#include <termios.h>
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +010038#include <sys/system_properties.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080039
Wink Saville9a9fbd22011-02-15 17:13:10 -080040#include "ril.h"
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -070041#include "hardware/qemu_pipe.h"
Wink Saville9a9fbd22011-02-15 17:13:10 -080042
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080043#define LOG_TAG "RIL"
44#include <utils/Log.h>
45
Etan Cohend3652192014-06-20 08:28:44 -070046static void *noopRemoveWarning( void *a ) { return a; }
47#define RIL_UNUSED_PARM(a) noopRemoveWarning((void *)&(a));
48
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080049#define MAX_AT_RESPONSE 0x1000
50
Wink Savillef4c4d362009-04-02 01:37:03 -070051/* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
Robert Greenwaltf838ede2010-07-15 18:54:53 -070052#define PPP_TTY_PATH "eth0"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080053
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -070054// Default MTU value
55#define DEFAULT_MTU 1500
56
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080057#ifdef USE_TI_COMMANDS
58
59// Enable a workaround
60// 1) Make incoming call, do not answer
61// 2) Hangup remote end
62// Expected: call should disappear from CLCC line
63// Actual: Call shows as "ACTIVE" before disappearing
64#define WORKAROUND_ERRONEOUS_ANSWER 1
65
66// Some varients of the TI stack do not support the +CGEV unsolicited
67// response. However, they seem to send an unsolicited +CME ERROR: 150
68#define WORKAROUND_FAKE_CGEV 1
69#endif
70
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -070071/* Modem Technology bits */
72#define MDM_GSM 0x01
73#define MDM_WCDMA 0x02
74#define MDM_CDMA 0x04
75#define MDM_EVDO 0x08
76#define MDM_LTE 0x10
77
78typedef struct {
79 int supportedTechs; // Bitmask of supported Modem Technology bits
80 int currentTech; // Technology the modem is currently using (in the format used by modem)
81 int isMultimode;
82
83 // Preferred mode bitmask. This is actually 4 byte-sized bitmasks with different priority values,
84 // in which the byte number from LSB to MSB give the priority.
85 //
86 // |MSB| | |LSB
87 // value: |00 |00 |00 |00
88 // byte #: |3 |2 |1 |0
89 //
90 // Higher byte order give higher priority. Thus, a value of 0x0000000f represents
91 // a preferred mode of GSM, WCDMA, CDMA, and EvDo in which all are equally preferrable, whereas
92 // 0x00000201 represents a mode with GSM and WCDMA, in which WCDMA is preferred over GSM
93 int32_t preferredNetworkMode;
94 int subscription_source;
95
96} ModemInfo;
97
98static ModemInfo *sMdmInfo;
99// TECH returns the current technology in the format used by the modem.
100// It can be used as an l-value
101#define TECH(mdminfo) ((mdminfo)->currentTech)
102// TECH_BIT returns the bitmask equivalent of the current tech
103#define TECH_BIT(mdminfo) (1 << ((mdminfo)->currentTech))
104#define IS_MULTIMODE(mdminfo) ((mdminfo)->isMultimode)
105#define TECH_SUPPORTED(mdminfo, tech) ((mdminfo)->supportedTechs & (tech))
106#define PREFERRED_NETWORK(mdminfo) ((mdminfo)->preferredNetworkMode)
107// CDMA Subscription Source
108#define SSOURCE(mdminfo) ((mdminfo)->subscription_source)
109
110static int net2modem[] = {
111 MDM_GSM | MDM_WCDMA, // 0 - GSM / WCDMA Pref
112 MDM_GSM, // 1 - GSM only
113 MDM_WCDMA, // 2 - WCDMA only
114 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
115 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
116 MDM_CDMA, // 5 - CDMA only
117 MDM_EVDO, // 6 - EvDo only
118 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
119 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
120 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
121 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
122 MDM_LTE, // 11 - LTE only
123};
124
125static int32_t net2pmask[] = {
126 MDM_GSM | (MDM_WCDMA << 8), // 0 - GSM / WCDMA Pref
127 MDM_GSM, // 1 - GSM only
128 MDM_WCDMA, // 2 - WCDMA only
129 MDM_GSM | MDM_WCDMA, // 3 - GSM / WCDMA Auto
130 MDM_CDMA | MDM_EVDO, // 4 - CDMA / EvDo Auto
131 MDM_CDMA, // 5 - CDMA only
132 MDM_EVDO, // 6 - EvDo only
133 MDM_GSM | MDM_WCDMA | MDM_CDMA | MDM_EVDO, // 7 - GSM/WCDMA, CDMA, EvDo
134 MDM_LTE | MDM_CDMA | MDM_EVDO, // 8 - LTE, CDMA and EvDo
135 MDM_LTE | MDM_GSM | MDM_WCDMA, // 9 - LTE, GSM/WCDMA
136 MDM_LTE | MDM_CDMA | MDM_EVDO | MDM_GSM | MDM_WCDMA, // 10 - LTE, CDMA, EvDo, GSM/WCDMA
137 MDM_LTE, // 11 - LTE only
138};
139
140static int is3gpp2(int radioTech) {
141 switch (radioTech) {
142 case RADIO_TECH_IS95A:
143 case RADIO_TECH_IS95B:
144 case RADIO_TECH_1xRTT:
145 case RADIO_TECH_EVDO_0:
146 case RADIO_TECH_EVDO_A:
147 case RADIO_TECH_EVDO_B:
148 case RADIO_TECH_EHRPD:
149 return 1;
150 default:
151 return 0;
152 }
153}
154
John Wang309ac292009-07-30 14:53:23 -0700155typedef enum {
156 SIM_ABSENT = 0,
157 SIM_NOT_READY = 1,
158 SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
159 SIM_PIN = 3,
160 SIM_PUK = 4,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700161 SIM_NETWORK_PERSONALIZATION = 5,
162 RUIM_ABSENT = 6,
163 RUIM_NOT_READY = 7,
164 RUIM_READY = 8,
165 RUIM_PIN = 9,
166 RUIM_PUK = 10,
167 RUIM_NETWORK_PERSONALIZATION = 11
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100168} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -0700169
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800170static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
171static RIL_RadioState currentState();
172static int onSupports (int requestCode);
173static void onCancel (RIL_Token t);
174static const char *getVersion();
175static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -0700176static SIM_Status getSIMStatus();
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700177static int getCardStatus(RIL_CardStatus_v6 **pp_card_status);
178static void freeCardStatus(RIL_CardStatus_v6 *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -0700179static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800180
181extern const char * requestToString(int request);
182
183/*** Static Variables ***/
184static const RIL_RadioFunctions s_callbacks = {
185 RIL_VERSION,
186 onRequest,
187 currentState,
188 onSupports,
189 onCancel,
190 getVersion
191};
192
193#ifdef RIL_SHLIB
194static const struct RIL_Env *s_rilenv;
195
196#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
197#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
198#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
199#endif
200
201static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
202
203static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
204static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
205
206static int s_port = -1;
207static const char * s_device_path = NULL;
208static int s_device_socket = 0;
209
210/* trigger change to this with s_state_cond */
211static int s_closed = 0;
212
213static int sFD; /* file desc of AT channel */
214static char sATBuffer[MAX_AT_RESPONSE+1];
215static char *sATBufferCur = NULL;
216
217static const struct timeval TIMEVAL_SIMPOLL = {1,0};
218static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
219static const struct timeval TIMEVAL_0 = {0,0};
220
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -0700221static int s_ims_registered = 0; // 0==unregistered
222static int s_ims_services = 1; // & 0x1 == sms over ims supported
223static int s_ims_format = 1; // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
224static int s_ims_cause_retry = 0; // 1==causes sms over ims to temp fail
225static int s_ims_cause_perm_failure = 0; // 1==causes sms over ims to permanent fail
226static int s_ims_gsm_retry = 0; // 1==causes sms over gsm to temp fail
227static int s_ims_gsm_fail = 0; // 1==causes sms over gsm to permanent fail
228
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800229#ifdef WORKAROUND_ERRONEOUS_ANSWER
230// Max number of times we'll try to repoll when we think
231// we have a AT+CLCC race condition
232#define REPOLL_CALLS_COUNT_MAX 4
233
234// Line index that was incoming or waiting at last poll, or -1 for none
235static int s_incomingOrWaitingLine = -1;
236// Number of times we've asked for a repoll of AT+CLCC
237static int s_repollCallsCount = 0;
238// Should we expect a call to be answered in the next CLCC?
239static int s_expectAnswer = 0;
240#endif /* WORKAROUND_ERRONEOUS_ANSWER */
241
Wink Saville8a9e0212013-04-09 12:11:38 -0700242static int s_cell_info_rate_ms = INT_MAX;
243static int s_mcc = 0;
244static int s_mnc = 0;
245static int s_lac = 0;
246static int s_cid = 0;
247
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800248static void pollSIMState (void *param);
249static void setRadioState(RIL_RadioState newState);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700250static void setRadioTechnology(ModemInfo *mdm, int newtech);
251static int query_ctec(ModemInfo *mdm, int *current, int32_t *preferred);
252static int parse_technology_response(const char *response, int *current, int32_t *preferred);
253static int techFromModemType(int mdmtype);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800254
255static int clccStateToRILState(int state, RIL_CallState *p_state)
256
257{
258 switch(state) {
259 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
260 case 1: *p_state = RIL_CALL_HOLDING; return 0;
261 case 2: *p_state = RIL_CALL_DIALING; return 0;
262 case 3: *p_state = RIL_CALL_ALERTING; return 0;
263 case 4: *p_state = RIL_CALL_INCOMING; return 0;
264 case 5: *p_state = RIL_CALL_WAITING; return 0;
265 default: return -1;
266 }
267}
268
269/**
270 * Note: directly modified line and has *p_call point directly into
271 * modified line
272 */
Wink Saville3d54e742009-05-18 18:00:44 -0700273static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800274{
275 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
276 // index,isMT,state,mode,isMpty(,number,TOA)?
277
278 int err;
279 int state;
280 int mode;
281
282 err = at_tok_start(&line);
283 if (err < 0) goto error;
284
285 err = at_tok_nextint(&line, &(p_call->index));
286 if (err < 0) goto error;
287
288 err = at_tok_nextbool(&line, &(p_call->isMT));
289 if (err < 0) goto error;
290
291 err = at_tok_nextint(&line, &state);
292 if (err < 0) goto error;
293
294 err = clccStateToRILState(state, &(p_call->state));
295 if (err < 0) goto error;
296
297 err = at_tok_nextint(&line, &mode);
298 if (err < 0) goto error;
299
300 p_call->isVoice = (mode == 0);
301
302 err = at_tok_nextbool(&line, &(p_call->isMpty));
303 if (err < 0) goto error;
304
305 if (at_tok_hasmore(&line)) {
306 err = at_tok_nextstr(&line, &(p_call->number));
307
308 /* tolerate null here */
309 if (err < 0) return 0;
310
311 // Some lame implementations return strings
312 // like "NOT AVAILABLE" in the CLCC line
313 if (p_call->number != NULL
314 && 0 == strspn(p_call->number, "+0123456789")
315 ) {
316 p_call->number = NULL;
317 }
318
319 err = at_tok_nextint(&line, &p_call->toa);
320 if (err < 0) goto error;
321 }
322
Wink Saville74fa3882009-12-22 15:35:41 -0800323 p_call->uusInfo = NULL;
324
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800325 return 0;
326
327error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800328 RLOGE("invalid CLCC line\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800329 return -1;
330}
331
332
333/** do post-AT+CFUN=1 initialization */
334static void onRadioPowerOn()
335{
336#ifdef USE_TI_COMMANDS
337 /* Must be after CFUN=1 */
338 /* TI specific -- notifications for CPHS things such */
339 /* as CPHS message waiting indicator */
340
341 at_send_command("AT%CPHS=1", NULL);
342
343 /* TI specific -- enable NITZ unsol notifs */
344 at_send_command("AT%CTZV=1", NULL);
345#endif
346
347 pollSIMState(NULL);
348}
349
350/** do post- SIM ready initialization */
351static void onSIMReady()
352{
353 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
354 /*
355 * Always send SMS messages directly to the TE
356 *
357 * mode = 1 // discard when link is reserved (link should never be
358 * reserved)
359 * mt = 2 // most messages routed to TE
360 * bm = 2 // new cell BM's routed to TE
361 * ds = 1 // Status reports routed to TE
362 * bfr = 1 // flush buffer
363 */
364 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
365}
366
367static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
368{
369 int onOff;
370
371 int err;
372 ATResponse *p_response = NULL;
373
374 assert (datalen >= sizeof(int *));
375 onOff = ((int *)data)[0];
376
377 if (onOff == 0 && sState != RADIO_STATE_OFF) {
378 err = at_send_command("AT+CFUN=0", &p_response);
379 if (err < 0 || p_response->success == 0) goto error;
380 setRadioState(RADIO_STATE_OFF);
381 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
382 err = at_send_command("AT+CFUN=1", &p_response);
383 if (err < 0|| p_response->success == 0) {
384 // Some stacks return an error when there is no SIM,
385 // but they really turn the RF portion on
386 // So, if we get an error, let's check to see if it
387 // turned on anyway
388
389 if (isRadioOn() != 1) {
390 goto error;
391 }
392 }
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700393 setRadioState(RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800394 }
395
396 at_response_free(p_response);
397 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
398 return;
399error:
400 at_response_free(p_response);
401 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
402}
403
Naveen Kallaa65a16a2014-07-31 16:48:31 -0700404static void requestShutdown(RIL_Token t)
405{
406 int onOff;
407
408 int err;
409 ATResponse *p_response = NULL;
410
411 if (sState != RADIO_STATE_OFF) {
412 err = at_send_command("AT+CFUN=0", &p_response);
413 setRadioState(RADIO_STATE_UNAVAILABLE);
414 }
415
416 at_response_free(p_response);
417 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
418 return;
419}
420
Wink Savillef4c4d362009-04-02 01:37:03 -0700421static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800422
Mark Salyzynba58c202014-03-12 15:20:22 -0700423static void onDataCallListChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800424{
Wink Savillef4c4d362009-04-02 01:37:03 -0700425 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800426}
427
Mark Salyzynba58c202014-03-12 15:20:22 -0700428static void requestDataCallList(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800429{
Wink Savillef4c4d362009-04-02 01:37:03 -0700430 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800431}
432
Wink Savillef4c4d362009-04-02 01:37:03 -0700433static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800434{
435 ATResponse *p_response;
436 ATLine *p_cur;
437 int err;
438 int n = 0;
439 char *out;
440
441 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
442 if (err != 0 || p_response->success == 0) {
443 if (t != NULL)
444 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
445 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700446 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800447 NULL, 0);
448 return;
449 }
450
451 for (p_cur = p_response->p_intermediates; p_cur != NULL;
452 p_cur = p_cur->p_next)
453 n++;
454
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700455 RIL_Data_Call_Response_v11 *responses =
456 alloca(n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800457
458 int i;
459 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800460 responses[i].status = -1;
Wink Saville250eb3c2011-06-22 09:11:34 -0700461 responses[i].suggestedRetryTime = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800462 responses[i].cid = -1;
463 responses[i].active = -1;
464 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800465 responses[i].ifname = "";
466 responses[i].addresses = "";
467 responses[i].dnses = "";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700468 responses[i].gateways = "";
Etan Cohend3652192014-06-20 08:28:44 -0700469 responses[i].pcscf = "";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700470 responses[i].mtu = 0;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800471 }
472
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700473 RIL_Data_Call_Response_v11 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800474 for (p_cur = p_response->p_intermediates; p_cur != NULL;
475 p_cur = p_cur->p_next) {
476 char *line = p_cur->line;
477
478 err = at_tok_start(&line);
479 if (err < 0)
480 goto error;
481
482 err = at_tok_nextint(&line, &response->cid);
483 if (err < 0)
484 goto error;
485
486 err = at_tok_nextint(&line, &response->active);
487 if (err < 0)
488 goto error;
489
490 response++;
491 }
492
493 at_response_free(p_response);
494
495 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
496 if (err != 0 || p_response->success == 0) {
497 if (t != NULL)
498 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
499 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700500 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800501 NULL, 0);
502 return;
503 }
504
505 for (p_cur = p_response->p_intermediates; p_cur != NULL;
506 p_cur = p_cur->p_next) {
507 char *line = p_cur->line;
508 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800509
510 err = at_tok_start(&line);
511 if (err < 0)
512 goto error;
513
514 err = at_tok_nextint(&line, &cid);
515 if (err < 0)
516 goto error;
517
518 for (i = 0; i < n; i++) {
519 if (responses[i].cid == cid)
520 break;
521 }
522
523 if (i >= n) {
524 /* details for a context we didn't hear about in the last request */
525 continue;
526 }
527
Wink Saville43808972011-01-13 17:39:51 -0800528 // Assume no error
529 responses[i].status = 0;
530
531 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800532 err = at_tok_nextstr(&line, &out);
533 if (err < 0)
534 goto error;
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700535
536 int type_size = strlen(out) + 1;
537 responses[i].type = alloca(type_size);
538 strlcpy(responses[i].type, out, type_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800539
Wink Saville43808972011-01-13 17:39:51 -0800540 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800541 err = at_tok_nextstr(&line, &out);
542 if (err < 0)
543 goto error;
544
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700545 int ifname_size = strlen(PPP_TTY_PATH) + 1;
546 responses[i].ifname = alloca(ifname_size);
547 strlcpy(responses[i].ifname, PPP_TTY_PATH, ifname_size);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800548
549 err = at_tok_nextstr(&line, &out);
550 if (err < 0)
551 goto error;
552
Naina Nalluri2be38ac2016-05-03 14:09:53 -0700553 int addresses_size = strlen(out) + 1;
554 responses[i].addresses = alloca(addresses_size);
555 strlcpy(responses[i].addresses, out, addresses_size);
Wink Saville43808972011-01-13 17:39:51 -0800556
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100557 {
558 char propValue[PROP_VALUE_MAX];
559
560 if (__system_property_get("ro.kernel.qemu", propValue) != 0) {
561 /* We are in the emulator - the dns servers are listed
562 * by the following system properties, setup in
563 * /system/etc/init.goldfish.sh:
564 * - net.eth0.dns1
565 * - net.eth0.dns2
566 * - net.eth0.dns3
567 * - net.eth0.dns4
568 */
569 const int dnslist_sz = 128;
570 char* dnslist = alloca(dnslist_sz);
571 const char* separator = "";
572 int nn;
573
574 dnslist[0] = 0;
575 for (nn = 1; nn <= 4; nn++) {
576 /* Probe net.eth0.dns<n> */
577 char propName[PROP_NAME_MAX];
578 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
579
580 /* Ignore if undefined */
581 if (__system_property_get(propName, propValue) == 0) {
582 continue;
583 }
584
585 /* Append the DNS IP address */
586 strlcat(dnslist, separator, dnslist_sz);
587 strlcat(dnslist, propValue, dnslist_sz);
588 separator = " ";
589 }
590 responses[i].dnses = dnslist;
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700591
592 /* There is only on gateway in the emulator */
593 responses[i].gateways = "10.0.2.2";
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700594 responses[i].mtu = DEFAULT_MTU;
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100595 }
596 else {
597 /* I don't know where we are, so use the public Google DNS
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700598 * servers by default and no gateway.
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100599 */
600 responses[i].dnses = "8.8.8.8 8.8.4.4";
Wink Saville2c1fb3a2011-03-19 13:42:45 -0700601 responses[i].gateways = "";
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100602 }
603 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800604 }
605
606 at_response_free(p_response);
607
608 if (t != NULL)
609 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700610 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800611 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700612 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800613 responses,
Sukanya Rajkhowab44dda32014-06-02 14:03:44 -0700614 n * sizeof(RIL_Data_Call_Response_v11));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800615
616 return;
617
618error:
619 if (t != NULL)
620 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
621 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700622 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800623 NULL, 0);
624
625 at_response_free(p_response);
626}
627
628static void requestQueryNetworkSelectionMode(
Mark Salyzynba58c202014-03-12 15:20:22 -0700629 void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800630{
631 int err;
632 ATResponse *p_response = NULL;
633 int response = 0;
634 char *line;
635
636 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
637
638 if (err < 0 || p_response->success == 0) {
639 goto error;
640 }
641
642 line = p_response->p_intermediates->line;
643
644 err = at_tok_start(&line);
645
646 if (err < 0) {
647 goto error;
648 }
649
650 err = at_tok_nextint(&line, &response);
651
652 if (err < 0) {
653 goto error;
654 }
655
656 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
657 at_response_free(p_response);
658 return;
659error:
660 at_response_free(p_response);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800661 RLOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800662 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
663}
664
Mark Salyzynba58c202014-03-12 15:20:22 -0700665static void sendCallStateChanged(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800666{
667 RIL_onUnsolicitedResponse (
668 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
669 NULL, 0);
670}
671
Mark Salyzynba58c202014-03-12 15:20:22 -0700672static void requestGetCurrentCalls(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800673{
674 int err;
675 ATResponse *p_response;
676 ATLine *p_cur;
677 int countCalls;
678 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700679 RIL_Call *p_calls;
680 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800681 int i;
682 int needRepoll = 0;
683
684#ifdef WORKAROUND_ERRONEOUS_ANSWER
685 int prevIncomingOrWaitingLine;
686
687 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
688 s_incomingOrWaitingLine = -1;
689#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
690
691 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
692
693 if (err != 0 || p_response->success == 0) {
694 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
695 return;
696 }
697
698 /* count the calls */
699 for (countCalls = 0, p_cur = p_response->p_intermediates
700 ; p_cur != NULL
701 ; p_cur = p_cur->p_next
702 ) {
703 countCalls++;
704 }
705
706 /* yes, there's an array of pointers and then an array of structures */
707
Wink Saville3d54e742009-05-18 18:00:44 -0700708 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
709 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
710 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800711
712 /* init the pointer array */
713 for(i = 0; i < countCalls ; i++) {
714 pp_calls[i] = &(p_calls[i]);
715 }
716
717 for (countValidCalls = 0, p_cur = p_response->p_intermediates
718 ; p_cur != NULL
719 ; p_cur = p_cur->p_next
720 ) {
721 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
722
723 if (err != 0) {
724 continue;
725 }
726
727#ifdef WORKAROUND_ERRONEOUS_ANSWER
728 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
729 || p_calls[countValidCalls].state == RIL_CALL_WAITING
730 ) {
731 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
732 }
733#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
734
735 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
736 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
737 ) {
738 needRepoll = 1;
739 }
740
741 countValidCalls++;
742 }
743
744#ifdef WORKAROUND_ERRONEOUS_ANSWER
745 // Basically:
746 // A call was incoming or waiting
747 // Now it's marked as active
748 // But we never answered it
749 //
750 // This is probably a bug, and the call will probably
751 // disappear from the call list in the next poll
752 if (prevIncomingOrWaitingLine >= 0
753 && s_incomingOrWaitingLine < 0
754 && s_expectAnswer == 0
755 ) {
756 for (i = 0; i < countValidCalls ; i++) {
757
758 if (p_calls[i].index == prevIncomingOrWaitingLine
759 && p_calls[i].state == RIL_CALL_ACTIVE
760 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
761 ) {
Wink Saville4dcab4f2012-11-19 16:05:13 -0800762 RLOGI(
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800763 "Hit WORKAROUND_ERRONOUS_ANSWER case."
764 " Repoll count: %d\n", s_repollCallsCount);
765 s_repollCallsCount++;
766 goto error;
767 }
768 }
769 }
770
771 s_expectAnswer = 0;
772 s_repollCallsCount = 0;
773#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
774
Wink Saville3d54e742009-05-18 18:00:44 -0700775 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
776 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800777
778 at_response_free(p_response);
779
780#ifdef POLL_CALL_STATE
781 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
782 // smd, so we're forced to poll until the call ends.
783#else
784 if (needRepoll) {
785#endif
786 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
787 }
788
789 return;
790error:
791 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
792 at_response_free(p_response);
793}
794
Mark Salyzynba58c202014-03-12 15:20:22 -0700795static void requestDial(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800796{
797 RIL_Dial *p_dial;
798 char *cmd;
799 const char *clir;
800 int ret;
801
802 p_dial = (RIL_Dial *)data;
803
804 switch (p_dial->clir) {
805 case 1: clir = "I"; break; /*invocation*/
806 case 2: clir = "i"; break; /*suppression*/
807 default:
808 case 0: clir = ""; break; /*subscription default*/
809 }
810
811 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
812
813 ret = at_send_command(cmd, NULL);
814
815 free(cmd);
816
817 /* success or failure is ignored by the upper layer here.
818 it will call GET_CURRENT_CALLS and determine success that way */
819 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
820}
821
Mark Salyzynba58c202014-03-12 15:20:22 -0700822static void requestWriteSmsToSim(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800823{
824 RIL_SMS_WriteArgs *p_args;
825 char *cmd;
826 int length;
827 int err;
828 ATResponse *p_response = NULL;
829
830 p_args = (RIL_SMS_WriteArgs *)data;
831
832 length = strlen(p_args->pdu)/2;
833 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
834
835 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
836
837 if (err != 0 || p_response->success == 0) goto error;
838
839 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
840 at_response_free(p_response);
841
842 return;
843error:
844 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
845 at_response_free(p_response);
846}
847
Mark Salyzynba58c202014-03-12 15:20:22 -0700848static void requestHangup(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800849{
850 int *p_line;
851
852 int ret;
853 char *cmd;
854
855 p_line = (int *)data;
856
857 // 3GPP 22.030 6.5.5
858 // "Releases a specific active call X"
859 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
860
861 ret = at_send_command(cmd, NULL);
862
863 free(cmd);
864
865 /* success or failure is ignored by the upper layer here.
866 it will call GET_CURRENT_CALLS and determine success that way */
867 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
868}
869
Mark Salyzynba58c202014-03-12 15:20:22 -0700870static void requestSignalStrength(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800871{
872 ATResponse *p_response = NULL;
873 int err;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800874 char *line;
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700875 int count =0;
876 int numofElements=sizeof(RIL_SignalStrength_v6)/sizeof(int);
877 int response[numofElements];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800878
879 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
880
881 if (err < 0 || p_response->success == 0) {
882 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
883 goto error;
884 }
885
886 line = p_response->p_intermediates->line;
887
888 err = at_tok_start(&line);
889 if (err < 0) goto error;
890
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700891 for (count =0; count < numofElements; count ++) {
892 err = at_tok_nextint(&line, &(response[count]));
893 if (err < 0) goto error;
894 }
Chih-Wei Huang28059052012-04-30 01:13:27 +0800895
Uma Maheswari Ramalingam9efcac52012-08-09 11:59:17 -0700896 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800897
898 at_response_free(p_response);
899 return;
900
901error:
Wink Saville4dcab4f2012-11-19 16:05:13 -0800902 RLOGE("requestSignalStrength must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800903 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
904 at_response_free(p_response);
905}
906
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700907/**
908 * networkModePossible. Decides whether the network mode is appropriate for the
909 * specified modem
910 */
911static int networkModePossible(ModemInfo *mdm, int nm)
912{
913 if ((net2modem[nm] & mdm->supportedTechs) == net2modem[nm]) {
914 return 1;
915 }
916 return 0;
917}
Mark Salyzynba58c202014-03-12 15:20:22 -0700918static void requestSetPreferredNetworkType( int request __unused, void *data,
919 size_t datalen __unused, RIL_Token t )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700920{
921 ATResponse *p_response = NULL;
922 char *cmd = NULL;
923 int value = *(int *)data;
924 int current, old;
925 int err;
926 int32_t preferred = net2pmask[value];
927
Wink Saville4dcab4f2012-11-19 16:05:13 -0800928 RLOGD("requestSetPreferredNetworkType: current: %x. New: %x", PREFERRED_NETWORK(sMdmInfo), preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700929 if (!networkModePossible(sMdmInfo, value)) {
930 RIL_onRequestComplete(t, RIL_E_MODE_NOT_SUPPORTED, NULL, 0);
931 return;
932 }
933 if (query_ctec(sMdmInfo, &current, NULL) < 0) {
934 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
935 return;
936 }
937 old = PREFERRED_NETWORK(sMdmInfo);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800938 RLOGD("old != preferred: %d", old != preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700939 if (old != preferred) {
940 asprintf(&cmd, "AT+CTEC=%d,\"%x\"", current, preferred);
Wink Saville4dcab4f2012-11-19 16:05:13 -0800941 RLOGD("Sending command: <%s>", cmd);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700942 err = at_send_command_singleline(cmd, "+CTEC:", &p_response);
943 free(cmd);
944 if (err || !p_response->success) {
945 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
946 return;
947 }
948 PREFERRED_NETWORK(sMdmInfo) = value;
949 if (!strstr( p_response->p_intermediates->line, "DONE") ) {
950 int current;
951 int res = parse_technology_response(p_response->p_intermediates->line, &current, NULL);
952 switch (res) {
953 case -1: // Error or unable to parse
954 break;
955 case 1: // Only able to parse current
956 case 0: // Both current and preferred were parsed
957 setRadioTechnology(sMdmInfo, current);
958 break;
959 }
960 }
961 }
962 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
963}
964
Mark Salyzynba58c202014-03-12 15:20:22 -0700965static void requestGetPreferredNetworkType(int request __unused, void *data __unused,
966 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700967{
968 int preferred;
969 unsigned i;
970
971 switch ( query_ctec(sMdmInfo, NULL, &preferred) ) {
972 case -1: // Error or unable to parse
973 case 1: // Only able to parse current
974 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
975 break;
976 case 0: // Both current and preferred were parsed
977 for ( i = 0 ; i < sizeof(net2pmask) / sizeof(int32_t) ; i++ ) {
978 if (preferred == net2pmask[i]) {
979 RIL_onRequestComplete(t, RIL_E_SUCCESS, &i, sizeof(int));
980 return;
981 }
982 }
Wink Saville4dcab4f2012-11-19 16:05:13 -0800983 RLOGE("Unknown preferred mode received from modem: %d", preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700984 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
985 break;
986 }
987
988}
989
Mark Salyzynba58c202014-03-12 15:20:22 -0700990static void requestCdmaPrlVersion(int request __unused, void *data __unused,
991 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -0700992{
993 int err;
994 char * responseStr;
995 ATResponse *p_response = NULL;
996 const char *cmd;
997 char *line;
998
999 err = at_send_command_singleline("AT+WPRL?", "+WPRL:", &p_response);
1000 if (err < 0 || !p_response->success) goto error;
1001 line = p_response->p_intermediates->line;
1002 err = at_tok_start(&line);
1003 if (err < 0) goto error;
1004 err = at_tok_nextstr(&line, &responseStr);
1005 if (err < 0 || !responseStr) goto error;
1006 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, strlen(responseStr));
1007 at_response_free(p_response);
1008 return;
1009error:
1010 at_response_free(p_response);
1011 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1012}
1013
Mark Salyzynba58c202014-03-12 15:20:22 -07001014static void requestCdmaBaseBandVersion(int request __unused, void *data __unused,
1015 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001016{
1017 int err;
1018 char * responseStr;
1019 ATResponse *p_response = NULL;
1020 const char *cmd;
1021 const char *prefix;
1022 char *line, *p;
1023 int commas;
1024 int skip;
1025 int count = 4;
1026
1027 // Fixed values. TODO: query modem
1028 responseStr = strdup("1.0.0.0");
1029 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, sizeof(responseStr));
1030 free(responseStr);
1031}
1032
Mark Salyzynba58c202014-03-12 15:20:22 -07001033static void requestCdmaDeviceIdentity(int request __unused, void *data __unused,
1034 size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001035{
1036 int err;
1037 int response[4];
1038 char * responseStr[4];
1039 ATResponse *p_response = NULL;
1040 const char *cmd;
1041 const char *prefix;
1042 char *line, *p;
1043 int commas;
1044 int skip;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001045 int count = 4;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001046
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001047 // Fixed values. TODO: Query modem
1048 responseStr[0] = "----";
1049 responseStr[1] = "----";
1050 responseStr[2] = "77777777";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001051
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001052 err = at_send_command_numeric("AT+CGSN", &p_response);
1053 if (err < 0 || p_response->success == 0) {
1054 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1055 return;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001056 } else {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001057 responseStr[3] = p_response->p_intermediates->line;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001058 }
1059
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001060 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1061 at_response_free(p_response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001062
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001063 return;
1064error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001065 RLOGE("requestCdmaDeviceIdentity must never return an error when radio is on");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001066 at_response_free(p_response);
1067 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1068}
1069
Mark Salyzynba58c202014-03-12 15:20:22 -07001070static void requestCdmaGetSubscriptionSource(int request __unused, void *data,
1071 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001072{
1073 int err;
1074 int *ss = (int *)data;
1075 ATResponse *p_response = NULL;
1076 char *cmd = NULL;
1077 char *line = NULL;
1078 int response;
1079
1080 asprintf(&cmd, "AT+CCSS?");
1081 if (!cmd) goto error;
1082
1083 err = at_send_command_singleline(cmd, "+CCSS:", &p_response);
1084 if (err < 0 || !p_response->success)
1085 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001086
1087 line = p_response->p_intermediates->line;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001088 err = at_tok_start(&line);
1089 if (err < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001090
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001091 err = at_tok_nextint(&line, &response);
1092 free(cmd);
1093 cmd = NULL;
1094
1095 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1096
1097 return;
1098error:
1099 free(cmd);
1100 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1101}
1102
Mark Salyzynba58c202014-03-12 15:20:22 -07001103static void requestCdmaSetSubscriptionSource(int request __unused, void *data,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001104 size_t datalen, RIL_Token t)
1105{
1106 int err;
1107 int *ss = (int *)data;
1108 ATResponse *p_response = NULL;
1109 char *cmd = NULL;
1110
1111 if (!ss || !datalen) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001112 RLOGE("RIL_REQUEST_CDMA_SET_SUBSCRIPTION without data!");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001113 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1114 return;
1115 }
1116 asprintf(&cmd, "AT+CCSS=%d", ss[0]);
1117 if (!cmd) goto error;
1118
1119 err = at_send_command(cmd, &p_response);
1120 if (err < 0 || !p_response->success)
1121 goto error;
1122 free(cmd);
1123 cmd = NULL;
1124
1125 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1126
1127 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED, ss, sizeof(ss[0]));
1128
1129 return;
1130error:
1131 free(cmd);
1132 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1133}
1134
Mark Salyzynba58c202014-03-12 15:20:22 -07001135static void requestCdmaSubscription(int request __unused, void *data __unused,
1136 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001137{
1138 int err;
1139 int response[5];
1140 char * responseStr[5];
1141 ATResponse *p_response = NULL;
1142 const char *cmd;
1143 const char *prefix;
1144 char *line, *p;
1145 int commas;
1146 int skip;
1147 int count = 5;
1148
1149 // Fixed values. TODO: Query modem
1150 responseStr[0] = "8587777777"; // MDN
1151 responseStr[1] = "1"; // SID
1152 responseStr[2] = "1"; // NID
1153 responseStr[3] = "8587777777"; // MIN
1154 responseStr[4] = "1"; // PRL Version
1155 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
1156
1157 return;
1158error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001159 RLOGE("requestRegistrationState must never return an error when radio is on");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001160 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1161}
1162
Mark Salyzynba58c202014-03-12 15:20:22 -07001163static void requestCdmaGetRoamingPreference(int request __unused, void *data __unused,
1164 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001165{
1166 int roaming_pref = -1;
1167 ATResponse *p_response = NULL;
1168 char *line;
1169 int res;
1170
1171 res = at_send_command_singleline("AT+WRMP?", "+WRMP:", &p_response);
1172 if (res < 0 || !p_response->success) {
1173 goto error;
1174 }
1175 line = p_response->p_intermediates->line;
1176
1177 res = at_tok_start(&line);
1178 if (res < 0) goto error;
1179
1180 res = at_tok_nextint(&line, &roaming_pref);
1181 if (res < 0) goto error;
1182
1183 RIL_onRequestComplete(t, RIL_E_SUCCESS, &roaming_pref, sizeof(roaming_pref));
1184 return;
1185error:
1186 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1187}
1188
Mark Salyzynba58c202014-03-12 15:20:22 -07001189static void requestCdmaSetRoamingPreference(int request __unused, void *data,
1190 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001191{
1192 int *pref = (int *)data;
1193 ATResponse *p_response = NULL;
1194 char *line;
1195 int res;
1196 char *cmd = NULL;
1197
1198 asprintf(&cmd, "AT+WRMP=%d", *pref);
1199 if (cmd == NULL) goto error;
1200
1201 res = at_send_command(cmd, &p_response);
1202 if (res < 0 || !p_response->success)
1203 goto error;
1204
1205 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1206 free(cmd);
1207 return;
1208error:
1209 free(cmd);
1210 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1211}
1212
1213static int parseRegistrationState(char *str, int *type, int *items, int **response)
1214{
1215 int err;
1216 char *line = str, *p;
1217 int *resp = NULL;
1218 int skip;
1219 int count = 3;
1220 int commas;
1221
Wink Saville4dcab4f2012-11-19 16:05:13 -08001222 RLOGD("parseRegistrationState. Parsing: %s",str);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001223 err = at_tok_start(&line);
1224 if (err < 0) goto error;
1225
1226 /* Ok you have to be careful here
1227 * The solicited version of the CREG response is
1228 * +CREG: n, stat, [lac, cid]
1229 * and the unsolicited version is
1230 * +CREG: stat, [lac, cid]
1231 * The <n> parameter is basically "is unsolicited creg on?"
1232 * which it should always be
1233 *
1234 * Now we should normally get the solicited version here,
1235 * but the unsolicited version could have snuck in
1236 * so we have to handle both
1237 *
1238 * Also since the LAC and CID are only reported when registered,
1239 * we can have 1, 2, 3, or 4 arguments here
1240 *
1241 * finally, a +CGREG: answer may have a fifth value that corresponds
1242 * to the network type, as in;
1243 *
1244 * +CGREG: n, stat [,lac, cid [,networkType]]
1245 */
1246
1247 /* count number of commas */
1248 commas = 0;
1249 for (p = line ; *p != '\0' ;p++) {
1250 if (*p == ',') commas++;
1251 }
1252
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001253 resp = (int *)calloc(commas + 1, sizeof(int));
1254 if (!resp) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001255 switch (commas) {
1256 case 0: /* +CREG: <stat> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001257 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001258 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001259 resp[1] = -1;
1260 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001261 break;
1262
1263 case 1: /* +CREG: <n>, <stat> */
1264 err = at_tok_nextint(&line, &skip);
1265 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001266 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001267 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001268 resp[1] = -1;
1269 resp[2] = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001270 if (err < 0) goto error;
1271 break;
1272
1273 case 2: /* +CREG: <stat>, <lac>, <cid> */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001274 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001275 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001276 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001277 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001278 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001279 if (err < 0) goto error;
1280 break;
1281 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
1282 err = at_tok_nextint(&line, &skip);
1283 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001284 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001285 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001286 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001287 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001288 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001289 if (err < 0) goto error;
1290 break;
1291 /* special case for CGREG, there is a fourth parameter
1292 * that is the network type (unknown/gprs/edge/umts)
1293 */
1294 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
1295 err = at_tok_nextint(&line, &skip);
1296 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001297 err = at_tok_nextint(&line, &resp[0]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001298 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001299 err = at_tok_nexthexint(&line, &resp[1]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001300 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001301 err = at_tok_nexthexint(&line, &resp[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001302 if (err < 0) goto error;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001303 err = at_tok_nexthexint(&line, &resp[3]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001304 if (err < 0) goto error;
1305 count = 4;
1306 break;
1307 default:
1308 goto error;
1309 }
Wink Saville8a9e0212013-04-09 12:11:38 -07001310 s_lac = resp[1];
1311 s_cid = resp[2];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001312 if (response)
1313 *response = resp;
1314 if (items)
1315 *items = commas + 1;
1316 if (type)
1317 *type = techFromModemType(TECH(sMdmInfo));
1318 return 0;
1319error:
1320 free(resp);
1321 return -1;
1322}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001323
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001324#define REG_STATE_LEN 15
1325#define REG_DATA_STATE_LEN 6
Mark Salyzynba58c202014-03-12 15:20:22 -07001326static void requestRegistrationState(int request, void *data __unused,
1327 size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001328{
1329 int err;
1330 int *registration;
1331 char **responseStr = NULL;
1332 ATResponse *p_response = NULL;
1333 const char *cmd;
1334 const char *prefix;
1335 char *line;
1336 int i = 0, j, numElements = 0;
1337 int count = 3;
1338 int type, startfrom;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001339
Wink Saville4dcab4f2012-11-19 16:05:13 -08001340 RLOGD("requestRegistrationState");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001341 if (request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1342 cmd = "AT+CREG?";
1343 prefix = "+CREG:";
1344 numElements = REG_STATE_LEN;
1345 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1346 cmd = "AT+CGREG?";
1347 prefix = "+CGREG:";
1348 numElements = REG_DATA_STATE_LEN;
1349 } else {
1350 assert(0);
1351 goto error;
1352 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001353
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001354 err = at_send_command_singleline(cmd, prefix, &p_response);
1355
1356 if (err != 0) goto error;
1357
1358 line = p_response->p_intermediates->line;
1359
1360 if (parseRegistrationState(line, &type, &count, &registration)) goto error;
1361
1362 responseStr = malloc(numElements * sizeof(char *));
1363 if (!responseStr) goto error;
1364 memset(responseStr, 0, numElements * sizeof(char *));
1365 /**
1366 * The first '4' bytes for both registration states remain the same.
1367 * But if the request is 'DATA_REGISTRATION_STATE',
1368 * the 5th and 6th byte(s) are optional.
1369 */
1370 if (is3gpp2(type) == 1) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001371 RLOGD("registration state type: 3GPP2");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001372 // TODO: Query modem
1373 startfrom = 3;
1374 if(request == RIL_REQUEST_VOICE_REGISTRATION_STATE) {
1375 asprintf(&responseStr[3], "8"); // EvDo revA
1376 asprintf(&responseStr[4], "1"); // BSID
1377 asprintf(&responseStr[5], "123"); // Latitude
1378 asprintf(&responseStr[6], "222"); // Longitude
1379 asprintf(&responseStr[7], "0"); // CSS Indicator
1380 asprintf(&responseStr[8], "4"); // SID
1381 asprintf(&responseStr[9], "65535"); // NID
1382 asprintf(&responseStr[10], "0"); // Roaming indicator
1383 asprintf(&responseStr[11], "1"); // System is in PRL
1384 asprintf(&responseStr[12], "0"); // Default Roaming indicator
1385 asprintf(&responseStr[13], "0"); // Reason for denial
1386 asprintf(&responseStr[14], "0"); // Primary Scrambling Code of Current cell
1387 } else if (request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1388 asprintf(&responseStr[3], "8"); // Available data radio technology
1389 }
1390 } else { // type == RADIO_TECH_3GPP
Wink Saville4dcab4f2012-11-19 16:05:13 -08001391 RLOGD("registration state type: 3GPP");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001392 startfrom = 0;
1393 asprintf(&responseStr[1], "%x", registration[1]);
1394 asprintf(&responseStr[2], "%x", registration[2]);
1395 if (count > 3)
1396 asprintf(&responseStr[3], "%d", registration[3]);
1397 }
1398 asprintf(&responseStr[0], "%d", registration[0]);
1399
1400 /**
1401 * Optional bytes for DATA_REGISTRATION_STATE request
1402 * 4th byte : Registration denial code
1403 * 5th byte : The max. number of simultaneous Data Calls
1404 */
1405 if(request == RIL_REQUEST_DATA_REGISTRATION_STATE) {
1406 // asprintf(&responseStr[4], "3");
1407 // asprintf(&responseStr[5], "1");
1408 }
1409
1410 for (j = startfrom; j < numElements; j++) {
1411 if (!responseStr[i]) goto error;
1412 }
1413 free(registration);
1414 registration = NULL;
1415
1416 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, numElements*sizeof(responseStr));
1417 for (j = 0; j < numElements; j++ ) {
1418 free(responseStr[j]);
1419 responseStr[j] = NULL;
1420 }
1421 free(responseStr);
1422 responseStr = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001423 at_response_free(p_response);
1424
1425 return;
1426error:
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001427 if (responseStr) {
1428 for (j = 0; j < numElements; j++) {
1429 free(responseStr[j]);
1430 responseStr[j] = NULL;
1431 }
1432 free(responseStr);
1433 responseStr = NULL;
1434 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08001435 RLOGE("requestRegistrationState must never return an error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001436 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1437 at_response_free(p_response);
1438}
1439
Mark Salyzynba58c202014-03-12 15:20:22 -07001440static void requestOperator(void *data __unused, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001441{
1442 int err;
1443 int i;
1444 int skip;
1445 ATLine *p_cur;
1446 char *response[3];
1447
1448 memset(response, 0, sizeof(response));
1449
1450 ATResponse *p_response = NULL;
1451
1452 err = at_send_command_multiline(
1453 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
1454 "+COPS:", &p_response);
1455
1456 /* we expect 3 lines here:
1457 * +COPS: 0,0,"T - Mobile"
1458 * +COPS: 0,1,"TMO"
1459 * +COPS: 0,2,"310170"
1460 */
1461
1462 if (err != 0) goto error;
1463
1464 for (i = 0, p_cur = p_response->p_intermediates
1465 ; p_cur != NULL
1466 ; p_cur = p_cur->p_next, i++
1467 ) {
1468 char *line = p_cur->line;
1469
1470 err = at_tok_start(&line);
1471 if (err < 0) goto error;
1472
1473 err = at_tok_nextint(&line, &skip);
1474 if (err < 0) goto error;
1475
1476 // If we're unregistered, we may just get
1477 // a "+COPS: 0" response
1478 if (!at_tok_hasmore(&line)) {
1479 response[i] = NULL;
1480 continue;
1481 }
1482
1483 err = at_tok_nextint(&line, &skip);
1484 if (err < 0) goto error;
1485
1486 // a "+COPS: 0, n" response is also possible
1487 if (!at_tok_hasmore(&line)) {
1488 response[i] = NULL;
1489 continue;
1490 }
1491
1492 err = at_tok_nextstr(&line, &(response[i]));
1493 if (err < 0) goto error;
Wink Saville8a9e0212013-04-09 12:11:38 -07001494 // Simple assumption that mcc and mnc are 3 digits each
1495 if (strlen(response[i]) == 6) {
1496 if (sscanf(response[i], "%3d%3d", &s_mcc, &s_mnc) != 2) {
1497 RLOGE("requestOperator expected mccmnc to be 6 decimal digits");
1498 }
1499 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001500 }
1501
1502 if (i != 3) {
1503 /* expect 3 lines exactly */
1504 goto error;
1505 }
1506
1507 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
1508 at_response_free(p_response);
1509
1510 return;
1511error:
Wink Saville4dcab4f2012-11-19 16:05:13 -08001512 RLOGE("requestOperator must not return error when radio is on");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001513 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1514 at_response_free(p_response);
1515}
1516
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001517static void requestCdmaSendSMS(void *data, size_t datalen, RIL_Token t)
1518{
1519 int err = 1; // Set to go to error:
1520 RIL_SMS_Response response;
1521 RIL_CDMA_SMS_Message* rcsm;
1522
Mark Salyzynba58c202014-03-12 15:20:22 -07001523 RLOGD("requestCdmaSendSMS datalen=%zu, sizeof(RIL_CDMA_SMS_Message)=%zu",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001524 datalen, sizeof(RIL_CDMA_SMS_Message));
1525
1526 // verify data content to test marshalling/unmarshalling:
1527 rcsm = (RIL_CDMA_SMS_Message*)data;
Wink Saville4dcab4f2012-11-19 16:05:13 -08001528 RLOGD("TeleserviceID=%d, bIsServicePresent=%d, \
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001529 uServicecategory=%d, sAddress.digit_mode=%d, \
1530 sAddress.Number_mode=%d, sAddress.number_type=%d, ",
1531 rcsm->uTeleserviceID, rcsm->bIsServicePresent,
1532 rcsm->uServicecategory,rcsm->sAddress.digit_mode,
1533 rcsm->sAddress.number_mode,rcsm->sAddress.number_type);
1534
1535 if (err != 0) goto error;
1536
1537 // Cdma Send SMS implementation will go here:
1538 // But it is not implemented yet.
1539
1540 memset(&response, 0, sizeof(response));
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001541 response.messageRef = 1;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001542 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1543 return;
1544
1545error:
1546 // Cdma Send SMS will always cause send retry error.
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001547 response.messageRef = -1;
1548 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001549}
1550
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001551static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
1552{
1553 int err;
1554 const char *smsc;
1555 const char *pdu;
1556 int tpLayerLength;
1557 char *cmd1, *cmd2;
1558 RIL_SMS_Response response;
1559 ATResponse *p_response = NULL;
1560
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001561 memset(&response, 0, sizeof(response));
Mark Salyzynba58c202014-03-12 15:20:22 -07001562 RLOGD("requestSendSMS datalen =%zu", datalen);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001563
1564 if (s_ims_gsm_fail != 0) goto error;
1565 if (s_ims_gsm_retry != 0) goto error2;
1566
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001567 smsc = ((const char **)data)[0];
1568 pdu = ((const char **)data)[1];
1569
1570 tpLayerLength = strlen(pdu)/2;
1571
1572 // "NULL for default SMSC"
1573 if (smsc == NULL) {
1574 smsc= "00";
1575 }
1576
1577 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
1578 asprintf(&cmd2, "%s%s", smsc, pdu);
1579
1580 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
1581
1582 if (err != 0 || p_response->success == 0) goto error;
1583
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001584 /* FIXME fill in messageRef and ackPDU */
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001585 response.messageRef = 1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001586 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
1587 at_response_free(p_response);
1588
1589 return;
1590error:
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001591 response.messageRef = -2;
1592 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001593 at_response_free(p_response);
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001594 return;
1595error2:
1596 // send retry error.
1597 response.messageRef = -1;
1598 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
1599 at_response_free(p_response);
1600 return;
1601 }
1602
1603static void requestImsSendSMS(void *data, size_t datalen, RIL_Token t)
1604{
1605 RIL_IMS_SMS_Message *p_args;
1606 RIL_SMS_Response response;
1607
1608 memset(&response, 0, sizeof(response));
1609
Mark Salyzynba58c202014-03-12 15:20:22 -07001610 RLOGD("requestImsSendSMS: datalen=%zu, "
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07001611 "registered=%d, service=%d, format=%d, ims_perm_fail=%d, "
1612 "ims_retry=%d, gsm_fail=%d, gsm_retry=%d",
1613 datalen, s_ims_registered, s_ims_services, s_ims_format,
1614 s_ims_cause_perm_failure, s_ims_cause_retry, s_ims_gsm_fail,
1615 s_ims_gsm_retry);
1616
1617 // figure out if this is gsm/cdma format
1618 // then route it to requestSendSMS vs requestCdmaSendSMS respectively
1619 p_args = (RIL_IMS_SMS_Message *)data;
1620
1621 if (0 != s_ims_cause_perm_failure ) goto error;
1622
1623 // want to fail over ims and this is first request over ims
1624 if (0 != s_ims_cause_retry && 0 == p_args->retry) goto error2;
1625
1626 if (RADIO_TECH_3GPP == p_args->tech) {
1627 return requestSendSMS(p_args->message.gsmMessage,
1628 datalen - sizeof(RIL_RadioTechnologyFamily),
1629 t);
1630 } else if (RADIO_TECH_3GPP2 == p_args->tech) {
1631 return requestCdmaSendSMS(p_args->message.cdmaMessage,
1632 datalen - sizeof(RIL_RadioTechnologyFamily),
1633 t);
1634 } else {
1635 RLOGE("requestImsSendSMS invalid format value =%d", p_args->tech);
1636 }
1637
1638error:
1639 response.messageRef = -2;
1640 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, &response, sizeof(response));
1641 return;
1642
1643error2:
1644 response.messageRef = -1;
1645 RIL_onRequestComplete(t, RIL_E_SMS_SEND_FAIL_RETRY, &response, sizeof(response));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001646}
1647
Wink Savillef4c4d362009-04-02 01:37:03 -07001648static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001649{
1650 const char *apn;
1651 char *cmd;
1652 int err;
1653 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001654
Wink Savillef4c4d362009-04-02 01:37:03 -07001655 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001656
1657#ifdef USE_TI_COMMANDS
1658 // Config for multislot class 10 (probably default anyway eh?)
1659 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1660 NULL);
1661
1662 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1663#endif /* USE_TI_COMMANDS */
1664
1665 int fd, qmistatus;
1666 size_t cur = 0;
1667 size_t len;
1668 ssize_t written, rlen;
1669 char status[32] = {0};
1670 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001671 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001672
Wink Saville4dcab4f2012-11-19 16:05:13 -08001673 RLOGD("requesting data connection to APN '%s'", apn);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001674
1675 fd = open ("/dev/qmi", O_RDWR);
1676 if (fd >= 0) { /* the device doesn't exist on the emulator */
1677
Wink Saville4dcab4f2012-11-19 16:05:13 -08001678 RLOGD("opened the qmi device\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001679 asprintf(&cmd, "up:%s", apn);
1680 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001681
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001682 while (cur < len) {
1683 do {
1684 written = write (fd, cmd + cur, len - cur);
1685 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001686
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001687 if (written < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001688 RLOGE("### ERROR writing to /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001689 close(fd);
1690 goto error;
1691 }
1692
1693 cur += written;
1694 }
1695
1696 // wait for interface to come online
1697
1698 do {
1699 sleep(1);
1700 do {
1701 rlen = read(fd, status, 31);
1702 } while (rlen < 0 && errno == EINTR);
1703
1704 if (rlen < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001705 RLOGE("### ERROR reading from /dev/qmi");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001706 close(fd);
1707 goto error;
1708 } else {
1709 status[rlen] = '\0';
Wink Saville4dcab4f2012-11-19 16:05:13 -08001710 RLOGD("### status: %s", status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001711 }
1712 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1713
1714 close(fd);
1715
1716 if (retry == 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001717 RLOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001718 goto error;
1719 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001720
1721 qmistatus = system("netcfg rmnet0 dhcp");
1722
Wink Saville4dcab4f2012-11-19 16:05:13 -08001723 RLOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001724
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001725 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001726
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001727 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001728
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001729 if (datalen > 6 * sizeof(char *)) {
1730 pdp_type = ((const char **)data)[6];
1731 } else {
1732 pdp_type = "IP";
1733 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001734
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001735 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001736 //FIXME check for error here
1737 err = at_send_command(cmd, NULL);
1738 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001739
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001740 // Set required QoS params to default
1741 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001742
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001743 // Set minimum QoS params to default
1744 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001745
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001746 // packet-domain event reporting
1747 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001748
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001749 // Hangup anything that's happening there now
1750 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001751
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001752 // Start data on PDP context 1
1753 err = at_send_command("ATD*99***1#", &p_response);
1754
1755 if (err < 0 || p_response->success == 0) {
1756 goto error;
1757 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001758 }
1759
Wink Saville43808972011-01-13 17:39:51 -08001760 requestOrSendDataCallList(&t);
1761
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001762 at_response_free(p_response);
1763
1764 return;
1765error:
1766 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1767 at_response_free(p_response);
1768
1769}
1770
Mark Salyzynba58c202014-03-12 15:20:22 -07001771static void requestSMSAcknowledge(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001772{
1773 int ackSuccess;
1774 int err;
1775
1776 ackSuccess = ((int *)data)[0];
1777
1778 if (ackSuccess == 1) {
1779 err = at_send_command("AT+CNMA=1", NULL);
1780 } else if (ackSuccess == 0) {
1781 err = at_send_command("AT+CNMA=2", NULL);
1782 } else {
Wink Saville4dcab4f2012-11-19 16:05:13 -08001783 RLOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001784 goto error;
1785 }
1786
1787 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1788error:
1789 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1790
1791}
1792
Mark Salyzynba58c202014-03-12 15:20:22 -07001793static void requestSIM_IO(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001794{
1795 ATResponse *p_response = NULL;
1796 RIL_SIM_IO_Response sr;
1797 int err;
1798 char *cmd = NULL;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001799 RIL_SIM_IO_v6 *p_args;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001800 char *line;
1801
1802 memset(&sr, 0, sizeof(sr));
1803
Wink Saville2c1fb3a2011-03-19 13:42:45 -07001804 p_args = (RIL_SIM_IO_v6 *)data;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001805
1806 /* FIXME handle pin2 */
1807
1808 if (p_args->data == NULL) {
1809 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1810 p_args->command, p_args->fileid,
1811 p_args->p1, p_args->p2, p_args->p3);
1812 } else {
1813 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1814 p_args->command, p_args->fileid,
1815 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1816 }
1817
1818 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1819
1820 if (err < 0 || p_response->success == 0) {
1821 goto error;
1822 }
1823
1824 line = p_response->p_intermediates->line;
1825
1826 err = at_tok_start(&line);
1827 if (err < 0) goto error;
1828
1829 err = at_tok_nextint(&line, &(sr.sw1));
1830 if (err < 0) goto error;
1831
1832 err = at_tok_nextint(&line, &(sr.sw2));
1833 if (err < 0) goto error;
1834
1835 if (at_tok_hasmore(&line)) {
1836 err = at_tok_nextstr(&line, &(sr.simResponse));
1837 if (err < 0) goto error;
1838 }
1839
1840 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1841 at_response_free(p_response);
1842 free(cmd);
1843
1844 return;
1845error:
1846 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1847 at_response_free(p_response);
1848 free(cmd);
1849
1850}
1851
1852static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1853{
1854 ATResponse *p_response = NULL;
1855 int err;
1856 char* cmd = NULL;
1857 const char** strings = (const char**)data;;
1858
1859 if ( datalen == sizeof(char*) ) {
1860 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1861 } else if ( datalen == 2*sizeof(char*) ) {
1862 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1863 } else
1864 goto error;
1865
1866 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1867 free(cmd);
1868
1869 if (err < 0 || p_response->success == 0) {
1870error:
1871 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1872 } else {
1873 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1874 }
1875 at_response_free(p_response);
1876}
1877
1878
Mark Salyzynba58c202014-03-12 15:20:22 -07001879static void requestSendUSSD(void *data, size_t datalen __unused, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001880{
1881 const char *ussdRequest;
1882
1883 ussdRequest = (char *)(data);
1884
1885
1886 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1887
1888// @@@ TODO
1889
1890}
1891
Mark Salyzynba58c202014-03-12 15:20:22 -07001892static void requestExitEmergencyMode(void *data __unused, size_t datalen __unused, RIL_Token t)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07001893{
1894 int err;
1895 ATResponse *p_response = NULL;
1896
1897 err = at_send_command("AT+WSOS=0", &p_response);
1898
1899 if (err < 0 || p_response->success == 0) {
1900 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1901 return;
1902 }
1903
1904 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1905}
1906
1907// TODO: Use all radio types
1908static int techFromModemType(int mdmtype)
1909{
1910 int ret = -1;
1911 switch (1 << mdmtype) {
1912 case MDM_CDMA:
1913 ret = RADIO_TECH_1xRTT;
1914 break;
1915 case MDM_EVDO:
1916 ret = RADIO_TECH_EVDO_A;
1917 break;
1918 case MDM_GSM:
1919 ret = RADIO_TECH_GPRS;
1920 break;
1921 case MDM_WCDMA:
1922 ret = RADIO_TECH_HSPA;
1923 break;
1924 case MDM_LTE:
1925 ret = RADIO_TECH_LTE;
1926 break;
1927 }
1928 return ret;
1929}
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001930
Mark Salyzynba58c202014-03-12 15:20:22 -07001931static void requestGetCellInfoList(void *data __unused, size_t datalen __unused, RIL_Token t)
Wink Saville8a9e0212013-04-09 12:11:38 -07001932{
1933 uint64_t curTime = ril_nano_time();
1934 RIL_CellInfo ci[1] =
1935 {
1936 { // ci[0]
1937 1, // cellInfoType
1938 1, // registered
1939 curTime - 1000, // Fake some time in the past
1940 { // union CellInfo
1941 { // RIL_CellInfoGsm gsm
1942 { // gsm.cellIdneityGsm
1943 s_mcc, // mcc
1944 s_mnc, // mnc
1945 s_lac, // lac
1946 s_cid, // cid
1947 0 // psc
1948 },
1949 { // gsm.signalStrengthGsm
1950 10, // signalStrength
1951 0 // bitErrorRate
1952 }
1953 }
1954 }
1955 }
1956 };
1957
1958 RIL_onRequestComplete(t, RIL_E_SUCCESS, ci, sizeof(ci));
1959}
1960
1961
1962static void requestSetCellInfoListRate(void *data, size_t datalen, RIL_Token t)
1963{
1964 // For now we'll save the rate but no RIL_UNSOL_CELL_INFO_LIST messages
1965 // will be sent.
1966 assert (datalen == sizeof(int));
1967 s_cell_info_rate_ms = ((int *)data)[0];
1968
1969 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1970}
1971
Etan Cohend3652192014-06-20 08:28:44 -07001972static void requestGetHardwareConfig(void *data, size_t datalen, RIL_Token t)
1973{
1974 // TODO - hook this up with real query/info from radio.
1975
1976 RIL_HardwareConfig hwCfg;
1977
1978 RIL_UNUSED_PARM(data);
1979 RIL_UNUSED_PARM(datalen);
1980
1981 hwCfg.type = -1;
1982
1983 RIL_onRequestComplete(t, RIL_E_SUCCESS, &hwCfg, sizeof(hwCfg));
1984}
1985
1986
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001987/*** Callback methods from the RIL library to us ***/
1988
1989/**
1990 * Call from RIL to us to make a RIL_REQUEST
1991 *
1992 * Must be completed with a call to RIL_onRequestComplete()
1993 *
1994 * RIL_onRequestComplete() may be called from any thread, before or after
1995 * this function returns.
1996 *
1997 * Will always be called from the same thread, so returning here implies
1998 * that the radio is ready to process another command (whether or not
1999 * the previous command has completed).
2000 */
2001static void
2002onRequest (int request, void *data, size_t datalen, RIL_Token t)
2003{
2004 ATResponse *p_response;
2005 int err;
2006
Wink Saville4dcab4f2012-11-19 16:05:13 -08002007 RLOGD("onRequest: %s", requestToString(request));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002008
2009 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
2010 * when RADIO_STATE_UNAVAILABLE.
2011 */
2012 if (sState == RADIO_STATE_UNAVAILABLE
2013 && request != RIL_REQUEST_GET_SIM_STATUS
2014 ) {
2015 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2016 return;
2017 }
2018
2019 /* Ignore all non-power requests when RADIO_STATE_OFF
2020 * (except RIL_REQUEST_GET_SIM_STATUS)
2021 */
2022 if (sState == RADIO_STATE_OFF
2023 && !(request == RIL_REQUEST_RADIO_POWER
2024 || request == RIL_REQUEST_GET_SIM_STATUS)
2025 ) {
2026 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
2027 return;
2028 }
2029
2030 switch (request) {
2031 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002032 RIL_CardStatus_v6 *p_card_status;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002033 char *p_buffer;
2034 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002035
Wink Savillef6aa7c12009-04-30 14:20:52 -07002036 int result = getCardStatus(&p_card_status);
2037 if (result == RIL_E_SUCCESS) {
2038 p_buffer = (char *)p_card_status;
2039 buffer_size = sizeof(*p_card_status);
2040 } else {
2041 p_buffer = NULL;
2042 buffer_size = 0;
2043 }
2044 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
2045 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002046 break;
2047 }
2048 case RIL_REQUEST_GET_CURRENT_CALLS:
2049 requestGetCurrentCalls(data, datalen, t);
2050 break;
2051 case RIL_REQUEST_DIAL:
2052 requestDial(data, datalen, t);
2053 break;
2054 case RIL_REQUEST_HANGUP:
2055 requestHangup(data, datalen, t);
2056 break;
2057 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
2058 // 3GPP 22.030 6.5.5
2059 // "Releases all held calls or sets User Determined User Busy
2060 // (UDUB) for a waiting call."
2061 at_send_command("AT+CHLD=0", NULL);
2062
2063 /* success or failure is ignored by the upper layer here.
2064 it will call GET_CURRENT_CALLS and determine success that way */
2065 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2066 break;
2067 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
2068 // 3GPP 22.030 6.5.5
2069 // "Releases all active calls (if any exist) and accepts
2070 // the other (held or waiting) call."
2071 at_send_command("AT+CHLD=1", NULL);
2072
2073 /* success or failure is ignored by the upper layer here.
2074 it will call GET_CURRENT_CALLS and determine success that way */
2075 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2076 break;
2077 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
2078 // 3GPP 22.030 6.5.5
2079 // "Places all active calls (if any exist) on hold and accepts
2080 // the other (held or waiting) call."
2081 at_send_command("AT+CHLD=2", NULL);
2082
2083#ifdef WORKAROUND_ERRONEOUS_ANSWER
2084 s_expectAnswer = 1;
2085#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2086
2087 /* success or failure is ignored by the upper layer here.
2088 it will call GET_CURRENT_CALLS and determine success that way */
2089 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2090 break;
2091 case RIL_REQUEST_ANSWER:
2092 at_send_command("ATA", NULL);
2093
2094#ifdef WORKAROUND_ERRONEOUS_ANSWER
2095 s_expectAnswer = 1;
2096#endif /* WORKAROUND_ERRONEOUS_ANSWER */
2097
2098 /* success or failure is ignored by the upper layer here.
2099 it will call GET_CURRENT_CALLS and determine success that way */
2100 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2101 break;
2102 case RIL_REQUEST_CONFERENCE:
2103 // 3GPP 22.030 6.5.5
2104 // "Adds a held call to the conversation"
2105 at_send_command("AT+CHLD=3", NULL);
2106
2107 /* success or failure is ignored by the upper layer here.
2108 it will call GET_CURRENT_CALLS and determine success that way */
2109 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2110 break;
2111 case RIL_REQUEST_UDUB:
2112 /* user determined user busy */
2113 /* sometimes used: ATH */
2114 at_send_command("ATH", NULL);
2115
2116 /* success or failure is ignored by the upper layer here.
2117 it will call GET_CURRENT_CALLS and determine success that way */
2118 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2119 break;
2120
2121 case RIL_REQUEST_SEPARATE_CONNECTION:
2122 {
2123 char cmd[12];
2124 int party = ((int*)data)[0];
2125
2126 // Make sure that party is in a valid range.
2127 // (Note: The Telephony middle layer imposes a range of 1 to 7.
2128 // It's sufficient for us to just make sure it's single digit.)
2129 if (party > 0 && party < 10) {
2130 sprintf(cmd, "AT+CHLD=2%d", party);
2131 at_send_command(cmd, NULL);
2132 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2133 } else {
2134 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2135 }
2136 }
2137 break;
2138
2139 case RIL_REQUEST_SIGNAL_STRENGTH:
2140 requestSignalStrength(data, datalen, t);
2141 break;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002142 case RIL_REQUEST_VOICE_REGISTRATION_STATE:
2143 case RIL_REQUEST_DATA_REGISTRATION_STATE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002144 requestRegistrationState(request, data, datalen, t);
2145 break;
2146 case RIL_REQUEST_OPERATOR:
2147 requestOperator(data, datalen, t);
2148 break;
2149 case RIL_REQUEST_RADIO_POWER:
2150 requestRadioPower(data, datalen, t);
2151 break;
2152 case RIL_REQUEST_DTMF: {
2153 char c = ((char *)data)[0];
2154 char *cmd;
2155 asprintf(&cmd, "AT+VTS=%c", (int)c);
2156 at_send_command(cmd, NULL);
2157 free(cmd);
2158 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2159 break;
2160 }
2161 case RIL_REQUEST_SEND_SMS:
Chaitanya Saggurthi33bbe432013-09-24 16:16:21 +05302162 case RIL_REQUEST_SEND_SMS_EXPECT_MORE:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002163 requestSendSMS(data, datalen, t);
2164 break;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002165 case RIL_REQUEST_CDMA_SEND_SMS:
2166 requestCdmaSendSMS(data, datalen, t);
2167 break;
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002168 case RIL_REQUEST_IMS_SEND_SMS:
2169 requestImsSendSMS(data, datalen, t);
2170 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07002171 case RIL_REQUEST_SETUP_DATA_CALL:
2172 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002173 break;
2174 case RIL_REQUEST_SMS_ACKNOWLEDGE:
2175 requestSMSAcknowledge(data, datalen, t);
2176 break;
2177
2178 case RIL_REQUEST_GET_IMSI:
2179 p_response = NULL;
2180 err = at_send_command_numeric("AT+CIMI", &p_response);
2181
2182 if (err < 0 || p_response->success == 0) {
2183 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2184 } else {
2185 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2186 p_response->p_intermediates->line, sizeof(char *));
2187 }
2188 at_response_free(p_response);
2189 break;
2190
2191 case RIL_REQUEST_GET_IMEI:
2192 p_response = NULL;
2193 err = at_send_command_numeric("AT+CGSN", &p_response);
2194
2195 if (err < 0 || p_response->success == 0) {
2196 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2197 } else {
2198 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2199 p_response->p_intermediates->line, sizeof(char *));
2200 }
2201 at_response_free(p_response);
2202 break;
2203
2204 case RIL_REQUEST_SIM_IO:
2205 requestSIM_IO(data,datalen,t);
2206 break;
2207
2208 case RIL_REQUEST_SEND_USSD:
2209 requestSendUSSD(data, datalen, t);
2210 break;
2211
2212 case RIL_REQUEST_CANCEL_USSD:
2213 p_response = NULL;
2214 err = at_send_command_numeric("AT+CUSD=2", &p_response);
2215
2216 if (err < 0 || p_response->success == 0) {
2217 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2218 } else {
2219 RIL_onRequestComplete(t, RIL_E_SUCCESS,
2220 p_response->p_intermediates->line, sizeof(char *));
2221 }
2222 at_response_free(p_response);
2223 break;
2224
2225 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
2226 at_send_command("AT+COPS=0", NULL);
2227 break;
2228
Wink Savillef4c4d362009-04-02 01:37:03 -07002229 case RIL_REQUEST_DATA_CALL_LIST:
2230 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002231 break;
2232
2233 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
2234 requestQueryNetworkSelectionMode(data, datalen, t);
2235 break;
2236
2237 case RIL_REQUEST_OEM_HOOK_RAW:
2238 // echo back data
2239 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2240 break;
2241
2242
2243 case RIL_REQUEST_OEM_HOOK_STRINGS: {
2244 int i;
2245 const char ** cur;
2246
Wink Saville4dcab4f2012-11-19 16:05:13 -08002247 RLOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002248
2249
2250 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
2251 i > 0 ; cur++, i --) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002252 RLOGD("> '%s'", *cur);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002253 }
2254
2255 // echo back strings
2256 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
2257 break;
2258 }
2259
2260 case RIL_REQUEST_WRITE_SMS_TO_SIM:
2261 requestWriteSmsToSim(data, datalen, t);
2262 break;
2263
2264 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
2265 char * cmd;
2266 p_response = NULL;
2267 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
2268 err = at_send_command(cmd, &p_response);
2269 free(cmd);
2270 if (err < 0 || p_response->success == 0) {
2271 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2272 } else {
2273 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
2274 }
2275 at_response_free(p_response);
2276 break;
2277 }
2278
2279 case RIL_REQUEST_ENTER_SIM_PIN:
2280 case RIL_REQUEST_ENTER_SIM_PUK:
2281 case RIL_REQUEST_ENTER_SIM_PIN2:
2282 case RIL_REQUEST_ENTER_SIM_PUK2:
2283 case RIL_REQUEST_CHANGE_SIM_PIN:
2284 case RIL_REQUEST_CHANGE_SIM_PIN2:
2285 requestEnterSimPin(data, datalen, t);
2286 break;
2287
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07002288 case RIL_REQUEST_IMS_REGISTRATION_STATE: {
2289 int reply[2];
2290 //0==unregistered, 1==registered
2291 reply[0] = s_ims_registered;
2292
2293 //to be used when changed to include service supporated info
2294 //reply[1] = s_ims_services;
2295
2296 // FORMAT_3GPP(1) vs FORMAT_3GPP2(2);
2297 reply[1] = s_ims_format;
2298
2299 RLOGD("IMS_REGISTRATION=%d, format=%d ",
2300 reply[0], reply[1]);
2301 if (reply[1] != -1) {
2302 RIL_onRequestComplete(t, RIL_E_SUCCESS, reply, sizeof(reply));
2303 } else {
2304 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2305 }
2306 break;
2307 }
2308
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002309 case RIL_REQUEST_VOICE_RADIO_TECH:
2310 {
2311 int tech = techFromModemType(TECH(sMdmInfo));
2312 if (tech < 0 )
2313 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
2314 else
2315 RIL_onRequestComplete(t, RIL_E_SUCCESS, &tech, sizeof(tech));
2316 }
2317 break;
2318 case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE:
2319 requestSetPreferredNetworkType(request, data, datalen, t);
2320 break;
2321
2322 case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE:
2323 requestGetPreferredNetworkType(request, data, datalen, t);
2324 break;
2325
Jun Tian58027012013-07-30 11:07:22 +08002326 case RIL_REQUEST_GET_CELL_INFO_LIST:
2327 requestGetCellInfoList(data, datalen, t);
2328 break;
2329
2330 case RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE:
2331 requestSetCellInfoListRate(data, datalen, t);
2332 break;
2333
Etan Cohend3652192014-06-20 08:28:44 -07002334 case RIL_REQUEST_GET_HARDWARE_CONFIG:
2335 requestGetHardwareConfig(data, datalen, t);
2336 break;
2337
Naveen Kallaa65a16a2014-07-31 16:48:31 -07002338 case RIL_REQUEST_SHUTDOWN:
2339 requestShutdown(t);
2340 break;
2341
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002342 /* CDMA Specific Requests */
2343 case RIL_REQUEST_BASEBAND_VERSION:
2344 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2345 requestCdmaBaseBandVersion(request, data, datalen, t);
2346 break;
2347 } // Fall-through if tech is not cdma
2348
2349 case RIL_REQUEST_DEVICE_IDENTITY:
2350 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2351 requestCdmaDeviceIdentity(request, data, datalen, t);
2352 break;
2353 } // Fall-through if tech is not cdma
2354
2355 case RIL_REQUEST_CDMA_SUBSCRIPTION:
2356 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2357 requestCdmaSubscription(request, data, datalen, t);
2358 break;
2359 } // Fall-through if tech is not cdma
2360
2361 case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE:
2362 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2363 requestCdmaSetSubscriptionSource(request, data, datalen, t);
2364 break;
2365 } // Fall-through if tech is not cdma
2366
2367 case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE:
2368 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2369 requestCdmaGetSubscriptionSource(request, data, datalen, t);
2370 break;
2371 } // Fall-through if tech is not cdma
2372
2373 case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE:
2374 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2375 requestCdmaGetRoamingPreference(request, data, datalen, t);
2376 break;
2377 } // Fall-through if tech is not cdma
2378
2379 case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE:
2380 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2381 requestCdmaSetRoamingPreference(request, data, datalen, t);
2382 break;
2383 } // Fall-through if tech is not cdma
2384
2385 case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE:
2386 if (TECH_BIT(sMdmInfo) == MDM_CDMA) {
2387 requestExitEmergencyMode(data, datalen, t);
2388 break;
2389 } // Fall-through if tech is not cdma
2390
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002391 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002392 RLOGD("Request not supported. Tech: %d",TECH(sMdmInfo));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002393 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
2394 break;
2395 }
2396}
2397
2398/**
2399 * Synchronous call from the RIL to us to return current radio state.
2400 * RADIO_STATE_UNAVAILABLE should be the initial state.
2401 */
2402static RIL_RadioState
2403currentState()
2404{
2405 return sState;
2406}
2407/**
2408 * Call from RIL to us to find out whether a specific request code
2409 * is supported by this implementation.
2410 *
2411 * Return 1 for "supported" and 0 for "unsupported"
2412 */
2413
2414static int
Mark Salyzynba58c202014-03-12 15:20:22 -07002415onSupports (int requestCode __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002416{
2417 //@@@ todo
2418
2419 return 1;
2420}
2421
Mark Salyzynba58c202014-03-12 15:20:22 -07002422static void onCancel (RIL_Token t __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002423{
2424 //@@@todo
2425
2426}
2427
2428static const char * getVersion(void)
2429{
2430 return "android reference-ril 1.0";
2431}
2432
2433static void
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002434setRadioTechnology(ModemInfo *mdm, int newtech)
2435{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002436 RLOGD("setRadioTechnology(%d)", newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002437
2438 int oldtech = TECH(mdm);
2439
2440 if (newtech != oldtech) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002441 RLOGD("Tech change (%d => %d)", oldtech, newtech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002442 TECH(mdm) = newtech;
2443 if (techFromModemType(newtech) != techFromModemType(oldtech)) {
2444 int tech = techFromModemType(TECH(sMdmInfo));
2445 if (tech > 0 ) {
2446 RIL_onUnsolicitedResponse(RIL_UNSOL_VOICE_RADIO_TECH_CHANGED,
2447 &tech, sizeof(tech));
2448 }
2449 }
2450 }
2451}
2452
2453static void
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002454setRadioState(RIL_RadioState newState)
2455{
Wink Saville4dcab4f2012-11-19 16:05:13 -08002456 RLOGD("setRadioState(%d)", newState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002457 RIL_RadioState oldState;
2458
2459 pthread_mutex_lock(&s_state_mutex);
2460
2461 oldState = sState;
2462
2463 if (s_closed > 0) {
2464 // If we're closed, the only reasonable state is
2465 // RADIO_STATE_UNAVAILABLE
2466 // This is here because things on the main thread
2467 // may attempt to change the radio state after the closed
2468 // event happened in another thread
2469 newState = RADIO_STATE_UNAVAILABLE;
2470 }
2471
2472 if (sState != newState || s_closed > 0) {
2473 sState = newState;
2474
2475 pthread_cond_broadcast (&s_state_cond);
2476 }
2477
2478 pthread_mutex_unlock(&s_state_mutex);
2479
2480
2481 /* do these outside of the mutex */
2482 if (sState != oldState) {
2483 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
2484 NULL, 0);
Alex Yakavenka81d14852013-12-04 13:54:37 -08002485 // Sim state can change as result of radio state change
2486 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED,
2487 NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002488
2489 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
2490 * from the AT reader thread
2491 * Currently, this doesn't happen, but if that changes then these
2492 * will need to be dispatched on the request thread
2493 */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002494 if (sState == RADIO_STATE_ON) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002495 onRadioPowerOn();
2496 }
2497 }
2498}
2499
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002500/** Returns RUIM_NOT_READY on error */
2501static SIM_Status
2502getRUIMStatus()
2503{
2504 ATResponse *p_response = NULL;
2505 int err;
2506 int ret;
2507 char *cpinLine;
2508 char *cpinResult;
2509
2510 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
2511 ret = SIM_NOT_READY;
2512 goto done;
2513 }
2514
2515 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2516
2517 if (err != 0) {
2518 ret = SIM_NOT_READY;
2519 goto done;
2520 }
2521
2522 switch (at_get_cme_error(p_response)) {
2523 case CME_SUCCESS:
2524 break;
2525
2526 case CME_SIM_NOT_INSERTED:
2527 ret = SIM_ABSENT;
2528 goto done;
2529
2530 default:
2531 ret = SIM_NOT_READY;
2532 goto done;
2533 }
2534
2535 /* CPIN? has succeeded, now look at the result */
2536
2537 cpinLine = p_response->p_intermediates->line;
2538 err = at_tok_start (&cpinLine);
2539
2540 if (err < 0) {
2541 ret = SIM_NOT_READY;
2542 goto done;
2543 }
2544
2545 err = at_tok_nextstr(&cpinLine, &cpinResult);
2546
2547 if (err < 0) {
2548 ret = SIM_NOT_READY;
2549 goto done;
2550 }
2551
2552 if (0 == strcmp (cpinResult, "SIM PIN")) {
2553 ret = SIM_PIN;
2554 goto done;
2555 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
2556 ret = SIM_PUK;
2557 goto done;
2558 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
2559 return SIM_NETWORK_PERSONALIZATION;
2560 } else if (0 != strcmp (cpinResult, "READY")) {
2561 /* we're treating unsupported lock types as "sim absent" */
2562 ret = SIM_ABSENT;
2563 goto done;
2564 }
2565
2566 at_response_free(p_response);
2567 p_response = NULL;
2568 cpinResult = NULL;
2569
2570 ret = SIM_READY;
2571
2572done:
2573 at_response_free(p_response);
2574 return ret;
2575}
2576
John Wang309ac292009-07-30 14:53:23 -07002577/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01002578static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002579getSIMStatus()
2580{
2581 ATResponse *p_response = NULL;
2582 int err;
2583 int ret;
2584 char *cpinLine;
2585 char *cpinResult;
2586
Wink Saville4dcab4f2012-11-19 16:05:13 -08002587 RLOGD("getSIMStatus(). sState: %d",sState);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002588 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
John Wang309ac292009-07-30 14:53:23 -07002589 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002590 goto done;
2591 }
2592
2593 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
2594
2595 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07002596 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002597 goto done;
2598 }
2599
2600 switch (at_get_cme_error(p_response)) {
2601 case CME_SUCCESS:
2602 break;
2603
2604 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07002605 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002606 goto done;
2607
2608 default:
John Wang309ac292009-07-30 14:53:23 -07002609 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002610 goto done;
2611 }
2612
2613 /* CPIN? has succeeded, now look at the result */
2614
2615 cpinLine = p_response->p_intermediates->line;
2616 err = at_tok_start (&cpinLine);
2617
2618 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002619 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002620 goto done;
2621 }
2622
2623 err = at_tok_nextstr(&cpinLine, &cpinResult);
2624
2625 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07002626 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002627 goto done;
2628 }
2629
2630 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002631 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002632 goto done;
2633 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07002634 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002635 goto done;
2636 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07002637 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002638 } else if (0 != strcmp (cpinResult, "READY")) {
2639 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07002640 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002641 goto done;
2642 }
2643
2644 at_response_free(p_response);
2645 p_response = NULL;
2646 cpinResult = NULL;
2647
John Wang309ac292009-07-30 14:53:23 -07002648 ret = SIM_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002649
2650done:
2651 at_response_free(p_response);
2652 return ret;
2653}
2654
2655
2656/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07002657 * Get the current card status.
2658 *
2659 * This must be freed using freeCardStatus.
2660 * @return: On success returns RIL_E_SUCCESS
2661 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002662static int getCardStatus(RIL_CardStatus_v6 **pp_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002663 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07002664 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07002665 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2666 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002667 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07002668 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2669 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002670 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07002671 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2672 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002673 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07002674 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2675 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002676 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07002677 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2678 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07002679 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07002680 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002681 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2682 // RUIM_ABSENT = 6
2683 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
2684 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2685 // RUIM_NOT_READY = 7
2686 { RIL_APPTYPE_RUIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
2687 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2688 // RUIM_READY = 8
2689 { RIL_APPTYPE_RUIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
2690 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
2691 // RUIM_PIN = 9
2692 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
2693 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
2694 // RUIM_PUK = 10
2695 { RIL_APPTYPE_RUIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
2696 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
2697 // RUIM_NETWORK_PERSONALIZATION = 11
2698 { RIL_APPTYPE_RUIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
2699 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
Wink Savillef6aa7c12009-04-30 14:20:52 -07002700 };
2701 RIL_CardState card_state;
2702 int num_apps;
2703
2704 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07002705 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002706 card_state = RIL_CARDSTATE_ABSENT;
2707 num_apps = 0;
2708 } else {
2709 card_state = RIL_CARDSTATE_PRESENT;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002710 num_apps = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002711 }
2712
2713 // Allocate and initialize base card status.
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002714 RIL_CardStatus_v6 *p_card_status = malloc(sizeof(RIL_CardStatus_v6));
Wink Savillef6aa7c12009-04-30 14:20:52 -07002715 p_card_status->card_state = card_state;
2716 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
2717 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
2718 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002719 p_card_status->ims_subscription_app_index = RIL_CARD_MAX_APPS;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002720 p_card_status->num_applications = num_apps;
2721
2722 // Initialize application status
2723 int i;
2724 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07002725 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002726 }
2727
2728 // Pickup the appropriate application status
2729 // that reflects sim_status for gsm.
2730 if (num_apps != 0) {
2731 // Only support one app, gsm
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002732 p_card_status->num_applications = 2;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002733 p_card_status->gsm_umts_subscription_app_index = 0;
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002734 p_card_status->cdma_subscription_app_index = 1;
Wink Savillef6aa7c12009-04-30 14:20:52 -07002735
2736 // Get the correct app status
2737 p_card_status->applications[0] = app_status_array[sim_status];
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002738 p_card_status->applications[1] = app_status_array[sim_status + RUIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07002739 }
2740
2741 *pp_card_status = p_card_status;
2742 return RIL_E_SUCCESS;
2743}
2744
2745/**
2746 * Free the card status returned by getCardStatus
2747 */
Wink Saville2c1fb3a2011-03-19 13:42:45 -07002748static void freeCardStatus(RIL_CardStatus_v6 *p_card_status) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07002749 free(p_card_status);
2750}
2751
2752/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002753 * SIM ready means any commands that access the SIM will work, including:
2754 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
2755 * (all SMS-related commands)
2756 */
2757
Mark Salyzynba58c202014-03-12 15:20:22 -07002758static void pollSIMState (void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002759{
2760 ATResponse *p_response;
2761 int ret;
2762
2763 if (sState != RADIO_STATE_SIM_NOT_READY) {
2764 // no longer valid to poll
2765 return;
2766 }
2767
2768 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07002769 case SIM_ABSENT:
2770 case SIM_PIN:
2771 case SIM_PUK:
2772 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002773 default:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002774 RLOGI("SIM ABSENT or LOCKED");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002775 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002776 return;
2777
John Wang309ac292009-07-30 14:53:23 -07002778 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002779 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
2780 return;
2781
John Wang309ac292009-07-30 14:53:23 -07002782 case SIM_READY:
Wink Saville4dcab4f2012-11-19 16:05:13 -08002783 RLOGI("SIM_READY");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002784 onSIMReady();
2785 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, NULL, 0);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002786 return;
2787 }
2788}
2789
2790/** returns 1 if on, 0 if off, and -1 on error */
2791static int isRadioOn()
2792{
2793 ATResponse *p_response = NULL;
2794 int err;
2795 char *line;
2796 char ret;
2797
2798 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
2799
2800 if (err < 0 || p_response->success == 0) {
2801 // assume radio is off
2802 goto error;
2803 }
2804
2805 line = p_response->p_intermediates->line;
2806
2807 err = at_tok_start(&line);
2808 if (err < 0) goto error;
2809
2810 err = at_tok_nextbool(&line, &ret);
2811 if (err < 0) goto error;
2812
2813 at_response_free(p_response);
2814
2815 return (int)ret;
2816
2817error:
2818
2819 at_response_free(p_response);
2820 return -1;
2821}
2822
2823/**
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002824 * Parse the response generated by a +CTEC AT command
2825 * The values read from the response are stored in current and preferred.
2826 * Both current and preferred may be null. The corresponding value is ignored in that case.
2827 *
2828 * @return: -1 if some error occurs (or if the modem doesn't understand the +CTEC command)
2829 * 1 if the response includes the current technology only
2830 * 0 if the response includes both current technology and preferred mode
2831 */
2832int parse_technology_response( const char *response, int *current, int32_t *preferred )
2833{
2834 int err;
2835 char *line, *p;
2836 int ct;
2837 int32_t pt = 0;
2838 char *str_pt;
2839
2840 line = p = strdup(response);
Wink Saville4dcab4f2012-11-19 16:05:13 -08002841 RLOGD("Response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002842 err = at_tok_start(&p);
2843 if (err || !at_tok_hasmore(&p)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002844 RLOGD("err: %d. p: %s", err, p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002845 free(line);
2846 return -1;
2847 }
2848
2849 err = at_tok_nextint(&p, &ct);
2850 if (err) {
2851 free(line);
2852 return -1;
2853 }
2854 if (current) *current = ct;
2855
Wink Saville4dcab4f2012-11-19 16:05:13 -08002856 RLOGD("line remaining after int: %s", p);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002857
2858 err = at_tok_nexthexint(&p, &pt);
2859 if (err) {
2860 free(line);
2861 return 1;
2862 }
2863 if (preferred) {
2864 *preferred = pt;
2865 }
2866 free(line);
2867
2868 return 0;
2869}
2870
Mark Salyzynba58c202014-03-12 15:20:22 -07002871int query_supported_techs( ModemInfo *mdm __unused, int *supported )
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002872{
2873 ATResponse *p_response;
2874 int err, val, techs = 0;
2875 char *tok;
2876 char *line;
2877
Wink Saville4dcab4f2012-11-19 16:05:13 -08002878 RLOGD("query_supported_techs");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002879 err = at_send_command_singleline("AT+CTEC=?", "+CTEC:", &p_response);
2880 if (err || !p_response->success)
2881 goto error;
2882 line = p_response->p_intermediates->line;
2883 err = at_tok_start(&line);
2884 if (err || !at_tok_hasmore(&line))
2885 goto error;
2886 while (!at_tok_nextint(&line, &val)) {
2887 techs |= ( 1 << val );
2888 }
2889 if (supported) *supported = techs;
2890 return 0;
2891error:
2892 at_response_free(p_response);
2893 return -1;
2894}
2895
2896/**
2897 * query_ctec. Send the +CTEC AT command to the modem to query the current
2898 * and preferred modes. It leaves values in the addresses pointed to by
2899 * current and preferred. If any of those pointers are NULL, the corresponding value
2900 * is ignored, but the return value will still reflect if retreiving and parsing of the
2901 * values suceeded.
2902 *
2903 * @mdm Currently unused
2904 * @current A pointer to store the current mode returned by the modem. May be null.
2905 * @preferred A pointer to store the preferred mode returned by the modem. May be null.
2906 * @return -1 on error (or failure to parse)
2907 * 1 if only the current mode was returned by modem (or failed to parse preferred)
2908 * 0 if both current and preferred were returned correctly
2909 */
Mark Salyzynba58c202014-03-12 15:20:22 -07002910int query_ctec(ModemInfo *mdm __unused, int *current, int32_t *preferred)
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002911{
2912 ATResponse *response = NULL;
2913 int err;
2914 int res;
2915
Colin Cross5cba4882014-02-05 18:55:42 -08002916 RLOGD("query_ctec. current: %p, preferred: %p", current, preferred);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002917 err = at_send_command_singleline("AT+CTEC?", "+CTEC:", &response);
2918 if (!err && response->success) {
2919 res = parse_technology_response(response->p_intermediates->line, current, preferred);
2920 at_response_free(response);
2921 return res;
2922 }
Colin Cross5cba4882014-02-05 18:55:42 -08002923 RLOGE("Error executing command: %d. response: %p. status: %d", err, response, response? response->success : -1);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002924 at_response_free(response);
2925 return -1;
2926}
2927
2928int is_multimode_modem(ModemInfo *mdm)
2929{
2930 ATResponse *response;
2931 int err;
2932 char *line;
2933 int tech;
2934 int32_t preferred;
2935
2936 if (query_ctec(mdm, &tech, &preferred) == 0) {
2937 mdm->currentTech = tech;
2938 mdm->preferredNetworkMode = preferred;
2939 if (query_supported_techs(mdm, &mdm->supportedTechs)) {
2940 return 0;
2941 }
2942 return 1;
2943 }
2944 return 0;
2945}
2946
2947/**
2948 * Find out if our modem is GSM, CDMA or both (Multimode)
2949 */
2950static void probeForModemMode(ModemInfo *info)
2951{
2952 ATResponse *response;
2953 int err;
2954 assert (info);
2955 // Currently, our only known multimode modem is qemu's android modem,
2956 // which implements the AT+CTEC command to query and set mode.
2957 // Try that first
2958
2959 if (is_multimode_modem(info)) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08002960 RLOGI("Found Multimode Modem. Supported techs mask: %8.8x. Current tech: %d",
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002961 info->supportedTechs, info->currentTech);
2962 return;
2963 }
2964
2965 /* Being here means that our modem is not multimode */
2966 info->isMultimode = 0;
2967
2968 /* CDMA Modems implement the AT+WNAM command */
2969 err = at_send_command_singleline("AT+WNAM","+WNAM:", &response);
2970 if (!err && response->success) {
2971 at_response_free(response);
2972 // TODO: find out if we really support EvDo
2973 info->supportedTechs = MDM_CDMA | MDM_EVDO;
2974 info->currentTech = MDM_CDMA;
Wink Saville4dcab4f2012-11-19 16:05:13 -08002975 RLOGI("Found CDMA Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002976 return;
2977 }
2978 if (!err) at_response_free(response);
2979 // TODO: find out if modem really supports WCDMA/LTE
2980 info->supportedTechs = MDM_GSM | MDM_WCDMA | MDM_LTE;
2981 info->currentTech = MDM_GSM;
Wink Saville4dcab4f2012-11-19 16:05:13 -08002982 RLOGI("Found GSM Modem");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002983}
2984
2985/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002986 * Initialize everything that can be configured while we're still in
2987 * AT+CFUN=0
2988 */
Mark Salyzynba58c202014-03-12 15:20:22 -07002989static void initializeCallback(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002990{
2991 ATResponse *p_response = NULL;
2992 int err;
2993
2994 setRadioState (RADIO_STATE_OFF);
2995
2996 at_handshake();
2997
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07002998 probeForModemMode(sMdmInfo);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002999 /* note: we don't check errors here. Everything important will
3000 be handled in onATTimeout and onATReaderClosed */
3001
3002 /* atchannel is tolerant of echo but it must */
3003 /* have verbose result codes */
3004 at_send_command("ATE0Q0V1", NULL);
3005
3006 /* No auto-answer */
3007 at_send_command("ATS0=0", NULL);
3008
3009 /* Extended errors */
3010 at_send_command("AT+CMEE=1", NULL);
3011
3012 /* Network registration events */
3013 err = at_send_command("AT+CREG=2", &p_response);
3014
3015 /* some handsets -- in tethered mode -- don't support CREG=2 */
3016 if (err < 0 || p_response->success == 0) {
3017 at_send_command("AT+CREG=1", NULL);
3018 }
3019
3020 at_response_free(p_response);
3021
3022 /* GPRS registration events */
3023 at_send_command("AT+CGREG=1", NULL);
3024
3025 /* Call Waiting notifications */
3026 at_send_command("AT+CCWA=1", NULL);
3027
3028 /* Alternating voice/data off */
3029 at_send_command("AT+CMOD=0", NULL);
3030
3031 /* Not muted */
3032 at_send_command("AT+CMUT=0", NULL);
3033
3034 /* +CSSU unsolicited supp service notifications */
3035 at_send_command("AT+CSSN=0,1", NULL);
3036
3037 /* no connected line identification */
3038 at_send_command("AT+COLP=0", NULL);
3039
3040 /* HEX character set */
3041 at_send_command("AT+CSCS=\"HEX\"", NULL);
3042
3043 /* USSD unsolicited */
3044 at_send_command("AT+CUSD=1", NULL);
3045
3046 /* Enable +CGEV GPRS event notifications, but don't buffer */
3047 at_send_command("AT+CGEREP=1,0", NULL);
3048
3049 /* SMS PDU mode */
3050 at_send_command("AT+CMGF=0", NULL);
3051
3052#ifdef USE_TI_COMMANDS
3053
3054 at_send_command("AT%CPI=3", NULL);
3055
3056 /* TI specific -- notifications when SMS is ready (currently ignored) */
3057 at_send_command("AT%CSTAT=1", NULL);
3058
3059#endif /* USE_TI_COMMANDS */
3060
3061
3062 /* assume radio is off on error */
3063 if (isRadioOn() > 0) {
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003064 setRadioState (RADIO_STATE_ON);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003065 }
3066}
3067
3068static void waitForClose()
3069{
3070 pthread_mutex_lock(&s_state_mutex);
3071
3072 while (s_closed == 0) {
3073 pthread_cond_wait(&s_state_cond, &s_state_mutex);
3074 }
3075
3076 pthread_mutex_unlock(&s_state_mutex);
3077}
3078
Sukanya Rajkhowaa18b9d12013-09-10 12:30:13 -07003079static void sendUnsolImsNetworkStateChanged()
3080{
3081#if 0 // to be used when unsol is changed to return data.
3082 int reply[2];
3083 reply[0] = s_ims_registered;
3084 reply[1] = s_ims_services;
3085 reply[1] = s_ims_format;
3086#endif
3087 RIL_onUnsolicitedResponse(RIL_UNSOL_RESPONSE_IMS_NETWORK_STATE_CHANGED,
3088 NULL, 0);
3089}
3090
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003091/**
3092 * Called by atchannel when an unsolicited line appears
3093 * This is called on atchannel's reader thread. AT commands may
3094 * not be issued here
3095 */
3096static void onUnsolicited (const char *s, const char *sms_pdu)
3097{
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003098 char *line = NULL, *p;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003099 int err;
3100
3101 /* Ignore unsolicited responses until we're initialized.
3102 * This is OK because the RIL library will poll for initial state
3103 */
3104 if (sState == RADIO_STATE_UNAVAILABLE) {
3105 return;
3106 }
3107
3108 if (strStartsWith(s, "%CTZV:")) {
3109 /* TI specific -- NITZ time */
3110 char *response;
3111
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003112 line = p = strdup(s);
3113 at_tok_start(&p);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003114
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003115 err = at_tok_nextstr(&p, &response);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003116
3117 if (err != 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003118 RLOGE("invalid NITZ line %s\n", s);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003119 } else {
3120 RIL_onUnsolicitedResponse (
3121 RIL_UNSOL_NITZ_TIME_RECEIVED,
3122 response, strlen(response));
3123 }
Ivan Krasin7c0165e2015-12-03 15:50:10 -08003124 free(line);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003125 } else if (strStartsWith(s,"+CRING:")
3126 || strStartsWith(s,"RING")
3127 || strStartsWith(s,"NO CARRIER")
3128 || strStartsWith(s,"+CCWA")
3129 ) {
3130 RIL_onUnsolicitedResponse (
3131 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
3132 NULL, 0);
3133#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07003134 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003135#endif /* WORKAROUND_FAKE_CGEV */
3136 } else if (strStartsWith(s,"+CREG:")
3137 || strStartsWith(s,"+CGREG:")
3138 ) {
3139 RIL_onUnsolicitedResponse (
Wink Saville2c1fb3a2011-03-19 13:42:45 -07003140 RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003141 NULL, 0);
3142#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07003143 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003144#endif /* WORKAROUND_FAKE_CGEV */
3145 } else if (strStartsWith(s, "+CMT:")) {
3146 RIL_onUnsolicitedResponse (
3147 RIL_UNSOL_RESPONSE_NEW_SMS,
3148 sms_pdu, strlen(sms_pdu));
3149 } else if (strStartsWith(s, "+CDS:")) {
3150 RIL_onUnsolicitedResponse (
3151 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
3152 sms_pdu, strlen(sms_pdu));
3153 } else if (strStartsWith(s, "+CGEV:")) {
3154 /* Really, we can ignore NW CLASS and ME CLASS events here,
3155 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07003156 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003157 */
3158 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07003159 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003160#ifdef WORKAROUND_FAKE_CGEV
3161 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07003162 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003163#endif /* WORKAROUND_FAKE_CGEV */
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003164 } else if (strStartsWith(s, "+CTEC: ")) {
3165 int tech, mask;
3166 switch (parse_technology_response(s, &tech, NULL))
3167 {
3168 case -1: // no argument could be parsed.
Wink Saville4dcab4f2012-11-19 16:05:13 -08003169 RLOGE("invalid CTEC line %s\n", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003170 break;
3171 case 1: // current mode correctly parsed
3172 case 0: // preferred mode correctly parsed
3173 mask = 1 << tech;
3174 if (mask != MDM_GSM && mask != MDM_CDMA &&
3175 mask != MDM_WCDMA && mask != MDM_LTE) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003176 RLOGE("Unknown technology %d\n", tech);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003177 } else {
3178 setRadioTechnology(sMdmInfo, tech);
3179 }
3180 break;
3181 }
3182 } else if (strStartsWith(s, "+CCSS: ")) {
3183 int source = 0;
3184 line = p = strdup(s);
3185 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003186 RLOGE("+CCSS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003187 return;
3188 }
3189 if (at_tok_start(&p) < 0) {
3190 free(line);
3191 return;
3192 }
3193 if (at_tok_nextint(&p, &source) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003194 RLOGE("invalid +CCSS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003195 free(line);
3196 return;
3197 }
3198 SSOURCE(sMdmInfo) = source;
3199 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED,
3200 &source, sizeof(source));
3201 } else if (strStartsWith(s, "+WSOS: ")) {
3202 char state = 0;
3203 int unsol;
3204 line = p = strdup(s);
3205 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003206 RLOGE("+WSOS: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003207 return;
3208 }
3209 if (at_tok_start(&p) < 0) {
3210 free(line);
3211 return;
3212 }
3213 if (at_tok_nextbool(&p, &state) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003214 RLOGE("invalid +WSOS response: %s", line);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003215 free(line);
3216 return;
3217 }
3218 free(line);
3219
3220 unsol = state ?
3221 RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE : RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE;
3222
3223 RIL_onUnsolicitedResponse(unsol, NULL, 0);
3224
3225 } else if (strStartsWith(s, "+WPRL: ")) {
3226 int version = -1;
3227 line = p = strdup(s);
3228 if (!line) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003229 RLOGE("+WPRL: Unable to allocate memory");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003230 return;
3231 }
3232 if (at_tok_start(&p) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003233 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003234 free(line);
3235 return;
3236 }
3237 if (at_tok_nextint(&p, &version) < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003238 RLOGE("invalid +WPRL response: %s", s);
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003239 free(line);
3240 return;
3241 }
3242 free(line);
3243 RIL_onUnsolicitedResponse(RIL_UNSOL_CDMA_PRL_CHANGED, &version, sizeof(version));
3244 } else if (strStartsWith(s, "+CFUN: 0")) {
3245 setRadioState(RADIO_STATE_OFF);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003246 }
3247}
3248
3249/* Called on command or reader thread */
3250static void onATReaderClosed()
3251{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003252 RLOGI("AT channel closed\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003253 at_close();
3254 s_closed = 1;
3255
3256 setRadioState (RADIO_STATE_UNAVAILABLE);
3257}
3258
3259/* Called on command thread */
3260static void onATTimeout()
3261{
Wink Saville4dcab4f2012-11-19 16:05:13 -08003262 RLOGI("AT channel timeout; closing\n");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003263 at_close();
3264
3265 s_closed = 1;
3266
3267 /* FIXME cause a radio reset here */
3268
3269 setRadioState (RADIO_STATE_UNAVAILABLE);
3270}
3271
Etan Cohend3652192014-06-20 08:28:44 -07003272/* Called to pass hardware configuration information to telephony
3273 * framework.
3274 */
3275static void setHardwareConfiguration(int num, RIL_HardwareConfig *cfg)
3276{
3277 RIL_onUnsolicitedResponse(RIL_UNSOL_HARDWARE_CONFIG_CHANGED, cfg, num*sizeof(*cfg));
3278}
3279
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003280static void usage(char *s)
3281{
3282#ifdef RIL_SHLIB
3283 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
3284#else
3285 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
3286 exit(-1);
3287#endif
3288}
3289
3290static void *
Mark Salyzynba58c202014-03-12 15:20:22 -07003291mainLoop(void *param __unused)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003292{
3293 int fd;
3294 int ret;
3295
3296 AT_DUMP("== ", "entering mainLoop()", -1 );
3297 at_set_on_reader_closed(onATReaderClosed);
3298 at_set_on_timeout(onATTimeout);
3299
3300 for (;;) {
3301 fd = -1;
3302 while (fd < 0) {
3303 if (s_port > 0) {
3304 fd = socket_loopback_client(s_port, SOCK_STREAM);
3305 } else if (s_device_socket) {
The Android Open Source Projecte6e6fb22009-03-18 17:39:47 -07003306 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -07003307 /* Before trying to connect to /dev/socket/qemud (which is
3308 * now another "legacy" way of communicating with the
3309 * emulator), we will try to connecto to gsm service via
3310 * qemu pipe. */
3311 fd = qemu_pipe_open("qemud:gsm");
3312 if (fd < 0) {
3313 /* Qemu-specific control socket */
3314 fd = socket_local_client( "qemud",
3315 ANDROID_SOCKET_NAMESPACE_RESERVED,
3316 SOCK_STREAM );
3317 if (fd >= 0 ) {
3318 char answer[2];
The Android Open Source Projecte6e6fb22009-03-18 17:39:47 -07003319
Vladimir Chtchetkine385a7392011-08-04 14:03:07 -07003320 if ( write(fd, "gsm", 3) != 3 ||
3321 read(fd, answer, 2) != 2 ||
3322 memcmp(answer, "OK", 2) != 0)
3323 {
3324 close(fd);
3325 fd = -1;
3326 }
3327 }
3328 }
The Android Open Source Projecte6e6fb22009-03-18 17:39:47 -07003329 }
3330 else
3331 fd = socket_local_client( s_device_path,
3332 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
3333 SOCK_STREAM );
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003334 } else if (s_device_path != NULL) {
3335 fd = open (s_device_path, O_RDWR);
3336 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
3337 /* disable echo on serial ports */
3338 struct termios ios;
3339 tcgetattr( fd, &ios );
3340 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
3341 tcsetattr( fd, TCSANOW, &ios );
3342 }
3343 }
3344
3345 if (fd < 0) {
3346 perror ("opening AT interface. retrying...");
3347 sleep(10);
3348 /* never returns */
3349 }
3350 }
3351
3352 s_closed = 0;
3353 ret = at_open(fd, onUnsolicited);
3354
3355 if (ret < 0) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003356 RLOGE ("AT error %d on at_open\n", ret);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003357 return 0;
3358 }
3359
3360 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
3361
3362 // Give initializeCallback a chance to dispatched, since
3363 // we don't presently have a cancellation mechanism
3364 sleep(1);
3365
3366 waitForClose();
Wink Saville4dcab4f2012-11-19 16:05:13 -08003367 RLOGI("Re-opening after close");
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003368 }
3369}
3370
3371#ifdef RIL_SHLIB
3372
3373pthread_t s_tid_mainloop;
3374
3375const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
3376{
3377 int ret;
3378 int fd = -1;
3379 int opt;
3380 pthread_attr_t attr;
3381
3382 s_rilenv = env;
3383
Sandeep Gutta11f27942014-07-10 05:00:25 +05303384 while ( -1 != (opt = getopt(argc, argv, "p:d:s:c:"))) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003385 switch (opt) {
3386 case 'p':
3387 s_port = atoi(optarg);
3388 if (s_port == 0) {
3389 usage(argv[0]);
3390 return NULL;
3391 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003392 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003393 break;
3394
3395 case 'd':
3396 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003397 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003398 break;
3399
3400 case 's':
3401 s_device_path = optarg;
3402 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003403 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003404 break;
3405
Sandeep Gutta11f27942014-07-10 05:00:25 +05303406 case 'c':
3407 RLOGI("Client id received %s\n", optarg);
3408 break;
3409
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003410 default:
3411 usage(argv[0]);
3412 return NULL;
3413 }
3414 }
3415
3416 if (s_port < 0 && s_device_path == NULL) {
3417 usage(argv[0]);
3418 return NULL;
3419 }
3420
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003421 sMdmInfo = calloc(1, sizeof(ModemInfo));
3422 if (!sMdmInfo) {
Wink Saville4dcab4f2012-11-19 16:05:13 -08003423 RLOGE("Unable to alloc memory for ModemInfo");
Jaime A Lopez-Sollanoe9645042012-08-29 07:27:27 -07003424 return NULL;
3425 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003426 pthread_attr_init (&attr);
3427 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
3428 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
3429
3430 return &s_callbacks;
3431}
3432#else /* RIL_SHLIB */
3433int main (int argc, char **argv)
3434{
3435 int ret;
3436 int fd = -1;
3437 int opt;
3438
3439 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
3440 switch (opt) {
3441 case 'p':
3442 s_port = atoi(optarg);
3443 if (s_port == 0) {
3444 usage(argv[0]);
3445 }
Wink Saville4dcab4f2012-11-19 16:05:13 -08003446 RLOGI("Opening loopback port %d\n", s_port);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003447 break;
3448
3449 case 'd':
3450 s_device_path = optarg;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003451 RLOGI("Opening tty device %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003452 break;
3453
3454 case 's':
3455 s_device_path = optarg;
3456 s_device_socket = 1;
Wink Saville4dcab4f2012-11-19 16:05:13 -08003457 RLOGI("Opening socket %s\n", s_device_path);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08003458 break;
3459
3460 default:
3461 usage(argv[0]);
3462 }
3463 }
3464
3465 if (s_port < 0 && s_device_path == NULL) {
3466 usage(argv[0]);
3467 }
3468
3469 RIL_register(&s_callbacks);
3470
3471 mainLoop(NULL);
3472
3473 return 0;
3474}
3475
3476#endif /* RIL_SHLIB */