|
13 | 13 |
|
14 | 14 | #include <pbio/busy_count.h> |
15 | 15 | #include <pbio/error.h> |
| 16 | +#include <pbio/int_math.h> |
16 | 17 | #include <pbio/os.h> |
17 | 18 | #include <pbio/protocol.h> |
18 | 19 |
|
@@ -303,15 +304,6 @@ pbio_error_t pbdrv_bluetooth_await_peripheral_command(pbio_os_state_t *state, vo |
303 | 304 | return peri->err; |
304 | 305 | } |
305 | 306 |
|
306 | | -void pbdrv_bluetooth_cancel_operation_request(void) { |
307 | | - // Only some peripheral actions support cancellation. |
308 | | - DEBUG_PRINT("Bluetooth operation cancel requested.\n"); |
309 | | - for (uint8_t i = 0; i < PBDRV_CONFIG_BLUETOOTH_NUM_PERIPHERALS; i++) { |
310 | | - pbdrv_bluetooth_peripheral_t *peri = pbdrv_bluetooth_peripheral_get_by_index(i); |
311 | | - peri->cancel = true; |
312 | | - } |
313 | | -} |
314 | | - |
315 | 307 | // |
316 | 308 | // Functions related to advertising and scanning. |
317 | 309 | // |
@@ -505,6 +497,60 @@ static bool update_and_get_event_buffer(uint8_t **buf, uint32_t **len) { |
505 | 497 | return false; |
506 | 498 | } |
507 | 499 |
|
| 500 | +#if PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS |
| 501 | +static pbdrv_bluetooth_classic_task_context_t pbdrv_bluetooth_classic_task_context; |
| 502 | + |
| 503 | +pbio_error_t pbdrv_bluetooth_start_inquiry_scan(pbdrv_bluetooth_inquiry_result_t *results, uint32_t *results_count, uint32_t *results_count_max, uint32_t duration_ms) { |
| 504 | + |
| 505 | + if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) { |
| 506 | + return PBIO_ERROR_INVALID_OP; |
| 507 | + } |
| 508 | + |
| 509 | + pbdrv_bluetooth_classic_task_context_t *task = &pbdrv_bluetooth_classic_task_context; |
| 510 | + |
| 511 | + if (task->func) { |
| 512 | + return PBIO_ERROR_BUSY; |
| 513 | + } |
| 514 | + |
| 515 | + // Initialize newly given task. |
| 516 | + task->inq_results = results; |
| 517 | + task->inq_count = results_count; |
| 518 | + task->inq_count_max = results_count_max; |
| 519 | + task->inq_duration = pbio_int_math_bind((duration_ms + 640) / 1280, 1, 255); |
| 520 | + |
| 521 | + // Request handling on the main loop. |
| 522 | + task->err = PBIO_ERROR_AGAIN; |
| 523 | + task->func = pbdrv_bluetooth_inquiry_scan_func; |
| 524 | + task->cancel = false; |
| 525 | + pbio_os_request_poll(); |
| 526 | + return PBIO_SUCCESS; |
| 527 | +} |
| 528 | + |
| 529 | +pbio_error_t pbdrv_bluetooth_await_classic_task(pbio_os_state_t *state, void *context) { |
| 530 | + |
| 531 | + pbdrv_bluetooth_classic_task_context_t *task = &pbdrv_bluetooth_classic_task_context; |
| 532 | + |
| 533 | + // If the user is no longer calling this then the operation is no longer |
| 534 | + // of interest and will be cancelled if the active function supports it. |
| 535 | + pbio_os_timer_set(&task->watchdog, 10); |
| 536 | + |
| 537 | + return task->err; |
| 538 | +} |
| 539 | +#endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS |
| 540 | + |
| 541 | +void pbdrv_bluetooth_cancel_operation_request(void) { |
| 542 | + // Only some peripheral actions support cancellation. |
| 543 | + DEBUG_PRINT("Bluetooth operation cancel requested.\n"); |
| 544 | + for (uint8_t i = 0; i < PBDRV_CONFIG_BLUETOOTH_NUM_PERIPHERALS; i++) { |
| 545 | + pbdrv_bluetooth_peripheral_t *peri = pbdrv_bluetooth_peripheral_get_by_index(i); |
| 546 | + peri->cancel = true; |
| 547 | + } |
| 548 | + #if PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS |
| 549 | + // Revisit: Cancel all. |
| 550 | + pbdrv_bluetooth_classic_task_context.cancel = true; |
| 551 | + #endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS |
| 552 | +} |
| 553 | + |
508 | 554 | static bool shutting_down; |
509 | 555 |
|
510 | 556 | /** |
@@ -595,6 +641,17 @@ pbio_error_t pbdrv_bluetooth_process_thread(pbio_os_state_t *state, void *contex |
595 | 641 | PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_start_observing_func(&sub, NULL)); |
596 | 642 | observe_restart_requested = false; |
597 | 643 | } |
| 644 | + |
| 645 | + #if PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS |
| 646 | + // Handle pending Bluetooth classic task, if any. |
| 647 | + static pbdrv_bluetooth_classic_task_context_t *task; |
| 648 | + task = &pbdrv_bluetooth_classic_task_context; |
| 649 | + if (task->func) { |
| 650 | + PBIO_OS_AWAIT(state, &sub, task->err = task->func(&sub, task)); |
| 651 | + task->func = NULL; |
| 652 | + task->cancel = false; |
| 653 | + } |
| 654 | + #endif // PBDRV_CONFIG_BLUETOOTH_NUM_CLASSIC_CONNECTIONS |
598 | 655 | } |
599 | 656 |
|
600 | 657 | DEBUG_PRINT("Shutdown requested.\n"); |
|
0 commit comments