1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <aos/kernel.h>
4 #include <cli_console.h>
5 #include <aos/cli.h>
6 #include <sys/socket.h>
7 #include <lwip/apps/telnetserver.h>
8 #include <aos/hal/uart.h>
9
10 #define RECV_BUFF_SIZE (1024)
11 typedef struct {
12 uint32_t write_idx;
13 uint32_t read_idx;
14 aos_sem_t telnet_rx_sem;
15 uint8_t data[RECV_BUFF_SIZE];
16 } telnet_buff;
17
18 static telnet_buff *telnet_recv_buf;
19
20
telnet_write_to_buffer(char c)21 int telnet_write_to_buffer(char c)
22 {
23 telnet_recv_buf->data[telnet_recv_buf->write_idx++] = c;
24 if (telnet_recv_buf->write_idx >= RECV_BUFF_SIZE) {
25 telnet_recv_buf->write_idx = 0;
26 }
27 aos_sem_signal(&telnet_recv_buf->telnet_rx_sem);
28 return 1;
29 }
30
telnet_read_from_buffer(char * c)31 int telnet_read_from_buffer(char *c)
32 {
33 if (telnet_recv_buf->read_idx == telnet_recv_buf->write_idx) {
34 return 0;
35 } else {
36 *c = telnet_recv_buf->data[telnet_recv_buf->read_idx++];
37 if (telnet_recv_buf->read_idx >= RECV_BUFF_SIZE) {
38 telnet_recv_buf->read_idx = 0;
39 }
40 }
41 return 1;
42 }
43
telnet_console_write(const void * buf,size_t len,void * privata_data)44 int telnet_console_write(const void *buf, size_t len, void *privata_data)
45 {
46 char *ptr = (char *)buf;
47 if (buf == NULL) {
48 return 0;
49 }
50 ptr[len] = 0;
51 TelnetWriteString((char *)buf);
52
53 return len;
54 }
55
56
telnet_console_read(void * buf,size_t len,void * privata_data)57 int telnet_console_read(void *buf, size_t len, void *privata_data)
58 {
59 char *inbuf = (char *)buf;
60 int ret;
61
62 ret = telnet_read_from_buffer(inbuf);
63 if (ret == 0) {
64 aos_sem_wait(&telnet_recv_buf->telnet_rx_sem, 20);
65 ret = telnet_read_from_buffer(inbuf);
66 }
67 return ret;
68 }
69
telnet_console_init(void * private_data)70 static int telnet_console_init(void *private_data)
71 {
72 telnet_recv_buf = aos_zalloc(sizeof(telnet_buff));
73 aos_sem_new(&telnet_recv_buf->telnet_rx_sem, 0);
74 return 1;
75 }
76
telnet_console_deinit(void * private_data)77 static int telnet_console_deinit(void *private_data)
78 {
79 if (telnet_recv_buf) {
80 aos_sem_free(&telnet_recv_buf->telnet_rx_sem);
81 aos_free(telnet_recv_buf);
82 telnet_recv_buf = NULL;
83 }
84 return 1;
85 }
86
87 static device_console telnet_console = {
88 .name = "telnet-console",
89 .write = telnet_console_write,
90 .read = telnet_console_read,
91 .init = telnet_console_init,
92 .deinit = telnet_console_deinit
93 };
94
95 cli_console cli_telnet_console = {
96 .i_list = {0},
97 .name = "cli-telnet",
98 .dev_console = &telnet_console,
99 .init_flag = 0,
100 .exit_flag = 0,
101 .alive = 1,
102 .private_data = NULL,
103 .cli_tag = {0},
104 .cli_tag_len = 0,
105 .task_list = {0},
106 .finsh_callback = NULL,
107 .start_callback = NULL,
108 };
109
telnet_console_create(int argc,char ** argv)110 int32_t telnet_console_create(int argc, char **argv)
111 {
112 int32_t ret;
113 ret = cli_console_task_create(&cli_telnet_console, CLI_CONFIG_STACK_SIZE, CLI_TASK_PRIORITY);
114 if (ret != 0) {
115 printf("Error: Failed to create telnet cli thread: %d\r\n", ret);
116 return -1;
117 }
118 printf("success to create telnet cli thread: %d\r\n", ret);
119 return 1;
120 }
121
telnet_console_destory(void)122 int telnet_console_destory(void)
123 {
124 cli_console_task_destory(&cli_telnet_console);
125 printf("telnet_console_destory\r\n");
126 return 0;
127 }
128
129 ALIOS_CLI_CMD_REGISTER(telnet_console_create, telnetcli, Console Telnet Create)
130