[Feature][ZXW-68][AUTOSUSPEND]add wakelock api and autosuspend api

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

Change-Id: I389ac6629a628d3cf197bea5809882c25f408439
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
new file mode 100755
index 0000000..b6fcdea
--- /dev/null
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-autosuspend/lynq-qser-autosuspend.cpp
@@ -0,0 +1,337 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <pthread.h>
+
+#include<unistd.h>
+
+
+
+#include <lynq_autosuspend.h>
+#include "lynq-qser-autosuspend.h"
+#include "liblog/lynq_deflog.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+extern int lynq_autosleep_enable(void);
+extern int lynq_autosleep_disable(void);
+extern int release_wake_lock(char *name);
+extern int acquire_wake_lock(int lock, char *name);
+
+#define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND"
+#define FILE_LOCK_TABLE "/tmp/.lock_table"
+
+
+LOCK_TABLE lock_status;
+
+
+int file_fd;
+int first_run = 0;
+
+
+int lock_table_init(void)
+{
+    int ret;
+    int err;
+
+    file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
+    if(file_fd < 0)
+    {
+        err = errno;
+        LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
+        return -err;
+    }
+    memset(&lock_status, 0, sizeof(lock_status));
+    ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
+    if(ret <= 0)
+    {
+        LYINFLOG("write fail\n");
+        close(file_fd);
+        return -1;
+    }
+    sync();
+    close(file_fd);
+    return 0;
+}
+
+
+int read_lock_table(void)
+{
+    int err;
+    int ret;
+    int i;
+    LYLOGSET(LOG_INFO);
+    LYLOGEINIT(USER_LOG_TAG);
+
+    if(access(FILE_LOCK_TABLE,0) < 0)
+    {
+        ret = lock_table_init();
+        if(ret < 0)
+        {
+            return -2;
+        }
+    }
+
+    file_fd = open(FILE_LOCK_TABLE,O_RDWR);
+    if(file_fd < 0)
+    {
+        err = errno;
+        LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
+        return -2;
+    }
+
+    memset(&lock_status, 0, sizeof(lock_status));
+    lseek(file_fd,0,SEEK_SET);
+    ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
+    LYINFLOG("read ret=%d\n", ret);
+    if(ret <= 0)
+    {
+        close(file_fd);
+        return -2;
+    }
+
+    for(i=0;i<MAX_LOCK_NUM;i++)
+    {
+        if(strlen(lock_status.lock_name[i]) != 0)
+        {
+            LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
+        }
+    }
+
+    close(file_fd);
+    return 0;
+}
+
+
+int save_lock_table(void)
+{
+    int err;
+    int ret;
+    file_fd = open(FILE_LOCK_TABLE,O_RDWR);
+    if(file_fd < 0)
+    {
+        err = errno;
+        LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
+        return -3;
+    }
+    LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
+    ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
+    LYINFLOG("write ret=%d\n", ret);
+    if(ret <= 0)
+    {
+        LYINFLOG("write fail\n");
+        close(file_fd);
+        return -3;
+    }
+    sync();
+    close(file_fd);
+
+    return 0;
+}
+
+
+int check_lock(char *name)
+{
+    int j;
+    int num;
+    for(j=0;j<MAX_LOCK_NUM;j++)
+    {
+        if(strcmp(lock_status.lock_name[j], name) == 0)
+        {
+            num = j;
+            break;
+        }
+    }
+
+    if(j < MAX_LOCK_NUM)
+    {
+        return num;
+    }
+
+    return -1;
+}
+
+
+int add_lock(char *name)
+{
+    int ret;
+    int i = 0;
+    int num;
+    int check_flag;
+    
+    LYINFLOG("name:%s\n", name);
+    ret = read_lock_table();
+    LYINFLOG("read_lock_table ret = %d\n", ret);
+    if(ret <0)
+    {
+        return ret;
+    }
+
+    check_flag = check_lock(name);
+
+    if(check_flag < 0)
+    {
+        for(i=0;i<MAX_LOCK_NUM;i++)
+        {
+            if(strlen(lock_status.lock_name[i]) == 0)
+            {
+                strcpy(lock_status.lock_name[i], name);
+                LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
+                break;
+            }
+        }
+        if(i == MAX_LOCK_NUM)
+        {
+            return -1;
+        }
+        else
+        {
+            num = i;
+        }
+    }
+    else
+    {
+        num = check_flag;
+    }
+
+    LYINFLOG("num = %d\n", num);
+    ret = save_lock_table();
+    if(ret < 0)
+    {
+        return ret;
+    }
+    return num;
+}
+
+int delete_lock(int fd)
+{
+    int ret;
+    int i;
+    ret = read_lock_table();
+    memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
+    ret = save_lock_table();
+    return ret;
+}
+
+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");
+    if(ret != 0)
+    {
+        LYINFLOG("uci set fail");
+    }
+    ret = system("./etc/init.d/lynq-autosuspend.sh restart");
+    if(ret != 0)
+    {
+        LYINFLOG("restart service fail");
+    }
+    return ret;
+}
+
+
+int qser_lpm_deinit(void)
+{
+    int ret;
+    system("uci set lynq_uci.lynq_autosuspend.init='0'");
+    system("uci commit");
+    if(ret != 0)
+    {
+        LYINFLOG("uci set fail");
+    }
+    system("./etc/init.d/lynq-autosuspend.sh restarts");
+    if(ret != 0)
+    {
+        LYINFLOG("restart service fail");
+    }
+    return 0;
+}
+
+int qser_autosuspend_enable(char enable)
+{
+    int ret;
+    if(enable == '0')
+    {
+        ret = lynq_autosleep_disable();
+
+    }
+    else if(enable == '1')
+    {
+        ret = lynq_autosleep_enable();
+    }
+    else
+    {
+        ret = -1;
+
+    }
+    return ret;
+
+}
+
+int qser_wakelock_create(const char *name, size_t len)
+{
+    int ret;
+    if(name == NULL)
+    {
+        return -1;
+    }
+    LYINFLOG("%s\n", name);
+    ret = add_lock(name);
+    return ret;
+}
+
+int qser_wakelock_lock(int fd)
+{
+
+    int ret;
+    if(fd < 0 || fd >= MAX_LOCK_NUM)
+    {
+        return -4;
+    }
+    ret = read_lock_table();
+    ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
+    return ret;
+}
+
+int qser_wakelock_unlock(int fd)
+{
+    int ret;
+    if(fd < 0 || fd >= MAX_LOCK_NUM)
+    {
+        return -4;
+    }
+    ret = read_lock_table();
+    if(strlen(lock_status.lock_name[fd]) == 0)
+    {
+        LYINFLOG("%d is null\n", fd);
+        return -1;
+    }
+    ret = release_wake_lock(lock_status.lock_name[fd]);
+    return ret;
+}
+
+int qser_wakelock_destroy(int fd)
+{
+    int ret;
+    if(fd < 0 || fd >= MAX_LOCK_NUM)
+    {
+        return -4;
+    }
+    ret = delete_lock(fd);
+    return ret;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+
+