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 pb_bytes_array_t *payload = (pb_bytes_array_t *) malloc(sizeof(pb_bytes_array_t) - 1 + reqLen);
110 if (payload == NULL) {
111 sendFailedResponse(msg->id, msg->token, 2, reqPtr, msg);
115 msg->payload = payload;
116 msg->payload->size = reqLen;
117 memcpy(msg->payload->bytes, reqPtr, reqLen);
119 RilSapSocket *sapSocket = RilSapSocket::getSocketById(rilSocketId);
121 RLOGD("SapImpl::addPayloadAndDispatchRequest: calling dispatchRequest");
122 sapSocket->dispatchRequest(msg);
124 RLOGE("SapImpl::addPayloadAndDispatchRequest: sapSocket is null");
125 sendFailedResponse(msg->id, msg->token, 3, payload, reqPtr, msg);
133 void SapImpl::sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...) {
135 va_start(ap, numPointers);
136 for (int i = 0; i < numPointers; i++) {
137 void *ptr = va_arg(ap, void *);
141 Return<void> retStatus;
143 case MsgId_RIL_SIM_SAP_CONNECT:
144 retStatus = sapCallback->connectResponse(token, SapConnectRsp::CONNECT_FAILURE, 0);
147 case MsgId_RIL_SIM_SAP_DISCONNECT:
148 retStatus = sapCallback->disconnectResponse(token);
151 case MsgId_RIL_SIM_SAP_APDU: {
152 hidl_vec<uint8_t> apduRsp;
153 retStatus = sapCallback->apduResponse(token, SapResultCode::GENERIC_FAILURE, apduRsp);
157 case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
158 hidl_vec<uint8_t> atr;
159 retStatus = sapCallback->transferAtrResponse(token, SapResultCode::GENERIC_FAILURE,
164 case MsgId_RIL_SIM_SAP_POWER:
165 retStatus = sapCallback->powerResponse(token, SapResultCode::GENERIC_FAILURE);
168 case MsgId_RIL_SIM_SAP_RESET_SIM:
169 retStatus = sapCallback->resetSimResponse(token, SapResultCode::GENERIC_FAILURE);
172 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
173 retStatus = sapCallback->transferCardReaderStatusResponse(token,
174 SapResultCode::GENERIC_FAILURE, 0);
177 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
178 retStatus = sapCallback->transferProtocolResponse(token, SapResultCode::NOT_SUPPORTED);
184 sapService[slotId]->checkReturnStatus(retStatus);
187 Return<void> SapImpl::connectReq(int32_t token, int32_t maxMsgSize) {
188 RLOGD("SapImpl::connectReq");
189 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_CONNECT, token);
191 RLOGE("SapImpl::connectReq: Error allocating memory for msg");
192 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 0);
196 /***** Encode RIL_SIM_SAP_CONNECT_REQ *****/
197 RIL_SIM_SAP_CONNECT_REQ req;
198 memset(&req, 0, sizeof(RIL_SIM_SAP_CONNECT_REQ));
199 req.max_message_size = maxMsgSize;
201 size_t encodedSize = 0;
202 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
203 RLOGE("SapImpl::connectReq: Error getting encoded size for RIL_SIM_SAP_CONNECT_REQ");
204 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
208 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
209 if (buffer == NULL) {
210 RLOGE("SapImpl::connectReq: Error allocating memory for buffer");
211 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
214 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
216 RLOGD("SapImpl::connectReq calling pb_encode");
217 if (!pb_encode(&stream, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
218 RLOGE("SapImpl::connectReq: Error encoding RIL_SIM_SAP_CONNECT_REQ");
219 sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 2, buffer, msg);
222 /***** Encode RIL_SIM_SAP_CONNECT_REQ done *****/
224 /* encoded req is payload */
225 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
228 Return<void> SapImpl::disconnectReq(int32_t token) {
229 RLOGD("SapImpl::disconnectReq");
230 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_DISCONNECT, token);
232 RLOGE("SapImpl::disconnectReq: Error allocating memory for msg");
233 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 0);
237 /***** Encode RIL_SIM_SAP_DISCONNECT_REQ *****/
238 RIL_SIM_SAP_DISCONNECT_REQ req;
239 memset(&req, 0, sizeof(RIL_SIM_SAP_DISCONNECT_REQ));
241 size_t encodedSize = 0;
242 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
243 RLOGE("SapImpl::disconnectReq: Error getting encoded size for RIL_SIM_SAP_DISCONNECT_REQ");
244 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
248 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
249 if (buffer == NULL) {
250 RLOGE("SapImpl::disconnectReq: Error allocating memory for buffer");
251 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
255 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
257 RLOGD("SapImpl::disconnectReq calling pb_encode");
258 if (!pb_encode(&stream, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
259 RLOGE("SapImpl::disconnectReq: Error encoding RIL_SIM_SAP_DISCONNECT_REQ");
260 sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 2, buffer, msg);
263 /***** Encode RIL_SIM_SAP_DISCONNECT_REQ done *****/
265 /* encoded req is payload */
266 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
269 Return<void> SapImpl::apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command) {
270 RLOGD("SapImpl::apduReq");
271 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_APDU, token);
273 RLOGE("SapImpl::apduReq: Error allocating memory for msg");
274 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 0);
278 /***** Encode RIL_SIM_SAP_APDU_REQ *****/
279 RIL_SIM_SAP_APDU_REQ req;
280 memset(&req, 0, sizeof(RIL_SIM_SAP_APDU_REQ));
281 req.type = (RIL_SIM_SAP_APDU_REQ_Type)type;
283 if (command.size() > 0) {
284 req.command = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + command.size());
285 if (req.command == NULL) {
286 RLOGE("SapImpl::apduReq: Error allocating memory for req.command");
287 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 1, msg);
290 req.command->size = command.size();
291 memcpy(req.command->bytes, command.data(), command.size());
294 size_t encodedSize = 0;
295 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
296 RLOGE("SapImpl::apduReq: Error getting encoded size for RIL_SIM_SAP_APDU_REQ");
297 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
301 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
302 if (buffer == NULL) {
303 RLOGE("SapImpl::apduReq: Error allocating memory for buffer");
304 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
308 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
310 RLOGD("SapImpl::apduReq calling pb_encode");
311 if (!pb_encode(&stream, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
312 RLOGE("SapImpl::apduReq: Error encoding RIL_SIM_SAP_APDU_REQ");
313 sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 3, req.command, buffer, msg);
316 /***** Encode RIL_SIM_SAP_APDU_REQ done *****/
318 /* encoded req is payload */
319 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
322 Return<void> SapImpl::transferAtrReq(int32_t token) {
323 RLOGD("SapImpl::transferAtrReq");
324 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token);
326 RLOGE("SapImpl::transferAtrReq: Error allocating memory for msg");
327 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 0);
331 /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ *****/
332 RIL_SIM_SAP_TRANSFER_ATR_REQ req;
333 memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_ATR_REQ));
335 size_t encodedSize = 0;
336 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
337 RLOGE("SapImpl::transferAtrReq: Error getting encoded size for "
338 "RIL_SIM_SAP_TRANSFER_ATR_REQ");
339 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
343 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
344 if (buffer == NULL) {
345 RLOGE("SapImpl::transferAtrReq: Error allocating memory for buffer");
346 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
350 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
352 RLOGD("SapImpl::transferAtrReq calling pb_encode");
353 if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
354 RLOGE("SapImpl::transferAtrReq: Error encoding RIL_SIM_SAP_TRANSFER_ATR_REQ");
355 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 2, buffer, msg);
358 /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ done *****/
360 /* encoded req is payload */
361 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
364 Return<void> SapImpl::powerReq(int32_t token, bool state) {
365 RLOGD("SapImpl::powerReq");
366 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_POWER, token);
368 RLOGE("SapImpl::powerReq: Error allocating memory for msg");
369 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 0);
373 /***** Encode RIL_SIM_SAP_POWER_REQ *****/
374 RIL_SIM_SAP_POWER_REQ req;
375 memset(&req, 0, sizeof(RIL_SIM_SAP_POWER_REQ));
378 size_t encodedSize = 0;
379 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
380 RLOGE("SapImpl::powerReq: Error getting encoded size for RIL_SIM_SAP_POWER_REQ");
381 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
385 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
386 if (buffer == NULL) {
387 RLOGE("SapImpl::powerReq: Error allocating memory for buffer");
388 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
392 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
394 RLOGD("SapImpl::powerReq calling pb_encode");
395 if (!pb_encode(&stream, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
396 RLOGE("SapImpl::powerReq: Error encoding RIL_SIM_SAP_POWER_REQ");
397 sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 2, buffer, msg);
400 /***** Encode RIL_SIM_SAP_POWER_REQ done *****/
402 /* encoded req is payload */
403 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
406 Return<void> SapImpl::resetSimReq(int32_t token) {
407 RLOGD("SapImpl::resetSimReq");
408 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_RESET_SIM, token);
410 RLOGE("SapImpl::resetSimReq: Error allocating memory for msg");
411 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 0);
415 /***** Encode RIL_SIM_SAP_RESET_SIM_REQ *****/
416 RIL_SIM_SAP_RESET_SIM_REQ req;
417 memset(&req, 0, sizeof(RIL_SIM_SAP_RESET_SIM_REQ));
419 size_t encodedSize = 0;
420 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
421 RLOGE("SapImpl::resetSimReq: Error getting encoded size for RIL_SIM_SAP_RESET_SIM_REQ");
422 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
426 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
427 if (buffer == NULL) {
428 RLOGE("SapImpl::resetSimReq: Error allocating memory for buffer");
429 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
433 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
435 RLOGD("SapImpl::resetSimReq calling pb_encode");
436 if (!pb_encode(&stream, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
437 RLOGE("SapImpl::resetSimReq: Error encoding RIL_SIM_SAP_RESET_SIM_REQ");
438 sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 2, buffer, msg);
441 /***** Encode RIL_SIM_SAP_RESET_SIM_REQ done *****/
443 /* encoded req is payload */
444 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
447 Return<void> SapImpl::transferCardReaderStatusReq(int32_t token) {
448 RLOGD("SapImpl::transferCardReaderStatusReq");
449 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token);
451 RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for msg");
452 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 0);
456 /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ *****/
457 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ req;
458 memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ));
460 size_t encodedSize = 0;
461 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields,
463 RLOGE("SapImpl::transferCardReaderStatusReq: Error getting encoded size for "
464 "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
465 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
469 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
470 if (buffer == NULL) {
471 RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for buffer");
472 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
476 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
478 RLOGD("SapImpl::transferCardReaderStatusReq calling pb_encode");
479 if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields, &req)) {
480 RLOGE("SapImpl::transferCardReaderStatusReq: Error encoding "
481 "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
482 sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 2, buffer, msg);
485 /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ done *****/
487 /* encoded req is payload */
488 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
491 Return<void> SapImpl::setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol) {
492 RLOGD("SapImpl::setTransferProtocolReq");
493 MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token);
495 RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for msg");
496 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 0);
500 /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ *****/
501 RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ req;
502 memset(&req, 0, sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ));
503 req.protocol = (RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_Protocol)transferProtocol;
505 size_t encodedSize = 0;
506 if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
507 RLOGE("SapImpl::setTransferProtocolReq: Error getting encoded size for "
508 "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
509 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
513 uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
514 if (buffer == NULL) {
515 RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for buffer");
516 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
520 pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
522 RLOGD("SapImpl::setTransferProtocolReq calling pb_encode");
523 if (!pb_encode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
524 RLOGE("SapImpl::setTransferProtocolReq: Error encoding "
525 "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
526 sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 2, buffer, msg);
529 /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ done *****/
531 /* encoded req is payload */
532 return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
535 void *sapDecodeMessage(MsgId msgId, MsgType msgType, uint8_t *payloadPtr, size_t payloadLen) {
536 void *responsePtr = NULL;
539 /* Create the stream */
540 stream = pb_istream_from_buffer((uint8_t *)payloadPtr, payloadLen);
542 /* Decode based on the message id */
545 case MsgId_RIL_SIM_SAP_CONNECT:
546 responsePtr = malloc(sizeof(RIL_SIM_SAP_CONNECT_RSP));
548 if (!pb_decode(&stream, RIL_SIM_SAP_CONNECT_RSP_fields, responsePtr)) {
549 RLOGE("Error decoding RIL_SIM_SAP_CONNECT_RSP");
555 case MsgId_RIL_SIM_SAP_DISCONNECT:
556 if (msgType == MsgType_RESPONSE) {
557 responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_RSP));
559 if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_RSP_fields, responsePtr)) {
560 RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_RSP");
565 responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_IND));
567 if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_IND_fields, responsePtr)) {
568 RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_IND");
575 case MsgId_RIL_SIM_SAP_APDU:
576 responsePtr = malloc(sizeof(RIL_SIM_SAP_APDU_RSP));
578 if (!pb_decode(&stream, RIL_SIM_SAP_APDU_RSP_fields, responsePtr)) {
579 RLOGE("Error decoding RIL_SIM_SAP_APDU_RSP");
585 case MsgId_RIL_SIM_SAP_TRANSFER_ATR:
586 responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_ATR_RSP));
588 if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_ATR_RSP_fields, responsePtr)) {
589 RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_ATR_RSP");
595 case MsgId_RIL_SIM_SAP_POWER:
596 responsePtr = malloc(sizeof(RIL_SIM_SAP_POWER_RSP));
598 if (!pb_decode(&stream, RIL_SIM_SAP_POWER_RSP_fields, responsePtr)) {
599 RLOGE("Error decoding RIL_SIM_SAP_POWER_RSP");
605 case MsgId_RIL_SIM_SAP_RESET_SIM:
606 responsePtr = malloc(sizeof(RIL_SIM_SAP_RESET_SIM_RSP));
608 if (!pb_decode(&stream, RIL_SIM_SAP_RESET_SIM_RSP_fields, responsePtr)) {
609 RLOGE("Error decoding RIL_SIM_SAP_RESET_SIM_RSP");
615 case MsgId_RIL_SIM_SAP_STATUS:
616 responsePtr = malloc(sizeof(RIL_SIM_SAP_STATUS_IND));
618 if (!pb_decode(&stream, RIL_SIM_SAP_STATUS_IND_fields, responsePtr)) {
619 RLOGE("Error decoding RIL_SIM_SAP_STATUS_IND");
625 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
626 responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP));
628 if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_fields,
630 RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP");
636 case MsgId_RIL_SIM_SAP_ERROR_RESP:
637 responsePtr = malloc(sizeof(RIL_SIM_SAP_ERROR_RSP));
639 if (!pb_decode(&stream, RIL_SIM_SAP_ERROR_RSP_fields, responsePtr)) {
640 RLOGE("Error decoding RIL_SIM_SAP_ERROR_RSP");
646 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
647 responsePtr = malloc(sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP));
649 if (!pb_decode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_fields,
651 RLOGE("Error decoding RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP");
661 } /* sapDecodeMessage */
663 sp<SapImpl> getSapImpl(RilSapSocket *sapSocket) {
664 switch (sapSocket->getSocketId()) {
666 RLOGD("getSapImpl: returning sapService[0]");
667 return sapService[0];
670 return sapService[1];
673 return sapService[2];
676 return sapService[3];
685 SapResultCode convertApduResponseProtoToHal(RIL_SIM_SAP_APDU_RSP_Response responseProto) {
686 switch(responseProto) {
687 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SUCCESS:
688 return SapResultCode::SUCCESS;
689 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_GENERIC_FAILURE:
690 return SapResultCode::GENERIC_FAILURE;
691 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_NOT_READY:
692 return SapResultCode::CARD_NOT_ACCESSSIBLE;
693 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
694 return SapResultCode::CARD_ALREADY_POWERED_OFF;
695 case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ABSENT:
696 return SapResultCode::CARD_REMOVED;
698 return SapResultCode::GENERIC_FAILURE;
702 SapResultCode convertTransferAtrResponseProtoToHal(
703 RIL_SIM_SAP_TRANSFER_ATR_RSP_Response responseProto) {
704 switch(responseProto) {
705 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SUCCESS:
706 return SapResultCode::SUCCESS;
707 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_GENERIC_FAILURE:
708 return SapResultCode::GENERIC_FAILURE;
709 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
710 return SapResultCode::CARD_ALREADY_POWERED_OFF;
711 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ABSENT:
712 return SapResultCode::CARD_REMOVED;
713 case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
714 return SapResultCode::DATA_NOT_AVAILABLE;
716 return SapResultCode::GENERIC_FAILURE;
720 SapResultCode convertPowerResponseProtoToHal(RIL_SIM_SAP_POWER_RSP_Response responseProto) {
721 switch(responseProto) {
722 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SUCCESS:
723 return SapResultCode::SUCCESS;
724 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_GENERIC_FAILURE:
725 return SapResultCode::GENERIC_FAILURE;
726 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ABSENT:
727 return SapResultCode::CARD_REMOVED;
728 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
729 return SapResultCode::CARD_ALREADY_POWERED_OFF;
730 case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_ON:
731 return SapResultCode::CARD_ALREADY_POWERED_ON;
733 return SapResultCode::GENERIC_FAILURE;
737 SapResultCode convertResetSimResponseProtoToHal(RIL_SIM_SAP_RESET_SIM_RSP_Response responseProto) {
738 switch(responseProto) {
739 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SUCCESS:
740 return SapResultCode::SUCCESS;
741 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_GENERIC_FAILURE:
742 return SapResultCode::GENERIC_FAILURE;
743 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ABSENT:
744 return SapResultCode::CARD_REMOVED;
745 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_NOT_READY:
746 return SapResultCode::CARD_NOT_ACCESSSIBLE;
747 case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
748 return SapResultCode::CARD_ALREADY_POWERED_OFF;
750 return SapResultCode::GENERIC_FAILURE;
753 SapResultCode convertTransferCardReaderStatusResponseProtoToHal(
754 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response responseProto) {
755 switch(responseProto) {
756 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SUCCESS:
757 return SapResultCode::SUCCESS;
758 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_GENERIC_FAILURE:
759 return SapResultCode::GENERIC_FAILURE;
760 case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
761 return SapResultCode::DATA_NOT_AVAILABLE;
763 return SapResultCode::GENERIC_FAILURE;
766 void processResponse(MsgHeader *rsp, RilSapSocket *sapSocket, MsgType msgType) {
767 MsgId msgId = rsp->id;
768 uint8_t *data = rsp->payload->bytes;
769 size_t dataLen = rsp->payload->size;
771 void *messagePtr = sapDecodeMessage(msgId, msgType, data, dataLen);
773 sp<SapImpl> sapImpl = getSapImpl(sapSocket);
774 if (sapImpl->sapCallback == NULL) {
775 RLOGE("processResponse: sapCallback == NULL; msgId = %d; msgType = %d",
780 if (messagePtr == NULL) {
781 RLOGE("processResponse: *messagePtr == NULL; msgId = %d; msgType = %d",
783 sapImpl->sendFailedResponse(msgId, rsp->token, 0);
787 RLOGD("processResponse: sapCallback != NULL; msgId = %d; msgType = %d",
790 Return<void> retStatus;
792 case MsgId_RIL_SIM_SAP_CONNECT: {
793 RIL_SIM_SAP_CONNECT_RSP *connectRsp = (RIL_SIM_SAP_CONNECT_RSP *)messagePtr;
794 RLOGD("processResponse: calling sapCallback->connectResponse %d %d %d",
796 connectRsp->response,
797 connectRsp->max_message_size);
798 retStatus = sapImpl->sapCallback->connectResponse(rsp->token,
799 (SapConnectRsp)connectRsp->response,
800 connectRsp->max_message_size);
804 case MsgId_RIL_SIM_SAP_DISCONNECT:
805 if (msgType == MsgType_RESPONSE) {
806 RLOGD("processResponse: calling sapCallback->disconnectResponse %d", rsp->token);
807 retStatus = sapImpl->sapCallback->disconnectResponse(rsp->token);
809 RIL_SIM_SAP_DISCONNECT_IND *disconnectInd =
810 (RIL_SIM_SAP_DISCONNECT_IND *)messagePtr;
811 RLOGD("processResponse: calling sapCallback->disconnectIndication %d %d",
812 rsp->token, disconnectInd->disconnectType);
813 retStatus = sapImpl->sapCallback->disconnectIndication(rsp->token,
814 (SapDisconnectType)disconnectInd->disconnectType);
818 case MsgId_RIL_SIM_SAP_APDU: {
819 RIL_SIM_SAP_APDU_RSP *apduRsp = (RIL_SIM_SAP_APDU_RSP *)messagePtr;
820 SapResultCode apduResponse = convertApduResponseProtoToHal(apduRsp->response);
821 RLOGD("processResponse: calling sapCallback->apduResponse %d %d",
822 rsp->token, apduResponse);
823 hidl_vec<uint8_t> apduRspVec;
824 if (apduRsp->apduResponse != NULL && apduRsp->apduResponse->size > 0) {
825 apduRspVec.setToExternal(apduRsp->apduResponse->bytes, apduRsp->apduResponse->size);
827 retStatus = sapImpl->sapCallback->apduResponse(rsp->token, apduResponse, apduRspVec);
831 case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
832 RIL_SIM_SAP_TRANSFER_ATR_RSP *transferAtrRsp =
833 (RIL_SIM_SAP_TRANSFER_ATR_RSP *)messagePtr;
834 SapResultCode transferAtrResponse =
835 convertTransferAtrResponseProtoToHal(transferAtrRsp->response);
836 RLOGD("processResponse: calling sapCallback->transferAtrResponse %d %d",
837 rsp->token, transferAtrResponse);
838 hidl_vec<uint8_t> transferAtrRspVec;
839 if (transferAtrRsp->atr != NULL && transferAtrRsp->atr->size > 0) {
840 transferAtrRspVec.setToExternal(transferAtrRsp->atr->bytes,
841 transferAtrRsp->atr->size);
843 retStatus = sapImpl->sapCallback->transferAtrResponse(rsp->token, transferAtrResponse,
848 case MsgId_RIL_SIM_SAP_POWER: {
849 SapResultCode powerResponse = convertPowerResponseProtoToHal(
850 ((RIL_SIM_SAP_POWER_RSP *)messagePtr)->response);
851 RLOGD("processResponse: calling sapCallback->powerResponse %d %d",
852 rsp->token, powerResponse);
853 retStatus = sapImpl->sapCallback->powerResponse(rsp->token, powerResponse);
857 case MsgId_RIL_SIM_SAP_RESET_SIM: {
858 SapResultCode resetSimResponse = convertResetSimResponseProtoToHal(
859 ((RIL_SIM_SAP_RESET_SIM_RSP *)messagePtr)->response);
860 RLOGD("processResponse: calling sapCallback->resetSimResponse %d %d",
861 rsp->token, resetSimResponse);
862 retStatus = sapImpl->sapCallback->resetSimResponse(rsp->token, resetSimResponse);
866 case MsgId_RIL_SIM_SAP_STATUS: {
867 RIL_SIM_SAP_STATUS_IND *statusInd = (RIL_SIM_SAP_STATUS_IND *)messagePtr;
868 RLOGD("processResponse: calling sapCallback->statusIndication %d %d",
869 rsp->token, statusInd->statusChange);
870 retStatus = sapImpl->sapCallback->statusIndication(rsp->token,
871 (SapStatus)statusInd->statusChange);
875 case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: {
876 RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *transferStatusRsp =
877 (RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *)messagePtr;
878 SapResultCode transferCardReaderStatusResponse =
879 convertTransferCardReaderStatusResponseProtoToHal(
880 transferStatusRsp->response);
881 RLOGD("processResponse: calling sapCallback->transferCardReaderStatusResponse %d %d %d",
883 transferCardReaderStatusResponse,
884 transferStatusRsp->CardReaderStatus);
885 retStatus = sapImpl->sapCallback->transferCardReaderStatusResponse(rsp->token,
886 transferCardReaderStatusResponse,
887 transferStatusRsp->CardReaderStatus);
891 case MsgId_RIL_SIM_SAP_ERROR_RESP: {
892 RLOGD("processResponse: calling sapCallback->errorResponse %d", rsp->token);
893 retStatus = sapImpl->sapCallback->errorResponse(rsp->token);
897 case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: {
898 SapResultCode setTransferProtocolResponse;
899 if (((RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP *)messagePtr)->response ==
900 RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_Response_RIL_E_SUCCESS) {
901 setTransferProtocolResponse = SapResultCode::SUCCESS;
903 setTransferProtocolResponse = SapResultCode::NOT_SUPPORTED;
905 RLOGD("processResponse: calling sapCallback->transferProtocolResponse %d %d",
906 rsp->token, setTransferProtocolResponse);
907 retStatus = sapImpl->sapCallback->transferProtocolResponse(rsp->token,
908 setTransferProtocolResponse);
915 sapImpl->checkReturnStatus(retStatus);
918 void sap::processResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
919 processResponse(rsp, sapSocket, MsgType_RESPONSE);
922 void sap::processUnsolResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
923 processResponse(rsp, sapSocket, MsgType_UNSOL_RESPONSE);
926 void sap::registerService(const RIL_RadioFunctions *callbacks) {
927 using namespace android::hardware;
929 const char *serviceNames[] = {
930 android::RIL_getServiceName()
942 RIL_SOCKET_ID socketIds[] = {
955 simCount = SIM_COUNT;
958 for (int i = 0; i < simCount; i++) {
959 sapService[i] = new SapImpl;
960 sapService[i]->slotId = i;
961 sapService[i]->rilSocketId = socketIds[i];
962 RLOGD("registerService: starting ISap %s for slotId %d", serviceNames[i], i);
963 android::status_t status = sapService[i]->registerAsService(serviceNames[i]);
964 RLOGD("registerService: started ISap %s status %d", serviceNames[i], status);