blob: 92b7a7b45d46a6c3f84200c9cd7f342e28857d1d [file] [log] [blame]
Dheeraj Shetty27976c42014-07-02 21:27:57 +02001/*
2* Copyright (C) 2014 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 __STDC_LIMIT_MACROS
18#include <stdint.h>
19#define RIL_SHLIB
20#include "telephony/ril.h"
21#include "RilSapSocket.h"
22#include "pb_decode.h"
23#include "pb_encode.h"
Sanket Padawef0c8ca72016-06-30 15:01:08 -070024#undef LOG_TAG
Dheeraj Shetty27976c42014-07-02 21:27:57 +020025#define LOG_TAG "RIL_UIM_SOCKET"
26#include <utils/Log.h>
27#include <arpa/inet.h>
Vinit Deshpande1b1ec2d2015-04-15 13:31:05 -070028#include <errno.h>
Amit Mahajan2056e962016-11-29 16:48:54 -080029#include <sap_service.h>
Dheeraj Shetty27976c42014-07-02 21:27:57 +020030
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -070031static RilSapSocket::RilSapSocketList *head = NULL;
Dheeraj Shetty27976c42014-07-02 21:27:57 +020032
Dheeraj Shetty27976c42014-07-02 21:27:57 +020033extern "C" void
34RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
35 const struct timeval *relativeTime);
36
37struct RIL_Env RilSapSocket::uimRilEnv = {
38 .OnRequestComplete = RilSapSocket::sOnRequestComplete,
39 .OnUnsolicitedResponse = RilSapSocket::sOnUnsolicitedResponse,
40 .RequestTimedCallback = RIL_requestTimedCallback
41};
42
43void RilSapSocket::sOnRequestComplete (RIL_Token t,
44 RIL_Errno e,
45 void *response,
46 size_t responselen) {
47 RilSapSocket *sap_socket;
48 SapSocketRequest *request = (SapSocketRequest*) t;
49
50 RLOGD("Socket id:%d", request->socketId);
51
52 sap_socket = getSocketById(request->socketId);
53
54 if (sap_socket) {
55 sap_socket->onRequestComplete(t,e,response,responselen);
56 } else {
57 RLOGE("Invalid socket id");
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -070058 if (request->curr->payload) {
59 free(request->curr->payload);
60 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +020061 free(request->curr);
62 free(request);
63 }
64}
65
66#if defined(ANDROID_MULTI_SIM)
67void RilSapSocket::sOnUnsolicitedResponse(int unsolResponse,
68 const void *data,
69 size_t datalen,
70 RIL_SOCKET_ID socketId) {
71 RilSapSocket *sap_socket = getSocketById(socketId);
72 if (sap_socket) {
73 sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
74 }
75}
76#else
77void RilSapSocket::sOnUnsolicitedResponse(int unsolResponse,
78 const void *data,
79 size_t datalen) {
80 RilSapSocket *sap_socket = getSocketById(RIL_SOCKET_1);
81 sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
82}
83#endif
84
85void RilSapSocket::printList() {
86 RilSapSocketList *current = head;
87 RLOGD("Printing socket list");
88 while(NULL != current) {
89 RLOGD("SocketName:%s",current->socket->name);
90 RLOGD("Socket id:%d",current->socket->id);
91 current = current->next;
92 }
93}
94
95RilSapSocket *RilSapSocket::getSocketById(RIL_SOCKET_ID socketId) {
96 RilSapSocket *sap_socket;
97 RilSapSocketList *current = head;
98
99 RLOGD("Entered getSocketById");
100 printList();
101
102 while(NULL != current) {
103 if(socketId == current->socket->id) {
104 sap_socket = current->socket;
105 return sap_socket;
106 }
107 current = current->next;
108 }
109 return NULL;
110}
111
112void RilSapSocket::initSapSocket(const char *socketName,
Nathan Harold14e42662017-07-31 19:44:46 -0700113 const RIL_RadioFunctions *uimFuncs) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200114
Amit Mahajan7955c432017-03-28 11:17:55 -0700115 if (strcmp(socketName, RIL1_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200116 if(!SocketExists(socketName)) {
117 addSocketToList(socketName, RIL_SOCKET_1, uimFuncs);
118 }
119 }
120
121#if (SIM_COUNT >= 2)
Amit Mahajan7955c432017-03-28 11:17:55 -0700122 if (strcmp(socketName, RIL2_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200123 if(!SocketExists(socketName)) {
124 addSocketToList(socketName, RIL_SOCKET_2, uimFuncs);
125 }
126 }
127#endif
128
129#if (SIM_COUNT >= 3)
Amit Mahajan7955c432017-03-28 11:17:55 -0700130 if (strcmp(socketName, RIL3_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200131 if(!SocketExists(socketName)) {
132 addSocketToList(socketName, RIL_SOCKET_3, uimFuncs);
133 }
134 }
135#endif
136
137#if (SIM_COUNT >= 4)
Amit Mahajan7955c432017-03-28 11:17:55 -0700138 if (strcmp(socketName, RIL4_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200139 if(!SocketExists(socketName)) {
140 addSocketToList(socketName, RIL_SOCKET_4, uimFuncs);
141 }
142 }
143#endif
144}
145
146void RilSapSocket::addSocketToList(const char *socketName, RIL_SOCKET_ID socketid,
Nathan Harold14e42662017-07-31 19:44:46 -0700147 const RIL_RadioFunctions *uimFuncs) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200148 RilSapSocket* socket = NULL;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200149 RilSapSocketList *current;
150
151 if(!SocketExists(socketName)) {
152 socket = new RilSapSocket(socketName, socketid, uimFuncs);
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700153 RilSapSocketList* listItem = (RilSapSocketList*)malloc(sizeof(RilSapSocketList));
154 if (!listItem) {
155 RLOGE("addSocketToList: OOM");
Yunlian Jiang0c049562016-11-08 18:19:11 -0800156 delete socket;
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700157 return;
158 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200159 listItem->socket = socket;
160 listItem->next = NULL;
161
162 RLOGD("Adding socket with id: %d", socket->id);
163
164 if(NULL == head) {
165 head = listItem;
166 head->next = NULL;
167 }
168 else {
169 current = head;
170 while(NULL != current->next) {
171 current = current->next;
172 }
173 current->next = listItem;
174 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200175 }
176}
177
178bool RilSapSocket::SocketExists(const char *socketName) {
179 RilSapSocketList* current = head;
180
181 while(NULL != current) {
182 if(strcmp(current->socket->name, socketName) == 0) {
183 return true;
184 }
185 current = current->next;
186 }
187 return false;
188}
189
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200190RilSapSocket::RilSapSocket(const char *socketName,
191 RIL_SOCKET_ID socketId,
Nathan Harold14e42662017-07-31 19:44:46 -0700192 const RIL_RadioFunctions *inputUimFuncs):
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200193 RilSocket(socketName, socketId) {
194 if (inputUimFuncs) {
195 uimFuncs = inputUimFuncs;
196 }
197}
198
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200199void RilSapSocket::dispatchRequest(MsgHeader *req) {
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700200 // SapSocketRequest will be deallocated in onRequestComplete()
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200201 SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest));
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700202 if (!currRequest) {
203 RLOGE("dispatchRequest: OOM");
204 // Free MsgHeader allocated in pushRecord()
205 free(req);
206 return;
207 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200208 currRequest->token = req->token;
209 currRequest->curr = req;
210 currRequest->p_next = NULL;
211 currRequest->socketId = id;
212
213 pendingResponseQueue.enqueue(currRequest);
214
215 if (uimFuncs) {
Amit Mahajan2056e962016-11-29 16:48:54 -0800216 RLOGI("RilSapSocket::dispatchRequest [%d] > SAP REQUEST type: %d. id: %d. error: %d, \
217 token 0x%p",
218 req->token,
219 req->type,
220 req->id,
221 req->error,
222 currRequest );
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200223
224#if defined(ANDROID_MULTI_SIM)
225 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest, id);
226#else
227 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest);
228#endif
229 }
230}
231
232void RilSapSocket::onRequestComplete(RIL_Token t, RIL_Errno e, void *response,
233 size_t response_len) {
234 SapSocketRequest* request= (SapSocketRequest*)t;
235 MsgHeader *hdr = request->curr;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200236
Sanket Padawe638a7232016-01-26 16:26:30 -0800237 MsgHeader rsp;
238 rsp.token = request->curr->token;
239 rsp.type = MsgType_RESPONSE;
240 rsp.id = request->curr->id;
241 rsp.error = (Error)e;
242 rsp.payload = (pb_bytes_array_t *)calloc(1, sizeof(pb_bytes_array_t) + response_len);
243 if (!rsp.payload) {
244 RLOGE("onRequestComplete: OOM");
245 } else {
246 if (response && response_len > 0) {
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700247 memcpy(rsp.payload->bytes, response, response_len);
248 rsp.payload->size = response_len;
Sanket Padawe638a7232016-01-26 16:26:30 -0800249 } else {
250 rsp.payload->size = 0;
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700251 }
Sanket Padawe638a7232016-01-26 16:26:30 -0800252
Amit Mahajan2056e962016-11-29 16:48:54 -0800253 RLOGE("RilSapSocket::onRequestComplete: Token:%d, MessageId:%d ril token 0x%p",
254 hdr->token, hdr->id, t);
Sanket Padawe638a7232016-01-26 16:26:30 -0800255
Amit Mahajan2056e962016-11-29 16:48:54 -0800256 sap::processResponse(&rsp, this);
Sanket Padawe638a7232016-01-26 16:26:30 -0800257 free(rsp.payload);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200258 }
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700259
260 // Deallocate SapSocketRequest
261 if(!pendingResponseQueue.checkAndDequeue(hdr->id, hdr->token)) {
262 RLOGE("Token:%d, MessageId:%d", hdr->token, hdr->id);
263 RLOGE ("RilSapSocket::onRequestComplete: invalid Token or Message Id");
264 }
265
266 // Deallocate MsgHeader
267 free(hdr);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200268}
269
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200270void RilSapSocket::onUnsolicitedResponse(int unsolResponse, void *data, size_t datalen) {
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700271 if (data && datalen > 0) {
272 pb_bytes_array_t *payload = (pb_bytes_array_t *)calloc(1,
273 sizeof(pb_bytes_array_t) + datalen);
274 if (!payload) {
275 RLOGE("onUnsolicitedResponse: OOM");
276 return;
277 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200278 memcpy(payload->bytes, data, datalen);
279 payload->size = datalen;
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700280 MsgHeader rsp;
281 rsp.payload = payload;
282 rsp.type = MsgType_UNSOL_RESPONSE;
283 rsp.id = (MsgId)unsolResponse;
284 rsp.error = Error_RIL_E_SUCCESS;
Amit Mahajan2056e962016-11-29 16:48:54 -0800285 sap::processUnsolResponse(&rsp, this);
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700286 free(payload);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200287 }
Nathan Harold14e42662017-07-31 19:44:46 -0700288}