[feature]sim io

Change-Id: Ib9dd5efce39066f716c1d718cc35decb232b27cc
diff --git a/lib/liblynq-sim/src/lynq_sim.cpp b/lib/liblynq-sim/src/lynq_sim.cpp
index 28cacb7..3eee001 100755
--- a/lib/liblynq-sim/src/lynq_sim.cpp
+++ b/lib/liblynq-sim/src/lynq_sim.cpp
@@ -32,6 +32,7 @@
 #define RIL_REQUEST_CHANGE_SIM_PIN 6
 #define RIL_REQUEST_OEM_HOOK_RAW 59
 #define RIL_REQUEST_SCREEN_STATE 61
+#define RIL_REQUEST_SIM_IO 28
 #define LYNQ_REQUEST_SET_DEFAULT_SIM_ALL 8008
 #define MAX_LEN 1024*8
 #define MAX_NUM 10
@@ -58,13 +59,30 @@
 int request = -1;
 int slot_id = -1;
 int error1 = -1;
+/**
+ * @brief mark call initialization state
+ * 0: deinit state
+ * 1: init state
+ */
+int g_lynq_sim_init_flag = 0;
+
+/**
+ * @brief lynq_req_sim_io need to send request
+ */
+char data_buf[32] = {0};
+char pin2_buf[32] = {0};
+char aidPtr_buf[32] = {0};
 
 int lynq_sim_init(int utoken){
+    if(g_lynq_sim_init_flag == 1)
+    {
+        RLOGD("lynq_sim_init  failed");
+        return -1;
+    }
+    g_lynq_sim_init_flag = 1;
     if(utoken < 0){
         return -1;
     }
-    // LYLOGSET(LOG_INFO);
-    // LYLOGEINIT(USER_LOG_TAG);
     Global_uToken = utoken;
     sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
 	if (-1 == sock_fd)
@@ -82,6 +100,12 @@
 }
 
 int lynq_sim_deinit(void){
+    if(g_lynq_sim_init_flag == 0)
+    {
+        RLOGD("lynq_sim_deinit  failed");
+        return -1;
+    }
+    g_lynq_sim_init_flag = 0;
     close(sock_fd);
     return 0;
 }
@@ -94,7 +118,7 @@
     return strndup16to8(s16, stringlen);
 }
 
-/*If you need to use any API under lynq_sim, you mustfirst call the init_sim() function to initialize these functions.*/
+/*If you need to use any API under lynq_sim, you mustfirst call the lynq_sim_init() function to initialize these functions.*/
 int lynq_get_sim_status(int *card_status)
 {   
     int ret = -1;
@@ -137,7 +161,7 @@
     }
     return error1;
 }
-/*AT> AT+CIMI     AT< IMSI*/
+
 int lynq_get_imsi(char buf[])
 {   
     int ret = -1;
@@ -658,6 +682,121 @@
     }
     return 0;
 }
+
+/**
+ * @brief                   Check whether the input is valid  for lynq_req_sim_io api
+ * @param  list             type: [IN] list[0]:one of the commands listed for TS 27.007 +CRSM.(command)
+ *                          type: [IN] list[1]:EF id(fileid)
+ *                          type: [IN] list[2]:offset(p1)
+ *                          type: [IN] list[3]:offset(p2)
+ *                          type: [IN] list[4]:response len,sometimes needn't care(p3)
+ * @param  path             type: [IN] "pathid" from TS 27.007 +CRSM command.
+                            type: [IN] Path is in hex asciii format eg "7f205f70"
+                            type: [IN] Path must always be provided.
+ * @param  data             type: [IN] May be NULL
+ * @param  pin2             type: [IN] May be NULL
+ * @param  aidPtr           type: [IN] AID value, See ETSI 102.221 8.1 and 101.220 4, NULL if no value.
+ * @param  sw               type: [OUT] 
+ * @param  simResponse      type: [OUT] response
+ * @return int
+ */
+static int judge_illegal(int list[5], char *path, char *data, char *pin2, char *aidPtr, int sw[2], char *simResponse)
+{
+    printf("__LINE__:%d\n", __LINE__);
+    if(list == NULL)
+    {
+        return -1;
+    }
+    if(path == NULL)
+    {
+        return -1;
+    }
+    if(sw == NULL){
+        return -1;
+    }
+    if(simResponse == NULL){
+        return -1;
+    }
+    if(data == NULL)
+    {
+        memcpy(data_buf, "null", 4);
+    }
+    else
+    {
+        bzero(data_buf,32);
+        memcpy(data_buf, data, strlen(data));
+    }
+    if(pin2 == NULL)
+    {
+        memcpy(pin2_buf, "null", 4);
+    }
+    else
+    {
+        bzero(pin2_buf,32);
+        memcpy(pin2_buf, data, strlen(data));
+    }
+    if(aidPtr == NULL)
+    {
+        memcpy(aidPtr_buf, "null", 4);
+    }
+    else
+    {
+        bzero(aidPtr_buf,32);
+        memcpy(aidPtr_buf, data, strlen(data));
+    }
+    return 0;
+}
+
+int lynq_req_sim_io(int list[5], char *path, char *data, char *pin2, char *aidPtr, int sw[2], char *simResponse)
+{   
+    int ret = -1;
+    if(judge_illegal(list, path, data, pin2, aidPtr, sw, simResponse))
+    {
+        return ret;
+    }
+    int send_num = 0;
+    int recv_num = 0;
+    char res_data[MAX_LEN] = {0};
+    int len = 0;
+    client_t.request = RIL_REQUEST_SIM_IO;
+    client_t.paramLen = 9;
+    client_t.uToken = Global_uToken;
+    sprintf(client_t.param, "%d %d %s %d %d %d %s %s %s\n", list[0], list[1], path, list[2], list[3], list[4], data_buf, pin2_buf, aidPtr_buf);
+    send_num = sendto(sock_fd, &client_t, sizeof(client_t), 0, (struct sockaddr *)&addr_serv, len_addr_serv);
+    if(send_num < 0)
+    {
+        RLOGD("sendto error:");
+        return send_num;
+    }
+    recv_num = recvfrom(sock_fd,res_data,sizeof(char)*MAX_LEN,0,(struct sockaddr *)&addr_serv,(socklen_t*)&len_addr_serv);
+    if(recv_num < 0 || recv_num == 0)
+    {
+        RLOGD("recvfrom step2 fail:");
+        return recv_num;
+    }
+    Parcel p;
+    p.setData((uint8_t *)res_data,sizeof(char)*recv_num); // p.setData((uint8_t *) buffer, buflen);
+    p.setDataPosition(0);
+    int num = -1;
+    if(p.dataAvail() > 0)
+    {   
+        char test[30] = {0};
+        char *argv[5] = {0};
+        p.readInt32(&resp_type);
+        p.readInt32(&request);
+        p.readInt32(&slot_id);
+        p.readInt32(&error1);
+        if(!error1)
+        {
+            p.readInt32(&sw[0]);
+            p.readInt32(&sw[1]);
+            char * test = lynqStrdupReadString(p);
+            memcpy(simResponse, test, strlen(test));
+        }
+
+    }
+    return error1;
+}
 #if FLAG_TESS
 int lynq_query_operator(char buf[]){
     int32_t token = -1;
@@ -705,7 +844,3 @@
 }
 
 #endif
-
-
-
-