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