ESPHome 2025.12.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 ESP_LOGV(TAG, "Creating descriptor - %s", this->uuid_.to_string().c_str());
38 esp_bt_uuid_t uuid = this->uuid_.get_uuid();
39 esp_err_t err = esp_ble_gatts_add_char_descr(this->characteristic_->get_service()->get_handle(), &uuid,
40 this->permissions_, &this->value_, &control);
41 if (err != ESP_OK) {
42 ESP_LOGE(TAG, "esp_ble_gatts_add_char_descr failed: %d", err);
43 this->state_ = FAILED;
44 return;
45 }
46 this->state_ = CREATING;
47}
48
49void BLEDescriptor::set_value(std::vector<uint8_t> &&buffer) { this->set_value_impl_(buffer.data(), buffer.size()); }
50
51void BLEDescriptor::set_value(std::initializer_list<uint8_t> data) { this->set_value_impl_(data.begin(), data.size()); }
52
53void BLEDescriptor::set_value_impl_(const uint8_t *data, size_t length) {
54 if (length > this->value_.attr_max_len) {
55 ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
56 return;
57 }
58 this->value_.attr_len = length;
59 memcpy(this->value_.attr_value, data, length);
60}
61
62void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
63 esp_ble_gatts_cb_param_t *param) {
64 switch (event) {
65 case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
66 if (this->characteristic_ != nullptr && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
67 this->characteristic_->get_service()->get_handle() == param->add_char_descr.service_handle &&
68 this->characteristic_ == this->characteristic_->get_service()->get_last_created_characteristic()) {
69 this->handle_ = param->add_char_descr.attr_handle;
70 this->state_ = CREATED;
71 }
72 break;
73 }
74 case ESP_GATTS_WRITE_EVT: {
75 if (this->handle_ != param->write.handle)
76 break;
77 this->value_.attr_len = param->write.len;
78 memcpy(this->value_.attr_value, param->write.value, param->write.len);
79 if (this->on_write_callback_) {
80 (*this->on_write_callback_)(std::span<const uint8_t>(param->write.value, param->write.len),
81 param->write.conn_id);
82 }
83 break;
84 }
85 default:
86 break;
87 }
88}
89
90} // namespace esp32_ble_server
91} // namespace esphome
92
93#endif
T * allocate(size_t n)
Definition helpers.h:1104
std::string to_string() 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