2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define __STDC_LIMIT_MACROS
20 #include "telephony/ril.h"
21 #include "RilSapSocket.h"
22 #include "pb_decode.h"
23 #include "pb_encode.h"
24 #define LOG_TAG "RIL_UIM_SOCKET"
25 #include <utils/Log.h>
26 #include <arpa/inet.h>
29 static RilSapSocket::RilSapSocketList *head = NULL;
31 void ril_sap_on_request_complete (
32 RIL_Token t, RIL_Errno e,
33 void *response, size_t responselen
36 void ril_sap_on_unsolicited_response (
37 int unsolResponse, const void *data,
41 RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
42 const struct timeval *relativeTime);
44 struct RIL_Env RilSapSocket::uimRilEnv = {
45 .OnRequestComplete = RilSapSocket::sOnRequestComplete,
46 .OnUnsolicitedResponse = RilSapSocket::sOnUnsolicitedResponse,
47 .RequestTimedCallback = RIL_requestTimedCallback
50 void RilSapSocket::sOnRequestComplete (RIL_Token t,
54 RilSapSocket *sap_socket;
55 SapSocketRequest *request = (SapSocketRequest*) t;
57 RLOGD("Socket id:%d", request->socketId);
59 sap_socket = getSocketById(request->socketId);
62 sap_socket->onRequestComplete(t,e,response,responselen);
64 RLOGE("Invalid socket id");
65 if (request->curr->payload) {
66 free(request->curr->payload);
73 #if defined(ANDROID_MULTI_SIM)
74 void RilSapSocket::sOnUnsolicitedResponse(int unsolResponse,
77 RIL_SOCKET_ID socketId) {
78 RilSapSocket *sap_socket = getSocketById(socketId);
80 sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
84 void RilSapSocket::sOnUnsolicitedResponse(int unsolResponse,
87 RilSapSocket *sap_socket = getSocketById(RIL_SOCKET_1);
88 sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
92 void RilSapSocket::printList() {
93 RilSapSocketList *current = head;
94 RLOGD("Printing socket list");
95 while(NULL != current) {
96 RLOGD("SocketName:%s",current->socket->name);
97 RLOGD("Socket id:%d",current->socket->id);
98 current = current->next;
102 RilSapSocket *RilSapSocket::getSocketById(RIL_SOCKET_ID socketId) {
103 RilSapSocket *sap_socket;
104 RilSapSocketList *current = head;
106 RLOGD("Entered getSocketById");
109 while(NULL != current) {
110 if(socketId == current->socket->id) {
111 sap_socket = current->socket;
114 current = current->next;
119 void RilSapSocket::initSapSocket(const char *socketName,
120 RIL_RadioFunctions *uimFuncs) {
122 if (strcmp(socketName, "sap_uim_socket1") == 0) {
123 if(!SocketExists(socketName)) {
124 addSocketToList(socketName, RIL_SOCKET_1, uimFuncs);
129 if (strcmp(socketName, "sap_uim_socket2") == 0) {
130 if(!SocketExists(socketName)) {
131 addSocketToList(socketName, RIL_SOCKET_2, uimFuncs);
137 if (strcmp(socketName, "sap_uim_socket3") == 0) {
138 if(!SocketExists(socketName)) {
139 addSocketToList(socketName, RIL_SOCKET_3, uimFuncs);
145 if (strcmp(socketName, "sap_uim_socket4") == 0) {
146 if(!SocketExists(socketName)) {
147 addSocketToList(socketName, RIL_SOCKET_4, uimFuncs);
153 void RilSapSocket::addSocketToList(const char *socketName, RIL_SOCKET_ID socketid,
154 RIL_RadioFunctions *uimFuncs) {
155 RilSapSocket* socket = NULL;
156 RilSapSocketList *current;
158 if(!SocketExists(socketName)) {
159 socket = new RilSapSocket(socketName, socketid, uimFuncs);
160 RilSapSocketList* listItem = (RilSapSocketList*)malloc(sizeof(RilSapSocketList));
162 RLOGE("addSocketToList: OOM");
165 listItem->socket = socket;
166 listItem->next = NULL;
168 RLOGD("Adding socket with id: %d", socket->id);
176 while(NULL != current->next) {
177 current = current->next;
179 current->next = listItem;
181 socket->socketInit();
185 bool RilSapSocket::SocketExists(const char *socketName) {
186 RilSapSocketList* current = head;
188 while(NULL != current) {
189 if(strcmp(current->socket->name, socketName) == 0) {
192 current = current->next;
197 void* RilSapSocket::processRequestsLoop(void) {
198 RLOGI("UIM_SOCKET:Request loop started");
201 SapSocketRequest *req = dispatchQueue.dequeue();
203 RLOGI("New request from the dispatch Queue");
206 dispatchRequest(req->curr);
209 RLOGE("Fetched null buffer from queue!");
215 RilSapSocket::RilSapSocket(const char *socketName,
216 RIL_SOCKET_ID socketId,
217 RIL_RadioFunctions *inputUimFuncs):
218 RilSocket(socketName, socketId) {
220 uimFuncs = inputUimFuncs;
224 #define BYTES_PER_LINE 16
226 #define NIBBLE_TO_HEX(n) ({ \
227 uint8_t __n = (uint8_t) n & 0x0f; \
228 __nibble >= 10 ? 'A' + __n - 10: '0' + __n; \
231 #define HEX_HIGH(b) ({ \
232 uint8_t __b = (uint8_t) b; \
233 uint8_t __nibble = (__b >> 4) & 0x0f; \
234 NIBBLE_TO_HEX(__nibble); \
237 #define HEX_LOW(b) ({ \
238 uint8_t __b = (uint8_t) b; \
239 uint8_t __nibble = __b & 0x0f; \
240 NIBBLE_TO_HEX(__nibble); \
243 void log_hex(const char *who, const uint8_t *buffer, int length) {
247 int dest_len = sizeof(out);
251 dest += sprintf(out, "%8.8s [%8.8x] ", who, source);
252 for(; source < length && dest_len - dest > 3 && per_line < BYTES_PER_LINE; source++,
254 out[dest++] = HEX_HIGH(buffer[source]);
255 out[dest++] = HEX_LOW(buffer[source]);
258 if (dest < dest_len && (per_line == BYTES_PER_LINE || source >= length)) {
264 } while(source < length && dest < dest_len);
267 void RilSapSocket::dispatchRequest(MsgHeader *req) {
268 // SapSocketRequest will be deallocated in onRequestComplete()
269 SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest));
271 RLOGE("dispatchRequest: OOM");
272 // Free MsgHeader allocated in pushRecord()
276 currRequest->token = req->token;
277 currRequest->curr = req;
278 currRequest->p_next = NULL;
279 currRequest->socketId = id;
281 pendingResponseQueue.enqueue(currRequest);
284 RLOGI("[%d] > SAP REQUEST type: %d. id: %d. error: %d",
290 #if defined(ANDROID_MULTI_SIM)
291 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest, id);
293 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest);
298 void RilSapSocket::onRequestComplete(RIL_Token t, RIL_Errno e, void *response,
299 size_t response_len) {
300 SapSocketRequest* request= (SapSocketRequest*)t;
301 MsgHeader *hdr = request->curr;
304 rsp.token = request->curr->token;
305 rsp.type = MsgType_RESPONSE;
306 rsp.id = request->curr->id;
307 rsp.error = (Error)e;
308 rsp.payload = (pb_bytes_array_t *)calloc(1, sizeof(pb_bytes_array_t) + response_len);
310 RLOGE("onRequestComplete: OOM");
312 if (response && response_len > 0) {
313 memcpy(rsp.payload->bytes, response, response_len);
314 rsp.payload->size = response_len;
316 rsp.payload->size = 0;
319 RLOGE("Token:%d, MessageId:%d", hdr->token, hdr->id);
325 // Deallocate SapSocketRequest
326 if(!pendingResponseQueue.checkAndDequeue(hdr->id, hdr->token)) {
327 RLOGE("Token:%d, MessageId:%d", hdr->token, hdr->id);
328 RLOGE ("RilSapSocket::onRequestComplete: invalid Token or Message Id");
331 // Deallocate MsgHeader
335 void RilSapSocket::sendResponse(MsgHeader* hdr) {
336 size_t encoded_size = 0;
337 uint32_t written_size;
338 size_t buffer_size = 0;
339 pb_ostream_t ostream;
340 bool success = false;
342 pthread_mutex_lock(&write_lock);
344 if ((success = pb_get_encoded_size(&encoded_size, MsgHeader_fields,
345 hdr)) && encoded_size <= INT32_MAX && commandFd != -1) {
346 buffer_size = encoded_size + sizeof(uint32_t);
347 uint8_t* buffer = (uint8_t*)malloc(buffer_size);
349 RLOGE("sendResponse: OOM");
350 pthread_mutex_unlock(&write_lock);
353 written_size = htonl((uint32_t) encoded_size);
354 ostream = pb_ostream_from_buffer(buffer, buffer_size);
355 pb_write(&ostream, (uint8_t *)&written_size, sizeof(written_size));
356 success = pb_encode(&ostream, MsgHeader_fields, hdr);
359 RLOGD("Size: %d (0x%x) Size as written: 0x%x", encoded_size, encoded_size,
361 log_hex("onRequestComplete", &buffer[sizeof(written_size)], encoded_size);
362 RLOGI("[%d] < SAP RESPONSE type: %d. id: %d. error: %d",
363 hdr->token, hdr->type, hdr->id,hdr->error );
365 if ( 0 != blockingWrite_helper(commandFd, buffer, buffer_size)) {
366 RLOGE("Error %d while writing to fd", errno);
368 RLOGD("Write successful");
371 RLOGE("Error while encoding response of type %d id %d buffer_size: %d: %s.",
372 hdr->type, hdr->id, buffer_size, PB_GET_ERROR(&ostream));
376 RLOGE("Not sending response type %d: encoded_size: %u. commandFd: %d. encoded size result: %d",
377 hdr->type, encoded_size, commandFd, success);
380 pthread_mutex_unlock(&write_lock);
383 void RilSapSocket::onUnsolicitedResponse(int unsolResponse, void *data, size_t datalen) {
384 if (data && datalen > 0) {
385 pb_bytes_array_t *payload = (pb_bytes_array_t *)calloc(1,
386 sizeof(pb_bytes_array_t) + datalen);
388 RLOGE("onUnsolicitedResponse: OOM");
391 memcpy(payload->bytes, data, datalen);
392 payload->size = datalen;
394 rsp.payload = payload;
395 rsp.type = MsgType_UNSOL_RESPONSE;
396 rsp.id = (MsgId)unsolResponse;
397 rsp.error = Error_RIL_E_SUCCESS;
403 void RilSapSocket::pushRecord(void *p_record, size_t recordlen) {
404 pb_istream_t stream = pb_istream_from_buffer((uint8_t *)p_record, recordlen);
405 // MsgHeader will be deallocated in onRequestComplete()
406 MsgHeader *reqHeader = (MsgHeader *)malloc(sizeof (MsgHeader));
408 RLOGE("pushRecord: OOM");
411 memset(reqHeader, 0, sizeof(MsgHeader));
413 log_hex("BtSapTest-Payload", (const uint8_t*)p_record, recordlen);
415 if (!pb_decode(&stream, MsgHeader_fields, reqHeader) ) {
416 RLOGE("Error decoding protobuf buffer : %s", PB_GET_ERROR(&stream));
419 // SapSocketRequest will be deallocated in processRequestsLoop()
420 SapSocketRequest *recv = (SapSocketRequest*)malloc(sizeof(SapSocketRequest));
422 RLOGE("pushRecord: OOM");
426 recv->token = reqHeader->token;
427 recv->curr = reqHeader;
430 dispatchQueue.enqueue(recv);
434 void RilSapSocket::sendDisconnect() {
435 size_t encoded_size = 0;
436 uint32_t written_size;
437 size_t buffer_size = 0;
438 pb_ostream_t ostream;
439 bool success = false;
441 RIL_SIM_SAP_DISCONNECT_REQ disconnectReq;
443 if ((success = pb_get_encoded_size(&encoded_size, RIL_SIM_SAP_DISCONNECT_REQ_fields,
444 &disconnectReq)) && encoded_size <= INT32_MAX) {
445 buffer_size = encoded_size + sizeof(uint32_t);
446 uint8_t* buffer = (uint8_t*)malloc(buffer_size);
448 RLOGE("sendDisconnect: OOM");
451 written_size = htonl((uint32_t) encoded_size);
452 ostream = pb_ostream_from_buffer(buffer, buffer_size);
453 pb_write(&ostream, (uint8_t *)&written_size, sizeof(written_size));
454 success = pb_encode(&ostream, RIL_SIM_SAP_DISCONNECT_REQ_fields, buffer);
457 // Buffer will be deallocated in sOnRequestComplete()
458 pb_bytes_array_t *payload = (pb_bytes_array_t *)calloc(1,
459 sizeof(pb_bytes_array_t) + written_size);
461 RLOGE("sendDisconnect: OOM");
464 memcpy(payload->bytes, buffer, written_size);
465 payload->size = written_size;
466 // MsgHeader will be deallocated in sOnRequestComplete()
467 MsgHeader *hdr = (MsgHeader *)malloc(sizeof(MsgHeader));
469 RLOGE("sendDisconnect: OOM");
473 hdr->payload = payload;
474 hdr->type = MsgType_REQUEST;
475 hdr->id = MsgId_RIL_SIM_SAP_DISCONNECT;
476 hdr->error = Error_RIL_E_SUCCESS;
477 dispatchDisconnect(hdr);
480 RLOGE("Encode failed in send disconnect!");
486 void RilSapSocket::dispatchDisconnect(MsgHeader *req) {
487 // SapSocketRequest will be deallocated in sOnRequestComplete()
488 SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest));
490 RLOGE("dispatchDisconnect: OOM");
491 // Free memory allocated in sendDisconnect
496 currRequest->token = -1;
497 currRequest->curr = req;
498 currRequest->p_next = NULL;
499 currRequest->socketId = (RIL_SOCKET_ID)99;
501 RLOGD("Sending disconnect on command close!");
503 #if defined(ANDROID_MULTI_SIM)
504 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest, id);
506 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest);
510 void RilSapSocket::onCommandsSocketClosed() {
512 RLOGE("Socket command closed");