blob: 58412a6b918342cf8089e2941cae9bff25cfb819 [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{
103 if (Close(fd) == -1)
104 {
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;
172
173 int client_sock = *((int *)sockfd);
174
175 // pthread_mutex_lock(&feedback_got_mutex);
176
177 while (1)
178 {
179 // ALOGI("deal_get_feedback thread wait.\n");
180
181 // pthread_cond_wait(&get_feedback_cond,&get_feedback_mutex);
182
183 ALOGI("start get feedback from the service.\n");
184
185 pthread_mutex_lock(&feedback_got_mutex);
186
187 memset(&time_info_client,0,sizeof(struct time_info_t));
188
189 rc = Read(client_sock,&time_info_client,sizeof(struct time_info_t));
190 if(rc == -1)
191 {
192 ALOGI("client read wakeup_feedback struct fail.\n");
193 Close(client_sock);
194 pthread_mutex_unlock(&feedback_got_mutex);
195 break ;
196 }
197 else if(rc == -2)
198 {
199 ALOGI("client read wakeup_feedback struct timeout.\n");
200 pthread_mutex_unlock(&feedback_got_mutex);
201 continue;
202 }
203
204 ALOGI("system sleep_start timestamps : %ld ms\n",time_info_client.sleep_start_time);
205 ALOGI("system wakeup timestamps : %ld ms\n",time_info_client.wakeup_time);
206
207 // pthread_cond_broadcast(&feedback_got_cond);
208
209 pthread_mutex_unlock(&feedback_got_mutex);
210
jb.qidd9c9892022-09-15 00:48:16 -0700211 usleep(10000); //给libautosuspend_get_feedback函数时间进入wait,不可删除,但可以减少
xjac563942022-05-04 16:50:24 +0800212
213 pthread_cond_broadcast(&feedback_got_cond);
214
jb.qidd9c9892022-09-15 00:48:16 -0700215 usleep(10000); //希望多给libautosuspend_get_feedback函数拿到锁的机会,不可删除,但可以减少
xjac563942022-05-04 16:50:24 +0800216
217 }
218
219}
220
221
222static int libautosuspend_init()
223{
224 if (libautosuspend_inited)
225 {
226 return 0;
227 }
228
229 ALOGI("Start libautosuspend_init.\n");
230
231 char client_cmd_path[40];
232 char client_data_path[40];
233
234
235 sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
236 sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
237
238 pthread_mutex_lock(&client_fd_mutex);
239
240 if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
241 {
242 ALOGI("cmd channel connect error.\n");
243 pthread_mutex_unlock(&client_fd_mutex);
244 return -1;
245 }
246
247 if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
248 {
249 ALOGI("data channel connect error.\n");
250 pthread_mutex_unlock(&client_fd_mutex);
251 return -1;
252 }
253
254 pthread_t feedback_tid;
255 pthread_create(&feedback_tid,NULL,deal_get_feedback,(void*)&client_data_sock_fd);
256 pthread_detach(feedback_tid);
257
258
259 libautosuspend_inited = true;
260
261 pthread_mutex_unlock(&client_fd_mutex);
262
263 return 0;
264
265}
266
267static int send_cmd(char * value,int len)
268{
269 int rc;
270
271 if(value == NULL)
272 {
273 return -1;
274 }
xjd5ccac02022-04-02 15:41:13 +0800275
276 /************************************/
277 /* Copy the data to the buffer and */
278 /* send it to the server socket. */
279 /************************************/
xjac563942022-05-04 16:50:24 +0800280 // strcpy(buf, DATA);
281
282 ALOGI("Sending data...\n");
283 rc = send(client_sock_fd, value, len, 0);
xjd5ccac02022-04-02 15:41:13 +0800284 if (rc == -1) {
285 ALOGI("SEND ERROR ");
xjac563942022-05-04 16:50:24 +0800286 Close(client_sock_fd);
xjd5ccac02022-04-02 15:41:13 +0800287 return -2;
288 }
289 else {
290 ALOGI("Data sent: %s\n",value);
291 }
292
xjac563942022-05-04 16:50:24 +0800293 // Close(client_sock);
xjd5ccac02022-04-02 15:41:13 +0800294
295 return rc;
296
297}
298
299int lynq_autosleep_enable(void)
300{
xjac563942022-05-04 16:50:24 +0800301 char value[15]="enable";
302 char res[15];
303
304 if(libautosuspend_init() != 0)
305 {
306 return -1;
307 }
308
309 // if(libautosuspend_enabled)
310 // {
311 // return 0;
312 // }
313
314 pthread_mutex_lock(&client_fd_mutex);
315
316 int rc = send_cmd(value,strlen(value));
317 if(rc < 0)
318 {
319 ALOGI("libautosuspend send enable cmd fail.\n");
320 pthread_mutex_unlock(&client_fd_mutex);
321 return -1;
322 }
323
xjda4933f2022-05-09 10:34:43 +0800324 // if(Read(client_sock_fd,res,sizeof(res)) <= 0)
325 // {
326 // ALOGI("libautosuspend get respond fail.\n");
327 // pthread_mutex_unlock(&client_fd_mutex);
328 // return -1;
329 // }
xjac563942022-05-04 16:50:24 +0800330
xjda4933f2022-05-09 10:34:43 +0800331 // ALOGI("libautosuspend get respond : %s.\n",res);
xjac563942022-05-04 16:50:24 +0800332
xjda4933f2022-05-09 10:34:43 +0800333 // if(strcmp(res,"enabled") != 0)
334 // {
335 // pthread_mutex_unlock(&client_fd_mutex);
336 // return -1;
337 // }
xjac563942022-05-04 16:50:24 +0800338
339 // libautosuspend_enabled = true;
340
341 pthread_mutex_unlock(&client_fd_mutex);
342
xj0bb28dc2022-05-10 08:22:22 +0800343 return 1;
xjac563942022-05-04 16:50:24 +0800344
xjd5ccac02022-04-02 15:41:13 +0800345}
346
347int lynq_autosleep_disable(void)
348{
xjac563942022-05-04 16:50:24 +0800349 char value[15]="disable";
350 char res[15];
351
352 if(libautosuspend_init() != 0)
353 {
354 return -1;
355 }
356
357 // if(!libautosuspend_enabled)
358 // {
359 // return 0;
360 // }
361
362 pthread_mutex_lock(&client_fd_mutex);
363
364 int rc = send_cmd(value,strlen(value));
365 if(rc < 0)
366 {
367 ALOGI("libautosuspend send disable cmd fail.\n");
368 pthread_mutex_unlock(&client_fd_mutex);
369 return -1;
370 }
371
xjda4933f2022-05-09 10:34:43 +0800372 // if(Read(client_sock_fd,res,sizeof(res)) <= 0)
373 // {
374 // ALOGI("libautosuspend get respond fail.\n");
375 // pthread_mutex_unlock(&client_fd_mutex);
376 // return -1;
377 // }
xjac563942022-05-04 16:50:24 +0800378
xjda4933f2022-05-09 10:34:43 +0800379 // ALOGI("libautosuspend get respond : %s.\n",res);
xjac563942022-05-04 16:50:24 +0800380
xjda4933f2022-05-09 10:34:43 +0800381 // if(strcmp(res,"disabled") != 0)
382 // {
383 // pthread_mutex_unlock(&client_fd_mutex);
384 // return -1;
385 // }
xjac563942022-05-04 16:50:24 +0800386
387 // libautosuspend_enabled = false;
388
389 pthread_mutex_unlock(&client_fd_mutex);
390
xj0bb28dc2022-05-10 08:22:22 +0800391 return 1;
xjd5ccac02022-04-02 15:41:13 +0800392
393}
394
395
xjac563942022-05-04 16:50:24 +0800396
xjda4933f2022-05-09 10:34:43 +0800397int libautosuspend_get_feedback(struct time_info_t *time_info)
xjac563942022-05-04 16:50:24 +0800398{
399 // char value[15]="feedback";
400 // char res[15];
401
402 // if(!libautosuspend_enabled)
403 // {
404 // ALOGI("system autosuspend disabled, can not get wakeup feedback.\n");
405 // return -1;
406 // }
407
408 ALOGI("start get feedback from the service.\n");
409
410 memset(time_info,0,sizeof(struct time_info_t));
411
xjda4933f2022-05-09 10:34:43 +0800412 // if(timeout == NULL)
413 // {
414 // ALOGI("client set timeout for receiving wakeup_feedback: NULL.\n");
415 // }
416 // else
417 // {
418 // struct timeval recv_timeout = {(*timeout),0};
419 // pthread_mutex_lock(&client_data_fd_mutex);
420 // if(setsockopt(client_data_sock_fd,SOL_SOCKET,SO_RCVTIMEO,(char*)&recv_timeout,sizeof(struct timeval)) == -1)
421 // {
422 // ALOGI("client set timeout for receiving wakeup_feedback: error.\n");
423 // pthread_mutex_unlock(&client_data_fd_mutex);
424 // return -1;
425 // }
xjac563942022-05-04 16:50:24 +0800426
xjda4933f2022-05-09 10:34:43 +0800427 // ALOGI("client set timeout for receiving wakeup_feedback: %d s.\n",(*timeout));
428 // pthread_mutex_unlock(&client_data_fd_mutex);
xjac563942022-05-04 16:50:24 +0800429
xjda4933f2022-05-09 10:34:43 +0800430 // }
xjac563942022-05-04 16:50:24 +0800431
432 // int rc = send_cmd(value,strlen(value));
433 // if(rc < 0)
434 // {
435 // ALOGI("libautosuspend send feedback cmd fail.\n");
436 // pthread_mutex_unlock(&client_fd_mutex);
437 // return -1;
438 // }
439
440 // if(Read(client_data_sock_fd,time_info,sizeof(struct time_info_t)) <= 0)
441 // {
442 // ALOGI("libautosuspend_get_feedback fail.\n");
443 // pthread_mutex_unlock(&client_fd_mutex);
444 // return -1;
445 // }
446
447
448 ALOGI("libautosuspend_get_feedback wait.\n");
449
450 pthread_mutex_lock(&feedback_got_mutex);
451
452 pthread_cond_wait(&feedback_got_cond,&feedback_got_mutex);
453
454 memcpy(time_info,&time_info_client,sizeof(struct time_info_t));
455
456 ALOGI("libautosuspend_get_feedback success.\n");
457
458 pthread_mutex_unlock(&feedback_got_mutex);
459 // ALOGI("[client] system sleep_start timestamps : %ld ms\n",time_info.sleep_start_time);
460 // ALOGI("[client] system wakeup timestamps : %ld ms\n",time_info.wakeup_time);
461
462 return 0;
463
464}
465
466
xjda4933f2022-05-09 10:34:43 +0800467int lynq_wait_wakeup_event(long *sleep_start_time, long * wakeup_time)
468{
469 int *socket_timeout = NULL;
470 struct time_info_t time_info;
471 int ret = 0;
jb.qi5fb67582023-02-09 00:12:59 -0800472 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time start*/
473 if(feedback_flag == true)
474 {
475 if(libautosuspend_init() != 0)
476 {
477 return -1;
478 }
479 }
480 feedback_flag = false;
481 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time end*/
xjda4933f2022-05-09 10:34:43 +0800482 memset(&time_info,0,sizeof(struct time_info_t));
483 if(sleep_start_time == NULL || wakeup_time == NULL )
484 {
485 ALOGI("lynq_wait_wakeup_event input errors.\n");
486 return -1;
487 }
488 ret=libautosuspend_get_feedback(&time_info);
489 if(ret == 0)
490 {
491 *sleep_start_time = time_info.sleep_start_time;
492 *wakeup_time = time_info.wakeup_time;
493 return 0;
494 }
495 else
496 {
497 return -1;
498 }
499
500
501}
xjac563942022-05-04 16:50:24 +0800502
503// static void libautosuspend_get_feedback()
504// {
505// pthread_t feedback_tid;
506// pthread_create(&feedback_tid,NULL,deal_get_feedback,(void*)&client_sock);
507// pthread_detach(&feedback_tid);
508
509// return ;
510
511// }
512
xjd5ccac02022-04-02 15:41:13 +0800513
514