[Bugfix][T106BUG-662]Fixed the problem that the profile_idx form callback function is wrong

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: I8486d1e7f0e0ca6a09df71858d25f5ad02baf1a8
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 786428f..995b7e4 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
@@ -29,6 +29,7 @@
 static pthread_t s_cb_tid = -1;
 static int s_qser_data_cb_thread_status = 0;
 static pthread_mutex_t s_qser_data_cb_mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t s_qser_data_database_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t s_qser_data_cb_cond = PTHREAD_COND_INITIALIZER;
 
 
@@ -453,10 +454,12 @@
     int idx = 0;
 
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -471,11 +474,13 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     };
     *profile_idx = (unsigned char)idx;
     LYINFLOG("get idx %d successfully\n", *profile_idx);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 static int lynq_apn_callback(void *data, int argc, char **argv, char **azColName){
@@ -505,10 +510,12 @@
     int rc;
     char sql[128]={0};
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     sprintf(sql,"%s", "SELECT * from LYNQAPN");
@@ -518,6 +525,7 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     //for(int cnt = 0; cnt < next_index;cnt++)
@@ -527,11 +535,14 @@
         {
             LYINFLOG("index = apn_index\n");
             sqlite3_close(apnDb);
+            pthread_mutex_unlock(&s_qser_data_database_mutex);
             return 1;
         }
     }
     LYINFLOG("cant find the index %d\n", index);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
+
     return 0;
 }
 
@@ -545,11 +556,12 @@
         return LYNQ_E_APN_DB_FAIL;
     }
     /* Open database */
-
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -565,10 +577,12 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     LYINFLOG("update apn %d successfully\n", profile_idx);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 static int apn_db_handle_get_cb(void *data, int argc, char **argv, char **azColName)
@@ -596,10 +610,12 @@
         return LYNQ_E_APN_DB_FAIL;
     }
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -614,10 +630,12 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     LYINFLOG("get idx %d successfully\n", *handle);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 
@@ -669,10 +687,12 @@
     char *sql;
 
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(path, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return 1;
     }
     LYINFLOG("[%s] Opened database successfully\n",__FUNCTION__);
@@ -693,16 +713,21 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return 1;
     }
     LYINFLOG("Table LYNQAPN created successfully\n");
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 static int init_apn_db()
 {
     int db_ret=0;
     int table_ret=0;
+
+    pthread_mutex_init(&s_qser_data_database_mutex, NULL);   
+
     db_ret = create_apn_db(APN_DB_PATH);
     if(db_ret==APN_DB_CREATE)
     {
@@ -732,10 +757,12 @@
     int default_handle = LYNQ_APN_CHANNEL_MAX + 1;
 
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -750,10 +777,12 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     LYINFLOG("clean apn table successfully\n");
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return RESULT_OK;
 }
 
@@ -769,10 +798,12 @@
     next_index = 0;
     LYINFLOG("index = %p",apn_indexs);
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
        LYERRLOG("Can't open database: %s\n", sqlite3_errmsg(apnDb));
+       pthread_mutex_unlock(&s_qser_data_database_mutex);
        return -1;
     }
     else
@@ -789,6 +820,7 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return -1;
     }
     else
@@ -808,6 +840,7 @@
         }
     }
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     LYINFLOG("count is %d\n", count);
     return count;
 }
@@ -818,10 +851,12 @@
     int rc;
     int default_handle = LYNQ_APN_CHANNEL_MAX + 1;
     /* Open database */
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s",__FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return 1;
     }
     LYINFLOG("[%s] Opened database successfully",__FUNCTION__);
@@ -835,10 +870,12 @@
         LYERRLOG( "SQL error: %s", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return 1;
     }
     LYINFLOG("write apn to apn db successfully");
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 
@@ -852,18 +889,19 @@
         return LYNQ_E_APN_DB_FAIL;
     }
     /* Open database */
-
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         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);
+    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);
@@ -872,10 +910,12 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     LYINFLOG("delete apn %d successfully\n", profile_idx);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 
@@ -889,11 +929,12 @@
         return LYNQ_E_APN_DB_FAIL;
     }
     /* Open database */
-
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -908,10 +949,12 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     LYINFLOG("set apn %d successfully\n",apn->profile_idx);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 
@@ -945,11 +988,12 @@
         return LYNQ_E_APN_DB_FAIL;
     }
     /* Open database */
-
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -964,10 +1008,12 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     LYINFLOG("set apn %d successfully\n",apn->profile_idx);
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 static int apn_db_query_list_cb(void *data, int argc, char **argv, char **azColName)
@@ -999,10 +1045,12 @@
     /* Open database */
     apn_count = 0;
     apn_list->cnt = 0;
+    pthread_mutex_lock(&s_qser_data_database_mutex);
     rc = sqlite3_open(APN_DB_PATH, &apnDb);
     if( rc )
     {
         LYERRLOG("[%s] Can't open database: %s\n", __FUNCTION__,sqlite3_errmsg(apnDb));
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
 
@@ -1017,9 +1065,11 @@
         LYERRLOG("SQL error: %s\n", zErrMsg);
         sqlite3_free(zErrMsg);
         sqlite3_close(apnDb);
+        pthread_mutex_unlock(&s_qser_data_database_mutex);
         return LYNQ_E_APN_DB_FAIL;
     }
     sqlite3_close(apnDb);
+    pthread_mutex_unlock(&s_qser_data_database_mutex);
     return 0;
 }
 // **************apn DB end****************************
@@ -1159,6 +1209,7 @@
         /*compare paramter*/
         //data_cb_state.profile_idx = (char)handle;
         apn_db_handle_get_profile(handle, &data_cb_state.profile_idx);
+        LYINFLOG("[thread_wait_cb_status]: callback profile_idx is %d\n", 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, 
@@ -1729,6 +1780,10 @@
 
 int qser_apn_del(unsigned char profile_idx)
 {
+    int ret = 0;
+    int handle = -1;
+
+    LYINFLOG("[%s] entry\n",__FUNCTION__);
     if(g_lynq_qser_data_init_flag == 0)
     {
         return LYNQ_E_NO_INIT;
@@ -1738,8 +1793,7 @@
         LYERRLOG("apn del incoming paramters error");
         return RESULT_ERROR;
     }
-    int ret = 0;
-    int handle = -1;
+
     ret = apn_db_handle_get(profile_idx, &handle);
     if(ret != 0)
     {