blob: 54c88fdcc2194eb251bfa9dbbf241a0bb9196a75 [file] [log] [blame]
jb.qi3a8298c2023-09-07 04:48:51 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <string.h>
5#include <sys/types.h>
6#include <sys/socket.h>
7#include <sys/un.h>
8#include <unistd.h>
9#include <dlfcn.h>
10#include <pthread.h>
11#include <stdbool.h>
12#include <time.h>
13#include "include/libauto/lynq_autosuspend.h"
14#ifdef MOBILETEK_TARGET_PLATFORM_T106
15#include <sc_bsp.h>
16#endif
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22
23#define LOG_TAG "libautosuspend"
24// #include <liblog/lynq_deflog.h>
25#include <log/log.h>
26#define SERVER_CMD_PATH "/tmp/autosuspend.cmd.server"
27#define SERVER_DATA_PATH "/tmp/autosuspend.data.server"
28// #define CLIENT_PATH "/tmp/autosuspend.client"
29static int client_sock_fd;
30static int client_data_sock_fd;
31static bool libautosuspend_inited;
32bool feedback_flag = true; //add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time
33// static bool libautosuspend_enabled;
34// static pthread_mutex_t get_feedback_mutex = PTHREAD_MUTEX_INITIALIZER;
35// static pthread_cond_t get_feedback_cond = PTHREAD_COND_INITIALIZER;
36static pthread_mutex_t client_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
37static pthread_mutex_t client_data_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
38static pthread_mutex_t feedback_got_mutex = PTHREAD_MUTEX_INITIALIZER;
39static pthread_cond_t feedback_got_cond = PTHREAD_COND_INITIALIZER;
40
41static struct time_info_t time_info_client;
42static ssize_t Read(int fd, void *ptr, size_t nbytes)
43{
44 ssize_t n;
45
46 while((n = read(fd, ptr, nbytes)) == -1)
47 {
48 //printf("READ,%d\n",fd);
49 if (errno == EINTR)
50 {
51 printf("read error eintr\n");
52 continue;
53 }
54 else if(errno == EAGAIN || errno == EWOULDBLOCK)
55 {
56 printf("read time out\n");
57 return -2;
58 }
59 else
60 {
61 printf("read error\n");
62 return -1;
63 }
64 }
65 //sleep(2);
66 //printf("READ1,%d\n", fd);
67 return n;
68}
69static ssize_t Write(int fd, const void *ptr, size_t nbytes)
70{
71 ssize_t n;
72 while((n = write(fd, ptr, nbytes)) == -1)
73 {
74 if (errno == EINTR)
75 continue;
76 else if(errno == EPIPE)
77 {
78 printf("write error epipe\n");
79 return -1;
80 }
81 else
82 return -1;
83 }
84 return n;
85}
86static int Close(int fd)
87{
88 if (Close(fd) == -1)
89 {
90 printf("Close error\n");
91 return -1;
92 }
93 return 0;
94}
95static int connect_to_server(int *cfd, char *client_path, char *server_path)
96{
97 int rc;
98 struct sockaddr_un server_sockaddr;
99 struct sockaddr_un client_sockaddr;
100 printf("Start bind and connect to the service.\n");
101 /**************************************/
102 /* Create a UNIX domain stream socket */
103 /**************************************/
104 *cfd = socket(AF_UNIX, SOCK_STREAM, 0);
105 if (*cfd == -1) {
106 printf("SOCKET ERROR ");
107 return -1;
108 }
109 /***************************************/
110 /* Set up the UNIX sockaddr structure */
111 /* by using AF_UNIX for the family and */
112 /* giving it a filepath to bind to. */
113 /* */
114 /* Unlink the file so the bind will */
115 /* succeed, then bind to that file. */
116 /***************************************/
117 client_sockaddr.sun_family = AF_UNIX;
118 strcpy(client_sockaddr.sun_path, client_path);
119
120 unlink(client_sockaddr.sun_path);
121 rc = bind(*cfd, (struct sockaddr *) &client_sockaddr, sizeof(client_sockaddr));
122 if (rc == -1){
123 printf("BIND ERROR ");
124 Close(*cfd);
125 return -1;
126 }
127
128 /***************************************/
129 /* Set up the UNIX sockaddr structure */
130 /* for the server socket and connect */
131 /* to it. */
132 /***************************************/
133 server_sockaddr.sun_family = AF_UNIX;
134
135 strcpy(server_sockaddr.sun_path, server_path);
136 rc = connect(*cfd, (struct sockaddr *) &server_sockaddr, sizeof(client_sockaddr));
137 if(rc == -1){
138 printf("CONNECT ERROR ");
139 Close(*cfd);
140 return -3;
141 }
142 return 0;
143
144}
145static void *deal_get_feedback(void *sockfd)
146{
147 int rc;
148 int client_sock = *((int *)sockfd);
149 // pthread_mutex_lock(&feedback_got_mutex);
150
151 while (1)
152 {
153 // printf("deal_get_feedback thread wait.\n");
154 // pthread_cond_wait(&get_feedback_cond,&get_feedback_mutex);
155 printf("start get feedback from the service.\n");
156 pthread_mutex_lock(&feedback_got_mutex);
157 memset(&time_info_client,0,sizeof(struct time_info_t));
158 rc = Read(client_sock,&time_info_client,sizeof(struct time_info_t));
159 if(rc == -1)
160 {
161 printf("client read wakeup_feedback struct fail.\n");
162 Close(client_sock);
163 pthread_mutex_unlock(&feedback_got_mutex);
164 break ;
165 }
166 else if(rc == -2)
167 {
168 printf("client read wakeup_feedback struct timeout.\n");
169 pthread_mutex_unlock(&feedback_got_mutex);
170 continue;
171 }
172 printf("system sleep_start timestamps : %ld ms\n",time_info_client.sleep_start_time);
173 printf("system wakeup timestamps : %ld ms\n",time_info_client.wakeup_time);
174 // pthread_cond_broadcast(&feedback_got_cond);
175 pthread_mutex_unlock(&feedback_got_mutex);
176 usleep(10000); //给libautosuspend_get_feedback函数时间进入wait,不可删除,但可以减少
177 pthread_cond_broadcast(&feedback_got_cond);
178 usleep(10000); //希望多给libautosuspend_get_feedback函数拿到锁的机会,不可删除,但可以减少
179
180 }
181
182}
183static int libautosuspend_init()
184{
185 if (libautosuspend_inited)
186 {
187 return 0;
188 }
189 printf("Start libautosuspend_init.\n");
190 char client_cmd_path[40];
191 char client_data_path[40];
192 sprintf(client_cmd_path,"/tmp/autosuspend.%d.cmd.client",(int)getpid());
193 sprintf(client_data_path,"/tmp/autosuspend.%d.data.client",(int)getpid());
194 pthread_mutex_lock(&client_fd_mutex);
195 if(connect_to_server(&client_sock_fd,client_cmd_path,SERVER_CMD_PATH) < 0)
196 {
197 printf("cmd channel connect error.\n");
198 pthread_mutex_unlock(&client_fd_mutex);
199 return -1;
200 }
201 if(connect_to_server(&client_data_sock_fd,client_data_path,SERVER_DATA_PATH) < 0)
202 {
203 printf("data channel connect error.\n");
204 pthread_mutex_unlock(&client_fd_mutex);
205 return -1;
206 }
207 pthread_t feedback_tid;
208 pthread_create(&feedback_tid,NULL,deal_get_feedback,(void*)&client_data_sock_fd);
209 pthread_detach(feedback_tid);
210 libautosuspend_inited = true;
211 pthread_mutex_unlock(&client_fd_mutex);
212
213 return 0;
214
215}
216static int send_cmd(char * value,int len)
217{
218 int rc;
219 if(value == NULL)
220 {
221 return -1;
222 }
223
224 /************************************/
225 /* Copy the data to the buffer and */
226 /* send it to the server socket. */
227 /************************************/
228 // strcpy(buf, DATA);
229 printf("Sending data...\n");
230 rc = send(client_sock_fd, value, len, 0);
231 if (rc == -1) {
232 printf("SEND ERROR ");
233 Close(client_sock_fd);
234 return -2;
235 }
236 else {
237 printf("Data sent: %s\n",value);
238 }
239 // Close(client_sock);
240 return rc;
241}
242int lynq_autosleep_enable(void)
243{
244 char value[15]="enable";
245 char res[15];
246 if(libautosuspend_init() != 0)
247 {
248 return -1;
249 }
250 // if(libautosuspend_enabled)
251 // {
252 // return 0;
253 // }
254 pthread_mutex_lock(&client_fd_mutex);
255 int rc = send_cmd(value,strlen(value));
256 if(rc < 0)
257 {
258 printf("libautosuspend send enable cmd fail.\n");
259 pthread_mutex_unlock(&client_fd_mutex);
260 return -1;
261 }
262 // if(Read(client_sock_fd,res,sizeof(res)) <= 0)
263 // {
264 // printf("libautosuspend get respond fail.\n");
265 // pthread_mutex_unlock(&client_fd_mutex);
266 // return -1;
267 // }
268 // printf("libautosuspend get respond : %s.\n",res);
269 // if(strcmp(res,"enabled") != 0)
270 // {
271 // pthread_mutex_unlock(&client_fd_mutex);
272 // return -1;
273 // }
274 // libautosuspend_enabled = true;
275 pthread_mutex_unlock(&client_fd_mutex);
276 return 1;
277
278}
279int lynq_autosleep_disable(void)
280{
281 char value[15]="disable";
282 char res[15];
283 if(libautosuspend_init() != 0)
284 {
285 return -1;
286 }
287 // if(!libautosuspend_enabled)
288 // {
289 // return 0;
290 // }
291 pthread_mutex_lock(&client_fd_mutex);
292 int rc = send_cmd(value,strlen(value));
293 if(rc < 0)
294 {
295 printf("libautosuspend send disable cmd fail.\n");
296 pthread_mutex_unlock(&client_fd_mutex);
297 return -1;
298 }
299 // if(Read(client_sock_fd,res,sizeof(res)) <= 0)
300 // {
301 // printf("libautosuspend get respond fail.\n");
302 // pthread_mutex_unlock(&client_fd_mutex);
303 // return -1;
304 // }
305 // printf("libautosuspend get respond : %s.\n",res);
306 // if(strcmp(res,"disabled") != 0)
307 // {
308 // pthread_mutex_unlock(&client_fd_mutex);
309 // return -1;
310 // }
311 // libautosuspend_enabled = false;
312 pthread_mutex_unlock(&client_fd_mutex);
313 return 1;
314}
315int libautosuspend_get_feedback(struct time_info_t *time_info)
316{
317 // char value[15]="feedback";
318 // char res[15];
319 // if(!libautosuspend_enabled)
320 // {
321 // printf("system autosuspend disabled, can not get wakeup feedback.\n");
322 // return -1;
323 // }
324 printf("start get feedback from the service.\n");
325 memset(time_info,0,sizeof(struct time_info_t));
326 // if(timeout == NULL)
327 // {
328 // printf("client set timeout for receiving wakeup_feedback: NULL.\n");
329 // }
330 // else
331 // {
332 // struct timeval recv_timeout = {(*timeout),0};
333 // pthread_mutex_lock(&client_data_fd_mutex);
334 // if(setsockopt(client_data_sock_fd,SOL_SOCKET,SO_RCVTIMEO,(char*)&recv_timeout,sizeof(struct timeval)) == -1)
335 // {
336 // printf("client set timeout for receiving wakeup_feedback: error.\n");
337 // pthread_mutex_unlock(&client_data_fd_mutex);
338 // return -1;
339 // }
340
341 // printf("client set timeout for receiving wakeup_feedback: %d s.\n",(*timeout));
342 // pthread_mutex_unlock(&client_data_fd_mutex);
343
344 // }
345 // int rc = send_cmd(value,strlen(value));
346 // if(rc < 0)
347 // {
348 // printf("libautosuspend send feedback cmd fail.\n");
349 // pthread_mutex_unlock(&client_fd_mutex);
350 // return -1;
351 // }
352 // if(Read(client_data_sock_fd,time_info,sizeof(struct time_info_t)) <= 0)
353 // {
354 // printf("libautosuspend_get_feedback fail.\n");
355 // pthread_mutex_unlock(&client_fd_mutex);
356 // return -1;
357 // }
358 printf("libautosuspend_get_feedback wait.\n");
359 pthread_mutex_lock(&feedback_got_mutex);
360 pthread_cond_wait(&feedback_got_cond,&feedback_got_mutex);
361 memcpy(time_info,&time_info_client,sizeof(struct time_info_t));
362 printf("libautosuspend_get_feedback success.\n");
363 pthread_mutex_unlock(&feedback_got_mutex);
364 // printf("[client] system sleep_start timestamps : %ld ms\n",time_info.sleep_start_time);
365 // printf("[client] system wakeup timestamps : %ld ms\n",time_info.wakeup_time);
366 return 0;
367}
368int lynq_wait_wakeup_event(long *sleep_start_time, long * wakeup_time)
369{
370 int *socket_timeout = NULL;
371 struct time_info_t time_info;
372 int ret = 0;
373 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time start*/
374 if(feedback_flag == true)
375 {
376 if(libautosuspend_init() != 0)
377 {
378 return -1;
379 }
380 }
381 feedback_flag = false;
382 /*add for after sleeping once calling lynq_wailt_wakeup_event does not return sleep time end*/
383 memset(&time_info,0,sizeof(struct time_info_t));
384 if(sleep_start_time == NULL || wakeup_time == NULL )
385 {
386 printf("lynq_wait_wakeup_event input errors.\n");
387 return -1;
388 }
389 ret=libautosuspend_get_feedback(&time_info);
390 if(ret == 0)
391 {
392 *sleep_start_time = time_info.sleep_start_time;
393 *wakeup_time = time_info.wakeup_time;
394 return 0;
395 }
396 else
397 {
398 return -1;
399 }
400
401}
402#ifdef MOBILETEK_TARGET_PLATFORM_T106
403int acquire_wake_lock(int lock, char *name)
404{
405 int ret;
406 printf("Get param:%s \n", name);
407 ret = sc_pm_wakelock_lock(name);
408 if (ret)
409 {
410 printf("do_wakelock failed, err:%d", ret);
411 return ret;
412 }
413 printf("do_wakelock succeed\n");
414 return ret;
415}
416int release_wake_lock(char *name)
417{
418 int ret;
419 printf("Get param:%s \n", name);
420 ret = sc_pm_wakelock_unlock(name);
421 if (ret)
422 {
423 printf("do_wakeunlock failed, err:%d", ret);
424 return ret;
425 }
426 printf("do_wakeunlock succeed\n");
427 return ret;
428}
429int lynq_set_lpmode(int lp_mode)
430{
431 int ret;
432 printf("Get param:%d \n", lp_mode);
433 ret = sc_pm_set_lp_mode(lp_mode);
434 if (ret) {
435 printf("do_set_lpmode failed, err:%d", ret);
436 exit(1);
437 }
438 printf("do_set_lpmode succeed\n");
439 return ret;
440}
441#endif
442
443#ifdef __cplusplus
444}
445#endif