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