blob: abf1a0d4d1e917f7ec6b694d59a451f1cd4b48c2 [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>
16#include "gsw_pm.h"
17
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
34#define LOGV(fmt, args ...) \
35 do{ \
36 char *file_ptr_1001 = __FILE__; \
37 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
38 char line_1001[10] = {0}; \
39 sprintf(line_1001, "%d", __LINE__); \
40 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
41 if(*ptr_1001 == '/') \
42 break; \
43 ptr_1001--; \
44 } \
45 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
46 } while(0)
47
48#define LOGI(fmt, args...) \
49 do{ \
50 char *file_ptr_1001 = __FILE__; \
51 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
52 char line_1001[10] = {0}; \
53 sprintf(line_1001, "%d", __LINE__); \
54 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
55 if(*ptr_1001 == '/') \
56 break; \
57 ptr_1001--; \
58 } \
59 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
60 } while(0)
61
62#define LOGD(fmt, args...) \
63 do{ \
64 char *file_ptr_1001 = __FILE__; \
65 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
66 char line_1001[10] = {0}; \
67 sprintf(line_1001, "%d", __LINE__); \
68 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
69 if(*ptr_1001 == '/') \
70 break; \
71 ptr_1001--; \
72 } \
73 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
74 } while(0)
75
76#define LOGW(fmt, args...) \
77 do{ \
78 char *file_ptr_1001 = __FILE__; \
79 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
80 char line_1001[10] = {0}; \
81 sprintf(line_1001, "%d", __LINE__); \
82 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
83 if(*ptr_1001 == '/') \
84 break; \
85 ptr_1001--; \
86 } \
87 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
88 } while(0)
89
90#define LOGE(fmt, args...) \
91 do{ \
92 char *file_ptr_1001 = __FILE__; \
93 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
94 char line_1001[10] = {0}; \
95 sprintf(line_1001, "%d", __LINE__); \
96 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
97 if(*ptr_1001 == '/') \
98 break; \
99 ptr_1001--; \
100 } \
101 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: " fmt, ptr_1001 + 1, line_1001, ##args); \
102 } while(0)
103
104
105#define GSW_HAL_SUCCESS 0
106#define GSW_HAL_FAIL -1 //表示失败(通用性)
107#define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
108#define LOCK_MAX_SIZE 129
109
110typedef void (*mbtk_lpm_handler_t)(int32_t wakeup_in);
111typedef void (*mbtk_log)(int level, const char *format,...);
112
113int (*mbtk_autosuspend_enable)(char);
114int (*mbtk_wakelock_create)(const char* name , size_t);
115int (*mbtk_wakelock_lock)(int);
116int (*mbtk_wakelock_unlock)(int);
117int (*mbtk_wakelock_destroy)(int);
118int (*mbtk_lpm_init)(mbtk_lpm_handler_t );
119int lock_fd = -1;
120
121typedef struct
122{
123 int fd;
124 char *name;
125} mbtk_lock_name_s;
126static mbtk_lock_name_s lock_name[LOCK_MAX_SIZE]={0};
127
128static mbtk_log fun_ptr_log = NULL;
129void *dlHandle_sleep = NULL;
130char *lynqLib_sleep = "/lib/libmbtk_lib.so";
131
132
133/**
134* @brief Enable autosleep
135* @param void
136* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
137*/
138
139static int handle()
140{
141 if(dlHandle_sleep == NULL || fun_ptr_log == NULL)
142 {
143 dlHandle_sleep = dlopen(lynqLib_sleep, RTLD_NOW);
144 fun_ptr_log = (mbtk_log)dlsym(dlHandle_sleep, "mbtk_log");
145 if(fun_ptr_log == NULL || dlHandle_sleep == NULL)
146 {
147 return GSW_HAL_FAIL;
148 }
149 }
150 return GSW_HAL_SUCCESS;
151}
152
153int32_t gsw_autosleep_enable(void)
154{
155 int ret;
156 if (handle())
157 return GSW_HAL_FAIL;
158 mbtk_autosuspend_enable=(int(*)(char))dlsym(dlHandle_sleep, "mbtk_autosuspend_enable");
159 ret = mbtk_autosuspend_enable(1);
160 if(ret < 0)
161 {
162 LOGE("mbtk_autosuspend_enable FAIL.\n");
163 dlclose(dlHandle_sleep);
164 dlHandle_sleep = NULL;
165 return GSW_HAL_FAIL;
166 }
167 dlclose(dlHandle_sleep);
168 dlHandle_sleep = NULL;
169 return GSW_HAL_SUCCESS;
170}
171
172/**
173* @brief Disable autosleep
174* @param void
175* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
176*/
177int32_t gsw_autosleep_disenable(void)
178{
179 int ret;
180 if (handle())
181 return GSW_HAL_FAIL;
182 mbtk_autosuspend_enable=(int(*)(char))dlsym(dlHandle_sleep, "mbtk_autosuspend_enable");
183 ret = mbtk_autosuspend_enable(0);
184 if(ret < 0)
185 {
186 LOGE("mbtk_autosuspend_enable FAIL.\n");
187 dlclose(dlHandle_sleep);
188 dlHandle_sleep = NULL;
189 return GSW_HAL_FAIL;
190 }
191 dlclose(dlHandle_sleep);
192 dlHandle_sleep = NULL;
193 return GSW_HAL_SUCCESS;
194}
195
196/**
197* @brief Init power manager module
198* @param [in]GSW_PM_WAKEUPCALLBACKHandler wakeup_callback
199* @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
200*/
201int32_t gsw_pm_sdk_init(GSW_PM_WAKEUPCALLBACK wakeup_callback)
202{
203 int ret;
204 if (handle())
205 return GSW_HAL_FAIL;
206 mbtk_lpm_init=(int(*)(mbtk_lpm_handler_t))dlsym(dlHandle_sleep, "mbtk_lpm_init");
207 ret = mbtk_lpm_init((mbtk_lpm_handler_t)wakeup_callback);
208 if(ret < 0)
209 {
210 LOGE("mbtk_lpm_init FAIL.\n");
211 dlclose(dlHandle_sleep);
212 dlHandle_sleep = NULL;
213 return GSW_HAL_FAIL;
214 }
215 return GSW_HAL_SUCCESS;
216}
217
218/**
219* @brief Release wake lock, enter sleep
220* @param [in]int32_t wakeup_in
221* @retval int
222*/
223int32_t gsw_pm_enter_sleep(const char *gsw_wakelock_name)
224{
225 int ret;
226 int i;
227 if (handle())
228 return GSW_HAL_FAIL;
229 if (gsw_wakelock_name == NULL)
230 return GSW_HAL_FAIL;
231 for(i=0 ;i<LOCK_MAX_SIZE;i++)
232 {
233 if(lock_name[i].name != NULL && strcmp(lock_name[i].name, gsw_wakelock_name) == 0)
234 {
235 lock_fd = lock_name[i].fd;
236 break;
237 }
238 }
239 if (i >= LOCK_MAX_SIZE)
240 {
241 LOGE("mbtk_wakelock_lock not create.\n");
242 return GSW_HAL_FAIL;
243 }
244
245 mbtk_wakelock_unlock=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_unlock");
246 ret = mbtk_wakelock_unlock(lock_fd);
247 if(ret < 0)
248 {
249 LOGE("mbtk_wakelock_unlock FAIL.\n");
250 dlclose(dlHandle_sleep);
251 dlHandle_sleep = NULL;
252 return GSW_HAL_FAIL;
253 }
254
255 mbtk_wakelock_destroy=(int(*)(int))dlsym(dlHandle_sleep, "mbtk_wakelock_destroy");
256 ret = mbtk_wakelock_destroy(lock_fd);
257 if(ret < 0)
258 {
259 LOGE("mbtk_wakelock_destroy FAIL.\n");
260 dlclose(dlHandle_sleep);
261 dlHandle_sleep = NULL;
262 return GSW_HAL_FAIL;
263 }
264 if (lock_name[i].name != NULL)
265 {
266 free(lock_name[i].name);
267 lock_name[i].name = NULL;
268 lock_name[i].fd = -1;
269 }
270
271 dlclose(dlHandle_sleep);
272 dlHandle_sleep = NULL;
273 return GSW_HAL_SUCCESS;
274}
275
276/**
277* @brief Creat wakeup lock
278* @param [in]int32_t wakeup_in
279* @retval int
280*/
281int32_t gsw_pm_exit_sleep(const char *gsw_wakelock_name)
282{
283 int ret;
284 int i;
285 if (handle())
286 return GSW_HAL_FAIL;
287 if (gsw_wakelock_name == NULL)
288 return GSW_HAL_FAIL;
289 mbtk_wakelock_create=(int(*)(const char* name , size_t))dlsym(dlHandle_sleep, "mbtk_wakelock_create");
290 lock_fd = mbtk_wakelock_create(gsw_wakelock_name, strlen(gsw_wakelock_name));
291 if(lock_fd < 0)
292 {
293 LOGE("mbtk_wakelock_create FAIL.\n");
294 dlclose(dlHandle_sleep);
295 dlHandle_sleep = NULL;
296 return GSW_HAL_FAIL;
297 }
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");
304 dlclose(dlHandle_sleep);
305 dlHandle_sleep = NULL;
306 return GSW_HAL_FAIL;
307 }
308 for(i=0 ;i<LOCK_MAX_SIZE;i++)
309 {
310 if(lock_name[i].name == NULL)
311 break;
312 }
313 lock_name[i].name = malloc(strlen(gsw_wakelock_name)+1);
314 if (lock_name[i].name == NULL)
315 {
316 LOGE("mbtk_wakelock_lock remeber FAIL.\n");
317 return GSW_HAL_FAIL;
318 }
319 memcpy(lock_name[i].name, gsw_wakelock_name, strlen(gsw_wakelock_name)+1);
320 lock_name[i].fd = lock_fd;
321
322 dlclose(dlHandle_sleep);
323 dlHandle_sleep = NULL;
324 return GSW_HAL_SUCCESS;
325}
326
327/**
328* @brief Module log disk drop, used when restarting or hibernating
329* @param [in]void
330* @retval void
331*/
332void gsw_modem_log_sync(void)
333{
334 FILE *fp;
335 char command[256];
336 char buffer[256];
337 int pid = -1;
338 const char *process_name = "mbtk_logd";
339
340 snprintf(command, sizeof(command), "pgrep %s", process_name);
341 fp = popen(command, "r");
342 if (fp == NULL)
343 {
344 perror("error comman");
345 return;
346 }
347
348 if (fgets(buffer, sizeof(buffer), fp) != NULL)
349 {
350 pid = atoi(buffer);
351 }
352 pclose(fp);
353
354 if (pid != -1)
355 {
356 if (kill(pid, SIGTERM) == -1)
357 {
358 perror("send SIGTERM signal failed");
359 return;
360 }
361 }
362 return;
363}