ESPHome  2024.11.1
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 BLEDescriptor::BLEDescriptor(ESPBTUUID uuid, uint16_t max_len) {
16  this->uuid_ = uuid;
17  this->value_.attr_len = 0;
18  this->value_.attr_max_len = max_len;
19  this->value_.attr_value = (uint8_t *) malloc(max_len); // NOLINT
20 }
21 
22 BLEDescriptor::~BLEDescriptor() { free(this->value_.attr_value); } // NOLINT
23 
25  this->characteristic_ = characteristic;
26  esp_attr_control_t control;
27  control.auto_rsp = ESP_GATT_AUTO_RSP;
28 
29  ESP_LOGV(TAG, "Creating descriptor - %s", this->uuid_.to_string().c_str());
30  esp_bt_uuid_t uuid = this->uuid_.get_uuid();
31  esp_err_t err = esp_ble_gatts_add_char_descr(this->characteristic_->get_service()->get_handle(), &uuid,
32  this->permissions_, &this->value_, &control);
33  if (err != ESP_OK) {
34  ESP_LOGE(TAG, "esp_ble_gatts_add_char_descr failed: %d", err);
35  this->state_ = FAILED;
36  return;
37  }
38  this->state_ = CREATING;
39 }
40 
41 void BLEDescriptor::set_value(const std::string &value) { this->set_value((uint8_t *) value.data(), value.length()); }
42 void BLEDescriptor::set_value(const uint8_t *data, size_t length) {
43  if (length > this->value_.attr_max_len) {
44  ESP_LOGE(TAG, "Size %d too large, must be no bigger than %d", length, this->value_.attr_max_len);
45  return;
46  }
47  this->value_.attr_len = length;
48  memcpy(this->value_.attr_value, data, length);
49 }
50 
51 void BLEDescriptor::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if,
52  esp_ble_gatts_cb_param_t *param) {
53  switch (event) {
54  case ESP_GATTS_ADD_CHAR_DESCR_EVT: {
55  if (this->characteristic_ != nullptr && this->uuid_ == ESPBTUUID::from_uuid(param->add_char_descr.descr_uuid) &&
56  this->characteristic_->get_service()->get_handle() == param->add_char_descr.service_handle &&
58  this->handle_ = param->add_char_descr.attr_handle;
59  this->state_ = CREATED;
60  }
61  break;
62  }
63  case ESP_GATTS_WRITE_EVT: {
64  if (this->handle_ == param->write.handle) {
65  this->value_.attr_len = param->write.len;
66  memcpy(this->value_.attr_value, param->write.value, param->write.len);
67  }
68  break;
69  }
70  default:
71  break;
72  }
73 }
74 
75 } // namespace esp32_ble_server
76 } // namespace esphome
77 
78 #endif
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:98
BLECharacteristic * get_last_created_characteristic()
Definition: ble_service.h:35
void set_value(const std::string &value)
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:172
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
BLEDescriptor(ESPBTUUID uuid, uint16_t max_len=100)
esp_bt_uuid_t get_uuid() const
Definition: ble_uuid.cpp:171