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