ESPHome 2026.5.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
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(std::initializer_list<uint8_t> buffer) { this->buffer_ = std::vector<uint8_t>(buffer); }
74 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
75 void play(const Ts &...x) override {
76 // If the listener is already set, do nothing
78 return;
79 // Set initial value
80 this->parent_->set_value(this->buffer_.value(x...));
81 // Set the listener for read events
82 this->parent_->on_read([this, x...](uint16_t id) {
83 // Set the value of the characteristic every time it is read
84 this->parent_->set_value(this->buffer_.value(x...));
85 });
86 // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
88 this->parent_, [this, x...]() { this->parent_->set_value(this->buffer_.value(x...)); });
89 }
90
91 protected:
93};
94#endif // USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
95
96#ifdef USE_ESP32_BLE_SERVER_NOTIFY_ACTION
97template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
98 public:
99 BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
100 void play(const Ts &...x) override {
101#ifdef USE_ESP32_BLE_SERVER_SET_VALUE_ACTION
102 // Call the pre-notify event
104#endif
105 // Notify the characteristic
106 this->parent_->notify();
107 }
108
109 protected:
111};
112#endif // USE_ESP32_BLE_SERVER_NOTIFY_ACTION
113
114#ifdef USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
115template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
116 public:
118 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
119 void set_buffer(std::initializer_list<uint8_t> buffer) { this->buffer_ = std::vector<uint8_t>(buffer); }
120 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
121 void play(const Ts &...x) override { this->parent_->set_value(this->buffer_.value(x...)); }
122
123 protected:
125};
126#endif // USE_ESP32_BLE_SERVER_DESCRIPTOR_SET_VALUE_ACTION
127
128} // namespace esp32_ble_server_automations
129} // namespace esp32_ble_server
130} // namespace esphome
131
132#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)
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
uint16_t x
Definition tt21100.cpp:5