ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ble_server_automations.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_server.h"
5#include "ble_descriptor.h"
6
8
9#include <vector>
10#include <functional>
11
12#ifdef USE_ESP32
13
14// Interface to interact with ESPHome actions and triggers
16
17using namespace esp32_ble;
18
20 public:
21#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
23 BLECharacteristic *characteristic);
24#endif
25#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
27#endif
28#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
30#endif
31#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
33#endif
34};
35
36#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
37// Class to make sure only one BLECharacteristicSetValueAction is active at a time for each characteristic
39 public:
40 // Singleton pattern
43 return &instance;
44 }
45 void set_listener(BLECharacteristic *characteristic, const std::function<void()> &pre_notify_listener);
46 bool has_listener(BLECharacteristic *characteristic) { return this->find_listener_(characteristic) != nullptr; }
47 void emit_pre_notify(BLECharacteristic *characteristic) {
48 for (const auto &entry : this->listeners_) {
49 if (entry.characteristic == characteristic) {
50 entry.pre_notify_listener();
51 break;
52 }
53 }
54 }
55
56 private:
57 struct ListenerEntry {
58 BLECharacteristic *characteristic;
59 std::function<void()> pre_notify_listener;
60 };
61 std::vector<ListenerEntry> listeners_;
62
63 ListenerEntry *find_listener_(BLECharacteristic *characteristic);
64 void remove_listener_(BLECharacteristic *characteristic);
65};
66
67template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
68 public:
69 BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
70 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
71 void set_buffer(std::initializer_list<uint8_t> buffer) { this->buffer_ = std::vector<uint8_t>(buffer); }
72 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
73 void play(const Ts &...x) override {
74 // If the listener is already set, do nothing
76 return;
77 // Set initial value
78 this->parent_->set_value(this->buffer_.value(x...));
79 // Set the listener for read events
80 this->parent_->on_read([this, x...](uint16_t id) {
81 // Set the value of the characteristic every time it is read
82 this->parent_->set_value(this->buffer_.value(x...));
83 });
84 // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
86 this->parent_, [this, x...]() { this->parent_->set_value(this->buffer_.value(x...)); });
87 }
88
89 protected:
91};
92#endif // USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
93
94#ifdef USE_ESP32_BLE_SERVER_NOTIFY_ACTION
95template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
96 public:
97 BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
98 void play(const Ts &...x) override {
99#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
100 // Call the pre-notify event
102#endif
103 // Notify the characteristic
104 this->parent_->notify();
105 }
106
107 protected:
109};
110#endif // USE_ESP32_BLE_SERVER_NOTIFY_ACTION
111
112#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
113template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
114 public:
116 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
117 void set_buffer(std::initializer_list<uint8_t> buffer) { this->buffer_ = std::vector<uint8_t>(buffer); }
118 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
119 void play(const Ts &...x) override { this->parent_->set_value(this->buffer_.value(x...)); }
120
121 protected:
123};
124#endif // USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
125
126} // namespace esphome::esp32_ble_server::esp32_ble_server_automations
127
128#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:37
std::vector< uint8_t > get_data()
Definition bytebuffer.h:299
void on_read(std::function< void(uint16_t)> &&callback)
void set_value(std::vector< uint8_t > &&buffer)
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)
uint16_t x
Definition tt21100.cpp:5