|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2025 The Pybricks Authors |
| 3 | + |
| 4 | +// This file defines the functions required to implement Pybricks' Bluetooth |
| 5 | +// classic functionality using BTStack. |
| 6 | + |
| 7 | +#include <pbdrv/config.h> |
| 8 | + |
| 9 | +#if PBDRV_CONFIG_BLUETOOTH_BTSTACK_CLASSIC |
| 10 | + |
| 11 | +#include <pbdrv/bluetooth.h> |
| 12 | + |
| 13 | +#include <inttypes.h> |
| 14 | +#include <stdint.h> |
| 15 | +#include <stdio.h> |
| 16 | + |
| 17 | +#include <btstack.h> |
| 18 | + |
| 19 | +#include <pbdrv/bluetooth.h> |
| 20 | + |
| 21 | +static int32_t pending_inquiry_response_count; |
| 22 | +static int32_t pending_inquiry_response_limit; |
| 23 | +static void* pending_inquiry_result_handler_context; |
| 24 | +static pbdrv_bluetooth_inquiry_result_handler_t pending_inquiry_result_handler; |
| 25 | + |
| 26 | +static void handle_hci_event_packet(uint8_t* packet, uint16_t size); |
| 27 | + |
| 28 | +void pbdrv_bluetooth_classic_handle_packet(uint8_t packet_type, uint16_t channel, uint8_t* packet, uint16_t size) { |
| 29 | + switch (packet_type) { |
| 30 | + case HCI_EVENT_PACKET: |
| 31 | + handle_hci_event_packet(packet, size); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +static void handle_hci_event_packet(uint8_t* packet, uint16_t size) { |
| 36 | + switch (hci_event_packet_get_type(packet)) { |
| 37 | + case GAP_EVENT_INQUIRY_RESULT: { |
| 38 | + if (!pending_inquiry_result_handler) { |
| 39 | + return; |
| 40 | + } |
| 41 | + pbdrv_bluetooth_inquiry_result_t result; |
| 42 | + gap_event_inquiry_result_get_bd_addr(packet, result.bdaddr); |
| 43 | + if (gap_event_inquiry_result_get_rssi_available(packet)) { |
| 44 | + result.rssi = gap_event_inquiry_result_get_rssi(packet); |
| 45 | + } |
| 46 | + if (gap_event_inquiry_result_get_name_available(packet)) { |
| 47 | + const uint8_t* name = gap_event_inquiry_result_get_name(packet); |
| 48 | + const size_t name_len = gap_event_inquiry_result_get_name_len(packet); |
| 49 | + snprintf(result.name, sizeof(result.name), "%.*s", (int)name_len, name); |
| 50 | + } |
| 51 | + result.class_of_device = gap_event_inquiry_result_get_class_of_device(packet); |
| 52 | + pending_inquiry_result_handler(pending_inquiry_result_handler_context, &result); |
| 53 | + if (pending_inquiry_response_limit > 0) { |
| 54 | + pending_inquiry_response_count++; |
| 55 | + if (pending_inquiry_response_count >= pending_inquiry_response_limit) { |
| 56 | + gap_inquiry_stop(); |
| 57 | + } |
| 58 | + } |
| 59 | + break; |
| 60 | + } |
| 61 | + case GAP_EVENT_INQUIRY_COMPLETE: { |
| 62 | + if (pending_inquiry_result_handler) { |
| 63 | + pending_inquiry_result_handler = NULL; |
| 64 | + pending_inquiry_result_handler_context = NULL; |
| 65 | + pbio_os_request_poll(); |
| 66 | + } |
| 67 | + break; |
| 68 | + } |
| 69 | + default: |
| 70 | + break; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +void pbdrv_bluetooth_classic_init() { |
| 75 | + static btstack_packet_callback_registration_t hci_event_handler_registration; |
| 76 | + hci_event_handler_registration.callback = pbdrv_bluetooth_classic_handle_packet; |
| 77 | + hci_add_event_handler(&hci_event_handler_registration); |
| 78 | +} |
| 79 | + |
| 80 | +pbio_error_t pbdrv_bluetooth_inquiry_scan( |
| 81 | + pbio_os_state_t* state, |
| 82 | + int32_t max_responses, |
| 83 | + int32_t timeout, |
| 84 | + void* context, |
| 85 | + pbdrv_bluetooth_inquiry_result_handler_t result_handler) { |
| 86 | + PBIO_OS_ASYNC_BEGIN(state); |
| 87 | + if (pending_inquiry_result_handler) { |
| 88 | + return PBIO_ERROR_BUSY; |
| 89 | + } |
| 90 | + PBIO_OS_AWAIT_UNTIL(state, hci_get_state() == HCI_STATE_WORKING); |
| 91 | + pending_inquiry_response_count = 0; |
| 92 | + pending_inquiry_response_limit = max_responses; |
| 93 | + pending_inquiry_result_handler = result_handler; |
| 94 | + pending_inquiry_result_handler_context = context; |
| 95 | + gap_inquiry_start(timeout); |
| 96 | + |
| 97 | + PBIO_OS_AWAIT_UNTIL(state, !pending_inquiry_result_handler); |
| 98 | + PBIO_OS_ASYNC_END(PBIO_SUCCESS); |
| 99 | +} |
| 100 | + |
| 101 | +#endif // PBDRV_CONFIG_BLUETOOTH_BTSTACK_CLASSIC |
0 commit comments