mirror of
https://github.com/esphome/esphome.git
synced 2026-09-18 02:28:42 +00:00
@@ -48,7 +48,7 @@ PROJECT_NAME = ESPHome
|
||||
# could be handy for archiving the generated documentation or if some version
|
||||
# control system is used.
|
||||
|
||||
PROJECT_NUMBER = 2026.9.0b3
|
||||
PROJECT_NUMBER = 2026.9.0b4
|
||||
|
||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
||||
# for a project that appears at the top of each page and should give viewer a
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ RUN \
|
||||
-r /requirements.txt
|
||||
|
||||
# Install the ESPHome Device Builder dashboard.
|
||||
RUN uv pip install --no-cache-dir esphome-device-builder==1.14.5
|
||||
RUN uv pip install --no-cache-dir esphome-device-builder==1.14.9
|
||||
|
||||
RUN \
|
||||
platformio settings set enable_telemetry No \
|
||||
|
||||
@@ -350,10 +350,9 @@ CONFIG_SCHEMA = cv.All(
|
||||
ln882x=5, # Moderate RAM
|
||||
nrf52=4, # ~256KB RAM, BSD sockets, Thread (single HA controller)
|
||||
): cv.int_range(min=1, max=20),
|
||||
# Maximum queued send buffers per connection before dropping connection
|
||||
# Each buffer uses ~8-12 bytes overhead plus actual message size
|
||||
# Max queued messages per connection, and 2 KB of backlog per slot up
|
||||
# to 64 KB (a lone message is exempt), before the connection is dropped
|
||||
# Platform defaults based on available RAM and typical message rates:
|
||||
# CONF_MAX_SEND_QUEUE defaults are power of 2 for efficient modulo
|
||||
cv.SplitDefault(
|
||||
CONF_MAX_SEND_QUEUE,
|
||||
esp8266=4, # Limited RAM, need to fail fast
|
||||
|
||||
@@ -1,20 +1,37 @@
|
||||
#include "api_buffer.h"
|
||||
#include <new>
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
#include "esphome/core/log.h"
|
||||
#endif
|
||||
|
||||
namespace esphome::api {
|
||||
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
void APIBuffer::debug_check_drop_(size_t drop) const {
|
||||
if (drop > this->size_) {
|
||||
ESP_LOGE("api.buffer", "drop_front: drop=%zu size=%u", drop, this->size_);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
bool APIBuffer::grow_(size_t n) {
|
||||
// nothrow (no zero-fill) so OOM is reportable; plain new aborts instead
|
||||
// (NEW_OOM_ABORT on ESP8266 Arduino, exception stub on ESP-IDF).
|
||||
// RAMAllocator is no fit here: unique_ptr needs delete[]-compatible memory.
|
||||
std::unique_ptr<uint8_t[]> new_data(new (std::nothrow) uint8_t[n]);
|
||||
if (new_data == nullptr)
|
||||
if (n > MAX_SIZE)
|
||||
return false;
|
||||
if (this->size_)
|
||||
std::memcpy(new_data.get(), this->data_.get(), this->size_);
|
||||
this->data_ = std::move(new_data);
|
||||
// realloc extends in place when it can, avoiding the copy
|
||||
uint8_t *grown = RAMAllocator<uint8_t>().reallocate(this->data_.get(), n);
|
||||
if (grown == nullptr)
|
||||
return false;
|
||||
(void) this->data_.release(); // realloc already freed or reused the old block
|
||||
this->data_.reset(grown);
|
||||
this->capacity_ = n;
|
||||
return true;
|
||||
}
|
||||
|
||||
uint8_t *APIBuffer::append(size_t n) {
|
||||
const size_t old_size = this->size_;
|
||||
if (!this->resize(old_size + n))
|
||||
return nullptr;
|
||||
return this->data_.get() + old_size;
|
||||
}
|
||||
|
||||
} // namespace esphome::api
|
||||
|
||||
@@ -25,6 +25,7 @@ namespace esphome::api {
|
||||
/// writes in debug builds.
|
||||
class APIBuffer {
|
||||
public:
|
||||
static constexpr size_t MAX_SIZE = UINT16_MAX; // API frames carry 16 bit lengths
|
||||
void clear() { this->size_ = 0; }
|
||||
/// Returns false if allocation fails; the buffer is left unchanged.
|
||||
[[nodiscard]] inline bool reserve(size_t n) ESPHOME_ALWAYS_INLINE { return n <= this->capacity_ || this->grow_(n); }
|
||||
@@ -36,9 +37,19 @@ class APIBuffer {
|
||||
[[nodiscard]] inline bool reserve_and_resize(size_t reserve_size, size_t new_size) ESPHOME_ALWAYS_INLINE {
|
||||
if (!this->reserve(std::max(reserve_size, new_size)))
|
||||
return false;
|
||||
this->size_ = new_size;
|
||||
this->size_ = static_cast<uint16_t>(new_size);
|
||||
return true;
|
||||
}
|
||||
/// Grow by n bytes; returns the new bytes, or nullptr on allocation failure.
|
||||
[[nodiscard]] uint8_t *append(size_t n);
|
||||
/// Drop the first `drop` bytes, sliding the rest down. Precondition: drop <= size().
|
||||
void drop_front(size_t drop) {
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
this->debug_check_drop_(drop);
|
||||
#endif
|
||||
this->size_ -= drop;
|
||||
std::memmove(this->data_.get(), this->data_.get() + drop, this->size_);
|
||||
}
|
||||
uint8_t *data() { return this->data_.get(); }
|
||||
const uint8_t *data() const { return this->data_.get(); }
|
||||
size_t size() const { return this->size_; }
|
||||
@@ -55,9 +66,14 @@ class APIBuffer {
|
||||
|
||||
protected:
|
||||
bool grow_(size_t n);
|
||||
std::unique_ptr<uint8_t[]> data_;
|
||||
size_t size_{0};
|
||||
size_t capacity_{0};
|
||||
#ifdef ESPHOME_DEBUG_API
|
||||
void debug_check_drop_(size_t drop) const;
|
||||
#endif
|
||||
// RAMAllocator: PSRAM when available, and it reports failure where
|
||||
// new (std::nothrow) still aborts on ESP-IDF without exceptions
|
||||
RAMUniquePtr<uint8_t[]> data_;
|
||||
uint16_t size_{0};
|
||||
uint16_t capacity_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome::api
|
||||
|
||||
@@ -364,7 +364,10 @@ void APIConnection::check_keepalive_(uint32_t now) {
|
||||
ESP_LOGVV(TAG, "Sending keepalive PING");
|
||||
PingRequest req;
|
||||
this->flags_.sent_ping = this->send_message(req);
|
||||
if (!this->flags_.sent_ping) {
|
||||
if (this->flags_.sent_ping) {
|
||||
// Quiet for a keepalive period and the ping is on its way: a one-off stall's storage can go
|
||||
this->helper_->release_overflow_buffer();
|
||||
} else {
|
||||
// If we can't send the ping request directly (tx_buffer full),
|
||||
// schedule it at the front of the batch so it will be sent with priority
|
||||
ESP_LOGW(TAG, "Buffer full, ping queued");
|
||||
|
||||
@@ -171,7 +171,7 @@ APIError APIFrameHelper::write_raw_iov_(const struct iovec *iov, int iovcnt, uin
|
||||
return APIError::OK;
|
||||
|
||||
// Queue unsent data into overflow buffer
|
||||
if (!this->overflow_buf_.enqueue_iov(iov, iovcnt, total_write_len, static_cast<uint16_t>(sent))) {
|
||||
if (!this->overflow_buf_.enqueue_iov(iov, iovcnt, total_write_len, sent)) {
|
||||
HELPER_LOG("Overflow buffer full or out of memory, dropping connection");
|
||||
this->state_ = State::FAILED;
|
||||
return APIError::SOCKET_WRITE_FAILED;
|
||||
|
||||
@@ -219,7 +219,10 @@ class APIFrameHelper {
|
||||
if (this->rx_buf_len_ == 0) {
|
||||
this->rx_buf_.release();
|
||||
}
|
||||
this->release_overflow_buffer();
|
||||
}
|
||||
// Free the send backlog storage once it has drained
|
||||
void release_overflow_buffer() { this->overflow_buf_.release(); }
|
||||
|
||||
protected:
|
||||
// Drain backlogged overflow data to the socket and handle errors.
|
||||
|
||||
@@ -67,15 +67,15 @@ APIError APINoiseFrameHelper::init() {
|
||||
}
|
||||
|
||||
// init prologue
|
||||
size_t old_size = prologue_.size();
|
||||
if (!prologue_.resize(old_size + PROLOGUE_INIT_LEN)) [[unlikely]] {
|
||||
uint8_t *dst = prologue_.append(PROLOGUE_INIT_LEN);
|
||||
if (dst == nullptr) [[unlikely]] {
|
||||
state_ = State::FAILED;
|
||||
return APIError::OUT_OF_MEMORY;
|
||||
}
|
||||
#ifdef USE_ESP8266
|
||||
memcpy_P(prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
memcpy_P(dst, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
#else
|
||||
std::memcpy(prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
std::memcpy(dst, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
|
||||
#endif
|
||||
|
||||
state_ = State::CLIENT_HELLO;
|
||||
@@ -272,17 +272,17 @@ APIError APINoiseFrameHelper::state_action_client_hello_() {
|
||||
return handle_handshake_frame_error_(aerr);
|
||||
}
|
||||
// ignore contents, may be used in future for flags
|
||||
// Resize for: existing prologue + 2 size bytes + frame data
|
||||
size_t old_size = this->prologue_.size();
|
||||
// Append 2 size bytes + frame data to the prologue
|
||||
size_t rx_size = this->rx_buf_.size();
|
||||
if (!this->prologue_.resize(old_size + 2 + rx_size)) [[unlikely]] {
|
||||
uint8_t *dst = this->prologue_.append(2 + rx_size);
|
||||
if (dst == nullptr) [[unlikely]] {
|
||||
state_ = State::FAILED;
|
||||
return APIError::OUT_OF_MEMORY;
|
||||
}
|
||||
this->prologue_[old_size] = (uint8_t) (rx_size >> 8);
|
||||
this->prologue_[old_size + 1] = (uint8_t) rx_size;
|
||||
dst[0] = (uint8_t) (rx_size >> 8);
|
||||
dst[1] = (uint8_t) rx_size;
|
||||
if (rx_size > 0) {
|
||||
std::memcpy(this->prologue_.data() + old_size + 2, this->rx_buf_.data(), rx_size);
|
||||
std::memcpy(dst + 2, this->rx_buf_.data(), rx_size);
|
||||
}
|
||||
|
||||
state_ = State::SERVER_HELLO;
|
||||
|
||||
@@ -1,98 +1,91 @@
|
||||
#include "api_overflow_buffer.h"
|
||||
#ifdef USE_API
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
|
||||
namespace esphome::api {
|
||||
|
||||
APIOverflowBuffer::~APIOverflowBuffer() {
|
||||
for (auto *entry : this->queue_) {
|
||||
if (entry != nullptr)
|
||||
Entry::destroy(entry);
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t APIOverflowBuffer::try_drain(socket::Socket *socket) {
|
||||
// socket->write() can re-enter this function: a log message emitted from an
|
||||
// lwip callback during the write goes out over the API and lands back in the
|
||||
// frame helper's write/drain path. If a nested drain ran here it would send
|
||||
// and free the entry the outer drain is still holding, causing a double free.
|
||||
// Report "no progress" instead; the outer drain keeps draining, and the
|
||||
// nested send is enqueued behind the existing backlog.
|
||||
// Nested call from inside socket->write(); see draining_
|
||||
if (this->draining_)
|
||||
return 0;
|
||||
|
||||
// RAII so the flag is cleared on every return path
|
||||
struct DrainGuard {
|
||||
explicit DrainGuard(bool &flag) : flag_(flag) { flag_ = true; }
|
||||
~DrainGuard() { this->flag_ = false; }
|
||||
bool &flag_;
|
||||
} guard(this->draining_);
|
||||
APIOverflowBuffer &owner;
|
||||
~DrainGuard() { this->owner.draining_ = false; }
|
||||
} guard{*this};
|
||||
this->draining_ = true;
|
||||
|
||||
while (this->count_ > 0) {
|
||||
Entry *front = this->queue_[this->head_];
|
||||
uint8_t *msg = this->buf_.data() + this->head_;
|
||||
size_t len = msg[0] | (msg[1] << 8);
|
||||
|
||||
ssize_t sent = socket->write(front->current_data(), front->remaining());
|
||||
|
||||
if (sent <= 0) {
|
||||
// -1 = error (caller checks errno for EWOULDBLOCK vs hard error)
|
||||
// 0 = nothing sent (treat as no progress)
|
||||
ssize_t sent = socket->write(msg + LEN_PREFIX, len);
|
||||
if (sent <= 0)
|
||||
return sent;
|
||||
if (static_cast<size_t>(sent) < len) {
|
||||
// Step past the sent bytes and rewrite the prefix there; it lands on bytes already sent
|
||||
this->head_ += sent;
|
||||
len -= sent;
|
||||
msg += sent;
|
||||
msg[0] = len;
|
||||
msg[1] = len >> 8;
|
||||
return sent;
|
||||
}
|
||||
|
||||
if (static_cast<uint16_t>(sent) < front->remaining()) {
|
||||
// Partially sent, update offset and stop
|
||||
front->offset += static_cast<uint16_t>(sent);
|
||||
return sent;
|
||||
}
|
||||
|
||||
// Entry fully sent — unlink it before freeing so a freed pointer is never
|
||||
// reachable from the queue
|
||||
this->queue_[this->head_] = nullptr;
|
||||
this->head_ = (this->head_ + 1) % API_MAX_SEND_QUEUE;
|
||||
this->head_ += LEN_PREFIX + len;
|
||||
this->count_--;
|
||||
Entry::destroy(front);
|
||||
}
|
||||
|
||||
return 0; // All drained
|
||||
this->head_ = 0;
|
||||
if (this->release_when_drained_) {
|
||||
this->release_when_drained_ = false;
|
||||
this->buf_.release();
|
||||
} else {
|
||||
this->buf_.clear();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool APIOverflowBuffer::enqueue_iov(const struct iovec *iov, int iovcnt, uint16_t total_len, uint16_t skip) {
|
||||
bool APIOverflowBuffer::enqueue_iov(const struct iovec *iov, int iovcnt, size_t total_len, size_t skip) {
|
||||
if (this->count_ >= API_MAX_SEND_QUEUE)
|
||||
return false;
|
||||
|
||||
uint16_t buffer_size = total_len - skip;
|
||||
// nothrow: a failed allocation returns nullptr so the connection is dropped
|
||||
// cleanly instead of plain new's crash or abort on OOM
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
|
||||
auto *data = new (std::nothrow) uint8_t[buffer_size];
|
||||
if (data == nullptr)
|
||||
return false;
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
|
||||
auto *entry = new (std::nothrow) Entry{data, buffer_size, 0};
|
||||
if (entry == nullptr) {
|
||||
delete[] data;
|
||||
const size_t new_len = total_len - skip;
|
||||
const size_t new_bytes = LEN_PREFIX + new_len;
|
||||
const size_t live = this->buf_.size() - this->head_;
|
||||
// A lone message is only bound by the buffer; refusing it would just drop the connection
|
||||
if (live + new_bytes > (this->count_ > 0 ? MAX_BYTES : MAX_LONE_BYTES))
|
||||
return false;
|
||||
|
||||
if (this->buf_.size() + new_bytes > this->buf_.capacity()) {
|
||||
// Storage would move under an outer drain's write()
|
||||
if (this->draining_)
|
||||
return false;
|
||||
if (this->head_ > 0) {
|
||||
// Reclaim the sent prefix before growing
|
||||
this->buf_.drop_front(this->head_);
|
||||
this->head_ = 0;
|
||||
}
|
||||
if (!this->buf_.reserve(reserve_for(live + new_bytes)))
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t to_skip = skip;
|
||||
uint16_t write_pos = 0;
|
||||
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
if (to_skip >= iov[i].iov_len) {
|
||||
to_skip -= static_cast<uint16_t>(iov[i].iov_len);
|
||||
uint8_t *dst = this->buf_.append(new_bytes);
|
||||
if (dst == nullptr)
|
||||
return false;
|
||||
dst[0] = new_len;
|
||||
dst[1] = new_len >> 8;
|
||||
dst += LEN_PREFIX;
|
||||
for (const struct iovec *end = iov + iovcnt; iov != end; iov++) {
|
||||
if (skip >= iov->iov_len) {
|
||||
skip -= iov->iov_len;
|
||||
} else {
|
||||
const uint8_t *src = reinterpret_cast<uint8_t *>(iov[i].iov_base) + to_skip;
|
||||
uint16_t len = static_cast<uint16_t>(iov[i].iov_len) - to_skip;
|
||||
std::memcpy(entry->data + write_pos, src, len);
|
||||
write_pos += len;
|
||||
to_skip = 0;
|
||||
const size_t len = iov->iov_len - skip;
|
||||
std::memcpy(dst, static_cast<const uint8_t *>(iov->iov_base) + skip, len);
|
||||
dst += len;
|
||||
skip = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Publish only after the copy completes so a half-built entry is never reachable
|
||||
this->queue_[this->tail_] = entry;
|
||||
this->tail_ = (this->tail_ + 1) % API_MAX_SEND_QUEUE;
|
||||
this->count_++;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -8,71 +9,57 @@
|
||||
|
||||
#include "esphome/components/socket/headers.h"
|
||||
#include "esphome/components/socket/socket.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "api_buffer.h"
|
||||
|
||||
namespace esphome::api {
|
||||
|
||||
/// Circular queue of heap-allocated byte buffers used as a TCP send backlog.
|
||||
///
|
||||
/// Under normal operation this buffer is **never used** — data goes straight
|
||||
/// from the frame helper to the socket. It only fills when the LWIP TCP
|
||||
/// send buffer is full (slow client, congested network, heavy logging).
|
||||
/// The queue drains automatically on subsequent write/loop calls once the
|
||||
/// socket becomes writable again.
|
||||
///
|
||||
/// Capacity is compile-time-fixed via API_MAX_SEND_QUEUE (set from Python
|
||||
/// config). If the queue fills completely the connection is marked failed.
|
||||
/// TCP send backlog, only used when the socket send buffer is full.
|
||||
/// One contiguous buffer per connection, allocated on the first stall and
|
||||
/// kept at its high-water mark so a lossy link does not churn the heap.
|
||||
/// Messages are stored as a 2 byte length prefix plus payload.
|
||||
/// API_MAX_SEND_QUEUE bounds queued messages and, at 2 KB per slot, queued
|
||||
/// bytes; exceeding either fails the connection.
|
||||
class APIOverflowBuffer {
|
||||
public:
|
||||
/// A single heap-allocated send-backlog entry.
|
||||
/// Lifetime is manually managed — see destroy().
|
||||
struct Entry {
|
||||
uint8_t *data;
|
||||
uint16_t size; // Total size of the buffer
|
||||
uint16_t offset; // Current send offset within the buffer
|
||||
|
||||
uint16_t remaining() const { return this->size - this->offset; }
|
||||
const uint8_t *current_data() const { return this->data + this->offset; }
|
||||
|
||||
/// Free this entry and its data buffer.
|
||||
static ESPHOME_ALWAYS_INLINE void destroy(Entry *entry) {
|
||||
delete[] entry->data;
|
||||
delete entry; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
};
|
||||
|
||||
~APIOverflowBuffer();
|
||||
|
||||
/// True when no backlogged data is waiting.
|
||||
bool empty() const { return this->count_ == 0; }
|
||||
|
||||
/// True when the queue has no room for another entry.
|
||||
bool full() const { return this->count_ >= API_MAX_SEND_QUEUE; }
|
||||
|
||||
/// Number of entries currently queued.
|
||||
uint8_t count() const { return this->count_; }
|
||||
|
||||
/// Try to drain queued data to the socket.
|
||||
/// Returns bytes-written > 0 on success/partial, 0 if all drained or no progress,
|
||||
/// -1 on error (caller must check errno to distinguish EWOULDBLOCK from hard errors).
|
||||
/// Callers only need to act on -1; 0 and positive values both mean "no error".
|
||||
/// Frees entries as they are fully sent.
|
||||
/// Drain queued messages to the socket.
|
||||
/// Returns bytes written, 0 for a re-entrant call, -1 on error (check errno
|
||||
/// for EWOULDBLOCK); callers only need to act on -1.
|
||||
ssize_t try_drain(socket::Socket *socket);
|
||||
|
||||
/// Enqueue unsent IOV data into the backlog.
|
||||
/// Copies iov data starting at byte offset `skip` into a new entry.
|
||||
/// Returns false if the queue is full or allocation fails (caller should fail the connection).
|
||||
bool enqueue_iov(const struct iovec *iov, int iovcnt, uint16_t total_len, uint16_t skip);
|
||||
/// Queue iov data from byte offset `skip` as one message.
|
||||
/// Returns false when a limit is hit, allocation fails, or storage would move
|
||||
/// during a drain; the caller should fail the connection.
|
||||
bool enqueue_iov(const struct iovec *iov, int iovcnt, size_t total_len, size_t skip);
|
||||
|
||||
/// Free the retained storage, now if empty, otherwise once it has drained.
|
||||
void release() {
|
||||
if (this->count_ == 0) {
|
||||
this->buf_.release();
|
||||
} else {
|
||||
this->release_when_drained_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
std::array<Entry *, API_MAX_SEND_QUEUE> queue_{};
|
||||
uint8_t head_{0};
|
||||
uint8_t tail_{0};
|
||||
static constexpr size_t LEN_PREFIX = 2;
|
||||
static constexpr size_t BYTES_PER_SLOT = 2048;
|
||||
// Reserve in 256 byte steps so a creeping high-water mark settles quickly
|
||||
static constexpr size_t GROW_QUANTUM = 256;
|
||||
// Lone message ceiling, rounded down so reserve_for() never exceeds the buffer limit
|
||||
static constexpr size_t MAX_LONE_BYTES = APIBuffer::MAX_SIZE & ~(GROW_QUANTUM - 1);
|
||||
static constexpr size_t MAX_BYTES = std::min(API_MAX_SEND_QUEUE * BYTES_PER_SLOT, MAX_LONE_BYTES);
|
||||
static constexpr size_t reserve_for(size_t want) { return (want + GROW_QUANTUM - 1) & ~(GROW_QUANTUM - 1); }
|
||||
|
||||
APIBuffer buf_;
|
||||
uint16_t head_{0}; // offset of the front message's length prefix; bytes before it are sent
|
||||
uint8_t count_{0};
|
||||
// Guards against re-entrant drains: socket->write() can re-enter the API
|
||||
// send path (e.g. a log message emitted from an lwip callback), and a nested
|
||||
// drain would free the entry the outer drain is still holding.
|
||||
bool draining_{false};
|
||||
// socket->write() can re-enter the send path (log from an lwip callback):
|
||||
// a nested drain makes no progress and a nested enqueue never moves storage
|
||||
bool draining_ : 1 {false};
|
||||
bool release_when_drained_ : 1 {false};
|
||||
};
|
||||
|
||||
} // namespace esphome::api
|
||||
|
||||
@@ -45,15 +45,7 @@ void BluedroidGattClient::setup() {
|
||||
|
||||
void BluedroidGattClient::loop() {
|
||||
if (!esp32_ble::global_ble->is_active()) {
|
||||
// Stack down: no CLOSE_EVT will come. Settle a live link so the consumer
|
||||
// frees its slot, then re-register the app on the next enable.
|
||||
auto down_st = this->state();
|
||||
if (down_st != ClientState::IDLE && down_st != ClientState::INIT) {
|
||||
this->release_services();
|
||||
this->set_idle_();
|
||||
this->listener_->on_connection_state(false, 0, ble_device_base::GATT_ERR_NOT_CONNECTED);
|
||||
}
|
||||
this->set_state(ClientState::INIT);
|
||||
// ble_before_disabled_event_handler() settles the slot.
|
||||
return;
|
||||
}
|
||||
auto st = this->state();
|
||||
@@ -65,7 +57,7 @@ void BluedroidGattClient::loop() {
|
||||
ESP_LOGE(TAG, "gattc app register failed: app_id=%d code=%d", this->app_id, ret);
|
||||
this->mark_failed();
|
||||
}
|
||||
// Do not wait for REG_EVT; a dropped event must not wedge the slot.
|
||||
// Do not wait for REG_EVT; connect() rejects until it lands.
|
||||
this->set_idle_();
|
||||
} else if (st == ClientState::DISCONNECTING || this->disconnect_pending()) {
|
||||
// The one teardown safety net: a lost CLOSE_EVT, or a scheduled
|
||||
@@ -78,8 +70,8 @@ void BluedroidGattClient::loop() {
|
||||
this->listener_->on_connection_state(false, 0, ESP_GATT_CONN_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
// The loop stays on while a link exists (stack-down watch, pre-started
|
||||
// search flush); it settles only back at IDLE.
|
||||
// The loop stays on while a link exists (pre-started search flush); it
|
||||
// settles only back at IDLE.
|
||||
this->deliver_pending_search_();
|
||||
if (this->state() == ClientState::IDLE) {
|
||||
this->disable_loop();
|
||||
@@ -87,6 +79,22 @@ void BluedroidGattClient::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// Stack down: no CLOSE_EVT will come. Settle a live link so the consumer
|
||||
// frees its slot, then register the app again on the next enable.
|
||||
void BluedroidGattClient::ble_before_disabled_event_handler() {
|
||||
auto st = this->state();
|
||||
if (st != ClientState::IDLE && st != ClientState::INIT) {
|
||||
this->release_services();
|
||||
this->set_idle_();
|
||||
this->listener_->on_connection_state(false, 0, ble_device_base::GATT_ERR_NOT_CONNECTED);
|
||||
}
|
||||
// The interface belongs to the torn-down stack.
|
||||
this->gattc_if_ = ESP_GATT_IF_NONE;
|
||||
this->set_state(ClientState::INIT);
|
||||
// An idle slot runs no loop; the INIT branch must run to register again.
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void BluedroidGattClient::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Bluedroid GATT client %d", this->connection_index_);
|
||||
if (this->is_failed()) {
|
||||
@@ -97,6 +105,11 @@ void BluedroidGattClient::dump_config() {
|
||||
// ---- contract ops ----
|
||||
|
||||
int BluedroidGattClient::connect(uint64_t address, uint8_t addr_type) {
|
||||
if (this->gattc_if_ == ESP_GATT_IF_NONE) {
|
||||
// Bluedroid drops an open on an unknown interface without any event.
|
||||
ESP_LOGW(TAG, "[%d] Connect rejected, GATT app not registered", this->connection_index_);
|
||||
return ble_device_base::GATT_ERR_NOT_CONNECTED;
|
||||
}
|
||||
// Only from idle: clobbering DISCONNECTING would open a new link the
|
||||
// stale CLOSE_EVT then tears down.
|
||||
if (this->state() != ClientState::IDLE) {
|
||||
|
||||
@@ -56,6 +56,7 @@ class BluedroidGattClient final : public esp32_ble_tracker::ESPBTClient, public
|
||||
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
|
||||
void connect() override;
|
||||
void disconnect() override;
|
||||
void ble_before_disabled_event_handler() override;
|
||||
bool wants_parsed_advertisements() override { return false; }
|
||||
void on_scan_end() override {}
|
||||
bool parse_device(const ble_device_base::ESPBTDevice &device) override { return false; }
|
||||
|
||||
@@ -83,18 +83,23 @@ void ESP32BLE::setup() {
|
||||
}
|
||||
}
|
||||
|
||||
void ESP32BLE::enable() {
|
||||
if (this->state_ != BLE_COMPONENT_STATE_DISABLED)
|
||||
return;
|
||||
|
||||
this->state_ = BLE_COMPONENT_STATE_ENABLE;
|
||||
}
|
||||
|
||||
void ESP32BLE::disable() {
|
||||
if (this->state_ == BLE_COMPONENT_STATE_DISABLED)
|
||||
return;
|
||||
|
||||
this->state_ = BLE_COMPONENT_STATE_DISABLE;
|
||||
// Queue the transition for loop(). A pending transition the other way is
|
||||
// cancelled instead, since nothing was torn down or brought up yet; any other
|
||||
// state is already there or on its way.
|
||||
void ESP32BLE::request_state_(bool enable) {
|
||||
if (enable) {
|
||||
if (this->state_ == BLE_COMPONENT_STATE_DISABLED) {
|
||||
this->state_ = BLE_COMPONENT_STATE_ENABLE;
|
||||
} else if (this->state_ == BLE_COMPONENT_STATE_DISABLE) {
|
||||
this->state_ = BLE_COMPONENT_STATE_ACTIVE;
|
||||
}
|
||||
} else {
|
||||
if (this->state_ == BLE_COMPONENT_STATE_ACTIVE) {
|
||||
this->state_ = BLE_COMPONENT_STATE_DISABLE;
|
||||
} else if (this->state_ == BLE_COMPONENT_STATE_ENABLE) {
|
||||
this->state_ = BLE_COMPONENT_STATE_DISABLED;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_ESP32_BLE_ADVERTISING
|
||||
@@ -580,7 +585,11 @@ void ESP32BLE::loop_handle_state_transition_not_active_() {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
this->state_ = BLE_COMPONENT_STATE_DISABLED;
|
||||
this->drain_ble_events_();
|
||||
// A status callback may have asked for BLE back; the stack is down now, so
|
||||
// that request becomes a bring-up.
|
||||
this->state_ =
|
||||
this->state_ == BLE_COMPONENT_STATE_ACTIVE ? BLE_COMPONENT_STATE_ENABLE : BLE_COMPONENT_STATE_DISABLED;
|
||||
} else if (this->state_ == BLE_COMPONENT_STATE_ENABLE) {
|
||||
ESP_LOGD(TAG, "Enabling");
|
||||
this->state_ = BLE_COMPONENT_STATE_OFF;
|
||||
|
||||
@@ -102,8 +102,8 @@ class ESP32BLE final : public Component {
|
||||
}
|
||||
uint32_t get_advertising_cycle_time() const { return this->advertising_cycle_time_; }
|
||||
|
||||
void enable();
|
||||
void disable();
|
||||
void enable() { this->request_state_(true); }
|
||||
void disable() { this->request_state_(false); }
|
||||
ESPHOME_ALWAYS_INLINE bool is_active() { return this->state_ == BLE_COMPONENT_STATE_ACTIVE; }
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
@@ -176,6 +176,15 @@ class ESP32BLE final : public Component {
|
||||
|
||||
bool ble_setup_();
|
||||
bool ble_dismantle_();
|
||||
void request_state_(bool enable);
|
||||
// Drop what the old stack queued; the next stack reuses the same interface ids.
|
||||
void drain_ble_events_() {
|
||||
BLEEvent *ble_event;
|
||||
while ((ble_event = this->ble_events_.pop()) != nullptr) {
|
||||
this->ble_event_pool_.release(ble_event);
|
||||
}
|
||||
this->ble_events_.get_and_reset_dropped_count();
|
||||
}
|
||||
bool ble_pre_setup_();
|
||||
#ifdef USE_ESP32_BLE_ADVERTISING
|
||||
void advertising_init_();
|
||||
|
||||
@@ -42,7 +42,7 @@ void BLEClientBase::set_state(espbt::ClientState st) {
|
||||
|
||||
void BLEClientBase::loop() {
|
||||
if (!esp32_ble::global_ble->is_active()) {
|
||||
this->set_state(espbt::ClientState::INIT);
|
||||
// ble_before_disabled_event_handler() resets the client.
|
||||
return;
|
||||
}
|
||||
if (this->state() == espbt::ClientState::INIT) {
|
||||
@@ -72,6 +72,21 @@ void BLEClientBase::loop() {
|
||||
|
||||
float BLEClientBase::get_setup_priority() const { return setup_priority::AFTER_BLUETOOTH; }
|
||||
|
||||
void BLEClientBase::ble_before_disabled_event_handler() {
|
||||
auto st = this->state();
|
||||
if (st != espbt::ClientState::IDLE && st != espbt::ClientState::INIT) {
|
||||
// No CLOSE_EVT will come: free the services and settle the link.
|
||||
this->release_services();
|
||||
this->set_idle_();
|
||||
this->on_disconnect_complete(ESP_GATT_CONN_TERMINATE_LOCAL_HOST);
|
||||
}
|
||||
// The interface belongs to the torn-down stack.
|
||||
this->gattc_if_ = ESP_GATT_IF_NONE;
|
||||
this->set_state(espbt::ClientState::INIT);
|
||||
// An idle client runs no loop; the INIT branch must run to register again.
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void BLEClientBase::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Address: %s\n"
|
||||
@@ -93,6 +108,10 @@ bool BLEClientBase::parse_device(const espbt::ESPBTDevice &device) {
|
||||
return false;
|
||||
if (this->state() != espbt::ClientState::IDLE)
|
||||
return false;
|
||||
// Not registered on this stack yet; promoting now would stop the scan for a
|
||||
// connect that connect() rejects anyway.
|
||||
if (this->gattc_if_ == ESP_GATT_IF_NONE)
|
||||
return false;
|
||||
|
||||
this->log_event_("Found device");
|
||||
if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG)
|
||||
@@ -117,6 +136,15 @@ void BLEClientBase::connect() {
|
||||
this->connection_index_, this->address_str_);
|
||||
return;
|
||||
}
|
||||
if (this->gattc_if_ == ESP_GATT_IF_NONE) {
|
||||
// Bluedroid drops an open on an unknown interface without any event.
|
||||
this->log_warning_("Connect rejected, GATT app not registered");
|
||||
// INIT stays so loop() still registers; only a promoted client goes back.
|
||||
if (this->state() == espbt::ClientState::DISCOVERED) {
|
||||
this->set_state(espbt::ClientState::IDLE);
|
||||
}
|
||||
return;
|
||||
}
|
||||
ESP_LOGI(TAG, "[%d] [%s] 0x%02x Connecting", this->connection_index_, this->address_str_, this->remote_addr_type_);
|
||||
this->paired_ = false;
|
||||
// A registration whose event never arrived must not block this connection's release.
|
||||
@@ -199,7 +227,10 @@ void BLEClientBase::release_services() {
|
||||
#ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH
|
||||
// Only the cache clean makes the stack's database unsafe to walk.
|
||||
this->services_released_ = true;
|
||||
esp_ble_gattc_cache_clean(this->remote_bda_);
|
||||
// A stack on its way down frees its own cache.
|
||||
if (esp32_ble::global_ble->is_active()) {
|
||||
esp_ble_gattc_cache_clean(this->remote_bda_);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
|
||||
void connect() override;
|
||||
esp_err_t pair();
|
||||
void disconnect() override;
|
||||
void ble_before_disabled_event_handler() override;
|
||||
void unconditional_disconnect();
|
||||
void release_services();
|
||||
|
||||
@@ -114,7 +115,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
|
||||
#endif
|
||||
|
||||
// Group 3: 4-byte types
|
||||
int gattc_if_;
|
||||
int gattc_if_{ESP_GATT_IF_NONE};
|
||||
esp_gatt_status_t status_{ESP_GATT_OK};
|
||||
|
||||
// Group 4: Arrays
|
||||
@@ -139,7 +140,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
|
||||
uint8_t pending_notify_regs_{0};
|
||||
bool auto_connect_{false};
|
||||
bool paired_{false};
|
||||
// Set only when release_services() cleans the stack's GATT cache, which no API may then walk
|
||||
// Set by release_services() on RAM-cache builds; the stack's GATT database must not be walked after it
|
||||
bool services_released_{false};
|
||||
// 8 bytes used, no padding
|
||||
|
||||
@@ -155,10 +156,11 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
|
||||
void log_connection_params_(const char *param_type);
|
||||
void handle_connection_result_(esp_err_t ret);
|
||||
/// Hook called once a connection has been fully torn down (after release_services() and
|
||||
/// set_idle_()), from both the CLOSE_EVT handler and the DISCONNECTING safety timeout.
|
||||
/// set_idle_()): CLOSE_EVT, the DISCONNECTING safety timeout, or the BLE stack going down.
|
||||
/// Subclasses with extra per-connection accounting (e.g. bluetooth_proxy slot state)
|
||||
/// override this to release that state. `reason` is the controller reason code, or
|
||||
/// ESP_GATT_CONN_TIMEOUT for the safety-timeout path.
|
||||
/// override this to release that state. `reason` is the controller reason code,
|
||||
/// ESP_GATT_CONN_TIMEOUT for the safety timeout, or ESP_GATT_CONN_TERMINATE_LOCAL_HOST
|
||||
/// for the stack going down.
|
||||
virtual void on_disconnect_complete(esp_err_t reason) {}
|
||||
/// Transition to IDLE and reset conn_id — call when the connection is fully dead.
|
||||
void set_idle_() {
|
||||
|
||||
@@ -62,6 +62,11 @@ void ESP32BLETracker::on_ota_global_state(ota::OTAState state, float progress, u
|
||||
for (auto *client : this->clients_) {
|
||||
client->disconnect();
|
||||
}
|
||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
|
||||
// The OTA transfer blocks the main loop, so the revert in loop() cannot run. No
|
||||
// active-connection gate here: every client was just told to disconnect.
|
||||
this->update_coex_preference_(false);
|
||||
#endif
|
||||
#endif
|
||||
} else if ((state == ota::OTA_ERROR || state == ota::OTA_ABORT) && this->scan_continuous_before_ota_) {
|
||||
this->scan_continuous_before_ota_ = false;
|
||||
@@ -74,11 +79,11 @@ void ESP32BLETracker::on_ota_global_state(ota::OTAState state, float progress, u
|
||||
|
||||
void ESP32BLETracker::loop() {
|
||||
if (!this->parent_->is_active()) {
|
||||
this->ble_was_disabled_ = true;
|
||||
return;
|
||||
} else if (this->ble_was_disabled_) {
|
||||
}
|
||||
if (this->ble_was_disabled_) {
|
||||
this->ble_was_disabled_ = false;
|
||||
// If the BLE stack was disabled, we need to start the scan again.
|
||||
// First start after boot or after the stack came back.
|
||||
if (this->scan_continuous_) {
|
||||
this->start_scan();
|
||||
}
|
||||
@@ -218,7 +223,27 @@ void ESP32BLETracker::stop_scan() {
|
||||
this->stop_scan_();
|
||||
}
|
||||
|
||||
void ESP32BLETracker::ble_before_disabled_event_handler() { this->stop_scan_(); }
|
||||
void ESP32BLETracker::ble_before_disabled_event_handler() {
|
||||
// Tell the controller to stop; a scan still starting has nothing to stop yet.
|
||||
if (this->scanner_state_ == ScannerState::RUNNING || this->scanner_state_ == ScannerState::FAILED) {
|
||||
this->stop_scan_();
|
||||
}
|
||||
#ifdef ESPHOME_ESP32_BLE_TRACKER_CLIENT_COUNT
|
||||
for (auto *client : this->clients_) {
|
||||
client->ble_before_disabled_event_handler();
|
||||
}
|
||||
this->skip_next_scan_end_ = false;
|
||||
#endif
|
||||
// The stop above never completes (stack torn down, events dropped); settle
|
||||
// here so start_scan_() sees IDLE once the stack is back.
|
||||
if (this->scanner_state_ != ScannerState::IDLE) {
|
||||
this->cleanup_scan_state_(true);
|
||||
}
|
||||
// A failure latched by the old stack must not be handled against the next.
|
||||
this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS;
|
||||
this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS;
|
||||
this->ble_was_disabled_ = true;
|
||||
}
|
||||
|
||||
bool ESP32BLETracker::stop_scan_() {
|
||||
if (this->scanner_state_ != ScannerState::RUNNING && this->scanner_state_ != ScannerState::FAILED) {
|
||||
|
||||
@@ -113,6 +113,9 @@ class ESPBTClient : public ESPBTDeviceListener {
|
||||
virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0;
|
||||
virtual void connect() = 0;
|
||||
virtual void disconnect() = 0;
|
||||
/// Called right before the BLE stack is dismantled. Nothing in flight will
|
||||
/// complete, and the GATT app must register again once the stack is back.
|
||||
virtual void ble_before_disabled_event_handler() {}
|
||||
bool disconnect_pending() const { return this->want_disconnect_; }
|
||||
void cancel_pending_disconnect() { this->want_disconnect_ = false; }
|
||||
|
||||
|
||||
@@ -842,7 +842,14 @@ bool ESPHomeOTAComponent::handle_auth_send_() {
|
||||
const size_t hex_size = hasher.get_size() * 2;
|
||||
const size_t nonce_len = hasher.get_size() / 4;
|
||||
const size_t auth_buf_size = 1 + 3 * hex_size;
|
||||
this->auth_buf_ = std::make_unique<uint8_t[]>(auth_buf_size);
|
||||
// Internal RAM first: 128 of these bytes go straight into the hardware SHA engine
|
||||
this->auth_buf_ =
|
||||
RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(auth_buf_size);
|
||||
if (!this->auth_buf_) {
|
||||
this->log_auth_warning_(LOG_STR("No memory"));
|
||||
this->send_error_and_cleanup_(ota::OTA_RESPONSE_ERROR_UNKNOWN);
|
||||
return false;
|
||||
}
|
||||
this->auth_buf_pos_ = 0;
|
||||
|
||||
char *buf = reinterpret_cast<char *>(this->auth_buf_.get() + 1);
|
||||
|
||||
@@ -145,13 +145,13 @@ class ESPHomeOTAComponent final : public ota::OTAComponent {
|
||||
|
||||
#ifdef USE_OTA_PASSWORD
|
||||
std::string password_;
|
||||
std::unique_ptr<uint8_t[]> auth_buf_;
|
||||
RAMUniquePtr<uint8_t[]> auth_buf_;
|
||||
#endif // USE_OTA_PASSWORD
|
||||
#ifdef USE_OTA_ENCRYPTION
|
||||
#ifndef USE_OTA_ENCRYPTION_FROM_API
|
||||
noise::NoiseContext noise_ctx_;
|
||||
#endif
|
||||
std::unique_ptr<NoiseSession> noise_;
|
||||
RAMUniquePtr<NoiseSession> noise_;
|
||||
#endif // USE_OTA_ENCRYPTION
|
||||
|
||||
socket::ListenSocket *server_{nullptr};
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include <pgmspace.h>
|
||||
@@ -43,9 +42,8 @@ ESPHomeOTAComponent::NoiseSession::~NoiseSession() {
|
||||
bool ESPHomeOTAComponent::noise_start_session_(uint8_t server_feature_flags) {
|
||||
// A provisioned key cleared between the offer and here is not guarded: the
|
||||
// session runs on the zero key load_psk fills in and fails the client's MAC.
|
||||
// Default-init: the frame buffer is written before it is read
|
||||
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
this->noise_ = std::unique_ptr<NoiseSession>(new (std::nothrow) NoiseSession);
|
||||
// Default placement, PSRAM first where present: the session only lives for one upload
|
||||
this->noise_ = RAMAllocator<NoiseSession>().make_unique();
|
||||
static constexpr size_t PROLOGUE_ACK_LEN = 2; // OTA_RESPONSE_OK + version
|
||||
static constexpr size_t PROLOGUE_CLIENT_FEATURES_LEN = 1;
|
||||
static constexpr size_t PROLOGUE_FEATURE_ACK_LEN = 2; // OTA_RESPONSE_FEATURE_FLAGS + server flags
|
||||
|
||||
@@ -6,17 +6,21 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
|
||||
namespace esphome::ethernet {
|
||||
|
||||
namespace {
|
||||
|
||||
// Per-device context returned by init() and handed back to read/write/deinit.
|
||||
// Context returned by init() and handed back to read/write/deinit. There is one W5500 per device, so a
|
||||
// single static instance replaces a heap allocation that could fail. It is always clear when init() runs:
|
||||
// esp_eth_mac_new_w5500() calls deinit() on every failure after init() succeeded, and nothing else
|
||||
// uninstalls the driver
|
||||
struct W5500CustomSpiContext {
|
||||
spi_device_handle_t handle;
|
||||
SemaphoreHandle_t lock;
|
||||
};
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) - intentional mutable state
|
||||
W5500CustomSpiContext w5500_context{};
|
||||
|
||||
// Transfers up to the ESP32 SPI hardware FIFO size (64 bytes) stay on the polling path; larger
|
||||
// transfers (the frame payloads) use the blocking, DMA-backed transmit.
|
||||
@@ -25,23 +29,20 @@ constexpr uint32_t W5500_SPI_LOCK_TIMEOUT_MS = 50;
|
||||
|
||||
void *w5500_custom_spi_init(const void *spi_config) {
|
||||
const auto *config = static_cast<const eth_w5500_config_t *>(spi_config);
|
||||
auto *ctx = new (std::nothrow) W5500CustomSpiContext{};
|
||||
if (ctx == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
auto *ctx = &w5500_context;
|
||||
// The W5500 SPI frame carries the 16-bit address in the command phase and the 8-bit control
|
||||
// byte in the address phase; mirror what the stock driver configures.
|
||||
spi_device_interface_config_t devcfg = *config->spi_devcfg;
|
||||
devcfg.command_bits = 16;
|
||||
devcfg.address_bits = 8;
|
||||
if (spi_bus_add_device(config->spi_host_id, &devcfg, &ctx->handle) != ESP_OK) {
|
||||
delete ctx;
|
||||
ctx->handle = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
ctx->lock = xSemaphoreCreateMutex();
|
||||
if (ctx->lock == nullptr) {
|
||||
spi_bus_remove_device(ctx->handle);
|
||||
delete ctx;
|
||||
ctx->handle = nullptr;
|
||||
return nullptr;
|
||||
}
|
||||
return ctx;
|
||||
@@ -51,7 +52,7 @@ esp_err_t w5500_custom_spi_deinit(void *spi_ctx) {
|
||||
auto *ctx = static_cast<W5500CustomSpiContext *>(spi_ctx);
|
||||
spi_bus_remove_device(ctx->handle);
|
||||
vSemaphoreDelete(ctx->lock);
|
||||
delete ctx;
|
||||
*ctx = {};
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,6 +53,13 @@ void I2SAudioSpeakerBase::dump_config() {
|
||||
void I2SAudioSpeakerBase::loop() {
|
||||
uint32_t event_group_bits = xEventGroupGetBits(this->event_group_);
|
||||
|
||||
// A stop that arrives while stopped cancels any start that has not been processed yet
|
||||
constexpr uint32_t stop_bits = SpeakerEventGroupBits::COMMAND_STOP | SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY;
|
||||
if ((event_group_bits & stop_bits) && (this->state_ == speaker::STATE_STOPPED)) {
|
||||
xEventGroupClearBits(this->event_group_, stop_bits | SpeakerEventGroupBits::COMMAND_START);
|
||||
event_group_bits &= ~(stop_bits | SpeakerEventGroupBits::COMMAND_START);
|
||||
}
|
||||
|
||||
if ((event_group_bits & SpeakerEventGroupBits::COMMAND_START) && (this->state_ == speaker::STATE_STOPPED)) {
|
||||
this->state_ = speaker::STATE_STARTING;
|
||||
xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::COMMAND_START);
|
||||
@@ -239,8 +246,6 @@ void I2SAudioSpeakerBase::start() {
|
||||
if ((this->state_ == speaker::STATE_STARTING) || (this->state_ == speaker::STATE_RUNNING))
|
||||
return;
|
||||
|
||||
// Mark STARTING immediately to avoid transient STOPPED observations before loop() processes COMMAND_START.
|
||||
this->state_ = speaker::STATE_STARTING;
|
||||
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_START);
|
||||
}
|
||||
|
||||
@@ -249,11 +254,10 @@ void I2SAudioSpeakerBase::stop() { this->stop_(false); }
|
||||
void I2SAudioSpeakerBase::finish() { this->stop_(true); }
|
||||
|
||||
void I2SAudioSpeakerBase::stop_(bool wait_on_empty) {
|
||||
if (this->is_failed())
|
||||
return;
|
||||
if (this->state_ == speaker::STATE_STOPPED)
|
||||
if (!this->is_ready() || this->is_failed())
|
||||
return;
|
||||
|
||||
// Always set the bit, even when stopped, so loop() can cancel a start that is still pending
|
||||
if (wait_on_empty) {
|
||||
xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY);
|
||||
} else {
|
||||
|
||||
@@ -227,6 +227,7 @@ LIST_ID_SCHEMA = cv.Schema({cv.Required(CONF_ID): cv.use_id(lv_list_t)})
|
||||
)
|
||||
async def list_add_text_to_code(config, action_id, template_arg, args):
|
||||
widgets = await get_widgets(config)
|
||||
await _wait_list_triggers_completed()
|
||||
|
||||
async def do_add_text(w: Widget):
|
||||
text = await lv_text.process(config[CONF_TEXT])
|
||||
@@ -370,6 +371,7 @@ async def list_add_to_code(config, action_id, template_arg, args):
|
||||
_register_lv_uses(w_type_name, w_conf)
|
||||
_register_dynamic_widget_style_uses(w_conf)
|
||||
widgets = await get_widgets(config)
|
||||
await _wait_list_triggers_completed()
|
||||
|
||||
async def do_add(w: Widget):
|
||||
index = None
|
||||
@@ -503,6 +505,7 @@ LIST_REMOVE_SCHEMA = LIST_ID_SCHEMA.extend(
|
||||
)
|
||||
async def list_remove_to_code(config, action_id, template_arg, args):
|
||||
widgets = await get_widgets(config)
|
||||
await _wait_list_triggers_completed()
|
||||
|
||||
async def do_remove(w: Widget):
|
||||
index = await lv_int.process(config[CONF_INDEX])
|
||||
@@ -536,6 +539,7 @@ async def list_remove_to_code(config, action_id, template_arg, args):
|
||||
)
|
||||
async def list_clear_to_code(config, action_id, template_arg, args):
|
||||
widgets = await get_widgets(config)
|
||||
await _wait_list_triggers_completed()
|
||||
|
||||
async def do_clear(w: Widget):
|
||||
await _wait_list_triggers_completed()
|
||||
|
||||
@@ -26,30 +26,34 @@ namespace esphome::network {
|
||||
|
||||
/// Return whether the node is connected to the network (through wifi, eth, ...)
|
||||
ESPHOME_ALWAYS_INLINE inline bool is_connected() {
|
||||
// With a single interface enabled the checks below collapse to `if (x) return true; return false;`, which
|
||||
// clang-tidy wants folded into one return. Keep the per-interface form so every enabled interface is checked.
|
||||
// NOLINTBEGIN(readability-simplify-boolean-expr)
|
||||
#ifdef USE_ETHERNET
|
||||
if (ethernet::global_eth_component != nullptr && ethernet::global_eth_component->is_connected())
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_MODEM
|
||||
if (modem::global_modem_component != nullptr)
|
||||
return modem::global_modem_component->is_connected();
|
||||
if (modem::global_modem_component != nullptr && modem::global_modem_component->is_connected())
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_WIFI
|
||||
if (wifi::global_wifi_component != nullptr)
|
||||
return wifi::global_wifi_component->is_connected();
|
||||
if (wifi::global_wifi_component != nullptr && wifi::global_wifi_component->is_connected())
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_OPENTHREAD
|
||||
if (openthread::global_openthread_component != nullptr)
|
||||
return openthread::global_openthread_component->is_connected();
|
||||
if (openthread::global_openthread_component != nullptr && openthread::global_openthread_component->is_connected())
|
||||
return true;
|
||||
#endif
|
||||
|
||||
#ifdef USE_HOST
|
||||
return true; // Assume it's connected
|
||||
#endif
|
||||
return false;
|
||||
// NOLINTEND(readability-simplify-boolean-expr)
|
||||
}
|
||||
|
||||
/// Return whether the network is disabled: every configured interface with a
|
||||
|
||||
@@ -13,6 +13,11 @@ namespace esphome::nextion {
|
||||
|
||||
static const char *const TAG = "nextion";
|
||||
|
||||
// A user entity may be named sleep_wake too; only the internal NO_RESULT command clears the sleeping flag
|
||||
static bool is_sleep_wake_command(const NextionComponentBase *component) {
|
||||
return component->get_queue_type() == NextionQueueType::NO_RESULT && component->get_variable_name() == "sleep_wake";
|
||||
}
|
||||
|
||||
// Nextion command terminator: three consecutive 0xFF bytes (per Nextion Instruction Set v1.1).
|
||||
static constexpr uint8_t COMMAND_DELIMITER[3] = {0xFF, 0xFF, 0xFF};
|
||||
static constexpr size_t DELIMITER_SIZE = sizeof(COMMAND_DELIMITER);
|
||||
@@ -163,6 +168,17 @@ bool Nextion::check_connect_() {
|
||||
#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
}
|
||||
|
||||
// NO_RESULT components are owned by their entry; every other component is a user entity. Entry and
|
||||
// component storage comes from RAMAllocator, so delete is not valid for either.
|
||||
void Nextion::release_queue_entry_(NextionQueue *nb) {
|
||||
if (nb->component != nullptr && nb->component->get_queue_type() == NextionQueueType::NO_RESULT) {
|
||||
nb->component->~NextionComponentBase();
|
||||
RAMAllocator<NextionComponentBase>().deallocate(nb->component, 1);
|
||||
}
|
||||
nb->~NextionQueue();
|
||||
RAMAllocator<NextionQueue>().deallocate(nb, 1);
|
||||
}
|
||||
|
||||
void Nextion::reset_(bool reset_nextion) {
|
||||
uint8_t d;
|
||||
|
||||
@@ -170,15 +186,12 @@ void Nextion::reset_(bool reset_nextion) {
|
||||
this->read_byte(&d);
|
||||
}
|
||||
for (auto *entry : this->nextion_queue_) {
|
||||
if (entry->component != nullptr && entry->component->get_queue_type() == NextionQueueType::NO_RESULT) {
|
||||
delete entry->component; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
delete entry; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(entry);
|
||||
}
|
||||
this->nextion_queue_.clear();
|
||||
#ifdef USE_NEXTION_WAVEFORM
|
||||
for (auto *entry : this->waveform_queue_) {
|
||||
delete entry; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(entry);
|
||||
}
|
||||
this->waveform_queue_.clear();
|
||||
#endif // USE_NEXTION_WAVEFORM
|
||||
@@ -421,6 +434,9 @@ bool Nextion::remove_from_q_(bool report_empty) {
|
||||
NextionQueue *nb = this->nextion_queue_.front();
|
||||
if (!nb || !nb->component) {
|
||||
ESP_LOGE(TAG, "Invalid queue");
|
||||
if (nb != nullptr) {
|
||||
this->release_queue_entry_(nb);
|
||||
}
|
||||
this->nextion_queue_.pop_front();
|
||||
return false;
|
||||
}
|
||||
@@ -428,13 +444,10 @@ bool Nextion::remove_from_q_(bool report_empty) {
|
||||
|
||||
ESP_LOGN(TAG, "Removed: %s", component->get_variable_name().c_str());
|
||||
|
||||
if (component->get_queue_type() == NextionQueueType::NO_RESULT) {
|
||||
if (component->get_variable_name() == "sleep_wake") {
|
||||
this->is_sleeping_ = false;
|
||||
}
|
||||
delete component; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
if (is_sleep_wake_command(component)) {
|
||||
this->is_sleeping_ = false;
|
||||
}
|
||||
delete nb; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nb);
|
||||
this->nextion_queue_.pop_front();
|
||||
return true;
|
||||
}
|
||||
@@ -544,7 +557,7 @@ void Nextion::process_nextion_commands_() {
|
||||
ESP_LOGW(TAG, "Invalid waveform ID %d/ch %d", component->get_component_id(),
|
||||
component->get_wave_channel_id());
|
||||
ESP_LOGN(TAG, "Remove waveform ID %d/ch %d", component->get_component_id(), component->get_wave_channel_id());
|
||||
delete nb; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nb);
|
||||
this->waveform_queue_.pop();
|
||||
}
|
||||
#else // USE_NEXTION_WAVEFORM
|
||||
@@ -647,6 +660,9 @@ void Nextion::process_nextion_commands_() {
|
||||
NextionQueue *nb = this->nextion_queue_.front();
|
||||
if (!nb || !nb->component) {
|
||||
ESP_LOGE(TAG, "Invalid queue entry");
|
||||
if (nb != nullptr) {
|
||||
this->release_queue_entry_(nb);
|
||||
}
|
||||
this->nextion_queue_.pop_front();
|
||||
return;
|
||||
}
|
||||
@@ -660,7 +676,7 @@ void Nextion::process_nextion_commands_() {
|
||||
component->set_state_from_string(to_process, true, false);
|
||||
}
|
||||
|
||||
delete nb; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nb);
|
||||
this->nextion_queue_.pop_front();
|
||||
|
||||
break;
|
||||
@@ -687,6 +703,9 @@ void Nextion::process_nextion_commands_() {
|
||||
NextionQueue *nb = this->nextion_queue_.front();
|
||||
if (!nb || !nb->component) {
|
||||
ESP_LOGE(TAG, "Invalid queue");
|
||||
if (nb != nullptr) {
|
||||
this->release_queue_entry_(nb);
|
||||
}
|
||||
this->nextion_queue_.pop_front();
|
||||
return;
|
||||
}
|
||||
@@ -703,7 +722,7 @@ void Nextion::process_nextion_commands_() {
|
||||
component->set_state_from_int(value, true, false);
|
||||
}
|
||||
|
||||
delete nb; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nb);
|
||||
this->nextion_queue_.pop_front();
|
||||
|
||||
break;
|
||||
@@ -890,7 +909,7 @@ void Nextion::process_nextion_commands_() {
|
||||
ESP_LOGN(TAG, "Send waveform: component id %d, waveform id %d, size %zu", component->get_component_id(),
|
||||
component->get_wave_channel_id(), buffer_to_send);
|
||||
component->clear_wave_buffer(buffer_to_send);
|
||||
delete nb; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nb);
|
||||
this->waveform_queue_.pop();
|
||||
#else // USE_NEXTION_WAVEFORM
|
||||
ESP_LOGW(TAG, "Waveform transmit ready but waveform not enabled");
|
||||
@@ -920,14 +939,10 @@ void Nextion::purge_stale_queue_entries_() {
|
||||
ESP_LOGV(TAG, "Remove old queue '%s':'%s'", component->get_queue_type_string(),
|
||||
component->get_variable_name().c_str());
|
||||
|
||||
if (component->get_queue_type() == NextionQueueType::NO_RESULT) {
|
||||
if (component->get_variable_name() == "sleep_wake") {
|
||||
this->is_sleeping_ = false;
|
||||
}
|
||||
delete component; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
if (is_sleep_wake_command(component)) {
|
||||
this->is_sleeping_ = false;
|
||||
}
|
||||
|
||||
delete *it; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(*it);
|
||||
it = this->nextion_queue_.erase(it);
|
||||
|
||||
} else {
|
||||
@@ -1079,6 +1094,34 @@ uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool
|
||||
return response.length();
|
||||
}
|
||||
|
||||
// Allocates a queue entry owning a bare NO_RESULT component; nullptr when the queue is full or memory is out
|
||||
NextionQueue *Nextion::make_no_result_entry_(const std::string &variable_name) {
|
||||
#ifdef USE_NEXTION_MAX_QUEUE_SIZE
|
||||
if (this->max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
|
||||
ESP_LOGW(TAG, "Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
#endif
|
||||
|
||||
auto *nextion_queue = RAMAllocator<nextion::NextionQueue>().allocate(1);
|
||||
if (nextion_queue == nullptr) {
|
||||
ESP_LOGW(TAG, "Queue alloc failed");
|
||||
return nullptr;
|
||||
}
|
||||
new (nextion_queue) nextion::NextionQueue;
|
||||
|
||||
nextion_queue->component = RAMAllocator<nextion::NextionComponentBase>().allocate(1);
|
||||
if (nextion_queue->component == nullptr) {
|
||||
ESP_LOGW(TAG, "Component alloc failed");
|
||||
this->release_queue_entry_(nextion_queue);
|
||||
return nullptr;
|
||||
}
|
||||
new (nextion_queue->component) nextion::NextionComponentBase;
|
||||
nextion_queue->component->set_variable_name(variable_name);
|
||||
nextion_queue->queue_time = App.get_loop_component_start_time();
|
||||
return nextion_queue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Add a command to the Nextion queue that expects no response.
|
||||
*
|
||||
@@ -1090,36 +1133,11 @@ uint16_t Nextion::recv_ret_string_(std::string &response, uint32_t timeout, bool
|
||||
* @param variable_name Name of the variable or component associated with the command.
|
||||
*/
|
||||
void Nextion::add_no_result_to_queue_(const std::string &variable_name) {
|
||||
#ifdef USE_NEXTION_MAX_QUEUE_SIZE
|
||||
if (this->max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
|
||||
ESP_LOGW(TAG, "Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
|
||||
auto *nextion_queue = this->make_no_result_entry_(variable_name);
|
||||
if (nextion_queue == nullptr)
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
RAMAllocator<nextion::NextionQueue> allocator;
|
||||
nextion::NextionQueue *nextion_queue = allocator.allocate(1);
|
||||
if (nextion_queue == nullptr) {
|
||||
ESP_LOGW(TAG, "Queue alloc failed");
|
||||
return;
|
||||
}
|
||||
new (nextion_queue) nextion::NextionQueue();
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
|
||||
nextion_queue->component = new (std::nothrow) nextion::NextionComponentBase;
|
||||
if (nextion_queue->component == nullptr) {
|
||||
ESP_LOGW(TAG, "Component alloc failed");
|
||||
nextion_queue->~NextionQueue();
|
||||
allocator.deallocate(nextion_queue, 1);
|
||||
return;
|
||||
}
|
||||
nextion_queue->component->set_variable_name(variable_name);
|
||||
|
||||
nextion_queue->queue_time = App.get_loop_component_start_time();
|
||||
|
||||
this->nextion_queue_.push_back(nextion_queue);
|
||||
|
||||
ESP_LOGN(TAG, "Queue NORESULT: %s", nextion_queue->component->get_variable_name().c_str());
|
||||
ESP_LOGN(TAG, "Queue NORESULT: %s", variable_name.c_str());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1153,32 +1171,10 @@ void Nextion::add_no_result_to_queue_with_command_(const std::string &variable_n
|
||||
#ifdef USE_NEXTION_COMMAND_SPACING
|
||||
void Nextion::add_no_result_to_queue_with_pending_command_(const std::string &variable_name,
|
||||
const std::string &command) {
|
||||
#ifdef USE_NEXTION_MAX_QUEUE_SIZE
|
||||
if (this->max_queue_size_ > 0 && this->nextion_queue_.size() >= this->max_queue_size_) {
|
||||
ESP_LOGW(TAG, "Queue full (%zu), drop: %s", this->nextion_queue_.size(), variable_name.c_str());
|
||||
auto *nextion_queue = this->make_no_result_entry_(variable_name);
|
||||
if (nextion_queue == nullptr)
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
RAMAllocator<nextion::NextionQueue> allocator;
|
||||
nextion::NextionQueue *nextion_queue = allocator.allocate(1);
|
||||
if (nextion_queue == nullptr) {
|
||||
ESP_LOGW(TAG, "Queue alloc failed");
|
||||
return;
|
||||
}
|
||||
new (nextion_queue) nextion::NextionQueue();
|
||||
|
||||
nextion_queue->component = new (std::nothrow) nextion::NextionComponentBase;
|
||||
if (nextion_queue->component == nullptr) {
|
||||
ESP_LOGW(TAG, "Component alloc failed");
|
||||
nextion_queue->~NextionQueue();
|
||||
allocator.deallocate(nextion_queue, 1);
|
||||
return;
|
||||
}
|
||||
nextion_queue->component->set_variable_name(variable_name);
|
||||
nextion_queue->queue_time = App.get_loop_component_start_time();
|
||||
nextion_queue->pending_command = command; // Store command for retry
|
||||
|
||||
this->nextion_queue_.push_back(nextion_queue);
|
||||
ESP_LOGVV(TAG, "Queue with pending command: %s", variable_name.c_str());
|
||||
}
|
||||
@@ -1312,7 +1308,7 @@ void Nextion::add_to_get_queue(NextionComponentBase *component) {
|
||||
ESP_LOGW(TAG, "Queue alloc failed");
|
||||
return;
|
||||
}
|
||||
new (nextion_queue) nextion::NextionQueue();
|
||||
new (nextion_queue) nextion::NextionQueue;
|
||||
|
||||
nextion_queue->component = component;
|
||||
nextion_queue->queue_time = App.get_loop_component_start_time();
|
||||
@@ -1334,7 +1330,7 @@ void Nextion::add_to_get_queue(NextionComponentBase *component) {
|
||||
if (this->send_command_(command)) {
|
||||
this->nextion_queue_.push_back(nextion_queue);
|
||||
} else {
|
||||
delete nextion_queue; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nextion_queue);
|
||||
}
|
||||
#endif // USE_NEXTION_COMMAND_SPACING
|
||||
}
|
||||
@@ -1355,14 +1351,14 @@ void Nextion::add_addt_command_to_queue(NextionComponentBase *component) {
|
||||
ESP_LOGW(TAG, "Queue alloc failed");
|
||||
return;
|
||||
}
|
||||
new (nextion_queue) nextion::NextionQueue();
|
||||
new (nextion_queue) nextion::NextionQueue;
|
||||
|
||||
nextion_queue->component = component;
|
||||
nextion_queue->queue_time = App.get_loop_component_start_time();
|
||||
|
||||
if (!this->waveform_queue_.push(nextion_queue)) {
|
||||
ESP_LOGW(TAG, "Waveform queue full, drop");
|
||||
delete nextion_queue; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
this->release_queue_entry_(nextion_queue);
|
||||
return;
|
||||
}
|
||||
if (this->waveform_queue_.size() == 1)
|
||||
|
||||
@@ -1469,6 +1469,8 @@ class Nextion final : public NextionBase, public PollingComponent, public uart::
|
||||
void all_components_send_state_(bool force_update = false);
|
||||
uint32_t comok_sent_ = 0;
|
||||
bool remove_from_q_(bool report_empty = true);
|
||||
void release_queue_entry_(NextionQueue *nb);
|
||||
NextionQueue *make_no_result_entry_(const std::string &variable_name);
|
||||
|
||||
/**
|
||||
* @brief Status flags for Nextion display state management
|
||||
|
||||
@@ -23,8 +23,7 @@ class NextionComponentBase;
|
||||
|
||||
class NextionQueue {
|
||||
public:
|
||||
virtual ~NextionQueue() = default;
|
||||
NextionComponentBase *component;
|
||||
NextionComponentBase *component{nullptr};
|
||||
uint32_t queue_time = 0;
|
||||
|
||||
// Store command for retry if spacing blocked it
|
||||
@@ -105,6 +104,6 @@ class NextionComponentBase {
|
||||
int wave_max_length_ = 255;
|
||||
#endif // USE_NEXTION_WAVEFORM
|
||||
|
||||
bool needs_to_send_update_;
|
||||
bool needs_to_send_update_{false};
|
||||
};
|
||||
} // namespace esphome::nextion
|
||||
|
||||
@@ -88,12 +88,12 @@ def encryption_schema(config: ConfigType | None) -> ConfigType:
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
cg.add_define("USE_NOISE")
|
||||
cg.add_library("esphome/noise-c", "0.1.26")
|
||||
cg.add_library("esphome/noise-c", "0.1.30")
|
||||
# noise-c depends on libsodium, but declaring it here too lets the
|
||||
# library manager see the full set up front instead of discovering
|
||||
# libsodium only after noise-c has downloaded, so the two can download
|
||||
# in parallel. The version must match noise-c's library.json.
|
||||
cg.add_library("esphome/libsodium", "1.10021.8")
|
||||
cg.add_library("esphome/libsodium", "1.10021.11")
|
||||
# Enable optimized memzero/memcmp in libsodium instead of volatile byte loops
|
||||
cg.add_build_flag("-DHAVE_WEAK_SYMBOLS=1")
|
||||
cg.add_build_flag("-DHAVE_INLINE_ASM=1")
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <esp_image_format.h>
|
||||
#include <esp_partition.h>
|
||||
#include <esp_rom_crc.h>
|
||||
@@ -235,9 +234,11 @@ bool IDFOTABackend::verify_signed_image_(const esp_partition_t *incoming) {
|
||||
// runs mid-OTA on the loop task, on top of the caller's live 1 KB OTA buffer
|
||||
// and mbedtls's own ~1 KB verify scratch, so keeping it off the stack widens
|
||||
// a thin margin. One short-lived allocation right before reboot is not the
|
||||
// fragmentation pattern the project guards against. nothrow so an OOM here
|
||||
// fails closed like every other error path, rather than aborting.
|
||||
std::unique_ptr<uint8_t[]> block(new (std::nothrow) uint8_t[SIG_BLOCK_SIZE]);
|
||||
// fragmentation pattern the project guards against. An OOM returns nullptr
|
||||
// and fails closed like every other error path. Internal RAM first: the
|
||||
// block is an esp_partition_read target.
|
||||
auto block =
|
||||
RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(SIG_BLOCK_SIZE);
|
||||
if (!block) {
|
||||
OTA_IDF_SIG_LOG(ESP_LOGE, "out of memory");
|
||||
return false;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
#include "esp_timer.h"
|
||||
@@ -12,6 +14,9 @@ namespace esphome::router {
|
||||
|
||||
static const char *const TAG = "router.speaker";
|
||||
|
||||
// Maximum time to wait for the active output to report running after start() before giving up
|
||||
static const uint32_t STATE_TRANSITION_TIMEOUT_MS = 5000;
|
||||
|
||||
static inline uint32_t atomic_subtract_clamped(std::atomic<uint32_t> &var, uint32_t amount) {
|
||||
uint32_t current = var.load(std::memory_order_acquire);
|
||||
uint32_t subtracted = 0;
|
||||
@@ -72,6 +77,7 @@ void Router::loop() {
|
||||
|
||||
this->apply_cached_state_to_active_();
|
||||
this->state_ = speaker::STATE_STARTING;
|
||||
this->state_start_ms_ = App.get_loop_component_start_time();
|
||||
active->start();
|
||||
}
|
||||
return;
|
||||
@@ -86,10 +92,17 @@ void Router::loop() {
|
||||
// set_audio_stream_info() and never reaches the output on its own; if the format
|
||||
// changed while stopped, only start()'s apply_cached_state_to_active_() pushes it
|
||||
// down before the output's play()-side auto-start locks in the stale format.
|
||||
if (active->is_stopped()) {
|
||||
// While STARTING, ignore a transient stopped report as speaker running state
|
||||
// is set asynchronously from start(). Timeout if the speaker never transitions.
|
||||
if (this->state_ == speaker::STATE_STARTING) {
|
||||
if (active->is_running()) {
|
||||
this->state_ = speaker::STATE_RUNNING;
|
||||
} else if ((App.get_loop_component_start_time() - this->state_start_ms_) > STATE_TRANSITION_TIMEOUT_MS) {
|
||||
ESP_LOGW(TAG, "Active output did not start; giving up");
|
||||
this->state_ = speaker::STATE_STOPPED;
|
||||
}
|
||||
} else if (active->is_stopped()) {
|
||||
this->state_ = speaker::STATE_STOPPED;
|
||||
} else if (this->state_ == speaker::STATE_STARTING && active->is_running()) {
|
||||
this->state_ = speaker::STATE_RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -133,6 +146,8 @@ void Router::start() {
|
||||
this->frames_in_pipeline_.store(0, std::memory_order_release);
|
||||
this->apply_cached_state_to_active_();
|
||||
this->state_ = speaker::STATE_STARTING;
|
||||
// May run on a producer task, so the cached loop timestamp is not usable here
|
||||
this->state_start_ms_ = millis();
|
||||
this->get_active_output()->start();
|
||||
}
|
||||
|
||||
|
||||
@@ -59,6 +59,9 @@ class Router final : public Component, public speaker::Speaker {
|
||||
// frames_in_pipeline_.
|
||||
std::atomic<uint32_t> frames_in_pipeline_{0};
|
||||
|
||||
// Set when entering STATE_STARTING; used to time out a start the output never acts on
|
||||
uint32_t state_start_ms_{0};
|
||||
|
||||
bool cached_pause_{false};
|
||||
|
||||
void apply_cached_state_to_active_();
|
||||
|
||||
@@ -307,7 +307,7 @@ async def to_code(config: ConfigType) -> None:
|
||||
player_cfg = data.player_config
|
||||
sample_rate = player_cfg[CONF_SAMPLE_RATE]
|
||||
|
||||
codecs = player_cfg[CONF_CODECS]
|
||||
codecs = [CODECS[codec] for codec in player_cfg[CONF_CODECS]]
|
||||
|
||||
def _audio_format(codec: MockObj, channels: int) -> cg.StructInitializer:
|
||||
return cg.StructInitializer(
|
||||
|
||||
@@ -623,6 +623,9 @@ async def to_code(config):
|
||||
networks = config.get(CONF_NETWORKS, [])
|
||||
if networks:
|
||||
cg.add(var.init_sta(len(networks)))
|
||||
if len(networks) > 1:
|
||||
# The ESP32 scan can filter one SSID in the driver; with several the whole list is kept
|
||||
cg.add_define("USE_WIFI_MULTI_SSID")
|
||||
|
||||
def add_sta(ap: cg.MockObj, network: dict) -> None:
|
||||
ip_config = network.get(CONF_MANUAL_IP, config.get(CONF_MANUAL_IP))
|
||||
|
||||
@@ -1498,8 +1498,8 @@ void WiFiComponent::check_scanning_finished() {
|
||||
return;
|
||||
}
|
||||
this->scan_done_ = false;
|
||||
this->has_completed_scan_after_captive_portal_start_ =
|
||||
true; // Track that we've done a scan since captive portal started
|
||||
// A driver filtered scan saw one SSID; a portal that started during it still needs a full scan
|
||||
this->has_completed_scan_after_captive_portal_start_ = !this->is_scan_driver_filtered_();
|
||||
this->retry_hidden_mode_ = RetryHiddenMode::SCAN_BASED;
|
||||
|
||||
if (this->scan_result_.empty()) {
|
||||
@@ -2415,7 +2415,7 @@ void WiFiComponent::handle_driver_roam_(const bssid_t &bssid, uint8_t channel) {
|
||||
void WiFiComponent::release_scan_results_() {
|
||||
if (!this->keep_scan_results_) {
|
||||
ScanResultsLock lock(this);
|
||||
#if defined(USE_RP2) || defined(USE_ESP32)
|
||||
#if defined(USE_RP2)
|
||||
// std::vector - use swap trick since shrink_to_fit is non-binding
|
||||
decltype(this->scan_result_)().swap(this->scan_result_);
|
||||
#else
|
||||
|
||||
@@ -178,12 +178,12 @@ struct EAPAuth {
|
||||
|
||||
using bssid_t = std::array<uint8_t, 6>;
|
||||
|
||||
/// Initial reserve size for filtered scan results (typical: 1-3 matching networks per SSID)
|
||||
static constexpr size_t WIFI_SCAN_RESULT_FILTERED_RESERVE = 8;
|
||||
// ESP32 with one configured network: the driver filters the scan by its SSID and only this many of
|
||||
// its BSSIDs are kept, the strongest ones
|
||||
static constexpr size_t WIFI_SCAN_RESULT_BOUND = 12;
|
||||
|
||||
// Use std::vector for RP2040 (callback-based) and ESP32 (destructive scan API)
|
||||
// Use FixedVector for ESP8266 and LibreTiny where two-pass exact allocation is possible
|
||||
#if defined(USE_RP2) || defined(USE_ESP32)
|
||||
// RP2040's callback delivers results one at a time with no count, so it needs a growable vector
|
||||
#if defined(USE_RP2)
|
||||
template<typename T> using wifi_scan_vector_t = std::vector<T>;
|
||||
#else
|
||||
template<typename T> using wifi_scan_vector_t = FixedVector<T>;
|
||||
@@ -948,6 +948,12 @@ class WiFiComponent final : public Component {
|
||||
uint8_t num_ipv6_addresses_{0};
|
||||
#endif /* USE_NETWORK_IPV6 */
|
||||
bool error_from_callback_{false};
|
||||
#if defined(USE_ESP32) && !defined(USE_WIFI_MULTI_SSID)
|
||||
bool scan_driver_filtered_{false};
|
||||
bool is_scan_driver_filtered_() const { return this->scan_driver_filtered_; }
|
||||
#else
|
||||
constexpr bool is_scan_driver_filtered_() const { return false; }
|
||||
#endif
|
||||
#if defined(USE_ESP8266) || defined(USE_LIBRETINY)
|
||||
// Platform-specific STA state enum, defined in platform cpp file.
|
||||
// On ESP8266, written from SDK system context (wifi_event_callback) —
|
||||
|
||||
@@ -773,7 +773,11 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) {
|
||||
}
|
||||
}
|
||||
|
||||
this->scan_result_.init(count); // Exact allocation
|
||||
if (!this->scan_result_.try_init(count)) {
|
||||
ESP_LOGW(TAG, "No memory for %zu scan results", count);
|
||||
this->scan_done_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// Second pass: store matching networks
|
||||
for (bss_info *it = head; it != nullptr; it = STAILQ_NEXT(it, next)) {
|
||||
|
||||
@@ -909,7 +909,8 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
ESP_LOGV(TAG, "Scan done: status=%" PRIu32 " number=%u scan_id=%u", it.status, it.number, it.scan_id);
|
||||
|
||||
uint16_t number = it.number;
|
||||
bool needs_full = this->needs_full_scan_results_();
|
||||
const bool filtered = this->is_scan_driver_filtered_();
|
||||
const bool needs_full = this->needs_full_scan_results_();
|
||||
{
|
||||
// Mutate in place under the lock; blocking a portal request is fine and
|
||||
// avoids scratch buffers
|
||||
@@ -926,8 +927,14 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Smart reserve: full capacity if needed, small reserve otherwise
|
||||
this->scan_result_.reserve(needs_full ? number : WIFI_SCAN_RESULT_FILTERED_RESERVE);
|
||||
const size_t wanted = filtered ? std::min<size_t>(number, WIFI_SCAN_RESULT_BOUND) : number;
|
||||
// Storage is reused across the scans of one retry cycle and freed on connect; an exhausted
|
||||
// heap drops this scan and the retry logic scans again
|
||||
if (this->scan_result_.capacity() < wanted && !this->scan_result_.try_init(wanted)) {
|
||||
esp_wifi_clear_ap_list();
|
||||
ESP_LOGW(TAG, "No memory for %zu scan results", wanted);
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_ESP32_HOSTED
|
||||
// getting records one at a time fails on P4 with hosted esp32 WiFi coprocessor
|
||||
@@ -955,22 +962,38 @@ void WiFiComponent::wifi_process_event_(IDFWiFiEvent *data) {
|
||||
}
|
||||
#endif // USE_ESP32_HOSTED
|
||||
|
||||
// Check C string first - avoid std::string construction for non-matching networks
|
||||
const char *ssid_cstr = reinterpret_cast<const char *>(record.ssid);
|
||||
|
||||
// Only construct std::string and store if needed
|
||||
if (needs_full || this->matches_configured_network_(ssid_cstr, record.bssid)) {
|
||||
bssid_t bssid;
|
||||
std::copy(record.bssid, record.bssid + 6, bssid.begin());
|
||||
if (!needs_full && !this->matches_configured_network_(ssid_cstr, record.bssid)) {
|
||||
this->log_discarded_scan_result_(ssid_cstr, record.bssid, record.rssi, record.primary);
|
||||
continue;
|
||||
}
|
||||
bssid_t bssid;
|
||||
std::copy(record.bssid, record.bssid + 6, bssid.begin());
|
||||
if (this->scan_result_.size() < wanted) {
|
||||
this->scan_result_.emplace_back(bssid, ssid_cstr, strlen(ssid_cstr), record.primary, record.rssi,
|
||||
record.authmode != WIFI_AUTH_OPEN, ssid_cstr[0] == '\0');
|
||||
} else {
|
||||
this->log_discarded_scan_result_(ssid_cstr, record.bssid, record.rssi, record.primary);
|
||||
continue;
|
||||
}
|
||||
// Records arrive in scan order, not by signal, so a bounded store keeps the strongest by
|
||||
// replacing its weakest entry. Only SSID and signal decide here; a channel or auth constrained
|
||||
// network hidden behind 12 stronger APs of its own SSID is not a real deployment
|
||||
WiFiScanResult *weakest = &this->scan_result_[0];
|
||||
for (auto &res : this->scan_result_) {
|
||||
if (res.get_rssi() < weakest->get_rssi())
|
||||
weakest = &res;
|
||||
}
|
||||
if (record.rssi <= weakest->get_rssi()) {
|
||||
this->log_discarded_scan_result_(ssid_cstr, record.bssid, record.rssi, record.primary);
|
||||
continue;
|
||||
}
|
||||
// Rebuilt in place rather than assigned; assignment pulls in CompactString's operators, 104 B of flash
|
||||
weakest->~WiFiScanResult();
|
||||
new (weakest) WiFiScanResult(bssid, ssid_cstr, strlen(ssid_cstr), record.primary, record.rssi,
|
||||
record.authmode != WIFI_AUTH_OPEN, ssid_cstr[0] == '\0');
|
||||
}
|
||||
}
|
||||
ESP_LOGV(TAG, "Scan complete: %u found, %zu stored%s", number, this->scan_result_.size(),
|
||||
needs_full ? "" : " (filtered)");
|
||||
filtered ? LOG_STR_LITERAL(" (driver filtered)") : LOG_STR_LITERAL(""));
|
||||
#ifdef USE_WIFI_SCAN_RESULTS_LISTENERS
|
||||
this->notify_scan_results_listeners_();
|
||||
#endif
|
||||
@@ -1047,6 +1070,16 @@ bool WiFiComponent::wifi_scan_start_(bool passive) {
|
||||
wifi_scan_config_t config{};
|
||||
config.ssid = nullptr;
|
||||
config.bssid = nullptr;
|
||||
#ifndef USE_WIFI_MULTI_SSID
|
||||
// One configured network with an SSID: let the driver keep only its APs, so the WiFi library
|
||||
// holds fewer records during the scan. Full results (portal, provisioning, listeners) and a
|
||||
// network configured by BSSID alone still scan everything
|
||||
this->scan_driver_filtered_ =
|
||||
!this->needs_full_scan_results_() && this->sta_.size() == 1 && !this->sta_[0].get_ssid().empty();
|
||||
if (this->scan_driver_filtered_) {
|
||||
config.ssid = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(this->sta_[0].get_ssid().c_str()));
|
||||
}
|
||||
#endif
|
||||
config.channel = 0;
|
||||
config.show_hidden = true;
|
||||
config.scan_type = passive ? WIFI_SCAN_TYPE_PASSIVE : WIFI_SCAN_TYPE_ACTIVE;
|
||||
|
||||
@@ -709,7 +709,11 @@ void WiFiComponent::wifi_scan_done_callback_() {
|
||||
}
|
||||
}
|
||||
|
||||
this->scan_result_.init(count); // Exact allocation
|
||||
if (!this->scan_result_.try_init(count)) {
|
||||
ESP_LOGW(TAG, "No memory for %zu scan results", count);
|
||||
WiFi.scanDelete();
|
||||
return;
|
||||
}
|
||||
|
||||
// Second pass: store matching networks
|
||||
for (int i = 0; i < num; i++) {
|
||||
|
||||
@@ -15,6 +15,7 @@ from ipaddress import (
|
||||
ip_network,
|
||||
)
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
from string import ascii_letters, digits
|
||||
@@ -1967,38 +1968,51 @@ def _remap_bundle_path(value: str) -> Path | None:
|
||||
return remap_bundle_path(value)
|
||||
|
||||
|
||||
def directory(value: object) -> Path:
|
||||
value = string(value)
|
||||
path = CORE.relative_config_path(value)
|
||||
def _declaring_document(value: str) -> Path | None:
|
||||
"""Return the on-disk YAML file *value* was loaded from, absolute, or None."""
|
||||
esp_range = getattr(value, "esp_range", None)
|
||||
if esp_range is None:
|
||||
return None
|
||||
document = Path(esp_range.start_mark.document).absolute()
|
||||
return document if document.is_file() else None
|
||||
|
||||
if not path.exists():
|
||||
remapped = _remap_bundle_path(value)
|
||||
if remapped is None:
|
||||
|
||||
def _existing_path(value: str, kind: str, is_kind: Callable[[Path], bool]) -> Path:
|
||||
"""Resolve *value* to a *kind* entry: config dir, then declaring document, then bundle remap."""
|
||||
path = CORE.relative_config_path(value)
|
||||
if is_kind(path):
|
||||
return path
|
||||
candidates = [path]
|
||||
tried_document: Path | None = None
|
||||
if (document := _declaring_document(value)) is not None:
|
||||
beside_document = document.parent / Path(value).expanduser()
|
||||
if os.path.normpath(beside_document) != os.path.normpath(path):
|
||||
candidates.append(beside_document)
|
||||
tried_document = document
|
||||
if (remapped := _remap_bundle_path(value)) is not None:
|
||||
candidates.append(remapped)
|
||||
for candidate in candidates:
|
||||
if is_kind(candidate):
|
||||
return candidate
|
||||
for candidate in candidates:
|
||||
if candidate.exists():
|
||||
raise Invalid(
|
||||
f"Could not find directory '{path}'. Please make sure it exists (full path: {path.resolve()})."
|
||||
f"Path '{candidate}' is not a {kind} (full path: {candidate.resolve()})."
|
||||
)
|
||||
path = remapped
|
||||
if not path.is_dir():
|
||||
raise Invalid(
|
||||
f"Path '{path}' is not a directory (full path: {path.resolve()})."
|
||||
)
|
||||
return path
|
||||
also = (
|
||||
f" Also looked next to {tried_document}." if tried_document is not None else ""
|
||||
)
|
||||
raise Invalid(
|
||||
f"Could not find {kind} '{path}'. Please make sure it exists (full path: {path.resolve()}).{also}"
|
||||
)
|
||||
|
||||
|
||||
def directory(value: object) -> Path:
|
||||
return _existing_path(string(value), "directory", Path.is_dir)
|
||||
|
||||
|
||||
def file_(value: object) -> Path:
|
||||
value = string(value)
|
||||
path = CORE.relative_config_path(value)
|
||||
|
||||
if not path.exists():
|
||||
remapped = _remap_bundle_path(value)
|
||||
if remapped is None:
|
||||
raise Invalid(
|
||||
f"Could not find file '{path}'. Please make sure it exists (full path: {path.resolve()})."
|
||||
)
|
||||
path = remapped
|
||||
if not path.is_file():
|
||||
raise Invalid(f"Path '{path}' is not a file (full path: {path.resolve()}).")
|
||||
return path
|
||||
return _existing_path(string(value), "file", Path.is_file)
|
||||
|
||||
|
||||
ENTITY_ID_CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789_"
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@ from enum import Enum
|
||||
|
||||
from esphome.enum import StrEnum
|
||||
|
||||
__version__ = "2026.9.0b3"
|
||||
__version__ = "2026.9.0b4"
|
||||
|
||||
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
|
||||
VALID_SUBSTITUTIONS_CHARACTERS = (
|
||||
|
||||
@@ -715,6 +715,7 @@ class EsphomeCore:
|
||||
self.defines = set()
|
||||
self.platformio_options = {}
|
||||
self.loaded_integrations = set()
|
||||
self.loaded_platforms = set()
|
||||
self.component_ids = set()
|
||||
self.platform_counts = defaultdict(int)
|
||||
self.unique_ids = {}
|
||||
|
||||
@@ -260,6 +260,8 @@
|
||||
#ifdef USE_ARDUINO
|
||||
#define USE_PROMETHEUS
|
||||
#define USE_WIFI_WPA2_EAP
|
||||
// Kept in the Arduino block so clang-tidy sees both scan storage paths
|
||||
#define USE_WIFI_MULTI_SSID
|
||||
#endif
|
||||
|
||||
// Platforms with native 64-bit time sources (no rollover tracking needed)
|
||||
|
||||
@@ -56,6 +56,15 @@ void EntityBase::configure_entity_(const char *name, uint32_t object_id_hash, ui
|
||||
this->flags_.entity_category = (entity_fields >> ENTITY_FIELD_ENTITY_CATEGORY_SHIFT) & 0x3;
|
||||
}
|
||||
|
||||
void EntityBase::set_internal(bool internal) {
|
||||
// Remove the after-setup path in 2027.3.0 and ignore the call instead.
|
||||
if (App.is_setup_complete()) {
|
||||
ESP_LOGE(TAG, "'%s': set_internal() after setup is undefined behavior, stops working in 2027.3.0",
|
||||
this->get_name().c_str());
|
||||
}
|
||||
this->flags_.internal = internal;
|
||||
}
|
||||
|
||||
// Weak default lookup functions — overridden by generated code in main.cpp
|
||||
__attribute__((weak)) const char *entity_device_class_lookup(uint8_t) { return ""; }
|
||||
__attribute__((weak)) const char *entity_uom_lookup(uint8_t) { return ""; }
|
||||
|
||||
@@ -88,13 +88,26 @@ class EntityBase {
|
||||
// Get whether this Entity should be hidden outside ESPHome
|
||||
bool is_internal() const { return this->flags_.internal; }
|
||||
|
||||
// Deprecated: Calling set_internal() at runtime is undefined behavior. Components and clients
|
||||
// are NOT notified of the change, the flag may have already been read during setup, and there
|
||||
// is NO guarantee any consumer will observe the new value. Use the 'internal:' YAML key instead.
|
||||
ESPDEPRECATED("set_internal() is undefined behavior at runtime — components and Home Assistant are NOT "
|
||||
"notified. Use the 'internal:' YAML key instead. Will be removed in 2027.3.0.",
|
||||
"2026.3.0")
|
||||
void set_internal(bool internal) { this->flags_.internal = internal; }
|
||||
// Set whether this Entity should be hidden outside ESPHome. Prefer the 'internal:' YAML key
|
||||
// whenever possible: it is guaranteed and has none of the limitations below. Use this only when
|
||||
// the decision can only be made at boot. Must be called before MQTT and the API read the flag:
|
||||
// from on_boot at the default priority, or a setup() that runs above setup_priority::AFTER_WIFI.
|
||||
// If the answer comes from a device handshake, hold setup with can_proceed() until it arrives.
|
||||
// Calls after setup finishes are undefined behavior: the flag is still written and an error is
|
||||
// logged, and from 2027.3.0 the call will be ignored.
|
||||
//
|
||||
// Known limitations. Not bugs, so no issue reports please; a PR that removes one with no RAM
|
||||
// or performance cost would be considered.
|
||||
// - No consumer is notified of a change, so the flag can only be decided once per boot.
|
||||
// - The guard is coarse: a call from a priority below AFTER_WIFI (an on_boot with a low priority,
|
||||
// or a setup() at LATE) still passes, but the API camera listener is already registered, MQTT
|
||||
// (AFTER_CONNECTION) has cached the flag, and an API client that connected while setup was
|
||||
// stalled on a slow component has already listed the entities, so they keep the old value.
|
||||
// - Un-hiding an entity declared 'internal: true' in YAML skips the duplicate name check that
|
||||
// codegen runs for exposed entities, so a name collision can surface at runtime. Entities with
|
||||
// only an 'id:' are forced internal and use the id as their name.
|
||||
// - Zigbee codegen skips YAML internal entities entirely, so un-hiding cannot add them to Zigbee.
|
||||
void set_internal(bool internal);
|
||||
|
||||
// Check if this object is declared to be disabled by default.
|
||||
// That means that when the device gets added to Home Assistant (or other clients) it should
|
||||
|
||||
+80
-12
@@ -5,16 +5,20 @@
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <concepts>
|
||||
#include <strings.h>
|
||||
@@ -38,6 +42,7 @@
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32
|
||||
#include <esp_system.h>
|
||||
#include <esp_heap_caps.h>
|
||||
#endif
|
||||
|
||||
@@ -539,7 +544,15 @@ template<typename T, size_t N> inline void init_array_from(std::array<T, N> &des
|
||||
}
|
||||
}
|
||||
|
||||
/// Fixed-capacity vector - allocates once at runtime, never reallocates
|
||||
// Abort with a reason that reaches the panic output on ESP32. Elsewhere the literal is dropped
|
||||
// before it can land in rodata, which is RAM on ESP8266
|
||||
#ifdef USE_ESP32
|
||||
#define ESPHOME_ABORT_WITH_REASON(reason) esp_system_abort(reason)
|
||||
#else
|
||||
#define ESPHOME_ABORT_WITH_REASON(reason) abort()
|
||||
#endif
|
||||
|
||||
/// Fixed-capacity vector - sized once through init() or try_init(); push_back never reallocates
|
||||
/// This avoids std::vector template overhead (_M_realloc_insert, _M_default_append)
|
||||
/// when size is known at initialization but not at compile time
|
||||
template<typename T> class FixedVector {
|
||||
@@ -562,8 +575,7 @@ template<typename T> class FixedVector {
|
||||
void cleanup_() {
|
||||
if (data_ != nullptr) {
|
||||
destroy_elements_();
|
||||
// Free raw memory
|
||||
::operator delete(data_);
|
||||
free(data_); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -632,16 +644,27 @@ template<typename T> class FixedVector {
|
||||
// Allocate capacity - can be called multiple times to reinit
|
||||
// IMPORTANT: After calling init(), you MUST use push_back() to add elements.
|
||||
// Direct assignment via operator[] does NOT update the size counter.
|
||||
// Aborts on exhaustion; use try_init() to handle failure.
|
||||
void init(size_t n) {
|
||||
if (!try_init(n))
|
||||
ESPHOME_ABORT_WITH_REASON("FixedVector: out of memory");
|
||||
}
|
||||
|
||||
// Same as init(), but returns false when memory is exhausted; the previous storage is freed either way
|
||||
bool try_init(size_t n) {
|
||||
cleanup_();
|
||||
reset_();
|
||||
if (n > 0) {
|
||||
// Allocate raw memory without calling constructors
|
||||
// sizeof(T) is correct here for any type T (value types, pointers, etc.)
|
||||
// NOLINTNEXTLINE(bugprone-sizeof-expression)
|
||||
data_ = static_cast<T *>(::operator new(n * sizeof(T)));
|
||||
capacity_ = n;
|
||||
}
|
||||
if (n == 0)
|
||||
return true;
|
||||
if (n > SIZE_MAX / sizeof(T))
|
||||
return false; // the byte count would wrap into a small block
|
||||
// sizeof(T) is correct here for any type T (value types, pointers, etc.)
|
||||
// NOLINTNEXTLINE(bugprone-sizeof-expression,cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
|
||||
data_ = static_cast<T *>(malloc(n * sizeof(T)));
|
||||
if (data_ == nullptr)
|
||||
return false;
|
||||
capacity_ = n;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Clear the vector (destroy all elements, reset size to 0, keep capacity)
|
||||
@@ -738,14 +761,22 @@ template<typename T> class FixedVector {
|
||||
template<size_t STACK_SIZE, typename T = uint8_t> class SmallBufferWithHeapFallback {
|
||||
public:
|
||||
explicit SmallBufferWithHeapFallback(size_t size) {
|
||||
static_assert(std::is_trivially_default_constructible_v<T> && std::is_trivially_destructible_v<T>,
|
||||
"the heap fallback leaves elements unconstructed");
|
||||
if (size <= STACK_SIZE) {
|
||||
this->buffer_ = this->stack_buffer_;
|
||||
} else {
|
||||
this->heap_buffer_ = new T[size];
|
||||
if (size <= SIZE_MAX / sizeof(T)) {
|
||||
// NOLINTNEXTLINE(bugprone-sizeof-expression,cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
|
||||
this->heap_buffer_ = static_cast<T *>(malloc(size * sizeof(T)));
|
||||
}
|
||||
// Callers write through get() unchecked, so exhaustion aborts like the new[] it replaces
|
||||
if (this->heap_buffer_ == nullptr)
|
||||
ESPHOME_ABORT_WITH_REASON("SmallBufferWithHeapFallback: out of memory");
|
||||
this->buffer_ = this->heap_buffer_;
|
||||
}
|
||||
}
|
||||
~SmallBufferWithHeapFallback() { delete[] this->heap_buffer_; }
|
||||
~SmallBufferWithHeapFallback() { free(this->heap_buffer_); } // NOLINT(cppcoreguidelines-no-malloc)
|
||||
|
||||
// Delete copy and move operations to prevent double-delete
|
||||
SmallBufferWithHeapFallback(const SmallBufferWithHeapFallback &) = delete;
|
||||
@@ -2095,6 +2126,10 @@ void delay_microseconds_safe(uint32_t us);
|
||||
/// @name Memory management
|
||||
///@{
|
||||
|
||||
template<typename T> struct RAMDeleter;
|
||||
/// unique_ptr over RAMAllocator storage
|
||||
template<typename T> using RAMUniquePtr = std::unique_ptr<T, RAMDeleter<T>>;
|
||||
|
||||
/** An STL allocator that uses SPI or internal RAM.
|
||||
* Returns `nullptr` in case no memory is available.
|
||||
*
|
||||
@@ -2165,6 +2200,26 @@ template<class T> class RAMAllocator {
|
||||
free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
|
||||
}
|
||||
|
||||
/// Value initialize one T; empty on exhaustion. new (std::nothrow) aborts on ESP-IDF instead.
|
||||
/// Default flags prefer PSRAM; pass PREFER_INTERNAL to keep an object where plain new put it.
|
||||
template<typename... Args> RAMUniquePtr<T> make_unique(Args &&...args) {
|
||||
static_assert(alignof(T) <= alignof(std::max_align_t), "malloc storage cannot hold an over aligned type");
|
||||
T *p = this->allocate(1);
|
||||
if (p == nullptr)
|
||||
return {};
|
||||
// ::new so a class scoped operator new cannot hide the global placement form
|
||||
return RAMUniquePtr<T>(::new (p) T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
/// n elements left uninitialized, as std::make_unique_for_overwrite does; empty on exhaustion, overflow, and n == 0
|
||||
RAMUniquePtr<T[]> make_unique_array_for_overwrite(size_t n) {
|
||||
static_assert(std::is_trivially_default_constructible_v<T>, "elements are left unconstructed");
|
||||
static_assert(alignof(T) <= alignof(std::max_align_t), "malloc storage cannot hold an over aligned type");
|
||||
if (n == 0 || n > SIZE_MAX / sizeof(T))
|
||||
return {};
|
||||
return RAMUniquePtr<T[]>(this->allocate(n));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the total heap space available via this allocator
|
||||
*/
|
||||
@@ -2227,6 +2282,19 @@ template<class T> class RAMAllocator {
|
||||
|
||||
template<class T> using ExternalRAMAllocator = RAMAllocator<T>;
|
||||
|
||||
/// Destroys and frees RAMAllocator storage. Not convertible: free() needs the address malloc returned
|
||||
template<typename T> struct RAMDeleter {
|
||||
void operator()(T *p) const {
|
||||
p->~T();
|
||||
RAMAllocator<T>().deallocate(p, 1);
|
||||
}
|
||||
};
|
||||
/// Array form: elements must be trivial, the count is not stored so only the storage is freed
|
||||
template<typename T> struct RAMDeleter<T[]> {
|
||||
static_assert(std::is_trivially_destructible_v<T>, "RAMUniquePtr<T[]> is for trivially destructible elements");
|
||||
void operator()(T *p) const { RAMAllocator<T>().deallocate(p, 1); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Functions to constrain the range of arithmetic values.
|
||||
*/
|
||||
|
||||
+3
-3
@@ -45,7 +45,7 @@ lib_deps_base =
|
||||
lib_deps =
|
||||
${common.lib_deps_base}
|
||||
https://github.com/dudanov/MideaUART.git#eeea6c3e9b4474f067054592b435be1c4e466815 ; midea
|
||||
esphome/noise-c@0.1.26 ; noise (api, ota)
|
||||
esphome/noise-c@0.1.30 ; noise (api, ota)
|
||||
improv/Improv@1.2.7 ; improv_serial / esp32_improv
|
||||
kikuchan98/pngle@1.1.0 ; online_image
|
||||
; Using the repository directly, otherwise ESP-IDF can't use the library
|
||||
@@ -244,7 +244,7 @@ lib_deps =
|
||||
${common:idf-component-libs.lib_deps}
|
||||
ESP32Async/ESPAsyncWebServer@3.9.6 ; web_server_base
|
||||
droscy/esp_wireguard@0.4.5 ; wireguard
|
||||
esphome/noise-c@0.1.26 ; noise (api, ota)
|
||||
esphome/noise-c@0.1.30 ; noise (api, ota)
|
||||
ESP32Async/AsyncTCP@3.4.5 ; async_tcp
|
||||
DNSServer ; captive_portal
|
||||
heman/AsyncMqttClient-esphome@2.0.0 ; mqtt
|
||||
@@ -641,7 +641,7 @@ build_unflags =
|
||||
extends = common
|
||||
platform = platformio/native
|
||||
lib_deps =
|
||||
esphome/noise-c@0.1.26 ; used by noise (api, ota)
|
||||
esphome/noise-c@0.1.30 ; used by noise (api, ota)
|
||||
lvgl/lvgl@9.5.0 ; lvgl
|
||||
build_flags =
|
||||
${common.build_flags}
|
||||
|
||||
@@ -36,7 +36,8 @@ PLATFORMIO_OPTIONS = {
|
||||
|
||||
|
||||
def run_tests(selected_components: list[str]) -> int:
|
||||
os.environ["ASAN_OPTIONS"] = "detect_leaks=0"
|
||||
# allocator_may_return_null: an oversized request must come back empty, not abort the run
|
||||
os.environ["ASAN_OPTIONS"] = "detect_leaks=0:allocator_may_return_null=1"
|
||||
return build_and_run(
|
||||
selected_components=selected_components,
|
||||
tests_dir=COMPONENTS_TESTS_DIR,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.core import CORE
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# USE_API compiles every api source, so emit what they need. No socket
|
||||
# override: an __init__.py there makes pytest import its conftest as socket.conftest.
|
||||
async def to_code_testing(config):
|
||||
cg.add_define("USE_API")
|
||||
cg.add_define("USE_API_PLAINTEXT")
|
||||
cg.add_define("API_MAX_SEND_QUEUE", 8)
|
||||
cg.add_define("MAX_API_CONNECTIONS", 1)
|
||||
cg.add_define("USE_SOCKET_IMPL_BSD_SOCKETS")
|
||||
CORE.register_controller() # api_server registers with the controller registry
|
||||
|
||||
manifest.to_code = to_code_testing
|
||||
@@ -0,0 +1,65 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#include "esphome/components/api/api_buffer.h"
|
||||
|
||||
namespace esphome::api::testing {
|
||||
|
||||
// Pointer plus two 16 bit sizes
|
||||
static_assert(sizeof(APIBuffer) <= 2 * sizeof(void *));
|
||||
|
||||
TEST(APIBuffer, RefusesSizesAbove16Bits) {
|
||||
APIBuffer buf;
|
||||
ASSERT_TRUE(buf.resize(16));
|
||||
EXPECT_FALSE(buf.reserve(UINT16_MAX + 1));
|
||||
EXPECT_EQ(buf.size(), 16u);
|
||||
EXPECT_EQ(buf.capacity(), 16u);
|
||||
EXPECT_TRUE(buf.reserve(UINT16_MAX));
|
||||
EXPECT_EQ(buf.capacity(), UINT16_MAX);
|
||||
}
|
||||
|
||||
static const uint8_t BYTES[] = {1, 2, 3, 4, 5, 6};
|
||||
|
||||
TEST(APIBuffer, AppendReturnsTheNewBytes) {
|
||||
APIBuffer buf;
|
||||
ASSERT_TRUE(buf.reserve(8));
|
||||
uint8_t *first = buf.append(3);
|
||||
ASSERT_NE(first, nullptr);
|
||||
std::memcpy(first, BYTES, 3);
|
||||
EXPECT_EQ(buf.size(), 3u);
|
||||
EXPECT_EQ(buf.capacity(), 8u);
|
||||
|
||||
// Grows through realloc and keeps what was there
|
||||
uint8_t *second = buf.append(6);
|
||||
ASSERT_EQ(second, buf.data() + 3);
|
||||
std::memcpy(second, BYTES + 3, 3);
|
||||
EXPECT_EQ(buf.size(), 9u);
|
||||
EXPECT_EQ(buf.capacity(), 9u);
|
||||
EXPECT_EQ(std::memcmp(buf.data(), BYTES, 6), 0);
|
||||
}
|
||||
|
||||
TEST(APIBuffer, DropFrontSlidesTheRestDown) {
|
||||
APIBuffer buf;
|
||||
uint8_t *bytes = buf.append(6);
|
||||
ASSERT_NE(bytes, nullptr);
|
||||
std::memcpy(bytes, BYTES, 6);
|
||||
|
||||
buf.drop_front(2);
|
||||
EXPECT_EQ(buf.size(), 4u);
|
||||
EXPECT_EQ(buf.capacity(), 6u);
|
||||
EXPECT_EQ(std::memcmp(buf.data(), BYTES + 2, 4), 0);
|
||||
|
||||
// Growing afterwards keeps the slid bytes
|
||||
ASSERT_TRUE(buf.reserve(64));
|
||||
EXPECT_EQ(buf.size(), 4u);
|
||||
EXPECT_EQ(std::memcmp(buf.data(), BYTES + 2, 4), 0);
|
||||
|
||||
// Dropping everything leaves an empty buffer with its capacity
|
||||
buf.drop_front(4);
|
||||
EXPECT_EQ(buf.size(), 0u);
|
||||
EXPECT_EQ(buf.capacity(), 64u);
|
||||
}
|
||||
|
||||
} // namespace esphome::api::testing
|
||||
@@ -0,0 +1,510 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/components/api/api_overflow_buffer.h"
|
||||
|
||||
#ifdef USE_HOST
|
||||
namespace esphome::api::testing {
|
||||
|
||||
// Idle cost is the buffer plus one word of bookkeeping
|
||||
static_assert(sizeof(APIOverflowBuffer) <= sizeof(APIBuffer) + sizeof(void *));
|
||||
|
||||
// Exposes storage so tests can check it is reused, not reallocated
|
||||
class TestOverflowBuffer : public APIOverflowBuffer {
|
||||
public:
|
||||
using APIOverflowBuffer::LEN_PREFIX;
|
||||
using APIOverflowBuffer::MAX_BYTES;
|
||||
using APIOverflowBuffer::MAX_LONE_BYTES;
|
||||
struct Storage {
|
||||
size_t capacity;
|
||||
const uint8_t *data;
|
||||
bool operator==(const Storage &) const = default;
|
||||
};
|
||||
size_t capacity() const { return this->buf_.capacity(); }
|
||||
Storage storage() const { return {this->buf_.capacity(), this->buf_.data()}; }
|
||||
uint8_t count() const { return this->count_; }
|
||||
size_t live() const { return this->buf_.size() - this->head_; }
|
||||
/// Simulates a socket write inside try_drain() re-entering the send path
|
||||
void set_draining(bool draining) { this->draining_ = draining; }
|
||||
};
|
||||
|
||||
static std::vector<uint8_t> make_message(size_t len, uint8_t seed) {
|
||||
std::vector<uint8_t> msg(len);
|
||||
for (size_t i = 0; i < len; i++)
|
||||
msg[i] = static_cast<uint8_t>(seed + i);
|
||||
return msg;
|
||||
}
|
||||
|
||||
static bool enqueue(TestOverflowBuffer &buf, const std::vector<uint8_t> &msg, uint16_t skip = 0) {
|
||||
struct iovec iov = {const_cast<uint8_t *>(msg.data()), msg.size()};
|
||||
return buf.enqueue_iov(&iov, 1, static_cast<uint16_t>(msg.size()), skip);
|
||||
}
|
||||
|
||||
static void append(std::vector<uint8_t> &dst, const std::vector<uint8_t> &src, size_t skip = 0) {
|
||||
dst.insert(dst.end(), src.begin() + skip, src.end());
|
||||
}
|
||||
|
||||
static std::vector<uint8_t> concat(std::initializer_list<std::vector<uint8_t>> parts) {
|
||||
std::vector<uint8_t> out;
|
||||
for (const auto &part : parts)
|
||||
append(out, part);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// The pipe delivers the filler first, then the drained messages.
|
||||
static void expect_after_filler(const std::vector<uint8_t> &received, size_t filler,
|
||||
const std::vector<uint8_t> &expected) {
|
||||
ASSERT_EQ(received.size(), filler + expected.size());
|
||||
EXPECT_TRUE(std::equal(expected.begin(), expected.end(), received.begin() + filler));
|
||||
}
|
||||
|
||||
// Non-blocking socket pair with small buffers, so the writer fills like a stalled TCP connection
|
||||
class OverflowBufferTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
int fds[2];
|
||||
ASSERT_EQ(::socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
|
||||
int size = 4096;
|
||||
ASSERT_EQ(::setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)), 0);
|
||||
ASSERT_EQ(::setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)), 0);
|
||||
ASSERT_EQ(::fcntl(fds[1], F_SETFL, O_NONBLOCK), 0);
|
||||
this->reader_ = fds[1];
|
||||
this->sock_ = std::make_unique<socket::Socket>(fds[0]);
|
||||
ASSERT_EQ(this->sock_->setblocking(false), 0);
|
||||
}
|
||||
void TearDown() override { ::close(this->reader_); }
|
||||
|
||||
/// Write filler until the socket refuses; returns the bytes accepted
|
||||
size_t fill_pipe_() {
|
||||
uint8_t junk[512];
|
||||
std::memset(junk, 0xEE, sizeof(junk));
|
||||
size_t total = 0;
|
||||
for (;;) {
|
||||
ssize_t written = this->sock_->write(junk, sizeof(junk));
|
||||
if (written <= 0)
|
||||
break;
|
||||
total += static_cast<size_t>(written);
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
/// Append whatever the pipe currently holds.
|
||||
void read_into_(std::vector<uint8_t> &out) {
|
||||
uint8_t tmp[1024];
|
||||
for (;;) {
|
||||
ssize_t n = ::read(this->reader_, tmp, sizeof(tmp));
|
||||
if (n <= 0)
|
||||
break;
|
||||
out.insert(out.end(), tmp, tmp + n);
|
||||
}
|
||||
}
|
||||
|
||||
/// Drain once; a refusal must be a would-block, never a hard error.
|
||||
ssize_t drain_(TestOverflowBuffer &buf) {
|
||||
ssize_t sent = buf.try_drain(this->sock_.get());
|
||||
if (sent == -1) {
|
||||
EXPECT_TRUE(errno == EWOULDBLOCK || errno == EAGAIN);
|
||||
}
|
||||
return sent;
|
||||
}
|
||||
|
||||
/// Read and drain until the backlog is empty; returns all bytes received
|
||||
std::vector<uint8_t> drain_all_(TestOverflowBuffer &buf) {
|
||||
std::vector<uint8_t> received;
|
||||
for (int i = 0; i < 10000 && !buf.empty(); i++) {
|
||||
this->read_into_(received);
|
||||
// A hard socket error would never clear the backlog; stop instead of spinning
|
||||
if (this->drain_(buf) == -1 && errno != EWOULDBLOCK && errno != EAGAIN)
|
||||
break;
|
||||
}
|
||||
EXPECT_TRUE(buf.empty());
|
||||
this->read_into_(received);
|
||||
return received;
|
||||
}
|
||||
|
||||
struct Stall {
|
||||
size_t filler;
|
||||
std::vector<uint8_t> first, second, received;
|
||||
TestOverflowBuffer::Storage before;
|
||||
};
|
||||
/// Park two messages, then drain the first fully and the second part way
|
||||
void stall_mid_message_(TestOverflowBuffer &buf, Stall &s) {
|
||||
s.filler = this->fill_pipe_();
|
||||
s.first = make_message(1500, 20);
|
||||
ASSERT_GT(s.filler, s.first.size()); // the first message must drain in one go
|
||||
// Larger than the whole pipe, so a drain always stops inside it
|
||||
s.second = make_message(std::max<size_t>(s.filler + 1, std::min<size_t>(s.filler * 3, 12000)), 60);
|
||||
ASSERT_GT(s.second.size(), s.filler);
|
||||
ASSERT_TRUE(enqueue(buf, s.first));
|
||||
ASSERT_TRUE(enqueue(buf, s.second));
|
||||
s.before = buf.storage();
|
||||
this->read_into_(s.received);
|
||||
ASSERT_GT(this->drain_(buf), 0);
|
||||
ASSERT_EQ(buf.count(), 1);
|
||||
}
|
||||
|
||||
int reader_{-1};
|
||||
std::unique_ptr<socket::Socket> sock_;
|
||||
};
|
||||
|
||||
TEST_F(OverflowBufferTest, IdleBufferOwnsNoStorage) {
|
||||
TestOverflowBuffer buf;
|
||||
EXPECT_TRUE(buf.empty());
|
||||
EXPECT_EQ(buf.capacity(), 0u);
|
||||
EXPECT_EQ(buf.storage().data, nullptr);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, StorageIsReusedAcrossStalls) {
|
||||
TestOverflowBuffer buf;
|
||||
auto msg = make_message(1000, 1);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
const auto storage = buf.storage();
|
||||
EXPECT_GE(storage.capacity, msg.size() + TestOverflowBuffer::LEN_PREFIX);
|
||||
|
||||
for (int stall = 0; stall < 5; stall++) {
|
||||
expect_after_filler(this->drain_all_(buf), filler, msg);
|
||||
EXPECT_TRUE(buf.empty());
|
||||
// Same allocation every time: no free, no new allocation
|
||||
EXPECT_EQ(buf.storage(), storage);
|
||||
|
||||
filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
EXPECT_EQ(buf.storage(), storage);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, ReleaseWhileQueuedFreesOnceDrained) {
|
||||
TestOverflowBuffer buf;
|
||||
auto msg = make_message(1000, 7);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
const size_t capacity = buf.capacity();
|
||||
|
||||
// Requested while the backlog still holds data: storage must stay until sent
|
||||
buf.release();
|
||||
EXPECT_FALSE(buf.empty());
|
||||
EXPECT_EQ(buf.capacity(), capacity);
|
||||
|
||||
expect_after_filler(this->drain_all_(buf), filler, msg);
|
||||
EXPECT_TRUE(buf.empty());
|
||||
EXPECT_EQ(buf.capacity(), 0u);
|
||||
EXPECT_EQ(buf.storage().data, nullptr);
|
||||
|
||||
// A later stall allocates again and keeps it, since nobody asked for a release
|
||||
filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
EXPECT_GT(buf.capacity(), 0u);
|
||||
this->drain_all_(buf);
|
||||
EXPECT_GT(buf.capacity(), 0u);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, ReleaseWhenEmptyFreesImmediately) {
|
||||
TestOverflowBuffer buf;
|
||||
auto msg = make_message(100, 3);
|
||||
|
||||
this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
this->drain_all_(buf);
|
||||
EXPECT_GT(buf.capacity(), 0u);
|
||||
|
||||
buf.release();
|
||||
EXPECT_EQ(buf.capacity(), 0u);
|
||||
EXPECT_EQ(buf.storage().data, nullptr);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, PreservesOrderAndSkipsSentPrefix) {
|
||||
TestOverflowBuffer buf;
|
||||
auto first = make_message(700, 10);
|
||||
auto second_a = make_message(300, 50);
|
||||
auto second_b = make_message(400, 90);
|
||||
auto third = make_message(200, 130);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
// 100 bytes of the first message were already accepted by the socket
|
||||
ASSERT_TRUE(enqueue(buf, first, 100));
|
||||
// Two iovecs with the skip covering all of the first one plus part of the second
|
||||
struct iovec iov[2] = {{second_a.data(), second_a.size()}, {second_b.data(), second_b.size()}};
|
||||
const uint16_t second_skip = static_cast<uint16_t>(second_a.size() + 5);
|
||||
ASSERT_TRUE(buf.enqueue_iov(iov, 2, static_cast<uint16_t>(second_a.size() + second_b.size()), second_skip));
|
||||
ASSERT_TRUE(enqueue(buf, third));
|
||||
EXPECT_EQ(buf.count(), 3);
|
||||
|
||||
// Nothing can go out while the pipe is full
|
||||
EXPECT_EQ(this->drain_(buf), -1);
|
||||
EXPECT_EQ(buf.count(), 3);
|
||||
|
||||
std::vector<uint8_t> expected;
|
||||
append(expected, first, 100);
|
||||
append(expected, second_b, 5);
|
||||
append(expected, third);
|
||||
expect_after_filler(this->drain_all_(buf), filler, expected);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, RefusesWhenQueueIsFull) {
|
||||
TestOverflowBuffer buf;
|
||||
auto msg = make_message(16, 1);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
for (int i = 0; i < API_MAX_SEND_QUEUE; i++) {
|
||||
ASSERT_TRUE(enqueue(buf, msg)) << "message " << i;
|
||||
}
|
||||
EXPECT_FALSE(enqueue(buf, msg));
|
||||
EXPECT_EQ(buf.count(), API_MAX_SEND_QUEUE);
|
||||
|
||||
// Draining frees the slots again
|
||||
std::vector<uint8_t> expected;
|
||||
for (int i = 0; i < API_MAX_SEND_QUEUE; i++)
|
||||
append(expected, msg);
|
||||
expect_after_filler(this->drain_all_(buf), filler, expected);
|
||||
this->fill_pipe_();
|
||||
EXPECT_TRUE(enqueue(buf, msg));
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, SkipAtIovecBoundary) {
|
||||
TestOverflowBuffer buf;
|
||||
auto sent = make_message(300, 50);
|
||||
auto unsent = make_message(400, 90);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
// The skip covers the first iovec exactly, so only the second is copied
|
||||
struct iovec iov[2] = {{sent.data(), sent.size()}, {unsent.data(), unsent.size()}};
|
||||
ASSERT_TRUE(
|
||||
buf.enqueue_iov(iov, 2, static_cast<uint16_t>(sent.size() + unsent.size()), static_cast<uint16_t>(sent.size())));
|
||||
EXPECT_EQ(buf.live(), unsent.size() + TestOverflowBuffer::LEN_PREFIX);
|
||||
expect_after_filler(this->drain_all_(buf), filler, unsent);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, AppendsBehindSentPrefixWhenItFits) {
|
||||
TestOverflowBuffer buf;
|
||||
size_t filler = this->fill_pipe_();
|
||||
auto first = make_message(200, 20);
|
||||
// Size the second message so the two land half way into a 256 byte step,
|
||||
// leaving exactly 128 bytes of slack whatever the pipe accepted
|
||||
const size_t base = std::max<size_t>(filler + 1, std::min<size_t>(filler * 3, 12000));
|
||||
const size_t second_len = (base / 256 + 1) * 256 + 128 - first.size() - 2 * TestOverflowBuffer::LEN_PREFIX;
|
||||
auto second = make_message(second_len, 60);
|
||||
ASSERT_GT(second.size(), filler);
|
||||
ASSERT_TRUE(enqueue(buf, first));
|
||||
ASSERT_TRUE(enqueue(buf, second));
|
||||
const auto storage = buf.storage();
|
||||
const size_t slack = storage.capacity - first.size() - second.size() - 2 * TestOverflowBuffer::LEN_PREFIX;
|
||||
ASSERT_EQ(slack, 128u);
|
||||
auto third = make_message(slack - TestOverflowBuffer::LEN_PREFIX, 200);
|
||||
|
||||
std::vector<uint8_t> received;
|
||||
this->read_into_(received);
|
||||
ASSERT_GT(this->drain_(buf), 0);
|
||||
ASSERT_EQ(buf.count(), 1);
|
||||
const size_t live = buf.live();
|
||||
|
||||
// Fits in the tail, so the sent prefix is left alone
|
||||
ASSERT_TRUE(enqueue(buf, third));
|
||||
EXPECT_EQ(buf.storage(), storage);
|
||||
EXPECT_EQ(buf.live(), live + third.size() + TestOverflowBuffer::LEN_PREFIX);
|
||||
|
||||
append(received, this->drain_all_(buf));
|
||||
expect_after_filler(received, filler, concat({first, second, third}));
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, ReleaseSurvivesFurtherEnqueues) {
|
||||
TestOverflowBuffer buf;
|
||||
auto first = make_message(300, 7);
|
||||
auto second = make_message(300, 70);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, first));
|
||||
buf.release();
|
||||
ASSERT_TRUE(enqueue(buf, second));
|
||||
EXPECT_GT(buf.capacity(), 0u);
|
||||
|
||||
expect_after_filler(this->drain_all_(buf), filler, concat({first, second}));
|
||||
EXPECT_EQ(buf.capacity(), 0u);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, RefusesWhenByteLimitIsExceeded) {
|
||||
TestOverflowBuffer buf;
|
||||
// Two of these fill the byte budget exactly, well before the slot count is reached
|
||||
static_assert(API_MAX_SEND_QUEUE >= 3);
|
||||
auto msg = make_message(TestOverflowBuffer::MAX_BYTES / 2 - TestOverflowBuffer::LEN_PREFIX, 1);
|
||||
|
||||
this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
EXPECT_FALSE(enqueue(buf, msg));
|
||||
EXPECT_EQ(buf.count(), 2);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, LoneMessageMayExceedByteLimit) {
|
||||
TestOverflowBuffer buf;
|
||||
// The oversized message must still fit under the lone message ceiling
|
||||
static_assert(TestOverflowBuffer::MAX_BYTES + 100 + TestOverflowBuffer::LEN_PREFIX <=
|
||||
TestOverflowBuffer::MAX_LONE_BYTES);
|
||||
auto big = make_message(TestOverflowBuffer::MAX_BYTES + 100, 5);
|
||||
auto small = make_message(16, 9);
|
||||
|
||||
// Refusing the only message would drop the connection for nothing
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, big));
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
// With a backlog present the byte limit applies again
|
||||
EXPECT_FALSE(enqueue(buf, small));
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
|
||||
expect_after_filler(this->drain_all_(buf), filler, big);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, LoneMessageAboveOffsetLimitIsRefused) {
|
||||
TestOverflowBuffer buf;
|
||||
// Payload plus prefix is past the lone message ceiling
|
||||
auto msg = make_message(TestOverflowBuffer::MAX_LONE_BYTES, 3);
|
||||
|
||||
this->fill_pipe_();
|
||||
EXPECT_FALSE(enqueue(buf, msg));
|
||||
EXPECT_TRUE(buf.empty());
|
||||
EXPECT_EQ(buf.capacity(), 0u);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, HardSocketErrorLeavesBacklogIntact) {
|
||||
TestOverflowBuffer buf;
|
||||
auto msg = make_message(300, 40);
|
||||
|
||||
this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
// A closed socket fails every write outright, unlike a full one
|
||||
ASSERT_EQ(this->sock_->close(), 0);
|
||||
|
||||
errno = 0;
|
||||
EXPECT_EQ(buf.try_drain(this->sock_.get()), -1);
|
||||
EXPECT_NE(errno, EWOULDBLOCK);
|
||||
EXPECT_NE(errno, EAGAIN);
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
EXPECT_EQ(buf.live(), msg.size() + TestOverflowBuffer::LEN_PREFIX);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, GrowsWhileReclaimingSentPrefix) {
|
||||
TestOverflowBuffer buf;
|
||||
Stall s;
|
||||
ASSERT_NO_FATAL_FAILURE(this->stall_mid_message_(buf, s));
|
||||
|
||||
// One byte too many to fit even after the sent prefix is reclaimed: grows in one copy
|
||||
auto third = make_message(s.before.capacity - buf.live() + 1, 200);
|
||||
ASSERT_TRUE(enqueue(buf, third));
|
||||
EXPECT_GT(buf.capacity(), s.before.capacity);
|
||||
EXPECT_EQ(buf.count(), 2);
|
||||
|
||||
append(s.received, this->drain_all_(buf));
|
||||
expect_after_filler(s.received, s.filler, concat({s.first, s.second, third}));
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, NestedDrainMakesNoProgress) {
|
||||
TestOverflowBuffer buf;
|
||||
auto msg = make_message(300, 40);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, msg));
|
||||
std::vector<uint8_t> received;
|
||||
this->read_into_(received);
|
||||
|
||||
// Room is available, but a nested drain must leave the outer one's message alone
|
||||
buf.set_draining(true);
|
||||
EXPECT_EQ(this->drain_(buf), 0);
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
std::vector<uint8_t> nothing;
|
||||
this->read_into_(nothing);
|
||||
EXPECT_TRUE(nothing.empty());
|
||||
|
||||
buf.set_draining(false);
|
||||
append(received, this->drain_all_(buf));
|
||||
expect_after_filler(received, filler, msg);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, NestedEnqueueAppendsWithinCapacity) {
|
||||
TestOverflowBuffer buf;
|
||||
auto first = make_message(500, 10);
|
||||
auto second = make_message(4, 90);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, first));
|
||||
const auto storage = buf.storage();
|
||||
ASSERT_GE(storage.capacity, first.size() + second.size() + 2 * TestOverflowBuffer::LEN_PREFIX);
|
||||
|
||||
buf.set_draining(true);
|
||||
EXPECT_TRUE(enqueue(buf, second));
|
||||
EXPECT_EQ(buf.count(), 2);
|
||||
EXPECT_EQ(buf.storage(), storage);
|
||||
buf.set_draining(false);
|
||||
|
||||
expect_after_filler(this->drain_all_(buf), filler, concat({first, second}));
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, NestedEnqueueRefusesToGrow) {
|
||||
TestOverflowBuffer buf;
|
||||
auto first = make_message(500, 10);
|
||||
auto second = make_message(100, 90);
|
||||
|
||||
size_t filler = this->fill_pipe_();
|
||||
ASSERT_TRUE(enqueue(buf, first));
|
||||
const auto storage = buf.storage();
|
||||
ASSERT_LT(storage.capacity, first.size() + second.size() + 2 * TestOverflowBuffer::LEN_PREFIX);
|
||||
|
||||
// Growing would free the bytes the outer write() is sending from
|
||||
buf.set_draining(true);
|
||||
EXPECT_FALSE(enqueue(buf, second));
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
EXPECT_EQ(buf.storage(), storage);
|
||||
buf.set_draining(false);
|
||||
|
||||
expect_after_filler(this->drain_all_(buf), filler, first);
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, NestedEnqueueRefusesToCompact) {
|
||||
TestOverflowBuffer buf;
|
||||
Stall s;
|
||||
ASSERT_NO_FATAL_FAILURE(this->stall_mid_message_(buf, s));
|
||||
auto third = make_message(1000, 200);
|
||||
|
||||
// Sliding the remainder down would move the bytes the outer write() points at
|
||||
buf.set_draining(true);
|
||||
EXPECT_FALSE(enqueue(buf, third));
|
||||
EXPECT_EQ(buf.count(), 1);
|
||||
EXPECT_EQ(buf.storage(), s.before);
|
||||
buf.set_draining(false);
|
||||
|
||||
// Once the drain is over the same enqueue compacts and succeeds
|
||||
ASSERT_TRUE(enqueue(buf, third));
|
||||
EXPECT_EQ(buf.storage(), s.before);
|
||||
append(s.received, this->drain_all_(buf));
|
||||
expect_after_filler(s.received, s.filler, concat({s.first, s.second, third}));
|
||||
}
|
||||
|
||||
TEST_F(OverflowBufferTest, CompactsInsteadOfGrowingAfterPartialDrain) {
|
||||
TestOverflowBuffer buf;
|
||||
Stall s;
|
||||
ASSERT_NO_FATAL_FAILURE(this->stall_mid_message_(buf, s));
|
||||
auto third = make_message(1000, 200);
|
||||
|
||||
// The sent first message is reclaimed by sliding the remainder down, not by reallocating
|
||||
ASSERT_TRUE(enqueue(buf, third));
|
||||
EXPECT_EQ(buf.storage(), s.before);
|
||||
|
||||
append(s.received, this->drain_all_(buf));
|
||||
expect_after_filler(s.received, s.filler, concat({s.first, s.second, third}));
|
||||
}
|
||||
|
||||
} // namespace esphome::api::testing
|
||||
#endif // USE_HOST
|
||||
@@ -6,15 +6,14 @@ def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
# close_service_batch compiles only under USE_BLUETOOTH_PROXY_CONNECTIONS;
|
||||
# emit the backend define so the host build exercises it.
|
||||
async def to_code_testing(config):
|
||||
# These defines are global to the merged host test binary; safe
|
||||
# because no co-compiled test observes them.
|
||||
# These defines are global to the merged host test binary. The api sources are
|
||||
# compiled in it too (the api tests define USE_API), and USE_BLUETOOTH_PROXY would make
|
||||
# them include and call bluetooth_proxy, which has no host build without a BLE hub.
|
||||
cg.add_define("USE_BLE_GATT_CLIENT")
|
||||
cg.add_define("USE_BLE_GATT_CLIENT_STUB_BACKEND")
|
||||
cg.add_define("USE_BLUETOOTH_PROXY")
|
||||
# Gates the connection half of the API surface, which is what
|
||||
# close_service_batch and the GATT response types live behind.
|
||||
cg.add_define("USE_BLUETOOTH_PROXY_CONNECTIONS")
|
||||
cg.add_define("BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE", 16)
|
||||
cg.add_define("BLUETOOTH_PROXY_MAX_CONNECTIONS", 1)
|
||||
|
||||
manifest.to_code = to_code_testing
|
||||
|
||||
@@ -348,4 +348,75 @@ TEST(StepToAccuracyDecimals, NonFiniteAndZero) {
|
||||
EXPECT_EQ(step_to_accuracy_decimals(-INFINITY), 0);
|
||||
}
|
||||
|
||||
// --- FixedVector::try_init() ---
|
||||
|
||||
// Keeps the block observable, else the compiler may drop the malloc and free pair and fold the check
|
||||
static void escape(const void *p) { asm volatile("" : : "g"(p) : "memory"); }
|
||||
|
||||
TEST(FixedVectorTryInit, ReportsExhaustionAndStaysEmpty) {
|
||||
FixedVector<uint32_t> v;
|
||||
const bool ok = v.try_init(SIZE_MAX / sizeof(uint32_t));
|
||||
escape(&v);
|
||||
EXPECT_FALSE(ok);
|
||||
EXPECT_EQ(v.capacity(), 0u);
|
||||
EXPECT_FALSE(v.try_init(SIZE_MAX / sizeof(uint32_t) + 1)); // byte count would wrap
|
||||
EXPECT_EQ(v.capacity(), 0u);
|
||||
EXPECT_TRUE(v.try_init(0));
|
||||
EXPECT_TRUE(v.try_init(4));
|
||||
v.push_back(7);
|
||||
EXPECT_EQ(v.size(), 1u);
|
||||
}
|
||||
|
||||
// --- RAMAllocator::make_unique() ---
|
||||
|
||||
namespace {
|
||||
struct Probe {
|
||||
static inline int live = 0;
|
||||
int a;
|
||||
int b;
|
||||
Probe(int a, int b) : a(a), b(b) { live++; }
|
||||
~Probe() { live--; }
|
||||
};
|
||||
} // namespace
|
||||
|
||||
static_assert(sizeof(RAMUniquePtr<Probe>) == sizeof(Probe *), "the deleter must not add storage");
|
||||
|
||||
TEST(RAMAllocatorMakeUnique, ForwardsArgsAndDestroysOnce) {
|
||||
auto p = RAMAllocator<Probe>().make_unique(3, 4);
|
||||
ASSERT_NE(p, nullptr);
|
||||
EXPECT_EQ(p->a, 3);
|
||||
EXPECT_EQ(p->b, 4);
|
||||
EXPECT_EQ(Probe::live, 1);
|
||||
p.reset();
|
||||
EXPECT_EQ(Probe::live, 0);
|
||||
}
|
||||
|
||||
TEST(RAMAllocatorMakeUnique, ValueInitializesLikeMakeUnique) {
|
||||
struct Plain {
|
||||
uint32_t words[8];
|
||||
};
|
||||
// Dirty a block of the same size first so a recycled allocation is not zero by chance
|
||||
auto dirty = RAMAllocator<uint8_t>().make_unique_array_for_overwrite(sizeof(Plain));
|
||||
std::memset(dirty.get(), 0xFF, sizeof(Plain));
|
||||
dirty.reset();
|
||||
auto p = RAMAllocator<Plain>().make_unique();
|
||||
ASSERT_NE(p, nullptr);
|
||||
// Under ASan fresh blocks are filled with 0xbe, so this holds even when the dirtied block is not reused
|
||||
EXPECT_TRUE(std::all_of(std::begin(p->words), std::end(p->words), [](uint32_t w) { return w == 0; }));
|
||||
}
|
||||
|
||||
TEST(RAMAllocatorMakeUnique, ArrayFormRejectsOverflowAndZero) {
|
||||
EXPECT_EQ(RAMAllocator<uint32_t>().make_unique_array_for_overwrite(SIZE_MAX / sizeof(uint32_t) + 1), nullptr);
|
||||
EXPECT_EQ(RAMAllocator<uint32_t>().make_unique_array_for_overwrite(0), nullptr);
|
||||
EXPECT_NE(RAMAllocator<uint32_t>().make_unique_array_for_overwrite(1), nullptr);
|
||||
}
|
||||
|
||||
TEST(RAMAllocatorMakeUnique, ArrayFormAllocatesElements) {
|
||||
RAMUniquePtr<uint8_t[]> buf = RAMAllocator<uint8_t>().make_unique_array_for_overwrite(256);
|
||||
ASSERT_NE(buf, nullptr);
|
||||
std::memset(buf.get(), 0xA5, 256);
|
||||
EXPECT_EQ(buf[0], 0xA5);
|
||||
EXPECT_EQ(buf[255], 0xA5);
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
|
||||
@@ -30,6 +30,18 @@ binary_sensor:
|
||||
widget: button_button
|
||||
state: pressed
|
||||
|
||||
globals:
|
||||
- id: counter
|
||||
type: int
|
||||
|
||||
script:
|
||||
- id: add_row
|
||||
then:
|
||||
- lvgl.list.add:
|
||||
id: test_list_id
|
||||
label:
|
||||
text: row
|
||||
|
||||
lvgl:
|
||||
id: lvgl_id
|
||||
rotation: 90
|
||||
@@ -1291,7 +1303,7 @@ lvgl:
|
||||
then:
|
||||
- logger.log:
|
||||
format: "table selected row %u col %u"
|
||||
args: [row, column]
|
||||
args: [(unsigned)row, (unsigned)column]
|
||||
on_click:
|
||||
then:
|
||||
- lvgl.table.cell.update:
|
||||
@@ -1347,10 +1359,12 @@ lvgl:
|
||||
- logger.log:
|
||||
format: "list entry added at %d"
|
||||
args: [list_index]
|
||||
- lambda: "id(counter)++;"
|
||||
on_remove:
|
||||
- logger.log:
|
||||
format: "list entry removed at %d"
|
||||
args: [list_index]
|
||||
- lambda: "id(counter)--;"
|
||||
on_click:
|
||||
- lvgl.list.add_text:
|
||||
id: test_list_id
|
||||
|
||||
@@ -9,4 +9,3 @@ media_source:
|
||||
static_delay_adjustable: true
|
||||
fixed_delay: 480us
|
||||
decode_memory: internal
|
||||
codecs: [pcm, opus, flac]
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
esphome:
|
||||
name: set-internal-at-boot
|
||||
on_boot:
|
||||
then:
|
||||
- lambda: |-
|
||||
id(hidden_at_boot).set_internal(true);
|
||||
id(shown_at_boot).set_internal(false);
|
||||
|
||||
host:
|
||||
|
||||
api:
|
||||
actions:
|
||||
- action: set_internal_late
|
||||
then:
|
||||
- lambda: id(untouched).set_internal(true);
|
||||
|
||||
logger:
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
name: "Hidden At Boot"
|
||||
id: hidden_at_boot
|
||||
lambda: return 1.0;
|
||||
|
||||
- platform: template
|
||||
name: "Shown At Boot"
|
||||
id: shown_at_boot
|
||||
internal: true
|
||||
lambda: return 2.0;
|
||||
|
||||
- platform: template
|
||||
name: "Untouched"
|
||||
id: untouched
|
||||
lambda: return 3.0;
|
||||
@@ -0,0 +1,41 @@
|
||||
"""Integration test for set_internal() called during and after setup."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from .log_utils import LineWaiter
|
||||
from .types import APIClientConnectedFactory, RunCompiledFunction
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_set_internal_at_boot(
|
||||
yaml_config: str,
|
||||
run_compiled: RunCompiledFunction,
|
||||
api_client_connected: APIClientConnectedFactory,
|
||||
) -> None:
|
||||
"""set_internal() in on_boot changes API exposure, later calls log an error."""
|
||||
waiter = LineWaiter()
|
||||
|
||||
async with (
|
||||
run_compiled(yaml_config, line_callback=waiter.callback),
|
||||
api_client_connected() as client,
|
||||
):
|
||||
entities, services = await client.list_entities_services()
|
||||
names = {entity.name for entity in entities}
|
||||
|
||||
assert "Hidden At Boot" not in names
|
||||
assert "Shown At Boot" in names
|
||||
assert "Untouched" in names
|
||||
|
||||
late = next(s for s in services if s.name == "set_internal_late")
|
||||
await client.execute_service(late, {})
|
||||
await waiter.wait_for(
|
||||
"'Untouched'",
|
||||
"set_internal() after setup is undefined behavior",
|
||||
timeout=5.0,
|
||||
)
|
||||
|
||||
# Still written during the deprecation window, ignored from 2027.3.0
|
||||
entities, _ = await client.list_entities_services()
|
||||
assert "Untouched" not in {entity.name for entity in entities}
|
||||
@@ -35,8 +35,8 @@ def _load_script():
|
||||
def test_spec_key_collapses_destinations() -> None:
|
||||
"""Two specs delivering one package share a directory and one key."""
|
||||
mod = _load_script()
|
||||
assert mod.spec_key("esphome/noise-c @ 0.1.26") == "noise-c"
|
||||
assert mod.spec_key("esphome/noise-c@0.1.26") == "noise-c"
|
||||
assert mod.spec_key("esphome/noise-c @ 1.0") == "noise-c"
|
||||
assert mod.spec_key("esphome/noise-c@1.0") == "noise-c"
|
||||
assert mod.spec_key("ESP32Async/AsyncTCP @ ^3.4.10") == mod.spec_key(
|
||||
"esp32async/asynctcp @ 3.5.0"
|
||||
)
|
||||
@@ -54,23 +54,23 @@ def test_parse_specs_and_cli_args(tmp_path: Path) -> None:
|
||||
"[env:a]\n"
|
||||
"platform = fake/platform@1\n"
|
||||
"lib_deps =\n"
|
||||
" esphome/noise-c @ 0.1.26\n"
|
||||
" esphome/noise-c @ 1.0\n"
|
||||
" ${common.lib_deps}\n"
|
||||
" internal_lib\n"
|
||||
"[env:b]\n"
|
||||
"lib_deps =\n"
|
||||
" esphome/noise-c @ 0.1.26\n"
|
||||
" esphome/noise-c @ 1.0\n"
|
||||
)
|
||||
mod = _load_script()
|
||||
args = Namespace(libraries=True, platforms=True, tools=False)
|
||||
libs, platforms, tools = mod.parse_specs(str(ini), args)
|
||||
# exact-string duplicates collapse; distinct version pins survive
|
||||
assert libs == ["esphome/noise-c @ 0.1.26"]
|
||||
assert libs == ["esphome/noise-c @ 1.0"]
|
||||
assert platforms == ["fake/platform@1"]
|
||||
assert tools == []
|
||||
assert mod.build_cli_args(libs, platforms, tools) == [
|
||||
"-l",
|
||||
"esphome/noise-c @ 0.1.26",
|
||||
"esphome/noise-c @ 1.0",
|
||||
"-p",
|
||||
"fake/platform@1",
|
||||
]
|
||||
@@ -162,13 +162,13 @@ def test_parallel_install_behavior(tmp_path: Path) -> None:
|
||||
mod.parallel_install(
|
||||
cls,
|
||||
[
|
||||
"esphome/noise-c @ 0.1.26",
|
||||
"esphome/noise-c @ 0.1.26",
|
||||
"esphome/noise-c @ 1.0",
|
||||
"esphome/noise-c @ 1.0",
|
||||
"esphome/already @ 1.0",
|
||||
"https://x/framework.tar.xz",
|
||||
],
|
||||
)
|
||||
assert cls.calls == ["esphome/noise-c @ 0.1.26"]
|
||||
assert cls.calls == ["esphome/noise-c @ 1.0"]
|
||||
assert cls.lock_events == ["lock", "unlock"]
|
||||
|
||||
|
||||
@@ -205,7 +205,7 @@ def test_parallel_install_runs_dependency_waves(tmp_path: Path) -> None:
|
||||
mod = _load_script()
|
||||
cls = _reset_fake(str(tmp_path))
|
||||
cls.deps = {
|
||||
"esphome/noise-c @ 0.1.26": [
|
||||
"esphome/noise-c @ 1.0": [
|
||||
{"owner": "esphome", "name": "libsodium", "version": "^1.0"},
|
||||
{"name": "SPI"},
|
||||
],
|
||||
@@ -213,12 +213,12 @@ def test_parallel_install_runs_dependency_waves(tmp_path: Path) -> None:
|
||||
{"owner": "esphome", "name": "libsodium", "version": "^1.0"},
|
||||
],
|
||||
}
|
||||
mod.parallel_install(cls, ["esphome/noise-c @ 0.1.26", "esphome/wg @ 1.0"])
|
||||
mod.parallel_install(cls, ["esphome/noise-c @ 1.0", "esphome/wg @ 1.0"])
|
||||
assert len(cls.calls) == 3 # the shared dep installs exactly once
|
||||
assert {mod.spec_key(c) for c in cls.calls} == {"noise-c", "wg", "libsodium"}
|
||||
# Wave-1 strings carry no compatibility; the dependency wave does
|
||||
compats = dict(cls.compat_calls)
|
||||
assert compats["esphome/noise-c @ 0.1.26"] is None
|
||||
assert compats["esphome/noise-c @ 1.0"] is None
|
||||
dep_compat = next(v for k, v in cls.compat_calls if "libsodium" in k)
|
||||
assert dep_compat is not None # mirrors pio's install_dependency
|
||||
|
||||
@@ -229,11 +229,11 @@ def test_dependency_wave_excludes_url_specs(tmp_path: Path) -> None:
|
||||
mod = _load_script()
|
||||
cls = _reset_fake(str(tmp_path))
|
||||
cls.deps = {
|
||||
"esphome/noise-c @ 0.1.26": [
|
||||
"esphome/noise-c @ 1.0": [
|
||||
{"name": "vendored", "version": "https://github.com/x/y.git"},
|
||||
],
|
||||
}
|
||||
mod.parallel_install(cls, ["esphome/noise-c @ 0.1.26"])
|
||||
mod.parallel_install(cls, ["esphome/noise-c @ 1.0"])
|
||||
assert {mod.spec_key(c) for c in cls.calls} == {"noise-c"}
|
||||
|
||||
|
||||
@@ -348,13 +348,13 @@ def test_warm_store_still_walks_dependencies(tmp_path: Path) -> None:
|
||||
"""Already-installed top-level packages still feed the dependency
|
||||
wave; a warm store can be missing a transitive dep."""
|
||||
mod = _load_script()
|
||||
cls = _reset_fake(str(tmp_path), installed={"esphome/noise-c @ 0.1.26"})
|
||||
cls = _reset_fake(str(tmp_path), installed={"esphome/noise-c @ 1.0"})
|
||||
cls.deps = {
|
||||
"esphome/noise-c @ 0.1.26": [
|
||||
"esphome/noise-c @ 1.0": [
|
||||
{"owner": "esphome", "name": "libsodium", "version": "^1.0"},
|
||||
],
|
||||
}
|
||||
mod.parallel_install(cls, ["esphome/noise-c @ 0.1.26"])
|
||||
mod.parallel_install(cls, ["esphome/noise-c @ 1.0"])
|
||||
assert [mod.spec_key(c) for c in cls.calls] == ["libsodium"]
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import importlib
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
@@ -20,6 +21,7 @@ from esphome.components.esp32 import (
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
)
|
||||
from esphome.components.substitutions import do_substitution_pass
|
||||
from esphome.config_validation import Invalid
|
||||
from esphome.const import (
|
||||
CONF_DAY,
|
||||
@@ -65,7 +67,13 @@ from esphome.core import (
|
||||
)
|
||||
from esphome.schema_extractors import SCHEMA_EXTRACT
|
||||
from esphome.util import Registry
|
||||
from esphome.yaml_util import ESPHomeDataBase, SensitiveStr, make_data_base
|
||||
from esphome.yaml_util import (
|
||||
ESPHomeDataBase,
|
||||
SensitiveStr,
|
||||
load_yaml,
|
||||
make_data_base,
|
||||
parse_yaml,
|
||||
)
|
||||
|
||||
|
||||
def test_check_not_templatable__invalid():
|
||||
@@ -3145,6 +3153,116 @@ def test_file__existing_relative_path(setup_core: Path) -> None:
|
||||
assert cv.file_("partitions.csv") == setup_core / "partitions.csv"
|
||||
|
||||
|
||||
def _package_value(setup_core: Path, path: str = "assets/ui.js") -> tuple[Path, str]:
|
||||
"""Write a package file next to an ``assets/`` dir; return the dir and its loaded *path* value."""
|
||||
package_dir = setup_core / ".esphome" / "packages" / "abc123" / "vendor"
|
||||
(package_dir / "assets").mkdir(parents=True)
|
||||
(package_dir / "assets" / "ui.js").write_text("js\n")
|
||||
(package_dir / "device.yaml").write_text(f"path: {path}\n")
|
||||
return package_dir, load_yaml(package_dir / "device.yaml")["path"]
|
||||
|
||||
|
||||
def test_file__resolves_relative_to_the_declaring_document(setup_core: Path) -> None:
|
||||
"""A package's own asset path resolves against the package file when the config dir lacks it."""
|
||||
package_dir, value = _package_value(setup_core)
|
||||
|
||||
assert cv.file_(value) == package_dir / "assets" / "ui.js"
|
||||
|
||||
|
||||
def test_file__resolves_a_substituted_path_against_the_use_site(
|
||||
setup_core: Path,
|
||||
) -> None:
|
||||
package_dir, _ = _package_value(setup_core)
|
||||
(package_dir / "device.yaml").write_text(
|
||||
"substitutions:\n ui: assets/ui.js\npath: ${ui}\n"
|
||||
)
|
||||
config = do_substitution_pass(load_yaml(package_dir / "device.yaml"))
|
||||
|
||||
assert cv.file_(config["path"]) == package_dir / "assets" / "ui.js"
|
||||
|
||||
|
||||
def test_file__result_is_absolute_for_a_relative_document(
|
||||
setup_core: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
"""A document loaded by a cwd-relative path still yields an absolute result."""
|
||||
package_dir, _ = _package_value(setup_core)
|
||||
monkeypatch.chdir(setup_core)
|
||||
value = load_yaml(Path(".esphome/packages/abc123/vendor/device.yaml"))["path"]
|
||||
|
||||
result = cv.file_(value)
|
||||
|
||||
assert result.is_absolute()
|
||||
assert result == package_dir / "assets" / "ui.js"
|
||||
|
||||
|
||||
def test_file__config_dir_entry_of_the_wrong_kind_does_not_shadow_the_package(
|
||||
setup_core: Path,
|
||||
) -> None:
|
||||
package_dir, value = _package_value(setup_core)
|
||||
(setup_core / "assets" / "ui.js").mkdir(parents=True)
|
||||
|
||||
assert cv.file_(value) == package_dir / "assets" / "ui.js"
|
||||
|
||||
|
||||
def test_file__miss_names_the_declaring_document(setup_core: Path) -> None:
|
||||
package_dir, value = _package_value(setup_core, "assets/other.js")
|
||||
|
||||
with pytest.raises(Invalid, match="Could not find file") as excinfo:
|
||||
cv.file_(value)
|
||||
|
||||
assert f"Also looked next to {package_dir / 'device.yaml'}" in str(excinfo.value)
|
||||
|
||||
|
||||
def test_file__document_spelled_through_dotdot_in_the_config_dir_adds_no_hint(
|
||||
setup_core: Path,
|
||||
) -> None:
|
||||
(setup_core / "sub").mkdir()
|
||||
(setup_core / "device.yaml").write_text("path: assets/other.js\n")
|
||||
value = load_yaml(setup_core / "sub" / ".." / "device.yaml")["path"]
|
||||
|
||||
with pytest.raises(Invalid) as excinfo:
|
||||
cv.file_(value)
|
||||
|
||||
assert "Also looked" not in str(excinfo.value)
|
||||
|
||||
|
||||
def test_file__wrong_kind_beside_the_document_is_reported(setup_core: Path) -> None:
|
||||
package_dir, value = _package_value(setup_core, "assets")
|
||||
|
||||
with pytest.raises(Invalid, match="is not a file") as excinfo:
|
||||
cv.file_(value)
|
||||
|
||||
assert str(package_dir / "assets") in str(excinfo.value)
|
||||
|
||||
|
||||
def test_file__config_dir_wins_over_the_declaring_document(setup_core: Path) -> None:
|
||||
_, value = _package_value(setup_core)
|
||||
(setup_core / "assets").mkdir()
|
||||
(setup_core / "assets" / "ui.js").write_text("local\n")
|
||||
|
||||
assert cv.file_(value) == setup_core / "assets" / "ui.js"
|
||||
|
||||
|
||||
def test_file__declared_in_an_in_memory_document_is_not_resolved(
|
||||
setup_core: Path,
|
||||
) -> None:
|
||||
"""A value whose source document isn't on disk falls through to the config-dir error."""
|
||||
value = parse_yaml(Path("<unicode string>"), io.StringIO("path: assets/ui.js\n"))[
|
||||
"path"
|
||||
]
|
||||
|
||||
with pytest.raises(Invalid, match="Could not find file"):
|
||||
cv.file_(value)
|
||||
|
||||
|
||||
def test_directory_resolves_relative_to_the_declaring_document(
|
||||
setup_core: Path,
|
||||
) -> None:
|
||||
package_dir, value = _package_value(setup_core, "assets")
|
||||
|
||||
assert cv.directory(value) == package_dir / "assets"
|
||||
|
||||
|
||||
def test_file__missing_raises(setup_core: Path) -> None:
|
||||
with pytest.raises(Invalid, match="Could not find file"):
|
||||
cv.file_("partitions.csv")
|
||||
|
||||
@@ -1663,7 +1663,7 @@ def test_preinstall_runs_dependency_waves(tmp_path: Path) -> None:
|
||||
{"name": "SPI"},
|
||||
]
|
||||
m.dependency_to_spec.side_effect = lambda dep: _FakeSpec(name=dep["name"])
|
||||
pf._preinstall(m, [("noise-c@0.1.26", _FakeSpec(name="noise-c"))])
|
||||
pf._preinstall(m, [("noise-c@1.0", _FakeSpec(name="noise-c"))])
|
||||
assert installed == ["noise-c", "libsodium"] # dep deduped, SPI left out
|
||||
# The dep wave carries its compatibility so _install searches qualified
|
||||
dep_call = m._install.call_args_list[-1]
|
||||
@@ -1683,7 +1683,7 @@ def test_preinstall_dependency_wave_skips_seen_names(tmp_path: Path) -> None:
|
||||
m._install.side_effect = lambda spec, skip_dependencies, compatibility=None: (
|
||||
installed.append(getattr(spec, "name", str(spec)))
|
||||
)
|
||||
pf._preinstall(m, [("noise-c@0.1.26", _FakeSpec(name="noise-c"))])
|
||||
pf._preinstall(m, [("noise-c@1.0", _FakeSpec(name="noise-c"))])
|
||||
assert installed == ["noise-c"]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user