1 #include <stdint.h>
2 #include <string.h>
3 #include <aos/kernel.h>
4 #include <cli_console.h>
5 #include <aos/hal/uart.h>
6 
7 int32_t g_cli_direct_read = 0;
8 
9 /* uart_input_read depends on mcu*/
uart_input_read()10 __attribute__((weak)) int uart_input_read()
11 {
12     return 1;
13 }
14 
15 int uart_console_init(void *private_data);
16 int uart_console_deinit(void *private_data);
17 int uart_console_write(const void *buf, size_t len, void *privata_data);
18 int uart_console_read(void *buf, size_t len, void *privata_data);
19 
20 static device_console uart_console = {
21     .name   = "uart-console",
22     .fd     = -1,
23     .write  = uart_console_write,
24     .read   = uart_console_read,
25     .init   = uart_console_init,
26     .deinit = uart_console_deinit
27 };
28 
uart_console_write(const void * buf,size_t len,void * privata_data)29 int uart_console_write(const void *buf, size_t len, void *privata_data)
30 {
31     uart_dev_t uart_stdio;
32     size_t i;
33     const char *tmp = (char *)buf;
34 
35     if (buf == NULL) {
36         return 0;
37     }
38 
39     memset(&uart_stdio, 0, sizeof(uart_stdio));
40     uart_stdio.port = HAL_UART_STDIO_PORT;
41 
42     for (i = 0; i < len; i++) {
43         if (*tmp == '\n') {
44             hal_uart_send(&uart_stdio, (void *)"\r", 1, AOS_WAIT_FOREVER);
45         }
46         hal_uart_send(&uart_stdio, (void *)tmp, 1, AOS_WAIT_FOREVER);
47         tmp++;
48     }
49 
50     return len;
51 }
52 
uart_console_read(void * buf,size_t len,void * privata_data)53 int uart_console_read(void *buf, size_t len, void *privata_data)
54 {
55     int ret = -1;
56 
57     uart_dev_t uart_stdio;
58     char *inbuf = (char *)buf;
59 
60     uint32_t recv_size = 0;
61 	unsigned char ch   = 0;
62 
63     if (buf == NULL) {
64         return 0;
65     }
66     memset(&uart_stdio, 0, sizeof(uart_dev_t));
67     uart_stdio.port = HAL_UART_STDIO_PORT;
68 
69     if ( g_cli_direct_read == 0 ) {
70         ret = hal_uart_recv_II(&uart_stdio, inbuf, 1, &recv_size, HAL_WAIT_FOREVER);
71         if ((ret == 0) && (recv_size == 1)) {
72             return recv_size;
73         } else {
74             return 0;
75         }
76     } else {
77         do {ch = uart_input_read();}while(ch == 0);
78         *inbuf = ch;
79         return 1;
80     }
81 }
82 
uart_console_init(void * private_data)83 int uart_console_init(void *private_data)
84 {
85     return 0;
86 }
87 
uart_console_deinit(void * private_data)88 int uart_console_deinit(void *private_data)
89 {
90     return 0;
91 }
92 
93 cli_console cli_uart_console = {
94     .i_list = {0},
95     .name = "ash",
96     .dev_console = &uart_console,
97     .init_flag = 0,
98     .exit_flag = 0,
99     .alive = 1,
100     .private_data = NULL,
101     .cli_tag = {0},
102     .cli_tag_len = 0,
103     .task_list = {0},
104     .finsh_callback = NULL,
105     .start_callback = NULL,
106 };
107