ESPHome 2025.10.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#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 bool prepare_auth_nonce_(HashBase *hasher);
51 bool verify_hash_auth_(HashBase *hasher, size_t hex_size);
52 size_t get_auth_hex_size_() const;
53 void cleanup_auth_();
54 void log_auth_warning_(const LogString *msg);
55#endif // USE_OTA_PASSWORD
56 bool readall_(uint8_t *buf, size_t len);
57 bool writeall_(const uint8_t *buf, size_t len);
58
59 bool try_read_(size_t to_read, const LogString *desc);
60 bool try_write_(size_t to_write, const LogString *desc);
61
62 inline bool would_block_(int error_code) const { return error_code == EAGAIN || error_code == EWOULDBLOCK; }
63 bool handle_read_error_(ssize_t read, const LogString *desc);
64 bool handle_write_error_(ssize_t written, const LogString *desc);
65 inline void transition_ota_state_(OTAState next_state) {
66 this->ota_state_ = next_state;
67 this->handshake_buf_pos_ = 0; // Reset buffer position for next state
68 }
69
70 void log_socket_error_(const LogString *msg);
71 void log_read_error_(const LogString *what);
72 void log_start_(const LogString *phase);
73 void log_remote_closed_(const LogString *during);
76 uint8_t error_byte = static_cast<uint8_t>(error);
77 this->client_->write(&error_byte, 1); // Best effort, non-blocking
78 this->cleanup_connection_();
79 }
81
82#ifdef USE_OTA_PASSWORD
83 std::string password_;
84#endif // USE_OTA_PASSWORD
85
86 std::unique_ptr<socket::Socket> server_;
87 std::unique_ptr<socket::Socket> client_;
88 std::unique_ptr<ota::OTABackend> backend_;
89
91 uint16_t port_;
92 uint8_t handshake_buf_[5];
95 uint8_t ota_features_{0};
96#ifdef USE_OTA_PASSWORD
97 std::unique_ptr<uint8_t[]> auth_buf_;
98 uint8_t auth_buf_pos_{0};
99 uint8_t auth_type_{0}; // Store auth type to know which hasher to use
100#endif // USE_OTA_PASSWORD
101};
102
103} // namespace esphome
104#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:62
std::unique_ptr< ota::OTABackend > backend_
Definition ota_esphome.h:88
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:97
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:75
bool handle_read_error_(ssize_t read, const LogString *desc)
bool prepare_auth_nonce_(HashBase *hasher)
void log_read_error_(const LogString *what)
bool verify_hash_auth_(HashBase *hasher, size_t hex_size)
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:86
void transition_ota_state_(OTAState next_state)
Definition ota_esphome.h:65
void log_remote_closed_(const LogString *during)
std::unique_ptr< socket::Socket > client_
Definition ota_esphome.h:87
void log_start_(const LogString *phase)
void log_socket_error_(const LogString *msg)
Base class for hash algorithms.
Definition hash_base.h:11
__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:291