| 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 |         } | 
 | 380 |         usleep(500); | 
 | 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); | 
 | 409 |  | 
 | 410 |         pthread_mutex_unlock(&feedback_mutex); | 
 | 411 |  | 
 | 412 |  | 
 | 413 |  | 
 | 414 |     } | 
 | 415 |  | 
 | 416 | } | 
 | 417 |  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 418 |  | 
 | 419 | int main(int argc, char **argv) { | 
 | 420 |  | 
 | 421 |  | 
 | 422 |     // int i = 0; | 
 | 423 |     // RLOGD("**Autosuspend Service Daemon Started**"); | 
 | 424 |     // RLOGD("**Autosuspend Service param count=%d**", argc); | 
 | 425 |     char tmp[20]; | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 426 |  | 
 | 427 |     int commfd, commfd_data, server_sock, server_data_sock,len, len_data; | 
 | 428 |  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 429 |     struct sockaddr_un server_sockaddr; | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 430 |     struct sockaddr_un server_data_sockaddr; | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 431 |     struct sockaddr_un client_sockaddr; | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 432 |  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 433 |     len = sizeof(server_sockaddr); | 
 | 434 |  | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 435 |  | 
| jb.qi | 6734e17 | 2022-11-12 21:47:13 -0800 | [diff] [blame] | 436 |     pthread_t tid,tid_1; //jb.qi add for service send when DTR is low on 20221111  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 437 |      | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 438 |     LYLOGEINIT(USER_LOG_TAG); | 
 | 439 |     LYLOGSET(LOG_DEBUG);     | 
 | 440 |     // LYLOGSET(LOG_ERROR); | 
 | 441 |  | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 442 |     int auto_enable = 0; | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 443 |  | 
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 444 |     lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "debug", tmp); // 即获取系统层面的环境变量 | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 445 |     ALOGI("Autosuspend Service Daemon. debug %s\n",tmp); | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 446 |     adb_debug_mode=atoi(tmp); | 
 | 447 |     lynq_get_value(LOG_UCI_FILE, LOG_UCI_MODULE, "auto_enable", tmp); | 
 | 448 |     auto_enable=atoi(tmp); | 
 | 449 |     ALOGI("Autosuspend Service Daemon. auto_enable %s\n",tmp); | 
 | 450 |     init_wakelock_func(); | 
 | 451 |     init_sim_func(); | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 452 |  | 
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 453 |     signal(SIGPIPE,SIG_IGN); // 忽略SIGPIPE信号,防止由于客户端关闭,继续往客户端write,会导致服务端收到SIGPIPE信号而Broken pipe | 
 | 454 |  | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 455 |      | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 456 |     // init_network_func(); | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 457 |  | 
 | 458 |     // if(pthread_cond_init(&feedback_cond,NULL) != 0) | 
 | 459 |     // { | 
 | 460 |     //     strerror_r(errno, buf, sizeof(buf)); | 
 | 461 |     //     ALOGI("Error creating cond: %s\n", buf); | 
 | 462 |     //     return -1; | 
 | 463 |     // } | 
 | 464 |  | 
 | 465 |     set_wakeup_callback(wakeup_feedback); | 
| xj | e74bccc | 2022-05-09 10:34:43 +0800 | [diff] [blame] | 466 |     // 注册回调函数 | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 467 |  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 468 |    if(auto_enable==0) | 
 | 469 |    { | 
 | 470 |       if(autosuspend_disable() < 0) | 
 | 471 |      { | 
 | 472 |        ALOGI("autosuspend_disable fail.\n"); | 
 | 473 |      } | 
 | 474 |      else | 
 | 475 |      { | 
 | 476 |        ALOGI("autosuspend_disable success.\n"); | 
 | 477 |      } | 
 | 478 |    } | 
 | 479 |    if(auto_enable==1) | 
 | 480 |    { | 
 | 481 |       if(autosuspend_enable() < 0) | 
 | 482 |      { | 
 | 483 |        ALOGI("autosuspend_enable fail.\n"); | 
 | 484 |      } | 
 | 485 |      else | 
 | 486 |      { | 
 | 487 |        ALOGI("autosuspend_enable success.\n"); | 
 | 488 |      } | 
 | 489 |    } | 
 | 490 |  | 
 | 491 |  | 
 | 492 |     server_sock = listen_port(&server_sockaddr,SOCK_PATH); | 
 | 493 |     if(server_sock == -1) | 
 | 494 |         return -1; | 
 | 495 |  | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 496 |     server_data_sock = listen_port(&server_data_sockaddr,SOCK_DATA_PATH); | 
 | 497 |     if(server_data_sock == -1) | 
 | 498 |         return -1; | 
 | 499 |  | 
| jb.qi | 6734e17 | 2022-11-12 21:47:13 -0800 | [diff] [blame] | 500 |     /*jb.qi add for service send when DTR is low on 20221111 start*/ | 
 | 501 |     pthread_create(&tid_1,NULL,dtr_wakeup,NULL); | 
 | 502 |     pthread_detach(tid_1); | 
 | 503 |     /*jb.qi add for service send when DTR is low on 20221111 end*/ | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 504 |  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 505 |     while (1) | 
 | 506 |     { | 
 | 507 |         ALOGI("service socket listening...\n"); | 
 | 508 |         commfd = Accept(server_sock,(struct sockaddr *)&client_sockaddr,&len); | 
 | 509 |         if(commfd == -1) | 
 | 510 |         { | 
 | 511 |             return -1; | 
 | 512 |         } | 
 | 513 |         if(getpeername(commfd, (struct sockaddr *)&client_sockaddr, &len) == -1) | 
 | 514 |         { | 
 | 515 |             ALOGI("GETPEERNAME ERROR.\n"); | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 516 |             // Close(server_sock); | 
 | 517 |             Close(commfd); | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 518 |             continue; | 
 | 519 |         } | 
 | 520 |         else | 
 | 521 |         { | 
 | 522 |             ALOGI("Client socket filepath: %s\n", client_sockaddr.sun_path); | 
 | 523 |         } | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 524 |  | 
 | 525 |         commfd_data = Accept(server_data_sock,NULL,NULL); | 
 | 526 |         if(commfd_data == -1) | 
 | 527 |         { | 
 | 528 |             return -1; | 
 | 529 |         } | 
 | 530 |         ALOGI("data channel connected.\n"); | 
 | 531 |          | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 532 |         pthread_create(&tid,NULL,deal_autosuspend,(void*)&commfd);//这里很容易错,最后一个参数要取地址,这是一个指针 | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 533 |         pthread_detach(tid);    | 
 | 534 |  | 
 | 535 |         pthread_create(&tid,NULL,send_feedback,(void*)&commfd_data); | 
 | 536 |         pthread_detach(tid);  | 
 | 537 |  | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 538 |  | 
 | 539 |     } | 
 | 540 |      | 
 | 541 |  /*   for (i = 1; i < argc ;) { | 
 | 542 |         if (0 == strcmp(argv[i], "-l") && (argc - i > 1)) { | 
 | 543 |             rilLibPath = argv[i + 1]; | 
 | 544 |             i += 2; | 
 | 545 |         } else if (0 == strcmp(argv[i], "--")) { | 
 | 546 |             i++; | 
 | 547 |             hasLibArgs = 1; | 
 | 548 |             break; | 
 | 549 |         } else if (0 == strcmp(argv[i], "-c") &&  (argc - i > 1)) { | 
 | 550 |             clientId = argv[i+1]; | 
 | 551 |             i += 2; | 
 | 552 |         } else { | 
 | 553 |             usage(argv[0]); | 
 | 554 |         } | 
 | 555 |     } | 
 | 556 |  | 
 | 557 |     if (clientId == NULL) { | 
 | 558 |         clientId = "0"; | 
 | 559 |     } else if (atoi(clientId) >= MAX_RILDS) { | 
 | 560 |         RLOGE("Max Number of rild's supported is: %d", MAX_RILDS); | 
 | 561 |         exit(0); | 
 | 562 |     } | 
 | 563 |     if (strncmp(clientId, "0", MAX_CLIENT_ID_LENGTH)) { | 
 | 564 |         strncpy(ril_service_name, ril_service_name_base, MAX_SERVICE_NAME_LENGTH); | 
 | 565 |         strncat(ril_service_name, clientId, MAX_SERVICE_NAME_LENGTH); | 
 | 566 |     } | 
 | 567 |  | 
 | 568 |     if (rilLibPath == NULL) { | 
 | 569 |         if ( 0 == property_get(LIB_PATH_PROPERTY, libPath, NULL)) { | 
 | 570 |             // No lib sepcified on the command line, and nothing set in props. | 
 | 571 |             // Assume "no-ril" case. | 
 | 572 |             goto done; | 
 | 573 |         } else { | 
 | 574 |             rilLibPath = libPath; | 
 | 575 |         } | 
 | 576 |     } | 
 | 577 |  | 
 | 578 |     dlHandle = dlopen(rilLibPath, RTLD_NOW); | 
 | 579 |  | 
 | 580 |     if (dlHandle == NULL) { | 
 | 581 |         RLOGE("dlopen failed: %s", dlerror()); | 
 | 582 |         exit(EXIT_FAILURE); | 
 | 583 |     } | 
 | 584 |  | 
 | 585 |     RIL_startEventLoop(); | 
 | 586 |  | 
 | 587 |     rilInit = | 
 | 588 |         (const RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) | 
 | 589 |         dlsym(dlHandle, "RIL_Init"); | 
 | 590 |  | 
 | 591 |     if (rilInit == NULL) { | 
 | 592 |         RLOGE("RIL_Init not defined or exported in %s\n", rilLibPath); | 
 | 593 |         exit(EXIT_FAILURE); | 
 | 594 |     } | 
 | 595 |  | 
 | 596 |     dlerror(); // Clear any previous dlerror | 
 | 597 |     rilUimInit = | 
 | 598 |         (RIL_RadioFunctions *(*)(const struct RIL_Env *, int, char **)) | 
 | 599 |         dlsym(dlHandle, "RIL_SAP_Init"); | 
 | 600 |     err_str = dlerror(); | 
 | 601 |     if (err_str) { | 
 | 602 |         RLOGW("RIL_SAP_Init not defined or exported in %s: %s\n", rilLibPath, err_str); | 
 | 603 |     } else if (!rilUimInit) { | 
 | 604 |         RLOGW("RIL_SAP_Init defined as null in %s. SAP Not usable\n", rilLibPath); | 
 | 605 |     } | 
 | 606 |  | 
 | 607 |     if (hasLibArgs) { | 
 | 608 |         rilArgv = argv + i - 1; | 
 | 609 |         argc = argc -i + 1; | 
 | 610 |     } else { | 
 | 611 |         static char * newArgv[MAX_LIB_ARGS]; | 
 | 612 |         static char args[PROPERTY_VALUE_MAX]; | 
 | 613 |         rilArgv = newArgv; | 
 | 614 |         property_get(LIB_ARGS_PROPERTY, args, ""); | 
 | 615 |         argc = make_argv(args, rilArgv); | 
 | 616 |     } | 
 | 617 |  | 
 | 618 |     rilArgv[argc++] = "-c"; | 
 | 619 |     rilArgv[argc++] = (char*)clientId; | 
 | 620 |     RLOGD("RIL_Init argc = %d clientId = %s", argc, rilArgv[argc-1]); | 
 | 621 |  | 
 | 622 |     // Make sure there's a reasonable argv[0] | 
 | 623 |     rilArgv[0] = argv[0]; | 
 | 624 |  | 
 | 625 |     funcs = rilInit(&s_rilEnv, argc, rilArgv); | 
 | 626 |     RLOGD("RIL_Init rilInit completed"); | 
 | 627 |  | 
 | 628 |     RIL_register(funcs); | 
 | 629 |  | 
 | 630 |     RLOGD("RIL_Init RIL_register completed"); | 
 | 631 |  | 
 | 632 |     if (rilUimInit) { | 
 | 633 |         RLOGD("RIL_register_socket started"); | 
 | 634 |         RIL_register_socket(rilUimInit, RIL_SAP_SOCKET, argc, rilArgv); | 
 | 635 |     } | 
 | 636 |  | 
 | 637 |     RLOGD("RIL_register_socket completed"); | 
 | 638 |  | 
 | 639 | done: | 
 | 640 |  | 
 | 641 |     rilc_thread_pool(); | 
 | 642 |  | 
 | 643 |     RLOGD("RIL_Init starting sleep loop");*/ | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 644 |     // while (1) { | 
 | 645 |     //     // ALOGI("start autosuspend_enable:%d\n",(i++)); | 
 | 646 |     //     sleep(5); | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 647 |  | 
| xj | 9db5b62 | 2022-05-04 16:50:24 +0800 | [diff] [blame] | 648 |     // } | 
| xj | 4049f51 | 2022-04-02 15:41:13 +0800 | [diff] [blame] | 649 | } |