Skip to content

Commit 96e5ba1

Browse files
committed
pbio/drv/bluetooth_simulation: Clean up terminal on exit.
1 parent 5b534e0 commit 96e5ba1

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

lib/pbio/drv/bluetooth/bluetooth_simulation.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
#if PBDRV_CONFIG_BLUETOOTH_SIMULATION
77

88
#include <errno.h>
9-
#include <unistd.h>
109
#include <fcntl.h>
11-
#include <termios.h>
10+
#include <signal.h>
1211
#include <stdio.h>
1312
#include <stdlib.h>
13+
#include <termios.h>
14+
#include <unistd.h>
1415

1516
#include "bluetooth.h"
1617
#include <pbdrv/bluetooth.h>
@@ -175,14 +176,32 @@ static pbio_error_t pbdrv_bluetooth_simulation_process_thread(pbio_os_state_t *s
175176
return bluetooth_thread_err;
176177
}
177178

179+
static struct termios oldt;
180+
181+
static void restore_terminal_settings(void) {
182+
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
183+
}
184+
185+
static void handle_signal(int sig) {
186+
restore_terminal_settings();
187+
signal(sig, SIG_DFL);
188+
raise(sig);
189+
}
190+
178191
void pbdrv_bluetooth_init_hci(void) {
179-
struct termios oldt, newt;
192+
struct termios newt;
180193

194+
// Save the original terminal settings
181195
if (tcgetattr(STDIN_FILENO, &oldt) != 0) {
182196
printf("DEBUG: Failed to get terminal attributes\n");
183197
return;
184198
}
185199

200+
// Register the cleanup function to restore terminal settings on exit
201+
atexit(restore_terminal_settings);
202+
signal(SIGINT, handle_signal);
203+
signal(SIGTERM, handle_signal);
204+
186205
newt = oldt;
187206

188207
// Get one char at a time instead of newline and disable CTRL+C for exit.

0 commit comments

Comments
 (0)