Add Multiple AT channels supported for ril api v2

Change-Id: I53f574c85d07bd7b8e0dd15d2e596d23c8772907
diff --git a/mbtk/mbtk_rild_v2/src/main.c b/mbtk/mbtk_rild_v2/src/main.c
index 939585f..14284d7 100755
--- a/mbtk/mbtk_rild_v2/src/main.c
+++ b/mbtk/mbtk_rild_v2/src/main.c
@@ -70,7 +70,7 @@
 mbtk_ril_err_enum sms_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);

 mbtk_ril_err_enum ecall_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);

 

-void data_call_retry(mbtk_ril_net_reg_state_info_t *reg_state);

+void data_call_retry(ATPortType_enum port, mbtk_ril_net_reg_state_info_t *reg_state);

 

 void data_call_state_change_cb(int cid, bool action, bool auto_change, int reason);

 static int send_pack_to_queue(sock_cli_info_t* cli_info, void* pack);

@@ -79,14 +79,14 @@
 static void onATTimeout()

 {

     LOGI("AT channel timeout; closing\n");

-    at_close();

+    at_close(ATPORTTYPE_0);

 }

 

 /* Called on command or reader thread */

 static void onATReaderClosed()

 {

     LOGI("AT channel closed\n");

-    at_close();

+    at_close(ATPORTTYPE_0);

 }

 

 static void sock_cli_free_func(void *data)
@@ -235,9 +235,9 @@
     }
 }
 
-static void ril_error_pack_send(int fd, int ril_id, int msg_index, int err)

+static void ril_error_pack_send(ATPortType_enum port, int fd, int ril_id, int msg_index, int err)

 {
-    ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_RSP, ril_id, msg_index, NULL, 0);

+    ril_msg_pack_info_t* pack = ril_msg_pack_creat(port, RIL_MSG_TYPE_RSP, ril_id, msg_index, NULL, 0);

     if(pack)
     {

         pack->err = (uint16)err;

@@ -250,9 +250,9 @@
     }
 }
 
-void ril_rsp_pack_send(int fd, int ril_id, int msg_index, const void* data, int data_len)

+void ril_rsp_pack_send(ATPortType_enum port, int fd, int ril_id, int msg_index, const void* data, int data_len)

 {

-    ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_RSP, ril_id, msg_index, data, data_len);

+    ril_msg_pack_info_t* pack = ril_msg_pack_creat(port, RIL_MSG_TYPE_RSP, ril_id, msg_index, data, data_len);

     if(pack)
     {
         pack->err = (uint16)MBTK_RIL_ERR_SUCCESS;

@@ -274,7 +274,7 @@
 

 void ril_ind_pack_send(int fd, int msg_id, const void* data, int data_len)

 {
-    ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_IND, msg_id, RIL_MSG_INDEX_INVALID, data, data_len);

+    ril_msg_pack_info_t* pack = ril_msg_pack_creat(ATPORTTYPE_NON, RIL_MSG_TYPE_IND, msg_id, RIL_MSG_INDEX_INVALID, data, data_len);

     if(pack)
     {
         pack->err = (uint16)0;

@@ -2034,25 +2034,25 @@
 

 static void ril_at_ready_process()
 {
-    ril_info.radio_state = ril_radio_state_get();

+    ril_info.radio_state = ril_radio_state_get(ATPORTTYPE_0);

     if (ril_info.radio_state != MBTK_RADIO_STATE_FULL_FUNC)

     {
-        ril_radio_state_set(MBTK_RADIO_STATE_FULL_FUNC, FALSE);

+        ril_radio_state_set(ATPORTTYPE_0, MBTK_RADIO_STATE_FULL_FUNC, FALSE);

     }

 
     if(ril_info.radio_state == MBTK_RADIO_STATE_FULL_FUNC)

     {

-        at_send_command("AT+CEREG=2", NULL);
+        at_send_command(ATPORTTYPE_0, "AT+CEREG=2", NULL);

     }
 
-    ril_info.sim_state = ril_sim_state_get();

+    ril_info.sim_state = ril_sim_state_get(ATPORTTYPE_0);

     if(ril_info.sim_state == MBTK_SIM_STATE_READY)

     {
         LOGD("SIM READY!");
-        at_send_command("AT+COPS=3", NULL);

+        at_send_command(ATPORTTYPE_0, "AT+COPS=3", NULL);

 

         // Set APN from prop.

-        apn_auto_conf_from_prop();

+        apn_auto_conf_from_prop(ATPORTTYPE_0);

     }
     else
     {
@@ -2084,12 +2084,20 @@
 

 // Process AT URC data
 static int send_pack_to_queue(sock_cli_info_t* cli_info, void* pack)

-{
-    if(ril_info.msg_queue.count >= PACK_PROCESS_QUEUE_MAX)

-    {
-        LOGE("Packet process queue is full");

-        return -1;
-    }
+{

+    if(cli_info) {

+        if(ril_info.msg_queue[cli_info->port].count >= PACK_PROCESS_QUEUE_MAX)

+        {

+            LOGE("Packet process queue is full");

+            return -1;

+        }

+    } else {

+        if(ril_info.msg_queue[ATPORTTYPE_0].count >= PACK_PROCESS_QUEUE_MAX)

+        {

+            LOGE("Packet process queue is full");

+            return -1;

+        }

+    }

 
     ril_msg_queue_info_t *item = (ril_msg_queue_info_t*)malloc(sizeof(ril_msg_queue_info_t));

     if(!item)
@@ -2098,13 +2106,23 @@
         return -1;
     }
     item->cli_info = cli_info;
-    item->pack = pack;
-    mbtk_queue_put(&ril_info.msg_queue, item);

-
-    // If thread is waitting,continue it.
-    pthread_mutex_lock(&ril_info.msg_mutex);

-    pthread_cond_signal(&ril_info.msg_cond);

-    pthread_mutex_unlock(&ril_info.msg_mutex);

+    item->pack = pack;

+

+    if(cli_info) {

+        mbtk_queue_put(&(ril_info.msg_queue[cli_info->port]), item);

+

+        // If thread is waitting,continue it.

+        pthread_mutex_lock(&(ril_info.msg_mutex[cli_info->port]));

+        pthread_cond_signal(&(ril_info.msg_cond[cli_info->port]));

+        pthread_mutex_unlock(&(ril_info.msg_mutex[cli_info->port]));

+    } else { // URC message, is null.

+        mbtk_queue_put(&(ril_info.msg_queue[ATPORTTYPE_0]), item);

+

+        // If thread is waitting,continue it.

+        pthread_mutex_lock(&(ril_info.msg_mutex[ATPORTTYPE_0]));

+        pthread_cond_signal(&(ril_info.msg_cond[ATPORTTYPE_0]));

+        pthread_mutex_unlock(&(ril_info.msg_mutex[ATPORTTYPE_0]));

+    }

 
     return 0;
 }

@@ -2128,11 +2146,11 @@
                 ind_regisger(cli_info, pack->msg_id);

             }

 

-            ril_error_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, err);

+            ril_error_pack_send(cli_info->port, cli_info->fd, pack->msg_id, pack->msg_index, err);

 

             ril_msg_pack_free(pack);

         } else {

-            LOGD("Start process REQ(%s), Length : %d", id2str(pack->msg_id), pack->data_len);

+            LOGD("Start process REQ(%s, Port : %d), Length : %d", id2str(pack->msg_id), cli_info->port, pack->data_len);

             if(0 && pack->data_len > 0)

             {

                 log_hex("DATA", pack->data, pack->data_len);

@@ -2192,7 +2210,7 @@
         case RIL_MSG_ID_IND_NET_REG_STATE_CHANGE:

         {

             mbtk_ril_net_reg_state_info_t *reg_state = (mbtk_ril_net_reg_state_info_t*)msg->data;

-            data_call_retry(reg_state);

+            data_call_retry(ATPORTTYPE_0, reg_state);

             break;

         }

         default:
@@ -2286,6 +2304,10 @@
                             {
                                 memset(info, 0, sizeof(sock_cli_info_t));

                                 info->fd = client_fd;

+

+                                // Default AT port.

+                                info->port = ATPORTTYPE_0;

+

                                 list_add(ril_info.sock_client_list, info);

                                 LOG("Add New Client FD Into List.");

 

@@ -2304,17 +2326,20 @@
                         // Read and process every message.
                         mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS;

                         ril_msg_pack_info_t** pack = ril_pack_recv(cli_info->fd, true, &err);

-
+

                         // Parse packet error,send error response to client.
                         if(pack == NULL)
                         {
-                            ril_error_pack_send(cli_info->fd, RIL_MSG_ID_UNKNOWN, RIL_MSG_INDEX_INVALID, err);

+                            ril_error_pack_send(cli_info->port, cli_info->fd, RIL_MSG_ID_UNKNOWN, RIL_MSG_INDEX_INVALID, err);

                         }
                         else
                         {
                             ril_msg_pack_info_t** pack_ptr = pack;

                             while(*pack_ptr)
-                            {
+                            {

+                                // Update AT port in the first.

+                                cli_info->port = (ATPortType_enum)((*pack_ptr)->at_port);

+

                                 pack_distribute(cli_info, *pack_ptr);
                                 // Not free,will free in pack_process() or packet process thread.
                                 //mbtk_info_pack_free(pack_ptr);
@@ -2346,17 +2371,18 @@
 

 static void* ril_process_thread(void* arg)

 {
-    UNUSED(arg);
+    UNUSED(arg);

+    ATPortType_enum *port = (ATPortType_enum*)arg;

     ril_msg_queue_info_t* item = NULL;

 
-    pthread_mutex_lock(&ril_info.msg_mutex);

+    pthread_mutex_lock(&(ril_info.msg_mutex[*port]));

     while(TRUE)
     {
-        if(mbtk_queue_empty(&ril_info.msg_queue))

+        if(mbtk_queue_empty(&(ril_info.msg_queue[*port])))

         {
-            LOG("Packet process wait...");
-            pthread_cond_wait(&ril_info.msg_cond, &ril_info.msg_mutex);

-            LOG("Packet process continue...");
+            LOG("[Port - %d]Packet process wait...", *port);

+            pthread_cond_wait(&(ril_info.msg_cond[*port]), &(ril_info.msg_mutex[*port]));

+            LOG("[Port - %d]Packet process continue...", *port);

         }
         else
         {
@@ -2365,18 +2391,18 @@
 
         // Process all information request.
         mbtk_ril_err_enum err;

-        while((item = (ril_msg_queue_info_t*)mbtk_queue_get(&ril_info.msg_queue)) != NULL)

+        while((item = (ril_msg_queue_info_t*)mbtk_queue_get(&(ril_info.msg_queue[*port]))) != NULL)

         {
             if(item->cli_info) { // REQ form client.
                 ril_msg_pack_info_t *pack = (ril_msg_pack_info_t*)item->pack;

                 LOGD("Process REQ %s.", id2str(pack->msg_id));

-                ril_info.at_process = true;

+                ril_info.at_process[*port] = true;

                 err = pack_req_process(item->cli_info, pack);
                 if(err != MBTK_RIL_ERR_SUCCESS)

                 {
-                    ril_error_pack_send(item->cli_info->fd, pack->msg_id, pack->msg_index, err);

+                    ril_error_pack_send(item->cli_info->port, item->cli_info->fd, pack->msg_id, pack->msg_index, err);

                 }
-                ril_info.at_process = false;

+                ril_info.at_process[*port] = false;

                 ril_msg_pack_free(pack);

                 free(item);
             } else { // REQ from myself.

@@ -2391,7 +2417,10 @@
             }
         }
     }
-    pthread_mutex_unlock(&ril_info.msg_mutex);

+    pthread_mutex_unlock(&(ril_info.msg_mutex[*port]));

+

+    free(port);

+

     return NULL;
 }

 

@@ -2521,9 +2550,9 @@
         goto error;
     }

 

-    mbtk_queue_init(&ril_info.msg_queue);

-    pthread_mutex_init(&ril_info.msg_mutex, NULL);

-    pthread_cond_init(&ril_info.msg_cond, NULL);

+    mbtk_queue_init(&(ril_info.msg_queue[ATPORTTYPE_0]));

+    pthread_mutex_init(&(ril_info.msg_mutex[ATPORTTYPE_0]), NULL);

+    pthread_cond_init(&(ril_info.msg_cond[ATPORTTYPE_0]), NULL);

 
     pthread_t info_pid, pack_pid/*, monitor_pid, urc_pid, bootconn_pid*/;

     pthread_attr_t thread_attr;
@@ -2539,12 +2568,22 @@
         LOGE("pthread_create() fail.");

         goto error;
     }
-
-    if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, NULL))

+

+    ATPortType_enum *port_0 = (ATPortType_enum*)malloc(sizeof(ATPortType_enum));

+    *port_0 = ATPORTTYPE_0;

+    if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, port_0))

     {
         LOGE("pthread_create() fail.");

         goto error;
-    }
+    }

+

+    ATPortType_enum *port_1 = (ATPortType_enum*)malloc(sizeof(ATPortType_enum));

+    *port_1 = ATPORTTYPE_1;

+    if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, port_1))

+    {
+        LOGE("pthread_create() fail.");

+        goto error;
+    }

 
     // Set Band
     // AT*BAND=15,78,147,482,134742231
@@ -2617,16 +2656,29 @@
         return -1;

     }

 

-    at_set_on_reader_closed(onATReaderClosed);

-    at_set_on_timeout(onATTimeout);

-

-    if(at_open(at_sock, uart_sock, onUnsolicited))

+    int at_sock_1 = openSocket("/tmp/atcmd_at_1");

+    if(at_sock_1 < 0)

     {

-        LOGE("Start AT thread fail.");

+        LOGE("Open AT Socket Fail[%d].", errno);

         return -1;

     }

 

-    if(at_handshake())

+    at_set_on_reader_closed(onATReaderClosed);

+    at_set_on_timeout(onATTimeout);

+

+    if(at_open(ATPORTTYPE_0, at_sock, uart_sock, onUnsolicited))

+    {

+        LOGE("Start AT_0 thread fail.");

+        return -1;

+    }

+

+    if(at_open(ATPORTTYPE_1, at_sock_1, uart_sock, onUnsolicited))

+    {

+        LOGE("Start AT_1 thread fail.");

+        return -1;

+    }

+

+    if(at_handshake(ATPORTTYPE_0))

     {

         LOGE("AT handshake fail.");

         return -1;