ESPHome  2023.11.6
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_OPEN_EVT:
23  this->client_state_ = espbt::ClientState::ESTABLISHED;
24  ESP_LOGW(TAG, "[%s] Connected successfully!", this->char_uuid_.to_string().c_str());
25  break;
26  case ESP_GATTC_DISCONNECT_EVT:
27  ESP_LOGW(TAG, "[%s] Disconnected", this->char_uuid_.to_string().c_str());
29  break;
30  case ESP_GATTC_WRITE_CHAR_EVT: {
31  if (param->write.status == 0) {
32  break;
33  }
34 
35  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
36  if (chr == nullptr) {
37  ESP_LOGW(TAG, "[%s] Characteristic not found.", this->char_uuid_.to_string().c_str());
38  break;
39  }
40  if (param->write.handle == chr->handle) {
41  ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_string().c_str(), param->write.status);
42  }
43  break;
44  }
45  default:
46  break;
47  }
48 }
49 
51  if (this->client_state_ != espbt::ClientState::ESTABLISHED) {
52  ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
53  this->char_uuid_.to_string().c_str());
54  return;
55  }
56 
57  auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
58  if (chr == nullptr) {
59  ESP_LOGW(TAG, "[%s] Characteristic not found. State update can not be written.",
60  this->char_uuid_.to_string().c_str());
61  return;
62  }
63 
64  uint8_t state_as_uint = (uint8_t) state;
65  ESP_LOGV(TAG, "[%s] Write State: %d", this->char_uuid_.to_string().c_str(), state_as_uint);
66  if (this->require_response_) {
67  chr->write_value(&state_as_uint, sizeof(state_as_uint), ESP_GATT_WRITE_TYPE_RSP);
68  } else {
69  chr->write_value(&state_as_uint, sizeof(state_as_uint), ESP_GATT_WRITE_TYPE_NO_RSP);
70  }
71 }
72 
73 } // namespace ble_client
74 } // namespace esphome
75 #endif
void write_state(bool state) override
esp_err_t write_value(uint8_t *new_val, int16_t new_val_size)
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
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