ESPHome 2026.1.5
Loading...
Searching...
No Matches
safe_mode.h
Go to the documentation of this file.
1#pragma once
2
7
8#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
9#include <esp_ota_ops.h>
10#endif
11
12namespace esphome::safe_mode {
13
16 public:
17 bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time, uint32_t boot_is_good_after);
18
20 void set_safe_mode_pending(const bool &pending);
22
23 void dump_config() override;
24 float get_setup_priority() const override;
25 void loop() override;
26
27 void clean_rtc();
28
29 void on_safe_shutdown() override;
30
31#ifdef USE_SAFE_MODE_CALLBACK
32 void add_on_safe_mode_callback(std::function<void()> &&callback) {
33 this->safe_mode_callback_.add(std::move(callback));
34 }
35#endif
36
37 protected:
38 void write_rtc_(uint32_t val);
39 uint32_t read_rtc_();
40
41 // Group all 4-byte aligned members together to avoid padding
43 uint32_t safe_mode_enable_time_{60000};
46 // Group 1-byte members together to minimize padding
47 bool boot_successful_{false};
49#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
50 esp_ota_img_states_t ota_state_{ESP_OTA_IMG_UNDEFINED};
51#endif
52 // Larger objects at the end
54#ifdef USE_SAFE_MODE_CALLBACK
56#endif
57
58 static const uint32_t ENTER_SAFE_MODE_MAGIC =
59 0x5afe5afe;
60};
61
62} // namespace esphome::safe_mode
SafeModeComponent provides a safe way to recover from repeated boot failures.
Definition safe_mode.h:15
bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time, uint32_t boot_is_good_after)
Definition safe_mode.cpp:96
uint32_t safe_mode_enable_time_
The time safe mode should remain active for.
Definition safe_mode.h:43
void add_on_safe_mode_callback(std::function< void()> &&callback)
Definition safe_mode.h:32
bool boot_successful_
set to true after boot is considered successful
Definition safe_mode.h:47
uint32_t safe_mode_start_time_
stores when safe mode was enabled
Definition safe_mode.h:45
uint32_t safe_mode_boot_is_good_after_
The amount of time after which the boot is considered successful.
Definition safe_mode.h:42
float get_setup_priority() const override
Definition safe_mode.cpp:61
esp_ota_img_states_t ota_state_
Definition safe_mode.h:50
void set_safe_mode_pending(const bool &pending)
Set to true if the next startup will enter safe mode.
Definition safe_mode.cpp:78
static const uint32_t ENTER_SAFE_MODE_MAGIC
a magic number to indicate that safe mode should be entered on next boot
Definition safe_mode.h:58
CallbackManager< void()> safe_mode_callback_
Definition safe_mode.h:55
mopeka_std_values val[4]