| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 1 | /* //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> |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 20 | #include <stdbool.h> |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 21 | #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> |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 28 | #include <log/log.h> |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 29 | #include <liblog/lynq_deflog.h> |
| 30 | #include <include/lynq_uci.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <sys/un.h> |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 33 | #include <signal.h> |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 34 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 35 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 36 | #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 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 43 | #define SOCK_PATH "/tmp/autosuspend.cmd.server" //不能在当前这个目录创建socket文件,否则报错找不到文件(可能是因为这是在共享文件夹下,不支持创建socket文件) |
| 44 | |
| 45 | #define SOCK_DATA_PATH "/tmp/autosuspend.data.server" |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 46 | |
| 47 | // #define LYINFLOG(X...) lynq_log_global_output(LOG_INFO,X) |
| 48 | |
| 49 | #define TIME_OUT_TIME 30 |
| 50 | |
| 51 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 52 | #define MAX_LIB_ARGS 16 |
| 53 | |
| 54 | int adb_debug_mode = 0; |
| 55 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 56 | |
| 57 | pthread_cond_t feedback_cond = PTHREAD_COND_INITIALIZER; |
| 58 | pthread_mutex_t feedback_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 59 | pthread_mutex_t time_info_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 60 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 61 | extern int autosuspend_enable(void); |
| 62 | extern int autosuspend_disable(void); |
| 63 | extern void init_wakelock_func(void); |
| 64 | extern void init_sim_func(); |
| 65 | extern void init_network_func(); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 66 | extern void set_wakeup_callback(void (*func)(bool success)); |
| 67 | extern void wakeup_feedback(bool success); |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 68 | extern int (*lynq_screen)(int num); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 69 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 70 | struct time_info_t |
| 71 | { |
| 72 | long sleep_start_time; |
| 73 | long wakeup_time; |
| 74 | }; |
| 75 | |
| 76 | struct time_info_t time_info; |
| 77 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 78 | static 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 | |
| 85 | static 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 | |
| 99 | static 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 | |
| 116 | static 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 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 127 | |
| 128 | static 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 | |
| 140 | static 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 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 150 | |
| 151 | static 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 | |
| 180 | static 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 | } |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 192 | else if(errno == EAGAIN || errno == EWOULDBLOCK) |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 193 | { |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 194 | ALOGI("read time out\n"); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 195 | 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 | |
| 208 | static 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 | |
| 227 | static int Close(int fd) |
| 228 | { |
| 229 | if (close(fd) == -1) |
| 230 | { |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 231 | ALOGI("close error\n"); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 232 | return -1; |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 237 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 238 | void *deal_autosuspend(void *sockfd) |
| 239 | { |
| 240 | int commfd = *((int *)sockfd); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 241 | char buf[20]; |
| 242 | char res[15]; |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 243 | |
| 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的现象 |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 250 | if(Read(commfd,buf,sizeof(buf)) <= 0) |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 251 | { |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 252 | ALOGI("service receive suspend_cmd fail or client is closed.\n"); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 253 | Close(commfd); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | if(strcmp(buf,"enable") == 0) |
| 257 | { |
| jb.qi | ae8158e | 2022-09-22 00:39:45 -0700 | [diff] [blame] | 258 | /* |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 259 | 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.qi | ae8158e | 2022-09-22 00:39:45 -0700 | [diff] [blame] | 267 | sleep(5);*/ |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 268 | if(autosuspend_enable() < 0) |
| 269 | { |
| 270 | ALOGI("autosuspend_enable fail.\n"); |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 271 | // 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 | // } |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 278 | } |
| 279 | else |
| 280 | { |
| 281 | ALOGI("autosuspend_enable success.\n"); |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 282 | // 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 | // } |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | else if(strcmp(buf,"disable") == 0) |
| 292 | { |
| 293 | if(autosuspend_disable() < 0) |
| 294 | { |
| 295 | ALOGI("autosuspend_disable fail.\n"); |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 296 | // 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 | // } |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 303 | } |
| 304 | else |
| 305 | { |
| 306 | ALOGI("autosuspend_disable success.\n"); |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 307 | // 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 | // } |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 314 | } |
| 315 | } |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 316 | // 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 | // } |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 345 | else |
| 346 | { |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 347 | ALOGI("Unknown cmd : %s\n",buf); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 348 | } |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 349 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
| 353 | |
| 354 | } |
| jb.qi | 6734e17 | 2022-11-12 21:47:13 -0800 | [diff] [blame] | 355 | /*jb.qi add for service send when DTR is low on 20221111 start */ |
| 356 | void *dtr_wakeup() |
| 357 | { |
| 358 | FILE *fp; |
| 359 | int ret; |
| 360 | bool success = true; |
| 361 | char buf[30]; |
| 362 | char dtr_buffer[25]; |
| 363 | RLOGD("dtr_wakeup start\n"); |
| 364 | while(1) |
| 365 | { |
| 366 | fp = popen("cat /sys/devices/platform/10005000.pinctrl/mt_gpio |grep 006:","r"); |
| 367 | fgets(dtr_buffer, sizeof(dtr_buffer), fp); |
| 368 | if(dtr_buffer[7] == '0') |
| 369 | { |
| 370 | time_info.sleep_start_time = 123; |
| 371 | time_info.wakeup_time = 123; |
| 372 | if (pthread_cond_broadcast(&feedback_cond) != 0) |
| 373 | { |
| 374 | strerror_r(errno, buf, sizeof(buf)); |
| 375 | ALOGI("Error broadcast cond: %s\n", buf); |
| 376 | } |
| 377 | RLOGD("dtr_wakeup success!\n"); |
| 378 | sleep(3); |
| 379 | } |
| jb.qi | 1256615 | 2023-01-03 22:54:36 -0800 | [diff] [blame] | 380 | usleep(500000); |
| jb.qi | 6734e17 | 2022-11-12 21:47:13 -0800 | [diff] [blame] | 381 | pclose(fp); |
| 382 | } |
| 383 | } |
| 384 | /*jb.qi add for service send when DTR is low on 20221111 end */ |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 385 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 386 | void *send_feedback(void *sockfd) |
| 387 | { |
| 388 | int commfd = *((int *)sockfd); |
| 389 | char buf[80]; |
| 390 | |
| 391 | while (1) |
| 392 | { |
| 393 | memset(buf,0,sizeof(buf)); |
| 394 | ALOGI("send_feedback thread wait to send.\n"); |
| 395 | pthread_mutex_lock(&feedback_mutex); |
| 396 | pthread_cond_wait(&feedback_cond,&feedback_mutex); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 397 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 398 | ALOGI("send_feedback thread is now sending the feedback to client.\n"); |
| 399 | pthread_mutex_lock(&time_info_mutex); |
| 400 | if(Write(commfd,&time_info,sizeof(struct time_info_t)) <= 0) |
| 401 | { |
| 402 | ALOGI("service send wakeup_feedback struct fail.\n"); |
| 403 | Close(commfd); |
| 404 | pthread_mutex_unlock(&time_info_mutex); |
| 405 | pthread_mutex_unlock(&feedback_mutex); |
| jb.qi | 6734e17 | 2022-11-12 21:47:13 -0800 | [diff] [blame] | 406 | continue ;//jb.qi add for service send when DTR is low on 20221111 |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 407 | } |
| 408 | pthread_mutex_unlock(&time_info_mutex); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 409 | pthread_mutex_unlock(&feedback_mutex); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 414 | |
| qjb | 66daefd | 2023-07-19 03:51:50 -0700 | [diff] [blame] | 415 | void *check_wakeup_sources(void *sockfd) |
| 416 | { |
| 417 | FILE *fp; |
| 418 | int ret; |
| 419 | char buf[256]; |
| 420 | RLOGD("start check wakeup_sources !!!\n"); |
| 421 | while(1) |
| 422 | { |
| 423 | memset(buf,0,sizeof(buf)); |
| 424 | fp = popen("cat /sys/kernel/debug/wakeup_sources|sed -e 's/\"^ \"/\"unnamed\"/g' | awk '{print $6 \"\t\" $1}'| grep -v \"^0\" |sort -n \n","r"); |
| 425 | while(fgets(buf, 255, fp) != NULL) |
| 426 | { |
| 427 | RLOGD("%s", buf); |
| 428 | } |
| 429 | pclose(fp); |
| 430 | sleep(3); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 435 | int main(int argc, char **argv) { |
| 436 | |
| 437 | |
| 438 | // int i = 0; |
| 439 | // RLOGD("**Autosuspend Service Daemon Started**"); |
| 440 | // RLOGD("**Autosuspend Service param count=%d**", argc); |
| 441 | char tmp[20]; |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 442 | |
| 443 | int commfd, commfd_data, server_sock, server_data_sock,len, len_data; |
| 444 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 445 | struct sockaddr_un server_sockaddr; |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 446 | struct sockaddr_un server_data_sockaddr; |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 447 | struct sockaddr_un client_sockaddr; |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 448 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 449 | len = sizeof(server_sockaddr); |
| 450 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 451 | |
| qjb | 66daefd | 2023-07-19 03:51:50 -0700 | [diff] [blame] | 452 | pthread_t tid,tid_1,tid_2; //jb.qi add for service send when DTR is low on 20221111 |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 453 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 454 | LYLOGEINIT(USER_LOG_TAG); |
| 455 | LYLOGSET(LOG_DEBUG); |
| 456 | // LYLOGSET(LOG_ERROR); |
| 457 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 458 | int auto_enable = 0; |
| jb.qi | 1bddf57 | 2023-11-29 21:33:46 -0800 | [diff] [blame^] | 459 | system("uci set lynq_uci.lynq_autosuspend.debug='0'");//jb.qi add for gsw close debug when power_on on 20231130 |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 460 | lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "debug", tmp); // 即获取系统层面的环境变量 |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 461 | ALOGI("Autosuspend Service Daemon. debug %s\n",tmp); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 462 | adb_debug_mode=atoi(tmp); |
| 463 | lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "auto_enable", tmp); |
| 464 | auto_enable=atoi(tmp); |
| 465 | ALOGI("Autosuspend Service Daemon. auto_enable %s\n",tmp); |
| 466 | init_wakelock_func(); |
| 467 | init_sim_func(); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 468 | |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 469 | signal(SIGPIPE,SIG_IGN); // 忽略SIGPIPE信号,防止由于客户端关闭,继续往客户端write,会导致服务端收到SIGPIPE信号而Broken pipe |
| 470 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 471 | set_wakeup_callback(wakeup_feedback); |
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 472 | // 注册回调函数 |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 473 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 474 | if(auto_enable==0) |
| 475 | { |
| 476 | if(autosuspend_disable() < 0) |
| 477 | { |
| 478 | ALOGI("autosuspend_disable fail.\n"); |
| 479 | } |
| 480 | else |
| 481 | { |
| 482 | ALOGI("autosuspend_disable success.\n"); |
| 483 | } |
| 484 | } |
| 485 | if(auto_enable==1) |
| 486 | { |
| 487 | if(autosuspend_enable() < 0) |
| 488 | { |
| 489 | ALOGI("autosuspend_enable fail.\n"); |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | ALOGI("autosuspend_enable success.\n"); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | |
| 498 | server_sock = listen_port(&server_sockaddr,SOCK_PATH); |
| 499 | if(server_sock == -1) |
| 500 | return -1; |
| 501 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 502 | server_data_sock = listen_port(&server_data_sockaddr,SOCK_DATA_PATH); |
| 503 | if(server_data_sock == -1) |
| 504 | return -1; |
| 505 | |
| jb.qi | 6734e17 | 2022-11-12 21:47:13 -0800 | [diff] [blame] | 506 | /*jb.qi add for service send when DTR is low on 20221111 start*/ |
| 507 | pthread_create(&tid_1,NULL,dtr_wakeup,NULL); |
| 508 | pthread_detach(tid_1); |
| 509 | /*jb.qi add for service send when DTR is low on 20221111 end*/ |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 510 | |
| qjb | 66daefd | 2023-07-19 03:51:50 -0700 | [diff] [blame] | 511 | if(adb_debug_mode == 2) |
| 512 | { |
| 513 | pthread_create(&tid_2,NULL, check_wakeup_sources,NULL); |
| 514 | pthread_detach(tid_2); |
| 515 | } |
| 516 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 517 | while (1) |
| 518 | { |
| 519 | ALOGI("service socket listening...\n"); |
| 520 | commfd = Accept(server_sock,(struct sockaddr *)&client_sockaddr,&len); |
| 521 | if(commfd == -1) |
| 522 | { |
| 523 | return -1; |
| 524 | } |
| 525 | if(getpeername(commfd, (struct sockaddr *)&client_sockaddr, &len) == -1) |
| 526 | { |
| 527 | ALOGI("GETPEERNAME ERROR.\n"); |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 528 | // Close(server_sock); |
| 529 | Close(commfd); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 530 | continue; |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | ALOGI("Client socket filepath: %s\n", client_sockaddr.sun_path); |
| 535 | } |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 536 | |
| 537 | commfd_data = Accept(server_data_sock,NULL,NULL); |
| 538 | if(commfd_data == -1) |
| 539 | { |
| 540 | return -1; |
| 541 | } |
| 542 | ALOGI("data channel connected.\n"); |
| 543 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 544 | pthread_create(&tid,NULL,deal_autosuspend,(void*)&commfd);//这里很容易错,最后一个参数要取地址,这是一个指针 |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 545 | pthread_detach(tid); |
| 546 | |
| 547 | pthread_create(&tid,NULL,send_feedback,(void*)&commfd_data); |
| 548 | pthread_detach(tid); |
| 549 | |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 550 | |
| 551 | } |
| 552 | |
| 553 | /* for (i = 1; i < argc ;) { |
| 554 | if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { |
| 555 | rilLibPath = argv[i + 1]; |
| 556 | i += 2; |
| 557 | } else if (0 == strcmp(argv[i], "--")) { |
| 558 | i++; |
| 559 | hasLibArgs = 1; |
| 560 | break; |
| 561 | } else if (0 == strcmp(argv[i], "-c") && (argc - i > 1)) { |
| 562 | clientId = argv[i+1]; |
| 563 | i += 2; |
| 564 | } else { |
| 565 | usage(argv[0]); |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | if (clientId == NULL) { |
| 570 | clientId = "0"; |
| 571 | } else if (atoi(clientId) >= MAX_RILDS) { |
| 572 | RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); |
| 573 | exit(0); |
| 574 | } |
| 575 | if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { |
| 576 | strncpy(ril_service_name, ril_service_name_base, MAX_SERVICE_NAME_LENGTH); |
| 577 | strncat(ril_service_name, clientId, MAX_SERVICE_NAME_LENGTH); |
| 578 | } |
| 579 | |
| 580 | if (rilLibPath == NULL) { |
| 581 | if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { |
| 582 | // No lib sepcified on the command line, and nothing set in props. |
| 583 | // Assume "no-ril" case. |
| 584 | goto done; |
| 585 | } else { |
| 586 | rilLibPath = libPath; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | dlHandle = dlopen(rilLibPath, RTLD_NOW); |
| 591 | |
| 592 | if (dlHandle == NULL) { |
| 593 | RLOGE("dlopen failed: %s", dlerror()); |
| 594 | exit(EXIT_FAILURE); |
| 595 | } |
| 596 | |
| 597 | RIL_startEventLoop(); |
| 598 | |
| 599 | rilInit = |
| 600 | (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
| 601 | dlsym(dlHandle, "RIL_Init"); |
| 602 | |
| 603 | if (rilInit == NULL) { |
| 604 | RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); |
| 605 | exit(EXIT_FAILURE); |
| 606 | } |
| 607 | |
| 608 | dlerror(); // Clear any previous dlerror |
| 609 | rilUimInit = |
| 610 | (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) |
| 611 | dlsym(dlHandle, "RIL_SAP_Init"); |
| 612 | err_str = dlerror(); |
| 613 | if (err_str) { |
| 614 | RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str); |
| 615 | } else if (!rilUimInit) { |
| 616 | RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath); |
| 617 | } |
| 618 | |
| 619 | if (hasLibArgs) { |
| 620 | rilArgv = argv + i - 1; |
| 621 | argc = argc -i + 1; |
| 622 | } else { |
| 623 | static char * newArgv[MAX_LIB_ARGS]; |
| 624 | static char args[PROPERTY_VALUE_MAX]; |
| 625 | rilArgv = newArgv; |
| 626 | property_get(LIB_ARGS_PROPERTY, args, ""); |
| 627 | argc = make_argv(args, rilArgv); |
| 628 | } |
| 629 | |
| 630 | rilArgv[argc++] = "-c"; |
| 631 | rilArgv[argc++] = (char*)clientId; |
| 632 | RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); |
| 633 | |
| 634 | // Make sure there's a reasonable argv[0] |
| 635 | rilArgv[0] = argv[0]; |
| 636 | |
| 637 | funcs = rilInit(&s_rilEnv, argc, rilArgv); |
| 638 | RLOGD("RIL_Init rilInit completed"); |
| 639 | |
| 640 | RIL_register(funcs); |
| 641 | |
| 642 | RLOGD("RIL_Init RIL_register completed"); |
| 643 | |
| 644 | if (rilUimInit) { |
| 645 | RLOGD("RIL_register_socket started"); |
| 646 | RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv); |
| 647 | } |
| 648 | |
| 649 | RLOGD("RIL_register_socket completed"); |
| 650 | |
| 651 | done: |
| 652 | |
| 653 | rilc_thread_pool(); |
| 654 | |
| 655 | RLOGD("RIL_Init starting sleep loop");*/ |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 656 | // while (1) { |
| 657 | // // ALOGI("start autosuspend_enable:%d\n",(i++)); |
| 658 | // sleep(5); |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 659 | |
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 660 | // } |
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 661 | } |