ESPHome 2026.1.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
10namespace esphome {
11namespace esp32_ble_server {
12
13static const char *const TAG = "esp32_ble_server.descriptor";
14
15static RAMAllocator<uint8_t> descriptor_allocator{}; // NOLINT
16
17BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len, bool read, bool write) {
18 this->uuid_ = uuid;
19 this->value_.attr_len = 0;
20 this->value_.attr_max_len = max_len;
21 this->value_.attr_value = descriptor_allocator.allocate(max_len);
22 if (read) {
23 this->permissions_ |= ESP_GATT_PERM_READ;
24 }
25 if (write) {
26 this->permissions_ |= ESP_GATT_PERM_WRITE;
27 }
28}
29
30BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
31
33 this->characteristic_ = characteristic;
34 esp_attr_control_t control;
35 control.auto_rsp = ESP_GATT_AUTO_RSP;
36
37#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
38 char uuid_buf[esp32_ble::UUID_STR_LEN];
39 this->uuid_.to_str(uuid_buf);
40 ESP_LOGV(TAG, "Creating descriptor - %s", uuid_buf);
41#endif
42 esp_bt_uuid_t uuid = this->uuid_.get_uuid();
43 esp_err_t err = esp_ble_gatts_add_char_descr(this->characteristic_->get_service()->get_handle(), &uuid,
44 this->permissions_, &this->value_, &control);
45 if (err != ESP_OK) {
46 ESP_LOGE(TAG, "esp_ble_gatts_add_char_descr failed: %d", err);
47 this->state_ = FAILED;
48 return;
49 }
50 this->state_ = CREATING;
51}
52
53void BLEDescriptor::set_value(std::vector<uint8_t> &&buffer) { this->set_value_impl_(buffer.data(), buffer.size()); }
54
55void BLEDescriptor::set_value(std::initializer_list<uint8_t> data) { this->set_value_impl_(data.begin(), data.size()); }
56
57void BLEDescriptor::set_value_impl_(const uint8_t *data, size_t length) {
58 if (length > this->value_.attr_max_len) {
59 ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
60 return;
61 }
62 this->value_.attr_len = length;
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 this->value_.attr_len = param->write.len;
82 memcpy(this->value_.attr_value, param->write.value, param->write.len);
83 if (this->on_write_callback_) {
84 (*this->on_write_callback_)(std::span<const uint8_t>(param->write.value, param->write.len),
85 param->write.conn_id);
86 }
87 break;
88 }
89 default:
90 break;
91 }
92}
93
94} // namespace esp32_ble_server
95} // namespace esphome
96
97#endif
T * allocate(size_t n)
Definition helpers.h:1294
void to_str(std::span< char, UUID_STR_LEN > output) const
Definition ble_uuid.cpp:146
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition ble_uuid.cpp:84
esp_bt_uuid_t get_uuid() const
Definition ble_uuid.cpp:145
std::unique_ptr< std::function< void(std::span< const uint8_t >, uint16_t)> > on_write_callback_
void set_value(std::vector< uint8_t > &&buffer)
void set_value_impl_(const uint8_t *data, size_t length)
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t length
Definition tt21100.cpp:0