blob: ea9b3816d2a9df704549517ba02daa532d44d83a [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.qifbef5142023-12-18 00:49:10 -080035
36struct 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;
320
321 g_lpm_handler = qser_lpm_handler;
322 ret = pthread_create(&thid,NULL,check_dtr,NULL);
jb.qi3a8298c2023-09-07 04:48:51 -0700323 if(ret != 0)
324 {
jb.qifbef5142023-12-18 00:49:10 -0800325 LYERRLOG("restart service fail");
jb.qi3a8298c2023-09-07 04:48:51 -0700326 }
327 return ret;
328}
329
330
331int qser_lpm_deinit(void)
332{
333 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800334 ret = pthread_cancel(thid);
jb.qie7aac6d2023-12-28 01:32:19 -0800335 close(device_fd);
jb.qifbef5142023-12-18 00:49:10 -0800336 if(!ret)
jb.qi3a8298c2023-09-07 04:48:51 -0700337 {
jb.qifbef5142023-12-18 00:49:10 -0800338 LYERRLOG("pthread cancel success, lpm deinit success\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700339 }
jb.qifbef5142023-12-18 00:49:10 -0800340 else
jb.qi3a8298c2023-09-07 04:48:51 -0700341 {
jb.qifbef5142023-12-18 00:49:10 -0800342 LYERRLOG("pthread cancel fail, lpm deinit fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700343 }
jb.qifbef5142023-12-18 00:49:10 -0800344 return ret;
345
jb.qi3a8298c2023-09-07 04:48:51 -0700346}
347
jb.qiaa241cf2023-09-18 18:35:00 -0700348
jb.qi3a8298c2023-09-07 04:48:51 -0700349int qser_autosuspend_enable(char enable)
350{
351 int ret;
352 if(enable == '0')
353 {
354 ret = lynq_autosleep_disable();
355
356 }
357 else if(enable == '1')
358 {
359 ret = lynq_autosleep_enable();
360 }
361 else
362 {
jb.qiaa241cf2023-09-18 18:35:00 -0700363 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700364
365 }
jb.qiaa241cf2023-09-18 18:35:00 -0700366 if(ret >= 0)
367 {
368 ret = 0;
369 }
jb.qi3a8298c2023-09-07 04:48:51 -0700370 return ret;
371
372}
373
jb.qiaa241cf2023-09-18 18:35:00 -0700374
jb.qi3a8298c2023-09-07 04:48:51 -0700375int qser_wakelock_create(const char *name, size_t len)
376{
377 int ret;
378 if(name == NULL)
379 {
jb.qiaa241cf2023-09-18 18:35:00 -0700380 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700381 }
382 LYINFLOG("%s\n", name);
383 ret = add_lock(name);
384 return ret;
385}
386
387int qser_wakelock_lock(int fd)
388{
389
390 int ret;
391 if(fd < 0 || fd >= MAX_LOCK_NUM)
392 {
jb.qiaa241cf2023-09-18 18:35:00 -0700393 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700394 }
395 ret = read_lock_table();
396 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700397 if(ret > 0)
398 {
399 ret = 0;
400 }
401 else
402 {
403 ret = -1;
404 }
jb.qi3a8298c2023-09-07 04:48:51 -0700405 return ret;
406}
407
408int qser_wakelock_unlock(int fd)
409{
410 int ret;
411 if(fd < 0 || fd >= MAX_LOCK_NUM)
412 {
jb.qiaa241cf2023-09-18 18:35:00 -0700413 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700414 }
415 ret = read_lock_table();
416 if(strlen(lock_status.lock_name[fd]) == 0)
417 {
418 LYINFLOG("%d is null\n", fd);
419 return -1;
420 }
421 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700422 if(ret > 0)
423 {
424 ret = 0;
425 }
426 else
427 {
428 ret = -1;
429 }
jb.qi3a8298c2023-09-07 04:48:51 -0700430 return ret;
431}
432
433int qser_wakelock_destroy(int fd)
434{
435 int ret;
436 if(fd < 0 || fd >= MAX_LOCK_NUM)
437 {
jb.qiaa241cf2023-09-18 18:35:00 -0700438 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700439 }
jb.qi52210352023-09-19 23:29:09 -0700440 ret = qser_wakelock_unlock(fd);
441 if(ret)
442 {
443 LYINFLOG("unlock is fail\n");
444 return ret;
445 }
446
jb.qi422ee112023-11-21 03:04:03 -0800447 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700448 return ret;
449}
450
jb.qi7a901862024-01-03 19:45:23 -0800451int qser_whitelist_set(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800452{
453 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800454 char cmd[64];
455 if(strlen(whitelist) != 4)
jb.qi422ee112023-11-21 03:04:03 -0800456 {
jb.qi7a901862024-01-03 19:45:23 -0800457 LYINFLOG("string len is error\n");
jb.qi422ee112023-11-21 03:04:03 -0800458 return -1;
459 }
jb.qi7a901862024-01-03 19:45:23 -0800460 sprintf(cmd, "uci set lynq_uci.lynq_autosuspend.whitelist_state='%s'", whitelist);
461 ret = system(cmd);
462 system("uci commit");
463 if(ret != 0)
464 {
465 LYINFLOG("qser_whitlist_set fail");
466 }
jb.qi422ee112023-11-21 03:04:03 -0800467 return ret;
468
469}
470
jb.qi7a901862024-01-03 19:45:23 -0800471int qser_whitelist_get(char *whitelist)
jb.qi422ee112023-11-21 03:04:03 -0800472{
473 int ret;
jb.qi7a901862024-01-03 19:45:23 -0800474 ret = lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "whitelist_state", whitelist);
475 LYERRLOG("ret =%d, whitelist_state is %s\n", ret, whitelist);
jb.qi422ee112023-11-21 03:04:03 -0800476 return ret;
477
478}
479
you.chen21c62b72023-09-08 09:41:11 +0800480DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
481
jb.qi3a8298c2023-09-07 04:48:51 -0700482#ifdef __cplusplus
483}
484#endif
485
486
487