[Feature][L805-15] RIL FWK update for DSDS

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

Change-Id: Ica4204d89e843274d04e5809ed25e81b66e2e35a
diff --git a/common_src/lib/liblynq-sim/include/lynq_sim.h b/common_src/lib/liblynq-sim/include/lynq_sim.h
index c2d7793..ac00626 100755
--- a/common_src/lib/liblynq-sim/include/lynq_sim.h
+++ b/common_src/lib/liblynq-sim/include/lynq_sim.h
@@ -84,6 +84,14 @@
  * 
  */
 int lynq_factory_radio_state(int num);
+/**
+ * @brief set default sim card
+ * @param slot: type [IN] <slot> sim card slot
+ * @return int
+ * 0:success
+ * other:fail
+ */
+int lynq_set_default_sim(const int slot);
 
 
 #ifdef __cplusplus
diff --git a/common_src/lib/liblynq-sim/lynq_sim_urc.cpp b/common_src/lib/liblynq-sim/lynq_sim_urc.cpp
new file mode 100755
index 0000000..daee1d3
--- /dev/null
+++ b/common_src/lib/liblynq-sim/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/lynq_sim_urc.h b/common_src/lib/liblynq-sim/lynq_sim_urc.h
new file mode 100755
index 0000000..5173311
--- /dev/null
+++ b/common_src/lib/liblynq-sim/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();
diff --git a/common_src/lib/liblynq-sim/makefile b/common_src/lib/liblynq-sim/makefile
index f34e6b0..25758ec 100755
--- a/common_src/lib/liblynq-sim/makefile
+++ b/common_src/lib/liblynq-sim/makefile
@@ -45,6 +45,7 @@
   -I$(ROOT)$(includedir)/vendor-ril \
   -I$(ROOT)$(includedir)/logger \
   -I$(ROOT)$(includedir)/liblog \
+  -I$(ROOT)$(includedir)/lynq-ril \
 
 
 LOCAL_LIBS := \
diff --git a/common_src/lib/liblynq-sim/src/lynq_sim.cpp b/common_src/lib/liblynq-sim/src/lynq_sim.cpp
index c780859..cbc8459 100755
--- a/common_src/lib/liblynq-sim/src/lynq_sim.cpp
+++ b/common_src/lib/liblynq-sim/src/lynq_sim.cpp
@@ -24,14 +24,16 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include "lynq_sim.h"
+#include "liblog/lynq_deflog.h"
+#include "lynq_sim_urc.h"
+
+
 #define MAX_BUF 20
 #define MAX_NUM 80
-#define LYNQ_REQUEST_SET_DEFAULT_SIM_ALL 8008
-#define LYNQ_REQUEST_CHANGE_SCREEN_STATE 8014 /*add for two sim suspend on 20220919*/
-#define LYNQ_REQUEST_CHANGE_RADIO 8015
+
 #define MAX_LEN 1024*8
-#define MAX_NUM 10
-#define LOG_TAG "LYNQ_SIM"
+
+#define USER_LOG_TAG "LYNQ_SIM"
 #define FLAG_TESS 0
 using ::android::Parcel;
 #define DEST_PORT 8088
@@ -79,42 +81,37 @@
     int error;
 }lynq_resp_t;
 
-#define RESULT_OK (0)
-#define RESULT_ERROR (-1)
+static pthread_mutex_t s_sim_allow_data_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t s_sim_allow_data_cond = PTHREAD_COND_INITIALIZER;
+static int s_sim_allow_data_value = -1;
+#define SIM_ALLOW_DATA_TIMEOUT 60*1000
 
-typedef enum{
-    /*base abnormal*/
-    LYNQ_E_PARAMETER_ANONALY=7000,
-    LYNQ_E_SEND_REQUEST_FAIL=7001,
-    LYNQ_E_GET_HEAD_ERROR=7002,
-    LYNQ_E_INNER_ERROR=7100,
-    LYNQ_E_MALLOC_ERROR=7101,
-    /**/
-    LYNQ_E_CARDSTATE_ERROR=8000,
-    /* The voice service state is out of service*/
-    LYNQ_E_STATE_OUT_OF_SERVICE=8001,
-    /* The voice service state is EMERGENCY_ONLY*/
-    LYNQ_E_STATE_EMERGENCY_ONLY=8002,
-    /* The radio power is power off*/
-    LYNQ_E_STATE_POWER_OFF=8003,
-    LYNQ_E_TIME_OUT=8004,
-    /*create or open sms DB fail */
-    LYNQ_E_SMS_DB_FAIL=8005,
-    /*Failed to execute sql statement*/
-    LYNQ_E_SMS_SQL_FAIL = 8006,
-    LYNQ_E_SMS_NOT_FIND = 8007,
-    /* The logic conflict*/
-    LYNQ_E_CONFLICT=9000,
-    /*Null anomaly*/
-    LYNQ_E_NULL_ANONALY=9001,
-     /*Invalid id anomaly*/
-    LYNQ_E_INVALID_ID_ANONALY=9002,
-#ifdef ECALL_SUPPORT
-    LYNQ_E_ECALL_BEING_RUNNING =9003,
-    LYNQ_E_ECALL_MSD_LENGTH_ERROR =9004,
-    LYNQ_E_ECALL_DAILING_NO_ANSWER =9005,
-#endif
-}LYNQ_E;
+int waitAllowDataSignal(int mtime)
+{
+    int ret = 0;
+    int sec = 0;
+    int usec = 0;
+    struct timeval now;
+    struct timespec timeout;
+    gettimeofday(&now,NULL);
+    sec = mtime/1000;
+    usec = mtime%1000;
+    timeout.tv_sec = now.tv_sec+sec;
+    timeout.tv_nsec = now.tv_usec*1000+usec*1000000;
+    pthread_mutex_lock(&s_sim_allow_data_mutex);
+    ret = pthread_cond_timedwait(&s_sim_allow_data_cond,&s_sim_allow_data_mutex,&timeout);
+    pthread_mutex_unlock(&s_sim_allow_data_mutex);
+    return ret;
+}
+
+void sendAllowDataSignal(int value)
+{
+    pthread_mutex_lock(&s_sim_allow_data_mutex);
+    s_sim_allow_data_value = value;
+    pthread_cond_signal(&s_sim_allow_data_cond);
+    pthread_mutex_unlock(&s_sim_allow_data_mutex);
+    return;
+}
 
 /**@brief print solicied response msg's head information
 * @param head [IN]: head information
@@ -213,7 +210,8 @@
 }
 
 int lynq_sim_init(int utoken){
-
+    LYLOGSET(LOG_DEBUG);
+    LYLOGEINIT(USER_LOG_TAG);
     RLOGE("%s start, parameter is %d", __func__,utoken);
     
     if(g_lynq_sim_init_flag == 1)
@@ -249,17 +247,24 @@
     timeOut.tv_usec = 0;
     if (setsockopt(sock_fd, SOL_SOCKET, SO_RCVTIMEO, &timeOut, sizeof(timeOut)) < 0) 
     {
-        RLOGD("time out setting failed\n"); 
+        RLOGD("time out setting failed\n");
         return -1;
     }
-    /* 设置address */  
-    memset(&addr_serv, 0, sizeof(addr_serv));  
-    addr_serv.sin_family = AF_INET;  
-    addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);  
-    addr_serv.sin_port = htons(DEST_PORT);  
-    len_addr_serv = sizeof(addr_serv);  
-    /*test*/
 
+    memset(&addr_serv, 0, sizeof(addr_serv));
+    addr_serv.sin_family = AF_INET;  
+    addr_serv.sin_addr.s_addr = inet_addr(DSET_IP_ADDRESS);
+    addr_serv.sin_port = htons(DEST_PORT);
+    len_addr_serv = sizeof(addr_serv);
+
+#ifdef MODE_DSDS
+    ret = lynq_start_all_urc_socket_thread();
+    if(ret != RESULT_OK)
+    {
+        LYERRLOG("init socket urc fail!!!");
+        return LYNQ_E_INNER_ERROR;
+    }
+#endif
     RLOGE("%s end suc", __func__);
     return 0;
 }
@@ -273,6 +278,10 @@
         RLOGD("lynq_sim_deinit  failed");
         return -1;
     }
+
+#ifdef DMODE_DSDS
+    lynq_close_all_urc_socket_thread();
+#endif
     g_lynq_sim_init_flag = 0;
     close(sock_fd);
 
@@ -639,6 +648,44 @@
     return 0;
 }
 
+#ifdef MODE_DSDS
+int lynq_set_default_sim(const int slot)
+{
+    if(g_lynq_sim_init_flag == 0)
+    {
+        RLOGE("can`t init SIM module");
+        return -1;
+    }
+    if(!judge(slot))
+    {
+        RLOGD("is not 0 or 1");
+        return -1;
+    }
+    int send_num = 0;
+    lynq_client_t client_t;
+    client_t.request = LYNQ_REQUEST_SET_DEFAULT_SIM_ALL;
+    client_t.paramLen = 1;
+    client_t.uToken = Global_uToken;
+    sprintf(client_t.param, "%d\n", slot);
+    pthread_mutex_lock(&g_lynq_sim_sendto_mutex);
+    send_num = sendto(sock_fd, &client_t, sizeof(client_t), 0, (struct sockaddr *)&addr_serv, len_addr_serv);
+    if(send_num < 0)
+    {
+        RLOGD("function %s sendto error:", __FUNCTION__);
+        return send_num;
+    }
+
+    if(waitAllowDataSignal(SIM_ALLOW_DATA_TIMEOUT) == ETIMEDOUT)
+    {
+        LYERRLOG("timeout:wait allow data fail!!!");
+        return LYNQ_E_TIME_OUT;
+    }
+    pthread_mutex_unlock(&g_lynq_sim_sendto_mutex);
+    LYERRLOG("sim allow data value %d",s_sim_allow_data_value);
+    return s_sim_allow_data_value;
+}
+#endif
+
 int lynq_switch_card(int slot){
     if(g_lynq_sim_init_flag == 0)
     {
@@ -914,3 +961,29 @@
     return 0;
 }
 
+void urc_msg_process(Parcel *p)
+{
+    int resp_type;
+    int urcid;
+    int slot_id;
+    int error_code;
+
+    int size=p->dataSize();
+    p->readInt32(&resp_type);
+    p->readInt32(&urcid);
+    p->readInt32(&slot_id);
+    LYINFLOG("%s urc id = %d, slot_id = %d, size is %d",__func__, urcid,slot_id,size);
+    switch (urcid)
+    {
+        case LYNQ_URC_ALLOW_DATA://new sms received
+        {
+            LYINFLOG("**************:resp_type=%d,urcid=%d,slot_id=%d",resp_type,urcid,slot_id);
+            p->readInt32(&error_code);
+            sendAllowDataSignal(error_code);
+            break;
+        }
+        default:
+            break;
+    }
+}
+