41#if defined(CLANG_TIDY)
45#define BK72XX_BLE_NO_SDK
46#elif !__has_include("ble_api.h")
48 "bk72xx_ble requires a BLE 5.x Beken SDK (ble_api.h). Supported SoCs: BK7231N/BK7236 (BLE 5.1) and BK7238/BK7252N/BK7253 (BLE 5.2). BK7231T/BK7251/BK7271 (BLE 4.2) and BK7231Q (no BLE) are not supported."
51#ifndef BK72XX_BLE_NO_SDK
63#ifdef BK72XX_BLE_HAS_COMMON_BDADDR
64#include "common_bt_defines.h"
77static const char *
const TAG =
"bk72xx_ble";
89static void ble_notice_callback(ble_notice_t notice,
void *param) {
90 if (s_ble ==
nullptr || param ==
nullptr)
92 if (notice != BLE_5_REPORT_ADV)
95 const recv_adv_t *info =
reinterpret_cast<const recv_adv_t *
>(param);
98 s_ble->
enqueue_scan_report(info->adv_addr,
static_cast<int8_t
>(info->rssi), info->adv_addr_type, info->data,
105 if (report ==
nullptr) {
110 memcpy(report->
mac, mac, 6);
114 (data_len <=
sizeof(report->
data)) ?
static_cast<uint8_t
>(data_len) :
static_cast<uint8_t
>(
sizeof(report->
data));
145 ble_set_notice_cb(ble_notice_callback);
154#ifdef BK72XX_BLE_HAS_COMMON_BDADDR
160 bool bdaddr_live =
false;
168 ESP_LOGW(TAG,
"Controller address still unset after init; BLE stack may not have started");
172 ESP_LOGD(TAG,
"BLE stack initialised");
179 if (report ==
nullptr)
183 listener->on_scan_report(*report);
189 uint16_t dropped = this->
report_queue_.get_and_reset_dropped_count();
191 ESP_LOGW(TAG,
"Dropped %u scan reports due to queue overflow", dropped);
195 for (
int i = 0; i < 6; i++)
204 " MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n"
215#ifdef BK72XX_BLE_HAS_COMMON_BDADDR
219 bool nonzero =
false;
241 const uint8_t ble[6] = {wifi_mac[0], wifi_mac[1], wifi_mac[2],
242 wifi_mac[3], wifi_mac[4],
static_cast<uint8_t
>(wifi_mac[5] + 1)};
244 for (
int i = 0; i < 6; i++)
262 struct scan_param
sp;
263 memset(&
sp, 0,
sizeof(
sp));
265 sp.interval = interval;
268 this->
scan_actv_idx_ = app_ble_get_idle_actv_idx_handle(SCAN_ACTV);
270 ESP_LOGE(TAG,
"Scan start failed: no idle activity handle");
273 ble_err_t ret = bk_ble_scan_start(this->
scan_actv_idx_, &sp,
nullptr);
274 if (ret != ERR_SUCCESS) {
275 ESP_LOGE(TAG,
"Scan start failed (err %d)",
static_cast<int>(ret));
struct bd_addr common_default_bdaddr
std::vector< BLEScanListener * > scan_listeners_
void enqueue_scan_report(const uint8_t *mac, int8_t rssi, uint8_t addr_type, const uint8_t *data, uint16_t data_len)
Internal: buffer one controller report (BDK notice callback, BLE task context — bounded copy under th...
void dump_config() override
void get_mac_lsb_first(uint8_t out[6]) const
Controller BLE address, least-significant octet first (BLE convention).
float get_setup_priority() const override
void enable()
Bring up the BDK BLE stack (one-time; the BDK has no teardown path).
bool scan_start(uint16_t interval, uint16_t window)
Start the controller scan.
esphome::LockFreeQueue< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE > report_queue_
esphome::EventPool< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE - 1 > report_pool_
void scan_stop()
Stop the controller scan (no-op when not scanning).
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
void HOT delay(uint32_t ms)
One advertisement report from the controller.