Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 1 | /* |
| 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 Padawe | f0c8ca7 | 2016-06-30 15:01:08 -0700 | [diff] [blame] | 24 | #undef LOG_TAG |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 25 | #define LOG_TAG "RIL_UIM_SOCKET" |
| 26 | #include <utils/Log.h> |
| 27 | #include <arpa/inet.h> |
Vinit Deshpande | 1b1ec2d | 2015-04-15 13:31:05 -0700 | [diff] [blame] | 28 | #include <errno.h> |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 29 | #include <sap_service.h> |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 30 | |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 31 | static RilSapSocket::RilSapSocketList *head = NULL; |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 32 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 33 | extern "C" void |
| 34 | RIL_requestTimedCallback (RIL_TimedCallback callback, void *param, |
| 35 | const struct timeval *relativeTime); |
| 36 | |
| 37 | struct RIL_Env RilSapSocket::uimRilEnv = { |
| 38 | .OnRequestComplete = RilSapSocket::sOnRequestComplete, |
| 39 | .OnUnsolicitedResponse = RilSapSocket::sOnUnsolicitedResponse, |
| 40 | .RequestTimedCallback = RIL_requestTimedCallback |
| 41 | }; |
| 42 | |
| 43 | void 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 Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 58 | if (request->curr->payload) { |
| 59 | free(request->curr->payload); |
| 60 | } |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 61 | free(request->curr); |
| 62 | free(request); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | #if defined(ANDROID_MULTI_SIM) |
| 67 | void 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 |
| 77 | void 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 | |
| 85 | void 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 | |
| 95 | RilSapSocket *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 | |
| 112 | void RilSapSocket::initSapSocket(const char *socketName, |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame^] | 113 | const RIL_RadioFunctions *uimFuncs) { |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 114 | |
Amit Mahajan | 7955c43 | 2017-03-28 11:17:55 -0700 | [diff] [blame] | 115 | if (strcmp(socketName, RIL1_SERVICE_NAME) == 0) { |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 116 | if(!SocketExists(socketName)) { |
| 117 | addSocketToList(socketName, RIL_SOCKET_1, uimFuncs); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | #if (SIM_COUNT >= 2) |
Amit Mahajan | 7955c43 | 2017-03-28 11:17:55 -0700 | [diff] [blame] | 122 | if (strcmp(socketName, RIL2_SERVICE_NAME) == 0) { |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 123 | if(!SocketExists(socketName)) { |
| 124 | addSocketToList(socketName, RIL_SOCKET_2, uimFuncs); |
| 125 | } |
| 126 | } |
| 127 | #endif |
| 128 | |
| 129 | #if (SIM_COUNT >= 3) |
Amit Mahajan | 7955c43 | 2017-03-28 11:17:55 -0700 | [diff] [blame] | 130 | if (strcmp(socketName, RIL3_SERVICE_NAME) == 0) { |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 131 | if(!SocketExists(socketName)) { |
| 132 | addSocketToList(socketName, RIL_SOCKET_3, uimFuncs); |
| 133 | } |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #if (SIM_COUNT >= 4) |
Amit Mahajan | 7955c43 | 2017-03-28 11:17:55 -0700 | [diff] [blame] | 138 | if (strcmp(socketName, RIL4_SERVICE_NAME) == 0) { |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 139 | if(!SocketExists(socketName)) { |
| 140 | addSocketToList(socketName, RIL_SOCKET_4, uimFuncs); |
| 141 | } |
| 142 | } |
| 143 | #endif |
| 144 | } |
| 145 | |
| 146 | void RilSapSocket::addSocketToList(const char *socketName, RIL_SOCKET_ID socketid, |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame^] | 147 | const RIL_RadioFunctions *uimFuncs) { |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 148 | RilSapSocket* socket = NULL; |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 149 | RilSapSocketList *current; |
| 150 | |
| 151 | if(!SocketExists(socketName)) { |
| 152 | socket = new RilSapSocket(socketName, socketid, uimFuncs); |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 153 | RilSapSocketList* listItem = (RilSapSocketList*)malloc(sizeof(RilSapSocketList)); |
| 154 | if (!listItem) { |
| 155 | RLOGE("addSocketToList: OOM"); |
Yunlian Jiang | 0c04956 | 2016-11-08 18:19:11 -0800 | [diff] [blame] | 156 | delete socket; |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 157 | return; |
| 158 | } |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 159 | 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 Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | bool 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 Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 190 | RilSapSocket::RilSapSocket(const char *socketName, |
| 191 | RIL_SOCKET_ID socketId, |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame^] | 192 | const RIL_RadioFunctions *inputUimFuncs): |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 193 | RilSocket(socketName, socketId) { |
| 194 | if (inputUimFuncs) { |
| 195 | uimFuncs = inputUimFuncs; |
| 196 | } |
| 197 | } |
| 198 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 199 | void RilSapSocket::dispatchRequest(MsgHeader *req) { |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 200 | // SapSocketRequest will be deallocated in onRequestComplete() |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 201 | SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest)); |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 202 | if (!currRequest) { |
| 203 | RLOGE("dispatchRequest: OOM"); |
| 204 | // Free MsgHeader allocated in pushRecord() |
| 205 | free(req); |
| 206 | return; |
| 207 | } |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 208 | 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 Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 216 | 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 Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 223 | |
| 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 | |
| 232 | void 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 Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 236 | |
Sanket Padawe | 638a723 | 2016-01-26 16:26:30 -0800 | [diff] [blame] | 237 | 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 Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 247 | memcpy(rsp.payload->bytes, response, response_len); |
| 248 | rsp.payload->size = response_len; |
Sanket Padawe | 638a723 | 2016-01-26 16:26:30 -0800 | [diff] [blame] | 249 | } else { |
| 250 | rsp.payload->size = 0; |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 251 | } |
Sanket Padawe | 638a723 | 2016-01-26 16:26:30 -0800 | [diff] [blame] | 252 | |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 253 | RLOGE("RilSapSocket::onRequestComplete: Token:%d, MessageId:%d ril token 0x%p", |
| 254 | hdr->token, hdr->id, t); |
Sanket Padawe | 638a723 | 2016-01-26 16:26:30 -0800 | [diff] [blame] | 255 | |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 256 | sap::processResponse(&rsp, this); |
Sanket Padawe | 638a723 | 2016-01-26 16:26:30 -0800 | [diff] [blame] | 257 | free(rsp.payload); |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 258 | } |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 259 | |
| 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 Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 270 | void RilSapSocket::onUnsolicitedResponse(int unsolResponse, void *data, size_t datalen) { |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 271 | 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 Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 278 | memcpy(payload->bytes, data, datalen); |
| 279 | payload->size = datalen; |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 280 | 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 Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 285 | sap::processUnsolResponse(&rsp, this); |
Pavel Zhamaitsiak | d33397b | 2015-08-18 11:40:01 -0700 | [diff] [blame] | 286 | free(payload); |
Dheeraj Shetty | 27976c4 | 2014-07-02 21:27:57 +0200 | [diff] [blame] | 287 | } |
Nathan Harold | 14e4266 | 2017-07-31 19:44:46 -0700 | [diff] [blame^] | 288 | } |