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