Public Access
fix: thread saftey and resource managament issues
Signed-off-by: bullshit <coding@ow-software.pl>
This commit is contained in:
2 files changed
+85
-44
No files matched your search
@@ -82,14 +82,16 @@ void NutUpsComponent::update() {
|
|||||||
update_sensors();
|
update_sensors();
|
||||||
} else {
|
} else {
|
||||||
consecutive_failures_++;
|
consecutive_failures_++;
|
||||||
ESP_LOGW(TAG, "Failed to read UPS data (failure #%u)", consecutive_failures_.load());
|
ESP_LOGW(TAG, "Failed to read UPS data (failure #%u)", consecutive_failures_);
|
||||||
|
|
||||||
// Implement exponential backoff for retries
|
// Implement limited retry logic with exponential backoff
|
||||||
if (consecutive_failures_ >= 3) {
|
if (consecutive_failures_ >= 3 && consecutive_failures_ <= max_consecutive_failures_) {
|
||||||
ESP_LOGE(TAG, "Multiple consecutive failures, attempting protocol re-detection");
|
ESP_LOGE(TAG, "Multiple consecutive failures, attempting protocol re-detection");
|
||||||
if (!detect_ups_protocol()) {
|
if (!detect_ups_protocol()) {
|
||||||
ESP_LOGE(TAG, "Protocol re-detection failed");
|
ESP_LOGE(TAG, "Protocol re-detection failed");
|
||||||
}
|
}
|
||||||
|
} else if (consecutive_failures_ > max_consecutive_failures_) {
|
||||||
|
ESP_LOGE(TAG, "Maximum re-detection attempts reached, giving up");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (millis() - last_successful_read_ > protocol_timeout_ms_) {
|
if (millis() - last_successful_read_ > protocol_timeout_ms_) {
|
||||||
@@ -213,11 +215,16 @@ bool NutUpsComponent::read_ups_data() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create temporary data structure to avoid corrupting existing data on failure
|
// Create temporary data structure to avoid corrupting existing data on failure
|
||||||
UpsData temp_data = ups_data_;
|
UpsData temp_data;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||||
|
temp_data = ups_data_;
|
||||||
|
}
|
||||||
|
|
||||||
if (active_protocol_->read_data(temp_data)) {
|
if (active_protocol_->read_data(temp_data)) {
|
||||||
// Only update if we got valid data
|
// Only update if we got valid data
|
||||||
if (temp_data.is_valid()) {
|
if (temp_data.is_valid()) {
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||||
ups_data_ = temp_data;
|
ups_data_ = temp_data;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
@@ -231,6 +238,13 @@ bool NutUpsComponent::read_ups_data() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NutUpsComponent::update_sensors() {
|
void NutUpsComponent::update_sensors() {
|
||||||
|
// Create local copy of data under lock to minimize lock time
|
||||||
|
UpsData local_data;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||||
|
local_data = ups_data_;
|
||||||
|
}
|
||||||
|
|
||||||
// Update numeric sensors with validation
|
// Update numeric sensors with validation
|
||||||
for (const auto &[type, sensor] : sensors_) {
|
for (const auto &[type, sensor] : sensors_) {
|
||||||
if (!sensor) continue; // Safety check
|
if (!sensor) continue; // Safety check
|
||||||
@@ -238,17 +252,17 @@ void NutUpsComponent::update_sensors() {
|
|||||||
float value = NAN;
|
float value = NAN;
|
||||||
|
|
||||||
if (type == "battery_level") {
|
if (type == "battery_level") {
|
||||||
value = ups_data_.battery_level;
|
value = local_data.battery_level;
|
||||||
} else if (type == "input_voltage") {
|
} else if (type == "input_voltage") {
|
||||||
value = ups_data_.input_voltage;
|
value = local_data.input_voltage;
|
||||||
} else if (type == "output_voltage") {
|
} else if (type == "output_voltage") {
|
||||||
value = ups_data_.output_voltage;
|
value = local_data.output_voltage;
|
||||||
} else if (type == "load_percent") {
|
} else if (type == "load_percent") {
|
||||||
value = ups_data_.load_percent;
|
value = local_data.load_percent;
|
||||||
} else if (type == "runtime") {
|
} else if (type == "runtime") {
|
||||||
value = ups_data_.runtime_minutes;
|
value = local_data.runtime_minutes;
|
||||||
} else if (type == "frequency") {
|
} else if (type == "frequency") {
|
||||||
value = ups_data_.frequency;
|
value = local_data.frequency;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only publish if value is valid and within reasonable bounds
|
// Only publish if value is valid and within reasonable bounds
|
||||||
@@ -273,17 +287,17 @@ void NutUpsComponent::update_sensors() {
|
|||||||
auto *sensor = pair.second;
|
auto *sensor = pair.second;
|
||||||
|
|
||||||
if (type == "online") {
|
if (type == "online") {
|
||||||
sensor->publish_state(ups_data_.status_flags & UPS_STATUS_ONLINE);
|
sensor->publish_state(local_data.status_flags & UPS_STATUS_ONLINE);
|
||||||
} else if (type == "on_battery") {
|
} else if (type == "on_battery") {
|
||||||
sensor->publish_state(ups_data_.status_flags & UPS_STATUS_ON_BATTERY);
|
sensor->publish_state(local_data.status_flags & UPS_STATUS_ON_BATTERY);
|
||||||
} else if (type == "low_battery") {
|
} else if (type == "low_battery") {
|
||||||
sensor->publish_state(ups_data_.status_flags & UPS_STATUS_LOW_BATTERY);
|
sensor->publish_state(local_data.status_flags & UPS_STATUS_LOW_BATTERY);
|
||||||
} else if (type == "fault") {
|
} else if (type == "fault") {
|
||||||
sensor->publish_state(ups_data_.status_flags & UPS_STATUS_FAULT);
|
sensor->publish_state(local_data.status_flags & UPS_STATUS_FAULT);
|
||||||
} else if (type == "overload") {
|
} else if (type == "overload") {
|
||||||
sensor->publish_state(ups_data_.status_flags & UPS_STATUS_OVERLOAD);
|
sensor->publish_state(local_data.status_flags & UPS_STATUS_OVERLOAD);
|
||||||
} else if (type == "charging") {
|
} else if (type == "charging") {
|
||||||
sensor->publish_state(ups_data_.status_flags & UPS_STATUS_CHARGING);
|
sensor->publish_state(local_data.status_flags & UPS_STATUS_CHARGING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -292,19 +306,19 @@ void NutUpsComponent::update_sensors() {
|
|||||||
const std::string &type = pair.first;
|
const std::string &type = pair.first;
|
||||||
auto *sensor = pair.second;
|
auto *sensor = pair.second;
|
||||||
|
|
||||||
if (type == "model" && !ups_data_.model.empty()) {
|
if (type == "model" && !local_data.model.empty()) {
|
||||||
sensor->publish_state(ups_data_.model);
|
sensor->publish_state(local_data.model);
|
||||||
} else if (type == "manufacturer" && !ups_data_.manufacturer.empty()) {
|
} else if (type == "manufacturer" && !local_data.manufacturer.empty()) {
|
||||||
sensor->publish_state(ups_data_.manufacturer);
|
sensor->publish_state(local_data.manufacturer);
|
||||||
} else if (type == "protocol") {
|
} else if (type == "protocol") {
|
||||||
sensor->publish_state(get_protocol_name());
|
sensor->publish_state(get_protocol_name());
|
||||||
} else if (type == "status") {
|
} else if (type == "status") {
|
||||||
std::string status_str = "";
|
std::string status_str = "";
|
||||||
if (ups_data_.status_flags & UPS_STATUS_ONLINE) status_str += "Online ";
|
if (local_data.status_flags & UPS_STATUS_ONLINE) status_str += "Online ";
|
||||||
if (ups_data_.status_flags & UPS_STATUS_ON_BATTERY) status_str += "OnBattery ";
|
if (local_data.status_flags & UPS_STATUS_ON_BATTERY) status_str += "OnBattery ";
|
||||||
if (ups_data_.status_flags & UPS_STATUS_LOW_BATTERY) status_str += "LowBattery ";
|
if (local_data.status_flags & UPS_STATUS_LOW_BATTERY) status_str += "LowBattery ";
|
||||||
if (ups_data_.status_flags & UPS_STATUS_CHARGING) status_str += "Charging ";
|
if (local_data.status_flags & UPS_STATUS_CHARGING) status_str += "Charging ";
|
||||||
if (ups_data_.status_flags & UPS_STATUS_FAULT) status_str += "Fault ";
|
if (local_data.status_flags & UPS_STATUS_FAULT) status_str += "Fault ";
|
||||||
if (status_str.empty()) status_str = "Unknown";
|
if (status_str.empty()) status_str = "Unknown";
|
||||||
sensor->publish_state(status_str);
|
sensor->publish_state(status_str);
|
||||||
}
|
}
|
||||||
@@ -351,7 +365,7 @@ void NutUpsComponent::register_text_sensor(text_sensor::TextSensor *sens, const
|
|||||||
|
|
||||||
bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
if (!device_connected_ || data.empty()) {
|
if (data.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -361,6 +375,12 @@ bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check connection status inside mutex to prevent race conditions
|
||||||
|
if (!device_connected_) {
|
||||||
|
xSemaphoreGive(usb_mutex_);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<uint8_t> dummy_response;
|
std::vector<uint8_t> dummy_response;
|
||||||
esp_err_t ret = usb_transfer_sync(data, dummy_response, 1000);
|
esp_err_t ret = usb_transfer_sync(data, dummy_response, 1000);
|
||||||
|
|
||||||
@@ -371,7 +391,7 @@ bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGV(TAG, "USB Write: %d bytes", data.size());
|
ESP_LOGV(TAG, "USB Write: %zu bytes", data.size());
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
return false;
|
return false;
|
||||||
@@ -380,15 +400,17 @@ bool NutUpsComponent::usb_write(const std::vector<uint8_t> &data) {
|
|||||||
|
|
||||||
bool NutUpsComponent::usb_read(std::vector<uint8_t> &data, uint32_t timeout_ms) {
|
bool NutUpsComponent::usb_read(std::vector<uint8_t> &data, uint32_t timeout_ms) {
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
if (!device_connected_) {
|
data.clear();
|
||||||
data.clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Take USB mutex for thread safety
|
// Take USB mutex for thread safety
|
||||||
if (xSemaphoreTake(usb_mutex_, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) {
|
if (xSemaphoreTake(usb_mutex_, pdMS_TO_TICKS(timeout_ms)) != pdTRUE) {
|
||||||
ESP_LOGW(TAG, "Failed to acquire USB mutex for read");
|
ESP_LOGW(TAG, "Failed to acquire USB mutex for read");
|
||||||
data.clear();
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check connection status inside mutex to prevent race conditions
|
||||||
|
if (!device_connected_) {
|
||||||
|
xSemaphoreGive(usb_mutex_);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +425,7 @@ bool NutUpsComponent::usb_read(std::vector<uint8_t> &data, uint32_t timeout_ms)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGV(TAG, "USB Read: %d bytes", data.size());
|
ESP_LOGV(TAG, "USB Read: %zu bytes", data.size());
|
||||||
return true;
|
return true;
|
||||||
#else
|
#else
|
||||||
data.clear();
|
data.clear();
|
||||||
@@ -497,8 +519,8 @@ esp_err_t NutUpsComponent::usb_host_lib_init() {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create USB Host Library task
|
// Create USB Host Library task with larger stack size for safety
|
||||||
if (xTaskCreate(usb_host_lib_task, "usb_host", 4096, this, 5, &usb_task_handle_) != pdTRUE) {
|
if (xTaskCreate(usb_host_lib_task, "usb_host", 8192, this, 5, &usb_task_handle_) != pdTRUE) {
|
||||||
ESP_LOGE(TAG, "Failed to create USB Host task");
|
ESP_LOGE(TAG, "Failed to create USB Host task");
|
||||||
return ESP_ERR_NO_MEM;
|
return ESP_ERR_NO_MEM;
|
||||||
}
|
}
|
||||||
@@ -557,12 +579,24 @@ esp_err_t NutUpsComponent::usb_device_enumerate() {
|
|||||||
ESP_LOGI(TAG, "Found UPS device: VID=0x%04X, PID=0x%04X", usb_device_.vid, usb_device_.pid);
|
ESP_LOGI(TAG, "Found UPS device: VID=0x%04X, PID=0x%04X", usb_device_.vid, usb_device_.pid);
|
||||||
|
|
||||||
// Claim interface and get endpoints
|
// Claim interface and get endpoints
|
||||||
if (usb_claim_interface() == ESP_OK && usb_get_endpoints() == ESP_OK) {
|
esp_err_t claim_ret = usb_claim_interface();
|
||||||
device_connected_ = true;
|
if (claim_ret == ESP_OK) {
|
||||||
return ESP_OK;
|
esp_err_t endpoint_ret = usb_get_endpoints();
|
||||||
|
if (endpoint_ret == ESP_OK) {
|
||||||
|
device_connected_ = true;
|
||||||
|
return ESP_OK;
|
||||||
|
}
|
||||||
|
ESP_LOGW(TAG, "Failed to get USB endpoints: %s", esp_err_to_name(endpoint_ret));
|
||||||
|
} else {
|
||||||
|
ESP_LOGW(TAG, "Failed to claim USB interface: %s", esp_err_to_name(claim_ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clean up on failure
|
||||||
|
usb_device_.dev_hdl = nullptr;
|
||||||
|
usb_device_.dev_addr = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close device handle if we opened it but didn't use it
|
||||||
usb_host_device_close(usb_device_.client_hdl, dev_hdl);
|
usb_host_device_close(usb_device_.client_hdl, dev_hdl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <atomic>
|
#include <mutex>
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
#include "esp_err.h"
|
#include "esp_err.h"
|
||||||
@@ -137,12 +137,17 @@ namespace esphome
|
|||||||
if (product_id != 0) usb_product_id_ = product_id;
|
if (product_id != 0) usb_product_id_ = product_id;
|
||||||
}
|
}
|
||||||
void set_protocol_timeout(uint32_t timeout_ms) {
|
void set_protocol_timeout(uint32_t timeout_ms) {
|
||||||
protocol_timeout_ms_ = std::max(timeout_ms, static_cast<uint32_t>(5000)); // Min 5 seconds
|
// Bound timeout between 5 seconds and 5 minutes for safety
|
||||||
|
protocol_timeout_ms_ = std::max(static_cast<uint32_t>(5000),
|
||||||
|
std::min(timeout_ms, static_cast<uint32_t>(300000)));
|
||||||
}
|
}
|
||||||
void set_auto_detect_protocol(bool auto_detect) { auto_detect_protocol_ = auto_detect; }
|
void set_auto_detect_protocol(bool auto_detect) { auto_detect_protocol_ = auto_detect; }
|
||||||
|
|
||||||
// Data getters for sensors
|
// Data getters for sensors (thread-safe)
|
||||||
const UpsData &get_ups_data() const { return ups_data_; }
|
UpsData get_ups_data() const {
|
||||||
|
std::lock_guard<std::mutex> lock(data_mutex_);
|
||||||
|
return ups_data_;
|
||||||
|
}
|
||||||
bool is_connected() const { return connected_; }
|
bool is_connected() const { return connected_; }
|
||||||
std::string get_protocol_name() const;
|
std::string get_protocol_name() const;
|
||||||
|
|
||||||
@@ -163,9 +168,11 @@ namespace esphome
|
|||||||
bool auto_detect_protocol_{true};
|
bool auto_detect_protocol_{true};
|
||||||
|
|
||||||
bool connected_{false};
|
bool connected_{false};
|
||||||
std::atomic<uint32_t> last_successful_read_{0};
|
uint32_t last_successful_read_{0};
|
||||||
std::atomic<uint32_t> consecutive_failures_{0};
|
uint32_t consecutive_failures_{0};
|
||||||
|
uint32_t max_consecutive_failures_{5}; // Limit re-detection attempts
|
||||||
UpsData ups_data_;
|
UpsData ups_data_;
|
||||||
|
mutable std::mutex data_mutex_; // Protect ups_data_ access
|
||||||
|
|
||||||
std::unique_ptr<UpsProtocolBase> active_protocol_;
|
std::unique_ptr<UpsProtocolBase> active_protocol_;
|
||||||
std::unordered_map<std::string, sensor::Sensor *> sensors_;
|
std::unordered_map<std::string, sensor::Sensor *> sensors_;
|
||||||
|
|||||||
Reference in new issue
Block a user