ESPHome  2023.9.3
ble_client.cpp
Go to the documentation of this file.
1 #include "ble_client.h"
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/log.h"
7 
8 #ifdef USE_ESP32
9 
10 namespace esphome {
11 namespace ble_client {
12 
13 static const char *const TAG = "ble_client";
14 
17  this->enabled = true;
18 }
19 
22  for (auto *node : this->nodes_)
23  node->loop();
24 }
25 
27  ESP_LOGCONFIG(TAG, "BLE Client:");
28  ESP_LOGCONFIG(TAG, " Address: %s", this->address_str().c_str());
29 }
30 
32  if (!this->enabled)
33  return false;
34  return BLEClientBase::parse_device(device);
35 }
36 
38  if (enabled == this->enabled)
39  return;
40  if (!enabled && this->state() != espbt::ClientState::IDLE) {
41  ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str().c_str());
42  auto ret = esp_ble_gattc_close(this->gattc_if_, this->conn_id_);
43  if (ret) {
44  ESP_LOGW(TAG, "esp_ble_gattc_close error, address=%s status=%d", this->address_str().c_str(), ret);
45  }
46  }
47  this->enabled = enabled;
48 }
49 
50 bool BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
51  esp_ble_gattc_cb_param_t *param) {
52  bool all_established = this->all_nodes_established_();
53 
54  if (!BLEClientBase::gattc_event_handler(event, esp_gattc_if, param))
55  return false;
56 
57  for (auto *node : this->nodes_)
58  node->gattc_event_handler(event, esp_gattc_if, param);
59 
60  // Delete characteristics after clients have used them to save RAM.
61  if (!all_established && this->all_nodes_established_()) {
62  for (auto &svc : this->services_)
63  delete svc; // NOLINT(cppcoreguidelines-owning-memory)
64  this->services_.clear();
65  }
66  return true;
67 }
68 
69 void BLEClient::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
71 
72  for (auto *node : this->nodes_)
73  node->gap_event_handler(event, param);
74 }
75 
78  for (auto &node : nodes_)
79  node->node_state = state;
80 }
81 
83  if (this->state() != espbt::ClientState::ESTABLISHED)
84  return false;
85  for (auto &node : nodes_) {
86  if (node->node_state != espbt::ClientState::ESTABLISHED)
87  return false;
88  }
89  return true;
90 }
91 
92 } // namespace ble_client
93 } // namespace esphome
94 
95 #endif
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
Definition: ble_client.cpp:50
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: ble_client.cpp:69
std::vector< BLEService * > services_
bool parse_device(const espbt::ESPBTDevice &device) override
Definition: ble_client.cpp:31
void set_enabled(bool enabled)
Definition: ble_client.cpp:37
virtual void set_state(ClientState st)
void set_state(espbt::ClientState state) override
Definition: ble_client.cpp:76
std::vector< BLEClientNode * > nodes_
Definition: ble_client.h:75
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool parse_device(const espbt::ESPBTDevice &device) override
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override