1 /* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <zephyr.h>
17 #include <uart.h>
18 #include <drivers/console/console.h>
19 #include <drivers/console/uart_console.h>
20 #include "getline-zephyr.h"
21
22 /* While app processes one input line, Zephyr will have another line
23 buffer to accumulate more console input. */
24 static struct console_input line_bufs[2];
25
26 static struct k_fifo free_queue;
27 static struct k_fifo used_queue;
28
zephyr_getline(void)29 char *zephyr_getline(void)
30 {
31 static struct console_input *cmd;
32
33 /* Recycle cmd buffer returned previous time */
34 if (cmd != NULL)
35 {
36 k_fifo_put(&free_queue, cmd);
37 }
38
39 cmd = k_fifo_get(&used_queue, K_FOREVER);
40 return cmd->line;
41 }
42
zephyr_getline_init(void)43 void zephyr_getline_init(void)
44 {
45 int i;
46
47 k_fifo_init(&used_queue);
48 k_fifo_init(&free_queue);
49 for (i = 0; i < sizeof(line_bufs) / sizeof(*line_bufs); i++)
50 {
51 k_fifo_put(&free_queue, &line_bufs[i]);
52 }
53
54 /* Zephyr UART handler takes an empty buffer from free_queue,
55 stores UART input in it until EOL, and then puts it into
56 used_queue. */
57 uart_register_input(&free_queue, &used_queue, NULL);
58 }
59