28 auto *packet = this->
encoder_->get_send_pin_request(this->
pin_);
30 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
31 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
32 ESP_LOGI(TAG,
"[%s] Logging into AM43", this->
get_name().c_str());
34 ESP_LOGW(TAG,
"[%s] Error writing set_pin to device, error = %d", this->
get_name().c_str(),
status);
51 if (this->
node_state != espbt::ClientState::ESTABLISHED) {
52 ESP_LOGW(TAG,
"[%s] Cannot send cover control, not connected", this->
get_name().c_str());
55 if (
call.get_stop()) {
56 auto *packet = this->
encoder_->get_stop_request();
58 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
59 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
61 ESP_LOGW(TAG,
"[%s] Error writing stop command to device, error = %d", this->
get_name().c_str(),
status);
64 if (
call.get_position().has_value()) {
65 auto pos = *
call.get_position();
69 auto *packet = this->
encoder_->get_set_position_request(100 - (uint8_t) (pos * 100));
71 esp_ble_gattc_write_char(this->
parent_->
get_gattc_if(), this->parent_->get_conn_id(), this->char_handle_,
72 packet->length, packet->data, ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
74 ESP_LOGW(TAG,
"[%s] Error writing set_position command to device, error = %d", this->
get_name().c_str(),
status);
80 esp_ble_gattc_cb_param_t *param) {
82 case ESP_GATTC_DISCONNECT_EVT: {
86 case ESP_GATTC_SEARCH_CMPL_EVT: {
90 ESP_LOGE(TAG,
"[%s] Detected a Tuya AM43 which is not supported, sorry.", this->
get_name().c_str());
92 ESP_LOGE(TAG,
"[%s] No control service found at device, not an AM43..?", this->
get_name().c_str());
101 ESP_LOGW(TAG,
"[%s] esp_ble_gattc_register_for_notify failed, status=%d", this->
get_name().c_str(),
status);
105 case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
106 this->
node_state = espbt::ClientState::ESTABLISHED;
109 case ESP_GATTC_NOTIFY_EVT: {
110 if (param->notify.handle != this->char_handle_)
112 this->
decoder_->decode(param->notify.value, param->notify.value_len);
114 if (this->
decoder_->has_position()) {
125 if (this->
decoder_->has_pin_response()) {
127 ESP_LOGI(TAG,
"[%s] AM43 pin accepted.", this->
get_name().c_str());
128 auto *packet = this->
encoder_->get_position_request();
130 this->char_handle_, packet->length, packet->data,
131 ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
133 ESP_LOGW(TAG,
"[%s] Error writing set_position to device, error = %d", this->
get_name().c_str(),
status);
136 ESP_LOGW(TAG,
"[%s] AM43 pin rejected!", this->
get_name().c_str());
140 if (this->
decoder_->has_set_position_response() && !this->decoder_->set_position_ok_) {
141 ESP_LOGW(TAG,
"[%s] Got nack after sending set_position. Bad pin?", this->
get_name().c_str());
144 if (this->
decoder_->has_set_state_response() && !this->decoder_->set_state_ok_) {
145 ESP_LOGW(TAG,
"[%s] Got nack after sending set_state. Bad pin?", this->
get_name().c_str());