ril: import Oreo AOSP libril

Change-Id: I00750bad812dc263e9c74a504b48512a892376c8
diff --git a/ril/libril/RilSapSocket.cpp b/ril/libril/RilSapSocket.cpp
index 33eabb5..cf99773 100644
--- a/ril/libril/RilSapSocket.cpp
+++ b/ril/libril/RilSapSocket.cpp
@@ -26,18 +26,10 @@
 #include <utils/Log.h>
 #include <arpa/inet.h>
 #include <errno.h>
+#include <sap_service.h>
 
 static RilSapSocket::RilSapSocketList *head = NULL;
 
-void ril_sap_on_request_complete (
-        RIL_Token t, RIL_Errno e,
-        void *response, size_t responselen
-);
-
-void ril_sap_on_unsolicited_response (
-        int unsolResponse, const void *data,
-        size_t datalen
-);
 extern "C" void
 RIL_requestTimedCallback (RIL_TimedCallback callback, void *param,
         const struct timeval *relativeTime);
@@ -86,7 +78,9 @@
        const void *data,
        size_t datalen) {
     RilSapSocket *sap_socket = getSocketById(RIL_SOCKET_1);
-    sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
+    if(sap_socket){
+        sap_socket->onUnsolicitedResponse(unsolResponse, (void *)data, datalen);
+    }
 }
 #endif
 
@@ -120,14 +114,14 @@
 void RilSapSocket::initSapSocket(const char *socketName,
         RIL_RadioFunctions *uimFuncs) {
 
-    if (strcmp(socketName, "sap_uim_socket1") == 0) {
+    if (strcmp(socketName, RIL1_SERVICE_NAME) == 0) {
         if(!SocketExists(socketName)) {
             addSocketToList(socketName, RIL_SOCKET_1, uimFuncs);
         }
     }
 
 #if (SIM_COUNT >= 2)
-    if (strcmp(socketName, "sap_uim_socket2") == 0) {
+    if (strcmp(socketName, RIL2_SERVICE_NAME) == 0) {
         if(!SocketExists(socketName)) {
             addSocketToList(socketName, RIL_SOCKET_2, uimFuncs);
         }
@@ -135,7 +129,7 @@
 #endif
 
 #if (SIM_COUNT >= 3)
-    if (strcmp(socketName, "sap_uim_socket3") == 0) {
+    if (strcmp(socketName, RIL3_SERVICE_NAME) == 0) {
         if(!SocketExists(socketName)) {
             addSocketToList(socketName, RIL_SOCKET_3, uimFuncs);
         }
@@ -143,7 +137,7 @@
 #endif
 
 #if (SIM_COUNT >= 4)
-    if (strcmp(socketName, "sap_uim_socket4") == 0) {
+    if (strcmp(socketName, RIL4_SERVICE_NAME) == 0) {
         if(!SocketExists(socketName)) {
             addSocketToList(socketName, RIL_SOCKET_4, uimFuncs);
         }
@@ -161,6 +155,7 @@
         RilSapSocketList* listItem = (RilSapSocketList*)malloc(sizeof(RilSapSocketList));
         if (!listItem) {
             RLOGE("addSocketToList: OOM");
+            delete socket;
             return;
         }
         listItem->socket = socket;
@@ -179,7 +174,6 @@
             }
             current->next = listItem;
         }
-        socket->socketInit();
     }
 }
 
@@ -195,24 +189,6 @@
     return false;
 }
 
-void* RilSapSocket::processRequestsLoop(void) {
-    RLOGI("UIM_SOCKET:Request loop started");
-
-    while(true) {
-        SapSocketRequest *req = dispatchQueue.dequeue();
-
-        RLOGI("New request from the dispatch Queue");
-
-        if (req != NULL) {
-            dispatchRequest(req->curr);
-            free(req);
-        } else {
-            RLOGE("Fetched null buffer from queue!");
-        }
-    }
-    return NULL;
-}
-
 RilSapSocket::RilSapSocket(const char *socketName,
         RIL_SOCKET_ID socketId,
         RIL_RadioFunctions *inputUimFuncs):
@@ -222,49 +198,6 @@
     }
 }
 
-#define BYTES_PER_LINE 16
-
-#define NIBBLE_TO_HEX(n) ({ \
-  uint8_t __n = (uint8_t) n & 0x0f; \
-  __nibble >= 10 ? 'A' + __n - 10: '0' + __n; \
-})
-
-#define HEX_HIGH(b) ({ \
-  uint8_t __b = (uint8_t) b; \
-  uint8_t __nibble = (__b >> 4) & 0x0f; \
-  NIBBLE_TO_HEX(__nibble); \
-})
-
-#define HEX_LOW(b) ({ \
-  uint8_t __b = (uint8_t) b; \
-  uint8_t __nibble = __b & 0x0f; \
-  NIBBLE_TO_HEX(__nibble); \
-})
-
-void log_hex(const char *who, const uint8_t *buffer, int length) {
-    char out[80];
-    int source = 0;
-    int dest = 0;
-    int dest_len = sizeof(out);
-    int per_line = 0;
-
-    do {
-        dest += sprintf(out, "%8.8s [%8.8x] ", who, source);
-        for(; source < length && dest_len - dest > 3 && per_line < BYTES_PER_LINE; source++,
-        per_line ++) {
-            out[dest++] = HEX_HIGH(buffer[source]);
-            out[dest++] = HEX_LOW(buffer[source]);
-            out[dest++] = ' ';
-        }
-        if (dest < dest_len && (per_line == BYTES_PER_LINE || source >= length)) {
-            out[dest++] = 0;
-            per_line = 0;
-            dest = 0;
-            RLOGD("%s\n", out);
-        }
-    } while(source < length && dest < dest_len);
-}
-
 void RilSapSocket::dispatchRequest(MsgHeader *req) {
     // SapSocketRequest will be deallocated in onRequestComplete()
     SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest));
@@ -282,11 +215,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);
@@ -317,9 +252,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);
     }
 
@@ -333,54 +269,6 @@
     free(hdr);
 }
 
-void RilSapSocket::sendResponse(MsgHeader* hdr) {
-    size_t encoded_size = 0;
-    uint32_t written_size;
-    size_t buffer_size = 0;
-    pb_ostream_t ostream;
-    bool success = false;
-
-    pthread_mutex_lock(&write_lock);
-
-    if ((success = pb_get_encoded_size(&encoded_size, MsgHeader_fields,
-        hdr)) && encoded_size <= INT32_MAX && commandFd != -1) {
-        buffer_size = encoded_size + sizeof(uint32_t);
-        uint8_t* buffer = (uint8_t*)malloc(buffer_size);
-        if (!buffer) {
-            RLOGE("sendResponse: OOM");
-            pthread_mutex_unlock(&write_lock);
-            return;
-        }
-        written_size = htonl((uint32_t) encoded_size);
-        ostream = pb_ostream_from_buffer(buffer, buffer_size);
-        pb_write(&ostream, (uint8_t *)&written_size, sizeof(written_size));
-        success = pb_encode(&ostream, MsgHeader_fields, hdr);
-
-        if (success) {
-            RLOGD("Size: %zu (0x%zx) Size as written: 0x%x", encoded_size,
-                    encoded_size, written_size);
-            log_hex("onRequestComplete", &buffer[sizeof(written_size)], encoded_size);
-            RLOGI("[%d] < SAP RESPONSE type: %d. id: %d. error: %d",
-        hdr->token, hdr->type, hdr->id,hdr->error );
-
-            if ( 0 != blockingWrite_helper(commandFd, buffer, buffer_size)) {
-                RLOGE("Error %d while writing to fd", errno);
-            } else {
-                RLOGD("Write successful");
-            }
-        } else {
-            RLOGE("Error while encoding response of type %d id %d buffer_size: %zu: %s.",
-                    hdr->type, hdr->id, buffer_size, PB_GET_ERROR(&ostream));
-        }
-        free(buffer);
-    } else {
-        RLOGE("Not sending response type %d: encoded_size: %zu. commandFd: %d. encoded size result:\
-                %d", hdr->type, encoded_size, commandFd, success);
-    }
-
-    pthread_mutex_unlock(&write_lock);
-}
-
 void RilSapSocket::onUnsolicitedResponse(int unsolResponse, void *data, size_t datalen) {
     if (data && datalen > 0) {
         pb_bytes_array_t *payload = (pb_bytes_array_t *)calloc(1,
@@ -396,119 +284,7 @@
         rsp.type = MsgType_UNSOL_RESPONSE;
         rsp.id = (MsgId)unsolResponse;
         rsp.error = Error_RIL_E_SUCCESS;
-        sendResponse(&rsp);
+        sap::processUnsolResponse(&rsp, this);
         free(payload);
     }
-}
-
-void RilSapSocket::pushRecord(void *p_record, size_t recordlen) {
-    pb_istream_t stream = pb_istream_from_buffer((uint8_t *)p_record, recordlen);
-    // MsgHeader will be deallocated in onRequestComplete()
-    MsgHeader *reqHeader = (MsgHeader *)malloc(sizeof (MsgHeader));
-    if (!reqHeader) {
-        RLOGE("pushRecord: OOM");
-        return;
-    }
-    memset(reqHeader, 0, sizeof(MsgHeader));
-
-    log_hex("BtSapTest-Payload", (const uint8_t*)p_record, recordlen);
-
-    if (!pb_decode(&stream, MsgHeader_fields, reqHeader) ) {
-        RLOGE("Error decoding protobuf buffer : %s", PB_GET_ERROR(&stream));
-        free(reqHeader);
-    } else {
-        // SapSocketRequest will be deallocated in processRequestsLoop()
-        SapSocketRequest *recv = (SapSocketRequest*)malloc(sizeof(SapSocketRequest));
-        if (!recv) {
-            RLOGE("pushRecord: OOM");
-            free(reqHeader);
-            return;
-        }
-        recv->token = reqHeader->token;
-        recv->curr = reqHeader;
-        recv->socketId = id;
-
-        dispatchQueue.enqueue(recv);
-    }
-}
-
-void RilSapSocket::sendDisconnect() {
-    size_t encoded_size = 0;
-    uint32_t written_size;
-    size_t buffer_size = 0;
-    pb_ostream_t ostream;
-    bool success = false;
-
-    RIL_SIM_SAP_DISCONNECT_REQ disconnectReq;
-
-   if ((success = pb_get_encoded_size(&encoded_size, RIL_SIM_SAP_DISCONNECT_REQ_fields,
-        &disconnectReq)) && encoded_size <= INT32_MAX) {
-        buffer_size = encoded_size + sizeof(uint32_t);
-        uint8_t* buffer = (uint8_t*)malloc(buffer_size);
-        if (!buffer) {
-            RLOGE("sendDisconnect: OOM");
-            return;
-        }
-        written_size = htonl((uint32_t) encoded_size);
-        ostream = pb_ostream_from_buffer(buffer, buffer_size);
-        pb_write(&ostream, (uint8_t *)&written_size, sizeof(written_size));
-        success = pb_encode(&ostream, RIL_SIM_SAP_DISCONNECT_REQ_fields, buffer);
-
-        if(success) {
-            // Buffer will be deallocated in sOnRequestComplete()
-            pb_bytes_array_t *payload = (pb_bytes_array_t *)calloc(1,
-                    sizeof(pb_bytes_array_t) + written_size);
-            if (!payload) {
-                RLOGE("sendDisconnect: OOM");
-                return;
-            }
-            memcpy(payload->bytes, buffer, written_size);
-            payload->size = written_size;
-            // MsgHeader will be deallocated in sOnRequestComplete()
-            MsgHeader *hdr = (MsgHeader *)malloc(sizeof(MsgHeader));
-            if (!hdr) {
-                RLOGE("sendDisconnect: OOM");
-                free(payload);
-                return;
-            }
-            hdr->payload = payload;
-            hdr->type = MsgType_REQUEST;
-            hdr->id = MsgId_RIL_SIM_SAP_DISCONNECT;
-            hdr->error = Error_RIL_E_SUCCESS;
-            dispatchDisconnect(hdr);
-        }
-        else {
-            RLOGE("Encode failed in send disconnect!");
-        }
-        free(buffer);
-    }
-}
-
-void RilSapSocket::dispatchDisconnect(MsgHeader *req) {
-    // SapSocketRequest will be deallocated in sOnRequestComplete()
-    SapSocketRequest* currRequest=(SapSocketRequest*)malloc(sizeof(SapSocketRequest));
-    if (!currRequest) {
-        RLOGE("dispatchDisconnect: OOM");
-        // Free memory allocated in sendDisconnect
-        free(req->payload);
-        free(req);
-        return;
-    }
-    currRequest->token = -1;
-    currRequest->curr = req;
-    currRequest->p_next = NULL;
-    currRequest->socketId = (RIL_SOCKET_ID)99;
-
-    RLOGD("Sending disconnect on command close!");
-
-#if defined(ANDROID_MULTI_SIM)
-    uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest, id);
-#else
-    uimFuncs->onRequest(req->id, req->payload->bytes, req->payload->size, currRequest);
-#endif
-}
-
-void RilSapSocket::onCommandsSocketClosed() {
-    sendDisconnect();
-    RLOGE("Socket command closed");
-}
+}
\ No newline at end of file