1 /*
2 * Copyright (c) 2011-2012, 2014-2015 Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief UART-driven console
10 *
11 *
12 * Serial console driver.
13 * Hooks into the printk and fputc (for printf) modules. Poll driven.
14 */
15
16 #include <kernel.h>
17
18 #include <stdio.h>
19 #include <zephyr/types.h>
20 #include <sys/__assert.h>
21 #include <errno.h>
22 #include <ctype.h>
23
24 #include <device.h>
25 #include <init.h>
26
27 #include <drivers/uart.h>
28 #include <drivers/console/console.h>
29 #include <drivers/console/uart_console.h>
30 #include <toolchain.h>
31 #include <linker/sections.h>
32 #include <sys/atomic.h>
33 #include <sys/printk.h>
34 #ifdef CONFIG_UART_CONSOLE_MCUMGR
35 #include "mgmt/mcumgr/serial.h"
36 #endif
37
38 static const struct device *uart_console_dev;
39
40 #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
41
42 static uart_console_in_debug_hook_t debug_hook_in;
uart_console_in_debug_hook_install(uart_console_in_debug_hook_t hook)43 void uart_console_in_debug_hook_install(uart_console_in_debug_hook_t hook)
44 {
45 debug_hook_in = hook;
46 }
47
UART_CONSOLE_OUT_DEBUG_HOOK_SIG(debug_hook_out_nop)48 static UART_CONSOLE_OUT_DEBUG_HOOK_SIG(debug_hook_out_nop) {
49 ARG_UNUSED(c);
50 return !UART_CONSOLE_DEBUG_HOOK_HANDLED;
51 }
52
53 static uart_console_out_debug_hook_t *debug_hook_out = debug_hook_out_nop;
uart_console_out_debug_hook_install(uart_console_out_debug_hook_t * hook)54 void uart_console_out_debug_hook_install(uart_console_out_debug_hook_t *hook)
55 {
56 debug_hook_out = hook;
57 }
58 #define HANDLE_DEBUG_HOOK_OUT(c) \
59 (debug_hook_out(c) == UART_CONSOLE_DEBUG_HOOK_HANDLED)
60
61 #endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */
62
63
64 #if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
65 /**
66 *
67 * @brief Output one character to UART
68 *
69 * Outputs both line feed and carriage return in the case of a '\n'.
70 *
71 * @param c Character to output
72 *
73 * @return The character passed as input.
74 */
75
console_out(int c)76 static int console_out(int c)
77 {
78 #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
79
80 int handled_by_debug_server = HANDLE_DEBUG_HOOK_OUT(c);
81
82 if (handled_by_debug_server) {
83 return c;
84 }
85
86 #endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */
87
88 if ('\n' == c) {
89 uart_poll_out(uart_console_dev, '\r');
90 }
91 uart_poll_out(uart_console_dev, c);
92
93 return c;
94 }
95
96 #endif
97
98 #if defined(CONFIG_STDOUT_CONSOLE)
99 extern void __stdout_hook_install(int (*hook)(int));
100 #else
101 #define __stdout_hook_install(x) \
102 do { /* nothing */ \
103 } while ((0))
104 #endif
105
106 #if defined(CONFIG_PRINTK)
107 extern void __printk_hook_install(int (*fn)(int));
108 #else
109 #define __printk_hook_install(x) \
110 do { /* nothing */ \
111 } while ((0))
112 #endif
113
114 #if defined(CONFIG_CONSOLE_HANDLER)
115 static struct k_fifo *avail_queue;
116 static struct k_fifo *lines_queue;
117 static uint8_t (*completion_cb)(char *line, uint8_t len);
118
119 /* Control characters */
120 #define BS 0x08
121 #define ESC 0x1b
122 #define DEL 0x7f
123
124 /* ANSI escape sequences */
125 #define ANSI_ESC '['
126 #define ANSI_UP 'A'
127 #define ANSI_DOWN 'B'
128 #define ANSI_FORWARD 'C'
129 #define ANSI_BACKWARD 'D'
130 #define ANSI_END 'F'
131 #define ANSI_HOME 'H'
132 #define ANSI_DEL '~'
133
read_uart(const struct device * uart,uint8_t * buf,unsigned int size)134 static int read_uart(const struct device *uart, uint8_t *buf,
135 unsigned int size)
136 {
137 int rx;
138
139 rx = uart_fifo_read(uart, buf, size);
140 if (rx < 0) {
141 /* Overrun issue. Stop the UART */
142 uart_irq_rx_disable(uart);
143
144 return -EIO;
145 }
146
147 return rx;
148 }
149
cursor_forward(unsigned int count)150 static inline void cursor_forward(unsigned int count)
151 {
152 printk("\x1b[%uC", count);
153 }
154
cursor_backward(unsigned int count)155 static inline void cursor_backward(unsigned int count)
156 {
157 printk("\x1b[%uD", count);
158 }
159
cursor_save(void)160 static inline void cursor_save(void)
161 {
162 printk("\x1b[s");
163 }
164
cursor_restore(void)165 static inline void cursor_restore(void)
166 {
167 printk("\x1b[u");
168 }
169
insert_char(char * pos,char c,uint8_t end)170 static void insert_char(char *pos, char c, uint8_t end)
171 {
172 char tmp;
173
174 /* Echo back to console */
175 uart_poll_out(uart_console_dev, c);
176
177 if (end == 0U) {
178 *pos = c;
179 return;
180 }
181
182 tmp = *pos;
183 *(pos++) = c;
184
185 cursor_save();
186
187 while (end-- > 0) {
188 uart_poll_out(uart_console_dev, tmp);
189 c = *pos;
190 *(pos++) = tmp;
191 tmp = c;
192 }
193
194 /* Move cursor back to right place */
195 cursor_restore();
196 }
197
del_char(char * pos,uint8_t end)198 static void del_char(char *pos, uint8_t end)
199 {
200 uart_poll_out(uart_console_dev, '\b');
201
202 if (end == 0U) {
203 uart_poll_out(uart_console_dev, ' ');
204 uart_poll_out(uart_console_dev, '\b');
205 return;
206 }
207
208 cursor_save();
209
210 while (end-- > 0) {
211 *pos = *(pos + 1);
212 uart_poll_out(uart_console_dev, *(pos++));
213 }
214
215 uart_poll_out(uart_console_dev, ' ');
216
217 /* Move cursor back to right place */
218 cursor_restore();
219 }
220
221 enum {
222 ESC_ESC,
223 ESC_ANSI,
224 ESC_ANSI_FIRST,
225 ESC_ANSI_VAL,
226 ESC_ANSI_VAL_2,
227 #ifdef CONFIG_UART_CONSOLE_MCUMGR
228 ESC_MCUMGR_PKT_1,
229 ESC_MCUMGR_PKT_2,
230 ESC_MCUMGR_FRAG_1,
231 ESC_MCUMGR_FRAG_2,
232 #endif
233 };
234
235 static atomic_t esc_state;
236 static unsigned int ansi_val, ansi_val_2;
237 static uint8_t cur, end;
238
handle_ansi(uint8_t byte,char * line)239 static void handle_ansi(uint8_t byte, char *line)
240 {
241 if (atomic_test_and_clear_bit(&esc_state, ESC_ANSI_FIRST)) {
242 if (!isdigit(byte)) {
243 ansi_val = 1U;
244 goto ansi_cmd;
245 }
246
247 atomic_set_bit(&esc_state, ESC_ANSI_VAL);
248 ansi_val = byte - '0';
249 ansi_val_2 = 0U;
250 return;
251 }
252
253 if (atomic_test_bit(&esc_state, ESC_ANSI_VAL)) {
254 if (isdigit(byte)) {
255 if (atomic_test_bit(&esc_state, ESC_ANSI_VAL_2)) {
256 ansi_val_2 *= 10U;
257 ansi_val_2 += byte - '0';
258 } else {
259 ansi_val *= 10U;
260 ansi_val += byte - '0';
261 }
262 return;
263 }
264
265 /* Multi value sequence, e.g. Esc[Line;ColumnH */
266 if (byte == ';' &&
267 !atomic_test_and_set_bit(&esc_state, ESC_ANSI_VAL_2)) {
268 return;
269 }
270
271 atomic_clear_bit(&esc_state, ESC_ANSI_VAL);
272 atomic_clear_bit(&esc_state, ESC_ANSI_VAL_2);
273 }
274
275 ansi_cmd:
276 switch (byte) {
277 case ANSI_BACKWARD:
278 if (ansi_val > cur) {
279 break;
280 }
281
282 end += ansi_val;
283 cur -= ansi_val;
284 cursor_backward(ansi_val);
285 break;
286 case ANSI_FORWARD:
287 if (ansi_val > end) {
288 break;
289 }
290
291 end -= ansi_val;
292 cur += ansi_val;
293 cursor_forward(ansi_val);
294 break;
295 case ANSI_HOME:
296 if (!cur) {
297 break;
298 }
299
300 cursor_backward(cur);
301 end += cur;
302 cur = 0U;
303 break;
304 case ANSI_END:
305 if (!end) {
306 break;
307 }
308
309 cursor_forward(end);
310 cur += end;
311 end = 0U;
312 break;
313 case ANSI_DEL:
314 if (!end) {
315 break;
316 }
317
318 cursor_forward(1);
319 del_char(&line[cur], --end);
320 break;
321 default:
322 break;
323 }
324
325 atomic_clear_bit(&esc_state, ESC_ANSI);
326 }
327
328 #ifdef CONFIG_UART_CONSOLE_MCUMGR
329
clear_mcumgr(void)330 static void clear_mcumgr(void)
331 {
332 atomic_clear_bit(&esc_state, ESC_MCUMGR_PKT_1);
333 atomic_clear_bit(&esc_state, ESC_MCUMGR_PKT_2);
334 atomic_clear_bit(&esc_state, ESC_MCUMGR_FRAG_1);
335 atomic_clear_bit(&esc_state, ESC_MCUMGR_FRAG_2);
336 }
337
338 /**
339 * These states indicate whether an mcumgr frame is being received.
340 */
341 #define CONSOLE_MCUMGR_STATE_NONE 1
342 #define CONSOLE_MCUMGR_STATE_HEADER 2
343 #define CONSOLE_MCUMGR_STATE_PAYLOAD 3
344
read_mcumgr_byte(uint8_t byte)345 static int read_mcumgr_byte(uint8_t byte)
346 {
347 bool frag_1;
348 bool frag_2;
349 bool pkt_1;
350 bool pkt_2;
351
352 pkt_1 = atomic_test_bit(&esc_state, ESC_MCUMGR_PKT_1);
353 pkt_2 = atomic_test_bit(&esc_state, ESC_MCUMGR_PKT_2);
354 frag_1 = atomic_test_bit(&esc_state, ESC_MCUMGR_FRAG_1);
355 frag_2 = atomic_test_bit(&esc_state, ESC_MCUMGR_FRAG_2);
356
357 if (pkt_2 || frag_2) {
358 /* Already fully framed. */
359 return CONSOLE_MCUMGR_STATE_PAYLOAD;
360 }
361
362 if (pkt_1) {
363 if (byte == MCUMGR_SERIAL_HDR_PKT_2) {
364 /* Final framing byte received. */
365 atomic_set_bit(&esc_state, ESC_MCUMGR_PKT_2);
366 return CONSOLE_MCUMGR_STATE_PAYLOAD;
367 }
368 } else if (frag_1) {
369 if (byte == MCUMGR_SERIAL_HDR_FRAG_2) {
370 /* Final framing byte received. */
371 atomic_set_bit(&esc_state, ESC_MCUMGR_FRAG_2);
372 return CONSOLE_MCUMGR_STATE_PAYLOAD;
373 }
374 } else {
375 if (byte == MCUMGR_SERIAL_HDR_PKT_1) {
376 /* First framing byte received. */
377 atomic_set_bit(&esc_state, ESC_MCUMGR_PKT_1);
378 return CONSOLE_MCUMGR_STATE_HEADER;
379 } else if (byte == MCUMGR_SERIAL_HDR_FRAG_1) {
380 /* First framing byte received. */
381 atomic_set_bit(&esc_state, ESC_MCUMGR_FRAG_1);
382 return CONSOLE_MCUMGR_STATE_HEADER;
383 }
384 }
385
386 /* Non-mcumgr byte received. */
387 return CONSOLE_MCUMGR_STATE_NONE;
388 }
389
390 /**
391 * @brief Attempts to process a received byte as part of an mcumgr frame.
392 *
393 * @param cmd The console command currently being received.
394 * @param byte The byte just received.
395 *
396 * @return true if the command being received is an mcumgr frame; false if it
397 * is a plain console command.
398 */
handle_mcumgr(struct console_input * cmd,uint8_t byte)399 static bool handle_mcumgr(struct console_input *cmd, uint8_t byte)
400 {
401 int mcumgr_state;
402
403 mcumgr_state = read_mcumgr_byte(byte);
404 if (mcumgr_state == CONSOLE_MCUMGR_STATE_NONE) {
405 /* Not an mcumgr command; let the normal console handling
406 * process the byte.
407 */
408 cmd->is_mcumgr = 0;
409 return false;
410 }
411
412 /* The received byte is part of an mcumgr command. Process the byte
413 * and return true to indicate that normal console handling should
414 * ignore it.
415 */
416 if (cur + end < sizeof(cmd->line) - 1) {
417 cmd->line[cur++] = byte;
418 }
419 if (mcumgr_state == CONSOLE_MCUMGR_STATE_PAYLOAD && byte == '\n') {
420 cmd->line[cur + end] = '\0';
421 cmd->is_mcumgr = 1;
422 k_fifo_put(lines_queue, cmd);
423
424 clear_mcumgr();
425 cmd = NULL;
426 cur = 0U;
427 end = 0U;
428 }
429
430 return true;
431 }
432
433 #endif /* CONFIG_UART_CONSOLE_MCUMGR */
434
uart_console_isr(const struct device * unused,void * user_data)435 static void uart_console_isr(const struct device *unused, void *user_data)
436 {
437 ARG_UNUSED(unused);
438 ARG_UNUSED(user_data);
439
440 while (uart_irq_update(uart_console_dev) &&
441 uart_irq_is_pending(uart_console_dev)) {
442 static struct console_input *cmd;
443 uint8_t byte;
444 int rx;
445
446 if (!uart_irq_rx_ready(uart_console_dev)) {
447 continue;
448 }
449
450 /* Character(s) have been received */
451
452 rx = read_uart(uart_console_dev, &byte, 1);
453 if (rx < 0) {
454 return;
455 }
456
457 #ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
458 if (debug_hook_in != NULL && debug_hook_in(byte) != 0) {
459 /*
460 * The input hook indicates that no further processing
461 * should be done by this handler.
462 */
463 return;
464 }
465 #endif
466
467 if (!cmd) {
468 cmd = k_fifo_get(avail_queue, K_NO_WAIT);
469 if (!cmd) {
470 return;
471 }
472 }
473
474 #ifdef CONFIG_UART_CONSOLE_MCUMGR
475 /* Divert this byte from normal console handling if it is part
476 * of an mcumgr frame.
477 */
478 if (handle_mcumgr(cmd, byte)) {
479 continue;
480 }
481 #endif /* CONFIG_UART_CONSOLE_MCUMGR */
482
483 /* Handle ANSI escape mode */
484 if (atomic_test_bit(&esc_state, ESC_ANSI)) {
485 handle_ansi(byte, cmd->line);
486 continue;
487 }
488
489 /* Handle escape mode */
490 if (atomic_test_and_clear_bit(&esc_state, ESC_ESC)) {
491 if (byte == ANSI_ESC) {
492 atomic_set_bit(&esc_state, ESC_ANSI);
493 atomic_set_bit(&esc_state, ESC_ANSI_FIRST);
494 }
495
496 continue;
497 }
498
499 /* Handle special control characters */
500 if (!isprint(byte)) {
501 switch (byte) {
502 case BS:
503 case DEL:
504 if (cur > 0) {
505 del_char(&cmd->line[--cur], end);
506 }
507 break;
508 case ESC:
509 atomic_set_bit(&esc_state, ESC_ESC);
510 break;
511 case '\r':
512 cmd->line[cur + end] = '\0';
513 uart_poll_out(uart_console_dev, '\r');
514 uart_poll_out(uart_console_dev, '\n');
515 cur = 0U;
516 end = 0U;
517 k_fifo_put(lines_queue, cmd);
518 cmd = NULL;
519 break;
520 case '\t':
521 if (completion_cb && !end) {
522 cur += completion_cb(cmd->line, cur);
523 }
524 break;
525 default:
526 break;
527 }
528
529 continue;
530 }
531
532 /* Ignore characters if there's no more buffer space */
533 if (cur + end < sizeof(cmd->line) - 1) {
534 insert_char(&cmd->line[cur++], byte, end);
535 }
536 }
537 }
538
console_input_init(void)539 static void console_input_init(void)
540 {
541 uint8_t c;
542
543 uart_irq_rx_disable(uart_console_dev);
544 uart_irq_tx_disable(uart_console_dev);
545
546 uart_irq_callback_set(uart_console_dev, uart_console_isr);
547
548 /* Drain the fifo */
549 while (uart_irq_rx_ready(uart_console_dev)) {
550 uart_fifo_read(uart_console_dev, &c, 1);
551 }
552
553 uart_irq_rx_enable(uart_console_dev);
554 }
555
uart_register_input(struct k_fifo * avail,struct k_fifo * lines,uint8_t (* completion)(char * str,uint8_t len))556 void uart_register_input(struct k_fifo *avail, struct k_fifo *lines,
557 uint8_t (*completion)(char *str, uint8_t len))
558 {
559 avail_queue = avail;
560 lines_queue = lines;
561 completion_cb = completion;
562
563 console_input_init();
564 }
565
566 #else
567 #define console_input_init(x) \
568 do { /* nothing */ \
569 } while ((0))
570 #define uart_register_input(x) \
571 do { /* nothing */ \
572 } while ((0))
573 #endif
574
575 /**
576 *
577 * @brief Install printk/stdout hook for UART console output
578 *
579 * @return N/A
580 */
581
uart_console_hook_install(void)582 static void uart_console_hook_install(void)
583 {
584 __stdout_hook_install(console_out);
585 __printk_hook_install(console_out);
586 }
587
588 /**
589 *
590 * @brief Initialize one UART as the console/debug port
591 *
592 * @return 0 if successful, otherwise failed.
593 */
uart_console_init(const struct device * arg)594 static int uart_console_init(const struct device *arg)
595 {
596
597 ARG_UNUSED(arg);
598
599 /* Claim console device */
600 uart_console_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
601 if (!device_is_ready(uart_console_dev)) {
602 return -ENODEV;
603 }
604
605 uart_console_hook_install();
606
607 return 0;
608 }
609
610 /* UART console initializes after the UART device itself */
611 SYS_INIT(uart_console_init,
612 #if defined(CONFIG_USB_UART_CONSOLE)
613 APPLICATION,
614 #elif defined(CONFIG_EARLY_CONSOLE)
615 PRE_KERNEL_1,
616 #else
617 POST_KERNEL,
618 #endif
619 CONFIG_UART_CONSOLE_INIT_PRIORITY);
620