14static constexpr size_t BLE_WRITE_MAX_LOG_BYTES = 64;
22 static const char *
const TAG;
31 esp_ble_gattc_cb_param_t *param)
override {
32 if (event == ESP_GATTC_SEARCH_CMPL_EVT) {
33 this->
node_state = espbt::ClientState::ESTABLISHED;
45 esp_ble_gattc_cb_param_t *param)
override {
49 case ESP_GATTC_SEARCH_CMPL_EVT: {
50 this->
node_state = espbt::ClientState::ESTABLISHED;
53 case ESP_GATTC_CLOSE_EVT: {
68 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
69 if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr))
78 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
79 if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr)) {
80 this->
trigger(param->ble_security.key_notif.passkey);
89 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
90 if (event == ESP_GAP_BLE_NC_REQ_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr)) {
91 this->
trigger(param->ble_security.key_notif.passkey);
101 ble_client_ = ble_client;
104 void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
105 void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
108 void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
109 void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
110 void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
113 this->value_.func = func;
119 this->value_.data = data;
123 void play(
const Ts &...
x)
override {}
127 this->var_ = std::make_tuple(
x...);
130 if (this->len_ >= 0) {
132 result = this->
write(this->value_.data, this->len_);
135 std::vector<uint8_t> value = this->value_.func(
x...);
136 result = this->
write(value);
154 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
155 esph_log_w(
Automation::TAG,
"Cannot write to BLE characteristic - not connected");
158#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
163 esp_ble_gattc_write_char(this->
parent()->get_gattc_if(), this->
parent()->get_conn_id(), this->char_handle_,
len,
164 const_cast<uint8_t *
>(data), this->write_type_, ESP_GATT_AUTH_REQ_NONE);
166 esph_log_e(
Automation::TAG,
"Error writing to characteristic: %s!", esp_err_to_name(err));
172 bool write(
const std::vector<uint8_t> &value) {
return this->
write(value.data(), value.size()); }
175 esp_ble_gattc_cb_param_t *param)
override {
177 case ESP_GATTC_WRITE_CHAR_EVT:
179 if (param->write.handle == this->char_handle_)
182 case ESP_GATTC_DISCONNECT_EVT:
186 case ESP_GATTC_SEARCH_CMPL_EVT: {
188 if (chr ==
nullptr) {
189 char char_buf[esp32_ble::UUID_STR_LEN];
190 char service_buf[esp32_ble::UUID_STR_LEN];
191 esph_log_w(
"ble_write_action",
"Characteristic %s was not found in service %s",
192 this->char_uuid_.
to_str(char_buf), this->service_uuid_.to_str(service_buf));
195 this->char_handle_ = chr->handle;
196 this->char_props_ = chr->properties;
197 if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
198 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
200 }
else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
201 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
204 char char_buf[esp32_ble::UUID_STR_LEN];
205 esph_log_e(
Automation::TAG,
"Characteristic %s does not allow writing", this->char_uuid_.
to_str(char_buf));
208 this->
node_state = espbt::ClientState::ESTABLISHED;
209 char char_buf[esp32_ble::UUID_STR_LEN];
210 esph_log_d(
Automation::TAG,
"Found characteristic %s on device %s", this->char_uuid_.
to_str(char_buf),
223 std::vector<uint8_t> (*func)(Ts...);
228 std::tuple<Ts...> var_{};
229 uint16_t char_handle_{};
230 esp_gatt_char_prop_t char_props_{};
231 esp_gatt_write_type_t write_type_{};
238 void play(
const Ts &...
x)
override {
240 if (has_simple_value_) {
241 passkey = this->value_.simple;
243 passkey = this->value_.template_func(
x...);
245 if (passkey > 999999)
247 esp_bd_addr_t remote_bda;
248 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
249 esp_ble_passkey_reply(remote_bda,
true, passkey);
253 this->value_.template_func = func;
254 this->has_simple_value_ =
false;
258 this->value_.simple = value;
259 this->has_simple_value_ =
true;
264 bool has_simple_value_ =
true;
268 } value_{.simple = 0};
275 void play(
const Ts &...
x)
override {
276 esp_bd_addr_t remote_bda;
277 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
278 if (has_simple_value_) {
279 esp_ble_confirm_reply(remote_bda, this->value_.simple);
281 esp_ble_confirm_reply(remote_bda, this->value_.template_func(
x...));
286 this->value_.template_func = func;
287 this->has_simple_value_ =
false;
291 this->value_.simple = value;
292 this->has_simple_value_ =
true;
297 bool has_simple_value_ =
true;
301 } value_{.simple =
false};
308 void play(
const Ts &...
x)
override {
309 esp_bd_addr_t remote_bda;
310 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
311 esp_ble_remove_bond_device(remote_bda);
322 ble_client_ = ble_client;
325 esp_ble_gattc_cb_param_t *param)
override {
329 case ESP_GATTC_SEARCH_CMPL_EVT:
330 this->
node_state = espbt::ClientState::ESTABLISHED;
334 case ESP_GATTC_DISCONNECT_EVT:
343 void play(
const Ts &...
x)
override {}
354 if (this->
node_state == espbt::ClientState::ESTABLISHED) {
357 this->var_ = std::make_tuple(
x...);
364 std::tuple<Ts...> var_{};
371 ble_client_ = ble_client;
374 esp_ble_gattc_cb_param_t *param)
override {
378 case ESP_GATTC_CLOSE_EVT:
379 case ESP_GATTC_DISCONNECT_EVT:
388 void play(
const Ts &...
x)
override {}
392 if (this->
node_state == espbt::ClientState::IDLE) {
395 this->var_ = std::make_tuple(
x...);
402 std::tuple<Ts...> var_{};
void play_next_(const Ts &...x)
virtual void stop_complex()
void play_next_tuple_(const std::tuple< Ts... > &tuple, std::index_sequence< S... >)
void trigger(const Ts &...x)
static const char *const TAG
void play_complex(const Ts &...x) override
BLEClientConnectAction(BLEClient *ble_client)
void play(const Ts &...x) override
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
BLEClientConnectTrigger(BLEClient *parent)
void play_complex(const Ts &...x) override
void play(const Ts &...x) override
BLEClientDisconnectAction(BLEClient *ble_client)
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
BLEClientDisconnectTrigger(BLEClient *parent)
void register_ble_node(BLEClientNode *node)
espbt::ClientState node_state
BLEClientNumericComparisonReplyAction(BLEClient *ble_client)
void set_value_template(bool(*func)(Ts...))
void set_value_simple(const bool &value)
void play(const Ts &...x) override
bool(* template_func)(Ts...)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
BLEClientNumericComparisonRequestTrigger(BLEClient *parent)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
BLEClientPasskeyNotificationTrigger(BLEClient *parent)
uint32_t(* template_func)(Ts...)
void play(const Ts &...x) override
void set_value_template(uint32_t(*func)(Ts...))
void set_value_simple(const uint32_t &value)
BLEClientPasskeyReplyAction(BLEClient *ble_client)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
BLEClientPasskeyRequestTrigger(BLEClient *parent)
void play(const Ts &...x) override
BLEClientRemoveBondAction(BLEClient *ble_client)
bool write(const uint8_t *data, size_t len)
Note about logging: the esph_log_X macros are used here because the CI checks complain about use of t...
void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
void set_service_uuid16(uint16_t uuid)
void set_value_template(std::vector< uint8_t >(*func)(Ts...))
void play(const Ts &...x) override
void set_char_uuid128(uint8_t *uuid)
void set_char_uuid32(uint32_t uuid)
void play_complex(const Ts &...x) override
void set_service_uuid128(uint8_t *uuid)
void set_service_uuid32(uint32_t uuid)
BLEClientWriteAction(BLEClient *ble_client)
bool write(const std::vector< uint8_t > &value)
void set_char_uuid16(uint16_t uuid)
void set_value_simple(const uint8_t *data, size_t len)
const char * to_str(std::span< char, UUID_STR_LEN > output) const
uint8_t * get_remote_bda()
const char * address_str() const
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
void disconnect() override
bool check_addr(esp_bd_addr_t &addr)
void run_later(std::function< void()> &&f)
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".