ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ble_descriptor.h
Go to the documentation of this file.
1#pragma once
2
5
6#ifdef USE_ESP32
7
8#include <esp_gatt_defs.h>
9#include <esp_gatts_api.h>
10#include <span>
11#include <functional>
12#include <memory>
13
15
16using namespace esp32_ble;
17using namespace bytebuffer;
18
19class BLECharacteristic;
20
21// Base class for BLE descriptors
23 public:
24 BLEDescriptor(ESPBTUUID uuid, uint16_t max_len = 100, bool read = true, bool write = true);
25 virtual ~BLEDescriptor();
26 void do_create(BLECharacteristic *characteristic);
27 ESPBTUUID get_uuid() const { return this->uuid_; }
28
29 void set_value(std::vector<uint8_t> &&buffer);
30 void set_value(std::initializer_list<uint8_t> data);
31 void set_value(ByteBuffer buffer) { this->set_value(buffer.get_data()); }
32
33 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
34
35 bool is_created() { return this->state_ == CREATED; }
36 bool is_failed() { return this->state_ == FAILED; }
37
38 // Direct callback registration - only allocates when callback is set
39 void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) {
40 this->on_write_callback_ =
41 std::make_unique<std::function<void(std::span<const uint8_t>, uint16_t)>>(std::move(callback));
42 }
43
44 protected:
45 void set_value_impl_(const uint8_t *data, size_t length);
46
49 uint16_t handle_{0xFFFF};
50
51 esp_attr_value_t value_{};
52
53 std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
54
55 esp_gatt_perm_t permissions_{};
56
57 enum State : uint8_t {
58 FAILED = 0x00,
62 } state_{INIT};
63};
64
65} // namespace esphome::esp32_ble_server
66
67#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:37
std::vector< uint8_t > get_data()
Definition bytebuffer.h:299
enum esphome::esp32_ble_server::BLEDescriptor::State INIT
std::unique_ptr< std::function< void(std::span< const uint8_t >, uint16_t)> > on_write_callback_
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
void set_value(std::vector< uint8_t > &&buffer)
void set_value_impl_(const uint8_t *data, size_t length)
void do_create(BLECharacteristic *characteristic)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
BLEDescriptor(ESPBTUUID uuid, uint16_t max_len=100, bool read=true, bool write=true)
uint16_t length
Definition tt21100.cpp:0