Fix auto data call for ril v2

Change-Id: I753b1c8ac7bcbeabd316734b54b4e79224537dea
diff --git a/mbtk/mbtk_rild_v2/src/main.c b/mbtk/mbtk_rild_v2/src/main.c
index 34d5656..0068bd3 100755
--- a/mbtk/mbtk_rild_v2/src/main.c
+++ b/mbtk/mbtk_rild_v2/src/main.c
@@ -1,3 +1,25 @@
+/*

+*    main.c

+*

+*    mbtk_rild main file.

+*

+* The main functions are as follows:

+* 1. Execute boot script /etc/init.d/mbtk_boot_server_ready after AT is ready(Only execute one time).

+* 2. Execute boot script /etc/init.d/mbtk_boot_net_ready after network is ready(Only execute one time).

+* 3. Set the frequency band based on the relevant information in the "device_info" partition.

+* 4. Create socket server(The client implementation in API).

+* 5. Create 3 connections between atcmdsrv, related devices: /tmp/atcmd_at ,

+*                           /tmp/atcmd_at_1 , /tmp/atcmd_at_2

+*/

+/******************************************************************************

+

+                          EDIT HISTORY FOR FILE

+

+  WHEN        WHO       WHAT,WHERE,WHY

+--------    --------    -------------------------------------------------------

+2024/08/13     LiuBin      Initial version

+

+******************************************************************************/

 #include <stdio.h>

 #include <stdlib.h>

 #include <unistd.h>

@@ -31,6 +53,7 @@
 #include "mbtk_ril.h"

 #include "mbtk_str.h"

 #include "mbtk_queue.h"

+#include "mbtk_file.h"

 

 #ifndef TEMP_FAILURE_RETRY

 #define TEMP_FAILURE_RETRY(exp) ({         \

@@ -57,6 +80,7 @@
 static mbtk_ril_call_state_info_t call_list[RIL_CALL_NUM_MAX];

 extern mbtk_cell_pack_info_t cell_info;

 extern ril_cgact_wait_t cgact_wait;

+extern int ril_cid_start;

 

 // int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len);

 // int mbtk_signal_log(char *data);

@@ -80,6 +104,8 @@
 {

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

     at_close(ATPORTTYPE_0);

+    at_close(ATPORTTYPE_1);

+    at_close(ATPORTTYPE_2);

 }

 

 /* Called on command or reader thread */

@@ -87,6 +113,8 @@
 {

     LOGI("AT channel closed\n");

     at_close(ATPORTTYPE_0);

+    at_close(ATPORTTYPE_1);

+    at_close(ATPORTTYPE_2);

 }

 

 static void sock_cli_free_func(void *data)
@@ -99,6 +127,23 @@
     }
 }

 

+bool asr_auto_data_call_enable()

+{

+    /*

+    uci show wan_default.default.enable

+    wan_default.default.enable='1'

+

+    uci get wan_default.default.enable

+    1

+    */

+    char buff[128] = {0};

+    if(mbtk_cmd_line("uci get wan_default.default.enable", buff, sizeof(buff)) && strlen(buff) > 0) {

+        return buff[0] == '1' ? TRUE : FALSE;

+    } else {

+        return FALSE;

+    }

+}

+

 /*

 * Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.

 */

@@ -294,7 +339,7 @@
     }
 }

 

-static void ril_state_change(ril_msg_id_enum msg_id, const void *data, int data_len)

+void ril_state_change(ril_msg_id_enum msg_id, const void *data, int data_len)

 {
     sock_cli_info_t *cli = NULL;

     list_first(ril_info.sock_client_list);

@@ -316,7 +361,16 @@
 {

     // Send urc msg to client.

     if(msg_id > RIL_MSG_ID_IND_BEGIN && msg_id < RIL_MSG_ID_IND_END) {

-        ril_state_change(msg_id, data, data_len);

+        if(msg_id == RIL_MSG_ID_IND_PDP_STATE_CHANGE) {

+            mbtk_ril_pdp_state_info_t *info = (mbtk_ril_pdp_state_info_t*)data;

+            if(info->action && !info->ip_info_valid) {

+                LOGD("PDP action but no IP information, not send.");

+            } else {

+                ril_state_change(msg_id, data, data_len);

+            }

+        } else {

+            ril_state_change(msg_id, data, data_len);

+        }

     }

 

     // Async process urc msg.

@@ -350,7 +404,7 @@
     memset(&state, 0, sizeof(mbtk_ril_sim_state_info_t));

     state.sim_type = MBTK_UNKNOWN;

 

-    char* tmp_s = memdup(s,strlen(s));

+    char* tmp_s = memdup(s,strlen(s) + 1);

     char *line = tmp_s;

     int tmp_int;

     char *tmp_str;

@@ -459,7 +513,7 @@
         state.sim_type = ril_info.sim_type;

         ril_info.sim_state = state.sim_state;

 

-        urc_msg_distribute(false, RIL_MSG_ID_IND_SIM_STATE_CHANGE, &state, sizeof(mbtk_ril_sim_state_info_t));

+        urc_msg_distribute(true, RIL_MSG_ID_IND_SIM_STATE_CHANGE, &state, sizeof(mbtk_ril_sim_state_info_t));

     } else if(strStartsWith(s, "*EUICC:")){

         if (at_tok_start(&line) < 0)

         {

@@ -483,7 +537,7 @@
 // +CPAS: 4

 static void urc_call_state_change_process(const char *s, const char *sms_pdu)

 {

-    char* tmp_s = memdup(s,strlen(s));

+    char* tmp_s = memdup(s,strlen(s) + 1);

     char *line = tmp_s;

     int tmp_int;

     char *tmp_str;

@@ -598,7 +652,7 @@
     mbtk_ril_ecall_state_info_t ecall_state;

     memset(&ecall_state, 0, sizeof(mbtk_ril_ecall_state_info_t));

 

-    char* tmp_s = memdup(s,strlen(s));

+    char* tmp_s = memdup(s,strlen(s) + 1);

     char *line = tmp_s;

     int tmp_int;

     char *tmp_str;

@@ -653,7 +707,7 @@
         state.type = MBTK_NET_REG_TYPE_DATA_LTE;

     }

 

-    char* tmp_s = memdup(s,strlen(s));

+    char* tmp_s = memdup(s,strlen(s) + 1);

     char *line = tmp_s;

     int tmp_int;

     char *tmp_str;

@@ -672,14 +726,17 @@
         {

             goto CGREG_EXIT;

         }

+

         if (at_tok_nextstr(&line, &tmp_str) < 0)

         {

             goto CGREG_EXIT;

         }

+

         if (at_tok_nextint(&line, &tmp_int) < 0)

         {

             goto CGREG_EXIT;

         }

+

         state.tech = (mbtk_radio_technology_enum)tmp_int; // AcT

     }

 

@@ -1025,7 +1082,7 @@
         if(cell_info.running) {

             int tmp_int;

             int i = 0;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1122,7 +1179,7 @@
         // phyCellId,euArfcn,rsrp,rsrq

         if(cell_info.running) {

             int tmp_int;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1212,7 +1269,7 @@
         if(cell_info.running) {

             int tmp_int;

             int i = 0;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1296,7 +1353,7 @@
         if(cell_info.running) {

             int tmp_int;

             int i = 0;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1349,7 +1406,7 @@
         if(cell_info.running) {

             int tmp_int;

             int i = 0;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1425,7 +1482,7 @@
         if(cell_info.running) {

             int tmp_int;

             int i = 0;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1538,7 +1595,7 @@
         if(cell_info.running) {

             int tmp_int;

             int i = 0;

-            char* tmp_s = memdup(s,strlen(s));

+            char* tmp_s = memdup(s,strlen(s) + 1);

             char* free_ptr = tmp_s;

             char *line = tmp_s;

             if (at_tok_start(&line) < 0)

@@ -1637,7 +1694,7 @@
 

 static void onUnsolicited(const char *s, const char *sms_pdu)

 {

-    LOGD("URC : %s", s);

+    LOGV("URC : %s", s);

     // MBTK_AT_READY

     if (strStartsWith(s, "MBTK_AT_READY")) // AT ready.

     {

@@ -2207,6 +2264,17 @@
             LOGD("Radio state : %d", state->radio_state);

             break;
         }

+        case RIL_MSG_ID_IND_SIM_STATE_CHANGE:

+        {

+            mbtk_ril_sim_state_info_t *state = (mbtk_ril_sim_state_info_t*)msg->data;

+            if(state->sim_state == MBTK_SIM_STATE_READY) {

+                LOGD("SIM READY!");

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

+

+                // Set APN from prop.

+                apn_auto_conf_from_prop(ATPORTTYPE_0);

+            }

+        }

         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;

@@ -2380,9 +2448,9 @@
     {
         if(mbtk_queue_empty(&(ril_info.msg_queue[*port])))

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

+            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);

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

         }
         else
         {
@@ -2584,15 +2652,15 @@
         LOGE("pthread_create() fail.");

         goto error;
     }

-
+

     ATPortType_enum *port_2 = (ATPortType_enum*)malloc(sizeof(ATPortType_enum));

     *port_2 = ATPORTTYPE_2;

     if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, port_2))

-    {

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

-        goto error;

+        goto error;
     }

-

+
     // Set Band
     // AT*BAND=15,78,147,482,134742231
     char buff[10];
@@ -2606,9 +2674,30 @@
         }
     }
 
-    pthread_attr_destroy(&thread_attr);
+    pthread_attr_destroy(&thread_attr);

+

+    if(asr_auto_data_call_enable()) {

+        ril_cid_start = MBTK_RIL_CID_2;

+

+        char def_cid[10] = {0};

+        char prop_data[100] = {0};

+        int cid = -1;

+        sprintf(def_cid, "%d", MBTK_RIL_CID_DEF);   // Default route and DNS is CID 1.

+        memset(prop_data, 0, sizeof(prop_data));

+        if(property_get(MBTK_DEF_DNS_CID, prop_data, def_cid) > 0 && !str_empty(prop_data)) {

+            cid = atoi(prop_data);

+        }

+

+        if(cid == MBTK_RIL_CID_DEF) {

+            if(file_link("/tmp/resolv.conf", "/etc/resolv.conf")) {

+                LOGE("Create link file fail.");

+            }

+        }

+    } else {

+        ril_cid_start = MBTK_RIL_CID_DEF;

+    }

 
-    LOGD("MBTK Ril Server Start...");

+    LOGD("MBTK Ril Server Start[CID start with : %d]...", ril_cid_start);

 
     return 0;
 error: