ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
ble_characteristic.cpp
Go to the documentation of this file.
2#include "ble_client_base.h"
3#include "ble_service.h"
4
5#include "esphome/core/log.h"
6
7#ifdef USE_ESP32
8#ifdef USE_ESP32_BLE_DEVICE
9
11
12static 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(), 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#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
54 char uuid_buf[espbt::UUID_STR_LEN];
55 desc->uuid.to_str(uuid_buf);
56 ESP_LOGV(TAG, "[%d] [%s] descriptor %s, handle 0x%x", this->service->client->get_connection_index(),
57 this->service->client->address_str(), uuid_buf, desc->handle);
58#endif
59 offset++;
60 }
61}
62
64 if (!this->parsed)
65 this->parse_descriptors();
66 for (auto &desc : this->descriptors) {
67 if (desc->uuid == uuid)
68 return desc;
69 }
70 return nullptr;
71}
73 return this->get_descriptor(espbt::ESPBTUUID::from_uint16(uuid));
74}
76 if (!this->parsed)
77 this->parse_descriptors();
78 for (auto &desc : this->descriptors) {
79 if (desc->handle == handle)
80 return desc;
81 }
82 return nullptr;
83}
84
85esp_err_t BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size, esp_gatt_write_type_t write_type) {
86 auto *client = this->service->client;
87 auto status = esp_ble_gattc_write_char(client->get_gattc_if(), client->get_conn_id(), this->handle, new_val_size,
88 new_val, write_type, ESP_GATT_AUTH_REQ_NONE);
89 if (status) {
90 ESP_LOGW(TAG, "[%d] [%s] Error sending write value to BLE gattc server, status=%d",
91 this->service->client->get_connection_index(), this->service->client->address_str(), status);
92 }
93 return status;
94}
95
96esp_err_t BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size) {
97 return write_value(new_val, new_val_size, ESP_GATT_WRITE_TYPE_NO_RSP);
98}
99
100} // namespace esphome::esp32_ble_client
101
102#endif // USE_ESP32_BLE_DEVICE
103#endif // USE_ESP32
uint8_t status
Definition bl0942.h:8
void to_str(std::span< char, UUID_STR_LEN > output) const
Definition ble_uuid.cpp:146
BLEDescriptor * get_descriptor(espbt::ESPBTUUID uuid)
BLEDescriptor * get_descriptor_by_handle(uint16_t handle)
esp_err_t write_value(uint8_t *new_val, int16_t new_val_size)