Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 | #include <android/hardware/radio/1.0/ISap.h> |
| 18 | |
| 19 | #include <hwbinder/IPCThreadState.h> |
| 20 | #include <hwbinder/ProcessState.h> |
| 21 | #include <sap_service.h> |
| 22 | #include "pb_decode.h" |
| 23 | #include "pb_encode.h" |
| 24 | |
| 25 | using namespace android::hardware::radio::V1_0; |
| 26 | using ::android::hardware::Return; |
| 27 | using ::android::hardware::Status; |
| 28 | using ::android::hardware::hidl_vec; |
| 29 | using ::android::hardware::hidl_array; |
| 30 | using ::android::hardware::Void; |
| 31 | using android::CommandInfo; |
| 32 | using android::RequestInfo; |
| 33 | using android::requestToString; |
| 34 | using android::sp; |
| 35 | |
| 36 | struct SapImpl; |
| 37 | |
| 38 | #if (SIM_COUNT >= 2) |
| 39 | sp<SapImpl> sapService[SIM_COUNT]; |
| 40 | #else |
| 41 | sp<SapImpl> sapService[1]; |
| 42 | #endif |
| 43 | |
| 44 | struct SapImpl : public ISap { |
| 45 | int32_t slotId; |
| 46 | sp<ISapCallback> sapCallback; |
| 47 | RIL_SOCKET_ID rilSocketId; |
| 48 | |
| 49 | Return<void> setCallback(const ::android::sp<ISapCallback>& sapCallbackParam); |
| 50 | |
| 51 | Return<void> connectReq(int32_t token, int32_t maxMsgSize); |
| 52 | |
| 53 | Return<void> disconnectReq(int32_t token); |
| 54 | |
| 55 | Return<void> apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command); |
| 56 | |
| 57 | Return<void> transferAtrReq(int32_t token); |
| 58 | |
| 59 | Return<void> powerReq(int32_t token, bool state); |
| 60 | |
| 61 | Return<void> resetSimReq(int32_t token); |
| 62 | |
| 63 | Return<void> transferCardReaderStatusReq(int32_t token); |
| 64 | |
| 65 | Return<void> setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol); |
| 66 | |
| 67 | MsgHeader* createMsgHeader(MsgId msgId, int32_t token); |
| 68 | |
| 69 | Return<void> addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen, uint8_t *reqPtr); |
| 70 | |
| 71 | void sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 72 | |
| 73 | void checkReturnStatus(Return<void>& ret); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 76 | void SapImpl::checkReturnStatus(Return<void>& ret) { |
| 77 | if (ret.isOk() == false) { |
| 78 | RLOGE("checkReturnStatus: unable to call response/indication callback"); |
| 79 | // Remote process (SapRilReceiver.java) hosting the callback must be dead. Reset the |
| 80 | // callback object; there's no other recovery to be done here. When the client process is |
| 81 | // back up, it will call setCallback() |
| 82 | sapCallback = NULL; |
| 83 | } |
| 84 | } |
| 85 | |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 86 | Return<void> SapImpl::setCallback(const ::android::sp<ISapCallback>& sapCallbackParam) { |
| 87 | RLOGD("SapImpl::setCallback for slotId %d", slotId); |
| 88 | sapCallback = sapCallbackParam; |
| 89 | return Status::ok(); |
| 90 | } |
| 91 | |
| 92 | MsgHeader* SapImpl::createMsgHeader(MsgId msgId, int32_t token) { |
| 93 | // Memory for msg will be freed by RilSapSocket::onRequestComplete() |
| 94 | MsgHeader *msg = (MsgHeader *)calloc(1, sizeof(MsgHeader)); |
| 95 | if (msg == NULL) { |
| 96 | return NULL; |
| 97 | } |
| 98 | msg->token = token; |
| 99 | msg->type = MsgType_REQUEST; |
| 100 | msg->id = msgId; |
| 101 | msg->error = Error_RIL_E_SUCCESS; |
| 102 | return msg; |
| 103 | } |
| 104 | |
| 105 | Return<void> SapImpl::addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen, |
| 106 | uint8_t *reqPtr) { |
| 107 | msg->payload = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + reqLen); |
| 108 | if (msg->payload == NULL) { |
| 109 | sendFailedResponse(msg->id, msg->token, 2, reqPtr, msg); |
| 110 | return Status::fromExceptionCode(Status::Exception::EX_NULL_POINTER); |
| 111 | } |
| 112 | msg->payload->size = reqLen; |
| 113 | memcpy(msg->payload->bytes, reqPtr, reqLen); |
| 114 | |
| 115 | RilSapSocket *sapSocket = RilSapSocket::getSocketById(rilSocketId); |
| 116 | if (sapSocket) { |
| 117 | RLOGD("SapImpl::addPayloadAndDispatchRequest: calling dispatchRequest"); |
| 118 | sapSocket->dispatchRequest(msg); |
| 119 | } else { |
| 120 | RLOGE("SapImpl::addPayloadAndDispatchRequest: sapSocket is null"); |
| 121 | sendFailedResponse(msg->id, msg->token, 3, msg->payload, reqPtr, msg); |
| 122 | return Status::fromExceptionCode(Status::Exception::EX_NULL_POINTER); |
| 123 | } |
| 124 | free(msg->payload); |
| 125 | free(reqPtr); |
| 126 | return Status::ok(); |
| 127 | } |
| 128 | |
| 129 | void SapImpl::sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...) { |
| 130 | va_list ap; |
| 131 | va_start(ap, numPointers); |
| 132 | for (int i = 0; i < numPointers; i++) { |
| 133 | void *ptr = va_arg(ap, void *); |
| 134 | if (ptr) free(ptr); |
| 135 | } |
| 136 | va_end(ap); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 137 | Return<void> retStatus; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 138 | switch(msgId) { |
| 139 | case MsgId_RIL_SIM_SAP_CONNECT: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 140 | retStatus = sapCallback->connectResponse(token, SapConnectRsp::CONNECT_FAILURE, 0); |
| 141 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 142 | |
| 143 | case MsgId_RIL_SIM_SAP_DISCONNECT: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 144 | retStatus = sapCallback->disconnectResponse(token); |
| 145 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 146 | |
| 147 | case MsgId_RIL_SIM_SAP_APDU: { |
| 148 | hidl_vec<uint8_t> apduRsp; |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 149 | retStatus = sapCallback->apduResponse(token, SapResultCode::GENERIC_FAILURE, apduRsp); |
| 150 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | case MsgId_RIL_SIM_SAP_TRANSFER_ATR: { |
| 154 | hidl_vec<uint8_t> atr; |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 155 | retStatus = sapCallback->transferAtrResponse(token, SapResultCode::GENERIC_FAILURE, |
| 156 | atr); |
| 157 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | case MsgId_RIL_SIM_SAP_POWER: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 161 | retStatus = sapCallback->powerResponse(token, SapResultCode::GENERIC_FAILURE); |
| 162 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 163 | |
| 164 | case MsgId_RIL_SIM_SAP_RESET_SIM: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 165 | retStatus = sapCallback->resetSimResponse(token, SapResultCode::GENERIC_FAILURE); |
| 166 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 167 | |
| 168 | case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 169 | retStatus = sapCallback->transferCardReaderStatusResponse(token, |
| 170 | SapResultCode::GENERIC_FAILURE, 0); |
| 171 | break; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 172 | |
| 173 | case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 174 | retStatus = sapCallback->transferProtocolResponse(token, SapResultCode::NOT_SUPPORTED); |
| 175 | break; |
| 176 | |
Wei Wang | 100ac9b | 2017-02-03 14:18:07 -0800 | [diff] [blame] | 177 | default: |
| 178 | return; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 179 | } |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 180 | sapService[slotId]->checkReturnStatus(retStatus); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | Return<void> SapImpl::connectReq(int32_t token, int32_t maxMsgSize) { |
| 184 | RLOGD("SapImpl::connectReq"); |
| 185 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_CONNECT, token); |
| 186 | if (msg == NULL) { |
| 187 | RLOGE("SapImpl::connectReq: Error allocating memory for msg"); |
| 188 | sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 0); |
| 189 | return Status::fromStatusT(android::NO_MEMORY); |
| 190 | } |
| 191 | |
| 192 | /***** Encode RIL_SIM_SAP_CONNECT_REQ *****/ |
| 193 | RIL_SIM_SAP_CONNECT_REQ req; |
| 194 | memset(&req, 0, sizeof(RIL_SIM_SAP_CONNECT_REQ)); |
| 195 | req.max_message_size = maxMsgSize; |
| 196 | |
| 197 | size_t encodedSize = 0; |
| 198 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) { |
| 199 | RLOGE("SapImpl::connectReq: Error getting encoded size for RIL_SIM_SAP_CONNECT_REQ"); |
| 200 | sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg); |
| 201 | return Status::fromStatusT(android::NO_MEMORY); |
| 202 | } |
| 203 | |
| 204 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 205 | if (buffer == NULL) { |
| 206 | RLOGE("SapImpl::connectReq: Error allocating memory for buffer"); |
| 207 | sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg); |
| 208 | return Status::fromStatusT(android::NO_MEMORY); |
| 209 | } |
| 210 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 211 | |
| 212 | RLOGD("SapImpl::connectReq calling pb_encode"); |
| 213 | if (!pb_encode(&stream, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) { |
| 214 | RLOGE("SapImpl::connectReq: Error encoding RIL_SIM_SAP_CONNECT_REQ"); |
| 215 | sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 2, buffer, msg); |
| 216 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 217 | } |
| 218 | /***** Encode RIL_SIM_SAP_CONNECT_REQ done *****/ |
| 219 | |
| 220 | /* encoded req is payload */ |
| 221 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 222 | } |
| 223 | |
| 224 | Return<void> SapImpl::disconnectReq(int32_t token) { |
| 225 | RLOGD("SapImpl::disconnectReq"); |
| 226 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_DISCONNECT, token); |
| 227 | if (msg == NULL) { |
| 228 | RLOGE("SapImpl::disconnectReq: Error allocating memory for msg"); |
| 229 | sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 0); |
| 230 | return Status::fromStatusT(android::NO_MEMORY); |
| 231 | } |
| 232 | |
| 233 | /***** Encode RIL_SIM_SAP_DISCONNECT_REQ *****/ |
| 234 | RIL_SIM_SAP_DISCONNECT_REQ req; |
| 235 | memset(&req, 0, sizeof(RIL_SIM_SAP_DISCONNECT_REQ)); |
| 236 | |
| 237 | size_t encodedSize = 0; |
| 238 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) { |
| 239 | RLOGE("SapImpl::disconnectReq: Error getting encoded size for RIL_SIM_SAP_DISCONNECT_REQ"); |
| 240 | sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg); |
| 241 | return Status::fromStatusT(android::NO_MEMORY); |
| 242 | } |
| 243 | |
| 244 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 245 | if (buffer == NULL) { |
| 246 | RLOGE("SapImpl::disconnectReq: Error allocating memory for buffer"); |
| 247 | sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg); |
| 248 | return Status::fromStatusT(android::NO_MEMORY); |
| 249 | } |
| 250 | |
| 251 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 252 | |
| 253 | RLOGD("SapImpl::disconnectReq calling pb_encode"); |
| 254 | if (!pb_encode(&stream, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) { |
| 255 | RLOGE("SapImpl::disconnectReq: Error encoding RIL_SIM_SAP_DISCONNECT_REQ"); |
| 256 | sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 2, buffer, msg); |
| 257 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 258 | } |
| 259 | /***** Encode RIL_SIM_SAP_DISCONNECT_REQ done *****/ |
| 260 | |
| 261 | /* encoded req is payload */ |
| 262 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 263 | } |
| 264 | |
| 265 | Return<void> SapImpl::apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command) { |
| 266 | RLOGD("SapImpl::apduReq"); |
| 267 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_APDU, token); |
| 268 | if (msg == NULL) { |
| 269 | RLOGE("SapImpl::apduReq: Error allocating memory for msg"); |
| 270 | sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 0); |
| 271 | return Status::fromStatusT(android::NO_MEMORY); |
| 272 | } |
| 273 | |
| 274 | /***** Encode RIL_SIM_SAP_APDU_REQ *****/ |
| 275 | RIL_SIM_SAP_APDU_REQ req; |
| 276 | memset(&req, 0, sizeof(RIL_SIM_SAP_APDU_REQ)); |
| 277 | req.type = (RIL_SIM_SAP_APDU_REQ_Type)type; |
| 278 | |
| 279 | if (command.size() > 0) { |
| 280 | req.command = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + command.size()); |
| 281 | if (req.command == NULL) { |
| 282 | RLOGE("SapImpl::apduReq: Error allocating memory for req.command"); |
| 283 | sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 1, msg); |
| 284 | return Status::fromStatusT(android::NO_MEMORY); |
| 285 | } |
| 286 | req.command->size = command.size(); |
| 287 | memcpy(req.command->bytes, command.data(), command.size()); |
| 288 | } |
| 289 | |
| 290 | size_t encodedSize = 0; |
| 291 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_APDU_REQ_fields, &req)) { |
| 292 | RLOGE("SapImpl::apduReq: Error getting encoded size for RIL_SIM_SAP_APDU_REQ"); |
| 293 | sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg); |
| 294 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 295 | } |
| 296 | |
| 297 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 298 | if (buffer == NULL) { |
| 299 | RLOGE("SapImpl::apduReq: Error allocating memory for buffer"); |
| 300 | sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg); |
| 301 | return Status::fromStatusT(android::NO_MEMORY); |
| 302 | } |
| 303 | |
| 304 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 305 | |
| 306 | RLOGD("SapImpl::apduReq calling pb_encode"); |
| 307 | if (!pb_encode(&stream, RIL_SIM_SAP_APDU_REQ_fields, &req)) { |
| 308 | RLOGE("SapImpl::apduReq: Error encoding RIL_SIM_SAP_APDU_REQ"); |
| 309 | sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 3, req.command, buffer, msg); |
| 310 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 311 | } |
| 312 | /***** Encode RIL_SIM_SAP_APDU_REQ done *****/ |
| 313 | |
| 314 | /* encoded req is payload */ |
| 315 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 316 | } |
| 317 | |
| 318 | Return<void> SapImpl::transferAtrReq(int32_t token) { |
| 319 | RLOGD("SapImpl::transferAtrReq"); |
| 320 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token); |
| 321 | if (msg == NULL) { |
| 322 | RLOGE("SapImpl::transferAtrReq: Error allocating memory for msg"); |
| 323 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 0); |
| 324 | return Status::fromStatusT(android::NO_MEMORY); |
| 325 | } |
| 326 | |
| 327 | /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ *****/ |
| 328 | RIL_SIM_SAP_TRANSFER_ATR_REQ req; |
| 329 | memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_ATR_REQ)); |
| 330 | |
| 331 | size_t encodedSize = 0; |
| 332 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) { |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 333 | RLOGE("SapImpl::transferAtrReq: Error getting encoded size for " |
| 334 | "RIL_SIM_SAP_TRANSFER_ATR_REQ"); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 335 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg); |
| 336 | return Status::fromStatusT(android::NO_MEMORY); |
| 337 | } |
| 338 | |
| 339 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 340 | if (buffer == NULL) { |
| 341 | RLOGE("SapImpl::transferAtrReq: Error allocating memory for buffer"); |
| 342 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg); |
| 343 | return Status::fromStatusT(android::NO_MEMORY); |
| 344 | } |
| 345 | |
| 346 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 347 | |
| 348 | RLOGD("SapImpl::transferAtrReq calling pb_encode"); |
| 349 | if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) { |
| 350 | RLOGE("SapImpl::transferAtrReq: Error encoding RIL_SIM_SAP_TRANSFER_ATR_REQ"); |
| 351 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 2, buffer, msg); |
| 352 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 353 | } |
| 354 | /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ done *****/ |
| 355 | |
| 356 | /* encoded req is payload */ |
| 357 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 358 | } |
| 359 | |
| 360 | Return<void> SapImpl::powerReq(int32_t token, bool state) { |
| 361 | RLOGD("SapImpl::powerReq"); |
| 362 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_POWER, token); |
| 363 | if (msg == NULL) { |
| 364 | RLOGE("SapImpl::powerReq: Error allocating memory for msg"); |
| 365 | sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 0); |
| 366 | return Status::fromStatusT(android::NO_MEMORY); |
| 367 | } |
| 368 | |
| 369 | /***** Encode RIL_SIM_SAP_POWER_REQ *****/ |
| 370 | RIL_SIM_SAP_POWER_REQ req; |
| 371 | memset(&req, 0, sizeof(RIL_SIM_SAP_POWER_REQ)); |
| 372 | req.state = state; |
| 373 | |
| 374 | size_t encodedSize = 0; |
| 375 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_POWER_REQ_fields, &req)) { |
| 376 | RLOGE("SapImpl::powerReq: Error getting encoded size for RIL_SIM_SAP_POWER_REQ"); |
| 377 | sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg); |
| 378 | return Status::fromStatusT(android::NO_MEMORY); |
| 379 | } |
| 380 | |
| 381 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 382 | if (buffer == NULL) { |
| 383 | RLOGE("SapImpl::powerReq: Error allocating memory for buffer"); |
| 384 | sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg); |
| 385 | return Status::fromStatusT(android::NO_MEMORY); |
| 386 | } |
| 387 | |
| 388 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 389 | |
| 390 | RLOGD("SapImpl::powerReq calling pb_encode"); |
| 391 | if (!pb_encode(&stream, RIL_SIM_SAP_POWER_REQ_fields, &req)) { |
| 392 | RLOGE("SapImpl::powerReq: Error encoding RIL_SIM_SAP_POWER_REQ"); |
| 393 | sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 2, buffer, msg); |
| 394 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 395 | } |
| 396 | /***** Encode RIL_SIM_SAP_POWER_REQ done *****/ |
| 397 | |
| 398 | /* encoded req is payload */ |
| 399 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 400 | } |
| 401 | |
| 402 | Return<void> SapImpl::resetSimReq(int32_t token) { |
| 403 | RLOGD("SapImpl::resetSimReq"); |
| 404 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_RESET_SIM, token); |
| 405 | if (msg == NULL) { |
| 406 | RLOGE("SapImpl::resetSimReq: Error allocating memory for msg"); |
| 407 | sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 0); |
| 408 | return Status::fromStatusT(android::NO_MEMORY); |
| 409 | } |
| 410 | |
| 411 | /***** Encode RIL_SIM_SAP_RESET_SIM_REQ *****/ |
| 412 | RIL_SIM_SAP_RESET_SIM_REQ req; |
| 413 | memset(&req, 0, sizeof(RIL_SIM_SAP_RESET_SIM_REQ)); |
| 414 | |
| 415 | size_t encodedSize = 0; |
| 416 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) { |
| 417 | RLOGE("SapImpl::resetSimReq: Error getting encoded size for RIL_SIM_SAP_RESET_SIM_REQ"); |
| 418 | sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg); |
| 419 | return Status::fromStatusT(android::NO_MEMORY); |
| 420 | } |
| 421 | |
| 422 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 423 | if (buffer == NULL) { |
| 424 | RLOGE("SapImpl::resetSimReq: Error allocating memory for buffer"); |
| 425 | sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg); |
| 426 | return Status::fromStatusT(android::NO_MEMORY); |
| 427 | } |
| 428 | |
| 429 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 430 | |
| 431 | RLOGD("SapImpl::resetSimReq calling pb_encode"); |
| 432 | if (!pb_encode(&stream, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) { |
| 433 | RLOGE("SapImpl::resetSimReq: Error encoding RIL_SIM_SAP_RESET_SIM_REQ"); |
| 434 | sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 2, buffer, msg); |
| 435 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 436 | } |
| 437 | /***** Encode RIL_SIM_SAP_RESET_SIM_REQ done *****/ |
| 438 | |
| 439 | /* encoded req is payload */ |
| 440 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 441 | } |
| 442 | |
| 443 | Return<void> SapImpl::transferCardReaderStatusReq(int32_t token) { |
| 444 | RLOGD("SapImpl::transferCardReaderStatusReq"); |
| 445 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token); |
| 446 | if (msg == NULL) { |
| 447 | RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for msg"); |
| 448 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 0); |
| 449 | return Status::fromStatusT(android::NO_MEMORY); |
| 450 | } |
| 451 | |
| 452 | /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ *****/ |
| 453 | RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ req; |
| 454 | memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ)); |
| 455 | |
| 456 | size_t encodedSize = 0; |
| 457 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields, |
| 458 | &req)) { |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 459 | RLOGE("SapImpl::transferCardReaderStatusReq: Error getting encoded size for " |
| 460 | "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ"); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 461 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg); |
| 462 | return Status::fromStatusT(android::NO_MEMORY); |
| 463 | } |
| 464 | |
| 465 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 466 | if (buffer == NULL) { |
| 467 | RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for buffer"); |
| 468 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg); |
| 469 | return Status::fromStatusT(android::NO_MEMORY); |
| 470 | } |
| 471 | |
| 472 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 473 | |
| 474 | RLOGD("SapImpl::transferCardReaderStatusReq calling pb_encode"); |
| 475 | if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields, &req)) { |
| 476 | RLOGE("SapImpl::transferCardReaderStatusReq: Error encoding " |
| 477 | "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ"); |
| 478 | sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 2, buffer, msg); |
| 479 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 480 | } |
| 481 | /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ done *****/ |
| 482 | |
| 483 | /* encoded req is payload */ |
| 484 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 485 | } |
| 486 | |
| 487 | Return<void> SapImpl::setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol) { |
| 488 | RLOGD("SapImpl::setTransferProtocolReq"); |
| 489 | MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token); |
| 490 | if (msg == NULL) { |
| 491 | RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for msg"); |
| 492 | sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 0); |
| 493 | return Status::fromStatusT(android::NO_MEMORY); |
| 494 | } |
| 495 | |
| 496 | /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ *****/ |
| 497 | RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ req; |
| 498 | memset(&req, 0, sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ)); |
| 499 | req.protocol = (RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_Protocol)transferProtocol; |
| 500 | |
| 501 | size_t encodedSize = 0; |
| 502 | if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) { |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 503 | RLOGE("SapImpl::setTransferProtocolReq: Error getting encoded size for " |
| 504 | "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ"); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 505 | sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg); |
| 506 | return Status::fromStatusT(android::NO_MEMORY); |
| 507 | } |
| 508 | |
| 509 | uint8_t *buffer = (uint8_t *)calloc(1, encodedSize); |
| 510 | if (buffer == NULL) { |
| 511 | RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for buffer"); |
| 512 | sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg); |
| 513 | return Status::fromStatusT(android::NO_MEMORY); |
| 514 | } |
| 515 | |
| 516 | pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize); |
| 517 | |
| 518 | RLOGD("SapImpl::setTransferProtocolReq calling pb_encode"); |
| 519 | if (!pb_encode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) { |
| 520 | RLOGE("SapImpl::setTransferProtocolReq: Error encoding " |
| 521 | "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ"); |
| 522 | sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 2, buffer, msg); |
| 523 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT); |
| 524 | } |
| 525 | /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ done *****/ |
| 526 | |
| 527 | /* encoded req is payload */ |
| 528 | return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer); |
| 529 | } |
| 530 | |
| 531 | void *sapDecodeMessage(MsgId msgId, MsgType msgType, uint8_t *payloadPtr, size_t payloadLen) { |
| 532 | void *responsePtr = NULL; |
| 533 | bool decodeStatus = false; |
| 534 | pb_istream_t stream; |
| 535 | |
| 536 | /* Create the stream */ |
| 537 | stream = pb_istream_from_buffer((uint8_t *)payloadPtr, payloadLen); |
| 538 | |
| 539 | /* Decode based on the message id */ |
| 540 | switch (msgId) |
| 541 | { |
| 542 | case MsgId_RIL_SIM_SAP_CONNECT: |
| 543 | responsePtr = malloc(sizeof(RIL_SIM_SAP_CONNECT_RSP)); |
| 544 | if (responsePtr) { |
| 545 | if (!pb_decode(&stream, RIL_SIM_SAP_CONNECT_RSP_fields, responsePtr)) { |
| 546 | RLOGE("Error decoding RIL_SIM_SAP_CONNECT_RSP"); |
| 547 | return NULL; |
| 548 | } |
| 549 | } |
| 550 | break; |
| 551 | |
| 552 | case MsgId_RIL_SIM_SAP_DISCONNECT: |
| 553 | if (msgType == MsgType_RESPONSE) { |
| 554 | responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_RSP)); |
| 555 | if (responsePtr) { |
| 556 | if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_RSP_fields, responsePtr)) { |
| 557 | RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_RSP"); |
| 558 | return NULL; |
| 559 | } |
| 560 | } |
| 561 | } else { |
| 562 | responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_IND)); |
| 563 | if (responsePtr) { |
| 564 | if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_IND_fields, responsePtr)) { |
| 565 | RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_IND"); |
| 566 | return NULL; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | break; |
| 571 | |
| 572 | case MsgId_RIL_SIM_SAP_APDU: |
| 573 | responsePtr = malloc(sizeof(RIL_SIM_SAP_APDU_RSP)); |
| 574 | if (responsePtr) { |
| 575 | if (!pb_decode(&stream, RIL_SIM_SAP_APDU_RSP_fields, responsePtr)) { |
| 576 | RLOGE("Error decoding RIL_SIM_SAP_APDU_RSP"); |
| 577 | return NULL; |
| 578 | } |
| 579 | } |
| 580 | break; |
| 581 | |
| 582 | case MsgId_RIL_SIM_SAP_TRANSFER_ATR: |
| 583 | responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_ATR_RSP)); |
| 584 | if (responsePtr) { |
| 585 | if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_ATR_RSP_fields, responsePtr)) { |
| 586 | RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_ATR_RSP"); |
| 587 | return NULL; |
| 588 | } |
| 589 | } |
| 590 | break; |
| 591 | |
| 592 | case MsgId_RIL_SIM_SAP_POWER: |
| 593 | responsePtr = malloc(sizeof(RIL_SIM_SAP_POWER_RSP)); |
| 594 | if (responsePtr) { |
| 595 | if (!pb_decode(&stream, RIL_SIM_SAP_POWER_RSP_fields, responsePtr)) { |
| 596 | RLOGE("Error decoding RIL_SIM_SAP_POWER_RSP"); |
| 597 | return NULL; |
| 598 | } |
| 599 | } |
| 600 | break; |
| 601 | |
| 602 | case MsgId_RIL_SIM_SAP_RESET_SIM: |
| 603 | responsePtr = malloc(sizeof(RIL_SIM_SAP_RESET_SIM_RSP)); |
| 604 | if (responsePtr) { |
| 605 | if (!pb_decode(&stream, RIL_SIM_SAP_RESET_SIM_RSP_fields, responsePtr)) { |
| 606 | RLOGE("Error decoding RIL_SIM_SAP_RESET_SIM_RSP"); |
| 607 | return NULL; |
| 608 | } |
| 609 | } |
| 610 | break; |
| 611 | |
| 612 | case MsgId_RIL_SIM_SAP_STATUS: |
| 613 | responsePtr = malloc(sizeof(RIL_SIM_SAP_STATUS_IND)); |
| 614 | if (responsePtr) { |
| 615 | if (!pb_decode(&stream, RIL_SIM_SAP_STATUS_IND_fields, responsePtr)) { |
| 616 | RLOGE("Error decoding RIL_SIM_SAP_STATUS_IND"); |
| 617 | return NULL; |
| 618 | } |
| 619 | } |
| 620 | break; |
| 621 | |
| 622 | case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: |
| 623 | responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP)); |
| 624 | if (responsePtr) { |
| 625 | if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_fields, |
| 626 | responsePtr)) { |
| 627 | RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP"); |
| 628 | return NULL; |
| 629 | } |
| 630 | } |
| 631 | break; |
| 632 | |
| 633 | case MsgId_RIL_SIM_SAP_ERROR_RESP: |
| 634 | responsePtr = malloc(sizeof(RIL_SIM_SAP_ERROR_RSP)); |
| 635 | if (responsePtr) { |
| 636 | if (!pb_decode(&stream, RIL_SIM_SAP_ERROR_RSP_fields, responsePtr)) { |
| 637 | RLOGE("Error decoding RIL_SIM_SAP_ERROR_RSP"); |
| 638 | return NULL; |
| 639 | } |
| 640 | } |
| 641 | break; |
| 642 | |
| 643 | case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: |
| 644 | responsePtr = malloc(sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP)); |
| 645 | if (responsePtr) { |
| 646 | if (!pb_decode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_fields, |
| 647 | responsePtr)) { |
| 648 | RLOGE("Error decoding RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP"); |
| 649 | return NULL; |
| 650 | } |
| 651 | } |
| 652 | break; |
| 653 | |
| 654 | default: |
| 655 | break; |
| 656 | } |
| 657 | return responsePtr; |
| 658 | } /* sapDecodeMessage */ |
| 659 | |
| 660 | sp<SapImpl> getSapImpl(RilSapSocket *sapSocket) { |
| 661 | switch (sapSocket->getSocketId()) { |
| 662 | case RIL_SOCKET_1: |
| 663 | RLOGD("getSapImpl: returning sapService[0]"); |
| 664 | return sapService[0]; |
| 665 | #if (SIM_COUNT >= 2) |
| 666 | case RIL_SOCKET_2: |
| 667 | return sapService[1]; |
| 668 | #if (SIM_COUNT >= 3) |
| 669 | case RIL_SOCKET_3: |
| 670 | return sapService[2]; |
| 671 | #if (SIM_COUNT >= 4) |
| 672 | case RIL_SOCKET_4: |
| 673 | return sapService[3]; |
| 674 | #endif |
| 675 | #endif |
| 676 | #endif |
| 677 | default: |
| 678 | return NULL; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | SapResultCode convertApduResponseProtoToHal(RIL_SIM_SAP_APDU_RSP_Response responseProto) { |
| 683 | switch(responseProto) { |
| 684 | case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SUCCESS: |
| 685 | return SapResultCode::SUCCESS; |
| 686 | case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_GENERIC_FAILURE: |
| 687 | return SapResultCode::GENERIC_FAILURE; |
| 688 | case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_NOT_READY: |
| 689 | return SapResultCode::CARD_NOT_ACCESSSIBLE; |
| 690 | case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF: |
| 691 | return SapResultCode::CARD_ALREADY_POWERED_OFF; |
| 692 | case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ABSENT: |
| 693 | return SapResultCode::CARD_REMOVED; |
Wei Wang | 100ac9b | 2017-02-03 14:18:07 -0800 | [diff] [blame] | 694 | default: |
| 695 | return SapResultCode::GENERIC_FAILURE; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 696 | } |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | SapResultCode convertTransferAtrResponseProtoToHal( |
| 700 | RIL_SIM_SAP_TRANSFER_ATR_RSP_Response responseProto) { |
| 701 | switch(responseProto) { |
| 702 | case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SUCCESS: |
| 703 | return SapResultCode::SUCCESS; |
| 704 | case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_GENERIC_FAILURE: |
| 705 | return SapResultCode::GENERIC_FAILURE; |
| 706 | case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF: |
| 707 | return SapResultCode::CARD_ALREADY_POWERED_OFF; |
| 708 | case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ABSENT: |
| 709 | return SapResultCode::CARD_REMOVED; |
| 710 | case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE: |
| 711 | return SapResultCode::DATA_NOT_AVAILABLE; |
Wei Wang | 100ac9b | 2017-02-03 14:18:07 -0800 | [diff] [blame] | 712 | default: |
| 713 | return SapResultCode::GENERIC_FAILURE; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 714 | } |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | SapResultCode convertPowerResponseProtoToHal(RIL_SIM_SAP_POWER_RSP_Response responseProto) { |
| 718 | switch(responseProto) { |
| 719 | case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SUCCESS: |
| 720 | return SapResultCode::SUCCESS; |
| 721 | case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_GENERIC_FAILURE: |
| 722 | return SapResultCode::GENERIC_FAILURE; |
| 723 | case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ABSENT: |
| 724 | return SapResultCode::CARD_REMOVED; |
| 725 | case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF: |
| 726 | return SapResultCode::CARD_ALREADY_POWERED_OFF; |
| 727 | case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_ON: |
| 728 | return SapResultCode::CARD_ALREADY_POWERED_ON; |
Wei Wang | 100ac9b | 2017-02-03 14:18:07 -0800 | [diff] [blame] | 729 | default: |
| 730 | return SapResultCode::GENERIC_FAILURE; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 731 | } |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | SapResultCode convertResetSimResponseProtoToHal(RIL_SIM_SAP_RESET_SIM_RSP_Response responseProto) { |
| 735 | switch(responseProto) { |
| 736 | case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SUCCESS: |
| 737 | return SapResultCode::SUCCESS; |
| 738 | case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_GENERIC_FAILURE: |
| 739 | return SapResultCode::GENERIC_FAILURE; |
| 740 | case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ABSENT: |
| 741 | return SapResultCode::CARD_REMOVED; |
| 742 | case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_NOT_READY: |
| 743 | return SapResultCode::CARD_NOT_ACCESSSIBLE; |
| 744 | case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF: |
| 745 | return SapResultCode::CARD_ALREADY_POWERED_OFF; |
| 746 | } |
| 747 | return SapResultCode::GENERIC_FAILURE; |
| 748 | } |
| 749 | |
| 750 | SapResultCode convertTransferCardReaderStatusResponseProtoToHal( |
| 751 | RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response responseProto) { |
| 752 | switch(responseProto) { |
| 753 | case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SUCCESS: |
| 754 | return SapResultCode::SUCCESS; |
| 755 | case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_GENERIC_FAILURE: |
| 756 | return SapResultCode::GENERIC_FAILURE; |
| 757 | case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE: |
| 758 | return SapResultCode::DATA_NOT_AVAILABLE; |
| 759 | } |
| 760 | return SapResultCode::GENERIC_FAILURE; |
| 761 | } |
| 762 | |
| 763 | void processResponse(MsgHeader *rsp, RilSapSocket *sapSocket, MsgType msgType) { |
| 764 | MsgId msgId = rsp->id; |
| 765 | uint8_t *data = rsp->payload->bytes; |
| 766 | size_t dataLen = rsp->payload->size; |
| 767 | |
| 768 | void *messagePtr = sapDecodeMessage(msgId, msgType, data, dataLen); |
| 769 | |
| 770 | sp<SapImpl> sapImpl = getSapImpl(sapSocket); |
| 771 | if (sapImpl->sapCallback == NULL) { |
| 772 | RLOGE("processResponse: sapCallback == NULL; msgId = %d; msgType = %d", |
| 773 | msgId, msgType); |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | RLOGD("processResponse: sapCallback != NULL; msgId = %d; msgType = %d", |
| 778 | msgId, msgType); |
| 779 | |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 780 | Return<void> retStatus; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 781 | switch (msgId) { |
| 782 | case MsgId_RIL_SIM_SAP_CONNECT: { |
| 783 | RIL_SIM_SAP_CONNECT_RSP *connectRsp = (RIL_SIM_SAP_CONNECT_RSP *)messagePtr; |
| 784 | RLOGD("processResponse: calling sapCallback->connectResponse %d %d %d", |
| 785 | rsp->token, |
| 786 | connectRsp->response, |
| 787 | connectRsp->max_message_size); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 788 | retStatus = sapImpl->sapCallback->connectResponse(rsp->token, |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 789 | (SapConnectRsp)connectRsp->response, |
| 790 | connectRsp->max_message_size); |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | case MsgId_RIL_SIM_SAP_DISCONNECT: |
| 795 | if (msgType == MsgType_RESPONSE) { |
| 796 | RLOGD("processResponse: calling sapCallback->disconnectResponse %d", rsp->token); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 797 | retStatus = sapImpl->sapCallback->disconnectResponse(rsp->token); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 798 | } else { |
| 799 | RIL_SIM_SAP_DISCONNECT_IND *disconnectInd = |
| 800 | (RIL_SIM_SAP_DISCONNECT_IND *)messagePtr; |
| 801 | RLOGD("processResponse: calling sapCallback->disconnectIndication %d %d", |
| 802 | rsp->token, disconnectInd->disconnectType); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 803 | retStatus = sapImpl->sapCallback->disconnectIndication(rsp->token, |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 804 | (SapDisconnectType)disconnectInd->disconnectType); |
| 805 | } |
| 806 | break; |
| 807 | |
| 808 | case MsgId_RIL_SIM_SAP_APDU: { |
| 809 | RIL_SIM_SAP_APDU_RSP *apduRsp = (RIL_SIM_SAP_APDU_RSP *)messagePtr; |
| 810 | SapResultCode apduResponse = convertApduResponseProtoToHal(apduRsp->response); |
| 811 | RLOGD("processResponse: calling sapCallback->apduResponse %d %d", |
| 812 | rsp->token, apduResponse); |
| 813 | hidl_vec<uint8_t> apduRspVec; |
| 814 | if (apduRsp->apduResponse != NULL && apduRsp->apduResponse->size > 0) { |
| 815 | apduRspVec.setToExternal(apduRsp->apduResponse->bytes, apduRsp->apduResponse->size); |
| 816 | } |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 817 | retStatus = sapImpl->sapCallback->apduResponse(rsp->token, apduResponse, apduRspVec); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 818 | break; |
| 819 | } |
| 820 | |
| 821 | case MsgId_RIL_SIM_SAP_TRANSFER_ATR: { |
| 822 | RIL_SIM_SAP_TRANSFER_ATR_RSP *transferAtrRsp = |
| 823 | (RIL_SIM_SAP_TRANSFER_ATR_RSP *)messagePtr; |
| 824 | SapResultCode transferAtrResponse = |
| 825 | convertTransferAtrResponseProtoToHal(transferAtrRsp->response); |
| 826 | RLOGD("processResponse: calling sapCallback->transferAtrResponse %d %d", |
| 827 | rsp->token, transferAtrResponse); |
| 828 | hidl_vec<uint8_t> transferAtrRspVec; |
| 829 | if (transferAtrRsp->atr != NULL && transferAtrRsp->atr->size > 0) { |
| 830 | transferAtrRspVec.setToExternal(transferAtrRsp->atr->bytes, |
| 831 | transferAtrRsp->atr->size); |
| 832 | } |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 833 | retStatus = sapImpl->sapCallback->transferAtrResponse(rsp->token, transferAtrResponse, |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 834 | transferAtrRspVec); |
| 835 | break; |
| 836 | } |
| 837 | |
| 838 | case MsgId_RIL_SIM_SAP_POWER: { |
| 839 | SapResultCode powerResponse = convertPowerResponseProtoToHal( |
| 840 | ((RIL_SIM_SAP_POWER_RSP *)messagePtr)->response); |
| 841 | RLOGD("processResponse: calling sapCallback->powerResponse %d %d", |
| 842 | rsp->token, powerResponse); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 843 | retStatus = sapImpl->sapCallback->powerResponse(rsp->token, powerResponse); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 844 | break; |
| 845 | } |
| 846 | |
| 847 | case MsgId_RIL_SIM_SAP_RESET_SIM: { |
| 848 | SapResultCode resetSimResponse = convertResetSimResponseProtoToHal( |
| 849 | ((RIL_SIM_SAP_RESET_SIM_RSP *)messagePtr)->response); |
| 850 | RLOGD("processResponse: calling sapCallback->resetSimResponse %d %d", |
| 851 | rsp->token, resetSimResponse); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 852 | retStatus = sapImpl->sapCallback->resetSimResponse(rsp->token, resetSimResponse); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 853 | break; |
| 854 | } |
| 855 | |
| 856 | case MsgId_RIL_SIM_SAP_STATUS: { |
| 857 | RIL_SIM_SAP_STATUS_IND *statusInd = (RIL_SIM_SAP_STATUS_IND *)messagePtr; |
| 858 | RLOGD("processResponse: calling sapCallback->statusIndication %d %d", |
| 859 | rsp->token, statusInd->statusChange); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 860 | retStatus = sapImpl->sapCallback->statusIndication(rsp->token, |
| 861 | (SapStatus)statusInd->statusChange); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | |
| 865 | case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: { |
| 866 | RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *transferStatusRsp = |
| 867 | (RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *)messagePtr; |
| 868 | SapResultCode transferCardReaderStatusResponse = |
| 869 | convertTransferCardReaderStatusResponseProtoToHal( |
| 870 | transferStatusRsp->response); |
| 871 | RLOGD("processResponse: calling sapCallback->transferCardReaderStatusResponse %d %d %d", |
| 872 | rsp->token, |
| 873 | transferCardReaderStatusResponse, |
| 874 | transferStatusRsp->CardReaderStatus); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 875 | retStatus = sapImpl->sapCallback->transferCardReaderStatusResponse(rsp->token, |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 876 | transferCardReaderStatusResponse, |
| 877 | transferStatusRsp->CardReaderStatus); |
| 878 | break; |
| 879 | } |
| 880 | |
| 881 | case MsgId_RIL_SIM_SAP_ERROR_RESP: { |
| 882 | RLOGD("processResponse: calling sapCallback->errorResponse %d", rsp->token); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 883 | retStatus = sapImpl->sapCallback->errorResponse(rsp->token); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 884 | break; |
| 885 | } |
| 886 | |
| 887 | case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: { |
| 888 | SapResultCode setTransferProtocolResponse; |
| 889 | if (((RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP *)messagePtr)->response == |
| 890 | RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_Response_RIL_E_SUCCESS) { |
| 891 | setTransferProtocolResponse = SapResultCode::SUCCESS; |
| 892 | } else { |
| 893 | setTransferProtocolResponse = SapResultCode::NOT_SUPPORTED; |
| 894 | } |
| 895 | RLOGD("processResponse: calling sapCallback->transferProtocolResponse %d %d", |
| 896 | rsp->token, setTransferProtocolResponse); |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 897 | retStatus = sapImpl->sapCallback->transferProtocolResponse(rsp->token, |
| 898 | setTransferProtocolResponse); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 899 | break; |
| 900 | } |
| 901 | |
| 902 | default: |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 903 | return; |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 904 | } |
Amit Mahajan | e40b5ac | 2017-02-13 11:58:11 -0800 | [diff] [blame^] | 905 | sapImpl->checkReturnStatus(retStatus); |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | void sap::processResponse(MsgHeader *rsp, RilSapSocket *sapSocket) { |
| 909 | processResponse(rsp, sapSocket, MsgType_RESPONSE); |
| 910 | } |
| 911 | |
| 912 | void sap::processUnsolResponse(MsgHeader *rsp, RilSapSocket *sapSocket) { |
| 913 | processResponse(rsp, sapSocket, MsgType_UNSOL_RESPONSE); |
| 914 | } |
| 915 | |
| 916 | void sap::registerService(RIL_RadioFunctions *callbacks) { |
| 917 | using namespace android::hardware; |
| 918 | int simCount = 1; |
Wei Wang | 100ac9b | 2017-02-03 14:18:07 -0800 | [diff] [blame] | 919 | const char *serviceNames[] = { |
Amit Mahajan | 2056e96 | 2016-11-29 16:48:54 -0800 | [diff] [blame] | 920 | "sap_uim_socket1" |
| 921 | #if (SIM_COUNT >= 2) |
| 922 | , "sap_uim_socket2" |
| 923 | #if (SIM_COUNT >= 3) |
| 924 | , "sap_uim_socket3" |
| 925 | #if (SIM_COUNT >= 4) |
| 926 | , "sap_uim_socket4" |
| 927 | #endif |
| 928 | #endif |
| 929 | #endif |
| 930 | }; |
| 931 | |
| 932 | RIL_SOCKET_ID socketIds[] = { |
| 933 | RIL_SOCKET_1 |
| 934 | #if (SIM_COUNT >= 2) |
| 935 | , RIL_SOCKET_2 |
| 936 | #if (SIM_COUNT >= 3) |
| 937 | , RIL_SOCKET_3 |
| 938 | #if (SIM_COUNT >= 4) |
| 939 | , RIL_SOCKET_4 |
| 940 | #endif |
| 941 | #endif |
| 942 | #endif |
| 943 | }; |
| 944 | #if (SIM_COUNT >= 2) |
| 945 | simCount = SIM_COUNT; |
| 946 | #endif |
| 947 | |
| 948 | for (int i = 0; i < simCount; i++) { |
| 949 | sapService[i] = new SapImpl; |
| 950 | sapService[i]->slotId = i; |
| 951 | sapService[i]->rilSocketId = socketIds[i]; |
| 952 | RLOGD("registerService: starting ISap %s for slotId %d", serviceNames[i], i); |
| 953 | android::status_t status = sapService[i]->registerAsService(serviceNames[i]); |
| 954 | RLOGD("registerService: started ISap %s status %d", serviceNames[i], status); |
| 955 | } |
Wei Wang | 100ac9b | 2017-02-03 14:18:07 -0800 | [diff] [blame] | 956 | } |