ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ble_server_automations.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4
5// Interface to interact with ESPHome automations and triggers
7
8using namespace esp32_ble;
9
10#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
12 BLECharacteristic *characteristic) {
13 Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
14 new Trigger<std::vector<uint8_t>, uint16_t>();
15 characteristic->on_write([on_write_trigger](std::span<const uint8_t> data, uint16_t id) {
16 // Convert span to vector for trigger
17 on_write_trigger->trigger(std::vector<uint8_t>(data.begin(), data.end()), id);
18 });
19 return on_write_trigger;
20}
21#endif
22
23#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
25 Trigger<std::vector<uint8_t>, uint16_t> *on_write_trigger = // NOLINT(cppcoreguidelines-owning-memory)
26 new Trigger<std::vector<uint8_t>, uint16_t>();
27 descriptor->on_write([on_write_trigger](std::span<const uint8_t> data, uint16_t id) {
28 // Convert span to vector for trigger
29 on_write_trigger->trigger(std::vector<uint8_t>(data.begin(), data.end()), id);
30 });
31 return on_write_trigger;
32}
33#endif
34
35#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
37 Trigger<uint16_t> *on_connect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
38 server->on_connect([on_connect_trigger](uint16_t conn_id) { on_connect_trigger->trigger(conn_id); });
39 return on_connect_trigger;
40}
41#endif
42
43#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
45 Trigger<uint16_t> *on_disconnect_trigger = new Trigger<uint16_t>(); // NOLINT(cppcoreguidelines-owning-memory)
46 server->on_disconnect([on_disconnect_trigger](uint16_t conn_id) { on_disconnect_trigger->trigger(conn_id); });
47 return on_disconnect_trigger;
48}
49#endif
50
51#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
53 const std::function<void()> &pre_notify_listener) {
54 // Find and remove existing listener for this characteristic
55 auto *existing = this->find_listener_(characteristic);
56 if (existing != nullptr) {
57 // Remove from vector
58 this->remove_listener_(characteristic);
59 }
60 // Save the entry to the vector
61 this->listeners_.push_back({characteristic, pre_notify_listener});
62}
63
64BLECharacteristicSetValueActionManager::ListenerEntry *BLECharacteristicSetValueActionManager::find_listener_(
65 BLECharacteristic *characteristic) {
66 for (auto &entry : this->listeners_) {
67 if (entry.characteristic == characteristic) {
68 return &entry;
69 }
70 }
71 return nullptr;
72}
73
74void BLECharacteristicSetValueActionManager::remove_listener_(BLECharacteristic *characteristic) {
75 // Since we typically have very few listeners, optimize by swapping with back and popping
76 for (size_t i = 0; i < this->listeners_.size(); i++) {
77 if (this->listeners_[i].characteristic == characteristic) {
78 // Swap with last element and pop (safe even when i is the last element)
79 this->listeners_[i] = this->listeners_.back();
80 this->listeners_.pop_back();
81 return;
82 }
83 }
84}
85#endif
86
87} // namespace esphome::esp32_ble_server::esp32_ble_server_automations
88
89#endif
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:482
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
void on_connect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:60
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:63
void set_listener(BLECharacteristic *characteristic, const std::function< void()> &pre_notify_listener)
static Trigger< std::vector< uint8_t >, uint16_t > * create_characteristic_on_write_trigger(BLECharacteristic *characteristic)
static Trigger< uint16_t > * create_server_on_disconnect_trigger(BLEServer *server)
static Trigger< uint16_t > * create_server_on_connect_trigger(BLEServer *server)
static Trigger< std::vector< uint8_t >, uint16_t > * create_descriptor_on_write_trigger(BLEDescriptor *descriptor)