blob: defbff34024def7dfe99fabe9624c7bce7b59b7c [file] [log] [blame]
xjd5ccac02022-04-02 15:41:13 +08001
2#include <stdio.h>
3#include <stdlib.h>
4#include <errno.h>
5#include <string.h>
6#include <sys/types.h>
7#include <sys/socket.h>
8#include <sys/un.h>
9#include <unistd.h>
10#include <dlfcn.h>
xjac563942022-05-04 16:50:24 +080011#include <pthread.h>
12#include <stdbool.h>
13#include <time.h>
xjd5ccac02022-04-02 15:41:13 +080014
15#define LOG_TAG "libautosuspend"
16
17// #include <liblog/lynq_deflog.h>
18#include <log/log.h>
19
xjac563942022-05-04 16:50:24 +080020#define SERVER_CMD_PATH "/tmp/autosuspend.cmd.server"
21#define SERVER_DATA_PATH "/tmp/autosuspend.data.server"
xjd5ccac02022-04-02 15:41:13 +080022// #define CLIENT_PATH "/tmp/autosuspend.client"
23
24
xjac563942022-05-04 16:50:24 +080025static int client_sock_fd;
26
27static int client_data_sock_fd;
28
29static bool libautosuspend_inited;
30
jb.qi5fb67582023-02-09 00:12:59 -080031bool feedback_flag = true; //add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time
xjac563942022-05-04 16:50:24 +080032// static bool libautosuspend_enabled;
33
34// static pthread_mutex_t get_feedback_mutex = PTHREAD_MUTEX_INITIALIZER;
35
36// static pthread_cond_t get_feedback_cond = PTHREAD_COND_INITIALIZER;
37
38static pthread_mutex_t client_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
39
40static pthread_mutex_t client_data_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
41
42static pthread_mutex_t feedback_got_mutex = PTHREAD_MUTEX_INITIALIZER;
43
44static pthread_cond_t feedback_got_cond = PTHREAD_COND_INITIALIZER;
45
46struct time_info_t
xjd5ccac02022-04-02 15:41:13 +080047{
xjac563942022-05-04 16:50:24 +080048 long sleep_start_time;
49 long wakeup_time;
50};
51
52static struct time_info_t time_info_client;
53
54static ssize_t Read(int fd, void *ptr, size_t nbytes)
55{
56 ssize_t n;
57
58 while((n = read(fd, ptr, nbytes)) == -1)
59 {
60 //printf("READ,%d\n",fd);
61 if (errno == EINTR)
62 {
63 ALOGI("read error eintr\n");
64 continue;
65 }
66 else if(errno == EAGAIN || errno == EWOULDBLOCK)
67 {
68 ALOGI("read time out\n");
69 return -2;
70 }
71 else
72 {
73 ALOGI("read error\n");
74 return -1;
75 }
76 }
77 //sleep(2);
78 //printf("READ1,%d\n", fd);
79 return n;
80}
81
82static ssize_t Write(int fd, const void *ptr, size_t nbytes)
83{
84 ssize_t n;
85
86 while((n = write(fd, ptr, nbytes)) == -1)
87 {
88 if (errno == EINTR)
89 continue;
90 else if(errno == EPIPE)
91 {
92 ALOGI("write error epipe\n");
93 return -1;
94 }
95 else
96 return -1;
97 }
98 return n;
99}
100
101static int Close(int fd)
102{
jb.qie82adab2023-12-10 17:19:08 -0800103 if (close(fd) == -1)
xjac563942022-05-04 16:50:24 +0800104 {
105 ALOGI("Close error\n");
106 return -1;
107 }
108 return 0;
109}
110
111static int connect_to_server(int *cfd, char *client_path, char *server_path)
112{
113 int rc;
xjd5ccac02022-04-02 15:41:13 +0800114 struct sockaddr_un server_sockaddr;
115 struct sockaddr_un client_sockaddr;
116
117
xjac563942022-05-04 16:50:24 +0800118 ALOGI("Start bind and connect to the service.\n");
xjd5ccac02022-04-02 15:41:13 +0800119
120 /**************************************/
121 /* Create a UNIX domain stream socket */
122 /**************************************/
xjac563942022-05-04 16:50:24 +0800123 *cfd = socket(AF_UNIX, SOCK_STREAM, 0);
124 if (*cfd == -1) {
xjd5ccac02022-04-02 15:41:13 +0800125 ALOGI("SOCKET ERROR ");
xjac563942022-05-04 16:50:24 +0800126 return -1;
xjd5ccac02022-04-02 15:41:13 +0800127 }
128
129 /***************************************/
130 /* Set up the UNIX sockaddr structure */
131 /* by using AF_UNIX for the family and */
132 /* giving it a filepath to bind to. */
133 /* */
134 /* Unlink the file so the bind will */
135 /* succeed, then bind to that file. */
136 /***************************************/
xjac563942022-05-04 16:50:24 +0800137 client_sockaddr.sun_family = AF_UNIX;
138 strcpy(client_sockaddr.sun_path, client_path);
xjd5ccac02022-04-02 15:41:13 +0800139
140 unlink(client_sockaddr.sun_path);
xjac563942022-05-04 16:50:24 +0800141
142 rc = bind(*cfd, (struct sockaddr *) &client_sockaddr, sizeof(client_sockaddr));
xjd5ccac02022-04-02 15:41:13 +0800143 if (rc == -1){
144 ALOGI("BIND ERROR ");
xjac563942022-05-04 16:50:24 +0800145 Close(*cfd);
146 return -1;
xjd5ccac02022-04-02 15:41:13 +0800147 }
148
149 /***************************************/
150 /* Set up the UNIX sockaddr structure */
151 /* for the server socket and connect */
152 /* to it. */
153 /***************************************/
154 server_sockaddr.sun_family = AF_UNIX;
xjac563942022-05-04 16:50:24 +0800155
156 strcpy(server_sockaddr.sun_path, server_path);
157
158 rc = connect(*cfd, (struct sockaddr *) &server_sockaddr, sizeof(client_sockaddr));
xjd5ccac02022-04-02 15:41:13 +0800159 if(rc == -1){
160 ALOGI("CONNECT ERROR ");
xjac563942022-05-04 16:50:24 +0800161 Close(*cfd);
xjd5ccac02022-04-02 15:41:13 +0800162 return -3;
163 }
xjac563942022-05-04 16:50:24 +0800164
165 return 0;
166
167}
168
169static void *deal_get_feedback(void *sockfd)
170{
171 int rc;
jb.qie82adab2023-12-10 17:19:08 -0800172 int ret;
173 int num[10]={5,5,10,10,30,30,60,60,90,90};
174 int i = 0;
xjac563942022-05-04 16:50:24 +0800175 while (1)
176 {
xjac563942022-05-04 16:50:24 +0800177
178 ALOGI("start get feedback from the service.\n");
xjac563942022-05-04 16:50:24 +0800179 pthread_mutex_lock(&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800180 memset(&time_info_client,0,sizeof(struct time_info_t));
jb.qie82adab2023-12-10 17:19:08 -0800181 rc = Read(client_data_sock_fd,&time_info_client,sizeof(struct time_info_t));
182 ALOGI("deal_get_feedback: rc=%d, client_data_sock_fd=%d.\n", rc, client_data_sock_fd);
xjac563942022-05-04 16:50:24 +0800183 if(rc == -1)
jb.qie82adab2023-12-10 17:19:08 -0800184 {
xjac563942022-05-04 16:50:24 +0800185 ALOGI("client read wakeup_feedback struct fail.\n");
jb.qie82adab2023-12-10 17:19:08 -0800186 Close(client_data_sock_fd);
xjac563942022-05-04 16:50:24 +0800187 pthread_mutex_unlock(&feedback_got_mutex);
188 break ;
189 }
190 else if(rc == -2)
191 {
192 ALOGI("client read wakeup_feedback struct timeout.\n");
193 pthread_mutex_unlock(&feedback_got_mutex);
194 continue;
195 }
jb.qie82adab2023-12-10 17:19:08 -0800196 else if(rc == 0)
197 {
198 if(i<10)
199 {
200 sleep(num[i]);
201 }
202 else
203 {
204 sleep(120);
205 }
206 i++;
207 system("rm -rf /tmp/*data.client");
208 system("rm -rf /tmp/*cmd.client");
209 ret = lynq_autosleep_retry();
210 if(ret > 0)
211 {
212 ALOGI("lynq autosuspend retry success\n");
213 i = 0;
214 }
215 pthread_mutex_unlock(&feedback_got_mutex);
216 continue;
217 }
xjac563942022-05-04 16:50:24 +0800218
219 ALOGI("system sleep_start timestamps : %ld ms\n",time_info_client.sleep_start_time);
220 ALOGI("system wakeup timestamps : %ld ms\n",time_info_client.wakeup_time);
221
xjac563942022-05-04 16:50:24 +0800222
223 pthread_mutex_unlock(&feedback_got_mutex);
jb.qidd9c9892022-09-15 00:48:16 -0700224 usleep(10000); //给libautosuspend_get_feedback函数时间进入wait,不可删除,但可以减少
xjac563942022-05-04 16:50:24 +0800225 pthread_cond_broadcast(&feedback_got_cond);
jb.qidd9c9892022-09-15 00:48:16 -0700226 usleep(10000); //希望多给libautosuspend_get_feedback函数拿到锁的机会,不可删除,但可以减少
jb.qie82adab2023-12-10 17:19:08 -0800227
xjac563942022-05-04 16:50:24 +0800228 }
jb.qie82adab2023-12-10 17:19:08 -0800229
xjac563942022-05-04 16:50:24 +0800230}
231
232
233static int libautosuspend_init()
234{
235 if (libautosuspend_inited)
236 {
237 return 0;
238 }
239
240 ALOGI("Start libautosuspend_init.\n");
241
242 char client_cmd_path[40];
243 char client_data_path[40];
244
245
246 sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
247 sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
248
249 pthread_mutex_lock(&client_fd_mutex);
250
251 if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
252 {
253 ALOGI("cmd channel connect error.\n");
254 pthread_mutex_unlock(&client_fd_mutex);
255 return -1;
256 }
257
258 if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
259 {
260 ALOGI("data channel connect error.\n");
261 pthread_mutex_unlock(&client_fd_mutex);
262 return -1;
263 }
264
265 pthread_t feedback_tid;
jb.qie82adab2023-12-10 17:19:08 -0800266 pthread_create(&feedback_tid,NULL,deal_get_feedback,NULL);
xjac563942022-05-04 16:50:24 +0800267 pthread_detach(feedback_tid);
xjac563942022-05-04 16:50:24 +0800268 libautosuspend_inited = true;
269
270 pthread_mutex_unlock(&client_fd_mutex);
jb.qie82adab2023-12-10 17:19:08 -0800271
272
xjac563942022-05-04 16:50:24 +0800273 return 0;
274
275}
276
277static int send_cmd(char * value,int len)
278{
279 int rc;
280
281 if(value == NULL)
282 {
283 return -1;
284 }
xjd5ccac02022-04-02 15:41:13 +0800285
286 /************************************/
287 /* Copy the data to the buffer and */
288 /* send it to the server socket. */
289 /************************************/
xjac563942022-05-04 16:50:24 +0800290 // strcpy(buf, DATA);
291
292 ALOGI("Sending data...\n");
293 rc = send(client_sock_fd, value, len, 0);
xjd5ccac02022-04-02 15:41:13 +0800294 if (rc == -1) {
295 ALOGI("SEND ERROR ");
xjac563942022-05-04 16:50:24 +0800296 Close(client_sock_fd);
xjd5ccac02022-04-02 15:41:13 +0800297 return -2;
298 }
299 else {
300 ALOGI("Data sent: %s\n",value);
301 }
302
xjac563942022-05-04 16:50:24 +0800303 // Close(client_sock);
xjd5ccac02022-04-02 15:41:13 +0800304
305 return rc;
306
307}
308
jb.qie82adab2023-12-10 17:19:08 -0800309static int libautosuspend_reconnect(void)
310{
311
312
313 ALOGI("Start libautosuspend_init.\n");
314
315 char client_cmd_path[40];
316 char client_data_path[40];
317
318 sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
319 sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
320
321
322 pthread_mutex_lock(&client_fd_mutex);
323
324 if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
325 {
326 ALOGI("cmd channel connect error.\n");
327 pthread_mutex_unlock(&client_fd_mutex);
328 return -1;
329 }
330
331 if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
332 {
333 ALOGI("data channel connect error.\n");
334 pthread_mutex_unlock(&client_fd_mutex);
335 return -1;
336 }
337
338 pthread_mutex_unlock(&client_fd_mutex);
339
340 return 0;
341
342}
343
344
345int lynq_autosleep_retry(void)
xjd5ccac02022-04-02 15:41:13 +0800346{
xjac563942022-05-04 16:50:24 +0800347 char value[15]="enable";
348 char res[15];
349
jb.qie82adab2023-12-10 17:19:08 -0800350 if(libautosuspend_reconnect() != 0)
xjac563942022-05-04 16:50:24 +0800351 {
352 return -1;
353 }
354
xjac563942022-05-04 16:50:24 +0800355 pthread_mutex_lock(&client_fd_mutex);
356
357 int rc = send_cmd(value,strlen(value));
358 if(rc < 0)
359 {
360 ALOGI("libautosuspend send enable cmd fail.\n");
361 pthread_mutex_unlock(&client_fd_mutex);
362 return -1;
363 }
jb.qie82adab2023-12-10 17:19:08 -0800364 pthread_mutex_unlock(&client_fd_mutex);
xjac563942022-05-04 16:50:24 +0800365
jb.qie82adab2023-12-10 17:19:08 -0800366 return 1;
367
368}
xjac563942022-05-04 16:50:24 +0800369
xjac563942022-05-04 16:50:24 +0800370
xjac563942022-05-04 16:50:24 +0800371
jb.qie82adab2023-12-10 17:19:08 -0800372int lynq_autosleep_enable(void)
373{
374 char value[15]="enable";
375 char res[15];
xjac563942022-05-04 16:50:24 +0800376
jb.qie82adab2023-12-10 17:19:08 -0800377 if(libautosuspend_init() != 0)
378 {
379 return -1;
380 }
381
382 pthread_mutex_lock(&client_fd_mutex);
383
384 int rc = send_cmd(value,strlen(value));
385 if(rc < 0)
386 {
387 ALOGI("libautosuspend send enable cmd fail.\n");
388 pthread_mutex_unlock(&client_fd_mutex);
389 return -1;
390 }
xjac563942022-05-04 16:50:24 +0800391 pthread_mutex_unlock(&client_fd_mutex);
392
xj0bb28dc2022-05-10 08:22:22 +0800393 return 1;
xjac563942022-05-04 16:50:24 +0800394
xjd5ccac02022-04-02 15:41:13 +0800395}
396
397int lynq_autosleep_disable(void)
398{
xjac563942022-05-04 16:50:24 +0800399 char value[15]="disable";
400 char res[15];
401
402 if(libautosuspend_init() != 0)
403 {
404 return -1;
405 }
406
xjac563942022-05-04 16:50:24 +0800407
408 pthread_mutex_lock(&client_fd_mutex);
409
410 int rc = send_cmd(value,strlen(value));
411 if(rc < 0)
412 {
413 ALOGI("libautosuspend send disable cmd fail.\n");
414 pthread_mutex_unlock(&client_fd_mutex);
415 return -1;
416 }
417
xjac563942022-05-04 16:50:24 +0800418 pthread_mutex_unlock(&client_fd_mutex);
419
xj0bb28dc2022-05-10 08:22:22 +0800420 return 1;
xjd5ccac02022-04-02 15:41:13 +0800421
422}
423
424
xjac563942022-05-04 16:50:24 +0800425
xjda4933f2022-05-09 10:34:43 +0800426int libautosuspend_get_feedback(struct time_info_t *time_info)
xjac563942022-05-04 16:50:24 +0800427{
xjac563942022-05-04 16:50:24 +0800428
429 ALOGI("start get feedback from the service.\n");
xjac563942022-05-04 16:50:24 +0800430 memset(time_info,0,sizeof(struct time_info_t));
431
xjac563942022-05-04 16:50:24 +0800432
433 ALOGI("libautosuspend_get_feedback wait.\n");
xjac563942022-05-04 16:50:24 +0800434 pthread_mutex_lock(&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800435 pthread_cond_wait(&feedback_got_cond,&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800436 memcpy(time_info,&time_info_client,sizeof(struct time_info_t));
xjac563942022-05-04 16:50:24 +0800437 ALOGI("libautosuspend_get_feedback success.\n");
xjac563942022-05-04 16:50:24 +0800438 pthread_mutex_unlock(&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800439
440 return 0;
441
442}
443
444
xjda4933f2022-05-09 10:34:43 +0800445int lynq_wait_wakeup_event(long *sleep_start_time, long * wakeup_time)
446{
jb.qie82adab2023-12-10 17:19:08 -0800447 int *socket_timeout = NULL;
448 struct time_info_t time_info;
449 int ret = 0;
450 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time start*/
451 if(feedback_flag == true)
452 {
453 if(libautosuspend_init() != 0)
454 {
455 return -1;
456 }
457 }
458 feedback_flag = false;
459 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time end*/
460 memset(&time_info,0,sizeof(struct time_info_t));
461 if(sleep_start_time == NULL || wakeup_time == NULL )
462 {
463 ALOGI("lynq_wait_wakeup_event input errors.\n");
464 return -1;
465 }
466 ret=libautosuspend_get_feedback(&time_info);
467 if(ret == 0)
468 {
469 *sleep_start_time = time_info.sleep_start_time;
470 *wakeup_time = time_info.wakeup_time;
471 return 0;
472 }
473 else
474 {
475 return -1;
476 }
xjda4933f2022-05-09 10:34:43 +0800477
478
479}
xjac563942022-05-04 16:50:24 +0800480
xjd5ccac02022-04-02 15:41:13 +0800481