8#include <BluetoothLock.h>
14static const char *
const TAG =
"rp2040_ble";
31 ESP_LOGE(TAG,
"Scan report pool warm-up failed");
48 ESP_LOGD(TAG,
"Enabling BLE...");
61#ifdef USE_BLE_GATT_CLIENT
66 gatt_client_mtu_enable_auto_negotiation(0);
68 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
69 sm_set_authentication_requirements(SM_AUTHREQ_BONDING);
82 hci_power_control(HCI_POWER_ON);
90 ESP_LOGD(TAG,
"Disabling BLE...");
95 hci_power_control(HCI_POWER_OFF);
99 ESP_LOGD(TAG,
"BLE disabled");
110 ESP_LOGI(TAG,
"BLE active (MAC %s)", bd_addr_to_str(this->
ble_mac_));
117 if (report ==
nullptr)
120#ifdef RP2040_BLE_SCAN_LISTENER_COUNT
122 listener->on_scan_report(*report);
127 uint16_t dropped = this->
report_queue_.get_and_reset_dropped_count();
129 ESP_LOGW(TAG,
"Dropped %u scan reports (queue full)", dropped);
153 " Enable on boot: %s\n"
165 if (
type != HCI_EVENT_PACKET) {
169 uint8_t event_type = hci_event_packet_get_type(packet);
171 switch (event_type) {
172 case BTSTACK_EVENT_STATE: {
173 uint8_t
state = btstack_event_state_get_state(packet);
179 case GAP_EVENT_ADVERTISING_REPORT: {
183 gap_event_advertising_report_get_address(packet, addr);
184 uint8_t mac_lsb[MAC_ADDRESS_SIZE];
185 reverse_bd_addr(addr, mac_lsb);
187 gap_event_advertising_report_get_address_type(packet),
188 gap_event_advertising_report_get_advertising_event_type(packet),
189 gap_event_advertising_report_get_data(packet),
190 gap_event_advertising_report_get_data_length(packet));
203 uint8_t adv_event_type,
const uint8_t *data, uint16_t data_len) {
205 if (report ==
nullptr) {
210 memcpy(report->
mac, mac_lsb_first, MAC_ADDRESS_SIZE);
215 (data_len <=
sizeof(report->
data)) ?
static_cast<uint8_t
>(data_len) :
static_cast<uint8_t
>(
sizeof(report->
data));
222 memcpy(out, this->
ble_mac_, MAC_ADDRESS_SIZE);
232#ifdef USE_BLE_GATT_CLIENT
240 ESP_LOGV(TAG,
"Scan start deferred (connect in progress)");
247 gap_set_scan_params(active ? 1 : 0, interval, window, 0 );
253#ifdef USE_BLE_GATT_CLIENT
263#ifdef USE_BLE_GATT_CLIENT
void mark_failed()
Mark this component as failed.
bool btstack_initialized_
static void packet_handler(uint8_t type, uint16_t channel, uint8_t *packet, uint16_t size)
void scan_stop()
Stop the controller scan (no-op when not scanning).
uint8_t ble_mac_[MAC_ADDRESS_SIZE]
uint8_t scan_inhibit_count_
bool scan_start(uint16_t interval, uint16_t window, bool active)
Start a controller scan; active sends scan requests and receives scan responses as separate reports.
float get_setup_priority() const override
StaticVector< BLEScanListener *, RP2040_BLE_SCAN_LISTENER_COUNT > scan_listeners_
esphome::EventPool< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE - 1 > report_pool_
void inhibit_scan()
Pause the physical scan for the duration of a GATT connect attempt (initiating and scanning contend f...
void get_mac_msb_first(uint8_t out[MAC_ADDRESS_SIZE]) const
Controller BLE address in printable (MSB-first) order, as gap_local_bd_addr() delivers it — note BLES...
esphome::LockFreeQueue< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE > report_queue_
void dump_config() override
void release_scan_inhibit()
btstack_packet_callback_registration_t hci_event_callback_registration_
void enqueue_scan_report_(const uint8_t *mac_lsb_first, int8_t rssi, uint8_t addr_type, uint8_t adv_event_type, const uint8_t *data, uint16_t data_len)
Buffer one controller report (BTstack packet handler, CYW43 async-context IRQ — bounded copy into the...
btstack_packet_callback_registration_t sm_event_callback_registration_
constexpr float BLUETOOTH
One advertisement report from the controller.
uint8_t mac[MAC_ADDRESS_SIZE]