blob: e82e2c051d73e8e2b26adaf9070958f872ea1476 [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>
10
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.qi422ee112023-11-21 03:04:03 -080014#include "sc_mnet_whitelist.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"
28
29
30LOCK_TABLE lock_status;
31
32
33int file_fd;
34int first_run = 0;
35
36
37int lock_table_init(void)
38{
39 int ret;
40 int err;
41
42 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
43 if(file_fd < 0)
44 {
45 err = errno;
46 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
47 return -err;
48 }
49 memset(&lock_status, 0, sizeof(lock_status));
50 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
51 if(ret <= 0)
52 {
53 LYINFLOG("write fail\n");
54 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070055 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070056 }
57 sync();
58 close(file_fd);
59 return 0;
60}
61
62
63int read_lock_table(void)
64{
65 int err;
66 int ret;
67 int i;
68 LYLOGSET(LOG_INFO);
69 LYLOGEINIT(USER_LOG_TAG);
70
71 if(access(FILE_LOCK_TABLE,0) < 0)
72 {
73 ret = lock_table_init();
74 if(ret < 0)
75 {
jb.qiaa241cf2023-09-18 18:35:00 -070076 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070077 }
78 }
79
80 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
81 if(file_fd < 0)
82 {
83 err = errno;
84 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -070085 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070086 }
87
88 memset(&lock_status, 0, sizeof(lock_status));
89 lseek(file_fd,0,SEEK_SET);
90 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
91 LYINFLOG("read ret=%d\n", ret);
92 if(ret <= 0)
93 {
94 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070095 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070096 }
97
98 for(i=0;i<MAX_LOCK_NUM;i++)
99 {
100 if(strlen(lock_status.lock_name[i]) != 0)
101 {
102 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
103 }
104 }
105
106 close(file_fd);
107 return 0;
108}
109
110
111int save_lock_table(void)
112{
113 int err;
114 int ret;
115 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
116 if(file_fd < 0)
117 {
118 err = errno;
119 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700120 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700121 }
122 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
123 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
124 LYINFLOG("write ret=%d\n", ret);
125 if(ret <= 0)
126 {
127 LYINFLOG("write fail\n");
128 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700129 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700130 }
131 sync();
132 close(file_fd);
133
134 return 0;
135}
136
137
138int check_lock(char *name)
139{
140 int j;
141 int num;
142 for(j=0;j<MAX_LOCK_NUM;j++)
143 {
144 if(strcmp(lock_status.lock_name[j], name) == 0)
145 {
146 num = j;
147 break;
148 }
149 }
150
151 if(j < MAX_LOCK_NUM)
152 {
153 return num;
154 }
155
156 return -1;
157}
158
159
160int add_lock(char *name)
161{
162 int ret;
163 int i = 0;
164 int num;
165 int check_flag;
166
167 LYINFLOG("name:%s\n", name);
168 ret = read_lock_table();
169 LYINFLOG("read_lock_table ret = %d\n", ret);
170 if(ret <0)
171 {
172 return ret;
173 }
174
175 check_flag = check_lock(name);
176
177 if(check_flag < 0)
178 {
179 for(i=0;i<MAX_LOCK_NUM;i++)
180 {
181 if(strlen(lock_status.lock_name[i]) == 0)
182 {
183 strcpy(lock_status.lock_name[i], name);
184 LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
185 break;
186 }
187 }
188 if(i == MAX_LOCK_NUM)
189 {
jb.qiaa241cf2023-09-18 18:35:00 -0700190 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700191 }
192 else
193 {
194 num = i;
195 }
196 }
197 else
198 {
jb.qiaa241cf2023-09-18 18:35:00 -0700199 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700200 }
201
202 LYINFLOG("num = %d\n", num);
203 ret = save_lock_table();
204 if(ret < 0)
205 {
206 return ret;
207 }
208 return num;
209}
210
211int delete_lock(int fd)
212{
213 int ret;
214 int i;
215 ret = read_lock_table();
216 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
217 ret = save_lock_table();
218 return ret;
219}
220
221int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
222{
223 int ret;
224 ret = system("uci set lynq_uci.lynq_autosuspend.init='1'");
225 system("uci commit");
226 if(ret != 0)
227 {
228 LYINFLOG("uci set fail");
229 }
230 ret = system("./etc/init.d/lynq-autosuspend.sh restart");
231 if(ret != 0)
232 {
233 LYINFLOG("restart service fail");
234 }
235 return ret;
236}
237
238
239int qser_lpm_deinit(void)
240{
241 int ret;
242 system("uci set lynq_uci.lynq_autosuspend.init='0'");
243 system("uci commit");
244 if(ret != 0)
245 {
246 LYINFLOG("uci set fail");
247 }
248 system("./etc/init.d/lynq-autosuspend.sh restarts");
249 if(ret != 0)
250 {
251 LYINFLOG("restart service fail");
252 }
253 return 0;
254}
255
jb.qiaa241cf2023-09-18 18:35:00 -0700256
jb.qi3a8298c2023-09-07 04:48:51 -0700257int qser_autosuspend_enable(char enable)
258{
259 int ret;
260 if(enable == '0')
261 {
262 ret = lynq_autosleep_disable();
263
264 }
265 else if(enable == '1')
266 {
267 ret = lynq_autosleep_enable();
268 }
269 else
270 {
jb.qiaa241cf2023-09-18 18:35:00 -0700271 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700272
273 }
jb.qiaa241cf2023-09-18 18:35:00 -0700274 if(ret >= 0)
275 {
276 ret = 0;
277 }
jb.qi3a8298c2023-09-07 04:48:51 -0700278 return ret;
279
280}
281
jb.qiaa241cf2023-09-18 18:35:00 -0700282
jb.qi3a8298c2023-09-07 04:48:51 -0700283int qser_wakelock_create(const char *name, size_t len)
284{
285 int ret;
286 if(name == NULL)
287 {
jb.qiaa241cf2023-09-18 18:35:00 -0700288 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700289 }
290 LYINFLOG("%s\n", name);
291 ret = add_lock(name);
292 return ret;
293}
294
295int qser_wakelock_lock(int fd)
296{
297
298 int ret;
299 if(fd < 0 || fd >= MAX_LOCK_NUM)
300 {
jb.qiaa241cf2023-09-18 18:35:00 -0700301 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700302 }
303 ret = read_lock_table();
304 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700305 if(ret > 0)
306 {
307 ret = 0;
308 }
309 else
310 {
311 ret = -1;
312 }
jb.qi3a8298c2023-09-07 04:48:51 -0700313 return ret;
314}
315
316int qser_wakelock_unlock(int fd)
317{
318 int ret;
319 if(fd < 0 || fd >= MAX_LOCK_NUM)
320 {
jb.qiaa241cf2023-09-18 18:35:00 -0700321 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700322 }
323 ret = read_lock_table();
324 if(strlen(lock_status.lock_name[fd]) == 0)
325 {
326 LYINFLOG("%d is null\n", fd);
327 return -1;
328 }
329 ret = release_wake_lock(lock_status.lock_name[fd]);
jb.qi68fe7782023-09-18 20:40:58 -0700330 if(ret > 0)
331 {
332 ret = 0;
333 }
334 else
335 {
336 ret = -1;
337 }
jb.qi3a8298c2023-09-07 04:48:51 -0700338 return ret;
339}
340
341int qser_wakelock_destroy(int fd)
342{
343 int ret;
344 if(fd < 0 || fd >= MAX_LOCK_NUM)
345 {
jb.qiaa241cf2023-09-18 18:35:00 -0700346 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700347 }
jb.qi52210352023-09-19 23:29:09 -0700348 ret = qser_wakelock_unlock(fd);
349 if(ret)
350 {
351 LYINFLOG("unlock is fail\n");
352 return ret;
353 }
354
jb.qi422ee112023-11-21 03:04:03 -0800355 ret = delete_lock(fd);
jb.qi3a8298c2023-09-07 04:48:51 -0700356 return ret;
357}
358
jb.qi422ee112023-11-21 03:04:03 -0800359int qser_whitelist_add(int id)
360{
361 int ret;
362 if(sc_mnet_whitelist_init() != 0)
363 {
364 LYINFLOG("sc_mnet_whitelist_init failed.\n");
365 return -1;
366 }
367
368 ret = sc_mnet_whitelist_add(id);
369 LYINFLOG("sc_mnet_whitelist_add ret %d.\n", ret);
370
371 sc_mnet_whitelist_deinit();
372 return ret;
373
374}
375
376int qser_whitelist_del(int id)
377{
378 int ret;
379 if(sc_mnet_whitelist_init() != 0)
380 {
381 LYINFLOG("sc_mnet_whitelist_init failed.\n");
382 return -1;
383 }
384
385 ret = sc_mnet_whitelist_del(id);
386 LYINFLOG("qser_whitelist_del ret %d.\n", ret);
387
388 sc_mnet_whitelist_deinit();
389 return ret;
390
391}
392
393int qser_whitelist_get(char* whitelist)
394{
395 int ret;
396 if(sc_mnet_whitelist_init() != 0)
397 {
398 LYINFLOG("sc_mnet_whitelist_init failed.\n");
399 return -1;
400 }
401
402 whitelist = sc_mnet_whitelist_get();
403 if(NULL != whitelist)
404 {
405 LYINFLOG("sc_mnet_whitelist_get :%s\n",whitelist);
406 ret = 0;
407 }
408 else
409 {
410 LYINFLOG("sc_mnet_whitelist_get empty\n");
411 ret = -1;
412 }
413 sc_mnet_whitelist_deinit();
414 return ret;
415}
416
417int qser_whitelist_clear(void)
418{
419 int ret;
420 if(sc_mnet_whitelist_init() != 0)
421 {
422 LYINFLOG("sc_mnet_whitelist_init failed.\n");
423 return -1;
424 }
425 ret = sc_mnet_whitelist_clear();
426 LYINFLOG("sc_mnet_whitelist_clear ret %d\n", ret);
427 sc_mnet_whitelist_deinit();
428 return ret;
429
430}
431
432
you.chen21c62b72023-09-08 09:41:11 +0800433DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
434
jb.qi3a8298c2023-09-07 04:48:51 -0700435#ifdef __cplusplus
436}
437#endif
438
439
440