ESPHome 2026.3.0
Loading...
Searching...
No Matches
ble_characteristic.h
Go to the documentation of this file.
1#pragma once
2
3#include "ble_descriptor.h"
6
7#include <vector>
8#include <span>
9#include <functional>
10#include <memory>
11
12#ifdef USE_ESP32
13
14#include <esp_gap_ble_api.h>
15#include <esp_gatt_defs.h>
16#include <esp_gattc_api.h>
17#include <esp_gatts_api.h>
18#include <esp_bt_defs.h>
19
20namespace esphome {
21namespace esp32_ble_server {
22
23using namespace esp32_ble;
24using namespace bytebuffer;
25
26class BLEService;
27
29 public:
30 BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
32
33 void set_value(ByteBuffer buffer);
34 void set_value(std::vector<uint8_t> &&buffer);
35 void set_value(std::initializer_list<uint8_t> data);
36 void set_value(const std::string &buffer);
37
38 void set_broadcast_property(bool value);
39 void set_indicate_property(bool value);
40 void set_notify_property(bool value);
41 void set_read_property(bool value);
42 void set_write_property(bool value);
43 void set_write_no_response_property(bool value);
44
45 void notify();
46
47 void do_create(BLEService *service);
48 void do_delete() { this->clients_to_notify_.clear(); }
49 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
50
51 void add_descriptor(BLEDescriptor *descriptor);
52 void remove_descriptor(BLEDescriptor *descriptor);
53
54 BLEService *get_service() { return this->service_; }
55 ESPBTUUID get_uuid() { return this->uuid_; }
56 std::vector<uint8_t> &get_value() { return this->value_; }
57
58 static constexpr uint32_t PROPERTY_READ = 1 << 0;
59 static constexpr uint32_t PROPERTY_WRITE = 1 << 1;
60 static constexpr uint32_t PROPERTY_NOTIFY = 1 << 2;
61 static constexpr uint32_t PROPERTY_BROADCAST = 1 << 3;
62 static constexpr uint32_t PROPERTY_INDICATE = 1 << 4;
63 static constexpr uint32_t PROPERTY_WRITE_NR = 1 << 5;
64
65 bool is_created();
66 bool is_failed();
67
68 // Direct callback registration - only allocates when callback is set
69 void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) {
70 this->on_write_callback_ =
71 std::make_unique<std::function<void(std::span<const uint8_t>, uint16_t)>>(std::move(callback));
72 }
73 void on_read(std::function<void(uint16_t)> &&callback) {
74 this->on_read_callback_ = std::make_unique<std::function<void(uint16_t)>>(std::move(callback));
75 }
76
77 protected:
80 esp_gatt_char_prop_t properties_;
81 uint16_t handle_{0xFFFF};
82
83 uint16_t value_read_offset_{0};
84 std::vector<uint8_t> value_;
85 std::vector<BLEDescriptor *> descriptors_;
86
88 uint16_t conn_id;
89 bool indicate; // true = indicate, false = notify
90 };
91 std::vector<ClientNotificationEntry> clients_to_notify_;
92
93 void remove_client_from_notify_list_(uint16_t conn_id);
95
96 void set_property_bit_(esp_gatt_char_prop_t bit, bool value);
97
98 std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
99 std::unique_ptr<std::function<void(uint16_t)>> on_read_callback_;
100
101 esp_gatt_perm_t permissions_ = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
102
110};
111
112} // namespace esp32_ble_server
113} // namespace esphome
114
115#endif
A class modelled on the Java ByteBuffer class.
Definition bytebuffer.h:38
ClientNotificationEntry * find_client_in_notify_list_(uint16_t conn_id)
void remove_descriptor(BLEDescriptor *descriptor)
std::unique_ptr< std::function< void(std::span< const uint8_t >, uint16_t)> > on_write_callback_
enum esphome::esp32_ble_server::BLECharacteristic::State INIT
void set_property_bit_(esp_gatt_char_prop_t bit, bool value)
BLECharacteristic(ESPBTUUID uuid, uint32_t properties)
void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
std::vector< BLEDescriptor * > descriptors_
void add_descriptor(BLEDescriptor *descriptor)
void on_read(std::function< void(uint16_t)> &&callback)
std::vector< ClientNotificationEntry > clients_to_notify_
std::unique_ptr< std::function< void(uint16_t)> > on_read_callback_
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
bool indicate
uint16_t conn_id