19 static const char *
const TAG;
28 esp_ble_gattc_cb_param_t *param)
override {
29 if (event == ESP_GATTC_SEARCH_CMPL_EVT) {
30 this->
node_state = espbt::ClientState::ESTABLISHED;
42 esp_ble_gattc_cb_param_t *param)
override {
46 case ESP_GATTC_SEARCH_CMPL_EVT: {
47 this->
node_state = espbt::ClientState::ESTABLISHED;
50 case ESP_GATTC_CLOSE_EVT: {
65 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
66 if (event == ESP_GAP_BLE_PASSKEY_REQ_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr))
75 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
76 if (event == ESP_GAP_BLE_PASSKEY_NOTIF_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr)) {
77 this->
trigger(param->ble_security.key_notif.passkey);
86 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
override {
87 if (event == ESP_GAP_BLE_NC_REQ_EVT && this->
parent_->
check_addr(param->ble_security.auth_cmpl.bd_addr)) {
88 this->
trigger(param->ble_security.key_notif.passkey);
98 ble_client_ = ble_client;
101 void set_service_uuid16(uint16_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
102 void set_service_uuid32(uint32_t uuid) { this->service_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
105 void set_char_uuid16(uint16_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint16(uuid); }
106 void set_char_uuid32(uint32_t uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_uint32(uuid); }
107 void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); }
110 this->value_.func = func;
116 this->value_.data = data;
120 void play(
const Ts &...
x)
override {}
124 this->var_ = std::make_tuple(
x...);
125 std::vector<uint8_t> value;
126 if (this->len_ >= 0) {
128 value.assign(this->value_.data, this->value_.data + this->len_);
131 value = this->value_.func(
x...);
147 bool write(
const std::vector<uint8_t> &value) {
148 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
149 esph_log_w(
Automation::TAG,
"Cannot write to BLE characteristic - not connected");
153 esp_err_t err = esp_ble_gattc_write_char(this->
parent()->get_gattc_if(), this->
parent()->get_conn_id(),
154 this->char_handle_, value.size(),
const_cast<uint8_t *
>(value.data()),
155 this->write_type_, ESP_GATT_AUTH_REQ_NONE);
157 esph_log_e(
Automation::TAG,
"Error writing to characteristic: %s!", esp_err_to_name(err));
164 esp_ble_gattc_cb_param_t *param)
override {
166 case ESP_GATTC_WRITE_CHAR_EVT:
168 if (param->write.handle == this->char_handle_)
171 case ESP_GATTC_DISCONNECT_EVT:
175 case ESP_GATTC_SEARCH_CMPL_EVT: {
177 if (chr ==
nullptr) {
178 esph_log_w(
"ble_write_action",
"Characteristic %s was not found in service %s",
179 this->char_uuid_.
to_string().c_str(), this->service_uuid_.to_string().c_str());
182 this->char_handle_ = chr->handle;
183 this->char_props_ = chr->properties;
184 if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE) {
185 this->write_type_ = ESP_GATT_WRITE_TYPE_RSP;
187 }
else if (this->char_props_ & ESP_GATT_CHAR_PROP_BIT_WRITE_NR) {
188 this->write_type_ = ESP_GATT_WRITE_TYPE_NO_RSP;
194 this->
node_state = espbt::ClientState::ESTABLISHED;
208 std::vector<uint8_t> (*func)(Ts...);
213 std::tuple<Ts...> var_{};
214 uint16_t char_handle_{};
215 esp_gatt_char_prop_t char_props_{};
216 esp_gatt_write_type_t write_type_{};
223 void play(
const Ts &...
x)
override {
225 if (has_simple_value_) {
226 passkey = this->value_.simple;
228 passkey = this->value_.template_func(
x...);
230 if (passkey > 999999)
232 esp_bd_addr_t remote_bda;
233 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
234 esp_ble_passkey_reply(remote_bda,
true, passkey);
238 this->value_.template_func = func;
239 this->has_simple_value_ =
false;
243 this->value_.simple = value;
244 this->has_simple_value_ =
true;
249 bool has_simple_value_ =
true;
253 } value_{.simple = 0};
260 void play(
const Ts &...
x)
override {
261 esp_bd_addr_t remote_bda;
262 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
263 if (has_simple_value_) {
264 esp_ble_confirm_reply(remote_bda, this->value_.simple);
266 esp_ble_confirm_reply(remote_bda, this->value_.template_func(
x...));
271 this->value_.template_func = func;
272 this->has_simple_value_ =
false;
276 this->value_.simple = value;
277 this->has_simple_value_ =
true;
282 bool has_simple_value_ =
true;
286 } value_{.simple =
false};
293 void play(
const Ts &...
x)
override {
294 esp_bd_addr_t remote_bda;
295 memcpy(remote_bda, parent_->
get_remote_bda(),
sizeof(esp_bd_addr_t));
296 esp_ble_remove_bond_device(remote_bda);
307 ble_client_ = ble_client;
310 esp_ble_gattc_cb_param_t *param)
override {
314 case ESP_GATTC_SEARCH_CMPL_EVT:
315 this->
node_state = espbt::ClientState::ESTABLISHED;
319 case ESP_GATTC_DISCONNECT_EVT:
328 void play(
const Ts &...
x)
override {}
339 if (this->
node_state == espbt::ClientState::ESTABLISHED) {
342 this->var_ = std::make_tuple(
x...);
349 std::tuple<Ts...> var_{};
356 ble_client_ = ble_client;
359 esp_ble_gattc_cb_param_t *param)
override {
363 case ESP_GATTC_CLOSE_EVT:
364 case ESP_GATTC_DISCONNECT_EVT:
373 void play(
const Ts &...
x)
override {}
377 if (this->
node_state == espbt::ClientState::IDLE) {
380 this->var_ = std::make_tuple(
x...);
387 std::tuple<Ts...> var_{};
void play_next_tuple_(const std::tuple< Ts... > &tuple, seq< S... >)
void play_next_(const Ts &...x)
virtual void stop_complex()
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)
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)
Note about logging: the esph_log_X macros are used here because the CI checks complain about use of t...
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 std::string & 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)
Providing packet encoding functions for exchanging data with a remote host.
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.