blob: 104e38cfd6e1eb405b0073e63cf29b93a8739c73 [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
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080018#include <stdio.h>
19#include <assert.h>
20#include <string.h>
21#include <errno.h>
22#include <unistd.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <pthread.h>
27#include <alloca.h>
28#include "atchannel.h"
29#include "at_tok.h"
30#include "misc.h"
31#include <getopt.h>
32#include <sys/socket.h>
33#include <cutils/sockets.h>
34#include <termios.h>
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +010035#include <sys/system_properties.h>
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080036
Wink Saville9a9fbd22011-02-15 17:13:10 -080037#include "ril.h"
38
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080039#define LOG_TAG "RIL"
40#include <utils/Log.h>
41
42#define MAX_AT_RESPONSE 0x1000
43
Wink Savillef4c4d362009-04-02 01:37:03 -070044/* pathname returned from RIL_REQUEST_SETUP_DATA_CALL / RIL_REQUEST_SETUP_DEFAULT_PDP */
Robert Greenwaltf838ede2010-07-15 18:54:53 -070045#define PPP_TTY_PATH "eth0"
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080046
47#ifdef USE_TI_COMMANDS
48
49// Enable a workaround
50// 1) Make incoming call, do not answer
51// 2) Hangup remote end
52// Expected: call should disappear from CLCC line
53// Actual: Call shows as "ACTIVE" before disappearing
54#define WORKAROUND_ERRONEOUS_ANSWER 1
55
56// Some varients of the TI stack do not support the +CGEV unsolicited
57// response. However, they seem to send an unsolicited +CME ERROR: 150
58#define WORKAROUND_FAKE_CGEV 1
59#endif
60
John Wang309ac292009-07-30 14:53:23 -070061typedef enum {
62 SIM_ABSENT = 0,
63 SIM_NOT_READY = 1,
64 SIM_READY = 2, /* SIM_READY means the radio state is RADIO_STATE_SIM_READY */
65 SIM_PIN = 3,
66 SIM_PUK = 4,
67 SIM_NETWORK_PERSONALIZATION = 5
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +010068} SIM_Status;
John Wang309ac292009-07-30 14:53:23 -070069
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080070static void onRequest (int request, void *data, size_t datalen, RIL_Token t);
71static RIL_RadioState currentState();
72static int onSupports (int requestCode);
73static void onCancel (RIL_Token t);
74static const char *getVersion();
75static int isRadioOn();
John Wang309ac292009-07-30 14:53:23 -070076static SIM_Status getSIMStatus();
Wink Savillef6aa7c12009-04-30 14:20:52 -070077static int getCardStatus(RIL_CardStatus **pp_card_status);
78static void freeCardStatus(RIL_CardStatus *p_card_status);
Wink Savillef4c4d362009-04-02 01:37:03 -070079static void onDataCallListChanged(void *param);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -080080
81extern const char * requestToString(int request);
82
83/*** Static Variables ***/
84static const RIL_RadioFunctions s_callbacks = {
85 RIL_VERSION,
86 onRequest,
87 currentState,
88 onSupports,
89 onCancel,
90 getVersion
91};
92
93#ifdef RIL_SHLIB
94static const struct RIL_Env *s_rilenv;
95
96#define RIL_onRequestComplete(t, e, response, responselen) s_rilenv->OnRequestComplete(t,e, response, responselen)
97#define RIL_onUnsolicitedResponse(a,b,c) s_rilenv->OnUnsolicitedResponse(a,b,c)
98#define RIL_requestTimedCallback(a,b,c) s_rilenv->RequestTimedCallback(a,b,c)
99#endif
100
101static RIL_RadioState sState = RADIO_STATE_UNAVAILABLE;
102
103static pthread_mutex_t s_state_mutex = PTHREAD_MUTEX_INITIALIZER;
104static pthread_cond_t s_state_cond = PTHREAD_COND_INITIALIZER;
105
106static int s_port = -1;
107static const char * s_device_path = NULL;
108static int s_device_socket = 0;
109
110/* trigger change to this with s_state_cond */
111static int s_closed = 0;
112
113static int sFD; /* file desc of AT channel */
114static char sATBuffer[MAX_AT_RESPONSE+1];
115static char *sATBufferCur = NULL;
116
117static const struct timeval TIMEVAL_SIMPOLL = {1,0};
118static const struct timeval TIMEVAL_CALLSTATEPOLL = {0,500000};
119static const struct timeval TIMEVAL_0 = {0,0};
120
121#ifdef WORKAROUND_ERRONEOUS_ANSWER
122// Max number of times we'll try to repoll when we think
123// we have a AT+CLCC race condition
124#define REPOLL_CALLS_COUNT_MAX 4
125
126// Line index that was incoming or waiting at last poll, or -1 for none
127static int s_incomingOrWaitingLine = -1;
128// Number of times we've asked for a repoll of AT+CLCC
129static int s_repollCallsCount = 0;
130// Should we expect a call to be answered in the next CLCC?
131static int s_expectAnswer = 0;
132#endif /* WORKAROUND_ERRONEOUS_ANSWER */
133
134static void pollSIMState (void *param);
135static void setRadioState(RIL_RadioState newState);
136
137static int clccStateToRILState(int state, RIL_CallState *p_state)
138
139{
140 switch(state) {
141 case 0: *p_state = RIL_CALL_ACTIVE; return 0;
142 case 1: *p_state = RIL_CALL_HOLDING; return 0;
143 case 2: *p_state = RIL_CALL_DIALING; return 0;
144 case 3: *p_state = RIL_CALL_ALERTING; return 0;
145 case 4: *p_state = RIL_CALL_INCOMING; return 0;
146 case 5: *p_state = RIL_CALL_WAITING; return 0;
147 default: return -1;
148 }
149}
150
151/**
152 * Note: directly modified line and has *p_call point directly into
153 * modified line
154 */
Wink Saville3d54e742009-05-18 18:00:44 -0700155static int callFromCLCCLine(char *line, RIL_Call *p_call)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800156{
157 //+CLCC: 1,0,2,0,0,\"+18005551212\",145
158 // index,isMT,state,mode,isMpty(,number,TOA)?
159
160 int err;
161 int state;
162 int mode;
163
164 err = at_tok_start(&line);
165 if (err < 0) goto error;
166
167 err = at_tok_nextint(&line, &(p_call->index));
168 if (err < 0) goto error;
169
170 err = at_tok_nextbool(&line, &(p_call->isMT));
171 if (err < 0) goto error;
172
173 err = at_tok_nextint(&line, &state);
174 if (err < 0) goto error;
175
176 err = clccStateToRILState(state, &(p_call->state));
177 if (err < 0) goto error;
178
179 err = at_tok_nextint(&line, &mode);
180 if (err < 0) goto error;
181
182 p_call->isVoice = (mode == 0);
183
184 err = at_tok_nextbool(&line, &(p_call->isMpty));
185 if (err < 0) goto error;
186
187 if (at_tok_hasmore(&line)) {
188 err = at_tok_nextstr(&line, &(p_call->number));
189
190 /* tolerate null here */
191 if (err < 0) return 0;
192
193 // Some lame implementations return strings
194 // like "NOT AVAILABLE" in the CLCC line
195 if (p_call->number != NULL
196 && 0 == strspn(p_call->number, "+0123456789")
197 ) {
198 p_call->number = NULL;
199 }
200
201 err = at_tok_nextint(&line, &p_call->toa);
202 if (err < 0) goto error;
203 }
204
Wink Saville74fa3882009-12-22 15:35:41 -0800205 p_call->uusInfo = NULL;
206
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800207 return 0;
208
209error:
210 LOGE("invalid CLCC line\n");
211 return -1;
212}
213
214
215/** do post-AT+CFUN=1 initialization */
216static void onRadioPowerOn()
217{
218#ifdef USE_TI_COMMANDS
219 /* Must be after CFUN=1 */
220 /* TI specific -- notifications for CPHS things such */
221 /* as CPHS message waiting indicator */
222
223 at_send_command("AT%CPHS=1", NULL);
224
225 /* TI specific -- enable NITZ unsol notifs */
226 at_send_command("AT%CTZV=1", NULL);
227#endif
228
229 pollSIMState(NULL);
230}
231
232/** do post- SIM ready initialization */
233static void onSIMReady()
234{
235 at_send_command_singleline("AT+CSMS=1", "+CSMS:", NULL);
236 /*
237 * Always send SMS messages directly to the TE
238 *
239 * mode = 1 // discard when link is reserved (link should never be
240 * reserved)
241 * mt = 2 // most messages routed to TE
242 * bm = 2 // new cell BM's routed to TE
243 * ds = 1 // Status reports routed to TE
244 * bfr = 1 // flush buffer
245 */
246 at_send_command("AT+CNMI=1,2,2,1,1", NULL);
247}
248
249static void requestRadioPower(void *data, size_t datalen, RIL_Token t)
250{
251 int onOff;
252
253 int err;
254 ATResponse *p_response = NULL;
255
256 assert (datalen >= sizeof(int *));
257 onOff = ((int *)data)[0];
258
259 if (onOff == 0 && sState != RADIO_STATE_OFF) {
260 err = at_send_command("AT+CFUN=0", &p_response);
261 if (err < 0 || p_response->success == 0) goto error;
262 setRadioState(RADIO_STATE_OFF);
263 } else if (onOff > 0 && sState == RADIO_STATE_OFF) {
264 err = at_send_command("AT+CFUN=1", &p_response);
265 if (err < 0|| p_response->success == 0) {
266 // Some stacks return an error when there is no SIM,
267 // but they really turn the RF portion on
268 // So, if we get an error, let's check to see if it
269 // turned on anyway
270
271 if (isRadioOn() != 1) {
272 goto error;
273 }
274 }
275 setRadioState(RADIO_STATE_SIM_NOT_READY);
276 }
277
278 at_response_free(p_response);
279 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
280 return;
281error:
282 at_response_free(p_response);
283 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
284}
285
Wink Savillef4c4d362009-04-02 01:37:03 -0700286static void requestOrSendDataCallList(RIL_Token *t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800287
Wink Savillef4c4d362009-04-02 01:37:03 -0700288static void onDataCallListChanged(void *param)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800289{
Wink Savillef4c4d362009-04-02 01:37:03 -0700290 requestOrSendDataCallList(NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800291}
292
Wink Savillef4c4d362009-04-02 01:37:03 -0700293static void requestDataCallList(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800294{
Wink Savillef4c4d362009-04-02 01:37:03 -0700295 requestOrSendDataCallList(&t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800296}
297
Wink Savillef4c4d362009-04-02 01:37:03 -0700298static void requestOrSendDataCallList(RIL_Token *t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800299{
300 ATResponse *p_response;
301 ATLine *p_cur;
302 int err;
303 int n = 0;
304 char *out;
305
306 err = at_send_command_multiline ("AT+CGACT?", "+CGACT:", &p_response);
307 if (err != 0 || p_response->success == 0) {
308 if (t != NULL)
309 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
310 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700311 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800312 NULL, 0);
313 return;
314 }
315
316 for (p_cur = p_response->p_intermediates; p_cur != NULL;
317 p_cur = p_cur->p_next)
318 n++;
319
Wink Saville43808972011-01-13 17:39:51 -0800320 RIL_Data_Call_Response_v5 *responses =
321 alloca(n * sizeof(RIL_Data_Call_Response_v5));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800322
323 int i;
324 for (i = 0; i < n; i++) {
Wink Saville43808972011-01-13 17:39:51 -0800325 responses[i].status = -1;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800326 responses[i].cid = -1;
327 responses[i].active = -1;
328 responses[i].type = "";
Wink Saville43808972011-01-13 17:39:51 -0800329 responses[i].ifname = "";
330 responses[i].addresses = "";
331 responses[i].dnses = "";
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800332 }
333
Wink Saville43808972011-01-13 17:39:51 -0800334 RIL_Data_Call_Response_v5 *response = responses;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800335 for (p_cur = p_response->p_intermediates; p_cur != NULL;
336 p_cur = p_cur->p_next) {
337 char *line = p_cur->line;
338
339 err = at_tok_start(&line);
340 if (err < 0)
341 goto error;
342
343 err = at_tok_nextint(&line, &response->cid);
344 if (err < 0)
345 goto error;
346
347 err = at_tok_nextint(&line, &response->active);
348 if (err < 0)
349 goto error;
350
351 response++;
352 }
353
354 at_response_free(p_response);
355
356 err = at_send_command_multiline ("AT+CGDCONT?", "+CGDCONT:", &p_response);
357 if (err != 0 || p_response->success == 0) {
358 if (t != NULL)
359 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
360 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700361 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800362 NULL, 0);
363 return;
364 }
365
366 for (p_cur = p_response->p_intermediates; p_cur != NULL;
367 p_cur = p_cur->p_next) {
368 char *line = p_cur->line;
369 int cid;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800370
371 err = at_tok_start(&line);
372 if (err < 0)
373 goto error;
374
375 err = at_tok_nextint(&line, &cid);
376 if (err < 0)
377 goto error;
378
379 for (i = 0; i < n; i++) {
380 if (responses[i].cid == cid)
381 break;
382 }
383
384 if (i >= n) {
385 /* details for a context we didn't hear about in the last request */
386 continue;
387 }
388
Wink Saville43808972011-01-13 17:39:51 -0800389 // Assume no error
390 responses[i].status = 0;
391
392 // type
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800393 err = at_tok_nextstr(&line, &out);
394 if (err < 0)
395 goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800396 responses[i].type = alloca(strlen(out) + 1);
397 strcpy(responses[i].type, out);
398
Wink Saville43808972011-01-13 17:39:51 -0800399 // APN ignored for v5
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800400 err = at_tok_nextstr(&line, &out);
401 if (err < 0)
402 goto error;
403
Wink Saville43808972011-01-13 17:39:51 -0800404 responses[i].ifname = alloca(strlen(PPP_TTY_PATH) + 1);
405 strcpy(responses[i].ifname, PPP_TTY_PATH);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800406
407 err = at_tok_nextstr(&line, &out);
408 if (err < 0)
409 goto error;
410
Wink Saville43808972011-01-13 17:39:51 -0800411 responses[i].addresses = alloca(strlen(out) + 1);
412 strcpy(responses[i].addresses, out);
413
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +0100414 {
415 char propValue[PROP_VALUE_MAX];
416
417 if (__system_property_get("ro.kernel.qemu", propValue) != 0) {
418 /* We are in the emulator - the dns servers are listed
419 * by the following system properties, setup in
420 * /system/etc/init.goldfish.sh:
421 * - net.eth0.dns1
422 * - net.eth0.dns2
423 * - net.eth0.dns3
424 * - net.eth0.dns4
425 */
426 const int dnslist_sz = 128;
427 char* dnslist = alloca(dnslist_sz);
428 const char* separator = "";
429 int nn;
430
431 dnslist[0] = 0;
432 for (nn = 1; nn <= 4; nn++) {
433 /* Probe net.eth0.dns<n> */
434 char propName[PROP_NAME_MAX];
435 snprintf(propName, sizeof propName, "net.eth0.dns%d", nn);
436
437 /* Ignore if undefined */
438 if (__system_property_get(propName, propValue) == 0) {
439 continue;
440 }
441
442 /* Append the DNS IP address */
443 strlcat(dnslist, separator, dnslist_sz);
444 strlcat(dnslist, propValue, dnslist_sz);
445 separator = " ";
446 }
447 responses[i].dnses = dnslist;
448 }
449 else {
450 /* I don't know where we are, so use the public Google DNS
451 * servers by default.
452 */
453 responses[i].dnses = "8.8.8.8 8.8.4.4";
454 }
455 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800456 }
457
458 at_response_free(p_response);
459
460 if (t != NULL)
461 RIL_onRequestComplete(*t, RIL_E_SUCCESS, responses,
Wink Saville43808972011-01-13 17:39:51 -0800462 n * sizeof(RIL_Data_Call_Response_v5));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800463 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700464 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800465 responses,
Wink Saville43808972011-01-13 17:39:51 -0800466 n * sizeof(RIL_Data_Call_Response_v5));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800467
468 return;
469
470error:
471 if (t != NULL)
472 RIL_onRequestComplete(*t, RIL_E_GENERIC_FAILURE, NULL, 0);
473 else
Wink Savillef4c4d362009-04-02 01:37:03 -0700474 RIL_onUnsolicitedResponse(RIL_UNSOL_DATA_CALL_LIST_CHANGED,
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800475 NULL, 0);
476
477 at_response_free(p_response);
478}
479
480static void requestQueryNetworkSelectionMode(
481 void *data, size_t datalen, RIL_Token t)
482{
483 int err;
484 ATResponse *p_response = NULL;
485 int response = 0;
486 char *line;
487
488 err = at_send_command_singleline("AT+COPS?", "+COPS:", &p_response);
489
490 if (err < 0 || p_response->success == 0) {
491 goto error;
492 }
493
494 line = p_response->p_intermediates->line;
495
496 err = at_tok_start(&line);
497
498 if (err < 0) {
499 goto error;
500 }
501
502 err = at_tok_nextint(&line, &response);
503
504 if (err < 0) {
505 goto error;
506 }
507
508 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(int));
509 at_response_free(p_response);
510 return;
511error:
512 at_response_free(p_response);
513 LOGE("requestQueryNetworkSelectionMode must never return error when radio is on");
514 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
515}
516
517static void sendCallStateChanged(void *param)
518{
519 RIL_onUnsolicitedResponse (
520 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
521 NULL, 0);
522}
523
524static void requestGetCurrentCalls(void *data, size_t datalen, RIL_Token t)
525{
526 int err;
527 ATResponse *p_response;
528 ATLine *p_cur;
529 int countCalls;
530 int countValidCalls;
Wink Saville3d54e742009-05-18 18:00:44 -0700531 RIL_Call *p_calls;
532 RIL_Call **pp_calls;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800533 int i;
534 int needRepoll = 0;
535
536#ifdef WORKAROUND_ERRONEOUS_ANSWER
537 int prevIncomingOrWaitingLine;
538
539 prevIncomingOrWaitingLine = s_incomingOrWaitingLine;
540 s_incomingOrWaitingLine = -1;
541#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
542
543 err = at_send_command_multiline ("AT+CLCC", "+CLCC:", &p_response);
544
545 if (err != 0 || p_response->success == 0) {
546 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
547 return;
548 }
549
550 /* count the calls */
551 for (countCalls = 0, p_cur = p_response->p_intermediates
552 ; p_cur != NULL
553 ; p_cur = p_cur->p_next
554 ) {
555 countCalls++;
556 }
557
558 /* yes, there's an array of pointers and then an array of structures */
559
Wink Saville3d54e742009-05-18 18:00:44 -0700560 pp_calls = (RIL_Call **)alloca(countCalls * sizeof(RIL_Call *));
561 p_calls = (RIL_Call *)alloca(countCalls * sizeof(RIL_Call));
562 memset (p_calls, 0, countCalls * sizeof(RIL_Call));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800563
564 /* init the pointer array */
565 for(i = 0; i < countCalls ; i++) {
566 pp_calls[i] = &(p_calls[i]);
567 }
568
569 for (countValidCalls = 0, p_cur = p_response->p_intermediates
570 ; p_cur != NULL
571 ; p_cur = p_cur->p_next
572 ) {
573 err = callFromCLCCLine(p_cur->line, p_calls + countValidCalls);
574
575 if (err != 0) {
576 continue;
577 }
578
579#ifdef WORKAROUND_ERRONEOUS_ANSWER
580 if (p_calls[countValidCalls].state == RIL_CALL_INCOMING
581 || p_calls[countValidCalls].state == RIL_CALL_WAITING
582 ) {
583 s_incomingOrWaitingLine = p_calls[countValidCalls].index;
584 }
585#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
586
587 if (p_calls[countValidCalls].state != RIL_CALL_ACTIVE
588 && p_calls[countValidCalls].state != RIL_CALL_HOLDING
589 ) {
590 needRepoll = 1;
591 }
592
593 countValidCalls++;
594 }
595
596#ifdef WORKAROUND_ERRONEOUS_ANSWER
597 // Basically:
598 // A call was incoming or waiting
599 // Now it's marked as active
600 // But we never answered it
601 //
602 // This is probably a bug, and the call will probably
603 // disappear from the call list in the next poll
604 if (prevIncomingOrWaitingLine >= 0
605 && s_incomingOrWaitingLine < 0
606 && s_expectAnswer == 0
607 ) {
608 for (i = 0; i < countValidCalls ; i++) {
609
610 if (p_calls[i].index == prevIncomingOrWaitingLine
611 && p_calls[i].state == RIL_CALL_ACTIVE
612 && s_repollCallsCount < REPOLL_CALLS_COUNT_MAX
613 ) {
614 LOGI(
615 "Hit WORKAROUND_ERRONOUS_ANSWER case."
616 " Repoll count: %d\n", s_repollCallsCount);
617 s_repollCallsCount++;
618 goto error;
619 }
620 }
621 }
622
623 s_expectAnswer = 0;
624 s_repollCallsCount = 0;
625#endif /*WORKAROUND_ERRONEOUS_ANSWER*/
626
Wink Saville3d54e742009-05-18 18:00:44 -0700627 RIL_onRequestComplete(t, RIL_E_SUCCESS, pp_calls,
628 countValidCalls * sizeof (RIL_Call *));
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800629
630 at_response_free(p_response);
631
632#ifdef POLL_CALL_STATE
633 if (countValidCalls) { // We don't seem to get a "NO CARRIER" message from
634 // smd, so we're forced to poll until the call ends.
635#else
636 if (needRepoll) {
637#endif
638 RIL_requestTimedCallback (sendCallStateChanged, NULL, &TIMEVAL_CALLSTATEPOLL);
639 }
640
641 return;
642error:
643 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
644 at_response_free(p_response);
645}
646
647static void requestDial(void *data, size_t datalen, RIL_Token t)
648{
649 RIL_Dial *p_dial;
650 char *cmd;
651 const char *clir;
652 int ret;
653
654 p_dial = (RIL_Dial *)data;
655
656 switch (p_dial->clir) {
657 case 1: clir = "I"; break; /*invocation*/
658 case 2: clir = "i"; break; /*suppression*/
659 default:
660 case 0: clir = ""; break; /*subscription default*/
661 }
662
663 asprintf(&cmd, "ATD%s%s;", p_dial->address, clir);
664
665 ret = at_send_command(cmd, NULL);
666
667 free(cmd);
668
669 /* success or failure is ignored by the upper layer here.
670 it will call GET_CURRENT_CALLS and determine success that way */
671 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
672}
673
674static void requestWriteSmsToSim(void *data, size_t datalen, RIL_Token t)
675{
676 RIL_SMS_WriteArgs *p_args;
677 char *cmd;
678 int length;
679 int err;
680 ATResponse *p_response = NULL;
681
682 p_args = (RIL_SMS_WriteArgs *)data;
683
684 length = strlen(p_args->pdu)/2;
685 asprintf(&cmd, "AT+CMGW=%d,%d", length, p_args->status);
686
687 err = at_send_command_sms(cmd, p_args->pdu, "+CMGW:", &p_response);
688
689 if (err != 0 || p_response->success == 0) goto error;
690
691 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
692 at_response_free(p_response);
693
694 return;
695error:
696 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
697 at_response_free(p_response);
698}
699
700static void requestHangup(void *data, size_t datalen, RIL_Token t)
701{
702 int *p_line;
703
704 int ret;
705 char *cmd;
706
707 p_line = (int *)data;
708
709 // 3GPP 22.030 6.5.5
710 // "Releases a specific active call X"
711 asprintf(&cmd, "AT+CHLD=1%d", p_line[0]);
712
713 ret = at_send_command(cmd, NULL);
714
715 free(cmd);
716
717 /* success or failure is ignored by the upper layer here.
718 it will call GET_CURRENT_CALLS and determine success that way */
719 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
720}
721
722static void requestSignalStrength(void *data, size_t datalen, RIL_Token t)
723{
724 ATResponse *p_response = NULL;
725 int err;
726 int response[2];
727 char *line;
728
729 err = at_send_command_singleline("AT+CSQ", "+CSQ:", &p_response);
730
731 if (err < 0 || p_response->success == 0) {
732 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
733 goto error;
734 }
735
736 line = p_response->p_intermediates->line;
737
738 err = at_tok_start(&line);
739 if (err < 0) goto error;
740
741 err = at_tok_nextint(&line, &(response[0]));
742 if (err < 0) goto error;
743
744 err = at_tok_nextint(&line, &(response[1]));
745 if (err < 0) goto error;
746
747 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
748
749 at_response_free(p_response);
750 return;
751
752error:
753 LOGE("requestSignalStrength must never return an error when radio is on");
754 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
755 at_response_free(p_response);
756}
757
758static void requestRegistrationState(int request, void *data,
759 size_t datalen, RIL_Token t)
760{
761 int err;
762 int response[4];
763 char * responseStr[4];
764 ATResponse *p_response = NULL;
765 const char *cmd;
766 const char *prefix;
767 char *line, *p;
768 int commas;
769 int skip;
770 int count = 3;
771
772
773 if (request == RIL_REQUEST_REGISTRATION_STATE) {
774 cmd = "AT+CREG?";
775 prefix = "+CREG:";
776 } else if (request == RIL_REQUEST_GPRS_REGISTRATION_STATE) {
777 cmd = "AT+CGREG?";
778 prefix = "+CGREG:";
779 } else {
780 assert(0);
781 goto error;
782 }
783
784 err = at_send_command_singleline(cmd, prefix, &p_response);
785
786 if (err != 0) goto error;
787
788 line = p_response->p_intermediates->line;
789
790 err = at_tok_start(&line);
791 if (err < 0) goto error;
792
793 /* Ok you have to be careful here
794 * The solicited version of the CREG response is
795 * +CREG: n, stat, [lac, cid]
796 * and the unsolicited version is
797 * +CREG: stat, [lac, cid]
798 * The <n> parameter is basically "is unsolicited creg on?"
799 * which it should always be
800 *
801 * Now we should normally get the solicited version here,
802 * but the unsolicited version could have snuck in
803 * so we have to handle both
804 *
805 * Also since the LAC and CID are only reported when registered,
806 * we can have 1, 2, 3, or 4 arguments here
807 *
808 * finally, a +CGREG: answer may have a fifth value that corresponds
809 * to the network type, as in;
810 *
811 * +CGREG: n, stat [,lac, cid [,networkType]]
812 */
813
814 /* count number of commas */
815 commas = 0;
816 for (p = line ; *p != '\0' ;p++) {
817 if (*p == ',') commas++;
818 }
819
820 switch (commas) {
821 case 0: /* +CREG: <stat> */
822 err = at_tok_nextint(&line, &response[0]);
823 if (err < 0) goto error;
824 response[1] = -1;
825 response[2] = -1;
826 break;
827
828 case 1: /* +CREG: <n>, <stat> */
829 err = at_tok_nextint(&line, &skip);
830 if (err < 0) goto error;
831 err = at_tok_nextint(&line, &response[0]);
832 if (err < 0) goto error;
833 response[1] = -1;
834 response[2] = -1;
835 if (err < 0) goto error;
836 break;
837
838 case 2: /* +CREG: <stat>, <lac>, <cid> */
839 err = at_tok_nextint(&line, &response[0]);
840 if (err < 0) goto error;
841 err = at_tok_nexthexint(&line, &response[1]);
842 if (err < 0) goto error;
843 err = at_tok_nexthexint(&line, &response[2]);
844 if (err < 0) goto error;
845 break;
846 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
847 err = at_tok_nextint(&line, &skip);
848 if (err < 0) goto error;
849 err = at_tok_nextint(&line, &response[0]);
850 if (err < 0) goto error;
851 err = at_tok_nexthexint(&line, &response[1]);
852 if (err < 0) goto error;
853 err = at_tok_nexthexint(&line, &response[2]);
854 if (err < 0) goto error;
855 break;
856 /* special case for CGREG, there is a fourth parameter
857 * that is the network type (unknown/gprs/edge/umts)
858 */
859 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
860 err = at_tok_nextint(&line, &skip);
861 if (err < 0) goto error;
862 err = at_tok_nextint(&line, &response[0]);
863 if (err < 0) goto error;
864 err = at_tok_nexthexint(&line, &response[1]);
865 if (err < 0) goto error;
866 err = at_tok_nexthexint(&line, &response[2]);
867 if (err < 0) goto error;
868 err = at_tok_nexthexint(&line, &response[3]);
869 if (err < 0) goto error;
870 count = 4;
871 break;
872 default:
873 goto error;
874 }
875
876 asprintf(&responseStr[0], "%d", response[0]);
John Wang6f189a62009-06-30 13:12:53 -0700877 asprintf(&responseStr[1], "%x", response[1]);
878 asprintf(&responseStr[2], "%x", response[2]);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -0800879
880 if (count > 3)
881 asprintf(&responseStr[3], "%d", response[3]);
882
883 RIL_onRequestComplete(t, RIL_E_SUCCESS, responseStr, count*sizeof(char*));
884 at_response_free(p_response);
885
886 return;
887error:
888 LOGE("requestRegistrationState must never return an error when radio is on");
889 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
890 at_response_free(p_response);
891}
892
893static void requestOperator(void *data, size_t datalen, RIL_Token t)
894{
895 int err;
896 int i;
897 int skip;
898 ATLine *p_cur;
899 char *response[3];
900
901 memset(response, 0, sizeof(response));
902
903 ATResponse *p_response = NULL;
904
905 err = at_send_command_multiline(
906 "AT+COPS=3,0;+COPS?;+COPS=3,1;+COPS?;+COPS=3,2;+COPS?",
907 "+COPS:", &p_response);
908
909 /* we expect 3 lines here:
910 * +COPS: 0,0,"T - Mobile"
911 * +COPS: 0,1,"TMO"
912 * +COPS: 0,2,"310170"
913 */
914
915 if (err != 0) goto error;
916
917 for (i = 0, p_cur = p_response->p_intermediates
918 ; p_cur != NULL
919 ; p_cur = p_cur->p_next, i++
920 ) {
921 char *line = p_cur->line;
922
923 err = at_tok_start(&line);
924 if (err < 0) goto error;
925
926 err = at_tok_nextint(&line, &skip);
927 if (err < 0) goto error;
928
929 // If we're unregistered, we may just get
930 // a "+COPS: 0" response
931 if (!at_tok_hasmore(&line)) {
932 response[i] = NULL;
933 continue;
934 }
935
936 err = at_tok_nextint(&line, &skip);
937 if (err < 0) goto error;
938
939 // a "+COPS: 0, n" response is also possible
940 if (!at_tok_hasmore(&line)) {
941 response[i] = NULL;
942 continue;
943 }
944
945 err = at_tok_nextstr(&line, &(response[i]));
946 if (err < 0) goto error;
947 }
948
949 if (i != 3) {
950 /* expect 3 lines exactly */
951 goto error;
952 }
953
954 RIL_onRequestComplete(t, RIL_E_SUCCESS, response, sizeof(response));
955 at_response_free(p_response);
956
957 return;
958error:
959 LOGE("requestOperator must not return error when radio is on");
960 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
961 at_response_free(p_response);
962}
963
964static void requestSendSMS(void *data, size_t datalen, RIL_Token t)
965{
966 int err;
967 const char *smsc;
968 const char *pdu;
969 int tpLayerLength;
970 char *cmd1, *cmd2;
971 RIL_SMS_Response response;
972 ATResponse *p_response = NULL;
973
974 smsc = ((const char **)data)[0];
975 pdu = ((const char **)data)[1];
976
977 tpLayerLength = strlen(pdu)/2;
978
979 // "NULL for default SMSC"
980 if (smsc == NULL) {
981 smsc= "00";
982 }
983
984 asprintf(&cmd1, "AT+CMGS=%d", tpLayerLength);
985 asprintf(&cmd2, "%s%s", smsc, pdu);
986
987 err = at_send_command_sms(cmd1, cmd2, "+CMGS:", &p_response);
988
989 if (err != 0 || p_response->success == 0) goto error;
990
991 memset(&response, 0, sizeof(response));
992
993 /* FIXME fill in messageRef and ackPDU */
994
995 RIL_onRequestComplete(t, RIL_E_SUCCESS, &response, sizeof(response));
996 at_response_free(p_response);
997
998 return;
999error:
1000 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1001 at_response_free(p_response);
1002}
1003
Wink Savillef4c4d362009-04-02 01:37:03 -07001004static void requestSetupDataCall(void *data, size_t datalen, RIL_Token t)
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001005{
1006 const char *apn;
1007 char *cmd;
1008 int err;
1009 ATResponse *p_response = NULL;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001010
Wink Savillef4c4d362009-04-02 01:37:03 -07001011 apn = ((const char **)data)[2];
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001012
1013#ifdef USE_TI_COMMANDS
1014 // Config for multislot class 10 (probably default anyway eh?)
1015 err = at_send_command("AT%CPRIM=\"GMM\",\"CONFIG MULTISLOT_CLASS=<10>\"",
1016 NULL);
1017
1018 err = at_send_command("AT%DATA=2,\"UART\",1,,\"SER\",\"UART\",0", NULL);
1019#endif /* USE_TI_COMMANDS */
1020
1021 int fd, qmistatus;
1022 size_t cur = 0;
1023 size_t len;
1024 ssize_t written, rlen;
1025 char status[32] = {0};
1026 int retry = 10;
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001027 const char *pdp_type;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001028
1029 LOGD("requesting data connection to APN '%s'", apn);
1030
1031 fd = open ("/dev/qmi", O_RDWR);
1032 if (fd >= 0) { /* the device doesn't exist on the emulator */
1033
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001034 LOGD("opened the qmi device\n");
1035 asprintf(&cmd, "up:%s", apn);
1036 len = strlen(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001037
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001038 while (cur < len) {
1039 do {
1040 written = write (fd, cmd + cur, len - cur);
1041 } while (written < 0 && errno == EINTR);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001042
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001043 if (written < 0) {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001044 LOGE("### ERROR writing to /dev/qmi");
1045 close(fd);
1046 goto error;
1047 }
1048
1049 cur += written;
1050 }
1051
1052 // wait for interface to come online
1053
1054 do {
1055 sleep(1);
1056 do {
1057 rlen = read(fd, status, 31);
1058 } while (rlen < 0 && errno == EINTR);
1059
1060 if (rlen < 0) {
1061 LOGE("### ERROR reading from /dev/qmi");
1062 close(fd);
1063 goto error;
1064 } else {
1065 status[rlen] = '\0';
1066 LOGD("### status: %s", status);
1067 }
1068 } while (strncmp(status, "STATE=up", 8) && strcmp(status, "online") && --retry);
1069
1070 close(fd);
1071
1072 if (retry == 0) {
1073 LOGE("### Failed to get data connection up\n");
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001074 goto error;
1075 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001076
1077 qmistatus = system("netcfg rmnet0 dhcp");
1078
1079 LOGD("netcfg rmnet0 dhcp: status %d\n", qmistatus);
1080
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001081 if (qmistatus < 0) goto error;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001082
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001083 } else {
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001084
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001085 if (datalen > 6 * sizeof(char *)) {
1086 pdp_type = ((const char **)data)[6];
1087 } else {
1088 pdp_type = "IP";
1089 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001090
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001091 asprintf(&cmd, "AT+CGDCONT=1,\"%s\",\"%s\",,0,0", pdp_type, apn);
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001092 //FIXME check for error here
1093 err = at_send_command(cmd, NULL);
1094 free(cmd);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001095
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001096 // Set required QoS params to default
1097 err = at_send_command("AT+CGQREQ=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001098
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001099 // Set minimum QoS params to default
1100 err = at_send_command("AT+CGQMIN=1", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001101
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001102 // packet-domain event reporting
1103 err = at_send_command("AT+CGEREP=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001104
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001105 // Hangup anything that's happening there now
1106 err = at_send_command("AT+CGACT=1,0", NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001107
Lorenzo Colitti4f81dcf2010-09-01 19:38:57 -07001108 // Start data on PDP context 1
1109 err = at_send_command("ATD*99***1#", &p_response);
1110
1111 if (err < 0 || p_response->success == 0) {
1112 goto error;
1113 }
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001114 }
1115
Wink Saville43808972011-01-13 17:39:51 -08001116 requestOrSendDataCallList(&t);
1117
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001118 at_response_free(p_response);
1119
1120 return;
1121error:
1122 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1123 at_response_free(p_response);
1124
1125}
1126
1127static void requestSMSAcknowledge(void *data, size_t datalen, RIL_Token t)
1128{
1129 int ackSuccess;
1130 int err;
1131
1132 ackSuccess = ((int *)data)[0];
1133
1134 if (ackSuccess == 1) {
1135 err = at_send_command("AT+CNMA=1", NULL);
1136 } else if (ackSuccess == 0) {
1137 err = at_send_command("AT+CNMA=2", NULL);
1138 } else {
1139 LOGE("unsupported arg to RIL_REQUEST_SMS_ACKNOWLEDGE\n");
1140 goto error;
1141 }
1142
1143 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1144error:
1145 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1146
1147}
1148
1149static void requestSIM_IO(void *data, size_t datalen, RIL_Token t)
1150{
1151 ATResponse *p_response = NULL;
1152 RIL_SIM_IO_Response sr;
1153 int err;
1154 char *cmd = NULL;
1155 RIL_SIM_IO *p_args;
1156 char *line;
1157
1158 memset(&sr, 0, sizeof(sr));
1159
1160 p_args = (RIL_SIM_IO *)data;
1161
1162 /* FIXME handle pin2 */
1163
1164 if (p_args->data == NULL) {
1165 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d",
1166 p_args->command, p_args->fileid,
1167 p_args->p1, p_args->p2, p_args->p3);
1168 } else {
1169 asprintf(&cmd, "AT+CRSM=%d,%d,%d,%d,%d,%s",
1170 p_args->command, p_args->fileid,
1171 p_args->p1, p_args->p2, p_args->p3, p_args->data);
1172 }
1173
1174 err = at_send_command_singleline(cmd, "+CRSM:", &p_response);
1175
1176 if (err < 0 || p_response->success == 0) {
1177 goto error;
1178 }
1179
1180 line = p_response->p_intermediates->line;
1181
1182 err = at_tok_start(&line);
1183 if (err < 0) goto error;
1184
1185 err = at_tok_nextint(&line, &(sr.sw1));
1186 if (err < 0) goto error;
1187
1188 err = at_tok_nextint(&line, &(sr.sw2));
1189 if (err < 0) goto error;
1190
1191 if (at_tok_hasmore(&line)) {
1192 err = at_tok_nextstr(&line, &(sr.simResponse));
1193 if (err < 0) goto error;
1194 }
1195
1196 RIL_onRequestComplete(t, RIL_E_SUCCESS, &sr, sizeof(sr));
1197 at_response_free(p_response);
1198 free(cmd);
1199
1200 return;
1201error:
1202 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1203 at_response_free(p_response);
1204 free(cmd);
1205
1206}
1207
1208static void requestEnterSimPin(void* data, size_t datalen, RIL_Token t)
1209{
1210 ATResponse *p_response = NULL;
1211 int err;
1212 char* cmd = NULL;
1213 const char** strings = (const char**)data;;
1214
1215 if ( datalen == sizeof(char*) ) {
1216 asprintf(&cmd, "AT+CPIN=%s", strings[0]);
1217 } else if ( datalen == 2*sizeof(char*) ) {
1218 asprintf(&cmd, "AT+CPIN=%s,%s", strings[0], strings[1]);
1219 } else
1220 goto error;
1221
1222 err = at_send_command_singleline(cmd, "+CPIN:", &p_response);
1223 free(cmd);
1224
1225 if (err < 0 || p_response->success == 0) {
1226error:
1227 RIL_onRequestComplete(t, RIL_E_PASSWORD_INCORRECT, NULL, 0);
1228 } else {
1229 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1230 }
1231 at_response_free(p_response);
1232}
1233
1234
1235static void requestSendUSSD(void *data, size_t datalen, RIL_Token t)
1236{
1237 const char *ussdRequest;
1238
1239 ussdRequest = (char *)(data);
1240
1241
1242 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1243
1244// @@@ TODO
1245
1246}
1247
1248
1249/*** Callback methods from the RIL library to us ***/
1250
1251/**
1252 * Call from RIL to us to make a RIL_REQUEST
1253 *
1254 * Must be completed with a call to RIL_onRequestComplete()
1255 *
1256 * RIL_onRequestComplete() may be called from any thread, before or after
1257 * this function returns.
1258 *
1259 * Will always be called from the same thread, so returning here implies
1260 * that the radio is ready to process another command (whether or not
1261 * the previous command has completed).
1262 */
1263static void
1264onRequest (int request, void *data, size_t datalen, RIL_Token t)
1265{
1266 ATResponse *p_response;
1267 int err;
1268
1269 LOGD("onRequest: %s", requestToString(request));
1270
1271 /* Ignore all requests except RIL_REQUEST_GET_SIM_STATUS
1272 * when RADIO_STATE_UNAVAILABLE.
1273 */
1274 if (sState == RADIO_STATE_UNAVAILABLE
1275 && request != RIL_REQUEST_GET_SIM_STATUS
1276 ) {
1277 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1278 return;
1279 }
1280
1281 /* Ignore all non-power requests when RADIO_STATE_OFF
1282 * (except RIL_REQUEST_GET_SIM_STATUS)
1283 */
1284 if (sState == RADIO_STATE_OFF
1285 && !(request == RIL_REQUEST_RADIO_POWER
1286 || request == RIL_REQUEST_GET_SIM_STATUS)
1287 ) {
1288 RIL_onRequestComplete(t, RIL_E_RADIO_NOT_AVAILABLE, NULL, 0);
1289 return;
1290 }
1291
1292 switch (request) {
1293 case RIL_REQUEST_GET_SIM_STATUS: {
Wink Savillef6aa7c12009-04-30 14:20:52 -07001294 RIL_CardStatus *p_card_status;
1295 char *p_buffer;
1296 int buffer_size;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001297
Wink Savillef6aa7c12009-04-30 14:20:52 -07001298 int result = getCardStatus(&p_card_status);
1299 if (result == RIL_E_SUCCESS) {
1300 p_buffer = (char *)p_card_status;
1301 buffer_size = sizeof(*p_card_status);
1302 } else {
1303 p_buffer = NULL;
1304 buffer_size = 0;
1305 }
1306 RIL_onRequestComplete(t, result, p_buffer, buffer_size);
1307 freeCardStatus(p_card_status);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001308 break;
1309 }
1310 case RIL_REQUEST_GET_CURRENT_CALLS:
1311 requestGetCurrentCalls(data, datalen, t);
1312 break;
1313 case RIL_REQUEST_DIAL:
1314 requestDial(data, datalen, t);
1315 break;
1316 case RIL_REQUEST_HANGUP:
1317 requestHangup(data, datalen, t);
1318 break;
1319 case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND:
1320 // 3GPP 22.030 6.5.5
1321 // "Releases all held calls or sets User Determined User Busy
1322 // (UDUB) for a waiting call."
1323 at_send_command("AT+CHLD=0", NULL);
1324
1325 /* success or failure is ignored by the upper layer here.
1326 it will call GET_CURRENT_CALLS and determine success that way */
1327 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1328 break;
1329 case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND:
1330 // 3GPP 22.030 6.5.5
1331 // "Releases all active calls (if any exist) and accepts
1332 // the other (held or waiting) call."
1333 at_send_command("AT+CHLD=1", NULL);
1334
1335 /* success or failure is ignored by the upper layer here.
1336 it will call GET_CURRENT_CALLS and determine success that way */
1337 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1338 break;
1339 case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE:
1340 // 3GPP 22.030 6.5.5
1341 // "Places all active calls (if any exist) on hold and accepts
1342 // the other (held or waiting) call."
1343 at_send_command("AT+CHLD=2", NULL);
1344
1345#ifdef WORKAROUND_ERRONEOUS_ANSWER
1346 s_expectAnswer = 1;
1347#endif /* WORKAROUND_ERRONEOUS_ANSWER */
1348
1349 /* success or failure is ignored by the upper layer here.
1350 it will call GET_CURRENT_CALLS and determine success that way */
1351 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1352 break;
1353 case RIL_REQUEST_ANSWER:
1354 at_send_command("ATA", NULL);
1355
1356#ifdef WORKAROUND_ERRONEOUS_ANSWER
1357 s_expectAnswer = 1;
1358#endif /* WORKAROUND_ERRONEOUS_ANSWER */
1359
1360 /* success or failure is ignored by the upper layer here.
1361 it will call GET_CURRENT_CALLS and determine success that way */
1362 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1363 break;
1364 case RIL_REQUEST_CONFERENCE:
1365 // 3GPP 22.030 6.5.5
1366 // "Adds a held call to the conversation"
1367 at_send_command("AT+CHLD=3", NULL);
1368
1369 /* success or failure is ignored by the upper layer here.
1370 it will call GET_CURRENT_CALLS and determine success that way */
1371 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1372 break;
1373 case RIL_REQUEST_UDUB:
1374 /* user determined user busy */
1375 /* sometimes used: ATH */
1376 at_send_command("ATH", NULL);
1377
1378 /* success or failure is ignored by the upper layer here.
1379 it will call GET_CURRENT_CALLS and determine success that way */
1380 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1381 break;
1382
1383 case RIL_REQUEST_SEPARATE_CONNECTION:
1384 {
1385 char cmd[12];
1386 int party = ((int*)data)[0];
1387
1388 // Make sure that party is in a valid range.
1389 // (Note: The Telephony middle layer imposes a range of 1 to 7.
1390 // It's sufficient for us to just make sure it's single digit.)
1391 if (party > 0 && party < 10) {
1392 sprintf(cmd, "AT+CHLD=2%d", party);
1393 at_send_command(cmd, NULL);
1394 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1395 } else {
1396 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1397 }
1398 }
1399 break;
1400
1401 case RIL_REQUEST_SIGNAL_STRENGTH:
1402 requestSignalStrength(data, datalen, t);
1403 break;
1404 case RIL_REQUEST_REGISTRATION_STATE:
1405 case RIL_REQUEST_GPRS_REGISTRATION_STATE:
1406 requestRegistrationState(request, data, datalen, t);
1407 break;
1408 case RIL_REQUEST_OPERATOR:
1409 requestOperator(data, datalen, t);
1410 break;
1411 case RIL_REQUEST_RADIO_POWER:
1412 requestRadioPower(data, datalen, t);
1413 break;
1414 case RIL_REQUEST_DTMF: {
1415 char c = ((char *)data)[0];
1416 char *cmd;
1417 asprintf(&cmd, "AT+VTS=%c", (int)c);
1418 at_send_command(cmd, NULL);
1419 free(cmd);
1420 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1421 break;
1422 }
1423 case RIL_REQUEST_SEND_SMS:
1424 requestSendSMS(data, datalen, t);
1425 break;
Wink Savillef4c4d362009-04-02 01:37:03 -07001426 case RIL_REQUEST_SETUP_DATA_CALL:
1427 requestSetupDataCall(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001428 break;
1429 case RIL_REQUEST_SMS_ACKNOWLEDGE:
1430 requestSMSAcknowledge(data, datalen, t);
1431 break;
1432
1433 case RIL_REQUEST_GET_IMSI:
1434 p_response = NULL;
1435 err = at_send_command_numeric("AT+CIMI", &p_response);
1436
1437 if (err < 0 || p_response->success == 0) {
1438 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1439 } else {
1440 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1441 p_response->p_intermediates->line, sizeof(char *));
1442 }
1443 at_response_free(p_response);
1444 break;
1445
1446 case RIL_REQUEST_GET_IMEI:
1447 p_response = NULL;
1448 err = at_send_command_numeric("AT+CGSN", &p_response);
1449
1450 if (err < 0 || p_response->success == 0) {
1451 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1452 } else {
1453 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1454 p_response->p_intermediates->line, sizeof(char *));
1455 }
1456 at_response_free(p_response);
1457 break;
1458
1459 case RIL_REQUEST_SIM_IO:
1460 requestSIM_IO(data,datalen,t);
1461 break;
1462
1463 case RIL_REQUEST_SEND_USSD:
1464 requestSendUSSD(data, datalen, t);
1465 break;
1466
1467 case RIL_REQUEST_CANCEL_USSD:
1468 p_response = NULL;
1469 err = at_send_command_numeric("AT+CUSD=2", &p_response);
1470
1471 if (err < 0 || p_response->success == 0) {
1472 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1473 } else {
1474 RIL_onRequestComplete(t, RIL_E_SUCCESS,
1475 p_response->p_intermediates->line, sizeof(char *));
1476 }
1477 at_response_free(p_response);
1478 break;
1479
1480 case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC:
1481 at_send_command("AT+COPS=0", NULL);
1482 break;
1483
Wink Savillef4c4d362009-04-02 01:37:03 -07001484 case RIL_REQUEST_DATA_CALL_LIST:
1485 requestDataCallList(data, datalen, t);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001486 break;
1487
1488 case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE:
1489 requestQueryNetworkSelectionMode(data, datalen, t);
1490 break;
1491
1492 case RIL_REQUEST_OEM_HOOK_RAW:
1493 // echo back data
1494 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1495 break;
1496
1497
1498 case RIL_REQUEST_OEM_HOOK_STRINGS: {
1499 int i;
1500 const char ** cur;
1501
1502 LOGD("got OEM_HOOK_STRINGS: 0x%8p %lu", data, (long)datalen);
1503
1504
1505 for (i = (datalen / sizeof (char *)), cur = (const char **)data ;
1506 i > 0 ; cur++, i --) {
1507 LOGD("> '%s'", *cur);
1508 }
1509
1510 // echo back strings
1511 RIL_onRequestComplete(t, RIL_E_SUCCESS, data, datalen);
1512 break;
1513 }
1514
1515 case RIL_REQUEST_WRITE_SMS_TO_SIM:
1516 requestWriteSmsToSim(data, datalen, t);
1517 break;
1518
1519 case RIL_REQUEST_DELETE_SMS_ON_SIM: {
1520 char * cmd;
1521 p_response = NULL;
1522 asprintf(&cmd, "AT+CMGD=%d", ((int *)data)[0]);
1523 err = at_send_command(cmd, &p_response);
1524 free(cmd);
1525 if (err < 0 || p_response->success == 0) {
1526 RIL_onRequestComplete(t, RIL_E_GENERIC_FAILURE, NULL, 0);
1527 } else {
1528 RIL_onRequestComplete(t, RIL_E_SUCCESS, NULL, 0);
1529 }
1530 at_response_free(p_response);
1531 break;
1532 }
1533
1534 case RIL_REQUEST_ENTER_SIM_PIN:
1535 case RIL_REQUEST_ENTER_SIM_PUK:
1536 case RIL_REQUEST_ENTER_SIM_PIN2:
1537 case RIL_REQUEST_ENTER_SIM_PUK2:
1538 case RIL_REQUEST_CHANGE_SIM_PIN:
1539 case RIL_REQUEST_CHANGE_SIM_PIN2:
1540 requestEnterSimPin(data, datalen, t);
1541 break;
1542
1543 default:
1544 RIL_onRequestComplete(t, RIL_E_REQUEST_NOT_SUPPORTED, NULL, 0);
1545 break;
1546 }
1547}
1548
1549/**
1550 * Synchronous call from the RIL to us to return current radio state.
1551 * RADIO_STATE_UNAVAILABLE should be the initial state.
1552 */
1553static RIL_RadioState
1554currentState()
1555{
1556 return sState;
1557}
1558/**
1559 * Call from RIL to us to find out whether a specific request code
1560 * is supported by this implementation.
1561 *
1562 * Return 1 for "supported" and 0 for "unsupported"
1563 */
1564
1565static int
1566onSupports (int requestCode)
1567{
1568 //@@@ todo
1569
1570 return 1;
1571}
1572
1573static void onCancel (RIL_Token t)
1574{
1575 //@@@todo
1576
1577}
1578
1579static const char * getVersion(void)
1580{
1581 return "android reference-ril 1.0";
1582}
1583
1584static void
1585setRadioState(RIL_RadioState newState)
1586{
1587 RIL_RadioState oldState;
1588
1589 pthread_mutex_lock(&s_state_mutex);
1590
1591 oldState = sState;
1592
1593 if (s_closed > 0) {
1594 // If we're closed, the only reasonable state is
1595 // RADIO_STATE_UNAVAILABLE
1596 // This is here because things on the main thread
1597 // may attempt to change the radio state after the closed
1598 // event happened in another thread
1599 newState = RADIO_STATE_UNAVAILABLE;
1600 }
1601
1602 if (sState != newState || s_closed > 0) {
1603 sState = newState;
1604
1605 pthread_cond_broadcast (&s_state_cond);
1606 }
1607
1608 pthread_mutex_unlock(&s_state_mutex);
1609
1610
1611 /* do these outside of the mutex */
1612 if (sState != oldState) {
1613 RIL_onUnsolicitedResponse (RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED,
1614 NULL, 0);
1615
1616 /* FIXME onSimReady() and onRadioPowerOn() cannot be called
1617 * from the AT reader thread
1618 * Currently, this doesn't happen, but if that changes then these
1619 * will need to be dispatched on the request thread
1620 */
1621 if (sState == RADIO_STATE_SIM_READY) {
1622 onSIMReady();
1623 } else if (sState == RADIO_STATE_SIM_NOT_READY) {
1624 onRadioPowerOn();
1625 }
1626 }
1627}
1628
John Wang309ac292009-07-30 14:53:23 -07001629/** Returns SIM_NOT_READY on error */
David 'Digit' Turneraf1298d2011-02-04 13:36:47 +01001630static SIM_Status
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001631getSIMStatus()
1632{
1633 ATResponse *p_response = NULL;
1634 int err;
1635 int ret;
1636 char *cpinLine;
1637 char *cpinResult;
1638
1639 if (sState == RADIO_STATE_OFF || sState == RADIO_STATE_UNAVAILABLE) {
John Wang309ac292009-07-30 14:53:23 -07001640 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001641 goto done;
1642 }
1643
1644 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
1645
1646 if (err != 0) {
John Wang309ac292009-07-30 14:53:23 -07001647 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001648 goto done;
1649 }
1650
1651 switch (at_get_cme_error(p_response)) {
1652 case CME_SUCCESS:
1653 break;
1654
1655 case CME_SIM_NOT_INSERTED:
John Wang309ac292009-07-30 14:53:23 -07001656 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001657 goto done;
1658
1659 default:
John Wang309ac292009-07-30 14:53:23 -07001660 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001661 goto done;
1662 }
1663
1664 /* CPIN? has succeeded, now look at the result */
1665
1666 cpinLine = p_response->p_intermediates->line;
1667 err = at_tok_start (&cpinLine);
1668
1669 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07001670 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001671 goto done;
1672 }
1673
1674 err = at_tok_nextstr(&cpinLine, &cpinResult);
1675
1676 if (err < 0) {
John Wang309ac292009-07-30 14:53:23 -07001677 ret = SIM_NOT_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001678 goto done;
1679 }
1680
1681 if (0 == strcmp (cpinResult, "SIM PIN")) {
John Wang309ac292009-07-30 14:53:23 -07001682 ret = SIM_PIN;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001683 goto done;
1684 } else if (0 == strcmp (cpinResult, "SIM PUK")) {
John Wang309ac292009-07-30 14:53:23 -07001685 ret = SIM_PUK;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001686 goto done;
1687 } else if (0 == strcmp (cpinResult, "PH-NET PIN")) {
John Wang309ac292009-07-30 14:53:23 -07001688 return SIM_NETWORK_PERSONALIZATION;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001689 } else if (0 != strcmp (cpinResult, "READY")) {
1690 /* we're treating unsupported lock types as "sim absent" */
John Wang309ac292009-07-30 14:53:23 -07001691 ret = SIM_ABSENT;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001692 goto done;
1693 }
1694
1695 at_response_free(p_response);
1696 p_response = NULL;
1697 cpinResult = NULL;
1698
John Wang309ac292009-07-30 14:53:23 -07001699 ret = SIM_READY;
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001700
1701done:
1702 at_response_free(p_response);
1703 return ret;
1704}
1705
1706
1707/**
Wink Savillef6aa7c12009-04-30 14:20:52 -07001708 * Get the current card status.
1709 *
1710 * This must be freed using freeCardStatus.
1711 * @return: On success returns RIL_E_SUCCESS
1712 */
1713static int getCardStatus(RIL_CardStatus **pp_card_status) {
1714 static RIL_AppStatus app_status_array[] = {
John Wang309ac292009-07-30 14:53:23 -07001715 // SIM_ABSENT = 0
Wink Savillef6aa7c12009-04-30 14:20:52 -07001716 { RIL_APPTYPE_UNKNOWN, RIL_APPSTATE_UNKNOWN, RIL_PERSOSUBSTATE_UNKNOWN,
1717 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07001718 // SIM_NOT_READY = 1
Wink Savillef6aa7c12009-04-30 14:20:52 -07001719 { RIL_APPTYPE_SIM, RIL_APPSTATE_DETECTED, RIL_PERSOSUBSTATE_UNKNOWN,
1720 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07001721 // SIM_READY = 2
Wink Savillef6aa7c12009-04-30 14:20:52 -07001722 { RIL_APPTYPE_SIM, RIL_APPSTATE_READY, RIL_PERSOSUBSTATE_READY,
1723 NULL, NULL, 0, RIL_PINSTATE_UNKNOWN, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07001724 // SIM_PIN = 3
Wink Savillef6aa7c12009-04-30 14:20:52 -07001725 { RIL_APPTYPE_SIM, RIL_APPSTATE_PIN, RIL_PERSOSUBSTATE_UNKNOWN,
1726 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07001727 // SIM_PUK = 4
Wink Savillef6aa7c12009-04-30 14:20:52 -07001728 { RIL_APPTYPE_SIM, RIL_APPSTATE_PUK, RIL_PERSOSUBSTATE_UNKNOWN,
1729 NULL, NULL, 0, RIL_PINSTATE_ENABLED_BLOCKED, RIL_PINSTATE_UNKNOWN },
John Wang309ac292009-07-30 14:53:23 -07001730 // SIM_NETWORK_PERSONALIZATION = 5
Wink Savillef6aa7c12009-04-30 14:20:52 -07001731 { RIL_APPTYPE_SIM, RIL_APPSTATE_SUBSCRIPTION_PERSO, RIL_PERSOSUBSTATE_SIM_NETWORK,
1732 NULL, NULL, 0, RIL_PINSTATE_ENABLED_NOT_VERIFIED, RIL_PINSTATE_UNKNOWN }
1733 };
1734 RIL_CardState card_state;
1735 int num_apps;
1736
1737 int sim_status = getSIMStatus();
John Wang309ac292009-07-30 14:53:23 -07001738 if (sim_status == SIM_ABSENT) {
Wink Savillef6aa7c12009-04-30 14:20:52 -07001739 card_state = RIL_CARDSTATE_ABSENT;
1740 num_apps = 0;
1741 } else {
1742 card_state = RIL_CARDSTATE_PRESENT;
1743 num_apps = 1;
1744 }
1745
1746 // Allocate and initialize base card status.
1747 RIL_CardStatus *p_card_status = malloc(sizeof(RIL_CardStatus));
1748 p_card_status->card_state = card_state;
1749 p_card_status->universal_pin_state = RIL_PINSTATE_UNKNOWN;
1750 p_card_status->gsm_umts_subscription_app_index = RIL_CARD_MAX_APPS;
1751 p_card_status->cdma_subscription_app_index = RIL_CARD_MAX_APPS;
1752 p_card_status->num_applications = num_apps;
1753
1754 // Initialize application status
1755 int i;
1756 for (i = 0; i < RIL_CARD_MAX_APPS; i++) {
John Wang309ac292009-07-30 14:53:23 -07001757 p_card_status->applications[i] = app_status_array[SIM_ABSENT];
Wink Savillef6aa7c12009-04-30 14:20:52 -07001758 }
1759
1760 // Pickup the appropriate application status
1761 // that reflects sim_status for gsm.
1762 if (num_apps != 0) {
1763 // Only support one app, gsm
1764 p_card_status->num_applications = 1;
1765 p_card_status->gsm_umts_subscription_app_index = 0;
1766
1767 // Get the correct app status
1768 p_card_status->applications[0] = app_status_array[sim_status];
1769 }
1770
1771 *pp_card_status = p_card_status;
1772 return RIL_E_SUCCESS;
1773}
1774
1775/**
1776 * Free the card status returned by getCardStatus
1777 */
1778static void freeCardStatus(RIL_CardStatus *p_card_status) {
1779 free(p_card_status);
1780}
1781
1782/**
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001783 * SIM ready means any commands that access the SIM will work, including:
1784 * AT+CPIN, AT+CSMS, AT+CNMI, AT+CRSM
1785 * (all SMS-related commands)
1786 */
1787
1788static void pollSIMState (void *param)
1789{
1790 ATResponse *p_response;
1791 int ret;
1792
1793 if (sState != RADIO_STATE_SIM_NOT_READY) {
1794 // no longer valid to poll
1795 return;
1796 }
1797
1798 switch(getSIMStatus()) {
John Wang309ac292009-07-30 14:53:23 -07001799 case SIM_ABSENT:
1800 case SIM_PIN:
1801 case SIM_PUK:
1802 case SIM_NETWORK_PERSONALIZATION:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001803 default:
1804 setRadioState(RADIO_STATE_SIM_LOCKED_OR_ABSENT);
1805 return;
1806
John Wang309ac292009-07-30 14:53:23 -07001807 case SIM_NOT_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001808 RIL_requestTimedCallback (pollSIMState, NULL, &TIMEVAL_SIMPOLL);
1809 return;
1810
John Wang309ac292009-07-30 14:53:23 -07001811 case SIM_READY:
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001812 setRadioState(RADIO_STATE_SIM_READY);
1813 return;
1814 }
1815}
1816
1817/** returns 1 if on, 0 if off, and -1 on error */
1818static int isRadioOn()
1819{
1820 ATResponse *p_response = NULL;
1821 int err;
1822 char *line;
1823 char ret;
1824
1825 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
1826
1827 if (err < 0 || p_response->success == 0) {
1828 // assume radio is off
1829 goto error;
1830 }
1831
1832 line = p_response->p_intermediates->line;
1833
1834 err = at_tok_start(&line);
1835 if (err < 0) goto error;
1836
1837 err = at_tok_nextbool(&line, &ret);
1838 if (err < 0) goto error;
1839
1840 at_response_free(p_response);
1841
1842 return (int)ret;
1843
1844error:
1845
1846 at_response_free(p_response);
1847 return -1;
1848}
1849
1850/**
1851 * Initialize everything that can be configured while we're still in
1852 * AT+CFUN=0
1853 */
1854static void initializeCallback(void *param)
1855{
1856 ATResponse *p_response = NULL;
1857 int err;
1858
1859 setRadioState (RADIO_STATE_OFF);
1860
1861 at_handshake();
1862
1863 /* note: we don't check errors here. Everything important will
1864 be handled in onATTimeout and onATReaderClosed */
1865
1866 /* atchannel is tolerant of echo but it must */
1867 /* have verbose result codes */
1868 at_send_command("ATE0Q0V1", NULL);
1869
1870 /* No auto-answer */
1871 at_send_command("ATS0=0", NULL);
1872
1873 /* Extended errors */
1874 at_send_command("AT+CMEE=1", NULL);
1875
1876 /* Network registration events */
1877 err = at_send_command("AT+CREG=2", &p_response);
1878
1879 /* some handsets -- in tethered mode -- don't support CREG=2 */
1880 if (err < 0 || p_response->success == 0) {
1881 at_send_command("AT+CREG=1", NULL);
1882 }
1883
1884 at_response_free(p_response);
1885
1886 /* GPRS registration events */
1887 at_send_command("AT+CGREG=1", NULL);
1888
1889 /* Call Waiting notifications */
1890 at_send_command("AT+CCWA=1", NULL);
1891
1892 /* Alternating voice/data off */
1893 at_send_command("AT+CMOD=0", NULL);
1894
1895 /* Not muted */
1896 at_send_command("AT+CMUT=0", NULL);
1897
1898 /* +CSSU unsolicited supp service notifications */
1899 at_send_command("AT+CSSN=0,1", NULL);
1900
1901 /* no connected line identification */
1902 at_send_command("AT+COLP=0", NULL);
1903
1904 /* HEX character set */
1905 at_send_command("AT+CSCS=\"HEX\"", NULL);
1906
1907 /* USSD unsolicited */
1908 at_send_command("AT+CUSD=1", NULL);
1909
1910 /* Enable +CGEV GPRS event notifications, but don't buffer */
1911 at_send_command("AT+CGEREP=1,0", NULL);
1912
1913 /* SMS PDU mode */
1914 at_send_command("AT+CMGF=0", NULL);
1915
1916#ifdef USE_TI_COMMANDS
1917
1918 at_send_command("AT%CPI=3", NULL);
1919
1920 /* TI specific -- notifications when SMS is ready (currently ignored) */
1921 at_send_command("AT%CSTAT=1", NULL);
1922
1923#endif /* USE_TI_COMMANDS */
1924
1925
1926 /* assume radio is off on error */
1927 if (isRadioOn() > 0) {
1928 setRadioState (RADIO_STATE_SIM_NOT_READY);
1929 }
1930}
1931
1932static void waitForClose()
1933{
1934 pthread_mutex_lock(&s_state_mutex);
1935
1936 while (s_closed == 0) {
1937 pthread_cond_wait(&s_state_cond, &s_state_mutex);
1938 }
1939
1940 pthread_mutex_unlock(&s_state_mutex);
1941}
1942
1943/**
1944 * Called by atchannel when an unsolicited line appears
1945 * This is called on atchannel's reader thread. AT commands may
1946 * not be issued here
1947 */
1948static void onUnsolicited (const char *s, const char *sms_pdu)
1949{
1950 char *line = NULL;
1951 int err;
1952
1953 /* Ignore unsolicited responses until we're initialized.
1954 * This is OK because the RIL library will poll for initial state
1955 */
1956 if (sState == RADIO_STATE_UNAVAILABLE) {
1957 return;
1958 }
1959
1960 if (strStartsWith(s, "%CTZV:")) {
1961 /* TI specific -- NITZ time */
1962 char *response;
1963
1964 line = strdup(s);
1965 at_tok_start(&line);
1966
1967 err = at_tok_nextstr(&line, &response);
1968
1969 if (err != 0) {
1970 LOGE("invalid NITZ line %s\n", s);
1971 } else {
1972 RIL_onUnsolicitedResponse (
1973 RIL_UNSOL_NITZ_TIME_RECEIVED,
1974 response, strlen(response));
1975 }
1976 } else if (strStartsWith(s,"+CRING:")
1977 || strStartsWith(s,"RING")
1978 || strStartsWith(s,"NO CARRIER")
1979 || strStartsWith(s,"+CCWA")
1980 ) {
1981 RIL_onUnsolicitedResponse (
1982 RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED,
1983 NULL, 0);
1984#ifdef WORKAROUND_FAKE_CGEV
Wink Savillef4c4d362009-04-02 01:37:03 -07001985 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL); //TODO use new function
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001986#endif /* WORKAROUND_FAKE_CGEV */
1987 } else if (strStartsWith(s,"+CREG:")
1988 || strStartsWith(s,"+CGREG:")
1989 ) {
1990 RIL_onUnsolicitedResponse (
1991 RIL_UNSOL_RESPONSE_NETWORK_STATE_CHANGED,
1992 NULL, 0);
1993#ifdef WORKAROUND_FAKE_CGEV
Wink Saville7f856802009-06-09 10:23:37 -07001994 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08001995#endif /* WORKAROUND_FAKE_CGEV */
1996 } else if (strStartsWith(s, "+CMT:")) {
1997 RIL_onUnsolicitedResponse (
1998 RIL_UNSOL_RESPONSE_NEW_SMS,
1999 sms_pdu, strlen(sms_pdu));
2000 } else if (strStartsWith(s, "+CDS:")) {
2001 RIL_onUnsolicitedResponse (
2002 RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT,
2003 sms_pdu, strlen(sms_pdu));
2004 } else if (strStartsWith(s, "+CGEV:")) {
2005 /* Really, we can ignore NW CLASS and ME CLASS events here,
2006 * but right now we don't since extranous
Wink Savillef4c4d362009-04-02 01:37:03 -07002007 * RIL_UNSOL_DATA_CALL_LIST_CHANGED calls are tolerated
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002008 */
2009 /* can't issue AT commands here -- call on main thread */
Wink Savillef4c4d362009-04-02 01:37:03 -07002010 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002011#ifdef WORKAROUND_FAKE_CGEV
2012 } else if (strStartsWith(s, "+CME ERROR: 150")) {
Wink Savillef4c4d362009-04-02 01:37:03 -07002013 RIL_requestTimedCallback (onDataCallListChanged, NULL, NULL);
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002014#endif /* WORKAROUND_FAKE_CGEV */
2015 }
2016}
2017
2018/* Called on command or reader thread */
2019static void onATReaderClosed()
2020{
2021 LOGI("AT channel closed\n");
2022 at_close();
2023 s_closed = 1;
2024
2025 setRadioState (RADIO_STATE_UNAVAILABLE);
2026}
2027
2028/* Called on command thread */
2029static void onATTimeout()
2030{
2031 LOGI("AT channel timeout; closing\n");
2032 at_close();
2033
2034 s_closed = 1;
2035
2036 /* FIXME cause a radio reset here */
2037
2038 setRadioState (RADIO_STATE_UNAVAILABLE);
2039}
2040
2041static void usage(char *s)
2042{
2043#ifdef RIL_SHLIB
2044 fprintf(stderr, "reference-ril requires: -p <tcp port> or -d /dev/tty_device\n");
2045#else
2046 fprintf(stderr, "usage: %s [-p <tcp port>] [-d /dev/tty_device]\n", s);
2047 exit(-1);
2048#endif
2049}
2050
2051static void *
2052mainLoop(void *param)
2053{
2054 int fd;
2055 int ret;
2056
2057 AT_DUMP("== ", "entering mainLoop()", -1 );
2058 at_set_on_reader_closed(onATReaderClosed);
2059 at_set_on_timeout(onATTimeout);
2060
2061 for (;;) {
2062 fd = -1;
2063 while (fd < 0) {
2064 if (s_port > 0) {
2065 fd = socket_loopback_client(s_port, SOCK_STREAM);
2066 } else if (s_device_socket) {
The Android Open Source Projecte6e6fb22009-03-18 17:39:47 -07002067 if (!strcmp(s_device_path, "/dev/socket/qemud")) {
2068 /* Qemu-specific control socket */
2069 fd = socket_local_client( "qemud",
2070 ANDROID_SOCKET_NAMESPACE_RESERVED,
2071 SOCK_STREAM );
2072 if (fd >= 0 ) {
2073 char answer[2];
2074
2075 if ( write(fd, "gsm", 3) != 3 ||
2076 read(fd, answer, 2) != 2 ||
2077 memcmp(answer, "OK", 2) != 0)
2078 {
2079 close(fd);
2080 fd = -1;
2081 }
2082 }
2083 }
2084 else
2085 fd = socket_local_client( s_device_path,
2086 ANDROID_SOCKET_NAMESPACE_FILESYSTEM,
2087 SOCK_STREAM );
The Android Open Source Project00f06fc2009-03-03 19:32:15 -08002088 } else if (s_device_path != NULL) {
2089 fd = open (s_device_path, O_RDWR);
2090 if ( fd >= 0 && !memcmp( s_device_path, "/dev/ttyS", 9 ) ) {
2091 /* disable echo on serial ports */
2092 struct termios ios;
2093 tcgetattr( fd, &ios );
2094 ios.c_lflag = 0; /* disable ECHO, ICANON, etc... */
2095 tcsetattr( fd, TCSANOW, &ios );
2096 }
2097 }
2098
2099 if (fd < 0) {
2100 perror ("opening AT interface. retrying...");
2101 sleep(10);
2102 /* never returns */
2103 }
2104 }
2105
2106 s_closed = 0;
2107 ret = at_open(fd, onUnsolicited);
2108
2109 if (ret < 0) {
2110 LOGE ("AT error %d on at_open\n", ret);
2111 return 0;
2112 }
2113
2114 RIL_requestTimedCallback(initializeCallback, NULL, &TIMEVAL_0);
2115
2116 // Give initializeCallback a chance to dispatched, since
2117 // we don't presently have a cancellation mechanism
2118 sleep(1);
2119
2120 waitForClose();
2121 LOGI("Re-opening after close");
2122 }
2123}
2124
2125#ifdef RIL_SHLIB
2126
2127pthread_t s_tid_mainloop;
2128
2129const RIL_RadioFunctions *RIL_Init(const struct RIL_Env *env, int argc, char **argv)
2130{
2131 int ret;
2132 int fd = -1;
2133 int opt;
2134 pthread_attr_t attr;
2135
2136 s_rilenv = env;
2137
2138 while ( -1 != (opt = getopt(argc, argv, "p:d:s:"))) {
2139 switch (opt) {
2140 case 'p':
2141 s_port = atoi(optarg);
2142 if (s_port == 0) {
2143 usage(argv[0]);
2144 return NULL;
2145 }
2146 LOGI("Opening loopback port %d\n", s_port);
2147 break;
2148
2149 case 'd':
2150 s_device_path = optarg;
2151 LOGI("Opening tty device %s\n", s_device_path);
2152 break;
2153
2154 case 's':
2155 s_device_path = optarg;
2156 s_device_socket = 1;
2157 LOGI("Opening socket %s\n", s_device_path);
2158 break;
2159
2160 default:
2161 usage(argv[0]);
2162 return NULL;
2163 }
2164 }
2165
2166 if (s_port < 0 && s_device_path == NULL) {
2167 usage(argv[0]);
2168 return NULL;
2169 }
2170
2171 pthread_attr_init (&attr);
2172 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2173 ret = pthread_create(&s_tid_mainloop, &attr, mainLoop, NULL);
2174
2175 return &s_callbacks;
2176}
2177#else /* RIL_SHLIB */
2178int main (int argc, char **argv)
2179{
2180 int ret;
2181 int fd = -1;
2182 int opt;
2183
2184 while ( -1 != (opt = getopt(argc, argv, "p:d:"))) {
2185 switch (opt) {
2186 case 'p':
2187 s_port = atoi(optarg);
2188 if (s_port == 0) {
2189 usage(argv[0]);
2190 }
2191 LOGI("Opening loopback port %d\n", s_port);
2192 break;
2193
2194 case 'd':
2195 s_device_path = optarg;
2196 LOGI("Opening tty device %s\n", s_device_path);
2197 break;
2198
2199 case 's':
2200 s_device_path = optarg;
2201 s_device_socket = 1;
2202 LOGI("Opening socket %s\n", s_device_path);
2203 break;
2204
2205 default:
2206 usage(argv[0]);
2207 }
2208 }
2209
2210 if (s_port < 0 && s_device_path == NULL) {
2211 usage(argv[0]);
2212 }
2213
2214 RIL_register(&s_callbacks);
2215
2216 mainLoop(NULL);
2217
2218 return 0;
2219}
2220
2221#endif /* RIL_SHLIB */