30 auto *packet = this->
encoder_->get_send_pin_request(this->
pin_);
32 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
33 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
34 ESP_LOGI(TAG,
"[%s] Logging into AM43", this->
get_name().c_str());
36 ESP_LOGW(TAG,
"[%s] Error writing set_pin to device, error = %d", this->
get_name().c_str(),
status);
53 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
54 ESP_LOGW(TAG,
"[%s] Cannot send cover control, not connected", this->
get_name().c_str());
57 if (
call.get_stop()) {
58 auto *packet = this->
encoder_->get_stop_request();
60 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
61 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
63 ESP_LOGW(TAG,
"[%s] Error writing stop command to device, error = %d", this->
get_name().c_str(),
status);
66 if (
call.get_position().has_value()) {
67 auto pos = *
call.get_position();
71 auto *packet = this->
encoder_->get_set_position_request(100 - (uint8_t) (pos * 100));
73 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
74 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
76 ESP_LOGW(TAG,
"[%s] Error writing set_position command to device, error = %d", this->
get_name().c_str(),
status);
82 esp_ble_gattc_cb_param_t *param) {
84 case ESP_GATTC_DISCONNECT_EVT: {
88 case ESP_GATTC_SEARCH_CMPL_EVT: {
92 ESP_LOGE(TAG,
"[%s] Detected a Tuya AM43 which is not supported, sorry.", this->
get_name().c_str());
94 ESP_LOGE(TAG,
"[%s] No control service found at device, not an AM43..?", this->
get_name().c_str());
103 ESP_LOGW(TAG,
"[%s] esp_ble_gattc_register_for_notify failed, status=%d", this->
get_name().c_str(),
status);
107 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
108 this->
node_state = espbt::ClientState::ESTABLISHED;
111 case ESP_GATTC_NOTIFY_EVT: {
112 if (param->notify.handle != this->char_handle_)
114 this->
decoder_->decode(param->notify.value, param->notify.value_len);
116 if (this->
decoder_->has_position()) {
127 if (this->
decoder_->has_pin_response()) {
129 ESP_LOGI(TAG,
"[%s] AM43 pin accepted.", this->
get_name().c_str());
130 auto *packet = this->
encoder_->get_position_request();
132 this->char_handle_, packet->length, packet->data,
133 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
135 ESP_LOGW(TAG,
"[%s] Error writing set_position to device, error = %d", this->
get_name().c_str(),
status);
138 ESP_LOGW(TAG,
"[%s] AM43 pin rejected!", this->
get_name().c_str());
142 if (this->
decoder_->has_set_position_response() && !this->decoder_->set_position_ok_) {
143 ESP_LOGW(TAG,
"[%s] Got nack after sending set_position. Bad pin?", this->
get_name().c_str());
146 if (this->
decoder_->has_set_state_response() && !this->decoder_->set_state_ok_) {
147 ESP_LOGW(TAG,
"[%s] Got nack after sending set_state. Bad pin?", this->
get_name().c_str());