Fix mbtk from v1265 GSW
Change-Id: I5d8d395616f284bc74c8b9448cfa347164b5a668
diff --git a/mbtk/libgsw_lib/gsw_pm.c b/mbtk/libgsw_lib/gsw_pm.c
new file mode 100755
index 0000000..abf1a0d
--- /dev/null
+++ b/mbtk/libgsw_lib/gsw_pm.c
@@ -0,0 +1,363 @@
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <termios.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <time.h>
+#include <sys/ioctl.h>
+#include <dlfcn.h>
+#include <stdint.h>
+#include "gsw_pm.h"
+
+#ifndef LOG_ERR_LEVEL
+#define LOG_ERR_LEVEL 3 /* error conditions */
+#endif
+#ifndef LOG_WARN_LEVEL
+#define LOG_WARN_LEVEL 4 /* warning conditions */
+#endif
+#ifndef LOG_INFO_LEVEL
+#define LOG_INFO_LEVEL 6 /* informational */
+#endif
+#ifndef LOG_DEBUG_LEVEL
+#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
+#endif
+#ifndef LOG_VERBOSE_LEVEL
+#define LOG_VERBOSE_LEVEL 8
+#endif
+
+#define LOGV(fmt, args ...) \
+ do{ \
+ char *file_ptr_1001 = __FILE__; \
+ char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
+ char line_1001[10] = {0}; \
+ sprintf(line_1001, "%d", __LINE__); \
+ while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
+ if(*ptr_1001 == '/') \
+ break; \
+ ptr_1001--; \
+ } \
+ fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
+ } while(0)
+
+#define LOGI(fmt, args...) \
+ do{ \
+ char *file_ptr_1001 = __FILE__; \
+ char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
+ char line_1001[10] = {0}; \
+ sprintf(line_1001, "%d", __LINE__); \
+ while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
+ if(*ptr_1001 == '/') \
+ break; \
+ ptr_1001--; \
+ } \
+ fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
+ } while(0)
+
+#define LOGD(fmt, args...) \
+ do{ \
+ char *file_ptr_1001 = __FILE__; \
+ char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
+ char line_1001[10] = {0}; \
+ sprintf(line_1001, "%d", __LINE__); \
+ while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
+ if(*ptr_1001 == '/') \
+ break; \
+ ptr_1001--; \
+ } \
+ fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
+ } while(0)
+
+#define LOGW(fmt, args...) \
+ do{ \
+ char *file_ptr_1001 = __FILE__; \
+ char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
+ char line_1001[10] = {0}; \
+ sprintf(line_1001, "%d", __LINE__); \
+ while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
+ if(*ptr_1001 == '/') \
+ break; \
+ ptr_1001--; \
+ } \
+ fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
+ } while(0)
+
+#define LOGE(fmt, args...) \
+ do{ \
+ char *file_ptr_1001 = __FILE__; \
+ char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
+ char line_1001[10] = {0}; \
+ sprintf(line_1001, "%d", __LINE__); \
+ while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
+ if(*ptr_1001 == '/') \
+ break; \
+ ptr_1001--; \
+ } \
+ fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
+ } while(0)
+
+
+#define GSW_HAL_SUCCESS 0
+#define GSW_HAL_FAIL -1 //表示失败(通用性)
+#define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
+#define LOCK_MAX_SIZE 129
+
+typedef void (*mbtk_lpm_handler_t)(int32_t wakeup_in);
+typedef void (*mbtk_log)(int level, const char *format,...);
+
+int (*mbtk_autosuspend_enable)(char);
+int (*mbtk_wakelock_create)(const char* name , size_t);
+int (*mbtk_wakelock_lock)(int);
+int (*mbtk_wakelock_unlock)(int);
+int (*mbtk_wakelock_destroy)(int);
+int (*mbtk_lpm_init)(mbtk_lpm_handler_t );
+int lock_fd = -1;
+
+typedef struct
+{
+ int fd;
+ char *name;
+} mbtk_lock_name_s;
+static mbtk_lock_name_s lock_name[LOCK_MAX_SIZE]={0};
+
+static mbtk_log fun_ptr_log = NULL;
+void *dlHandle_sleep = NULL;
+char *lynqLib_sleep = "/lib/libmbtk_lib.so";
+
+
+/**
+* @brief Enable autosleep
+* @param void
+* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
+*/
+
+static int handle()
+{
+ if(dlHandle_sleep == NULL || fun_ptr_log == NULL)
+ {
+ dlHandle_sleep = dlopen(lynqLib_sleep, RTLD_NOW);
+ fun_ptr_log = (mbtk_log)dlsym(dlHandle_sleep, "mbtk_log");
+ if(fun_ptr_log == NULL || dlHandle_sleep == NULL)
+ {
+ return GSW_HAL_FAIL;
+ }
+ }
+ return GSW_HAL_SUCCESS;
+}
+
+int32_t gsw_autosleep_enable(void)
+{
+ int ret;
+ if (handle())
+ return GSW_HAL_FAIL;
+ mbtk_autosuspend_enable=(int(*)(char))dlsym(dlHandle_sleep, "mbtk_autosuspend_enable");
+ ret = mbtk_autosuspend_enable(1);
+ if(ret < 0)
+ {
+ LOGE("mbtk_autosuspend_enable FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_SUCCESS;
+}
+
+/**
+* @brief Disable autosleep
+* @param void
+* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
+*/
+int32_t gsw_autosleep_disenable(void)
+{
+ int ret;
+ if (handle())
+ return GSW_HAL_FAIL;
+ mbtk_autosuspend_enable=(int(*)(char))dlsym(dlHandle_sleep, "mbtk_autosuspend_enable");
+ ret = mbtk_autosuspend_enable(0);
+ if(ret < 0)
+ {
+ LOGE("mbtk_autosuspend_enable FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_SUCCESS;
+}
+
+/**
+* @brief Init power manager module
+* @param [in]GSW_PM_WAKEUPCALLBACKHandler wakeup_callback
+* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
+*/
+int32_t gsw_pm_sdk_init(GSW_PM_WAKEUPCALLBACK wakeup_callback)
+{
+ int ret;
+ if (handle())
+ return GSW_HAL_FAIL;
+ mbtk_lpm_init=(int(*)(mbtk_lpm_handler_t))dlsym(dlHandle_sleep, "mbtk_lpm_init");
+ ret = mbtk_lpm_init((mbtk_lpm_handler_t)wakeup_callback);
+ if(ret < 0)
+ {
+ LOGE("mbtk_lpm_init FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+ return GSW_HAL_SUCCESS;
+}
+
+/**
+* @brief Release wake lock, enter sleep
+* @param [in]int32_t wakeup_in
+* @retval int
+*/
+int32_t gsw_pm_enter_sleep(const char *gsw_wakelock_name)
+{
+ int ret;
+ int i;
+ if (handle())
+ return GSW_HAL_FAIL;
+ if (gsw_wakelock_name == NULL)
+ return GSW_HAL_FAIL;
+ for(i=0 ;i<LOCK_MAX_SIZE;i++)
+ {
+ if(lock_name[i].name != NULL && strcmp(lock_name[i].name, gsw_wakelock_name) == 0)
+ {
+ lock_fd = lock_name[i].fd;
+ break;
+ }
+ }
+ if (i >= LOCK_MAX_SIZE)
+ {
+ LOGE("mbtk_wakelock_lock not create.\n");
+ return GSW_HAL_FAIL;
+ }
+
+ mbtk_wakelock_unlock=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_unlock");
+ ret = mbtk_wakelock_unlock(lock_fd);
+ if(ret < 0)
+ {
+ LOGE("mbtk_wakelock_unlock FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+
+ mbtk_wakelock_destroy=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_destroy");
+ ret = mbtk_wakelock_destroy(lock_fd);
+ if(ret < 0)
+ {
+ LOGE("mbtk_wakelock_destroy FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+ if (lock_name[i].name != NULL)
+ {
+ free(lock_name[i].name);
+ lock_name[i].name = NULL;
+ lock_name[i].fd = -1;
+ }
+
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_SUCCESS;
+}
+
+/**
+* @brief Creat wakeup lock
+* @param [in]int32_t wakeup_in
+* @retval int
+*/
+int32_t gsw_pm_exit_sleep(const char *gsw_wakelock_name)
+{
+ int ret;
+ int i;
+ if (handle())
+ return GSW_HAL_FAIL;
+ if (gsw_wakelock_name == NULL)
+ return GSW_HAL_FAIL;
+ mbtk_wakelock_create=(int(*)(const char* name , size_t))dlsym(dlHandle_sleep, "mbtk_wakelock_create");
+ lock_fd = mbtk_wakelock_create(gsw_wakelock_name, strlen(gsw_wakelock_name));
+ if(lock_fd < 0)
+ {
+ LOGE("mbtk_wakelock_create FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+ mbtk_wakelock_lock=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_lock");
+
+ ret = mbtk_wakelock_lock(lock_fd);
+ if(ret < 0)
+ {
+ LOGE("mbtk_wakelock_lock FAIL.\n");
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_FAIL;
+ }
+ for(i=0 ;i<LOCK_MAX_SIZE;i++)
+ {
+ if(lock_name[i].name == NULL)
+ break;
+ }
+ lock_name[i].name = malloc(strlen(gsw_wakelock_name)+1);
+ if (lock_name[i].name == NULL)
+ {
+ LOGE("mbtk_wakelock_lock remeber FAIL.\n");
+ return GSW_HAL_FAIL;
+ }
+ memcpy(lock_name[i].name, gsw_wakelock_name, strlen(gsw_wakelock_name)+1);
+ lock_name[i].fd = lock_fd;
+
+ dlclose(dlHandle_sleep);
+ dlHandle_sleep = NULL;
+ return GSW_HAL_SUCCESS;
+}
+
+/**
+* @brief Module log disk drop, used when restarting or hibernating
+* @param [in]void
+* @retval void
+*/
+void gsw_modem_log_sync(void)
+{
+ FILE *fp;
+ char command[256];
+ char buffer[256];
+ int pid = -1;
+ const char *process_name = "mbtk_logd";
+
+ snprintf(command, sizeof(command), "pgrep %s", process_name);
+ fp = popen(command, "r");
+ if (fp == NULL)
+ {
+ perror("error comman");
+ return;
+ }
+
+ if (fgets(buffer, sizeof(buffer), fp) != NULL)
+ {
+ pid = atoi(buffer);
+ }
+ pclose(fp);
+
+ if (pid != -1)
+ {
+ if (kill(pid, SIGTERM) == -1)
+ {
+ perror("send SIGTERM signal failed");
+ return;
+ }
+ }
+ return;
+}
\ No newline at end of file