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 esph_log_w(
"ble_write_action",
"Characteristic %s was not found in service %s",
190 this->char_uuid_.
to_string().c_str(), this->service_uuid_.to_string().c_str());
193 this->char_handle_ = chr->handle;
194 this->char_props_ = chr->properties;
195 if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
196 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
198 }
else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
199 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
205 this->
node_state = espbt::ClientState::ESTABLISHED;
219 std::vector<uint8_t> (*func)(Ts...);
224 std::tuple<Ts...> var_{};
225 uint16_t char_handle_{};
226 esp_gatt_char_prop_t char_props_{};
227 esp_gatt_write_type_t write_type_{};
234 void play(
const Ts &...
x)
override {
236 if (has_simple_value_) {
237 passkey = this->value_.simple;
239 passkey = this->value_.template_func(
x...);
241 if (passkey > 999999)
243 esp_bd_addr_t remote_bda;
244 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
245 esp_ble_passkey_reply(remote_bda,
true, passkey);
249 this->value_.template_func = func;
250 this->has_simple_value_ =
false;
254 this->value_.simple = value;
255 this->has_simple_value_ =
true;
260 bool has_simple_value_ =
true;
264 } value_{.simple = 0};
271 void play(
const Ts &...
x)
override {
272 esp_bd_addr_t remote_bda;
273 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
274 if (has_simple_value_) {
275 esp_ble_confirm_reply(remote_bda, this->value_.simple);
277 esp_ble_confirm_reply(remote_bda, this->value_.template_func(
x...));
282 this->value_.template_func = func;
283 this->has_simple_value_ =
false;
287 this->value_.simple = value;
288 this->has_simple_value_ =
true;
293 bool has_simple_value_ =
true;
297 } value_{.simple =
false};
304 void play(
const Ts &...
x)
override {
305 esp_bd_addr_t remote_bda;
306 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
307 esp_ble_remove_bond_device(remote_bda);
318 ble_client_ = ble_client;
321 esp_ble_gattc_cb_param_t *param)
override {
325 case ESP_GATTC_SEARCH_CMPL_EVT:
326 this->
node_state = espbt::ClientState::ESTABLISHED;
330 case ESP_GATTC_DISCONNECT_EVT:
339 void play(
const Ts &...
x)
override {}
350 if (this->
node_state == espbt::ClientState::ESTABLISHED) {
353 this->var_ = std::make_tuple(
x...);
360 std::tuple<Ts...> var_{};
367 ble_client_ = ble_client;
370 esp_ble_gattc_cb_param_t *param)
override {
374 case ESP_GATTC_CLOSE_EVT:
375 case ESP_GATTC_DISCONNECT_EVT:
384 void play(
const Ts &...
x)
override {}
388 if (this->
node_state == espbt::ClientState::IDLE) {
391 this->var_ = std::make_tuple(
x...);
398 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)
std::string to_string() 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".