Converting SAP from protobuf to hidl.

Test: Basic SAP sanity.
Bug: 32020264
Change-Id: I87dd2bd320736d06e10733dd40743c83e548d3c9
diff --git a/libril/Android.mk b/libril/Android.mk
index 7d68c52..625b00f 100644
--- a/libril/Android.mk
+++ b/libril/Android.mk
@@ -8,7 +8,8 @@
     ril_event.cpp\
     RilSocket.cpp \
     RilSapSocket.cpp \
-    ril_service.cpp
+    ril_service.cpp \
+    sap_service.cpp
 
 LOCAL_SHARED_LIBRARIES := \
     liblog \
diff --git a/libril/RilSapSocket.cpp b/libril/RilSapSocket.cpp
index 7dbad93..47af0b5 100644
--- a/libril/RilSapSocket.cpp
+++ b/libril/RilSapSocket.cpp
@@ -26,6 +26,7 @@
 #include <utils/Log.h>
 #include <arpa/inet.h>
 #include <errno.h>
+#include <sap_service.h>
 
 static RilSapSocket::RilSapSocketList *head = NULL;
 
@@ -285,11 +286,13 @@
     pendingResponseQueue.enqueue(currRequest);
 
     if (uimFuncs) {
-        RLOGI("[%d] > SAP REQUEST type: %d. id: %d. error: %d",
-        req->token,
-        req->type,
-        req->id,
-        req->error );
+        RLOGI("RilSapSocket::dispatchRequest [%d] > SAP REQUEST type: %d. id: %d. error: %d, \
+                token 0x%p",
+                req->token,
+                req->type,
+                req->id,
+                req->error,
+                currRequest );
 
 #if defined(ANDROID_MULTI_SIM)
         uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest, id);
@@ -320,9 +323,10 @@
             rsp.payload->size = 0;
         }
 
-        RLOGE("Token:%d, MessageId:%d", hdr->token, hdr->id);
+        RLOGE("RilSapSocket::onRequestComplete: Token:%d, MessageId:%d ril token 0x%p",
+                hdr->token, hdr->id, t);
 
-        sendResponse(&rsp);
+        sap::processResponse(&rsp, this);
         free(rsp.payload);
     }
 
@@ -399,7 +403,7 @@
         rsp.type = MsgType_UNSOL_RESPONSE;
         rsp.id = (MsgId)unsolResponse;
         rsp.error = Error_RIL_E_SUCCESS;
-        sendResponse(&rsp);
+        sap::processUnsolResponse(&rsp, this);
         free(payload);
     }
 }
diff --git a/libril/RilSapSocket.h b/libril/RilSapSocket.h
index 75c3965..68ee4b4 100644
--- a/libril/RilSapSocket.h
+++ b/libril/RilSapSocket.h
@@ -104,6 +104,22 @@
         void onCommandsSocketClosed(void);
 
         /**
+         * Dispatches the request to the lower layers.
+         * It calls the on request function.
+         *
+         * @param request The request message.
+         */
+        void dispatchRequest(MsgHeader *request);
+
+        /**
+         * Class method to get the socket from the socket list.
+         *
+         * @param socketId Socket id.
+         * @return the sap socket.
+         */
+        static RilSapSocket* getSocketById(RIL_SOCKET_ID socketId);
+
+        /**
          * Datatype to handle the socket list.
          */
         typedef struct RilSapSocketList {
@@ -146,14 +162,6 @@
         void *data, size_t datalen);
 
         /**
-         * Class method to get the socket from the socket list.
-         *
-         * @param Socket id.
-         * @return the sap socket.
-         */
-        static RilSapSocket* getSocketById(RIL_SOCKET_ID socketId);
-
-        /**
          * Method to send response to SAP. It does an atomic write operation on the
          * socket.
          *
@@ -208,14 +216,6 @@
         RIL_RadioFunctions *inputUimFuncs);
 
         /**
-         * Dispatches the request to the lower layers.
-         * It calls the on request function.
-         *
-         * @param The request message.
-         */
-        void dispatchRequest(MsgHeader *request);
-
-        /**
          * Class method that selects the socket on which the onRequestComplete
          * is called.
          *
diff --git a/libril/RilSocket.cpp b/libril/RilSocket.cpp
index a002d94..fc59ca1 100644
--- a/libril/RilSocket.cpp
+++ b/libril/RilSocket.cpp
@@ -169,6 +169,10 @@
     return &callbackEvent;
 }
 
+RIL_SOCKET_ID RilSocket::getSocketId(void) {
+    return id;
+}
+
 extern "C"
 void *ril_socket_process_requests_loop(void *arg) {
     RilSocket *socket = (RilSocket *)arg;
diff --git a/libril/RilSocket.h b/libril/RilSocket.h
index 619401a..3f5de61 100644
--- a/libril/RilSocket.h
+++ b/libril/RilSocket.h
@@ -228,6 +228,13 @@
          */
         ril_event* getCallbackEvent(void);
 
+        /**
+         * Get socket id.
+         *
+         * @return RIL_SOCKET_ID socket id.
+         */
+        RIL_SOCKET_ID getSocketId(void);
+
         virtual ~RilSocket(){}
 
     protected:
diff --git a/libril/ril.cpp b/libril/ril.cpp
index 6a7e694..d5c0222 100644
--- a/libril/ril.cpp
+++ b/libril/ril.cpp
@@ -47,6 +47,7 @@
 #include <cutils/properties.h>
 #include <RilSapSocket.h>
 #include <ril_service.h>
+#include <sap_service.h>
 
 extern "C" void
 RIL_onRequestComplete(RIL_Token t, RIL_Errno e, void *response, size_t responselen);
@@ -4873,6 +4874,9 @@
                 break;
             default:;
         }
+
+        RLOGI("RIL_register_socket: calling registerService");
+        sap::registerService(UimFuncs);
     }
 }
 
diff --git a/libril/sap_service.cpp b/libril/sap_service.cpp
new file mode 100644
index 0000000..36ccc0f
--- /dev/null
+++ b/libril/sap_service.cpp
@@ -0,0 +1,930 @@
+/*
+ * Copyright (c) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <android/hardware/radio/1.0/ISap.h>
+
+#include <hwbinder/IPCThreadState.h>
+#include <hwbinder/ProcessState.h>
+#include <sap_service.h>
+#include "pb_decode.h"
+#include "pb_encode.h"
+
+using namespace android::hardware::radio::V1_0;
+using ::android::hardware::Return;
+using ::android::hardware::Status;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::hidl_array;
+using ::android::hardware::Void;
+using android::CommandInfo;
+using android::RequestInfo;
+using android::requestToString;
+using android::sp;
+
+struct SapImpl;
+
+#if (SIM_COUNT >= 2)
+sp<SapImpl> sapService[SIM_COUNT];
+#else
+sp<SapImpl> sapService[1];
+#endif
+
+struct SapImpl : public ISap {
+    int32_t slotId;
+    sp<ISapCallback> sapCallback;
+    RIL_SOCKET_ID rilSocketId;
+
+    Return<void> setCallback(const ::android::sp<ISapCallback>& sapCallbackParam);
+
+    Return<void> connectReq(int32_t token, int32_t maxMsgSize);
+
+    Return<void> disconnectReq(int32_t token);
+
+    Return<void> apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command);
+
+    Return<void> transferAtrReq(int32_t token);
+
+    Return<void> powerReq(int32_t token, bool state);
+
+    Return<void> resetSimReq(int32_t token);
+
+    Return<void> transferCardReaderStatusReq(int32_t token);
+
+    Return<void> setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol);
+
+    MsgHeader* createMsgHeader(MsgId msgId, int32_t token);
+
+    Return<void> addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen, uint8_t *reqPtr);
+
+    void sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...);
+};
+
+Return<void> SapImpl::setCallback(const ::android::sp<ISapCallback>& sapCallbackParam) {
+    RLOGD("SapImpl::setCallback for slotId %d", slotId);
+    sapCallback = sapCallbackParam;
+    return Status::ok();
+}
+
+MsgHeader* SapImpl::createMsgHeader(MsgId msgId, int32_t token) {
+    // Memory for msg will be freed by RilSapSocket::onRequestComplete()
+    MsgHeader *msg = (MsgHeader *)calloc(1, sizeof(MsgHeader));
+    if (msg == NULL) {
+        return NULL;
+    }
+    msg->token = token;
+    msg->type = MsgType_REQUEST;
+    msg->id = msgId;
+    msg->error = Error_RIL_E_SUCCESS;
+    return msg;
+}
+
+Return<void> SapImpl::addPayloadAndDispatchRequest(MsgHeader *msg, uint16_t reqLen,
+        uint8_t *reqPtr) {
+    msg->payload = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + reqLen);
+    if (msg->payload == NULL) {
+        sendFailedResponse(msg->id, msg->token, 2, reqPtr, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_NULL_POINTER);
+    }
+    msg->payload->size = reqLen;
+    memcpy(msg->payload->bytes, reqPtr, reqLen);
+
+    RilSapSocket *sapSocket = RilSapSocket::getSocketById(rilSocketId);
+    if (sapSocket) {
+        RLOGD("SapImpl::addPayloadAndDispatchRequest: calling dispatchRequest");
+        sapSocket->dispatchRequest(msg);
+    } else {
+        RLOGE("SapImpl::addPayloadAndDispatchRequest: sapSocket is null");
+        sendFailedResponse(msg->id, msg->token, 3, msg->payload, reqPtr, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_NULL_POINTER);
+    }
+    free(msg->payload);
+    free(reqPtr);
+    return Status::ok();
+}
+
+void SapImpl::sendFailedResponse(MsgId msgId, int32_t token, int numPointers, ...) {
+    va_list ap;
+    va_start(ap, numPointers);
+    for (int i = 0; i < numPointers; i++) {
+        void *ptr = va_arg(ap, void *);
+        if (ptr) free(ptr);
+    }
+    va_end(ap);
+    switch(msgId) {
+        case MsgId_RIL_SIM_SAP_CONNECT:
+            sapCallback->connectResponse(token, SapConnectRsp::CONNECT_FAILURE, 0);
+            return;
+
+        case MsgId_RIL_SIM_SAP_DISCONNECT:
+            sapCallback->disconnectResponse(token);
+            return;
+
+        case MsgId_RIL_SIM_SAP_APDU: {
+            hidl_vec<uint8_t> apduRsp;
+            sapCallback->apduResponse(token, SapResultCode::GENERIC_FAILURE, apduRsp);
+            return;
+        }
+
+        case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
+            hidl_vec<uint8_t> atr;
+            sapCallback->transferAtrResponse(token, SapResultCode::GENERIC_FAILURE, atr);
+            return;
+        }
+
+        case MsgId_RIL_SIM_SAP_POWER:
+            sapCallback->powerResponse(token, SapResultCode::GENERIC_FAILURE);
+            return;
+
+        case MsgId_RIL_SIM_SAP_RESET_SIM:
+            sapCallback->resetSimResponse(token, SapResultCode::GENERIC_FAILURE);
+            return;
+
+        case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
+            sapCallback->transferCardReaderStatusResponse(token, SapResultCode::GENERIC_FAILURE, 0);
+            return;
+
+        case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
+            sapCallback->transferProtocolResponse(token, SapResultCode::NOT_SUPPORTED);
+            return;
+    }
+}
+
+Return<void> SapImpl::connectReq(int32_t token, int32_t maxMsgSize) {
+    RLOGD("SapImpl::connectReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_CONNECT, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::connectReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_CONNECT_REQ *****/
+    RIL_SIM_SAP_CONNECT_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_CONNECT_REQ));
+    req.max_message_size = maxMsgSize;
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
+        RLOGE("SapImpl::connectReq: Error getting encoded size for RIL_SIM_SAP_CONNECT_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::connectReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::connectReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_CONNECT_REQ_fields, &req)) {
+        RLOGE("SapImpl::connectReq: Error encoding RIL_SIM_SAP_CONNECT_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_CONNECT, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_CONNECT_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::disconnectReq(int32_t token) {
+    RLOGD("SapImpl::disconnectReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_DISCONNECT, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::disconnectReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_DISCONNECT_REQ *****/
+    RIL_SIM_SAP_DISCONNECT_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_DISCONNECT_REQ));
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
+        RLOGE("SapImpl::disconnectReq: Error getting encoded size for RIL_SIM_SAP_DISCONNECT_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::disconnectReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::disconnectReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_DISCONNECT_REQ_fields, &req)) {
+        RLOGE("SapImpl::disconnectReq: Error encoding RIL_SIM_SAP_DISCONNECT_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_DISCONNECT, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_DISCONNECT_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::apduReq(int32_t token, SapApduType type, const hidl_vec<uint8_t>& command) {
+    RLOGD("SapImpl::apduReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_APDU, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::apduReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_APDU_REQ *****/
+    RIL_SIM_SAP_APDU_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_APDU_REQ));
+    req.type = (RIL_SIM_SAP_APDU_REQ_Type)type;
+
+    if (command.size() > 0) {
+        req.command = (pb_bytes_array_t *)malloc(sizeof(pb_bytes_array_t) - 1 + command.size());
+        if (req.command == NULL) {
+            RLOGE("SapImpl::apduReq: Error allocating memory for req.command");
+            sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 1, msg);
+            return Status::fromStatusT(android::NO_MEMORY);
+        }
+        req.command->size = command.size();
+        memcpy(req.command->bytes, command.data(), command.size());
+    }
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
+        RLOGE("SapImpl::apduReq: Error getting encoded size for RIL_SIM_SAP_APDU_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::apduReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 2, req.command, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::apduReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_APDU_REQ_fields, &req)) {
+        RLOGE("SapImpl::apduReq: Error encoding RIL_SIM_SAP_APDU_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_APDU, token, 3, req.command, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_APDU_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::transferAtrReq(int32_t token) {
+    RLOGD("SapImpl::transferAtrReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::transferAtrReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ *****/
+    RIL_SIM_SAP_TRANSFER_ATR_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_ATR_REQ));
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
+        RLOGE("SapImpl::transferAtrReq: Error getting encoded size for \
+                RIL_SIM_SAP_TRANSFER_ATR_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::transferAtrReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::transferAtrReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_ATR_REQ_fields, &req)) {
+        RLOGE("SapImpl::transferAtrReq: Error encoding RIL_SIM_SAP_TRANSFER_ATR_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_ATR, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_TRANSFER_ATR_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::powerReq(int32_t token, bool state) {
+    RLOGD("SapImpl::powerReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_POWER, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::powerReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_POWER_REQ *****/
+    RIL_SIM_SAP_POWER_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_POWER_REQ));
+    req.state = state;
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
+        RLOGE("SapImpl::powerReq: Error getting encoded size for RIL_SIM_SAP_POWER_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::powerReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::powerReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_POWER_REQ_fields, &req)) {
+        RLOGE("SapImpl::powerReq: Error encoding RIL_SIM_SAP_POWER_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_POWER, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_POWER_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::resetSimReq(int32_t token) {
+    RLOGD("SapImpl::resetSimReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_RESET_SIM, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::resetSimReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_RESET_SIM_REQ *****/
+    RIL_SIM_SAP_RESET_SIM_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_RESET_SIM_REQ));
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
+        RLOGE("SapImpl::resetSimReq: Error getting encoded size for RIL_SIM_SAP_RESET_SIM_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::resetSimReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::resetSimReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_RESET_SIM_REQ_fields, &req)) {
+        RLOGE("SapImpl::resetSimReq: Error encoding RIL_SIM_SAP_RESET_SIM_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_RESET_SIM, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_RESET_SIM_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::transferCardReaderStatusReq(int32_t token) {
+    RLOGD("SapImpl::transferCardReaderStatusReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ *****/
+    RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ));
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields,
+            &req)) {
+        RLOGE("SapImpl::transferCardReaderStatusReq: Error getting encoded size for \
+                RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::transferCardReaderStatusReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::transferCardReaderStatusReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ_fields, &req)) {
+        RLOGE("SapImpl::transferCardReaderStatusReq: Error encoding "
+                "RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+Return<void> SapImpl::setTransferProtocolReq(int32_t token, SapTransferProtocol transferProtocol) {
+    RLOGD("SapImpl::setTransferProtocolReq");
+    MsgHeader *msg = createMsgHeader(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token);
+    if (msg == NULL) {
+        RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for msg");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 0);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ *****/
+    RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ req;
+    memset(&req, 0, sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ));
+    req.protocol = (RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_Protocol)transferProtocol;
+
+    size_t encodedSize = 0;
+    if (!pb_get_encoded_size(&encodedSize, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
+        RLOGE("SapImpl::setTransferProtocolReq: Error getting encoded size for \
+                RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    uint8_t *buffer = (uint8_t *)calloc(1, encodedSize);
+    if (buffer == NULL) {
+        RLOGE("SapImpl::setTransferProtocolReq: Error allocating memory for buffer");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 1, msg);
+        return Status::fromStatusT(android::NO_MEMORY);
+    }
+
+    pb_ostream_t stream = pb_ostream_from_buffer(buffer, encodedSize);
+
+    RLOGD("SapImpl::setTransferProtocolReq calling pb_encode");
+    if (!pb_encode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ_fields, &req)) {
+        RLOGE("SapImpl::setTransferProtocolReq: Error encoding "
+                "RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ");
+        sendFailedResponse(MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL, token, 2, buffer, msg);
+        return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_ARGUMENT);
+    }
+    /***** Encode RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_REQ done *****/
+
+    /* encoded req is payload */
+    return addPayloadAndDispatchRequest(msg, stream.bytes_written, buffer);
+}
+
+void *sapDecodeMessage(MsgId msgId, MsgType msgType, uint8_t *payloadPtr, size_t payloadLen) {
+    void *responsePtr = NULL;
+    bool decodeStatus = false;
+    pb_istream_t stream;
+
+    /* Create the stream */
+    stream = pb_istream_from_buffer((uint8_t *)payloadPtr, payloadLen);
+
+    /* Decode based on the message id */
+    switch (msgId)
+    {
+        case MsgId_RIL_SIM_SAP_CONNECT:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_CONNECT_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_CONNECT_RSP_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_CONNECT_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_DISCONNECT:
+            if (msgType == MsgType_RESPONSE) {
+                responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_RSP));
+                if (responsePtr) {
+                    if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_RSP_fields, responsePtr)) {
+                        RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_RSP");
+                        return NULL;
+                    }
+                }
+            } else {
+                responsePtr = malloc(sizeof(RIL_SIM_SAP_DISCONNECT_IND));
+                if (responsePtr) {
+                    if (!pb_decode(&stream, RIL_SIM_SAP_DISCONNECT_IND_fields, responsePtr)) {
+                        RLOGE("Error decoding RIL_SIM_SAP_DISCONNECT_IND");
+                        return NULL;
+                    }
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_APDU:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_APDU_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_APDU_RSP_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_APDU_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_TRANSFER_ATR:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_ATR_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_ATR_RSP_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_ATR_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_POWER:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_POWER_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_POWER_RSP_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_POWER_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_RESET_SIM:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_RESET_SIM_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_RESET_SIM_RSP_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_RESET_SIM_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_STATUS:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_STATUS_IND));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_STATUS_IND_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_STATUS_IND");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_fields,
+                        responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_ERROR_RESP:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_ERROR_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_ERROR_RSP_fields, responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_ERROR_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL:
+            responsePtr = malloc(sizeof(RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP));
+            if (responsePtr) {
+                if (!pb_decode(&stream, RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_fields,
+                        responsePtr)) {
+                    RLOGE("Error decoding RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP");
+                    return NULL;
+                }
+            }
+            break;
+
+        default:
+            break;
+    }
+    return responsePtr;
+} /* sapDecodeMessage */
+
+sp<SapImpl> getSapImpl(RilSapSocket *sapSocket) {
+    switch (sapSocket->getSocketId()) {
+        case RIL_SOCKET_1:
+            RLOGD("getSapImpl: returning sapService[0]");
+            return sapService[0];
+        #if (SIM_COUNT >= 2)
+        case RIL_SOCKET_2:
+            return sapService[1];
+        #if (SIM_COUNT >= 3)
+        case RIL_SOCKET_3:
+            return sapService[2];
+        #if (SIM_COUNT >= 4)
+        case RIL_SOCKET_4:
+            return sapService[3];
+        #endif
+        #endif
+        #endif
+        default:
+            return NULL;
+    }
+}
+
+SapResultCode convertApduResponseProtoToHal(RIL_SIM_SAP_APDU_RSP_Response responseProto) {
+    switch(responseProto) {
+        case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SUCCESS:
+            return SapResultCode::SUCCESS;
+        case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_GENERIC_FAILURE:
+            return SapResultCode::GENERIC_FAILURE;
+        case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_NOT_READY:
+            return SapResultCode::CARD_NOT_ACCESSSIBLE;
+        case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
+            return SapResultCode::CARD_ALREADY_POWERED_OFF;
+        case RIL_SIM_SAP_APDU_RSP_Response_RIL_E_SIM_ABSENT:
+            return SapResultCode::CARD_REMOVED;
+    }
+    return SapResultCode::GENERIC_FAILURE;
+}
+
+SapResultCode convertTransferAtrResponseProtoToHal(
+        RIL_SIM_SAP_TRANSFER_ATR_RSP_Response responseProto) {
+    switch(responseProto) {
+        case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SUCCESS:
+            return SapResultCode::SUCCESS;
+        case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_GENERIC_FAILURE:
+            return SapResultCode::GENERIC_FAILURE;
+        case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
+            return SapResultCode::CARD_ALREADY_POWERED_OFF;
+        case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_ABSENT:
+            return SapResultCode::CARD_REMOVED;
+        case RIL_SIM_SAP_TRANSFER_ATR_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
+            return SapResultCode::DATA_NOT_AVAILABLE;
+    }
+    return SapResultCode::GENERIC_FAILURE;
+}
+
+SapResultCode convertPowerResponseProtoToHal(RIL_SIM_SAP_POWER_RSP_Response responseProto) {
+    switch(responseProto) {
+        case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SUCCESS:
+            return SapResultCode::SUCCESS;
+        case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_GENERIC_FAILURE:
+            return SapResultCode::GENERIC_FAILURE;
+        case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ABSENT:
+            return SapResultCode::CARD_REMOVED;
+        case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
+            return SapResultCode::CARD_ALREADY_POWERED_OFF;
+        case RIL_SIM_SAP_POWER_RSP_Response_RIL_E_SIM_ALREADY_POWERED_ON:
+            return SapResultCode::CARD_ALREADY_POWERED_ON;
+    }
+    return SapResultCode::GENERIC_FAILURE;
+}
+
+SapResultCode convertResetSimResponseProtoToHal(RIL_SIM_SAP_RESET_SIM_RSP_Response responseProto) {
+    switch(responseProto) {
+        case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SUCCESS:
+            return SapResultCode::SUCCESS;
+        case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_GENERIC_FAILURE:
+            return SapResultCode::GENERIC_FAILURE;
+        case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ABSENT:
+            return SapResultCode::CARD_REMOVED;
+        case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_NOT_READY:
+            return SapResultCode::CARD_NOT_ACCESSSIBLE;
+        case RIL_SIM_SAP_RESET_SIM_RSP_Response_RIL_E_SIM_ALREADY_POWERED_OFF:
+            return SapResultCode::CARD_ALREADY_POWERED_OFF;
+    }
+    return SapResultCode::GENERIC_FAILURE;
+}
+
+SapResultCode convertTransferCardReaderStatusResponseProtoToHal(
+        RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response responseProto) {
+    switch(responseProto) {
+        case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SUCCESS:
+            return SapResultCode::SUCCESS;
+        case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_GENERIC_FAILURE:
+            return SapResultCode::GENERIC_FAILURE;
+        case RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP_Response_RIL_E_SIM_DATA_NOT_AVAILABLE:
+            return SapResultCode::DATA_NOT_AVAILABLE;
+    }
+    return SapResultCode::GENERIC_FAILURE;
+}
+
+void processResponse(MsgHeader *rsp, RilSapSocket *sapSocket, MsgType msgType) {
+    MsgId msgId = rsp->id;
+    uint8_t *data = rsp->payload->bytes;
+    size_t dataLen = rsp->payload->size;
+
+    void *messagePtr = sapDecodeMessage(msgId, msgType, data, dataLen);
+
+    sp<SapImpl> sapImpl = getSapImpl(sapSocket);
+    if (sapImpl->sapCallback == NULL) {
+        RLOGE("processResponse: sapCallback == NULL; msgId = %d; msgType = %d",
+                msgId, msgType);
+        return;
+    }
+
+    RLOGD("processResponse: sapCallback != NULL; msgId = %d; msgType = %d",
+            msgId, msgType);
+
+    switch (msgId) {
+        case MsgId_RIL_SIM_SAP_CONNECT: {
+            RIL_SIM_SAP_CONNECT_RSP *connectRsp = (RIL_SIM_SAP_CONNECT_RSP *)messagePtr;
+            RLOGD("processResponse: calling sapCallback->connectResponse %d %d %d",
+                    rsp->token,
+                    connectRsp->response,
+                    connectRsp->max_message_size);
+            sapImpl->sapCallback->connectResponse(rsp->token,
+                    (SapConnectRsp)connectRsp->response,
+                    connectRsp->max_message_size);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_DISCONNECT:
+            if (msgType == MsgType_RESPONSE) {
+                RLOGD("processResponse: calling sapCallback->disconnectResponse %d", rsp->token);
+                sapImpl->sapCallback->disconnectResponse(rsp->token);
+            } else {
+                RIL_SIM_SAP_DISCONNECT_IND *disconnectInd =
+                        (RIL_SIM_SAP_DISCONNECT_IND *)messagePtr;
+                RLOGD("processResponse: calling sapCallback->disconnectIndication %d %d",
+                        rsp->token, disconnectInd->disconnectType);
+                sapImpl->sapCallback->disconnectIndication(rsp->token,
+                        (SapDisconnectType)disconnectInd->disconnectType);
+            }
+            break;
+
+        case MsgId_RIL_SIM_SAP_APDU: {
+            RIL_SIM_SAP_APDU_RSP *apduRsp = (RIL_SIM_SAP_APDU_RSP *)messagePtr;
+            SapResultCode apduResponse = convertApduResponseProtoToHal(apduRsp->response);
+            RLOGD("processResponse: calling sapCallback->apduResponse %d %d",
+                    rsp->token, apduResponse);
+            hidl_vec<uint8_t> apduRspVec;
+            if (apduRsp->apduResponse != NULL && apduRsp->apduResponse->size > 0) {
+                apduRspVec.setToExternal(apduRsp->apduResponse->bytes, apduRsp->apduResponse->size);
+            }
+            sapImpl->sapCallback->apduResponse(rsp->token, apduResponse, apduRspVec);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_TRANSFER_ATR: {
+            RIL_SIM_SAP_TRANSFER_ATR_RSP *transferAtrRsp =
+                (RIL_SIM_SAP_TRANSFER_ATR_RSP *)messagePtr;
+            SapResultCode transferAtrResponse =
+                convertTransferAtrResponseProtoToHal(transferAtrRsp->response);
+            RLOGD("processResponse: calling sapCallback->transferAtrResponse %d %d",
+                    rsp->token, transferAtrResponse);
+            hidl_vec<uint8_t> transferAtrRspVec;
+            if (transferAtrRsp->atr != NULL && transferAtrRsp->atr->size > 0) {
+                transferAtrRspVec.setToExternal(transferAtrRsp->atr->bytes,
+                        transferAtrRsp->atr->size);
+            }
+            sapImpl->sapCallback->transferAtrResponse(rsp->token, transferAtrResponse,
+                    transferAtrRspVec);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_POWER: {
+            SapResultCode powerResponse = convertPowerResponseProtoToHal(
+                    ((RIL_SIM_SAP_POWER_RSP *)messagePtr)->response);
+            RLOGD("processResponse: calling sapCallback->powerResponse %d %d",
+                    rsp->token, powerResponse);
+            sapImpl->sapCallback->powerResponse(rsp->token, powerResponse);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_RESET_SIM: {
+            SapResultCode resetSimResponse = convertResetSimResponseProtoToHal(
+                    ((RIL_SIM_SAP_RESET_SIM_RSP *)messagePtr)->response);
+            RLOGD("processResponse: calling sapCallback->resetSimResponse %d %d",
+                    rsp->token, resetSimResponse);
+            sapImpl->sapCallback->resetSimResponse(rsp->token, resetSimResponse);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_STATUS: {
+            RIL_SIM_SAP_STATUS_IND *statusInd = (RIL_SIM_SAP_STATUS_IND *)messagePtr;
+            RLOGD("processResponse: calling sapCallback->statusIndication %d %d",
+                    rsp->token, statusInd->statusChange);
+            sapImpl->sapCallback->statusIndication(rsp->token, (SapStatus)statusInd->statusChange);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS: {
+            RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *transferStatusRsp =
+                    (RIL_SIM_SAP_TRANSFER_CARD_READER_STATUS_RSP *)messagePtr;
+            SapResultCode transferCardReaderStatusResponse =
+                    convertTransferCardReaderStatusResponseProtoToHal(
+                    transferStatusRsp->response);
+            RLOGD("processResponse: calling sapCallback->transferCardReaderStatusResponse %d %d %d",
+                    rsp->token,
+                    transferCardReaderStatusResponse,
+                    transferStatusRsp->CardReaderStatus);
+            sapImpl->sapCallback->transferCardReaderStatusResponse(rsp->token,
+                    transferCardReaderStatusResponse,
+                    transferStatusRsp->CardReaderStatus);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_ERROR_RESP: {
+            RLOGD("processResponse: calling sapCallback->errorResponse %d", rsp->token);
+            sapImpl->sapCallback->errorResponse(rsp->token);
+            break;
+        }
+
+        case MsgId_RIL_SIM_SAP_SET_TRANSFER_PROTOCOL: {
+            SapResultCode setTransferProtocolResponse;
+            if (((RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP *)messagePtr)->response ==
+                    RIL_SIM_SAP_SET_TRANSFER_PROTOCOL_RSP_Response_RIL_E_SUCCESS) {
+                setTransferProtocolResponse = SapResultCode::SUCCESS;
+            } else {
+                setTransferProtocolResponse = SapResultCode::NOT_SUPPORTED;
+            }
+            RLOGD("processResponse: calling sapCallback->transferProtocolResponse %d %d",
+                    rsp->token, setTransferProtocolResponse);
+            sapImpl->sapCallback->transferProtocolResponse(rsp->token, setTransferProtocolResponse);
+            break;
+        }
+
+        default:
+            break;
+    }
+}
+
+void sap::processResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
+    processResponse(rsp, sapSocket, MsgType_RESPONSE);
+}
+
+void sap::processUnsolResponse(MsgHeader *rsp, RilSapSocket *sapSocket) {
+    processResponse(rsp, sapSocket, MsgType_UNSOL_RESPONSE);
+}
+
+void sap::registerService(RIL_RadioFunctions *callbacks) {
+    using namespace android::hardware;
+    int simCount = 1;
+    char *serviceNames[] = {
+        "sap_uim_socket1"
+        #if (SIM_COUNT >= 2)
+        , "sap_uim_socket2"
+        #if (SIM_COUNT >= 3)
+        , "sap_uim_socket3"
+        #if (SIM_COUNT >= 4)
+        , "sap_uim_socket4"
+        #endif
+        #endif
+        #endif
+    };
+
+    RIL_SOCKET_ID socketIds[] = {
+        RIL_SOCKET_1
+        #if (SIM_COUNT >= 2)
+        , RIL_SOCKET_2
+        #if (SIM_COUNT >= 3)
+        , RIL_SOCKET_3
+        #if (SIM_COUNT >= 4)
+        , RIL_SOCKET_4
+        #endif
+        #endif
+        #endif
+    };
+    #if (SIM_COUNT >= 2)
+    simCount = SIM_COUNT;
+    #endif
+
+    for (int i = 0; i < simCount; i++) {
+        sapService[i] = new SapImpl;
+        sapService[i]->slotId = i;
+        sapService[i]->rilSocketId = socketIds[i];
+        RLOGD("registerService: starting ISap %s for slotId %d", serviceNames[i], i);
+        android::status_t status = sapService[i]->registerAsService(serviceNames[i]);
+        RLOGD("registerService: started ISap %s status %d", serviceNames[i], status);
+    }
+}
\ No newline at end of file
diff --git a/libril/sap_service.h b/libril/sap_service.h
new file mode 100644
index 0000000..afed612
--- /dev/null
+++ b/libril/sap_service.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SAP_SERVICE_H
+#define SAP_SERVICE_H
+
+#include <telephony/ril.h>
+#include <ril_internal.h>
+#include <RilSapSocket.h>
+#include <hardware/ril/librilutils/proto/sap-api.pb.h>
+
+namespace sap {
+
+void registerService(RIL_RadioFunctions *callbacks);
+void processResponse(MsgHeader *rsp, RilSapSocket *sapSocket);
+void processUnsolResponse(MsgHeader *rsp, RilSapSocket *sapSocket);
+
+}   // namespace android
+
+#endif  // RIL_SERVICE_H
\ No newline at end of file