[Bugfix][T108-GSW][bug-view-1923] gsw_nw_sdk_init sometimes is blocked

    Only Configure: No
    Affected branch: unknown
    Affected module: rtp
    Is it affected on both ZXIC and ASR: only ASR
    Self-test: yes
    Doc Update: no

Change-Id: I169fdf6e44d79ead3028d9c3106c56b8682cffb7
diff --git a/mbtk/libgsw_lib/gsw_nw_interface.c b/mbtk/libgsw_lib/gsw_nw_interface.c
index 7be4b24..beb811c 100755
--- a/mbtk/libgsw_lib/gsw_nw_interface.c
+++ b/mbtk/libgsw_lib/gsw_nw_interface.c
@@ -403,6 +403,8 @@
     GSW_PREFER_MODE_NR5G_LTE_WCDMA_GSM    = 32,   /**<  5G/4G/3G/2G*/

 } PREFER_MODE_E;

 

+#define NW_THEAD_NUM (3)

+

 static int mbtk_nw_api_import()

 {

     dlHandle_mbtk = dlopen(lib_mbtk_path, RTLD_NOW);

@@ -1549,7 +1551,7 @@
     {

         return GSW_HAL_NORMAL_FAIL;

     }

-

+    

     void* cb_func;

     if(handle_ptr == NULL)

     {

@@ -1599,6 +1601,70 @@
     return GSW_HAL_SUCCESS;

 }

 

+static void gsw_un_reg_all()

+{

+    gsw_reg_serving_info_callback(NULL);

+    gsw_reg_sig_info_callback(NULL);

+    gsw_reg_operating_mode_callback(NULL);

+    gsw_reg_set_modem_status_event_callback(NULL);

+    gsw_reg_rej_cause_callback(NULL);

+}

+

+pthread_t s_tid[NW_THEAD_NUM] = {-1,-1,-1};

+void* s_thread_func[NW_THEAD_NUM]={

+         (void*)gsw_sig_info_timer,

+         (void*)gsw_modem_state_timer,

+         (void*)gsw_serving_info_timer

+         };

+

+static void gsw_close_all_thread()

+{

+    int i,ret;

+

+    

+    nw_init_flag=0;

+

+/*  had better not use thread cancel, maybe handle->send_mutex is locked now, cancel will not release this mutex

+    for(i=0; i<NW_THEAD_NUM; i++)

+    {

+        if(s_tid[i]!=-1)

+        {

+            ret = pthread_cancel(s_tid[i]);

+            LOGE(GSW_NW,"pthread %d cancel, ret is %d",i, ret); 

+        }

+    }

+*/

+

+    for(i=0;i<NW_THEAD_NUM;i++)

+    {

+        if(s_tid[i]!=-1)

+        {

+            ret = pthread_join(s_tid[i],NULL);

+            LOGE(GSW_NW,"pthread %d join, ret is %d",i, ret); 

+            s_tid[i]=-1;

+        }

+    }

+}

+

+static int gsw_start_all_thread()

+{ 

+    int i,ret;

+    for(i=0; i<NW_THEAD_NUM;i++)

+    {

+        ret = pthread_create(&(s_tid[i]), NULL,s_thread_func[i], NULL);

+        if (ret != 0)

+        {

+            LOGE(GSW_NW,"pthread_create fail %d,ret is %d",i,ret);

+            gsw_close_all_thread();

+            return GSW_HAL_ERROR_GNSS_NO_THRESHOLDS;

+        }

+    }

+    

+    return GSW_HAL_SUCCESS;

+

+}

+

+

 

 /**

  * @brief network sdk init

@@ -1608,18 +1674,15 @@
  */

 int gsw_nw_sdk_init(int token)

 {

-    int ret = -1;

-    pthread_t nw_info_thread;

-

     if (nw_init_flag == 1 && nw_info_handle != NULL)

     {

         return GSW_HAL_SUCCESS;

     }

 

-    ret = mbtk_nw_api_import();

+    int ret = mbtk_nw_api_import();

     if (ret != 0)

     {

-        printf("mbtk_nw_api_import fail\n");

+        printf("mbtk_nw_api_import fail");

         return GSW_HAL_NORMAL_FAIL;

     }

 

@@ -1627,7 +1690,7 @@
 

     if (nw_info_handle == NULL)

     {

-        LOGE(GSW_NW,"mbtk_info_handle_get fail\n");

+        LOGE(GSW_NW,"mbtk_info_handle_get fail");

         return GSW_HAL_NORMAL_FAIL;

     }

 

@@ -1635,39 +1698,23 @@
     ret = mbtk_fplmn_get(nw_info_handle, fplmn);

     if(ret != 0)

     {

-        LOGE(GSW_NW,"mbtk_fplmn_get failed : %d\n",ret);

+        LOGE(GSW_NW,"mbtk_fplmn_get failed, ret is %d",ret);

         return GSW_HAL_NORMAL_FAIL;

     }

     fplmn_max_length = (strlen(fplmn)/6);

-    LOGE(GSW_NW,"fplmn = %s, fplmn_max_length = %d\n",fplmn,fplmn_max_length);

-   

+    LOGE(GSW_NW,"fplmn = %s, fplmn_max_length = %d",fplmn,fplmn_max_length);

 

     nw_init_flag = 1;

+    ret=gsw_start_all_thread();

 

-    ret = pthread_create(&nw_info_thread, NULL, (void*)gsw_sig_info_timer, NULL);

-    if (ret != 0)

+    if(ret != 0) 

     {

-        LOGE(GSW_NW,"pthread_create fail 1,ret is %d", ret);

-        nw_init_flag = 0;

-        return GSW_HAL_ERROR_GNSS_NO_THRESHOLDS;

+         nw_init_flag = 0;

+        LOGE(GSW_NW,"gsw_start_all_thread failed , ret is %d",ret);

+        return GSW_HAL_NORMAL_FAIL;

     }

 

-    

-    ret = pthread_create(&nw_info_thread, NULL, (void*)gsw_modem_state_timer, NULL);

-    if (ret != 0)

-    {

-        LOGE(GSW_NW,"pthread_create fail 2,ret is %d", ret);

-        nw_init_flag = 0;

-        return GSW_HAL_ERROR_GNSS_NO_THRESHOLDS;

-    }

-

-    ret = pthread_create(&nw_info_thread, NULL, (void*)gsw_serving_info_timer, NULL);

-    if (ret != 0)

-    {

-        LOGE(GSW_NW,"pthread_create fail 3,ret is %d", ret);

-        nw_init_flag = 0;

-        return GSW_HAL_ERROR_GNSS_NO_THRESHOLDS;

-    }

+    LOGE(GSW_NW,"gsw nw init suc");

 

     return GSW_HAL_SUCCESS;

 }

@@ -1688,16 +1735,20 @@
         return GSW_HAL_NORMAL_FAIL;

     }

 

+    gsw_un_reg_all();// had better un-reg before thread close

+    gsw_close_all_thread();//in it,  nw_init_flag=0  and  cb unreg

+    

     ret = mbtk_info_handle_free(&nw_info_handle);

     if(ret != GSW_HAL_SUCCESS)

     {

-        LOGE(GSW_NW,"mbtk_info_handle_free fail\n");

+        LOGE(GSW_NW,"mbtk_info_handle_free fail");

         return GSW_HAL_NORMAL_FAIL;

     }

 

     dlclose(dlHandle_mbtk);

     nw_info_handle = NULL;

-    nw_init_flag = 0;

+    

+    LOGE(GSW_NW,"gsw_nw_sdk_deinit suc");

 

     return GSW_HAL_SUCCESS;