ESPHome  2024.11.0
ble_service.cpp
Go to the documentation of this file.
1 #include "ble_service.h"
2 #include "ble_client_base.h"
3 
4 #include "esphome/core/log.h"
5 
6 #ifdef USE_ESP32
7 
8 namespace esphome {
9 namespace esp32_ble_client {
10 
11 static const char *const TAG = "esp32_ble_client";
12 
14  if (!this->parsed)
15  this->parse_characteristics();
16  for (auto &chr : this->characteristics) {
17  if (chr->uuid == uuid)
18  return chr;
19  }
20  return nullptr;
21 }
22 
25 }
26 
28  for (auto &chr : this->characteristics)
29  delete chr; // NOLINT(cppcoreguidelines-owning-memory)
30 }
31 
33  this->parsed = false;
34  for (auto &chr : this->characteristics)
35  delete chr; // NOLINT(cppcoreguidelines-owning-memory)
36  this->characteristics.clear();
37 }
38 
40  this->parsed = true;
41  uint16_t offset = 0;
42  esp_gattc_char_elem_t result;
43 
44  while (true) {
45  uint16_t count = 1;
46  esp_gatt_status_t status =
47  esp_ble_gattc_get_all_char(this->client->get_gattc_if(), this->client->get_conn_id(), this->start_handle,
48  this->end_handle, &result, &count, offset);
49  if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) {
50  break;
51  }
52  if (status != ESP_GATT_OK) {
53  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->client->get_connection_index(),
54  this->client->address_str().c_str(), status);
55  break;
56  }
57  if (count == 0) {
58  break;
59  }
60 
61  BLECharacteristic *characteristic = new BLECharacteristic(); // NOLINT(cppcoreguidelines-owning-memory)
62  characteristic->uuid = espbt::ESPBTUUID::from_uuid(result.uuid);
63  characteristic->properties = result.properties;
64  characteristic->handle = result.char_handle;
65  characteristic->service = this;
66  this->characteristics.push_back(characteristic);
67  ESP_LOGV(TAG, "[%d] [%s] characteristic %s, handle 0x%x, properties 0x%x", this->client->get_connection_index(),
68  this->client->address_str().c_str(), characteristic->uuid.to_string().c_str(), characteristic->handle,
69  characteristic->properties);
70  offset++;
71  }
72 }
73 
74 } // namespace esp32_ble_client
75 } // namespace esphome
76 
77 #endif // USE_ESP32
BLECharacteristic * get_characteristic(espbt::ESPBTUUID uuid)
Definition: ble_service.cpp:13
std::vector< BLECharacteristic * > characteristics
Definition: ble_service.h:25
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:98
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
std::string to_string() const
Definition: ble_uuid.cpp:172
uint8_t status
Definition: bl0942.h:74
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7