|
23 | 23 | #define DEBUG 0 |
24 | 24 |
|
25 | 25 | #if DEBUG |
26 | | -#include <pbdrv/../../drv/uart/uart_debug_first_port.h> |
27 | | -#define DEBUG_PRINT pbdrv_uart_debug_printf |
| 26 | +#include <pbio/debug.h> |
| 27 | +#define DEBUG_PRINT pbio_debug |
28 | 28 | #else |
29 | 29 | #define DEBUG_PRINT(...) |
30 | 30 | #endif |
@@ -158,6 +158,10 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_periphe |
158 | 158 |
|
159 | 159 | pbdrv_bluetooth_peripheral_t *peri = &peripheral_singleton; |
160 | 160 |
|
| 161 | + if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) { |
| 162 | + return PBIO_ERROR_INVALID_OP; |
| 163 | + } |
| 164 | + |
161 | 165 | // Can't connect if already connected or already busy. |
162 | 166 | if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PERIPHERAL) || peri->func) { |
163 | 167 | return PBIO_ERROR_BUSY; |
@@ -188,8 +192,12 @@ pbio_error_t pbdrv_bluetooth_peripheral_disconnect(void) { |
188 | 192 | return PBIO_SUCCESS; |
189 | 193 | } |
190 | 194 |
|
| 195 | + // Clear notification handler to avoid receiving further data. |
| 196 | + if (peri->config) { |
| 197 | + peri->config->notification_handler = NULL; |
| 198 | + } |
| 199 | + |
191 | 200 | // Initialize operation for handling on the main thread. |
192 | | - peri->config->notification_handler = NULL; |
193 | 201 | peri->func = pbdrv_bluetooth_peripheral_disconnect_func; |
194 | 202 | peri->err = PBIO_ERROR_AGAIN; |
195 | 203 | pbio_os_request_poll(); |
@@ -296,6 +304,10 @@ pbdrv_bluetooth_advertising_state_t pbdrv_bluetooth_advertising_state; |
296 | 304 |
|
297 | 305 | pbio_error_t pbdrv_bluetooth_start_advertising(bool start) { |
298 | 306 |
|
| 307 | + if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) { |
| 308 | + return PBIO_ERROR_INVALID_OP; |
| 309 | + } |
| 310 | + |
299 | 311 | bool is_advertising = pbdrv_bluetooth_advertising_state == PBDRV_BLUETOOTH_ADVERTISING_STATE_ADVERTISING_PYBRICKS; |
300 | 312 |
|
301 | 313 | // Already in requested state. This makes it safe to call stop advertising |
@@ -326,6 +338,10 @@ uint8_t pbdrv_bluetooth_broadcast_data_size; |
326 | 338 |
|
327 | 339 | pbio_error_t pbdrv_bluetooth_start_broadcasting(const uint8_t *data, size_t size) { |
328 | 340 |
|
| 341 | + if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) { |
| 342 | + return PBIO_ERROR_INVALID_OP; |
| 343 | + } |
| 344 | + |
329 | 345 | if (advertising_or_scan_func) { |
330 | 346 | return PBIO_ERROR_BUSY; |
331 | 347 | } |
@@ -370,6 +386,10 @@ pbdrv_bluetooth_start_observing_callback_t pbdrv_bluetooth_observe_callback; |
370 | 386 |
|
371 | 387 | pbio_error_t pbdrv_bluetooth_start_observing(pbdrv_bluetooth_start_observing_callback_t callback) { |
372 | 388 |
|
| 389 | + if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) { |
| 390 | + return PBIO_ERROR_INVALID_OP; |
| 391 | + } |
| 392 | + |
373 | 393 | if (advertising_or_scan_func) { |
374 | 394 | return PBIO_ERROR_BUSY; |
375 | 395 | } |
@@ -589,14 +609,19 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti |
589 | 609 |
|
590 | 610 | void pbdrv_bluetooth_deinit(void) { |
591 | 611 |
|
| 612 | + // If Bluetooth is not even initialized, nothing to do. |
| 613 | + if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) { |
| 614 | + return; |
| 615 | + } |
| 616 | + |
592 | 617 | // Under normal operation ::pbdrv_bluetooth_close_user_tasks completes |
593 | 618 | // normally and there should be no user activity at this point. If there |
594 | 619 | // is, a task got stuck, so exit forcefully. |
595 | 620 | pbio_os_state_t unused; |
596 | 621 | if (pbdrv_bluetooth_await_advertise_or_scan_command(&unused, NULL) != PBIO_SUCCESS || |
597 | 622 | pbdrv_bluetooth_await_peripheral_command(&unused, NULL) != PBIO_SUCCESS) { |
598 | 623 |
|
599 | | - // Hard reset without waitng on completion of any process. |
| 624 | + // Hard reset without waiting on completion of any process. |
600 | 625 | pbdrv_bluetooth_controller_reset_hard(); |
601 | 626 | return; |
602 | 627 | } |
|
0 commit comments