ESPHome 2026.3.0
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
5#include "esphome/core/log.h"
6
8
9static const char *const TAG = "rp2040_ble";
10
11// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
13
15 global_ble = this;
16
17 if (this->enable_on_boot_) {
18 this->enable();
19 } else {
21 }
22}
23
26 return;
27 }
28
29 ESP_LOGD(TAG, "Enabling BLE...");
31 this->active_logged_ = false;
32
33 if (!this->btstack_initialized_) {
34 // BTstack init functions are not idempotent — only call once
35 l2cap_init();
36 sm_init();
37
39 hci_add_event_handler(&this->hci_event_callback_registration_);
40
42 sm_add_event_handler(&this->sm_event_callback_registration_);
43
44 this->btstack_initialized_ = true;
45 }
46
47 hci_power_control(HCI_POWER_ON);
48}
49
52 return;
53 }
54
55 ESP_LOGD(TAG, "Disabling BLE...");
57
58 hci_power_control(HCI_POWER_OFF);
59
61 ESP_LOGD(TAG, "BLE disabled");
62}
63
65 if (this->state_ == BLEComponentState::ACTIVE && !this->active_logged_) {
66 this->active_logged_ = true;
67 ESP_LOGI(TAG, "BLE active");
68 }
69}
70
71static const char *state_to_str(BLEComponentState state) {
72 switch (state) {
74 return "OFF";
76 return "ENABLING";
78 return "ACTIVE";
80 return "DISABLING";
82 return "DISABLED";
83 default:
84 return "UNKNOWN";
85 }
86}
87
89 ESP_LOGCONFIG(TAG,
90 "RP2040 BLE:\n"
91 " Enable on boot: %s\n"
92 " State: %s",
93 YESNO(this->enable_on_boot_), state_to_str(this->state_));
94}
95
97
98void RP2040BLE::packet_handler_(uint8_t type, uint16_t channel, uint8_t *packet, uint16_t size) {
99 if (global_ble == nullptr) {
100 return;
101 }
102
103 if (type != HCI_EVENT_PACKET) {
104 return;
105 }
106
107 uint8_t event_type = hci_event_packet_get_type(packet);
108
109 switch (event_type) {
110 case BTSTACK_EVENT_STATE: {
111 uint8_t state = btstack_event_state_get_state(packet);
112 if (state == HCI_STATE_WORKING && global_ble->state_ == BLEComponentState::ENABLING) {
114 }
115 break;
116 }
117 default:
118 break;
119 }
120}
121
122} // namespace esphome::rp2040_ble
123
124#endif // USE_RP2040_BLE
float get_setup_priority() const override
static void packet_handler_(uint8_t type, uint16_t channel, uint8_t *packet, uint16_t size)
btstack_packet_callback_registration_t hci_event_callback_registration_
Definition rp2040_ble.h:37
btstack_packet_callback_registration_t sm_event_callback_registration_
Definition rp2040_ble.h:38
uint16_t type
bool state
Definition fan.h:2
RP2040BLE * global_ble
constexpr float BLUETOOTH
Definition component.h:34
size_t size
Definition helpers.h:929