ESPHome  2023.11.6
ble_client_base.cpp
Go to the documentation of this file.
1 #include "ble_client_base.h"
2 
3 #include "esphome/core/helpers.h"
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 static const esp_bt_uuid_t NOTIFY_DESC_UUID = {
13  .len = ESP_UUID_LEN_16,
14  .uuid =
15  {
16  .uuid16 = ESP_GATT_UUID_CHAR_CLIENT_CONFIG,
17  },
18 };
19 
21  static uint8_t connection_index = 0;
22  this->connection_index_ = connection_index++;
23 }
24 
26  if (!esp32_ble::global_ble->is_active()) {
28  return;
29  }
30  if (this->state_ == espbt::ClientState::INIT) {
31  auto ret = esp_ble_gattc_app_register(this->app_id);
32  if (ret) {
33  ESP_LOGE(TAG, "gattc app register failed. app_id=%d code=%d", this->app_id, ret);
34  this->mark_failed();
35  }
37  }
38  // READY_TO_CONNECT means we have discovered the device
39  // and the scanner has been stopped by the tracker.
40  if (this->state_ == espbt::ClientState::READY_TO_CONNECT) {
41  this->connect();
42  }
43 }
44 
46 
48  if (this->address_ == 0 || device.address_uint64() != this->address_)
49  return false;
50  if (this->state_ != espbt::ClientState::IDLE && this->state_ != espbt::ClientState::SEARCHING)
51  return false;
52 
53  ESP_LOGD(TAG, "[%d] [%s] Found device", this->connection_index_, this->address_str_.c_str());
54  this->set_state(espbt::ClientState::DISCOVERED);
55 
56  auto addr = device.address_uint64();
57  this->remote_bda_[0] = (addr >> 40) & 0xFF;
58  this->remote_bda_[1] = (addr >> 32) & 0xFF;
59  this->remote_bda_[2] = (addr >> 24) & 0xFF;
60  this->remote_bda_[3] = (addr >> 16) & 0xFF;
61  this->remote_bda_[4] = (addr >> 8) & 0xFF;
62  this->remote_bda_[5] = (addr >> 0) & 0xFF;
63  this->remote_addr_type_ = device.get_address_type();
64  return true;
65 }
66 
68  ESP_LOGI(TAG, "[%d] [%s] 0x%02x Attempting BLE connection", this->connection_index_, this->address_str_.c_str(),
69  this->remote_addr_type_);
70  this->paired_ = false;
71  auto ret = esp_ble_gattc_open(this->gattc_if_, this->remote_bda_, this->remote_addr_type_, true);
72  if (ret) {
73  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_open error, status=%d", this->connection_index_, this->address_str_.c_str(),
74  ret);
76  } else {
77  this->set_state(espbt::ClientState::CONNECTING);
78  }
79 }
80 
81 esp_err_t BLEClientBase::pair() { return esp_ble_set_encryption(this->remote_bda_, ESP_BLE_SEC_ENCRYPT); }
82 
84  if (this->state_ == espbt::ClientState::IDLE || this->state_ == espbt::ClientState::DISCONNECTING)
85  return;
86  ESP_LOGI(TAG, "[%d] [%s] Disconnecting.", this->connection_index_, this->address_str_.c_str());
87  auto err = esp_ble_gattc_close(this->gattc_if_, this->conn_id_);
88  if (err != ESP_OK) {
89  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_close error, err=%d", this->connection_index_, this->address_str_.c_str(),
90  err);
91  }
92 
93  if (this->state_ == espbt::ClientState::SEARCHING || this->state_ == espbt::ClientState::READY_TO_CONNECT ||
94  this->state_ == espbt::ClientState::DISCOVERED) {
95  this->set_address(0);
97  } else {
98  this->set_state(espbt::ClientState::DISCONNECTING);
99  }
100 }
101 
103  for (auto &svc : this->services_)
104  delete svc; // NOLINT(cppcoreguidelines-owning-memory)
105  this->services_.clear();
106 #ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH
107  esp_ble_gattc_cache_clean(this->remote_bda_);
108 #endif
109 }
110 
111 bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
112  esp_ble_gattc_cb_param_t *param) {
113  if (event == ESP_GATTC_REG_EVT && this->app_id != param->reg.app_id)
114  return false;
115  if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->gattc_if_)
116  return false;
117 
118  ESP_LOGV(TAG, "[%d] [%s] gattc_event_handler: event=%d gattc_if=%d", this->connection_index_,
119  this->address_str_.c_str(), event, esp_gattc_if);
120 
121  switch (event) {
122  case ESP_GATTC_REG_EVT: {
123  if (param->reg.status == ESP_GATT_OK) {
124  ESP_LOGV(TAG, "[%d] [%s] gattc registered app id %d", this->connection_index_, this->address_str_.c_str(),
125  this->app_id);
126  this->gattc_if_ = esp_gattc_if;
127  } else {
128  ESP_LOGE(TAG, "[%d] [%s] gattc app registration failed id=%d code=%d", this->connection_index_,
129  this->address_str_.c_str(), param->reg.app_id, param->reg.status);
130  }
131  break;
132  }
133  case ESP_GATTC_OPEN_EVT: {
134  ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_OPEN_EVT", this->connection_index_, this->address_str_.c_str());
135  this->conn_id_ = param->open.conn_id;
136  this->service_count_ = 0;
137  if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
138  ESP_LOGW(TAG, "[%d] [%s] Connection failed, status=%d", this->connection_index_, this->address_str_.c_str(),
139  param->open.status);
141  break;
142  }
143  auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if_, param->open.conn_id);
144  if (ret) {
145  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_send_mtu_req failed, status=%x", this->connection_index_,
146  this->address_str_.c_str(), ret);
147  }
148  if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
149  ESP_LOGI(TAG, "[%d] [%s] Connected", this->connection_index_, this->address_str_.c_str());
151  this->state_ = espbt::ClientState::ESTABLISHED;
152  break;
153  }
154  esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id, nullptr);
155  break;
156  }
157  case ESP_GATTC_CFG_MTU_EVT: {
158  if (param->cfg_mtu.status != ESP_GATT_OK) {
159  ESP_LOGW(TAG, "[%d] [%s] cfg_mtu failed, mtu %d, status %d", this->connection_index_,
160  this->address_str_.c_str(), param->cfg_mtu.mtu, param->cfg_mtu.status);
162  break;
163  }
164  ESP_LOGV(TAG, "[%d] [%s] cfg_mtu status %d, mtu %d", this->connection_index_, this->address_str_.c_str(),
165  param->cfg_mtu.status, param->cfg_mtu.mtu);
166  this->mtu_ = param->cfg_mtu.mtu;
167  break;
168  }
169  case ESP_GATTC_DISCONNECT_EVT: {
170  if (memcmp(param->disconnect.remote_bda, this->remote_bda_, 6) != 0)
171  return false;
172  ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_DISCONNECT_EVT, reason %d", this->connection_index_,
173  this->address_str_.c_str(), param->disconnect.reason);
174  this->release_services();
176  break;
177  }
178  case ESP_GATTC_SEARCH_RES_EVT: {
179  this->service_count_++;
180  if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
181  // V3 clients don't need services initialized since
182  // they only request by handle after receiving the services.
183  break;
184  }
185  BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory)
186  ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
187  ble_service->start_handle = param->search_res.start_handle;
188  ble_service->end_handle = param->search_res.end_handle;
189  ble_service->client = this;
190  this->services_.push_back(ble_service);
191  break;
192  }
193  case ESP_GATTC_SEARCH_CMPL_EVT: {
194  ESP_LOGV(TAG, "[%d] [%s] ESP_GATTC_SEARCH_CMPL_EVT", this->connection_index_, this->address_str_.c_str());
195  for (auto &svc : this->services_) {
196  ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(),
197  svc->uuid.to_string().c_str());
198  ESP_LOGV(TAG, "[%d] [%s] start_handle: 0x%x end_handle: 0x%x", this->connection_index_,
199  this->address_str_.c_str(), svc->start_handle, svc->end_handle);
200  }
201  ESP_LOGI(TAG, "[%d] [%s] Connected", this->connection_index_, this->address_str_.c_str());
203  this->state_ = espbt::ClientState::ESTABLISHED;
204  break;
205  }
206  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
207  if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
208  this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
209  // Client is responsible for flipping the descriptor value
210  // when using the cache
211  break;
212  }
213  esp_gattc_descr_elem_t desc_result;
214  uint16_t count = 1;
215  esp_gatt_status_t descr_status =
216  esp_ble_gattc_get_descr_by_char_handle(this->gattc_if_, this->connection_index_, param->reg_for_notify.handle,
217  NOTIFY_DESC_UUID, &desc_result, &count);
218  if (descr_status != ESP_GATT_OK) {
219  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_descr_by_char_handle error, status=%d", this->connection_index_,
220  this->address_str_.c_str(), descr_status);
221  break;
222  }
223  esp_gattc_char_elem_t char_result;
224  esp_gatt_status_t char_status =
225  esp_ble_gattc_get_all_char(this->gattc_if_, this->connection_index_, param->reg_for_notify.handle,
226  param->reg_for_notify.handle, &char_result, &count, 0);
227  if (char_status != ESP_GATT_OK) {
228  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_,
229  this->address_str_.c_str(), char_status);
230  break;
231  }
232 
233  /*
234  1 = notify
235  2 = indicate
236  */
237  uint16_t notify_en = char_result.properties & ESP_GATT_CHAR_PROP_BIT_NOTIFY ? 1 : 2;
238  esp_err_t status =
239  esp_ble_gattc_write_char_descr(this->gattc_if_, this->conn_id_, desc_result.handle, sizeof(notify_en),
240  (uint8_t *) &notify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
241  if (status) {
242  ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char_descr error, status=%d", this->connection_index_,
243  this->address_str_.c_str(), status);
244  }
245  break;
246  }
247 
248  default:
249  break;
250  }
251  return true;
252 }
253 
254 void BLEClientBase::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
255  switch (event) {
256  // This event is sent by the server when it requests security
257  case ESP_GAP_BLE_SEC_REQ_EVT:
258  if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
259  break;
260  ESP_LOGV(TAG, "[%d] [%s] ESP_GAP_BLE_SEC_REQ_EVT %x", this->connection_index_, this->address_str_.c_str(), event);
261  esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
262  break;
263  // This event is sent once authentication has completed
264  case ESP_GAP_BLE_AUTH_CMPL_EVT:
265  if (memcmp(param->ble_security.auth_cmpl.bd_addr, this->remote_bda_, 6) != 0)
266  break;
267  esp_bd_addr_t bd_addr;
268  memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t));
269  ESP_LOGI(TAG, "[%d] [%s] auth complete. remote BD_ADDR: %s", this->connection_index_, this->address_str_.c_str(),
270  format_hex(bd_addr, 6).c_str());
271  if (!param->ble_security.auth_cmpl.success) {
272  ESP_LOGE(TAG, "[%d] [%s] auth fail reason = 0x%x", this->connection_index_, this->address_str_.c_str(),
273  param->ble_security.auth_cmpl.fail_reason);
274  } else {
275  this->paired_ = true;
276  ESP_LOGV(TAG, "[%d] [%s] auth success. address type = %d auth mode = %d", this->connection_index_,
277  this->address_str_.c_str(), param->ble_security.auth_cmpl.addr_type,
278  param->ble_security.auth_cmpl.auth_mode);
279  }
280  break;
281  // There are other events we'll want to implement at some point to support things like pass key
282  // https://github.com/espressif/esp-idf/blob/cba69dd088344ed9d26739f04736ae7a37541b3a/examples/bluetooth/bluedroid/ble/gatt_security_client/tutorial/Gatt_Security_Client_Example_Walkthrough.md
283  default:
284  break;
285  }
286 }
287 
288 // Parse GATT values into a float for a sensor.
289 // Ref: https://www.bluetooth.com/specifications/assigned-numbers/format-types/
290 float BLEClientBase::parse_char_value(uint8_t *value, uint16_t length) {
291  // A length of one means a single octet value.
292  if (length == 0)
293  return 0;
294  if (length == 1)
295  return (float) ((uint8_t) value[0]);
296 
297  switch (value[0]) {
298  case 0x1: // boolean.
299  case 0x2: // 2bit.
300  case 0x3: // nibble.
301  case 0x4: // uint8.
302  return (float) ((uint8_t) value[1]);
303  case 0x5: // uint12.
304  case 0x6: // uint16.
305  if (length > 2) {
306  return (float) encode_uint16(value[1], value[2]);
307  }
308  // fall through
309  case 0x7: // uint24.
310  if (length > 3) {
311  return (float) encode_uint24(value[1], value[2], value[3]);
312  }
313  // fall through
314  case 0x8: // uint32.
315  if (length > 4) {
316  return (float) encode_uint32(value[1], value[2], value[3], value[4]);
317  }
318  // fall through
319  case 0xC: // int8.
320  return (float) ((int8_t) value[1]);
321  case 0xD: // int12.
322  case 0xE: // int16.
323  if (length > 2) {
324  return (float) ((int16_t) (value[1] << 8) + (int16_t) value[2]);
325  }
326  // fall through
327  case 0xF: // int24.
328  if (length > 3) {
329  return (float) ((int32_t) (value[1] << 16) + (int32_t) (value[2] << 8) + (int32_t) (value[3]));
330  }
331  // fall through
332  case 0x10: // int32.
333  if (length > 4) {
334  return (float) ((int32_t) (value[1] << 24) + (int32_t) (value[2] << 16) + (int32_t) (value[3] << 8) +
335  (int32_t) (value[4]));
336  }
337  }
338  ESP_LOGW(TAG, "[%d] [%s] Cannot parse characteristic value of type 0x%x length %d", this->connection_index_,
339  this->address_str_.c_str(), value[0], length);
340  return NAN;
341 }
342 
344  for (auto *svc : this->services_) {
345  if (svc->uuid == uuid)
346  return svc;
347  }
348  return nullptr;
349 }
350 
352 
354  auto *svc = this->get_service(service);
355  if (svc == nullptr)
356  return nullptr;
357  return svc->get_characteristic(chr);
358 }
359 
360 BLECharacteristic *BLEClientBase::get_characteristic(uint16_t service, uint16_t chr) {
362 }
363 
365  for (auto *svc : this->services_) {
366  if (!svc->parsed)
367  svc->parse_characteristics();
368  for (auto *chr : svc->characteristics) {
369  if (chr->handle == handle)
370  return chr;
371  }
372  }
373  return nullptr;
374 }
375 
377  auto *chr = this->get_characteristic(handle);
378  if (chr != nullptr) {
379  if (!chr->parsed)
380  chr->parse_descriptors();
381  for (auto &desc : chr->descriptors) {
382  if (desc->uuid.get_uuid().uuid.uuid16 == ESP_GATT_UUID_CHAR_CLIENT_CONFIG)
383  return desc;
384  }
385  }
386  return nullptr;
387 }
388 
390  auto *svc = this->get_service(service);
391  if (svc == nullptr)
392  return nullptr;
393  auto *ch = svc->get_characteristic(chr);
394  if (ch == nullptr)
395  return nullptr;
396  return ch->get_descriptor(descr);
397 }
398 
399 BLEDescriptor *BLEClientBase::get_descriptor(uint16_t service, uint16_t chr, uint16_t descr) {
402 }
403 
405  for (auto *svc : this->services_) {
406  if (!svc->parsed)
407  svc->parse_characteristics();
408  for (auto *chr : svc->characteristics) {
409  if (!chr->parsed)
410  chr->parse_descriptors();
411  for (auto *desc : chr->descriptors) {
412  if (desc->handle == handle)
413  return desc;
414  }
415  }
416  }
417  return nullptr;
418 }
419 
420 } // namespace esp32_ble_client
421 } // namespace esphome
422 
423 #endif // USE_ESP32
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
BLEService * get_service(espbt::ESPBTUUID uuid)
ESP32BLE * global_ble
Definition: ble.cpp:394
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition: helpers.cpp:338
std::vector< BLEService * > services_
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition: helpers.h:186
const float AFTER_BLUETOOTH
Definition: component.cpp:21
BLEDescriptor * get_config_descriptor(uint16_t handle)
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)
Definition: ble_uuid.cpp:91
virtual void set_state(ClientState st)
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: ble_uuid.cpp:16
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition: helpers.h:191
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition: helpers.h:182
esp_ble_addr_type_t get_address_type() const
uint8_t status
Definition: bl0942.h:23
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
uint16_t length
Definition: tt21100.cpp:12
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
float parse_char_value(uint8_t *value, uint16_t length)