ESPHome 2026.1.4
Loading...
Searching...
No Matches
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
6namespace esphome::ble_client {
7
8static const char *const TAG = "ble_binary_output";
9
11 ESP_LOGCONFIG(TAG, "BLE Binary Output:");
12 char service_buf[esp32_ble::UUID_STR_LEN];
13 char char_buf[esp32_ble::UUID_STR_LEN];
14 this->service_uuid_.to_str(service_buf);
15 this->char_uuid_.to_str(char_buf);
16 ESP_LOGCONFIG(TAG,
17 " MAC address : %s\n"
18 " Service UUID : %s\n"
19 " Characteristic UUID: %s",
20 this->parent_->address_str(), service_buf, char_buf);
21 LOG_BINARY_OUTPUT(this);
22}
23
24void BLEBinaryOutput::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
25 esp_ble_gattc_cb_param_t *param) {
26 switch (event) {
27 case ESP_GATTC_SEARCH_CMPL_EVT: {
28 auto *chr = this->parent()->get_characteristic(this->service_uuid_, this->char_uuid_);
29 if (chr == nullptr) {
30 char char_buf[esp32_ble::UUID_STR_LEN];
31 char service_buf[esp32_ble::UUID_STR_LEN];
32 ESP_LOGW(TAG, "Characteristic %s was not found in service %s", this->char_uuid_.to_str(char_buf),
33 this->service_uuid_.to_str(service_buf));
34 break;
35 }
36 this->char_handle_ = chr->handle;
37 this->char_props_ = chr->properties;
38 if (this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
39 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
40 ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_RSP");
41 } else if (!this->require_response_ && this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
42 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
43 ESP_LOGD(TAG, "Write type: ESP_GATT_WRITE_TYPE_NO_RSP");
44 } else {
45 char char_buf[esp32_ble::UUID_STR_LEN];
46 ESP_LOGE(TAG, "Characteristic %s does not allow writing with%s response", this->char_uuid_.to_str(char_buf),
47 this->require_response_ ? "" : "out");
48 break;
49 }
50 this->node_state = espbt::ClientState::ESTABLISHED;
51 char char_buf[esp32_ble::UUID_STR_LEN];
52 ESP_LOGD(TAG, "Found characteristic %s on device %s", this->char_uuid_.to_str(char_buf),
53 this->parent()->address_str());
54 this->node_state = espbt::ClientState::ESTABLISHED;
55 break;
56 }
57 case ESP_GATTC_WRITE_CHAR_EVT: {
58 if (param->write.handle == this->char_handle_) {
59 if (param->write.status != 0) {
60 char char_buf[esp32_ble::UUID_STR_LEN];
61 ESP_LOGW(TAG, "[%s] Write error, status=%d", this->char_uuid_.to_str(char_buf), param->write.status);
62 }
63 }
64 break;
65 }
66 default:
67 break;
68 }
69}
70
72 char char_buf[esp32_ble::UUID_STR_LEN];
73 if (this->node_state != espbt::ClientState::ESTABLISHED) {
74 ESP_LOGW(TAG, "[%s] Not connected to BLE client. State update can not be written.",
75 this->char_uuid_.to_str(char_buf));
76 return;
77 }
78 uint8_t state_as_uint = (uint8_t) state;
79 ESP_LOGV(TAG, "[%s] Write State: %d", this->char_uuid_.to_str(char_buf), state_as_uint);
80 esp_err_t err =
81 esp_ble_gattc_write_char(this->parent()->get_gattc_if(), this->parent()->get_conn_id(), this->char_handle_,
82 sizeof(state_as_uint), &state_as_uint, this->write_type_, ESP_GATT_AUTH_REQ_NONE);
83 if (err != ESP_GATT_OK)
84 ESP_LOGW(TAG, "[%s] Write error, err=%d", this->char_uuid_.to_str(char_buf), err);
85}
86
87} // namespace esphome::ble_client
88#endif
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
const char * to_str(std::span< char, UUID_STR_LEN > output) const
Definition ble_uuid.cpp:146
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
bool state
Definition fan.h:0