blob: 211371e2a272273b744b6352c069c87f041c410a [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 Mahajanc2c71852016-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");
Gohulan Balachandran46b28cc2017-10-20 09:37:52 -070058 if (request->curr) {
59 free(request->curr);
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -070060 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +020061 free(request);
62 }
63}
64
65#if defined(ANDROID_MULTI_SIM)
66void RilSapSocket::sOnUnsolicitedResponse(int unsolResponse,
67 const void *data,
68 size_t datalen,
69 RIL_SOCKET_ID socketId) {
70 RilSapSocket *sap_socket = getSocketById(socketId);
71 if (sap_socket) {
72 sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
73 }
74}
75#else
76void RilSapSocket::sOnUnsolicitedResponse(int unsolResponse,
77 const void *data,
78 size_t datalen) {
79 RilSapSocket *sap_socket = getSocketById(RIL_SOCKET_1);
Naina Nallurib97624b2016-02-24 18:15:41 -080080 if(sap_socket){
81 sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
82 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +020083}
84#endif
85
86void RilSapSocket::printList() {
87 RilSapSocketList *current = head;
88 RLOGD("Printing socket list");
89 while(NULL != current) {
90 RLOGD("SocketName:%s",current->socket->name);
91 RLOGD("Socket id:%d",current->socket->id);
92 current = current->next;
93 }
94}
95
96RilSapSocket *RilSapSocket::getSocketById(RIL_SOCKET_ID socketId) {
97 RilSapSocket *sap_socket;
98 RilSapSocketList *current = head;
99
100 RLOGD("Entered getSocketById");
101 printList();
102
103 while(NULL != current) {
104 if(socketId == current->socket->id) {
105 sap_socket = current->socket;
106 return sap_socket;
107 }
108 current = current->next;
109 }
110 return NULL;
111}
112
113void RilSapSocket::initSapSocket(const char *socketName,
Nathan Harold14e42662017-07-31 19:44:46 -0700114 const RIL_RadioFunctions *uimFuncs) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200115
Amit Mahajan7955c432017-03-28 11:17:55 -0700116 if (strcmp(socketName, RIL1_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200117 if(!SocketExists(socketName)) {
118 addSocketToList(socketName, RIL_SOCKET_1, uimFuncs);
119 }
120 }
121
122#if (SIM_COUNT >= 2)
Amit Mahajan7955c432017-03-28 11:17:55 -0700123 if (strcmp(socketName, RIL2_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200124 if(!SocketExists(socketName)) {
125 addSocketToList(socketName, RIL_SOCKET_2, uimFuncs);
126 }
127 }
128#endif
129
130#if (SIM_COUNT >= 3)
Amit Mahajan7955c432017-03-28 11:17:55 -0700131 if (strcmp(socketName, RIL3_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200132 if(!SocketExists(socketName)) {
133 addSocketToList(socketName, RIL_SOCKET_3, uimFuncs);
134 }
135 }
136#endif
137
138#if (SIM_COUNT >= 4)
Amit Mahajan7955c432017-03-28 11:17:55 -0700139 if (strcmp(socketName, RIL4_SERVICE_NAME) == 0) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200140 if(!SocketExists(socketName)) {
141 addSocketToList(socketName, RIL_SOCKET_4, uimFuncs);
142 }
143 }
144#endif
145}
146
147void RilSapSocket::addSocketToList(const char *socketName, RIL_SOCKET_ID socketid,
Nathan Harold14e42662017-07-31 19:44:46 -0700148 const RIL_RadioFunctions *uimFuncs) {
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200149 RilSapSocket* socket = NULL;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200150 RilSapSocketList *current;
151
152 if(!SocketExists(socketName)) {
153 socket = new RilSapSocket(socketName, socketid, uimFuncs);
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700154 RilSapSocketList* listItem = (RilSapSocketList*)malloc(sizeof(RilSapSocketList));
155 if (!listItem) {
156 RLOGE("addSocketToList: OOM");
Yunlian Jiang0c049562016-11-08 18:19:11 -0800157 delete socket;
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700158 return;
159 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200160 listItem->socket = socket;
161 listItem->next = NULL;
162
163 RLOGD("Adding socket with id: %d", socket->id);
164
165 if(NULL == head) {
166 head = listItem;
167 head->next = NULL;
168 }
169 else {
170 current = head;
171 while(NULL != current->next) {
172 current = current->next;
173 }
174 current->next = listItem;
175 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200176 }
177}
178
179bool RilSapSocket::SocketExists(const char *socketName) {
180 RilSapSocketList* current = head;
181
182 while(NULL != current) {
183 if(strcmp(current->socket->name, socketName) == 0) {
184 return true;
185 }
186 current = current->next;
187 }
188 return false;
189}
190
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200191RilSapSocket::RilSapSocket(const char *socketName,
192 RIL_SOCKET_ID socketId,
Nathan Harold14e42662017-07-31 19:44:46 -0700193 const RIL_RadioFunctions *inputUimFuncs):
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200194 RilSocket(socketName, socketId) {
195 if (inputUimFuncs) {
196 uimFuncs = inputUimFuncs;
197 }
198}
199
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200200void RilSapSocket::dispatchRequest(MsgHeader *req) {
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700201 // SapSocketRequest will be deallocated in onRequestComplete()
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200202 SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest));
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700203 if (!currRequest) {
204 RLOGE("dispatchRequest: OOM");
205 // Free MsgHeader allocated in pushRecord()
206 free(req);
207 return;
208 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200209 currRequest->token = req->token;
210 currRequest->curr = req;
211 currRequest->p_next = NULL;
212 currRequest->socketId = id;
213
214 pendingResponseQueue.enqueue(currRequest);
215
216 if (uimFuncs) {
Amit Mahajanc2c71852016-11-29 16:48:54 -0800217 RLOGI("RilSapSocket::dispatchRequest [%d] > SAP REQUEST type: %d. id: %d. error: %d, \
218 token 0x%p",
219 req->token,
220 req->type,
221 req->id,
222 req->error,
223 currRequest );
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200224
225#if defined(ANDROID_MULTI_SIM)
226 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest, id);
227#else
228 uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest);
229#endif
230 }
231}
232
233void RilSapSocket::onRequestComplete(RIL_Token t, RIL_Errno e, void *response,
234 size_t response_len) {
235 SapSocketRequest* request= (SapSocketRequest*)t;
Gohulan Balachandran46b28cc2017-10-20 09:37:52 -0700236
237 if (!request || !request->curr) {
238 RLOGE("RilSapSocket::onRequestComplete: request/request->curr is NULL");
239 return;
240 }
241
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200242 MsgHeader *hdr = request->curr;
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200243
Sanket Padawe638a7232016-01-26 16:26:30 -0800244 MsgHeader rsp;
245 rsp.token = request->curr->token;
246 rsp.type = MsgType_RESPONSE;
247 rsp.id = request->curr->id;
248 rsp.error = (Error)e;
249 rsp.payload = (pb_bytes_array_t *)calloc(1, sizeof(pb_bytes_array_t) + response_len);
250 if (!rsp.payload) {
251 RLOGE("onRequestComplete: OOM");
252 } else {
253 if (response && response_len > 0) {
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700254 memcpy(rsp.payload->bytes, response, response_len);
255 rsp.payload->size = response_len;
Sanket Padawe638a7232016-01-26 16:26:30 -0800256 } else {
257 rsp.payload->size = 0;
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700258 }
Sanket Padawe638a7232016-01-26 16:26:30 -0800259
Amit Mahajanc2c71852016-11-29 16:48:54 -0800260 RLOGE("RilSapSocket::onRequestComplete: Token:%d, MessageId:%d ril token 0x%p",
261 hdr->token, hdr->id, t);
Sanket Padawe638a7232016-01-26 16:26:30 -0800262
Amit Mahajanc2c71852016-11-29 16:48:54 -0800263 sap::processResponse(&rsp, this);
Sanket Padawe638a7232016-01-26 16:26:30 -0800264 free(rsp.payload);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200265 }
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700266
267 // Deallocate SapSocketRequest
268 if(!pendingResponseQueue.checkAndDequeue(hdr->id, hdr->token)) {
269 RLOGE("Token:%d, MessageId:%d", hdr->token, hdr->id);
270 RLOGE ("RilSapSocket::onRequestComplete: invalid Token or Message Id");
271 }
272
273 // Deallocate MsgHeader
274 free(hdr);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200275}
276
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200277void RilSapSocket::onUnsolicitedResponse(int unsolResponse, void *data, size_t datalen) {
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700278 if (data && datalen > 0) {
279 pb_bytes_array_t *payload = (pb_bytes_array_t *)calloc(1,
280 sizeof(pb_bytes_array_t) + datalen);
281 if (!payload) {
282 RLOGE("onUnsolicitedResponse: OOM");
283 return;
284 }
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200285 memcpy(payload->bytes, data, datalen);
286 payload->size = datalen;
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700287 MsgHeader rsp;
288 rsp.payload = payload;
289 rsp.type = MsgType_UNSOL_RESPONSE;
290 rsp.id = (MsgId)unsolResponse;
291 rsp.error = Error_RIL_E_SUCCESS;
Amit Mahajanc2c71852016-11-29 16:48:54 -0800292 sap::processUnsolResponse(&rsp, this);
Pavel Zhamaitsiakd33397b2015-08-18 11:40:01 -0700293 free(payload);
Dheeraj Shetty27976c42014-07-02 21:27:57 +0200294 }
Nathan Harold14e42662017-07-31 19:44:46 -0700295}