ESPHome  2025.2.0
ble_descriptor.cpp
Go to the documentation of this file.
1 #include "ble_descriptor.h"
2 #include "ble_characteristic.h"
3 #include "ble_service.h"
4 #include "esphome/core/log.h"
5 
6 #include <cstring>
7 
8 #ifdef USE_ESP32
9 
10 namespace esphome {
11 namespace esp32_ble_server {
12 
13 static const char *const TAG = "esp32_ble_server.descriptor";
14 
15 static RAMAllocator<uint8_t> descriptor_allocator{}; // NOLINT
16 
17 BLEDescriptor::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 
30 BLEDescriptor::~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 
49 void BLEDescriptor::set_value(std::vector<uint8_t> buffer) {
50  size_t length = buffer.size();
51 
52  if (length > this->value_.attr_max_len) {
53  ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
54  return;
55  }
56  this->value_.attr_len = length;
57  memcpy(this->value_.attr_value, buffer.data(), length);
58 }
59 
60 void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
61  esp_ble_gatts_cb_param_t *param) {
62  switch (event) {
63  case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
64  if (this->characteristic_ != nullptr && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
65  this->characteristic_->get_service()->get_handle() == param->add_char_descr.service_handle &&
67  this->handle_ = param->add_char_descr.attr_handle;
68  this->state_ = CREATED;
69  }
70  break;
71  }
72  case ESP_GATTS_WRITE_EVT: {
73  if (this->handle_ != param->write.handle)
74  break;
75  this->value_.attr_len = param->write.len;
76  memcpy(this->value_.attr_value, param->write.value, param->write.len);
78  std::vector<uint8_t>(param->write.value, param->write.value + param->write.len),
79  param->write.conn_id);
80  break;
81  }
82  default:
83  break;
84  }
85 }
86 
87 } // namespace esp32_ble_server
88 } // namespace esphome
89 
90 #endif
BLEDescriptor(ESPBTUUID uuid, uint16_t max_len=100, bool read=true, bool write=true)
T * allocate(size_t n)
Definition: helpers.h:703
void set_value(std::vector< uint8_t > buffer)
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:97
BLECharacteristic * get_last_created_characteristic()
Definition: ble_service.h:36
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)
std::string to_string() const
Definition: ble_uuid.cpp:171
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:170