ESPHome 2026.6.0-dev
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#else
32 // Stub so lambdas referencing set_auth_password() produce a clear error instead of
33 // a cryptic "no member" diagnostic. Only fires if the stub is actually instantiated.
34 template<bool B = false> void set_auth_password(const std::string &) {
35 static_assert(B, "set_auth_password() requires the OTA auth path to be compiled. "
36 "Add 'password: \"\"' (empty string) to your 'ota: - platform: esphome' "
37 "config to enable runtime password rotation.");
38 }
39#endif // USE_OTA_PASSWORD
40
42 void set_port(uint16_t port);
43
44 void setup() override;
45 void dump_config() override;
46 float get_setup_priority() const override;
47 void loop() override;
48
49 uint16_t get_port() const;
50
51 protected:
52 void handle_handshake_();
53 void handle_data_();
54#ifdef USE_OTA_PASSWORD
55 static constexpr size_t SHA256_HEX_SIZE = 64; // SHA256 hash as hex string (32 bytes * 2)
56 bool handle_auth_send_();
57 bool handle_auth_read_();
58 bool select_auth_type_();
59 void cleanup_auth_();
60 void log_auth_warning_(const LogString *msg);
61#endif // USE_OTA_PASSWORD
62 bool readall_(uint8_t *buf, size_t len);
63 bool writeall_(const uint8_t *buf, size_t len);
64 inline bool write_byte_(uint8_t byte) { return this->writeall_(&byte, 1); }
65
66 bool try_read_(size_t to_read, const LogString *desc);
67 bool try_write_(size_t to_write, const LogString *desc);
68
69 inline bool would_block_(int error_code) const { return error_code == EAGAIN || error_code == EWOULDBLOCK; }
70 bool handle_read_error_(ssize_t read, const LogString *desc);
71 bool handle_write_error_(ssize_t written, const LogString *desc);
72 inline void transition_ota_state_(OTAState next_state) {
73 this->ota_state_ = next_state;
74 this->handshake_buf_pos_ = 0; // Reset buffer position for next state
75 }
76
77 void server_failed_(const LogString *msg);
78 void log_socket_error_(const LogString *msg);
79 void log_read_error_(const LogString *what);
80 void log_start_(const LogString *phase);
81 void log_remote_closed_(const LogString *during);
84 uint8_t error_byte = static_cast<uint8_t>(error);
85 this->client_->write(&error_byte, 1); // Best effort, non-blocking
86 this->cleanup_connection_();
87 }
89
90#ifdef USE_OTA_PASSWORD
91 std::string password_;
92 std::unique_ptr<uint8_t[]> auth_buf_;
93#endif // USE_OTA_PASSWORD
94
96 std::unique_ptr<socket::Socket> client_;
98
100 static constexpr size_t HANDSHAKE_BUF_SIZE = 5;
101#ifdef USE_OTA_PARTITIONS
104#endif
105 uint16_t port_;
109 uint8_t ota_features_{0};
110#ifdef USE_OTA_PASSWORD
111 uint8_t auth_buf_pos_{0};
112 uint8_t auth_type_{0}; // Store auth type to know which hasher to use
113#endif // USE_OTA_PASSWORD
114 bool extended_proto_{false};
115};
116
117} // namespace esphome
118#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:69
static constexpr size_t SHA256_HEX_SIZE
Definition ota_esphome.h:55
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
ota::OTABackendPtr backend_
Definition ota_esphome.h:97
bool try_write_(size_t to_write, const LogString *desc)
std::unique_ptr< uint8_t[]> auth_buf_
Definition ota_esphome.h:92
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:83
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.
void set_auth_password(const std::string &)
Definition ota_esphome.h:34
bool write_byte_(uint8_t byte)
Definition ota_esphome.h:64
uint8_t handshake_buf_[HANDSHAKE_BUF_SIZE]
static constexpr size_t HANDSHAKE_BUF_SIZE
void server_failed_(const LogString *msg)
void transition_ota_state_(OTAState next_state)
Definition ota_esphome.h:72
socket::ListenSocket * server_
Definition ota_esphome.h:95
void log_remote_closed_(const LogString *during)
std::unique_ptr< socket::Socket > client_
Definition ota_esphome.h:96
void log_start_(const LogString *phase)
void log_socket_error_(const LogString *msg)
__int64 ssize_t
Definition httplib.h:178
decltype(make_ota_backend()) OTABackendPtr
const void size_t len
Definition hal.h:64
int written
Definition helpers.h:1045
static void uint32_t