blob: 260dcb6867d681010450e6e57969d21ce09defac [file] [log] [blame]
jb.qi3a8298c2023-09-07 04:48:51 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
7#include <errno.h>
8#include <pthread.h>
jb.qie7aac6d2023-12-28 01:32:19 -08009#include <unistd.h>
jb.qi1f6204d2024-01-30 22:14:24 -080010
jb.qi3a8298c2023-09-07 04:48:51 -070011
jb.qi3a8298c2023-09-07 04:48:51 -070012#include <lynq_autosuspend.h>
13#include "lynq-qser-autosuspend.h"
14#include "liblog/lynq_deflog.h"
jb.qi7a901862024-01-03 19:45:23 -080015#include <include/lynq_uci.h>
jb.qi3a8298c2023-09-07 04:48:51 -070016
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21
22extern int lynq_autosleep_enable(void);
23extern int lynq_autosleep_disable(void);
24extern int release_wake_lock(char *name);
25extern int acquire_wake_lock(int lock, char *name);
26
27#define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND"
28#define FILE_LOCK_TABLE "/tmp/.lock_table"
jb.qi7a901862024-01-03 19:45:23 -080029#define LOG_UCI_MODULE "lynq_autosuspend"
30#define LOG_UCI_FILE "lynq_uci"
31
jb.qifbef5142023-12-18 00:49:10 -080032#define UEVENT_MSG_LEN 128
33
jb.qi1b4d54e2024-01-03 22:22:42 -080034int g_init_flag = 0;
jb.qifbef5142023-12-18 00:49:10 -080035
36
37qser_lpm_Handler_t g_lpm_handler = NULL;
jb.qi3a8298c2023-09-07 04:48:51 -070038
39
40LOCK_TABLE lock_status;
41
42
43int file_fd;
44int first_run = 0;
jb.qifbef5142023-12-18 00:49:10 -080045pthread_t thid;
jb.qi3a8298c2023-09-07 04:48:51 -070046
47
48int lock_table_init(void)
49{
50 int ret;
51 int err;
52
53 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
54 if(file_fd < 0)
55 {
56 err = errno;
57 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
58 return -err;
59 }
60 memset(&lock_status, 0, sizeof(lock_status));
61 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
62 if(ret <= 0)
63 {
64 LYINFLOG("write fail\n");
65 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070066 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070067 }
68 sync();
69 close(file_fd);
70 return 0;
71}
72
73
74int read_lock_table(void)
75{
76 int err;
77 int ret;
78 int i;
79 LYLOGSET(LOG_INFO);
80 LYLOGEINIT(USER_LOG_TAG);
81
82 if(access(FILE_LOCK_TABLE,0) < 0)
83 {
84 ret = lock_table_init();
85 if(ret < 0)
86 {
jb.qiaa241cf2023-09-18 18:35:00 -070087 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070088 }
89 }
90
91 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
92 if(file_fd < 0)
93 {
94 err = errno;
95 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -070096 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070097 }
98
99 memset(&lock_status, 0, sizeof(lock_status));
100 lseek(file_fd,0,SEEK_SET);
101 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
102 LYINFLOG("read ret=%d\n", ret);
103 if(ret <= 0)
104 {
105 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700106 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700107 }
108
109 for(i=0;i<MAX_LOCK_NUM;i++)
110 {
111 if(strlen(lock_status.lock_name[i]) != 0)
112 {
113 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
114 }
115 }
116
117 close(file_fd);
118 return 0;
119}
120
121
122int save_lock_table(void)
123{
124 int err;
125 int ret;
126 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
127 if(file_fd < 0)
128 {
129 err = errno;
130 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700131 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700132 }
133 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
134 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
135 LYINFLOG("write ret=%d\n", ret);
136 if(ret <= 0)
137 {
138 LYINFLOG("write fail\n");
139 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700140 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700141 }
142 sync();
143 close(file_fd);
144
145 return 0;
146}
147
148
149int check_lock(char *name)
150{
151 int j;
152 int num;
153 for(j=0;j<MAX_LOCK_NUM;j++)
154 {
155 if(strcmp(lock_status.lock_name[j], name) == 0)
156 {
157 num = j;
158 break;
159 }
160 }
161
162 if(j < MAX_LOCK_NUM)
163 {
164 return num;
165 }
166
167 return -1;
168}
169
170
171int add_lock(char *name)
172{
173 int ret;
174 int i = 0;
175 int num;
176 int check_flag;
177
178 LYINFLOG("name:%s\n", name);
179 ret = read_lock_table();
180 LYINFLOG("read_lock_table ret = %d\n", ret);
181 if(ret <0)
182 {
183 return ret;
184 }
185
186 check_flag = check_lock(name);
187
188 if(check_flag < 0)
189 {
190 for(i=0;i<MAX_LOCK_NUM;i++)
191 {
192 if(strlen(lock_status.lock_name[i]) == 0)
193 {
194 strcpy(lock_status.lock_name[i], name);
195 LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
196 break;
197 }
198 }
199 if(i == MAX_LOCK_NUM)
200 {
jb.qiaa241cf2023-09-18 18:35:00 -0700201 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700202 }
203 else
204 {
205 num = i;
206 }
207 }
208 else
209 {
jb.qiaa241cf2023-09-18 18:35:00 -0700210 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700211 }
212
213 LYINFLOG("num = %d\n", num);
214 ret = save_lock_table();
215 if(ret < 0)
216 {
217 return ret;
218 }
219 return num;
220}
221
222int delete_lock(int fd)
223{
224 int ret;
225 int i;
226 ret = read_lock_table();
227 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
228 ret = save_lock_table();
229 return ret;
230}
231
jb.qifbef5142023-12-18 00:49:10 -0800232
233void *check_dtr(void * arg)
234{
jb.qifbef5142023-12-18 00:49:10 -0800235 qser_lpm_edge_t lpm_edge;
jb.qi1f6204d2024-01-30 22:14:24 -0800236 char msg[16];
237 int fd=0;
238 int ret=0;
239
jb.qifbef5142023-12-18 00:49:10 -0800240 while(1)
241 {
jb.qi1f6204d2024-01-30 22:14:24 -0800242 fd = open("/sys/xp2xp/xp2xp_notify/xp2xp_state", O_RDONLY);
243 ret=read(fd, &msg,15);
244 LYERRLOG("xp2xp_state ret = %d\n", ret);
245 close(fd);
246
247 if(ret == 5)
jb.qifbef5142023-12-18 00:49:10 -0800248 {
jb.qi1f6204d2024-01-30 22:14:24 -0800249 lpm_edge =E_QL_LPM_FALLING;
jb.qifbef5142023-12-18 00:49:10 -0800250 }
jb.qi1f6204d2024-01-30 22:14:24 -0800251 else if(ret == 6)
252 {
253 lpm_edge =E_QL_LPM_RISING;
254 }
255 else
256 {
257 continue;
258 }
259 g_lpm_handler(lpm_edge);
260
jb.qifbef5142023-12-18 00:49:10 -0800261 }
262
263 return 0;
264}
265
266
jb.qi3a8298c2023-09-07 04:48:51 -0700267int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
268{
269 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800270 int num;
jb.qi1b4d54e2024-01-03 22:22:42 -0800271 if(g_init_flag != 0)
272 {
273 LYERRLOG("g_init_flag is error\n");
274 return -1;
275 }
276 g_init_flag = 1;
jb.qifbef5142023-12-18 00:49:10 -0800277 g_lpm_handler = qser_lpm_handler;
278 ret = pthread_create(&thid,NULL,check_dtr,NULL);
jb.qi3a8298c2023-09-07 04:48:51 -0700279 if(ret != 0)
280 {
jb.qi1b4d54e2024-01-03 22:22:42 -0800281 LYERRLOG("pthread create fail, qser_lpm_init fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700282 }
283 return ret;
284}
285
286
287int qser_lpm_deinit(void)
288{
289 int ret;
jb.qi1b4d54e2024-01-03 22:22:42 -0800290 if(g_init_flag != 1)
291 {
292 LYERRLOG("g_init_flag is error");
293 return -1;
294 }
295 g_init_flag = 0;
jb.qifbef5142023-12-18 00:49:10 -0800296 ret = pthread_cancel(thid);
297 if(!ret)
jb.qi3a8298c2023-09-07 04:48:51 -0700298 {
jb.qifbef5142023-12-18 00:49:10 -0800299 LYERRLOG("pthread cancel success, lpm deinit success\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700300 }
jb.qifbef5142023-12-18 00:49:10 -0800301 else
jb.qi3a8298c2023-09-07 04:48:51 -0700302 {
jb.qifbef5142023-12-18 00:49:10 -0800303 LYERRLOG("pthread cancel fail, lpm deinit fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700304 }
jb.qifbef5142023-12-18 00:49:10 -0800305 return ret;
306
jb.qi3a8298c2023-09-07 04:48:51 -0700307}
308
jb.qiaa241cf2023-09-18 18:35:00 -0700309
jb.qi3a8298c2023-09-07 04:48:51 -0700310int qser_autosuspend_enable(char enable)
311{
312 int ret;
313 if(enable == '0')
314 {
315 ret = lynq_autosleep_disable();
316
317 }
318 else if(enable == '1')
319 {
320 ret = lynq_autosleep_enable();
321 }
322 else
323 {
jb.qiaa241cf2023-09-18 18:35:00 -0700324 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700325
326 }
jb.qiaa241cf2023-09-18 18:35:00 -0700327 if(ret >= 0)
328 {
329 ret = 0;
330 }
jb.qi3a8298c2023-09-07 04:48:51 -0700331 return ret;
332
333}
334
jb.qiaa241cf2023-09-18 18:35:00 -0700335
jb.qi3a8298c2023-09-07 04:48:51 -0700336int qser_wakelock_create(const char *name, size_t len)
337{
338 int ret;
339 if(name == NULL)
340 {
jb.qiaa241cf2023-09-18 18:35:00 -0700341 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700342 }
343 LYINFLOG("%s\n", name);
344 ret = add_lock(name);
345 return ret;
346}
347
348int qser_wakelock_lock(int fd)
349{
350
351 int ret;
352 if(fd < 0 || fd >= MAX_LOCK_NUM)
353 {
jb.qiaa241cf2023-09-18 18:35:00 -0700354 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700355 }
356 ret = read_lock_table();
357 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700358 if(ret > 0)
359 {
360 ret = 0;
361 }
362 else
363 {
364 ret = -1;
365 }
jb.qi3a8298c2023-09-07 04:48:51 -0700366 return ret;
367}
368
369int qser_wakelock_unlock(int fd)
370{
371 int ret;
372 if(fd < 0 || fd >= MAX_LOCK_NUM)
373 {
jb.qiaa241cf2023-09-18 18:35:00 -0700374 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700375 }
376 ret = read_lock_table();
377 if(strlen(lock_status.lock_name[fd]) == 0)
378 {
379 LYINFLOG("%d is null\n", fd);
380 return -1;
381 }
382 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700383 if(ret > 0)
384 {
385 ret = 0;
386 }
387 else
388 {
389 ret = -1;
390 }
jb.qi3a8298c2023-09-07 04:48:51 -0700391 return ret;
392}
393
394int qser_wakelock_destroy(int fd)
395{
396 int ret;
397 if(fd < 0 || fd >= MAX_LOCK_NUM)
398 {
jb.qiaa241cf2023-09-18 18:35:00 -0700399 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700400 }
jb.qi52210352023-09-19 23:29:09 -0700401 ret = qser_wakelock_unlock(fd);
402 if(ret)
403 {
404 LYINFLOG("unlock is fail\n");
405 return ret;
406 }
407
jb.qi422ee112023-11-21 03:04:03 -0800408 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700409 return ret;
410}
411
jb.qi7a901862024-01-03 19:45:23 -0800412int qser_whitelist_set(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800413{
414 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800415 char cmd[64];
416 if(strlen(whitelist) != 4)
jb.qi422ee112023-11-21 03:04:03 -0800417 {
jb.qi7a901862024-01-03 19:45:23 -0800418 LYINFLOG("string len is error\n");
jb.qi422ee112023-11-21 03:04:03 -0800419 return -1;
420 }
jb.qi7a901862024-01-03 19:45:23 -0800421 sprintf(cmd, "uci set lynq_uci.lynq_autosuspend.whitelist_state='%s'", whitelist);
422 ret = system(cmd);
423 system("uci commit");
424 if(ret != 0)
425 {
426 LYINFLOG("qser_whitlist_set fail");
427 }
jb.qi422ee112023-11-21 03:04:03 -0800428 return ret;
429
430}
431
jb.qi7a901862024-01-03 19:45:23 -0800432int qser_whitelist_get(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800433{
434 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800435 ret = lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "whitelist_state", whitelist);
436 LYERRLOG("ret =%d, whitelist_state is %s\n", ret, whitelist);
jb.qi422ee112023-11-21 03:04:03 -0800437 return ret;
438
439}
440
you.chen21c62b72023-09-08 09:41:11 +0800441DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
442
jb.qi3a8298c2023-09-07 04:48:51 -0700443#ifdef __cplusplus
444}
445#endif
446
447
448