blob: 147f102a7a08c77c72ead5766ce69b55761e5ee5 [file] [log] [blame]
xj4049f512022-04-02 15:41:13 +08001/* //device/system/rild/rild.c
2**
3** Copyright 2006 The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdio.h>
19#include <stdlib.h>
xj9db5b622022-05-04 16:50:24 +080020#include <stdbool.h>
xj4049f512022-04-02 15:41:13 +080021#include <dlfcn.h>
22#include <string.h>
23#include <pthread.h>
24#include <stdint.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <errno.h>
xj9db5b622022-05-04 16:50:24 +080028#include <log/log.h>
xj4049f512022-04-02 15:41:13 +080029#include <liblog/lynq_deflog.h>
30#include <include/lynq_uci.h>
31#include <sys/socket.h>
32#include <sys/un.h>
xje74bccc2022-05-09 10:34:43 +080033#include <signal.h>
xj4049f512022-04-02 15:41:13 +080034
xj9db5b622022-05-04 16:50:24 +080035
xj4049f512022-04-02 15:41:13 +080036#define LOG_UCI_MODULE "lynq_autosuspend"
37#define LOG_UCI_FILE "lynq_uci"
38
39#define LOG_TAG "AUTOSUSPEND"
40
41#define USER_LOG_TAG "PMS"
42
xj9db5b622022-05-04 16:50:24 +080043#define SOCK_PATH "/tmp/autosuspend.cmd.server" //不能在当前这个目录创建socket文件,否则报错找不到文件(可能是因为这是在共享文件夹下,不支持创建socket文件)
44
45#define SOCK_DATA_PATH "/tmp/autosuspend.data.server"
xj4049f512022-04-02 15:41:13 +080046
47// #define LYINFLOG(X...) lynq_log_global_output(LOG_INFO,X)
48
49#define TIME_OUT_TIME 30
50
51
xj4049f512022-04-02 15:41:13 +080052#define MAX_LIB_ARGS 16
53
54int adb_debug_mode = 0;
55
xj9db5b622022-05-04 16:50:24 +080056
57pthread_cond_t feedback_cond = PTHREAD_COND_INITIALIZER;
58pthread_mutex_t feedback_mutex = PTHREAD_MUTEX_INITIALIZER;
59pthread_mutex_t time_info_mutex = PTHREAD_MUTEX_INITIALIZER;
60
xj4049f512022-04-02 15:41:13 +080061extern int autosuspend_enable(void);
62extern int autosuspend_disable(void);
63extern void init_wakelock_func(void);
64extern void init_sim_func();
65extern void init_network_func();
xj9db5b622022-05-04 16:50:24 +080066extern void set_wakeup_callback(void (*func)(bool success));
67extern void wakeup_feedback(bool success);
xje74bccc2022-05-09 10:34:43 +080068extern int (*lynq_screen)(int num);
xj4049f512022-04-02 15:41:13 +080069
xj9db5b622022-05-04 16:50:24 +080070struct time_info_t
71{
72 long sleep_start_time;
73 long wakeup_time;
74};
75
76struct time_info_t time_info;
77
xj4049f512022-04-02 15:41:13 +080078static void usage(const char *argv0) {
79 fprintf(stderr, "Usage: %s -l <possible_max_sleep_time> [-- <args for Autosuspend Service>]\n", argv0);
80 exit(EXIT_FAILURE);
81}
82
83
84
85static int make_argv(char * args, char ** argv) {
86 // Note: reserve argv[0]
87 int count = 1;
88 char * tok;
89 char * s = args;
90
91 while ((tok = strtok(s, " \0"))) {
92 argv[count] = tok;
93 s = NULL;
94 count++;
95 }
96 return count;
97}
98
99static int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
100{
101 int n;
102
103 while((n = accept(fd, sa, salenptr)) < 0)
104 {
105 if((errno == ECONNABORTED) || (errno == EINTR))
106 continue;
107 else
108 {
109 ALOGI("accept error\n");
110 return -1;
111 }
112 }
113 return n;
114}
115
116static int Bind(int fd, const struct sockaddr *sa, socklen_t salen)
117{
118 if(bind(fd, sa, salen) < 0)
119 {
120 // ALOGI("bind error\n");
121 perror("bind error");
122 return -1;
123 }
124 return 0;
125}
126
xj4049f512022-04-02 15:41:13 +0800127
128static int Socket(int family, int type, int protocol)
129{
130 int n;
131
132 if ( (n = socket(family, type, protocol)) < 0)
133 {
134 ALOGI("socket error\n");
135 return -1;
136 }
137 return n;
138}
139
140static int Listen(int fd, int backlog)
141{
142 if(listen(fd, backlog) < 0)
143 {
144 ALOGI("listen error\n");
145 return -1;
146 }
147 return 0;
148}
149
xj4049f512022-04-02 15:41:13 +0800150
151static int listen_port(struct sockaddr_un *addr, char *sockpath)
152{
153 int listenfd;
154 listenfd = Socket(AF_UNIX,SOCK_STREAM,0);
155 if(listenfd == -1)
156 return -1;
157 memset(addr, 0, sizeof(struct sockaddr_un));
158 addr->sun_family = AF_UNIX;
159 strcpy(addr->sun_path,sockpath);
160 // int opt = 1;
161 // if(setsockopt(listenfd, SOL_SOCKET,SO_REUSEADDR, (const void *)&opt, sizeof(opt)) == -1)
162 // {
163 // perror("setsockopt error");
164 // return -1;
165 // }
166
167// 以上方法对非网络的本地socket无效,应该用unlink函数避免Address already in use的错误
168
169
170 unlink(sockpath);
171 if(Bind(listenfd,(struct sockaddr *)addr,sizeof(*addr)) == -1)
172 return -1;
173
174 if(Listen(listenfd,20) == -1)
175 return -1;
176
177 return listenfd;
178}
179
180static ssize_t Read(int fd, void *ptr, size_t nbytes)
181{
182 ssize_t n;
183
184 while((n = read(fd, ptr, nbytes)) == -1)
185 {
186 //printf("READ,%d\n",fd);
187 if (errno == EINTR)
188 {
189 ALOGI("read error eintr\n");
190 continue;
191 }
xj9db5b622022-05-04 16:50:24 +0800192 else if(errno == EAGAIN || errno == EWOULDBLOCK)
xj4049f512022-04-02 15:41:13 +0800193 {
xj9db5b622022-05-04 16:50:24 +0800194 ALOGI("read time out\n");
xj4049f512022-04-02 15:41:13 +0800195 return -1;
196 }
197 else
198 {
199 ALOGI("read error\n");
200 return -1;
201 }
202 }
203 //sleep(2);
204 //printf("READ1,%d\n", fd);
205 return n;
206}
207
208static ssize_t Write(int fd, const void *ptr, size_t nbytes)
209{
210 ssize_t n;
211
212 while((n = write(fd, ptr, nbytes)) == -1)
213 {
214 if (errno == EINTR)
215 continue;
216 else if(errno == EPIPE)
217 {
218 ALOGI("write error epipe\n");
219 return -1;
220 }
221 else
222 return -1;
223 }
224 return n;
225}
226
227static int Close(int fd)
228{
229 if (close(fd) == -1)
230 {
xj9db5b622022-05-04 16:50:24 +0800231 ALOGI("close error\n");
xj4049f512022-04-02 15:41:13 +0800232 return -1;
233 }
234 return 0;
235}
236
xj9db5b622022-05-04 16:50:24 +0800237
xj4049f512022-04-02 15:41:13 +0800238void *deal_autosuspend(void *sockfd)
239{
240 int commfd = *((int *)sockfd);
xj9db5b622022-05-04 16:50:24 +0800241 char buf[20];
242 char res[15];
xj4049f512022-04-02 15:41:13 +0800243
244 while(1)
245 {
246 memset(buf,0,sizeof(buf));
247 ALOGI("deal_autosuspend start to read.\n");
248 // 错误点:read函数在对端关闭后,也会直接返回0,不会阻塞,因此要判断是否返回0,返回0表示对端已经关闭,此时要跳出while循环不再监听
249 // 为什么对端会关闭?因为在客户端没有用nohup方式打开的情况下,系统睡眠后客户端进行会直接被杀死,对端会关闭,所以会导致read不阻塞,且总是返回0的现象
xj9db5b622022-05-04 16:50:24 +0800250 if(Read(commfd,buf,sizeof(buf)) <= 0)
xj4049f512022-04-02 15:41:13 +0800251 {
xje74bccc2022-05-09 10:34:43 +0800252 ALOGI("service receive suspend_cmd fail or client is closed.\n");
xj9db5b622022-05-04 16:50:24 +0800253 Close(commfd);
xj4049f512022-04-02 15:41:13 +0800254 break;
255 }
256 if(strcmp(buf,"enable") == 0)
257 {
jb.qiae8158e2022-09-22 00:39:45 -0700258 /*
xje74bccc2022-05-09 10:34:43 +0800259 system("echo 7 | emdlogger_ctrl");
260
261 if (lynq_screen(0) < 0) //notify ril for screen off
262 {
263 ALOGI("lynq_screen off fail\n");
264 return -1;
265 }
266
jb.qiae8158e2022-09-22 00:39:45 -0700267 sleep(5);*/
xj4049f512022-04-02 15:41:13 +0800268 if(autosuspend_enable() < 0)
269 {
270 ALOGI("autosuspend_enable fail.\n");
xje74bccc2022-05-09 10:34:43 +0800271 // strcpy(res,"fail");
272 // if(Write(commfd,res,strlen(res)) <= 0)
273 // {
274 // ALOGI("service send respond fail.\n");
275 // Close(commfd);
276 // break;
277 // }
xj4049f512022-04-02 15:41:13 +0800278 }
279 else
280 {
281 ALOGI("autosuspend_enable success.\n");
xje74bccc2022-05-09 10:34:43 +0800282 // strcpy(res,"enabled");
283 // if(Write(commfd,res,strlen(res)) <= 0)
284 // {
285 // ALOGI("service send respond fail.\n");
286 // Close(commfd);
287 // break;
288 // }
xj4049f512022-04-02 15:41:13 +0800289 }
290 }
291 else if(strcmp(buf,"disable") == 0)
292 {
293 if(autosuspend_disable() < 0)
294 {
295 ALOGI("autosuspend_disable fail.\n");
xje74bccc2022-05-09 10:34:43 +0800296 // strcpy(res,"fail");
297 // if(Write(commfd,res,strlen(res)) <= 0)
298 // {
299 // ALOGI("service send respond fail.\n");
300 // Close(commfd);
301 // break;
302 // }
xj4049f512022-04-02 15:41:13 +0800303 }
304 else
305 {
306 ALOGI("autosuspend_disable success.\n");
xje74bccc2022-05-09 10:34:43 +0800307 // strcpy(res,"disabled");
308 // if(Write(commfd,res,strlen(res)) <= 0)
309 // {
310 // ALOGI("service send respond fail.\n");
311 // Close(commfd);
312 // break;
313 // }
xj4049f512022-04-02 15:41:13 +0800314 }
315 }
xj9db5b622022-05-04 16:50:24 +0800316 // else if(strcmp(buf,"feedback") == 0)
317 // {
318
319 // ALOGI("send_feedback thread wait to send.\n");
320 // if (pthread_cond_wait(&feedback_cond,&feedback_mutex) != 0)
321 // {
322 // strerror_r(errno, buf, sizeof(buf));
323 // ALOGI("Error waiting on cond: %s\n", buf);
324 // Close(commfd);
325 // break;
326 // }
327
328 // ALOGI("send_feedback thread is now sending the feedback to client.\n");
329 // if(Write(commfd,&time_info,sizeof(struct time_info_t)) <= 0)
330 // {
331 // ALOGI("service send wakeup_feedback struct fail.\n");
332 // Close(commfd);
333 // break ;
334 // }
335 // ALOGI("service send feedback success.\n");
336 // strcpy(res,"success");
337 // if(Write(commfd,res,strlen(res)) <= 0)
338 // {
339 // ALOGI("service send respond fail.\n");
340 // Close(commfd);
341 // break;
342 // }
343
344 // }
xj4049f512022-04-02 15:41:13 +0800345 else
346 {
xj9db5b622022-05-04 16:50:24 +0800347 ALOGI("Unknown cmd : %s\n",buf);
xj4049f512022-04-02 15:41:13 +0800348 }
xj9db5b622022-05-04 16:50:24 +0800349
xj4049f512022-04-02 15:41:13 +0800350 }
351
352
353
354}
355
356
xj9db5b622022-05-04 16:50:24 +0800357void *send_feedback(void *sockfd)
358{
359 int commfd = *((int *)sockfd);
360 char buf[80];
361
362 while (1)
363 {
364 memset(buf,0,sizeof(buf));
365 ALOGI("send_feedback thread wait to send.\n");
366 pthread_mutex_lock(&feedback_mutex);
367 pthread_cond_wait(&feedback_cond,&feedback_mutex);
xj9db5b622022-05-04 16:50:24 +0800368
xj9db5b622022-05-04 16:50:24 +0800369 ALOGI("send_feedback thread is now sending the feedback to client.\n");
370 pthread_mutex_lock(&time_info_mutex);
371 if(Write(commfd,&time_info,sizeof(struct time_info_t)) <= 0)
372 {
373 ALOGI("service send wakeup_feedback struct fail.\n");
374 Close(commfd);
375 pthread_mutex_unlock(&time_info_mutex);
376 pthread_mutex_unlock(&feedback_mutex);
377 break ;
378 }
379 pthread_mutex_unlock(&time_info_mutex);
380
381 pthread_mutex_unlock(&feedback_mutex);
382
383
384
385 }
386
387}
388
xj4049f512022-04-02 15:41:13 +0800389
390int main(int argc, char **argv) {
391
392
393 // int i = 0;
394 // RLOGD("**Autosuspend Service Daemon Started**");
395 // RLOGD("**Autosuspend Service param count=%d**", argc);
396 char tmp[20];
xj9db5b622022-05-04 16:50:24 +0800397
398 int commfd, commfd_data, server_sock, server_data_sock,len, len_data;
399
xj4049f512022-04-02 15:41:13 +0800400 struct sockaddr_un server_sockaddr;
xj9db5b622022-05-04 16:50:24 +0800401 struct sockaddr_un server_data_sockaddr;
xj4049f512022-04-02 15:41:13 +0800402 struct sockaddr_un client_sockaddr;
xj9db5b622022-05-04 16:50:24 +0800403
xj4049f512022-04-02 15:41:13 +0800404 len = sizeof(server_sockaddr);
405
xj9db5b622022-05-04 16:50:24 +0800406
xj4049f512022-04-02 15:41:13 +0800407 pthread_t tid;
408
xj4049f512022-04-02 15:41:13 +0800409 LYLOGEINIT(USER_LOG_TAG);
410 LYLOGSET(LOG_DEBUG);
411 // LYLOGSET(LOG_ERROR);
412
xj9db5b622022-05-04 16:50:24 +0800413 int auto_enable = 0;
xj4049f512022-04-02 15:41:13 +0800414
xje74bccc2022-05-09 10:34:43 +0800415 lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "debug", tmp); // 即获取系统层面的环境变量
xj9db5b622022-05-04 16:50:24 +0800416 ALOGI("Autosuspend Service Daemon. debug %s\n",tmp);
xj4049f512022-04-02 15:41:13 +0800417 adb_debug_mode=atoi(tmp);
418 lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "auto_enable", tmp);
419 auto_enable=atoi(tmp);
420 ALOGI("Autosuspend Service Daemon. auto_enable %s\n",tmp);
421 init_wakelock_func();
422 init_sim_func();
xj9db5b622022-05-04 16:50:24 +0800423
xje74bccc2022-05-09 10:34:43 +0800424 signal(SIGPIPE,SIG_IGN); // 忽略SIGPIPE信号,防止由于客户端关闭,继续往客户端write,会导致服务端收到SIGPIPE信号而Broken pipe
425
xj9db5b622022-05-04 16:50:24 +0800426
xj4049f512022-04-02 15:41:13 +0800427 // init_network_func();
xj9db5b622022-05-04 16:50:24 +0800428
429 // if(pthread_cond_init(&feedback_cond,NULL) != 0)
430 // {
431 // strerror_r(errno, buf, sizeof(buf));
432 // ALOGI("Error creating cond: %s\n", buf);
433 // return -1;
434 // }
435
436 set_wakeup_callback(wakeup_feedback);
xje74bccc2022-05-09 10:34:43 +0800437 // 注册回调函数
xj9db5b622022-05-04 16:50:24 +0800438
xj4049f512022-04-02 15:41:13 +0800439 if(auto_enable==0)
440 {
441 if(autosuspend_disable() < 0)
442 {
443 ALOGI("autosuspend_disable fail.\n");
444 }
445 else
446 {
447 ALOGI("autosuspend_disable success.\n");
448 }
449 }
450 if(auto_enable==1)
451 {
452 if(autosuspend_enable() < 0)
453 {
454 ALOGI("autosuspend_enable fail.\n");
455 }
456 else
457 {
458 ALOGI("autosuspend_enable success.\n");
459 }
460 }
461
462
463 server_sock = listen_port(&server_sockaddr,SOCK_PATH);
464 if(server_sock == -1)
465 return -1;
466
xj9db5b622022-05-04 16:50:24 +0800467 server_data_sock = listen_port(&server_data_sockaddr,SOCK_DATA_PATH);
468 if(server_data_sock == -1)
469 return -1;
470
471
xj4049f512022-04-02 15:41:13 +0800472 while (1)
473 {
474 ALOGI("service socket listening...\n");
475 commfd = Accept(server_sock,(struct sockaddr *)&client_sockaddr,&len);
476 if(commfd == -1)
477 {
478 return -1;
479 }
480 if(getpeername(commfd, (struct sockaddr *)&client_sockaddr, &len) == -1)
481 {
482 ALOGI("GETPEERNAME ERROR.\n");
xj9db5b622022-05-04 16:50:24 +0800483 // Close(server_sock);
484 Close(commfd);
xj4049f512022-04-02 15:41:13 +0800485 continue;
486 }
487 else
488 {
489 ALOGI("Client socket filepath: %s\n", client_sockaddr.sun_path);
490 }
xj9db5b622022-05-04 16:50:24 +0800491
492 commfd_data = Accept(server_data_sock,NULL,NULL);
493 if(commfd_data == -1)
494 {
495 return -1;
496 }
497 ALOGI("data channel connected.\n");
498
xj4049f512022-04-02 15:41:13 +0800499 pthread_create(&tid,NULL,deal_autosuspend,(void*)&commfd);//这里很容易错,最后一个参数要取地址,这是一个指针
xj9db5b622022-05-04 16:50:24 +0800500 pthread_detach(tid);
501
502 pthread_create(&tid,NULL,send_feedback,(void*)&commfd_data);
503 pthread_detach(tid);
504
xj4049f512022-04-02 15:41:13 +0800505
506 }
507
508 /* for (i = 1; i < argc ;) {
509 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
510 rilLibPath = argv[i + 1];
511 i += 2;
512 } else if (0 == strcmp(argv[i], "--")) {
513 i++;
514 hasLibArgs = 1;
515 break;
516 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
517 clientId = argv[i+1];
518 i += 2;
519 } else {
520 usage(argv[0]);
521 }
522 }
523
524 if (clientId == NULL) {
525 clientId = "0";
526 } else if (atoi(clientId) >= MAX_RILDS) {
527 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
528 exit(0);
529 }
530 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
531 strncpy(ril_service_name, ril_service_name_base, MAX_SERVICE_NAME_LENGTH);
532 strncat(ril_service_name, clientId, MAX_SERVICE_NAME_LENGTH);
533 }
534
535 if (rilLibPath == NULL) {
536 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
537 // No lib sepcified on the command line, and nothing set in props.
538 // Assume "no-ril" case.
539 goto done;
540 } else {
541 rilLibPath = libPath;
542 }
543 }
544
545 dlHandle = dlopen(rilLibPath, RTLD_NOW);
546
547 if (dlHandle == NULL) {
548 RLOGE("dlopen failed: %s", dlerror());
549 exit(EXIT_FAILURE);
550 }
551
552 RIL_startEventLoop();
553
554 rilInit =
555 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
556 dlsym(dlHandle, "RIL_Init");
557
558 if (rilInit == NULL) {
559 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
560 exit(EXIT_FAILURE);
561 }
562
563 dlerror(); // Clear any previous dlerror
564 rilUimInit =
565 (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
566 dlsym(dlHandle, "RIL_SAP_Init");
567 err_str = dlerror();
568 if (err_str) {
569 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
570 } else if (!rilUimInit) {
571 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
572 }
573
574 if (hasLibArgs) {
575 rilArgv = argv + i - 1;
576 argc = argc -i + 1;
577 } else {
578 static char * newArgv[MAX_LIB_ARGS];
579 static char args[PROPERTY_VALUE_MAX];
580 rilArgv = newArgv;
581 property_get(LIB_ARGS_PROPERTY, args, "");
582 argc = make_argv(args, rilArgv);
583 }
584
585 rilArgv[argc++] = "-c";
586 rilArgv[argc++] = (char*)clientId;
587 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
588
589 // Make sure there's a reasonable argv[0]
590 rilArgv[0] = argv[0];
591
592 funcs = rilInit(&s_rilEnv, argc, rilArgv);
593 RLOGD("RIL_Init rilInit completed");
594
595 RIL_register(funcs);
596
597 RLOGD("RIL_Init RIL_register completed");
598
599 if (rilUimInit) {
600 RLOGD("RIL_register_socket started");
601 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
602 }
603
604 RLOGD("RIL_register_socket completed");
605
606done:
607
608 rilc_thread_pool();
609
610 RLOGD("RIL_Init starting sleep loop");*/
xj9db5b622022-05-04 16:50:24 +0800611 // while (1) {
612 // // ALOGI("start autosuspend_enable:%d\n",(i++));
613 // sleep(5);
xj4049f512022-04-02 15:41:13 +0800614
xj9db5b622022-05-04 16:50:24 +0800615 // }
xj4049f512022-04-02 15:41:13 +0800616}