ESPHome 2025.10.0-dev
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
9
10#include <vector>
11#include <functional>
12
13#ifdef USE_ESP32
14
15namespace esphome {
16namespace esp32_ble_server {
17// Interface to interact with ESPHome actions and triggers
18namespace esp32_ble_server_automations {
19
20using namespace esp32_ble;
21using namespace event_emitter;
22
23// Invalid listener ID constant - 0 is used as sentinel value in EventEmitter
24static constexpr EventEmitterListenerID INVALID_LISTENER_ID = 0;
25
27 public:
28#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
30 BLECharacteristic *characteristic);
31#endif
32#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
34#endif
35#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
37#endif
38#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
40#endif
41};
42
43#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
47
48// Class to make sure only one BLECharacteristicSetValueAction is active at a time for each characteristic
50 : public EventEmitter<BLECharacteristicSetValueActionEvt, BLECharacteristic *> {
51 public:
52 // Singleton pattern
55 return &instance;
56 }
57 void set_listener(BLECharacteristic *characteristic, EventEmitterListenerID listener_id,
58 const std::function<void()> &pre_notify_listener);
60 for (const auto &entry : this->listeners_) {
61 if (entry.characteristic == characteristic) {
62 return entry.listener_id;
63 }
64 }
65 return INVALID_LISTENER_ID;
66 }
67 void emit_pre_notify(BLECharacteristic *characteristic) {
69 }
70
71 private:
72 struct ListenerEntry {
73 BLECharacteristic *characteristic;
74 EventEmitterListenerID listener_id;
75 EventEmitterListenerID pre_notify_listener_id;
76 };
77 std::vector<ListenerEntry> listeners_;
78
79 ListenerEntry *find_listener_(BLECharacteristic *characteristic);
80 void remove_listener_(BLECharacteristic *characteristic);
81};
82
83template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
84 public:
85 BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
86 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
87 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
88 void play(Ts... x) override {
89 // If the listener is already set, do nothing
91 return;
92 // Set initial value
93 this->parent_->set_value(this->buffer_.value(x...));
94 // Set the listener for read events
95 this->listener_id_ = this->parent_->EventEmitter<BLECharacteristicEvt::EmptyEvt, uint16_t>::on(
96 BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](uint16_t id) {
97 // Set the value of the characteristic every time it is read
98 this->parent_->set_value(this->buffer_.value(x...));
99 });
100 // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
102 this->parent_, this->listener_id_, [this, x...]() { this->parent_->set_value(this->buffer_.value(x...)); });
103 }
104
105 protected:
108};
109#endif // USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
110
111#ifdef USE_ESP32_BLE_SERVER_NOTIFY_ACTION
112template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
113 public:
114 BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
115 void play(Ts... x) override {
116#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
117 // Call the pre-notify event
119#endif
120 // Notify the characteristic
121 this->parent_->notify();
122 }
123
124 protected:
126};
127#endif // USE_ESP32_BLE_SERVER_NOTIFY_ACTION
128
129#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
130template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
131 public:
133 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
134 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
135 void play(Ts... x) override { this->parent_->set_value(this->buffer_.value(x...)); }
136
137 protected:
139};
140#endif // USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
141
142} // namespace esp32_ble_server_automations
143} // namespace esp32_ble_server
144} // namespace esphome
145
146#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
std::vector< uint8_t > get_data()
Definition bytebuffer.h:300
void set_value(std::vector< uint8_t > buffer)
TEMPLATABLE_VALUE(std::vector< uint8_t >, buffer) void set_buffer(ByteBuffer buffer)
void set_listener(BLECharacteristic *characteristic, EventEmitterListenerID listener_id, const std::function< void()> &pre_notify_listener)
TEMPLATABLE_VALUE(std::vector< uint8_t >, buffer) void set_buffer(ByteBuffer buffer)
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
uint16_t x
Definition tt21100.cpp:5