|
6 | 6 | #if PBDRV_CONFIG_BLUETOOTH_SIMULATION |
7 | 7 |
|
8 | 8 | #include <errno.h> |
9 | | -#include <fcntl.h> |
10 | 9 | #include <signal.h> |
11 | 10 | #include <stdio.h> |
12 | 11 | #include <stdlib.h> |
13 | | -#include <termios.h> |
14 | 12 | #include <unistd.h> |
15 | 13 |
|
16 | 14 | #include "bluetooth.h" |
@@ -131,17 +129,17 @@ pbio_error_t pbdrv_bluetooth_controller_initialize(pbio_os_state_t *state, pbio_ |
131 | 129 | #define STDIN_HEADER_SIZE (1) |
132 | 130 |
|
133 | 131 | static void pbdrv_bluetooth_simulation_tick_handler() { |
134 | | - uint8_t buf[256 + STDIN_HEADER_SIZE]; |
135 | | - ssize_t r = read(STDIN_FILENO, buf + STDIN_HEADER_SIZE, sizeof(buf) - STDIN_HEADER_SIZE); |
136 | | - |
137 | | - if (r > 0) { |
138 | | - buf[0] = PBIO_PYBRICKS_COMMAND_WRITE_STDIN; |
139 | | - pbdrv_bluetooth_receive_handler(buf, r + STDIN_HEADER_SIZE); |
140 | | - } else if (r == 0) { |
141 | | - // EOF. |
142 | | - } else if (errno != EAGAIN && errno != EWOULDBLOCK) { |
143 | | - // No data available. |
144 | | - } |
| 132 | + // uint8_t buf[256 + STDIN_HEADER_SIZE]; |
| 133 | + // ssize_t r = read(STDIN_FILENO, buf + STDIN_HEADER_SIZE, sizeof(buf) - STDIN_HEADER_SIZE); |
| 134 | + |
| 135 | + // if (r > 0) { |
| 136 | + // buf[0] = PBIO_PYBRICKS_COMMAND_WRITE_STDIN; |
| 137 | + // pbdrv_bluetooth_receive_handler(buf, r + STDIN_HEADER_SIZE); |
| 138 | + // } else if (r == 0) { |
| 139 | + // // EOF. |
| 140 | + // } else if (errno != EAGAIN && errno != EWOULDBLOCK) { |
| 141 | + // // No data available. |
| 142 | + // } |
145 | 143 | } |
146 | 144 |
|
147 | 145 | static pbio_os_process_t pbdrv_bluetooth_simulation_process; |
@@ -170,59 +168,7 @@ static pbio_error_t pbdrv_bluetooth_simulation_process_thread(pbio_os_state_t *s |
170 | 168 | return bluetooth_thread_err; |
171 | 169 | } |
172 | 170 |
|
173 | | -static struct termios oldt; |
174 | | - |
175 | | -static void restore_terminal_settings(void) { |
176 | | - tcsetattr(STDIN_FILENO, TCSANOW, &oldt); |
177 | | -} |
178 | | - |
179 | | -static void handle_signal(int sig) { |
180 | | - restore_terminal_settings(); |
181 | | - signal(sig, SIG_DFL); |
182 | | - raise(sig); |
183 | | -} |
184 | | - |
185 | 171 | void pbdrv_bluetooth_init_hci(void) { |
186 | | - struct termios newt; |
187 | | - |
188 | | - // Save the original terminal settings |
189 | | - if (tcgetattr(STDIN_FILENO, &oldt) != 0) { |
190 | | - printf("DEBUG: Failed to get terminal attributes\n"); |
191 | | - return; |
192 | | - } |
193 | | - |
194 | | - // Register the cleanup function to restore terminal settings on exit |
195 | | - atexit(restore_terminal_settings); |
196 | | - signal(SIGINT, handle_signal); |
197 | | - signal(SIGTERM, handle_signal); |
198 | | - |
199 | | - newt = oldt; |
200 | | - |
201 | | - // Get one char at a time instead of newline and disable CTRL+C for exit. |
202 | | - newt.c_lflag &= ~(ICANON | ECHO | ISIG); |
203 | | - |
204 | | - // MicroPython REPL expects \r for newline. |
205 | | - newt.c_iflag |= INLCR; |
206 | | - newt.c_iflag &= ~ICRNL; |
207 | | - |
208 | | - if (tcsetattr(STDIN_FILENO, TCSANOW, &newt) != 0) { |
209 | | - printf("Failed to set terminal attributes\n"); |
210 | | - return; |
211 | | - } |
212 | | - |
213 | | - // Set stdin non-blocking so we can service it in the runloop like on |
214 | | - // embedded hubs. |
215 | | - int flags = fcntl(STDIN_FILENO, F_GETFL, 0); |
216 | | - if (flags == -1) { |
217 | | - printf("Failed to get fcntl flags\n"); |
218 | | - return; |
219 | | - } |
220 | | - |
221 | | - if (fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK) == -1) { |
222 | | - printf("Failed to set non-blocking\n"); |
223 | | - return; |
224 | | - } |
225 | | - |
226 | 172 | bluetooth_thread_err = PBIO_ERROR_AGAIN; |
227 | 173 | bluetooth_thread_state = 0; |
228 | 174 | pbio_os_process_start(&pbdrv_bluetooth_simulation_process, pbdrv_bluetooth_simulation_process_thread, NULL); |
|
0 commit comments