1 /*
2  * Copyright (c) 2017 Linaro Limited.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr.h>
8 #include <drivers/uart.h>
9 #include <drivers/console/console.h>
10 #include <drivers/console/uart_console.h>
11 
12 /* While app processes one input line, Zephyr will have another line
13  * buffer to accumulate more console input.
14  */
15 static struct console_input line_bufs[2];
16 
17 static K_FIFO_DEFINE(free_queue);
18 static K_FIFO_DEFINE(used_queue);
19 
console_getline(void)20 char *console_getline(void)
21 {
22 	static struct console_input *cmd;
23 
24 	/* Recycle cmd buffer returned previous time */
25 	if (cmd != NULL) {
26 		k_fifo_put(&free_queue, cmd);
27 	}
28 
29 	cmd = k_fifo_get(&used_queue, K_FOREVER);
30 	return cmd->line;
31 }
32 
console_getline_init(void)33 void console_getline_init(void)
34 {
35 	int i;
36 
37 	for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
38 		k_fifo_put(&free_queue, &line_bufs[i]);
39 	}
40 
41 	/* Zephyr UART handler takes an empty buffer from free_queue,
42 	 * stores UART input in it until EOL, and then puts it into
43 	 * used_queue.
44 	 */
45 	uart_register_input(&free_queue, &used_queue, NULL);
46 }
47