[Feature][L805-13] lynq_set_default_sim_except_data and lynq_get_default_sim for DSDS

Only Configure:No
Affected branch:DSDS
Affected module:SIM
Is it affected on both ZXIC and MTK: MTK
Self-test: Yes
Doc Update:Need

Change-Id: I863d9c6cf69b8e36a8980b178cac8e1dbbb77237
diff --git a/common_src/lib/liblynq-sim/src/lynq_sim.cpp b/common_src/lib/liblynq-sim/src/lynq_sim.cpp
index cbc8459..97a9292 100755
--- a/common_src/lib/liblynq-sim/src/lynq_sim.cpp
+++ b/common_src/lib/liblynq-sim/src/lynq_sim.cpp
@@ -187,9 +187,24 @@
         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)
         {
+        #ifdef MODE_DSDS
+            if (errno == EAGAIN || errno == EWOULDBLOCK) 
+            {
+                RLOGD("recv from timeout");
+                pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
+                return LYNQ_E_TIME_OUT;
+            } 
+            else 
+            {
+                RLOGD("recv request fail, recv num is %d", recv_num);
+                pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
+                return recv_num;
+            }
+        #else
             RLOGD("recv request fail, recv num is %d", recv_num);
             pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
             return recv_num;
+        #endif
         }
         p->setData((uint8_t *)res_data,sizeof(char)*recv_num);
         p->setDataPosition(0);
@@ -279,7 +294,7 @@
         return -1;
     }
 
-#ifdef DMODE_DSDS
+    #ifdef MODE_DSDS
     lynq_close_all_urc_socket_thread();
 #endif
     g_lynq_sim_init_flag = 0;
@@ -684,6 +699,69 @@
     LYERRLOG("sim allow data value %d",s_sim_allow_data_value);
     return s_sim_allow_data_value;
 }
+
+
+int lynq_set_default_sim_except_data(const int slot)
+{
+    RLOGD("Enter %s",__FUNCTION__);
+
+    if(g_lynq_sim_init_flag == 0)
+    {
+        return -1;
+    }
+
+    RLOGD("Slot is %d",slot);
+
+    if(!judge(slot))
+    {
+        RLOGD("is not 0 or 1");
+        return -1;
+    }
+
+    int send_num = 0;
+    Parcel p;
+    RLOGD(
+"send LYNQ_REQUEST_SET_DEFAULT_SIM_ALL_EXCEPT_DATA");
+    int res = lynq_send_common_request(&p,LYNQ_REQUEST_SET_DEFAULT_SIM_ALL_EXCEPT_DATA,1,1,"%d",slot);
+    if(res != 0)
+    {
+        RLOGD("function %s execute error", __FUNCTION__);
+        return res;
+    }
+    
+    return 0;
+}
+
+
+
+int lynq_get_default_sim_all(int *bit_slot)
+{
+    RLOGD("Enter %s",__FUNCTION__);
+
+    int sim_except_data = 0;
+    int sim_data = 0;
+
+    if(g_lynq_sim_init_flag == 0)
+    {
+        return -1;
+    }
+
+    int send_num = 0;
+    Parcel p;
+    
+    RLOGD(
+"send LYNQ_REQUEST_GET_DEFAULT_SIM_ALL");
+    int res = lynq_send_common_request(&p,LYNQ_REQUEST_GET_DEFAULT_SIM_ALL,0,1,"");
+    if(res != 0)
+    {
+        RLOGD("function %s execute error", __FUNCTION__);
+        return res;
+    }
+
+    p.readInt32(bit_slot);
+    return 0;
+}
+
 #endif
 
 int lynq_switch_card(int slot){
diff --git a/common_src/lib/liblynq-sim/src/lynq_sim_urc.cpp b/common_src/lib/liblynq-sim/src/lynq_sim_urc.cpp
new file mode 100755
index 0000000..daee1d3
--- /dev/null
+++ b/common_src/lib/liblynq-sim/src/lynq_sim_urc.cpp
@@ -0,0 +1,173 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <string.h>
+#include <unistd.h>
+#include <binder/Parcel.h>
+#include <log/log.h>
+#include <cutils/jstring.h>
+#include <pthread.h>
+#include <list>
+#include <vendor-ril/telephony/ril.h>
+#include <vendor-ril/telephony/mtk_ril_sp.h>
+#include "liblog/lynq_deflog.h"
+#include "lynq_sim_urc.h"
+
+int module_len_urc_addr_serv;
+struct sockaddr_in module_urc_addr_serv;
+static int module_urc_sock_fd = -1;
+int module_urc_status = 1;
+pthread_t module_urc_tid = -1;
+
+static pthread_mutex_t s_ProcessUrcMsgBlockMutex = PTHREAD_MUTEX_INITIALIZER;
+#define BLOCK_PROCESS_URC_MSG_INIT() pthread_mutex_init(&s_ProcessUrcMsgBlockMutex,NULL)
+#define BLOCK_PROCESS_URC_MSG_LOCK() pthread_mutex_lock(&s_ProcessUrcMsgBlockMutex)
+#define BLOCK_PROCESS_URC_MSG_UNLOCK() pthread_mutex_unlock(&s_ProcessUrcMsgBlockMutex)
+
+bool is_support_urc(int urc_id)
+{
+    switch(urc_id)
+    {
+        case LYNQ_URC_ALLOW_DATA:
+            return true;
+        default:
+            return false;
+    }
+}
+
+void *thread_urc_recv(void *p)
+{
+    Parcel *urc_p =NULL;
+    char urc_data[LYNQ_REC_BUF];
+    int res = 0;
+    lynq_head_t* phead;
+    LYINFLOG("urc recv thread is running");
+    while(module_urc_status)
+    {
+        bzero(urc_data,LYNQ_REC_BUF);
+        res = recvfrom(module_urc_sock_fd,urc_data,sizeof(urc_data),0,(struct sockaddr *)&module_urc_addr_serv,(socklen_t*)&module_len_urc_addr_serv);
+        if(res<sizeof(int32_t)*2)
+        {
+            LYERRLOG("thread_urc_recv step2 fail: res is %d",res);
+            continue;
+        }
+
+        phead=(lynq_head_t*) urc_data;
+        if(is_support_urc(phead->urcid)==false)
+        {
+            continue;
+        }
+        urc_p = new Parcel();
+        if(urc_p == NULL)
+        {
+            LYERRLOG("new parcel failure!!!");
+            continue;
+        }
+        urc_p->setData((uint8_t *)urc_data,res); // p.setData((uint8_t *) buffer, buflen);
+        urc_p->setDataPosition(0);
+        if(urc_p->dataAvail()>0)
+        {
+            urc_msg_process(urc_p);
+        }
+        else 
+        {
+            delete urc_p;
+            urc_p = NULL;
+        }        
+    }
+    LYINFLOG("urc recv thread ended");
+    return NULL;
+}
+
+void lynq_close_urc_rev_thread()
+{
+    int ret;
+
+    BLOCK_PROCESS_URC_MSG_LOCK();  //just cancel urc process tid when recv from
+    module_urc_status = 0;
+    if(module_urc_tid!=-1)
+    {
+        ret = pthread_cancel(module_urc_tid);
+        LYINFLOG("pthread cancel urc rev ret = %d",ret);
+    }
+    BLOCK_PROCESS_URC_MSG_UNLOCK();
+    if(module_urc_tid != -1)
+    {
+        ret = pthread_join(module_urc_tid,NULL);
+        LYINFLOG("pthread join urc tid ret = %d",ret);
+        module_urc_tid =-1;
+    }
+}
+
+void lynq_close_urc_socket()
+{
+    if (module_urc_sock_fd >= 0)
+    {
+        close(module_urc_sock_fd);
+        module_urc_sock_fd =-1;
+    }
+}
+
+int lynq_setup_urc_socket()
+{
+    int on = 1;
+    int ret = 0;
+    module_len_urc_addr_serv = sizeof(sockaddr_in);
+    module_urc_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
+    if (module_urc_sock_fd <0){
+        LYERRLOG("urc socket error");
+        return RESULT_ERROR;  
+    }
+    module_urc_addr_serv.sin_family = AF_INET;
+    module_urc_addr_serv.sin_port = htons(LYNQ_URC_SERVICE_PORT);
+    module_urc_addr_serv.sin_addr.s_addr = inet_addr(LYNQ_URC_ADDRESS);
+    /* Set socket to allow reuse of address and port, SO_REUSEADDR value is 2*/
+    ret = setsockopt(module_urc_sock_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof on);
+    if(ret <0)
+    {
+        LYERRLOG("urc socket set error");
+        close(module_urc_sock_fd);
+        module_urc_sock_fd =-1;
+        return RESULT_ERROR;  
+    }
+    ret = bind(module_urc_sock_fd ,(struct sockaddr*)&module_urc_addr_serv, sizeof(module_urc_addr_serv));
+    if(ret <0)
+    {
+        LYERRLOG("urc socket bind error");
+        close(module_urc_sock_fd);
+        module_urc_sock_fd =-1;
+        return RESULT_ERROR;
+    }
+    return RESULT_OK;
+}
+
+int lynq_start_all_urc_socket_thread()
+{
+    int ret= lynq_setup_urc_socket();
+    if(ret!=RESULT_OK)
+    {
+        LYERRLOG("call lynq_setup_urc_socket fail");
+        return RESULT_ERROR;
+    }
+
+    module_urc_status = 1;
+    ret = pthread_create(&module_urc_tid,NULL,thread_urc_recv,NULL);
+    if(ret <0)
+    {
+        LYERRLOG("urc recv pthread create error");
+        module_urc_status = 0;
+        lynq_close_urc_socket();
+        return RESULT_ERROR;
+    }
+    LYINFLOG("urc start success");
+    return RESULT_OK;  
+}
+
+void lynq_close_all_urc_socket_thread()
+{
+    lynq_close_urc_rev_thread();
+    lynq_close_urc_socket();
+    LYERRLOG("close all urc socket thread!!!");
+}
+
diff --git a/common_src/lib/liblynq-sim/src/lynq_sim_urc.h b/common_src/lib/liblynq-sim/src/lynq_sim_urc.h
new file mode 100755
index 0000000..5173311
--- /dev/null
+++ b/common_src/lib/liblynq-sim/src/lynq_sim_urc.h
@@ -0,0 +1,23 @@
+#include "lynq_interface.h"
+using ::android::Parcel;
+
+#define LYNQ_ADDRESS "127.0.0.1"
+#define LYNQ_URC_SERVICE_PORT 8086
+#ifdef GSW_RIL_CFG
+#define LYNQ_URC_ADDRESS "127.255.255.255" /*hong.liu change broadcast addr on 2024.2.18*/
+#else
+#define LYNQ_URC_ADDRESS "0.0.0.0"
+#endif
+#define LYNQ_REC_BUF 8192
+
+#define RESULT_OK (0)
+#define RESULT_ERROR (-1)
+
+typedef struct{
+    int resp_type;
+    int urcid;   
+}lynq_head_t;
+
+void urc_msg_process(Parcel *p);
+int lynq_start_all_urc_socket_thread();
+void lynq_close_all_urc_socket_thread();