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