ESPHome  2024.12.2
ble_binary_output.cpp
Go to the documentation of this file.
1 #include "ble_binary_output.h"
2 #include "esphome/core/log.h"
4 
5 #ifdef USE_ESP32
6 namespace esphome {
7 namespace ble_client {
8 
9 static const char *const TAG = "ble_binary_output";
10 
12  ESP_LOGCONFIG(TAG, "BLE Binary Output:");
13  ESP_LOGCONFIG(TAG, " MAC address : %s", this->parent_->address_str().c_str());
14  ESP_LOGCONFIG(TAG, " Service UUID : %s", this->service_uuid_.to_string().c_str());
15  ESP_LOGCONFIG(TAG, " Characteristic UUID: %s", this->char_uuid_.to_string().c_str());
16  LOG_BINARY_OUTPUT(this);
17 }
18 
19 void BLEBinaryOutput::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
20  esp_ble_gattc_cb_param_t *param) {
21  switch (event) {
22  case ESP_GATTC_SEARCH_CMPL_EVT: {
23  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
24  if (chr == nullptr) {
25  ESP_LOGW(TAG, "Characteristic %s was not found in service %s", this->char_uuid_.to_string().c_str(),
26  this->service_uuid_.to_string().c_str());
27  break;
28  }
29  this->char_handle_ = chr->handle;
30  this->char_props_ = chr->properties;
31  if (this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
32  this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
33  ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
34  } else if (!this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
35  this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
36  ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
37  } else {
38  ESP_LOGE(TAG, "Characteristic %s does not allow writing with%s response", this->char_uuid_.to_string().c_str(),
39  this->require_response_ ? "" : "out");
40  break;
41  }
42  this->node_state = espbt::ClientState::ESTABLISHED;
43  ESP_LOGD(TAG, "Found characteristic %s on device %s", this->char_uuid_.to_string().c_str(),
44  this->parent()->address_str().c_str());
45  this->node_state = espbt::ClientState::ESTABLISHED;
46  break;
47  }
48  case ESP_GATTC_WRITE_CHAR_EVT: {
49  if (param->write.handle == this->char_handle_) {
50  if (param->write.status != 0)
51  ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_string().c_str(), param->write.status);
52  }
53  break;
54  }
55  default:
56  break;
57  }
58 }
59 
61  if (this->node_state != espbt::ClientState::ESTABLISHED) {
62  ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
63  this->char_uuid_.to_string().c_str());
64  return;
65  }
66  uint8_t state_as_uint = (uint8_t) state;
67  ESP_LOGV(TAG, "[%s] Write State: %d", this->char_uuid_.to_string().c_str(), state_as_uint);
68  esp_err_t err =
69  esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->char_handle_,
70  sizeof(state_as_uint), &state_as_uint, this->write_type_, ESP_GATT_AUTH_REQ_NONE);
71  if (err != ESP_GATT_OK)
72  ESP_LOGW(TAG, "[%s] Write error, err=%d", this->char_uuid_.to_string().c_str(), err);
73 }
74 
75 } // namespace ble_client
76 } // namespace esphome
77 #endif
void write_state(bool state) override
std::string to_string() const
Definition: ble_uuid.cpp:171
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool state
Definition: fan.h:34
espbt::ClientState node_state
Definition: ble_client.h:38