blob: 01ccd5d89b409790ba89419000881422a3f636f9 [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.qifbef5142023-12-18 00:49:10 -080010#include <cutils/uevent.h>
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.qie7aac6d2023-12-28 01:32:19 -080034int device_fd;
jb.qi1b4d54e2024-01-03 22:22:42 -080035int g_init_flag = 0;
jb.qifbef5142023-12-18 00:49:10 -080036struct dtr_uevent {
37 const char *action;
38 const char *path;
39 const char *subsystem;
40 const char *usbevent;
41};
42
43static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent);
44
45
46qser_lpm_Handler_t g_lpm_handler = NULL;
jb.qi3a8298c2023-09-07 04:48:51 -070047
48
49LOCK_TABLE lock_status;
50
51
52int file_fd;
53int first_run = 0;
jb.qifbef5142023-12-18 00:49:10 -080054pthread_t thid;
jb.qi3a8298c2023-09-07 04:48:51 -070055
56
57int lock_table_init(void)
58{
59 int ret;
60 int err;
61
62 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
63 if(file_fd < 0)
64 {
65 err = errno;
66 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
67 return -err;
68 }
69 memset(&lock_status, 0, sizeof(lock_status));
70 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
71 if(ret <= 0)
72 {
73 LYINFLOG("write fail\n");
74 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070075 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070076 }
77 sync();
78 close(file_fd);
79 return 0;
80}
81
82
83int read_lock_table(void)
84{
85 int err;
86 int ret;
87 int i;
88 LYLOGSET(LOG_INFO);
89 LYLOGEINIT(USER_LOG_TAG);
90
91 if(access(FILE_LOCK_TABLE,0) < 0)
92 {
93 ret = lock_table_init();
94 if(ret < 0)
95 {
jb.qiaa241cf2023-09-18 18:35:00 -070096 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070097 }
98 }
99
100 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
101 if(file_fd < 0)
102 {
103 err = errno;
104 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700105 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700106 }
107
108 memset(&lock_status, 0, sizeof(lock_status));
109 lseek(file_fd,0,SEEK_SET);
110 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
111 LYINFLOG("read ret=%d\n", ret);
112 if(ret <= 0)
113 {
114 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700115 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700116 }
117
118 for(i=0;i<MAX_LOCK_NUM;i++)
119 {
120 if(strlen(lock_status.lock_name[i]) != 0)
121 {
122 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
123 }
124 }
125
126 close(file_fd);
127 return 0;
128}
129
130
131int save_lock_table(void)
132{
133 int err;
134 int ret;
135 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
136 if(file_fd < 0)
137 {
138 err = errno;
139 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700140 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700141 }
142 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
143 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
144 LYINFLOG("write ret=%d\n", ret);
145 if(ret <= 0)
146 {
147 LYINFLOG("write fail\n");
148 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700149 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700150 }
151 sync();
152 close(file_fd);
153
154 return 0;
155}
156
157
158int check_lock(char *name)
159{
160 int j;
161 int num;
162 for(j=0;j<MAX_LOCK_NUM;j++)
163 {
164 if(strcmp(lock_status.lock_name[j], name) == 0)
165 {
166 num = j;
167 break;
168 }
169 }
170
171 if(j < MAX_LOCK_NUM)
172 {
173 return num;
174 }
175
176 return -1;
177}
178
179
180int add_lock(char *name)
181{
182 int ret;
183 int i = 0;
184 int num;
185 int check_flag;
186
187 LYINFLOG("name:%s\n", name);
188 ret = read_lock_table();
189 LYINFLOG("read_lock_table ret = %d\n", ret);
190 if(ret <0)
191 {
192 return ret;
193 }
194
195 check_flag = check_lock(name);
196
197 if(check_flag < 0)
198 {
199 for(i=0;i<MAX_LOCK_NUM;i++)
200 {
201 if(strlen(lock_status.lock_name[i]) == 0)
202 {
203 strcpy(lock_status.lock_name[i], name);
204 LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
205 break;
206 }
207 }
208 if(i == MAX_LOCK_NUM)
209 {
jb.qiaa241cf2023-09-18 18:35:00 -0700210 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700211 }
212 else
213 {
214 num = i;
215 }
216 }
217 else
218 {
jb.qiaa241cf2023-09-18 18:35:00 -0700219 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700220 }
221
222 LYINFLOG("num = %d\n", num);
223 ret = save_lock_table();
224 if(ret < 0)
225 {
226 return ret;
227 }
228 return num;
229}
230
231int delete_lock(int fd)
232{
233 int ret;
234 int i;
235 ret = read_lock_table();
236 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
237 ret = save_lock_table();
238 return ret;
239}
240
jb.qifbef5142023-12-18 00:49:10 -0800241static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent)
242{
243 dtr_uevent->action = "";
244 dtr_uevent->path = "";
245 dtr_uevent->subsystem = "";
246 dtr_uevent->usbevent = "";
247
248 while (*msg) {
249 if (!strncmp(msg, "ACTION=", 7))
250 {
251 msg += 7;
252 dtr_uevent->action = msg;
253 }
254 else if (!strncmp(msg, "DEVPATH=", 8))
255 {
256 msg += 8;
257 dtr_uevent->path = msg;
258 }
259 else if (!strncmp(msg, "SUBSYSTEM=", 10))
260 {
261 msg += 10;
262 dtr_uevent->subsystem = msg;
263 }
264 else if (!strncmp(msg, "USBEVENT=", 9)) {
265 msg += 9;
266 dtr_uevent->usbevent = msg;
267 }
268
269 while(*msg++);
270 }
271
272 LYINFLOG("event { '%s', '%s', '%s', '%s'}\n",
273 dtr_uevent->action, dtr_uevent->path, dtr_uevent->subsystem,dtr_uevent->usbevent);
274}
275
276
277
278void *check_dtr(void * arg)
279{
jb.qifbef5142023-12-18 00:49:10 -0800280 qser_lpm_edge_t lpm_edge;
281 char msg[UEVENT_MSG_LEN+2];
282 struct dtr_uevent dtr_uevent;
283 int n;
284 int i;
285
286 device_fd = uevent_open_socket(4*UEVENT_MSG_LEN, true);
287 if(device_fd < 0)
288 {
289 LYERRLOG("uevent_open_socket fail\n");
290 return -1;
291 }
292 while(1)
293 {
294 n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN);
295 msg[n] = '\0';
296 msg[n+1] = '\0';
297 parse_event(msg, &dtr_uevent);
jb.qie7aac6d2023-12-28 01:32:19 -0800298 if((!strcmp(dtr_uevent.subsystem, "xp2xp")))
jb.qifbef5142023-12-18 00:49:10 -0800299 {
jb.qie7aac6d2023-12-28 01:32:19 -0800300 if(!strcmp(dtr_uevent.action, "online"))
301 {
302 lpm_edge =E_QL_LPM_FALLING;
303 }
304 else
305 {
306 lpm_edge =E_QL_LPM_RISING;
307 }
jb.qifbef5142023-12-18 00:49:10 -0800308 g_lpm_handler(lpm_edge);
309 }
310 }
311
312 return 0;
313}
314
315
jb.qi3a8298c2023-09-07 04:48:51 -0700316int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
317{
318 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800319 int num;
jb.qi1b4d54e2024-01-03 22:22:42 -0800320 if(g_init_flag != 0)
321 {
322 LYERRLOG("g_init_flag is error\n");
323 return -1;
324 }
325 g_init_flag = 1;
jb.qifbef5142023-12-18 00:49:10 -0800326 g_lpm_handler = qser_lpm_handler;
327 ret = pthread_create(&thid,NULL,check_dtr,NULL);
jb.qi3a8298c2023-09-07 04:48:51 -0700328 if(ret != 0)
329 {
jb.qi1b4d54e2024-01-03 22:22:42 -0800330 LYERRLOG("pthread create fail, qser_lpm_init fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700331 }
332 return ret;
333}
334
335
336int qser_lpm_deinit(void)
337{
338 int ret;
jb.qi1b4d54e2024-01-03 22:22:42 -0800339 if(g_init_flag != 1)
340 {
341 LYERRLOG("g_init_flag is error");
342 return -1;
343 }
344 g_init_flag = 0;
jb.qifbef5142023-12-18 00:49:10 -0800345 ret = pthread_cancel(thid);
jb.qie7aac6d2023-12-28 01:32:19 -0800346 close(device_fd);
jb.qifbef5142023-12-18 00:49:10 -0800347 if(!ret)
jb.qi3a8298c2023-09-07 04:48:51 -0700348 {
jb.qifbef5142023-12-18 00:49:10 -0800349 LYERRLOG("pthread cancel success, lpm deinit success\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700350 }
jb.qifbef5142023-12-18 00:49:10 -0800351 else
jb.qi3a8298c2023-09-07 04:48:51 -0700352 {
jb.qifbef5142023-12-18 00:49:10 -0800353 LYERRLOG("pthread cancel fail, lpm deinit fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700354 }
jb.qifbef5142023-12-18 00:49:10 -0800355 return ret;
356
jb.qi3a8298c2023-09-07 04:48:51 -0700357}
358
jb.qiaa241cf2023-09-18 18:35:00 -0700359
jb.qi3a8298c2023-09-07 04:48:51 -0700360int qser_autosuspend_enable(char enable)
361{
362 int ret;
363 if(enable == '0')
364 {
365 ret = lynq_autosleep_disable();
366
367 }
368 else if(enable == '1')
369 {
370 ret = lynq_autosleep_enable();
371 }
372 else
373 {
jb.qiaa241cf2023-09-18 18:35:00 -0700374 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700375
376 }
jb.qiaa241cf2023-09-18 18:35:00 -0700377 if(ret >= 0)
378 {
379 ret = 0;
380 }
jb.qi3a8298c2023-09-07 04:48:51 -0700381 return ret;
382
383}
384
jb.qiaa241cf2023-09-18 18:35:00 -0700385
jb.qi3a8298c2023-09-07 04:48:51 -0700386int qser_wakelock_create(const char *name, size_t len)
387{
388 int ret;
389 if(name == NULL)
390 {
jb.qiaa241cf2023-09-18 18:35:00 -0700391 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700392 }
393 LYINFLOG("%s\n", name);
394 ret = add_lock(name);
395 return ret;
396}
397
398int qser_wakelock_lock(int fd)
399{
400
401 int ret;
402 if(fd < 0 || fd >= MAX_LOCK_NUM)
403 {
jb.qiaa241cf2023-09-18 18:35:00 -0700404 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700405 }
406 ret = read_lock_table();
407 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700408 if(ret > 0)
409 {
410 ret = 0;
411 }
412 else
413 {
414 ret = -1;
415 }
jb.qi3a8298c2023-09-07 04:48:51 -0700416 return ret;
417}
418
419int qser_wakelock_unlock(int fd)
420{
421 int ret;
422 if(fd < 0 || fd >= MAX_LOCK_NUM)
423 {
jb.qiaa241cf2023-09-18 18:35:00 -0700424 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700425 }
426 ret = read_lock_table();
427 if(strlen(lock_status.lock_name[fd]) == 0)
428 {
429 LYINFLOG("%d is null\n", fd);
430 return -1;
431 }
432 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700433 if(ret > 0)
434 {
435 ret = 0;
436 }
437 else
438 {
439 ret = -1;
440 }
jb.qi3a8298c2023-09-07 04:48:51 -0700441 return ret;
442}
443
444int qser_wakelock_destroy(int fd)
445{
446 int ret;
447 if(fd < 0 || fd >= MAX_LOCK_NUM)
448 {
jb.qiaa241cf2023-09-18 18:35:00 -0700449 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700450 }
jb.qi52210352023-09-19 23:29:09 -0700451 ret = qser_wakelock_unlock(fd);
452 if(ret)
453 {
454 LYINFLOG("unlock is fail\n");
455 return ret;
456 }
457
jb.qi422ee112023-11-21 03:04:03 -0800458 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700459 return ret;
460}
461
jb.qi7a901862024-01-03 19:45:23 -0800462int qser_whitelist_set(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800463{
464 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800465 char cmd[64];
466 if(strlen(whitelist) != 4)
jb.qi422ee112023-11-21 03:04:03 -0800467 {
jb.qi7a901862024-01-03 19:45:23 -0800468 LYINFLOG("string len is error\n");
jb.qi422ee112023-11-21 03:04:03 -0800469 return -1;
470 }
jb.qi7a901862024-01-03 19:45:23 -0800471 sprintf(cmd, "uci set lynq_uci.lynq_autosuspend.whitelist_state='%s'", whitelist);
472 ret = system(cmd);
473 system("uci commit");
474 if(ret != 0)
475 {
476 LYINFLOG("qser_whitlist_set fail");
477 }
jb.qi422ee112023-11-21 03:04:03 -0800478 return ret;
479
480}
481
jb.qi7a901862024-01-03 19:45:23 -0800482int qser_whitelist_get(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800483{
484 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800485 ret = lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "whitelist_state", whitelist);
486 LYERRLOG("ret =%d, whitelist_state is %s\n", ret, whitelist);
jb.qi422ee112023-11-21 03:04:03 -0800487 return ret;
488
489}
490
you.chen21c62b72023-09-08 09:41:11 +0800491DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
492
jb.qi3a8298c2023-09-07 04:48:51 -0700493#ifdef __cplusplus
494}
495#endif
496
497
498