blob: 204660a8801ccbd6065937cefa526079da926d9a [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.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
jb.qie7aac6d2023-12-28 01:32:19 -080031int device_fd;
jb.qifbef5142023-12-18 00:49:10 -080032
33struct dtr_uevent {
34 const char *action;
35 const char *path;
36 const char *subsystem;
37 const char *usbevent;
38};
39
40static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent);
41
42
43qser_lpm_Handler_t g_lpm_handler = NULL;
jb.qi3a8298c2023-09-07 04:48:51 -070044
45
46LOCK_TABLE lock_status;
47
48
49int file_fd;
50int first_run = 0;
jb.qifbef5142023-12-18 00:49:10 -080051pthread_t thid;
jb.qi3a8298c2023-09-07 04:48:51 -070052
53
54int lock_table_init(void)
55{
56 int ret;
57 int err;
58
59 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
60 if(file_fd < 0)
61 {
62 err = errno;
63 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
64 return -err;
65 }
66 memset(&lock_status, 0, sizeof(lock_status));
67 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
68 if(ret <= 0)
69 {
70 LYINFLOG("write fail\n");
71 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070072 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070073 }
74 sync();
75 close(file_fd);
76 return 0;
77}
78
79
80int read_lock_table(void)
81{
82 int err;
83 int ret;
84 int i;
85 LYLOGSET(LOG_INFO);
86 LYLOGEINIT(USER_LOG_TAG);
87
88 if(access(FILE_LOCK_TABLE,0) < 0)
89 {
90 ret = lock_table_init();
91 if(ret < 0)
92 {
jb.qiaa241cf2023-09-18 18:35:00 -070093 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070094 }
95 }
96
97 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
98 if(file_fd < 0)
99 {
100 err = errno;
101 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700102 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700103 }
104
105 memset(&lock_status, 0, sizeof(lock_status));
106 lseek(file_fd,0,SEEK_SET);
107 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
108 LYINFLOG("read ret=%d\n", ret);
109 if(ret <= 0)
110 {
111 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700112 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -0700113 }
114
115 for(i=0;i<MAX_LOCK_NUM;i++)
116 {
117 if(strlen(lock_status.lock_name[i]) != 0)
118 {
119 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
120 }
121 }
122
123 close(file_fd);
124 return 0;
125}
126
127
128int save_lock_table(void)
129{
130 int err;
131 int ret;
132 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
133 if(file_fd < 0)
134 {
135 err = errno;
136 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700137 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700138 }
139 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
140 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
141 LYINFLOG("write ret=%d\n", ret);
142 if(ret <= 0)
143 {
144 LYINFLOG("write fail\n");
145 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700146 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700147 }
148 sync();
149 close(file_fd);
150
151 return 0;
152}
153
154
155int check_lock(char *name)
156{
157 int j;
158 int num;
159 for(j=0;j<MAX_LOCK_NUM;j++)
160 {
161 if(strcmp(lock_status.lock_name[j], name) == 0)
162 {
163 num = j;
164 break;
165 }
166 }
167
168 if(j < MAX_LOCK_NUM)
169 {
170 return num;
171 }
172
173 return -1;
174}
175
176
177int add_lock(char *name)
178{
179 int ret;
180 int i = 0;
181 int num;
182 int check_flag;
183
184 LYINFLOG("name:%s\n", name);
185 ret = read_lock_table();
186 LYINFLOG("read_lock_table ret = %d\n", ret);
187 if(ret <0)
188 {
189 return ret;
190 }
191
192 check_flag = check_lock(name);
193
194 if(check_flag < 0)
195 {
196 for(i=0;i<MAX_LOCK_NUM;i++)
197 {
198 if(strlen(lock_status.lock_name[i]) == 0)
199 {
200 strcpy(lock_status.lock_name[i], name);
201 LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
202 break;
203 }
204 }
205 if(i == MAX_LOCK_NUM)
206 {
jb.qiaa241cf2023-09-18 18:35:00 -0700207 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700208 }
209 else
210 {
211 num = i;
212 }
213 }
214 else
215 {
jb.qiaa241cf2023-09-18 18:35:00 -0700216 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700217 }
218
219 LYINFLOG("num = %d\n", num);
220 ret = save_lock_table();
221 if(ret < 0)
222 {
223 return ret;
224 }
225 return num;
226}
227
228int delete_lock(int fd)
229{
230 int ret;
231 int i;
232 ret = read_lock_table();
233 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
234 ret = save_lock_table();
235 return ret;
236}
237
jb.qifbef5142023-12-18 00:49:10 -0800238static void parse_event(const char *msg, struct dtr_uevent *dtr_uevent)
239{
240 dtr_uevent->action = "";
241 dtr_uevent->path = "";
242 dtr_uevent->subsystem = "";
243 dtr_uevent->usbevent = "";
244
245 while (*msg) {
246 if (!strncmp(msg, "ACTION=", 7))
247 {
248 msg += 7;
249 dtr_uevent->action = msg;
250 }
251 else if (!strncmp(msg, "DEVPATH=", 8))
252 {
253 msg += 8;
254 dtr_uevent->path = msg;
255 }
256 else if (!strncmp(msg, "SUBSYSTEM=", 10))
257 {
258 msg += 10;
259 dtr_uevent->subsystem = msg;
260 }
261 else if (!strncmp(msg, "USBEVENT=", 9)) {
262 msg += 9;
263 dtr_uevent->usbevent = msg;
264 }
265
266 while(*msg++);
267 }
268
269 LYINFLOG("event { '%s', '%s', '%s', '%s'}\n",
270 dtr_uevent->action, dtr_uevent->path, dtr_uevent->subsystem,dtr_uevent->usbevent);
271}
272
273
274
275void *check_dtr(void * arg)
276{
jb.qifbef5142023-12-18 00:49:10 -0800277 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);
jb.qie7aac6d2023-12-28 01:32:19 -0800295 if((!strcmp(dtr_uevent.subsystem, "xp2xp")))
jb.qifbef5142023-12-18 00:49:10 -0800296 {
jb.qie7aac6d2023-12-28 01:32:19 -0800297 if(!strcmp(dtr_uevent.action, "online"))
298 {
299 lpm_edge =E_QL_LPM_FALLING;
300 }
301 else
302 {
303 lpm_edge =E_QL_LPM_RISING;
304 }
jb.qifbef5142023-12-18 00:49:10 -0800305 g_lpm_handler(lpm_edge);
306 }
307 }
308
309 return 0;
310}
311
312
jb.qi3a8298c2023-09-07 04:48:51 -0700313int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
314{
315 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800316 int num;
317
318 g_lpm_handler = qser_lpm_handler;
319 ret = pthread_create(&thid,NULL,check_dtr,NULL);
jb.qi3a8298c2023-09-07 04:48:51 -0700320 if(ret != 0)
321 {
jb.qifbef5142023-12-18 00:49:10 -0800322 LYERRLOG("restart service fail");
jb.qi3a8298c2023-09-07 04:48:51 -0700323 }
324 return ret;
325}
326
327
328int qser_lpm_deinit(void)
329{
330 int ret;
jb.qifbef5142023-12-18 00:49:10 -0800331 ret = pthread_cancel(thid);
jb.qie7aac6d2023-12-28 01:32:19 -0800332 close(device_fd);
jb.qifbef5142023-12-18 00:49:10 -0800333 if(!ret)
jb.qi3a8298c2023-09-07 04:48:51 -0700334 {
jb.qifbef5142023-12-18 00:49:10 -0800335 LYERRLOG("pthread cancel success, lpm deinit success\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700336 }
jb.qifbef5142023-12-18 00:49:10 -0800337 else
jb.qi3a8298c2023-09-07 04:48:51 -0700338 {
jb.qifbef5142023-12-18 00:49:10 -0800339 LYERRLOG("pthread cancel fail, lpm deinit fail\n");
jb.qi3a8298c2023-09-07 04:48:51 -0700340 }
jb.qifbef5142023-12-18 00:49:10 -0800341 return ret;
342
jb.qi3a8298c2023-09-07 04:48:51 -0700343}
344
jb.qiaa241cf2023-09-18 18:35:00 -0700345
jb.qi3a8298c2023-09-07 04:48:51 -0700346int qser_autosuspend_enable(char enable)
347{
348 int ret;
349 if(enable == '0')
350 {
351 ret = lynq_autosleep_disable();
352
353 }
354 else if(enable == '1')
355 {
356 ret = lynq_autosleep_enable();
357 }
358 else
359 {
jb.qiaa241cf2023-09-18 18:35:00 -0700360 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700361
362 }
jb.qiaa241cf2023-09-18 18:35:00 -0700363 if(ret >= 0)
364 {
365 ret = 0;
366 }
jb.qi3a8298c2023-09-07 04:48:51 -0700367 return ret;
368
369}
370
jb.qiaa241cf2023-09-18 18:35:00 -0700371
jb.qi3a8298c2023-09-07 04:48:51 -0700372int qser_wakelock_create(const char *name, size_t len)
373{
374 int ret;
375 if(name == NULL)
376 {
jb.qiaa241cf2023-09-18 18:35:00 -0700377 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700378 }
379 LYINFLOG("%s\n", name);
380 ret = add_lock(name);
381 return ret;
382}
383
384int qser_wakelock_lock(int fd)
385{
386
387 int ret;
388 if(fd < 0 || fd >= MAX_LOCK_NUM)
389 {
jb.qiaa241cf2023-09-18 18:35:00 -0700390 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700391 }
392 ret = read_lock_table();
393 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700394 if(ret > 0)
395 {
396 ret = 0;
397 }
398 else
399 {
400 ret = -1;
401 }
jb.qi3a8298c2023-09-07 04:48:51 -0700402 return ret;
403}
404
405int qser_wakelock_unlock(int fd)
406{
407 int ret;
408 if(fd < 0 || fd >= MAX_LOCK_NUM)
409 {
jb.qiaa241cf2023-09-18 18:35:00 -0700410 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700411 }
412 ret = read_lock_table();
413 if(strlen(lock_status.lock_name[fd]) == 0)
414 {
415 LYINFLOG("%d is null\n", fd);
416 return -1;
417 }
418 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700419 if(ret > 0)
420 {
421 ret = 0;
422 }
423 else
424 {
425 ret = -1;
426 }
jb.qi3a8298c2023-09-07 04:48:51 -0700427 return ret;
428}
429
430int qser_wakelock_destroy(int fd)
431{
432 int ret;
433 if(fd < 0 || fd >= MAX_LOCK_NUM)
434 {
jb.qiaa241cf2023-09-18 18:35:00 -0700435 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700436 }
jb.qi52210352023-09-19 23:29:09 -0700437 ret = qser_wakelock_unlock(fd);
438 if(ret)
439 {
440 LYINFLOG("unlock is fail\n");
441 return ret;
442 }
443
jb.qi422ee112023-11-21 03:04:03 -0800444 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700445 return ret;
446}
447
jb.qi422ee112023-11-21 03:04:03 -0800448int qser_whitelist_add(int id)
449{
450 int ret;
451 if(sc_mnet_whitelist_init() != 0)
452 {
453 LYINFLOG("sc_mnet_whitelist_init failed.\n");
454 return -1;
455 }
456
457 ret = sc_mnet_whitelist_add(id);
458 LYINFLOG("sc_mnet_whitelist_add ret %d.\n", ret);
459
460 sc_mnet_whitelist_deinit();
461 return ret;
462
463}
464
465int qser_whitelist_del(int id)
466{
467 int ret;
468 if(sc_mnet_whitelist_init() != 0)
469 {
470 LYINFLOG("sc_mnet_whitelist_init failed.\n");
471 return -1;
472 }
473
474 ret = sc_mnet_whitelist_del(id);
475 LYINFLOG("qser_whitelist_del ret %d.\n", ret);
476
477 sc_mnet_whitelist_deinit();
478 return ret;
479
480}
481
482int qser_whitelist_get(char* whitelist)
483{
484 int ret;
485 if(sc_mnet_whitelist_init() != 0)
486 {
487 LYINFLOG("sc_mnet_whitelist_init failed.\n");
488 return -1;
489 }
490
491 whitelist = sc_mnet_whitelist_get();
492 if(NULL != whitelist)
493 {
494 LYINFLOG("sc_mnet_whitelist_get :%s\n",whitelist);
495 ret = 0;
496 }
497 else
498 {
499 LYINFLOG("sc_mnet_whitelist_get empty\n");
500 ret = -1;
501 }
502 sc_mnet_whitelist_deinit();
503 return ret;
504}
505
506int qser_whitelist_clear(void)
507{
508 int ret;
509 if(sc_mnet_whitelist_init() != 0)
510 {
511 LYINFLOG("sc_mnet_whitelist_init failed.\n");
512 return -1;
513 }
514 ret = sc_mnet_whitelist_clear();
515 LYINFLOG("sc_mnet_whitelist_clear ret %d\n", ret);
516 sc_mnet_whitelist_deinit();
517 return ret;
518
519}
520
521
you.chen21c62b72023-09-08 09:41:11 +0800522DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
523
jb.qi3a8298c2023-09-07 04:48:51 -0700524#ifdef __cplusplus
525}
526#endif
527
528
529