liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <errno.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <string.h> |
| 7 | #include <cutils/properties.h> |
| 8 | #include <time.h> |
| 9 | #include <sys/time.h> |
| 10 | #include <termios.h> //UART |
| 11 | |
| 12 | #include "mbtk_log.h" |
| 13 | #include "mbtk_adb_info.h" |
| 14 | #include "mbtk_file.h" |
| 15 | #include "mbtk_utils.h" |
| 16 | |
| 17 | // 同步收发数据 |
| 18 | #define MBTK_ADB_MSG_SYNC 1 |
| 19 | #define ADB_BUFF_SIZE 2048 |
| 20 | |
| 21 | #define DATABITS CS8 |
| 22 | #define STOPBITS 0 |
| 23 | #define PARITYON 0 |
| 24 | #define PARITY 0 |
| 25 | |
| 26 | static uint8 adb_buff[ADB_BUFF_SIZE]; |
| 27 | static int adb_buff_size = 0; |
| 28 | static uint8 *adb_buff_ptr = NULL; |
| 29 | static int file_fd = -1; |
| 30 | static int adb_fd = -1; |
| 31 | static char shell_cmd[ADB_BUFF_SIZE]; |
| 32 | |
| 33 | static int adb_port_open(const char *dev, unsigned int baud) |
| 34 | { |
| 35 | #if 0 |
| 36 | //unsigned int baud = B921600; |
| 37 | //unsigned int baud = B3000000; |
| 38 | struct termios options; |
| 39 | #if MBTK_ADB_MSG_SYNC |
| 40 | int fd = open(dev, O_RDWR | O_NOCTTY, 0644); |
| 41 | #else |
| 42 | int fd = open(dev, O_RDWR | O_NOCTTY | O_NONBLOCK, 0644); |
| 43 | #endif |
| 44 | if (fd < 0) |
| 45 | { |
| 46 | LOGE("Can't open device = (%s)", dev); |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | tcgetattr(fd, &options); |
| 51 | cfmakeraw(&options); |
| 52 | cfsetospeed(&options, baud); |
| 53 | if (tcsetattr(fd, TCSAFLUSH, &options) != 0) |
| 54 | { |
| 55 | LOGE("setting fd tc"); |
| 56 | close(fd); |
| 57 | return -1; |
| 58 | } |
| 59 | |
| 60 | tcflush(fd, TCIFLUSH); |
| 61 | return fd; |
| 62 | #else |
| 63 | |
| 64 | int fd = open(dev, O_RDWR); |
| 65 | /* set newtio */ |
| 66 | struct termios newtio; |
| 67 | memset(&newtio, 0, sizeof(newtio)); |
| 68 | (void)fcntl(fd, F_SETFL, 0); |
| 69 | #if 1 //UART2_AT |
| 70 | /* no flow control for uart by default */ |
| 71 | newtio.c_cflag = baud | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD; |
| 72 | #else |
| 73 | newtio.c_cflag = baud | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD; |
| 74 | #endif |
| 75 | newtio.c_iflag = IGNPAR; |
| 76 | //newtio.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); |
| 77 | newtio.c_oflag = 0; |
| 78 | newtio.c_lflag = 0; /* disable ECHO, ICANON, etc... */ |
| 79 | |
| 80 | newtio.c_cc[VERASE] = 0x8; /* del */ |
| 81 | newtio.c_cc[VEOF] = 4; /* Ctrl-d */ |
| 82 | newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */ |
| 83 | newtio.c_cc[VEOL] = 0xD; /* '\0' */ |
| 84 | |
| 85 | tcflush(fd, TCIFLUSH); |
| 86 | tcsetattr(fd, TCSANOW, &newtio); |
| 87 | |
| 88 | return fd; |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | static int adb_msg_send(int fd, int msg, const void *data, int data_len) |
| 93 | { |
| 94 | uint8 buff[ADB_BUFF_SIZE] = {0}; |
| 95 | uint8 *ptr = buff; |
| 96 | |
| 97 | uint32_2_byte(MBTK_ADB_PACK_FLAG, ptr, true); |
| 98 | ptr += sizeof(uint32); |
| 99 | |
| 100 | *ptr = (uint8)msg; |
| 101 | ptr++; |
| 102 | |
| 103 | uint16_2_byte((uint16)data_len, ptr, false); |
| 104 | ptr += sizeof(uint16); |
| 105 | |
| 106 | if(data && data_len > 0) { |
| 107 | memcpy(ptr, data, data_len); |
| 108 | ptr += data_len; |
| 109 | } |
| 110 | int result = file_write(fd, buff, ptr - buff); |
| 111 | |
| 112 | LOGD("SEND : %d / %d", result, ptr - buff); |
| 113 | log_hex("SEND", buff, ptr - buff); |
| 114 | return result; |
| 115 | } |
| 116 | |
| 117 | static void shell_cmd_cb_func(char *buf,int buf_size) |
| 118 | { |
| 119 | LOGD("data_len : %d", buf_size); |
| 120 | if(buf == NULL || buf_size == 0) { // shell complete |
| 121 | if(adb_msg_send(adb_fd, MBTK_ADB_MSG_CMD_RSP_COMPLETE, NULL, 0) <= 0) { |
| 122 | LOGE("Send MBTK_ADB_MSG_CMD_RSP_COMPLETE fail."); |
| 123 | } |
| 124 | |
| 125 | memset(shell_cmd, 0, ADB_BUFF_SIZE); |
| 126 | } else { |
| 127 | if(adb_msg_send(adb_fd, MBTK_ADB_MSG_CMD_RSP, buf, buf_size) <= 0) { |
| 128 | LOGE("Send MBTK_ADB_MSG_CMD_RSP fail."); |
| 129 | } |
| 130 | |
| 131 | usleep(10); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static void* shell_or_at_func(void* argv) |
| 136 | { |
| 137 | UNUSED(argv); |
| 138 | |
| 139 | // Process in thread. |
| 140 | LOGD("CMD : %s", shell_cmd); |
| 141 | if(!strncasecmp(shell_cmd, "at ", 3)) { // AT command. |
| 142 | LOGE("Process AT:%s", shell_cmd + 3); |
| 143 | |
| 144 | // "at ati" |
| 145 | if(!mbtk_cmd_line_ex(shell_cmd, shell_cmd_cb_func)) { |
| 146 | LOGE("Shell or AT command error."); |
| 147 | } |
| 148 | } else if(!strncasecmp(shell_cmd, "shell ", 6)) { // Shell command. |
| 149 | if(!strcmp(shell_cmd + 6, "cd") || !strncmp(shell_cmd + 6, "cd ", 3)) { |
| 150 | char *cmd = NULL; |
| 151 | if(!strncmp(shell_cmd + 6, "cd ", 3)) { |
| 152 | cmd = shell_cmd + 9; |
| 153 | } else { |
| 154 | cmd = "/"; |
| 155 | } |
| 156 | |
| 157 | if(chdir(cmd)) { |
| 158 | char buff[ADB_BUFF_SIZE] = {0}; |
| 159 | sprintf(buff, "Can't cd to %s", cmd); |
| 160 | if(adb_msg_send(adb_fd, MBTK_ADB_MSG_CMD_RSP_COMPLETE, buff, strlen(buff)) <= 0) { |
| 161 | LOGE("Send MBTK_ADB_MSG_CMD_RSP_COMPLETE fail."); |
| 162 | } |
| 163 | } else { |
| 164 | if(adb_msg_send(adb_fd, MBTK_ADB_MSG_CMD_RSP_COMPLETE, NULL, 0) <= 0) { |
| 165 | LOGE("Send MBTK_ADB_MSG_CMD_RSP_COMPLETE fail."); |
| 166 | } |
| 167 | } |
| 168 | } else { |
| 169 | if(!mbtk_cmd_line_ex(shell_cmd + 6, shell_cmd_cb_func)) { |
| 170 | LOGE("Shell or AT command error."); |
| 171 | } |
| 172 | } |
| 173 | } else { |
| 174 | LOGE("Command error."); |
| 175 | if(adb_msg_send(adb_fd, MBTK_ADB_MSG_ERR_COMMAND, NULL, 0) <= 0) { |
| 176 | LOGE("Send MBTK_ADB_MSG_ERR_COMMAND fail."); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | static mbtk_adb_msg_err_enum adb_pack_process(int fd, mbtk_adb_pack_t *pack) |
| 184 | { |
| 185 | static int data_recv_len = 0; |
| 186 | static int data_recv_count_len = 0; |
| 187 | // mbtk_adb_msg_err_enum err = MBTK_ADB_MSG_ERR_SUCCESS; |
| 188 | switch(pack->msg_id) { |
| 189 | case MBTK_ADB_MSG_CONN_START: |
| 190 | { |
| 191 | if(adb_msg_send(fd, MBTK_ADB_MSG_CONN_SUCCESS, NULL, 0) <= 0) { |
| 192 | return MBTK_ADB_MSG_ERR_IO; |
| 193 | } |
| 194 | |
| 195 | LOGD("CONN_SUCCESS"); |
| 196 | |
| 197 | chdir("/"); |
| 198 | break; |
| 199 | } |
| 200 | case MBTK_ADB_MSG_PUSH: |
| 201 | { |
| 202 | // data_len[4] path_len[2] path[path_len] |
| 203 | uint8 *ptr = pack->data; |
| 204 | |
| 205 | data_recv_count_len = byte_2_uint32(ptr, false); |
| 206 | if(data_recv_count_len <= 0) { |
| 207 | LOGE("File size error:%d", data_recv_count_len); |
| 208 | return MBTK_ADB_MSG_ERR_FILE_SIZE; |
| 209 | } |
| 210 | ptr += sizeof(uint32); |
| 211 | |
| 212 | char path[1024] = {0}; |
| 213 | int path_len = byte_2_uint16(ptr, false); |
| 214 | if(path_len <= 0) { |
| 215 | LOGE("path length error:%d", path_len); |
| 216 | return MBTK_ADB_MSG_ERR_FILE_PATH; |
| 217 | } |
| 218 | ptr += sizeof(uint16); |
| 219 | |
| 220 | memcpy(path, ptr, path_len); |
| 221 | |
| 222 | file_fd = open(path, O_CREAT | O_WRONLY | O_TRUNC, 0777); |
| 223 | if(file_fd < 0) { |
| 224 | LOGE("Open file(%s) error:%d", path, errno); |
| 225 | return MBTK_ADB_MSG_ERR_FILE_PATH; |
| 226 | } |
| 227 | |
| 228 | data_recv_len = 0; |
| 229 | if(adb_msg_send(fd, MBTK_ADB_MSG_PUSH_READY, NULL, 0) <= 0) { |
| 230 | return MBTK_ADB_MSG_ERR_IO; |
| 231 | } |
| 232 | |
| 233 | LOGD("Start recv file data."); |
| 234 | break; |
| 235 | } |
| 236 | case MBTK_ADB_MSG_PUSH_COMPLETE: |
| 237 | { |
| 238 | close(file_fd); |
| 239 | file_fd = -1; |
| 240 | |
| 241 | if(adb_msg_send(fd, MBTK_ADB_MSG_PUSH_SIZE, &data_recv_len, sizeof(uint32)) <= 0) { |
| 242 | return MBTK_ADB_MSG_ERR_IO; |
| 243 | } |
| 244 | |
| 245 | LOGD("PUSH_COMPLETE : %d / %d", data_recv_len, data_recv_count_len); |
| 246 | break; |
| 247 | } |
| 248 | case MBTK_ADB_MSG_CMD_REQ: |
| 249 | { |
| 250 | // Shell command or AT command. |
| 251 | memset(shell_cmd, 0, ADB_BUFF_SIZE); |
| 252 | memcpy(shell_cmd, pack->data, pack->data_len); |
| 253 | |
| 254 | pthread_t pid; |
| 255 | if (pthread_create(&pid, NULL, shell_or_at_func, NULL) != 0) { |
| 256 | LOGE("Create shell/at thread fail."); |
| 257 | return MBTK_ADB_MSG_ERR_COMMAND; |
| 258 | } |
| 259 | break; |
| 260 | } |
| 261 | case MBTK_ADB_MSG_CMD_KILL: |
| 262 | { |
| 263 | if(!strncasecmp(shell_cmd, "at ", 3)) { // AT command. |
| 264 | LOGE("Kill AT:%s", shell_cmd + 3); |
| 265 | |
| 266 | } else if(!strncasecmp(shell_cmd, "shell ", 6)) { // Shell command. |
| 267 | LOGD("Kill cmd:%s", shell_cmd + 6); |
| 268 | char cmd[1024] = {0}; |
| 269 | snprintf(cmd,1024,"kill `pidof %s | awk '{print $1}'`", shell_cmd + 6); |
| 270 | system(cmd); |
| 271 | } else { |
| 272 | LOGE("Kill Command/AT error."); |
| 273 | } |
| 274 | break; |
| 275 | } |
| 276 | case MBTK_ADB_MSG_CLOSE_START: |
| 277 | { |
| 278 | if(adb_msg_send(fd, MBTK_ADB_MSG_CLOSE_SUCCESS, NULL, 0) <= 0) { |
| 279 | return MBTK_ADB_MSG_ERR_IO; |
| 280 | } |
| 281 | break; |
| 282 | } |
| 283 | case MBTK_ADB_MSG_DATA: |
| 284 | { |
| 285 | if(file_fd < 0) { |
| 286 | LOGE("Data transfer not started."); |
| 287 | return MBTK_ADB_MSG_ERR_TRANS_NO_START; |
| 288 | } |
| 289 | |
| 290 | if(pack->data_len > 0) { |
| 291 | int size = write(file_fd, pack->data, pack->data_len); |
| 292 | if(size == pack->data_len) { |
| 293 | data_recv_len += size; |
| 294 | } else { |
| 295 | LOGE("Write error:%d", errno); |
| 296 | return MBTK_ADB_MSG_ERR_IO; |
| 297 | } |
| 298 | } |
| 299 | break; |
| 300 | } |
| 301 | default: |
| 302 | { |
| 303 | LOGE("MSG id error:%d", pack->msg_id); |
| 304 | return MBTK_ADB_MSG_ERR_UNKNOWN_MSG; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return MBTK_ADB_MSG_ERR_SUCCESS; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * 1 2 data_len |
| 313 | * F6 F7 F8 F9 [msg_id] [data_len] [data...] |
| 314 | */ |
| 315 | static mbtk_adb_msg_err_enum adb_read_a_pack(int fd, mbtk_adb_pack_t *pack) |
| 316 | { |
| 317 | memset(pack, 0x0, sizeof(mbtk_adb_pack_t)); |
| 318 | if(adb_buff_size < MBTK_ADB_PACK_HEAD_SIZE) { |
| 319 | read_once: |
| 320 | // LOGD("Will read:adb_buff_size = %d", adb_buff_size); |
| 321 | if(adb_buff_size > 0) { |
| 322 | memmove(adb_buff, adb_buff_ptr, adb_buff_size); |
| 323 | adb_buff_ptr = adb_buff; |
| 324 | } else if(adb_buff_size == 0) { |
| 325 | adb_buff_ptr = adb_buff; |
| 326 | } else { |
| 327 | LOGE("Data error:%d", adb_buff_size); |
| 328 | goto error; |
| 329 | } |
| 330 | int size = read(fd, adb_buff_ptr + adb_buff_size, ADB_BUFF_SIZE - (adb_buff_ptr - adb_buff) - adb_buff_size); |
| 331 | if(size <= 0) { |
| 332 | LOGE("read() error:%d.", errno); |
| 333 | goto error; |
| 334 | } |
| 335 | |
| 336 | #if 0 |
| 337 | log_hex("RECV", adb_buff_ptr + adb_buff_size, size); |
| 338 | #endif |
| 339 | adb_buff_size += size; |
| 340 | } |
| 341 | |
| 342 | if(adb_buff_size < MBTK_ADB_PACK_HEAD_SIZE) { |
| 343 | goto read_once; |
| 344 | } |
| 345 | |
| 346 | if(MBTK_ADB_PACK_FLAG != byte_2_uint32(adb_buff_ptr, true)) |
| 347 | { |
| 348 | LOGE("Packet FLAG error."); |
| 349 | goto error; |
| 350 | } |
| 351 | |
| 352 | pack->msg_id = (mbtk_adb_msg_enum)*(adb_buff_ptr + sizeof(uint32)); |
| 353 | pack->data_len = byte_2_uint16(adb_buff_ptr + sizeof(uint32) + sizeof(uint8), false); |
| 354 | |
| 355 | if(pack->data_len > 0 && adb_buff_size < MBTK_ADB_PACK_HEAD_SIZE + pack->data_len) { |
| 356 | goto read_once; |
| 357 | } |
| 358 | |
| 359 | if(pack->msg_id == MBTK_ADB_MSG_DATA) |
| 360 | LOGD("DATA : %d", pack->data_len); |
| 361 | else |
| 362 | log_hex("PACK", adb_buff_ptr, MBTK_ADB_PACK_HEAD_SIZE + pack->data_len); |
| 363 | |
| 364 | // Jump 'flag' |
| 365 | adb_buff_ptr += MBTK_ADB_PACK_HEAD_SIZE; |
| 366 | adb_buff_size -= MBTK_ADB_PACK_HEAD_SIZE; |
| 367 | |
| 368 | if(pack->data_len > 0) { |
| 369 | pack->data = adb_buff_ptr; |
| 370 | |
| 371 | // Jump 'data' |
| 372 | adb_buff_ptr += pack->data_len; |
| 373 | adb_buff_size -= pack->data_len; |
| 374 | } |
| 375 | |
| 376 | return MBTK_ADB_MSG_ERR_SUCCESS; |
| 377 | error: |
| 378 | return MBTK_ADB_MSG_ERR_PACK; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | int main(int argc, char *argv[]) |
| 383 | { |
| 384 | char port_config[32] = {0}; |
| 385 | mbtk_log_init("radio", "MBTK_ADBD"); |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 386 | |
b.liu | bcf86c9 | 2024-08-19 19:48:28 +0800 | [diff] [blame^] | 387 | MBTK_SOURCE_INFO_PRINT("mbtk_adbd"); |
| 388 | |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 389 | #ifdef MBTK_DUMP_SUPPORT |
| 390 | mbtk_debug_open(NULL, TRUE); |
| 391 | #endif |
| 392 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 393 | memset(port_config, 0, 32); |
| 394 | property_get("persist.mbtk.dev_ttyGS0", port_config, "at"); |
| 395 | if(strcmp(port_config, "adb")) { |
| 396 | memset(port_config, 0, 32); |
| 397 | property_get("persist.mbtk.dev_ttymodem0", port_config, "at"); |
| 398 | if(strcmp(port_config, "adb")) { |
| 399 | memset(port_config, 0, 32); |
| 400 | property_get("persist.mbtk.dev_ttyS1", port_config, "at"); |
| 401 | if(strcmp(port_config, "adb")) { |
| 402 | LOGE("No config mbtk adb port."); |
| 403 | return 0; |
| 404 | } else { |
| 405 | memset(port_config, 0, 32); |
| 406 | strcpy(port_config, "/dev/ttyS1"); |
| 407 | } |
| 408 | } else { |
| 409 | memset(port_config, 0, 32); |
| 410 | strcpy(port_config, "/dev/ttymodem0"); |
| 411 | } |
| 412 | } else { |
| 413 | memset(port_config, 0, 32); |
| 414 | strcpy(port_config, "/dev/ttyGS0"); |
| 415 | } |
| 416 | |
| 417 | LOGD("Port : %s", port_config); |
| 418 | |
| 419 | if(access(port_config, R_OK | W_OK)) { |
| 420 | LOGE("Dev %s error:%d", port_config, errno); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | if((adb_fd = adb_port_open(port_config, B921600)) < 0) { |
| 425 | LOGE("Open dev %s fail:%d", port_config, errno); |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | #if MBTK_ADB_MSG_SYNC |
| 430 | |
| 431 | mbtk_adb_pack_t pack; |
| 432 | mbtk_adb_msg_err_enum err; |
| 433 | while(1) { |
| 434 | memset(adb_buff, 0x0, ADB_BUFF_SIZE); |
| 435 | memset(&pack, 0x0, sizeof(mbtk_adb_pack_t)); |
| 436 | adb_buff_size = 0; |
| 437 | adb_buff_ptr = adb_buff; |
| 438 | |
| 439 | LOGD("Start/Restart connectting."); |
| 440 | while(1) { |
| 441 | err = adb_read_a_pack(adb_fd, &pack); |
| 442 | if(MBTK_ADB_MSG_ERR_SUCCESS != err) { |
| 443 | adb_msg_send(adb_fd, err, NULL, 0); |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | err = adb_pack_process(adb_fd, &pack); |
| 448 | if(MBTK_ADB_MSG_ERR_SUCCESS != err) { |
| 449 | adb_msg_send(adb_fd, err, NULL, 0); |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | #else |
| 455 | int select_timeout = 30; // 30s |
| 456 | do |
| 457 | { |
| 458 | struct timeval timeval; |
| 459 | fd_set fdr, fdw; |
| 460 | int ret = 0; |
| 461 | |
| 462 | FD_ZERO(&fdw); |
| 463 | FD_ZERO(&fdr); |
| 464 | FD_SET(adb_fd, &fdr); |
| 465 | |
| 466 | timeval.tv_sec = select_timeout; |
| 467 | timeval.tv_usec = 0; |
| 468 | |
| 469 | ret = select(adb_fd + 1, &fdr, &fdw, 0, &timeval); |
| 470 | if (ret < 0) |
| 471 | { |
| 472 | if (errno == EINTR) |
| 473 | { |
| 474 | continue; |
| 475 | } |
| 476 | LOGE("select error, errno = %d (%s)", errno, strerror(errno)); |
| 477 | break; |
| 478 | } |
| 479 | else if (ret == 0) |
| 480 | { |
| 481 | LOGE("select timeout"); |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | // New AT command recv. |
| 486 | if (FD_ISSET(adb_fd, &fdr)) |
| 487 | { |
| 488 | |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | LOGW("Unknown select event."); |
| 493 | continue; |
| 494 | } |
| 495 | } while(1); |
| 496 | #endif |
| 497 | |
| 498 | LOGD("EXIT!"); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |