Skip to content

Commit

Permalink
Fix posix console missing chars, restore terminal attributes on exit
Browse files Browse the repository at this point in the history
Change-Id: Ie2a581ddadba0e76b1a9d2dc7ab4c8cd3de63785
  • Loading branch information
frankdarcyacn authored and marcmo committed Dec 1, 2024
1 parent b90c307 commit ec25295
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 28 deletions.
13 changes: 13 additions & 0 deletions executables/referenceApp/platforms/posix/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@

#include <estd/typed_mem.h>

#include <signal.h>
#include <unistd.h>

#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
Expand Down Expand Up @@ -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
}
14 changes: 12 additions & 2 deletions platforms/posix/bsp/bspMcu/src/reset/softwareSystemReset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@

#include <unistd.h>

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"
72 changes: 46 additions & 26 deletions platforms/posix/bsp/bspStdio/src/bsp/stdIo/stdIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,63 @@

#include <platform/estdint.h>

#include <errno.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

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;
}

0 comments on commit ec25295

Please sign in to comment.