blob: bdc6866b2301bb726a8afd412af6231b4b5c7456 [file] [log] [blame]
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07001/*
2 * Copyright (c) 2011-2012, 2014-2015 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -07005 */
6
Anas Nashif275ca602015-12-04 10:09:39 -05007/**
8 * @file
9 * @brief UART-driven console
10 *
Dan Kalowskyda67b292015-10-20 09:42:33 -070011 *
12 * Serial console driver.
13 * Hooks into the printk and fputc (for printf) modules. Poll driven.
Anas Nashifea0d0b22015-07-01 17:22:39 -040014 */
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070015
Baohong Liufa2d38e2016-11-30 14:58:45 -080016#include <kernel.h>
Dan Kalowskyc02dd342015-05-28 10:56:47 -070017#include <arch/cpu.h>
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030018
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070019#include <stdio.h>
Kumar Gala78908162017-04-19 10:32:08 -050020#include <zephyr/types.h>
Andrei Emeltchenko139c8562015-04-20 11:04:22 +030021#include <errno.h>
Johan Hedbergf88cccd2015-12-08 10:35:04 +020022#include <ctype.h>
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070023
Daniel Leungad2d2962015-08-12 10:17:35 -070024#include <device.h>
25#include <init.h>
26
Tomasz Bursztyka17e06fb2015-10-15 09:48:03 +030027#include <uart.h>
Tomasz Bursztyka2f1af492017-01-24 10:08:08 +010028#include <console/console.h>
Tomasz Bursztykaae09a482015-04-23 13:12:51 +030029#include <console/uart_console.h>
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070030#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040031#include <linker/sections.h>
Johan Hedberg8683dc42015-12-09 11:36:25 +020032#include <atomic.h>
Johan Hedberga9f6f892015-12-08 22:45:12 +020033#include <misc/printk.h>
Christopher Collins14735112018-01-17 18:01:52 -080034#ifdef CONFIG_UART_CONSOLE_MCUMGR
35#include "mgmt/serial.h"
36#endif
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070037
Daniel Leung08b4fd42015-12-01 08:42:20 -080038static struct device *uart_console_dev;
39
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040040#ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
Marcus Shawcroft1bc999c2016-10-23 08:53:21 +010041
42static uart_console_in_debug_hook_t debug_hook_in;
43void uart_console_in_debug_hook_install(uart_console_in_debug_hook_t hook)
44{
45 debug_hook_in = hook;
46}
47
Christopher Collins14735112018-01-17 18:01:52 -080048static UART_CONSOLE_OUT_DEBUG_HOOK_SIG(debug_hook_out_nop) {
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040049 ARG_UNUSED(c);
50 return !UART_CONSOLE_DEBUG_HOOK_HANDLED;
51}
52
53static uart_console_out_debug_hook_t *debug_hook_out = debug_hook_out_nop;
54void 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)
Vincenzo Frascinof3a92412016-11-16 12:01:48 +000060
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040061#endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */
62
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070063#if 0 /* NOTUSED */
Anas Nashifea0d0b22015-07-01 17:22:39 -040064/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070065 *
Anas Nashiff367f072015-07-01 17:51:40 -040066 * @brief Get a character from UART
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070067 *
Anas Nashif1362e3c2015-07-01 17:29:04 -040068 * @return the character or EOF if nothing present
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070069 */
70
Daniel Leung08b4fd42015-12-01 08:42:20 -080071static int console_in(void)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070072{
73 unsigned char c;
Dan Kalowskyda67b292015-10-20 09:42:33 -070074
Tomasz Bursztykafb981092017-01-24 09:06:08 +010075 if (uart_poll_in(uart_console_dev, &c) < 0) {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070076 return EOF;
Tomasz Bursztykafb981092017-01-24 09:06:08 +010077 } else {
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070078 return (int)c;
Tomasz Bursztykafb981092017-01-24 09:06:08 +010079 }
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070080}
81#endif
82
83#if defined(CONFIG_PRINTK) || defined(CONFIG_STDOUT_CONSOLE)
Anas Nashifea0d0b22015-07-01 17:22:39 -040084/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070085 *
Anas Nashiff367f072015-07-01 17:51:40 -040086 * @brief Output one character to UART
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070087 *
88 * Outputs both line feed and carriage return in the case of a '\n'.
89 *
Daniel Leung08b4fd42015-12-01 08:42:20 -080090 * @param c Character to output
91 *
Anas Nashif1362e3c2015-07-01 17:29:04 -040092 * @return The character passed as input.
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070093 */
94
Daniel Leung08b4fd42015-12-01 08:42:20 -080095static int console_out(int c)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -070096{
Vincenzo Frascinof3a92412016-11-16 12:01:48 +000097#ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
98
Benjamin Walsh0ff5d372016-04-11 17:11:28 -040099 int handled_by_debug_server = HANDLE_DEBUG_HOOK_OUT(c);
100
101 if (handled_by_debug_server) {
102 return c;
103 }
104
Christopher Collins14735112018-01-17 18:01:52 -0800105#endif /* CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS */
Vincenzo Frascinof3a92412016-11-16 12:01:48 +0000106
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700107 if ('\n' == c) {
Andy Ross425145d2016-09-01 08:49:10 -0700108 uart_poll_out(uart_console_dev, '\r');
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700109 }
Andy Ross425145d2016-09-01 08:49:10 -0700110 uart_poll_out(uart_console_dev, c);
111
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700112 return c;
113}
Daniel Leung08b4fd42015-12-01 08:42:20 -0800114
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700115#endif
116
117#if defined(CONFIG_STDOUT_CONSOLE)
118extern void __stdout_hook_install(int (*hook)(int));
119#else
Christopher Collins14735112018-01-17 18:01:52 -0800120#define __stdout_hook_install(x) \
121 do { /* nothing */ \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700122 } while ((0))
123#endif
124
125#if defined(CONFIG_PRINTK)
126extern void __printk_hook_install(int (*fn)(int));
127#else
Christopher Collins14735112018-01-17 18:01:52 -0800128#define __printk_hook_install(x) \
129 do { /* nothing */ \
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700130 } while ((0))
131#endif
132
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300133#if defined(CONFIG_CONSOLE_HANDLER)
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200134static struct k_fifo *avail_queue;
135static struct k_fifo *lines_queue;
Kumar Galaccad5bf2017-04-21 10:03:20 -0500136static u8_t (*completion_cb)(char *line, u8_t len);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300137
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200138/* Control characters */
qianfan Zhao13e1c3c2018-05-29 12:43:16 +0800139#define BS 0x08
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200140#define ESC 0x1b
141#define DEL 0x7f
142
143/* ANSI escape sequences */
144#define ANSI_ESC '['
145#define ANSI_UP 'A'
146#define ANSI_DOWN 'B'
147#define ANSI_FORWARD 'C'
148#define ANSI_BACKWARD 'D'
Szymon Janc6b0cf542016-10-26 18:10:50 +0200149#define ANSI_END 'F'
150#define ANSI_HOME 'H'
Szymon Janca0373162016-10-26 18:15:02 +0200151#define ANSI_DEL '~'
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200152
Kumar Galaccad5bf2017-04-21 10:03:20 -0500153static int read_uart(struct device *uart, u8_t *buf, unsigned int size)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300154{
155 int rx;
156
157 rx = uart_fifo_read(uart, buf, size);
158 if (rx < 0) {
159 /* Overrun issue. Stop the UART */
160 uart_irq_rx_disable(uart);
161
162 return -EIO;
163 }
164
165 return rx;
166}
167
Johan Hedberga9f6f892015-12-08 22:45:12 +0200168static inline void cursor_forward(unsigned int count)
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200169{
Johan Hedberga9f6f892015-12-08 22:45:12 +0200170 printk("\x1b[%uC", count);
171}
172
173static inline void cursor_backward(unsigned int count)
174{
175 printk("\x1b[%uD", count);
176}
177
Johan Hedbergceba31a2015-12-09 12:40:29 +0200178static inline void cursor_save(void)
179{
180 printk("\x1b[s");
181}
182
183static inline void cursor_restore(void)
184{
185 printk("\x1b[u");
186}
187
Kumar Galaccad5bf2017-04-21 10:03:20 -0500188static void insert_char(char *pos, char c, u8_t end)
Johan Hedberga9f6f892015-12-08 22:45:12 +0200189{
Johan Hedberga9f6f892015-12-08 22:45:12 +0200190 char tmp;
191
192 /* Echo back to console */
193 uart_poll_out(uart_console_dev, c);
194
195 if (end == 0) {
196 *pos = c;
197 return;
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200198 }
Johan Hedberga9f6f892015-12-08 22:45:12 +0200199
200 tmp = *pos;
201 *(pos++) = c;
202
Johan Hedbergceba31a2015-12-09 12:40:29 +0200203 cursor_save();
204
205 while (end-- > 0) {
Johan Hedberga9f6f892015-12-08 22:45:12 +0200206 uart_poll_out(uart_console_dev, tmp);
Johan Hedbergceba31a2015-12-09 12:40:29 +0200207 c = *pos;
208 *(pos++) = tmp;
Johan Hedberga9f6f892015-12-08 22:45:12 +0200209 tmp = c;
210 }
211
212 /* Move cursor back to right place */
Johan Hedbergceba31a2015-12-09 12:40:29 +0200213 cursor_restore();
Johan Hedberga9f6f892015-12-08 22:45:12 +0200214}
215
Kumar Galaccad5bf2017-04-21 10:03:20 -0500216static void del_char(char *pos, u8_t end)
Johan Hedberga9f6f892015-12-08 22:45:12 +0200217{
Johan Hedberga9f6f892015-12-08 22:45:12 +0200218 uart_poll_out(uart_console_dev, '\b');
219
220 if (end == 0) {
221 uart_poll_out(uart_console_dev, ' ');
222 uart_poll_out(uart_console_dev, '\b');
223 return;
224 }
225
Johan Hedbergceba31a2015-12-09 12:40:29 +0200226 cursor_save();
227
228 while (end-- > 0) {
229 *pos = *(pos + 1);
230 uart_poll_out(uart_console_dev, *(pos++));
Johan Hedberga9f6f892015-12-08 22:45:12 +0200231 }
232
233 uart_poll_out(uart_console_dev, ' ');
234
235 /* Move cursor back to right place */
Johan Hedbergceba31a2015-12-09 12:40:29 +0200236 cursor_restore();
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200237}
238
Johan Hedberg8683dc42015-12-09 11:36:25 +0200239enum {
240 ESC_ESC,
241 ESC_ANSI,
242 ESC_ANSI_FIRST,
243 ESC_ANSI_VAL,
Christopher Collins14735112018-01-17 18:01:52 -0800244 ESC_ANSI_VAL_2,
245#ifdef CONFIG_UART_CONSOLE_MCUMGR
246 ESC_MCUMGR_PKT_1,
247 ESC_MCUMGR_PKT_2,
248 ESC_MCUMGR_FRAG_1,
249 ESC_MCUMGR_FRAG_2,
250#endif
Johan Hedberg8683dc42015-12-09 11:36:25 +0200251};
252
253static atomic_t esc_state;
254static unsigned int ansi_val, ansi_val_2;
Kumar Galaccad5bf2017-04-21 10:03:20 -0500255static u8_t cur, end;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200256
Kumar Galaccad5bf2017-04-21 10:03:20 -0500257static void handle_ansi(u8_t byte, char *line)
Johan Hedberg8683dc42015-12-09 11:36:25 +0200258{
259 if (atomic_test_and_clear_bit(&esc_state, ESC_ANSI_FIRST)) {
260 if (!isdigit(byte)) {
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800261 ansi_val = 1U;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200262 goto ansi_cmd;
263 }
264
265 atomic_set_bit(&esc_state, ESC_ANSI_VAL);
266 ansi_val = byte - '0';
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800267 ansi_val_2 = 0U;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200268 return;
269 }
270
271 if (atomic_test_bit(&esc_state, ESC_ANSI_VAL)) {
272 if (isdigit(byte)) {
273 if (atomic_test_bit(&esc_state, ESC_ANSI_VAL_2)) {
274 ansi_val_2 *= 10;
275 ansi_val_2 += byte - '0';
276 } else {
277 ansi_val *= 10;
278 ansi_val += byte - '0';
279 }
280 return;
281 }
282
283 /* Multi value sequence, e.g. Esc[Line;ColumnH */
284 if (byte == ';' &&
285 !atomic_test_and_set_bit(&esc_state, ESC_ANSI_VAL_2)) {
286 return;
287 }
288
289 atomic_clear_bit(&esc_state, ESC_ANSI_VAL);
290 atomic_clear_bit(&esc_state, ESC_ANSI_VAL_2);
291 }
292
293ansi_cmd:
294 switch (byte) {
295 case ANSI_BACKWARD:
296 if (ansi_val > cur) {
297 break;
298 }
299
300 end += ansi_val;
301 cur -= ansi_val;
302 cursor_backward(ansi_val);
303 break;
304 case ANSI_FORWARD:
305 if (ansi_val > end) {
306 break;
307 }
308
309 end -= ansi_val;
310 cur += ansi_val;
311 cursor_forward(ansi_val);
312 break;
Szymon Janc6b0cf542016-10-26 18:10:50 +0200313 case ANSI_HOME:
314 if (!cur) {
315 break;
316 }
317
318 cursor_backward(cur);
319 end += cur;
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800320 cur = 0U;
Szymon Janc6b0cf542016-10-26 18:10:50 +0200321 break;
322 case ANSI_END:
323 if (!end) {
324 break;
325 }
326
327 cursor_forward(end);
328 cur += end;
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800329 end = 0U;
Szymon Janc6b0cf542016-10-26 18:10:50 +0200330 break;
Szymon Janca0373162016-10-26 18:15:02 +0200331 case ANSI_DEL:
332 if (!end) {
333 break;
334 }
335
336 cursor_forward(1);
337 del_char(&line[cur], --end);
338 break;
Johan Hedberg8683dc42015-12-09 11:36:25 +0200339 default:
340 break;
341 }
342
343 atomic_clear_bit(&esc_state, ESC_ANSI);
344}
345
Christopher Collins14735112018-01-17 18:01:52 -0800346#ifdef CONFIG_UART_CONSOLE_MCUMGR
347
348static void clear_mcumgr(void)
349{
350 atomic_clear_bit(&esc_state, ESC_MCUMGR_PKT_1);
351 atomic_clear_bit(&esc_state, ESC_MCUMGR_PKT_2);
352 atomic_clear_bit(&esc_state, ESC_MCUMGR_FRAG_1);
353 atomic_clear_bit(&esc_state, ESC_MCUMGR_FRAG_2);
354}
355
356/**
357 * These states indicate whether an mcumgr frame is being received.
358 */
359#define CONSOLE_MCUMGR_STATE_NONE 1
360#define CONSOLE_MCUMGR_STATE_HEADER 2
361#define CONSOLE_MCUMGR_STATE_PAYLOAD 3
362
363static int read_mcumgr_byte(uint8_t byte)
364{
365 bool frag_1;
366 bool frag_2;
367 bool pkt_1;
368 bool pkt_2;
369
370 pkt_1 = atomic_test_bit(&esc_state, ESC_MCUMGR_PKT_1);
371 pkt_2 = atomic_test_bit(&esc_state, ESC_MCUMGR_PKT_2);
372 frag_1 = atomic_test_bit(&esc_state, ESC_MCUMGR_FRAG_1);
373 frag_2 = atomic_test_bit(&esc_state, ESC_MCUMGR_FRAG_2);
374
375 if (pkt_2 || frag_2) {
376 /* Already fully framed. */
377 return CONSOLE_MCUMGR_STATE_PAYLOAD;
378 }
379
380 if (pkt_1) {
381 if (byte == MCUMGR_SERIAL_HDR_PKT_2) {
382 /* Final framing byte received. */
383 atomic_set_bit(&esc_state, ESC_MCUMGR_PKT_2);
384 return CONSOLE_MCUMGR_STATE_PAYLOAD;
385 }
386 } else if (frag_1) {
387 if (byte == MCUMGR_SERIAL_HDR_FRAG_2) {
388 /* Final framing byte received. */
389 atomic_set_bit(&esc_state, ESC_MCUMGR_FRAG_2);
390 return CONSOLE_MCUMGR_STATE_PAYLOAD;
391 }
392 } else {
393 if (byte == MCUMGR_SERIAL_HDR_PKT_1) {
394 /* First framing byte received. */
395 atomic_set_bit(&esc_state, ESC_MCUMGR_PKT_1);
396 return CONSOLE_MCUMGR_STATE_HEADER;
397 } else if (byte == MCUMGR_SERIAL_HDR_FRAG_1) {
398 /* First framing byte received. */
399 atomic_set_bit(&esc_state, ESC_MCUMGR_FRAG_1);
400 return CONSOLE_MCUMGR_STATE_HEADER;
401 }
402 }
403
404 /* Non-mcumgr byte received. */
405 return CONSOLE_MCUMGR_STATE_NONE;
406}
407
408/**
409 * @brief Attempts to process a received byte as part of an mcumgr frame.
410 *
411 * @param cmd The console command currently being received.
412 * @param byte The byte just received.
413 *
414 * @return true if the command being received is an mcumgr frame; false if it
415 * is a plain console command.
416 */
417static bool handle_mcumgr(struct console_input *cmd, uint8_t byte)
418{
419 int mcumgr_state;
420
421 mcumgr_state = read_mcumgr_byte(byte);
422 if (mcumgr_state == CONSOLE_MCUMGR_STATE_NONE) {
423 /* Not an mcumgr command; let the normal console handling
424 * process the byte.
425 */
426 cmd->is_mcumgr = 0;
427 return false;
428 }
429
430 /* The received byte is part of an mcumgr command. Process the byte
431 * and return true to indicate that normal console handling should
432 * ignore it.
433 */
434 if (cur + end < sizeof(cmd->line) - 1) {
435 cmd->line[cur++] = byte;
436 }
437 if (mcumgr_state == CONSOLE_MCUMGR_STATE_PAYLOAD && byte == '\n') {
438 cmd->line[cur + end] = '\0';
439 cmd->is_mcumgr = 1;
440 k_fifo_put(lines_queue, cmd);
441
442 clear_mcumgr();
443 cmd = NULL;
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800444 cur = 0U;
445 end = 0U;
Christopher Collins14735112018-01-17 18:01:52 -0800446 }
447
448 return true;
449}
450
451#endif /* CONFIG_UART_CONSOLE_MCUMGR */
452
Daniel Leunge643ced2016-03-03 10:14:50 -0800453void uart_console_isr(struct device *unused)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300454{
455 ARG_UNUSED(unused);
456
Johan Hedberg5ad78032015-12-07 15:03:20 +0200457 while (uart_irq_update(uart_console_dev) &&
458 uart_irq_is_pending(uart_console_dev)) {
Tomasz Bursztyka2f1af492017-01-24 10:08:08 +0100459 static struct console_input *cmd;
Kumar Galaccad5bf2017-04-21 10:03:20 -0500460 u8_t byte;
Johan Hedberg5ad78032015-12-07 15:03:20 +0200461 int rx;
462
463 if (!uart_irq_rx_ready(uart_console_dev)) {
464 continue;
465 }
466
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300467 /* Character(s) have been received */
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300468
Johan Hedberg5ad78032015-12-07 15:03:20 +0200469 rx = read_uart(uart_console_dev, &byte, 1);
470 if (rx < 0) {
471 return;
472 }
473
Marcus Shawcroft1bc999c2016-10-23 08:53:21 +0100474#ifdef CONFIG_UART_CONSOLE_DEBUG_SERVER_HOOKS
475 if (debug_hook_in != NULL && debug_hook_in(byte) != 0) {
Johan Hedberg5ad78032015-12-07 15:03:20 +0200476 /*
477 * The input hook indicates that no further processing
478 * should be done by this handler.
479 */
480 return;
481 }
Marcus Shawcroft1bc999c2016-10-23 08:53:21 +0100482#endif
Johan Hedberg5ad78032015-12-07 15:03:20 +0200483
484 if (!cmd) {
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200485 cmd = k_fifo_get(avail_queue, K_NO_WAIT);
Tomasz Bursztykafb981092017-01-24 09:06:08 +0100486 if (!cmd) {
Peter Mitsisb1c10202015-09-17 13:22:00 -0400487 return;
Tomasz Bursztykafb981092017-01-24 09:06:08 +0100488 }
Johan Hedberg5ad78032015-12-07 15:03:20 +0200489 }
Peter Mitsisb1c10202015-09-17 13:22:00 -0400490
Christopher Collins14735112018-01-17 18:01:52 -0800491#ifdef CONFIG_UART_CONSOLE_MCUMGR
492 /* Divert this byte from normal console handling if it is part
493 * of an mcumgr frame.
494 */
495 if (handle_mcumgr(cmd, byte)) {
496 continue;
497 }
498#endif /* CONFIG_UART_CONSOLE_MCUMGR */
499
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200500 /* Handle ANSI escape mode */
Johan Hedberg8683dc42015-12-09 11:36:25 +0200501 if (atomic_test_bit(&esc_state, ESC_ANSI)) {
Szymon Janca0373162016-10-26 18:15:02 +0200502 handle_ansi(byte, cmd->line);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200503 continue;
504 }
505
506 /* Handle escape mode */
Johan Hedberg8683dc42015-12-09 11:36:25 +0200507 if (atomic_test_and_clear_bit(&esc_state, ESC_ESC)) {
Tomasz Bursztykafb981092017-01-24 09:06:08 +0100508 if (byte == ANSI_ESC) {
Johan Hedberg8683dc42015-12-09 11:36:25 +0200509 atomic_set_bit(&esc_state, ESC_ANSI);
510 atomic_set_bit(&esc_state, ESC_ANSI_FIRST);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200511 }
512
513 continue;
514 }
515
Ruslan Mstoica94b862018-02-22 18:33:01 +0200516 /* Handle special control characters */
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200517 if (!isprint(byte)) {
518 switch (byte) {
qianfan Zhao13e1c3c2018-05-29 12:43:16 +0800519 case BS:
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200520 case DEL:
Johan Hedberga9f6f892015-12-08 22:45:12 +0200521 if (cur > 0) {
522 del_char(&cmd->line[--cur], end);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200523 }
524 break;
525 case ESC:
Johan Hedberg8683dc42015-12-09 11:36:25 +0200526 atomic_set_bit(&esc_state, ESC_ESC);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200527 break;
528 case '\r':
Johan Hedberga9f6f892015-12-08 22:45:12 +0200529 cmd->line[cur + end] = '\0';
Johan Hedberg65433182016-02-05 13:45:48 +0200530 uart_poll_out(uart_console_dev, '\r');
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200531 uart_poll_out(uart_console_dev, '\n');
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800532 cur = 0U;
533 end = 0U;
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200534 k_fifo_put(lines_queue, cmd);
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200535 cmd = NULL;
536 break;
Szymon Jancd7e8fd02016-05-25 16:23:42 +0200537 case '\t':
538 if (completion_cb && !end) {
539 cur += completion_cb(cmd->line, cur);
540 }
541 break;
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200542 default:
543 break;
544 }
Ruslan Mstoica94b862018-02-22 18:33:01 +0200545
546 continue;
Johan Hedbergf88cccd2015-12-08 10:35:04 +0200547 }
548
Johan Hedberg6147fc62015-12-08 09:26:31 +0200549 /* Ignore characters if there's no more buffer space */
Johan Hedberga9f6f892015-12-08 22:45:12 +0200550 if (cur + end < sizeof(cmd->line) - 1) {
551 insert_char(&cmd->line[cur++], byte, end);
Johan Hedberg6147fc62015-12-08 09:26:31 +0200552 }
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300553 }
554}
555
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300556static void console_input_init(void)
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300557{
Kumar Galaccad5bf2017-04-21 10:03:20 -0500558 u8_t c;
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300559
Daniel Leung08b4fd42015-12-01 08:42:20 -0800560 uart_irq_rx_disable(uart_console_dev);
561 uart_irq_tx_disable(uart_console_dev);
Daniel Leunge643ced2016-03-03 10:14:50 -0800562
563 uart_irq_callback_set(uart_console_dev, uart_console_isr);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300564
565 /* Drain the fifo */
Daniel Leung08b4fd42015-12-01 08:42:20 -0800566 while (uart_irq_rx_ready(uart_console_dev)) {
567 uart_fifo_read(uart_console_dev, &c, 1);
Daniel Leung1ad2a562015-08-05 12:13:36 -0700568 }
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300569
Daniel Leung08b4fd42015-12-01 08:42:20 -0800570 uart_irq_rx_enable(uart_console_dev);
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300571}
572
Luiz Augusto von Dentz6b2443e2016-11-11 14:11:44 +0200573void uart_register_input(struct k_fifo *avail, struct k_fifo *lines,
Kumar Galaccad5bf2017-04-21 10:03:20 -0500574 u8_t (*completion)(char *str, u8_t len))
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300575{
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300576 avail_queue = avail;
577 lines_queue = lines;
Szymon Jancd7e8fd02016-05-25 16:23:42 +0200578 completion_cb = completion;
Andrei Emeltchenko879541a2015-05-04 14:43:36 +0300579
580 console_input_init();
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300581}
Szymon Jancd7e8fd02016-05-25 16:23:42 +0200582
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300583#else
Christopher Collins14735112018-01-17 18:01:52 -0800584#define console_input_init(x) \
585 do { /* nothing */ \
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300586 } while ((0))
Christopher Collins14735112018-01-17 18:01:52 -0800587#define uart_register_input(x) \
588 do { /* nothing */ \
Andrei Emeltchenko139c8562015-04-20 11:04:22 +0300589 } while ((0))
590#endif
591
Anas Nashifea0d0b22015-07-01 17:22:39 -0400592/**
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700593 *
Daniel Leungad2d2962015-08-12 10:17:35 -0700594 * @brief Install printk/stdout hook for UART console output
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700595 *
Anas Nashif1362e3c2015-07-01 17:29:04 -0400596 * @return N/A
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700597 */
598
Daniel Leungad2d2962015-08-12 10:17:35 -0700599void uart_console_hook_install(void)
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700600{
Daniel Leung08b4fd42015-12-01 08:42:20 -0800601 __stdout_hook_install(console_out);
602 __printk_hook_install(console_out);
Inaky Perez-Gonzalez8ddf82c2015-04-10 16:44:37 -0700603}
Daniel Leungad2d2962015-08-12 10:17:35 -0700604
605/**
606 *
607 * @brief Initialize one UART as the console/debug port
608 *
Andre Guedes024cfe72016-03-09 14:01:20 -0300609 * @return 0 if successful, otherwise failed.
Daniel Leungad2d2962015-08-12 10:17:35 -0700610 */
611static int uart_console_init(struct device *arg)
612{
Jithu Josephfca0add2016-11-06 16:58:14 -0800613
Daniel Leungad2d2962015-08-12 10:17:35 -0700614 ARG_UNUSED(arg);
615
Daniel Leung08b4fd42015-12-01 08:42:20 -0800616 uart_console_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
617
Adithya Baglody23d946f2017-04-20 14:03:36 +0530618#if defined(CONFIG_USB_UART_CONSOLE) && defined(CONFIG_USB_UART_DTR_WAIT)
Jithu Josephfca0add2016-11-06 16:58:14 -0800619 while (1) {
Patrik Flykt8ff96b52018-11-29 11:12:22 -0800620 u32_t dtr = 0U;
Johan Hedberg488f72d2016-11-12 11:32:32 +0200621
Jithu Josephfca0add2016-11-06 16:58:14 -0800622 uart_line_ctrl_get(uart_console_dev, LINE_CTRL_DTR, &dtr);
623 if (dtr) {
624 break;
625 }
626 }
Baohong Liufa2d38e2016-11-30 14:58:45 -0800627 k_busy_wait(1000000);
Jithu Josephfca0add2016-11-06 16:58:14 -0800628#endif
629
Daniel Leungad2d2962015-08-12 10:17:35 -0700630 uart_console_hook_install();
631
Andre Guedes024cfe72016-03-09 14:01:20 -0300632 return 0;
Daniel Leungad2d2962015-08-12 10:17:35 -0700633}
Dmitriy Korovkin57f27412015-10-26 15:56:02 -0400634
Chuck Jordan12e29fe2016-05-06 12:43:56 -0700635/* UART console initializes after the UART device itself */
Benjamin Walsha4ec9632016-01-28 15:16:31 -0500636SYS_INIT(uart_console_init,
Jithu Josephfca0add2016-11-06 16:58:14 -0800637#if defined(CONFIG_USB_UART_CONSOLE)
Christopher Collins14735112018-01-17 18:01:52 -0800638 APPLICATION,
Jithu Josephfca0add2016-11-06 16:58:14 -0800639#elif defined(CONFIG_EARLY_CONSOLE)
Christopher Collins14735112018-01-17 18:01:52 -0800640 PRE_KERNEL_1,
Dmitriy Korovkin57f27412015-10-26 15:56:02 -0400641#else
Christopher Collins14735112018-01-17 18:01:52 -0800642 POST_KERNEL,
Dmitriy Korovkin57f27412015-10-26 15:56:02 -0400643#endif
Christopher Collins14735112018-01-17 18:01:52 -0800644 CONFIG_UART_CONSOLE_INIT_PRIORITY);