ESPHome  2024.5.3
ota_component.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/helpers.h"
7 #include "esphome/core/defines.h"
8 
9 namespace esphome {
10 namespace ota {
11 
15 
24 
39 };
40 
42 
44 class OTAComponent : public Component {
45  public:
46  OTAComponent();
47 #ifdef USE_OTA_PASSWORD
48  void set_auth_password(const std::string &password) { password_ = password; }
49 #endif // USE_OTA_PASSWORD
50 
52  void set_port(uint16_t port);
53 
54  bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time);
55 
57  void set_safe_mode_pending(const bool &pending);
58  bool get_safe_mode_pending();
59 
60 #ifdef USE_OTA_STATE_CALLBACK
61  void add_on_state_callback(std::function<void(OTAState, float, uint8_t)> &&callback);
62 #endif
63 
64  // ========== INTERNAL METHODS ==========
65  // (In most use cases you won't need these)
66  void setup() override;
67  void dump_config() override;
68  float get_setup_priority() const override;
69  void loop() override;
70 
71  uint16_t get_port() const;
72 
73  void clean_rtc();
74 
75  void on_safe_shutdown() override;
76 
77  protected:
78  void write_rtc_(uint32_t val);
79  uint32_t read_rtc_();
80 
81  void handle_();
82  bool readall_(uint8_t *buf, size_t len);
83  bool writeall_(const uint8_t *buf, size_t len);
84 
85 #ifdef USE_OTA_PASSWORD
86  std::string password_;
87 #endif // USE_OTA_PASSWORD
88 
89  uint16_t port_;
90 
91  std::unique_ptr<socket::Socket> server_;
92  std::unique_ptr<socket::Socket> client_;
93 
94  bool has_safe_mode_{false};
96  uint32_t safe_mode_enable_time_{60000};
100 
101  static const uint32_t ENTER_SAFE_MODE_MAGIC =
102  0x5afe5afe;
103 
104 #ifdef USE_OTA_STATE_CALLBACK
106 #endif
107 };
108 
109 extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
110 
111 } // namespace ota
112 } // namespace esphome
uint32_t safe_mode_enable_time_
The time safe mode should be on for.
Definition: ota_component.h:96
ESPPreferenceObject rtc_
Definition: ota_component.h:99
void add_on_state_callback(std::function< void(OTAState, float, uint8_t)> &&callback)
mopeka_std_values val[4]
void set_auth_password(const std::string &password)
Definition: ota_component.h:48
uint32_t safe_mode_start_time_
stores when safe mode was enabled.
Definition: ota_component.h:95
bool readall_(uint8_t *buf, size_t len)
static const uint32_t ENTER_SAFE_MODE_MAGIC
a magic number to indicate that safe mode should be entered on next boot
void set_port(uint16_t port)
Manually set the port OTA should listen on.
std::unique_ptr< socket::Socket > client_
Definition: ota_component.h:92
float get_setup_priority() const override
bool has_safe_mode_
stores whether safe mode can be enabled.
Definition: ota_component.h:94
void write_rtc_(uint32_t val)
void on_safe_shutdown() override
OTAComponent * global_ota_component
bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time)
std::string size_t len
Definition: helpers.h:292
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_safe_mode_pending(const bool &pending)
Set to true if the next startup will enter safe mode.
bool writeall_(const uint8_t *buf, size_t len)
std::unique_ptr< socket::Socket > server_
Definition: ota_component.h:91
CallbackManager< void(OTAState, float, uint8_t)> state_callback_
OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA...
Definition: ota_component.h:44