Compare commits

..

5 Commits

Author SHA1 Message Date
J. Nick Koston
42be5381e6 fix 2025-11-21 07:47:17 -06:00
J. Nick Koston
ada557b9fe Merge branch 'dev' into status_set_error_dangling_pointer 2025-11-21 06:43:07 -06:00
J. Nick Koston
150e26dc2b [cst816][http_request] Fix status_set_error() dangling pointer bugs (#12033) 2025-11-21 06:41:48 -06:00
Jonathan Swoboda
0dea7a23e3 [jsn_sr04t] Fix model AJ_SR04M (#11992) 2025-11-21 07:39:59 -05:00
J. Nick Koston
8ad4bb9255 [core] Fix status_set_error() dangling pointer by accepting std::string 2025-11-20 11:47:40 -06:00
6 changed files with 34 additions and 37 deletions

View File

@@ -19,7 +19,7 @@ void CST816Touchscreen::continue_setup_() {
case CST816T_CHIP_ID:
break;
default:
this->status_set_error(str_sprintf("Unknown chip ID 0x%02X", this->chip_id_).c_str());
this->status_set_error(str_sprintf("Unknown chip ID 0x%02X", this->chip_id_));
this->mark_failed();
return;
}

View File

@@ -85,6 +85,7 @@ void ESP32InternalGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpi
break;
}
gpio_set_intr_type(this->get_pin_num(), idf_type);
gpio_intr_enable(this->get_pin_num());
if (!isr_service_installed) {
auto res = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL3);
if (res != ESP_OK) {
@@ -94,7 +95,6 @@ void ESP32InternalGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpi
isr_service_installed = true;
}
gpio_isr_handler_add(this->get_pin_num(), func, arg);
gpio_intr_enable(this->get_pin_num());
}
std::string ESP32InternalGPIOPin::dump_summary() const {

View File

@@ -51,7 +51,7 @@ void HttpRequestUpdate::update_task(void *params) {
if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
std::string msg = str_sprintf("Failed to fetch manifest from %s", this_update->source_url_.c_str());
// Defer to main loop to avoid race condition on component_state_ read-modify-write
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
this_update->defer([this_update, msg]() { this_update->status_set_error(msg); });
UPDATE_RETURN;
}
@@ -60,7 +60,7 @@ void HttpRequestUpdate::update_task(void *params) {
if (data == nullptr) {
std::string msg = str_sprintf("Failed to allocate %zu bytes for manifest", container->content_length);
// Defer to main loop to avoid race condition on component_state_ read-modify-write
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
this_update->defer([this_update, msg]() { this_update->status_set_error(msg); });
container->end();
UPDATE_RETURN;
}
@@ -123,7 +123,7 @@ void HttpRequestUpdate::update_task(void *params) {
if (!valid) {
std::string msg = str_sprintf("Failed to parse JSON from %s", this_update->source_url_.c_str());
// Defer to main loop to avoid race condition on component_state_ read-modify-write
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
this_update->defer([this_update, msg]() { this_update->status_set_error(msg); });
UPDATE_RETURN;
}

View File

@@ -10,7 +10,7 @@ namespace jsn_sr04t {
static const char *const TAG = "jsn_sr04t.sensor";
void Jsnsr04tComponent::update() {
this->write_byte(0x55);
this->write_byte((this->model_ == AJ_SR04M) ? 0x01 : 0x55);
ESP_LOGV(TAG, "Request read out from sensor");
}
@@ -31,19 +31,10 @@ void Jsnsr04tComponent::loop() {
}
void Jsnsr04tComponent::check_buffer_() {
uint8_t checksum = 0;
switch (this->model_) {
case JSN_SR04T:
checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
break;
case AJ_SR04M:
checksum = this->buffer_[1] + this->buffer_[2];
break;
}
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
if (this->buffer_[3] == checksum) {
uint16_t distance = encode_uint16(this->buffer_[1], this->buffer_[2]);
if (distance > 250) {
if (distance > ((this->model_ == AJ_SR04M) ? 200 : 250)) {
float meters = distance / 1000.0f;
ESP_LOGV(TAG, "Distance from sensor: %umm, %.3fm", distance, meters);
this->publish_state(meters);

View File

@@ -35,7 +35,7 @@ static const char *const TAG = "component";
namespace {
struct ComponentErrorMessage {
const Component *component;
const char *message;
std::string message;
};
struct ComponentPriorityOverride {
@@ -146,7 +146,7 @@ void Component::call_dump_config() {
if (component_error_messages) {
for (const auto &entry : *component_error_messages) {
if (entry.component == this) {
error_msg = entry.message;
error_msg = entry.message.c_str();
break;
}
}
@@ -307,28 +307,33 @@ void Component::status_set_warning(const LogString *message) {
ESP_LOGW(TAG, "%s set Warning flag: %s", LOG_STR_ARG(this->get_component_log_str()),
message ? LOG_STR_ARG(message) : LOG_STR_LITERAL("unspecified"));
}
void Component::status_set_error(const char *message) {
void Component::status_set_error(const std::string &message) {
if ((this->component_state_ & STATUS_LED_ERROR) != 0)
return;
this->component_state_ |= STATUS_LED_ERROR;
App.app_state_ |= STATUS_LED_ERROR;
ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()),
message ? message : LOG_STR_LITERAL("unspecified"));
if (message != nullptr) {
// Lazy allocate the error messages vector if needed
if (!component_error_messages) {
component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
}
// Check if this component already has an error message
for (auto &entry : *component_error_messages) {
if (entry.component == this) {
entry.message = message;
return;
}
}
// Add new error message
component_error_messages->emplace_back(ComponentErrorMessage{this, message});
ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()), message.c_str());
// Lazy allocate the error messages vector if needed
if (!component_error_messages) {
component_error_messages = std::make_unique<std::vector<ComponentErrorMessage>>();
}
// Check if this component already has an error message
for (auto &entry : *component_error_messages) {
if (entry.component == this) {
entry.message = message;
return;
}
}
// Add new error message
component_error_messages->emplace_back(ComponentErrorMessage{this, message});
}
void Component::status_set_error() {
// No message version - just set the error flag
if ((this->component_state_ & STATUS_LED_ERROR) != 0)
return;
this->component_state_ |= STATUS_LED_ERROR;
App.app_state_ |= STATUS_LED_ERROR;
ESP_LOGE(TAG, "%s set Error flag: %s", LOG_STR_ARG(this->get_component_log_str()), LOG_STR_LITERAL("unspecified"));
}
void Component::status_clear_warning() {
if ((this->component_state_ & STATUS_LED_WARNING) == 0)

View File

@@ -216,7 +216,8 @@ class Component {
void status_set_warning(const char *message = nullptr);
void status_set_warning(const LogString *message);
void status_set_error(const char *message = nullptr);
void status_set_error(const std::string &message);
void status_set_error();
void status_clear_warning();