blob: 5a88ef7fc5d2d81ddbc31dd54bb859c1eddb0fdb [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.qi3a8298c2023-09-07 04:48:51 -07009#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.qi422ee112023-11-21 03:04:03 -080015#include "sc_mnet_whitelist.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.qifbef5142023-12-18 00:49:10 -080029#define UEVENT_MSG_LEN 128
30
31
32struct dtr_uevent {
33 const char *action;
34 const char *path;
35 const char *subsystem;
36 const char *usbevent;
37};
38
39static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent);
40
41
42qser_lpm_Handler_t g_lpm_handler = NULL;
jb.qi3a8298c2023-09-07 04:48:51 -070043
44
45LOCK_TABLE lock_status;
46
47
48int file_fd;
49int first_run = 0;
jb.qifbef5142023-12-18 00:49:10 -080050pthread_t thid;
jb.qi3a8298c2023-09-07 04:48:51 -070051
52
53int lock_table_init(void)
54{
55 int ret;
56 int err;
57
58 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
59 if(file_fd < 0)
60 {
61 err = errno;
62 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
63 return -err;
64 }
65 memset(&lock_status, 0, sizeof(lock_status));
66 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
67 if(ret <= 0)
68 {
69 LYINFLOG("write fail\n");
70 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070071 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070072 }
73 sync();
74 close(file_fd);
75 return 0;
76}
77
78
79int read_lock_table(void)
80{
81 int err;
82 int ret;
83 int i;
84 LYLOGSET(LOG_INFO);
85 LYLOGEINIT(USER_LOG_TAG);
86
87 if(access(FILE_LOCK_TABLE,0) < 0)
88 {
89 ret = lock_table_init();
90 if(ret < 0)
91 {
jb.qiaa241cf2023-09-18 18:35:00 -070092 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070093 }
94 }
95
96 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
97 if(file_fd < 0)
98 {
99 err = errno;
100 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700101 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700102 }
103
104 memset(&lock_status, 0, sizeof(lock_status));
105 lseek(file_fd,0,SEEK_SET);
106 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
107 LYINFLOG("read ret=%d\n", ret);
108 if(ret <= 0)
109 {
110 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700111 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700112 }
113
114 for(i=0;i<MAX_LOCK_NUM;i++)
115 {
116 if(strlen(lock_status.lock_name[i]) != 0)
117 {
118 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
119 }
120 }
121
122 close(file_fd);
123 return 0;
124}
125
126
127int save_lock_table(void)
128{
129 int err;
130 int ret;
131 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
132 if(file_fd < 0)
133 {
134 err = errno;
135 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700136 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700137 }
138 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
139 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
140 LYINFLOG("write ret=%d\n", ret);
141 if(ret <= 0)
142 {
143 LYINFLOG("write fail\n");
144 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700145 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700146 }
147 sync();
148 close(file_fd);
149
150 return 0;
151}
152
153
154int check_lock(char *name)
155{
156 int j;
157 int num;
158 for(j=0;j<MAX_LOCK_NUM;j++)
159 {
160 if(strcmp(lock_status.lock_name[j], name) == 0)
161 {
162 num = j;
163 break;
164 }
165 }
166
167 if(j < MAX_LOCK_NUM)
168 {
169 return num;
170 }
171
172 return -1;
173}
174
175
176int add_lock(char *name)
177{
178 int ret;
179 int i = 0;
180 int num;
181 int check_flag;
182
183 LYINFLOG("name:%s\n", name);
184 ret = read_lock_table();
185 LYINFLOG("read_lock_table ret = %d\n", ret);
186 if(ret <0)
187 {
188 return ret;
189 }
190
191 check_flag = check_lock(name);
192
193 if(check_flag < 0)
194 {
195 for(i=0;i<MAX_LOCK_NUM;i++)
196 {
197 if(strlen(lock_status.lock_name[i]) == 0)
198 {
199 strcpy(lock_status.lock_name[i], name);
200 LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
201 break;
202 }
203 }
204 if(i == MAX_LOCK_NUM)
205 {
jb.qiaa241cf2023-09-18 18:35:00 -0700206 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700207 }
208 else
209 {
210 num = i;
211 }
212 }
213 else
214 {
jb.qiaa241cf2023-09-18 18:35:00 -0700215 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700216 }
217
218 LYINFLOG("num = %d\n", num);
219 ret = save_lock_table();
220 if(ret < 0)
221 {
222 return ret;
223 }
224 return num;
225}
226
227int delete_lock(int fd)
228{
229 int ret;
230 int i;
231 ret = read_lock_table();
232 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
233 ret = save_lock_table();
234 return ret;
235}
236
jb.qifbef5142023-12-18 00:49:10 -0800237static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent)
238{
239 dtr_uevent->action = "";
240 dtr_uevent->path = "";
241 dtr_uevent->subsystem = "";
242 dtr_uevent->usbevent = "";
243
244 while (*msg) {
245 if (!strncmp(msg, "ACTION=", 7))
246 {
247 msg += 7;
248 dtr_uevent->action = msg;
249 }
250 else if (!strncmp(msg, "DEVPATH=", 8))
251 {
252 msg += 8;
253 dtr_uevent->path = msg;
254 }
255 else if (!strncmp(msg, "SUBSYSTEM=", 10))
256 {
257 msg += 10;
258 dtr_uevent->subsystem = msg;
259 }
260 else if (!strncmp(msg, "USBEVENT=", 9)) {
261 msg += 9;
262 dtr_uevent->usbevent = msg;
263 }
264
265 while(*msg++);
266 }
267
268 LYINFLOG("event { '%s', '%s', '%s', '%s'}\n",
269 dtr_uevent->action, dtr_uevent->path, dtr_uevent->subsystem,dtr_uevent->usbevent);
270}
271
272
273
274void *check_dtr(void * arg)
275{
276 int device_fd;
277 qser_lpm_edge_t lpm_edge;
278 char msg[UEVENT_MSG_LEN+2];
279 struct dtr_uevent dtr_uevent;
280 int n;
281 int i;
282
283 device_fd = uevent_open_socket(4*UEVENT_MSG_LEN, true);
284 if(device_fd < 0)
285 {
286 LYERRLOG("uevent_open_socket fail\n");
287 return -1;
288 }
289 while(1)
290 {
291 n = uevent_kernel_multicast_recv(device_fd, msg, UEVENT_MSG_LEN);
292 msg[n] = '\0';
293 msg[n+1] = '\0';
294 parse_event(msg, &dtr_uevent);
295 if((!strcmp(dtr_uevent.subsystem, "xp2xp")) && (!strcmp(dtr_uevent.action, "online")))
296 {
297 g_lpm_handler(lpm_edge);
298 }
299 }
300
301 return 0;
302}
303
304
jb.qi3a8298c2023-09-07 04:48:51 -0700305int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
306{
307 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800308 int num;
309
310 g_lpm_handler = qser_lpm_handler;
311 ret = pthread_create(&thid,NULL,check_dtr,NULL);
jb.qi3a8298c2023-09-07 04:48:51 -0700312 if(ret != 0)
313 {
jb.qifbef5142023-12-18 00:49:10 -0800314 LYERRLOG("restart service fail");
jb.qi3a8298c2023-09-07 04:48:51 -0700315 }
316 return ret;
317}
318
319
320int qser_lpm_deinit(void)
321{
322 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800323 ret = pthread_cancel(thid);
324 if(!ret)
jb.qi3a8298c2023-09-07 04:48:51 -0700325 {
jb.qifbef5142023-12-18 00:49:10 -0800326 LYERRLOG("pthread cancel success, lpm deinit success\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700327 }
jb.qifbef5142023-12-18 00:49:10 -0800328 else
jb.qi3a8298c2023-09-07 04:48:51 -0700329 {
jb.qifbef5142023-12-18 00:49:10 -0800330 LYERRLOG("pthread cancel fail, lpm deinit fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700331 }
jb.qifbef5142023-12-18 00:49:10 -0800332 return ret;
333
jb.qi3a8298c2023-09-07 04:48:51 -0700334}
335
jb.qiaa241cf2023-09-18 18:35:00 -0700336
jb.qi3a8298c2023-09-07 04:48:51 -0700337int qser_autosuspend_enable(char enable)
338{
339 int ret;
340 if(enable == '0')
341 {
342 ret = lynq_autosleep_disable();
343
344 }
345 else if(enable == '1')
346 {
347 ret = lynq_autosleep_enable();
348 }
349 else
350 {
jb.qiaa241cf2023-09-18 18:35:00 -0700351 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700352
353 }
jb.qiaa241cf2023-09-18 18:35:00 -0700354 if(ret >= 0)
355 {
356 ret = 0;
357 }
jb.qi3a8298c2023-09-07 04:48:51 -0700358 return ret;
359
360}
361
jb.qiaa241cf2023-09-18 18:35:00 -0700362
jb.qi3a8298c2023-09-07 04:48:51 -0700363int qser_wakelock_create(const char *name, size_t len)
364{
365 int ret;
366 if(name == NULL)
367 {
jb.qiaa241cf2023-09-18 18:35:00 -0700368 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700369 }
370 LYINFLOG("%s\n", name);
371 ret = add_lock(name);
372 return ret;
373}
374
375int qser_wakelock_lock(int fd)
376{
377
378 int ret;
379 if(fd < 0 || fd >= MAX_LOCK_NUM)
380 {
jb.qiaa241cf2023-09-18 18:35:00 -0700381 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700382 }
383 ret = read_lock_table();
384 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700385 if(ret > 0)
386 {
387 ret = 0;
388 }
389 else
390 {
391 ret = -1;
392 }
jb.qi3a8298c2023-09-07 04:48:51 -0700393 return ret;
394}
395
396int qser_wakelock_unlock(int fd)
397{
398 int ret;
399 if(fd < 0 || fd >= MAX_LOCK_NUM)
400 {
jb.qiaa241cf2023-09-18 18:35:00 -0700401 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700402 }
403 ret = read_lock_table();
404 if(strlen(lock_status.lock_name[fd]) == 0)
405 {
406 LYINFLOG("%d is null\n", fd);
407 return -1;
408 }
409 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700410 if(ret > 0)
411 {
412 ret = 0;
413 }
414 else
415 {
416 ret = -1;
417 }
jb.qi3a8298c2023-09-07 04:48:51 -0700418 return ret;
419}
420
421int qser_wakelock_destroy(int fd)
422{
423 int ret;
424 if(fd < 0 || fd >= MAX_LOCK_NUM)
425 {
jb.qiaa241cf2023-09-18 18:35:00 -0700426 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700427 }
jb.qi52210352023-09-19 23:29:09 -0700428 ret = qser_wakelock_unlock(fd);
429 if(ret)
430 {
431 LYINFLOG("unlock is fail\n");
432 return ret;
433 }
434
jb.qi422ee112023-11-21 03:04:03 -0800435 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700436 return ret;
437}
438
jb.qi422ee112023-11-21 03:04:03 -0800439int qser_whitelist_add(int id)
440{
441 int ret;
442 if(sc_mnet_whitelist_init() != 0)
443 {
444 LYINFLOG("sc_mnet_whitelist_init failed.\n");
445 return -1;
446 }
447
448 ret = sc_mnet_whitelist_add(id);
449 LYINFLOG("sc_mnet_whitelist_add ret %d.\n", ret);
450
451 sc_mnet_whitelist_deinit();
452 return ret;
453
454}
455
456int qser_whitelist_del(int id)
457{
458 int ret;
459 if(sc_mnet_whitelist_init() != 0)
460 {
461 LYINFLOG("sc_mnet_whitelist_init failed.\n");
462 return -1;
463 }
464
465 ret = sc_mnet_whitelist_del(id);
466 LYINFLOG("qser_whitelist_del ret %d.\n", ret);
467
468 sc_mnet_whitelist_deinit();
469 return ret;
470
471}
472
473int qser_whitelist_get(char* whitelist)
474{
475 int ret;
476 if(sc_mnet_whitelist_init() != 0)
477 {
478 LYINFLOG("sc_mnet_whitelist_init failed.\n");
479 return -1;
480 }
481
482 whitelist = sc_mnet_whitelist_get();
483 if(NULL != whitelist)
484 {
485 LYINFLOG("sc_mnet_whitelist_get :%s\n",whitelist);
486 ret = 0;
487 }
488 else
489 {
490 LYINFLOG("sc_mnet_whitelist_get empty\n");
491 ret = -1;
492 }
493 sc_mnet_whitelist_deinit();
494 return ret;
495}
496
497int qser_whitelist_clear(void)
498{
499 int ret;
500 if(sc_mnet_whitelist_init() != 0)
501 {
502 LYINFLOG("sc_mnet_whitelist_init failed.\n");
503 return -1;
504 }
505 ret = sc_mnet_whitelist_clear();
506 LYINFLOG("sc_mnet_whitelist_clear ret %d\n", ret);
507 sc_mnet_whitelist_deinit();
508 return ret;
509
510}
511
512
you.chen21c62b72023-09-08 09:41:11 +0800513DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
514
jb.qi3a8298c2023-09-07 04:48:51 -0700515#ifdef __cplusplus
516}
517#endif
518
519
520