blob: c8f8d0cf077a052ebe0ebafafb9279512ab5f219 [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>
jb.qi5c94c2d2024-12-04 00:10:12 -080014#include <unistd.h>
15#include <fcntl.h>
xjd5ccac02022-04-02 15:41:13 +080016
17#define LOG_TAG "libautosuspend"
18
19// #include <liblog/lynq_deflog.h>
20#include <log/log.h>
21
xjac563942022-05-04 16:50:24 +080022#define SERVER_CMD_PATH "/tmp/autosuspend.cmd.server"
23#define SERVER_DATA_PATH "/tmp/autosuspend.data.server"
xjd5ccac02022-04-02 15:41:13 +080024// #define CLIENT_PATH "/tmp/autosuspend.client"
25
jb.qi5c94c2d2024-12-04 00:10:12 -080026typedef void (*lynq_lpm_Handler_t)(int lpm_edge);
27
28lynq_lpm_Handler_t g_lpm_handler = NULL;
29
30pthread_t dtr_thid;
31
32int g_init_flag;
xjd5ccac02022-04-02 15:41:13 +080033
xjac563942022-05-04 16:50:24 +080034static int client_sock_fd;
35
36static int client_data_sock_fd;
37
38static bool libautosuspend_inited;
39
jb.qi5fb67582023-02-09 00:12:59 -080040bool feedback_flag = true; //add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time
xjac563942022-05-04 16:50:24 +080041// static bool libautosuspend_enabled;
42
43// static pthread_mutex_t get_feedback_mutex = PTHREAD_MUTEX_INITIALIZER;
44
45// static pthread_cond_t get_feedback_cond = PTHREAD_COND_INITIALIZER;
46
47static pthread_mutex_t client_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
48
49static pthread_mutex_t client_data_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
50
51static pthread_mutex_t feedback_got_mutex = PTHREAD_MUTEX_INITIALIZER;
52
53static pthread_cond_t feedback_got_cond = PTHREAD_COND_INITIALIZER;
54
55struct time_info_t
xjd5ccac02022-04-02 15:41:13 +080056{
xjac563942022-05-04 16:50:24 +080057 long sleep_start_time;
58 long wakeup_time;
59};
60
61static struct time_info_t time_info_client;
62
63static ssize_t Read(int fd, void *ptr, size_t nbytes)
64{
65 ssize_t n;
66
67 while((n = read(fd, ptr, nbytes)) == -1)
68 {
69 //printf("READ,%d\n",fd);
70 if (errno == EINTR)
71 {
72 ALOGI("read error eintr\n");
73 continue;
74 }
75 else if(errno == EAGAIN || errno == EWOULDBLOCK)
76 {
77 ALOGI("read time out\n");
78 return -2;
79 }
80 else
81 {
82 ALOGI("read error\n");
83 return -1;
84 }
85 }
86 //sleep(2);
87 //printf("READ1,%d\n", fd);
88 return n;
89}
90
91static ssize_t Write(int fd, const void *ptr, size_t nbytes)
92{
93 ssize_t n;
94
95 while((n = write(fd, ptr, nbytes)) == -1)
96 {
97 if (errno == EINTR)
98 continue;
99 else if(errno == EPIPE)
100 {
101 ALOGI("write error epipe\n");
102 return -1;
103 }
104 else
105 return -1;
106 }
107 return n;
108}
109
110static int Close(int fd)
111{
jb.qie82adab2023-12-10 17:19:08 -0800112 if (close(fd) == -1)
xjac563942022-05-04 16:50:24 +0800113 {
114 ALOGI("Close error\n");
115 return -1;
116 }
117 return 0;
118}
119
120static int connect_to_server(int *cfd, char *client_path, char *server_path)
121{
122 int rc;
xjd5ccac02022-04-02 15:41:13 +0800123 struct sockaddr_un server_sockaddr;
124 struct sockaddr_un client_sockaddr;
125
126
xjac563942022-05-04 16:50:24 +0800127 ALOGI("Start bind and connect to the service.\n");
xjd5ccac02022-04-02 15:41:13 +0800128
129 /**************************************/
130 /* Create a UNIX domain stream socket */
131 /**************************************/
xjac563942022-05-04 16:50:24 +0800132 *cfd = socket(AF_UNIX, SOCK_STREAM, 0);
133 if (*cfd == -1) {
xjd5ccac02022-04-02 15:41:13 +0800134 ALOGI("SOCKET ERROR ");
xjac563942022-05-04 16:50:24 +0800135 return -1;
xjd5ccac02022-04-02 15:41:13 +0800136 }
137
138 /***************************************/
139 /* Set up the UNIX sockaddr structure */
140 /* by using AF_UNIX for the family and */
141 /* giving it a filepath to bind to. */
142 /* */
143 /* Unlink the file so the bind will */
144 /* succeed, then bind to that file. */
145 /***************************************/
xjac563942022-05-04 16:50:24 +0800146 client_sockaddr.sun_family = AF_UNIX;
147 strcpy(client_sockaddr.sun_path, client_path);
xjd5ccac02022-04-02 15:41:13 +0800148
149 unlink(client_sockaddr.sun_path);
xjac563942022-05-04 16:50:24 +0800150
151 rc = bind(*cfd, (struct sockaddr *) &client_sockaddr, sizeof(client_sockaddr));
xjd5ccac02022-04-02 15:41:13 +0800152 if (rc == -1){
153 ALOGI("BIND ERROR ");
xjac563942022-05-04 16:50:24 +0800154 Close(*cfd);
155 return -1;
xjd5ccac02022-04-02 15:41:13 +0800156 }
157
158 /***************************************/
159 /* Set up the UNIX sockaddr structure */
160 /* for the server socket and connect */
161 /* to it. */
162 /***************************************/
163 server_sockaddr.sun_family = AF_UNIX;
xjac563942022-05-04 16:50:24 +0800164
165 strcpy(server_sockaddr.sun_path, server_path);
166
167 rc = connect(*cfd, (struct sockaddr *) &server_sockaddr, sizeof(client_sockaddr));
xjd5ccac02022-04-02 15:41:13 +0800168 if(rc == -1){
169 ALOGI("CONNECT ERROR ");
xjac563942022-05-04 16:50:24 +0800170 Close(*cfd);
xjd5ccac02022-04-02 15:41:13 +0800171 return -3;
172 }
xjac563942022-05-04 16:50:24 +0800173
174 return 0;
175
176}
177
178static void *deal_get_feedback(void *sockfd)
179{
180 int rc;
jb.qie82adab2023-12-10 17:19:08 -0800181 int ret;
182 int num[10]={5,5,10,10,30,30,60,60,90,90};
183 int i = 0;
xjac563942022-05-04 16:50:24 +0800184 while (1)
185 {
xjac563942022-05-04 16:50:24 +0800186
187 ALOGI("start get feedback from the service.\n");
xjac563942022-05-04 16:50:24 +0800188 pthread_mutex_lock(&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800189 memset(&time_info_client,0,sizeof(struct time_info_t));
jb.qie82adab2023-12-10 17:19:08 -0800190 rc = Read(client_data_sock_fd,&time_info_client,sizeof(struct time_info_t));
191 ALOGI("deal_get_feedback: rc=%d, client_data_sock_fd=%d.\n", rc, client_data_sock_fd);
xjac563942022-05-04 16:50:24 +0800192 if(rc == -1)
jb.qie82adab2023-12-10 17:19:08 -0800193 {
xjac563942022-05-04 16:50:24 +0800194 ALOGI("client read wakeup_feedback struct fail.\n");
jb.qie82adab2023-12-10 17:19:08 -0800195 Close(client_data_sock_fd);
xjac563942022-05-04 16:50:24 +0800196 pthread_mutex_unlock(&feedback_got_mutex);
197 break ;
198 }
199 else if(rc == -2)
200 {
201 ALOGI("client read wakeup_feedback struct timeout.\n");
202 pthread_mutex_unlock(&feedback_got_mutex);
203 continue;
204 }
jb.qie82adab2023-12-10 17:19:08 -0800205 else if(rc == 0)
206 {
207 if(i<10)
208 {
209 sleep(num[i]);
210 }
211 else
212 {
213 sleep(120);
214 }
215 i++;
216 system("rm -rf /tmp/*data.client");
217 system("rm -rf /tmp/*cmd.client");
218 ret = lynq_autosleep_retry();
219 if(ret > 0)
220 {
221 ALOGI("lynq autosuspend retry success\n");
222 i = 0;
223 }
224 pthread_mutex_unlock(&feedback_got_mutex);
225 continue;
226 }
xjac563942022-05-04 16:50:24 +0800227
228 ALOGI("system sleep_start timestamps : %ld ms\n",time_info_client.sleep_start_time);
229 ALOGI("system wakeup timestamps : %ld ms\n",time_info_client.wakeup_time);
230
xjac563942022-05-04 16:50:24 +0800231
232 pthread_mutex_unlock(&feedback_got_mutex);
jb.qidd9c9892022-09-15 00:48:16 -0700233 usleep(10000); //给libautosuspend_get_feedback函数时间进入wait,不可删除,但可以减少
xjac563942022-05-04 16:50:24 +0800234 pthread_cond_broadcast(&feedback_got_cond);
jb.qidd9c9892022-09-15 00:48:16 -0700235 usleep(10000); //希望多给libautosuspend_get_feedback函数拿到锁的机会,不可删除,但可以减少
jb.qie82adab2023-12-10 17:19:08 -0800236
xjac563942022-05-04 16:50:24 +0800237 }
jb.qie82adab2023-12-10 17:19:08 -0800238
xjac563942022-05-04 16:50:24 +0800239}
240
241
242static int libautosuspend_init()
243{
244 if (libautosuspend_inited)
245 {
246 return 0;
247 }
248
249 ALOGI("Start libautosuspend_init.\n");
250
251 char client_cmd_path[40];
252 char client_data_path[40];
253
254
255 sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
256 sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
257
258 pthread_mutex_lock(&client_fd_mutex);
259
260 if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
261 {
262 ALOGI("cmd channel connect error.\n");
263 pthread_mutex_unlock(&client_fd_mutex);
264 return -1;
265 }
266
267 if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
268 {
269 ALOGI("data channel connect error.\n");
270 pthread_mutex_unlock(&client_fd_mutex);
271 return -1;
272 }
273
274 pthread_t feedback_tid;
jb.qie82adab2023-12-10 17:19:08 -0800275 pthread_create(&feedback_tid,NULL,deal_get_feedback,NULL);
xjac563942022-05-04 16:50:24 +0800276 pthread_detach(feedback_tid);
xjac563942022-05-04 16:50:24 +0800277 libautosuspend_inited = true;
278
279 pthread_mutex_unlock(&client_fd_mutex);
jb.qie82adab2023-12-10 17:19:08 -0800280
281
xjac563942022-05-04 16:50:24 +0800282 return 0;
283
284}
285
286static int send_cmd(char * value,int len)
287{
288 int rc;
289
290 if(value == NULL)
291 {
292 return -1;
293 }
xjd5ccac02022-04-02 15:41:13 +0800294
295 /************************************/
296 /* Copy the data to the buffer and */
297 /* send it to the server socket. */
298 /************************************/
xjac563942022-05-04 16:50:24 +0800299 // strcpy(buf, DATA);
300
301 ALOGI("Sending data...\n");
302 rc = send(client_sock_fd, value, len, 0);
xjd5ccac02022-04-02 15:41:13 +0800303 if (rc == -1) {
304 ALOGI("SEND ERROR ");
xjac563942022-05-04 16:50:24 +0800305 Close(client_sock_fd);
xjd5ccac02022-04-02 15:41:13 +0800306 return -2;
307 }
308 else {
309 ALOGI("Data sent: %s\n",value);
310 }
311
xjac563942022-05-04 16:50:24 +0800312 // Close(client_sock);
xjd5ccac02022-04-02 15:41:13 +0800313
314 return rc;
315
316}
317
jb.qie82adab2023-12-10 17:19:08 -0800318static int libautosuspend_reconnect(void)
319{
320
321
322 ALOGI("Start libautosuspend_init.\n");
323
324 char client_cmd_path[40];
325 char client_data_path[40];
326
327 sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
328 sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
329
330
331 pthread_mutex_lock(&client_fd_mutex);
332
333 if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
334 {
335 ALOGI("cmd channel connect error.\n");
336 pthread_mutex_unlock(&client_fd_mutex);
337 return -1;
338 }
339
340 if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
341 {
342 ALOGI("data channel connect error.\n");
343 pthread_mutex_unlock(&client_fd_mutex);
344 return -1;
345 }
346
347 pthread_mutex_unlock(&client_fd_mutex);
348
349 return 0;
350
351}
352
353
354int lynq_autosleep_retry(void)
xjd5ccac02022-04-02 15:41:13 +0800355{
xjac563942022-05-04 16:50:24 +0800356 char value[15]="enable";
357 char res[15];
358
jb.qie82adab2023-12-10 17:19:08 -0800359 if(libautosuspend_reconnect() != 0)
xjac563942022-05-04 16:50:24 +0800360 {
361 return -1;
362 }
363
xjac563942022-05-04 16:50:24 +0800364 pthread_mutex_lock(&client_fd_mutex);
365
366 int rc = send_cmd(value,strlen(value));
367 if(rc < 0)
368 {
369 ALOGI("libautosuspend send enable cmd fail.\n");
370 pthread_mutex_unlock(&client_fd_mutex);
371 return -1;
372 }
jb.qie82adab2023-12-10 17:19:08 -0800373 pthread_mutex_unlock(&client_fd_mutex);
xjac563942022-05-04 16:50:24 +0800374
jb.qie82adab2023-12-10 17:19:08 -0800375 return 1;
376
377}
xjac563942022-05-04 16:50:24 +0800378
xjac563942022-05-04 16:50:24 +0800379
xjac563942022-05-04 16:50:24 +0800380
jb.qie82adab2023-12-10 17:19:08 -0800381int lynq_autosleep_enable(void)
382{
383 char value[15]="enable";
384 char res[15];
xjac563942022-05-04 16:50:24 +0800385
jb.qie82adab2023-12-10 17:19:08 -0800386 if(libautosuspend_init() != 0)
387 {
388 return -1;
389 }
390
391 pthread_mutex_lock(&client_fd_mutex);
392
393 int rc = send_cmd(value,strlen(value));
394 if(rc < 0)
395 {
396 ALOGI("libautosuspend send enable cmd fail.\n");
397 pthread_mutex_unlock(&client_fd_mutex);
398 return -1;
399 }
xjac563942022-05-04 16:50:24 +0800400 pthread_mutex_unlock(&client_fd_mutex);
401
xj0bb28dc2022-05-10 08:22:22 +0800402 return 1;
xjac563942022-05-04 16:50:24 +0800403
xjd5ccac02022-04-02 15:41:13 +0800404}
405
406int lynq_autosleep_disable(void)
407{
xjac563942022-05-04 16:50:24 +0800408 char value[15]="disable";
409 char res[15];
410
411 if(libautosuspend_init() != 0)
412 {
413 return -1;
414 }
415
xjac563942022-05-04 16:50:24 +0800416
417 pthread_mutex_lock(&client_fd_mutex);
418
419 int rc = send_cmd(value,strlen(value));
420 if(rc < 0)
421 {
422 ALOGI("libautosuspend send disable cmd fail.\n");
423 pthread_mutex_unlock(&client_fd_mutex);
424 return -1;
425 }
426
xjac563942022-05-04 16:50:24 +0800427 pthread_mutex_unlock(&client_fd_mutex);
428
xj0bb28dc2022-05-10 08:22:22 +0800429 return 1;
xjd5ccac02022-04-02 15:41:13 +0800430
431}
432
433
xjac563942022-05-04 16:50:24 +0800434
xjda4933f2022-05-09 10:34:43 +0800435int libautosuspend_get_feedback(struct time_info_t *time_info)
xjac563942022-05-04 16:50:24 +0800436{
xjac563942022-05-04 16:50:24 +0800437
438 ALOGI("start get feedback from the service.\n");
xjac563942022-05-04 16:50:24 +0800439 memset(time_info,0,sizeof(struct time_info_t));
440
xjac563942022-05-04 16:50:24 +0800441
442 ALOGI("libautosuspend_get_feedback wait.\n");
xjac563942022-05-04 16:50:24 +0800443 pthread_mutex_lock(&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800444 pthread_cond_wait(&feedback_got_cond,&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800445 memcpy(time_info,&time_info_client,sizeof(struct time_info_t));
xjac563942022-05-04 16:50:24 +0800446 ALOGI("libautosuspend_get_feedback success.\n");
xjac563942022-05-04 16:50:24 +0800447 pthread_mutex_unlock(&feedback_got_mutex);
xjac563942022-05-04 16:50:24 +0800448
449 return 0;
450
451}
452
453
xjda4933f2022-05-09 10:34:43 +0800454int lynq_wait_wakeup_event(long *sleep_start_time, long * wakeup_time)
455{
jb.qie82adab2023-12-10 17:19:08 -0800456 int *socket_timeout = NULL;
457 struct time_info_t time_info;
458 int ret = 0;
459 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time start*/
460 if(feedback_flag == true)
461 {
462 if(libautosuspend_init() != 0)
463 {
464 return -1;
465 }
466 }
467 feedback_flag = false;
468 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time end*/
469 memset(&time_info,0,sizeof(struct time_info_t));
470 if(sleep_start_time == NULL || wakeup_time == NULL )
471 {
472 ALOGI("lynq_wait_wakeup_event input errors.\n");
473 return -1;
474 }
475 ret=libautosuspend_get_feedback(&time_info);
476 if(ret == 0)
477 {
478 *sleep_start_time = time_info.sleep_start_time;
479 *wakeup_time = time_info.wakeup_time;
480 return 0;
481 }
482 else
483 {
484 return -1;
485 }
jb.qi5c94c2d2024-12-04 00:10:12 -0800486}
487
488
489
490static void *check_dtr(void * arg)
491{
492 int lpm_edge;
493 char msg[32];
494 int fd=0;
495 int ret=0;
496
497 while(1)
498 {
499 fd = open("/dev/wakeup_dtr_dev", O_RDONLY);
500 ret=read(fd, &msg,31);
501 ALOGI("wakeup_dtr_dev: %s\n", msg);
502 close(fd);
503
504 if(msg[14] == '0')
505 {
506 lpm_edge = 0;
507 }
508 else if(msg[14] =='1')
509 {
510 lpm_edge = 1;
511 }
512 else
513 {
514 continue;
515 }
516 ALOGI("dtr status: %d\n", lpm_edge);
517 g_lpm_handler(lpm_edge);
518
519 }
520
521 return 0;
522}
523
524
525int lynq_dtr_register(lynq_lpm_Handler_t qser_lpm_handler)
526{
527 int ret;
528 if(lynq_lpm_handler == NULL)
529 {
530 RLOGI("lynq_lpm_handler is NULL\n");
531 return -1;
532 }
533 if(g_init_flag != 0)
534 {
535 RLOGI("g_init_flag is error\n");
536 return -1;
537 }
538 g_init_flag = 1;
539 g_lpm_handler = qser_lpm_handler;
540 ret = pthread_create(&dtr_thid,NULL,check_dtr,NULL);
541 if(ret != 0)
542 {
543 RLOGI("pthread create fail, qser_lpm_init fail\n");
544 }
545 return ret;
546}
547
548int qser_dtr_deregister(void)
549{
550 int ret;
551 if(g_init_flag != 1)
552 {
553 RLOGI("g_init_flag is error");
554 return -1;
555 }
556 g_init_flag = 0;
557 g_lpm_handler=NULL;
558 ret = pthread_cancel(dtr_thid);
559 if(!ret)
560 {
561 RLOGI("pthread cancel success, lpm deinit success\n");
562 }
563 else
564 {
565 RLOGI("pthread cancel fail, lpm deinit fail\n");
566 }
567 return ret;
xjda4933f2022-05-09 10:34:43 +0800568
569}
xjac563942022-05-04 16:50:24 +0800570
xjd5ccac02022-04-02 15:41:13 +0800571
jb.qi5c94c2d2024-12-04 00:10:12 -0800572