blob: bb659eff00b47fb1e3edde197e943aaaa66b21be [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#include <lynq_autosuspend.h>
12#include "lynq-qser-autosuspend.h"
13#include "liblog/lynq_deflog.h"
jb.qi7a901862024-01-03 19:45:23 -080014#include <include/lynq_uci.h>
jb.qi3a8298c2023-09-07 04:48:51 -070015
16#ifdef __cplusplus
17extern "C" {
18#endif
19
20
21extern int lynq_autosleep_enable(void);
22extern int lynq_autosleep_disable(void);
23extern int release_wake_lock(char *name);
24extern int acquire_wake_lock(int lock, char *name);
25
26#define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND"
27#define FILE_LOCK_TABLE "/tmp/.lock_table"
jb.qi7a901862024-01-03 19:45:23 -080028#define LOG_UCI_MODULE "lynq_autosuspend"
29#define LOG_UCI_FILE "lynq_uci"
30
jb.qifbef5142023-12-18 00:49:10 -080031#define UEVENT_MSG_LEN 128
32
jb.qi1b4d54e2024-01-03 22:22:42 -080033int g_init_flag = 0;
jb.qifbef5142023-12-18 00:49:10 -080034
35
36qser_lpm_Handler_t g_lpm_handler = NULL;
jb.qi3a8298c2023-09-07 04:48:51 -070037
38
39LOCK_TABLE lock_status;
40
41
42int file_fd;
43int first_run = 0;
jb.qifbef5142023-12-18 00:49:10 -080044pthread_t thid;
jb.qi3a8298c2023-09-07 04:48:51 -070045
46
47int lock_table_init(void)
48{
49 int ret;
50 int err;
51
52 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
53 if(file_fd < 0)
54 {
55 err = errno;
56 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
57 return -err;
58 }
59 memset(&lock_status, 0, sizeof(lock_status));
60 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
61 if(ret <= 0)
62 {
63 LYINFLOG("write fail\n");
64 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070065 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070066 }
67 sync();
68 close(file_fd);
69 return 0;
70}
71
72
73int read_lock_table(void)
74{
75 int err;
76 int ret;
77 int i;
78 LYLOGSET(LOG_INFO);
79 LYLOGEINIT(USER_LOG_TAG);
80
81 if(access(FILE_LOCK_TABLE,0) < 0)
82 {
83 ret = lock_table_init();
84 if(ret < 0)
85 {
jb.qiaa241cf2023-09-18 18:35:00 -070086 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070087 }
88 }
89
90 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
91 if(file_fd < 0)
92 {
93 err = errno;
94 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -070095 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070096 }
97
98 memset(&lock_status, 0, sizeof(lock_status));
99 lseek(file_fd,0,SEEK_SET);
100 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
101 LYINFLOG("read ret=%d\n", ret);
102 if(ret <= 0)
103 {
104 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700105 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700106 }
107
108 for(i=0;i<MAX_LOCK_NUM;i++)
109 {
110 if(strlen(lock_status.lock_name[i]) != 0)
111 {
112 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
113 }
114 }
115
116 close(file_fd);
117 return 0;
118}
119
120
121int save_lock_table(void)
122{
123 int err;
124 int ret;
125 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
126 if(file_fd < 0)
127 {
128 err = errno;
129 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700130 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700131 }
132 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
133 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
134 LYINFLOG("write ret=%d\n", ret);
135 if(ret <= 0)
136 {
137 LYINFLOG("write fail\n");
138 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700139 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700140 }
141 sync();
142 close(file_fd);
143
144 return 0;
145}
146
jb.qi562fd032024-02-01 02:42:10 -0800147int check_pid(int pid)
148{
149 char cmd1[64];
150 int ret = -1;
151 sprintf(cmd1, "ps -ef |awk '{print $1}'|grep \"^%d$\"", pid);
152 ret = system(cmd1);
153 return ret;
154}
jb.qi3a8298c2023-09-07 04:48:51 -0700155
156int check_lock(char *name)
157{
158 int j;
159 int num;
jb.qi562fd032024-02-01 02:42:10 -0800160 int ret=-1;
jb.qi3a8298c2023-09-07 04:48:51 -0700161 for(j=0;j<MAX_LOCK_NUM;j++)
162 {
163 if(strcmp(lock_status.lock_name[j], name) == 0)
164 {
165 num = j;
166 break;
167 }
168 }
169
170 if(j < MAX_LOCK_NUM)
171 {
jb.qi562fd032024-02-01 02:42:10 -0800172 ret = check_pid(lock_status.lock_pid[j]);
173 if(!ret)
174 {
175 LYINFLOG("the pid is exist\n");
176 num = MAX_LOCK_NUM+1;
177 return num;
178 }
179 else
180 {
181 LYINFLOG("the pid is not exist\n");
182 return num;
183 }
jb.qi3a8298c2023-09-07 04:48:51 -0700184 }
185
186 return -1;
187}
188
189
190int add_lock(char *name)
191{
192 int ret;
193 int i = 0;
194 int num;
195 int check_flag;
jb.qi562fd032024-02-01 02:42:10 -0800196 int pid;
197
jb.qi3a8298c2023-09-07 04:48:51 -0700198 ret = read_lock_table();
199 LYINFLOG("read_lock_table ret = %d\n", ret);
200 if(ret <0)
201 {
202 return ret;
203 }
jb.qi562fd032024-02-01 02:42:10 -0800204 pid = getpid();
jb.qi3a8298c2023-09-07 04:48:51 -0700205 check_flag = check_lock(name);
206
207 if(check_flag < 0)
208 {
209 for(i=0;i<MAX_LOCK_NUM;i++)
210 {
211 if(strlen(lock_status.lock_name[i]) == 0)
212 {
213 strcpy(lock_status.lock_name[i], name);
jb.qi562fd032024-02-01 02:42:10 -0800214 lock_status.lock_pid[i] = pid;
215 LYINFLOG("lock_name[%d] %s, lock_pid = %d\n", i, lock_status.lock_name[i], pid);
jb.qi3a8298c2023-09-07 04:48:51 -0700216 break;
217 }
218 }
219 if(i == MAX_LOCK_NUM)
220 {
jb.qiaa241cf2023-09-18 18:35:00 -0700221 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700222 }
223 else
224 {
225 num = i;
226 }
227 }
jb.qi562fd032024-02-01 02:42:10 -0800228 else if(check_flag > MAX_LOCK_NUM)
jb.qi3a8298c2023-09-07 04:48:51 -0700229 {
jb.qiaa241cf2023-09-18 18:35:00 -0700230 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700231 }
jb.qi562fd032024-02-01 02:42:10 -0800232 else
233 {
234 num = check_flag;
235 lock_status.lock_pid[num] = pid;
236 }
jb.qi3a8298c2023-09-07 04:48:51 -0700237
238 LYINFLOG("num = %d\n", num);
239 ret = save_lock_table();
240 if(ret < 0)
241 {
242 return ret;
243 }
244 return num;
245}
246
247int delete_lock(int fd)
248{
249 int ret;
250 int i;
251 ret = read_lock_table();
252 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
jb.qi562fd032024-02-01 02:42:10 -0800253 lock_status.lock_pid[fd] = -1;
jb.qi3a8298c2023-09-07 04:48:51 -0700254 ret = save_lock_table();
255 return ret;
256}
257
jb.qifbef5142023-12-18 00:49:10 -0800258
259void *check_dtr(void * arg)
260{
jb.qifbef5142023-12-18 00:49:10 -0800261 qser_lpm_edge_t lpm_edge;
jb.qi1f6204d2024-01-30 22:14:24 -0800262 char msg[16];
263 int fd=0;
264 int ret=0;
265
jb.qifbef5142023-12-18 00:49:10 -0800266 while(1)
267 {
jb.qi1f6204d2024-01-30 22:14:24 -0800268 fd = open("/sys/xp2xp/xp2xp_notify/xp2xp_state", O_RDONLY);
269 ret=read(fd, &msg,15);
270 LYERRLOG("xp2xp_state ret = %d\n", ret);
271 close(fd);
272
273 if(ret == 5)
jb.qifbef5142023-12-18 00:49:10 -0800274 {
jb.qi1f6204d2024-01-30 22:14:24 -0800275 lpm_edge =E_QL_LPM_FALLING;
jb.qifbef5142023-12-18 00:49:10 -0800276 }
jb.qi1f6204d2024-01-30 22:14:24 -0800277 else if(ret == 6)
278 {
279 lpm_edge =E_QL_LPM_RISING;
280 }
281 else
282 {
283 continue;
284 }
285 g_lpm_handler(lpm_edge);
286
jb.qifbef5142023-12-18 00:49:10 -0800287 }
288
289 return 0;
290}
291
292
jb.qi3a8298c2023-09-07 04:48:51 -0700293int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
294{
295 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800296 int num;
jb.qi1b4d54e2024-01-03 22:22:42 -0800297 if(g_init_flag != 0)
298 {
299 LYERRLOG("g_init_flag is error\n");
300 return -1;
301 }
302 g_init_flag = 1;
jb.qifbef5142023-12-18 00:49:10 -0800303 g_lpm_handler = qser_lpm_handler;
304 ret = pthread_create(&thid,NULL,check_dtr,NULL);
jb.qi3a8298c2023-09-07 04:48:51 -0700305 if(ret != 0)
306 {
jb.qi1b4d54e2024-01-03 22:22:42 -0800307 LYERRLOG("pthread create fail, qser_lpm_init fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700308 }
309 return ret;
310}
311
312
313int qser_lpm_deinit(void)
314{
315 int ret;
jb.qi1b4d54e2024-01-03 22:22:42 -0800316 if(g_init_flag != 1)
317 {
318 LYERRLOG("g_init_flag is error");
319 return -1;
320 }
321 g_init_flag = 0;
jb.qifbef5142023-12-18 00:49:10 -0800322 ret = pthread_cancel(thid);
323 if(!ret)
jb.qi3a8298c2023-09-07 04:48:51 -0700324 {
jb.qifbef5142023-12-18 00:49:10 -0800325 LYERRLOG("pthread cancel success, lpm deinit success\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700326 }
jb.qifbef5142023-12-18 00:49:10 -0800327 else
jb.qi3a8298c2023-09-07 04:48:51 -0700328 {
jb.qifbef5142023-12-18 00:49:10 -0800329 LYERRLOG("pthread cancel fail, lpm deinit fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700330 }
jb.qifbef5142023-12-18 00:49:10 -0800331 return ret;
332
jb.qi3a8298c2023-09-07 04:48:51 -0700333}
334
jb.qiaa241cf2023-09-18 18:35:00 -0700335
jb.qi3a8298c2023-09-07 04:48:51 -0700336int qser_autosuspend_enable(char enable)
337{
338 int ret;
339 if(enable == '0')
340 {
341 ret = lynq_autosleep_disable();
342
343 }
344 else if(enable == '1')
345 {
346 ret = lynq_autosleep_enable();
347 }
348 else
349 {
jb.qiaa241cf2023-09-18 18:35:00 -0700350 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700351
352 }
jb.qiaa241cf2023-09-18 18:35:00 -0700353 if(ret >= 0)
354 {
355 ret = 0;
356 }
jb.qi3a8298c2023-09-07 04:48:51 -0700357 return ret;
358
359}
360
jb.qiaa241cf2023-09-18 18:35:00 -0700361
jb.qi3a8298c2023-09-07 04:48:51 -0700362int qser_wakelock_create(const char *name, size_t len)
363{
364 int ret;
365 if(name == NULL)
366 {
jb.qiaa241cf2023-09-18 18:35:00 -0700367 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700368 }
369 LYINFLOG("%s\n", name);
370 ret = add_lock(name);
371 return ret;
372}
373
374int qser_wakelock_lock(int fd)
375{
376
377 int ret;
378 if(fd < 0 || fd >= MAX_LOCK_NUM)
379 {
jb.qiaa241cf2023-09-18 18:35:00 -0700380 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700381 }
382 ret = read_lock_table();
383 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700384 if(ret > 0)
385 {
386 ret = 0;
387 }
388 else
389 {
390 ret = -1;
391 }
jb.qi3a8298c2023-09-07 04:48:51 -0700392 return ret;
393}
394
395int qser_wakelock_unlock(int fd)
396{
397 int ret;
398 if(fd < 0 || fd >= MAX_LOCK_NUM)
399 {
jb.qiaa241cf2023-09-18 18:35:00 -0700400 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700401 }
402 ret = read_lock_table();
403 if(strlen(lock_status.lock_name[fd]) == 0)
404 {
405 LYINFLOG("%d is null\n", fd);
406 return -1;
407 }
408 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700409 if(ret > 0)
410 {
411 ret = 0;
412 }
413 else
414 {
415 ret = -1;
416 }
jb.qi3a8298c2023-09-07 04:48:51 -0700417 return ret;
418}
419
420int qser_wakelock_destroy(int fd)
421{
422 int ret;
423 if(fd < 0 || fd >= MAX_LOCK_NUM)
424 {
jb.qiaa241cf2023-09-18 18:35:00 -0700425 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700426 }
jb.qi52210352023-09-19 23:29:09 -0700427 ret = qser_wakelock_unlock(fd);
428 if(ret)
429 {
430 LYINFLOG("unlock is fail\n");
431 return ret;
432 }
433
jb.qi422ee112023-11-21 03:04:03 -0800434 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700435 return ret;
436}
437
jb.qi7a901862024-01-03 19:45:23 -0800438int qser_whitelist_set(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800439{
440 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800441 char cmd[64];
442 if(strlen(whitelist) != 4)
jb.qi422ee112023-11-21 03:04:03 -0800443 {
jb.qi7a901862024-01-03 19:45:23 -0800444 LYINFLOG("string len is error\n");
jb.qi422ee112023-11-21 03:04:03 -0800445 return -1;
446 }
jb.qi7a901862024-01-03 19:45:23 -0800447 sprintf(cmd, "uci set lynq_uci.lynq_autosuspend.whitelist_state='%s'", whitelist);
448 ret = system(cmd);
449 system("uci commit");
450 if(ret != 0)
451 {
452 LYINFLOG("qser_whitlist_set fail");
453 }
jb.qi422ee112023-11-21 03:04:03 -0800454 return ret;
455
456}
457
jb.qi7a901862024-01-03 19:45:23 -0800458int qser_whitelist_get(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800459{
460 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800461 ret = lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "whitelist_state", whitelist);
462 LYERRLOG("ret =%d, whitelist_state is %s\n", ret, whitelist);
jb.qi422ee112023-11-21 03:04:03 -0800463 return ret;
464
465}
466
you.chen21c62b72023-09-08 09:41:11 +0800467DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
468
jb.qi3a8298c2023-09-07 04:48:51 -0700469#ifdef __cplusplus
470}
471#endif
472
473
474