2 * Copyright (c) 2016 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 LOG_TAG "RIL_SAP"
19 #include <android/hardware/radio/1.1/ISap.h>
21 #include <hwbinder/IPCThreadState.h>
22 #include <hwbinder/ProcessState.h>
23 #include <sap_service.h>
24 #include "pb_decode.h"
25 #include "pb_encode.h"
27 using namespace android::hardware::radio::V1_0;
28 using ::android::hardware::Return;
29 using ::android::hardware::hidl_vec;
30 using ::android::hardware::hidl_array;
31 using ::android::hardware::Void;
32 using android::CommandInfo;
33 using android::RequestInfo;
34 using android::requestToString;
40 sp<SapImpl> sapService[SIM_COUNT];
42 sp<SapImpl> sapService[1];
45 struct SapImpl : public android::hardware::radio::V1_1::ISap {
47 sp<ISapCallback> sapCallback;
48 RIL_SOCKET_ID rilSocketId;
50 Return<void> setCallback(const ::android::sp<ISapCallback>& sapCallbackParam);
52 Return<void> connectReq(int32_t token, int32_t maxMsgSize);
54 Return<void> disconnectReq(int32_t token);
56 Return<void> apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command);
58 Return<void> transferAtrReq(int32_t token);
60 Return<void> powerReq(int32_t token, bool state);
62 Return<void> resetSimReq(int32_t token);
64 Return<void> transferCardReaderStatusReq(int32_t token);
66 Return<void> setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol);
68 MsgHeader* createMsgHeader(MsgId msgId, int32_t token);
70 Return<void> addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen, uint8_t *reqPtr);
72 void sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...);
74 void checkReturnStatus(Return<void>& ret);
77 void SapImpl::checkReturnStatus(Return<void>& ret) {
78 if (ret.isOk() == false) {
79 RLOGE("checkReturnStatus: unable to call response/indication callback: %s",
80 ret.description().c_str());
81 // Remote process (SapRilReceiver.java) hosting the callback must be dead. Reset the
82 // callback object; there's no other recovery to be done here. When the client process is
83 // back up, it will call setCallback()
88 Return<void> SapImpl::setCallback(const ::android::sp<ISapCallback>& sapCallbackParam) {
89 RLOGD("SapImpl::setCallback for slotId %d", slotId);
90 sapCallback = sapCallbackParam;
94 MsgHeader* SapImpl::createMsgHeader(MsgId msgId, int32_t token) {
95 // Memory for msg will be freed by RilSapSocket::onRequestComplete()
96 MsgHeader *msg = (MsgHeader *)calloc(1, sizeof(MsgHeader));
101 msg->type = MsgType_REQUEST;
103 msg->error = Error_RIL_E_SUCCESS;
107 Return<void> SapImpl::addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen,
109 msg->payload = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + reqLen);
110 if (msg->payload == NULL) {
111 sendFailedResponse(msg->id, msg->token, 2, reqPtr, msg);
114 msg->payload->size = reqLen;
115 memcpy(msg->payload->bytes, reqPtr, reqLen);
117 RilSapSocket *sapSocket = RilSapSocket::getSocketById(rilSocketId);
119 RLOGD("SapImpl::addPayloadAndDispatchRequest: calling dispatchRequest");
120 sapSocket->dispatchRequest(msg);
122 RLOGE("SapImpl::addPayloadAndDispatchRequest: sapSocket is null");
123 sendFailedResponse(msg->id, msg->token, 3, msg->payload, reqPtr, msg);
131 void SapImpl::sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...) {
133 va_start(ap, numPointers);
134 for (int i = 0; i < numPointers; i++) {
135 void *ptr = va_arg(ap, void *);
139 Return<void> retStatus;
141 case MsgId_RIL_SIM_SAP_CONNECT:
142 retStatus = sapCallback->connectResponse(token, SapConnectRsp::CONNECT_FAILURE, 0);
145 case MsgId_RIL_SIM_SAP_DISCONNECT:
146 retStatus = sapCallback->disconnectResponse(token);
149 case MsgId_RIL_SIM_SAP_APDU: {
150 hidl_vec<uint8_t> apduRsp;
151 retStatus = sapCallback->apduResponse(token, SapResultCode::GENERIC_FAILURE, apduRsp);
155 case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
156 hidl_vec<uint8_t> atr;
157 retStatus = sapCallback->transferAtrResponse(token, SapResultCode::GENERIC_FAILURE,
162 case MsgId_RIL_SIM_SAP_POWER:
163 retStatus = sapCallback->powerResponse(token, SapResultCode::GENERIC_FAILURE);
166 case MsgId_RIL_SIM_SAP_RESET_SIM:
167 retStatus = sapCallback->resetSimResponse(token, SapResultCode::GENERIC_FAILURE);
170 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
171 retStatus = sapCallback->transferCardReaderStatusResponse(token,
172 SapResultCode::GENERIC_FAILURE, 0);
175 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
176 retStatus = sapCallback->transferProtocolResponse(token, SapResultCode::NOT_SUPPORTED);
182 sapService[slotId]->checkReturnStatus(retStatus);
185 Return<void> SapImpl::connectReq(int32_t token, int32_t maxMsgSize) {
186 RLOGD("SapImpl::connectReq");
187 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_CONNECT, token);
189 RLOGE("SapImpl::connectReq: Error allocating memory for msg");
190 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 0);
194 /***** Encode RIL_SIM_SAP_CONNECT_REQ *****/
195 RIL_SIM_SAP_CONNECT_REQ req;
196 memset(&req, 0, sizeof(RIL_SIM_SAP_CONNECT_REQ));
197 req.max_message_size = maxMsgSize;
199 size_t encodedSize = 0;
200 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
201 RLOGE("SapImpl::connectReq: Error getting encoded size for RIL_SIM_SAP_CONNECT_REQ");
202 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
206 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
207 if (buffer == NULL) {
208 RLOGE("SapImpl::connectReq: Error allocating memory for buffer");
209 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
212 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
214 RLOGD("SapImpl::connectReq calling pb_encode");
215 if (!pb_encode(&stream, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
216 RLOGE("SapImpl::connectReq: Error encoding RIL_SIM_SAP_CONNECT_REQ");
217 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 2, buffer, msg);
220 /***** Encode RIL_SIM_SAP_CONNECT_REQ done *****/
222 /* encoded req is payload */
223 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
226 Return<void> SapImpl::disconnectReq(int32_t token) {
227 RLOGD("SapImpl::disconnectReq");
228 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_DISCONNECT, token);
230 RLOGE("SapImpl::disconnectReq: Error allocating memory for msg");
231 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 0);
235 /***** Encode RIL_SIM_SAP_DISCONNECT_REQ *****/
236 RIL_SIM_SAP_DISCONNECT_REQ req;
237 memset(&req, 0, sizeof(RIL_SIM_SAP_DISCONNECT_REQ));
239 size_t encodedSize = 0;
240 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
241 RLOGE("SapImpl::disconnectReq: Error getting encoded size for RIL_SIM_SAP_DISCONNECT_REQ");
242 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
246 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
247 if (buffer == NULL) {
248 RLOGE("SapImpl::disconnectReq: Error allocating memory for buffer");
249 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
253 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
255 RLOGD("SapImpl::disconnectReq calling pb_encode");
256 if (!pb_encode(&stream, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
257 RLOGE("SapImpl::disconnectReq: Error encoding RIL_SIM_SAP_DISCONNECT_REQ");
258 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 2, buffer, msg);
261 /***** Encode RIL_SIM_SAP_DISCONNECT_REQ done *****/
263 /* encoded req is payload */
264 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
267 Return<void> SapImpl::apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command) {
268 RLOGD("SapImpl::apduReq");
269 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_APDU, token);
271 RLOGE("SapImpl::apduReq: Error allocating memory for msg");
272 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 0);
276 /***** Encode RIL_SIM_SAP_APDU_REQ *****/
277 RIL_SIM_SAP_APDU_REQ req;
278 memset(&req, 0, sizeof(RIL_SIM_SAP_APDU_REQ));
279 req.type = (RIL_SIM_SAP_APDU_REQ_Type)type;
281 if (command.size() > 0) {
282 req.command = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + command.size());
283 if (req.command == NULL) {
284 RLOGE("SapImpl::apduReq: Error allocating memory for req.command");
285 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 1, msg);
288 req.command->size = command.size();
289 memcpy(req.command->bytes, command.data(), command.size());
292 size_t encodedSize = 0;
293 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
294 RLOGE("SapImpl::apduReq: Error getting encoded size for RIL_SIM_SAP_APDU_REQ");
295 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
299 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
300 if (buffer == NULL) {
301 RLOGE("SapImpl::apduReq: Error allocating memory for buffer");
302 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
306 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
308 RLOGD("SapImpl::apduReq calling pb_encode");
309 if (!pb_encode(&stream, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
310 RLOGE("SapImpl::apduReq: Error encoding RIL_SIM_SAP_APDU_REQ");
311 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 3, req.command, buffer, msg);
314 /***** Encode RIL_SIM_SAP_APDU_REQ done *****/
316 /* encoded req is payload */
317 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
320 Return<void> SapImpl::transferAtrReq(int32_t token) {
321 RLOGD("SapImpl::transferAtrReq");
322 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token);
324 RLOGE("SapImpl::transferAtrReq: Error allocating memory for msg");
325 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 0);
329 /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ *****/
330 RIL_SIM_SAP_TRANSFER_ATR_REQ req;
331 memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_ATR_REQ));
333 size_t encodedSize = 0;
334 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
335 RLOGE("SapImpl::transferAtrReq: Error getting encoded size for "
336 "RIL_SIM_SAP_TRANSFER_ATR_REQ");
337 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
341 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
342 if (buffer == NULL) {
343 RLOGE("SapImpl::transferAtrReq: Error allocating memory for buffer");
344 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
348 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
350 RLOGD("SapImpl::transferAtrReq calling pb_encode");
351 if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
352 RLOGE("SapImpl::transferAtrReq: Error encoding RIL_SIM_SAP_TRANSFER_ATR_REQ");
353 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 2, buffer, msg);
356 /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ done *****/
358 /* encoded req is payload */
359 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
362 Return<void> SapImpl::powerReq(int32_t token, bool state) {
363 RLOGD("SapImpl::powerReq");
364 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_POWER, token);
366 RLOGE("SapImpl::powerReq: Error allocating memory for msg");
367 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 0);
371 /***** Encode RIL_SIM_SAP_POWER_REQ *****/
372 RIL_SIM_SAP_POWER_REQ req;
373 memset(&req, 0, sizeof(RIL_SIM_SAP_POWER_REQ));
376 size_t encodedSize = 0;
377 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
378 RLOGE("SapImpl::powerReq: Error getting encoded size for RIL_SIM_SAP_POWER_REQ");
379 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
383 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
384 if (buffer == NULL) {
385 RLOGE("SapImpl::powerReq: Error allocating memory for buffer");
386 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
390 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
392 RLOGD("SapImpl::powerReq calling pb_encode");
393 if (!pb_encode(&stream, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
394 RLOGE("SapImpl::powerReq: Error encoding RIL_SIM_SAP_POWER_REQ");
395 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 2, buffer, msg);
398 /***** Encode RIL_SIM_SAP_POWER_REQ done *****/
400 /* encoded req is payload */
401 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
404 Return<void> SapImpl::resetSimReq(int32_t token) {
405 RLOGD("SapImpl::resetSimReq");
406 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_RESET_SIM, token);
408 RLOGE("SapImpl::resetSimReq: Error allocating memory for msg");
409 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 0);
413 /***** Encode RIL_SIM_SAP_RESET_SIM_REQ *****/
414 RIL_SIM_SAP_RESET_SIM_REQ req;
415 memset(&req, 0, sizeof(RIL_SIM_SAP_RESET_SIM_REQ));
417 size_t encodedSize = 0;
418 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
419 RLOGE("SapImpl::resetSimReq: Error getting encoded size for RIL_SIM_SAP_RESET_SIM_REQ");
420 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
424 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
425 if (buffer == NULL) {
426 RLOGE("SapImpl::resetSimReq: Error allocating memory for buffer");
427 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
431 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
433 RLOGD("SapImpl::resetSimReq calling pb_encode");
434 if (!pb_encode(&stream, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
435 RLOGE("SapImpl::resetSimReq: Error encoding RIL_SIM_SAP_RESET_SIM_REQ");
436 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 2, buffer, msg);
439 /***** Encode RIL_SIM_SAP_RESET_SIM_REQ done *****/
441 /* encoded req is payload */
442 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
445 Return<void> SapImpl::transferCardReaderStatusReq(int32_t token) {
446 RLOGD("SapImpl::transferCardReaderStatusReq");
447 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token);
449 RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for msg");
450 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 0);
454 /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ *****/
455 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ req;
456 memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ));
458 size_t encodedSize = 0;
459 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields,
461 RLOGE("SapImpl::transferCardReaderStatusReq: Error getting encoded size for "
462 "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
463 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
467 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
468 if (buffer == NULL) {
469 RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for buffer");
470 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
474 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
476 RLOGD("SapImpl::transferCardReaderStatusReq calling pb_encode");
477 if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields, &req)) {
478 RLOGE("SapImpl::transferCardReaderStatusReq: Error encoding "
479 "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
480 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 2, buffer, msg);
483 /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ done *****/
485 /* encoded req is payload */
486 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
489 Return<void> SapImpl::setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol) {
490 RLOGD("SapImpl::setTransferProtocolReq");
491 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token);
493 RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for msg");
494 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 0);
498 /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ *****/
499 RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ req;
500 memset(&req, 0, sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ));
501 req.protocol = (RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_Protocol)transferProtocol;
503 size_t encodedSize = 0;
504 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
505 RLOGE("SapImpl::setTransferProtocolReq: Error getting encoded size for "
506 "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
507 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
511 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
512 if (buffer == NULL) {
513 RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for buffer");
514 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
518 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
520 RLOGD("SapImpl::setTransferProtocolReq calling pb_encode");
521 if (!pb_encode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
522 RLOGE("SapImpl::setTransferProtocolReq: Error encoding "
523 "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
524 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 2, buffer, msg);
527 /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ done *****/
529 /* encoded req is payload */
530 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
533 void *sapDecodeMessage(MsgId msgId, MsgType msgType, uint8_t *payloadPtr, size_t payloadLen) {
534 void *responsePtr = NULL;
537 /* Create the stream */
538 stream = pb_istream_from_buffer((uint8_t *)payloadPtr, payloadLen);
540 /* Decode based on the message id */
543 case MsgId_RIL_SIM_SAP_CONNECT:
544 responsePtr = malloc(sizeof(RIL_SIM_SAP_CONNECT_RSP));
546 if (!pb_decode(&stream, RIL_SIM_SAP_CONNECT_RSP_fields, responsePtr)) {
547 RLOGE("Error decoding RIL_SIM_SAP_CONNECT_RSP");
553 case MsgId_RIL_SIM_SAP_DISCONNECT:
554 if (msgType == MsgType_RESPONSE) {
555 responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_RSP));
557 if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_RSP_fields, responsePtr)) {
558 RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_RSP");
563 responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_IND));
565 if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_IND_fields, responsePtr)) {
566 RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_IND");
573 case MsgId_RIL_SIM_SAP_APDU:
574 responsePtr = malloc(sizeof(RIL_SIM_SAP_APDU_RSP));
576 if (!pb_decode(&stream, RIL_SIM_SAP_APDU_RSP_fields, responsePtr)) {
577 RLOGE("Error decoding RIL_SIM_SAP_APDU_RSP");
583 case MsgId_RIL_SIM_SAP_TRANSFER_ATR:
584 responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_ATR_RSP));
586 if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_ATR_RSP_fields, responsePtr)) {
587 RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_ATR_RSP");
593 case MsgId_RIL_SIM_SAP_POWER:
594 responsePtr = malloc(sizeof(RIL_SIM_SAP_POWER_RSP));
596 if (!pb_decode(&stream, RIL_SIM_SAP_POWER_RSP_fields, responsePtr)) {
597 RLOGE("Error decoding RIL_SIM_SAP_POWER_RSP");
603 case MsgId_RIL_SIM_SAP_RESET_SIM:
604 responsePtr = malloc(sizeof(RIL_SIM_SAP_RESET_SIM_RSP));
606 if (!pb_decode(&stream, RIL_SIM_SAP_RESET_SIM_RSP_fields, responsePtr)) {
607 RLOGE("Error decoding RIL_SIM_SAP_RESET_SIM_RSP");
613 case MsgId_RIL_SIM_SAP_STATUS:
614 responsePtr = malloc(sizeof(RIL_SIM_SAP_STATUS_IND));
616 if (!pb_decode(&stream, RIL_SIM_SAP_STATUS_IND_fields, responsePtr)) {
617 RLOGE("Error decoding RIL_SIM_SAP_STATUS_IND");
623 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
624 responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP));
626 if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_fields,
628 RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP");
634 case MsgId_RIL_SIM_SAP_ERROR_RESP:
635 responsePtr = malloc(sizeof(RIL_SIM_SAP_ERROR_RSP));
637 if (!pb_decode(&stream, RIL_SIM_SAP_ERROR_RSP_fields, responsePtr)) {
638 RLOGE("Error decoding RIL_SIM_SAP_ERROR_RSP");
644 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
645 responsePtr = malloc(sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP));
647 if (!pb_decode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_fields,
649 RLOGE("Error decoding RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP");
659 } /* sapDecodeMessage */
661 sp<SapImpl> getSapImpl(RilSapSocket *sapSocket) {
662 switch (sapSocket->getSocketId()) {
664 RLOGD("getSapImpl: returning sapService[0]");
665 return sapService[0];
668 return sapService[1];
671 return sapService[2];
674 return sapService[3];
683 SapResultCode convertApduResponseProtoToHal(RIL_SIM_SAP_APDU_RSP_Response responseProto) {
684 switch(responseProto) {
685 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SUCCESS:
686 return SapResultCode::SUCCESS;
687 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_GENERIC_FAILURE:
688 return SapResultCode::GENERIC_FAILURE;
689 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_NOT_READY:
690 return SapResultCode::CARD_NOT_ACCESSSIBLE;
691 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
692 return SapResultCode::CARD_ALREADY_POWERED_OFF;
693 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ABSENT:
694 return SapResultCode::CARD_REMOVED;
696 return SapResultCode::GENERIC_FAILURE;
700 SapResultCode convertTransferAtrResponseProtoToHal(
701 RIL_SIM_SAP_TRANSFER_ATR_RSP_Response responseProto) {
702 switch(responseProto) {
703 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SUCCESS:
704 return SapResultCode::SUCCESS;
705 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_GENERIC_FAILURE:
706 return SapResultCode::GENERIC_FAILURE;
707 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
708 return SapResultCode::CARD_ALREADY_POWERED_OFF;
709 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ABSENT:
710 return SapResultCode::CARD_REMOVED;
711 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
712 return SapResultCode::DATA_NOT_AVAILABLE;
714 return SapResultCode::GENERIC_FAILURE;
718 SapResultCode convertPowerResponseProtoToHal(RIL_SIM_SAP_POWER_RSP_Response responseProto) {
719 switch(responseProto) {
720 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SUCCESS:
721 return SapResultCode::SUCCESS;
722 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_GENERIC_FAILURE:
723 return SapResultCode::GENERIC_FAILURE;
724 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ABSENT:
725 return SapResultCode::CARD_REMOVED;
726 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
727 return SapResultCode::CARD_ALREADY_POWERED_OFF;
728 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_ON:
729 return SapResultCode::CARD_ALREADY_POWERED_ON;
731 return SapResultCode::GENERIC_FAILURE;
735 SapResultCode convertResetSimResponseProtoToHal(RIL_SIM_SAP_RESET_SIM_RSP_Response responseProto) {
736 switch(responseProto) {
737 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SUCCESS:
738 return SapResultCode::SUCCESS;
739 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_GENERIC_FAILURE:
740 return SapResultCode::GENERIC_FAILURE;
741 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ABSENT:
742 return SapResultCode::CARD_REMOVED;
743 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_NOT_READY:
744 return SapResultCode::CARD_NOT_ACCESSSIBLE;
745 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
746 return SapResultCode::CARD_ALREADY_POWERED_OFF;
748 return SapResultCode::GENERIC_FAILURE;
751 SapResultCode convertTransferCardReaderStatusResponseProtoToHal(
752 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response responseProto) {
753 switch(responseProto) {
754 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SUCCESS:
755 return SapResultCode::SUCCESS;
756 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_GENERIC_FAILURE:
757 return SapResultCode::GENERIC_FAILURE;
758 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
759 return SapResultCode::DATA_NOT_AVAILABLE;
761 return SapResultCode::GENERIC_FAILURE;
764 void processResponse(MsgHeader *rsp, RilSapSocket *sapSocket, MsgType msgType) {
765 MsgId msgId = rsp->id;
766 uint8_t *data = rsp->payload->bytes;
767 size_t dataLen = rsp->payload->size;
769 void *messagePtr = sapDecodeMessage(msgId, msgType, data, dataLen);
771 sp<SapImpl> sapImpl = getSapImpl(sapSocket);
772 if (sapImpl->sapCallback == NULL) {
773 RLOGE("processResponse: sapCallback == NULL; msgId = %d; msgType = %d",
778 RLOGD("processResponse: sapCallback != NULL; msgId = %d; msgType = %d",
781 Return<void> retStatus;
783 case MsgId_RIL_SIM_SAP_CONNECT: {
784 RIL_SIM_SAP_CONNECT_RSP *connectRsp = (RIL_SIM_SAP_CONNECT_RSP *)messagePtr;
785 RLOGD("processResponse: calling sapCallback->connectResponse %d %d %d",
787 connectRsp->response,
788 connectRsp->max_message_size);
789 retStatus = sapImpl->sapCallback->connectResponse(rsp->token,
790 (SapConnectRsp)connectRsp->response,
791 connectRsp->max_message_size);
795 case MsgId_RIL_SIM_SAP_DISCONNECT:
796 if (msgType == MsgType_RESPONSE) {
797 RLOGD("processResponse: calling sapCallback->disconnectResponse %d", rsp->token);
798 retStatus = sapImpl->sapCallback->disconnectResponse(rsp->token);
800 RIL_SIM_SAP_DISCONNECT_IND *disconnectInd =
801 (RIL_SIM_SAP_DISCONNECT_IND *)messagePtr;
802 RLOGD("processResponse: calling sapCallback->disconnectIndication %d %d",
803 rsp->token, disconnectInd->disconnectType);
804 retStatus = sapImpl->sapCallback->disconnectIndication(rsp->token,
805 (SapDisconnectType)disconnectInd->disconnectType);
809 case MsgId_RIL_SIM_SAP_APDU: {
810 RIL_SIM_SAP_APDU_RSP *apduRsp = (RIL_SIM_SAP_APDU_RSP *)messagePtr;
811 SapResultCode apduResponse = convertApduResponseProtoToHal(apduRsp->response);
812 RLOGD("processResponse: calling sapCallback->apduResponse %d %d",
813 rsp->token, apduResponse);
814 hidl_vec<uint8_t> apduRspVec;
815 if (apduRsp->apduResponse != NULL && apduRsp->apduResponse->size > 0) {
816 apduRspVec.setToExternal(apduRsp->apduResponse->bytes, apduRsp->apduResponse->size);
818 retStatus = sapImpl->sapCallback->apduResponse(rsp->token, apduResponse, apduRspVec);
822 case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
823 RIL_SIM_SAP_TRANSFER_ATR_RSP *transferAtrRsp =
824 (RIL_SIM_SAP_TRANSFER_ATR_RSP *)messagePtr;
825 SapResultCode transferAtrResponse =
826 convertTransferAtrResponseProtoToHal(transferAtrRsp->response);
827 RLOGD("processResponse: calling sapCallback->transferAtrResponse %d %d",
828 rsp->token, transferAtrResponse);
829 hidl_vec<uint8_t> transferAtrRspVec;
830 if (transferAtrRsp->atr != NULL && transferAtrRsp->atr->size > 0) {
831 transferAtrRspVec.setToExternal(transferAtrRsp->atr->bytes,
832 transferAtrRsp->atr->size);
834 retStatus = sapImpl->sapCallback->transferAtrResponse(rsp->token, transferAtrResponse,
839 case MsgId_RIL_SIM_SAP_POWER: {
840 SapResultCode powerResponse = convertPowerResponseProtoToHal(
841 ((RIL_SIM_SAP_POWER_RSP *)messagePtr)->response);
842 RLOGD("processResponse: calling sapCallback->powerResponse %d %d",
843 rsp->token, powerResponse);
844 retStatus = sapImpl->sapCallback->powerResponse(rsp->token, powerResponse);
848 case MsgId_RIL_SIM_SAP_RESET_SIM: {
849 SapResultCode resetSimResponse = convertResetSimResponseProtoToHal(
850 ((RIL_SIM_SAP_RESET_SIM_RSP *)messagePtr)->response);
851 RLOGD("processResponse: calling sapCallback->resetSimResponse %d %d",
852 rsp->token, resetSimResponse);
853 retStatus = sapImpl->sapCallback->resetSimResponse(rsp->token, resetSimResponse);
857 case MsgId_RIL_SIM_SAP_STATUS: {
858 RIL_SIM_SAP_STATUS_IND *statusInd = (RIL_SIM_SAP_STATUS_IND *)messagePtr;
859 RLOGD("processResponse: calling sapCallback->statusIndication %d %d",
860 rsp->token, statusInd->statusChange);
861 retStatus = sapImpl->sapCallback->statusIndication(rsp->token,
862 (SapStatus)statusInd->statusChange);
866 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: {
867 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *transferStatusRsp =
868 (RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *)messagePtr;
869 SapResultCode transferCardReaderStatusResponse =
870 convertTransferCardReaderStatusResponseProtoToHal(
871 transferStatusRsp->response);
872 RLOGD("processResponse: calling sapCallback->transferCardReaderStatusResponse %d %d %d",
874 transferCardReaderStatusResponse,
875 transferStatusRsp->CardReaderStatus);
876 retStatus = sapImpl->sapCallback->transferCardReaderStatusResponse(rsp->token,
877 transferCardReaderStatusResponse,
878 transferStatusRsp->CardReaderStatus);
882 case MsgId_RIL_SIM_SAP_ERROR_RESP: {
883 RLOGD("processResponse: calling sapCallback->errorResponse %d", rsp->token);
884 retStatus = sapImpl->sapCallback->errorResponse(rsp->token);
888 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: {
889 SapResultCode setTransferProtocolResponse;
890 if (((RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP *)messagePtr)->response ==
891 RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_Response_RIL_E_SUCCESS) {
892 setTransferProtocolResponse = SapResultCode::SUCCESS;
894 setTransferProtocolResponse = SapResultCode::NOT_SUPPORTED;
896 RLOGD("processResponse: calling sapCallback->transferProtocolResponse %d %d",
897 rsp->token, setTransferProtocolResponse);
898 retStatus = sapImpl->sapCallback->transferProtocolResponse(rsp->token,
899 setTransferProtocolResponse);
906 sapImpl->checkReturnStatus(retStatus);
909 void sap::processResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
910 processResponse(rsp, sapSocket, MsgType_RESPONSE);
913 void sap::processUnsolResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
914 processResponse(rsp, sapSocket, MsgType_UNSOL_RESPONSE);
917 void sap::registerService(RIL_RadioFunctions *callbacks) {
918 using namespace android::hardware;
920 const char *serviceNames[] = {
921 android::RIL_getServiceName()
933 RIL_SOCKET_ID socketIds[] = {
946 simCount = SIM_COUNT;
949 for (int i = 0; i < simCount; i++) {
950 sapService[i] = new SapImpl;
951 sapService[i]->slotId = i;
952 sapService[i]->rilSocketId = socketIds[i];
953 RLOGD("registerService: starting ISap %s for slotId %d", serviceNames[i], i);
954 android::status_t status = sapService[i]->registerAsService(serviceNames[i]);
955 RLOGD("registerService: started ISap %s status %d", serviceNames[i], status);