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