1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdarg.h>
9 #include <string.h>
10
11 #include "amp_config.h"
12 #include "aos_system.h"
13 #include "amp_task.h"
14 #include "amp_defines.h"
15 #include "aos_hal_uart.h"
16 #include "quickjs.h"
17 #include "repl.h"
18
19 #ifdef AOS_COMP_CLI
20 #include "aos/cli.h"
21 #endif
22
23 #define MOD_STR "REPL"
24
25 int g_repl_config = 0;
26 static uart_dev_t g_repl_uart;
27 static char g_repl_tag[64] = {0};
28 static uint8_t g_repl_tag_len = 0;
29 static char repl_message[REPL_OUTBUF_SIZE] = {0};
30
aos_repl_read(char * inbuf,uint32_t expected_length,uint32_t * recv_size)31 int aos_repl_read(char *inbuf, uint32_t expected_length, uint32_t *recv_size)
32 {
33 int32_t ret = REPL_OK;
34
35 ret = aos_hal_uart_recv_II(&g_repl_uart, inbuf, expected_length, recv_size, 100);
36 if (ret == 0) {
37 return *recv_size;
38 } else {
39 return 0;
40 }
41 }
42
repl_putstr(char * msg)43 static int32_t repl_putstr(char *msg)
44 {
45 if (msg[0] != 0)
46 {
47 aos_hal_uart_send(&g_repl_uart, (void *)msg, strlen(msg), 0xFFFFFFFFU);
48 }
49
50 return REPL_OK;
51 }
52
repl_printf(const char * buffer,...)53 int32_t repl_printf(const char *buffer, ...)
54 {
55 va_list ap;
56
57 int32_t sz, len;
58
59 char *pos = NULL;
60
61 memset(repl_message, 0, REPL_OUTBUF_SIZE);
62
63 sz = 0;
64 if (g_repl_tag_len > 0)
65 {
66 len = strlen(g_repl_tag);
67 strncpy(repl_message, g_repl_tag, len);
68 sz = len;
69 }
70
71 pos = repl_message + sz;
72
73 va_start(ap, buffer);
74 len = vsnprintf(pos, REPL_OUTBUF_SIZE - sz, buffer, ap);
75 va_end(ap);
76 if (len <= 0)
77 {
78 return REPL_OK;
79 }
80
81 len = strlen(repl_message);
82
83 repl_putstr(repl_message);
84 if(repl_message[len-1] == '\n') {
85 repl_putstr("\r");
86 }
87
88 return REPL_OK;
89 }
90
aos_repl_write(char * str)91 int aos_repl_write(char *str)
92 {
93 repl_printf("%s", str);
94 return 0;
95 }
96
repl_init(void)97 static int32_t repl_init(void)
98 {
99 g_repl_uart.port = AMP_REPL_UART_PORT;
100 g_repl_uart.config.baud_rate = AMP_REPL_UART_BAUDRATE;
101 g_repl_uart.config.data_width = DATA_WIDTH_8BIT;
102 g_repl_uart.config.flow_control = FLOW_CONTROL_DISABLED;
103 g_repl_uart.config.mode = MODE_TX_RX;
104 g_repl_uart.config.parity = NO_PARITY;
105 g_repl_uart.config.stop_bits = STOP_BITS_1;
106 aos_hal_uart_init(&g_repl_uart);
107
108 repl_read_task_start();
109
110 amp_debug(MOD_STR, "REPL Enabled\r\n");
111 return 0;
112 }
113
aos_repl_init(void * arg)114 void aos_repl_init(void *arg)
115 {
116 #ifdef AOS_COMP_CLI
117 repl_printf("cli suspend\n");
118 aos_cli_suspend();
119 repl_printf("repl init\r\n");
120 #endif
121 repl_init();
122
123 return ;
124 }
125
aos_repl_close()126 int32_t aos_repl_close()
127 {
128 #ifdef AOS_COMP_CLI
129 repl_printf("cli resume\n");
130 aos_cli_resume();
131 #endif
132 return 0;
133 }
134
135 #ifdef AOS_COMP_CLI
jsrepl_startup()136 void jsrepl_startup()
137 {
138 aos_task_t repl_task;
139 aos_task_new_ext(&repl_task, "amp init task", aos_repl_init, NULL, 1024 * 4, AOS_DEFAULT_APP_PRI);
140 }
141 /* reg args: fun, cmd, description*/
142 ALIOS_CLI_CMD_REGISTER(jsrepl_startup, jsrepl, "start js amp repl")
143 #endif
144