ESPHome 2025.10.3
Loading...
Searching...
No Matches
ota_esphome.h
Go to the documentation of this file.
1#pragma once
2
4#ifdef USE_OTA
8#include "esphome/core/log.h"
11
12namespace esphome {
13
16 public:
17 enum class OTAState : uint8_t {
18 IDLE,
19 MAGIC_READ, // Reading magic bytes
20 MAGIC_ACK, // Sending OK and version after magic bytes
21 FEATURE_READ, // Reading feature flags from client
22 FEATURE_ACK, // Sending feature acknowledgment
23#ifdef USE_OTA_PASSWORD
24 AUTH_SEND, // Sending authentication request
25 AUTH_READ, // Reading authentication data
26#endif // USE_OTA_PASSWORD
27 DATA, // BLOCKING! Processing OTA data (update, etc.)
28 };
29#ifdef USE_OTA_PASSWORD
30 void set_auth_password(const std::string &password) { password_ = password; }
31#endif // USE_OTA_PASSWORD
32
34 void set_port(uint16_t port);
35
36 void setup() override;
37 void dump_config() override;
38 float get_setup_priority() const override;
39 void loop() override;
40
41 uint16_t get_port() const;
42
43 protected:
44 void handle_handshake_();
45 void handle_data_();
46#ifdef USE_OTA_PASSWORD
47 bool handle_auth_send_();
48 bool handle_auth_read_();
49 bool select_auth_type_();
50 size_t get_auth_hex_size_() const;
51 void cleanup_auth_();
52 void log_auth_warning_(const LogString *msg);
53#endif // USE_OTA_PASSWORD
54 bool readall_(uint8_t *buf, size_t len);
55 bool writeall_(const uint8_t *buf, size_t len);
56
57 bool try_read_(size_t to_read, const LogString *desc);
58 bool try_write_(size_t to_write, const LogString *desc);
59
60 inline bool would_block_(int error_code) const { return error_code == EAGAIN || error_code == EWOULDBLOCK; }
61 bool handle_read_error_(ssize_t read, const LogString *desc);
62 bool handle_write_error_(ssize_t written, const LogString *desc);
63 inline void transition_ota_state_(OTAState next_state) {
64 this->ota_state_ = next_state;
65 this->handshake_buf_pos_ = 0; // Reset buffer position for next state
66 }
67
68 void log_socket_error_(const LogString *msg);
69 void log_read_error_(const LogString *what);
70 void log_start_(const LogString *phase);
71 void log_remote_closed_(const LogString *during);
74 uint8_t error_byte = static_cast<uint8_t>(error);
75 this->client_->write(&error_byte, 1); // Best effort, non-blocking
76 this->cleanup_connection_();
77 }
79
80#ifdef USE_OTA_PASSWORD
81 std::string password_;
82#endif // USE_OTA_PASSWORD
83
84 std::unique_ptr<socket::Socket> server_;
85 std::unique_ptr<socket::Socket> client_;
86 std::unique_ptr<ota::OTABackend> backend_;
87
89 uint16_t port_;
90 uint8_t handshake_buf_[5];
93 uint8_t ota_features_{0};
94#ifdef USE_OTA_PASSWORD
95 std::unique_ptr<uint8_t[]> auth_buf_;
96 uint8_t auth_buf_pos_{0};
97 uint8_t auth_type_{0}; // Store auth type to know which hasher to use
98#endif // USE_OTA_PASSWORD
99};
100
101} // namespace esphome
102#endif
ESPHomeOTAComponent provides a simple way to integrate Over-the-Air updates into your app using Ardui...
Definition ota_esphome.h:15
bool would_block_(int error_code) const
Definition ota_esphome.h:60
std::unique_ptr< ota::OTABackend > backend_
Definition ota_esphome.h:86
bool writeall_(const uint8_t *buf, size_t len)
bool try_read_(size_t to_read, const LogString *desc)
void set_auth_password(const std::string &password)
Definition ota_esphome.h:30
bool try_write_(size_t to_write, const LogString *desc)
std::unique_ptr< uint8_t[]> auth_buf_
Definition ota_esphome.h:95
bool handle_write_error_(ssize_t written, const LogString *desc)
void log_auth_warning_(const LogString *msg)
float get_setup_priority() const override
void send_error_and_cleanup_(ota::OTAResponseTypes error)
Definition ota_esphome.h:73
bool handle_read_error_(ssize_t read, const LogString *desc)
void log_read_error_(const LogString *what)
bool readall_(uint8_t *buf, size_t len)
void set_port(uint16_t port)
Manually set the port OTA should listen on.
std::unique_ptr< socket::Socket > server_
Definition ota_esphome.h:84
void transition_ota_state_(OTAState next_state)
Definition ota_esphome.h:63
void log_remote_closed_(const LogString *during)
std::unique_ptr< socket::Socket > client_
Definition ota_esphome.h:85
void log_start_(const LogString *phase)
void log_socket_error_(const LogString *msg)
__int64 ssize_t
Definition httplib.h:178
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:304