blob: 3ff26343d6288429bbc3e099f15e1b9ab8abd23d [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"
14
15#ifdef __cplusplus
16extern "C" {
17#endif
18
19
20extern int lynq_autosleep_enable(void);
21extern int lynq_autosleep_disable(void);
22extern int release_wake_lock(char *name);
23extern int acquire_wake_lock(int lock, char *name);
24
25#define USER_LOG_TAG "LYNQ_QSER_AUTOSUSPEND"
26#define FILE_LOCK_TABLE "/tmp/.lock_table"
27
28
29LOCK_TABLE lock_status;
30
31
32int file_fd;
33int first_run = 0;
34
35
36int lock_table_init(void)
37{
38 int ret;
39 int err;
40
41 file_fd = open(FILE_LOCK_TABLE, O_RDWR| O_CREAT,0777);
42 if(file_fd < 0)
43 {
44 err = errno;
45 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
46 return -err;
47 }
48 memset(&lock_status, 0, sizeof(lock_status));
49 ret = write(file_fd, (char*)&lock_status, sizeof(lock_status));
50 if(ret <= 0)
51 {
52 LYINFLOG("write fail\n");
53 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070054 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -070055 }
56 sync();
57 close(file_fd);
58 return 0;
59}
60
61
62int read_lock_table(void)
63{
64 int err;
65 int ret;
66 int i;
67 LYLOGSET(LOG_INFO);
68 LYLOGEINIT(USER_LOG_TAG);
69
70 if(access(FILE_LOCK_TABLE,0) < 0)
71 {
72 ret = lock_table_init();
73 if(ret < 0)
74 {
jb.qiaa241cf2023-09-18 18:35:00 -070075 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070076 }
77 }
78
79 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
80 if(file_fd < 0)
81 {
82 err = errno;
83 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -070084 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070085 }
86
87 memset(&lock_status, 0, sizeof(lock_status));
88 lseek(file_fd,0,SEEK_SET);
89 ret = read(file_fd,(unsigned char *)&lock_status,sizeof(lock_status));
90 LYINFLOG("read ret=%d\n", ret);
91 if(ret <= 0)
92 {
93 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -070094 return E_READ;
jb.qi3a8298c2023-09-07 04:48:51 -070095 }
96
97 for(i=0;i<MAX_LOCK_NUM;i++)
98 {
99 if(strlen(lock_status.lock_name[i]) != 0)
100 {
101 LYINFLOG("fd: %d lock_name:%s strlen:%d\n", i, lock_status.lock_name[i], strlen(lock_status.lock_name[i]));
102 }
103 }
104
105 close(file_fd);
106 return 0;
107}
108
109
110int save_lock_table(void)
111{
112 int err;
113 int ret;
114 file_fd = open(FILE_LOCK_TABLE,O_RDWR);
115 if(file_fd < 0)
116 {
117 err = errno;
118 LYINFLOG("Error open lock_table file:%s\n", strerror(errno));
jb.qiaa241cf2023-09-18 18:35:00 -0700119 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700120 }
121 LYINFLOG("write lock_name[0]: %s\n", lock_status.lock_name[0]);
122 ret = write(file_fd, (unsigned char *)&lock_status, sizeof(lock_status));
123 LYINFLOG("write ret=%d\n", ret);
124 if(ret <= 0)
125 {
126 LYINFLOG("write fail\n");
127 close(file_fd);
jb.qiaa241cf2023-09-18 18:35:00 -0700128 return E_WRITE;
jb.qi3a8298c2023-09-07 04:48:51 -0700129 }
130 sync();
131 close(file_fd);
132
133 return 0;
134}
135
136
137int check_lock(char *name)
138{
139 int j;
140 int num;
141 for(j=0;j<MAX_LOCK_NUM;j++)
142 {
143 if(strcmp(lock_status.lock_name[j], name) == 0)
144 {
145 num = j;
146 break;
147 }
148 }
149
150 if(j < MAX_LOCK_NUM)
151 {
152 return num;
153 }
154
155 return -1;
156}
157
158
159int add_lock(char *name)
160{
161 int ret;
162 int i = 0;
163 int num;
164 int check_flag;
165
166 LYINFLOG("name:%s\n", name);
167 ret = read_lock_table();
168 LYINFLOG("read_lock_table ret = %d\n", ret);
169 if(ret <0)
170 {
171 return ret;
172 }
173
174 check_flag = check_lock(name);
175
176 if(check_flag < 0)
177 {
178 for(i=0;i<MAX_LOCK_NUM;i++)
179 {
180 if(strlen(lock_status.lock_name[i]) == 0)
181 {
182 strcpy(lock_status.lock_name[i], name);
183 LYINFLOG("lock_name[%d] %s\n", i, lock_status.lock_name[i]);
184 break;
185 }
186 }
187 if(i == MAX_LOCK_NUM)
188 {
jb.qiaa241cf2023-09-18 18:35:00 -0700189 return E_TABLE_FULL;
jb.qi3a8298c2023-09-07 04:48:51 -0700190 }
191 else
192 {
193 num = i;
194 }
195 }
196 else
197 {
jb.qiaa241cf2023-09-18 18:35:00 -0700198 return E_LOCK_EXIST;
jb.qi3a8298c2023-09-07 04:48:51 -0700199 }
200
201 LYINFLOG("num = %d\n", num);
202 ret = save_lock_table();
203 if(ret < 0)
204 {
205 return ret;
206 }
207 return num;
208}
209
210int delete_lock(int fd)
211{
212 int ret;
213 int i;
214 ret = read_lock_table();
215 memset(lock_status.lock_name[fd], 0, sizeof(lock_status.lock_name[fd]));
216 ret = save_lock_table();
217 return ret;
218}
219
220int qser_lpm_init(qser_lpm_Handler_t qser_lpm_handler, qser_pm_cfg_t *qser_lpm_cfg)
221{
222 int ret;
223 ret = system("uci set lynq_uci.lynq_autosuspend.init='1'");
224 system("uci commit");
225 if(ret != 0)
226 {
227 LYINFLOG("uci set fail");
228 }
229 ret = system("./etc/init.d/lynq-autosuspend.sh restart");
230 if(ret != 0)
231 {
232 LYINFLOG("restart service fail");
233 }
234 return ret;
235}
236
237
238int qser_lpm_deinit(void)
239{
240 int ret;
241 system("uci set lynq_uci.lynq_autosuspend.init='0'");
242 system("uci commit");
243 if(ret != 0)
244 {
245 LYINFLOG("uci set fail");
246 }
247 system("./etc/init.d/lynq-autosuspend.sh restarts");
248 if(ret != 0)
249 {
250 LYINFLOG("restart service fail");
251 }
252 return 0;
253}
254
jb.qiaa241cf2023-09-18 18:35:00 -0700255
jb.qi3a8298c2023-09-07 04:48:51 -0700256int qser_autosuspend_enable(char enable)
257{
258 int ret;
259 if(enable == '0')
260 {
261 ret = lynq_autosleep_disable();
262
263 }
264 else if(enable == '1')
265 {
266 ret = lynq_autosleep_enable();
267 }
268 else
269 {
jb.qiaa241cf2023-09-18 18:35:00 -0700270 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700271
272 }
jb.qiaa241cf2023-09-18 18:35:00 -0700273 if(ret >= 0)
274 {
275 ret = 0;
276 }
jb.qi3a8298c2023-09-07 04:48:51 -0700277 return ret;
278
279}
280
jb.qiaa241cf2023-09-18 18:35:00 -0700281
jb.qi3a8298c2023-09-07 04:48:51 -0700282int qser_wakelock_create(const char *name, size_t len)
283{
284 int ret;
285 if(name == NULL)
286 {
jb.qiaa241cf2023-09-18 18:35:00 -0700287 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700288 }
289 LYINFLOG("%s\n", name);
290 ret = add_lock(name);
291 return ret;
292}
293
294int qser_wakelock_lock(int fd)
295{
296
297 int ret;
298 if(fd < 0 || fd >= MAX_LOCK_NUM)
299 {
jb.qiaa241cf2023-09-18 18:35:00 -0700300 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700301 }
302 ret = read_lock_table();
303 ret = acquire_wake_lock( 0, lock_status.lock_name[fd]);
304 return ret;
305}
306
307int qser_wakelock_unlock(int fd)
308{
309 int ret;
310 if(fd < 0 || fd >= MAX_LOCK_NUM)
311 {
jb.qiaa241cf2023-09-18 18:35:00 -0700312 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700313 }
314 ret = read_lock_table();
315 if(strlen(lock_status.lock_name[fd]) == 0)
316 {
317 LYINFLOG("%d is null\n", fd);
318 return -1;
319 }
320 ret = release_wake_lock(lock_status.lock_name[fd]);
321 return ret;
322}
323
324int qser_wakelock_destroy(int fd)
325{
326 int ret;
327 if(fd < 0 || fd >= MAX_LOCK_NUM)
328 {
jb.qiaa241cf2023-09-18 18:35:00 -0700329 return E_INPUT_ERROR;
jb.qi3a8298c2023-09-07 04:48:51 -0700330 }
331 ret = delete_lock(fd);
332 return ret;
333}
334
you.chen21c62b72023-09-08 09:41:11 +0800335DEFINE_LYNQ_LIB_LOG(LYNQ_QSER_AUTOSUSPEND)
336
jb.qi3a8298c2023-09-07 04:48:51 -0700337#ifdef __cplusplus
338}
339#endif
340
341
342