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