ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
rp2040_ble.cpp
Go to the documentation of this file.
1#include "rp2040_ble.h"
2
3#ifdef USE_RP2040_BLE
4
6#include "esphome/core/log.h"
7
8#include <BluetoothLock.h>
9
10#include <cstring>
11
12namespace esphome::rp2040_ble {
13
14ESPHOME_LOG_TAG(TAG, "rp2040_ble");
15
16// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
18
20 global_ble = this;
21
22 // Pre-create every pool entry so the packet handler's allocate() is always a
23 // free-list pop — the IRQ path must never reach malloc() (the newlib malloc
24 // lock is not IRQ-safe). Deliberately
25 // unconditional: warming lazily on the first scan would move the allocations
26 // after setup, and doing it here keeps the pool's RAM cost visible at
27 // startup instead of appearing once scanning begins. On an incomplete warm,
28 // refuse to run instead (the stack is never enabled, so the packet handler
29 // cannot fire).
30 if (!this->report_pool_.warm()) {
31 ESP_LOGE(TAG, "Scan report pool warm-up failed");
32 this->mark_failed();
33 return;
34 }
35
36 if (this->enable_on_boot_) {
37 this->enable();
38 } else {
40 }
41}
42
45 return;
46 }
47
48 ESP_LOGD(TAG, "Enabling BLE...");
50 this->active_logged_ = false;
51
52 if (!this->btstack_initialized_) {
53 // Serialize with the BTstack background worker while wiring the stack up
54 // (arduino-pico's BluetoothHCI::install() takes the same lock here).
55 BluetoothLock lock;
56
57 // BTstack init functions are not idempotent — only call once
58 l2cap_init();
59 sm_init();
60
61#ifdef USE_BLE_GATT_CLIENT
62 gatt_client_init();
63 // The GATT engine kicks the MTU exchange explicitly right after a
64 // connection completes (auto negotiation would only run on the first
65 // query, which a with-cache connection never issues).
66 gatt_client_mtu_enable_auto_negotiation(0);
67 // Just-works security for peripheral-initiated pairing.
68 sm_set_io_capabilities(IO_CAPABILITY_NO_INPUT_NO_OUTPUT);
69 sm_set_authentication_requirements(SM_AUTHREQ_BONDING);
70#endif
71
73 hci_add_event_handler(&this->hci_event_callback_registration_);
74
76 sm_add_event_handler(&this->sm_event_callback_registration_);
77
78 this->btstack_initialized_ = true;
79 }
80
81 BluetoothLock lock;
82 hci_power_control(HCI_POWER_ON);
83}
84
87 return;
88 }
89
90 ESP_LOGD(TAG, "Disabling BLE...");
92
93 {
94 BluetoothLock lock;
95 hci_power_control(HCI_POWER_OFF);
96 }
97
99 ESP_LOGD(TAG, "BLE disabled");
100}
101
103 if (this->state_ == BLEComponentState::ACTIVE && !this->active_logged_) {
104 this->active_logged_ = true;
105 // The controller address becomes readable once HCI reaches WORKING.
106 // bd_addr_to_str() formats into a BTstack-internal static buffer, so both
107 // calls stay under the lock like every other BTstack call from the loop.
108 BluetoothLock lock;
109 gap_local_bd_addr(this->ble_mac_);
110 ESP_LOGI(TAG, "BLE active (MAC %s)", bd_addr_to_str(this->ble_mac_));
111 }
112
113 // Drain the lock-free ring filled by the BTstack packet handler; all
114 // per-report work runs here on the main loop, then the report returns to
115 // the pool.
116 BLEScanReport *report = this->report_queue_.pop();
117 if (report == nullptr)
118 return;
119 do {
120 for (auto *listener : this->scan_listeners_)
121 listener->on_scan_report(*report);
122 this->report_pool_.release(report);
123 } while ((report = this->report_queue_.pop()) != nullptr);
124
125 uint16_t dropped = this->report_queue_.get_and_reset_dropped_count();
126 if (dropped > 0) {
127 ESP_LOGW(TAG, "Dropped %u scan reports (queue full)", dropped);
128 }
129}
130
131static const char *state_to_str(BLEComponentState state) {
132 switch (state) {
134 return "OFF";
136 return "ENABLING";
138 return "ACTIVE";
140 return "DISABLING";
142 return "DISABLED";
143 default:
144 return "UNKNOWN";
145 }
146}
147
149 ESP_LOGCONFIG(TAG,
150 "RP2040 BLE:\n"
151 " Enable on boot: %s\n"
152 " State: %s",
153 YESNO(this->enable_on_boot_), state_to_str(this->state_));
154}
155
157
158void RP2040BLE::packet_handler(uint8_t type, uint16_t channel, uint8_t *packet, uint16_t size) {
159 if (global_ble == nullptr) {
160 return;
161 }
162
163 if (type != HCI_EVENT_PACKET) {
164 return;
165 }
166
167 uint8_t event_type = hci_event_packet_get_type(packet);
168
169 switch (event_type) {
170 case BTSTACK_EVENT_STATE: {
171 uint8_t state = btstack_event_state_get_state(packet);
172 if (state == HCI_STATE_WORKING && global_ble->state_ == BLEComponentState::ENABLING) {
174 }
175 break;
176 }
177 case GAP_EVENT_ADVERTISING_REPORT: {
178 // Runs in the CYW43 async-context worker (low-priority IRQ), NOT the
179 // ESPHome main loop: bounded copy into the lock-free queue only.
180 bd_addr_t addr; // accessor returns printable (MSB-first) order
181 gap_event_advertising_report_get_address(packet, addr);
182 uint8_t mac_lsb[MAC_ADDRESS_SIZE];
183 reverse_bd_addr(addr, mac_lsb); // LSB-first, the BLE convention consumers expect
184 global_ble->enqueue_scan_report_(mac_lsb, static_cast<int8_t>(gap_event_advertising_report_get_rssi(packet)),
185 gap_event_advertising_report_get_address_type(packet),
186 gap_event_advertising_report_get_advertising_event_type(packet),
187 gap_event_advertising_report_get_data(packet),
188 gap_event_advertising_report_get_data_length(packet));
189 break;
190 }
191 default:
192 break;
193 }
194}
195
196// The analyzer traces a leak on the failed-push path, which cannot happen: the
197// pool is sized to the queue capacity (SIZE-1), so allocate() returns nullptr
198// before push() can find the ring full.
199// NOLINTBEGIN(clang-analyzer-unix.Malloc)
200void RP2040BLE::enqueue_scan_report_(const uint8_t *mac_lsb_first, int8_t rssi, uint8_t addr_type,
201 uint8_t adv_event_type, const uint8_t *data, uint16_t data_len) {
202 BLEScanReport *report = this->report_pool_.allocate();
203 if (report == nullptr) {
204 // Pool exhausted — the queue is full; count and drop.
205 this->report_queue_.increment_dropped_count();
206 return;
207 }
208 memcpy(report->mac, mac_lsb_first, MAC_ADDRESS_SIZE);
209 report->rssi = rssi;
210 report->addr_type = addr_type;
211 report->adv_event_type = adv_event_type;
212 report->data_len =
213 (data_len <= sizeof(report->data)) ? static_cast<uint8_t>(data_len) : static_cast<uint8_t>(sizeof(report->data));
214 memcpy(report->data, data, report->data_len);
215 this->report_queue_.push(report);
216}
217// NOLINTEND(clang-analyzer-unix.Malloc)
218
219void RP2040BLE::get_mac_msb_first(uint8_t out[MAC_ADDRESS_SIZE]) const {
220 memcpy(out, this->ble_mac_, MAC_ADDRESS_SIZE);
221}
222
223bool RP2040BLE::scan_start(uint16_t interval, uint16_t window, bool active) {
224 if (!this->is_active()) {
225 // Power control stays with the user (enable_on_boot or an explicit
226 // enable() call) — auto-enabling here would defeat enable_on_boot: false
227 // the moment a tracker retries. Callers retry until the stack is up.
228 return false;
229 }
230#ifdef USE_BLE_GATT_CLIENT
231 this->scan_interval_ = interval;
232 this->scan_window_ = window;
234 this->scan_desired_ = true;
235 if (this->scan_inhibit_count_ > 0) {
236 // A connect attempt owns the radio; the scan starts physically when the
237 // inhibit is released. Report success — the controller will run it.
238 ESP_LOGV(TAG, "Scan start deferred (connect in progress)");
239 return true;
240 }
241#endif
242 // Serialize with the BTstack background worker (arduino-pico's BluetoothHCI
243 // takes the same lock around its gap_* calls).
244 BluetoothLock lock;
245 gap_set_scan_params(active ? 1 : 0, interval, window, 0 /* accept all */);
246 gap_start_scan();
247 return true;
248}
249
251#ifdef USE_BLE_GATT_CLIENT
252 this->scan_desired_ = false;
253#endif
254 if (!this->is_active()) {
255 return; // nothing can be scanning on a stack that is not up
256 }
257 BluetoothLock lock;
258 gap_stop_scan();
259}
260
261#ifdef USE_BLE_GATT_CLIENT
263 if (this->scan_inhibit_count_++ != 0) {
264 return; // another connect attempt already owns the radio
265 }
266 if (this->scan_desired_ && this->is_active()) {
267 BluetoothLock lock;
268 gap_stop_scan();
269 }
270}
271
273 if (this->scan_inhibit_count_ == 0) {
274 return;
275 }
276 this->scan_inhibit_count_--;
277 if (this->scan_inhibit_count_ != 0) {
278 return;
279 }
280 if (this->scan_desired_) {
281 // One physical-start path: scan_start re-applies the remembered params.
282 this->scan_start(this->scan_interval_, this->scan_window_, this->scan_active_mode_);
283 }
284}
285#endif // USE_BLE_GATT_CLIENT
286
287} // namespace esphome::rp2040_ble
288
289#endif // USE_RP2040_BLE
void mark_failed()
Mark this component as failed.
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]
Definition rp2040_ble.h:129
float get_setup_priority() const override
esphome::EventPool< BLEScanReport, MAX_SCAN_REPORT_QUEUE_SIZE - 1 > report_pool_
Definition rp2040_ble.h:124
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_
Definition rp2040_ble.h:120
btstack_packet_callback_registration_t hci_event_callback_registration_
Definition rp2040_ble.h:126
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_
Definition rp2040_ble.h:127
uint8_t type
bool state
Definition fan.h:2
std::vector< uint8_t > & out
Definition gif.cpp:49
RP2040BLE * global_ble
ESPHOME_LOG_TAG(TAG, "rp2040_ble")
constexpr float BLUETOOTH
Definition component.h:49
uint16_t size
Definition helpers.cpp:44
uint32_t * scan_start
One advertisement report from the controller.
Definition rp2040_ble.h:27
uint8_t mac[MAC_ADDRESS_SIZE]
Definition rp2040_ble.h:28
SemaphoreHandle_t lock