diff --git a/boards/posix/native_posix/Kconfig b/boards/posix/native_posix/Kconfig index da782b92864..e476ff10c1b 100644 --- a/boards/posix/native_posix/Kconfig +++ b/boards/posix/native_posix/Kconfig @@ -3,7 +3,7 @@ config BOARD_NATIVE_POSIX bool select NATIVE_POSIX_TIMER - select NATIVE_POSIX_CONSOLE + select POSIX_ARCH_CONSOLE if BOARD_NATIVE_POSIX diff --git a/boards/posix/native_posix/Kconfig.defconfig b/boards/posix/native_posix/Kconfig.defconfig index 141e1686b8e..985e6661847 100644 --- a/boards/posix/native_posix/Kconfig.defconfig +++ b/boards/posix/native_posix/Kconfig.defconfig @@ -42,7 +42,7 @@ endif # LOG if CONSOLE -config NATIVE_POSIX_CONSOLE +config POSIX_ARCH_CONSOLE default y if !SERIAL config UART_CONSOLE diff --git a/boards/posix/nrf52_bsim/Kconfig.defconfig b/boards/posix/nrf52_bsim/Kconfig.defconfig index 3164cb6d9cf..d2dc77e9d1c 100644 --- a/boards/posix/nrf52_bsim/Kconfig.defconfig +++ b/boards/posix/nrf52_bsim/Kconfig.defconfig @@ -43,7 +43,7 @@ endif # LOG if CONSOLE -config BSIM_CONSOLE +config POSIX_ARCH_CONSOLE default y if !SERIAL config UART_CONSOLE diff --git a/drivers/console/CMakeLists.txt b/drivers/console/CMakeLists.txt index f4875ae4cbc..40d1e53081d 100644 --- a/drivers/console/CMakeLists.txt +++ b/drivers/console/CMakeLists.txt @@ -2,17 +2,12 @@ zephyr_library() -if (${CONFIG_BSIM_CONSOLE}) - zephyr_library_sources(bsim_console.c) - zephyr_library_include_directories(${BSIM_COMPONENTS_PATH}/libUtilv1/src/) -endif() - zephyr_library_sources_ifdef(CONFIG_GSM_MUX gsm_mux.c) zephyr_library_sources_ifdef(CONFIG_IPM_CONSOLE_RECEIVER ipm_console_receiver.c) zephyr_library_sources_ifdef(CONFIG_IPM_CONSOLE_SENDER ipm_console_sender.c) zephyr_library_sources_ifdef(CONFIG_IPM_CONSOLE ipm_console.c) zephyr_library_sources_ifdef(CONFIG_JAILHOUSE_DEBUG_CONSOLE jailhouse_debug_console.c) -zephyr_library_sources_ifdef(CONFIG_NATIVE_POSIX_CONSOLE native_posix_console.c) +zephyr_library_sources_ifdef(CONFIG_POSIX_ARCH_CONSOLE posix_arch_console.c) zephyr_library_sources_ifdef(CONFIG_RAM_CONSOLE ram_console.c) zephyr_library_sources_ifdef(CONFIG_RTT_CONSOLE rtt_console.c) zephyr_library_sources_ifdef(CONFIG_SEMIHOST_CONSOLE semihost_console.c) diff --git a/drivers/console/Kconfig b/drivers/console/Kconfig index 1243907d7da..5bee3c21ed6 100644 --- a/drivers/console/Kconfig +++ b/drivers/console/Kconfig @@ -227,53 +227,17 @@ config XTENSA_SIM_CONSOLE help Use simulator console to print messages. -config BSIM_CONSOLE - bool "Use the BabbleSim tracing system for console output" - depends on ARCH_POSIX - select CONSOLE_HAS_DRIVER - help - Use the BabbleSim tracing system for the Zephyr console. - -config BSIM_CONSOLE_INIT_PRIORITY - int "Init priority" - default 99 - depends on BSIM_CONSOLE - help - Device driver initialization priority. - -config NATIVE_POSIX_CONSOLE +config POSIX_ARCH_CONSOLE bool "Use the host terminal for console" depends on ARCH_POSIX select CONSOLE_HAS_DRIVER help - Use the host terminal (where the native_posix binary was launched) for the - Zephyr console + Zephyr's printk messages will be directed to the host terminal stdout -config NATIVE_POSIX_STDIN_CONSOLE - bool "Use the host terminal stdin" - depends on NATIVE_POSIX_CONSOLE - help - No current use. Kept only as there is plans to start using these - drivers with the shell - -config NATIVE_STDIN_POLL_PERIOD - int "Polling period for stdin" - depends on NATIVE_POSIX_STDIN_CONSOLE - default 20 - help - In ms, polling period for stdin - -config NATIVE_POSIX_STDOUT_CONSOLE - bool "Print to the host terminal stdout" - depends on NATIVE_POSIX_CONSOLE - default y - help - Zephyr's printk messages will be directed to the host terminal stdout. - -config NATIVE_POSIX_CONSOLE_INIT_PRIORITY +config POSIX_ARCH_CONSOLE_INIT_PRIORITY int "Init priority" default 99 - depends on NATIVE_POSIX_CONSOLE + depends on POSIX_ARCH_CONSOLE help Device driver initialization priority. diff --git a/drivers/console/native_posix_console.c b/drivers/console/native_posix_console.c deleted file mode 100644 index af39a555d4d..00000000000 --- a/drivers/console/native_posix_console.c +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright (c) 2018 Oticon A/S - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include -#include -#include "posix_board_if.h" -#include -#include -#include -#include - -#define DEBUG_ECHO 0 - -#if (DEBUG_ECHO) -#define ECHO(...) printf(__VA_ARGS__) -#else -#define ECHO(...) -#endif - -#if defined(CONFIG_NATIVE_POSIX_STDOUT_CONSOLE) -/** - * - * @brief Initialize the driver that provides the printk output - * - */ -static void native_posix_stdout_init(void) -{ -#ifdef CONFIG_PRINTK - extern void __printk_hook_install(int (*fn)(int)); - __printk_hook_install(putchar); -#endif -} - -/** - * Ensure that whatever was written thru printk is displayed now - */ -void posix_flush_stdout(void) -{ - fflush(stdout); -} -#endif /* CONFIG_NATIVE_POSIX_STDOUT_CONSOLE */ - -#if defined(CONFIG_NATIVE_POSIX_STDIN_CONSOLE) - -#define VALID_DIRECTIVES \ - "Valid native console driver directives:\n" \ - " !wait %%u\n" \ - " !quit\n" - -static struct k_fifo *avail_queue; -static struct k_fifo *lines_queue; -static uint8_t (*completion_cb)(char *line, uint8_t len); -static bool stdin_is_tty; - -static K_KERNEL_STACK_DEFINE(stack, CONFIG_ARCH_POSIX_RECOMMENDED_STACK_SIZE); -static struct k_thread native_stdio_thread; - -static inline void found_eof(void) -{ - /* - * Once stdin is closed or the input file has ended, - * there is no need to try again - */ - ECHO("Got EOF\n"); - k_thread_abort(&native_stdio_thread); -} - -/* - * Check if the command is a directive the driver handles on its own - * and if it is, handle it. - * If not return 0 (so it can be passed to the shell) - * - * Inputs - * s Command string - * towait Pointer to the amount of time wait until attempting to receive - * the next command - * - * return 0 if it is not a directive - * return > 0 if it was a directive (command starts with '!') - * return 2 if the driver directive requires to pause processing input - */ -static int catch_directive(char *s, int32_t *towait) -{ - while (*s != 0 && isspace(*s) != 0) { - s++; - } - - if (*s != '!') { - return 0; - } - - if (strncmp(s, "!wait", 5) == 0) { - int ret; - - ret = sscanf(&s[5], "%i", towait); - if (ret != 1) { - posix_print_error_and_exit("%s(): '%s' not understood, " - "!wait syntax: !wait %%i\n", - __func__, s); - } - return 2; - } else if (strcmp(s, "!quit") == 0) { - posix_exit(0); - } - - posix_print_warning("%s(): '%s' not understood\n" VALID_DIRECTIVES, - __func__, s); - - return 1; -} - -/** - * Check if there is data ready in stdin - */ -static int stdin_not_ready(void) -{ - int ready; - fd_set readfds; - struct timeval timeout; - - timeout.tv_usec = 0; - timeout.tv_sec = 0; - - FD_ZERO(&readfds); - FD_SET(STDIN_FILENO, &readfds); - - ready = select(STDIN_FILENO+1, &readfds, NULL, NULL, &timeout); - - if (ready == 0) { - return 1; - } else if (ready == -1) { - posix_print_error_and_exit("%s: Error on select ()\n", - __func__); - } - - return 0; -} - -/** - * Check if there is any line in the stdin buffer, - * if there is and we have available shell buffers feed it to the shell - * - * This function returns how long the thread should wait in ms, - * before checking again the stdin buffer - */ -static int32_t attempt_read_from_stdin(void) -{ - static struct console_input *cmd; - int32_t towait = CONFIG_NATIVE_STDIN_POLL_PERIOD; - - while (1) { - char *ret; - int last; - int is_directive; - - if (feof(stdin)) { - found_eof(); - } - - /* - * If stdin comes from a terminal, we check if the user has - * input something, and if not we pause the process. - * - * If stdin is not coming from a terminal, but from a file or - * pipe, we always proceed to try to get data and block until - * we do - */ - if (stdin_is_tty && stdin_not_ready()) { - return towait; - } - - /* Pick next available shell line buffer */ - if (!cmd) { - cmd = k_fifo_get(avail_queue, K_NO_WAIT); - if (!cmd) { - return towait; - } - } - - /* - * By default stdin is (_IOLBF) line buffered when connected to - * a terminal and fully buffered (_IOFBF) when connected to a - * pipe/file. - * If we got a terminal: we already checked for it to be ready - * and therefore a full line should be there for us. - * - * If we got a pipe or file we will block until we get a line, - * or we reach EOF - */ - ret = fgets(cmd->line, CONSOLE_MAX_LINE_LEN, stdin); - if (ret == NULL) { - if (feof(stdin)) { - found_eof(); - } - /* - * Otherwise this was an unexpected error we do - * not try to handle - */ - return towait; - } - - /* Remove a possible end of line and other trailing spaces */ - last = (int)strlen(cmd->line) - 1; - while ((last >= 0) && isspace(cmd->line[last]) != 0) { - cmd->line[last--] = 0; - } - - ECHO("Got: \"%s\"\n", cmd->line); - - /* - * This console has a special set of directives which start with - * "!" which we capture here - */ - is_directive = catch_directive(cmd->line, &towait); - if (is_directive == 2) { - return towait; - } else if (is_directive > 0) { - continue; - } - - /* Let's give it to the shell to handle */ - k_fifo_put(lines_queue, cmd); - cmd = NULL; - } - - return towait; -} - -/** - * This thread will check if there is any new line in the stdin buffer - * every CONFIG_NATIVE_STDIN_POLL_PERIOD ms - * - * If there is, it will feed it to the shell - */ -static void native_stdio_runner(void *p1, void *p2, void *p3) -{ - stdin_is_tty = isatty(STDIN_FILENO); - - while (1) { - int32_t wait_time = attempt_read_from_stdin(); - - k_sleep(wait_time); - } -} -#endif /* CONFIG_NATIVE_POSIX_STDIN_CONSOLE */ - -static int native_posix_console_init(void) -{ - -#if defined(CONFIG_NATIVE_POSIX_STDOUT_CONSOLE) - native_posix_stdout_init(); -#endif - - return 0; -} - -SYS_INIT(native_posix_console_init, PRE_KERNEL_1, - CONFIG_NATIVE_POSIX_CONSOLE_INIT_PRIORITY); diff --git a/drivers/console/bsim_console.c b/drivers/console/posix_arch_console.c similarity index 60% rename from drivers/console/bsim_console.c rename to drivers/console/posix_arch_console.c index 8615e994d49..75c2f57dde9 100644 --- a/drivers/console/bsim_console.c +++ b/drivers/console/posix_arch_console.c @@ -6,14 +6,13 @@ */ #include -#include "bs_tracing.h" -#include "posix_board_if.h" +#include #define _STDOUT_BUF_SIZE 256 static char stdout_buff[_STDOUT_BUF_SIZE]; static int n_pend; /* Number of pending characters in buffer */ -int bsim_print_char(int c) +static int print_char(int c) { int printnow = 0; @@ -29,8 +28,7 @@ int bsim_print_char(int c) } if (printnow) { - bs_trace_print(BS_TRACE_RAW, NULL, 0, 2, BS_TRACE_AUTOTIME, 0, - "%s\n", stdout_buff); + posix_print_trace("%s\n", stdout_buff); n_pend = 0; stdout_buff[0] = 0; } @@ -44,28 +42,24 @@ void posix_flush_stdout(void) { if (n_pend) { stdout_buff[n_pend] = 0; - bs_trace_print(BS_TRACE_RAW, NULL, 0, 2, BS_TRACE_AUTOTIME, 0, - "%s", stdout_buff); + posix_print_trace("%s", stdout_buff); n_pend = 0; stdout_buff[0] = 0; - fflush(stdout); } } -/* - * @brief Initialize the driver that provides the printk output - * - * @return 0 if successful, otherwise failed. - */ -static int bsim_console_init(void) +static int posix_arch_console_init(void) { - #ifdef CONFIG_PRINTK extern void __printk_hook_install(int (*fn)(int)); - __printk_hook_install(bsim_print_char); + __printk_hook_install(print_char); +#endif +#ifdef CONFIG_STDOUT_CONSOLE + extern void __stdout_hook_install(int (*fn)(int)); + __stdout_hook_install(print_char); #endif - return 0; } -SYS_INIT(bsim_console_init, PRE_KERNEL_1, CONFIG_BSIM_CONSOLE_INIT_PRIORITY); +SYS_INIT(posix_arch_console_init, PRE_KERNEL_1, + CONFIG_POSIX_ARCH_CONSOLE_INIT_PRIORITY); diff --git a/drivers/serial/Kconfig.native_posix b/drivers/serial/Kconfig.native_posix index 09f1e92d7ac..39acf155f9b 100644 --- a/drivers/serial/Kconfig.native_posix +++ b/drivers/serial/Kconfig.native_posix @@ -32,8 +32,7 @@ config NATIVE_UART_0_ON_STDINOUT which invoked the native_posix executable. This is good enough for automated testing, or when feeding from a file/pipe. Note that other, non UART messages, will also be printed to the - terminal. This option should NOT be used in conjunction with - NATIVE_POSIX_STDIN_CONSOLE + terminal. It is strongly discouraged to try to use this option with the new shell interactively, as the default terminal configuration is NOT appropriate for interactive use. diff --git a/include/zephyr/drivers/console/native_posix_console.h b/include/zephyr/drivers/console/native_posix_console.h index 711329a8c2f..6a4429ab830 100644 --- a/include/zephyr/drivers/console/native_posix_console.h +++ b/include/zephyr/drivers/console/native_posix_console.h @@ -7,16 +7,11 @@ #ifndef ZEPHYR_INCLUDE_DRIVERS_CONSOLE_NATIVE_POSIX_CONSOLE_H_ #define ZEPHYR_INCLUDE_DRIVERS_CONSOLE_NATIVE_POSIX_CONSOLE_H_ -#include +/* + * This header is left for compatibility with old applications + * The console for native_posix is now provided by the posix_arch_console driver + */ -#ifdef __cplusplus -extern "C" { -#endif - -void posix_flush_stdout(void); - -#ifdef __cplusplus -} -#endif +#include #endif /* ZEPHYR_INCLUDE_DRIVERS_CONSOLE_NATIVE_POSIX_CONSOLE_H_ */ diff --git a/include/zephyr/drivers/console/posix_arch_console.h b/include/zephyr/drivers/console/posix_arch_console.h new file mode 100644 index 00000000000..e331245c0cd --- /dev/null +++ b/include/zephyr/drivers/console/posix_arch_console.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef ZEPHYR_INCLUDE_DRIVERS_CONSOLE_POSIX_ARCH_CONSOLE_H_ +#define ZEPHYR_INCLUDE_DRIVERS_CONSOLE_POSIX_ARCH_CONSOLE_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void posix_flush_stdout(void); + +#ifdef __cplusplus +} +#endif + +#endif /* ZEPHYR_INCLUDE_DRIVERS_CONSOLE_POSIX_ARCH_CONSOLE_H_ */ diff --git a/subsys/testsuite/arch/unit_testing/Kconfig b/subsys/testsuite/arch/unit_testing/Kconfig index c2fdd2e2744..fcb56a145df 100644 --- a/subsys/testsuite/arch/unit_testing/Kconfig +++ b/subsys/testsuite/arch/unit_testing/Kconfig @@ -13,7 +13,7 @@ config X86 if CONSOLE -config NATIVE_POSIX_CONSOLE +config POSIX_ARCH_CONSOLE bool default y select CONSOLE_HAS_DRIVER