ESPHome 2025.12.0-dev
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#include <freertos/FreeRTOS.h>
20#include <freertos/semphr.h>
21
22namespace esphome {
23namespace esp32_ble_server {
24
25using namespace esp32_ble;
26using namespace bytebuffer;
27
28class BLEService;
29
31 public:
32 BLECharacteristic(ESPBTUUID uuid, uint32_t properties);
34
35 void set_value(ByteBuffer buffer);
36 void set_value(std::vector<uint8_t> &&buffer);
37 void set_value(std::initializer_list<uint8_t> data);
38 void set_value(const std::string &buffer);
39
40 void set_broadcast_property(bool value);
41 void set_indicate_property(bool value);
42 void set_notify_property(bool value);
43 void set_read_property(bool value);
44 void set_write_property(bool value);
45 void set_write_no_response_property(bool value);
46
47 void notify();
48
49 void do_create(BLEService *service);
50 void do_delete() { this->clients_to_notify_.clear(); }
51 void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
52
53 void add_descriptor(BLEDescriptor *descriptor);
54 void remove_descriptor(BLEDescriptor *descriptor);
55
56 BLEService *get_service() { return this->service_; }
57 ESPBTUUID get_uuid() { return this->uuid_; }
58 std::vector<uint8_t> &get_value() { return this->value_; }
59
60 static const uint32_t PROPERTY_READ = 1 << 0;
61 static const uint32_t PROPERTY_WRITE = 1 << 1;
62 static const uint32_t PROPERTY_NOTIFY = 1 << 2;
63 static const uint32_t PROPERTY_BROADCAST = 1 << 3;
64 static const uint32_t PROPERTY_INDICATE = 1 << 4;
65 static const uint32_t PROPERTY_WRITE_NR = 1 << 5;
66
67 bool is_created();
68 bool is_failed();
69
70 // Direct callback registration - only allocates when callback is set
71 void on_write(std::function<void(std::span<const uint8_t>, uint16_t)> &&callback) {
72 this->on_write_callback_ =
73 std::make_unique<std::function<void(std::span<const uint8_t>, uint16_t)>>(std::move(callback));
74 }
75 void on_read(std::function<void(uint16_t)> &&callback) {
76 this->on_read_callback_ = std::make_unique<std::function<void(uint16_t)>>(std::move(callback));
77 }
78
79 protected:
80 bool write_event_{false};
83 esp_gatt_char_prop_t properties_;
84 uint16_t handle_{0xFFFF};
85
86 uint16_t value_read_offset_{0};
87 std::vector<uint8_t> value_;
88 SemaphoreHandle_t set_value_lock_;
89
90 std::vector<BLEDescriptor *> descriptors_;
91
93 uint16_t conn_id;
94 bool indicate; // true = indicate, false = notify
95 };
96 std::vector<ClientNotificationEntry> clients_to_notify_;
97
98 void remove_client_from_notify_list_(uint16_t conn_id);
100
101 void set_property_bit_(esp_gatt_char_prop_t bit, bool value);
102
103 std::unique_ptr<std::function<void(std::span<const uint8_t>, uint16_t)>> on_write_callback_;
104 std::unique_ptr<std::function<void(uint16_t)>> on_read_callback_;
105
106 esp_gatt_perm_t permissions_ = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE;
107
115};
116
117} // namespace esp32_ble_server
118} // namespace esphome
119
120#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
bool indicate
uint16_t conn_id