ESPHome 2025.5.0
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 <unordered_map>
12#include <functional>
13
14#ifdef USE_ESP32
15
16namespace esphome {
17namespace esp32_ble_server {
18// Interface to interact with ESPHome actions and triggers
19namespace esp32_ble_server_automations {
20
21using namespace esp32_ble;
22using namespace event_emitter;
23
32
36
37// Class to make sure only one BLECharacteristicSetValueAction is active at a time for each characteristic
39 : public EventEmitter<BLECharacteristicSetValueActionEvt, BLECharacteristic *> {
40 public:
41 // Singleton pattern
44 return &instance;
45 }
46 void set_listener(BLECharacteristic *characteristic, EventEmitterListenerID listener_id,
47 const std::function<void()> &pre_notify_listener);
49 return this->listeners_[characteristic].first;
50 }
51 void emit_pre_notify(BLECharacteristic *characteristic) {
53 }
54
55 private:
56 std::unordered_map<BLECharacteristic *, std::pair<EventEmitterListenerID, EventEmitterListenerID>> listeners_;
57};
58
59template<typename... Ts> class BLECharacteristicSetValueAction : public Action<Ts...> {
60 public:
61 BLECharacteristicSetValueAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
62 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
63 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
64 void play(Ts... x) override {
65 // If the listener is already set, do nothing
67 return;
68 // Set initial value
69 this->parent_->set_value(this->buffer_.value(x...));
70 // Set the listener for read events
71 this->listener_id_ = this->parent_->EventEmitter<BLECharacteristicEvt::EmptyEvt, uint16_t>::on(
72 BLECharacteristicEvt::EmptyEvt::ON_READ, [this, x...](uint16_t id) {
73 // Set the value of the characteristic every time it is read
74 this->parent_->set_value(this->buffer_.value(x...));
75 });
76 // Set the listener in the global manager so only one BLECharacteristicSetValueAction is set for each characteristic
78 this->parent_, this->listener_id_, [this, x...]() { this->parent_->set_value(this->buffer_.value(x...)); });
79 }
80
81 protected:
84};
85
86template<typename... Ts> class BLECharacteristicNotifyAction : public Action<Ts...> {
87 public:
88 BLECharacteristicNotifyAction(BLECharacteristic *characteristic) : parent_(characteristic) {}
89 void play(Ts... x) override {
90 // Call the pre-notify event
92 // Notify the characteristic
93 this->parent_->notify();
94 }
95
96 protected:
98};
99
100template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
101 public:
103 TEMPLATABLE_VALUE(std::vector<uint8_t>, buffer)
104 void set_buffer(ByteBuffer buffer) { this->set_buffer(buffer.get_data()); }
105 void play(Ts... x) override { this->parent_->set_value(this->buffer_.value(x...)); }
106
107 protected:
109};
110
111} // namespace esp32_ble_server_automations
112} // namespace esp32_ble_server
113} // namespace esphome
114
115#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