ESPHome 2026.11.0-dev
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
15constexpr uint32_t RTC_KEY = 233825507UL;
16
18class SafeModeComponent final : public Component {
19 public:
20 // User provided, not "= default": `new(p) SafeModeComponent()` would zero-fill .bss that is already zero.
22
23 bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time, uint32_t boot_is_good_after, bool in_flash);
24
26 void set_safe_mode_pending(const bool &pending);
30
31 void dump_config() override;
32 float get_setup_priority() const override;
33 void loop() override;
34
35 void clean_rtc();
36
37 void on_safe_shutdown() override;
38
39 void mark_successful();
40
41#ifdef USE_SAFE_MODE_CALLBACK
42 template<typename F> void add_on_safe_mode_callback(F &&callback) {
43 this->safe_mode_callback_.add(std::forward<F>(callback));
44 }
45#endif
46
47 protected:
50#ifdef USE_OTA_ROLLBACK
51 void confirm_app_image_();
52#endif
53
54 // Group all 4-byte aligned members together to avoid padding
59#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
60 esp_ota_img_states_t ota_state_{ESP_OTA_IMG_UNDEFINED}; // 4-byte enum
61#endif
62 // Group 1-byte members together to minimize padding
63 bool boot_successful_{false};
65#if defined(USE_ESP32) && defined(USE_OTA_ROLLBACK)
67#endif
68 // Larger objects at the end
70#ifdef USE_SAFE_MODE_CALLBACK
71 StaticCallbackManager<ESPHOME_SAFE_MODE_CALLBACK_COUNT, void()> safe_mode_callback_{};
72#endif
73
75 0x5afe5afe;
76};
77
78} // namespace esphome::safe_mode
CallbackManager backed by StaticVector for compile-time-known callback counts.
Definition helpers.h:1960
SafeModeComponent provides a safe way to recover from repeated boot failures.
Definition safe_mode.h:18
uint32_t safe_mode_enable_time_
The time safe mode should remain active for.
Definition safe_mode.h:56
bool boot_successful_
set to true after boot is considered successful
Definition safe_mode.h:63
uint32_t safe_mode_start_time_
stores when safe mode was enabled
Definition safe_mode.h:58
void add_on_safe_mode_callback(F &&callback)
Definition safe_mode.h:42
uint32_t safe_mode_boot_is_good_after_
The amount of time after which the boot is considered successful.
Definition safe_mode.h:55
float get_setup_priority() const override
StaticCallbackManager< ESPHOME_SAFE_MODE_CALLBACK_COUNT, void()> safe_mode_callback_
Definition safe_mode.h:71
esp_ota_img_states_t ota_state_
Definition safe_mode.h:60
void restart_in_safe_mode()
Set safe mode pending and reboot; run by the safe mode button.
bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time, uint32_t boot_is_good_after, bool in_flash)
void set_safe_mode_pending(const bool &pending)
Set to true if the next startup will enter safe mode.
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:74
mopeka_std_values val[3]
constexpr uint32_t RTC_KEY
RTC key for storing boot loop counter - used by safe_mode and preferences backends.
Definition safe_mode.h:15
static void uint32_t