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