ESPHome  2024.3.1
ble_characteristic.cpp
Go to the documentation of this file.
1 #include "ble_characteristic.h"
2 #include "ble_client_base.h"
3 #include "ble_service.h"
4 
5 #include "esphome/core/log.h"
6 
7 #ifdef USE_ESP32
8 
9 namespace esphome {
10 namespace esp32_ble_client {
11 
12 static const char *const TAG = "esp32_ble_client";
13 
15  for (auto &desc : this->descriptors)
16  delete desc; // NOLINT(cppcoreguidelines-owning-memory)
17 }
18 
20  this->parsed = false;
21  for (auto &desc : this->descriptors)
22  delete desc; // NOLINT(cppcoreguidelines-owning-memory)
23  this->descriptors.clear();
24 }
25 
27  this->parsed = true;
28  uint16_t offset = 0;
29  esp_gattc_descr_elem_t result;
30 
31  while (true) {
32  uint16_t count = 1;
33  esp_gatt_status_t status =
34  esp_ble_gattc_get_all_descr(this->service->client->get_gattc_if(), this->service->client->get_conn_id(),
35  this->handle, &result, &count, offset);
36  if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) {
37  break;
38  }
39  if (status != ESP_GATT_OK) {
40  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d",
41  this->service->client->get_connection_index(), this->service->client->address_str().c_str(), status);
42  break;
43  }
44  if (count == 0) {
45  break;
46  }
47 
48  BLEDescriptor *desc = new BLEDescriptor(); // NOLINT(cppcoreguidelines-owning-memory)
49  desc->uuid = espbt::ESPBTUUID::from_uuid(result.uuid);
50  desc->handle = result.handle;
51  desc->characteristic = this;
52  this->descriptors.push_back(desc);
53  ESP_LOGV(TAG, "[%d] [%s] descriptor %s, handle 0x%x", this->service->client->get_connection_index(),
54  this->service->client->address_str().c_str(), desc->uuid.to_string().c_str(), desc->handle);
55  offset++;
56  }
57 }
58 
60  if (!this->parsed)
61  this->parse_descriptors();
62  for (auto &desc : this->descriptors) {
63  if (desc->uuid == uuid)
64  return desc;
65  }
66  return nullptr;
67 }
70 }
72  if (!this->parsed)
73  this->parse_descriptors();
74  for (auto &desc : this->descriptors) {
75  if (desc->handle == handle)
76  return desc;
77  }
78  return nullptr;
79 }
80 
81 esp_err_t BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size, esp_gatt_write_type_t write_type) {
82  auto *client = this->service->client;
83  auto status = esp_ble_gattc_write_char(client->get_gattc_if(), client->get_conn_id(), this->handle, new_val_size,
84  new_val, write_type, ESP_GATT_AUTH_REQ_NONE);
85  if (status) {
86  ESP_LOGW(TAG, "[%d] [%s] Error sending write value to BLE gattc server, status=%d",
87  this->service->client->get_connection_index(), this->service->client->address_str().c_str(), status);
88  }
89  return status;
90 }
91 
92 esp_err_t BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size) {
93  return write_value(new_val, new_val_size, ESP_GATT_WRITE_TYPE_NO_RSP);
94 }
95 
96 } // namespace esp32_ble_client
97 } // namespace esphome
98 
99 #endif // USE_ESP32
esp_err_t write_value(uint8_t *new_val, int16_t new_val_size)
BLEDescriptor * get_descriptor_by_handle(uint16_t handle)
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:91
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
BLEDescriptor * get_descriptor(espbt::ESPBTUUID uuid)
std::vector< BLEDescriptor * > descriptors
std::string to_string() const
Definition: ble_uuid.cpp:165
uint8_t status
Definition: bl0942.h:23
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7