blob: 33f6851b69aa21b13c871a6ee567dab8c61e7c00 [file] [log] [blame]
codeworkxf1587a32012-08-03 23:32:29 +02001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "SapClient"
18
19#include <binder/Parcel.h>
20#include <telephony/ril.h>
21#include <cutils/record_stream.h>
22
23#include <unistd.h>
24#include <errno.h>
25#include <cutils/sockets.h>
26#include <netinet/in.h>
27#include <sys/types.h>
28#include <string.h>
29#include <fcntl.h>
30#include <utils/Log.h>
31#include <pthread.h>
32#include "secril-client-sap.h"
33#include <hardware_legacy/power.h> // For wakelock
34
35#define RIL_CLIENT_WAKE_LOCK "client-sap-interface"
36
37namespace android {
38
39//---------------------------------------------------------------------------
40// Defines
41//---------------------------------------------------------------------------
42#define DBG 0
43
44#define MULTI_CLIENT_SOCKET_NAME "Multiclient"
45
46#define MAX_COMMAND_BYTES (8 * 1024)
47#define REQ_POOL_SIZE 32
48#define TOKEN_POOL_SIZE 32
49
50// Constants for response types
51#define RESPONSE_SOLICITED 0
52#define RESPONSE_UNSOLICITED 1
53
54#define max(a, b) ((a) > (b) ? (a) : (b))
55
56#define REQ_OEM_HOOK_RAW RIL_REQUEST_OEM_HOOK_RAW
57
58//---------------------------------------------------------------------------
59// Type definitions
60//---------------------------------------------------------------------------
61typedef struct _ReqHistory {
62 int token; // token used for request
63 uint32_t id; // request ID
64} ReqHistory;
65
66typedef struct _ReqRespHandler {
67 uint32_t id; // request ID
68 RilOnComplete handler; // handler function
69} ReqRespHandler;
70
71typedef struct _UnsolHandler {
72 uint32_t id; // unsolicited response ID
73 RilOnUnsolicited handler; // handler function
74} UnsolHandler;
75
76typedef struct _RilClientPrv {
77 HRilClient parent;
78 uint8_t b_connect; // connected to server?
79 int sock; // socket
80 int pipefd[2];
81 fd_set sock_rfds; // for read with select()
82 RecordStream *p_rs;
83 uint32_t token_pool; // each bit in token_pool used for token.
84 // so, pool size is 32.
85 pthread_t tid_reader; // socket reader thread id
86 ReqHistory history[TOKEN_POOL_SIZE]; // request history
87 ReqRespHandler req_handlers[REQ_POOL_SIZE]; // request response handler list
88 UnsolHandler unsol_handlers[REQ_POOL_SIZE]; // unsolicited response handler list
89 RilOnError err_cb; // error callback
90 void *err_cb_data; // error callback data
91 uint8_t b_del_handler;
92} RilClientPrv;
93
94
95//---------------------------------------------------------------------------
96// Local static function prototypes
97//---------------------------------------------------------------------------
98static void * RxReaderFunc(void *param);
99static int processRxBuffer(RilClientPrv *prv, void *buffer, size_t buflen);
100static uint32_t AllocateToken(uint32_t *token_pool);
101static void FreeToken(uint32_t *token_pool, uint32_t token);
102static uint8_t IsValidToken(uint32_t *token_pool, uint32_t token);
103static int blockingWrite(int fd, const void *buffer, size_t len);
104static int RecordReqHistory(RilClientPrv *prv, int token, uint32_t id);
105static void ClearReqHistory(RilClientPrv *prv, int token);
106static RilOnComplete FindReqHandler(RilClientPrv *prv, int token, uint32_t *id);
107static RilOnUnsolicited FindUnsolHandler(RilClientPrv *prv, uint32_t id);
108static int SendOemRequestHookRaw(HRilClient client, int req_id, char *data, size_t len);
109
110/**
111 * @fn int RegisterUnsolicitedHandler(HRilClient client, uint32_t id, RilOnUnsolicited handler)
112 *
113 * @params client: Client handle.
114 * id: Unsolicited response ID to which handler is registered.
115 * handler: Unsolicited handler. NULL for deregistration.
116 *
117 * @return 0 on success or error code.
118 */
119extern "C"
120int RegisterUnsolicitedHandler(HRilClient client, uint32_t id, RilOnUnsolicited handler) {
121 RilClientPrv *client_prv;
122 int match_slot = -1;
123 int first_empty_slot = -1;
124 int i;
125
126 if (client == NULL || client->prv == NULL)
127 return RIL_CLIENT_ERR_INVAL;
128
129 client_prv = (RilClientPrv *)(client->prv);
130
131 for (i = 0; i < REQ_POOL_SIZE; i++) {
132 // Check if there is matched handler.
133 if (id == client_prv->unsol_handlers[i].id) {
134 match_slot = i;
135 }
136 // Find first empty handler slot.
137 if (first_empty_slot == -1 && client_prv->unsol_handlers[i].id == 0) {
138 first_empty_slot = i;
139 }
140 }
141
142 if (handler == NULL) { // Unregister.
143 if (match_slot >= 0) {
144 memset(&(client_prv->unsol_handlers[match_slot]), 0, sizeof(UnsolHandler));
145 return RIL_CLIENT_ERR_SUCCESS;
146 }
147 else {
148 return RIL_CLIENT_ERR_SUCCESS;
149 }
150 }
151 else {// Register.
152 if (match_slot >= 0) {
153 client_prv->unsol_handlers[match_slot].handler = handler; // Just update.
154 }
155 else if (first_empty_slot >= 0) {
156 client_prv->unsol_handlers[first_empty_slot].id = id;
157 client_prv->unsol_handlers[first_empty_slot].handler = handler;
158 }
159 else {
160 return RIL_CLIENT_ERR_RESOURCE;
161 }
162 }
163
164 return RIL_CLIENT_ERR_SUCCESS;
165}
166
167
168/**
169 * @fn int RegisterRequestCompleteHandler(HRilClient client, uint32_t id, RilOnComplete handler)
170 *
171 * @params client: Client handle.
172 * id: Request ID to which handler is registered.
173 * handler: Request complete handler. NULL for deregistration.
174 *
175 * @return 0 on success or error code.
176 */
177extern "C"
178int RegisterRequestCompleteHandler(HRilClient client, uint32_t id, RilOnComplete handler) {
179 RilClientPrv *client_prv;
180 int match_slot = -1;
181 int first_empty_slot = -1;
182 int i;
183
184 if (client == NULL || client->prv == NULL)
185 return RIL_CLIENT_ERR_INVAL;
186
187 client_prv = (RilClientPrv *)(client->prv);
188
189 for (i = 0; i < REQ_POOL_SIZE; i++) {
190 // Check if there is matched handler.
191 if (id == client_prv->req_handlers[i].id) {
192 match_slot = i;
193 }
194 // Find first empty handler slot.
195 if (first_empty_slot == -1 && client_prv->req_handlers[i].id == 0) {
196 first_empty_slot = i;
197 }
198 }
199
200 if (handler == NULL) { // Unregister.
201 if (match_slot >= 0) {
202 memset(&(client_prv->req_handlers[match_slot]), 0, sizeof(ReqRespHandler));
203 return RIL_CLIENT_ERR_SUCCESS;
204 }
205 else {
206 return RIL_CLIENT_ERR_SUCCESS;
207 }
208 }
209 else { // Register.
210 if (match_slot >= 0) {
211 client_prv->req_handlers[match_slot].handler = handler; // Just update.
212 }
213 else if (first_empty_slot >= 0) {
214 client_prv->req_handlers[first_empty_slot].id = id;
215 client_prv->req_handlers[first_empty_slot].handler = handler;
216 }
217 else {
218 return RIL_CLIENT_ERR_RESOURCE;
219 }
220 }
221
222 return RIL_CLIENT_ERR_SUCCESS;
223}
224
225
226/**
227 * @fn int RegisterErrorCallback(HRilClient client, RilOnError cb, void *data)
228 *
229 * @params client: Client handle.
230 * cb: Error callback. NULL for unregistration.
231 * data: Callback data.
232 *
233 * @return 0 for success or error code.
234 */
235extern "C"
236int RegisterErrorCallback(HRilClient client, RilOnError cb, void *data) {
237 RilClientPrv *client_prv;
238
239 if (client == NULL || client->prv == NULL)
240 return RIL_CLIENT_ERR_INVAL;
241
242 client_prv = (RilClientPrv *)(client->prv);
243
244 client_prv->err_cb = cb;
245 client_prv->err_cb_data = data;
246
247 return RIL_CLIENT_ERR_SUCCESS;
248}
249
250
251/**
252 * @fn HRilClient OpenClient_RILD(void)
253 *
254 * @params None.
255 *
256 * @return Client handle, NULL on error.
257 */
258extern "C"
259HRilClient OpenClient_RILD(void) {
260 HRilClient client = (HRilClient)malloc(sizeof(struct RilClient));
261 if (client == NULL)
262 return NULL;
263
264 client->prv = (RilClientPrv *)malloc(sizeof(RilClientPrv));
265 if (client->prv == NULL) {
266 free(client);
267 return NULL;
268 }
269
270 memset(client->prv, 0, sizeof(RilClientPrv));
271
272 ((RilClientPrv *)(client->prv))->parent = client;
273 ((RilClientPrv *)(client->prv))->sock = -1;
274
275 return client;
276}
277
278
279/**
280 * @fn int Connect_RILD(void)
281 *
282 * @params client: Client handle.
283 *
284 * @return 0, or error code.
285 */
286extern "C"
287int Connect_RILD(HRilClient client) {
288 RilClientPrv *client_prv;
289
290 if (client == NULL || client->prv == NULL) {
291 ALOGE("%s: Invalid client %p", __FUNCTION__, client);
292 return RIL_CLIENT_ERR_INVAL;
293 }
294
295 client_prv = (RilClientPrv *)(client->prv);
296
297 // Open client socket and connect to server.
298 client_prv->sock = socket_local_client(MULTI_CLIENT_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM );
299
300 if (client_prv->sock < 0) {
301 ALOGE("%s: Connecting failed. %s(%d)", __FUNCTION__, strerror(errno), errno);
302 return RIL_CLIENT_ERR_CONNECT;
303 }
304
305 client_prv->b_connect = 1;
306
307 if (fcntl(client_prv->sock, F_SETFL, O_NONBLOCK) < 0) {
308 close(client_prv->sock);
309 return RIL_CLIENT_ERR_IO;
310 }
311
312 client_prv->p_rs = record_stream_new(client_prv->sock, MAX_COMMAND_BYTES);
313
314 if (pipe(client_prv->pipefd) < 0) {
315 close(client_prv->sock);
316 ALOGE("%s: Creating command pipe failed. %s(%d)", __FUNCTION__, strerror(errno), errno);
317 return RIL_CLIENT_ERR_IO;
318 }
319
320 if (fcntl(client_prv->pipefd[0], F_SETFL, O_NONBLOCK) < 0) {
321 close(client_prv->sock);
322 close(client_prv->pipefd[0]);
323 close(client_prv->pipefd[1]);
324 return RIL_CLIENT_ERR_IO;
325 }
326
327 // Start socket read thread.
328 if (pthread_create(&(client_prv->tid_reader), NULL, RxReaderFunc, (void *)client_prv) != 0) {
329 close(client_prv->sock);
330 close(client_prv->pipefd[0]);
331 close(client_prv->pipefd[1]);
332
333 memset(client_prv, 0, sizeof(RilClientPrv));
334 client_prv->sock = -1;
335 ALOGE("%s: Can't create Reader thread. %s(%d)", __FUNCTION__, strerror(errno), errno);
336 return RIL_CLIENT_ERR_CONNECT;
337 }
338
339 return RIL_CLIENT_ERR_SUCCESS;
340}
341
342/**
343 * @fn int Disconnect_RILD(HRilClient client)
344 *
345 * @params client: Client handle.
346 *
347 * @return 0 on success, or error code.
348 */
349extern "C"
350int Disconnect_RILD(HRilClient client) {
351 RilClientPrv *client_prv;
352 int ret = 0;
353
354 if (client == NULL || client->prv == NULL) {
355 ALOGE("%s: invalid client %p", __FUNCTION__, client);
356 return RIL_CLIENT_ERR_INVAL;
357 }
358
359 client_prv = (RilClientPrv *)(client->prv);
360
361 if (client_prv->sock == -1)
362 return RIL_CLIENT_ERR_SUCCESS;
363
364 ALOGD("[*] %s(): sock=%d\n", __FUNCTION__, client_prv->sock);
365
366 if (client_prv->sock > 0) {
367 do {
368 ret = write(client_prv->pipefd[1], "close", strlen("close"));
369 } while (ret < 0 && errno == EINTR);
370 }
371
372 client_prv->b_connect = 0;
373
374 pthread_join(client_prv->tid_reader, NULL);
375
376 return RIL_CLIENT_ERR_SUCCESS;
377}
378
379
380/**
381 * @fn int CloseClient_RILD(HRilClient client)
382 *
383 * @params client: Client handle.
384 *
385 * @return 0 on success, or error code.
386 */
387extern "C"
388int CloseClient_RILD(HRilClient client) {
389 if (client == NULL || client->prv == NULL) {
390 ALOGE("%s: invalid client %p", __FUNCTION__, client);
391 return RIL_CLIENT_ERR_INVAL;
392 }
393
394 Disconnect_RILD(client);
395
396 free(client->prv);
397 free(client);
398
399 return RIL_CLIENT_ERR_SUCCESS;
400}
401
402/**
403 * @fn int InvokeOemRequestHookRaw(HRilClient client, char *data, size_t len)
404 *
405 * @params client: Client handle.
406 * data: Request data.
407 * len: Request data length.
408 *
409 * @return 0 for success or error code. On receiving RIL_CLIENT_ERR_AGAIN,
410 * caller should retry.
411 */
412extern "C"
413int InvokeOemRequestHookRaw(HRilClient client, char *data, size_t len) {
414 RilClientPrv *client_prv;
415
416 if (client == NULL || client->prv == NULL) {
417 ALOGE("%s: Invalid client %p", __FUNCTION__, client);
418 return RIL_CLIENT_ERR_INVAL;
419 }
420
421 client_prv = (RilClientPrv *)(client->prv);
422
423 if (client_prv->sock < 0 ) {
424 ALOGE("%s: Not connected.", __FUNCTION__);
425 return RIL_CLIENT_ERR_CONNECT;
426 }
427
428 return SendOemRequestHookRaw(client, REQ_OEM_HOOK_RAW, data, len);
429}
430
431
432static int SendOemRequestHookRaw(HRilClient client, int req_id, char *data, size_t len) {
433 int token = 0;
434 int ret = 0;
435 uint32_t header = 0;
436 android::Parcel p;
437 RilClientPrv *client_prv;
438 int maxfd = -1;
439
440 client_prv = (RilClientPrv *)(client->prv);
441
442 // Allocate a token.
443 token = AllocateToken(&(client_prv->token_pool));
444 if (token == 0) {
445 ALOGE("%s: No token.", __FUNCTION__);
446 return RIL_CLIENT_ERR_AGAIN;
447 }
448
449 // Record token for the request sent.
450 if (RecordReqHistory(client_prv, token, req_id) != RIL_CLIENT_ERR_SUCCESS) {
451 goto error;
452 }
453
454 // Make OEM request data.
455 p.writeInt32(RIL_REQUEST_OEM_HOOK_RAW);
456 p.writeInt32(token);
457 p.writeInt32(len);
458 p.write((void *)data, len);
459
460 // DO TX: header(size).
461 header = htonl(p.dataSize());
462
463 if (DBG) ALOGD("%s(): token = %d\n", __FUNCTION__, token);
464
465 ret = blockingWrite(client_prv->sock, (void *)&header, sizeof(header));
466 if (ret < 0) {
467 ALOGE("%s: send request header failed. (%d)", __FUNCTION__, ret);
468 goto error;
469 }
470
471 // Do TX: response data.
472 ret = blockingWrite(client_prv->sock, p.data(), p.dataSize());
473 if (ret < 0) {
474 ALOGE("%s: send request data failed. (%d)", __FUNCTION__, ret);
475 goto error;
476 }
477
478 return RIL_CLIENT_ERR_SUCCESS;
479
480error:
481 FreeToken(&(client_prv->token_pool), token);
482 ClearReqHistory(client_prv, token);
483
484 return RIL_CLIENT_ERR_UNKNOWN;
485}
486
487static void * RxReaderFunc(void *param) {
488 RilClientPrv *client_prv = (RilClientPrv *)param;
489 int maxfd = 0;
490 int token = 0;
491 void *p_record = NULL;
492 size_t recordlen = 0;
493 int ret = 0;
494 int n;
495
496 if (client_prv == NULL)
497 return NULL;
498
499 maxfd = max(client_prv->sock, client_prv->pipefd[0]) + 1;
500
501 ALOGD("[*] %s() b_connect=%d, maxfd=%d\n", __FUNCTION__, client_prv->b_connect, maxfd);
502 while (client_prv->b_connect) {
503 FD_ZERO(&(client_prv->sock_rfds));
504
505 FD_SET(client_prv->sock, &(client_prv->sock_rfds));
506 FD_SET(client_prv->pipefd[0], &(client_prv->sock_rfds));
507
508 if (DBG) ALOGD("[*] %s() b_connect=%d\n", __FUNCTION__, client_prv->b_connect);
509 if (select(maxfd, &(client_prv->sock_rfds), NULL, NULL, NULL) > 0) {
510 if (FD_ISSET(client_prv->sock, &(client_prv->sock_rfds))) {
511 // Read incoming data
512 for (;;) {
513 // loop until EAGAIN/EINTR, end of stream, or other error
514 ret = record_stream_get_next(client_prv->p_rs, &p_record, &recordlen);
515 if (ret == 0 && p_record == NULL) { // end-of-stream
516 break;
517 }
518 else if (ret < 0) {
519 break;
520 }
521 else if (ret == 0) { // && p_record != NULL
522 n = processRxBuffer(client_prv, p_record, recordlen);
523 if (n != RIL_CLIENT_ERR_SUCCESS) {
524 ALOGE("%s: processRXBuffer returns %d", __FUNCTION__, n);
525 }
526 }
527 else {
528 ALOGD("[*] %s()\n", __FUNCTION__);
529 }
530 }
531
532 if (ret == 0 || !(errno == EAGAIN || errno == EINTR)) {
533 // fatal error or end-of-stream
534 if (client_prv->sock > 0) {
535 close(client_prv->sock);
536 client_prv->sock = -1;
537 client_prv->b_connect = 0;
538 }
539
540 if (client_prv->p_rs)
541 record_stream_free(client_prv->p_rs);
542
543 // EOS
544 if (client_prv->err_cb) {
545 client_prv->err_cb(client_prv->err_cb_data, RIL_CLIENT_ERR_CONNECT);
546 return NULL;
547 }
548
549 break;
550 }
551 }
552 if (FD_ISSET(client_prv->pipefd[0], &(client_prv->sock_rfds))) {
553 char end_cmd[10];
554
555 if (DBG) ALOGD("%s(): close\n", __FUNCTION__);
556
557 if (read(client_prv->pipefd[0], end_cmd, sizeof(end_cmd)) > 0) {
558 close(client_prv->sock);
559 close(client_prv->pipefd[0]);
560 close(client_prv->pipefd[1]);
561
562 client_prv->sock = -1;
563 client_prv->b_connect = 0;
564 }
565 }
566 }
567 }
568
569 return NULL;
570}
571
572
573static int processUnsolicited(RilClientPrv *prv, Parcel &p) {
574 int32_t resp_id, len;
575 status_t status;
576 const void *data = NULL;
577 RilOnUnsolicited unsol_func = NULL;
578
579 status = p.readInt32(&resp_id);
580 if (status != NO_ERROR) {
581 ALOGE("%s: read resp_id failed.", __FUNCTION__);
582 return RIL_CLIENT_ERR_IO;
583 }
584
585 status = p.readInt32(&len);
586 if (status != NO_ERROR) {
587 //LOGE("%s: read length failed. assume zero length.", __FUNCTION__);
588 len = 0;
589 }
590
591 ALOGD("%s(): resp_id (%d), len(%d)\n", __FUNCTION__, resp_id, len);
592
593 if (len)
594 data = p.readInplace(len);
595
596 // Find unsolicited response handler.
597 unsol_func = FindUnsolHandler(prv, (uint32_t)resp_id);
598 if (unsol_func) {
599 unsol_func(prv->parent, data, len);
600 }
601
602 return RIL_CLIENT_ERR_SUCCESS;
603}
604
605
606static int processSolicited(RilClientPrv *prv, Parcel &p) {
607 int32_t token, err, len;
608 status_t status;
609 const void *data = NULL;
610 RilOnComplete req_func = NULL;
611 int ret = RIL_CLIENT_ERR_SUCCESS;
612 uint32_t req_id = 0;
613
614 if (DBG) ALOGD("%s()", __FUNCTION__);
615
616 status = p.readInt32(&token);
617 if (status != NO_ERROR) {
618 ALOGE("%s: Read token fail. Status %d\n", __FUNCTION__, status);
619 return RIL_CLIENT_ERR_IO;
620 }
621
622 if (IsValidToken(&(prv->token_pool), token) == 0) {
623 ALOGE("%s: Invalid Token", __FUNCTION__);
624 return RIL_CLIENT_ERR_INVAL; // Invalid token.
625 }
626
627 status = p.readInt32(&err);
628 if (status != NO_ERROR) {
629 ALOGE("%s: Read err fail. Status %d\n", __FUNCTION__, status);
630 ret = RIL_CLIENT_ERR_IO;
631 goto error;
632 }
633
634 // Don't go further for error response.
635 if (err != RIL_CLIENT_ERR_SUCCESS) {
636 ALOGE("%s: Error %d\n", __FUNCTION__, err);
637 if (prv->err_cb)
638 prv->err_cb(prv->err_cb_data, err);
639 ret = RIL_CLIENT_ERR_SUCCESS;
640 goto error;
641 }
642
643 status = p.readInt32(&len);
644 if (status != NO_ERROR) {
645 /* no length field */
646 len = 0;
647 }
648
649 if (len)
650 data = p.readInplace(len);
651
652 // Find request handler for the token.
653 // First, FindReqHandler() searches request history with the token
654 // and finds out a request ID. Then, it search request handler table
655 // with the request ID.
656 req_func = FindReqHandler(prv, token, &req_id);
657 if (req_func)
658 {
659 if (DBG) ALOGD("[*] Call handler");
660 req_func(prv->parent, data, len);
661
662 if(prv->b_del_handler) {
663 prv->b_del_handler = 0;
664 RegisterRequestCompleteHandler(prv->parent, req_id, NULL);
665 }
666 } else {
667 if (DBG) ALOGD("%s: No handler for token %d\n", __FUNCTION__, token);
668 }
669
670error:
671 FreeToken(&(prv->token_pool), token);
672 ClearReqHistory(prv, token);
673 return ret;
674}
675
676
677static int processRxBuffer(RilClientPrv *prv, void *buffer, size_t buflen) {
678 Parcel p;
679 int32_t response_type;
680 status_t status;
681 int ret = RIL_CLIENT_ERR_SUCCESS;
682
683 acquire_wake_lock(PARTIAL_WAKE_LOCK, RIL_CLIENT_WAKE_LOCK);
684
685 p.setData((uint8_t *)buffer, buflen);
686
687 status = p.readInt32(&response_type);
688 if (DBG) ALOGD("%s: status %d response_type %d", __FUNCTION__, status, response_type);
689
690 if (status != NO_ERROR) {
691 ret = RIL_CLIENT_ERR_IO;
692 goto EXIT;
693 }
694
695 // FOr unsolicited response.
696 if (response_type == RESPONSE_UNSOLICITED) {
697 ret = processUnsolicited(prv, p);
698 }
699 // For solicited response.
700 else if (response_type == RESPONSE_SOLICITED) {
701 ret = processSolicited(prv, p);
702 if (ret != RIL_CLIENT_ERR_SUCCESS && prv->err_cb) {
703 prv->err_cb(prv->err_cb_data, ret);
704 }
705 }
706 else {
707 ret = RIL_CLIENT_ERR_INVAL;
708 }
709
710EXIT:
711 release_wake_lock(RIL_CLIENT_WAKE_LOCK);
712 return ret;
713}
714
715
716static uint32_t AllocateToken(uint32_t *token_pool) {
717 int i;
718
719 // Token pool is full.
720 if (*token_pool == 0xFFFFFFFF)
721 return 0;
722
723 for (i = 0; i < 32; i++) {
724 uint32_t new_token = 0x00000001 << i;
725
726 if ((*token_pool & new_token) == 0) {
727 *token_pool |= new_token;
728 return new_token;
729 }
730 }
731
732 return 0;
733}
734
735
736static void FreeToken(uint32_t *token_pool, uint32_t token) {
737 *token_pool &= ~token;
738}
739
740
741static uint8_t IsValidToken(uint32_t *token_pool, uint32_t token) {
742 if (token == 0)
743 return 0;
744
745 if ((*token_pool & token) == token)
746 return 1;
747 else
748 return 0;
749}
750
751
752static int RecordReqHistory(RilClientPrv *prv, int token, uint32_t id) {
753 int i = 0;
754
755 if (DBG) ALOGD("[*] %s(): token(%d), ID(%d)\n", __FUNCTION__, token, id);
756 for (i = 0; i < TOKEN_POOL_SIZE; i++) {
757 if (prv->history[i].token == 0) {
758 prv->history[i].token = token;
759 prv->history[i].id = id;
760
761 if (DBG) ALOGD("[*] %s(): token(%d), ID(%d)\n", __FUNCTION__, token, id);
762
763 return RIL_CLIENT_ERR_SUCCESS;
764 }
765 }
766
767 ALOGE("%s: No free record for token %d", __FUNCTION__, token);
768
769 return RIL_CLIENT_ERR_RESOURCE;
770}
771
772static void ClearReqHistory(RilClientPrv *prv, int token) {
773 int i = 0;
774
775 if (DBG) ALOGD("[*] %s(): token(%d)\n", __FUNCTION__, token);
776 for (i = 0; i < TOKEN_POOL_SIZE; i++) {
777 if (prv->history[i].token == token) {
778 memset(&(prv->history[i]), 0, sizeof(ReqHistory));
779 break;
780 }
781 }
782}
783
784
785static RilOnUnsolicited FindUnsolHandler(RilClientPrv *prv, uint32_t id) {
786 int i;
787
788 // Search unsolicited handler table.
789 for (i = 0; i < REQ_POOL_SIZE; i++) {
790 if (prv->unsol_handlers[i].id == id)
791 return prv->unsol_handlers[i].handler;
792 }
793
794 return (RilOnUnsolicited)NULL;
795}
796
797
798static RilOnComplete FindReqHandler(RilClientPrv *prv, int token, uint32_t *id) {
799 int i = 0;
800 int j = 0;
801
802 if (DBG) ALOGD("[*] %s(): token(%d)\n", __FUNCTION__, token);
803
804 // Search request history.
805 for (i = 0; i < TOKEN_POOL_SIZE; i++) {
806 ALOGD("[*] %s(): history_token(%d)\n", __FUNCTION__, prv->history[i].token);
807 if (prv->history[i].token == token) {
808 // Search request handler with request ID found.
809 for (j = 0; j < REQ_POOL_SIZE; j++) {
810 ALOGD("[*] %s(): token(%d), req_id(%d), history_id(%d)\n", __FUNCTION__, token, prv->history[i].id, prv->history[i].id);
811 if (prv->req_handlers[j].id == prv->history[i].id) {
812 *id = prv->req_handlers[j].id;
813 return prv->req_handlers[j].handler;
814 }
815 }
816 }
817 }
818
819 return NULL;
820}
821
822
823static int blockingWrite(int fd, const void *buffer, size_t len) {
824 size_t writeOffset = 0;
825 const uint8_t *toWrite;
826 ssize_t written = 0;
827
828 if (buffer == NULL)
829 return -1;
830
831 toWrite = (const uint8_t *)buffer;
832
833 while (writeOffset < len) {
834 do
835 {
836 written = write(fd, toWrite + writeOffset, len - writeOffset);
837 } while (written < 0 && errno == EINTR);
838
839 if (written >= 0) {
840 writeOffset += written;
841 }
842 else {
843 ALOGE ("RIL Response: unexpected error on write errno:%d", errno);
844 close(fd);
845 return -1;
846 }
847 }
848
849 return 0;
850}
851
852} // namespace android
853
854// end of file
855