[Feature][ZXW-202][autosuspend]add init and deinit

Only Configure: No
Affected branch: master
Affected module: autosuspend
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: Yes

Change-Id: I67c92bc675575436bac058d7f8c409c948926b01
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
index e82e2c0..5a88ef7 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <pthread.h>
 #include<unistd.h>
+#include <cutils/uevent.h>
 
 #include <lynq_autosuspend.h>
 #include "lynq-qser-autosuspend.h"
@@ -25,6 +26,20 @@
 
 #define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND"
 #define FILE_LOCK_TABLE "/tmp/.lock_table"
+#define UEVENT_MSG_LEN 128
+
+
+struct dtr_uevent {
+    const char *action;
+    const char *path;
+    const char *subsystem;
+    const char *usbevent;
+};
+
+static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent);
+
+
+qser_lpm_Handler_t g_lpm_handler = NULL;
 
 
 LOCK_TABLE lock_status;
@@ -32,6 +47,7 @@
 
 int file_fd;
 int first_run = 0;
+pthread_t thid;
 
 
 int lock_table_init(void)
@@ -218,19 +234,84 @@
     return ret;
 }
 
+static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent)
+{
+    dtr_uevent->action = "";
+    dtr_uevent->path = "";
+    dtr_uevent->subsystem = "";
+    dtr_uevent->usbevent = "";
+
+    while (*msg) {
+        if (!strncmp(msg, "ACTION=", 7))
+        {
+            msg += 7;
+            dtr_uevent->action = msg;
+        }
+        else if (!strncmp(msg, "DEVPATH=", 8))
+        {
+            msg += 8;
+            dtr_uevent->path = msg;
+        }
+        else if (!strncmp(msg, "SUBSYSTEM=", 10))
+        {
+            msg += 10;
+            dtr_uevent->subsystem = msg;
+        }
+        else if (!strncmp(msg, "USBEVENT=", 9)) {
+            msg += 9;
+            dtr_uevent->usbevent = msg;
+        }
+
+        while(*msg++);
+    }
+
+    LYINFLOG("event { '%s', '%s', '%s', '%s'}\n",
+                    dtr_uevent->action, dtr_uevent->path, dtr_uevent->subsystem,dtr_uevent->usbevent);
+}
+
+
+
+void *check_dtr(void * arg)
+{
+    int device_fd;
+    qser_lpm_edge_t lpm_edge;
+    char msg[UEVENT_MSG_LEN+2];
+    struct dtr_uevent dtr_uevent;
+    int n;
+    int i;
+ 
+    device_fd = uevent_open_socket(4*UEVENT_MSG_LEN, true);
+    if(device_fd < 0)
+    {
+        LYERRLOG("uevent_open_socket fail\n");
+        return -1;
+    }
+    while(1) 
+    {
+        n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN);
+        msg[n] = '\0';
+        msg[n+1] = '\0';
+        parse_event(msg, &dtr_uevent);
+        if((!strcmp(dtr_uevent.subsystem, "xp2xp")) && (!strcmp(dtr_uevent.action, "online")))
+        {
+            g_lpm_handler(lpm_edge);
+        }
+    }
+ 
+    return 0;
+}
+
+
 int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
 {
     int ret;
-    ret = system("uci set lynq_uci.lynq_autosuspend.init='1'");
-    system("uci commit");
+    int num;
+
+    g_lpm_handler = qser_lpm_handler;
+    ret = pthread_create(&thid,NULL,check_dtr,NULL);
     if(ret != 0)
     {
-        LYINFLOG("uci set fail");
-    }
-    ret = system("./etc/init.d/lynq-autosuspend.sh restart");
-    if(ret != 0)
-    {
-        LYINFLOG("restart service fail");
+        LYERRLOG("restart service fail");
     }
     return ret;
 }
@@ -239,18 +320,17 @@
 int qser_lpm_deinit(void)
 {
     int ret;
-    system("uci set lynq_uci.lynq_autosuspend.init='0'");
-    system("uci commit");
-    if(ret != 0)
+    ret = pthread_cancel(thid);
+    if(!ret)
     {
-        LYINFLOG("uci set fail");
+        LYERRLOG("pthread cancel success, lpm deinit success\n");
     }
-    system("./etc/init.d/lynq-autosuspend.sh restarts");
-    if(ret != 0)
+    else
     {
-        LYINFLOG("restart service fail");
+        LYERRLOG("pthread cancel fail, lpm deinit fail\n");
     }
-    return 0;
+    return ret;
+
 }