ESPHome  2025.2.0
ble_server_automations.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "ble_server.h"
4 #include "ble_characteristic.h"
5 #include "ble_descriptor.h"
6 
9 
10 #include <vector>
11 #include <unordered_map>
12 #include <functional>
13 
14 #ifdef USE_ESP32
15 
16 namespace esphome {
17 namespace esp32_ble_server {
18 // Interface to interact with ESPHome actions and triggers
19 namespace esp32_ble_server_automations {
20 
21 using namespace esp32_ble;
22 using namespace event_emitter;
23 
24 class BLETriggers {
25  public:
26  static Trigger<std::vector<uint8_t>, uint16_t> *create_characteristic_on_write_trigger(
27  BLECharacteristic *characteristic);
28  static Trigger<std::vector<uint8_t>, uint16_t> *create_descriptor_on_write_trigger(BLEDescriptor *descriptor);
29  static Trigger<uint16_t> *create_server_on_connect_trigger(BLEServer *server);
30  static Trigger<uint16_t> *create_server_on_disconnect_trigger(BLEServer *server);
31 };
32 
35 };
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) {
52  this->emit_(BLECharacteristicSetValueActionEvt::PRE_NOTIFY, characteristic);
53  }
54 
55  private:
56  std::unordered_map<BLECharacteristic *, std::pair<EventEmitterListenerID, EventEmitterListenerID>> listeners_;
57 };
58 
59 template<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
66  if (BLECharacteristicSetValueActionManager::get_instance()->get_listener(this->parent_) == this->listener_id_)
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 
86 template<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 
100 template<typename... Ts> class BLEDescriptorSetValueAction : public Action<Ts...> {
101  public:
102  BLEDescriptorSetValueAction(BLEDescriptor *descriptor) : parent_(descriptor) {}
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
void set_listener(BLECharacteristic *characteristic, EventEmitterListenerID listener_id, const std::function< void()> &pre_notify_listener)
uint16_t x
Definition: tt21100.cpp:17
std::vector< uint8_t > get_data()
Definition: bytebuffer.h:300
A class modelled on the Java ByteBuffer class.
Definition: bytebuffer.h:38
uint32_t EventEmitterListenerID
Definition: event_emitter.h:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
TEMPLATABLE_VALUE(std::vector< uint8_t >, buffer) void set_buffer(ByteBuffer buffer)
TEMPLATABLE_VALUE(std::vector< uint8_t >, buffer) void set_buffer(ByteBuffer buffer)