18 static const char *
const TAG;
27 esp_ble_gattc_cb_param_t *param)
override {
28 if (event == ESP_GATTC_SEARCH_CMPL_EVT) {
29 this->
node_state = espbt::ClientState::ESTABLISHED;
41 esp_ble_gattc_cb_param_t *param)
override {
45 case ESP_GATTC_SEARCH_CMPL_EVT: {
46 this->
node_state = espbt::ClientState::ESTABLISHED;
49 case ESP_GATTC_CLOSE_EVT: {
64 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
65 if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr))
74 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
75 if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr)) {
76 this->
trigger(param->ble_security.key_notif.passkey);
85 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
86 if (event == ESP_GAP_BLE_NC_REQ_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr)) {
87 this->
trigger(param->ble_security.key_notif.passkey);
97 ble_client_ = ble_client;
100 void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
101 void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
104 void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
105 void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
106 void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
109 this->value_.func = func;
115 this->value_.data = data;
119 void play(
const Ts &...
x)
override {}
123 this->var_ = std::make_tuple(
x...);
126 if (this->len_ >= 0) {
128 result = this->
write(this->value_.data, this->len_);
131 std::vector<uint8_t> value = this->value_.func(
x...);
132 result = this->
write(value);
150 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
151 esph_log_w(
Automation::TAG,
"Cannot write to BLE characteristic - not connected");
156 esp_ble_gattc_write_char(this->
parent()->get_gattc_if(), this->
parent()->get_conn_id(), this->char_handle_,
len,
157 const_cast<uint8_t *
>(data), this->write_type_, ESP_GATT_AUTH_REQ_NONE);
159 esph_log_e(
Automation::TAG,
"Error writing to characteristic: %s!", esp_err_to_name(err));
165 bool write(
const std::vector<uint8_t> &value) {
return this->
write(value.data(), value.size()); }
168 esp_ble_gattc_cb_param_t *param)
override {
170 case ESP_GATTC_WRITE_CHAR_EVT:
172 if (param->write.handle == this->char_handle_)
175 case ESP_GATTC_DISCONNECT_EVT:
179 case ESP_GATTC_SEARCH_CMPL_EVT: {
181 if (chr ==
nullptr) {
182 esph_log_w(
"ble_write_action",
"Characteristic %s was not found in service %s",
183 this->char_uuid_.
to_string().c_str(), this->service_uuid_.to_string().c_str());
186 this->char_handle_ = chr->handle;
187 this->char_props_ = chr->properties;
188 if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
189 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
191 }
else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
192 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
198 this->
node_state = espbt::ClientState::ESTABLISHED;
212 std::vector<uint8_t> (*func)(Ts...);
217 std::tuple<Ts...> var_{};
218 uint16_t char_handle_{};
219 esp_gatt_char_prop_t char_props_{};
220 esp_gatt_write_type_t write_type_{};
227 void play(
const Ts &...
x)
override {
229 if (has_simple_value_) {
230 passkey = this->value_.simple;
232 passkey = this->value_.template_func(
x...);
234 if (passkey > 999999)
236 esp_bd_addr_t remote_bda;
237 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
238 esp_ble_passkey_reply(remote_bda,
true, passkey);
242 this->value_.template_func = func;
243 this->has_simple_value_ =
false;
247 this->value_.simple = value;
248 this->has_simple_value_ =
true;
253 bool has_simple_value_ =
true;
257 } value_{.simple = 0};
264 void play(
const Ts &...
x)
override {
265 esp_bd_addr_t remote_bda;
266 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
267 if (has_simple_value_) {
268 esp_ble_confirm_reply(remote_bda, this->value_.simple);
270 esp_ble_confirm_reply(remote_bda, this->value_.template_func(
x...));
275 this->value_.template_func = func;
276 this->has_simple_value_ =
false;
280 this->value_.simple = value;
281 this->has_simple_value_ =
true;
286 bool has_simple_value_ =
true;
290 } value_{.simple =
false};
297 void play(
const Ts &...
x)
override {
298 esp_bd_addr_t remote_bda;
299 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
300 esp_ble_remove_bond_device(remote_bda);
311 ble_client_ = ble_client;
314 esp_ble_gattc_cb_param_t *param)
override {
318 case ESP_GATTC_SEARCH_CMPL_EVT:
319 this->
node_state = espbt::ClientState::ESTABLISHED;
323 case ESP_GATTC_DISCONNECT_EVT:
332 void play(
const Ts &...
x)
override {}
343 if (this->
node_state == espbt::ClientState::ESTABLISHED) {
346 this->var_ = std::make_tuple(
x...);
353 std::tuple<Ts...> var_{};
360 ble_client_ = ble_client;
363 esp_ble_gattc_cb_param_t *param)
override {
367 case ESP_GATTC_CLOSE_EVT:
368 case ESP_GATTC_DISCONNECT_EVT:
377 void play(
const Ts &...
x)
override {}
381 if (this->
node_state == espbt::ClientState::IDLE) {
384 this->var_ = std::make_tuple(
x...);
391 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)
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.