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