29 auto *packet = this->
encoder_->get_send_pin_request(this->
pin_);
31 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
32 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
33 ESP_LOGI(TAG,
"[%s] Logging into AM43", LOG_STR_ARG(this->
get_log_name()));
35 ESP_LOGW(TAG,
"[%s] Error writing set_pin to device, error = %d", LOG_STR_ARG(this->
get_log_name()),
status);
52 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
53 ESP_LOGW(TAG,
"[%s] Cannot send cover control, not connected", LOG_STR_ARG(this->
get_log_name()));
57 auto *packet = this->
encoder_->get_stop_request();
59 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
60 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
62 ESP_LOGW(TAG,
"[%s] Error writing stop command to device, error = %d", LOG_STR_ARG(this->
get_log_name()),
status);
65 auto opt_pos =
call.get_position();
66 if (opt_pos.has_value()) {
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", LOG_STR_ARG(this->
get_log_name()),
83 esp_ble_gattc_cb_param_t *param) {
85 case ESP_GATTC_DISCONNECT_EVT: {
89 case ESP_GATTC_SEARCH_CMPL_EVT: {
93 ESP_LOGE(TAG,
"[%s] Detected a Tuya AM43 which is not supported, sorry.", LOG_STR_ARG(this->
get_log_name()));
95 ESP_LOGE(TAG,
"[%s] No control service found at device, not an AM43..?", LOG_STR_ARG(this->
get_log_name()));
104 ESP_LOGW(TAG,
"[%s] esp_ble_gattc_register_for_notify failed, status=%d", LOG_STR_ARG(this->
get_log_name()),
109 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
110 this->
node_state = espbt::ClientState::ESTABLISHED;
113 case ESP_GATTC_NOTIFY_EVT: {
114 if (param->notify.handle != this->char_handle_)
116 this->
decoder_->decode(param->notify.value, param->notify.value_len);
118 if (this->
decoder_->has_position()) {
129 if (this->
decoder_->has_pin_response()) {
131 ESP_LOGI(TAG,
"[%s] AM43 pin accepted.", LOG_STR_ARG(this->
get_log_name()));
132 auto *packet = this->
encoder_->get_position_request();
134 this->char_handle_, packet->length, packet->data,
135 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
137 ESP_LOGW(TAG,
"[%s] Error writing set_position to device, error = %d", LOG_STR_ARG(this->
get_log_name()),
141 ESP_LOGW(TAG,
"[%s] AM43 pin rejected!", LOG_STR_ARG(this->
get_log_name()));
145 if (this->
decoder_->has_set_position_response() && !this->decoder_->set_position_ok_) {
146 ESP_LOGW(TAG,
"[%s] Got nack after sending set_position. Bad pin?", LOG_STR_ARG(this->
get_log_name()));
149 if (this->
decoder_->has_set_state_response() && !this->decoder_->set_state_ok_) {
150 ESP_LOGW(TAG,
"[%s] Got nack after sending set_state. Bad pin?", LOG_STR_ARG(this->
get_log_name()));