diff --git a/executables/referenceApp/platforms/posix/main/src/main.cpp b/executables/referenceApp/platforms/posix/main/src/main.cpp index 34698834e4..b2f30596a8 100644 --- a/executables/referenceApp/platforms/posix/main/src/main.cpp +++ b/executables/referenceApp/platforms/posix/main/src/main.cpp @@ -5,10 +5,15 @@ #include +#include +#include + #ifdef PLATFORM_SUPPORT_CAN #include "systems/CanSystem.h" #endif // PLATFORM_SUPPORT_CAN +void terminal_setup(void); +void terminal_cleanup(void); extern void app_main(); namespace platform @@ -37,8 +42,16 @@ ::can::ICanSystem& getCanSystem() { return *::platform::canSystem; } } // namespace systems #endif // PLATFORM_SUPPORT_CAN +void intHandler(int sig) +{ + terminal_cleanup(); + _exit(0); +} + int main() { + signal(SIGINT, intHandler); + terminal_setup(); app_main(); // entry point for the generic part return (1); // we never reach this point } diff --git a/platforms/posix/bsp/bspMcu/src/reset/softwareSystemReset.cpp b/platforms/posix/bsp/bspMcu/src/reset/softwareSystemReset.cpp index 083b6c65af..75c54f1813 100644 --- a/platforms/posix/bsp/bspMcu/src/reset/softwareSystemReset.cpp +++ b/platforms/posix/bsp/bspMcu/src/reset/softwareSystemReset.cpp @@ -4,10 +4,20 @@ #include +void terminal_cleanup(void); + extern "C" { -[[noreturn]] void softwareSystemReset() { _exit(0); } +[[noreturn]] void softwareSystemReset() +{ + terminal_cleanup(); + _exit(0); +} -void softwareDestructiveReset() { _exit(0); } +void softwareDestructiveReset() +{ + terminal_cleanup(); + _exit(0); +} } // extern "C" diff --git a/platforms/posix/bsp/bspStdio/src/bsp/stdIo/stdIo.cpp b/platforms/posix/bsp/bspStdio/src/bsp/stdIo/stdIo.cpp index eb5d4b0356..b9f0df3534 100644 --- a/platforms/posix/bsp/bspStdio/src/bsp/stdIo/stdIo.cpp +++ b/platforms/posix/bsp/bspStdio/src/bsp/stdIo/stdIo.cpp @@ -2,43 +2,63 @@ #include +#include #include #include +#include -namespace -{ -static char input = -1; +static bool terminal_setup_done = false; +static int terminal_stdout_fd = -1; +static struct termios terminal_original_attr; -int internal_kbhit(void) +void terminal_setup(void) { - struct termios org; - int const fd = fileno(stdin); - (void)tcgetattr(fd, &org); - struct termios tmp = org; - - tmp.c_lflag &= ~(ICANON | ECHO); - - (void)tcsetattr(fd, TCSANOW, &tmp); - int const chr = getchar(); - (void)tcsetattr(fd, TCSANOW, &org); - - input = chr; - return chr != -1; + if (!terminal_setup_done) + { + terminal_stdout_fd = fileno(stdout); + if (terminal_stdout_fd == -1) + { + return; + } + (void)tcgetattr(terminal_stdout_fd, &terminal_original_attr); + struct termios tmp = terminal_original_attr; + tmp.c_lflag &= ~(ICANON | ECHO); + (void)tcsetattr(terminal_stdout_fd, TCSANOW, &tmp); + terminal_setup_done = true; + } + return; } -inline int internal_getch(void) { return input; } - -} // namespace +void terminal_cleanup(void) +{ + if (terminal_setup_done) + { + (void)tcsetattr(terminal_stdout_fd, TCSANOW, &terminal_original_attr); + terminal_setup_done = false; + } +} -extern "C" void putByteToStdout(uint8_t byte) { (void)printf("%c", byte); } +extern "C" void putByteToStdout(uint8_t byte) +{ + if (terminal_setup_done) + { + while (write(terminal_stdout_fd, &byte, 1) != 1) + { + // Only if write is interrupted by a signal before it writes any data + // then try to write the byte again + if (errno != EINTR) + { + break; + } + } + } +} extern "C" int32_t getByteFromStdin() { - if (!internal_kbhit()) + if (terminal_setup_done) { - return -1; + return getchar(); } - - int const byte = internal_getch(); - return byte ? byte : -1; + return -1; }