1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include <zephyr.h> 17425bb815Sopenharmony_ci#include <uart.h> 18425bb815Sopenharmony_ci#include <drivers/console/console.h> 19425bb815Sopenharmony_ci#include <drivers/console/uart_console.h> 20425bb815Sopenharmony_ci#include "getline-zephyr.h" 21425bb815Sopenharmony_ci 22425bb815Sopenharmony_ci/* While app processes one input line, Zephyr will have another line 23425bb815Sopenharmony_ci buffer to accumulate more console input. */ 24425bb815Sopenharmony_cistatic struct console_input line_bufs[2]; 25425bb815Sopenharmony_ci 26425bb815Sopenharmony_cistatic struct k_fifo free_queue; 27425bb815Sopenharmony_cistatic struct k_fifo used_queue; 28425bb815Sopenharmony_ci 29425bb815Sopenharmony_cichar *zephyr_getline(void) 30425bb815Sopenharmony_ci{ 31425bb815Sopenharmony_ci static struct console_input *cmd; 32425bb815Sopenharmony_ci 33425bb815Sopenharmony_ci /* Recycle cmd buffer returned previous time */ 34425bb815Sopenharmony_ci if (cmd != NULL) 35425bb815Sopenharmony_ci { 36425bb815Sopenharmony_ci k_fifo_put(&free_queue, cmd); 37425bb815Sopenharmony_ci } 38425bb815Sopenharmony_ci 39425bb815Sopenharmony_ci cmd = k_fifo_get(&used_queue, K_FOREVER); 40425bb815Sopenharmony_ci return cmd->line; 41425bb815Sopenharmony_ci} 42425bb815Sopenharmony_ci 43425bb815Sopenharmony_civoid zephyr_getline_init(void) 44425bb815Sopenharmony_ci{ 45425bb815Sopenharmony_ci int i; 46425bb815Sopenharmony_ci 47425bb815Sopenharmony_ci k_fifo_init(&used_queue); 48425bb815Sopenharmony_ci k_fifo_init(&free_queue); 49425bb815Sopenharmony_ci for (i = 0; i < sizeof(line_bufs) / sizeof(*line_bufs); i++) 50425bb815Sopenharmony_ci { 51425bb815Sopenharmony_ci k_fifo_put(&free_queue, &line_bufs[i]); 52425bb815Sopenharmony_ci } 53425bb815Sopenharmony_ci 54425bb815Sopenharmony_ci /* Zephyr UART handler takes an empty buffer from free_queue, 55425bb815Sopenharmony_ci stores UART input in it until EOL, and then puts it into 56425bb815Sopenharmony_ci used_queue. */ 57425bb815Sopenharmony_ci uart_register_input(&free_queue, &used_queue, NULL); 58425bb815Sopenharmony_ci} 59