blob: 9b997db4c609cd67eea92d141700360fc975e32f [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 {
xje74bccc2022-05-09 10:34:43 +0800258 system("echo 7 | emdlogger_ctrl");
259
260 if (lynq_screen(0) < 0) //notify ril for screen off
261 {
262 ALOGI("lynq_screen off fail\n");
263 return -1;
264 }
265
266 sleep(5);
xj4049f512022-04-02 15:41:13 +0800267 if(autosuspend_enable() < 0)
268 {
269 ALOGI("autosuspend_enable fail.\n");
xje74bccc2022-05-09 10:34:43 +0800270 // strcpy(res,"fail");
271 // if(Write(commfd,res,strlen(res)) <= 0)
272 // {
273 // ALOGI("service send respond fail.\n");
274 // Close(commfd);
275 // break;
276 // }
xj4049f512022-04-02 15:41:13 +0800277 }
278 else
279 {
280 ALOGI("autosuspend_enable success.\n");
xje74bccc2022-05-09 10:34:43 +0800281 // strcpy(res,"enabled");
282 // if(Write(commfd,res,strlen(res)) <= 0)
283 // {
284 // ALOGI("service send respond fail.\n");
285 // Close(commfd);
286 // break;
287 // }
xj4049f512022-04-02 15:41:13 +0800288 }
289 }
290 else if(strcmp(buf,"disable") == 0)
291 {
292 if(autosuspend_disable() < 0)
293 {
294 ALOGI("autosuspend_disable fail.\n");
xje74bccc2022-05-09 10:34:43 +0800295 // strcpy(res,"fail");
296 // if(Write(commfd,res,strlen(res)) <= 0)
297 // {
298 // ALOGI("service send respond fail.\n");
299 // Close(commfd);
300 // break;
301 // }
xj4049f512022-04-02 15:41:13 +0800302 }
303 else
304 {
305 ALOGI("autosuspend_disable success.\n");
xje74bccc2022-05-09 10:34:43 +0800306 // strcpy(res,"disabled");
307 // if(Write(commfd,res,strlen(res)) <= 0)
308 // {
309 // ALOGI("service send respond fail.\n");
310 // Close(commfd);
311 // break;
312 // }
xj4049f512022-04-02 15:41:13 +0800313 }
314 }
xj9db5b622022-05-04 16:50:24 +0800315 // else if(strcmp(buf,"feedback") == 0)
316 // {
317
318 // ALOGI("send_feedback thread wait to send.\n");
319 // if (pthread_cond_wait(&feedback_cond,&feedback_mutex) != 0)
320 // {
321 // strerror_r(errno, buf, sizeof(buf));
322 // ALOGI("Error waiting on cond: %s\n", buf);
323 // Close(commfd);
324 // break;
325 // }
326
327 // ALOGI("send_feedback thread is now sending the feedback to client.\n");
328 // if(Write(commfd,&time_info,sizeof(struct time_info_t)) <= 0)
329 // {
330 // ALOGI("service send wakeup_feedback struct fail.\n");
331 // Close(commfd);
332 // break ;
333 // }
334 // ALOGI("service send feedback success.\n");
335 // strcpy(res,"success");
336 // if(Write(commfd,res,strlen(res)) <= 0)
337 // {
338 // ALOGI("service send respond fail.\n");
339 // Close(commfd);
340 // break;
341 // }
342
343 // }
xj4049f512022-04-02 15:41:13 +0800344 else
345 {
xj9db5b622022-05-04 16:50:24 +0800346 ALOGI("Unknown cmd : %s\n",buf);
xj4049f512022-04-02 15:41:13 +0800347 }
xj9db5b622022-05-04 16:50:24 +0800348
xj4049f512022-04-02 15:41:13 +0800349 }
350
351
352
353}
354
355
xj9db5b622022-05-04 16:50:24 +0800356void *send_feedback(void *sockfd)
357{
358 int commfd = *((int *)sockfd);
359 char buf[80];
360
361 while (1)
362 {
363 memset(buf,0,sizeof(buf));
364 ALOGI("send_feedback thread wait to send.\n");
365 pthread_mutex_lock(&feedback_mutex);
366 pthread_cond_wait(&feedback_cond,&feedback_mutex);
xj9db5b622022-05-04 16:50:24 +0800367
xj9db5b622022-05-04 16:50:24 +0800368 ALOGI("send_feedback thread is now sending the feedback to client.\n");
369 pthread_mutex_lock(&time_info_mutex);
370 if(Write(commfd,&time_info,sizeof(struct time_info_t)) <= 0)
371 {
372 ALOGI("service send wakeup_feedback struct fail.\n");
373 Close(commfd);
374 pthread_mutex_unlock(&time_info_mutex);
375 pthread_mutex_unlock(&feedback_mutex);
376 break ;
377 }
378 pthread_mutex_unlock(&time_info_mutex);
379
380 pthread_mutex_unlock(&feedback_mutex);
381
382
383
384 }
385
386}
387
xj4049f512022-04-02 15:41:13 +0800388
389int main(int argc, char **argv) {
390
391
392 // int i = 0;
393 // RLOGD("**Autosuspend Service Daemon Started**");
394 // RLOGD("**Autosuspend Service param count=%d**", argc);
395 char tmp[20];
xj9db5b622022-05-04 16:50:24 +0800396
397 int commfd, commfd_data, server_sock, server_data_sock,len, len_data;
398
xj4049f512022-04-02 15:41:13 +0800399 struct sockaddr_un server_sockaddr;
xj9db5b622022-05-04 16:50:24 +0800400 struct sockaddr_un server_data_sockaddr;
xj4049f512022-04-02 15:41:13 +0800401 struct sockaddr_un client_sockaddr;
xj9db5b622022-05-04 16:50:24 +0800402
xj4049f512022-04-02 15:41:13 +0800403 len = sizeof(server_sockaddr);
404
xj9db5b622022-05-04 16:50:24 +0800405
xj4049f512022-04-02 15:41:13 +0800406 pthread_t tid;
407
xj4049f512022-04-02 15:41:13 +0800408 LYLOGEINIT(USER_LOG_TAG);
409 LYLOGSET(LOG_DEBUG);
410 // LYLOGSET(LOG_ERROR);
411
xj9db5b622022-05-04 16:50:24 +0800412 int auto_enable = 0;
xj4049f512022-04-02 15:41:13 +0800413
xje74bccc2022-05-09 10:34:43 +0800414 lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "debug", tmp); // 即获取系统层面的环境变量
xj9db5b622022-05-04 16:50:24 +0800415 ALOGI("Autosuspend Service Daemon. debug %s\n",tmp);
xj4049f512022-04-02 15:41:13 +0800416 adb_debug_mode=atoi(tmp);
417 lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "auto_enable", tmp);
418 auto_enable=atoi(tmp);
419 ALOGI("Autosuspend Service Daemon. auto_enable %s\n",tmp);
420 init_wakelock_func();
421 init_sim_func();
xj9db5b622022-05-04 16:50:24 +0800422
xje74bccc2022-05-09 10:34:43 +0800423 signal(SIGPIPE,SIG_IGN); // 忽略SIGPIPE信号,防止由于客户端关闭,继续往客户端write,会导致服务端收到SIGPIPE信号而Broken pipe
424
xj9db5b622022-05-04 16:50:24 +0800425
xj4049f512022-04-02 15:41:13 +0800426 // init_network_func();
xj9db5b622022-05-04 16:50:24 +0800427
428 // if(pthread_cond_init(&feedback_cond,NULL) != 0)
429 // {
430 // strerror_r(errno, buf, sizeof(buf));
431 // ALOGI("Error creating cond: %s\n", buf);
432 // return -1;
433 // }
434
435 set_wakeup_callback(wakeup_feedback);
xje74bccc2022-05-09 10:34:43 +0800436 // 注册回调函数
xj9db5b622022-05-04 16:50:24 +0800437
xj4049f512022-04-02 15:41:13 +0800438 if(auto_enable==0)
439 {
440 if(autosuspend_disable() < 0)
441 {
442 ALOGI("autosuspend_disable fail.\n");
443 }
444 else
445 {
446 ALOGI("autosuspend_disable success.\n");
447 }
448 }
449 if(auto_enable==1)
450 {
451 if(autosuspend_enable() < 0)
452 {
453 ALOGI("autosuspend_enable fail.\n");
454 }
455 else
456 {
457 ALOGI("autosuspend_enable success.\n");
458 }
459 }
460
461
462 server_sock = listen_port(&server_sockaddr,SOCK_PATH);
463 if(server_sock == -1)
464 return -1;
465
xj9db5b622022-05-04 16:50:24 +0800466 server_data_sock = listen_port(&server_data_sockaddr,SOCK_DATA_PATH);
467 if(server_data_sock == -1)
468 return -1;
469
470
xj4049f512022-04-02 15:41:13 +0800471 while (1)
472 {
473 ALOGI("service socket listening...\n");
474 commfd = Accept(server_sock,(struct sockaddr *)&client_sockaddr,&len);
475 if(commfd == -1)
476 {
477 return -1;
478 }
479 if(getpeername(commfd, (struct sockaddr *)&client_sockaddr, &len) == -1)
480 {
481 ALOGI("GETPEERNAME ERROR.\n");
xj9db5b622022-05-04 16:50:24 +0800482 // Close(server_sock);
483 Close(commfd);
xj4049f512022-04-02 15:41:13 +0800484 continue;
485 }
486 else
487 {
488 ALOGI("Client socket filepath: %s\n", client_sockaddr.sun_path);
489 }
xj9db5b622022-05-04 16:50:24 +0800490
491 commfd_data = Accept(server_data_sock,NULL,NULL);
492 if(commfd_data == -1)
493 {
494 return -1;
495 }
496 ALOGI("data channel connected.\n");
497
xj4049f512022-04-02 15:41:13 +0800498 pthread_create(&tid,NULL,deal_autosuspend,(void*)&commfd);//这里很容易错,最后一个参数要取地址,这是一个指针
xj9db5b622022-05-04 16:50:24 +0800499 pthread_detach(tid);
500
501 pthread_create(&tid,NULL,send_feedback,(void*)&commfd_data);
502 pthread_detach(tid);
503
xj4049f512022-04-02 15:41:13 +0800504
505 }
506
507 /* for (i = 1; i < argc ;) {
508 if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) {
509 rilLibPath = argv[i + 1];
510 i += 2;
511 } else if (0 == strcmp(argv[i], "--")) {
512 i++;
513 hasLibArgs = 1;
514 break;
515 } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) {
516 clientId = argv[i+1];
517 i += 2;
518 } else {
519 usage(argv[0]);
520 }
521 }
522
523 if (clientId == NULL) {
524 clientId = "0";
525 } else if (atoi(clientId) >= MAX_RILDS) {
526 RLOGE("Max Number of rild's supported is: %d", MAX_RILDS);
527 exit(0);
528 }
529 if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) {
530 strncpy(ril_service_name, ril_service_name_base, MAX_SERVICE_NAME_LENGTH);
531 strncat(ril_service_name, clientId, MAX_SERVICE_NAME_LENGTH);
532 }
533
534 if (rilLibPath == NULL) {
535 if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) {
536 // No lib sepcified on the command line, and nothing set in props.
537 // Assume "no-ril" case.
538 goto done;
539 } else {
540 rilLibPath = libPath;
541 }
542 }
543
544 dlHandle = dlopen(rilLibPath, RTLD_NOW);
545
546 if (dlHandle == NULL) {
547 RLOGE("dlopen failed: %s", dlerror());
548 exit(EXIT_FAILURE);
549 }
550
551 RIL_startEventLoop();
552
553 rilInit =
554 (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
555 dlsym(dlHandle, "RIL_Init");
556
557 if (rilInit == NULL) {
558 RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath);
559 exit(EXIT_FAILURE);
560 }
561
562 dlerror(); // Clear any previous dlerror
563 rilUimInit =
564 (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **))
565 dlsym(dlHandle, "RIL_SAP_Init");
566 err_str = dlerror();
567 if (err_str) {
568 RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str);
569 } else if (!rilUimInit) {
570 RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath);
571 }
572
573 if (hasLibArgs) {
574 rilArgv = argv + i - 1;
575 argc = argc -i + 1;
576 } else {
577 static char * newArgv[MAX_LIB_ARGS];
578 static char args[PROPERTY_VALUE_MAX];
579 rilArgv = newArgv;
580 property_get(LIB_ARGS_PROPERTY, args, "");
581 argc = make_argv(args, rilArgv);
582 }
583
584 rilArgv[argc++] = "-c";
585 rilArgv[argc++] = (char*)clientId;
586 RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]);
587
588 // Make sure there's a reasonable argv[0]
589 rilArgv[0] = argv[0];
590
591 funcs = rilInit(&s_rilEnv, argc, rilArgv);
592 RLOGD("RIL_Init rilInit completed");
593
594 RIL_register(funcs);
595
596 RLOGD("RIL_Init RIL_register completed");
597
598 if (rilUimInit) {
599 RLOGD("RIL_register_socket started");
600 RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv);
601 }
602
603 RLOGD("RIL_register_socket completed");
604
605done:
606
607 rilc_thread_pool();
608
609 RLOGD("RIL_Init starting sleep loop");*/
xj9db5b622022-05-04 16:50:24 +0800610 // while (1) {
611 // // ALOGI("start autosuspend_enable:%d\n",(i++));
612 // sleep(5);
xj4049f512022-04-02 15:41:13 +0800613
xj9db5b622022-05-04 16:50:24 +0800614 // }
xj4049f512022-04-02 15:41:13 +0800615}