ESPHome 2025.10.3
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
14namespace esphome {
15namespace esp32_ble_server {
16// Interface to interact with ESPHome actions and triggers
17namespace esp32_ble_server_automations {
18
19using namespace esp32_ble;
20
22 public:
23#ifdef USE_ESP32_BLE_SERVER_CHARACTERISTIC_ON_WRITE
25 BLECharacteristic *characteristic);
26#endif
27#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_ON_WRITE
29#endif
30#ifdef USE_ESP32_BLE_SERVER_ON_CONNECT
32#endif
33#ifdef USE_ESP32_BLE_SERVER_ON_DISCONNECT
35#endif
36};
37
38#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
39// Class to make sure only one BLECharacteristicSetValueAction is active at a time for each characteristic
41 public:
42 // Singleton pattern
45 return &instance;
46 }
47 void set_listener(BLECharacteristic *characteristic, const std::function<void()> &pre_notify_listener);
48 bool has_listener(BLECharacteristic *characteristic) { return this->find_listener_(characteristic) != nullptr; }
49 void emit_pre_notify(BLECharacteristic *characteristic) {
50 for (const auto &entry : this->listeners_) {
51 if (entry.characteristic == characteristic) {
52 entry.pre_notify_listener();
53 break;
54 }
55 }
56 }
57
58 private:
59 struct ListenerEntry {
60 BLECharacteristic *characteristic;
61 std::function<void()> pre_notify_listener;
62 };
63 std::vector<ListenerEntry> listeners_;
64
65 ListenerEntry *find_listener_(BLECharacteristic *characteristic);
66 void remove_listener_(BLECharacteristic *characteristic);
67};
68
69template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
70 public:
71 BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
72 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
73 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
74 void play(Ts... x) override {
75 // If the listener is already set, do nothing
77 return;
78 // Set initial value
79 this->parent_->set_value(this->buffer_.value(x...));
80 // Set the listener for read events
81 this->parent_->on_read([this, x...](uint16_t id) {
82 // Set the value of the characteristic every time it is read
83 this->parent_->set_value(this->buffer_.value(x...));
84 });
85 // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
87 this->parent_, [this, x...]() { this->parent_->set_value(this->buffer_.value(x...)); });
88 }
89
90 protected:
92};
93#endif // USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
94
95#ifdef USE_ESP32_BLE_SERVER_NOTIFY_ACTION
96template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
97 public:
98 BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
99 void play(Ts... x) override {
100#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
101 // Call the pre-notify event
103#endif
104 // Notify the characteristic
105 this->parent_->notify();
106 }
107
108 protected:
110};
111#endif // USE_ESP32_BLE_SERVER_NOTIFY_ACTION
112
113#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
114template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
115 public:
117 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
118 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
119 void play(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 esp32_ble_server_automations
127} // namespace esp32_ble_server
128} // namespace esphome
129
130#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
std::vector< uint8_t > get_data()
Definition bytebuffer.h:300
void on_read(std::function< void(uint16_t)> &&callback)
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, 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