[Bugfix] fix the bug of suspend failure when autosuspend is enabled

Change-Id: I4ed78e29eeb9ee163636bbbf2d28354936abe04f
diff --git a/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/autosuspend_wakeup_count.c b/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/autosuspend_wakeup_count.c
index d567ca3..05a2110 100644
--- a/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/autosuspend_wakeup_count.c
+++ b/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/autosuspend_wakeup_count.c
@@ -27,6 +27,7 @@
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/time.h>
 
 #include <unistd.h>
 #include <dlfcn.h>
diff --git a/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/main.c b/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/main.c
index a735b1d..9b997db 100755
--- a/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/main.c
+++ b/meta/meta-mediatek-mt2735/recipes-lynq/suspend-service/files/main.c
@@ -30,6 +30,7 @@
 #include <include/lynq_uci.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <signal.h>
 
 
 #define LOG_UCI_MODULE "lynq_autosuspend"
@@ -64,7 +65,7 @@
 extern void init_network_func();
 extern void set_wakeup_callback(void (*func)(bool success));
 extern void wakeup_feedback(bool success);
-
+extern int (*lynq_screen)(int num);
 
 struct time_info_t
 {
@@ -248,33 +249,42 @@
         // 为什么对端会关闭?因为在客户端没有用nohup方式打开的情况下,系统睡眠后客户端进行会直接被杀死,对端会关闭,所以会导致read不阻塞,且总是返回0的现象
         if(Read(commfd,buf,sizeof(buf)) <= 0) 
         {
-            ALOGI("service receive suspend_cmd fail.\n");
+            ALOGI("service receive suspend_cmd fail or client is closed.\n");
             Close(commfd);
             break;
         }
         if(strcmp(buf,"enable") == 0)
         {
+            system("echo 7 | emdlogger_ctrl");
+
+            if (lynq_screen(0) < 0)  //notify ril for screen off
+            {
+                ALOGI("lynq_screen off fail\n");
+                return -1;
+            }
+
+            sleep(5);
             if(autosuspend_enable() < 0)
             {
                 ALOGI("autosuspend_enable fail.\n");
-                strcpy(res,"fail");
-                if(Write(commfd,res,strlen(res)) <= 0)
-                {
-                    ALOGI("service send respond fail.\n");
-                    Close(commfd);
-                    break;
-                }                
+                // strcpy(res,"fail");
+                // if(Write(commfd,res,strlen(res)) <= 0)
+                // {
+                //     ALOGI("service send respond fail.\n");
+                //     Close(commfd);
+                //     break;
+                // }                
             }
             else
             {
                 ALOGI("autosuspend_enable success.\n");
-                strcpy(res,"enabled");
-                if(Write(commfd,res,strlen(res)) <= 0)
-                {
-                    ALOGI("service send respond fail.\n");
-                    Close(commfd);
-                    break;
-                }
+                // strcpy(res,"enabled");
+                // if(Write(commfd,res,strlen(res)) <= 0)
+                // {
+                //     ALOGI("service send respond fail.\n");
+                //     Close(commfd);
+                //     break;
+                // }
             }        
         }
         else if(strcmp(buf,"disable") == 0)
@@ -282,24 +292,24 @@
             if(autosuspend_disable() < 0)
             {
                 ALOGI("autosuspend_disable fail.\n");
-                strcpy(res,"fail");
-                if(Write(commfd,res,strlen(res)) <= 0)
-                {
-                    ALOGI("service send respond fail.\n");
-                    Close(commfd);
-                    break;
-                }                     
+                // strcpy(res,"fail");
+                // if(Write(commfd,res,strlen(res)) <= 0)
+                // {
+                //     ALOGI("service send respond fail.\n");
+                //     Close(commfd);
+                //     break;
+                // }                     
             }
             else
             {
                 ALOGI("autosuspend_disable success.\n");
-                strcpy(res,"disabled");
-                if(Write(commfd,res,strlen(res)) <= 0)
-                {
-                    ALOGI("service send respond fail.\n");
-                    Close(commfd);
-                    break;
-                }
+                // strcpy(res,"disabled");
+                // if(Write(commfd,res,strlen(res)) <= 0)
+                // {
+                //     ALOGI("service send respond fail.\n");
+                //     Close(commfd);
+                //     break;
+                // }
             }        
         }
         // else if(strcmp(buf,"feedback") == 0)
@@ -354,15 +364,7 @@
         ALOGI("send_feedback thread wait to send.\n");
         pthread_mutex_lock(&feedback_mutex);
         pthread_cond_wait(&feedback_cond,&feedback_mutex);
-    
 
-        // strcpy(buf,"System is woken up!");
-
-        // if(Write(commfd,buf,sizeof(buf)) <= 0) 
-        // {
-        //     ALOGI("service send wakeup_feedback fail.\n");
-        //     return ;
-        // }
         ALOGI("send_feedback thread is now sending the feedback to client.\n");
         pthread_mutex_lock(&time_info_mutex);
         if(Write(commfd,&time_info,sizeof(struct time_info_t)) <= 0) 
@@ -409,7 +411,7 @@
 
     int auto_enable = 0;
 
-    lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "debug", tmp);
+    lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "debug", tmp); // 即获取系统层面的环境变量
     ALOGI("Autosuspend Service Daemon. debug %s\n",tmp);
     adb_debug_mode=atoi(tmp);
     lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "auto_enable", tmp);
@@ -418,6 +420,8 @@
     init_wakelock_func();
     init_sim_func();
 
+    signal(SIGPIPE,SIG_IGN); // 忽略SIGPIPE信号,防止由于客户端关闭,继续往客户端write,会导致服务端收到SIGPIPE信号而Broken pipe
+
     
     // init_network_func();
 
@@ -429,6 +433,7 @@
     // }
 
     set_wakeup_callback(wakeup_feedback);
+    // 注册回调函数
 
    if(auto_enable==0)
    {
diff --git a/src/lynq/lib/libautosuspend/libautosuspend.c b/src/lynq/lib/libautosuspend/libautosuspend.c
index 2588b04..a273595 100644
--- a/src/lynq/lib/libautosuspend/libautosuspend.c
+++ b/src/lynq/lib/libautosuspend/libautosuspend.c
@@ -320,20 +320,20 @@
         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;
-    }
+    // 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);
+    // ALOGI("libautosuspend get respond : %s.\n",res);
 
-    if(strcmp(res,"enabled") != 0)
-    {
-        pthread_mutex_unlock(&client_fd_mutex);
-        return -1;
-    }
+    // if(strcmp(res,"enabled") != 0)
+    // {
+    //     pthread_mutex_unlock(&client_fd_mutex);
+    //     return -1;
+    // }
 
     // libautosuspend_enabled = true;
 
@@ -368,20 +368,20 @@
         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;
-    }
+    // 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);
+    // ALOGI("libautosuspend get respond : %s.\n",res);
 
-    if(strcmp(res,"disabled") != 0)
-    {
-        pthread_mutex_unlock(&client_fd_mutex);
-        return -1;
-    }
+    // if(strcmp(res,"disabled") != 0)
+    // {
+    //     pthread_mutex_unlock(&client_fd_mutex);
+    //     return -1;
+    // }
 
     // libautosuspend_enabled = false;
 
@@ -392,34 +392,8 @@
 }
 
 
-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;
-    
-   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,socket_timeout);
-   if(ret == 0)
-   {
-     *sleep_start_time = time_info.sleep_start_time;
-	 *wakeup_time = time_info.wakeup_time;
-	 return 0;
-   }
-   else
-   {
-      return -1;
-   }
-   
 
-}
-
-int libautosuspend_get_feedback(struct time_info_t *time_info, int *timeout)
+int libautosuspend_get_feedback(struct time_info_t *time_info)
 {
     // char value[15]="feedback";
     // char res[15];    
@@ -434,25 +408,25 @@
 
     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;
-        }
+    // 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);
+    //     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)
@@ -489,6 +463,32 @@
 }
 
 
+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;
+    
+   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()
 // {