ESPHome 2026.11.0b6
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 : BLEDescriptor(uuid, max_len,
18 static_cast<esp_gatt_perm_t>((read ? ESP_GATT_PERM_READ : 0) | (write ? ESP_GATT_PERM_WRITE : 0))) {
19}
20
21BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len, esp_gatt_perm_t permissions)
22 : uuid_(uuid), permissions_(permissions) {
23 this->value_.attr_len = 0;
24 this->value_.attr_max_len = max_len;
25 this->value_.attr_value = descriptor_allocator.allocate(max_len);
26}
27
28BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
29
31 this->characteristic_ = characteristic;
32 esp_attr_control_t control;
33 control.auto_rsp = ESP_GATT_AUTO_RSP;
34
35#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
36 char uuid_buf[esp32_ble::UUID_STR_LEN];
37 this->uuid_.to_str(uuid_buf);
38 ESP_LOGV(TAG, "Creating descriptor - %s", uuid_buf);
39#endif
40 esp_bt_uuid_t uuid = this->uuid_.get_uuid();
41 esp_err_t err = esp_ble_gatts_add_char_descr(this->characteristic_->get_service()->get_handle(), &uuid,
42 this->permissions_, &this->value_, &control);
43 if (err != ESP_OK) {
44 ESP_LOGE(TAG, "esp_ble_gatts_add_char_descr failed: %d", err);
45 this->state_ = FAILED;
46 return;
47 }
48 this->state_ = CREATING;
49}
50
51void BLEDescriptor::set_value(std::vector<uint8_t> &&buffer) { this->set_value(buffer.data(), buffer.size()); }
52
53void BLEDescriptor::set_value(std::initializer_list<uint8_t> data) { this->set_value(data.begin(), data.size()); }
54
55void BLEDescriptor::set_value(const uint8_t *data, size_t length) {
56 if (length > this->value_.attr_max_len) {
57 ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
58 return;
59 }
60 this->value_.attr_len = length;
61 if (length != 0) // An empty constant is a nullptr table
62 memcpy(this->value_.attr_value, data, length);
63}
64
65void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
66 esp_ble_gatts_cb_param_t *param) {
67 switch (event) {
68 case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
69 if (this->characteristic_ != nullptr && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
70 this->characteristic_->get_service()->get_handle() == param->add_char_descr.service_handle &&
71 this->characteristic_ == this->characteristic_->get_service()->get_last_created_characteristic()) {
72 this->handle_ = param->add_char_descr.attr_handle;
73 this->state_ = CREATED;
74 }
75 break;
76 }
77 case ESP_GATTS_WRITE_EVT: {
78 if (this->handle_ != param->write.handle)
79 break;
80 if (param->write.len > this->value_.attr_max_len) {
81 ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", param->write.len, this->value_.attr_max_len);
82 break;
83 }
84 this->value_.attr_len = param->write.len;
85 memcpy(this->value_.attr_value, param->write.value, param->write.len);
86 if (this->on_write_callback_) {
87 (*this->on_write_callback_)(std::span<const uint8_t>(param->write.value, param->write.len),
88 param->write.conn_id);
89 }
90 break;
91 }
92 default:
93 break;
94 }
95}
96
97} // namespace esphome::esp32_ble_server
98
99#endif
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
T * allocate(size_t n)
Definition helpers.h:2423
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