[Bugfix][T106BUG-408]Add parameter "handle" for apn_xml

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

Change-Id: Ifedabc4bc9ac0069a7eb7aeb86e21249b4b74545
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp
index 489bf64..538584b 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq-qser-data.cpp
@@ -386,11 +386,210 @@
     return ;
 }
 
+int apn_xml_handle_get_profile(int handle, unsigned char *profile_idx)
+{
+    //xmlDocPtr apn_table_xml_pdoc = NULL;
+    xmlNodePtr node = NULL;
+    xmlNodePtr modify_node = NULL;
+    unsigned char temp = NULL;
+    //apn_table_xml_pdoc = xmlReadFile(data_xml_path ,"UTF-8",XML_PARSE_RECOVER);
+    if(NULL == apn_table_xml_pdoc)
+    {
+        LYERRLOG("open xml file error");
+        goto FAILED;
+    }
+    
+    node = xmlDocGetRootElement(apn_table_xml_pdoc);
+    if (NULL == node)
+    {
+        LYERRLOG("xmlDocGetRootElement() error");
+        goto FAILED;
+    }
+    modify_node = node->xmlChildrenNode;
+    if(modify_node != NULL)
+    {
+        modify_node = modify_node->next;
+    }
+    else
+    {
+        LYERRLOG("modify_node is null\n");
+        goto FAILED;
+    }
+    for (;;)
+    {
+        if(modify_node == NULL)
+        {
+            LYERRLOG("modify_node is null\n");
+            goto FAILED;
+        }
+        if (xmlGetProp(modify_node, "handle") == NULL)  //Null Node
+        {
+            modify_node = modify_node->next;
+            continue;
+        }
+        else if((int)atoi(xmlGetProp(modify_node, "handle")) == handle)
+        {
+            break;
+        }
+        modify_node = modify_node->next;
+    }
+    if(modify_node == NULL)
+    {
+        LYERRLOG("modify_node is null\n");
+        goto FAILED;
+    }
+    *profile_idx = (unsigned char)atoi(xmlGetProp(modify_node, "profile_idx"));
+    //xmlFreeDoc(apn_table_xml_pdoc);
+    return RESULT_OK;
+
+    FAILED:
+    // if (apn_table_xml_pdoc)
+    // {
+    //     xmlFreeDoc(apn_table_xml_pdoc);
+    // }
+    return RESULT_ERROR;
+}
+int apn_xml_handle_set(unsigned char profile_idx, int handle)
+{
+    int node_num = 0;
+    char temp_buff[12];
+    //xmlDocPtr apn_table_xml_pdoc = NULL;
+    xmlNodePtr node = NULL;
+    xmlNodePtr modify_node = NULL;
+    xmlNodePtr check_node = NULL;
+    //apn_table_xml_pdoc = xmlReadFile(data_xml_path ,"UTF-8",XML_PARSE_RECOVER);
+    if(NULL == apn_table_xml_pdoc)
+    {
+        LYERRLOG("open xml file error");
+        goto FAILED;
+    }
+    node = xmlDocGetRootElement(apn_table_xml_pdoc);
+    if (NULL == node)
+    {
+        LYERRLOG("xmlDocGetRootElement() error");
+        goto FAILED;
+    }
+    modify_node = node->xmlChildrenNode;
+    if(modify_node != NULL)
+    {
+        modify_node = modify_node->next;
+    }
+    else
+    {
+        LYERRLOG("modify_node is null\n");
+        goto FAILED;
+    }
+    for (;;)
+    {
+        if(modify_node == NULL)
+        {
+            LYERRLOG("modify_node is null\n");
+            goto FAILED;
+        }
+        if (xmlGetProp(modify_node, "profile_idx") == NULL)  //Null Node
+        {
+            modify_node = modify_node->next;
+            node_num--;
+            continue;
+        }
+        else if((int)atoi(xmlGetProp(modify_node, "profile_idx")) == (int)profile_idx)
+        {
+            break;
+        }
+        modify_node = modify_node->next;
+    }
+    if(modify_node == NULL)
+    {
+        LYERRLOG("modify_node is null\n");
+        goto FAILED;
+    }
+    bzero(temp_buff,12);
+    snprintf(temp_buff,sizeof(temp_buff),"%d",handle);
+    xmlSetProp(modify_node,BAD_CAST"handle",(xmlChar *)temp_buff);
+    xmlSaveFormatFileEnc(data_xml_path, apn_table_xml_pdoc, "UTF-8", 1);
+    //xmlFreeDoc(apn_table_xml_pdoc);
+    return RESULT_OK;
+
+    FAILED:
+    // if (apn_table_xml_pdoc)
+    // {
+    //     xmlFreeDoc(apn_table_xml_pdoc);
+    // }
+    return RESULT_ERROR;
+}
+int apn_xml_handle_get(unsigned char profile_idx, int *handle)
+{
+    int node_num = 0;
+    //xmlDocPtr apn_table_xml_pdoc = NULL;
+    xmlNodePtr node = NULL;
+    xmlNodePtr modify_node = NULL;
+    unsigned char temp = NULL;
+    //apn_table_xml_pdoc = xmlReadFile(data_xml_path ,"UTF-8",XML_PARSE_RECOVER);
+    if(NULL == apn_table_xml_pdoc)
+    {
+        LYERRLOG("open xml file error");
+        goto FAILED;
+    }
+    
+    node = xmlDocGetRootElement(apn_table_xml_pdoc);
+    if (NULL == node)
+    {
+        LYERRLOG("xmlDocGetRootElement() error");
+        goto FAILED;
+    }
+    modify_node = node->xmlChildrenNode;
+    if(modify_node != NULL)
+    {
+        modify_node = modify_node->next;
+    }
+    else
+    {
+        LYERRLOG("modify_node is null\n");
+        goto FAILED;
+    }
+    LYINFLOG("profile_idx is %d\n", (int)profile_idx);
+    for (;;)
+    {
+        if(modify_node == NULL)
+        {
+            LYERRLOG("modify_node is null\n");
+            goto FAILED;
+        }
+        if (xmlGetProp(modify_node, "profile_idx") == NULL)  //Null Node
+        {
+            modify_node = modify_node->next;
+            node_num--;
+            continue;
+        }
+        else if((int)atoi(xmlGetProp(modify_node, "profile_idx")) == (int)profile_idx)
+        {
+            break;
+        }
+        modify_node = modify_node->next;
+    }
+    if(modify_node == NULL)
+    {
+        LYERRLOG("modify_node is null\n");
+        goto FAILED;
+    }
+    *handle = (int)atoi(xmlGetProp(modify_node, "handle"));
+    xmlSaveFormatFileEnc(data_xml_path, apn_table_xml_pdoc, "UTF-8", 1);
+    //xmlFreeDoc(apn_table_xml_pdoc);
+    return RESULT_OK;
+
+    FAILED:
+    // if (apn_table_xml_pdoc)
+    // {
+    //     xmlFreeDoc(apn_table_xml_pdoc);
+    // }
+    return RESULT_ERROR;
+}
 
 int apn_xml_add(qser_apn_add_s *apn,unsigned char *apn_num)
 {
     int node_num = 0;
     char temp_buff[12];
+    int default_handle = LYNQ_APN_CHANNEL_MAX + 1;
     //xmlDocPtr apn_table_xml_pdoc = NULL;
     xmlNodePtr node = NULL;
     xmlNodePtr tmp_node = NULL;
@@ -447,6 +646,9 @@
     xmlNewProp(tmp_node,BAD_CAST"username",(xmlChar *)apn->username);
     xmlNewProp(tmp_node,BAD_CAST"password",(xmlChar *)apn->password);
     xmlNewProp(tmp_node,BAD_CAST"apn_type",(xmlChar *)apn->apn_type);
+    bzero(temp_buff,12);
+    snprintf(temp_buff,sizeof(temp_buff),"%d",default_handle);
+    xmlNewProp(tmp_node,BAD_CAST"handle",(xmlChar *)temp_buff);
     xmlAddChild(node,tmp_node);
     xmlSaveFormatFileEnc(data_xml_path, apn_table_xml_pdoc, "UTF-8", 1);
    
@@ -857,6 +1059,7 @@
 {
     int handle = -1;
     int ret = 0;
+    int default_handle = LYNQ_APN_CHANNEL_MAX + 1;
     lynq_data_call_response_v11_t data_urc_info = {0};
     qser_data_call_state_s data_cb_state;
     while (s_qser_data_cb_thread_status)
@@ -876,7 +1079,8 @@
         memset(&data_cb_state, 0, sizeof(data_cb_state));
         lynq_get_data_call_list(&handle,&data_urc_info);
         /*compare paramter*/
-        data_cb_state.profile_idx = (char)handle;
+        //data_cb_state.profile_idx = (char)handle;
+        apn_xml_handle_get_profile(handle, &data_cb_state.profile_idx);
         LYINFLOG("[thread_wait_cb_status]: status=%d, suggestedRetryTime=%d, cid=%d, active=%d, type=%s, ifname=%s, addresses=%s, dnses=%s, gateways=%s, pcscf=%s, mtu=%d\n",
         data_urc_info.status, data_urc_info.suggestedRetryTime, data_urc_info.cid, data_urc_info.active, 
         data_urc_info.type, data_urc_info.ifname, data_urc_info.addresses, data_urc_info.dnses, data_urc_info.gateways, data_urc_info.pcscf, 
@@ -906,6 +1110,11 @@
         else
         {
             data_cb_state.state = QSER_DATA_CALL_DISCONNECTED;
+            ret = apn_xml_handle_set(data_cb_state.profile_idx, default_handle);
+            if(ret != 0)
+            {
+                LYERRLOG("handle set error");
+            }
         }
         if (data_cb_state.ip_family == QSER_DATA_CALL_TYPE_IPV4)
         {
@@ -1031,6 +1240,7 @@
 int qser_data_call_start(qser_data_call_s *data_call, qser_data_call_error_e *err)
 {
     int ret = -1;
+    int error = -1;
     int handle = 0;
     if(g_lynq_qser_data_init_flag == 0)
     {
@@ -1071,12 +1281,21 @@
     {
         *err = QSER_DATA_CALL_ERROR_INVALID_PARAMS;
     }
+    else
+    {
+        error = apn_xml_handle_set(data_call->profile_idx, handle);
+        if(error != 0)
+        {
+            LYERRLOG("handle set error");
+        }
+    }
     return ret;
 }
 
 int qser_data_call_start_async(qser_data_call_s *data_call, qser_data_call_error_e *err)
 {
     int ret = -1;
+    int error = -1;
     int handle = 0;
     if(g_lynq_qser_data_init_flag == 0)
     {
@@ -1117,6 +1336,14 @@
     {
         *err = QSER_DATA_CALL_ERROR_INVALID_PARAMS;
     }
+    else
+    {
+        error = apn_xml_handle_set(data_call->profile_idx, handle);
+        if(error != 0)
+        {
+            LYERRLOG("handle set error");
+        }
+    }
     return ret;
 }
 
@@ -1214,6 +1441,7 @@
 int qser_apn_set(qser_apn_info_s *apn)
 {
     int ret = 0;
+    int handle = -1;
     if(g_lynq_qser_data_init_flag == 0)
     {
         return LYNQ_E_NO_INIT;
@@ -1223,6 +1451,16 @@
         LYERRLOG("apn set incoming paramters error");
         return RESULT_ERROR;
     }
+    ret = apn_xml_handle_get(apn->profile_idx, &handle);
+    if(ret != 0)
+    {
+        LYERRLOG("handle set error");
+    }
+    if(handle >= 0 && handle < LYNQ_APN_CHANNEL_MAX)
+    {
+        LYERRLOG("It has setup datacall");
+        return RESULT_ERROR;
+    }
     ret = apn_xml_modify(apn);
     if (ret < 0)
     {
@@ -1321,6 +1559,17 @@
         return RESULT_OK;
     }
     int ret = 0;
+    int handle = -1;
+    ret = apn_xml_handle_get(profile_idx, &handle);
+    if(ret != 0)
+    {
+        LYERRLOG("handle set error");
+    }
+    if(handle >= 0 && handle < LYNQ_APN_CHANNEL_MAX)
+    {
+        LYERRLOG("It has setup datacall");
+        return RESULT_ERROR;
+    }
     ret = apn_xml_delete(profile_idx);
     if (ret < 0)
     {
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq_qser_data_apn.xml b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq_qser_data_apn.xml
index c9c8304..d881dc0 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq_qser_data_apn.xml
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq_qser_data_apn.xml
@@ -1,3 +1,3 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <lynq_qser_data_apn>
-  <apn profile_idx="0" pdp_type="3" auth_proto="0" apn_name="default" username="NULL" password="NULL" apn_type="default"/></lynq_qser_data_apn>
+  <apn profile_idx="0" pdp_type="3" auth_proto="0" apn_name="default" username="NULL" password="NULL" apn_type="default" handle="11"/></lynq_qser_data_apn>