1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-01 17:42:22 +01:00

ESP-IDF support and generic target platforms (#2303)

* Socket refactor and SSL

* esp-idf temp

* Fixes

* Echo component and noise

* Add noise API transport support

* Updates

* ESP-IDF

* Complete

* Fixes

* Fixes

* Versions update

* New i2c APIs

* Complete i2c refactor

* SPI migration

* Revert ESP Preferences migration, too complex for now

* OTA support

* Remove echo again

* Remove ssl again

* GPIOFlags updates

* Rename esphal and ICACHE_RAM_ATTR

* Make ESP32 arduino compilable again

* Fix GPIO flags

* Complete pin registry refactor and fixes

* Fixes to make test1 compile

* Remove sdkconfig file

* Ignore sdkconfig file

* Fixes in reviewing

* Make test2 compile

* Make test4 compile

* Make test5 compile

* Run clang-format

* Fix lint errors

* Use esp-idf APIs instead of btStart

* Another round of fixes

* Start implementing ESP8266

* Make test3 compile

* Guard esp8266 code

* Lint

* Reformat

* Fixes

* Fixes v2

* more fixes

* ESP-IDF tidy target

* Convert ARDUINO_ARCH_ESPxx

* Update WiFiSignalSensor

* Update time ifdefs

* OTA needs millis from hal

* RestartSwitch needs delay from hal

* ESP-IDF Uart

* Fix OTA blank password

* Allow setting sdkconfig

* Fix idf partitions and allow setting sdkconfig from yaml

* Re-add read/write compat APIs and fix esp8266 uart

* Fix esp8266 store log strings in flash

* Fix ESP32 arduino preferences not initialized

* Update ifdefs

* Change how sdkconfig change is detected

* Add checks to ci-custom and fix them

* Run clang-format

* Add esp-idf clang-tidy target and fix errors

* Fixes from clang-tidy idf round 2

* Fixes from compiling tests with esp-idf

* Run clang-format

* Switch test5.yaml to esp-idf

* Implement ESP8266 Preferences

* Lint

* Re-do PIO package version selection a bit

* Fix arduinoespressif32 package version

* Fix unit tests

* Lint

* Lint fixes

* Fix readv/writev not defined

* Fix graphing component

* Re-add all old options from core/config.py

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Otto Winter
2021-09-20 11:47:51 +02:00
committed by GitHub
parent 1e8e471dec
commit ac0d921413
583 changed files with 9008 additions and 5420 deletions

View File

@@ -15,6 +15,7 @@ from esphome.core import CORE, coroutine_with_priority
CODEOWNERS = ["@esphome/core"]
DEPENDENCIES = ["network"]
AUTO_LOAD = ["socket"]
CONF_ON_STATE_CHANGE = "on_state_change"
CONF_ON_BEGIN = "on_begin"
@@ -33,12 +34,21 @@ OTAProgressTrigger = ota_ns.class_("OTAProgressTrigger", automation.Trigger.temp
OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template())
OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template())
def validate_password_support(value):
if CORE.using_arduino:
return value
if CORE.using_esp_idf:
raise cv.Invalid("Password support is not implemented yet for ESP-IDF")
raise NotImplementedError
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(OTAComponent),
cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean,
cv.SplitDefault(CONF_PORT, esp8266=8266, esp32=3232): cv.port,
cv.Optional(CONF_PASSWORD, default=""): cv.string,
cv.Optional(CONF_PASSWORD): cv.All(cv.string, validate_password_support),
cv.Optional(
CONF_REBOOT_TIMEOUT, default="5min"
): cv.positive_time_period_milliseconds,
@@ -76,7 +86,9 @@ CONFIG_SCHEMA = cv.Schema(
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_port(config[CONF_PORT]))
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
if CONF_PASSWORD in config:
cg.add(var.set_auth_password(config[CONF_PASSWORD]))
cg.add_define("USE_OTA_PASSWORD")
await cg.register_component(var, config)
@@ -88,7 +100,7 @@ async def to_code(config):
if CORE.is_esp8266:
cg.add_library("Update", None)
elif CORE.is_esp32:
elif CORE.is_esp32 and CORE.using_arduino:
cg.add_library("Hash", None)
use_state_callback = False

View File

@@ -2,14 +2,31 @@
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/util.h"
#include "esphome/components/network/util.h"
#include <cerrno>
#include <cstdio>
#ifdef USE_ARDUINO
#ifdef USE_OTA_PASSWORD
#include <MD5Builder.h>
#ifdef ARDUINO_ARCH_ESP32
#endif // USE_OTA_PASSWORD
#ifdef USE_ESP32
#include <Update.h>
#endif // USE_ESP32
#endif // USE_ARDUINO
#ifdef USE_ESP8266
#include <Updater.h>
#include "esphome/components/esp8266/preferences.h"
#endif // USE_ESP8266
#ifdef USE_ESP_IDF
#include <esp_ota_ops.h>
#endif
#include <StreamString.h>
namespace esphome {
namespace ota {
@@ -18,19 +35,177 @@ static const char *const TAG = "ota";
static const uint8_t OTA_VERSION_1_0 = 1;
class OTABackend {
public:
virtual ~OTABackend() = default;
virtual OTAResponseTypes begin(size_t image_size) = 0;
virtual void set_update_md5(const char *md5) = 0;
virtual OTAResponseTypes write(uint8_t *data, size_t len) = 0;
virtual OTAResponseTypes end() = 0;
virtual void abort() = 0;
};
#ifdef USE_ARDUINO
class ArduinoOTABackend : public OTABackend {
public:
OTAResponseTypes begin(size_t image_size) override {
bool ret = Update.begin(image_size, U_FLASH);
if (ret) {
#ifdef USE_ESP8266
esp8266::preferences_prevent_write(true);
#endif
return OTA_RESPONSE_OK;
}
uint8_t error = Update.getError();
#ifdef USE_ESP8266
if (error == UPDATE_ERROR_BOOTSTRAP)
return OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING;
if (error == UPDATE_ERROR_NEW_FLASH_CONFIG)
return OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG;
if (error == UPDATE_ERROR_FLASH_CONFIG)
return OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG;
if (error == UPDATE_ERROR_SPACE)
return OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE;
#endif
#ifdef USE_ESP32
if (error == UPDATE_ERROR_SIZE)
return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE;
#endif
return OTA_RESPONSE_ERROR_UNKNOWN;
}
void set_update_md5(const char *md5) override { Update.setMD5(md5); }
OTAResponseTypes write(uint8_t *data, size_t len) override {
size_t written = Update.write(data, len);
if (written != len) {
return OTA_RESPONSE_ERROR_WRITING_FLASH;
}
return OTA_RESPONSE_OK;
}
OTAResponseTypes end() override {
if (!Update.end())
return OTA_RESPONSE_ERROR_UPDATE_END;
return OTA_RESPONSE_OK;
}
void abort() override {
#ifdef USE_ESP32
Update.abort();
#endif
#ifdef USE_ESP8266
Update.end();
esp8266::preferences_prevent_write(false);
#endif
}
};
std::unique_ptr<OTABackend> make_ota_backend() { return make_unique<ArduinoOTABackend>(); }
#endif // USE_ARDUINO
#ifdef USE_ESP_IDF
class IDFOTABackend : public OTABackend {
public:
esp_ota_handle_t update_handle = 0;
OTAResponseTypes begin(size_t image_size) override {
const esp_partition_t *update_partition = esp_ota_get_next_update_partition(nullptr);
if (update_partition == nullptr) {
return OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION;
}
esp_err_t err = esp_ota_begin(update_partition, image_size, &update_handle);
if (err != ESP_OK) {
esp_ota_abort(update_handle);
update_handle = 0;
if (err == ESP_ERR_INVALID_SIZE) {
return OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE;
} else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
return OTA_RESPONSE_ERROR_WRITING_FLASH;
}
return OTA_RESPONSE_ERROR_UNKNOWN;
}
return OTA_RESPONSE_OK;
}
void set_update_md5(const char *md5) override {
// pass
}
OTAResponseTypes write(uint8_t *data, size_t len) override {
esp_err_t err = esp_ota_write(update_handle, data, len);
if (err != ESP_OK) {
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
return OTA_RESPONSE_ERROR_MAGIC;
} else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
return OTA_RESPONSE_ERROR_WRITING_FLASH;
}
return OTA_RESPONSE_ERROR_UNKNOWN;
}
return OTA_RESPONSE_OK;
}
OTAResponseTypes end() override {
esp_err_t err = esp_ota_end(update_handle);
update_handle = 0;
if (err != ESP_OK) {
if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
return OTA_RESPONSE_ERROR_UPDATE_END;
}
return OTA_RESPONSE_ERROR_UNKNOWN;
}
return OTA_RESPONSE_OK;
}
void abort() override { esp_ota_abort(update_handle); }
};
std::unique_ptr<OTABackend> make_ota_backend() { return make_unique<IDFOTABackend>(); }
#endif // USE_ESP_IDF
void OTAComponent::setup() {
this->server_ = make_unique<WiFiServer>(this->port_);
this->server_->begin();
server_ = socket::socket(AF_INET, SOCK_STREAM, 0);
if (server_ == nullptr) {
ESP_LOGW(TAG, "Could not create socket.");
this->mark_failed();
return;
}
int enable = 1;
int err = server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err);
// we can still continue
}
err = server_->setblocking(false);
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err);
this->mark_failed();
return;
}
struct sockaddr_in server;
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = ESPHOME_INADDR_ANY;
server.sin_port = htons(this->port_);
err = server_->bind((struct sockaddr *) &server, sizeof(server));
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno);
this->mark_failed();
return;
}
err = server_->listen(4);
if (err != 0) {
ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno);
this->mark_failed();
return;
}
this->dump_config();
}
void OTAComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Over-The-Air Updates:");
ESP_LOGCONFIG(TAG, " Address: %s:%u", network_get_address().c_str(), this->port_);
ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_);
#ifdef USE_OTA_PASSWORD
if (!this->password_.empty()) {
ESP_LOGCONFIG(TAG, " Using Password.");
}
#endif
if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1) {
ESP_LOGW(TAG, "Last Boot was an unhandled reset, will proceed to safe mode in %d restarts",
this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_);
@@ -57,25 +232,31 @@ void OTAComponent::handle_() {
char *sbuf = reinterpret_cast<char *>(buf);
uint32_t ota_size;
uint8_t ota_features;
std::unique_ptr<OTABackend> backend;
(void) ota_features;
if (!this->client_.connected()) {
this->client_ = this->server_->available();
if (client_ == nullptr) {
struct sockaddr_storage source_addr;
socklen_t addr_len = sizeof(source_addr);
client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len);
}
if (client_ == nullptr)
return;
if (!this->client_.connected())
return;
int enable = 1;
int err = client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
if (err != 0) {
ESP_LOGW(TAG, "Socket could not enable tcp nodelay, errno: %d", errno);
return;
}
// enable nodelay for outgoing data
this->client_.setNoDelay(true);
ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_.remoteIP().toString().c_str());
ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str());
this->status_set_warning();
#ifdef USE_OTA_STATE_CALLBACK
this->state_callback_.call(OTA_STARTED, 0.0f, 0);
#endif
if (!this->wait_receive_(buf, 5)) {
if (!this->readall_(buf, 5)) {
ESP_LOGW(TAG, "Reading magic bytes failed!");
goto error;
}
@@ -88,11 +269,12 @@ void OTAComponent::handle_() {
}
// Send OK and version - 2 bytes
this->client_.write(OTA_RESPONSE_OK);
this->client_.write(OTA_VERSION_1_0);
buf[0] = OTA_RESPONSE_OK;
buf[1] = OTA_VERSION_1_0;
this->writeall_(buf, 2);
// Read features - 1 byte
if (!this->wait_receive_(buf, 1)) {
if (!this->readall_(buf, 1)) {
ESP_LOGW(TAG, "Reading features failed!");
goto error;
}
@@ -100,10 +282,13 @@ void OTAComponent::handle_() {
ESP_LOGV(TAG, "OTA features is 0x%02X", ota_features);
// Acknowledge header - 1 byte
this->client_.write(OTA_RESPONSE_HEADER_OK);
buf[0] = OTA_RESPONSE_HEADER_OK;
this->writeall_(buf, 1);
#ifdef USE_OTA_PASSWORD
if (!this->password_.empty()) {
this->client_.write(OTA_RESPONSE_REQUEST_AUTH);
buf[0] = OTA_RESPONSE_REQUEST_AUTH;
this->writeall_(buf, 1);
MD5Builder md5_builder{};
md5_builder.begin();
sprintf(sbuf, "%08X", random_uint32());
@@ -113,7 +298,7 @@ void OTAComponent::handle_() {
ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf);
// Send nonce, 32 bytes hex MD5
if (this->client_.write(reinterpret_cast<uint8_t *>(sbuf), 32) != 32) {
if (!this->writeall_(reinterpret_cast<uint8_t *>(sbuf), 32)) {
ESP_LOGW(TAG, "Auth: Writing nonce failed!");
goto error;
}
@@ -125,7 +310,7 @@ void OTAComponent::handle_() {
md5_builder.add(sbuf);
// Receive cnonce, 32 bytes hex MD5
if (!this->wait_receive_(buf, 32)) {
if (!this->readall_(buf, 32)) {
ESP_LOGW(TAG, "Auth: Reading cnonce failed!");
goto error;
}
@@ -140,7 +325,7 @@ void OTAComponent::handle_() {
ESP_LOGV(TAG, "Auth: Result is %s", sbuf);
// Receive result, 32 bytes hex MD5
if (!this->wait_receive_(buf + 64, 32)) {
if (!this->writeall_(buf + 64, 32)) {
ESP_LOGW(TAG, "Auth: Reading response failed!");
goto error;
}
@@ -157,12 +342,14 @@ void OTAComponent::handle_() {
goto error;
}
}
#endif // USE_OTA_PASSWORD
// Acknowledge auth OK - 1 byte
this->client_.write(OTA_RESPONSE_AUTH_OK);
buf[0] = OTA_RESPONSE_AUTH_OK;
this->writeall_(buf, 1);
// Read size, 4 bytes MSB first
if (!this->wait_receive_(buf, 4)) {
if (!this->readall_(buf, 4)) {
ESP_LOGW(TAG, "Reading size failed!");
goto error;
}
@@ -173,72 +360,46 @@ void OTAComponent::handle_() {
}
ESP_LOGV(TAG, "OTA size is %u bytes", ota_size);
#ifdef ARDUINO_ARCH_ESP8266
global_preferences.prevent_write(true);
#endif
if (!Update.begin(ota_size, U_FLASH)) {
uint8_t error = Update.getError();
StreamString ss;
Update.printError(ss);
#ifdef ARDUINO_ARCH_ESP8266
if (error == UPDATE_ERROR_BOOTSTRAP) {
error_code = OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING;
goto error;
}
if (error == UPDATE_ERROR_NEW_FLASH_CONFIG) {
error_code = OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG;
goto error;
}
if (error == UPDATE_ERROR_FLASH_CONFIG) {
error_code = OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG;
goto error;
}
if (error == UPDATE_ERROR_SPACE) {
error_code = OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE;
goto error;
}
#endif
#ifdef ARDUINO_ARCH_ESP32
if (error == UPDATE_ERROR_SIZE) {
error_code = OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE;
goto error;
}
#endif
ESP_LOGW(TAG, "Preparing OTA partition failed! '%s'", ss.c_str());
error_code = OTA_RESPONSE_ERROR_UPDATE_PREPARE;
backend = make_ota_backend();
error_code = backend->begin(ota_size);
if (error_code != OTA_RESPONSE_OK)
goto error;
}
update_started = true;
// Acknowledge prepare OK - 1 byte
this->client_.write(OTA_RESPONSE_UPDATE_PREPARE_OK);
buf[0] = OTA_RESPONSE_UPDATE_PREPARE_OK;
this->writeall_(buf, 1);
// Read binary MD5, 32 bytes
if (!this->wait_receive_(buf, 32)) {
if (!this->readall_(buf, 32)) {
ESP_LOGW(TAG, "Reading binary MD5 checksum failed!");
goto error;
}
sbuf[32] = '\0';
ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf);
Update.setMD5(sbuf);
backend->set_update_md5(sbuf);
// Acknowledge MD5 OK - 1 byte
this->client_.write(OTA_RESPONSE_BIN_MD5_OK);
buf[0] = OTA_RESPONSE_BIN_MD5_OK;
this->writeall_(buf, 1);
while (!Update.isFinished()) {
size_t available = this->wait_receive_(buf, 0);
if (!available) {
while (total < ota_size) {
// TODO: timeout check
size_t requested = std::min(sizeof(buf), ota_size - total);
ssize_t read = this->client_->read(buf, requested);
if (read == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
continue;
ESP_LOGW(TAG, "Error receiving data for update, errno: %d", errno);
goto error;
}
uint32_t written = Update.write(buf, available);
if (written != available) {
ESP_LOGW(TAG, "Error writing binary data to flash: %u != %u!", written, available); // NOLINT
error_code = OTA_RESPONSE_ERROR_WRITING_FLASH;
error_code = backend->write(buf, read);
if (error_code != OTA_RESPONSE_OK) {
ESP_LOGW(TAG, "Error writing binary data to flash!");
goto error;
}
total += written;
total += read;
uint32_t now = millis();
if (now - last_progress > 1000) {
@@ -254,24 +415,27 @@ void OTAComponent::handle_() {
}
// Acknowledge receive OK - 1 byte
this->client_.write(OTA_RESPONSE_RECEIVE_OK);
buf[0] = OTA_RESPONSE_RECEIVE_OK;
this->writeall_(buf, 1);
if (!Update.end()) {
error_code = OTA_RESPONSE_ERROR_UPDATE_END;
error_code = backend->end();
if (error_code != OTA_RESPONSE_OK) {
ESP_LOGW(TAG, "Error ending OTA!");
goto error;
}
// Acknowledge Update end OK - 1 byte
this->client_.write(OTA_RESPONSE_UPDATE_END_OK);
buf[0] = OTA_RESPONSE_UPDATE_END_OK;
this->writeall_(buf, 1);
// Read ACK
if (!this->wait_receive_(buf, 1, false) || buf[0] != OTA_RESPONSE_OK) {
if (!this->readall_(buf, 1) || buf[0] != OTA_RESPONSE_OK) {
ESP_LOGW(TAG, "Reading back acknowledgement failed!");
// do not go to error, this is not fatal
}
this->client_.flush();
this->client_.stop();
this->client_->close();
this->client_ = nullptr;
delay(10);
ESP_LOGI(TAG, "OTA update finished!");
this->status_clear_warning();
@@ -282,88 +446,72 @@ void OTAComponent::handle_() {
App.safe_reboot();
error:
if (update_started) {
StreamString ss;
Update.printError(ss);
ESP_LOGW(TAG, "Update end failed! Error: %s", ss.c_str());
}
if (this->client_.connected()) {
this->client_.write(static_cast<uint8_t>(error_code));
this->client_.flush();
}
this->client_.stop();
buf[0] = static_cast<uint8_t>(error_code);
this->writeall_(buf, 1);
this->client_->close();
this->client_ = nullptr;
#ifdef ARDUINO_ARCH_ESP32
if (update_started) {
Update.abort();
if (backend != nullptr && update_started) {
backend->abort();
}
#endif
#ifdef ARDUINO_ARCH_ESP8266
if (update_started) {
Update.end();
}
#endif
this->status_momentary_error("onerror", 5000);
#ifdef USE_OTA_STATE_CALLBACK
this->state_callback_.call(OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
#endif
#ifdef ARDUINO_ARCH_ESP8266
global_preferences.prevent_write(false);
#endif
}
size_t OTAComponent::wait_receive_(uint8_t *buf, size_t bytes, bool check_disconnected) {
size_t available = 0;
bool OTAComponent::readall_(uint8_t *buf, size_t len) {
uint32_t start = millis();
do {
App.feed_wdt();
if (check_disconnected && !this->client_.connected()) {
ESP_LOGW(TAG, "Error client disconnected while receiving data!");
return 0;
}
int availi = this->client_.available();
if (availi < 0) {
ESP_LOGW(TAG, "Error reading data!");
return 0;
}
uint32_t at = 0;
while (len - at > 0) {
uint32_t now = millis();
if (availi == 0 && now - start > 10000) {
ESP_LOGW(TAG, "Timeout waiting for data!");
return 0;
if (now - start > 1000) {
ESP_LOGW(TAG, "Timed out reading %d bytes of data", len);
return false;
}
available = size_t(availi);
yield();
} while (bytes == 0 ? available == 0 : available < bytes);
if (bytes == 0)
bytes = std::min(available, size_t(1024));
bool success = false;
for (uint32_t i = 0; !success && i < 100; i++) {
int res = this->client_.read(buf, bytes);
if (res != int(bytes)) {
// ESP32 implementation has an issue where calling read can fail with EAGAIN (race condition)
// so just re-try it until it works (with generous timeout of 1s)
// because we check with available() first this should not cause us any trouble in all other cases
delay(10);
ssize_t read = this->client_->read(buf + at, len - at);
if (read == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
delay(1);
continue;
}
ESP_LOGW(TAG, "Failed to read %d bytes of data, errno: %d", len, errno);
return false;
} else {
success = true;
at += read;
}
delay(1);
}
if (!success) {
ESP_LOGW(TAG, "Reading %u bytes of binary data failed!", bytes); // NOLINT
return 0;
}
return bytes;
return true;
}
bool OTAComponent::writeall_(const uint8_t *buf, size_t len) {
uint32_t start = millis();
uint32_t at = 0;
while (len - at > 0) {
uint32_t now = millis();
if (now - start > 1000) {
ESP_LOGW(TAG, "Timed out writing %d bytes of data", len);
return false;
}
void OTAComponent::set_auth_password(const std::string &password) { this->password_ = password; }
ssize_t written = this->client_->write(buf + at, len - at);
if (written == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
delay(1);
continue;
}
ESP_LOGW(TAG, "Failed to write %d bytes of data, errno: %d", len, errno);
return false;
} else {
at += written;
}
delay(1);
}
return true;
}
float OTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
uint16_t OTAComponent::get_port() const { return this->port_; }
@@ -373,7 +521,7 @@ bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_
this->safe_mode_start_time_ = millis();
this->safe_mode_enable_time_ = enable_time;
this->safe_mode_num_attempts_ = num_attempts;
this->rtc_ = global_preferences.make_preference<uint32_t>(233825507UL, false);
this->rtc_ = global_preferences->make_preference<uint32_t>(233825507UL, false);
this->safe_mode_rtc_value_ = this->read_rtc_();
ESP_LOGCONFIG(TAG, "There have been %u suspected unsuccessful boot attempts.", this->safe_mode_rtc_value_);

View File

@@ -1,12 +1,10 @@
#pragma once
#include <memory>
#include "esphome/components/socket/socket.h"
#include "esphome/core/component.h"
#include "esphome/core/preferences.h"
#include "esphome/core/helpers.h"
#include <WiFiServer.h>
#include <WiFiClient.h>
#include "esphome/core/defines.h"
namespace esphome {
namespace ota {
@@ -32,6 +30,7 @@ enum OTAResponseTypes {
OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 135,
OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 136,
OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 137,
OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 138,
OTA_RESPONSE_ERROR_UNKNOWN = 255,
};
@@ -40,14 +39,9 @@ enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR };
/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA.
class OTAComponent : public Component {
public:
/** Set a plaintext password that OTA will use for authentication.
*
* Warning: This password will be stored in plaintext in the ROM and can be read
* by intruders.
*
* @param password The plaintext password.
*/
void set_auth_password(const std::string &password);
#ifdef USE_OTA_PASSWORD
void set_auth_password(const std::string &password) { password_ = password; }
#endif // USE_OTA_PASSWORD
/// Manually set the port OTA should listen on.
void set_port(uint16_t port);
@@ -76,14 +70,17 @@ class OTAComponent : public Component {
uint32_t read_rtc_();
void handle_();
size_t wait_receive_(uint8_t *buf, size_t bytes, bool check_disconnected = true);
bool readall_(uint8_t *buf, size_t len);
bool writeall_(const uint8_t *buf, size_t len);
#ifdef USE_OTA_PASSWORD
std::string password_;
#endif // USE_OTA_PASSWORD
uint16_t port_;
std::unique_ptr<WiFiServer> server_{nullptr};
WiFiClient client_{};
std::unique_ptr<socket::Socket> server_;
std::unique_ptr<socket::Socket> client_;
bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled.
uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled.