ESPHome 2025.12.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
14namespace esphome {
15namespace esp32_ble_server {
16
17using namespace esp32_ble;
18using namespace bytebuffer;
19
20class BLECharacteristic;
21
22// Base class for BLE descriptors
24 public:
25 BLEDescriptor(ESPBTUUID uuid, uint16_t max_len = 100, bool read = true, bool write = true);
26 virtual ~BLEDescriptor();
27 void do_create(BLECharacteristic *characteristic);
28 ESPBTUUID get_uuid() const { return this->uuid_; }
29
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:
46 void set_value_impl_(const uint8_t *data, size_t length);
47
50 uint16_t handle_{0xFFFF};
51
52 esp_attr_value_t value_{};
53
54 std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
55
56 esp_gatt_perm_t permissions_{};
57
58 enum State : uint8_t {
59 FAILED = 0x00,
63 } state_{INIT};
64};
65
66} // namespace esp32_ble_server
67} // namespace esphome
68
69#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
std::vector< uint8_t > get_data()
Definition bytebuffer.h:300
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t length
Definition tt21100.cpp:0