ESPHome  2023.11.6
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 
23 
38 };
39 
41 
43 class OTAComponent : public Component {
44  public:
45  OTAComponent();
46 #ifdef USE_OTA_PASSWORD
47  void set_auth_password(const std::string &password) { password_ = password; }
48 #endif // USE_OTA_PASSWORD
49 
51  void set_port(uint16_t port);
52 
53  bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time);
54 
56  void set_safe_mode_pending(const bool &pending);
57  bool get_safe_mode_pending();
58 
59 #ifdef USE_OTA_STATE_CALLBACK
60  void add_on_state_callback(std::function<void(OTAState, float, uint8_t)> &&callback);
61 #endif
62 
63  // ========== INTERNAL METHODS ==========
64  // (In most use cases you won't need these)
65  void setup() override;
66  void dump_config() override;
67  float get_setup_priority() const override;
68  void loop() override;
69 
70  uint16_t get_port() const;
71 
72  void clean_rtc();
73 
74  void on_safe_shutdown() override;
75 
76  protected:
77  void write_rtc_(uint32_t val);
78  uint32_t read_rtc_();
79 
80  void handle_();
81  bool readall_(uint8_t *buf, size_t len);
82  bool writeall_(const uint8_t *buf, size_t len);
83 
84 #ifdef USE_OTA_PASSWORD
85  std::string password_;
86 #endif // USE_OTA_PASSWORD
87 
88  uint16_t port_;
89 
90  std::unique_ptr<socket::Socket> server_;
91  std::unique_ptr<socket::Socket> client_;
92 
93  bool has_safe_mode_{false};
95  uint32_t safe_mode_enable_time_{60000};
99 
100  static const uint32_t ENTER_SAFE_MODE_MAGIC =
101  0x5afe5afe;
102 
103 #ifdef USE_OTA_STATE_CALLBACK
105 #endif
106 };
107 
108 extern OTAComponent *global_ota_component; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
109 
110 } // namespace ota
111 } // namespace esphome
uint32_t safe_mode_enable_time_
The time safe mode should be on for.
Definition: ota_component.h:95
ESPPreferenceObject rtc_
Definition: ota_component.h:98
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:47
uint32_t safe_mode_start_time_
stores when safe mode was enabled.
Definition: ota_component.h:94
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:91
float get_setup_priority() const override
bool has_safe_mode_
stores whether safe mode can be enabled.
Definition: ota_component.h:93
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
Implementation of SPI Controller mode.
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:90
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:43