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