mirror of
https://github.com/esphome/esphome.git
synced 2025-09-03 03:42:20 +01:00
[debug] Show source of last software reboot (#8595)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "debug_component.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include <esp_sleep.h>
|
||||
@@ -10,12 +11,12 @@
|
||||
#include <esp_chip_info.h>
|
||||
#include <esp_partition.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include <Esp.h>
|
||||
#endif
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace esphome {
|
||||
namespace debug {
|
||||
|
||||
@@ -42,16 +43,39 @@ static const char *const RESET_REASONS[] = {
|
||||
"CPU lock up",
|
||||
};
|
||||
|
||||
static const char *const REBOOT_KEY = "reboot_source";
|
||||
static const size_t REBOOT_MAX_LEN = 24;
|
||||
|
||||
// on shutdown, store the source of the reboot request
|
||||
void DebugComponent::on_shutdown() {
|
||||
auto *component = App.get_current_component();
|
||||
char buffer[REBOOT_MAX_LEN]{};
|
||||
auto pref = global_preferences->make_preference(REBOOT_MAX_LEN, fnv1_hash(REBOOT_KEY + App.get_name()));
|
||||
if (component != nullptr) {
|
||||
strncpy(buffer, component->get_component_source(), REBOOT_MAX_LEN - 1);
|
||||
}
|
||||
ESP_LOGD(TAG, "Storing reboot source: %s", buffer);
|
||||
pref.save(&buffer);
|
||||
global_preferences->sync();
|
||||
}
|
||||
|
||||
std::string DebugComponent::get_reset_reason_() {
|
||||
std::string reset_reason;
|
||||
unsigned reason = esp_reset_reason();
|
||||
if (reason < sizeof(RESET_REASONS) / sizeof(RESET_REASONS[0])) {
|
||||
reset_reason = RESET_REASONS[reason];
|
||||
if (reason == ESP_RST_SW) {
|
||||
auto pref = global_preferences->make_preference(REBOOT_MAX_LEN, fnv1_hash(REBOOT_KEY + App.get_name()));
|
||||
char buffer[REBOOT_MAX_LEN]{};
|
||||
if (pref.load(&buffer)) {
|
||||
reset_reason = "Reboot request from " + std::string(buffer);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
reset_reason = "unknown source";
|
||||
}
|
||||
ESP_LOGD(TAG, "Reset Reason: %s", reset_reason.c_str());
|
||||
return "Reset by " + reset_reason;
|
||||
return reset_reason;
|
||||
}
|
||||
|
||||
static const char *const WAKEUP_CAUSES[] = {
|
||||
|
Reference in New Issue
Block a user