[multiple] Fix reliability issues in 5 components (#14655)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Jonathan Swoboda
2026-03-09 18:32:57 -04:00
committed by GitHub
co-authored by Claude Opus 4.6 J. Nick Koston
parent d2686b49be
commit d96be88ff5
5 changed files with 37 additions and 2 deletions
@@ -271,10 +271,16 @@ void BME680BSECComponent::read_() {
int64_t curr_time_ns = this->get_time_ns_();
if (this->bme680_settings_.trigger_measurement) {
uint32_t start = millis();
while (this->bme680_.power_mode != BME680_SLEEP_MODE) {
if (millis() - start > 50) {
ESP_LOGE(TAG, "Timeout waiting for BME680 to enter sleep mode");
return;
}
this->bme680_status_ = bme680_get_sensor_mode(&this->bme680_);
if (this->bme680_status_ != BME680_OK) {
ESP_LOGW(TAG, "Failed to get sensor mode (BME680 Error Code %d)", this->bme680_status_);
ESP_LOGE(TAG, "Failed to get sensor mode (BME680 Error Code %d)", this->bme680_status_);
return;
}
}
}
@@ -6,6 +6,7 @@
namespace esphome::hlk_fm22x {
static const char *const TAG = "hlk_fm22x";
static constexpr uint32_t PAYLOAD_TIMEOUT_MS = 20;
void HlkFm22xComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up HLK-FM22X...");
@@ -133,6 +134,21 @@ void HlkFm22xComponent::recv_command_() {
checksum ^= byte;
length |= byte;
// Wait for remaining data (payload + checksum) to arrive.
// Header bytes are already consumed, so we must finish reading this message.
uint32_t start = millis();
while (this->available() < length + 1) {
if (millis() - start > PAYLOAD_TIMEOUT_MS) {
ESP_LOGE(TAG, "Timeout waiting for payload (%u bytes)", length);
// Drain any partial payload bytes to resync the parser
while (this->available() > 0) {
this->read();
}
return;
}
delay(1);
}
// Read up to buffer size; discard excess bytes while still computing checksum
// GET_ALL_FACE_IDS can return all enrolled face data (hundreds of bytes)
// but handlers only need the first few bytes
+6
View File
@@ -163,8 +163,11 @@ void LvglComponent::show_page(size_t index, lv_scr_load_anim_t anim, uint32_t ti
void LvglComponent::show_next_page(lv_scr_load_anim_t anim, uint32_t time) {
if (this->pages_.empty() || (this->current_page_ == this->pages_.size() - 1 && !this->page_wrap_))
return;
size_t start = this->current_page_;
do {
this->current_page_ = (this->current_page_ + 1) % this->pages_.size();
if (this->current_page_ == start)
return; // all pages have skip=true (guaranteed not to happen by YAML validation)
} while (this->pages_[this->current_page_]->skip); // skip empty pages()
this->show_page(this->current_page_, anim, time);
}
@@ -172,8 +175,11 @@ void LvglComponent::show_next_page(lv_scr_load_anim_t anim, uint32_t time) {
void LvglComponent::show_prev_page(lv_scr_load_anim_t anim, uint32_t time) {
if (this->pages_.empty() || (this->current_page_ == 0 && !this->page_wrap_))
return;
size_t start = this->current_page_;
do {
this->current_page_ = (this->current_page_ + this->pages_.size() - 1) % this->pages_.size();
if (this->current_page_ == start)
return; // all pages have skip=true (guaranteed not to happen by YAML validation)
} while (this->pages_[this->current_page_]->skip); // skip empty pages()
this->show_page(this->current_page_, anim, time);
}
+3 -1
View File
@@ -44,8 +44,10 @@ MQTTClientComponent::MQTTClientComponent() {
void MQTTClientComponent::setup() {
this->mqtt_backend_.set_on_message(
[this](const char *topic, const char *payload, size_t len, size_t index, size_t total) {
if (index == 0)
if (index == 0) {
this->payload_buffer_.clear();
this->payload_buffer_.reserve(total);
}
// append new payload, may contain incomplete MQTT message
this->payload_buffer_.append(payload, len);
+5
View File
@@ -106,10 +106,15 @@ std::vector<CdcEps> USBUartTypeCdcAcm::parse_descriptors(usb_device_handle_t dev
}
void RingBuffer::push(uint8_t item) {
if (this->get_free_space() == 0)
return;
this->buffer_[this->insert_pos_] = item;
this->insert_pos_ = (this->insert_pos_ + 1) % this->buffer_size_;
}
void RingBuffer::push(const uint8_t *data, size_t len) {
size_t free = this->get_free_space();
if (len > free)
len = free;
for (size_t i = 0; i != len; i++) {
this->buffer_[this->insert_pos_] = *data++;
this->insert_pos_ = (this->insert_pos_ + 1) % this->buffer_size_;