ESPHome  2023.11.6
automation.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32
2 
3 #include "automation.h"
4 
5 #include <esp_bt_defs.h>
6 #include <esp_gap_ble_api.h>
7 #include <esp_gattc_api.h>
8 
9 #include "esphome/core/log.h"
10 
11 namespace esphome {
12 namespace ble_client {
13 static const char *const TAG = "ble_client.automation";
14 
15 void BLEWriterClientNode::write(const std::vector<uint8_t> &value) {
16  if (this->node_state != espbt::ClientState::ESTABLISHED) {
17  ESP_LOGW(TAG, "Cannot write to BLE characteristic - not connected");
18  return;
19  } else if (this->ble_char_handle_ == 0) {
20  ESP_LOGW(TAG, "Cannot write to BLE characteristic - characteristic not found");
21  return;
22  }
23  esp_gatt_write_type_t write_type;
24  if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
25  write_type = ESP_GATT_WRITE_TYPE_RSP;
26  ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
27  } else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
28  write_type = ESP_GATT_WRITE_TYPE_NO_RSP;
29  ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
30  } else {
31  ESP_LOGE(TAG, "Characteristic %s does not allow writing", this->char_uuid_.to_string().c_str());
32  return;
33  }
34  ESP_LOGVV(TAG, "Will write %d bytes: %s", value.size(), format_hex_pretty(value).c_str());
35  esp_err_t err =
36  esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->ble_char_handle_,
37  value.size(), const_cast<uint8_t *>(value.data()), write_type, ESP_GATT_AUTH_REQ_NONE);
38  if (err != ESP_OK) {
39  ESP_LOGE(TAG, "Error writing to characteristic: %s!", esp_err_to_name(err));
40  }
41 }
42 
43 void BLEWriterClientNode::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
44  esp_ble_gattc_cb_param_t *param) {
45  switch (event) {
46  case ESP_GATTC_REG_EVT:
47  break;
48  case ESP_GATTC_OPEN_EVT:
49  this->node_state = espbt::ClientState::ESTABLISHED;
50  ESP_LOGD(TAG, "Connection established with %s", ble_client_->address_str().c_str());
51  break;
52  case ESP_GATTC_SEARCH_CMPL_EVT: {
53  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
54  if (chr == nullptr) {
55  ESP_LOGW("ble_write_action", "Characteristic %s was not found in service %s",
56  this->char_uuid_.to_string().c_str(), this->service_uuid_.to_string().c_str());
57  break;
58  }
59  this->ble_char_handle_ = chr->handle;
60  this->char_props_ = chr->properties;
61  this->node_state = espbt::ClientState::ESTABLISHED;
62  ESP_LOGD(TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
63  ble_client_->address_str().c_str());
64  break;
65  }
66  case ESP_GATTC_DISCONNECT_EVT:
68  this->ble_char_handle_ = 0;
69  ESP_LOGD(TAG, "Disconnected from %s", ble_client_->address_str().c_str());
70  break;
71  default:
72  break;
73  }
74 }
75 
76 } // namespace ble_client
77 } // namespace esphome
78 
79 #endif
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition: helpers.cpp:350
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: automation.cpp:43
void write(const std::vector< uint8_t > &value)
Definition: automation.cpp:15
std::string to_string() const
Definition: ble_uuid.cpp:165
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
espbt::ClientState node_state
Definition: ble_client.h:38