ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ble_descriptor.cpp
Go to the documentation of this file.
1#include "ble_descriptor.h"
3#include "ble_service.h"
4#include "esphome/core/log.h"
5
6#include <cstring>
7
8#ifdef USE_ESP32
9
11
12ESPHOME_LOG_TAG(TAG, "esp32_ble_server.descriptor");
13
14static RAMAllocator<uint8_t> descriptor_allocator{}; // NOLINT
15
16BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len, bool read, bool write) {
17 this->uuid_ = uuid;
18 this->value_.attr_len = 0;
19 this->value_.attr_max_len = max_len;
20 this->value_.attr_value = descriptor_allocator.allocate(max_len);
21 if (read) {
22 this->permissions_ |= ESP_GATT_PERM_READ;
23 }
24 if (write) {
25 this->permissions_ |= ESP_GATT_PERM_WRITE;
26 }
27}
28
29BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
30
32 this->characteristic_ = characteristic;
33 esp_attr_control_t control;
34 control.auto_rsp = ESP_GATT_AUTO_RSP;
35
36#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
37 char uuid_buf[esp32_ble::UUID_STR_LEN];
38 this->uuid_.to_str(uuid_buf);
39 ESP_LOGV(TAG, "Creating descriptor - %s", uuid_buf);
40#endif
41 esp_bt_uuid_t uuid = this->uuid_.get_uuid();
42 esp_err_t err = esp_ble_gatts_add_char_descr(this->characteristic_->get_service()->get_handle(), &uuid,
43 this->permissions_, &this->value_, &control);
44 if (err != ESP_OK) {
45 ESP_LOGE(TAG, "esp_ble_gatts_add_char_descr failed: %d", err);
46 this->state_ = FAILED;
47 return;
48 }
49 this->state_ = CREATING;
50}
51
52void BLEDescriptor::set_value(std::vector<uint8_t> &&buffer) { this->set_value(buffer.data(), buffer.size()); }
53
54void BLEDescriptor::set_value(std::initializer_list<uint8_t> data) { this->set_value(data.begin(), data.size()); }
55
56void BLEDescriptor::set_value(const uint8_t *data, size_t length) {
57 if (length > this->value_.attr_max_len) {
58 ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
59 return;
60 }
61 this->value_.attr_len = length;
62 if (length != 0) // An empty constant is a nullptr table
63 memcpy(this->value_.attr_value, data, length);
64}
65
66void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
67 esp_ble_gatts_cb_param_t *param) {
68 switch (event) {
69 case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
70 if (this->characteristic_ != nullptr && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
71 this->characteristic_->get_service()->get_handle() == param->add_char_descr.service_handle &&
72 this->characteristic_ == this->characteristic_->get_service()->get_last_created_characteristic()) {
73 this->handle_ = param->add_char_descr.attr_handle;
74 this->state_ = CREATED;
75 }
76 break;
77 }
78 case ESP_GATTS_WRITE_EVT: {
79 if (this->handle_ != param->write.handle)
80 break;
81 if (param->write.len > this->value_.attr_max_len) {
82 ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", param->write.len, this->value_.attr_max_len);
83 break;
84 }
85 this->value_.attr_len = param->write.len;
86 memcpy(this->value_.attr_value, param->write.value, param->write.len);
87 if (this->on_write_callback_) {
88 (*this->on_write_callback_)(std::span<const uint8_t>(param->write.value, param->write.len),
89 param->write.conn_id);
90 }
91 break;
92 }
93 default:
94 break;
95 }
96}
97
98} // namespace esphome::esp32_ble_server
99
100#endif
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2298
T * allocate(size_t n)
Definition helpers.h:2325
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Source compatibility with the historical esp32_ble API (esp32 builds only).
std::unique_ptr< std::function< void(std::span< const uint8_t >, uint16_t)> > on_write_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
ESPHOME_LOG_TAG(TAG, "esp32_ble")
uint16_t length
Definition tt21100.cpp:0