blob: 25acf7ae75cd011acf90f752373e36c52968d2d3 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdio.h>
2#include <string.h>
3#include <strings.h>
4#include <stdlib.h>
5#include <errno.h>
6#include <termios.h>
7#include <fcntl.h>
8#include <signal.h>
9#include <sys/types.h>
10#include <unistd.h>
11#include <pthread.h>
12#include <time.h>
13#include <sys/ioctl.h>
14#include <dlfcn.h>
15#include <stdint.h>
hong.liucd370792025-05-28 06:29:19 -070016#include "gsw_pm_interface.h"
b.liu68a94c92025-05-24 12:53:41 +080017
18#ifndef LOG_ERR_LEVEL
19#define LOG_ERR_LEVEL 3 /* error conditions */
20#endif
21#ifndef LOG_WARN_LEVEL
22#define LOG_WARN_LEVEL 4 /* warning conditions */
23#endif
24#ifndef LOG_INFO_LEVEL
25#define LOG_INFO_LEVEL 6 /* informational */
26#endif
27#ifndef LOG_DEBUG_LEVEL
28#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
29#endif
30#ifndef LOG_VERBOSE_LEVEL
31#define LOG_VERBOSE_LEVEL 8
32#endif
33
l.yang6a42e4d2025-05-28 01:04:20 -070034#define GSW_PM "[HAL][GSW_PM]"
35
b.liu68a94c92025-05-24 12:53:41 +080036#define LOGV(fmt, args ...) \
37 do{ \
38 char *file_ptr_1001 = __FILE__; \
39 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
40 char line_1001[10] = {0}; \
41 sprintf(line_1001, "%d", __LINE__); \
42 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
43 if(*ptr_1001 == '/') \
44 break; \
45 ptr_1001--; \
46 } \
l.yang6a42e4d2025-05-28 01:04:20 -070047 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_PM"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080048 } while(0)
49
50#define LOGI(fmt, args...) \
51 do{ \
52 char *file_ptr_1001 = __FILE__; \
53 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
54 char line_1001[10] = {0}; \
55 sprintf(line_1001, "%d", __LINE__); \
56 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
57 if(*ptr_1001 == '/') \
58 break; \
59 ptr_1001--; \
60 } \
l.yang6a42e4d2025-05-28 01:04:20 -070061 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_PM"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080062 } while(0)
63
64#define LOGD(fmt, args...) \
65 do{ \
66 char *file_ptr_1001 = __FILE__; \
67 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
68 char line_1001[10] = {0}; \
69 sprintf(line_1001, "%d", __LINE__); \
70 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
71 if(*ptr_1001 == '/') \
72 break; \
73 ptr_1001--; \
74 } \
l.yang6a42e4d2025-05-28 01:04:20 -070075 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_PM"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080076 } while(0)
77
78#define LOGW(fmt, args...) \
79 do{ \
80 char *file_ptr_1001 = __FILE__; \
81 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
82 char line_1001[10] = {0}; \
83 sprintf(line_1001, "%d", __LINE__); \
84 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
85 if(*ptr_1001 == '/') \
86 break; \
87 ptr_1001--; \
88 } \
l.yang6a42e4d2025-05-28 01:04:20 -070089 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_PM"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080090 } while(0)
91
92#define LOGE(fmt, args...) \
93 do{ \
94 char *file_ptr_1001 = __FILE__; \
95 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
96 char line_1001[10] = {0}; \
97 sprintf(line_1001, "%d", __LINE__); \
98 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
99 if(*ptr_1001 == '/') \
100 break; \
101 ptr_1001--; \
102 } \
l.yang6a42e4d2025-05-28 01:04:20 -0700103 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: "GSW_PM"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +0800104 } while(0)
105
106
hong.liucd370792025-05-28 06:29:19 -0700107
b.liu68a94c92025-05-24 12:53:41 +0800108#define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
109#define LOCK_MAX_SIZE 129
hong.liucd370792025-05-28 06:29:19 -0700110typedef void (*GSW_PM_WAKEUPCALLBACK)(int32_t wakeup_in);
b.liu68a94c92025-05-24 12:53:41 +0800111typedef void (*mbtk_lpm_handler_t)(int32_t wakeup_in);
112typedef void (*mbtk_log)(int level, const char *format,...);
113
114int (*mbtk_autosuspend_enable)(char);
115int (*mbtk_wakelock_create)(const char* name , size_t);
116int (*mbtk_wakelock_lock)(int);
117int (*mbtk_wakelock_unlock)(int);
118int (*mbtk_wakelock_destroy)(int);
119int (*mbtk_lpm_init)(mbtk_lpm_handler_t );
120int lock_fd = -1;
121
122typedef struct
123{
124 int fd;
125 char *name;
126} mbtk_lock_name_s;
127static mbtk_lock_name_s lock_name[LOCK_MAX_SIZE]={0};
128
129static mbtk_log fun_ptr_log = NULL;
130void *dlHandle_sleep = NULL;
131char *lynqLib_sleep = "/lib/libmbtk_lib.so";
132
133
134/**
135* @brief Enable autosleep
136* @param void
hong.liucd370792025-05-28 06:29:19 -0700137* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
b.liu68a94c92025-05-24 12:53:41 +0800138*/
139
140static int handle()
141{
142 if(dlHandle_sleep == NULL || fun_ptr_log == NULL)
143 {
144 dlHandle_sleep = dlopen(lynqLib_sleep, RTLD_NOW);
145 fun_ptr_log = (mbtk_log)dlsym(dlHandle_sleep, "mbtk_log");
146 if(fun_ptr_log == NULL || dlHandle_sleep == NULL)
147 {
hong.liucd370792025-05-28 06:29:19 -0700148 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800149 }
150 }
151 return GSW_HAL_SUCCESS;
152}
153
154int32_t gsw_autosleep_enable(void)
155{
156 int ret;
157 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700158 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800159 mbtk_autosuspend_enable=(int(*)(char))dlsym(dlHandle_sleep, "mbtk_autosuspend_enable");
160 ret = mbtk_autosuspend_enable(1);
161 if(ret < 0)
162 {
163 LOGE("mbtk_autosuspend_enable FAIL.\n");
164 dlclose(dlHandle_sleep);
165 dlHandle_sleep = NULL;
hong.liucd370792025-05-28 06:29:19 -0700166 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800167 }
168 dlclose(dlHandle_sleep);
169 dlHandle_sleep = NULL;
170 return GSW_HAL_SUCCESS;
171}
172
173/**
174* @brief Disable autosleep
175* @param void
hong.liucd370792025-05-28 06:29:19 -0700176* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
b.liu68a94c92025-05-24 12:53:41 +0800177*/
178int32_t gsw_autosleep_disenable(void)
179{
180 int ret;
181 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700182 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800183 mbtk_autosuspend_enable=(int(*)(char))dlsym(dlHandle_sleep, "mbtk_autosuspend_enable");
184 ret = mbtk_autosuspend_enable(0);
185 if(ret < 0)
186 {
187 LOGE("mbtk_autosuspend_enable FAIL.\n");
188 dlclose(dlHandle_sleep);
189 dlHandle_sleep = NULL;
hong.liucd370792025-05-28 06:29:19 -0700190 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800191 }
192 dlclose(dlHandle_sleep);
193 dlHandle_sleep = NULL;
194 return GSW_HAL_SUCCESS;
195}
196
197/**
198* @brief Init power manager module
199* @param [in]GSW_PM_WAKEUPCALLBACKHandler wakeup_callback
hong.liucd370792025-05-28 06:29:19 -0700200* @retval GSW_HAL_SUCCESS\GSW_HAL_NORMAL_FAIL
b.liu68a94c92025-05-24 12:53:41 +0800201*/
202int32_t gsw_pm_sdk_init(GSW_PM_WAKEUPCALLBACK wakeup_callback)
203{
204 int ret;
205 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700206 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800207 mbtk_lpm_init=(int(*)(mbtk_lpm_handler_t))dlsym(dlHandle_sleep, "mbtk_lpm_init");
208 ret = mbtk_lpm_init((mbtk_lpm_handler_t)wakeup_callback);
209 if(ret < 0)
210 {
211 LOGE("mbtk_lpm_init FAIL.\n");
212 dlclose(dlHandle_sleep);
213 dlHandle_sleep = NULL;
hong.liucd370792025-05-28 06:29:19 -0700214 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800215 }
216 return GSW_HAL_SUCCESS;
217}
218
219/**
220* @brief Release wake lock, enter sleep
221* @param [in]int32_t wakeup_in
222* @retval int
223*/
224int32_t gsw_pm_enter_sleep(const char *gsw_wakelock_name)
225{
226 int ret;
227 int i;
228 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700229 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800230 if (gsw_wakelock_name == NULL)
hong.liucd370792025-05-28 06:29:19 -0700231 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800232 for(i=0 ;i<LOCK_MAX_SIZE;i++)
233 {
234 if(lock_name[i].name != NULL && strcmp(lock_name[i].name, gsw_wakelock_name) == 0)
235 {
236 lock_fd = lock_name[i].fd;
237 break;
238 }
239 }
240 if (i >= LOCK_MAX_SIZE)
241 {
242 LOGE("mbtk_wakelock_lock not create.\n");
hong.liucd370792025-05-28 06:29:19 -0700243 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800244 }
245
246 mbtk_wakelock_unlock=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_unlock");
247 ret = mbtk_wakelock_unlock(lock_fd);
248 if(ret < 0)
249 {
250 LOGE("mbtk_wakelock_unlock FAIL.\n");
251 dlclose(dlHandle_sleep);
252 dlHandle_sleep = NULL;
hong.liucd370792025-05-28 06:29:19 -0700253 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800254 }
255
256 mbtk_wakelock_destroy=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_destroy");
257 ret = mbtk_wakelock_destroy(lock_fd);
258 if(ret < 0)
259 {
260 LOGE("mbtk_wakelock_destroy FAIL.\n");
261 dlclose(dlHandle_sleep);
262 dlHandle_sleep = NULL;
hong.liucd370792025-05-28 06:29:19 -0700263 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800264 }
265 if (lock_name[i].name != NULL)
266 {
267 free(lock_name[i].name);
268 lock_name[i].name = NULL;
269 lock_name[i].fd = -1;
270 }
271
272 dlclose(dlHandle_sleep);
273 dlHandle_sleep = NULL;
274 return GSW_HAL_SUCCESS;
275}
276
277/**
278* @brief Creat wakeup lock
279* @param [in]int32_t wakeup_in
280* @retval int
281*/
282int32_t gsw_pm_exit_sleep(const char *gsw_wakelock_name)
283{
284 int ret;
285 int i;
286 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700287 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800288 if (gsw_wakelock_name == NULL)
hong.liucd370792025-05-28 06:29:19 -0700289 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800290 mbtk_wakelock_create=(int(*)(const char* name , size_t))dlsym(dlHandle_sleep, "mbtk_wakelock_create");
291 lock_fd = mbtk_wakelock_create(gsw_wakelock_name, strlen(gsw_wakelock_name));
292 if(lock_fd < 0)
293 {
294 LOGE("mbtk_wakelock_create FAIL.\n");
hong.liucd370792025-05-28 06:29:19 -0700295 ret = GSW_HAL_NORMAL_FAIL;
296 goto end;
b.liu68a94c92025-05-24 12:53:41 +0800297 }
298 mbtk_wakelock_lock=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_lock");
299
300 ret = mbtk_wakelock_lock(lock_fd);
301 if(ret < 0)
302 {
303 LOGE("mbtk_wakelock_lock FAIL.\n");
hong.liucd370792025-05-28 06:29:19 -0700304 ret = GSW_HAL_NORMAL_FAIL;
305 goto end;
b.liu68a94c92025-05-24 12:53:41 +0800306 }
307 for(i=0 ;i<LOCK_MAX_SIZE;i++)
308 {
309 if(lock_name[i].name == NULL)
310 break;
311 }
hong.liucd370792025-05-28 06:29:19 -0700312 if(i == LOCK_MAX_SIZE)
313 {
314 LOGE("mbtk_wakelock_lock not space FAIL.\n");
315 ret = GSW_HAL_NORMAL_FAIL;
316 goto end;
317 }
b.liu68a94c92025-05-24 12:53:41 +0800318 lock_name[i].name = malloc(strlen(gsw_wakelock_name)+1);
319 if (lock_name[i].name == NULL)
320 {
321 LOGE("mbtk_wakelock_lock remeber FAIL.\n");
hong.liucd370792025-05-28 06:29:19 -0700322 ret = GSW_HAL_NORMAL_FAIL;
323 goto end;
b.liu68a94c92025-05-24 12:53:41 +0800324 }
325 memcpy(lock_name[i].name, gsw_wakelock_name, strlen(gsw_wakelock_name)+1);
326 lock_name[i].fd = lock_fd;
hong.liucd370792025-05-28 06:29:19 -0700327 ret = GSW_HAL_SUCCESS;
328end:
b.liu68a94c92025-05-24 12:53:41 +0800329 dlclose(dlHandle_sleep);
330 dlHandle_sleep = NULL;
hong.liucd370792025-05-28 06:29:19 -0700331 return ret;
b.liu68a94c92025-05-24 12:53:41 +0800332}
333
334/**
335* @brief Module log disk drop, used when restarting or hibernating
336* @param [in]void
337* @retval void
338*/
339void gsw_modem_log_sync(void)
340{
341 FILE *fp;
342 char command[256];
343 char buffer[256];
344 int pid = -1;
345 const char *process_name = "mbtk_logd";
346
347 snprintf(command, sizeof(command), "pgrep %s", process_name);
348 fp = popen(command, "r");
349 if (fp == NULL)
350 {
351 perror("error comman");
352 return;
353 }
354
355 if (fgets(buffer, sizeof(buffer), fp) != NULL)
356 {
357 pid = atoi(buffer);
358 }
359 pclose(fp);
360
361 if (pid != -1)
362 {
363 if (kill(pid, SIGTERM) == -1)
364 {
365 perror("send SIGTERM signal failed");
366 return;
367 }
368 }
369 return;
370}