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