ESPHome  2022.9.1
ble_client.cpp
Go to the documentation of this file.
1 #include "esphome/core/log.h"
3 #include "esphome/core/helpers.h"
5 #include "ble_client.h"
6 
7 #ifdef USE_ESP32
8 
9 namespace esphome {
10 namespace ble_client {
11 
12 static const char *const TAG = "ble_client";
13 
15 
17  auto ret = esp_ble_gattc_app_register(this->app_id);
18  if (ret) {
19  ESP_LOGE(TAG, "gattc app register failed. app_id=%d code=%d", this->app_id, ret);
20  this->mark_failed();
21  }
23  this->enabled = true;
24 }
25 
27  if (this->state() == espbt::ClientState::DISCOVERED) {
28  this->connect();
29  }
30  for (auto *node : this->nodes_)
31  node->loop();
32 }
33 
35  ESP_LOGCONFIG(TAG, "BLE Client:");
36  ESP_LOGCONFIG(TAG, " Address: %s", this->address_str().c_str());
37 }
38 
40  if (!this->enabled)
41  return false;
42  if (device.address_uint64() != this->address)
43  return false;
44  if (this->state() != espbt::ClientState::IDLE)
45  return false;
46 
47  ESP_LOGD(TAG, "Found device at MAC address [%s]", device.address_str().c_str());
48  this->set_states_(espbt::ClientState::DISCOVERED);
49 
50  auto addr = device.address_uint64();
51  this->remote_bda[0] = (addr >> 40) & 0xFF;
52  this->remote_bda[1] = (addr >> 32) & 0xFF;
53  this->remote_bda[2] = (addr >> 24) & 0xFF;
54  this->remote_bda[3] = (addr >> 16) & 0xFF;
55  this->remote_bda[4] = (addr >> 8) & 0xFF;
56  this->remote_bda[5] = (addr >> 0) & 0xFF;
57  this->remote_addr_type = device.get_address_type();
58  return true;
59 }
60 
61 std::string BLEClient::address_str() const {
62  char buf[20];
63  sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", (uint8_t)(this->address >> 40) & 0xff,
64  (uint8_t)(this->address >> 32) & 0xff, (uint8_t)(this->address >> 24) & 0xff,
65  (uint8_t)(this->address >> 16) & 0xff, (uint8_t)(this->address >> 8) & 0xff,
66  (uint8_t)(this->address >> 0) & 0xff);
67  std::string ret;
68  ret = buf;
69  return ret;
70 }
71 
73  if (enabled == this->enabled)
74  return;
75  if (!enabled && this->state() != espbt::ClientState::IDLE) {
76  ESP_LOGI(TAG, "[%s] Disabling BLE client.", this->address_str().c_str());
77  auto ret = esp_ble_gattc_close(this->gattc_if, this->conn_id);
78  if (ret) {
79  ESP_LOGW(TAG, "esp_ble_gattc_close error, address=%s status=%d", this->address_str().c_str(), ret);
80  }
81  }
82  this->enabled = enabled;
83 }
84 
86  ESP_LOGI(TAG, "Attempting BLE connection to %s", this->address_str().c_str());
87  auto ret = esp_ble_gattc_open(this->gattc_if, this->remote_bda, this->remote_addr_type, true);
88  if (ret) {
89  ESP_LOGW(TAG, "esp_ble_gattc_open error, address=%s status=%d", this->address_str().c_str(), ret);
91  } else {
92  this->set_states_(espbt::ClientState::CONNECTING);
93  }
94 }
95 
96 void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t esp_gattc_if,
97  esp_ble_gattc_cb_param_t *param) {
98  if (event == ESP_GATTC_REG_EVT && this->app_id != param->reg.app_id)
99  return;
100  if (event != ESP_GATTC_REG_EVT && esp_gattc_if != ESP_GATT_IF_NONE && esp_gattc_if != this->gattc_if)
101  return;
102 
103  bool all_established = this->all_nodes_established_();
104 
105  switch (event) {
106  case ESP_GATTC_REG_EVT: {
107  if (param->reg.status == ESP_GATT_OK) {
108  ESP_LOGV(TAG, "gattc registered app id %d", this->app_id);
109  this->gattc_if = esp_gattc_if;
110  } else {
111  ESP_LOGE(TAG, "gattc app registration failed id=%d code=%d", param->reg.app_id, param->reg.status);
112  }
113  break;
114  }
115  case ESP_GATTC_OPEN_EVT: {
116  ESP_LOGV(TAG, "[%s] ESP_GATTC_OPEN_EVT", this->address_str().c_str());
117  this->conn_id = param->open.conn_id;
118  if (param->open.status != ESP_GATT_OK) {
119  ESP_LOGW(TAG, "connect to %s failed, status=%d", this->address_str().c_str(), param->open.status);
121  break;
122  }
123  break;
124  }
125  case ESP_GATTC_CONNECT_EVT: {
126  ESP_LOGV(TAG, "[%s] ESP_GATTC_CONNECT_EVT", this->address_str().c_str());
127  if (this->conn_id != param->connect.conn_id) {
128  ESP_LOGD(TAG, "[%s] Unexpected conn_id in CONNECT_EVT: param conn=%d, open conn=%d",
129  this->address_str().c_str(), param->connect.conn_id, this->conn_id);
130  }
131  auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if, param->connect.conn_id);
132  if (ret) {
133  ESP_LOGW(TAG, "esp_ble_gattc_send_mtu_req failed, status=%x", ret);
134  }
135  break;
136  }
137  case ESP_GATTC_CFG_MTU_EVT: {
138  if (param->cfg_mtu.status != ESP_GATT_OK) {
139  ESP_LOGW(TAG, "cfg_mtu to %s failed, mtu %d, status %d", this->address_str().c_str(), param->cfg_mtu.mtu,
140  param->cfg_mtu.status);
142  break;
143  }
144  ESP_LOGV(TAG, "cfg_mtu status %d, mtu %d", param->cfg_mtu.status, param->cfg_mtu.mtu);
145  esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id, nullptr);
146  break;
147  }
148  case ESP_GATTC_DISCONNECT_EVT: {
149  if (memcmp(param->disconnect.remote_bda, this->remote_bda, 6) != 0) {
150  return;
151  }
152  ESP_LOGV(TAG, "[%s] ESP_GATTC_DISCONNECT_EVT, reason %d", this->address_str().c_str(), param->disconnect.reason);
153  for (auto &svc : this->services_)
154  delete svc; // NOLINT(cppcoreguidelines-owning-memory)
155  this->services_.clear();
157  break;
158  }
159  case ESP_GATTC_SEARCH_RES_EVT: {
160  BLEService *ble_service = new BLEService(); // NOLINT(cppcoreguidelines-owning-memory)
161  ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
162  ble_service->start_handle = param->search_res.start_handle;
163  ble_service->end_handle = param->search_res.end_handle;
164  ble_service->client = this;
165  this->services_.push_back(ble_service);
166  break;
167  }
168  case ESP_GATTC_SEARCH_CMPL_EVT: {
169  ESP_LOGV(TAG, "[%s] ESP_GATTC_SEARCH_CMPL_EVT", this->address_str().c_str());
170  for (auto &svc : this->services_) {
171  ESP_LOGI(TAG, "Service UUID: %s", svc->uuid.to_string().c_str());
172  ESP_LOGI(TAG, " start_handle: 0x%x end_handle: 0x%x", svc->start_handle, svc->end_handle);
173  svc->parse_characteristics();
174  }
176  this->set_state(espbt::ClientState::ESTABLISHED);
177  break;
178  }
179  case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
180  auto *descr = this->get_config_descriptor(param->reg_for_notify.handle);
181  if (descr == nullptr) {
182  ESP_LOGW(TAG, "No descriptor found for notify of handle 0x%x", param->reg_for_notify.handle);
183  break;
184  }
185  if (descr->uuid.get_uuid().len != ESP_UUID_LEN_16 ||
186  descr->uuid.get_uuid().uuid.uuid16 != ESP_GATT_UUID_CHAR_CLIENT_CONFIG) {
187  ESP_LOGW(TAG, "Handle 0x%x (uuid %s) is not a client config char uuid", param->reg_for_notify.handle,
188  descr->uuid.to_string().c_str());
189  break;
190  }
191  uint16_t notify_en = 1;
192  auto status =
193  esp_ble_gattc_write_char_descr(this->gattc_if, this->conn_id, descr->handle, sizeof(notify_en),
194  (uint8_t *) &notify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
195  if (status) {
196  ESP_LOGW(TAG, "esp_ble_gattc_write_char_descr error, status=%d", status);
197  }
198  break;
199  }
200 
201  default:
202  break;
203  }
204  for (auto *node : this->nodes_)
205  node->gattc_event_handler(event, esp_gattc_if, param);
206 
207  // Delete characteristics after clients have used them to save RAM.
208  if (!all_established && this->all_nodes_established_()) {
209  for (auto &svc : this->services_)
210  delete svc; // NOLINT(cppcoreguidelines-owning-memory)
211  this->services_.clear();
212  }
213 }
214 
215 void BLEClient::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
216  switch (event) {
217  // This event is sent by the server when it requests security
218  case ESP_GAP_BLE_SEC_REQ_EVT:
219  ESP_LOGV(TAG, "ESP_GAP_BLE_SEC_REQ_EVT %x", event);
220  esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true);
221  break;
222  // This event is sent once authentication has completed
223  case ESP_GAP_BLE_AUTH_CMPL_EVT:
224  esp_bd_addr_t bd_addr;
225  memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t));
226  ESP_LOGI(TAG, "auth complete. remote BD_ADDR: %s", format_hex(bd_addr, 6).c_str());
227  if (!param->ble_security.auth_cmpl.success) {
228  ESP_LOGE(TAG, "auth fail reason = 0x%x", param->ble_security.auth_cmpl.fail_reason);
229  } else {
230  ESP_LOGV(TAG, "auth success. address type = %d auth mode = %d", param->ble_security.auth_cmpl.addr_type,
231  param->ble_security.auth_cmpl.auth_mode);
232  }
233  break;
234  // There are other events we'll want to implement at some point to support things like pass key
235  // https://github.com/espressif/esp-idf/blob/cba69dd088344ed9d26739f04736ae7a37541b3a/examples/bluetooth/bluedroid/ble/gatt_security_client/tutorial/Gatt_Security_Client_Example_Walkthrough.md
236  default:
237  break;
238  }
239 }
240 
241 // Parse GATT values into a float for a sensor.
242 // Ref: https://www.bluetooth.com/specifications/assigned-numbers/format-types/
243 float BLEClient::parse_char_value(uint8_t *value, uint16_t length) {
244  // A length of one means a single octet value.
245  if (length == 0)
246  return 0;
247  if (length == 1)
248  return (float) ((uint8_t) value[0]);
249 
250  switch (value[0]) {
251  case 0x1: // boolean.
252  case 0x2: // 2bit.
253  case 0x3: // nibble.
254  case 0x4: // uint8.
255  return (float) ((uint8_t) value[1]);
256  case 0x5: // uint12.
257  case 0x6: // uint16.
258  if (length > 2) {
259  return (float) ((uint16_t)(value[1] << 8) + (uint16_t) value[2]);
260  }
261  case 0x7: // uint24.
262  if (length > 3) {
263  return (float) ((uint32_t)(value[1] << 16) + (uint32_t)(value[2] << 8) + (uint32_t)(value[3]));
264  }
265  case 0x8: // uint32.
266  if (length > 4) {
267  return (float) ((uint32_t)(value[1] << 24) + (uint32_t)(value[2] << 16) + (uint32_t)(value[3] << 8) +
268  (uint32_t)(value[4]));
269  }
270  case 0xC: // int8.
271  return (float) ((int8_t) value[1]);
272  case 0xD: // int12.
273  case 0xE: // int16.
274  if (length > 2) {
275  return (float) ((int16_t)(value[1] << 8) + (int16_t) value[2]);
276  }
277  case 0xF: // int24.
278  if (length > 3) {
279  return (float) ((int32_t)(value[1] << 16) + (int32_t)(value[2] << 8) + (int32_t)(value[3]));
280  }
281  case 0x10: // int32.
282  if (length > 4) {
283  return (float) ((int32_t)(value[1] << 24) + (int32_t)(value[2] << 16) + (int32_t)(value[3] << 8) +
284  (int32_t)(value[4]));
285  }
286  }
287  ESP_LOGW(TAG, "Cannot parse characteristic value of type 0x%x length %d", value[0], length);
288  return NAN;
289 }
290 
292  for (auto *svc : this->services_) {
293  if (svc->uuid == uuid)
294  return svc;
295  }
296  return nullptr;
297 }
298 
300 
302  auto *svc = this->get_service(service);
303  if (svc == nullptr)
304  return nullptr;
305  return svc->get_characteristic(chr);
306 }
307 
308 BLECharacteristic *BLEClient::get_characteristic(uint16_t service, uint16_t chr) {
310 }
311 
313  for (auto &svc : this->services_) {
314  for (auto &chr : svc->characteristics) {
315  if (chr->handle == handle) {
316  for (auto &desc : chr->descriptors) {
317  if (desc->uuid == espbt::ESPBTUUID::from_uint16(0x2902))
318  return desc;
319  }
320  }
321  }
322  }
323  return nullptr;
324 }
325 
327  for (auto &chr : this->characteristics) {
328  if (chr->uuid == uuid)
329  return chr;
330  }
331  return nullptr;
332 }
333 
336 }
337 
339  auto *svc = this->get_service(service);
340  if (svc == nullptr)
341  return nullptr;
342  auto *ch = svc->get_characteristic(chr);
343  if (ch == nullptr)
344  return nullptr;
345  return ch->get_descriptor(descr);
346 }
347 
348 BLEDescriptor *BLEClient::get_descriptor(uint16_t service, uint16_t chr, uint16_t descr) {
351 }
352 
354  for (auto &chr : this->characteristics)
355  delete chr; // NOLINT(cppcoreguidelines-owning-memory)
356 }
357 
359  uint16_t offset = 0;
360  esp_gattc_char_elem_t result;
361 
362  while (true) {
363  uint16_t count = 1;
364  esp_gatt_status_t status = esp_ble_gattc_get_all_char(
365  this->client->gattc_if, this->client->conn_id, this->start_handle, this->end_handle, &result, &count, offset);
366  if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) {
367  break;
368  }
369  if (status != ESP_GATT_OK) {
370  ESP_LOGW(TAG, "esp_ble_gattc_get_all_char error, status=%d", status);
371  break;
372  }
373  if (count == 0) {
374  break;
375  }
376 
377  BLECharacteristic *characteristic = new BLECharacteristic(); // NOLINT(cppcoreguidelines-owning-memory)
378  characteristic->uuid = espbt::ESPBTUUID::from_uuid(result.uuid);
379  characteristic->properties = result.properties;
380  characteristic->handle = result.char_handle;
381  characteristic->service = this;
382  this->characteristics.push_back(characteristic);
383  ESP_LOGI(TAG, " characteristic %s, handle 0x%x, properties 0x%x", characteristic->uuid.to_string().c_str(),
384  characteristic->handle, characteristic->properties);
385  characteristic->parse_descriptors();
386  offset++;
387  }
388 }
389 
391  for (auto &desc : this->descriptors)
392  delete desc; // NOLINT(cppcoreguidelines-owning-memory)
393 }
394 
396  uint16_t offset = 0;
397  esp_gattc_descr_elem_t result;
398 
399  while (true) {
400  uint16_t count = 1;
401  esp_gatt_status_t status = esp_ble_gattc_get_all_descr(
402  this->service->client->gattc_if, this->service->client->conn_id, this->handle, &result, &count, offset);
403  if (status == ESP_GATT_INVALID_OFFSET || status == ESP_GATT_NOT_FOUND) {
404  break;
405  }
406  if (status != ESP_GATT_OK) {
407  ESP_LOGW(TAG, "esp_ble_gattc_get_all_descr error, status=%d", status);
408  break;
409  }
410  if (count == 0) {
411  break;
412  }
413 
414  BLEDescriptor *desc = new BLEDescriptor(); // NOLINT(cppcoreguidelines-owning-memory)
415  desc->uuid = espbt::ESPBTUUID::from_uuid(result.uuid);
416  desc->handle = result.handle;
417  desc->characteristic = this;
418  this->descriptors.push_back(desc);
419  ESP_LOGV(TAG, " descriptor %s, handle 0x%x", desc->uuid.to_string().c_str(), desc->handle);
420  offset++;
421  }
422 }
423 
425  for (auto &desc : this->descriptors) {
426  if (desc->uuid == uuid)
427  return desc;
428  }
429  return nullptr;
430 }
432  return this->get_descriptor(espbt::ESPBTUUID::from_uint16(uuid));
433 }
434 
435 void BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size, esp_gatt_write_type_t write_type) {
436  auto *client = this->service->client;
437  auto status = esp_ble_gattc_write_char(client->gattc_if, client->conn_id, this->handle, new_val_size, new_val,
438  write_type, ESP_GATT_AUTH_REQ_NONE);
439  if (status) {
440  ESP_LOGW(TAG, "Error sending write value to BLE gattc server, status=%d", status);
441  }
442 }
443 
444 void BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size) {
445  write_value(new_val, new_val_size, ESP_GATT_WRITE_TYPE_NO_RSP);
446 }
447 
448 } // namespace ble_client
449 } // namespace esphome
450 
451 #endif
float get_setup_priority() const override
Definition: ble_client.cpp:14
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
Definition: ble_client.cpp:338
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:202
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Definition: ble_client.cpp:301
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
Definition: ble_client.cpp:215
bool parse_device(const espbt::ESPBTDevice &device) override
Definition: ble_client.cpp:39
BLEDescriptor * get_config_descriptor(uint16_t handle)
Definition: ble_client.cpp:312
BLECharacteristic * characteristic
Definition: ble_client.h:50
void set_states_(espbt::ClientState st)
Definition: ble_client.h:125
const float AFTER_BLUETOOTH
Definition: component.cpp:21
void set_enabled(bool enabled)
Definition: ble_client.cpp:72
BLECharacteristic * get_characteristic(espbt::ESPBTUUID uuid)
Definition: ble_client.cpp:326
std::string address_str() const
Definition: ble_client.cpp:61
void 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:96
std::vector< BLEClientNode * > nodes_
Definition: ble_client.h:140
std::vector< BLEService * > services_
Definition: ble_client.h:141
float parse_char_value(uint8_t *value, uint16_t length)
Definition: ble_client.cpp:243
esp_ble_addr_type_t get_address_type() const
void write_value(uint8_t *new_val, int16_t new_val_size)
Definition: ble_client.cpp:444
BLEDescriptor * get_descriptor(espbt::ESPBTUUID uuid)
Definition: ble_client.cpp:424
uint8_t status
Definition: bl0942.h:23
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:111
static ESPBTUUID from_uint16(uint16_t uuid)
Definition: a4988.cpp:4
esp_ble_addr_type_t remote_addr_type
Definition: ble_client.h:118
BLEService * get_service(espbt::ESPBTUUID uuid)
Definition: ble_client.cpp:291
static ESPBTUUID from_uuid(esp_bt_uuid_t uuid)