ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
bk72xx_ble.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_BK72XX_BLE
6
10
11#include <cstdint>
12#include <vector>
13
14namespace esphome::bk72xx_ble {
15
16enum class BLEComponentState : uint8_t {
17 STATE_OFF = 0,
19 ACTIVE,
20};
21
24 uint8_t mac[6]; // LSB-first, as the controller delivers it
25 int8_t rssi; // signed dBm
26 uint8_t addr_type;
27 uint8_t data_len; // bytes valid in data[]
28 uint8_t data[62]; // legacy advertisement (31) + scan response (31)
29
30 // EventPool contract: nothing is heap-allocated inside a report.
31 void release() {}
32};
33
39 public:
40 virtual void on_scan_report(const BLEScanReport &report) = 0;
41
42 protected:
43 ~BLEScanListener() = default; // deletion via this interface is not part of the contract
44};
45
46// Maximum reports buffered between the BLE task and loop().
47static constexpr uint8_t MAX_SCAN_REPORT_QUEUE_SIZE = 64;
48
49class BK72xxBLE final : public Component {
50 public:
51 void setup() override;
52 void loop() override;
53 void dump_config() override;
54 float get_setup_priority() const override;
55
57 void enable();
58 bool is_active() const { return this->state_ == BLEComponentState::ACTIVE; }
59
60 void set_enable_on_boot(bool enable_on_boot) { this->enable_on_boot_ = enable_on_boot; }
61
63 void get_mac_lsb_first(uint8_t out[6]) const;
64
66 void register_scan_listener(BLEScanListener *listener) { this->scan_listeners_.push_back(listener); }
67
70 bool scan_start(uint16_t interval, uint16_t window);
72 void scan_stop();
73
76 void enqueue_scan_report(const uint8_t *mac, int8_t rssi, uint8_t addr_type, const uint8_t *data, uint16_t data_len);
77
78 protected:
79 void resolve_mac_();
80
81 std::vector<BLEScanListener *> scan_listeners_;
82 // Report ring: the BDK notice callback (BLE task) allocates a report from the
83 // pool, fills it and pushes the pointer; loop() pops, dispatches and releases.
84 // Lock-free SPSC, zero allocation at steady state — the esp32_ble pattern.
86 // Pool sized to queue capacity (SIZE-1): the ring reserves one slot, so
87 // allocate() returns nullptr before push() can fail. This prevents leaking a
88 // pool slot on a failed push and keeps release() off the producer path.
89 esphome::EventPool<BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE - 1> report_pool_;
90 uint8_t ble_mac_[6]{0}; // LSB-first (BLE convention)
91 uint8_t scan_actv_idx_{0xFF};
93 bool enable_on_boot_{false};
94};
95
96} // namespace esphome::bk72xx_ble
97
98#endif // USE_BK72XX_BLE
std::vector< BLEScanListener * > scan_listeners_
Definition bk72xx_ble.h:81
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 register_scan_listener(BLEScanListener *listener)
Register a consumer for scan reports (delivered on the main task via loop()).
Definition bk72xx_ble.h:66
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).
esphome::LockFreeQueue< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE > report_queue_
Definition bk72xx_ble.h:85
esphome::EventPool< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE - 1 > report_pool_
Definition bk72xx_ble.h:89
void set_enable_on_boot(bool enable_on_boot)
Definition bk72xx_ble.h:60
void scan_stop()
Stop the controller scan (no-op when not scanning).
Consumer interface for controller scan reports.
Definition bk72xx_ble.h:38
virtual void on_scan_report(const BLEScanReport &report)=0
uint32_t * scan_start
One advertisement report from the controller.
Definition bk72xx_ble.h:23