mirror of
https://github.com/esphome/esphome.git
synced 2026-10-27 08:22:25 +00:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d7d62a286c |
No files matched your search
@@ -15,25 +15,32 @@ ESPHOME_LOG_TAG(TAG, "http_request.ota");
|
|||||||
|
|
||||||
void OtaHttpRequestComponent::dump_config() { ESP_LOGCONFIG(TAG, "Over-The-Air updates via HTTP request"); };
|
void OtaHttpRequestComponent::dump_config() { ESP_LOGCONFIG(TAG, "Over-The-Air updates via HTTP request"); };
|
||||||
|
|
||||||
|
OtaHttpRequestComponent::Request &OtaHttpRequestComponent::get_request_() {
|
||||||
|
if (this->request_ == nullptr)
|
||||||
|
this->request_ = make_unique<Request>();
|
||||||
|
return *this->request_;
|
||||||
|
}
|
||||||
|
|
||||||
void OtaHttpRequestComponent::set_md5_url(const std::string &url) {
|
void OtaHttpRequestComponent::set_md5_url(const std::string &url) {
|
||||||
if (!this->validate_url_(url)) {
|
if (!this->validate_url_(url)) {
|
||||||
this->md5_url_.clear(); // URL was not valid; prevent flashing until it is
|
this->get_request_().md5_url.clear(); // URL was not valid; prevent flashing until it is
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->md5_url_ = url;
|
Request &request = this->get_request_();
|
||||||
this->md5_expected_.clear(); // to be retrieved later
|
request.md5_url = url;
|
||||||
|
request.md5_expected.clear(); // to be retrieved later
|
||||||
}
|
}
|
||||||
|
|
||||||
void OtaHttpRequestComponent::set_url(const std::string &url) {
|
void OtaHttpRequestComponent::set_url(const std::string &url) {
|
||||||
if (!this->validate_url_(url)) {
|
if (!this->validate_url_(url)) {
|
||||||
this->url_.clear(); // URL was not valid; prevent flashing until it is
|
this->get_request_().url.clear(); // URL was not valid; prevent flashing until it is
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this->url_ = url;
|
this->get_request_().url = url;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OtaHttpRequestComponent::flash() {
|
void OtaHttpRequestComponent::flash() {
|
||||||
if (this->url_.empty()) {
|
if (this->request_ == nullptr || this->request_->url.empty()) {
|
||||||
ESP_LOGE(TAG, "URL not set; cannot start update");
|
ESP_LOGE(TAG, "URL not set; cannot start update");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -58,8 +65,8 @@ void OtaHttpRequestComponent::flash() {
|
|||||||
#ifdef USE_OTA_STATE_LISTENER
|
#ifdef USE_OTA_STATE_LISTENER
|
||||||
this->notify_state_(ota::OTA_ERROR, 0.0f, ota_status);
|
this->notify_state_(ota::OTA_ERROR, 0.0f, ota_status);
|
||||||
#endif
|
#endif
|
||||||
this->md5_computed_.clear(); // will be reset at next attempt
|
this->request_->md5_computed.clear(); // will be reset at next attempt
|
||||||
this->md5_expected_.clear(); // will be reset at next attempt
|
this->request_->md5_expected.clear(); // will be reset at next attempt
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,18 +88,18 @@ uint8_t OtaHttpRequestComponent::do_ota_() {
|
|||||||
md5::MD5Digest md5_receive;
|
md5::MD5Digest md5_receive;
|
||||||
char md5_receive_str[33];
|
char md5_receive_str[33];
|
||||||
|
|
||||||
if (this->md5_expected_.empty() && !this->http_get_md5_()) {
|
if (this->request_->md5_expected.empty() && !this->http_get_md5_()) {
|
||||||
return OTA_MD5_INVALID;
|
return OTA_MD5_INVALID;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "MD5 expected: %s", this->md5_expected_.c_str());
|
ESP_LOGD(TAG, "MD5 expected: %s", this->request_->md5_expected.c_str());
|
||||||
|
|
||||||
auto url_with_auth = this->get_url_with_auth_(this->url_);
|
auto url_with_auth = this->get_url_with_auth_(this->request_->url);
|
||||||
if (url_with_auth.empty()) {
|
if (url_with_auth.empty()) {
|
||||||
return OTA_BAD_URL;
|
return OTA_BAD_URL;
|
||||||
}
|
}
|
||||||
ESP_LOGVV(TAG, "url_with_auth: %s", url_with_auth.c_str());
|
ESP_LOGVV(TAG, "url_with_auth: %s", url_with_auth.c_str());
|
||||||
ESP_LOGI(TAG, "Connecting to: %s", this->url_.c_str());
|
ESP_LOGI(TAG, "Connecting to: %s", this->request_->url.c_str());
|
||||||
|
|
||||||
auto container = this->parent_->get(url_with_auth);
|
auto container = this->parent_->get(url_with_auth);
|
||||||
|
|
||||||
@@ -179,9 +186,9 @@ uint8_t OtaHttpRequestComponent::do_ota_() {
|
|||||||
// verify MD5 is as expected and act accordingly
|
// verify MD5 is as expected and act accordingly
|
||||||
md5_receive.calculate();
|
md5_receive.calculate();
|
||||||
md5_receive.get_hex(md5_receive_str);
|
md5_receive.get_hex(md5_receive_str);
|
||||||
this->md5_computed_ = md5_receive_str;
|
this->request_->md5_computed = md5_receive_str;
|
||||||
if (strncmp(this->md5_computed_.c_str(), this->md5_expected_.c_str(), MD5_SIZE) != 0) {
|
if (strncmp(this->request_->md5_computed.c_str(), this->request_->md5_expected.c_str(), MD5_SIZE) != 0) {
|
||||||
ESP_LOGE(TAG, "MD5 computed: %s - Aborting due to MD5 mismatch", this->md5_computed_.c_str());
|
ESP_LOGE(TAG, "MD5 computed: %s - Aborting due to MD5 mismatch", this->request_->md5_computed.c_str());
|
||||||
this->cleanup_(std::move(backend), container, /*abort_backend=*/true);
|
this->cleanup_(std::move(backend), container, /*abort_backend=*/true);
|
||||||
return ota::OTA_RESPONSE_ERROR_MD5_MISMATCH;
|
return ota::OTA_RESPONSE_ERROR_MD5_MISMATCH;
|
||||||
} else {
|
} else {
|
||||||
@@ -223,11 +230,15 @@ static std::string url_encode(const std::string &str) {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OtaHttpRequestComponent::set_password(const std::string &password) { this->password_ = url_encode(password); }
|
void OtaHttpRequestComponent::set_password(const std::string &password) {
|
||||||
void OtaHttpRequestComponent::set_username(const std::string &username) { this->username_ = url_encode(username); }
|
this->get_request_().password = url_encode(password);
|
||||||
|
}
|
||||||
|
void OtaHttpRequestComponent::set_username(const std::string &username) {
|
||||||
|
this->get_request_().username = url_encode(username);
|
||||||
|
}
|
||||||
|
|
||||||
std::string OtaHttpRequestComponent::get_url_with_auth_(const std::string &url) {
|
std::string OtaHttpRequestComponent::get_url_with_auth_(const std::string &url) {
|
||||||
if (this->username_.empty() || this->password_.empty()) {
|
if (this->request_->username.empty() || this->request_->password.empty()) {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,23 +251,23 @@ std::string OtaHttpRequestComponent::get_url_with_auth_(const std::string &url)
|
|||||||
ESP_LOGD(TAG, "Using basic HTTP authentication");
|
ESP_LOGD(TAG, "Using basic HTTP authentication");
|
||||||
|
|
||||||
start_char += 3; // skip '://' characters
|
start_char += 3; // skip '://' characters
|
||||||
auto url_with_auth =
|
auto url_with_auth = url.substr(0, start_char) + this->request_->username + ":" + this->request_->password + "@" +
|
||||||
url.substr(0, start_char) + this->username_ + ":" + this->password_ + "@" + url.substr(start_char);
|
url.substr(start_char);
|
||||||
return url_with_auth;
|
return url_with_auth;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OtaHttpRequestComponent::http_get_md5_() {
|
bool OtaHttpRequestComponent::http_get_md5_() {
|
||||||
if (this->md5_url_.empty()) {
|
if (this->request_->md5_url.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto url_with_auth = this->get_url_with_auth_(this->md5_url_);
|
auto url_with_auth = this->get_url_with_auth_(this->request_->md5_url);
|
||||||
if (url_with_auth.empty()) {
|
if (url_with_auth.empty()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGVV(TAG, "url_with_auth: %s", url_with_auth.c_str());
|
ESP_LOGVV(TAG, "url_with_auth: %s", url_with_auth.c_str());
|
||||||
ESP_LOGI(TAG, "Connecting to: %s", this->md5_url_.c_str());
|
ESP_LOGI(TAG, "Connecting to: %s", this->request_->md5_url.c_str());
|
||||||
auto container = this->parent_->get(url_with_auth);
|
auto container = this->parent_->get(url_with_auth);
|
||||||
if (container == nullptr) {
|
if (container == nullptr) {
|
||||||
ESP_LOGE(TAG, "Failed to connect to MD5 URL");
|
ESP_LOGE(TAG, "Failed to connect to MD5 URL");
|
||||||
@@ -273,8 +284,8 @@ bool OtaHttpRequestComponent::http_get_md5_() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->md5_expected_.resize(MD5_SIZE);
|
this->request_->md5_expected.resize(MD5_SIZE);
|
||||||
auto result = http_read_fully(container.get(), (uint8_t *) this->md5_expected_.data(), MD5_SIZE, MD5_SIZE,
|
auto result = http_read_fully(container.get(), (uint8_t *) this->request_->md5_expected.data(), MD5_SIZE, MD5_SIZE,
|
||||||
this->parent_->get_timeout());
|
this->parent_->get_timeout());
|
||||||
container->end();
|
container->end();
|
||||||
|
|
||||||
|
|||||||
@@ -27,29 +27,35 @@ class OtaHttpRequestComponent final : public ota::OTAComponent, public Parented<
|
|||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
|
|
||||||
void set_md5_url(const std::string &md5_url);
|
void set_md5_url(const std::string &md5_url);
|
||||||
void set_md5(const std::string &md5) { this->md5_expected_ = md5; }
|
void set_md5(const std::string &md5) { this->get_request_().md5_expected = md5; }
|
||||||
void set_password(const std::string &password);
|
void set_password(const std::string &password);
|
||||||
void set_url(const std::string &url);
|
void set_url(const std::string &url);
|
||||||
void set_username(const std::string &username);
|
void set_username(const std::string &username);
|
||||||
|
|
||||||
std::string md5_computed() { return this->md5_computed_; }
|
std::string md5_computed() { return this->request_ != nullptr ? this->request_->md5_computed : std::string(); }
|
||||||
std::string md5_expected() { return this->md5_expected_; }
|
std::string md5_expected() { return this->request_ != nullptr ? this->request_->md5_expected : std::string(); }
|
||||||
|
|
||||||
void flash();
|
void flash();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
/// Allocated by the first setter, which only runs before a flash, and never freed, so a flash never loses it.
|
||||||
|
struct Request {
|
||||||
|
std::string md5_computed;
|
||||||
|
std::string md5_expected;
|
||||||
|
std::string md5_url;
|
||||||
|
std::string password;
|
||||||
|
std::string username;
|
||||||
|
std::string url;
|
||||||
|
};
|
||||||
|
Request &get_request_();
|
||||||
|
|
||||||
void cleanup_(ota::OTABackendPtr backend, const std::shared_ptr<HttpContainer> &container, bool abort_backend);
|
void cleanup_(ota::OTABackendPtr backend, const std::shared_ptr<HttpContainer> &container, bool abort_backend);
|
||||||
uint8_t do_ota_();
|
uint8_t do_ota_();
|
||||||
std::string get_url_with_auth_(const std::string &url);
|
std::string get_url_with_auth_(const std::string &url);
|
||||||
bool http_get_md5_();
|
bool http_get_md5_();
|
||||||
bool validate_url_(const std::string &url);
|
bool validate_url_(const std::string &url);
|
||||||
|
|
||||||
std::string md5_computed_{};
|
std::unique_ptr<Request> request_;
|
||||||
std::string md5_expected_{};
|
|
||||||
std::string md5_url_{};
|
|
||||||
std::string password_{};
|
|
||||||
std::string username_{};
|
|
||||||
std::string url_{};
|
|
||||||
int status_ = -1;
|
int status_ = -1;
|
||||||
static const uint16_t HTTP_RECV_BUFFER = 256; // the firmware GET chunk size
|
static const uint16_t HTTP_RECV_BUFFER = 256; // the firmware GET chunk size
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -72,6 +72,23 @@ button:
|
|||||||
id(ota_update).set_source_url(std::string("http://my.ha.net:8123/local/esphome/manifest.json"));
|
id(ota_update).set_source_url(std::string("http://my.ha.net:8123/local/esphome/manifest.json"));
|
||||||
ESP_LOGI("test", "Manifest is now %s", id(ota_update).get_source_url());
|
ESP_LOGI("test", "Manifest is now %s", id(ota_update).get_source_url());
|
||||||
- component.update: ota_update
|
- component.update: ota_update
|
||||||
|
# The OTA component's public setters and getters as lambdas use them; every shape must compile.
|
||||||
|
- platform: template
|
||||||
|
name: Flash from lambda
|
||||||
|
on_press:
|
||||||
|
then:
|
||||||
|
- lambda: |-
|
||||||
|
std::string url = "http://my.ha.net:8123/local/esphome/";
|
||||||
|
url += "firmware.bin";
|
||||||
|
id(http_request_ota).set_md5_url("http://my.ha.net:8123/local/esphome/firmware.md5");
|
||||||
|
id(http_request_ota).set_md5(std::string("0123456789abcdef0123456789abcdef"));
|
||||||
|
id(http_request_ota).set_username("user");
|
||||||
|
id(http_request_ota).set_password(std::string("pass"));
|
||||||
|
id(http_request_ota).set_url(url);
|
||||||
|
id(http_request_ota).set_url(url.c_str());
|
||||||
|
ESP_LOGI("test", "MD5 %s / %s", id(http_request_ota).md5_expected().c_str(),
|
||||||
|
id(http_request_ota).md5_computed().c_str());
|
||||||
|
id(http_request_ota).flash();
|
||||||
|
|
||||||
update:
|
update:
|
||||||
- platform: http_request
|
- platform: http_request
|
||||||
|
|||||||
Reference in new issue
Block a user