ESPHome 2026.11.0-dev
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(const uint8_t *data, size_t length);
30 void set_value(std::vector<uint8_t> &&buffer);
31 void set_value(std::initializer_list<uint8_t> data);
32 void set_value(ByteBuffer buffer) { this->set_value(buffer.get_data()); }
33
34 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
35
36 bool is_created() { return this->state_ == CREATED; }
37 bool is_failed() { return this->state_ == FAILED; }
38
39 // Direct callback registration - only allocates when callback is set
40 void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) {
41 this->on_write_callback_ =
42 std::make_unique<std::function<void(std::span<const uint8_t>, uint16_t)>>(std::move(callback));
43 }
44
45 protected:
47 ESPBTUUID uuid_;
48 uint16_t handle_{0xFFFF};
49
50 esp_attr_value_t value_{};
51
52 std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
53
54 esp_gatt_perm_t permissions_{};
55
56 enum State : uint8_t {
57 FAILED = 0x00,
61 } state_{INIT};
62};
63
64} // namespace esphome::esp32_ble_server
65
66#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 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)
void set_value(const uint8_t *data, size_t length)
uint32_t buffer
Definition gif.cpp:50
uint16_t length
Definition tt21100.cpp:0