Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix posix console missing chars, restore terminal attributes on exit #30

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading