ESPHome  2024.10.2
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  ESP_LOGCONFIG(TAG, " Auto-Connect: %s", TRUEFALSE(this->auto_connect_));
30 }
31 
33  if (!this->enabled)
34  return false;
35  return BLEClientBase::parse_device(device);
36 }
37 
39  if (enabled == this->enabled)
40  return;
41  this->enabled = enabled;
42  if (!enabled) {
43  ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str().c_str());
44  this->disconnect();
45  }
46 }
47 
48 bool BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
49  esp_ble_gattc_cb_param_t *param) {
50  if (!BLEClientBase::gattc_event_handler(event, esp_gattc_if, param))
51  return false;
52 
53  for (auto *node : this->nodes_)
54  node->gattc_event_handler(event, esp_gattc_if, param);
55 
56  if (!this->services_.empty() && this->all_nodes_established_()) {
57  this->release_services();
58  ESP_LOGD(TAG, "All clients established, services released");
59  }
60  return true;
61 }
62 
63 void BLEClient::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
65 
66  for (auto *node : this->nodes_)
67  node->gap_event_handler(event, param);
68 }
69 
72  for (auto &node : nodes_)
73  node->node_state = state;
74 }
75 
77  if (this->state() != espbt::ClientState::ESTABLISHED)
78  return false;
79  for (auto &node : nodes_) {
80  if (node->node_state != espbt::ClientState::ESTABLISHED)
81  return false;
82  }
83  return true;
84 }
85 
86 } // namespace ble_client
87 } // namespace esphome
88 
89 #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:48
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: ble_client.cpp:63
std::vector< BLEService * > services_
bool parse_device(const espbt::ESPBTDevice &device) override
Definition: ble_client.cpp:32
void set_enabled(bool enabled)
Definition: ble_client.cpp:38
virtual void set_state(ClientState st)
void set_state(espbt::ClientState state) override
Definition: ble_client.cpp:70
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