[Feature][ZXW-224][T106BUG-128]Change to database storage apntable

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: I0d0bb495a5a159d84a046e0c7c813331bdf3278e
diff --git a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-data/liblynq-qser-data.bb b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-data/liblynq-qser-data.bb
index bc83716..32555cc 100644
--- a/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-data/liblynq-qser-data.bb
+++ b/cap/zx297520v3/sources/meta-zxic-custom/recipes-lynq/liblynq-qser-data/liblynq-qser-data.bb
@@ -3,7 +3,7 @@
 DESCRIPTION = "liblynq-qser-data"
 LICENSE = "CLOSED"
 LIC_FILES_CHKSUM = "file://LICENSE;md5=e1696b147d49d491bcb4da1a57173fff"
-DEPENDS += "liblynq-log liblynq-data libxml2"
+DEPENDS += "liblynq-log liblynq-data"
 #inherit workonsrc
 WORKONSRC = "${TOPDIR}/../src/lynq/lib/liblynq-qser-data/"
 FILESEXTRAPATHS_prepend :="${TOPDIR}/../src/lynq/lib/:"
@@ -47,8 +47,6 @@
         install -d ${D}${includedir}/
         cp -af ${SRC-DIR}/include/ ${D}${includedir}/
     fi 
-    install -d ${D}/data/
-    cp -R ${WORKONSRC}/lynq_qser_data_apn.xml ${D}/data/
 }
 
 addtask bachclean
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 a4e452e..94af56b 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
@@ -9,17 +9,20 @@
 #include <libdata/lynq_data.h>
 #include <liblog/lynq_deflog.h>
 #include <pthread.h>
-#include <libxml/tree.h>
-#include <libxml/parser.h>
+#include <sqlite3.h>
 #include "lynq-qser-data.h"
 
 #define USER_LOG_TAG "LYNQ_QSER_DATA"
-
+#define APN_DB_PATH "/mnt/userdata/apn.db"
+#define APN_DB_READED 0
+#define APN_DB_ERR 1
+#define APN_DB_CREATE 2
 #define RESULT_OK (0)
 #define RESULT_ERROR (-1)
 
 typedef enum{
-    LYNQ_E_NO_INIT=9002
+    LYNQ_E_NO_INIT=9002,
+    LYNQ_E_APN_DB_FAIL=9003
 }LYNQ_E;
 
 static int g_lynq_qser_data_init_flag = 0;
@@ -28,13 +31,38 @@
 static pthread_mutex_t s_qser_data_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t s_qser_data_cb_cond = PTHREAD_COND_INITIALIZER;
 
-#define data_xml_path "/mnt/userdata/lynq_qser_data_apn.xml"
-#define COPY_XML_RETRY_TIMES 3
 
 static qser_data_call_evt_cb_t s_data_call_cb = NULL;
-static xmlDocPtr apn_table_xml_pdoc = NULL;
+static sqlite3* apnDb;
+qser_apn_add_s default_apn = {QSER_APN_PDP_TYPE_IPV4V6, QSER_APN_AUTH_PROTO_DEFAULT, "default", "NULL", "NULL", "default"};
+int apn_indexs[LYNQ_APN_CHANNEL_MAX]={0};
+int next_index = 0;
+int apn_count = 0;
 const int apndb_offset = 683;
 
+static int init_apn_db();
+static int create_apn_db(char *path);
+static int create_apn_table(char*path);
+static int has_apn_db_created(char *path,sqlite3* apnDb_l);
+static int apn_db_handle_clean();
+
+static int lynq_apn_callback(void *data, int argc, char **argv, char **azColName);
+static int lynq_check_index(unsigned char index);
+static int apn_db_handle_get_profile_cb(void *data, int argc, char **argv, char **azColName);
+static int apn_db_handle_get_profile(int handle, unsigned char *profile_idx);
+static int apn_db_handle_set(unsigned char profile_idx, int handle);
+static int apn_db_handle_get_cb(void *data, int argc, char **argv, char **azColName);
+static int apn_db_handle_get(unsigned char profile_idx, int *handle);
+
+static int find_unuse_apn_index(char *path);
+static int apn_db_add(int profile_id, qser_apn_add_s *apn);
+static int apn_db_delete(unsigned char profile_idx);
+static int apn_db_modify(qser_apn_info_s *apn);
+static int lynq_apn_db_query_cb(void *data, int argc, char **argv, char **azColName);
+static int apn_db_query(unsigned char profile_idx,qser_apn_info_s *apn);
+static int apn_db_query_list_cb(void *data, int argc, char **argv, char **azColName);
+static int apn_db_query_list(qser_apn_info_list_s *apn_list);
+
 void lynq_ipv4_aton_urc(lynq_data_call_response_v11_t *libdata,qser_data_call_state_s *data_res)
 {
 #ifdef MOBILETEK_TARGET_PLATFORM_T106
@@ -404,645 +432,595 @@
     return ;
 }
 
-int apn_xml_handle_get_profile(int handle, unsigned char *profile_idx)
+// ****************apn DB start******************
+static int apn_db_handle_get_profile_cb(void *data, int argc, char **argv, char **azColName)
 {
-    //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)
+    if(data==NULL)
     {
-        LYERRLOG("open xml file error");
-        goto FAILED;
+        return 1;
     }
-    
-    node = xmlDocGetRootElement(apn_table_xml_pdoc);
-    if (NULL == node)
-    {
-        LYERRLOG("xmlDocGetRootElement() error");
-        goto FAILED;
+    int *temp = (int *)data;
+    for(int i=0; i<argc; i++){
+       LYINFLOG("[%s]%s = %s", __FUNCTION__,azColName[i], argv[i] ? argv[i] : "NULL");
     }
-    modify_node = node->xmlChildrenNode;
-    if(modify_node != NULL)
+    *temp = atoi(argv[0]);
+    return 0;
+}
+static int apn_db_handle_get_profile(int handle, unsigned char *profile_idx)
+{
+    char *zErrMsg = 0;
+    int rc;
+    int idx = 0;
+
+    /* Open database */
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
     {
-        modify_node = modify_node->next;
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    char *sql = sqlite3_mprintf("SELECT ID from LYNQAPN WHERE HANDLE=%d", handle);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, apn_db_handle_get_profile_cb, &idx, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    };
+    *profile_idx = (unsigned char)idx;
+    LYINFLOG("get idx %d successfully\n", *profile_idx);
+    sqlite3_close(apnDb);
+    return 0;
+}
+static int lynq_apn_callback(void *data, int argc, char **argv, char **azColName){
+    int i;
+
+    apn_indexs[atoi(argv[0])] = atoi(argv[0]);
+    LYINFLOG("apn_indexs[%d] is %d", atoi(argv[0]), apn_indexs[atoi(argv[0])]);
+
+    for(i=0; i<argc; i++){
+       LYINFLOG("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    return 0;
+}
+/**
+ * @brief Check whether the ID exists in the database
+ * 
+ * @param index 
+ * @return int 
+ * find:turn 1 
+ * not find:turn 0
+ */
+static int lynq_check_index(unsigned char index)
+{
+    memset(apn_indexs,0,sizeof(apn_indexs));
+    next_index = 0;
+    char *zErrMsg = 0;
+    int rc;
+    char sql[128]={0};
+    /* Open database */
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    sprintf(sql,"%s", "SELECT * from LYNQAPN");
+    rc = sqlite3_exec(apnDb, sql, lynq_apn_callback, NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    //for(int cnt = 0; cnt < next_index;cnt++)
+    for(int cnt = 0; cnt <= QSER_APN_MAX_LIST; cnt++)
+    {
+        if(index == apn_indexs[cnt])
+        {
+            LYINFLOG("index = apn_index\n");
+            sqlite3_close(apnDb);
+            return 1;
+        }
+    }
+    LYINFLOG("cant find the index %d\n", index);
+    sqlite3_close(apnDb);
+    return 0;
+}
+
+static int apn_db_handle_set(unsigned char profile_idx, int handle)
+{
+    char *zErrMsg = 0;
+    int rc;
+    //char sql[128]={0};
+    if(!lynq_check_index(profile_idx))
+    {
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    /* Open database */
+
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    //sql = "SELECT * from LYNQAPN";
+    char *sql = sqlite3_mprintf("UPDATE LYNQAPN SET HANDLE=%d WHERE ID=%d", handle, (int)profile_idx);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, NULL, NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    LYINFLOG("update apn %d successfully\n", profile_idx);
+    sqlite3_close(apnDb);
+    return 0;
+}
+static int apn_db_handle_get_cb(void *data, int argc, char **argv, char **azColName)
+{
+    if(data==NULL)
+    {
+        return 1;
+    }
+    int *temp = (int *)data;
+    for(int i=0; i<argc; i++){
+       LYINFLOG("[%s]%s = %s", __FUNCTION__,azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    *temp = atoi(argv[0]);
+    LYINFLOG("The handle is %d\n", *temp);
+    return 0;
+}
+static int apn_db_handle_get(unsigned char profile_idx, int *handle)
+{
+    char *zErrMsg = 0;
+    int rc;
+
+    //char sql[128]={0};
+    if(!lynq_check_index(profile_idx))
+    {
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    /* Open database */
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    char *sql = sqlite3_mprintf("SELECT HANDLE from LYNQAPN WHERE ID=%d", (int)profile_idx);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, apn_db_handle_get_cb, handle, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    LYINFLOG("get idx %d successfully\n", *handle);
+    sqlite3_close(apnDb);
+    return 0;
+}
+
+static int has_apn_db_created(char *path,sqlite3* apnDb_l)
+{
+    if (path == NULL || strlen(path) == 0)
+    {
+        LYERRLOG("initApnDb error with null path!");
+        return APN_DB_ERR;
+    }
+    int rc = sqlite3_open_v2(path, &apnDb_l, SQLITE_OPEN_READWRITE,NULL);
+    if (rc == SQLITE_OK) 
+    {
+        LYINFLOG("check init success!");
+        return APN_DB_READED;
+    } 
+    else 
+    {
+        LYERRLOG("db has not create %s!", sqlite3_errmsg(apnDb_l));
+        sqlite3_close_v2(apnDb_l);
+        return APN_DB_ERR;
+    }
+}
+static int create_apn_db(char *path)
+{
+    if (path == NULL || strlen(path) == 0) {
+        LYERRLOG("initApnDb error with null path!");
+        return APN_DB_ERR;
+    }
+    if(APN_DB_READED==has_apn_db_created(path,apnDb))
+    {
+        return APN_DB_READED;
+    }
+    int rc = sqlite3_open(path, &apnDb);
+    if (rc != SQLITE_OK) {
+        LYERRLOG("initDb failed %d", sqlite3_errcode(apnDb));
+        return APN_DB_ERR;
+    } else {
+        LYINFLOG("create db in %s", path);
+    }
+    sqlite3_close(apnDb);
+    return APN_DB_CREATE;
+}
+static int create_apn_table(char*path)
+{
+    char *zErrMsg = 0;
+    int rc;
+    char *sql;
+
+    /* Open database */
+    rc = sqlite3_open(path, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return 1;
+    }
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    sql = "CREATE TABLE LYNQAPN( \
+         ID               INT                   NOT NULL, \
+         PDP_TYPE         INT                   NOT NULL, \
+         AUTH_PROTO       INT                   NOT NULL, \
+         APN_NAME         TEXT                  NOT NULL, \
+         USERNAME         TEXT                  NOT NULL,\
+         PASSWORD         TEXT                  NOT NULL, \
+         APN_TYPE         TEXT  PRIMARY KEY     NOT NULL, \
+         HANDLE           INT                   NOT NULL)";
+   /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, NULL, 0, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return 1;
+    }
+    LYINFLOG("Table LYNQAPN created successfully\n");
+    sqlite3_close(apnDb);
+    return 0;
+}
+static int init_apn_db()
+{
+    int db_ret=0;
+    int table_ret=0;
+    db_ret = create_apn_db(APN_DB_PATH);
+    if(db_ret==APN_DB_CREATE)
+    {
+        table_ret = create_apn_table(APN_DB_PATH);
+        if(table_ret!=0)
+        {
+            LYERRLOG("create apn table fail!!!");
+            return RESULT_ERROR;
+        }
+        apn_db_add(0, &default_apn);
+    }
+    else if(db_ret==APN_DB_READED)
+    {
+        LYERRLOG("[%s] apn db has be build!!!",__FUNCTION__);
     }
     else
     {
-        LYERRLOG("modify_node is null\n");
-        goto FAILED;
+        LYERRLOG("init apn db fail!!!");
     }
-    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_handle_clean()
+static int apn_db_handle_clean()
 {
-    int node_num = 0;
+    char *zErrMsg = 0;
+    int rc;
     int default_handle = LYNQ_APN_CHANNEL_MAX + 1;
-    xmlNodePtr node = NULL;
-    xmlNodePtr modify_node = NULL;
-    xmlChar *temp_char;
-    char temp_buff[12];
 
-    if(NULL == apn_table_xml_pdoc)
+    /* Open database */
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
     {
-        LYERRLOG("open xml file error");
-        goto FAILED;
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
     }
-    
-    node = xmlDocGetRootElement(apn_table_xml_pdoc);
-    if (NULL == node)
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    char *sql = sqlite3_mprintf("UPDATE LYNQAPN SET HANDLE=%d", default_handle);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, NULL, NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
     {
-        LYERRLOG("xmlDocGetRootElement() error");
-        goto FAILED;
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
     }
-    modify_node = node->xmlChildrenNode;
-    if(modify_node != NULL)
+    LYINFLOG("clean apn table successfully\n");
+    sqlite3_close(apnDb);
+    return RESULT_OK;
+}
+
+static int find_unuse_apn_index(char *path)
+{
+    char *zErrMsg = 0;
+    int rc;
+    int count =1;
+    bool apn_usable = false;
+    char *sql;
+
+    memset(apn_indexs,0,sizeof(apn_indexs));
+    next_index = 0;
+    LYINFLOG("index = %p",apn_indexs);
+    /* Open database */
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
     {
-        modify_node = modify_node->next;
+       LYERRLOG("Can't open database: %s\n", sqlite3_errmsg(apnDb));
+       return -1;
     }
     else
     {
-        LYERRLOG("modify_node is null\n");
-        goto FAILED;
+       LYINFLOG("Opened database successfully\n");
     }
-    while (modify_node != NULL)
-    {
-        temp_char = xmlGetProp(modify_node, "handle");
-        if (temp_char == NULL) 
-        {
-            modify_node = modify_node->next;
-            continue;
-        }
-        bzero(temp_buff,12);
-        snprintf(temp_buff,sizeof(temp_buff),"%d",default_handle);
-        xmlSetProp(modify_node,BAD_CAST"handle",(xmlChar *)temp_buff);
-        modify_node = modify_node->next;
-    }
-    xmlSaveFormatFileEnc(data_xml_path, apn_table_xml_pdoc, "UTF-8", 1);
-    return RESULT_OK;
 
-    FAILED:
-    // if (apn_table_xml_pdoc)
-    // {
-    //     xmlFreeDoc(apn_table_xml_pdoc);
-    // }
-    return RESULT_ERROR;
+    /* Create SQL statement */
+    sql = "SELECT ID from LYNQAPN";
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, lynq_apn_callback,NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        return -1;
+    }
+    else
+    {
+       LYINFLOG("Operation done successfully\n");
+    }
+    //indexs = temp_index;
+    LYINFLOG("index = %p",apn_indexs);
+    for(count;count<=QSER_APN_MAX_LIST;count++)
+    {
+        //if(apn_indexs[count-1]!=count)
+        LYINFLOG("apn_indexs[count] = %d, count = %d\n", apn_indexs[count], count);
+        if(apn_indexs[count]!=count)
+        {
+            //apn_usable=true;
+            break;
+        }
+    }
+    sqlite3_close(apnDb);
+    LYINFLOG("count is %d\n", count);
+    return count;
 }
 
-int apn_xml_add(qser_apn_add_s *apn,unsigned char *apn_num)
+static int apn_db_add(int profile_id, qser_apn_add_s *apn)
 {
-    int node_num = 0;
-    char temp_buff[12];
+    char *zErrMsg = 0;
+    int rc;
     int default_handle = LYNQ_APN_CHANNEL_MAX + 1;
-    //xmlDocPtr apn_table_xml_pdoc = NULL;
-    xmlNodePtr node = NULL;
-    xmlNodePtr tmp_node = NULL;
-    xmlNodePtr sum_node = NULL;
-
-//    apn_table_xml_pdoc = xmlReadFile(data_xml_path ,"UTF-8",XML_PARSE_RECOVER);
-    if(NULL == apn_table_xml_pdoc)
+    /* Open database */
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
     {
-        LYERRLOG("open xml file error");
-        goto FAILED;
+        LYERRLOG("[%s] Can't open database: %s",__FUNCTION__,sqlite3_errmsg(apnDb));
+        return 1;
     }
-    
-    node = xmlDocGetRootElement(apn_table_xml_pdoc);
-    if (NULL == node)
+    LYINFLOG("[%s] Opened database successfully",__FUNCTION__);
+    /* Create SQL statement */
+    /* Execute SQL statement */
+    LYINFLOG("the profile id is %d\n", profile_id);
+    char *sql = sqlite3_mprintf("INSERT INTO LYNQAPN VALUES('%d','%d','%d','%q','%q','%q','%q','%d')", profile_id, apn->pdp_type, apn->auth_proto, apn->apn_name, apn->username, apn->password, apn->apn_type, default_handle);
+    rc = sqlite3_exec(apnDb, sql, NULL, NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
     {
-        LYERRLOG("xmlDocGetRootElement() error");
-        goto FAILED;
+        LYERRLOG( "SQL error: %s", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return 1;
     }
-    sum_node = node->xmlChildrenNode;
-    sum_node = sum_node->next;
-    while (sum_node != NULL)
-    {
-        if (xmlGetProp(sum_node, "profile_idx") == NULL)  //Null Node
-        {
-            sum_node = sum_node->next;
-            continue;
-        }
-        else if(strcmp((char *)xmlGetProp(sum_node, "apn_type"), apn->apn_type) == 0)
-        {
-            LYERRLOG("apntype already exists\n");
-            goto FAILED;
-        }
-        node_num++;
-        sum_node = sum_node->next;
-    }
-    LYINFLOG("apn_num = %d ",node_num);
-    if(node_num >= QSER_APN_MAX_LIST)
-    {
-        LYERRLOG("apn num reached the max");
-        goto FAILED;
-    }
-    tmp_node = xmlNewNode(NULL,BAD_CAST"apn");
-    *apn_num = node_num;
-    bzero(temp_buff,12);
-    snprintf(temp_buff,sizeof(temp_buff),"%d",*apn_num);
-    xmlNewProp(tmp_node,BAD_CAST"profile_idx",(xmlChar *)temp_buff);
-    bzero(temp_buff,12);
-    snprintf(temp_buff,sizeof(temp_buff),"%d",apn->pdp_type);
-    xmlNewProp(tmp_node,BAD_CAST"pdp_type",(xmlChar *)temp_buff);
-    bzero(temp_buff,12);
-    snprintf(temp_buff,sizeof(temp_buff),"%d",apn->auth_proto);
-    xmlNewProp(tmp_node,BAD_CAST"auth_proto",(xmlChar *)temp_buff);
-    xmlNewProp(tmp_node,BAD_CAST"apn_name",(xmlChar *)apn->apn_name);
-    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);
-   
-//    xmlFreeDoc(apn_table_xml_pdoc);
-    return RESULT_OK;
-    
-    FAILED:
-    /* if (apn_table_xml_pdoc)
-    {
-        xmlFreeDoc(apn_table_xml_pdoc);
-    }*/
-    return RESULT_ERROR;
+    LYINFLOG("write apn to apn db successfully");
+    sqlite3_close(apnDb);
+    return 0;
 }
 
-int apn_xml_delete(unsigned char profile_idx)
+static int apn_db_delete(unsigned char profile_idx)
 {
-    int node_num = 0;
-    char temp_buff[12];
-    //xmlDocPtr apn_table_xml_pdoc = NULL;
-    xmlNodePtr node = NULL;
-    xmlNodePtr modify_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 (node_num=0 ;node_num<(int)profile_idx ; node_num++)
-    {
-        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;
-        }
-        modify_node = modify_node->next;
-    }
-    if(modify_node == NULL)
-    {
-        LYERRLOG("modify_node is null\n");
-        goto FAILED;
-    }
-    xmlUnlinkNode(modify_node);
-    xmlFreeNode(modify_node);
-    modify_node = NULL;
-    node_num = 0;
-    modify_node = node->xmlChildrenNode;
-    modify_node = modify_node->next;
-    while (modify_node != NULL)
-    {
-        if (xmlGetProp(modify_node, "profile_idx") == NULL)  //Null Node
-        {
-            modify_node = modify_node->next;
-            continue;
-        }
-        bzero(temp_buff,12);
-        snprintf(temp_buff,sizeof(temp_buff),"%d",node_num);
-        xmlSetProp(modify_node,BAD_CAST"profile_idx",(xmlChar *)temp_buff);
-        modify_node = modify_node->next;
-        node_num++;
-    }
-    xmlSaveFormatFileEnc(data_xml_path, apn_table_xml_pdoc, "UTF-8", 1);
-//    xmlFreeDoc(apn_table_xml_pdoc);
-    return RESULT_OK;
+    char *zErrMsg = 0;
+    int rc;
 
-    FAILED:
-    // if (apn_table_xml_pdoc)
-    // {
-    //     xmlFreeDoc(apn_table_xml_pdoc);
-    // }
-    return RESULT_ERROR;
+    if(!lynq_check_index(profile_idx))
+    {
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    /* Open database */
+
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    //sql = "SELECT * from LYNQAPN";
+    char *sql = sqlite3_mprintf("DELETE  from LYNQAPN WHERE ID=%d",(int)profile_idx);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, NULL, NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    LYINFLOG("delete apn %d successfully\n", profile_idx);
+    sqlite3_close(apnDb);
+    return 0;
 }
 
-int apn_xml_modify(qser_apn_info_s *apn)
+static int apn_db_modify(qser_apn_info_s *apn)
 {
-    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;
-    }
-    //check apn_type
-    check_node = modify_node;
-    bzero(temp_buff,12);
-    snprintf(temp_buff,sizeof(temp_buff),"%d",(unsigned int)apn->profile_idx);
-    while (check_node != NULL)
-    {
-        if (xmlGetProp(check_node, "profile_idx") == NULL)  //Null Node
-        {
-            check_node = check_node->next;
-            continue;
-        }
-        else if(strcmp((char *)xmlGetProp(check_node, "apn_type"), apn->apn_type) == 0)
-        {
-            if(strcmp((char *)xmlGetProp(check_node, "profile_idx"), temp_buff) == 0)
-            {
-                check_node = check_node->next;
-                continue;
-            }
-            LYERRLOG("apntype already exists\n");
-            goto FAILED;
-        }
-        check_node = check_node->next;
-    }
-    //check apn_type end
-    for (node_num=0; node_num<(int)apn->profile_idx;node_num++)
-    {
-        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;
-        }
-        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",node_num);
-    xmlSetProp(modify_node,BAD_CAST"profile_idx",(xmlChar *)temp_buff);
-    bzero(temp_buff,12);
-    snprintf(temp_buff,sizeof(temp_buff),"%d",apn->pdp_type);
-    xmlSetProp(modify_node,BAD_CAST"pdp_type",(xmlChar *)temp_buff);
-    bzero(temp_buff,12);
-    snprintf(temp_buff,sizeof(temp_buff),"%d",apn->auth_proto);
-    xmlSetProp(modify_node,BAD_CAST"auth_proto",(xmlChar *)temp_buff);
-    xmlSetProp(modify_node,BAD_CAST"apn_name",(xmlChar *)apn->apn_name);
-    xmlSetProp(modify_node,BAD_CAST"username",(xmlChar *)apn->username);
-    xmlSetProp(modify_node,BAD_CAST"password",(xmlChar *)apn->password);
-    xmlSetProp(modify_node,BAD_CAST"apn_type",(xmlChar *)apn->apn_type);
-    xmlSaveFormatFileEnc(data_xml_path, apn_table_xml_pdoc, "UTF-8", 1);
-//    xmlFreeDoc(apn_table_xml_pdoc);
-    return RESULT_OK;
+    char *zErrMsg = 0;
+    int rc;
 
-    FAILED:
-    // if (apn_table_xml_pdoc)
-    // {
-    //     xmlFreeDoc(apn_table_xml_pdoc);
-    // }
-    return RESULT_ERROR;
+    if(!lynq_check_index(apn->profile_idx))
+    {
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    /* Open database */
+
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    char *sql = sqlite3_mprintf("UPDATE LYNQAPN set PDP_TYPE = %d, AUTH_PROTO = %d, APN_NAME = '%q', USERNAME = '%q', PASSWORD = '%q', APN_TYPE = '%q' WHERE ID=%d", apn->pdp_type, apn->auth_proto, apn->apn_name, apn->username, apn->password, apn->apn_type, apn->profile_idx);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, NULL, NULL, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    LYINFLOG("set apn %d successfully\n",apn->profile_idx);
+    sqlite3_close(apnDb);
+    return 0;
 }
 
-
-int apn_xml_query(unsigned char profile_idx,qser_apn_info_s *apn)
+static int lynq_apn_db_query_cb(void *data, int argc, char **argv, char **azColName)
 {
-    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)
+    if(data==NULL)
     {
-        LYERRLOG("open xml file error");
-        goto FAILED;
+        return 1;
     }
-    
-    node = xmlDocGetRootElement(apn_table_xml_pdoc);
-    if (NULL == node)
-    {
-        LYERRLOG("xmlDocGetRootElement() error");
-        goto FAILED;
+    qser_apn_info_s* temp = (qser_apn_info_s *)data;
+    for(int i=0; i<argc; i++){
+       LYINFLOG("[%s]%s = %s", __FUNCTION__,azColName[i], argv[i] ? argv[i] : "NULL");
     }
-    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 (node_num = 0;(node_num<(int)profile_idx);node_num++)
-    {
-        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;
-        }
-        modify_node = modify_node->next;
-    }
-    if(modify_node == NULL)
-    {
-        LYERRLOG("modify_node is null\n");
-        goto FAILED;
-    }
-    apn->profile_idx = (unsigned char)atoi(xmlGetProp(modify_node, "profile_idx"));
-    apn->pdp_type = (qser_apn_pdp_type_e)atoi(xmlGetProp(modify_node, "pdp_type"));
-    apn->auth_proto = (qser_apn_auth_proto_e)atoi(xmlGetProp(modify_node, "auth_proto"));
-    strncpy(apn->apn_name,(char *)xmlGetProp(modify_node, "apn_name"), QSER_APN_NAME_SIZE);
-    strncpy(apn->username,(char *)xmlGetProp(modify_node, "username"), QSER_APN_USERNAME_SIZE);
-    strncpy(apn->password,(char *)xmlGetProp(modify_node, "password"), QSER_APN_PASSWORD_SIZE);
-    strncpy(apn->apn_type,(char *)xmlGetProp(modify_node, "apn_type"), QSER_APN_NAME_SIZE);
-//    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;
+    temp->profile_idx = atoi(argv[0]);
+    temp->pdp_type = atoi(argv[1]);
+    temp->auth_proto = atoi(argv[2]);
+    memcpy(temp->apn_name,argv[3],QSER_APN_NAME_SIZE);
+    memcpy(temp->username,argv[4],QSER_APN_USERNAME_SIZE);
+    memcpy(temp->password,argv[5],QSER_APN_PASSWORD_SIZE);
+    memcpy(temp->apn_type,argv[6],QSER_APN_NAME_SIZE);
+    LYINFLOG("lynq_apn_db_query_cb: profile_idx is %d", atoi(argv[0]));
+    return 0;
 }
-
-int apn_xml_query_list(qser_apn_info_list_s *apn_list)
+static int apn_db_query(unsigned char profile_idx,qser_apn_info_s *apn)
 {
-    int node_num = 0;
-    //xmlDocPtr apn_table_xml_pdoc = NULL;
-    xmlNodePtr node = NULL;
-    xmlNodePtr modify_node = NULL;
-    xmlChar *temp_char;
-    char temp[64];
-//    apn_table_xml_pdoc = xmlReadFile(data_xml_path ,"UTF-8",XML_PARSE_RECOVER);
-    if(NULL == apn_table_xml_pdoc)
+    char *zErrMsg = 0;
+    int rc;
+
+    if(!lynq_check_index(profile_idx))
     {
-        LYERRLOG("open xml file error");
-        goto FAILED;
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    /* Open database */
+
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    char *sql = sqlite3_mprintf("SELECT * from LYNQAPN WHERE ID=%d", (int)profile_idx);
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, lynq_apn_db_query_cb, (qser_apn_info_s *)apn, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    LYINFLOG("set apn %d successfully\n",apn->profile_idx);
+    sqlite3_close(apnDb);
+    return 0;
+}
+static int apn_db_query_list_cb(void *data, int argc, char **argv, char **azColName)
+{
+    int i;
+
+    //apn_count++;
+
+    qser_apn_info_list_s* temp = (qser_apn_info_list_s *)data;
+    for(i=0; i<argc; i++){
+       LYINFLOG("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
     }
     
-    node = xmlDocGetRootElement(apn_table_xml_pdoc);
-    if (NULL == node)
-    {
-        LYERRLOG("xmlDocGetRootElement() error");
-        goto FAILED;
-    }
-    modify_node = node->xmlChildrenNode;
-    modify_node = modify_node->next;
-    while (modify_node != NULL)
-    {
-        temp_char = xmlGetProp(modify_node, "profile_idx");
-        if (temp_char == NULL) 
-        {
-            modify_node = modify_node->next;
-            continue;
-        }
-        sprintf(temp,"%s",temp_char);
-        apn_list->apn[node_num].profile_idx = (unsigned char)atoi(temp);
-        apn_list->apn[node_num].pdp_type = (qser_apn_pdp_type_e)atoi(xmlGetProp(modify_node, "pdp_type"));
-        apn_list->apn[node_num].auth_proto = (qser_apn_auth_proto_e)atoi(xmlGetProp(modify_node, "auth_proto"));
-        strncpy(apn_list->apn[node_num].apn_name,(char *)xmlGetProp(modify_node, "apn_name"), QSER_APN_NAME_SIZE);
-        strncpy(apn_list->apn[node_num].username,(char *)xmlGetProp(modify_node, "username"), QSER_APN_USERNAME_SIZE);
-        strncpy(apn_list->apn[node_num].password,(char *)xmlGetProp(modify_node, "password"), QSER_APN_PASSWORD_SIZE);
-        strncpy(apn_list->apn[node_num].apn_type,(char *)xmlGetProp(modify_node, "apn_type"), QSER_APN_NAME_SIZE);
-        node_num ++;
-        modify_node = modify_node->next;
-    }
-    apn_list->cnt = node_num;
-//    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;
+    temp->apn[temp->cnt].profile_idx = atoi(argv[0]);
+    temp->apn[temp->cnt].pdp_type = atoi(argv[1]);
+    temp->apn[temp->cnt].auth_proto = atoi(argv[2]);
+    memcpy(temp->apn[temp->cnt].apn_name,argv[3],QSER_APN_NAME_SIZE);
+    memcpy(temp->apn[temp->cnt].username,argv[4],QSER_APN_USERNAME_SIZE);
+    memcpy(temp->apn[temp->cnt].password,argv[5],QSER_APN_PASSWORD_SIZE);
+    memcpy(temp->apn[temp->cnt].apn_type,argv[6],QSER_APN_NAME_SIZE);
+    temp->cnt++;
+    return 0;
 }
+static int apn_db_query_list(qser_apn_info_list_s *apn_list)
+{
+    char *zErrMsg = 0;
+    int rc;
 
+    /* Open database */
+    apn_count = 0;
+    apn_list->cnt = 0;
+    rc = sqlite3_open(APN_DB_PATH, &apnDb);
+    if( rc )
+    {
+        LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        return LYNQ_E_APN_DB_FAIL;
+    }
+
+    LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
+    /* Create SQL statement */
+    char *sql = sqlite3_mprintf("SELECT * from LYNQAPN");
+    LYINFLOG("sql: %s\n", sql);
+    /* Execute SQL statement */
+    rc = sqlite3_exec(apnDb, sql, apn_db_query_list_cb, (qser_apn_info_list_s *)apn_list, &zErrMsg);
+    if( rc != SQLITE_OK )
+    {
+        LYERRLOG("SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+        sqlite3_close(apnDb);
+        return LYNQ_E_APN_DB_FAIL;
+    }
+    sqlite3_close(apnDb);
+    return 0;
+}
+// **************apn DB end****************************
 void judge_pdp_type(qser_apn_pdp_type_e pdp_type,char *out_pdp_type)
 {
     switch (pdp_type)
@@ -1123,10 +1101,10 @@
     }
     lynq_get_apn_table(&table_num,apn_table);
     memset(&apn,0,sizeof(qser_apn_info_s));
-    ret = apn_xml_query(profile_idx,&apn);
+    ret = apn_db_query(profile_idx,&apn);
     if (ret < 0)
     {
-        LYERRLOG("apn_xml_query error");
+        LYERRLOG("apn_db_query error");
         return ret;
     }
     for (int j = 0;j < table_num;j++)
@@ -1178,7 +1156,7 @@
         lynq_get_data_call_list(&handle,&data_urc_info);
         /*compare paramter*/
         //data_cb_state.profile_idx = (char)handle;
-        apn_xml_handle_get_profile(handle, &data_cb_state.profile_idx);
+        apn_db_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, 
@@ -1208,7 +1186,7 @@
         else
         {
             data_cb_state.state = QSER_DATA_CALL_DISCONNECTED;
-            ret = apn_xml_handle_set(data_cb_state.profile_idx, default_handle);
+            ret = apn_db_handle_set(data_cb_state.profile_idx, default_handle);
             if(ret != 0)
             {
                 LYERRLOG("handle set error");
@@ -1266,23 +1244,6 @@
     }
     return;
 }
-int check_xml_file(const char *file)
-{
-    /* Check for existence */
-    if((access(file, F_OK)) == -1)   
-    {   
-        LYERRLOG("no such xml file.\n");
-        system("cp /data/lynq_qser_data_apn.xml /mnt/userdata/");
-    }
-
-    if((access(file, F_OK)) == -1)   
-    {   
-        LYERRLOG("error copy xml file.\n");
-        return -1;
-    }
-    return RESULT_OK;
-}
-
 int qser_data_call_init(qser_data_call_evt_cb_t evt_cb)
 {   
     int ret = 0;
@@ -1293,13 +1254,6 @@
         return RESULT_ERROR;
     }
 
-    ret = check_xml_file(data_xml_path);
-    if (ret != RESULT_OK)
-    {
-        LYERRLOG("check xml file error");
-        return RESULT_ERROR;
-    }
-
     s_data_call_cb = evt_cb;
     
     ret = lynq_init_data(utoken);
@@ -1310,13 +1264,14 @@
         return RESULT_ERROR;
     }
     qser_cb_pthread_create();
-    apn_table_xml_pdoc = xmlReadFile(data_xml_path ,"UTF-8",XML_PARSE_RECOVER);
-    if(NULL == apn_table_xml_pdoc)
+    ret = init_apn_db();
+    if(ret != RESULT_OK)
     {
-        LYERRLOG("open xml file error");
+        LYERRLOG("init apn db error");
         return RESULT_ERROR;
     }
-    ret = apn_xml_handle_clean();
+
+    ret = apn_db_handle_clean();
     if(ret != RESULT_OK)
     {
         LYERRLOG("clean handle error");
@@ -1329,10 +1284,6 @@
 void qser_data_call_destroy(void)
 {
     LYINFLOG("[%s] start [%d]",__FUNCTION__,__LINE__);
-    if(apn_table_xml_pdoc)
-    {
-        xmlFreeDoc(apn_table_xml_pdoc);
-    }
     lynq_deinit_data();
     qser_cb_pthread_cancel();
     s_data_call_cb = NULL;
@@ -1371,7 +1322,8 @@
     {
         char pdptype[16];
         qser_apn_info_s apn_info;
-        ret = qser_apn_get(data_call->profile_idx,&apn_info);
+        LYINFLOG("datacall->profile_idx is %d\n", data_call->profile_idx);
+        ret = qser_apn_get((unsigned char)data_call->profile_idx,&apn_info);
         if (ret != 0)
         {
             LYERRLOG("qser_apn_get error");
@@ -1387,7 +1339,7 @@
     }
     else
     {
-        error = apn_xml_handle_set(data_call->profile_idx, handle);
+        error = apn_db_handle_set((unsigned char)data_call->profile_idx, handle);
         if(error != 0)
         {
             LYERRLOG("handle set error");
@@ -1426,7 +1378,8 @@
     {
         char pdptype[16];
         qser_apn_info_s apn_info;
-        ret = qser_apn_get(data_call->profile_idx,&apn_info);
+        LYINFLOG("datacall->profile_idx is %d\n", data_call->profile_idx);
+        ret = qser_apn_get((unsigned char)data_call->profile_idx,&apn_info);
         if (ret != 0)
         {
             LYERRLOG("qser_apn_get error");
@@ -1442,7 +1395,7 @@
     }
     else
     {
-        error = apn_xml_handle_set(data_call->profile_idx, handle);
+        error = apn_db_handle_set((unsigned char)data_call->profile_idx, handle);
         if(error != 0)
         {
             LYERRLOG("handle set error");
@@ -1555,7 +1508,7 @@
         LYERRLOG("apn set incoming paramters error");
         return RESULT_ERROR;
     }
-    ret = apn_xml_handle_get(apn->profile_idx, &handle);
+    ret = apn_db_handle_get(apn->profile_idx, &handle);
     if(ret != 0)
     {
         LYERRLOG("handle set error");
@@ -1565,10 +1518,10 @@
         LYERRLOG("It has setup datacall");
         return RESULT_ERROR;
     }
-    ret = apn_xml_modify(apn);
+    ret = apn_db_modify(apn);
     if (ret < 0)
     {
-        LYERRLOG("apn_xml_modify error");
+        LYERRLOG("apn_db_modify error");
         return ret;
     }
 #ifndef MOBILETEK_TARGET_PLATFORM_T106
@@ -1598,16 +1551,16 @@
     {
         return LYNQ_E_NO_INIT;
     }
-    if (profile_idx < 0 || profile_idx > 24 || NULL == apn)
+    if (profile_idx < 0 || profile_idx > QSER_APN_MAX_LIST || NULL == apn)
     {
         LYERRLOG("apn get incoming paramters error");
         return RESULT_ERROR;
     }
     int ret = 0;
-    ret = apn_xml_query(profile_idx,apn);
+    ret = apn_db_query(profile_idx,apn);
     if (ret < 0)
     {
-        LYERRLOG("apn_xml_query error");
+        LYERRLOG("apn_db_query error");
         return ret;
     }
     return ret;
@@ -1616,6 +1569,7 @@
 int qser_apn_add(qser_apn_add_s *apn, unsigned char *profile_idx)
 {
     int ret = 0;
+
     if(g_lynq_qser_data_init_flag == 0)
     {
         return LYNQ_E_NO_INIT;
@@ -1625,12 +1579,26 @@
         LYERRLOG("apn add incoming paramters error");
         return RESULT_ERROR;
     }
-    ret = apn_xml_add(apn,profile_idx);
-    if (ret < 0)
+
+    *profile_idx = 0;
+    *profile_idx = (unsigned char)find_unuse_apn_index(APN_DB_PATH);
+    LYINFLOG("[%s] id = %d\n",__FUNCTION__,(int)*profile_idx);
+    if((int)*profile_idx > 0 && (int)*profile_idx < QSER_APN_MAX_LIST)
     {
-        LYERRLOG("apn_xml_add error");
-        return ret;
+        LYINFLOG("[%s] apn full is false\n",__FUNCTION__);
+        ret = apn_db_add((int)*profile_idx, apn);
+        if (ret != 0)
+        {
+            LYERRLOG("apn_db_add error");
+            return RESULT_ERROR;
+        }
+        else
+        {
+            return RESULT_OK;
+        }
     }
+    LYERRLOG("apn storage space is full!!!");
+    return RESULT_ERROR;
 #ifndef MOBILETEK_TARGET_PLATFORM_T106
     int apn_id = 0;
     char tmp_id[12];
@@ -1664,7 +1632,7 @@
     }
     int ret = 0;
     int handle = -1;
-    ret = apn_xml_handle_get(profile_idx, &handle);
+    ret = apn_db_handle_get(profile_idx, &handle);
     if(ret != 0)
     {
         LYERRLOG("handle set error");
@@ -1674,10 +1642,10 @@
         LYERRLOG("It has setup datacall");
         return RESULT_ERROR;
     }
-    ret = apn_xml_delete(profile_idx);
+    ret = apn_db_delete(profile_idx);
     if (ret < 0)
     {
-        LYERRLOG("apn_xml_delete error");
+        LYERRLOG("apn_db_delete error");
         return ret;
     }
 #ifndef MOBILETEK_TARGET_PLATFORM_T106
@@ -1709,10 +1677,10 @@
         return RESULT_ERROR;
     }
     int ret = 0;
-    ret = apn_xml_query_list(apn_list);
+    ret = apn_db_query_list(apn_list);
     if (ret < 0)
     {
-        LYERRLOG("apn_xml_query_list error");
+        LYERRLOG("apn_db_query_list error");
         return ret;
     }
     return ret;
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
deleted file mode 100755
index d881dc0..0000000
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/lynq_qser_data_apn.xml
+++ /dev/null
@@ -1,3 +0,0 @@
-<?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" handle="11"/></lynq_qser_data_apn>
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/makefile b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/makefile
index 4b5eb82..4bc0a6c 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/makefile
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-data/makefile
@@ -24,7 +24,6 @@
   -I$(ROOT)$(includedir)/logger \
   -I$(ROOT)$(includedir)/liblog \
   -I$(ROOT)$(includedir)/libdata \
-  -I$(ROOT)$(includedir)/libxml2 \
 
 
 LOCAL_LIBS := \
@@ -35,7 +34,7 @@
     -lpthread \
     -llynq-log \
     -llynq-data \
-	-lxml2 \
+    -lsqlite3 \
 
 
 SOURCES = $(wildcard *.cpp)
diff --git a/cap/zx297520v3/src/lynq/packages/apps/Mobiletek_Tester_RDIT/data.cpp b/cap/zx297520v3/src/lynq/packages/apps/Mobiletek_Tester_RDIT/data.cpp
index 92cf3a8..f69cc13 100755
--- a/cap/zx297520v3/src/lynq/packages/apps/Mobiletek_Tester_RDIT/data.cpp
+++ b/cap/zx297520v3/src/lynq/packages/apps/Mobiletek_Tester_RDIT/data.cpp
@@ -451,8 +451,8 @@
     printf("%s%d\n",TEST_RESULT,ret);

     for(int i = 0; i < apn_list.cnt; i++)

     {

-        printf("LYNQ_QSER_APN_GET_LIST: pdp_type=%d, auth_proto=%d, apn_name=%s, username=%s, password=%s, apn_type=%s\n"

-            ,apn_list.apn[i].pdp_type, apn_list.apn[i].auth_proto, apn_list.apn[i].apn_name, apn_list.apn[i].username, apn_list.apn[i].password, apn_list.apn[i].apn_type);

+        printf("LYNQ_QSER_APN_GET_LIST: profile_idx=%d, pdp_type=%d, auth_proto=%d, apn_name=%s, username=%s, password=%s, apn_type=%s\n"

+            ,apn_list.apn[i].profile_idx, apn_list.apn[i].pdp_type, apn_list.apn[i].auth_proto, apn_list.apn[i].apn_name, apn_list.apn[i].username, apn_list.apn[i].password, apn_list.apn[i].apn_type);

     }

     return ret;

 }