mirror of
https://github.com/esphome/esphome.git
synced 2025-10-31 23:21:54 +00:00
[esphome][ota] Add write_byte_() helper to reduce code duplication
This commit is contained in:
@@ -281,19 +281,15 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Acknowledge auth OK - 1 byte
|
// Acknowledge auth OK - 1 byte
|
||||||
buf[0] = ota::OTA_RESPONSE_AUTH_OK;
|
this->write_byte_(ota::OTA_RESPONSE_AUTH_OK);
|
||||||
this->writeall_(buf, 1);
|
|
||||||
|
|
||||||
// Read size, 4 bytes MSB first
|
// Read size, 4 bytes MSB first
|
||||||
if (!this->readall_(buf, 4)) {
|
if (!this->readall_(buf, 4)) {
|
||||||
this->log_read_error_(LOG_STR("size"));
|
this->log_read_error_(LOG_STR("size"));
|
||||||
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
|
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
|
||||||
}
|
}
|
||||||
ota_size = 0;
|
ota_size = (static_cast<size_t>(buf[0]) << 24) | (static_cast<size_t>(buf[1]) << 16) |
|
||||||
for (uint8_t i = 0; i < 4; i++) {
|
(static_cast<size_t>(buf[2]) << 8) | buf[3];
|
||||||
ota_size <<= 8;
|
|
||||||
ota_size |= buf[i];
|
|
||||||
}
|
|
||||||
ESP_LOGV(TAG, "Size is %u bytes", ota_size);
|
ESP_LOGV(TAG, "Size is %u bytes", ota_size);
|
||||||
|
|
||||||
// Now that we've passed authentication and are actually
|
// Now that we've passed authentication and are actually
|
||||||
@@ -313,8 +309,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
update_started = true;
|
update_started = true;
|
||||||
|
|
||||||
// Acknowledge prepare OK - 1 byte
|
// Acknowledge prepare OK - 1 byte
|
||||||
buf[0] = ota::OTA_RESPONSE_UPDATE_PREPARE_OK;
|
this->write_byte_(ota::OTA_RESPONSE_UPDATE_PREPARE_OK);
|
||||||
this->writeall_(buf, 1);
|
|
||||||
|
|
||||||
// Read binary MD5, 32 bytes
|
// Read binary MD5, 32 bytes
|
||||||
if (!this->readall_(buf, 32)) {
|
if (!this->readall_(buf, 32)) {
|
||||||
@@ -326,8 +321,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
this->backend_->set_update_md5(sbuf);
|
this->backend_->set_update_md5(sbuf);
|
||||||
|
|
||||||
// Acknowledge MD5 OK - 1 byte
|
// Acknowledge MD5 OK - 1 byte
|
||||||
buf[0] = ota::OTA_RESPONSE_BIN_MD5_OK;
|
this->write_byte_(ota::OTA_RESPONSE_BIN_MD5_OK);
|
||||||
this->writeall_(buf, 1);
|
|
||||||
|
|
||||||
while (total < ota_size) {
|
while (total < ota_size) {
|
||||||
// TODO: timeout check
|
// TODO: timeout check
|
||||||
@@ -354,8 +348,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
total += read;
|
total += read;
|
||||||
#if USE_OTA_VERSION == 2
|
#if USE_OTA_VERSION == 2
|
||||||
while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) {
|
while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) {
|
||||||
buf[0] = ota::OTA_RESPONSE_CHUNK_OK;
|
this->write_byte_(ota::OTA_RESPONSE_CHUNK_OK);
|
||||||
this->writeall_(buf, 1);
|
|
||||||
size_acknowledged += OTA_BLOCK_SIZE;
|
size_acknowledged += OTA_BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -374,8 +367,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acknowledge receive OK - 1 byte
|
// Acknowledge receive OK - 1 byte
|
||||||
buf[0] = ota::OTA_RESPONSE_RECEIVE_OK;
|
this->write_byte_(ota::OTA_RESPONSE_RECEIVE_OK);
|
||||||
this->writeall_(buf, 1);
|
|
||||||
|
|
||||||
error_code = this->backend_->end();
|
error_code = this->backend_->end();
|
||||||
if (error_code != ota::OTA_RESPONSE_OK) {
|
if (error_code != ota::OTA_RESPONSE_OK) {
|
||||||
@@ -384,8 +376,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Acknowledge Update end OK - 1 byte
|
// Acknowledge Update end OK - 1 byte
|
||||||
buf[0] = ota::OTA_RESPONSE_UPDATE_END_OK;
|
this->write_byte_(ota::OTA_RESPONSE_UPDATE_END_OK);
|
||||||
this->writeall_(buf, 1);
|
|
||||||
|
|
||||||
// Read ACK
|
// Read ACK
|
||||||
if (!this->readall_(buf, 1) || buf[0] != ota::OTA_RESPONSE_OK) {
|
if (!this->readall_(buf, 1) || buf[0] != ota::OTA_RESPONSE_OK) {
|
||||||
@@ -404,8 +395,7 @@ void ESPHomeOTAComponent::handle_data_() {
|
|||||||
App.safe_reboot();
|
App.safe_reboot();
|
||||||
|
|
||||||
error:
|
error:
|
||||||
buf[0] = static_cast<uint8_t>(error_code);
|
this->write_byte_(static_cast<uint8_t>(error_code));
|
||||||
this->writeall_(buf, 1);
|
|
||||||
this->cleanup_connection_();
|
this->cleanup_connection_();
|
||||||
|
|
||||||
if (this->backend_ != nullptr && update_started) {
|
if (this->backend_ != nullptr && update_started) {
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ class ESPHomeOTAComponent : public ota::OTAComponent {
|
|||||||
#endif // USE_OTA_PASSWORD
|
#endif // USE_OTA_PASSWORD
|
||||||
bool readall_(uint8_t *buf, size_t len);
|
bool readall_(uint8_t *buf, size_t len);
|
||||||
bool writeall_(const uint8_t *buf, size_t len);
|
bool writeall_(const uint8_t *buf, size_t len);
|
||||||
|
inline bool write_byte_(uint8_t byte) { return this->writeall_(&byte, 1); }
|
||||||
|
|
||||||
bool try_read_(size_t to_read, const LogString *desc);
|
bool try_read_(size_t to_read, const LogString *desc);
|
||||||
bool try_write_(size_t to_write, const LogString *desc);
|
bool try_write_(size_t to_write, const LogString *desc);
|
||||||
|
|||||||
Reference in New Issue
Block a user