[Feature][T8TSK-302][SUSPEND]Protection of the lib library during service restart

Only Configure:No
Affected branch:GSW3.0-No-Connman-2CoreT
Affected module:autosuspend
Is it affected on both ZXIC and MTK:only MTK
Self-test:Yes
Doc Update:No

Change-Id: I942134d420327b5fe1abe1b8c4afd66b20645904
diff --git a/lib/libautosuspend/libautosuspend.c b/lib/libautosuspend/libautosuspend.c
old mode 100644
new mode 100755
index 58412a6..defbff3
--- a/lib/libautosuspend/libautosuspend.c
+++ b/lib/libautosuspend/libautosuspend.c
@@ -100,7 +100,7 @@
 
 static int Close(int fd)
 {
-    if (Close(fd) == -1)
+    if (close(fd) == -1)
     {
         ALOGI("Close error\n");
         return -1;
@@ -169,28 +169,21 @@
 static void *deal_get_feedback(void *sockfd)
 {
     int rc;
-
-    int client_sock = *((int *)sockfd);
-
-    // pthread_mutex_lock(&feedback_got_mutex);
-    
+    int ret;
+    int num[10]={5,5,10,10,30,30,60,60,90,90};
+    int i = 0;
     while (1)
     {
-        // ALOGI("deal_get_feedback thread wait.\n");
-
-        // pthread_cond_wait(&get_feedback_cond,&get_feedback_mutex);
 
         ALOGI("start get feedback from the service.\n");
-
         pthread_mutex_lock(&feedback_got_mutex);
-
         memset(&time_info_client,0,sizeof(struct time_info_t));
-
-        rc = Read(client_sock,&time_info_client,sizeof(struct time_info_t));
+        rc = Read(client_data_sock_fd,&time_info_client,sizeof(struct time_info_t));
+        ALOGI("deal_get_feedback: rc=%d, client_data_sock_fd=%d.\n", rc, client_data_sock_fd);
         if(rc == -1)
-        {           
+        {
             ALOGI("client read wakeup_feedback struct fail.\n");
-            Close(client_sock);
+            Close(client_data_sock_fd);
             pthread_mutex_unlock(&feedback_got_mutex);
             break ;
         }
@@ -200,22 +193,40 @@
             pthread_mutex_unlock(&feedback_got_mutex);
             continue;
         }
+        else if(rc == 0)
+        {
+            if(i<10)
+            {
+                sleep(num[i]);
+            }
+            else
+            {
+                sleep(120);
+            }
+            i++;
+            system("rm -rf /tmp/*data.client");
+            system("rm -rf /tmp/*cmd.client");
+            ret = lynq_autosleep_retry();
+            if(ret > 0)
+            {
+                ALOGI("lynq autosuspend retry success\n");
+                i = 0;
+            }
+            pthread_mutex_unlock(&feedback_got_mutex);
+            continue;
+        }
 
         ALOGI("system sleep_start timestamps : %ld ms\n",time_info_client.sleep_start_time);
         ALOGI("system wakeup timestamps : %ld ms\n",time_info_client.wakeup_time);
 
-        // pthread_cond_broadcast(&feedback_got_cond);
 
         pthread_mutex_unlock(&feedback_got_mutex);
-
         usleep(10000);  //给libautosuspend_get_feedback函数时间进入wait,不可删除,但可以减少
-
         pthread_cond_broadcast(&feedback_got_cond);
-
         usleep(10000); //希望多给libautosuspend_get_feedback函数拿到锁的机会,不可删除,但可以减少
-        
+
     }
-    
+
 }
 
 
@@ -252,14 +263,13 @@
     }
 
     pthread_t feedback_tid;
-    pthread_create(&feedback_tid,NULL,deal_get_feedback,(void*)&client_data_sock_fd);
+    pthread_create(&feedback_tid,NULL,deal_get_feedback,NULL);
     pthread_detach(feedback_tid);
-
-
     libautosuspend_inited = true;
 
     pthread_mutex_unlock(&client_fd_mutex);
-    
+
+
     return 0;
     
 }
@@ -296,21 +306,52 @@
 
 }
 
-int lynq_autosleep_enable(void)
+static int libautosuspend_reconnect(void)
+{
+
+
+    ALOGI("Start libautosuspend_init.\n");
+
+    char client_cmd_path[40];
+    char client_data_path[40];
+
+    sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
+    sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
+
+
+    pthread_mutex_lock(&client_fd_mutex);
+
+    if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
+    {
+        ALOGI("cmd channel connect error.\n");
+        pthread_mutex_unlock(&client_fd_mutex);
+        return -1;
+    }
+
+    if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
+    {
+        ALOGI("data channel connect error.\n");
+        pthread_mutex_unlock(&client_fd_mutex);
+        return -1;
+    }
+
+    pthread_mutex_unlock(&client_fd_mutex);
+
+    return 0;
+
+}
+
+
+int lynq_autosleep_retry(void)
 {
     char value[15]="enable";
     char res[15];
 
-    if(libautosuspend_init() != 0)
+    if(libautosuspend_reconnect() != 0)
     {
         return -1;
     }
 
-    // if(libautosuspend_enabled)
-    // {
-    //     return 0;
-    // }
-
     pthread_mutex_lock(&client_fd_mutex);
 
     int rc = send_cmd(value,strlen(value));
@@ -320,24 +361,33 @@
         pthread_mutex_unlock(&client_fd_mutex);
         return -1;
     }
+    pthread_mutex_unlock(&client_fd_mutex);
 
-    // if(Read(client_sock_fd,res,sizeof(res)) <= 0)
-    // {
-    //     ALOGI("libautosuspend get respond fail.\n");
-    //     pthread_mutex_unlock(&client_fd_mutex);
-    //     return -1;
-    // }
+    return 1;
+  
+}
 
-    // ALOGI("libautosuspend get respond : %s.\n",res);
 
-    // if(strcmp(res,"enabled") != 0)
-    // {
-    //     pthread_mutex_unlock(&client_fd_mutex);
-    //     return -1;
-    // }
 
-    // libautosuspend_enabled = true;
+int lynq_autosleep_enable(void)
+{
+    char value[15]="enable";
+    char res[15];
 
+    if(libautosuspend_init() != 0)
+    {
+        return -1;
+    }
+
+    pthread_mutex_lock(&client_fd_mutex);
+
+    int rc = send_cmd(value,strlen(value));
+    if(rc < 0)
+    {
+        ALOGI("libautosuspend send enable cmd fail.\n");
+        pthread_mutex_unlock(&client_fd_mutex);
+        return -1;
+    }
     pthread_mutex_unlock(&client_fd_mutex);
 
     return 1;
@@ -354,10 +404,6 @@
         return -1;
     }
 
-    // if(!libautosuspend_enabled)
-    // {
-    //     return 0;
-    // }
 
     pthread_mutex_lock(&client_fd_mutex);
 
@@ -369,23 +415,6 @@
         return -1;
     }
 
-    // if(Read(client_sock_fd,res,sizeof(res)) <= 0)
-    // {
-    //     ALOGI("libautosuspend get respond fail.\n");
-    //     pthread_mutex_unlock(&client_fd_mutex);
-    //     return -1;
-    // }
-
-    // ALOGI("libautosuspend get respond : %s.\n",res);
-
-    // if(strcmp(res,"disabled") != 0)
-    // {
-    //     pthread_mutex_unlock(&client_fd_mutex);
-    //     return -1;
-    // }
-
-    // libautosuspend_enabled = false;
-
     pthread_mutex_unlock(&client_fd_mutex);
 
     return 1;
@@ -396,68 +425,17 @@
 
 int libautosuspend_get_feedback(struct time_info_t *time_info)
 {
-    // char value[15]="feedback";
-    // char res[15];    
-
-    // if(!libautosuspend_enabled)
-    // {
-    //     ALOGI("system autosuspend disabled, can not get wakeup feedback.\n");
-    //     return -1;
-    // }
 
     ALOGI("start get feedback from the service.\n");
-
     memset(time_info,0,sizeof(struct time_info_t));
 
-    // if(timeout == NULL)
-    // {
-    //     ALOGI("client set timeout for receiving wakeup_feedback: NULL.\n");
-    // }
-    // else
-    // {
-    //     struct timeval recv_timeout = {(*timeout),0};
-    //     pthread_mutex_lock(&client_data_fd_mutex);
-    //     if(setsockopt(client_data_sock_fd,SOL_SOCKET,SO_RCVTIMEO,(char*)&recv_timeout,sizeof(struct timeval)) == -1)
-    //     {
-    //         ALOGI("client set timeout for receiving wakeup_feedback: error.\n");
-    //         pthread_mutex_unlock(&client_data_fd_mutex);
-    //         return -1;
-    //     }
-            
-    //     ALOGI("client set timeout for receiving wakeup_feedback: %d s.\n",(*timeout));
-    //     pthread_mutex_unlock(&client_data_fd_mutex);
-        
-    // }
-
-    // int rc = send_cmd(value,strlen(value));
-    // if(rc < 0)
-    // {
-    //     ALOGI("libautosuspend send feedback cmd fail.\n");
-    //     pthread_mutex_unlock(&client_fd_mutex);
-    //     return -1;
-    // }
-
-    // if(Read(client_data_sock_fd,time_info,sizeof(struct time_info_t)) <= 0)
-    // {
-    //     ALOGI("libautosuspend_get_feedback fail.\n");
-    //     pthread_mutex_unlock(&client_fd_mutex);
-    //     return -1;
-    // }
-
 
     ALOGI("libautosuspend_get_feedback wait.\n");
-
     pthread_mutex_lock(&feedback_got_mutex);
-
     pthread_cond_wait(&feedback_got_cond,&feedback_got_mutex);
-
     memcpy(time_info,&time_info_client,sizeof(struct time_info_t));
-
     ALOGI("libautosuspend_get_feedback success.\n");
-
     pthread_mutex_unlock(&feedback_got_mutex);
-    // ALOGI("[client] system sleep_start timestamps : %ld ms\n",time_info.sleep_start_time);
-    // ALOGI("[client] system wakeup timestamps : %ld ms\n",time_info.wakeup_time);
 
     return 0;
 
@@ -466,49 +444,38 @@
 
 int lynq_wait_wakeup_event(long *sleep_start_time, long * wakeup_time)
 {
-   int *socket_timeout = NULL;
-   struct time_info_t time_info;
-   int ret = 0;
-   /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time start*/
-   if(feedback_flag == true)
-   {
-       if(libautosuspend_init() != 0)
-       {
-           return -1;
-       }
-   }
-   feedback_flag = false; 
-   /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time end*/
-   memset(&time_info,0,sizeof(struct time_info_t));
-   if(sleep_start_time == NULL || wakeup_time == NULL )
-   {
-      ALOGI("lynq_wait_wakeup_event input errors.\n");
-	  return -1;
-   }
-   ret=libautosuspend_get_feedback(&time_info);
-   if(ret == 0)
-   {
-     *sleep_start_time = time_info.sleep_start_time;
-	 *wakeup_time = time_info.wakeup_time;
-	 return 0;
-   }
-   else
-   {
-      return -1;
-   }
+    int *socket_timeout = NULL;
+    struct time_info_t time_info;
+    int ret = 0;
+    /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time start*/
+    if(feedback_flag == true)
+    {
+        if(libautosuspend_init() != 0)
+        {
+            return -1;
+        }
+    }
+    feedback_flag = false;
+    /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time end*/
+    memset(&time_info,0,sizeof(struct time_info_t));
+    if(sleep_start_time == NULL || wakeup_time == NULL )
+    {
+        ALOGI("lynq_wait_wakeup_event input errors.\n");
+        return -1;
+    }
+    ret=libautosuspend_get_feedback(&time_info);
+    if(ret == 0)
+    {
+        *sleep_start_time = time_info.sleep_start_time;
+        *wakeup_time = time_info.wakeup_time;
+        return 0;
+    }
+    else
+    {
+        return -1;
+    }
    
 
 }
 
-// static void libautosuspend_get_feedback()
-// {   
-//     pthread_t feedback_tid;
-//     pthread_create(&feedback_tid,NULL,deal_get_feedback,(void*)&client_sock);
-//     pthread_detach(&feedback_tid);
-
-//     return ;
-    
-// }
-
-