mirror of
https://github.com/esphome/esphome.git
synced 2026-11-16 10:06:00 +00:00
Compare commits
35
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
205318ee1a | ||
|
|
48d8df449a | ||
|
|
383ac1b41a | ||
|
|
0ac2299482 | ||
|
|
b58038af63 | ||
|
|
c741de7a47 | ||
|
|
8a5e07e29f | ||
|
|
f7b52be670 | ||
|
|
d5317669c9 | ||
|
|
5d0bd88bc6 | ||
|
|
b714d8f69b | ||
|
|
ecabdbb24b | ||
|
|
ffbc4148fb | ||
|
|
2169167908 | ||
|
|
900574c03a | ||
|
|
7b1f9c6bf1 | ||
|
|
3587e29725 | ||
|
|
d4244c5388 | ||
|
|
bec53baf1d | ||
|
|
98fd3786dd | ||
|
|
1f531ff022 | ||
|
|
65e7eb6b06 | ||
|
|
7538864e1e | ||
|
|
c0f97475d3 | ||
|
|
df6fcaa6fe | ||
|
|
eb1b793af9 | ||
|
|
b8ce904e0c | ||
|
|
6befa73b00 | ||
|
|
02211e15d8 | ||
|
|
dbbe02f423 | ||
|
|
97a1aa781e | ||
|
|
0b67a754cd | ||
|
|
320b6a8221 | ||
|
|
4e5a5ec32d | ||
|
|
09ce8d7d6e |
No files matched your search
@@ -651,9 +651,7 @@ esphome/components/sun_gtil2/* @Mat931
|
||||
esphome/components/switch/* @esphome/core
|
||||
esphome/components/switch/binary_sensor/* @ssieb
|
||||
esphome/components/sx126x/* @swoboda1337
|
||||
esphome/components/sx126x/packet_interface/* @clydebarrow
|
||||
esphome/components/sx127x/* @swoboda1337
|
||||
esphome/components/sx127x/packet_interface/* @clydebarrow
|
||||
esphome/components/sy6970/* @linkedupbits
|
||||
esphome/components/syslog/* @clydebarrow
|
||||
esphome/components/systa_bus/* @Mat931
|
||||
@@ -665,8 +663,6 @@ esphome/components/tca8418/* @zebble
|
||||
esphome/components/tca9548a/* @andreashergert1984
|
||||
esphome/components/tca9555/* @mobrembski
|
||||
esphome/components/tcl112/* @glmnet
|
||||
esphome/components/tcp/* @clydebarrow
|
||||
esphome/components/tcp/packet_interface/* @clydebarrow
|
||||
esphome/components/tcp_uart/* @Bascht74
|
||||
esphome/components/tee501/* @Stock-M
|
||||
esphome/components/teleinfo/* @0hax @FredM67
|
||||
|
||||
@@ -10,7 +10,7 @@ from esphome.components.const import CONF_PROVIDER, CONF_REMOTE_ID
|
||||
from esphome.components.packet_interface import (
|
||||
CONF_PACKET_INTERFACE,
|
||||
final_validate_interface,
|
||||
interface_has_layer,
|
||||
interface_layers,
|
||||
interface_packet_size,
|
||||
)
|
||||
from esphome.components.packet_interface.layers import CONF_SESSION
|
||||
@@ -103,13 +103,11 @@ CONFIG_SCHEMA = cv.Schema(
|
||||
|
||||
def _final_validate(config: ConfigType) -> ConfigType:
|
||||
"""With a session layer, which clears the states when the peer is lost, nothing would refresh an unchanged state."""
|
||||
if CONF_TIMEOUT in config and interface_has_layer(
|
||||
config[CONF_PACKET_INTERFACE], CONF_SESSION
|
||||
):
|
||||
layers = interface_layers(config[CONF_PACKET_INTERFACE])
|
||||
if CONF_TIMEOUT in config and CONF_SESSION in layers:
|
||||
raise cv.Invalid(
|
||||
f"Not allowed with a '{CONF_SESSION}' layer, or a link that does its job, which already clears the "
|
||||
"states when the other device cannot be reached; unchanged states may never be sent again, so they "
|
||||
"would time out on a working link",
|
||||
f"Not allowed with a '{CONF_SESSION}' layer, which already clears the states when the other device "
|
||||
"cannot be reached; unchanged states may never be sent again, so they would time out on a working link",
|
||||
path=[CONF_TIMEOUT],
|
||||
)
|
||||
return config
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from collections.abc import Callable, Iterator
|
||||
from collections.abc import Callable
|
||||
import importlib
|
||||
import pkgutil
|
||||
from typing import Any
|
||||
@@ -6,7 +6,7 @@ from typing import Any
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import CONF_MAX_PACKET_SIZE
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_TIMEOUT
|
||||
from esphome.const import CONF_ID
|
||||
from esphome.core import CORE, ID
|
||||
from esphome.cpp_generator import MockObj, MockObjClass
|
||||
import esphome.final_validate as fv
|
||||
@@ -19,20 +19,16 @@ from .layers import (
|
||||
CONF_ADDRESSING,
|
||||
CONF_DESTINATION,
|
||||
CONF_PEERS,
|
||||
CONF_RELIABILITY,
|
||||
CONF_SESSION,
|
||||
STACK_ORDER,
|
||||
PacketInterface,
|
||||
build_stack,
|
||||
check_packet_sizes,
|
||||
check_session,
|
||||
check_single_peer,
|
||||
layer_needing_session,
|
||||
packet_interface_ns,
|
||||
stack_auto_load,
|
||||
stack_schema,
|
||||
)
|
||||
from .layers.hdlc_reliability import DEFAULT_TIMEOUT, check_ack_delay
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DOMAIN = "packet_interface"
|
||||
@@ -44,9 +40,6 @@ CONF_PACKET_INTERFACE = "packet_interface"
|
||||
for module_info in pkgutil.iter_modules(layers.__path__):
|
||||
importlib.import_module(f".layers.{module_info.name}", package=__package__)
|
||||
|
||||
# The layer keys that each link class does itself, by class name
|
||||
_PROVIDED_LAYERS: dict[str, tuple[str, ...]] = {}
|
||||
|
||||
LINK_LAYER_SCHEMAS = {
|
||||
cv.Optional(CONF_ADDRESSING): ADDRESSING.schema(),
|
||||
**stack_schema(),
|
||||
@@ -55,7 +48,6 @@ LINK_LAYER_SCHEMAS = {
|
||||
__all__ = [
|
||||
"CONF_MAX_PACKET_SIZE",
|
||||
"CONF_PACKET_INTERFACE",
|
||||
"interface_has_layer",
|
||||
"interface_layers",
|
||||
"interface_packet_size",
|
||||
"PacketInterface",
|
||||
@@ -150,14 +142,6 @@ def interface_packet_size(interface_id: ID) -> int:
|
||||
return check_packet_sizes(layers_config, size)
|
||||
|
||||
|
||||
def interface_has_layer(interface_id: ID, key: str) -> bool:
|
||||
"""During final validation, whether the packet interface with this id has the layer, configured or done by its link."""
|
||||
link, layers_config = _find_interface(interface_id, fv.full_config.get())
|
||||
return key in layers_config or key in _PROVIDED_LAYERS.get(
|
||||
str(link[CONF_ID].type), ()
|
||||
)
|
||||
|
||||
|
||||
def final_validate_interface(key: str) -> Callable[[ConfigType], ConfigType]:
|
||||
"""Final validation for an application: a link with peers must be used through one of the peers."""
|
||||
|
||||
@@ -174,73 +158,25 @@ def final_validate_interface(key: str) -> Callable[[ConfigType], ConfigType]:
|
||||
return validator
|
||||
|
||||
|
||||
def _check_provided_session(config: ConfigType) -> ConfigType:
|
||||
"""A link's own session does not send packets through the layers, so none can need them from the peer."""
|
||||
if key := layer_needing_session(config):
|
||||
raise cv.Invalid(
|
||||
f"This '{key}' needs packets from the peer, which this link does not send through its layers, "
|
||||
"so it cannot be used on this link",
|
||||
path=[key],
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def reliability_layers(config: ConfigType) -> Iterator[tuple[list, ConfigType]]:
|
||||
"""The reliability layers on a link and in its peers, each with its path in the link's configuration."""
|
||||
if (layer := config.get(CONF_RELIABILITY)) is not None:
|
||||
yield [CONF_RELIABILITY], layer
|
||||
for index, peer in enumerate(config.get(CONF_ADDRESSING, {}).get(CONF_PEERS, [])):
|
||||
if (layer := peer.get(CONF_RELIABILITY)) is not None:
|
||||
yield [CONF_ADDRESSING, CONF_PEERS, index, CONF_RELIABILITY], layer
|
||||
|
||||
|
||||
def _check_default_ack_delays(config: ConfigType) -> ConfigType:
|
||||
"""A reliability layer without a timeout uses the default one on this link."""
|
||||
for path, layer in reliability_layers(config):
|
||||
if CONF_TIMEOUT not in layer:
|
||||
try:
|
||||
check_ack_delay(layer, DEFAULT_TIMEOUT)
|
||||
except cv.Invalid as err:
|
||||
err.prepend(path)
|
||||
raise
|
||||
return config
|
||||
|
||||
|
||||
def packet_interface_schema(
|
||||
class_: MockObjClass,
|
||||
default_max_packet_size: int,
|
||||
max_packet_size_limit: int = 4096,
|
||||
provides: tuple[str, ...] = (),
|
||||
chooses_reliability_timeout: bool = False,
|
||||
) -> cv.Schema:
|
||||
"""Schema for a link platform; provides names the layer keys, such as reliability, that the link does itself.
|
||||
|
||||
chooses_reliability_timeout is for a link that works out a reliability timeout of its own, when none is set.
|
||||
"""
|
||||
if provides:
|
||||
_PROVIDED_LAYERS[str(class_)] = provides
|
||||
link_layers = {
|
||||
key: (
|
||||
cv.invalid(f"This link provides '{key}' itself, so it cannot be configured")
|
||||
if key in provides
|
||||
else schema
|
||||
"""Schema for a link platform, with the layers that can be stacked on it."""
|
||||
return (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(class_),
|
||||
cv.Optional(
|
||||
CONF_MAX_PACKET_SIZE, default=default_max_packet_size
|
||||
): cv.int_range(16, max_packet_size_limit),
|
||||
**LINK_LAYER_SCHEMAS,
|
||||
}
|
||||
)
|
||||
for key, schema in LINK_LAYER_SCHEMAS.items()
|
||||
}
|
||||
schema = cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(class_),
|
||||
cv.Optional(
|
||||
CONF_MAX_PACKET_SIZE, default=default_max_packet_size
|
||||
): cv.int_range(16, max_packet_size_limit),
|
||||
**link_layers,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA)
|
||||
if CONF_SESSION in provides:
|
||||
schema = schema.add_extra(_check_provided_session)
|
||||
if not chooses_reliability_timeout:
|
||||
schema = schema.add_extra(_check_default_ack_delays)
|
||||
return schema.add_extra(_validate_layers)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.add_extra(_validate_layers)
|
||||
)
|
||||
|
||||
|
||||
async def new_packet_interface(config: ConfigType, *args: Any) -> MockObj:
|
||||
|
||||
@@ -88,12 +88,8 @@ bool BasicFragmentation::send_(const PacketBuffer &data, const PacketMetaData &m
|
||||
header[1] = index | (index + 1 == fragments ? LAST_FRAGMENT : 0);
|
||||
PacketBuffer body(this->tx_buffer_, length);
|
||||
if (!this->lower_->send_to_interface(PacketBuffer(header, HEADER_SIZE, &body, nullptr, 0), meta_data)) {
|
||||
if (index == 0) {
|
||||
ESP_LOGV(TAG, "%s: Fragment 0 of %zu not accepted", this->name_, fragments);
|
||||
} else {
|
||||
// The peer drops the part already sent when the next packet starts
|
||||
ESP_LOGW(TAG, "%s: Packet cut short, fragment %zu of %zu not accepted", this->name_, index, fragments);
|
||||
}
|
||||
// The peer drops the part already sent when the next packet starts
|
||||
ESP_LOGV(TAG, "%s: Fragment %zu of %zu not accepted", this->name_, index, fragments);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ bool HdlcReliability::send_supervisory_(uint8_t type) {
|
||||
// components run between frames: on a radio, sending blocks, and the peer's frames are received in between. If the
|
||||
// lower layer refuses the frame, it is sent again shortly.
|
||||
bool HdlcReliability::send_pending_() {
|
||||
if (this->vn_ == this->vs_ || (this->resend_held_ && this->vn_ != this->va_))
|
||||
if (this->vn_ == this->vs_)
|
||||
return true;
|
||||
uint8_t control = (this->vn_ << 1) | (this->vr_ << 5);
|
||||
if (!this->send_frame_(control, this->slot_(this->vn_), this->tx_lengths_[this->slot_index_(this->vn_)])) {
|
||||
@@ -178,15 +178,6 @@ bool HdlcReliability::send_pending_() {
|
||||
}
|
||||
|
||||
void HdlcReliability::resend_outstanding_() {
|
||||
this->resend_held_ = false;
|
||||
this->vn_ = this->va_;
|
||||
this->send_pending_();
|
||||
}
|
||||
|
||||
// Resending the whole queue after every timeout lets two peers whose acknowledgements were lost keep sending long
|
||||
// bursts that collide again. The peer answers the oldest frame with its N(R), which also covers frames it already has.
|
||||
void HdlcReliability::resend_oldest_() {
|
||||
this->resend_held_ = true;
|
||||
this->vn_ = this->va_;
|
||||
this->send_pending_();
|
||||
}
|
||||
@@ -222,8 +213,8 @@ void HdlcReliability::on_timeout_() {
|
||||
return;
|
||||
}
|
||||
this->retries_++;
|
||||
ESP_LOGV(TAG, "%s: Timeout, resending frame %u, %u unacknowledged", this->name_, this->va_, this->outstanding_());
|
||||
this->resend_oldest_();
|
||||
ESP_LOGV(TAG, "%s: Timeout, resending %u frames", this->name_, this->outstanding_());
|
||||
this->resend_outstanding_();
|
||||
this->start_timer_();
|
||||
}
|
||||
|
||||
@@ -237,7 +228,6 @@ void HdlcReliability::start_reset_() {
|
||||
this->vr_ = 0;
|
||||
this->retries_ = 0;
|
||||
this->rej_sent_ = false;
|
||||
this->resend_held_ = false;
|
||||
this->ack_sent_();
|
||||
this->cancel_timeout(RETRANSMIT_TIMER_ID);
|
||||
this->request_loop_();
|
||||
@@ -272,11 +262,6 @@ void HdlcReliability::process_ack_(uint8_t nr) {
|
||||
this->head_ = (this->head_ + acked) % this->max_queue_length_;
|
||||
this->va_ = nr;
|
||||
this->retries_ = 0;
|
||||
if (this->resend_held_) {
|
||||
this->resend_held_ = false;
|
||||
if (this->vn_ != this->vs_)
|
||||
this->request_loop_();
|
||||
}
|
||||
if (this->outstanding_() == 0) {
|
||||
this->cancel_timeout(RETRANSMIT_TIMER_ID);
|
||||
} else {
|
||||
|
||||
@@ -17,8 +17,7 @@ namespace esphome::packet_interface {
|
||||
* - SABM resets both peers' sequence numbers, and UA answers it.
|
||||
*
|
||||
* Each frame is the HDLC control byte, the payload, then a CRC-16 of both in little endian order.
|
||||
* After the timeout the oldest unacknowledged frame is resent, and the rest follow once the peer answers it, so
|
||||
* a lost acknowledgement costs one frame of airtime. After max_retries the link is reset.
|
||||
* Unacknowledged frames are resent after the timeout; after max_retries the link is reset.
|
||||
*/
|
||||
class HdlcReliability : public PacketInterface {
|
||||
public:
|
||||
@@ -64,7 +63,6 @@ class HdlcReliability : public PacketInterface {
|
||||
bool send_supervisory_(uint8_t type);
|
||||
bool send_pending_();
|
||||
void resend_outstanding_();
|
||||
void resend_oldest_();
|
||||
void retry_send_();
|
||||
/// An RR is owed: at once, or after the ack delay unless an I-frame carries it first.
|
||||
void owe_ack_(bool now);
|
||||
@@ -98,7 +96,6 @@ class HdlcReliability : public PacketInterface {
|
||||
bool ack_pending_ : 1 {false}; // an RR is owed
|
||||
bool ack_due_ : 1 {false}; // and should be sent now
|
||||
bool rej_sent_ : 1 {false};
|
||||
bool resend_held_ : 1 {false}; // only the oldest frame is resent until the peer acknowledges something
|
||||
bool peer_frame_seen_ : 1 {false}; // a numbered frame has arrived since the link came up
|
||||
bool oversize_warned_ : 1 {false};
|
||||
bool reset_wait_logged_ : 1 {false}; // the queue filled while waiting for this reset to be answered
|
||||
|
||||
@@ -176,24 +176,19 @@ def stack_schema() -> dict:
|
||||
return {cv.Optional(kind.key): kind.schema() for kind in STACK_ORDER}
|
||||
|
||||
|
||||
def layer_needing_session(config: ConfigType) -> str | None:
|
||||
"""The key of the first layer in this stack that needs packets back from the peer, if any."""
|
||||
def check_session(config: ConfigType) -> None:
|
||||
"""Layers that need packets back from the peer need a session layer, whose keep-alives send them."""
|
||||
if CONF_SESSION in config:
|
||||
return
|
||||
for kind in STACK_ORDER:
|
||||
if (layer := config.get(kind.key)) is not None and kind.get_type(
|
||||
layer
|
||||
).needs_session(layer):
|
||||
return kind.key
|
||||
return None
|
||||
|
||||
|
||||
def check_session(config: ConfigType) -> None:
|
||||
"""Layers that need packets back from the peer need a session layer, whose keep-alives send them."""
|
||||
if CONF_SESSION not in config and (key := layer_needing_session(config)):
|
||||
raise cv.Invalid(
|
||||
f"This '{key}' needs packets from the peer, so it needs a '{CONF_SESSION}' layer, whose "
|
||||
"keep-alives make sure they are sent",
|
||||
path=[key],
|
||||
)
|
||||
raise cv.Invalid(
|
||||
f"This '{kind.key}' needs packets from the peer, so it needs a '{CONF_SESSION}' layer, whose "
|
||||
"keep-alives make sure they are sent",
|
||||
path=[kind.key],
|
||||
)
|
||||
|
||||
|
||||
def check_single_peer(config: ConfigType, destination: int) -> None:
|
||||
|
||||
@@ -22,25 +22,18 @@ CONF_MAX_RETRIES = "max_retries"
|
||||
# The control byte and the CRC-16
|
||||
FRAME_OVERHEAD = 3
|
||||
|
||||
# Unless the link works out a longer one from how long its packets take, as a radio link does
|
||||
DEFAULT_TIMEOUT = cv.TimePeriodMilliseconds(milliseconds=200)
|
||||
|
||||
HdlcReliability = packet_interface_ns.class_("HdlcReliability", PacketInterface)
|
||||
|
||||
|
||||
def check_ack_delay(config: ConfigType, timeout: cv.TimePeriod) -> None:
|
||||
"""The peer, configured the same, must hear the acknowledgement before its timeout."""
|
||||
if config[CONF_ACK_DELAY].total_milliseconds >= timeout.total_milliseconds:
|
||||
raise cv.Invalid(
|
||||
f"Must be shorter than '{CONF_TIMEOUT}', which is {timeout.total_milliseconds}ms",
|
||||
path=[CONF_ACK_DELAY],
|
||||
)
|
||||
|
||||
|
||||
def _ack_delay_within_timeout(config: ConfigType) -> ConfigType:
|
||||
# Without a timeout, the link checks against the one it chooses
|
||||
if (timeout := config.get(CONF_TIMEOUT)) is not None:
|
||||
check_ack_delay(config, timeout)
|
||||
"""The peer, configured the same, must hear the acknowledgement before its timeout."""
|
||||
if (
|
||||
config[CONF_ACK_DELAY].total_milliseconds
|
||||
>= config[CONF_TIMEOUT].total_milliseconds
|
||||
):
|
||||
raise cv.Invalid(
|
||||
f"Must be shorter than '{CONF_TIMEOUT}'", path=[CONF_ACK_DELAY]
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
@@ -49,7 +42,7 @@ HDLC_SCHEMA = cv.All(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(HdlcReliability),
|
||||
# Retransmitting more often than the main loop runs would exhaust the retries at once
|
||||
cv.Optional(CONF_TIMEOUT): cv.All(
|
||||
cv.Optional(CONF_TIMEOUT, default="200ms"): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(min=cv.TimePeriod(milliseconds=50)),
|
||||
),
|
||||
@@ -83,7 +76,7 @@ async def build_hdlc(
|
||||
psram=True,
|
||||
)
|
||||
var = cg.new_Pvariable(config[CONF_ID], lower, tx_slots, queue_length)
|
||||
cg.add(var.set_ack_timeout(config.get(CONF_TIMEOUT, DEFAULT_TIMEOUT)))
|
||||
cg.add(var.set_ack_timeout(config[CONF_TIMEOUT]))
|
||||
cg.add(var.set_ack_delay(config[CONF_ACK_DELAY]))
|
||||
cg.add(var.set_max_retries(config[CONF_MAX_RETRIES]))
|
||||
await cg.register_component(var, config)
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace esphome::packet_interface {
|
||||
|
||||
/**
|
||||
* Decides whether a half-duplex radio link may send while its radio reports that it is receiving a packet.
|
||||
*
|
||||
* Sending is refused while the radio is busy, so the packet being received is not lost. A busy period starts when a
|
||||
* send first finds the radio busy, and ends when a send finds it free or end_busy() is called. Once a busy period has
|
||||
* lasted max_busy_ms, sends go ahead, so a busy flag that never clears cannot stop sending for good, however often
|
||||
* sends are tried. Code generation sets max_busy_ms from the time the radio takes to receive the largest packet.
|
||||
*
|
||||
* On a link whose peer replies, sending also takes turns: after a send, a send in a later loop pass waits up to
|
||||
* turnaround_ms, the time the peer takes to start a reply and the radio to notice it, unless a packet arrives first.
|
||||
* Without this, the next frame of a burst goes out on top of the peer's acknowledgement. Sends in the same loop pass,
|
||||
* such as the fragments of one packet, are not held back.
|
||||
*/
|
||||
class ListenBeforeTalk {
|
||||
public:
|
||||
static constexpr uint32_t DEFAULT_MAX_BUSY_MS = 2000;
|
||||
|
||||
void set_max_busy(uint32_t max_busy_ms) { this->max_busy_ms_ = max_busy_ms; }
|
||||
uint32_t get_max_busy() const { return this->max_busy_ms_; }
|
||||
void set_turnaround(uint32_t turnaround_ms) { this->turnaround_ms_ = turnaround_ms; }
|
||||
|
||||
/// True if a packet may be sent at \p now, in the loop pass that started at \p pass; \p busy says whether the
|
||||
/// radio is receiving.
|
||||
bool may_send(bool busy, uint32_t now, uint32_t pass) {
|
||||
if (!busy) {
|
||||
this->busy_ = false;
|
||||
return !this->turn_held_ || pass == this->sent_pass_ || now - this->sent_ms_ >= this->turnaround_ms_;
|
||||
}
|
||||
if (!this->busy_) {
|
||||
this->busy_ = true;
|
||||
this->busy_since_ms_ = now;
|
||||
}
|
||||
return now - this->busy_since_ms_ >= this->max_busy_ms_;
|
||||
}
|
||||
/// Ends the busy period, when the radio is seen to be free other than by a send.
|
||||
void end_busy() { this->busy_ = false; }
|
||||
/// A packet was sent, ending at \p now, in the loop pass that started at \p pass.
|
||||
void sent(uint32_t now, uint32_t pass) {
|
||||
this->turn_held_ = this->turnaround_ms_ != 0;
|
||||
this->sent_ms_ = now;
|
||||
this->sent_pass_ = pass;
|
||||
}
|
||||
/// A packet arrived, so the peer has taken its turn.
|
||||
void received() { this->turn_held_ = false; }
|
||||
/// How long the current busy period has lasted at \p now.
|
||||
uint32_t busy_for(uint32_t now) const { return now - this->busy_since_ms_; }
|
||||
|
||||
protected:
|
||||
uint32_t busy_since_ms_{0};
|
||||
uint32_t max_busy_ms_{DEFAULT_MAX_BUSY_MS};
|
||||
uint32_t sent_ms_{0};
|
||||
uint32_t sent_pass_{0};
|
||||
uint32_t turnaround_ms_{0};
|
||||
bool busy_{false};
|
||||
bool turn_held_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::packet_interface
|
||||
@@ -1,70 +0,0 @@
|
||||
#include "radio_link.h"
|
||||
#include <cinttypes>
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome::packet_interface {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "packet_interface.radio");
|
||||
|
||||
bool RadioLinkBase::check_size_(size_t size) const {
|
||||
if (size <= this->max_packet_size_)
|
||||
return true;
|
||||
ESP_LOGW(TAG, "Packet of %zu bytes is larger than %zu", size, this->max_packet_size_);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool RadioLinkBase::may_send_(bool busy) {
|
||||
if (busy)
|
||||
this->enable_loop();
|
||||
return this->listen_before_talk_.may_send(busy, this->current_time_ms(), App.get_loop_component_start_time());
|
||||
}
|
||||
|
||||
bool RadioLinkBase::sent_(bool sent, bool busy) {
|
||||
if (busy) {
|
||||
uint32_t busy_ms = this->listen_before_talk_.busy_for(this->current_time_ms());
|
||||
// The radio listens again, and a reception that starts now must be waited for afresh
|
||||
this->listen_before_talk_.end_busy();
|
||||
// The limit is longer than any packet takes, so the radio's busy indication is stuck or the channel is jammed
|
||||
ESP_LOGW(TAG, "Radio busy for %" PRIu32 "ms, %s", busy_ms,
|
||||
sent ? LOG_STR_LITERAL("sent anyway") : LOG_STR_LITERAL("tried to send anyway, which failed"));
|
||||
}
|
||||
if (sent)
|
||||
this->listen_before_talk_.sent(this->current_time_ms(), App.get_loop_component_start_time());
|
||||
return sent;
|
||||
}
|
||||
|
||||
bool RadioLinkBase::skip_empty_() const {
|
||||
ESP_LOGV(TAG, "Empty packet not sent");
|
||||
return true;
|
||||
}
|
||||
|
||||
void RadioLinkBase::radio_not_ready_(bool failed) {
|
||||
if (!failed || this->failed_warned_)
|
||||
return;
|
||||
this->failed_warned_ = true;
|
||||
ESP_LOGW(TAG, "%s: The radio failed to set up, so nothing can be sent", this->name_);
|
||||
}
|
||||
|
||||
void RadioLinkBase::radio_free_() {
|
||||
this->listen_before_talk_.end_busy();
|
||||
this->disable_loop();
|
||||
}
|
||||
|
||||
void RadioLinkBase::receive_(const std::vector<uint8_t> &packet, float rssi, float snr) {
|
||||
// The radio takes packets up to its own limit, so a larger one is from a device configured differently
|
||||
if (packet.size() > this->max_packet_size_) {
|
||||
ESP_LOGV(TAG, "%s: Packet of %zu bytes is larger than %zu, dropped", this->name_, packet.size(),
|
||||
this->max_packet_size_);
|
||||
this->note_received_(false);
|
||||
return;
|
||||
}
|
||||
this->note_received_(true);
|
||||
this->listen_before_talk_.received();
|
||||
PacketMetaData meta_data;
|
||||
meta_data.rssi = rssi;
|
||||
meta_data.snr = snr;
|
||||
this->on_receive_from_interface_(PacketBuffer(packet.data(), packet.size()), meta_data);
|
||||
}
|
||||
|
||||
} // namespace esphome::packet_interface
|
||||
@@ -1,101 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/components/packet_interface/listen_before_talk.h"
|
||||
#include "esphome/components/packet_interface/packet_interface.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome::packet_interface {
|
||||
|
||||
/// The parts of RadioLink that do not depend on the radio.
|
||||
class RadioLinkBase : public PacketInterface {
|
||||
public:
|
||||
void set_max_busy(uint32_t max_busy_ms) { this->listen_before_talk_.set_max_busy(max_busy_ms); }
|
||||
void set_turnaround(uint32_t turnaround_ms) { this->listen_before_talk_.set_turnaround(turnaround_ms); }
|
||||
|
||||
protected:
|
||||
explicit RadioLinkBase(size_t max_packet_size) : PacketInterface(max_packet_size) {}
|
||||
/// The time now, not the loop's start: earlier sends in the same loop pass may have waited seconds for the radio.
|
||||
virtual uint32_t current_time_ms() const { return millis(); }
|
||||
/// False if a packet of this size is too large.
|
||||
bool check_size_(size_t size) const;
|
||||
/// False if sending must wait, as the radio is busy or the peer may be about to reply; starts loop() to watch a
|
||||
/// busy radio.
|
||||
bool may_send_(bool busy);
|
||||
/**
|
||||
* Returns \p sent, after ending the busy period and warning if the packet was sent over a radio that seemed busy.
|
||||
* Called only once the radio has the packet, as a log line can run an automation that sends again and overwrites
|
||||
* the buffer it came from.
|
||||
*/
|
||||
bool sent_(bool sent, bool busy);
|
||||
/// Returns true for an empty packet, which the radios cannot send, so there is nothing to retry.
|
||||
bool skip_empty_() const;
|
||||
/// Warns once when a send finds the radio failed; until its setup has run, sends are refused quietly.
|
||||
void radio_not_ready_(bool failed);
|
||||
/// Ends the busy period when loop() finds the radio free.
|
||||
void radio_free_();
|
||||
void receive_(const std::vector<uint8_t> &packet, float rssi, float snr);
|
||||
|
||||
// First, so it fills the spare byte at the end of PacketInterface
|
||||
bool failed_warned_{false};
|
||||
ListenBeforeTalk listen_before_talk_;
|
||||
};
|
||||
|
||||
/**
|
||||
* A packet interface link over a packet radio: each packet is one radio packet, with the RSSI and SNR of received
|
||||
* packets in the metadata. Sending waits for the radio to finish transmitting. A radio cannot hear while it sends, so
|
||||
* sending is refused while the radio is receiving a packet. A reliability layer sends the packet again shortly, and
|
||||
* entity_transport after a delay; other callers drop it, logging at debug level at most. While a busy period lasts,
|
||||
* loop() watches the radio and ends the period as soon as the radio is free, so a later reception starts a new one.
|
||||
*
|
||||
* Radio provides is_receiving(), register_listener(Listener *) and transmit_packet(const uint8_t *, size_t), which
|
||||
* returns an Error whose value 0 means success. Listener declares on_packet(packet, rssi, snr). MaxPacketSize is the
|
||||
* radio's limit.
|
||||
*/
|
||||
template<typename Radio, typename Listener, typename Error, size_t MaxPacketSize>
|
||||
class RadioLink : public RadioLinkBase, public Listener {
|
||||
public:
|
||||
RadioLink(Radio *radio, size_t max_packet_size) : RadioLinkBase(max_packet_size), radio_(radio) {}
|
||||
|
||||
void setup() override {
|
||||
this->radio_->register_listener(this);
|
||||
this->disable_loop();
|
||||
}
|
||||
// Runs only during a busy period; a busy flag that never clears keeps it running. A radio that failed meanwhile
|
||||
// is not read again, as reading it may wait on the radio every time
|
||||
void loop() override {
|
||||
if (!this->radio_->is_ready() || !this->radio_->is_receiving())
|
||||
this->radio_free_();
|
||||
}
|
||||
|
||||
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override {
|
||||
// The radio is set up after the layers above, which may send from their own setup
|
||||
if (!this->radio_->is_ready()) {
|
||||
this->radio_not_ready_(this->radio_->is_failed());
|
||||
return false;
|
||||
}
|
||||
size_t size = data.size();
|
||||
if (size == 0)
|
||||
return this->skip_empty_();
|
||||
bool busy = this->radio_->is_receiving();
|
||||
if (!this->check_size_(size) || !this->may_send_(busy))
|
||||
return false;
|
||||
if (data.is_contiguous()) {
|
||||
auto span = data.as_span();
|
||||
return this->sent_(this->radio_->transmit_packet(span.data(), span.size()) == Error{}, busy);
|
||||
}
|
||||
uint8_t buf[MaxPacketSize];
|
||||
data.copy_to(buf, size);
|
||||
return this->sent_(this->radio_->transmit_packet(buf, size) == Error{}, busy);
|
||||
}
|
||||
|
||||
void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) override {
|
||||
this->receive_(packet, rssi, snr);
|
||||
}
|
||||
|
||||
protected:
|
||||
Radio *radio_;
|
||||
};
|
||||
|
||||
} // namespace esphome::packet_interface
|
||||
@@ -1,311 +0,0 @@
|
||||
"""Shared code generation for the packet interface links over packet radios, such as the SX126x and SX127x."""
|
||||
|
||||
from collections.abc import Callable, Iterator
|
||||
import logging
|
||||
import math
|
||||
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import CONF_MAX_PACKET_SIZE
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_FRAGMENTATION, CONF_ID, CONF_TIMEOUT
|
||||
from esphome.core import CORE
|
||||
from esphome.cpp_generator import MockObjClass
|
||||
import esphome.final_validate as fv
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from . import new_packet_interface, packet_interface_schema, reliability_layers
|
||||
from .layers import (
|
||||
ADDRESSING,
|
||||
CONF_ADDRESSING,
|
||||
CONF_PEERS,
|
||||
CONF_RELIABILITY,
|
||||
STACK_ORDER,
|
||||
)
|
||||
from .layers.basic_fragmentation import HEADER_SIZE as FRAGMENT_HEADER_SIZE
|
||||
from .layers.hdlc_reliability import (
|
||||
CONF_ACK_DELAY,
|
||||
CONF_MAX_QUEUE_LENGTH,
|
||||
DEFAULT_TIMEOUT,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# The largest packet a radio receives, from any device on the channel, given the radio's settings
|
||||
ReceiveLimit = Callable[[ConfigType], int]
|
||||
|
||||
# The radios' payload length register is 8 bits
|
||||
MAX_PACKET_SIZE = 255
|
||||
# The smallest max_packet_size that packet_interface_schema() takes
|
||||
MIN_PACKET_SIZE = 16
|
||||
|
||||
# Listen before talk holds a send back for at most the airtime of the largest packet, and never less than this
|
||||
MIN_MAX_BUSY_MS = 2000
|
||||
# Room for the preamble detection and the radio's own delays
|
||||
MARGIN = 1.25
|
||||
# The radio drivers stop waiting for a packet to be sent after this long
|
||||
TRANSMIT_TIMEOUT_MS = 4000
|
||||
# An acknowledgement, with room for the layers below the reliability layer, such as addressing and the rolling code
|
||||
ACK_PACKET_SIZE = 32
|
||||
# From the end of a packet to the start of the peer's reply: a loop pass to read the packet, one to answer it, and the
|
||||
# radio's start-up
|
||||
REPLY_START_MS = 50
|
||||
# A LoRa packet's explicit header is in its first 8 symbols
|
||||
LORA_HEADER_SYMBOLS = 8
|
||||
|
||||
|
||||
def _lora_symbol_s(radio: ConfigType) -> float:
|
||||
bandwidth_hz = (
|
||||
float(str(radio["bandwidth"]).removesuffix("kHz").replace("_", ".")) * 1000
|
||||
)
|
||||
return (1 << radio["spreading_factor"]) / bandwidth_hz
|
||||
|
||||
|
||||
def packet_airtime_ms(radio: ConfigType, size: int) -> float:
|
||||
"""The time a radio configured as in radio takes to send a packet of size bytes.
|
||||
|
||||
LoRa follows the Semtech formula for an explicit header; the low data rate optimization is assumed when a symbol
|
||||
takes more than 16 ms, as the drivers enable it. Other modulations count the preamble, sync word, length byte,
|
||||
payload and CRC at the bitrate, treating the preamble size as bytes, which gives the longer time.
|
||||
"""
|
||||
if str(radio["modulation"]) == "LORA":
|
||||
spreading_factor = radio["spreading_factor"]
|
||||
coding_rate = int(str(radio["coding_rate"])[-1]) - 4
|
||||
symbol_s = _lora_symbol_s(radio)
|
||||
low_data_rate = 1 if symbol_s > 0.016 else 0
|
||||
crc = 16 if radio.get("crc_enable", False) else 0
|
||||
payload_symbols = 8 + max(
|
||||
math.ceil(
|
||||
(8 * size - 4 * spreading_factor + 28 + crc)
|
||||
/ (4 * (spreading_factor - 2 * low_data_rate))
|
||||
)
|
||||
* (coding_rate + 4),
|
||||
0,
|
||||
)
|
||||
return (radio["preamble_size"] + 4.25 + payload_symbols) * symbol_s * 1000
|
||||
bytes_sent = (
|
||||
radio["preamble_size"] + len(radio.get("sync_value", [])) + 1 + size + 2
|
||||
)
|
||||
return bytes_sent * 8 / radio["bitrate"] * 1000
|
||||
|
||||
|
||||
def turnaround_ms(radio: ConfigType) -> int:
|
||||
"""How long, after sending, to wait for the peer's reply to start and for the radio to receive its header, so
|
||||
the peer's reply is not sent over.
|
||||
"""
|
||||
if str(radio["modulation"]) == "LORA":
|
||||
symbols = radio["preamble_size"] + 4.25 + LORA_HEADER_SYMBOLS
|
||||
header_ms = symbols * _lora_symbol_s(radio) * 1000
|
||||
else:
|
||||
header_bytes = radio["preamble_size"] + len(radio.get("sync_value", [])) + 1
|
||||
header_ms = header_bytes * 8 / radio["bitrate"] * 1000
|
||||
return math.ceil((header_ms + REPLY_START_MS) * MARGIN)
|
||||
|
||||
|
||||
def max_busy_ms(radio: ConfigType, max_packet_size: int) -> int:
|
||||
"""How long listen before talk may hold a send back, for packets of up to max_packet_size bytes."""
|
||||
return max(
|
||||
MIN_MAX_BUSY_MS, math.ceil(packet_airtime_ms(radio, max_packet_size) * MARGIN)
|
||||
)
|
||||
|
||||
|
||||
def _fits_transmit_time(radio: ConfigType, size: int) -> bool:
|
||||
return packet_airtime_ms(radio, size) * MARGIN <= TRANSMIT_TIMEOUT_MS
|
||||
|
||||
|
||||
def _largest_sendable(radio: ConfigType, limit: int) -> int | None:
|
||||
"""The largest max_packet_size, up to limit, whose packets the radio sends within the drivers' wait."""
|
||||
return next(
|
||||
(
|
||||
size
|
||||
for size in range(limit, MIN_PACKET_SIZE - 1, -1)
|
||||
if _fits_transmit_time(radio, size)
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
|
||||
def channel_packet_size(radio: ConfigType, receive_limit: int) -> int:
|
||||
"""The largest packet any device on the channel can send: no more than the radio receives, and no more than a
|
||||
device with these radio settings is allowed to send.
|
||||
"""
|
||||
return _largest_sendable(radio, receive_limit) or receive_limit
|
||||
|
||||
|
||||
def reliability_timeout_ms(
|
||||
radio: ConfigType, packet_size: int, ack_delay_ms: int
|
||||
) -> int:
|
||||
"""The shortest reliability timeout: before acknowledging, the peer may be sending, or receiving from another
|
||||
device, a packet of packet_size bytes.
|
||||
"""
|
||||
airtime = packet_airtime_ms(radio, packet_size) + packet_airtime_ms(
|
||||
radio, ACK_PACKET_SIZE
|
||||
)
|
||||
return math.ceil(airtime * MARGIN) + ack_delay_ms
|
||||
|
||||
|
||||
def _check_transmit_time(config: ConfigType, radio: ConfigType) -> None:
|
||||
"""The radio drivers give up on a packet that takes longer to send than they wait."""
|
||||
size = config[CONF_MAX_PACKET_SIZE]
|
||||
if _fits_transmit_time(radio, size):
|
||||
return
|
||||
largest = _largest_sendable(radio, size - 1)
|
||||
advice = (
|
||||
f"set '{CONF_MAX_PACKET_SIZE}' to {largest} or less"
|
||||
if largest is not None
|
||||
else "use faster radio settings"
|
||||
)
|
||||
raise cv.Invalid(
|
||||
f"With these radio settings a packet of {size} bytes takes {packet_airtime_ms(radio, size):.0f} ms to "
|
||||
f"send, longer than the radio waits for; {advice}",
|
||||
path=[CONF_MAX_PACKET_SIZE],
|
||||
)
|
||||
|
||||
|
||||
def _check_reliability_timeouts(
|
||||
config: ConfigType, radio: ConfigType, packet_size: int
|
||||
) -> None:
|
||||
"""A reliability timeout shorter than a packet and its acknowledgement take would send packets again too soon."""
|
||||
for path, layer in reliability_layers(config):
|
||||
if (timeout := layer.get(CONF_TIMEOUT)) is None:
|
||||
continue
|
||||
needed = reliability_timeout_ms(
|
||||
radio, packet_size, layer[CONF_ACK_DELAY].total_milliseconds
|
||||
)
|
||||
if timeout.total_milliseconds < needed:
|
||||
raise cv.Invalid(
|
||||
f"With these radio settings a packet and its acknowledgement can take {needed} ms, so this must be "
|
||||
f"at least {needed}ms; leave it out to use that",
|
||||
path=[*path, CONF_TIMEOUT],
|
||||
)
|
||||
|
||||
|
||||
def _stacks(config: ConfigType) -> Iterator[tuple[ConfigType, int]]:
|
||||
"""The link's stacks of layers, on the link or in each peer, each with the largest packet below it."""
|
||||
size = config[CONF_MAX_PACKET_SIZE]
|
||||
if (addressing := config.get(CONF_ADDRESSING)) is None:
|
||||
yield config, size
|
||||
return
|
||||
size = ADDRESSING.get_type(addressing).packet_size(addressing, size)
|
||||
yield from ((stack, size) for stack in addressing.get(CONF_PEERS, [config]))
|
||||
|
||||
|
||||
def _radio_packets_per_send(stack: ConfigType, size: int) -> int:
|
||||
"""How many radio packets one send through the stack can send one after another, each waited for in turn."""
|
||||
if (reliability := stack.get(CONF_RELIABILITY)) is not None:
|
||||
# It sends, and after a timeout sends again, every packet in its queue at once
|
||||
return reliability[CONF_MAX_QUEUE_LENGTH]
|
||||
if (fragmentation := stack.get(CONF_FRAGMENTATION)) is None:
|
||||
return 1
|
||||
for kind in STACK_ORDER:
|
||||
if kind.key == CONF_FRAGMENTATION:
|
||||
break
|
||||
if (layer := stack.get(kind.key)) is not None:
|
||||
size = kind.get_type(layer).packet_size(layer, size)
|
||||
fragment = size - FRAGMENT_HEADER_SIZE
|
||||
if fragment <= 0:
|
||||
return 1
|
||||
return math.ceil(fragmentation[CONF_MAX_PACKET_SIZE] / fragment)
|
||||
|
||||
|
||||
def _warn_long_sends(config: ConfigType, radio: ConfigType) -> None:
|
||||
"""The radio drivers wait for each packet to be sent, so a send of several packets holds up everything else."""
|
||||
airtime = packet_airtime_ms(radio, config[CONF_MAX_PACKET_SIZE])
|
||||
for stack, size in _stacks(config):
|
||||
count = _radio_packets_per_send(stack, size)
|
||||
if count > 1 and count * airtime > TRANSMIT_TIMEOUT_MS:
|
||||
_LOGGER.warning(
|
||||
"A send through '%s' can send %d radio packets in a row, holding up other components for about "
|
||||
"%.1f s while the radio sends them; reduce '%s' or the reliability '%s', or use faster radio "
|
||||
"settings",
|
||||
stack[CONF_ID].id,
|
||||
count,
|
||||
count * airtime / 1000,
|
||||
CONF_MAX_PACKET_SIZE,
|
||||
CONF_MAX_QUEUE_LENGTH,
|
||||
)
|
||||
|
||||
|
||||
def radio_link_schema(
|
||||
class_: MockObjClass, radio_id_key: str, radio_class: MockObjClass
|
||||
) -> cv.Schema:
|
||||
"""Schema for a radio link, referring to its radio by radio_id_key."""
|
||||
return packet_interface_schema(
|
||||
class_, MAX_PACKET_SIZE, MAX_PACKET_SIZE, chooses_reliability_timeout=True
|
||||
).extend({cv.GenerateID(radio_id_key): cv.use_id(radio_class)})
|
||||
|
||||
|
||||
def _two_way_layer(config: ConfigType) -> str | None:
|
||||
"""The first layer on the link or in a peer that keeps state for one peer, so needs packets back from it."""
|
||||
for stack in (config, *config.get(CONF_ADDRESSING, {}).get(CONF_PEERS, [])):
|
||||
for kind in STACK_ORDER:
|
||||
if (layer := stack.get(kind.key)) is not None and kind.get_type(
|
||||
layer
|
||||
).single_peer(layer):
|
||||
return kind.key
|
||||
return None
|
||||
|
||||
|
||||
def takes_turns(config: ConfigType) -> bool:
|
||||
"""Whether sends wait for the peer to reply: only a reliability layer's peer answers each packet at once."""
|
||||
return next(reliability_layers(config), None) is not None
|
||||
|
||||
|
||||
def radio_link_final_validate(
|
||||
radio_id_key: str,
|
||||
payload_length_key: str,
|
||||
rx_start_key: str,
|
||||
check: Callable[[ConfigType, ConfigType], None] | None = None,
|
||||
receive_limit: ReceiveLimit = lambda radio: MAX_PACKET_SIZE,
|
||||
) -> Callable[[ConfigType], ConfigType]:
|
||||
"""Final validation for a radio link: packets vary in length, the radio listens after sending when a layer needs
|
||||
replies, and check(link, radio) checks the rest.
|
||||
"""
|
||||
|
||||
def validator(config: ConfigType) -> ConfigType:
|
||||
radio = fv.get_declaring_config(config[radio_id_key])
|
||||
if radio[payload_length_key] != 0:
|
||||
raise cv.Invalid(
|
||||
f"Packets vary in length, so the radio's '{payload_length_key}' must be 0",
|
||||
path=[radio_id_key],
|
||||
)
|
||||
if not radio[rx_start_key] and (layer := _two_way_layer(config)) is not None:
|
||||
raise cv.Invalid(
|
||||
f"This '{layer}' needs packets from the peer, but the radio sleeps after sending unless "
|
||||
f"'{rx_start_key}' is true",
|
||||
path=[radio_id_key],
|
||||
)
|
||||
if check is not None:
|
||||
check(config, radio)
|
||||
_check_transmit_time(config, radio)
|
||||
_check_reliability_timeouts(
|
||||
config, radio, channel_packet_size(radio, receive_limit(radio))
|
||||
)
|
||||
_warn_long_sends(config, radio)
|
||||
return config
|
||||
|
||||
return validator
|
||||
|
||||
|
||||
async def new_radio_link(
|
||||
config: ConfigType,
|
||||
radio_id_key: str,
|
||||
receive_limit: ReceiveLimit = lambda radio: MAX_PACKET_SIZE,
|
||||
) -> None:
|
||||
"""Create a radio link, with timings worked out from the radio's settings and the largest packet on the channel."""
|
||||
radio_config = fv.get_declaring_config(config[radio_id_key], CORE.config)
|
||||
packet_size = channel_packet_size(radio_config, receive_limit(radio_config))
|
||||
# A reliability layer without a timeout gets one long enough for a packet and its acknowledgement
|
||||
for _, layer in reliability_layers(config):
|
||||
if CONF_TIMEOUT not in layer:
|
||||
needed = reliability_timeout_ms(
|
||||
radio_config, packet_size, layer[CONF_ACK_DELAY].total_milliseconds
|
||||
)
|
||||
layer[CONF_TIMEOUT] = cv.TimePeriodMilliseconds(
|
||||
milliseconds=max(DEFAULT_TIMEOUT.total_milliseconds, needed)
|
||||
)
|
||||
radio = await cg.get_variable(config[radio_id_key])
|
||||
var = await new_packet_interface(config, radio, config[CONF_MAX_PACKET_SIZE])
|
||||
cg.add(var.set_max_busy(max_busy_ms(radio_config, packet_size)))
|
||||
# Taking turns would hold up a run of one-way sends, and a reliability layer sends again what it refuses
|
||||
if takes_turns(config):
|
||||
cg.add(var.set_turnaround(turnaround_ms(radio_config)))
|
||||
@@ -46,8 +46,6 @@ class XxteaEncryption : public PacketInterface {
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override;
|
||||
// One encrypted packet per packet, so a fragmentation layer above sees the link's room
|
||||
size_t get_send_capacity() const override { return this->lower_->get_send_capacity(); }
|
||||
|
||||
void set_key(std::initializer_list<uint32_t> key);
|
||||
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
from esphome.components import packet_interface
|
||||
from esphome.components.packet_interface.radio_link import (
|
||||
new_radio_link,
|
||||
radio_link_final_validate,
|
||||
radio_link_schema,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from .. import (
|
||||
CONF_MODULATION,
|
||||
CONF_PAYLOAD_LENGTH,
|
||||
CONF_RX_START,
|
||||
CONF_SX126X_ID,
|
||||
CONF_SYNC_VALUE,
|
||||
SX126x,
|
||||
SX126xListener,
|
||||
sx126x_ns,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DEPENDENCIES = ["sx126x"]
|
||||
|
||||
SX126xPacketInterface = sx126x_ns.class_(
|
||||
"SX126xPacketInterface", packet_interface.PacketInterface, SX126xListener
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = radio_link_schema(SX126xPacketInterface, CONF_SX126X_ID, SX126x)
|
||||
|
||||
|
||||
def _check_fsk(config: ConfigType, radio: ConfigType) -> None:
|
||||
"""Without LoRa, the radio can tell that a packet is arriving only from its sync word, which listen before talk
|
||||
needs.
|
||||
"""
|
||||
if radio[CONF_MODULATION] != "LORA" and not radio[CONF_SYNC_VALUE]:
|
||||
raise cv.Invalid(
|
||||
f"Without LoRa modulation the radio needs a '{CONF_SYNC_VALUE}', so that the link can tell when a packet "
|
||||
"is arriving and not send over it",
|
||||
path=[CONF_SX126X_ID],
|
||||
)
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = radio_link_final_validate(
|
||||
CONF_SX126X_ID, CONF_PAYLOAD_LENGTH, CONF_RX_START, _check_fsk
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
await new_radio_link(config, CONF_SX126X_ID)
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/packet_interface/radio_link.h"
|
||||
#include "esphome/components/sx126x/sx126x.h"
|
||||
|
||||
namespace esphome::sx126x {
|
||||
|
||||
using SX126xPacketInterface = packet_interface::RadioLink<SX126x, SX126xListener, SX126xError, SX126X_MAX_PACKET_SIZE>;
|
||||
|
||||
} // namespace esphome::sx126x
|
||||
@@ -99,8 +99,6 @@ class SX126x final : public Component {
|
||||
bool cancel_transmit_async(SX126xTransmitListener *listener);
|
||||
bool is_transmitting() const { return this->radio_.is_transmitting(); }
|
||||
void register_listener(SX126xListener *listener) { this->listeners_.push_back(listener); }
|
||||
// True while a packet is being received, or has been received and not yet read; sending would lose it.
|
||||
bool is_receiving() { return this->radio_.is_receiving(); }
|
||||
Trigger<std::vector<uint8_t>, float, float> *get_packet_trigger() { return &this->packet_trigger_; }
|
||||
|
||||
private:
|
||||
|
||||
@@ -246,20 +246,18 @@ void SX126xCommands::calibrate_image(uint32_t frequency) {
|
||||
}
|
||||
}
|
||||
|
||||
// Only the dio1 interrupts are routed to DIO1; DIO2 and DIO3 stay unused.
|
||||
void SX126xCommands::set_dio_irq_params_(uint16_t flags, uint16_t dio1) {
|
||||
// The IRQ mask also routes to DIO1; DIO2 and DIO3 stay unused.
|
||||
void SX126xCommands::set_dio_irq_params_(uint16_t irq) {
|
||||
uint8_t buf[8];
|
||||
put_be16(buf, flags);
|
||||
put_be16(buf + 2, dio1);
|
||||
put_be16(buf, irq);
|
||||
put_be16(buf + 2, irq);
|
||||
put_be16(buf + 4, IRQ_RADIO_NONE);
|
||||
put_be16(buf + 6, IRQ_RADIO_NONE);
|
||||
this->write_opcode_(RADIO_SET_DIOIRQPARAMS, buf, 8);
|
||||
}
|
||||
|
||||
void SX126xCommands::receive() {
|
||||
// A valid header or sync word is flagged for SX126xRadio::is_receiving(), but not signalled on DIO1
|
||||
const uint16_t irq = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR;
|
||||
this->set_dio_irq_params_(irq | IRQ_HEADER_VALID | IRQ_SYNCWORD_VALID, irq);
|
||||
this->set_dio_irq_params_(IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR);
|
||||
|
||||
uint8_t buf[3];
|
||||
|
||||
@@ -275,7 +273,7 @@ void SX126xCommands::receive() {
|
||||
}
|
||||
|
||||
void SX126xCommands::transmit(uint32_t timeout_ms) {
|
||||
this->set_dio_irq_params_(IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT, IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT);
|
||||
this->set_dio_irq_params_(IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT);
|
||||
|
||||
uint8_t buf[3];
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class SX126xCommands {
|
||||
private:
|
||||
void read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size);
|
||||
void write_opcode_(uint8_t opcode, const uint8_t *data, uint8_t size);
|
||||
void set_dio_irq_params_(uint16_t flags, uint16_t dio1);
|
||||
void set_dio_irq_params_(uint16_t irq);
|
||||
void read_register_(uint16_t reg, uint8_t *data, uint8_t size);
|
||||
void write_register_(uint16_t reg, const uint8_t *data, uint8_t size);
|
||||
static void encode_packet_params(const RadioProfile &profile, AppliedProfile &applied);
|
||||
|
||||
@@ -195,26 +195,6 @@ bool SX126xRadio::restore_() {
|
||||
return this->bus().ok();
|
||||
}
|
||||
|
||||
bool SX126xRadio::is_receiving() {
|
||||
if (this->state_ == RadioState::RADIO_STATE_HOLDING_RX) {
|
||||
return true;
|
||||
}
|
||||
// A radio that sleeps after sending is not listening, and reading it would wake it
|
||||
if (this->busy_ || this->state_ != RadioState::RADIO_STATE_IDLE || !this->applied_.rx_start) {
|
||||
return false;
|
||||
}
|
||||
// DIO1 signals a received packet that loop() has not read yet
|
||||
if (this->pending_irq_ != 0 || this->dio1_pin_->digital_read()) {
|
||||
return true;
|
||||
}
|
||||
this->busy_ = true;
|
||||
this->bus().begin();
|
||||
const uint16_t status = this->commands_.read_irq();
|
||||
this->busy_ = false;
|
||||
// A failed read counts as receiving, so the link holds the send and warns if the radio stays unreadable
|
||||
return !this->bus().ok() || (status & (IRQ_HEADER_VALID | IRQ_SYNCWORD_VALID)) != 0;
|
||||
}
|
||||
|
||||
RadioEffects SX126xRadio::poll(uint32_t now, std::vector<uint8_t> &packet) {
|
||||
if (this->busy_) {
|
||||
return {};
|
||||
|
||||
@@ -104,9 +104,6 @@ class SX126xRadio {
|
||||
bool can_configure() const { return !this->busy_ && !this->is_transmitting(); }
|
||||
bool can_change_mode() const { return !this->busy_ && this->state_ == RadioState::RADIO_STATE_IDLE; }
|
||||
size_t max_packet_size() const { return this->applied_.max_packet_size(); }
|
||||
// True while a packet is being received, or has been received and not yet read, so a send would lose it.
|
||||
// Reads the chip only when the radio is idle and listening; any other state reports false, a failed read true.
|
||||
bool is_receiving();
|
||||
|
||||
// Requires can_configure(). Resets the chip and applies the requested profile.
|
||||
RadioEffects configure(const RadioProfile &requested, std::span<char, 16> version);
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
from esphome.components import packet_interface
|
||||
from esphome.components.packet_interface import CONF_MAX_PACKET_SIZE
|
||||
from esphome.components.packet_interface.radio_link import (
|
||||
MAX_PACKET_SIZE,
|
||||
new_radio_link,
|
||||
radio_link_final_validate,
|
||||
radio_link_schema,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.types import ConfigType
|
||||
|
||||
from .. import (
|
||||
CONF_MODULATION,
|
||||
CONF_PACKET_MODE,
|
||||
CONF_PAYLOAD_LENGTH,
|
||||
CONF_RX_START,
|
||||
CONF_SX127X_ID,
|
||||
CONF_SYNC_VALUE,
|
||||
SX127x,
|
||||
SX127xListener,
|
||||
sx127x_ns,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DEPENDENCIES = ["sx127x"]
|
||||
|
||||
# Outside LoRa mode the 64 byte FIFO also holds the length byte
|
||||
FSK_MAX_PACKET_SIZE = 63
|
||||
|
||||
SX127xPacketInterface = sx127x_ns.class_(
|
||||
"SX127xPacketInterface", packet_interface.PacketInterface, SX127xListener
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = radio_link_schema(SX127xPacketInterface, CONF_SX127X_ID, SX127x)
|
||||
|
||||
|
||||
def _check_fsk(config: ConfigType, radio: ConfigType) -> None:
|
||||
"""Without LoRa, the radio must send packets, not a continuous bit stream, and holds fewer bytes.
|
||||
|
||||
It can tell that a packet is arriving only from its sync word, which listen before talk needs.
|
||||
"""
|
||||
if radio[CONF_MODULATION] == "LORA":
|
||||
return
|
||||
if not radio.get(CONF_PACKET_MODE, False):
|
||||
raise cv.Invalid(
|
||||
f"Without LoRa modulation the radio's '{CONF_PACKET_MODE}' must be true, as a link sends packets",
|
||||
path=[CONF_SX127X_ID],
|
||||
)
|
||||
if not radio.get(CONF_SYNC_VALUE):
|
||||
raise cv.Invalid(
|
||||
f"Without LoRa modulation the radio needs a '{CONF_SYNC_VALUE}', so that the link can tell when a "
|
||||
"packet is arriving and not send over it",
|
||||
path=[CONF_SX127X_ID],
|
||||
)
|
||||
if config[CONF_MAX_PACKET_SIZE] > FSK_MAX_PACKET_SIZE:
|
||||
raise cv.Invalid(
|
||||
f"Without LoRa modulation the radio sends at most {FSK_MAX_PACKET_SIZE} bytes",
|
||||
path=[CONF_MAX_PACKET_SIZE],
|
||||
)
|
||||
|
||||
|
||||
def _receive_limit(radio: ConfigType) -> int:
|
||||
return MAX_PACKET_SIZE if radio[CONF_MODULATION] == "LORA" else FSK_MAX_PACKET_SIZE
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = radio_link_final_validate(
|
||||
CONF_SX127X_ID, CONF_PAYLOAD_LENGTH, CONF_RX_START, _check_fsk, _receive_limit
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
await new_radio_link(config, CONF_SX127X_ID, _receive_limit)
|
||||
@@ -1,10 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/components/packet_interface/radio_link.h"
|
||||
#include "esphome/components/sx127x/sx127x.h"
|
||||
|
||||
namespace esphome::sx127x {
|
||||
|
||||
using SX127xPacketInterface = packet_interface::RadioLink<SX127x, SX127xListener, SX127xError, SX127X_MAX_PACKET_SIZE>;
|
||||
|
||||
} // namespace esphome::sx127x
|
||||
@@ -62,20 +62,15 @@ void SX127x::read_fifo_(std::vector<uint8_t> &packet) {
|
||||
this->disable();
|
||||
}
|
||||
|
||||
// Burst write: the radio moves to the next register after each byte, except for REG_FIFO
|
||||
void SX127x::write_registers_(uint8_t reg, const uint8_t *data, size_t len) {
|
||||
void SX127x::write_fifo_(const uint8_t *data, size_t len) {
|
||||
this->enable();
|
||||
this->write_byte(reg | 0x80);
|
||||
this->write_array(data, len);
|
||||
this->write_byte(REG_FIFO | 0x80);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
this->transfer_byte(data[i]);
|
||||
}
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void SX127x::write_register16_(uint8_t msb_reg, uint16_t value) {
|
||||
uint8_t bytes[2];
|
||||
put_be16(bytes, value);
|
||||
this->write_registers_(msb_reg, bytes, sizeof(bytes));
|
||||
}
|
||||
|
||||
void SX127x::setup() {
|
||||
// setup reset
|
||||
this->rst_pin_->setup();
|
||||
@@ -160,9 +155,9 @@ const LogString *SX127x::configure_radio_() {
|
||||
|
||||
// set freq
|
||||
uint64_t frf = ((uint64_t) this->frequency_ << 19) / FXOSC;
|
||||
uint8_t frf_bytes[3];
|
||||
put_be24(frf_bytes, static_cast<uint32_t>(frf));
|
||||
this->write_registers_(REG_FRF_MSB, frf_bytes, sizeof(frf_bytes));
|
||||
this->write_register_(REG_FRF_MSB, (uint8_t) ((frf >> 16) & 0xFF));
|
||||
this->write_register_(REG_FRF_MID, (uint8_t) ((frf >> 8) & 0xFF));
|
||||
this->write_register_(REG_FRF_LSB, (uint8_t) ((frf >> 0) & 0xFF));
|
||||
|
||||
// enter standby mode
|
||||
if (!this->set_mode_(MOD_FSK, MODE_STDBY)) {
|
||||
@@ -213,12 +208,14 @@ void SX127x::configure_fsk_ook_() {
|
||||
this->write_register_(REG_RX_BW, progmem_read_byte(&BW_FSK_OOK[this->bandwidth_]));
|
||||
|
||||
// set fdev
|
||||
uint16_t fdev = clamp_at_most((this->deviation_ * 4096) / 250000, uint32_t{0x3FFF});
|
||||
this->write_register16_(REG_FDEV_MSB, fdev);
|
||||
uint32_t fdev = std::min((this->deviation_ * 4096) / 250000, (uint32_t) 0x3FFF);
|
||||
this->write_register_(REG_FDEV_MSB, (uint8_t) ((fdev >> 8) & 0xFF));
|
||||
this->write_register_(REG_FDEV_LSB, (uint8_t) ((fdev >> 0) & 0xFF));
|
||||
|
||||
// set bitrate
|
||||
uint16_t bitrate = (FXOSC + this->bitrate_ / 2) / this->bitrate_; // round up
|
||||
this->write_register16_(REG_BITRATE_MSB, bitrate);
|
||||
uint64_t bitrate = (FXOSC + this->bitrate_ / 2) / this->bitrate_; // round up
|
||||
this->write_register_(REG_BITRATE_MSB, (uint8_t) ((bitrate >> 8) & 0xFF));
|
||||
this->write_register_(REG_BITRATE_LSB, (uint8_t) ((bitrate >> 0) & 0xFF));
|
||||
|
||||
// configure rx and afc
|
||||
this->write_register_(REG_AFC_BW, progmem_read_byte(&BW_FSK_OOK[this->afc_bandwidth_]));
|
||||
@@ -238,7 +235,7 @@ void SX127x::configure_fsk_ook_() {
|
||||
this->write_register_(REG_PAYLOAD_LENGTH_LSB, this->payload_length_);
|
||||
this->write_register_(REG_PACKET_CONFIG_1, crc_mode | FIXED_LENGTH);
|
||||
} else {
|
||||
this->write_register_(REG_PAYLOAD_LENGTH_LSB, this->get_max_packet_size());
|
||||
this->write_register_(REG_PAYLOAD_LENGTH_LSB, this->get_max_packet_size() - 1);
|
||||
this->write_register_(REG_PACKET_CONFIG_1, crc_mode | VARIABLE_LENGTH);
|
||||
}
|
||||
this->write_register_(REG_PACKET_CONFIG_2, PACKET_MODE);
|
||||
@@ -267,7 +264,8 @@ void SX127x::configure_fsk_ook_() {
|
||||
} else {
|
||||
this->write_register_(REG_PREAMBLE_DETECT, PREAMBLE_DETECTOR_OFF);
|
||||
}
|
||||
this->write_register16_(REG_PREAMBLE_SIZE_MSB, this->preamble_size_);
|
||||
this->write_register_(REG_PREAMBLE_SIZE_MSB, this->preamble_size_ >> 8);
|
||||
this->write_register_(REG_PREAMBLE_SIZE_LSB, this->preamble_size_ & 0xFF);
|
||||
|
||||
// config sync generation and setup ook threshold
|
||||
uint8_t bitsync = this->bitsync_ ? BIT_SYNC_ON : BIT_SYNC_OFF;
|
||||
@@ -295,7 +293,8 @@ void SX127x::configure_lora_() {
|
||||
|
||||
// config preamble
|
||||
if (this->preamble_size_ >= 6) {
|
||||
this->write_register16_(REG_PREAMBLE_LEN_MSB, this->preamble_size_);
|
||||
this->write_register_(REG_PREAMBLE_LEN_MSB, this->preamble_size_ >> 8);
|
||||
this->write_register_(REG_PREAMBLE_LEN_LSB, this->preamble_size_ & 0xFF);
|
||||
}
|
||||
|
||||
// optimize detection
|
||||
@@ -326,8 +325,7 @@ size_t SX127x::get_max_packet_size() {
|
||||
if (this->applied_.modulation == MOD_LORA) {
|
||||
return SX127X_MAX_PACKET_SIZE;
|
||||
}
|
||||
// The 64 byte FIFO also holds the length byte
|
||||
return 63;
|
||||
return 64;
|
||||
}
|
||||
|
||||
SX127xError SX127x::transmit_packet(const uint8_t *data, size_t len) {
|
||||
@@ -392,7 +390,7 @@ SX127xError SX127x::start_transmit_(SX127xTransmitListener *listener, const uint
|
||||
} else if (this->applied_.payload_length == 0) {
|
||||
this->write_register_(REG_FIFO, len);
|
||||
}
|
||||
this->write_registers_(REG_FIFO, data, len);
|
||||
this->write_fifo_(data, len);
|
||||
if (!this->enter_tx_()) {
|
||||
this->fail_(set_mode_failure());
|
||||
return SX127xError::SX127X_ERROR_NOT_READY;
|
||||
@@ -453,28 +451,6 @@ bool SX127x::restore_() {
|
||||
return this->applied_.rx_start ? this->enter_rx_() : this->set_mode_(this->applied_.modulation, MODE_SLEEP);
|
||||
}
|
||||
|
||||
bool SX127x::is_receiving() {
|
||||
if (this->state_ == RadioState::RADIO_STATE_HOLDING_RX) {
|
||||
return true;
|
||||
}
|
||||
// A radio that sleeps after sending is not listening
|
||||
if (this->state_ != RadioState::RADIO_STATE_IDLE || !this->applied_.rx_start || this->dio0_pin_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
const bool lora = this->applied_.modulation == MOD_LORA;
|
||||
// In receive mode DIO0 signals a received packet that loop() has not read yet. In LoRa it also signals TX_DONE
|
||||
// after the TX mode action.
|
||||
if (this->dio0_pin_->digital_read() && (!lora || (this->read_register_(REG_IRQ_FLAGS) & RX_DONE) != 0)) {
|
||||
return true;
|
||||
}
|
||||
if (!lora) {
|
||||
// Set from the sync word until the packet has been read; the preamble flag stays set when no sync word follows
|
||||
return (this->read_register_(REG_IRQ_FLAGS_1) & IRQ_FLAGS_1_SYNC_ADDRESS_MATCH) != 0;
|
||||
}
|
||||
return (this->read_register_(REG_MODEM_STAT) &
|
||||
(MODEM_STAT_SIGNAL_DETECTED | MODEM_STAT_SIGNAL_SYNCHRONIZED | MODEM_STAT_HEADER_VALID)) != 0;
|
||||
}
|
||||
|
||||
bool SX127x::packet_waiting_() {
|
||||
if (this->applied_.modulation == MOD_LORA) {
|
||||
const uint8_t flags = this->read_register_(REG_IRQ_FLAGS);
|
||||
|
||||
@@ -126,8 +126,6 @@ class SX127x final : public Component,
|
||||
bool cancel_transmit_async(SX127xTransmitListener *listener);
|
||||
bool is_transmitting() const { return this->state_ == RadioState::RADIO_STATE_TRANSMITTING; }
|
||||
void register_listener(SX127xListener *listener) { this->listeners_.push_back(listener); }
|
||||
// True while a packet is being received, or has been received and not yet read; sending would lose it.
|
||||
bool is_receiving();
|
||||
Trigger<std::vector<uint8_t>, float, float> *get_packet_trigger() { return &this->packet_trigger_; }
|
||||
|
||||
protected:
|
||||
@@ -166,8 +164,7 @@ class SX127x final : public Component,
|
||||
// True once the send has ended, with its outcome in result
|
||||
bool poll_transmit_(uint32_t now, SX127xError &result);
|
||||
SX127xError finish_transmit_(SX127xError result);
|
||||
void write_registers_(uint8_t reg, const uint8_t *data, size_t len);
|
||||
void write_register16_(uint8_t msb_reg, uint16_t value);
|
||||
void write_fifo_(const uint8_t *data, size_t len);
|
||||
void read_fifo_(std::vector<uint8_t> &packet);
|
||||
void write_register_(uint8_t reg, uint8_t value);
|
||||
void call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr);
|
||||
|
||||
@@ -46,7 +46,6 @@ enum SX127xReg : uint8_t {
|
||||
REG_PAYLOAD_LENGTH_LSB = 0x32,
|
||||
REG_FIFO_THRESH = 0x35,
|
||||
REG_IMAGE_CAL = 0x3B,
|
||||
REG_IRQ_FLAGS_1 = 0x3E,
|
||||
REG_IRQ_FLAGS2 = 0x3F,
|
||||
// LoRa registers
|
||||
REG_FIFO_ADDR_PTR = 0x0D,
|
||||
@@ -262,17 +261,6 @@ enum SX127xIrqFlags : uint8_t {
|
||||
CAD_DETECTED = 0x01,
|
||||
};
|
||||
|
||||
enum SX127xIrqFlags1 : uint8_t {
|
||||
IRQ_FLAGS_1_PREAMBLE_DETECT = 0x02,
|
||||
IRQ_FLAGS_1_SYNC_ADDRESS_MATCH = 0x01,
|
||||
};
|
||||
|
||||
enum SX127xModemStat : uint8_t {
|
||||
MODEM_STAT_SIGNAL_DETECTED = 0x01,
|
||||
MODEM_STAT_SIGNAL_SYNCHRONIZED = 0x02,
|
||||
MODEM_STAT_HEADER_VALID = 0x08,
|
||||
};
|
||||
|
||||
enum SX127xIrqFlags2 : uint8_t {
|
||||
PACKET_SENT = 0x08,
|
||||
PAYLOAD_READY = 0x04,
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
"""TCP links. The packet_interface platform carries packets over a TCP connection."""
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DOMAIN = "tcp"
|
||||
@@ -1,132 +0,0 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor, packet_interface, socket
|
||||
from esphome.components.const import (
|
||||
CONF_ALLOWED_IPS,
|
||||
CONF_CONNECTED,
|
||||
CONF_HOST,
|
||||
CONF_RECONNECT_INTERVAL,
|
||||
CONF_ROLE,
|
||||
)
|
||||
from esphome.components.packet_interface import (
|
||||
CONF_MAX_PACKET_SIZE,
|
||||
new_packet_interface,
|
||||
packet_interface_schema,
|
||||
)
|
||||
from esphome.components.packet_interface.layers import (
|
||||
CONF_ADDRESSING,
|
||||
CONF_RELIABILITY,
|
||||
CONF_SESSION,
|
||||
)
|
||||
from esphome.components.packet_interface.layers.basic_session import CONF_KEEP_ALIVE
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_PORT,
|
||||
CONF_TIMEOUT,
|
||||
DEVICE_CLASS_CONNECTIVITY,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
)
|
||||
from esphome.types import ConfigType
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DEPENDENCIES = ["network"]
|
||||
|
||||
|
||||
def AUTO_LOAD(config: ConfigType | None = None) -> list[str]:
|
||||
"""The binary_sensor component only for the connected sensor; without a config, as tooling calls it, both."""
|
||||
if not config or CONF_CONNECTED in config:
|
||||
return ["socket", "binary_sensor"]
|
||||
return ["socket"]
|
||||
|
||||
|
||||
# Each packet and its 2 byte length must fit the socket link's 1024 byte send buffer
|
||||
MAX_PACKET_SIZE = 1022
|
||||
|
||||
tcp_ns = cg.esphome_ns.namespace("tcp")
|
||||
TcpPacketInterface = tcp_ns.class_(
|
||||
"TcpPacketInterface", packet_interface.PacketInterface
|
||||
)
|
||||
|
||||
|
||||
def _timeout(config: ConfigType) -> ConfigType:
|
||||
"""Default the timeout to three keep-alive intervals, and require it to be longer than one."""
|
||||
keep_alive = config[CONF_KEEP_ALIVE].total_milliseconds
|
||||
if (timeout := config.get(CONF_TIMEOUT)) is None:
|
||||
config[CONF_TIMEOUT] = cv.positive_time_period_milliseconds(
|
||||
f"{keep_alive * 3}ms"
|
||||
)
|
||||
elif timeout.total_milliseconds <= keep_alive:
|
||||
raise cv.Invalid("Must be longer than keep_alive", path=[CONF_TIMEOUT])
|
||||
return config
|
||||
|
||||
|
||||
BASE_SCHEMA = packet_interface_schema(
|
||||
TcpPacketInterface,
|
||||
MAX_PACKET_SIZE,
|
||||
MAX_PACKET_SIZE,
|
||||
# TCP resends what is lost, joins exactly two devices, and its connection state is the session
|
||||
provides=(CONF_ADDRESSING, CONF_RELIABILITY, CONF_SESSION),
|
||||
).extend(
|
||||
{
|
||||
cv.Required(CONF_PORT): cv.port,
|
||||
cv.Optional(
|
||||
CONF_RECONNECT_INTERVAL, default="5s"
|
||||
): cv.positive_time_period_milliseconds,
|
||||
cv.Optional(CONF_KEEP_ALIVE, default="10s"): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(min=cv.TimePeriod(seconds=1), max=cv.TimePeriod(hours=1)),
|
||||
),
|
||||
# Nothing received for this long closes the connection; it must be longer than the peer's keep_alive
|
||||
cv.Optional(CONF_TIMEOUT): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=cv.TimePeriod(hours=3)),
|
||||
),
|
||||
cv.Optional(CONF_CONNECTED): binary_sensor.binary_sensor_schema(
|
||||
device_class=DEVICE_CLASS_CONNECTIVITY,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.typed_schema(
|
||||
{
|
||||
"client": cv.All(
|
||||
BASE_SCHEMA.extend({cv.Required(CONF_HOST): socket.ipv4_host}),
|
||||
_timeout,
|
||||
),
|
||||
"server": cv.All(
|
||||
BASE_SCHEMA.extend(
|
||||
{cv.Optional(CONF_ALLOWED_IPS): socket.IPV4_ALLOW_SCHEMA}
|
||||
),
|
||||
_timeout,
|
||||
),
|
||||
},
|
||||
key=CONF_ROLE,
|
||||
default_type="client",
|
||||
),
|
||||
# A client uses one socket; a server one listener and one accepted socket
|
||||
socket.consume_role_sockets("tcp.packet_interface"),
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config: ConfigType) -> None:
|
||||
rx_buffer = cg.static_buffer(
|
||||
config[CONF_ID], cg.uint8, config[CONF_MAX_PACKET_SIZE]
|
||||
)
|
||||
var = await new_packet_interface(config, rx_buffer)
|
||||
if config[CONF_ROLE] == "server":
|
||||
socket.require_tcp_listener()
|
||||
cg.add(var.set_server(True))
|
||||
socket.add_ipv4_allow(
|
||||
var.set_allow, config.get(CONF_ALLOWED_IPS), config[CONF_ID]
|
||||
)
|
||||
else:
|
||||
socket.require_tcp_client_link()
|
||||
cg.add(var.set_host(config[CONF_HOST]))
|
||||
cg.add(var.set_port(config[CONF_PORT]))
|
||||
cg.add(var.set_reconnect_interval(config[CONF_RECONNECT_INTERVAL]))
|
||||
cg.add(var.set_keep_alive(config[CONF_KEEP_ALIVE]))
|
||||
cg.add(var.set_receive_timeout(config[CONF_TIMEOUT]))
|
||||
binary_sensors = binary_sensor.sub_binary_sensors(config)
|
||||
await binary_sensors(CONF_CONNECTED, var.set_connected_binary_sensor)
|
||||
@@ -1,182 +0,0 @@
|
||||
#include "tcp_packet_interface.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome::tcp {
|
||||
|
||||
using packet_interface::PacketBuffer;
|
||||
using packet_interface::PacketMetaData;
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "tcp.packet_interface");
|
||||
|
||||
// The packet length before each packet
|
||||
static constexpr size_t LENGTH_SIZE = 2;
|
||||
// A peer that sends without a pause must not hold the main loop here; a frame being read carries over to the next pass
|
||||
static constexpr uint8_t MAX_FRAMES_PER_LOOP = 8;
|
||||
|
||||
TcpPacketInterface::TcpPacketInterface(std::span<uint8_t> rx_buffer)
|
||||
: packet_interface::PacketInterface(rx_buffer.size()), rx_buffer_(rx_buffer) {
|
||||
this->capabilities_ = packet_interface::CAPABILITY_RELIABILITY | packet_interface::CAPABILITY_SESSION |
|
||||
packet_interface::CAPABILITY_ADDRESSING;
|
||||
}
|
||||
|
||||
void TcpPacketInterface::setup() {
|
||||
this->link_.begin(TAG);
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
this->listener_.begin(TAG);
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (this->connected_binary_sensor_ != nullptr)
|
||||
this->connected_binary_sensor_->publish_initial_state(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
void TcpPacketInterface::dump_config() {
|
||||
bool server = false;
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
server = this->server_;
|
||||
#endif
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"TCP Packet Interface '%s':\n"
|
||||
" Role: %s\n"
|
||||
" Address: %s:%u\n"
|
||||
" Reconnect Interval: %" PRIu32 "ms\n"
|
||||
" Keep Alive: %" PRIu32 "ms\n"
|
||||
" Timeout: %" PRIu32 "ms\n"
|
||||
" Max Packet Size: %zu",
|
||||
this->name_, server ? LOG_STR_LITERAL("server") : LOG_STR_LITERAL("client"),
|
||||
server ? LOG_STR_LITERAL("*") : this->link_.host(), this->link_.port(),
|
||||
this->link_.reconnect_interval(), this->keep_alive_ms_, this->timeout_ms_, this->max_packet_size_);
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
this->listener_.dump_config();
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
LOG_BINARY_SENSOR(" ", "Connected", this->connected_binary_sensor_);
|
||||
#endif
|
||||
}
|
||||
|
||||
void TcpPacketInterface::on_shutdown() {
|
||||
this->link_.close();
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
this->listener_.close();
|
||||
#endif
|
||||
}
|
||||
|
||||
void TcpPacketInterface::loop() {
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
if (this->server_) {
|
||||
// A new client is accepted only once the drop of the last has been reported
|
||||
this->listener_.poll(this->link_, !this->connected_);
|
||||
} else {
|
||||
this->link_.poll();
|
||||
}
|
||||
#else
|
||||
this->link_.poll();
|
||||
#endif
|
||||
if (this->link_.connected() != this->connected_)
|
||||
this->on_connection_(this->link_.connected());
|
||||
if (!this->connected_)
|
||||
return;
|
||||
this->read_();
|
||||
if (!this->connected_)
|
||||
return;
|
||||
uint32_t now = App.get_loop_component_start_time();
|
||||
if (now - this->last_rx_ms_ > this->timeout_ms_) {
|
||||
ESP_LOGW(TAG, "%s: Nothing received for %" PRIu32 "ms, closing the connection", this->name_, this->timeout_ms_);
|
||||
this->link_.close();
|
||||
this->link_.note_attempt();
|
||||
this->on_connection_(false);
|
||||
return;
|
||||
}
|
||||
if (now - this->last_tx_ms_ >= this->keep_alive_ms_)
|
||||
this->queue_frame_(nullptr, 0);
|
||||
this->flush_();
|
||||
}
|
||||
|
||||
void TcpPacketInterface::flush_() { this->tx_waiting_ = !this->link_.flush_tx(); }
|
||||
|
||||
size_t TcpPacketInterface::get_send_capacity() const {
|
||||
if (!this->tx_waiting_)
|
||||
return SIZE_MAX;
|
||||
return this->link_.tx_free() / (this->max_packet_size_ + LENGTH_SIZE);
|
||||
}
|
||||
|
||||
void TcpPacketInterface::on_connection_(bool connected) {
|
||||
ESP_LOGD(TAG, "%s: Connection %s", this->name_, connected ? LOG_STR_LITERAL("up") : LOG_STR_LITERAL("down"));
|
||||
this->connected_ = connected;
|
||||
this->tx_waiting_ = false;
|
||||
this->rx_have_ = 0;
|
||||
this->last_rx_ms_ = this->last_tx_ms_ = App.get_loop_component_start_time();
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (this->connected_binary_sensor_ != nullptr)
|
||||
this->connected_binary_sensor_->publish_state(connected);
|
||||
#endif
|
||||
this->session_callback_.call(connected);
|
||||
}
|
||||
|
||||
void TcpPacketInterface::read_() {
|
||||
for (uint8_t frames = 0; frames != MAX_FRAMES_PER_LOOP;) {
|
||||
ssize_t count;
|
||||
if (this->rx_have_ < LENGTH_SIZE) {
|
||||
count = this->link_.read(this->rx_header_ + this->rx_have_, LENGTH_SIZE - this->rx_have_);
|
||||
} else {
|
||||
count = this->link_.read(this->rx_buffer_.data() + this->rx_have_ - LENGTH_SIZE,
|
||||
this->rx_length_ + LENGTH_SIZE - this->rx_have_);
|
||||
}
|
||||
// Nothing more for now; a dropped link is seen on the next loop
|
||||
if (count <= 0)
|
||||
return;
|
||||
this->last_rx_ms_ = App.get_loop_component_start_time();
|
||||
this->rx_have_ += count;
|
||||
if (this->rx_have_ < LENGTH_SIZE)
|
||||
continue;
|
||||
if (this->rx_have_ == LENGTH_SIZE) {
|
||||
this->rx_length_ = encode_uint16(this->rx_header_[1], this->rx_header_[0]);
|
||||
if (this->rx_length_ > this->max_packet_size_) {
|
||||
// The stream cannot be resynchronised, so start a new connection
|
||||
ESP_LOGW(TAG, "%s: Packet of %u bytes is larger than %zu, closing the connection", this->name_,
|
||||
this->rx_length_, this->max_packet_size_);
|
||||
this->link_.close();
|
||||
this->link_.note_attempt();
|
||||
this->on_connection_(false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (this->rx_have_ == this->rx_length_ + LENGTH_SIZE) {
|
||||
this->rx_have_ = 0;
|
||||
frames++;
|
||||
// A zero length packet is a keep-alive
|
||||
if (this->rx_length_ != 0)
|
||||
this->on_receive_from_interface_(PacketBuffer(this->rx_buffer_.data(), this->rx_length_), {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool TcpPacketInterface::queue_frame_(const PacketBuffer *data, size_t size) {
|
||||
if (this->link_.tx_free() < size + LENGTH_SIZE)
|
||||
return false;
|
||||
uint8_t *tail = this->link_.tx_tail();
|
||||
tail[0] = static_cast<uint8_t>(size);
|
||||
tail[1] = static_cast<uint8_t>(size >> 8);
|
||||
if (data != nullptr)
|
||||
data->copy_to(tail + LENGTH_SIZE, size);
|
||||
this->link_.tx_commit(size + LENGTH_SIZE);
|
||||
this->last_tx_ms_ = App.get_loop_component_start_time();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TcpPacketInterface::send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) {
|
||||
size_t size = data.size();
|
||||
if (size > this->max_packet_size_) {
|
||||
ESP_LOGW(TAG, "%s: Packet of %zu bytes is larger than %zu", this->name_, size, this->max_packet_size_);
|
||||
return false;
|
||||
}
|
||||
if (!this->connected_ || !this->queue_frame_(&data, size))
|
||||
return false;
|
||||
// A write that fails closes the link and discards what was queued, this packet included
|
||||
this->flush_();
|
||||
return this->link_.connected();
|
||||
}
|
||||
|
||||
} // namespace esphome::tcp
|
||||
@@ -1,93 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <span>
|
||||
|
||||
#include "esphome/components/packet_interface/packet_interface.h"
|
||||
#include "esphome/components/socket/tcp_client_link.h"
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
#include "esphome/components/socket/tcp_listener.h"
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#endif
|
||||
|
||||
namespace esphome::tcp {
|
||||
|
||||
/**
|
||||
* A packet interface link over one TCP connection, as a client that connects to a host, or a server that accepts
|
||||
* one client at a time.
|
||||
*
|
||||
* Each packet is sent as a 2 byte length, little endian, then the packet. TCP delivers the stream in order and
|
||||
* resends what is lost, and the connection joins exactly two devices, so the link provides the reliability,
|
||||
* addressing and session capabilities itself. Its connection state is the session state.
|
||||
*
|
||||
* Each packet is written to the socket as it is sent, so the link takes packets as fast as the socket does and keeps
|
||||
* the default, unlimited, send capacity; a packet is refused only while the connection is down or the socket is full.
|
||||
*
|
||||
* A zero length packet is a keep-alive, sent when nothing else has been sent for keep_alive. TCP can take minutes to
|
||||
* notice a peer that has gone without closing the connection, so the link closes it, and reconnects or accepts again,
|
||||
* when nothing has been received for the timeout, which must be longer than the peer's keep-alive interval.
|
||||
*/
|
||||
class TcpPacketInterface : public packet_interface::PacketInterface {
|
||||
public:
|
||||
/// @param rx_buffer Holds the packet being received; its size is the largest packet, and it must outlive the link
|
||||
explicit TcpPacketInterface(std::span<uint8_t> rx_buffer);
|
||||
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
void on_shutdown() override;
|
||||
bool send_to_interface(const packet_interface::PacketBuffer &data,
|
||||
const packet_interface::PacketMetaData &meta_data) override;
|
||||
/**
|
||||
* Unlimited while the socket takes what is sent. Once frames wait for it, how many more full frames fit. A socket
|
||||
* that stalls part way through a fragmented packet still cuts it short.
|
||||
*/
|
||||
size_t get_send_capacity() const override;
|
||||
|
||||
void set_host(const char *host) { this->link_.set_host(host); }
|
||||
void set_port(uint16_t port) { this->link_.set_port(port); }
|
||||
void set_reconnect_interval(uint32_t interval_ms) { this->link_.set_reconnect_interval(interval_ms); }
|
||||
void set_keep_alive(uint32_t keep_alive_ms) { this->keep_alive_ms_ = keep_alive_ms; }
|
||||
/// The connection is closed when nothing has been received for this long; the peer's keep_alive sets the pace.
|
||||
void set_receive_timeout(uint32_t timeout_ms) { this->timeout_ms_ = timeout_ms; }
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
void set_server(bool server) { this->server_ = server; }
|
||||
#ifdef USE_SOCKET_IPV4_ALLOW
|
||||
void set_allow(const socket::Ipv4AllowEntry *entries, size_t count) { this->listener_.set_allow(entries, count); }
|
||||
#endif
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
SUB_BINARY_SENSOR(connected)
|
||||
#endif
|
||||
|
||||
bool is_connected() const { return this->connected_; }
|
||||
|
||||
protected:
|
||||
void on_connection_(bool connected);
|
||||
void read_();
|
||||
bool queue_frame_(const packet_interface::PacketBuffer *data, size_t size);
|
||||
void flush_();
|
||||
|
||||
socket::TcpClientLink link_;
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
socket::TcpListener listener_;
|
||||
#endif
|
||||
const std::span<uint8_t> rx_buffer_;
|
||||
uint32_t keep_alive_ms_{10000};
|
||||
uint32_t timeout_ms_{30000};
|
||||
uint32_t last_rx_ms_{0};
|
||||
uint32_t last_tx_ms_{0};
|
||||
uint16_t rx_length_{0};
|
||||
// Bytes of the current frame received so far, counting the 2 byte length
|
||||
uint16_t rx_have_{0};
|
||||
uint8_t rx_header_[2]{};
|
||||
bool connected_ : 1 {false};
|
||||
bool tx_waiting_ : 1 {false}; // frames are queued that the socket has not taken yet
|
||||
#ifdef USE_SOCKET_TCP_LISTENER
|
||||
bool server_ : 1 {false};
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace esphome::tcp
|
||||
@@ -614,9 +614,25 @@ async def to_code(config: ConfigType) -> None:
|
||||
|
||||
|
||||
def FILTER_SOURCE_FILES() -> list[str]:
|
||||
"""Filter out web_server_v1.cpp when version is not 1."""
|
||||
"""Filter out web_server_v1.cpp when version is not 1, the ESP32 event stream elsewhere, and the server on host."""
|
||||
files_to_filter: list[str] = []
|
||||
|
||||
# The host has no web server; it builds only sse_chunk.cpp, for its unit tests
|
||||
if CORE.is_host:
|
||||
files_to_filter.extend(
|
||||
(
|
||||
"event_source.cpp",
|
||||
"list_entities.cpp",
|
||||
"web_server.cpp",
|
||||
"web_server_v1.cpp",
|
||||
)
|
||||
)
|
||||
return files_to_filter
|
||||
|
||||
# The /events implementation is ESP32 only
|
||||
if not CORE.is_esp32:
|
||||
files_to_filter.extend(("event_source.cpp", "sse_chunk.cpp"))
|
||||
|
||||
# web_server_v1.cpp is only needed when version is 1
|
||||
config = CORE.config.get(DOMAIN, {})
|
||||
if config.get(CONF_VERSION, 2) != 1:
|
||||
|
||||
@@ -0,0 +1,491 @@
|
||||
#include "event_source.h"
|
||||
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iterator>
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "web_server.h"
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "web_server_idf");
|
||||
|
||||
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
|
||||
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic error "-Wstack-usage=2048"
|
||||
#endif
|
||||
|
||||
AsyncEventSource::~AsyncEventSource() {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
|
||||
for (auto *ses : *vec) {
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
|
||||
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
}
|
||||
this->web_server_->enable_loop_soon_any_context();
|
||||
}
|
||||
|
||||
// clang-analyzer traces a false-positive leak path from loop() through
|
||||
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
|
||||
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
bool AsyncEventSource::loop() {
|
||||
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
|
||||
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
|
||||
this->adopt_pending_sessions_main_loop_();
|
||||
}
|
||||
|
||||
// Clean up dead sessions safely
|
||||
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
|
||||
// and the main loop handles the actual cleanup to avoid race conditions
|
||||
for (size_t i = 0; i < this->sessions_.size();) {
|
||||
auto *ses = this->sessions_[i];
|
||||
// If the session has a dead socket (marked by destroy callback)
|
||||
if (ses->safe_to_delete()) {
|
||||
// destroy() already logged the close with the fd; don't double-log here.
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
|
||||
this->sessions_[i] = this->sessions_.back();
|
||||
this->sessions_.pop_back();
|
||||
} else {
|
||||
ses->loop();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
// A session still waiting for httpd to commit its context keeps the loop alive too
|
||||
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
|
||||
std::vector<AsyncEventSourceResponse *> incoming;
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
incoming.swap(this->pending_sessions_);
|
||||
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
for (auto *rsp : incoming) {
|
||||
// Already disconnected? Drop it; skip session start on a dead session.
|
||||
if (rsp->safe_to_delete()) {
|
||||
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
continue;
|
||||
}
|
||||
// httpd commits the session context only after the creating handler returns, so stay
|
||||
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
|
||||
if (!rsp->is_attached()) {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
continue;
|
||||
}
|
||||
this->sessions_.push_back(rsp);
|
||||
rsp->start_session_main_loop_();
|
||||
}
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->is_connected()) {
|
||||
ses->try_send_nodefer(message, message_len, event, id, reconnect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
|
||||
// Skip if no connected clients to avoid unnecessary processing
|
||||
if (this->empty())
|
||||
return;
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->is_connected()) {
|
||||
ses->deferrable_send_state(source, detail_all, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AsyncEventSourceResponse::AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server,
|
||||
WebServer *ws)
|
||||
: server_(server), web_server_(ws), entities_iterator_(ws, this) {
|
||||
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
|
||||
this->begin(request, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, SSE_FIRST_CHUNK);
|
||||
}
|
||||
static_assert(web_server_base::stream_head_iov_count(std::size(SSE_HEADERS) + WEB_SERVER_DEFAULT_HEADERS_COUNT) <=
|
||||
web_server_base::STREAM_HEAD_MAX_IOV,
|
||||
"the event stream head must go out in one write");
|
||||
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||
void AsyncEventSourceResponse::start_session_main_loop_() {
|
||||
auto *ws = this->web_server_;
|
||||
|
||||
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
|
||||
// allocation; nothing retries the greeting, so close and let the client reconnect.
|
||||
auto message = ws->get_config_json();
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
|
||||
ESP_LOGW(TAG, "Config not sent to fd %d", this->log_id());
|
||||
this->close();
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER_SORTING
|
||||
for (auto &group : ws->sorting_groups_) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
root["name"] = json::linked(group.second.name);
|
||||
root["sorting_weight"] = group.second.weight;
|
||||
message = builder.serialize();
|
||||
|
||||
// a (very) large number of these should be able to be queued initially without defer
|
||||
// since the only thing in the send buffer at this point is the initial ping/config.
|
||||
// A refusal means the socket is full or closing; the remaining groups are not sent. The
|
||||
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
|
||||
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->log_id());
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
this->entities_iterator_.begin(ws->include_internal_);
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
|
||||
DeferredEvent item(source, message_generator);
|
||||
|
||||
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
|
||||
for (auto &event : this->deferred_queue_) {
|
||||
if (event == item) {
|
||||
return; // Already in queue, no need to update since items are equal
|
||||
}
|
||||
}
|
||||
this->deferred_queue_.push_back(item);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::process_deferred_queue_() {
|
||||
if (this->is_closing()) {
|
||||
return;
|
||||
}
|
||||
while (!deferred_queue_.empty()) {
|
||||
DeferredEvent &de = deferred_queue_.front();
|
||||
if (this->send_json_(de.source_, de.message_generator_)) {
|
||||
if (this->is_closing() || deferred_queue_.empty()) {
|
||||
return;
|
||||
}
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::release_buffers_() {
|
||||
std::vector<DeferredEvent>().swap(this->deferred_queue_);
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->tail_len_ = 0;
|
||||
this->tail_sent_ = 0;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::drain_tail_() {
|
||||
if (this->sending_ || this->is_closing() || this->tail_len_ == 0) {
|
||||
return;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
const size_t remaining = this->tail_len_ - this->tail_sent_;
|
||||
const ssize_t sent = this->write(this->tail_.get() + this->tail_sent_, remaining);
|
||||
if (sent <= 0) {
|
||||
return; // write() owns the stall clock and the close
|
||||
}
|
||||
this->no_memory_logged_ = false;
|
||||
this->tail_sent_ += sent;
|
||||
if (this->tail_sent_ < this->tail_len_) {
|
||||
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
|
||||
return;
|
||||
}
|
||||
// Fully sent; the storage stays for the next stall
|
||||
this->tail_len_ = 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
|
||||
if (this->tail_cap_ >= len) {
|
||||
return true;
|
||||
}
|
||||
if (len > TAIL_MAX_SIZE) {
|
||||
return false;
|
||||
}
|
||||
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
|
||||
// the tail where plain new put it.
|
||||
this->tail_.reset();
|
||||
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
|
||||
this->tail_cap_ = this->tail_ ? len : 0;
|
||||
return this->tail_cap_ != 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
|
||||
size_t message_len, size_t total, size_t sent) {
|
||||
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
|
||||
// refuse it for size, only for memory
|
||||
static_assert(SSE_SEP_LEN * LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
|
||||
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
|
||||
if (!this->reserve_tail_(total)) {
|
||||
if (sent != 0) {
|
||||
// Part of the chunk is on the wire, so the stream is broken and the client has to go
|
||||
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
|
||||
this->close();
|
||||
} else {
|
||||
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
|
||||
}
|
||||
return false;
|
||||
}
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
dst += prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &out = *static_cast<uint8_t **>(ctx);
|
||||
std::memcpy(out, piece, len);
|
||||
out += len;
|
||||
},
|
||||
&dst);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = sent;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::loop() {
|
||||
AsyncStreamResponse::loop();
|
||||
if (this->is_closing()) {
|
||||
if (this->tail_cap_ != 0 || this->deferred_queue_.capacity() != 0) {
|
||||
this->release_buffers_();
|
||||
}
|
||||
return;
|
||||
}
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
if (this->is_closing())
|
||||
return;
|
||||
// One step per loop; refusals retry next pass
|
||||
this->entities_iterator_.try_advance(1);
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
|
||||
#endif
|
||||
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
|
||||
message_generator_t *generator,
|
||||
char *buffer, size_t *size_out) {
|
||||
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
|
||||
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
|
||||
SendGuard guard{*this};
|
||||
json::JsonArena<json::JSON_ARENA_SIZE> arena;
|
||||
json::JsonBuilder builder(&arena);
|
||||
generator(this->web_server_, source, builder);
|
||||
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
|
||||
if (*size_out < JSON_BUF_SIZE) {
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
|
||||
}
|
||||
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
|
||||
|
||||
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
|
||||
size_t json_len = 0;
|
||||
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
|
||||
for (;;) {
|
||||
if (!this->reserve_tail_(cap)) {
|
||||
*size_out = cap;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
|
||||
}
|
||||
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
|
||||
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
|
||||
if (json_len < room) {
|
||||
break;
|
||||
}
|
||||
if (cap >= TAIL_MAX_SIZE) {
|
||||
*size_out = room;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
|
||||
}
|
||||
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
|
||||
}
|
||||
|
||||
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
|
||||
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = 0;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
|
||||
}
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
char buffer[JSON_BUF_SIZE];
|
||||
size_t size;
|
||||
const auto result = this->prepare_json_(source, generator, buffer, &size);
|
||||
switch (result) {
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
|
||||
return this->try_send_nodefer(buffer, size, "state");
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
|
||||
this->drain_tail_();
|
||||
return true;
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
|
||||
SendGuard guard{*this};
|
||||
this->tail_alloc_failed_(size);
|
||||
return false;
|
||||
}
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
|
||||
SendGuard guard{*this};
|
||||
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->stall_.clear();
|
||||
this->no_memory_logged_ = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
|
||||
// Same stall clock as a socket that stops draining, so a session cannot retry forever. A refused write may
|
||||
// already have started it, so the warning keeps its own flag.
|
||||
if (!this->no_memory_logged_) {
|
||||
this->no_memory_logged_ = true;
|
||||
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
|
||||
}
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (!this->stall_.is_running()) {
|
||||
this->stall_.start(now);
|
||||
return;
|
||||
}
|
||||
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
|
||||
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", this->stall_.elapsed(now));
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::ready_to_send_() {
|
||||
if (this->sending_ || !this->is_connected() || this->is_closing()) {
|
||||
return false;
|
||||
}
|
||||
drain_tail_();
|
||||
return !this->is_closing() && this->tail_len_ == 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
// Everything after the prefix goes out straight from the caller's buffer
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
|
||||
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
|
||||
return true; // Match ESPAsyncWebServer: nothing to send
|
||||
}
|
||||
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
|
||||
// The appenders truncate silently, which would put a malformed event on the wire
|
||||
ESP_LOGW(TAG, "Event name too long, dropped");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gather list: the prefix, then the data lines and their separators from the message
|
||||
struct Gather {
|
||||
web_server_base::StreamIovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
|
||||
int iovcnt{1};
|
||||
size_t total{0};
|
||||
bool fits{true};
|
||||
} g;
|
||||
g.total = prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &g = *static_cast<Gather *>(ctx);
|
||||
g.total += len;
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
if (g.iovcnt == MAX_SEND_IOV) {
|
||||
g.fits = false;
|
||||
return;
|
||||
}
|
||||
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
|
||||
},
|
||||
&g);
|
||||
// The header and the terminator are not part of the chunk length
|
||||
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
g.iov[0] = {prefix, prefix_len};
|
||||
|
||||
// A message with more lines than the list holds skips straight to the tail
|
||||
const ssize_t sent = g.fits ? this->write(g.iov, g.iovcnt) : 0;
|
||||
if (sent < 0) {
|
||||
return false;
|
||||
}
|
||||
if (sent > 0) {
|
||||
this->no_memory_logged_ = false;
|
||||
}
|
||||
if (static_cast<size_t>(sent) == g.total) {
|
||||
return true;
|
||||
}
|
||||
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
|
||||
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
|
||||
message_generator_t *message_generator) {
|
||||
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
|
||||
// initial connect sends less
|
||||
if (!detail_all && !this->entities_iterator_.completed())
|
||||
return;
|
||||
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
|
||||
if (this->is_closing()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
|
||||
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
|
||||
// trying to send first
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
// A send error closes the session and clears the queue; nothing is queued after that
|
||||
if (!this->send_json_(source, message_generator) && !this->is_closing()) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
#endif // USE_ESP32 && USE_WEBSERVER
|
||||
@@ -0,0 +1,176 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#if defined(USE_ESP32) && defined(USE_WEBSERVER)
|
||||
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/web_server_base/stream_response.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
#include "list_entities.h"
|
||||
#include "sse_chunk.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
class WebServer;
|
||||
class AsyncEventSource;
|
||||
class AsyncEventSourceResponse;
|
||||
|
||||
using message_generator_t = void(WebServer *, void *, json::JsonBuilder &);
|
||||
|
||||
/*
|
||||
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
|
||||
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
|
||||
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
|
||||
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
|
||||
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
|
||||
because of dedup) would take up only 0.8 kB.
|
||||
*/
|
||||
struct DeferredEvent {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
protected:
|
||||
void *source_;
|
||||
message_generator_t *message_generator_;
|
||||
|
||||
public:
|
||||
DeferredEvent(void *source, message_generator_t *message_generator)
|
||||
: source_(source), message_generator_(message_generator) {}
|
||||
bool operator==(const DeferredEvent &test) const {
|
||||
return (source_ == test.source_ && message_generator_ == test.message_generator_);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
|
||||
"DeferredEvent should have no padding");
|
||||
|
||||
class AsyncEventSourceResponse : public web_server_base::AsyncStreamResponse {
|
||||
friend class AsyncEventSource;
|
||||
|
||||
public:
|
||||
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
// detail_all events go out while entities are still being listed; state events wait until listing is done
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
void loop();
|
||||
|
||||
protected:
|
||||
AsyncEventSourceResponse(AsyncWebServerRequest *request, AsyncEventSource *server, WebServer *ws);
|
||||
|
||||
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
|
||||
void start_session_main_loop_();
|
||||
|
||||
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
|
||||
void process_deferred_queue_();
|
||||
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
|
||||
bool ready_to_send_();
|
||||
// Push what is left of the chunk in tail_ to the socket
|
||||
void drain_tail_();
|
||||
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
|
||||
bool reserve_tail_(size_t len);
|
||||
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
|
||||
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
|
||||
size_t sent);
|
||||
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
|
||||
bool send_json_(void *source, message_generator_t *generator);
|
||||
enum class JsonPrepareResult : uint8_t {
|
||||
JSON_PREPARE_RESULT_BUFFER,
|
||||
JSON_PREPARE_RESULT_TAIL,
|
||||
JSON_PREPARE_RESULT_NO_MEMORY,
|
||||
JSON_PREPARE_RESULT_TOO_LARGE,
|
||||
};
|
||||
// Keep the arena out of the caller's frame so it is gone before the state send.
|
||||
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
|
||||
// unused for TAIL.
|
||||
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
|
||||
size_t *size_out);
|
||||
// Warn once per stall, and close the session once the stall timeout passes with no memory for the tail
|
||||
void tail_alloc_failed_(size_t cap);
|
||||
|
||||
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
|
||||
struct SendGuard {
|
||||
AsyncEventSourceResponse &owner;
|
||||
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
|
||||
~SendGuard() { this->owner.sending_ = false; }
|
||||
};
|
||||
// Frees the tail and the queue's storage once the session is closing
|
||||
void release_buffers_();
|
||||
|
||||
AsyncEventSource *server_;
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
WebServer *web_server_;
|
||||
ListEntitiesIterator entities_iterator_;
|
||||
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
|
||||
// on the send path
|
||||
RAMUniquePtr<uint8_t[]> tail_;
|
||||
uint16_t tail_cap_{0};
|
||||
uint16_t tail_len_{0}; // Zero means nothing pending
|
||||
uint16_t tail_sent_{0};
|
||||
bool sending_{false};
|
||||
// The no-memory warning went out during the current stall; cleared on send progress
|
||||
bool no_memory_logged_{false};
|
||||
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
|
||||
// longer one goes through the tail
|
||||
static constexpr size_t MAX_SEND_LINES = 22;
|
||||
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
|
||||
// Chunk header, retry/id/event lines and the first "data: "
|
||||
static constexpr size_t PREFIX_BUF_SIZE = 128;
|
||||
|
||||
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
|
||||
static constexpr size_t JSON_BUF_SIZE = 1024;
|
||||
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
|
||||
// document is dropped before anything is on the wire
|
||||
static constexpr size_t JSON_MAX_SIZE = 5120;
|
||||
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
|
||||
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
|
||||
};
|
||||
|
||||
class AsyncEventSource : public AsyncWebHandler {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
public:
|
||||
AsyncEventSource(StringRef url, WebServer *ws) : url_(url), web_server_(ws) {}
|
||||
~AsyncEventSource() override;
|
||||
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
bool canHandle(AsyncWebServerRequest *request) const override {
|
||||
if (request->method() != HTTP_GET)
|
||||
return false;
|
||||
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
|
||||
return request->url_to(url_buf) == this->url_;
|
||||
}
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void handleRequest(AsyncWebServerRequest *request) override;
|
||||
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
/// Returns true if there are sessions remaining (including pending cleanup).
|
||||
bool loop();
|
||||
bool empty() { return this->count() == 0; }
|
||||
|
||||
size_t count() const { return this->sessions_.size(); }
|
||||
|
||||
protected:
|
||||
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
|
||||
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
|
||||
|
||||
StringRef url_; // Must outlive this object (string literal)
|
||||
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
|
||||
std::vector<AsyncEventSourceResponse *> sessions_;
|
||||
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
|
||||
std::vector<AsyncEventSourceResponse *> pending_sessions_;
|
||||
Mutex pending_mutex_;
|
||||
WebServer *web_server_;
|
||||
std::atomic<bool> has_pending_sessions_{false};
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
#endif // USE_ESP32 && USE_WEBSERVER
|
||||
@@ -8,14 +8,8 @@
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
#ifdef USE_ESP32
|
||||
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, AsyncEventSource *es) : web_server_(ws), events_(es) {}
|
||||
#elif USE_ARDUINO
|
||||
ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es)
|
||||
: web_server_(ws), events_(es) {}
|
||||
#endif
|
||||
void ListEntitiesIterator::send_all_(void *source, message_generator_t *generator) {
|
||||
this->events_->deferrable_send_state(source, true, generator);
|
||||
this->session_->deferrable_send_state(source, true, generator);
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
|
||||
@@ -1,33 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#ifdef USE_WEBSERVER
|
||||
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/component_iterator.h"
|
||||
namespace esphome::json {
|
||||
class JsonBuilder;
|
||||
} // namespace esphome::json
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
#ifdef USE_ESP32
|
||||
class AsyncEventSource;
|
||||
#endif
|
||||
} // namespace esphome::web_server_idf
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
#if !defined(USE_ESP32) && defined(USE_ARDUINO)
|
||||
// The /events session the iterator lists entities to
|
||||
#ifdef USE_ESP32
|
||||
class AsyncEventSourceResponse;
|
||||
using EventSession = AsyncEventSourceResponse;
|
||||
#elif defined(USE_ARDUINO)
|
||||
class DeferredUpdateEventSource;
|
||||
using EventSession = DeferredUpdateEventSource;
|
||||
#endif
|
||||
class WebServer;
|
||||
|
||||
class ListEntitiesIterator final : public ComponentIterator {
|
||||
public:
|
||||
#ifdef USE_ESP32
|
||||
ListEntitiesIterator(const WebServer *ws, esphome::web_server_idf::AsyncEventSource *es);
|
||||
#elif defined(USE_ARDUINO)
|
||||
ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es);
|
||||
#endif
|
||||
ListEntitiesIterator(const WebServer *ws, EventSession *session) : web_server_(ws), session_(session) {}
|
||||
|
||||
// Entity overrides (generated from entity_types.h).
|
||||
// Implementations live in list_entities.cpp.
|
||||
@@ -45,11 +40,7 @@ class ListEntitiesIterator final : public ComponentIterator {
|
||||
void send_all_(void *source, void (*generator)(WebServer *, void *, json::JsonBuilder &));
|
||||
|
||||
const WebServer *web_server_;
|
||||
#ifdef USE_ESP32
|
||||
esphome::web_server_idf::AsyncEventSource *events_;
|
||||
#elif USE_ARDUINO
|
||||
DeferredUpdateEventSource *events_;
|
||||
#endif
|
||||
EventSession *session_;
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
namespace esphome::web_server {
|
||||
|
||||
void for_each_chunk_piece(const char *message, size_t message_len, ChunkPieceSink sink, void *ctx) {
|
||||
if (message == nullptr) {
|
||||
@@ -63,4 +63,4 @@ void write_chunk_header(char *buf, size_t chunk_len) {
|
||||
buf[9] = '\n';
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server_idf
|
||||
} // namespace esphome::web_server
|
||||
+15
-2
@@ -3,7 +3,9 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
#include "esphome/components/web_server_base/stream_head.h"
|
||||
|
||||
namespace esphome::web_server {
|
||||
|
||||
// Wire framing of one Server-Sent-Events event inside the chunked /events response:
|
||||
// "%08x\r\n" + fields + "data: " line ["\r\ndata: " line]... "\r\n" + "\r\n" + "\r\n"
|
||||
@@ -22,6 +24,17 @@ constexpr size_t SSE_SEP_LEN = sizeof(SSE_SEP) - 1;
|
||||
constexpr char SSE_SUFFIX[] = "\r\n\r\n\r\n";
|
||||
constexpr size_t SSE_SUFFIX_LEN = sizeof(SSE_SUFFIX) - 1;
|
||||
|
||||
// Status, Content-Type and headers of the /events response head
|
||||
constexpr char SSE_STATUS[] = "200 OK";
|
||||
constexpr char SSE_CONTENT_TYPE[] = "text/event-stream";
|
||||
constexpr web_server_base::HttpHeader SSE_HEADERS[] = {
|
||||
{"Transfer-Encoding", "chunked"},
|
||||
{"Cache-Control", "no-cache"},
|
||||
{"Connection", "keep-alive"},
|
||||
};
|
||||
// Hand-framed first chunk holding an empty event (one blank line), as httpd_resp_send_chunk() sent it before
|
||||
constexpr char SSE_FIRST_CHUNK[] = "2\r\n\r\n\r\n";
|
||||
|
||||
// Receives one piece of the chunk; ctx is whatever the caller passed to for_each_chunk_piece()
|
||||
using ChunkPieceSink = void (*)(void *ctx, const char *piece, size_t len);
|
||||
|
||||
@@ -37,4 +50,4 @@ size_t build_chunk_prefix(char *buf, size_t size, const char *event, uint32_t id
|
||||
// Fills the header placeholder at the start of buf once the chunk length is known
|
||||
void write_chunk_header(char *buf, size_t chunk_len);
|
||||
|
||||
} // namespace esphome::web_server_idf
|
||||
} // namespace esphome::web_server
|
||||
@@ -273,7 +273,7 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
} else {
|
||||
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncEventSourceResponse::drain_tail_().
|
||||
// NOTE: Similar logic exists in web_server_idf/web_server_idf.cpp in AsyncStreamResponse::write().
|
||||
// The close mechanisms are platform-specific (this path calls close() directly; the IDF path is time-based and
|
||||
// closes through HTTPD to preserve session ownership), but both drop a client after roughly 20 seconds without
|
||||
// send progress. Keep that stall policy in sync when changing either side.
|
||||
|
||||
@@ -5,7 +5,11 @@
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/web_server_base/web_server_base.h"
|
||||
#include "esphome/components/web_server_base/captive_dns.h"
|
||||
#ifdef USE_WEBSERVER
|
||||
// The host only builds sse_chunk.cpp for unit tests, but other tests can define USE_WEBSERVER there
|
||||
#if defined(USE_WEBSERVER) && (defined(USE_ESP32) || defined(USE_ARDUINO))
|
||||
#ifdef USE_ESP32
|
||||
#include "event_source.h"
|
||||
#endif
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/entity_includes.h"
|
||||
|
||||
@@ -105,9 +105,9 @@ async def to_code(config: ConfigType) -> None:
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
cg.add(cg.RawExpression(f"{web_server_base_ns}::global_web_server_base = {var}"))
|
||||
|
||||
# Default headers added in init(): web_server_idf's StaticVector, or the library's DefaultHeaders
|
||||
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
|
||||
if CORE.is_esp32:
|
||||
# Count for StaticVector in web_server_idf - matches headers added in init()
|
||||
cg.add_define("WEB_SERVER_DEFAULT_HEADERS_COUNT", 1)
|
||||
return
|
||||
|
||||
# ESP32 uses IDF web server (early return above), so this is for other Arduino platforms
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <span>
|
||||
|
||||
#include <strings.h>
|
||||
|
||||
// Pure helpers of AsyncStreamResponse, kept free of httpd and lwIP so they build on the host.
|
||||
// The iovec type is a template parameter: lwIP defines its own on the device, POSIX on the host.
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
|
||||
struct HttpHeader {
|
||||
const char *name;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
/// Drops the first sent bytes from a gather list after a write; a short write resumes inside the
|
||||
/// element it stopped in. count reaches 0 once everything went out.
|
||||
template<typename Iovec> void advance_iovecs(Iovec *&iov, int &count, size_t sent) {
|
||||
while (count > 0 && sent >= iov->iov_len) {
|
||||
sent -= iov->iov_len;
|
||||
++iov;
|
||||
--count;
|
||||
}
|
||||
if (count > 0) {
|
||||
iov->iov_base = static_cast<char *>(iov->iov_base) + sent;
|
||||
iov->iov_len -= sent;
|
||||
}
|
||||
}
|
||||
|
||||
/// Pieces in a head: 4 for the status line and Content-Type, 4 per header, the blank line and the body
|
||||
constexpr size_t stream_head_iov_count(size_t header_count) { return 4 + 4 * header_count + 2; }
|
||||
|
||||
/// Pieces per batch: a head with up to four headers goes out in one write; 192 bytes on 32-bit
|
||||
static constexpr int STREAM_HEAD_MAX_IOV = 24;
|
||||
|
||||
/// True if headers holds a header named name, compared case insensitively
|
||||
inline bool has_header(std::span<const HttpHeader> headers, const char *name) {
|
||||
for (const auto &header : headers) {
|
||||
if (strcasecmp(header.name, name) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Gathers the head of a stream response: "HTTP/1.1 <status>", Content-Type, headers, the default_headers that
|
||||
/// headers does not replace, the blank line and body (may be null). send(Iovec *iov, int count) gets each full batch of
|
||||
/// STREAM_HEAD_MAX_IOV pieces and then the rest; once it returns false the later pieces are skipped. Returns false if
|
||||
/// any send failed.
|
||||
template<typename Iovec, typename Send>
|
||||
bool write_stream_head(Send &&send, const char *status, const char *content_type, std::span<const HttpHeader> headers,
|
||||
std::span<const HttpHeader> default_headers, const char *body) {
|
||||
struct Batch {
|
||||
Send &send;
|
||||
int count{0};
|
||||
bool ok{true};
|
||||
Iovec iov[STREAM_HEAD_MAX_IOV]; // left uninitialized on purpose
|
||||
|
||||
void flush() {
|
||||
if (this->ok && this->count > 0) {
|
||||
this->ok = this->send(this->iov, this->count);
|
||||
}
|
||||
this->count = 0;
|
||||
}
|
||||
void add(const char *buf, size_t len) {
|
||||
if (this->count == STREAM_HEAD_MAX_IOV) {
|
||||
this->flush();
|
||||
}
|
||||
this->iov[this->count++] = {const_cast<char *>(buf), len};
|
||||
}
|
||||
void add(const char *str) { this->add(str, strlen(str)); }
|
||||
void add_header(const HttpHeader &header) {
|
||||
this->add("\r\n", 2);
|
||||
this->add(header.name);
|
||||
this->add(": ", 2);
|
||||
this->add(header.value);
|
||||
}
|
||||
} batch{send};
|
||||
|
||||
batch.add("HTTP/1.1 ", 9);
|
||||
batch.add(status);
|
||||
batch.add("\r\nContent-Type: ", 16);
|
||||
batch.add(content_type);
|
||||
for (const auto &header : headers) {
|
||||
batch.add_header(header);
|
||||
}
|
||||
for (const auto &header : default_headers) {
|
||||
if (!has_header(headers, header.name)) {
|
||||
batch.add_header(header);
|
||||
}
|
||||
}
|
||||
batch.add("\r\n\r\n", 4);
|
||||
if (body != nullptr) {
|
||||
batch.add(body);
|
||||
}
|
||||
batch.flush();
|
||||
return batch.ok;
|
||||
}
|
||||
|
||||
/// How long sends have made no progress. Zero means not running, so a start at millis() == 0 records 1.
|
||||
/// The comparison is wraparound safe across a millis() rollover.
|
||||
class StallClock {
|
||||
public:
|
||||
/// Starts the clock; a running clock keeps its start time.
|
||||
void start(uint32_t now) {
|
||||
if (this->started_ms_ == 0) {
|
||||
this->started_ms_ = now != 0 ? now : 1;
|
||||
}
|
||||
}
|
||||
void clear() { this->started_ms_ = 0; }
|
||||
bool is_running() const { return this->started_ms_ != 0; }
|
||||
/// True once the clock has run for timeout_ms
|
||||
bool is_expired(uint32_t now, uint32_t timeout_ms) const {
|
||||
return this->started_ms_ != 0 && static_cast<int32_t>(now - (this->started_ms_ + timeout_ms)) >= 0;
|
||||
}
|
||||
uint32_t elapsed(uint32_t now) const { return now - this->started_ms_; }
|
||||
|
||||
protected:
|
||||
uint32_t started_ms_{0};
|
||||
};
|
||||
|
||||
} // namespace esphome::web_server_base
|
||||
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
// The long lived stream response the /events layer writes to, for the platform's web server
|
||||
#ifdef USE_ESP32
|
||||
#include "esphome/components/web_server_idf/web_server_idf.h"
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
namespace esphome::web_server_base {
|
||||
using web_server_idf::AsyncStreamResponse;
|
||||
// One entry of the gather list AsyncStreamResponse::write() takes
|
||||
using StreamIovec = ::iovec;
|
||||
} // namespace esphome::web_server_base
|
||||
#endif
|
||||
@@ -17,7 +17,6 @@
|
||||
#include <freertos/task.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "sse_chunk.h"
|
||||
#include "web_server_idf.h"
|
||||
|
||||
#ifdef USE_WEBSERVER_AUTH_DIGEST
|
||||
@@ -30,17 +29,15 @@
|
||||
#include "multipart.h" // For parse_multipart_boundary and other utils
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
#include "esphome/components/web_server/web_server.h"
|
||||
#include "esphome/components/web_server/list_entities.h"
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
// Include socket headers after Arduino headers to avoid IPADDR_NONE/INADDR_NONE macro conflicts
|
||||
#include <cerrno>
|
||||
#include <sys/socket.h>
|
||||
|
||||
namespace esphome::web_server_idf {
|
||||
|
||||
using web_server_base::advance_iovecs;
|
||||
using web_server_base::write_stream_head;
|
||||
|
||||
// Status strings not provided by esp_http_server.h
|
||||
#ifndef HTTPD_401
|
||||
#define HTTPD_401 "401 Unauthorized"
|
||||
@@ -55,13 +52,10 @@ namespace esphome::web_server_idf {
|
||||
#define HTTPD_422 "422 Unprocessable Entity"
|
||||
#endif
|
||||
|
||||
#define CRLF_STR "\r\n"
|
||||
#define CRLF_LEN (sizeof(CRLF_STR) - 1)
|
||||
|
||||
ESPHOME_LOG_TAG(TAG, "web_server_idf");
|
||||
|
||||
// Only prepare_json_() may hold the JSON arena: every other frame in this file is capped below one
|
||||
// arena. Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
|
||||
// Every frame in this file is capped below one JSON arena.
|
||||
// Measured at -Os on GCC 14; newer toolchains stay checked on purpose, older ones skip it.
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic error "-Wstack-usage=2048"
|
||||
#endif
|
||||
@@ -140,6 +134,8 @@ void AsyncWebServer::safe_close_with_shutdown(httpd_handle_t hd, int sockfd) {
|
||||
//
|
||||
// See: https://github.com/esphome/esphome-webserver/issues/163
|
||||
|
||||
AsyncStreamResponse::socket_closing(hd, sockfd);
|
||||
|
||||
// Attempt shutdown - ignore errors as socket may already be closed
|
||||
shutdown(sockfd, SHUT_RD);
|
||||
|
||||
@@ -736,224 +732,84 @@ void AsyncResponseStream::printf(const char *fmt, ...) {
|
||||
this->print(str);
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
AsyncEventSource::~AsyncEventSource() {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
for (auto *vec : {&this->sessions_, &this->pending_sessions_}) {
|
||||
for (auto *ses : *vec) {
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) {
|
||||
// Httpd task: set up the live httpd_req_t and park the session; main loop does the rest.
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory,clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
auto *rsp = new AsyncEventSourceResponse(request, this, this->web_server_);
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
}
|
||||
this->web_server_->enable_loop_soon_any_context();
|
||||
}
|
||||
|
||||
// clang-analyzer traces a false-positive leak path from loop() through
|
||||
// adopt_pending_sessions_main_loop_() into start_session_main_loop_() and
|
||||
// finally ArduinoJson. Suppress along the entire in-our-code call chain.
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
bool AsyncEventSource::loop() {
|
||||
// Fast path: one atomic load per tick. Slow path is out-of-line on connect.
|
||||
if (this->has_pending_sessions_.load(std::memory_order_acquire)) {
|
||||
this->adopt_pending_sessions_main_loop_();
|
||||
}
|
||||
|
||||
// Clean up dead sessions safely
|
||||
// This follows the ESP-IDF pattern where free_ctx marks resources as dead
|
||||
// and the main loop handles the actual cleanup to avoid race conditions
|
||||
for (size_t i = 0; i < this->sessions_.size();) {
|
||||
auto *ses = this->sessions_[i];
|
||||
// If the session has a dead socket (marked by destroy callback)
|
||||
if (ses->safe_to_delete_()) {
|
||||
// destroy() already logged the close with the fd; don't double-log here.
|
||||
delete ses; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
// Remove by swapping with last element (O(1) removal, order doesn't matter for sessions)
|
||||
this->sessions_[i] = this->sessions_.back();
|
||||
this->sessions_.pop_back();
|
||||
} else {
|
||||
ses->loop();
|
||||
++i;
|
||||
}
|
||||
}
|
||||
// A session still waiting for httpd to commit its context keeps the loop alive too
|
||||
return !this->sessions_.empty() || this->has_pending_sessions_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
void AsyncEventSource::adopt_pending_sessions_main_loop_() {
|
||||
std::vector<AsyncEventSourceResponse *> incoming;
|
||||
{
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
incoming.swap(this->pending_sessions_);
|
||||
this->has_pending_sessions_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
for (auto *rsp : incoming) {
|
||||
// Already disconnected? Drop it; skip session start on a dead session.
|
||||
if (rsp->safe_to_delete_()) {
|
||||
delete rsp; // NOLINT(cppcoreguidelines-owning-memory)
|
||||
continue;
|
||||
}
|
||||
// httpd commits the session context only after the creating handler returns, so stay
|
||||
// pending until then; httpd_req_cleanup() always commits it or calls destroy()
|
||||
if (httpd_sess_get_ctx(rsp->hd_, rsp->fd_.load()) != rsp) {
|
||||
LockGuard guard{this->pending_mutex_};
|
||||
this->pending_sessions_.push_back(rsp);
|
||||
this->has_pending_sessions_.store(true, std::memory_order_release);
|
||||
continue;
|
||||
}
|
||||
this->sessions_.push_back(rsp);
|
||||
rsp->start_session_main_loop_();
|
||||
}
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
void AsyncEventSource::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->fd_.load() != 0) { // Skip dead sessions
|
||||
ses->try_send_nodefer(message, message_len, event, id, reconnect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSource::deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator) {
|
||||
// Skip if no connected clients to avoid unnecessary processing
|
||||
if (this->empty())
|
||||
return;
|
||||
for (auto *ses : this->sessions_) {
|
||||
if (ses->fd_.load() != 0) { // Skip dead sessions
|
||||
ses->deferrable_send_state(source, detail_all, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *request,
|
||||
esphome::web_server_idf::AsyncEventSource *server,
|
||||
esphome::web_server::WebServer *ws)
|
||||
: server_(server), web_server_(ws), entities_iterator_(ws, server) {
|
||||
// Httpd task only. start_session_main_loop_() sends the greeting and starts the iterator.
|
||||
void AsyncStreamResponse::begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, const char *body) {
|
||||
httpd_req_t *req = *request;
|
||||
|
||||
httpd_resp_set_status(req, HTTPD_200);
|
||||
httpd_resp_set_type(req, "text/event-stream");
|
||||
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
|
||||
httpd_resp_set_hdr(req, "Connection", "keep-alive");
|
||||
|
||||
DefaultHeaders::Instance().apply_(req);
|
||||
|
||||
httpd_resp_send_chunk(req, CRLF_STR, CRLF_LEN);
|
||||
// Same head httpd_resp_send_chunk() writes, without its first chunk. Blocking sends: the session's non-blocking
|
||||
// send override is not installed yet.
|
||||
const int fd = httpd_req_to_sockfd(req);
|
||||
const auto &defaults = DefaultHeaders::Instance().headers_;
|
||||
const bool head_sent = write_stream_head<struct iovec>(
|
||||
[fd](struct iovec *iov, int count) {
|
||||
while (count > 0) {
|
||||
struct msghdr msg{};
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = count;
|
||||
const ssize_t sent = sendmsg(fd, &msg, 0);
|
||||
if (sent <= 0) {
|
||||
return false;
|
||||
}
|
||||
advance_iovecs(iov, count, sent);
|
||||
}
|
||||
return true;
|
||||
},
|
||||
status, content_type, headers, std::span<const HttpHeader>(defaults.data(), defaults.size()), body);
|
||||
|
||||
req->sess_ctx = this;
|
||||
req->free_ctx = AsyncEventSourceResponse::destroy;
|
||||
req->free_ctx = AsyncStreamResponse::destroy;
|
||||
|
||||
this->hd_ = req->handle;
|
||||
this->fd_.store(httpd_req_to_sockfd(req));
|
||||
this->fd_.store(fd);
|
||||
|
||||
// Use non-blocking send to prevent watchdog timeouts when TCP buffers are full
|
||||
httpd_sess_set_send_override(this->hd_, this->fd_.load(), nonblocking_send);
|
||||
httpd_sess_set_send_override(this->hd_, fd, nonblocking_send);
|
||||
|
||||
if (!head_sent) {
|
||||
// A partly sent head must not be continued by later writes. close() is main loop only, so shut the socket
|
||||
// down here; the first write() then fails and closes the stream, and httpd frees the session as usual.
|
||||
shutdown(fd, SHUT_RDWR);
|
||||
}
|
||||
}
|
||||
|
||||
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
|
||||
void AsyncEventSourceResponse::start_session_main_loop_() {
|
||||
auto *ws = this->web_server_;
|
||||
// Held around a stream's ownership check and send, and while its socket is marked closed.
|
||||
// Created at boot, so no allocation can fail once requests arrive.
|
||||
static Mutex stream_send_mutex; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
// The tcp send buffer is empty on connect. A refusal is a closing session or a failed tail
|
||||
// allocation; nothing retries the greeting, so close and let the client reconnect.
|
||||
auto message = ws->get_config_json();
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "ping", millis(), 30000)) {
|
||||
ESP_LOGW(TAG, "Config not sent to fd %d", this->fd_.load());
|
||||
this->request_close_();
|
||||
void AsyncStreamResponse::socket_closing(httpd_handle_t hd, int fd) {
|
||||
// Only streams set a session context
|
||||
auto *rsp = static_cast<AsyncStreamResponse *>(httpd_sess_get_ctx(hd, fd));
|
||||
if (rsp == nullptr)
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef USE_WEBSERVER_SORTING
|
||||
for (auto &group : ws->sorting_groups_) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
root["name"] = json::linked(group.second.name);
|
||||
root["sorting_weight"] = group.second.weight;
|
||||
message = builder.serialize();
|
||||
|
||||
// a (very) large number of these should be able to be queued initially without defer
|
||||
// since the only thing in the send buffer at this point is the initial ping/config.
|
||||
// A refusal means the socket is full or closing; the remaining groups are not sent. The
|
||||
// session stays up on purpose: partial grouping beats a reconnect loop on a slow link.
|
||||
if (!this->try_send_nodefer(message.c_str(), message.size(), "sorting_group")) {
|
||||
ESP_LOGW(TAG, "Sorting groups not sent to fd %d", this->fd_.load());
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
this->entities_iterator_.begin(ws->include_internal_);
|
||||
LockGuard guard{stream_send_mutex};
|
||||
rsp->socket_closed_ = true;
|
||||
}
|
||||
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
|
||||
|
||||
void AsyncEventSourceResponse::destroy(void *ptr) {
|
||||
auto *rsp = static_cast<AsyncEventSourceResponse *>(ptr);
|
||||
void AsyncStreamResponse::destroy(void *ptr) {
|
||||
auto *rsp = static_cast<AsyncStreamResponse *>(ptr);
|
||||
int fd = rsp->fd_.exchange(0); // Atomically get and clear fd
|
||||
ESP_LOGD(TAG, "Event source connection closed (fd: %d)", fd);
|
||||
// Mark as dead - will be cleaned up in the main loop
|
||||
// Note: We don't delete or remove from set here to avoid race conditions
|
||||
ESP_LOGD(TAG, "Stream connection closed (fd: %d)", fd);
|
||||
// Mark as dead - the owner deletes it from the main loop once safe_to_delete().
|
||||
// httpd will call our custom close_fn (safe_close_with_shutdown) which handles
|
||||
// shutdown() before close() to prevent lwIP race conditions
|
||||
}
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) {
|
||||
DeferredEvent item(source, message_generator);
|
||||
|
||||
// Use range-based for loop instead of std::find_if to reduce template instantiation overhead and binary size
|
||||
for (auto &event : this->deferred_queue_) {
|
||||
if (event == item) {
|
||||
return; // Already in queue, no need to update since items are equal
|
||||
}
|
||||
}
|
||||
this->deferred_queue_.push_back(item);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::process_deferred_queue_() {
|
||||
if (this->close_requested_) {
|
||||
return;
|
||||
}
|
||||
while (!deferred_queue_.empty()) {
|
||||
DeferredEvent &de = deferred_queue_.front();
|
||||
if (this->send_json_(de.source_, de.message_generator_)) {
|
||||
if (this->close_requested_ || deferred_queue_.empty()) {
|
||||
return;
|
||||
}
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::request_close_() {
|
||||
void AsyncStreamResponse::close() {
|
||||
if (!this->close_requested_) {
|
||||
this->close_requested_ = true;
|
||||
this->deferred_queue_.clear();
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->tail_len_ = 0;
|
||||
this->tail_sent_ = 0;
|
||||
this->next_close_attempt_ms_ = App.get_loop_component_start_time();
|
||||
}
|
||||
|
||||
this->process_close_();
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::process_close_() {
|
||||
void AsyncStreamResponse::loop() {
|
||||
if (this->close_requested_) {
|
||||
this->process_close_();
|
||||
} else {
|
||||
this->refresh_lru_();
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncStreamResponse::process_close_() {
|
||||
if (!this->close_requested_ || this->close_work_queued_.load(std::memory_order_acquire)) {
|
||||
return;
|
||||
}
|
||||
@@ -971,7 +827,7 @@ void AsyncEventSourceResponse::process_close_() {
|
||||
// httpd_sess_trigger_close() queues only a reusable fd/session slot and can
|
||||
// therefore close a new client if the original peer disconnects meanwhile.
|
||||
this->close_work_queued_.store(true, std::memory_order_release);
|
||||
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncEventSourceResponse::close_session_work, this);
|
||||
const esp_err_t err = httpd_queue_work(this->hd_, &AsyncStreamResponse::close_session_work, this);
|
||||
this->next_close_attempt_ms_ = now + (err == ESP_OK ? CLOSE_CONFIRM_INTERVAL_MS : CLOSE_RETRY_INTERVAL_MS);
|
||||
if (err == ESP_OK) {
|
||||
return;
|
||||
@@ -979,17 +835,17 @@ void AsyncEventSourceResponse::process_close_() {
|
||||
|
||||
this->close_work_queued_.store(false, std::memory_order_release);
|
||||
if (!this->close_retry_warning_logged_) {
|
||||
ESP_LOGW(TAG, "Failed to queue EventSource close (%s); retrying", esp_err_to_name(err));
|
||||
ESP_LOGW(TAG, "Failed to queue stream close (%s); retrying", esp_err_to_name(err));
|
||||
this->close_retry_warning_logged_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::refresh_lru_() {
|
||||
// httpd only advances a session's LRU counter when a request arrives, and an event stream receives one
|
||||
void AsyncStreamResponse::refresh_lru_() {
|
||||
// httpd only advances a session's LRU counter when a request arrives, and a stream receives one
|
||||
// request ever, so the purge that frees a socket for a new client would evict the live streams first.
|
||||
// The HTTPD task owns the session table, so the refresh runs there.
|
||||
// A stalled stream is the socket the purge should take, and a zeroed fd has no session left to refresh.
|
||||
if (this->send_failure_started_ms_ != 0 || this->fd_.load() == 0)
|
||||
if (this->stall_.is_running() || this->fd_.load() == 0)
|
||||
return;
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (now - this->last_lru_refresh_ms_ < LRU_REFRESH_INTERVAL_MS ||
|
||||
@@ -998,13 +854,13 @@ void AsyncEventSourceResponse::refresh_lru_() {
|
||||
}
|
||||
this->last_lru_refresh_ms_ = now; // a full queue also waits for the next interval
|
||||
this->lru_work_queued_.store(true, std::memory_order_release);
|
||||
if (httpd_queue_work(this->hd_, &AsyncEventSourceResponse::refresh_lru_work, this) != ESP_OK) {
|
||||
if (httpd_queue_work(this->hd_, &AsyncStreamResponse::refresh_lru_work, this) != ESP_OK) {
|
||||
this->lru_work_queued_.store(false, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
|
||||
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
|
||||
void AsyncStreamResponse::refresh_lru_work(void *arg) {
|
||||
auto *response = static_cast<AsyncStreamResponse *>(arg);
|
||||
const int fd = response->fd_.load();
|
||||
if (response->owns_session_(fd)) {
|
||||
httpd_sess_update_lru_counter(response->hd_, fd);
|
||||
@@ -1013,8 +869,8 @@ void AsyncEventSourceResponse::refresh_lru_work(void *arg) {
|
||||
response->lru_work_queued_.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::close_session_work(void *arg) {
|
||||
auto *response = static_cast<AsyncEventSourceResponse *>(arg);
|
||||
void AsyncStreamResponse::close_session_work(void *arg) {
|
||||
auto *response = static_cast<AsyncStreamResponse *>(arg);
|
||||
const int fd = response->fd_.load();
|
||||
if (response->owns_session_(fd)) {
|
||||
// The HTTPD task remains the session owner. Shutting the socket down makes
|
||||
@@ -1026,320 +882,61 @@ void AsyncEventSourceResponse::close_session_work(void *arg) {
|
||||
response->close_work_queued_.store(false, std::memory_order_release);
|
||||
}
|
||||
|
||||
ssize_t AsyncEventSourceResponse::send_(struct iovec *iov, int iovcnt) {
|
||||
// httpd frees a session before closing its socket, so the fd may already be a new client's.
|
||||
ssize_t AsyncStreamResponse::write(struct iovec *iov, int iovcnt) {
|
||||
if (this->close_requested_) {
|
||||
return -1;
|
||||
}
|
||||
// Nothing to send is not a stall
|
||||
size_t total = 0;
|
||||
for (int i = 0; i < iovcnt; i++) {
|
||||
total += iov[i].iov_len;
|
||||
}
|
||||
if (total == 0) {
|
||||
return 0;
|
||||
}
|
||||
// httpd closes the socket before it clears the session, so the fd number may already be a new client's.
|
||||
// Treated as would-block; the stall timer ends a session that never becomes ours again.
|
||||
const int fd = this->fd_.load();
|
||||
if (!this->owns_session_(fd)) {
|
||||
return 0;
|
||||
ssize_t sent = 0;
|
||||
int err = 0;
|
||||
{
|
||||
// Held across the check and the send, so socket_closing() cannot close the socket in between
|
||||
LockGuard guard{stream_send_mutex};
|
||||
if (!this->socket_closed_ && this->owns_session_(fd)) {
|
||||
struct msghdr msg{};
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = iovcnt;
|
||||
sent = sendmsg(fd, &msg, MSG_DONTWAIT);
|
||||
err = errno;
|
||||
}
|
||||
}
|
||||
struct msghdr msg{};
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = iovcnt;
|
||||
const ssize_t sent = sendmsg(fd, &msg, MSG_DONTWAIT);
|
||||
if (sent >= 0) {
|
||||
if (sent > 0) {
|
||||
this->stall_.clear();
|
||||
return sent;
|
||||
}
|
||||
const int err = errno;
|
||||
if (err == EAGAIN || err == EWOULDBLOCK) {
|
||||
return 0;
|
||||
if (sent < 0 && err != EAGAIN && err != EWOULDBLOCK) {
|
||||
ESP_LOGD(TAG, "send error: errno %d", err);
|
||||
// A hard error is not a timeout, so is_stalled() must not report one
|
||||
this->stall_.clear();
|
||||
this->close();
|
||||
return -1;
|
||||
}
|
||||
ESP_LOGD(TAG, "send error: errno %d", err);
|
||||
this->request_close_();
|
||||
return -1;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::drain_tail_() {
|
||||
if (this->sending_ || this->close_requested_ || this->tail_len_ == 0) {
|
||||
return;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
const size_t remaining = this->tail_len_ - this->tail_sent_;
|
||||
struct iovec iov = {this->tail_.get() + this->tail_sent_, remaining};
|
||||
const ssize_t sent = this->send_(&iov, 1);
|
||||
if (sent < 0) {
|
||||
return;
|
||||
}
|
||||
if (sent == 0) {
|
||||
// Socket buffer full, try again later
|
||||
// NOTE: Similar logic exists in web_server/web_server.cpp in DeferredUpdateEventSource::process_deferred_queue_().
|
||||
// The IDF path is intentionally time-based and closes through HTTPD to preserve session ownership.
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (this->send_failure_started_ms_ == 0) {
|
||||
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
|
||||
}
|
||||
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
|
||||
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu32 " ms without send progress",
|
||||
now - this->send_failure_started_ms_);
|
||||
this->request_close_();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
this->send_failure_started_ms_ = 0;
|
||||
this->tail_sent_ += sent;
|
||||
if (this->tail_sent_ < this->tail_len_) {
|
||||
ESP_LOGV(TAG, "Partial send: %zd/%zu bytes (total: %u/%u)", sent, remaining, this->tail_sent_, this->tail_len_);
|
||||
return;
|
||||
}
|
||||
// Fully sent; the storage stays for the next stall
|
||||
this->tail_len_ = 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::reserve_tail_(size_t len) {
|
||||
if (this->tail_cap_ >= len) {
|
||||
return true;
|
||||
}
|
||||
if (len > TAIL_MAX_SIZE) {
|
||||
return false;
|
||||
}
|
||||
// Nothing is pending while the tail grows, so free the old block first. PREFER_INTERNAL keeps
|
||||
// the tail where plain new put it.
|
||||
this->tail_.reset();
|
||||
this->tail_ = RAMAllocator<uint8_t>(RAMAllocator<uint8_t>::PREFER_INTERNAL).make_unique_array_for_overwrite(len);
|
||||
this->tail_cap_ = this->tail_ ? len : 0;
|
||||
return this->tail_cap_ != 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::stash_chunk_(const char *prefix, size_t prefix_len, const char *message,
|
||||
size_t message_len, size_t total, size_t sent) {
|
||||
// A log event of nothing but line breaks is the largest chunk; reserve_tail_() must never
|
||||
// refuse it for size, only for memory
|
||||
static_assert(SSE_SEP_LEN * web_server::LOG_EVENT_MAX_LEN + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN <= TAIL_MAX_SIZE,
|
||||
"the log cut in web_server.h must keep a worst case log event inside the tail ceiling");
|
||||
if (!this->reserve_tail_(total)) {
|
||||
if (sent != 0) {
|
||||
// Part of the chunk is on the wire, so the stream is broken and the client has to go
|
||||
ESP_LOGW(TAG, "Cannot buffer a %zu byte chunk, closing", total);
|
||||
this->request_close_();
|
||||
} else {
|
||||
this->tail_alloc_failed_(total); // nothing on the wire, the caller retries on the stall clock
|
||||
}
|
||||
return false;
|
||||
}
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
dst += prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &out = *static_cast<uint8_t **>(ctx);
|
||||
std::memcpy(out, piece, len);
|
||||
out += len;
|
||||
},
|
||||
&dst);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = sent;
|
||||
return true;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::loop() {
|
||||
if (this->close_requested_) {
|
||||
this->process_close_();
|
||||
return;
|
||||
}
|
||||
this->refresh_lru_();
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
if (this->close_requested_)
|
||||
return;
|
||||
// One step per loop; refusals retry next pass
|
||||
this->entities_iterator_.try_advance(1);
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wstack-usage=" // the one frame that holds the JSON arena
|
||||
#endif
|
||||
AsyncEventSourceResponse::JsonPrepareResult AsyncEventSourceResponse::prepare_json_(void *source,
|
||||
message_generator_t *generator,
|
||||
char *buffer, size_t *size_out) {
|
||||
// Log lines emitted while the generator runs are dropped for this session: on the large path they would fill
|
||||
// tail_ before it is overwritten, and on the small path they would go out ahead of the event being built.
|
||||
SendGuard guard{*this};
|
||||
json::JsonArena<json::JSON_ARENA_SIZE> arena;
|
||||
json::JsonBuilder builder(&arena);
|
||||
generator(this->web_server_, source, builder);
|
||||
*size_out = builder.serialize_to(buffer, JSON_BUF_SIZE);
|
||||
if (*size_out < JSON_BUF_SIZE) {
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER;
|
||||
}
|
||||
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), "state", 0, 0, true);
|
||||
|
||||
// Nothing reaches the wire until the complete document and its framing are owned by the caller.
|
||||
size_t json_len = 0;
|
||||
size_t cap = std::max<size_t>(JSON_BUF_SIZE * 2, this->tail_cap_);
|
||||
for (;;) {
|
||||
if (!this->reserve_tail_(cap)) {
|
||||
*size_out = cap;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY;
|
||||
}
|
||||
const size_t room = cap - prefix_len - SSE_SUFFIX_LEN;
|
||||
json_len = builder.serialize_to(reinterpret_cast<char *>(this->tail_.get()) + prefix_len, room);
|
||||
if (json_len < room) {
|
||||
break;
|
||||
}
|
||||
if (cap >= TAIL_MAX_SIZE) {
|
||||
*size_out = room;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE;
|
||||
}
|
||||
cap = std::min<size_t>(cap * 2, TAIL_MAX_SIZE);
|
||||
}
|
||||
|
||||
const size_t total = prefix_len + json_len + SSE_SUFFIX_LEN;
|
||||
write_chunk_header(prefix, total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
uint8_t *dst = this->tail_.get();
|
||||
std::memcpy(dst, prefix, prefix_len);
|
||||
std::memcpy(dst + prefix_len + json_len, SSE_SUFFIX, SSE_SUFFIX_LEN);
|
||||
this->tail_len_ = total;
|
||||
this->tail_sent_ = 0;
|
||||
return JsonPrepareResult::JSON_PREPARE_RESULT_TAIL;
|
||||
}
|
||||
#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ >= 14 && defined(__OPTIMIZE_SIZE__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
bool AsyncEventSourceResponse::send_json_(void *source, message_generator_t *generator) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
char buffer[JSON_BUF_SIZE];
|
||||
size_t size;
|
||||
const auto result = this->prepare_json_(source, generator, buffer, &size);
|
||||
switch (result) {
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_BUFFER:
|
||||
return this->try_send_nodefer(buffer, size, "state");
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TAIL:
|
||||
this->drain_tail_();
|
||||
return true;
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_NO_MEMORY: {
|
||||
SendGuard guard{*this};
|
||||
this->tail_alloc_failed_(size);
|
||||
return false;
|
||||
}
|
||||
case JsonPrepareResult::JSON_PREPARE_RESULT_TOO_LARGE: {
|
||||
SendGuard guard{*this};
|
||||
ESP_LOGW(TAG, "State event does not fit %zu bytes, dropped", size);
|
||||
this->tail_.reset();
|
||||
this->tail_cap_ = 0;
|
||||
this->send_failure_started_ms_ = 0;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::tail_alloc_failed_(size_t cap) {
|
||||
// Same stall clock as a socket that stops draining, so a session cannot retry forever
|
||||
// Socket buffer full, try again later; a socket that stays full past the stall timeout is closed
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (this->send_failure_started_ms_ == 0) {
|
||||
this->send_failure_started_ms_ = now != 0 ? now : 1; // Reserve zero for no stall.
|
||||
ESP_LOGW(TAG, "No memory for a %zu byte chunk", cap);
|
||||
return;
|
||||
}
|
||||
if (static_cast<int32_t>(now - (this->send_failure_started_ms_ + SEND_STALL_TIMEOUT_MS)) >= 0) {
|
||||
ESP_LOGW(TAG, "Closing EventSource after %" PRIu32 " ms without memory", now - this->send_failure_started_ms_);
|
||||
this->request_close_();
|
||||
this->stall_.start(now);
|
||||
if (this->stall_.is_expired(now, SEND_STALL_TIMEOUT_MS)) {
|
||||
ESP_LOGW(TAG, "Closing stuck stream after %" PRIu32 " ms without send progress", this->stall_.elapsed(now));
|
||||
this->close();
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::ready_to_send_() {
|
||||
if (this->sending_ || this->fd_.load() == 0 || this->close_requested_) {
|
||||
return false;
|
||||
}
|
||||
drain_tail_();
|
||||
return !this->close_requested_ && this->tail_len_ == 0;
|
||||
ssize_t AsyncStreamResponse::write(const void *data, size_t len) {
|
||||
struct iovec iov = {const_cast<void *>(data), len};
|
||||
return this->write(&iov, 1);
|
||||
}
|
||||
|
||||
bool AsyncEventSourceResponse::try_send_nodefer(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
uint32_t reconnect) {
|
||||
if (!this->ready_to_send_()) {
|
||||
return false;
|
||||
}
|
||||
SendGuard guard{*this};
|
||||
|
||||
// Everything after the prefix goes out straight from the caller's buffer
|
||||
char prefix[PREFIX_BUF_SIZE];
|
||||
const size_t prefix_len = build_chunk_prefix(prefix, sizeof(prefix), event, id, reconnect, message != nullptr);
|
||||
if (message == nullptr && prefix_len == CHUNK_HDR_LEN) {
|
||||
return true; // Match ESPAsyncWebServer: nothing to send
|
||||
}
|
||||
if (prefix_len >= PREFIX_BUF_SIZE - 1) {
|
||||
// The appenders truncate silently, which would put a malformed event on the wire
|
||||
ESP_LOGW(TAG, "Event name too long, dropped");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Gather list: the prefix, then the data lines and their separators from the message
|
||||
struct Gather {
|
||||
struct iovec iov[MAX_SEND_IOV]; // left uninitialized on purpose
|
||||
int iovcnt{1};
|
||||
size_t total{0};
|
||||
bool fits{true};
|
||||
} g;
|
||||
g.total = prefix_len;
|
||||
for_each_chunk_piece(
|
||||
message, message_len,
|
||||
[](void *ctx, const char *piece, size_t len) {
|
||||
auto &g = *static_cast<Gather *>(ctx);
|
||||
g.total += len;
|
||||
if (len == 0) {
|
||||
return;
|
||||
}
|
||||
if (g.iovcnt == MAX_SEND_IOV) {
|
||||
g.fits = false;
|
||||
return;
|
||||
}
|
||||
g.iov[g.iovcnt++] = {const_cast<char *>(piece), len};
|
||||
},
|
||||
&g);
|
||||
// The header and the terminator are not part of the chunk length
|
||||
write_chunk_header(prefix, g.total - CHUNK_HDR_LEN - CHUNK_END_LEN);
|
||||
g.iov[0] = {prefix, prefix_len};
|
||||
|
||||
// A message with more lines than the list holds skips straight to the tail
|
||||
const ssize_t sent = g.fits ? this->send_(g.iov, g.iovcnt) : 0;
|
||||
if (sent < 0) {
|
||||
return false;
|
||||
}
|
||||
if (static_cast<size_t>(sent) == g.total) {
|
||||
this->send_failure_started_ms_ = 0; // progress, whichever stall clock was running
|
||||
return true;
|
||||
}
|
||||
// The caller's buffers do not outlive this call, so keep the chunk and continue from loop()
|
||||
return this->stash_chunk_(prefix, prefix_len, message, message_len, g.total, sent);
|
||||
}
|
||||
|
||||
void AsyncEventSourceResponse::deferrable_send_state(void *source, bool detail_all,
|
||||
message_generator_t *message_generator) {
|
||||
// Let the detail_all listing finish before bare state events, so the web UI never shows unnamed entries and
|
||||
// initial connect sends less
|
||||
if (!detail_all && !this->entities_iterator_.completed())
|
||||
return;
|
||||
|
||||
drain_tail_();
|
||||
process_deferred_queue_();
|
||||
|
||||
if (this->close_requested_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->tail_len_ != 0 || !deferred_queue_.empty()) {
|
||||
// outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point
|
||||
// trying to send first
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
// A send error closes the session and clears the queue; nothing is queued after that
|
||||
if (!this->send_json_(source, message_generator) && !this->close_requested_) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_WEBSERVER_OTA
|
||||
esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) {
|
||||
// Parse boundary and create reader
|
||||
|
||||
@@ -15,21 +15,14 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/web_server/list_entities.h"
|
||||
#include "sse_chunk.h"
|
||||
#endif
|
||||
#include "esphome/components/web_server_base/stream_head.h"
|
||||
|
||||
struct iovec; // NOLINT(readability-identifier-naming) - forward decl of lwip's gather list entry
|
||||
|
||||
namespace esphome {
|
||||
#ifdef USE_WEBSERVER
|
||||
namespace web_server {
|
||||
class WebServer;
|
||||
}; // namespace web_server
|
||||
#endif
|
||||
namespace web_server_idf {
|
||||
namespace esphome::web_server_idf {
|
||||
|
||||
using web_server_base::HttpHeader;
|
||||
using web_server_base::StallClock;
|
||||
|
||||
class AsyncWebParameter {
|
||||
public:
|
||||
@@ -191,6 +184,7 @@ class AsyncWebServerRequest {
|
||||
void onDisconnect(std::function<void()> fn) { this->on_disconnect_ = std::move(fn); }
|
||||
#endif
|
||||
|
||||
/// sess_ctx is reserved for AsyncStreamResponse: the server's close_fn treats any session context as one.
|
||||
operator httpd_req_t *() const { return this->req_; }
|
||||
optional<std::string> get_header(const char *name) const;
|
||||
/// Copy a header value into buf without heap allocation.
|
||||
@@ -269,204 +263,85 @@ class AsyncWebHandler {
|
||||
virtual bool isRequestHandlerTrivial() const { return true; }
|
||||
};
|
||||
|
||||
#ifdef USE_WEBSERVER
|
||||
class AsyncEventSource;
|
||||
class AsyncEventSourceResponse;
|
||||
|
||||
using message_generator_t = void(esphome::web_server::WebServer *, void *, json::JsonBuilder &);
|
||||
|
||||
/*
|
||||
This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function
|
||||
that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for
|
||||
the same component are backed up, and take up only two pointers of memory. The entry in the deferred queue (a
|
||||
std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only two pointers
|
||||
per entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing
|
||||
because of dedup) would take up only 0.8 kB.
|
||||
*/
|
||||
struct DeferredEvent {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
protected:
|
||||
void *source_;
|
||||
message_generator_t *message_generator_;
|
||||
|
||||
/// A response that keeps its connection open and is written from the main loop, such as an event stream or
|
||||
/// an audio stream. Use it directly or derive from it. Call loop() and delete the object only through the type
|
||||
/// it was created as: subclasses hide loop() instead of overriding it, and the destructor is not virtual.
|
||||
///
|
||||
/// Lifecycle:
|
||||
/// 1. Httpd task, inside handleRequest(): create it and call begin(), then hand the pointer to the main loop.
|
||||
/// 2. Main loop: once is_attached() is true, write() may go through. Call loop() on every pass.
|
||||
/// 3. Main loop: delete it once safe_to_delete() is true. httpd owns the socket throughout.
|
||||
class AsyncStreamResponse {
|
||||
public:
|
||||
DeferredEvent(void *source, message_generator_t *message_generator)
|
||||
: source_(source), message_generator_(message_generator) {}
|
||||
bool operator==(const DeferredEvent &test) const {
|
||||
return (source_ == test.source_ && message_generator_ == test.message_generator_);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(DeferredEvent) == sizeof(void *) + sizeof(message_generator_t *),
|
||||
"DeferredEvent should have no padding");
|
||||
/// Httpd task, once. Sends the status line (for example HTTPD_200), Content-Type, headers, the default headers
|
||||
/// and body (a C string or null, sent as is), then makes this object the session context. A chunked response
|
||||
/// passes {"Transfer-Encoding", "chunked"} as its first header and frames each chunk itself.
|
||||
void begin(AsyncWebServerRequest *request, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, const char *body = nullptr);
|
||||
|
||||
class AsyncEventSourceResponse {
|
||||
friend class AsyncEventSource;
|
||||
|
||||
public:
|
||||
bool try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
// detail_all events go out while entities are still being listed; state events wait until listing is done
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
/// Main loop. Non-blocking gather write. Returns the bytes accepted, which may be fewer than offered: the caller
|
||||
/// keeps the rest and continues from there. 0 means the socket is full or not ours yet; -1 means the stream failed
|
||||
/// and is closing. Refused writes for SEND_STALL_TIMEOUT_MS in a row also close it.
|
||||
ssize_t write(struct iovec *iov, int iovcnt);
|
||||
ssize_t write(const void *data, size_t len);
|
||||
/// Main loop. Shuts the connection down through httpd; safe to call more than once.
|
||||
void close();
|
||||
/// Main loop, every pass. Keeps the session fresh in httpd's LRU and retries a pending close.
|
||||
void loop();
|
||||
/// Httpd task, from the server's close_fn: no write() reaches the socket after this returns.
|
||||
static void socket_closing(httpd_handle_t hd, int fd);
|
||||
|
||||
protected:
|
||||
AsyncEventSourceResponse(const AsyncWebServerRequest *request, esphome::web_server_idf::AsyncEventSource *server,
|
||||
esphome::web_server::WebServer *ws);
|
||||
|
||||
// Main-loop only: sends initial ping/config/sorting_groups, starts entity iterator.
|
||||
void start_session_main_loop_();
|
||||
|
||||
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
|
||||
void process_deferred_queue_();
|
||||
// A new chunk may go out: not re-entered from a log line, session alive, tail empty
|
||||
bool ready_to_send_();
|
||||
// Non-blocking gather write. Returns bytes written, 0 on would-block or a socket that is not
|
||||
// ours, -1 after requesting the close on any other error.
|
||||
ssize_t send_(struct iovec *iov, int iovcnt);
|
||||
// Push what is left of the chunk in tail_ to the socket; owns the stall timer.
|
||||
void drain_tail_();
|
||||
// Grow tail_ to hold len bytes, kept at its high-water mark. False on OOM.
|
||||
bool reserve_tail_(size_t len);
|
||||
// Keep the whole chunk in tail_ and continue from sent; false when the tail cannot be allocated.
|
||||
bool stash_chunk_(const char *prefix, size_t prefix_len, const char *message, size_t message_len, size_t total,
|
||||
size_t sent);
|
||||
// Send a state event; JSON too large for the stack buffer is serialized into tail_ instead
|
||||
bool send_json_(void *source, message_generator_t *generator);
|
||||
enum class JsonPrepareResult : uint8_t {
|
||||
JSON_PREPARE_RESULT_BUFFER,
|
||||
JSON_PREPARE_RESULT_TAIL,
|
||||
JSON_PREPARE_RESULT_NO_MEMORY,
|
||||
JSON_PREPARE_RESULT_TOO_LARGE,
|
||||
};
|
||||
// Keep the arena out of the caller's frame so it is gone before the state send.
|
||||
// size_out: BUFFER = JSON length, NO_MEMORY = requested tail capacity, TOO_LARGE = available JSON room;
|
||||
// unused for TAIL.
|
||||
JsonPrepareResult __attribute__((noinline)) prepare_json_(void *source, message_generator_t *generator, char *buffer,
|
||||
size_t *size_out);
|
||||
// Warn once, and close the session once the stall timeout passes with no memory for the tail
|
||||
void tail_alloc_failed_(size_t cap);
|
||||
void request_close_();
|
||||
|
||||
// A log line emitted inside a send re-enters try_send_nodefer on this session; refuse it
|
||||
struct SendGuard {
|
||||
AsyncEventSourceResponse &owner;
|
||||
explicit SendGuard(AsyncEventSourceResponse &owner) : owner(owner) { owner.sending_ = true; }
|
||||
~SendGuard() { this->owner.sending_ = false; }
|
||||
};
|
||||
void process_close_();
|
||||
static void close_session_work(void *arg);
|
||||
void refresh_lru_();
|
||||
static void refresh_lru_work(void *arg);
|
||||
// httpd frees a session before closing its socket, so the fd may already belong to a new client
|
||||
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
|
||||
|
||||
// Deletable only after destroy() zeroed fd_ and no queued HTTPD work still references this object.
|
||||
bool safe_to_delete_() const {
|
||||
/// httpd holds this response as the session context; begin() has sent the whole head by then.
|
||||
bool is_attached() const { return this->owns_session_(this->fd_.load()); }
|
||||
/// The client is still connected; httpd has not freed the session.
|
||||
bool is_connected() const { return this->fd_.load() != 0; }
|
||||
bool is_closing() const { return this->close_requested_; }
|
||||
/// The socket fd, for log lines only
|
||||
int log_id() const { return this->fd_.load(); }
|
||||
/// Sends are making no progress; after write() returns -1 this tells a send timeout from a send error.
|
||||
bool is_stalled() const { return this->stall_.is_running(); }
|
||||
/// The session is gone and no queued httpd work still references this object.
|
||||
bool safe_to_delete() const {
|
||||
return this->fd_.load() == 0 && !this->close_work_queued_.load(std::memory_order_acquire) &&
|
||||
!this->lru_work_queued_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
|
||||
|
||||
protected:
|
||||
void process_close_();
|
||||
static void close_session_work(void *arg);
|
||||
void refresh_lru_();
|
||||
static void refresh_lru_work(void *arg);
|
||||
// httpd closes the socket before it clears the session, so this can still match a closed fd; write() also checks
|
||||
// socket_closed_
|
||||
bool owns_session_(int fd) const { return fd != 0 && httpd_sess_get_ctx(this->hd_, fd) == this; }
|
||||
static void destroy(void *p);
|
||||
AsyncEventSource *server_;
|
||||
|
||||
httpd_handle_t hd_{};
|
||||
std::atomic<int> fd_{};
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
esphome::web_server::WebServer *web_server_;
|
||||
esphome::web_server::ListEntitiesIterator entities_iterator_;
|
||||
// One chunk the socket did not take whole, allocated on the first stall; the only heap use
|
||||
// on the send path
|
||||
RAMUniquePtr<uint8_t[]> tail_;
|
||||
uint32_t send_failure_started_ms_{0}; // Zero means no send stall in progress.
|
||||
// Runs while sends make no progress; a running clock keeps its start time
|
||||
StallClock stall_;
|
||||
uint32_t next_close_attempt_ms_{0};
|
||||
uint32_t last_lru_refresh_ms_{0};
|
||||
uint16_t tail_cap_{0};
|
||||
uint16_t tail_len_{0}; // Zero means nothing pending
|
||||
uint16_t tail_sent_{0};
|
||||
// Set on the main loop before queueing close work, cleared by the HTTPD-task callback when done.
|
||||
std::atomic<bool> close_work_queued_{false};
|
||||
std::atomic<bool> lru_work_queued_{false};
|
||||
// Guarded by the stream send mutex
|
||||
bool socket_closed_{false};
|
||||
// Main-loop only; the HTTPD task never reads or writes these flags.
|
||||
bool close_requested_{false};
|
||||
bool close_retry_warning_logged_{false};
|
||||
bool sending_{false};
|
||||
// The longest multi line log message in the tree (a climate dump_config) has 22 lines; a
|
||||
// longer one goes through the tail
|
||||
static constexpr size_t MAX_SEND_LINES = 22;
|
||||
static constexpr size_t MAX_SEND_IOV = 1 + 2 * MAX_SEND_LINES;
|
||||
// Chunk header, retry/id/event lines and the first "data: "
|
||||
static constexpr size_t PREFIX_BUF_SIZE = 128;
|
||||
|
||||
// Stack buffer for a state event's JSON; a larger document is serialized into the tail
|
||||
static constexpr size_t JSON_BUF_SIZE = 1024;
|
||||
// Same ceiling JsonBuilder::serialize() applies (max_heap_size in json_util.cpp); a larger
|
||||
// document is dropped before anything is on the wire
|
||||
static constexpr size_t JSON_MAX_SIZE = 5120;
|
||||
// Most RAM a stalled session keeps: the largest state document plus any accepted framing
|
||||
static constexpr size_t TAIL_MAX_SIZE = JSON_MAX_SIZE + PREFIX_BUF_SIZE + SSE_SUFFIX_LEN;
|
||||
static constexpr uint32_t SEND_STALL_TIMEOUT_MS = 20000;
|
||||
static constexpr uint32_t CLOSE_RETRY_INTERVAL_MS = 250;
|
||||
static constexpr uint32_t CLOSE_CONFIRM_INTERVAL_MS = 1000;
|
||||
static constexpr uint32_t LRU_REFRESH_INTERVAL_MS = 1000;
|
||||
};
|
||||
|
||||
using AsyncEventSourceClient = AsyncEventSourceResponse;
|
||||
|
||||
class AsyncEventSource : public AsyncWebHandler {
|
||||
friend class AsyncEventSourceResponse;
|
||||
|
||||
public:
|
||||
AsyncEventSource(StringRef url, esphome::web_server::WebServer *ws) : url_(url), web_server_(ws) {}
|
||||
~AsyncEventSource() override;
|
||||
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
bool canHandle(AsyncWebServerRequest *request) const override {
|
||||
if (request->method() != HTTP_GET)
|
||||
return false;
|
||||
char url_buf[AsyncWebServerRequest::URL_BUF_SIZE];
|
||||
return request->url_to(url_buf) == this->url_;
|
||||
}
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
void handleRequest(AsyncWebServerRequest *request) override;
|
||||
void try_send_nodefer(const char *message, size_t message_len, const char *event = nullptr, uint32_t id = 0,
|
||||
uint32_t reconnect = 0);
|
||||
void deferrable_send_state(void *source, bool detail_all, message_generator_t *message_generator);
|
||||
// The old event name argument would otherwise convert to true
|
||||
void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator) = delete;
|
||||
/// Returns true if there are sessions remaining (including pending cleanup).
|
||||
bool loop();
|
||||
bool empty() { return this->count() == 0; }
|
||||
|
||||
size_t count() const { return this->sessions_.size(); }
|
||||
|
||||
protected:
|
||||
// Cold path: move sessions from pending_sessions_ into sessions_ and greet each one.
|
||||
void __attribute__((noinline, cold)) adopt_pending_sessions_main_loop_();
|
||||
|
||||
StringRef url_; // Must outlive this object (string literal)
|
||||
// Main-loop only. Vector: SSE sessions are 1-5 connections, linear search beats set.
|
||||
std::vector<AsyncEventSourceResponse *> sessions_;
|
||||
// Httpd-task intake; guarded by pending_mutex_, gated by has_pending_sessions_.
|
||||
std::vector<AsyncEventSourceResponse *> pending_sessions_;
|
||||
Mutex pending_mutex_;
|
||||
esphome::web_server::WebServer *web_server_;
|
||||
std::atomic<bool> has_pending_sessions_{false};
|
||||
};
|
||||
#endif // USE_WEBSERVER
|
||||
|
||||
struct HttpHeader {
|
||||
const char *name;
|
||||
const char *value;
|
||||
};
|
||||
|
||||
class DefaultHeaders {
|
||||
friend class AsyncWebServerRequest;
|
||||
friend class AsyncStreamResponse;
|
||||
friend class AsyncWebServerResponse;
|
||||
#ifdef USE_WEBSERVER
|
||||
friend class AsyncEventSourceResponse;
|
||||
#endif
|
||||
static_assert(WEB_SERVER_DEFAULT_HEADERS_COUNT <= 8, "AsyncWebServerResponse::replaced_defaults_ holds 8 bits");
|
||||
|
||||
public:
|
||||
@@ -484,8 +359,7 @@ class DefaultHeaders {
|
||||
void apply_(httpd_req_t *req, uint8_t skip_mask = 0) const;
|
||||
};
|
||||
|
||||
} // namespace web_server_idf
|
||||
} // namespace esphome
|
||||
} // namespace esphome::web_server_idf
|
||||
|
||||
using namespace esphome::web_server_idf; // NOLINT(google-global-names-in-headers)
|
||||
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.logger import request_log_listener
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
async def to_code_testing(config: object) -> None:
|
||||
request_log_listener()
|
||||
cg.add_define("PACKET_INTERFACE_ADDRESSING_PORT_COUNT", 4)
|
||||
cg.add_define("USE_PACKET_INTERFACE_BASIC_ADDRESSING")
|
||||
cg.add_define("USE_PACKET_INTERFACE_BASIC_FRAGMENTATION")
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "esphome/components/logger/logger.h"
|
||||
#include "esphome/components/packet_interface/basic_fragmentation.h"
|
||||
#include "esphome/components/packet_interface/hdlc_reliability.h"
|
||||
#include "esphome/core/application.h"
|
||||
@@ -28,8 +26,6 @@ class MockLink : public PacketInterface {
|
||||
// Before the packet is read, as a log line in a real link would run it
|
||||
if (this->on_send)
|
||||
this->on_send();
|
||||
if (this->sent.size() == this->accept)
|
||||
return false;
|
||||
this->sent.push_back(to_vector(data));
|
||||
return true;
|
||||
}
|
||||
@@ -43,27 +39,6 @@ class MockLink : public PacketInterface {
|
||||
std::vector<Packet> sent;
|
||||
std::function<void()> on_send;
|
||||
size_t capacity{SIZE_MAX};
|
||||
// Packets taken before the link refuses more
|
||||
size_t accept{SIZE_MAX};
|
||||
};
|
||||
|
||||
// Collects each warning logged
|
||||
class Warnings {
|
||||
public:
|
||||
Warnings() : previous_(logger::global_logger) {
|
||||
this->logger_.pre_setup();
|
||||
this->logger_.set_log_level(ESPHOME_LOG_LEVEL_WARN);
|
||||
this->logger_.add_log_callback(this, [](void *instance, uint8_t, const char *, const char *message, size_t len) {
|
||||
static_cast<Warnings *>(instance)->lines.emplace_back(message, len);
|
||||
});
|
||||
}
|
||||
~Warnings() { logger::global_logger = this->previous_; }
|
||||
|
||||
std::vector<std::string> lines;
|
||||
|
||||
private:
|
||||
logger::Logger *previous_;
|
||||
logger::Logger logger_{0};
|
||||
};
|
||||
|
||||
struct BufferHolder {
|
||||
@@ -192,19 +167,6 @@ TEST_F(BasicFragmentationTest, NotStartedWithoutRoomForAllFragments) {
|
||||
EXPECT_FALSE(a.send(make_packet(101)));
|
||||
}
|
||||
|
||||
TEST_F(BasicFragmentationTest, PacketCutShortIsWarned) {
|
||||
Peer a(&this->link_a_);
|
||||
Warnings warnings;
|
||||
this->link_a_.accept = 0;
|
||||
EXPECT_FALSE(a.send(make_packet(30)));
|
||||
EXPECT_TRUE(warnings.lines.empty()); // nothing was sent
|
||||
this->link_a_.accept = 1;
|
||||
EXPECT_FALSE(a.send(make_packet(30)));
|
||||
EXPECT_EQ(this->link_a_.sent.size(), 1u);
|
||||
ASSERT_EQ(warnings.lines.size(), 1u);
|
||||
EXPECT_NE(warnings.lines[0].find("Packet cut short, fragment 1 of 3"), std::string::npos);
|
||||
}
|
||||
|
||||
TEST_F(BasicFragmentationTest, InterleavedSourcesNotMixed) {
|
||||
Peer a(&this->link_a_), c(&this->link_a_), b(&this->link_b_);
|
||||
a.send(make_packet(20));
|
||||
|
||||
@@ -257,65 +257,6 @@ TEST_F(HdlcReliabilityTest, LostAckDoesNotDuplicate) {
|
||||
EXPECT_TRUE(a.send({0x01}));
|
||||
}
|
||||
|
||||
// Runs a layer's loop until it stops asking, without delivering what it sends
|
||||
static void run_until_idle(TestHdlc &hdlc) {
|
||||
for (int i = 0; i != 10 && !hdlc.is_idle(); i++)
|
||||
hdlc.loop();
|
||||
}
|
||||
|
||||
static size_t count_info(const std::vector<Frame> &frames) {
|
||||
size_t count = 0;
|
||||
for (const auto &frame : frames)
|
||||
count += is_info(frame);
|
||||
return count;
|
||||
}
|
||||
|
||||
TEST_F(HdlcReliabilityTest, TimeoutResendsOnlyOldestFrame) {
|
||||
Peer a, b;
|
||||
pump(a, b);
|
||||
for (uint8_t i = 0; i != 3; i++)
|
||||
ASSERT_TRUE(a.send({i}));
|
||||
pump(a, b, nullptr, [](const Frame &frame) { return is_ack(frame); });
|
||||
ASSERT_EQ(b.received.size(), 3);
|
||||
ASSERT_EQ(a.hdlc->outstanding_(), 3);
|
||||
a.hdlc->on_timeout_();
|
||||
run_until_idle(*a.hdlc);
|
||||
EXPECT_EQ(count_info(a.link.sent), 1);
|
||||
// The peer's answer to the duplicate acknowledges all three
|
||||
pump(a, b);
|
||||
EXPECT_EQ(b.received.size(), 3);
|
||||
EXPECT_EQ(a.hdlc->outstanding_(), 0);
|
||||
}
|
||||
|
||||
TEST_F(HdlcReliabilityTest, RestFollowOnceOldestAcknowledged) {
|
||||
Peer a, b;
|
||||
pump(a, b);
|
||||
for (uint8_t i = 0; i != 3; i++)
|
||||
ASSERT_TRUE(a.send({i}));
|
||||
pump(a, b, [](const Frame &frame) { return is_info(frame); });
|
||||
ASSERT_TRUE(b.received.empty());
|
||||
a.hdlc->on_timeout_();
|
||||
run_until_idle(*a.hdlc);
|
||||
EXPECT_EQ(count_info(a.link.sent), 1);
|
||||
pump(a, b);
|
||||
EXPECT_EQ(b.received, (std::vector<Frame>{{0}, {1}, {2}}));
|
||||
EXPECT_EQ(a.hdlc->outstanding_(), 0);
|
||||
}
|
||||
|
||||
TEST_F(HdlcReliabilityTest, NewFrameWaitsWhileOldestIsResent) {
|
||||
Peer a, b;
|
||||
pump(a, b);
|
||||
ASSERT_TRUE(a.send({0}));
|
||||
pump(a, b, [](const Frame &frame) { return is_info(frame); });
|
||||
a.hdlc->on_timeout_();
|
||||
run_until_idle(*a.hdlc);
|
||||
ASSERT_TRUE(a.send({1}));
|
||||
run_until_idle(*a.hdlc);
|
||||
EXPECT_EQ(count_info(a.link.sent), 1);
|
||||
pump(a, b);
|
||||
EXPECT_EQ(b.received, (std::vector<Frame>{{0}, {1}}));
|
||||
}
|
||||
|
||||
TEST_F(HdlcReliabilityTest, QueueFull) {
|
||||
Peer a(2), b(2);
|
||||
pump(a, b);
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "esphome/components/packet_interface/listen_before_talk.h"
|
||||
|
||||
namespace esphome::packet_interface::lbt_testing {
|
||||
|
||||
static constexpr uint32_t START = 10000;
|
||||
static constexpr uint32_t MAX_BUSY = ListenBeforeTalk::DEFAULT_MAX_BUSY_MS;
|
||||
|
||||
TEST(ListenBeforeTalkTest, SendsWhenNotBusy) {
|
||||
ListenBeforeTalk lbt;
|
||||
EXPECT_TRUE(lbt.may_send(false, START, START));
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, RefusesWhileBusyUpToLimit) {
|
||||
ListenBeforeTalk lbt;
|
||||
EXPECT_FALSE(lbt.may_send(true, START, START));
|
||||
// Refused every 50 ms, as the reliability layer retries
|
||||
uint32_t now = START;
|
||||
while (now - START < MAX_BUSY - 50) {
|
||||
now += 50;
|
||||
EXPECT_FALSE(lbt.may_send(true, now, now));
|
||||
}
|
||||
now += 50;
|
||||
EXPECT_TRUE(lbt.may_send(true, now, now));
|
||||
EXPECT_EQ(lbt.busy_for(now), MAX_BUSY);
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, BusyFlagThatNeverClearsReleasedForSlowSends) {
|
||||
ListenBeforeTalk lbt;
|
||||
// A send every 10 s, as session keep-alives, always finding the radio busy
|
||||
EXPECT_FALSE(lbt.may_send(true, START, START));
|
||||
EXPECT_TRUE(lbt.may_send(true, START + 10000, START + 10000));
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, FreeRadioEndsBusyPeriod) {
|
||||
ListenBeforeTalk lbt;
|
||||
EXPECT_FALSE(lbt.may_send(true, START, START));
|
||||
EXPECT_TRUE(lbt.may_send(false, START + 1500, START + 1500));
|
||||
// A new busy period starts, so the earlier one does not count
|
||||
EXPECT_FALSE(lbt.may_send(true, START + MAX_BUSY + 100, START + MAX_BUSY + 100));
|
||||
EXPECT_EQ(lbt.busy_for(START + MAX_BUSY + 100), 0);
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, EndBusyStartsNewPeriod) {
|
||||
ListenBeforeTalk lbt;
|
||||
EXPECT_FALSE(lbt.may_send(true, START, START));
|
||||
// The link saw the radio free between sends, so a later reception is a new busy period
|
||||
lbt.end_busy();
|
||||
EXPECT_FALSE(lbt.may_send(true, START + MAX_BUSY + 100, START + MAX_BUSY + 100));
|
||||
EXPECT_EQ(lbt.busy_for(START + MAX_BUSY + 100), 0);
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, LimitCanBeLonger) {
|
||||
ListenBeforeTalk lbt;
|
||||
lbt.set_max_busy(8000);
|
||||
EXPECT_FALSE(lbt.may_send(true, START, START));
|
||||
EXPECT_FALSE(lbt.may_send(true, START + 7999, START + 7999));
|
||||
EXPECT_TRUE(lbt.may_send(true, START + 8000, START + 8000));
|
||||
}
|
||||
|
||||
static constexpr uint32_t TURNAROUND = 250;
|
||||
|
||||
TEST(ListenBeforeTalkTest, SendInLaterPassWaitsForTurnaround) {
|
||||
ListenBeforeTalk lbt;
|
||||
lbt.set_turnaround(TURNAROUND);
|
||||
lbt.sent(START + 300, START);
|
||||
EXPECT_FALSE(lbt.may_send(false, START + 320, START + 320));
|
||||
EXPECT_FALSE(lbt.may_send(false, START + 300 + TURNAROUND - 1, START + 520));
|
||||
EXPECT_TRUE(lbt.may_send(false, START + 300 + TURNAROUND, START + 540));
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, SendInSamePassNotHeld) {
|
||||
ListenBeforeTalk lbt;
|
||||
lbt.set_turnaround(TURNAROUND);
|
||||
// The next fragment of the same packet
|
||||
lbt.sent(START + 300, START);
|
||||
EXPECT_TRUE(lbt.may_send(false, START + 301, START));
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, ReceptionEndsTurnaround) {
|
||||
ListenBeforeTalk lbt;
|
||||
lbt.set_turnaround(TURNAROUND);
|
||||
lbt.sent(START + 300, START);
|
||||
lbt.received();
|
||||
EXPECT_TRUE(lbt.may_send(false, START + 320, START + 320));
|
||||
}
|
||||
|
||||
TEST(ListenBeforeTalkTest, NoTurnaroundByDefault) {
|
||||
ListenBeforeTalk lbt;
|
||||
lbt.sent(START + 300, START);
|
||||
EXPECT_TRUE(lbt.may_send(false, START + 320, START + 320));
|
||||
}
|
||||
|
||||
} // namespace esphome::packet_interface::lbt_testing
|
||||
@@ -1,213 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include "esphome/components/packet_interface/radio_link.h"
|
||||
#include "esphome/core/application.h"
|
||||
|
||||
namespace esphome::packet_interface::radio_link_testing {
|
||||
|
||||
using Packet = std::vector<uint8_t>;
|
||||
|
||||
static constexpr uint32_t START = 10000;
|
||||
static constexpr uint32_t MAX_BUSY = ListenBeforeTalk::DEFAULT_MAX_BUSY_MS;
|
||||
|
||||
class FakeListener {
|
||||
public:
|
||||
virtual void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) = 0;
|
||||
};
|
||||
|
||||
enum class FakeError { NONE, BUSY };
|
||||
|
||||
class FakeRadio : public Component {
|
||||
public:
|
||||
bool is_receiving() { return this->receiving; }
|
||||
void register_listener(FakeListener *listener) { this->listener = listener; }
|
||||
void fail() { this->mark_failed(); }
|
||||
FakeError transmit_packet(const uint8_t *data, size_t length) {
|
||||
this->sent.emplace_back(data, data + length);
|
||||
return this->result;
|
||||
}
|
||||
|
||||
FakeListener *listener{nullptr};
|
||||
std::vector<Packet> sent;
|
||||
bool receiving{false};
|
||||
FakeError result{FakeError::NONE};
|
||||
};
|
||||
|
||||
// Reads the time that set_time() sets
|
||||
class TestLink : public RadioLink<FakeRadio, FakeListener, FakeError, 16> {
|
||||
public:
|
||||
using RadioLink::RadioLink;
|
||||
uint8_t not_understood_count() const { return this->not_understood_count_; }
|
||||
|
||||
protected:
|
||||
uint32_t current_time_ms() const override { return App.get_loop_component_start_time(); }
|
||||
};
|
||||
|
||||
static void set_time(uint32_t now) { LoopBlockingGuard guard{nullptr, nullptr, now}; }
|
||||
|
||||
class RadioLinkTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
this->radio_.call();
|
||||
this->link_.call();
|
||||
}
|
||||
bool send_() {
|
||||
static const Packet PACKET{1, 2, 3};
|
||||
return this->link_.send_to_interface(PacketBuffer(PACKET), {});
|
||||
}
|
||||
|
||||
FakeRadio radio_;
|
||||
TestLink link_{&radio_, 16};
|
||||
};
|
||||
|
||||
TEST_F(RadioLinkTest, LoopRunsOnlyWhileBusy) {
|
||||
EXPECT_EQ(this->radio_.listener, &this->link_);
|
||||
EXPECT_TRUE(this->link_.is_idle());
|
||||
set_time(START);
|
||||
this->radio_.receiving = true;
|
||||
EXPECT_FALSE(this->send_());
|
||||
EXPECT_TRUE(this->link_.is_in_loop_state());
|
||||
this->link_.loop();
|
||||
EXPECT_TRUE(this->link_.is_in_loop_state());
|
||||
this->radio_.receiving = false;
|
||||
this->link_.loop();
|
||||
EXPECT_TRUE(this->link_.is_idle());
|
||||
EXPECT_TRUE(this->radio_.sent.empty());
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, ReceptionAfterStalePeriodIsNotSentOver) {
|
||||
set_time(START);
|
||||
this->radio_.receiving = true;
|
||||
EXPECT_FALSE(this->send_());
|
||||
// The reception ends with no send to see it
|
||||
this->radio_.receiving = false;
|
||||
this->link_.loop();
|
||||
// A new reception, longer after the first than the limit, still holds the send back
|
||||
set_time(START + MAX_BUSY + 100);
|
||||
this->radio_.receiving = true;
|
||||
EXPECT_FALSE(this->send_());
|
||||
EXPECT_TRUE(this->radio_.sent.empty());
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, BusyFlagThatNeverClearsIsSentOver) {
|
||||
set_time(START);
|
||||
this->radio_.receiving = true;
|
||||
EXPECT_FALSE(this->send_());
|
||||
this->link_.loop();
|
||||
set_time(START + MAX_BUSY);
|
||||
EXPECT_TRUE(this->send_());
|
||||
ASSERT_EQ(this->radio_.sent.size(), 1u);
|
||||
EXPECT_EQ(this->radio_.sent[0], (Packet{1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, EmptyPacketCountsAsSentWithoutTransmitting) {
|
||||
static const Packet EMPTY;
|
||||
EXPECT_TRUE(this->link_.send_to_interface(PacketBuffer(EMPTY), {}));
|
||||
EXPECT_TRUE(this->radio_.sent.empty());
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, FailedRadioRefusesSends) {
|
||||
this->radio_.fail();
|
||||
EXPECT_FALSE(this->send_());
|
||||
EXPECT_FALSE(this->send_());
|
||||
EXPECT_TRUE(this->radio_.sent.empty());
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, RadioFailingWhileBusyEndsBusyPeriod) {
|
||||
set_time(START);
|
||||
this->radio_.receiving = true;
|
||||
EXPECT_FALSE(this->send_());
|
||||
EXPECT_TRUE(this->link_.is_in_loop_state());
|
||||
this->radio_.fail();
|
||||
this->link_.loop();
|
||||
EXPECT_TRUE(this->link_.is_idle());
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, ReceivedPacketCarriesSignal) {
|
||||
Packet received;
|
||||
PacketMetaData meta;
|
||||
this->link_.add_packet_interface_listener([&](const PacketBuffer &data, const PacketMetaData &meta_data) {
|
||||
received.resize(data.size());
|
||||
data.copy_to(received.data(), received.size());
|
||||
meta = meta_data;
|
||||
});
|
||||
this->link_.on_packet({4, 5}, -80.0f, 7.5f);
|
||||
EXPECT_EQ(received, (Packet{4, 5}));
|
||||
EXPECT_FLOAT_EQ(meta.rssi, -80.0f);
|
||||
EXPECT_FLOAT_EQ(meta.snr, 7.5f);
|
||||
}
|
||||
|
||||
// After a send, a send in a later loop pass waits for the peer's reply, which may come at once
|
||||
TEST_F(RadioLinkTest, LaterSendWaitsForPeerToReply) {
|
||||
this->link_.set_turnaround(250);
|
||||
set_time(START);
|
||||
EXPECT_TRUE(this->send_());
|
||||
EXPECT_TRUE(this->send_()); // the same loop pass, as the fragments of one packet
|
||||
set_time(START + 100);
|
||||
EXPECT_FALSE(this->send_());
|
||||
set_time(START + 250);
|
||||
EXPECT_TRUE(this->send_());
|
||||
set_time(START + 300);
|
||||
this->radio_.listener->on_packet(Packet{4, 5}, -50, 5);
|
||||
EXPECT_TRUE(this->send_());
|
||||
EXPECT_EQ(this->radio_.sent.size(), 4u);
|
||||
}
|
||||
|
||||
// A packet in two parts, as a layer adds its header, is joined before it goes to the radio
|
||||
TEST_F(RadioLinkTest, SplitPacketIsJoined) {
|
||||
static const Packet HEADER{9, 8};
|
||||
static const Packet BODY{1, 2, 3};
|
||||
PacketBuffer body(BODY);
|
||||
EXPECT_TRUE(this->link_.send_to_interface(PacketBuffer(HEADER.data(), HEADER.size(), &body, nullptr, 0), {}));
|
||||
ASSERT_EQ(this->radio_.sent.size(), 1u);
|
||||
EXPECT_EQ(this->radio_.sent[0], (Packet{9, 8, 1, 2, 3}));
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, OversizedPacketRefused) {
|
||||
static const Packet LARGEST(16, 0x55);
|
||||
static const Packet OVERSIZED(17, 0x55);
|
||||
EXPECT_FALSE(this->link_.send_to_interface(PacketBuffer(OVERSIZED), {}));
|
||||
EXPECT_TRUE(this->radio_.sent.empty());
|
||||
EXPECT_TRUE(this->link_.send_to_interface(PacketBuffer(LARGEST), {}));
|
||||
EXPECT_EQ(this->radio_.sent.size(), 1u);
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, TransmitErrorIsReported) {
|
||||
this->radio_.result = FakeError::BUSY;
|
||||
EXPECT_FALSE(this->send_());
|
||||
this->radio_.result = FakeError::NONE;
|
||||
EXPECT_TRUE(this->send_());
|
||||
}
|
||||
|
||||
// The radio receives packets up to its own limit, which can be more than the link's
|
||||
TEST_F(RadioLinkTest, OversizedReceptionDropped) {
|
||||
std::vector<size_t> sizes;
|
||||
this->link_.add_packet_interface_listener(
|
||||
[&](const PacketBuffer &data, const PacketMetaData &) { sizes.push_back(data.size()); });
|
||||
this->link_.on_packet(Packet(17, 0x55), -80.0f, 7.5f);
|
||||
this->link_.on_packet(Packet(16, 0x55), -80.0f, 7.5f);
|
||||
EXPECT_EQ(sizes, std::vector<size_t>{16});
|
||||
}
|
||||
|
||||
// A send made over a busy flag that seemed stuck lets the radio listen again, so a reception that starts before
|
||||
// loop() runs is waited for in a new busy period
|
||||
TEST_F(RadioLinkTest, ForcedSendEndsBusyPeriod) {
|
||||
set_time(START);
|
||||
this->radio_.receiving = true;
|
||||
EXPECT_FALSE(this->send_());
|
||||
set_time(START + MAX_BUSY);
|
||||
EXPECT_TRUE(this->send_());
|
||||
set_time(START + MAX_BUSY + 100);
|
||||
EXPECT_FALSE(this->send_());
|
||||
EXPECT_EQ(this->radio_.sent.size(), 1u);
|
||||
}
|
||||
|
||||
TEST_F(RadioLinkTest, AcceptedPacketEndsRunOfOversizedOnes) {
|
||||
this->link_.on_packet(Packet(17, 0x55), -80.0f, 7.5f);
|
||||
EXPECT_EQ(this->link_.not_understood_count(), 1);
|
||||
this->link_.on_packet(Packet(16, 0x55), -80.0f, 7.5f);
|
||||
EXPECT_EQ(this->link_.not_understood_count(), 0);
|
||||
}
|
||||
|
||||
} // namespace esphome::packet_interface::radio_link_testing
|
||||
@@ -29,11 +29,9 @@ class MockLink : public PacketInterface {
|
||||
return true;
|
||||
}
|
||||
void inject(const Packet &packet) { this->on_receive_from_interface_(PacketBuffer(packet), {}); }
|
||||
size_t get_send_capacity() const override { return this->capacity; }
|
||||
|
||||
std::vector<Packet> sent;
|
||||
std::function<void()> on_send;
|
||||
size_t capacity{SIZE_MAX};
|
||||
};
|
||||
|
||||
static constexpr size_t ROLLING_TRAILER_SIZE = 16;
|
||||
@@ -95,16 +93,6 @@ TEST_F(XxteaEncryptionTest, MaxPacketSizeAllowsForTrailerAndWords) {
|
||||
EXPECT_EQ(c.encryption->get_max_packet_size(), 64 - TRAILER_SIZE);
|
||||
}
|
||||
|
||||
// A link such as TCP can run out of room; a fragmentation layer above must see that
|
||||
TEST_F(XxteaEncryptionTest, ForwardsTheLinksSendCapacity) {
|
||||
Peer a;
|
||||
EXPECT_EQ(a.encryption->get_send_capacity(), SIZE_MAX);
|
||||
a.link.capacity = 1;
|
||||
EXPECT_EQ(a.encryption->get_send_capacity(), 1u);
|
||||
a.link.capacity = 0;
|
||||
EXPECT_EQ(a.encryption->get_send_capacity(), 0u);
|
||||
}
|
||||
|
||||
static void round_trip_all_sizes(bool rolling_code, size_t trailer_size) {
|
||||
Peer a(0x12345678, 64, rolling_code), b(0x12345678, 64, rolling_code);
|
||||
a.introduce(b);
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
sx126x:
|
||||
cs_pin: ${cs_pin}
|
||||
rst_pin: ${rst_pin}
|
||||
busy_pin: ${busy_pin}
|
||||
dio1_pin: ${dio1_pin}
|
||||
hw_version: sx1262
|
||||
rf_switch: true
|
||||
frequency: 917MHz
|
||||
modulation: LORA
|
||||
crc_enable: true
|
||||
|
||||
packet_interface:
|
||||
- platform: sx126x
|
||||
id: sx126x_packet_interface
|
||||
addressing:
|
||||
address: 0x0001
|
||||
destination: 0x0002
|
||||
reliability:
|
||||
ack_delay: 100ms
|
||||
@@ -1,59 +0,0 @@
|
||||
#include "sx126x_test_helpers.h"
|
||||
|
||||
namespace esphome::sx126x::testing {
|
||||
|
||||
// The radio link refuses to send while is_receiving(), as a send would lose the packet
|
||||
TEST(SX126xReceiving, ReportsAPacketOnTheWayOrWaiting) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, true);
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
hw.irq = IRQ_HEADER_VALID;
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
hw.irq = IRQ_SYNCWORD_VALID;
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
hw.irq = IRQ_RX_DONE;
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
// Held by a send that found it waiting
|
||||
EXPECT_EQ(hw.radio.transmit_packet(hw.packet), SX126xError::SX126X_ERROR_BUSY);
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
hw.radio.loop();
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
}
|
||||
|
||||
// The link would otherwise send over a reception it could not see
|
||||
TEST(SX126xReceiving, FailedReadCountsAsReceiving) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, true);
|
||||
hw.clear_trace();
|
||||
hw.fail_at = 1; // BUSY stays high for the IRQ read
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
hw.clear_trace();
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
}
|
||||
|
||||
TEST(SX126xReceiving, NotWhileSendingOrAsleep) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, false);
|
||||
hw.irq = IRQ_HEADER_VALID;
|
||||
hw.clear_trace();
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
EXPECT_TRUE(hw.transactions.empty()); // A sleeping radio is not woken to ask
|
||||
}
|
||||
|
||||
// A valid header or sync word is flagged, but only the end of a packet is signalled on DIO1
|
||||
TEST(SX126xReceiving, ReceiveFlagsHeaderAndSyncWithoutSignallingThem) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, true);
|
||||
const auto params = std::find_if(hw.transactions.rbegin(), hw.transactions.rend(), [](const auto &command) {
|
||||
return !command.empty() && command[0] == RADIO_SET_DIOIRQPARAMS;
|
||||
});
|
||||
ASSERT_NE(params, hw.transactions.rend());
|
||||
ASSERT_GE(params->size(), 5u);
|
||||
const uint16_t flags = encode_uint16((*params)[1], (*params)[2]);
|
||||
const uint16_t dio1 = encode_uint16((*params)[3], (*params)[4]);
|
||||
EXPECT_EQ(flags & (IRQ_HEADER_VALID | IRQ_SYNCWORD_VALID), IRQ_HEADER_VALID | IRQ_SYNCWORD_VALID);
|
||||
EXPECT_EQ(dio1 & (IRQ_HEADER_VALID | IRQ_SYNCWORD_VALID), 0);
|
||||
EXPECT_NE(dio1 & IRQ_RX_DONE, 0);
|
||||
}
|
||||
|
||||
} // namespace esphome::sx126x::testing
|
||||
@@ -1,9 +0,0 @@
|
||||
substitutions:
|
||||
cs_pin: GPIO12
|
||||
rst_pin: GPIO13
|
||||
busy_pin: GPIO25
|
||||
dio1_pin: GPIO26
|
||||
|
||||
packages:
|
||||
spi: !include ../../test_build_components/common/spi/esp32-idf.yaml
|
||||
sx126x: !include common-packet-interface.yaml
|
||||
@@ -1,17 +0,0 @@
|
||||
sx127x:
|
||||
cs_pin: ${cs_pin}
|
||||
rst_pin: ${rst_pin}
|
||||
dio0_pin: ${dio0_pin}
|
||||
frequency: 917MHz
|
||||
modulation: LORA
|
||||
preamble_size: 8
|
||||
crc_enable: true
|
||||
|
||||
packet_interface:
|
||||
- platform: sx127x
|
||||
id: sx127x_packet_interface
|
||||
addressing:
|
||||
address: 0x0001
|
||||
destination: 0x0002
|
||||
reliability:
|
||||
ack_delay: 100ms
|
||||
@@ -1,64 +0,0 @@
|
||||
#include "sx127x_test_helpers.h"
|
||||
|
||||
namespace esphome::sx127x::testing {
|
||||
|
||||
// The radio link refuses to send while is_receiving(), as a send would lose the packet
|
||||
TEST(SX127xReceiving, LoRaReportsAPacketOnTheWayOrWaiting) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, true);
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
for (const uint8_t stat : {MODEM_STAT_SIGNAL_DETECTED, MODEM_STAT_SIGNAL_SYNCHRONIZED, MODEM_STAT_HEADER_VALID}) {
|
||||
hw.regs[REG_MODEM_STAT] = stat;
|
||||
EXPECT_TRUE(hw.radio.is_receiving()) << int(stat);
|
||||
}
|
||||
hw.regs[REG_MODEM_STAT] = 0;
|
||||
hw.receive({0x01, 0x02});
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
// Held by a send that found it waiting
|
||||
EXPECT_EQ(hw.radio.transmit_packet(hw.packet), SX127xError::SX127X_ERROR_BUSY);
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
hw.radio.loop();
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
}
|
||||
|
||||
TEST(SX127xReceiving, FskReportsAMatchedSyncWord) {
|
||||
RadioHardware hw;
|
||||
hw.configure(false, true);
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
hw.regs[REG_IRQ_FLAGS_1] = IRQ_FLAGS_1_PREAMBLE_DETECT;
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
hw.regs[REG_IRQ_FLAGS_1] = IRQ_FLAGS_1_SYNC_ADDRESS_MATCH;
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
hw.regs[REG_IRQ_FLAGS_1] = 0;
|
||||
hw.receive({0x01, 0x02});
|
||||
EXPECT_TRUE(hw.radio.is_receiving());
|
||||
}
|
||||
|
||||
TEST(SX127xReceiving, NotAfterRawTxOrWhileSendingOrAsleep) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, true);
|
||||
hw.radio.set_mode_tx(); // Its TX_DONE raises DIO0, which is not a packet
|
||||
EXPECT_TRUE(hw.dio0.digital_read());
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
hw.radio.set_mode_rx();
|
||||
TransmitListener listener;
|
||||
hw.auto_complete = false;
|
||||
ASSERT_EQ(hw.radio.start_transmit_async(&listener, hw.packet), SX127xError::SX127X_ERROR_NONE);
|
||||
hw.regs[REG_MODEM_STAT] = MODEM_STAT_HEADER_VALID;
|
||||
EXPECT_FALSE(hw.radio.is_receiving());
|
||||
EXPECT_TRUE(hw.radio.cancel_transmit_async(&listener));
|
||||
|
||||
RadioHardware asleep;
|
||||
asleep.configure(true, false);
|
||||
asleep.regs[REG_MODEM_STAT] = MODEM_STAT_HEADER_VALID;
|
||||
EXPECT_FALSE(asleep.radio.is_receiving());
|
||||
}
|
||||
|
||||
TEST(SX127xReceiving, FskFifoHoldsTheLengthByte) {
|
||||
RadioHardware hw;
|
||||
hw.configure(false, true);
|
||||
EXPECT_EQ(hw.radio.get_max_packet_size(), 63u);
|
||||
EXPECT_EQ(hw.regs[REG_PAYLOAD_LENGTH_LSB], 63);
|
||||
}
|
||||
|
||||
} // namespace esphome::sx127x::testing
|
||||
@@ -1,30 +0,0 @@
|
||||
#include "sx127x_test_helpers.h"
|
||||
|
||||
namespace esphome::sx127x::testing {
|
||||
|
||||
// Values from the datasheet formulas for the RadioHardware defaults (433.92 MHz, 4800 bps,
|
||||
// 5 kHz deviation, 8 symbol preamble), independent of the code under test
|
||||
static constexpr uint32_t FRF = 0x6C7AE1; // 433920000 * 2^19 / 32 MHz
|
||||
static constexpr uint16_t BITRATE = 0x1A0B; // 32 MHz / 4800, rounded
|
||||
static constexpr uint16_t FDEV = 0x0051; // 5000 * 2^19 / 32 MHz
|
||||
static constexpr uint16_t PREAMBLE = 8;
|
||||
|
||||
static uint32_t reg16(const RadioHardware &hw, uint8_t msb) { return (hw.regs[msb] << 8) | hw.regs[msb + 1]; }
|
||||
|
||||
TEST(SX127xRegisters, FskWritesEachByteOfTheMultiByteSettings) {
|
||||
RadioHardware hw;
|
||||
hw.configure(false, false);
|
||||
EXPECT_EQ((hw.regs[REG_FRF_MSB] << 16) | reg16(hw, REG_FRF_MID), FRF);
|
||||
EXPECT_EQ(reg16(hw, REG_BITRATE_MSB), BITRATE);
|
||||
EXPECT_EQ(reg16(hw, REG_FDEV_MSB), FDEV);
|
||||
EXPECT_EQ(reg16(hw, REG_PREAMBLE_SIZE_MSB), PREAMBLE);
|
||||
}
|
||||
|
||||
TEST(SX127xRegisters, LoraWritesEachByteOfTheMultiByteSettings) {
|
||||
RadioHardware hw;
|
||||
hw.configure(true, false);
|
||||
EXPECT_EQ((hw.regs[REG_FRF_MSB] << 16) | reg16(hw, REG_FRF_MID), FRF);
|
||||
EXPECT_EQ(reg16(hw, REG_PREAMBLE_LEN_MSB), PREAMBLE);
|
||||
}
|
||||
|
||||
} // namespace esphome::sx127x::testing
|
||||
@@ -28,8 +28,7 @@ class TestPin : public GPIOPin {
|
||||
};
|
||||
|
||||
// An SX127x register file behind the real software SPI bus. The first byte of each
|
||||
// transaction is the address, with bit 7 set for a write; FIFO reads and writes stream, and
|
||||
// further bytes of any other write go to the next register.
|
||||
// transaction is the address, with bit 7 set for a write; FIFO reads and writes stream.
|
||||
class RadioHardware {
|
||||
public:
|
||||
RadioHardware() {
|
||||
@@ -87,7 +86,6 @@ class RadioHardware {
|
||||
this->radio.set_preamble_size(8);
|
||||
this->radio.set_crc_enable(true);
|
||||
this->radio.set_sync_value({0x12});
|
||||
this->radio.set_rx_floor(-94); // FSK setup converts it to an int, so it must be set
|
||||
}
|
||||
~RadioHardware() {
|
||||
// The configuration retry interval (id 0) must not outlive the radio
|
||||
@@ -219,8 +217,6 @@ class RadioHardware {
|
||||
this->fifo.push_back(value);
|
||||
return;
|
||||
}
|
||||
// As on the chip, each further byte of a burst goes to the next register
|
||||
this->addr_ = 0x80 | ((addr + 1) & 0x7F);
|
||||
if (addr == REG_OP_MODE) {
|
||||
this->write_mode_(value);
|
||||
return;
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
substitutions:
|
||||
cs_pin: GPIO12
|
||||
rst_pin: GPIO13
|
||||
dio0_pin: GPIO26
|
||||
|
||||
packages:
|
||||
spi: !include ../../test_build_components/common/spi/esp32-idf.yaml
|
||||
sx127x: !include common-packet-interface.yaml
|
||||
@@ -1,18 +0,0 @@
|
||||
from esphome.components import socket as socket_component
|
||||
from esphome.core import CORE
|
||||
from esphome.types import ConfigType
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
async def to_code_testing(config: ConfigType) -> None:
|
||||
socket_component.require_tcp_listener()
|
||||
# No entity platforms are configured, so count the connected sensor to emit USE_BINARY_SENSOR
|
||||
CORE.platform_counts["binary_sensor"] += 1
|
||||
|
||||
manifest.to_code = to_code_testing
|
||||
manifest.dependencies = manifest.dependencies + [
|
||||
"binary_sensor",
|
||||
"packet_interface",
|
||||
"socket",
|
||||
]
|
||||
@@ -1,4 +0,0 @@
|
||||
packet_interface:
|
||||
- platform: tcp
|
||||
role: server
|
||||
port: 6000
|
||||
@@ -1,36 +0,0 @@
|
||||
wifi:
|
||||
ssid: MySSID
|
||||
password: password1
|
||||
|
||||
packet_interface:
|
||||
- platform: tcp
|
||||
id: tcp_client_link
|
||||
host: 192.168.1.50
|
||||
port: 6060
|
||||
keep_alive: 5s
|
||||
encryption: Johnny Cash
|
||||
fragmentation:
|
||||
max_packet_size: 2048
|
||||
connected:
|
||||
name: TCP link connected
|
||||
- platform: tcp
|
||||
id: tcp_server_link
|
||||
role: server
|
||||
port: 6061
|
||||
allowed_ips:
|
||||
- 192.168.1.0/24
|
||||
|
||||
sensor:
|
||||
- platform: template
|
||||
id: tcp_sensor
|
||||
lambda: return 1.0;
|
||||
|
||||
packet_transport:
|
||||
- platform: packet_interface
|
||||
packet_interface: tcp_client_link
|
||||
sensors:
|
||||
- tcp_sensor
|
||||
- platform: packet_interface
|
||||
packet_interface: tcp_server_link
|
||||
sensors:
|
||||
- tcp_sensor
|
||||
@@ -1,358 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <csignal>
|
||||
#include <cstddef>
|
||||
#include <fcntl.h>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/components/tcp/packet_interface/tcp_packet_interface.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/wake.h"
|
||||
|
||||
#ifdef USE_HOST
|
||||
|
||||
namespace esphome::tcp::testing {
|
||||
|
||||
// Copies a packet into a vector, for comparing in tests
|
||||
static std::vector<uint8_t> to_vector(const esphome::packet_interface::PacketBuffer &data) {
|
||||
std::vector<uint8_t> result(data.size());
|
||||
data.copy_to(result.data(), result.size());
|
||||
return result;
|
||||
}
|
||||
|
||||
using packet_interface::PacketBuffer;
|
||||
using packet_interface::PacketMetaData;
|
||||
using Packet = std::vector<uint8_t>;
|
||||
|
||||
// The receive buffer is a base, so it is set up before the link's constructor sees it
|
||||
struct TcpBufferStorage {
|
||||
uint8_t rx_buffer_storage[64]{};
|
||||
};
|
||||
|
||||
class TestTcp : public TcpBufferStorage, public TcpPacketInterface {
|
||||
public:
|
||||
TestTcp() : TcpPacketInterface(this->rx_buffer_storage) {
|
||||
this->set_host("peer");
|
||||
this->set_port(1);
|
||||
}
|
||||
socket::TcpClientLink &link() { return this->link_; }
|
||||
};
|
||||
|
||||
// A layer stacked on the link, to check that the session state reaches it
|
||||
class UpperLayer : public packet_interface::PacketInterface {
|
||||
public:
|
||||
explicit UpperLayer(PacketInterface *lower) : packet_interface::PacketInterface(lower->get_max_packet_size()) {
|
||||
this->stack_on_(lower, packet_interface::CAPABILITY_ENCRYPTION);
|
||||
}
|
||||
bool send_to_interface(const PacketBuffer &, const PacketMetaData &) override { return true; }
|
||||
};
|
||||
|
||||
struct End {
|
||||
End() {
|
||||
this->link.add_packet_interface_listener(
|
||||
[this](const PacketBuffer &data, const PacketMetaData &) { this->received.push_back(to_vector(data)); });
|
||||
this->link.add_on_session_state_callback([this](bool connected) { this->states.push_back(connected); });
|
||||
this->link.setup();
|
||||
}
|
||||
bool send(const Packet &data) { return this->link.send_to_interface(PacketBuffer(data), {}); }
|
||||
|
||||
TestTcp link;
|
||||
std::vector<Packet> received;
|
||||
std::vector<bool> states;
|
||||
};
|
||||
|
||||
class TcpPacketInterfaceTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
App.~Application();
|
||||
new (&App) Application();
|
||||
static char name[] = "tcp-tests";
|
||||
static char friendly_name[] = "";
|
||||
App.pre_setup(name, sizeof(name) - 1, friendly_name, 0);
|
||||
set_time(1000);
|
||||
int fds[2];
|
||||
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
|
||||
this->a.link.link().adopt(std::make_unique<socket::Socket>(fds[0]));
|
||||
this->b.link.link().adopt(std::make_unique<socket::Socket>(fds[1]));
|
||||
this->loops(1);
|
||||
}
|
||||
void TearDown() override {
|
||||
this->a.link.link().close();
|
||||
this->b.link.link().close();
|
||||
App.~Application();
|
||||
new (&App) Application(); // later tests and the test listener use App
|
||||
}
|
||||
static void set_time(uint32_t now) { LoopBlockingGuard guard{nullptr, nullptr, now}; }
|
||||
void loops(int count) {
|
||||
for (int i = 0; i != count; i++) {
|
||||
this->a.link.loop();
|
||||
this->b.link.loop();
|
||||
}
|
||||
}
|
||||
|
||||
End a, b;
|
||||
};
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, ProvidesReliabilitySessionAndAddressing) {
|
||||
for (auto capability : {packet_interface::CAPABILITY_RELIABILITY, packet_interface::CAPABILITY_SESSION,
|
||||
packet_interface::CAPABILITY_ADDRESSING})
|
||||
EXPECT_TRUE(this->a.link.has_capability(capability));
|
||||
EXPECT_EQ(this->a.states, std::vector<bool>{true});
|
||||
EXPECT_TRUE(this->a.link.is_connected());
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, PacketsBothWays) {
|
||||
Packet big(64);
|
||||
for (size_t i = 0; i != big.size(); i++)
|
||||
big[i] = i;
|
||||
ASSERT_TRUE(this->a.send({1, 2, 3}));
|
||||
ASSERT_TRUE(this->a.send(big));
|
||||
ASSERT_TRUE(this->b.send({9}));
|
||||
this->loops(3);
|
||||
EXPECT_EQ(this->b.received, (std::vector<Packet>{{1, 2, 3}, big}));
|
||||
EXPECT_EQ(this->a.received, std::vector<Packet>{{9}});
|
||||
EXPECT_FALSE(this->a.send(Packet(65)));
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, SessionReachesLayersAbove) {
|
||||
UpperLayer upper(&this->a.link);
|
||||
std::vector<bool> states;
|
||||
upper.add_on_session_state_callback([&states](bool connected) { states.push_back(connected); });
|
||||
EXPECT_TRUE(upper.has_capability(packet_interface::CAPABILITY_SESSION));
|
||||
this->b.link.link().close(); // the peer goes away
|
||||
this->loops(3);
|
||||
EXPECT_EQ(states, std::vector<bool>{false});
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, KeepAliveKeepsIdleLinkUp) {
|
||||
for (uint32_t now = 1000; now <= 61000; now += 5000) {
|
||||
set_time(now);
|
||||
this->loops(2);
|
||||
}
|
||||
EXPECT_TRUE(this->a.link.is_connected());
|
||||
EXPECT_TRUE(this->b.link.is_connected());
|
||||
EXPECT_TRUE(this->a.received.empty()); // keep-alives are not passed up
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, SilentPeerClosesLink) {
|
||||
set_time(1000 + 30001);
|
||||
this->a.link.loop(); // b has sent nothing for the default timeout of 30 s
|
||||
EXPECT_FALSE(this->a.link.is_connected());
|
||||
EXPECT_EQ(this->a.states, (std::vector<bool>{true, false}));
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, SlowerPeerMatchedByTimeout) {
|
||||
// b sends keep-alives every 20 s; three of a's own 5 s intervals would close the link
|
||||
this->a.link.set_keep_alive(5000);
|
||||
this->a.link.set_receive_timeout(60000);
|
||||
this->b.link.set_keep_alive(20000);
|
||||
this->b.link.set_receive_timeout(60000);
|
||||
for (uint32_t now = 1000; now <= 121000; now += 5000) {
|
||||
set_time(now);
|
||||
this->loops(2);
|
||||
}
|
||||
EXPECT_TRUE(this->a.link.is_connected());
|
||||
EXPECT_TRUE(this->b.link.is_connected());
|
||||
EXPECT_EQ(this->a.states, std::vector<bool>{true});
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, ConnectedSensorStartsFalse) {
|
||||
TestTcp link;
|
||||
binary_sensor::BinarySensor sensor;
|
||||
link.set_connected_binary_sensor(&sensor);
|
||||
link.setup();
|
||||
EXPECT_TRUE(sensor.has_state());
|
||||
EXPECT_FALSE(sensor.state);
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, OversizedLengthClosesLink) {
|
||||
End c;
|
||||
int fds[2];
|
||||
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
|
||||
c.link.link().adopt(std::make_unique<socket::Socket>(fds[0]));
|
||||
c.link.loop();
|
||||
uint8_t header[2] = {0xFF, 0x00}; // 255 bytes, more than 64
|
||||
ASSERT_EQ(::write(fds[1], header, sizeof(header)), 2);
|
||||
c.link.loop();
|
||||
EXPECT_FALSE(c.link.is_connected());
|
||||
EXPECT_EQ(c.states, (std::vector<bool>{true, false}));
|
||||
::close(fds[1]);
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, NotSentWhileDown) {
|
||||
this->a.link.link().close();
|
||||
this->a.link.loop();
|
||||
EXPECT_FALSE(this->a.send({1}));
|
||||
}
|
||||
|
||||
// A write that fails drops the link and the packet with it, so the packet is reported as not sent
|
||||
TEST_F(TcpPacketInterfaceTest, FailedWriteReportsNotSent) {
|
||||
this->b.link.link().close();
|
||||
EXPECT_FALSE(this->a.send({1}));
|
||||
}
|
||||
|
||||
TEST_F(TcpPacketInterfaceTest, FrameAssembledAcrossReads) {
|
||||
End c;
|
||||
int fds[2];
|
||||
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
|
||||
c.link.link().adopt(std::make_unique<socket::Socket>(fds[0]));
|
||||
c.link.loop();
|
||||
// A keep-alive, then a 3 byte packet in three writes: half the length, the rest of it, then the payload
|
||||
const uint8_t keep_alive[2] = {0x00, 0x00};
|
||||
ASSERT_EQ(::write(fds[1], keep_alive, 2), 2);
|
||||
c.link.loop();
|
||||
const uint8_t length_low = 0x03;
|
||||
ASSERT_EQ(::write(fds[1], &length_low, 1), 1);
|
||||
c.link.loop();
|
||||
const uint8_t length_high_and_start[2] = {0x00, 0xAA};
|
||||
ASSERT_EQ(::write(fds[1], length_high_and_start, 2), 2);
|
||||
c.link.loop();
|
||||
EXPECT_TRUE(c.received.empty());
|
||||
const uint8_t rest[2] = {0xBB, 0xCC};
|
||||
ASSERT_EQ(::write(fds[1], rest, 2), 2);
|
||||
c.link.loop();
|
||||
EXPECT_EQ(c.received, (std::vector<Packet>{{0xAA, 0xBB, 0xCC}}));
|
||||
EXPECT_TRUE(c.link.is_connected());
|
||||
::close(fds[1]);
|
||||
}
|
||||
|
||||
// A peer that sends without a pause is read a few frames at a time, so other components still run
|
||||
TEST_F(TcpPacketInterfaceTest, FramesReadPerLoopAreLimited) {
|
||||
End c;
|
||||
int fds[2];
|
||||
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
|
||||
c.link.link().adopt(std::make_unique<socket::Socket>(fds[0]));
|
||||
c.link.loop();
|
||||
const uint8_t frame[3] = {0x01, 0x00, 0x42};
|
||||
for (int i = 0; i != 10; i++)
|
||||
ASSERT_EQ(::write(fds[1], frame, sizeof(frame)), 3);
|
||||
c.link.loop();
|
||||
EXPECT_EQ(c.received.size(), 8u);
|
||||
c.link.loop();
|
||||
EXPECT_EQ(c.received.size(), 10u);
|
||||
::close(fds[1]);
|
||||
}
|
||||
|
||||
// Once frames wait for a peer that is not reading, the capacity reported is what still fits, so a fragmented packet
|
||||
// is not started only to be cut short
|
||||
TEST_F(TcpPacketInterfaceTest, CapacityFollowsBacklog) {
|
||||
End c;
|
||||
int fds[2];
|
||||
ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
|
||||
c.link.link().adopt(std::make_unique<socket::Socket>(fds[0]));
|
||||
c.link.loop();
|
||||
EXPECT_EQ(c.link.get_send_capacity(), SIZE_MAX);
|
||||
const Packet packet(c.link.get_max_packet_size(), 0x55);
|
||||
for (int i = 0; i != 100000 && c.link.get_send_capacity() == SIZE_MAX; i++)
|
||||
c.send(packet);
|
||||
ASSERT_NE(c.link.get_send_capacity(), SIZE_MAX);
|
||||
EXPECT_LT(c.link.get_send_capacity(), 1024 / (packet.size() + 2) + 1);
|
||||
// Once the peer reads everything, the link takes what is sent again
|
||||
ASSERT_EQ(fcntl(fds[1], F_SETFL, O_NONBLOCK), 0);
|
||||
uint8_t sink[4096];
|
||||
for (int i = 0; i != 1000 && c.link.get_send_capacity() != SIZE_MAX; i++) {
|
||||
while (::read(fds[1], sink, sizeof(sink)) > 0) {
|
||||
}
|
||||
c.link.loop();
|
||||
}
|
||||
EXPECT_EQ(c.link.get_send_capacity(), SIZE_MAX);
|
||||
::close(fds[1]);
|
||||
}
|
||||
|
||||
// Waits in the main loop's select(), which marks the sockets that are ready to read
|
||||
static void wait_for_sockets() { esphome::internal::wakeable_delay(5); }
|
||||
|
||||
// The server role, accepting connections on a loopback port
|
||||
class TcpServerTest : public ::testing::Test {
|
||||
protected:
|
||||
void SetUp() override {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
App.~Application();
|
||||
new (&App) Application();
|
||||
static char name[] = "tcp-tests";
|
||||
static char friendly_name[] = "";
|
||||
App.pre_setup(name, sizeof(name) - 1, friendly_name, 0);
|
||||
set_time(1000);
|
||||
this->port = free_port();
|
||||
this->server.link.set_server(true);
|
||||
this->server.link.set_port(this->port);
|
||||
}
|
||||
void TearDown() override {
|
||||
this->server.link.on_shutdown();
|
||||
for (int fd : this->clients)
|
||||
::close(fd);
|
||||
App.~Application();
|
||||
new (&App) Application(); // later tests and the test listener use App
|
||||
}
|
||||
static void set_time(uint32_t now) { LoopBlockingGuard guard{nullptr, nullptr, now}; }
|
||||
static uint16_t free_port() {
|
||||
int fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
socklen_t len = sizeof(addr);
|
||||
EXPECT_EQ(::bind(fd, reinterpret_cast<struct sockaddr *>(&addr), len), 0);
|
||||
EXPECT_EQ(::getsockname(fd, reinterpret_cast<struct sockaddr *>(&addr), &len), 0);
|
||||
::close(fd);
|
||||
return ntohs(addr.sin_port);
|
||||
}
|
||||
// Connects a client and runs the server's loop until it has accepted it
|
||||
int connect_client() {
|
||||
int fd = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = htons(this->port);
|
||||
EXPECT_EQ(::connect(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(addr)), 0);
|
||||
this->clients.push_back(fd);
|
||||
for (int i = 0; i != 100 && !this->server.link.is_connected(); i++) {
|
||||
this->server.link.loop();
|
||||
wait_for_sockets();
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
End server;
|
||||
uint16_t port{0};
|
||||
std::vector<int> clients;
|
||||
};
|
||||
|
||||
TEST_F(TcpServerTest, AcceptsClientAndReceives) {
|
||||
this->server.link.loop(); // starts listening
|
||||
int client = this->connect_client();
|
||||
ASSERT_TRUE(this->server.link.is_connected());
|
||||
EXPECT_EQ(this->server.states, std::vector<bool>{true});
|
||||
const uint8_t frame[3] = {0x01, 0x00, 0x42};
|
||||
ASSERT_EQ(::write(client, frame, sizeof(frame)), 3);
|
||||
for (int i = 0; i != 100 && this->server.received.empty(); i++) {
|
||||
this->server.link.loop();
|
||||
wait_for_sockets();
|
||||
}
|
||||
EXPECT_EQ(this->server.received, std::vector<Packet>{{0x42}});
|
||||
}
|
||||
|
||||
TEST_F(TcpServerTest, AcceptsNextClientAfterDisconnect) {
|
||||
this->server.link.loop();
|
||||
int first = this->connect_client();
|
||||
ASSERT_TRUE(this->server.link.is_connected());
|
||||
::close(first);
|
||||
this->clients.clear();
|
||||
for (int i = 0; i != 100 && this->server.link.is_connected(); i++) {
|
||||
this->server.link.loop();
|
||||
wait_for_sockets();
|
||||
}
|
||||
EXPECT_EQ(this->server.states, (std::vector<bool>{true, false}));
|
||||
this->connect_client();
|
||||
EXPECT_TRUE(this->server.link.is_connected());
|
||||
EXPECT_EQ(this->server.states, (std::vector<bool>{true, false, true}));
|
||||
}
|
||||
|
||||
} // namespace esphome::tcp::testing
|
||||
|
||||
#endif
|
||||
@@ -1,2 +0,0 @@
|
||||
packages:
|
||||
tcp: !include common.yaml
|
||||
+3
-3
@@ -6,9 +6,9 @@
|
||||
#include <random>
|
||||
#include <string>
|
||||
|
||||
#include "esphome/components/web_server_idf/sse_chunk.h"
|
||||
#include "esphome/components/web_server/sse_chunk.h"
|
||||
|
||||
namespace esphome::web_server_idf::testing {
|
||||
namespace esphome::web_server::testing {
|
||||
|
||||
// The chunk as try_send_nodefer lays it out: header, fields, first "data: ", then the pieces
|
||||
static std::string build_chunk(const char *message, size_t message_len, const char *event, uint32_t id,
|
||||
@@ -128,4 +128,4 @@ TEST(SseChunk, MatchesReferenceOnRandomMessages) {
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server_idf::testing
|
||||
} // namespace esphome::web_server::testing
|
||||
@@ -0,0 +1,300 @@
|
||||
#ifdef USE_HOST
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <sys/uio.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/components/web_server/sse_chunk.h"
|
||||
#include "esphome/components/web_server_base/stream_head.h"
|
||||
|
||||
namespace esphome::web_server::testing {
|
||||
|
||||
using namespace web_server_base; // NOLINT(google-build-using-namespace)
|
||||
|
||||
// Collects every batch write_stream_head() sends
|
||||
struct Capture {
|
||||
std::string bytes;
|
||||
std::vector<int> batch_sizes;
|
||||
int attempts{0};
|
||||
int fail_after_batches{-1};
|
||||
|
||||
bool operator()(struct iovec *iov, int count) {
|
||||
this->attempts++;
|
||||
if (static_cast<int>(this->batch_sizes.size()) == this->fail_after_batches) {
|
||||
return false;
|
||||
}
|
||||
this->batch_sizes.push_back(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
this->bytes.append(static_cast<const char *>(iov[i].iov_base), iov[i].iov_len);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
static std::string build_head(Capture &cap, const char *status, const char *content_type,
|
||||
std::span<const HttpHeader> headers, std::span<const HttpHeader> defaults,
|
||||
const char *body) {
|
||||
EXPECT_TRUE(write_stream_head<struct iovec>(cap, status, content_type, headers, defaults, body));
|
||||
return cap.bytes;
|
||||
}
|
||||
|
||||
// What httpd_resp_send_chunk() put on the wire for its first chunk (esp_http_server/src/httpd_txrx.c in
|
||||
// ESP-IDF 5.5): the chunked head with Transfer-Encoding, each httpd_resp_set_hdr() header, the blank line,
|
||||
// then "%lx\r\n", the chunk data and "\r\n".
|
||||
static std::string reference_send_chunk(const char *status, const char *content_type,
|
||||
const std::vector<HttpHeader> &resp_hdrs, const char *buf, size_t buf_len) {
|
||||
char head[256];
|
||||
std::string out(
|
||||
head, snprintf(head, sizeof(head), "HTTP/1.1 %s\r\nContent-Type: %s\r\nTransfer-Encoding: chunked\r\n", status,
|
||||
content_type));
|
||||
for (const auto &hdr : resp_hdrs) {
|
||||
out.append(hdr.name).append(": ").append(hdr.value).append("\r\n");
|
||||
}
|
||||
out.append("\r\n");
|
||||
char len_str[10];
|
||||
out.append(len_str, snprintf(len_str, sizeof(len_str), "%lx\r\n", static_cast<long>(buf_len)));
|
||||
out.append(buf, buf_len);
|
||||
out.append("\r\n");
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr HttpHeader DEFAULTS[] = {
|
||||
{"Access-Control-Allow-Origin", "*"},
|
||||
{"Access-Control-Allow-Private-Network", "true"},
|
||||
};
|
||||
|
||||
TEST(StreamHead, SseHeadMatchesHttpdRespSendChunk) {
|
||||
// The old event source set Cache-Control and Connection, then the default headers, and sent "\r\n" as a chunk
|
||||
const std::string expected = reference_send_chunk(
|
||||
"200 OK", "text/event-stream",
|
||||
{{"Cache-Control", "no-cache"}, {"Connection", "keep-alive"}, DEFAULTS[0], DEFAULTS[1]}, "\r\n", 2);
|
||||
Capture cap;
|
||||
EXPECT_EQ(build_head(cap, SSE_STATUS, SSE_CONTENT_TYPE, SSE_HEADERS, DEFAULTS, SSE_FIRST_CHUNK), expected);
|
||||
}
|
||||
|
||||
TEST(StreamHead, HeaderReplacesDefaultOfSameName) {
|
||||
// A default the caller also sets, in any case, goes out once with the caller's value
|
||||
static constexpr HttpHeader HEADERS[] = {
|
||||
{"access-control-allow-origin", "http://example.com"},
|
||||
};
|
||||
Capture cap;
|
||||
EXPECT_EQ(build_head(cap, "200 OK", "text/plain", HEADERS, DEFAULTS, nullptr),
|
||||
"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\naccess-control-allow-origin: http://example.com"
|
||||
"\r\nAccess-Control-Allow-Private-Network: true\r\n\r\n");
|
||||
}
|
||||
|
||||
TEST(StreamHead, RawHeadWithCustomHeadersAndNoBody) {
|
||||
static constexpr HttpHeader HEADERS[] = {
|
||||
{"Cache-Control", "no-store"},
|
||||
{"X-Stream", "audio"},
|
||||
};
|
||||
Capture cap;
|
||||
EXPECT_EQ(build_head(cap, "200 OK", "audio/wav", HEADERS, {}, nullptr), "HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: audio/wav\r\n"
|
||||
"Cache-Control: no-store\r\n"
|
||||
"X-Stream: audio\r\n"
|
||||
"\r\n");
|
||||
}
|
||||
|
||||
TEST(StreamHead, BareHead) {
|
||||
Capture cap;
|
||||
EXPECT_EQ(build_head(cap, "204 No Content", "text/plain", {}, {}, nullptr),
|
||||
"HTTP/1.1 204 No Content\r\nContent-Type: text/plain\r\n\r\n");
|
||||
EXPECT_EQ(cap.batch_sizes, std::vector<int>{5});
|
||||
}
|
||||
|
||||
TEST(StreamHead, ManyHeadersSplitIntoBatchesWithSameBytes) {
|
||||
// 4 + 12 * 4 + 2 = 54 pieces, more than two batches hold
|
||||
static constexpr HttpHeader HEADERS[] = {
|
||||
{"X-Header-0", "value-0"}, {"X-Header-1", "value-1"}, {"X-Header-2", "value-2"}, {"X-Header-3", "value-3"},
|
||||
{"X-Header-4", "value-4"}, {"X-Header-5", "value-5"}, {"X-Header-6", "value-6"}, {"X-Header-7", "value-7"},
|
||||
{"X-Header-8", "value-8"}, {"X-Header-9", "value-9"},
|
||||
};
|
||||
const std::span<const HttpHeader> headers{HEADERS};
|
||||
|
||||
Capture batched;
|
||||
const std::string bytes = build_head(batched, "200 OK", "text/plain", headers, DEFAULTS, "body");
|
||||
EXPECT_EQ(batched.batch_sizes, (std::vector<int>{STREAM_HEAD_MAX_IOV, STREAM_HEAD_MAX_IOV, 6}));
|
||||
|
||||
std::string expected = "HTTP/1.1 200 OK\r\nContent-Type: text/plain";
|
||||
const auto append_headers = [&expected](std::span<const HttpHeader> list) {
|
||||
for (const auto &h : list) {
|
||||
expected.append("\r\n").append(h.name).append(": ").append(h.value);
|
||||
}
|
||||
};
|
||||
append_headers(headers);
|
||||
append_headers(DEFAULTS);
|
||||
expected.append("\r\n\r\nbody");
|
||||
EXPECT_EQ(bytes, expected);
|
||||
}
|
||||
|
||||
TEST(StreamHead, BatchBoundary) {
|
||||
// 4 + 4 * 4 + 1 + 1 = 22 pieces fit one batch; a fifth header makes 4 + 5 * 4 = 24 before the blank line
|
||||
static constexpr HttpHeader HEADERS[] = {{"A", "1"}, {"B", "2"}, {"C", "3"}, {"D", "4"}, {"E", "5"}};
|
||||
Capture four;
|
||||
build_head(four, "200 OK", "text/plain", std::span(HEADERS, 4), {}, "x");
|
||||
EXPECT_EQ(four.batch_sizes, std::vector<int>{22});
|
||||
EXPECT_EQ(stream_head_iov_count(4), 22u);
|
||||
Capture five;
|
||||
const std::string bytes = build_head(five, "200 OK", "text/plain", HEADERS, {}, "x");
|
||||
EXPECT_EQ(five.batch_sizes, (std::vector<int>{STREAM_HEAD_MAX_IOV, 2}));
|
||||
EXPECT_EQ(bytes, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nA: 1\r\nB: 2\r\nC: 3\r\nD: 4\r\nE: 5\r\n\r\nx");
|
||||
}
|
||||
|
||||
TEST(StreamHead, FailedSendSkipsLaterBatches) {
|
||||
// Three batches; the second fails, so the third is never sent
|
||||
std::vector<HttpHeader> headers(12, HttpHeader{"X", "y"});
|
||||
Capture full;
|
||||
build_head(full, "200 OK", "text/plain", headers, {}, nullptr);
|
||||
ASSERT_EQ(full.batch_sizes.size(), 3u);
|
||||
Capture cap;
|
||||
cap.fail_after_batches = 1;
|
||||
EXPECT_FALSE(write_stream_head<struct iovec>(cap, "200 OK", "text/plain", headers, {}, nullptr));
|
||||
EXPECT_EQ(cap.attempts, 2);
|
||||
EXPECT_EQ(cap.batch_sizes.size(), 1u);
|
||||
}
|
||||
|
||||
// Three elements of 3, 4 and 5 bytes
|
||||
struct IovFixture {
|
||||
char a[4] = "abc";
|
||||
char b[5] = "defg";
|
||||
char c[6] = "hijkl";
|
||||
struct iovec iov[3] = {{a, 3}, {b, 4}, {c, 5}};
|
||||
struct iovec *cur = iov;
|
||||
int count = 3;
|
||||
|
||||
std::string rest() const {
|
||||
std::string out;
|
||||
for (int i = 0; i < this->count; i++) {
|
||||
out.append(static_cast<const char *>(this->cur[i].iov_base), this->cur[i].iov_len);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(AdvanceIovecs, ZeroLengthWriteChangesNothing) {
|
||||
IovFixture f;
|
||||
advance_iovecs(f.cur, f.count, 0);
|
||||
EXPECT_EQ(f.cur, f.iov);
|
||||
EXPECT_EQ(f.count, 3);
|
||||
EXPECT_EQ(f.rest(), "abcdefghijkl");
|
||||
}
|
||||
|
||||
TEST(AdvanceIovecs, PartialInsideFirstElement) {
|
||||
IovFixture f;
|
||||
advance_iovecs(f.cur, f.count, 2);
|
||||
EXPECT_EQ(f.cur, f.iov);
|
||||
EXPECT_EQ(f.count, 3);
|
||||
EXPECT_EQ(f.cur->iov_len, 1u);
|
||||
EXPECT_EQ(f.rest(), "cdefghijkl");
|
||||
}
|
||||
|
||||
TEST(AdvanceIovecs, ExactlyOnElementBoundary) {
|
||||
IovFixture f;
|
||||
advance_iovecs(f.cur, f.count, 3);
|
||||
EXPECT_EQ(f.cur, &f.iov[1]);
|
||||
EXPECT_EQ(f.count, 2);
|
||||
EXPECT_EQ(f.rest(), "defghijkl");
|
||||
}
|
||||
|
||||
TEST(AdvanceIovecs, AcrossElementBoundaries) {
|
||||
IovFixture f;
|
||||
advance_iovecs(f.cur, f.count, 9);
|
||||
EXPECT_EQ(f.cur, &f.iov[2]);
|
||||
EXPECT_EQ(f.count, 1);
|
||||
EXPECT_EQ(f.rest(), "jkl");
|
||||
}
|
||||
|
||||
TEST(AdvanceIovecs, RepeatedShortWrites) {
|
||||
IovFixture f;
|
||||
std::string sent;
|
||||
while (f.count > 0) {
|
||||
const std::string before = f.rest();
|
||||
advance_iovecs(f.cur, f.count, 2);
|
||||
sent += before.substr(0, before.size() - f.rest().size());
|
||||
}
|
||||
EXPECT_EQ(sent, "abcdefghijkl");
|
||||
}
|
||||
|
||||
TEST(AdvanceIovecs, EverythingWritten) {
|
||||
IovFixture f;
|
||||
advance_iovecs(f.cur, f.count, 12);
|
||||
EXPECT_EQ(f.count, 0);
|
||||
EXPECT_EQ(f.rest(), "");
|
||||
}
|
||||
|
||||
TEST(AdvanceIovecs, SkipsZeroLengthElements) {
|
||||
char a[] = "ab";
|
||||
char c[] = "cd";
|
||||
struct iovec iov[3] = {{a, 2}, {nullptr, 0}, {c, 2}};
|
||||
struct iovec *cur = iov;
|
||||
int count = 3;
|
||||
advance_iovecs(cur, count, 2);
|
||||
EXPECT_EQ(cur, &iov[2]);
|
||||
EXPECT_EQ(count, 1);
|
||||
}
|
||||
|
||||
static constexpr uint32_t TIMEOUT = 20000;
|
||||
|
||||
TEST(StallClock, NotStarted) {
|
||||
StallClock clock;
|
||||
EXPECT_FALSE(clock.is_running());
|
||||
EXPECT_FALSE(clock.is_expired(0, TIMEOUT));
|
||||
EXPECT_FALSE(clock.is_expired(UINT32_MAX, TIMEOUT));
|
||||
}
|
||||
|
||||
TEST(StallClock, ExpiresAtExactlyTheTimeout) {
|
||||
StallClock clock;
|
||||
clock.start(1000);
|
||||
EXPECT_TRUE(clock.is_running());
|
||||
EXPECT_FALSE(clock.is_expired(1000, TIMEOUT));
|
||||
EXPECT_FALSE(clock.is_expired(1000 + TIMEOUT - 1, TIMEOUT));
|
||||
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
|
||||
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT + 1, TIMEOUT));
|
||||
EXPECT_EQ(clock.elapsed(1000 + TIMEOUT), TIMEOUT);
|
||||
}
|
||||
|
||||
TEST(StallClock, RunningClockKeepsItsStart) {
|
||||
StallClock clock;
|
||||
clock.start(1000);
|
||||
clock.start(5000);
|
||||
EXPECT_TRUE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
|
||||
}
|
||||
|
||||
TEST(StallClock, Cleared) {
|
||||
StallClock clock;
|
||||
clock.start(1000);
|
||||
clock.clear();
|
||||
EXPECT_FALSE(clock.is_running());
|
||||
EXPECT_FALSE(clock.is_expired(1000 + TIMEOUT, TIMEOUT));
|
||||
clock.start(50000);
|
||||
EXPECT_FALSE(clock.is_expired(50000 + TIMEOUT - 1, TIMEOUT));
|
||||
EXPECT_TRUE(clock.is_expired(50000 + TIMEOUT, TIMEOUT));
|
||||
}
|
||||
|
||||
TEST(StallClock, StartAtZeroStillRuns) {
|
||||
StallClock clock;
|
||||
clock.start(0);
|
||||
EXPECT_TRUE(clock.is_running());
|
||||
// Recorded as 1, so it expires one millisecond late
|
||||
EXPECT_FALSE(clock.is_expired(TIMEOUT, TIMEOUT));
|
||||
EXPECT_TRUE(clock.is_expired(TIMEOUT + 1, TIMEOUT));
|
||||
}
|
||||
|
||||
TEST(StallClock, AcrossMillisWraparound) {
|
||||
StallClock clock;
|
||||
const uint32_t start = UINT32_MAX - 5000;
|
||||
clock.start(start);
|
||||
EXPECT_FALSE(clock.is_expired(UINT32_MAX, TIMEOUT));
|
||||
EXPECT_FALSE(clock.is_expired(start + TIMEOUT - 1, TIMEOUT)); // wrapped to 14998
|
||||
EXPECT_TRUE(clock.is_expired(start + TIMEOUT, TIMEOUT));
|
||||
EXPECT_EQ(clock.elapsed(start + TIMEOUT), TIMEOUT);
|
||||
}
|
||||
|
||||
} // namespace esphome::web_server::testing
|
||||
|
||||
#endif // USE_HOST
|
||||
@@ -1,234 +1,230 @@
|
||||
{
|
||||
"tests/integration/test_action_concurrent_reentry.py": 6.84,
|
||||
"tests/integration/test_addressable_light_transition.py": 4.79,
|
||||
"tests/integration/test_alarm_control_panel_state_transitions.py": 5.63,
|
||||
"tests/integration/test_api_action_metadata.py": 6.5,
|
||||
"tests/integration/test_api_action_responses.py": 5.15,
|
||||
"tests/integration/test_api_action_timeout.py": 7.0,
|
||||
"tests/integration/test_api_conditional_memory.py": 3.27,
|
||||
"tests/integration/test_api_cover_toggle.py": 4.89,
|
||||
"tests/integration/test_api_custom_services.py": 3.6,
|
||||
"tests/integration/test_api_decode_wire_types.py": 6.57,
|
||||
"tests/integration/test_api_empty_message_roundtrip.py": 3.25,
|
||||
"tests/integration/test_api_encode_boundaries.py": 3.9,
|
||||
"tests/integration/test_api_event_order_backpressure.py": 4.42,
|
||||
"tests/integration/test_api_get_time_response_timezone.py": 4.41,
|
||||
"tests/integration/test_api_homeassistant.py": 4.71,
|
||||
"tests/integration/test_api_homeassistant_action_no_subscriber.py": 3.56,
|
||||
"tests/integration/test_api_homeassistant_binary_sensor_initial_state.py": 3.61,
|
||||
"tests/integration/test_api_homeassistant_entities.py": 3.57,
|
||||
"tests/integration/test_api_homeassistant_light.py": 26.77,
|
||||
"tests/integration/test_api_list_entities_backpressure.py": 4.79,
|
||||
"tests/integration/test_api_message_size_batching.py": 2.95,
|
||||
"tests/integration/test_api_outgoing_connection.py": 27.8,
|
||||
"tests/integration/test_api_reboot_timeout.py": 2.92,
|
||||
"tests/integration/test_api_string_lambda.py": 3.79,
|
||||
"tests/integration/test_api_vv_logging.py": 3.85,
|
||||
"tests/integration/test_api_zero_psk_provisioning.py": 20.97,
|
||||
"tests/integration/test_areas_and_devices.py": 4.44,
|
||||
"tests/integration/test_automation_merged_actions.py": 4.18,
|
||||
"tests/integration/test_automation_wait_actions.py": 3.88,
|
||||
"tests/integration/test_automations.py": 7.54,
|
||||
"tests/integration/test_batch_delay_zero_rapid_transitions.py": 4.99,
|
||||
"tests/integration/test_binary_sensor_autorepeat_filter.py": 3.73,
|
||||
"tests/integration/test_binary_sensor_delay_filters.py": 5.07,
|
||||
"tests/integration/test_binary_sensor_invalidate_state.py": 2.74,
|
||||
"tests/integration/test_blocking_warning_log_time_not_charged_to_next_operation.py": 3.75,
|
||||
"tests/integration/test_branch_resume_async.py": 7.19,
|
||||
"tests/integration/test_build_info.py": 3.22,
|
||||
"tests/integration/test_camera_mock.py": 3.34,
|
||||
"tests/integration/test_canbus_on_frame.py": 3.37,
|
||||
"tests/integration/test_climate_control_action.py": 4.14,
|
||||
"tests/integration/test_climate_custom_modes.py": 3.94,
|
||||
"tests/integration/test_continuation_actions.py": 3.66,
|
||||
"tests/integration/test_copy_cover_toggle.py": 3.44,
|
||||
"tests/integration/test_copy_entity_state.py": 3.42,
|
||||
"tests/integration/test_copy_initially_off.py": 3.05,
|
||||
"tests/integration/test_copy_lock_open.py": 3.47,
|
||||
"tests/integration/test_core_automation_order.py": 3.2,
|
||||
"tests/integration/test_cover_control_action.py": 4.96,
|
||||
"tests/integration/test_cover_position_action_cancel.py": 11.43,
|
||||
"tests/integration/test_cover_stop_tilt_cancel.py": 7.2,
|
||||
"tests/integration/test_crc8_helper.py": 3.06,
|
||||
"tests/integration/test_device_id_in_state.py": 4.38,
|
||||
"tests/integration/test_duplicate_entities.py": 3.28,
|
||||
"tests/integration/test_entity_icon.py": 3.39,
|
||||
"tests/integration/test_entity_missing_state.py": 3.32,
|
||||
"tests/integration/test_fan_turn_on_action.py": 3.68,
|
||||
"tests/integration/test_fnv1_hash_object_id.py": 3.4,
|
||||
"tests/integration/test_fnv1a_hash.py": 2.76,
|
||||
"tests/integration/test_gpio_expander_cache.py": 3.07,
|
||||
"tests/integration/test_host_logger_thread_safety.py": 3.77,
|
||||
"tests/integration/test_host_mode_basic.py": 3.2,
|
||||
"tests/integration/test_host_mode_batch_delay.py": 3.02,
|
||||
"tests/integration/test_host_mode_climate_basic_state.py": 4.6,
|
||||
"tests/integration/test_host_mode_climate_control.py": 3.61,
|
||||
"tests/integration/test_host_mode_empty_string_options.py": 3.51,
|
||||
"tests/integration/test_host_mode_entity_fields.py": 3.14,
|
||||
"tests/integration/test_host_mode_fan_preset.py": 5.18,
|
||||
"tests/integration/test_host_mode_many_entities.py": 4.05,
|
||||
"tests/integration/test_host_mode_many_entities_multiple_connections.py": 3.38,
|
||||
"tests/integration/test_host_mode_noise_encryption.py": 7.82,
|
||||
"tests/integration/test_host_mode_reconnect.py": 2.85,
|
||||
"tests/integration/test_host_mode_sensor.py": 3.58,
|
||||
"tests/integration/test_host_ota.py": 79.09,
|
||||
"tests/integration/test_host_preferences.py": 2.95,
|
||||
"tests/integration/test_host_preferences_suspend_resume.py": 5.49,
|
||||
"tests/integration/test_host_write_after_peer_close.py": 3.37,
|
||||
"tests/integration/test_improv_serial_uart.py": 4.16,
|
||||
"tests/integration/test_in_range_conditions.py": 3.51,
|
||||
"tests/integration/test_includes_lambda_order.py": 2.97,
|
||||
"tests/integration/test_interval_startup_delay.py": 4.05,
|
||||
"tests/integration/test_ir_rf_transmit_complete.py": 4.25,
|
||||
"tests/integration/test_large_message_batching.py": 3.58,
|
||||
"tests/integration/test_legacy_area.py": 2.62,
|
||||
"tests/integration/test_light_automations.py": 3.91,
|
||||
"tests/integration/test_light_binary_effect_off_phase.py": 9.47,
|
||||
"tests/integration/test_light_calls.py": 8.37,
|
||||
"tests/integration/test_light_constant_brightness.py": 5.31,
|
||||
"tests/integration/test_light_control_action.py": 4.64,
|
||||
"tests/integration/test_light_dim_relative_action.py": 4.44,
|
||||
"tests/integration/test_light_effect_cycle_action.py": 4.4,
|
||||
"tests/integration/test_light_effect_zero_brightness.py": 5.45,
|
||||
"tests/integration/test_light_initial_state.py": 3.49,
|
||||
"tests/integration/test_light_repeat_effect.py": 12.46,
|
||||
"tests/integration/test_light_restore_state.py": 3.63,
|
||||
"tests/integration/test_light_resume_effect.py": 5.85,
|
||||
"tests/integration/test_light_toggle_action.py": 3.66,
|
||||
"tests/integration/test_light_transition_state_publish_interval.py": 18.86,
|
||||
"tests/integration/test_lock_automations.py": 2.93,
|
||||
"tests/integration/test_logger_buffered_recursion_guard.py": 3.17,
|
||||
"tests/integration/test_loop_disable_enable.py": 3.68,
|
||||
"tests/integration/test_loop_failed_before_setup.py": 2.88,
|
||||
"tests/integration/test_loop_interval_decoupling.py": 6.29,
|
||||
"tests/integration/test_loop_interval_default_not_pulled_forward.py": 5.71,
|
||||
"tests/integration/test_lvgl_color_picker_render.py": 82.81,
|
||||
"tests/integration/test_lvgl_headless_render.py": 72.6,
|
||||
"tests/integration/test_micros_to_millis.py": 3.01,
|
||||
"tests/integration/test_multi_click_trigger.py": 7.11,
|
||||
"tests/integration/test_multi_device_preferences.py": 3.74,
|
||||
"tests/integration/test_noise_encryption_key_protection.py": 6.03,
|
||||
"tests/integration/test_object_id_api_verification.py": 2.83,
|
||||
"tests/integration/test_object_id_friendly_name_no_mac_suffix.py": 3.02,
|
||||
"tests/integration/test_object_id_no_friendly_name.py": 6.9,
|
||||
"tests/integration/test_online_image_auto_detects_image_bmp_mime.py": 4.69,
|
||||
"tests/integration/test_online_image_auto_detects_redirected_image_bmp_mime.py": 10.89,
|
||||
"tests/integration/test_online_image_bmp.py": 5.16,
|
||||
"tests/integration/test_online_image_stalled_download.py": 5.18,
|
||||
"tests/integration/test_oversized_payloads.py": 17.75,
|
||||
"tests/integration/test_preference_key_stability.py": 3.94,
|
||||
"tests/integration/test_reboot_hook_order.py": 8.25,
|
||||
"tests/integration/test_runtime_stats.py": 5.26,
|
||||
"tests/integration/test_safe_mode_loop_runs.py": 3.08,
|
||||
"tests/integration/test_scheduler_background_wake.py": 8.49,
|
||||
"tests/integration/test_scheduler_blocking_warning.py": 10.27,
|
||||
"tests/integration/test_scheduler_bulk_cleanup.py": 3.12,
|
||||
"tests/integration/test_scheduler_defer_cancel.py": 3.62,
|
||||
"tests/integration/test_scheduler_defer_cancel_regular.py": 2.99,
|
||||
"tests/integration/test_scheduler_defer_fifo_simple.py": 2.89,
|
||||
"tests/integration/test_scheduler_defer_stress.py": 3.31,
|
||||
"tests/integration/test_scheduler_heap_stress.py": 3.0,
|
||||
"tests/integration/test_scheduler_internal_id_no_collision.py": 4.28,
|
||||
"tests/integration/test_scheduler_interval_reschedule.py": 7.2,
|
||||
"tests/integration/test_scheduler_interval_zero_coerced.py": 3.3,
|
||||
"tests/integration/test_scheduler_loop_phase_timeout.py": 8.31,
|
||||
"tests/integration/test_scheduler_null_name.py": 2.81,
|
||||
"tests/integration/test_scheduler_numeric_id_test.py": 4.23,
|
||||
"tests/integration/test_scheduler_pool.py": 4.72,
|
||||
"tests/integration/test_scheduler_rapid_cancellation.py": 3.77,
|
||||
"tests/integration/test_scheduler_recursive_timeout.py": 3.21,
|
||||
"tests/integration/test_scheduler_removed_item_race.py": 3.55,
|
||||
"tests/integration/test_scheduler_self_keyed.py": 4.28,
|
||||
"tests/integration/test_scheduler_simultaneous_callbacks.py": 3.4,
|
||||
"tests/integration/test_scheduler_string_test.py": 5.27,
|
||||
"tests/integration/test_scheduler_verbose_log_on_message.py": 3.16,
|
||||
"tests/integration/test_script_array_params.py": 3.54,
|
||||
"tests/integration/test_script_delay_params.py": 3.63,
|
||||
"tests/integration/test_script_on_stop_and_choose.py": 3.88,
|
||||
"tests/integration/test_script_queued.py": 5.6,
|
||||
"tests/integration/test_script_queued_idle_loop.py": 3.28,
|
||||
"tests/integration/test_script_synchronous.py": 4.17,
|
||||
"tests/integration/test_script_wait_on_boot.py": 2.97,
|
||||
"tests/integration/test_sdl_headless_screenshot.py": 10.29,
|
||||
"tests/integration/test_select_stringref_trigger.py": 3.29,
|
||||
"tests/integration/test_sensor_filters_apply.py": 4.08,
|
||||
"tests/integration/test_sensor_filters_delta.py": 4.13,
|
||||
"tests/integration/test_sensor_filters_rh_correction.py": 3.54,
|
||||
"tests/integration/test_sensor_filters_ring_buffer.py": 3.77,
|
||||
"tests/integration/test_sensor_filters_sliding_window.py": 9.43,
|
||||
"tests/integration/test_sensor_filters_value_list.py": 5.0,
|
||||
"tests/integration/test_sensor_raw_state.py": 6.81,
|
||||
"tests/integration/test_sensor_timeout_filter.py": 4.18,
|
||||
"tests/integration/test_sensor_timeout_throttle_filter.py": 5.85,
|
||||
"tests/integration/test_serial_proxy_backpressure.py": 6.22,
|
||||
"tests/integration/test_set_internal_at_boot.py": 3.59,
|
||||
"tests/integration/test_snapshot_display.py": 11.35,
|
||||
"tests/integration/test_socket_ipv4_resolve.py": 2.69,
|
||||
"tests/integration/test_socket_tcp_client_link.py": 3.84,
|
||||
"tests/integration/test_socket_wake_gate_tcp.py": 6.37,
|
||||
"tests/integration/test_socket_wake_gate_udp.py": 3.02,
|
||||
"tests/integration/test_source_entity_follow.py": 4.63,
|
||||
"tests/integration/test_status_flags.py": 8.6,
|
||||
"tests/integration/test_status_momentary.py": 6.18,
|
||||
"tests/integration/test_store_yaml_recovery.py": 21.71,
|
||||
"tests/integration/test_strftime_to.py": 3.69,
|
||||
"tests/integration/test_syslog.py": 7.12,
|
||||
"tests/integration/test_tcp_uart_bridge.py": 6.76,
|
||||
"tests/integration/test_tcp_uart_server.py": 2.75,
|
||||
"tests/integration/test_template_alarm_control_panel_many_sensors.py": 3.46,
|
||||
"tests/integration/test_template_climate_basic.py": 3.36,
|
||||
"tests/integration/test_template_climate_custom_modes.py": 4.52,
|
||||
"tests/integration/test_template_climate_nonoptimistic.py": 3.76,
|
||||
"tests/integration/test_template_climate_on_control_ordering.py": 4.25,
|
||||
"tests/integration/test_template_climate_publish_all_fields.py": 3.33,
|
||||
"tests/integration/test_template_climate_sensor_push.py": 3.66,
|
||||
"tests/integration/test_template_climate_set_actions.py": 3.28,
|
||||
"tests/integration/test_template_climate_two_point_temperature.py": 2.98,
|
||||
"tests/integration/test_template_text_save.py": 4.83,
|
||||
"tests/integration/test_template_time.py": 3.96,
|
||||
"tests/integration/test_text_command.py": 2.71,
|
||||
"tests/integration/test_text_sensor_raw_state.py": 4.22,
|
||||
"tests/integration/test_uart_mock_bl0906_stray_rx.py": 3.7,
|
||||
"tests/integration/test_uart_mock_dfrobot_sen0395.py": 4.63,
|
||||
"tests/integration/test_uart_mock_dfrobot_sen0465.py": 10.02,
|
||||
"tests/integration/test_uart_mock_dfrobot_sen0609.py": 4.61,
|
||||
"tests/integration/test_uart_mock_duco.py": 25.59,
|
||||
"tests/integration/test_uart_mock_ld2410.py": 17.8,
|
||||
"tests/integration/test_uart_mock_ld2410_query_ack.py": 4.4,
|
||||
"tests/integration/test_uart_mock_ld2410_selects.py": 3.63,
|
||||
"tests/integration/test_uart_mock_ld2410s.py": 31.84,
|
||||
"tests/integration/test_uart_mock_ld2412.py": 48.32,
|
||||
"tests/integration/test_uart_mock_ld2420.py": 12.7,
|
||||
"tests/integration/test_uart_mock_ld2420_config_exit.py": 36.34,
|
||||
"tests/integration/test_uart_mock_ld2420_select_mode.py": 11.81,
|
||||
"tests/integration/test_uart_mock_ld2450.py": 4.54,
|
||||
"tests/integration/test_uart_mock_ld2450_has_target_only.py": 5.65,
|
||||
"tests/integration/test_uart_mock_ld2450_link_loss.py": 29.62,
|
||||
"tests/integration/test_uart_mock_ld2450_polygon_zone_restore.py": 4.18,
|
||||
"tests/integration/test_uart_mock_ld2450_polygon_zone_timeout.py": 5.08,
|
||||
"tests/integration/test_uart_mock_ld2450_polygon_zones.py": 5.17,
|
||||
"tests/integration/test_uart_mock_ld2450_target_xy.py": 5.73,
|
||||
"tests/integration/test_uart_mock_ld2450_target_xy_unfiltered.py": 26.14,
|
||||
"tests/integration/test_uart_mock_ld2450_target_xy_unfiltered_raw_press.py": 3.56,
|
||||
"tests/integration/test_uart_mock_ld2460.py": 4.17,
|
||||
"tests/integration/test_uart_mock_modbus.py": 112.87,
|
||||
"tests/integration/test_uart_mock_pipsolar_switches.py": 4.39,
|
||||
"tests/integration/test_uart_mock_pylontech.py": 3.97,
|
||||
"tests/integration/test_uart_mock_sim800l_callbacks.py": 3.39,
|
||||
"tests/integration/test_uart_mock_ufm01.py": 56.92,
|
||||
"tests/integration/test_uart_tcp_bridge.py": 3.88,
|
||||
"tests/integration/test_udp.py": 4.73,
|
||||
"tests/integration/test_udp_ipv6.py": 3.29,
|
||||
"tests/integration/test_udp_multicast.py": 6.28,
|
||||
"tests/integration/test_use_address_runtime.py": 7.3,
|
||||
"tests/integration/test_valve_control_action.py": 3.92,
|
||||
"tests/integration/test_varint_five_byte_device_id.py": 3.4,
|
||||
"tests/integration/test_vnc_client.py": 17.8,
|
||||
"tests/integration/test_vnc_client_auth.py": 23.37,
|
||||
"tests/integration/test_wait_until_mid_loop_timing.py": 3.79,
|
||||
"tests/integration/test_wait_until_on_boot.py": 4.0,
|
||||
"tests/integration/test_wait_until_ordering.py": 3.68,
|
||||
"tests/integration/test_wait_until_reentrant_restart.py": 5.43,
|
||||
"tests/integration/test_wake_loop_forces_phase_b.py": 4.03,
|
||||
"tests/integration/test_water_heater_template.py": 9.07
|
||||
"tests/integration/test_action_concurrent_reentry.py": 10.97,
|
||||
"tests/integration/test_addressable_light_transition.py": 12.18,
|
||||
"tests/integration/test_alarm_control_panel_state_transitions.py": 12.65,
|
||||
"tests/integration/test_api_action_metadata.py": 10.16,
|
||||
"tests/integration/test_api_action_responses.py": 15.49,
|
||||
"tests/integration/test_api_action_timeout.py": 14.17,
|
||||
"tests/integration/test_api_conditional_memory.py": 10.47,
|
||||
"tests/integration/test_api_cover_toggle.py": 11.06,
|
||||
"tests/integration/test_api_custom_services.py": 9.09,
|
||||
"tests/integration/test_api_decode_wire_types.py": 12.55,
|
||||
"tests/integration/test_api_empty_message_roundtrip.py": 11.24,
|
||||
"tests/integration/test_api_encode_boundaries.py": 12.26,
|
||||
"tests/integration/test_api_event_order_backpressure.py": 9.95,
|
||||
"tests/integration/test_api_get_time_response_timezone.py": 11.59,
|
||||
"tests/integration/test_api_homeassistant.py": 13.82,
|
||||
"tests/integration/test_api_homeassistant_action_no_subscriber.py": 9.5,
|
||||
"tests/integration/test_api_homeassistant_binary_sensor_initial_state.py": 9.11,
|
||||
"tests/integration/test_api_homeassistant_entities.py": 9.46,
|
||||
"tests/integration/test_api_homeassistant_light.py": 35.6,
|
||||
"tests/integration/test_api_list_entities_backpressure.py": 12.99,
|
||||
"tests/integration/test_api_message_size_batching.py": 8.92,
|
||||
"tests/integration/test_api_outgoing_connection.py": 21.77,
|
||||
"tests/integration/test_api_reboot_timeout.py": 11.09,
|
||||
"tests/integration/test_api_string_lambda.py": 9.35,
|
||||
"tests/integration/test_api_vv_logging.py": 7.54,
|
||||
"tests/integration/test_api_zero_psk_provisioning.py": 19.69,
|
||||
"tests/integration/test_areas_and_devices.py": 12.79,
|
||||
"tests/integration/test_automation_merged_actions.py": 12.57,
|
||||
"tests/integration/test_automation_wait_actions.py": 7.68,
|
||||
"tests/integration/test_automations.py": 22.18,
|
||||
"tests/integration/test_batch_delay_zero_rapid_transitions.py": 13.59,
|
||||
"tests/integration/test_binary_sensor_autorepeat_filter.py": 10.51,
|
||||
"tests/integration/test_binary_sensor_delay_filters.py": 12.32,
|
||||
"tests/integration/test_binary_sensor_invalidate_state.py": 7.91,
|
||||
"tests/integration/test_blocking_warning_log_time_not_charged_to_next_operation.py": 8.66,
|
||||
"tests/integration/test_branch_resume_async.py": 13.11,
|
||||
"tests/integration/test_build_info.py": 10.18,
|
||||
"tests/integration/test_camera_mock.py": 8.88,
|
||||
"tests/integration/test_canbus_on_frame.py": 11.18,
|
||||
"tests/integration/test_climate_control_action.py": 11.73,
|
||||
"tests/integration/test_climate_custom_modes.py": 9.79,
|
||||
"tests/integration/test_continuation_actions.py": 9.17,
|
||||
"tests/integration/test_copy_cover_toggle.py": 8.78,
|
||||
"tests/integration/test_copy_entity_state.py": 8.73,
|
||||
"tests/integration/test_copy_initially_off.py": 8.45,
|
||||
"tests/integration/test_copy_lock_open.py": 11.4,
|
||||
"tests/integration/test_core_automation_order.py": 9.56,
|
||||
"tests/integration/test_cover_control_action.py": 10.08,
|
||||
"tests/integration/test_cover_position_action_cancel.py": 16.82,
|
||||
"tests/integration/test_cover_stop_tilt_cancel.py": 12.4,
|
||||
"tests/integration/test_crc8_helper.py": 8.45,
|
||||
"tests/integration/test_device_id_in_state.py": 16.78,
|
||||
"tests/integration/test_duplicate_entities.py": 10.71,
|
||||
"tests/integration/test_entity_icon.py": 10.79,
|
||||
"tests/integration/test_entity_missing_state.py": 10.32,
|
||||
"tests/integration/test_fan_turn_on_action.py": 10.68,
|
||||
"tests/integration/test_fnv1_hash_object_id.py": 7.85,
|
||||
"tests/integration/test_fnv1a_hash.py": 10.71,
|
||||
"tests/integration/test_gpio_expander_cache.py": 6.86,
|
||||
"tests/integration/test_host_logger_thread_safety.py": 8.82,
|
||||
"tests/integration/test_host_mode_basic.py": 7.93,
|
||||
"tests/integration/test_host_mode_batch_delay.py": 9.54,
|
||||
"tests/integration/test_host_mode_climate_basic_state.py": 9.48,
|
||||
"tests/integration/test_host_mode_climate_control.py": 11.44,
|
||||
"tests/integration/test_host_mode_empty_string_options.py": 9.3,
|
||||
"tests/integration/test_host_mode_entity_fields.py": 11.0,
|
||||
"tests/integration/test_host_mode_fan_preset.py": 12.9,
|
||||
"tests/integration/test_host_mode_many_entities.py": 19.93,
|
||||
"tests/integration/test_host_mode_many_entities_multiple_connections.py": 10.24,
|
||||
"tests/integration/test_host_mode_noise_encryption.py": 20.34,
|
||||
"tests/integration/test_host_mode_reconnect.py": 8.4,
|
||||
"tests/integration/test_host_mode_sensor.py": 9.84,
|
||||
"tests/integration/test_host_ota.py": 106.68,
|
||||
"tests/integration/test_host_preferences.py": 9.21,
|
||||
"tests/integration/test_host_preferences_suspend_resume.py": 12.71,
|
||||
"tests/integration/test_host_write_after_peer_close.py": 8.15,
|
||||
"tests/integration/test_improv_serial_uart.py": 10.83,
|
||||
"tests/integration/test_in_range_conditions.py": 9.25,
|
||||
"tests/integration/test_includes_lambda_order.py": 9.19,
|
||||
"tests/integration/test_interval_startup_delay.py": 10.02,
|
||||
"tests/integration/test_ir_rf_transmit_complete.py": 14.13,
|
||||
"tests/integration/test_large_message_batching.py": 8.48,
|
||||
"tests/integration/test_legacy_area.py": 8.56,
|
||||
"tests/integration/test_light_automations.py": 10.6,
|
||||
"tests/integration/test_light_binary_effect_off_phase.py": 15.66,
|
||||
"tests/integration/test_light_calls.py": 15.34,
|
||||
"tests/integration/test_light_constant_brightness.py": 10.13,
|
||||
"tests/integration/test_light_control_action.py": 9.62,
|
||||
"tests/integration/test_light_dim_relative_action.py": 10.79,
|
||||
"tests/integration/test_light_effect_cycle_action.py": 11.64,
|
||||
"tests/integration/test_light_effect_zero_brightness.py": 12.02,
|
||||
"tests/integration/test_light_initial_state.py": 13.36,
|
||||
"tests/integration/test_light_repeat_effect.py": 20.4,
|
||||
"tests/integration/test_light_restore_state.py": 4.1,
|
||||
"tests/integration/test_light_resume_effect.py": 12.94,
|
||||
"tests/integration/test_light_toggle_action.py": 9.85,
|
||||
"tests/integration/test_light_transition_state_publish_interval.py": 40.3,
|
||||
"tests/integration/test_lock_automations.py": 9.37,
|
||||
"tests/integration/test_logger_buffered_recursion_guard.py": 10.38,
|
||||
"tests/integration/test_loop_disable_enable.py": 8.13,
|
||||
"tests/integration/test_loop_failed_before_setup.py": 9.22,
|
||||
"tests/integration/test_loop_interval_decoupling.py": 13.71,
|
||||
"tests/integration/test_loop_interval_default_not_pulled_forward.py": 14.17,
|
||||
"tests/integration/test_lvgl_color_picker_render.py": 80.88,
|
||||
"tests/integration/test_lvgl_headless_render.py": 74.94,
|
||||
"tests/integration/test_micros_to_millis.py": 9.88,
|
||||
"tests/integration/test_multi_click_trigger.py": 8.97,
|
||||
"tests/integration/test_multi_device_preferences.py": 10.2,
|
||||
"tests/integration/test_noise_encryption_key_protection.py": 16.06,
|
||||
"tests/integration/test_object_id_api_verification.py": 11.47,
|
||||
"tests/integration/test_object_id_friendly_name_no_mac_suffix.py": 13.0,
|
||||
"tests/integration/test_object_id_no_friendly_name.py": 15.04,
|
||||
"tests/integration/test_online_image_auto_detects_image_bmp_mime.py": 21.82,
|
||||
"tests/integration/test_online_image_auto_detects_redirected_image_bmp_mime.py": 17.86,
|
||||
"tests/integration/test_online_image_bmp.py": 16.2,
|
||||
"tests/integration/test_online_image_stalled_download.py": 15.45,
|
||||
"tests/integration/test_oversized_payloads.py": 29.9,
|
||||
"tests/integration/test_preference_key_stability.py": 10.4,
|
||||
"tests/integration/test_runtime_stats.py": 10.76,
|
||||
"tests/integration/test_safe_mode_loop_runs.py": 6.19,
|
||||
"tests/integration/test_scheduler_background_wake.py": 12.87,
|
||||
"tests/integration/test_scheduler_blocking_warning.py": 19.98,
|
||||
"tests/integration/test_scheduler_bulk_cleanup.py": 11.54,
|
||||
"tests/integration/test_scheduler_defer_cancel.py": 11.15,
|
||||
"tests/integration/test_scheduler_defer_cancel_regular.py": 8.25,
|
||||
"tests/integration/test_scheduler_defer_fifo_simple.py": 9.99,
|
||||
"tests/integration/test_scheduler_defer_stress.py": 9.2,
|
||||
"tests/integration/test_scheduler_heap_stress.py": 4.01,
|
||||
"tests/integration/test_scheduler_internal_id_no_collision.py": 8.82,
|
||||
"tests/integration/test_scheduler_interval_reschedule.py": 11.73,
|
||||
"tests/integration/test_scheduler_interval_zero_coerced.py": 9.65,
|
||||
"tests/integration/test_scheduler_loop_phase_timeout.py": 13.05,
|
||||
"tests/integration/test_scheduler_null_name.py": 3.48,
|
||||
"tests/integration/test_scheduler_numeric_id_test.py": 13.02,
|
||||
"tests/integration/test_scheduler_pool.py": 12.01,
|
||||
"tests/integration/test_scheduler_rapid_cancellation.py": 3.52,
|
||||
"tests/integration/test_scheduler_recursive_timeout.py": 3.88,
|
||||
"tests/integration/test_scheduler_removed_item_race.py": 12.1,
|
||||
"tests/integration/test_scheduler_self_keyed.py": 4.9,
|
||||
"tests/integration/test_scheduler_simultaneous_callbacks.py": 8.85,
|
||||
"tests/integration/test_scheduler_string_test.py": 11.49,
|
||||
"tests/integration/test_scheduler_verbose_log_on_message.py": 10.96,
|
||||
"tests/integration/test_script_array_params.py": 8.29,
|
||||
"tests/integration/test_script_delay_params.py": 4.37,
|
||||
"tests/integration/test_script_on_stop_and_choose.py": 11.33,
|
||||
"tests/integration/test_script_queued.py": 12.76,
|
||||
"tests/integration/test_script_queued_idle_loop.py": 8.03,
|
||||
"tests/integration/test_script_synchronous.py": 8.15,
|
||||
"tests/integration/test_script_wait_on_boot.py": 7.95,
|
||||
"tests/integration/test_sdl_headless_screenshot.py": 13.67,
|
||||
"tests/integration/test_select_stringref_trigger.py": 11.51,
|
||||
"tests/integration/test_sensor_filters_apply.py": 9.04,
|
||||
"tests/integration/test_sensor_filters_delta.py": 8.84,
|
||||
"tests/integration/test_sensor_filters_rh_correction.py": 11.57,
|
||||
"tests/integration/test_sensor_filters_ring_buffer.py": 10.91,
|
||||
"tests/integration/test_sensor_filters_sliding_window.py": 27.62,
|
||||
"tests/integration/test_sensor_filters_value_list.py": 10.87,
|
||||
"tests/integration/test_sensor_raw_state.py": 15.83,
|
||||
"tests/integration/test_sensor_timeout_filter.py": 9.98,
|
||||
"tests/integration/test_sensor_timeout_throttle_filter.py": 11.36,
|
||||
"tests/integration/test_set_internal_at_boot.py": 8.34,
|
||||
"tests/integration/test_snapshot_display.py": 11.19,
|
||||
"tests/integration/test_socket_ipv4_resolve.py": 9.59,
|
||||
"tests/integration/test_socket_tcp_client_link.py": 8.12,
|
||||
"tests/integration/test_socket_wake_gate_tcp.py": 11.4,
|
||||
"tests/integration/test_socket_wake_gate_udp.py": 10.33,
|
||||
"tests/integration/test_source_entity_follow.py": 10.25,
|
||||
"tests/integration/test_status_flags.py": 15.81,
|
||||
"tests/integration/test_status_momentary.py": 15.1,
|
||||
"tests/integration/test_store_yaml_recovery.py": 16.16,
|
||||
"tests/integration/test_strftime_to.py": 11.89,
|
||||
"tests/integration/test_syslog.py": 15.01,
|
||||
"tests/integration/test_tcp_uart_bridge.py": 13.24,
|
||||
"tests/integration/test_tcp_uart_server.py": 11.94,
|
||||
"tests/integration/test_template_alarm_control_panel_many_sensors.py": 9.41,
|
||||
"tests/integration/test_template_climate_basic.py": 9.35,
|
||||
"tests/integration/test_template_climate_custom_modes.py": 7.29,
|
||||
"tests/integration/test_template_climate_nonoptimistic.py": 10.02,
|
||||
"tests/integration/test_template_climate_on_control_ordering.py": 8.62,
|
||||
"tests/integration/test_template_climate_publish_all_fields.py": 9.65,
|
||||
"tests/integration/test_template_climate_sensor_push.py": 9.79,
|
||||
"tests/integration/test_template_climate_set_actions.py": 9.7,
|
||||
"tests/integration/test_template_climate_two_point_temperature.py": 9.82,
|
||||
"tests/integration/test_template_text_save.py": 12.72,
|
||||
"tests/integration/test_template_time.py": 7.72,
|
||||
"tests/integration/test_text_command.py": 10.2,
|
||||
"tests/integration/test_text_sensor_raw_state.py": 11.06,
|
||||
"tests/integration/test_uart_mock_bl0906_stray_rx.py": 9.79,
|
||||
"tests/integration/test_uart_mock_dfrobot_sen0395.py": 12.43,
|
||||
"tests/integration/test_uart_mock_dfrobot_sen0465.py": 18.59,
|
||||
"tests/integration/test_uart_mock_dfrobot_sen0609.py": 15.46,
|
||||
"tests/integration/test_uart_mock_duco.py": 32.44,
|
||||
"tests/integration/test_uart_mock_ld2410.py": 29.35,
|
||||
"tests/integration/test_uart_mock_ld2410_query_ack.py": 11.05,
|
||||
"tests/integration/test_uart_mock_ld2410_selects.py": 9.11,
|
||||
"tests/integration/test_uart_mock_ld2410s.py": 56.59,
|
||||
"tests/integration/test_uart_mock_ld2412.py": 118.06,
|
||||
"tests/integration/test_uart_mock_ld2420.py": 18.66,
|
||||
"tests/integration/test_uart_mock_ld2420_config_exit.py": 39.09,
|
||||
"tests/integration/test_uart_mock_ld2420_select_mode.py": 23.62,
|
||||
"tests/integration/test_uart_mock_ld2450.py": 14.29,
|
||||
"tests/integration/test_uart_mock_ld2450_has_target_only.py": 11.09,
|
||||
"tests/integration/test_uart_mock_ld2450_link_loss.py": 43.3,
|
||||
"tests/integration/test_uart_mock_ld2450_polygon_zone_restore.py": 12.66,
|
||||
"tests/integration/test_uart_mock_ld2450_polygon_zone_timeout.py": 13.91,
|
||||
"tests/integration/test_uart_mock_ld2450_polygon_zones.py": 14.87,
|
||||
"tests/integration/test_uart_mock_ld2450_target_xy.py": 12.24,
|
||||
"tests/integration/test_uart_mock_ld2460.py": 9.0,
|
||||
"tests/integration/test_uart_mock_modbus.py": 221.56,
|
||||
"tests/integration/test_uart_mock_pipsolar_switches.py": 10.41,
|
||||
"tests/integration/test_uart_mock_pylontech.py": 10.67,
|
||||
"tests/integration/test_uart_mock_sim800l_callbacks.py": 11.0,
|
||||
"tests/integration/test_uart_mock_ufm01.py": 66.28,
|
||||
"tests/integration/test_uart_tcp_bridge.py": 10.65,
|
||||
"tests/integration/test_udp.py": 8.05,
|
||||
"tests/integration/test_udp_ipv6.py": 8.19,
|
||||
"tests/integration/test_udp_multicast.py": 9.88,
|
||||
"tests/integration/test_use_address_runtime.py": 17.43,
|
||||
"tests/integration/test_valve_control_action.py": 9.33,
|
||||
"tests/integration/test_varint_five_byte_device_id.py": 9.7,
|
||||
"tests/integration/test_vnc_client.py": 19.44,
|
||||
"tests/integration/test_vnc_client_auth.py": 19.49,
|
||||
"tests/integration/test_wait_until_mid_loop_timing.py": 12.29,
|
||||
"tests/integration/test_wait_until_on_boot.py": 12.05,
|
||||
"tests/integration/test_wait_until_ordering.py": 9.65,
|
||||
"tests/integration/test_wait_until_reentrant_restart.py": 9.36,
|
||||
"tests/integration/test_wake_loop_forces_phase_b.py": 9.94,
|
||||
"tests/integration/test_water_heater_template.py": 16.44
|
||||
}
|
||||
@@ -116,45 +116,6 @@ def test_timeout_not_with_session_on_peer(setup_core) -> None:
|
||||
fv.full_config.reset(token)
|
||||
|
||||
|
||||
def test_timeout_not_with_link_session(setup_core) -> None:
|
||||
"""A link such as TCP does the session's job itself, so it has no session key but the same rule applies."""
|
||||
from esphome.components.tcp.packet_interface import TcpPacketInterface
|
||||
|
||||
token = fv.full_config.set(
|
||||
{
|
||||
"packet_interface": [
|
||||
{CONF_ID: ID("tcp_link", is_manual=True, type=TcpPacketInterface)}
|
||||
]
|
||||
}
|
||||
)
|
||||
try:
|
||||
with pytest.raises(cv.Invalid, match="session") as error:
|
||||
FINAL_VALIDATE_SCHEMA(
|
||||
CONFIG_SCHEMA({"packet_interface": "tcp_link", CONF_TIMEOUT: "1min"})
|
||||
)
|
||||
assert error.value.path == [CONF_TIMEOUT]
|
||||
finally:
|
||||
fv.full_config.reset(token)
|
||||
|
||||
|
||||
def test_timeout_not_with_link_session_on_peer(setup_core) -> None:
|
||||
"""An addressing peer has what its link provides, as well as its own layers."""
|
||||
from esphome.components.tcp.packet_interface import TcpPacketInterface
|
||||
|
||||
link = {
|
||||
CONF_ID: ID("session_link", is_manual=True, type=TcpPacketInterface),
|
||||
"addressing": {"peers": [{CONF_ID: ID("link_peer", is_manual=True)}]},
|
||||
}
|
||||
token = fv.full_config.set({"packet_interface": [link]})
|
||||
try:
|
||||
with pytest.raises(cv.Invalid, match="session"):
|
||||
FINAL_VALIDATE_SCHEMA(
|
||||
CONFIG_SCHEMA({"packet_interface": "link_peer", CONF_TIMEOUT: "1min"})
|
||||
)
|
||||
finally:
|
||||
fv.full_config.reset(token)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("layers", "allowed"),
|
||||
[
|
||||
|
||||
@@ -1,330 +0,0 @@
|
||||
"""Unit tests for the sx126x and sx127x packet interface platforms."""
|
||||
|
||||
import math
|
||||
|
||||
import pytest
|
||||
|
||||
from esphome import config_validation as cv
|
||||
from esphome.components.packet_interface import CONF_MAX_PACKET_SIZE, radio_link
|
||||
from esphome.components.sx126x import packet_interface as sx126x_packet_interface
|
||||
from esphome.components.sx127x import packet_interface as sx127x_packet_interface
|
||||
import esphome.final_validate as fv
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("schema", "radio_key"),
|
||||
[
|
||||
(sx126x_packet_interface.CONFIG_SCHEMA, "sx126x_id"),
|
||||
(sx127x_packet_interface.CONFIG_SCHEMA, "sx127x_id"),
|
||||
],
|
||||
)
|
||||
def test_max_packet_size_is_radio_limit(
|
||||
setup_core, schema: cv.Schema, radio_key: str
|
||||
) -> None:
|
||||
"""A radio packet holds at most 255 bytes, which is also the default."""
|
||||
assert schema({radio_key: "radio"})[CONF_MAX_PACKET_SIZE] == 255
|
||||
with pytest.raises(cv.Invalid):
|
||||
schema({radio_key: "radio", CONF_MAX_PACKET_SIZE: 256})
|
||||
|
||||
|
||||
def _lora(**options: object) -> dict:
|
||||
return {
|
||||
"modulation": "LORA",
|
||||
"spreading_factor": 7,
|
||||
"bandwidth": "125_0kHz",
|
||||
"coding_rate": "CR_4_5",
|
||||
"preamble_size": 8,
|
||||
"crc_enable": False,
|
||||
**options,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("radio", "airtime_ms"),
|
||||
[
|
||||
# Worked by hand from the Semtech formula, for a 255 byte packet with an explicit header
|
||||
(_lora(spreading_factor=7), 394.5),
|
||||
(_lora(spreading_factor=7, crc_enable=True), 399.6),
|
||||
(_lora(spreading_factor=10), 2254.8),
|
||||
# A symbol takes over 16 ms, so the low data rate optimization lengthens the packet
|
||||
(_lora(spreading_factor=12), 9019.4),
|
||||
],
|
||||
)
|
||||
def test_lora_airtime(radio: dict, airtime_ms: float) -> None:
|
||||
"""The airtime of the largest LoRa packet follows the Semtech formula."""
|
||||
assert radio_link.packet_airtime_ms(radio, 255) == pytest.approx(
|
||||
airtime_ms, abs=0.5
|
||||
)
|
||||
|
||||
|
||||
def test_max_busy_follows_airtime() -> None:
|
||||
"""Listen before talk waits at least 2 s, and longer than the largest packet takes."""
|
||||
assert radio_link.max_busy_ms(_lora(), 255) == 2000
|
||||
assert radio_link.max_busy_ms(_lora(spreading_factor=12), 255) > 9019
|
||||
fsk = {
|
||||
"modulation": "FSK",
|
||||
"preamble_size": 4,
|
||||
"bitrate": 1200,
|
||||
"sync_value": [1, 2],
|
||||
}
|
||||
assert radio_link.packet_airtime_ms(fsk, 64) == pytest.approx(
|
||||
(4 + 2 + 1 + 64 + 2) * 8 / 1200 * 1000
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("radio", "expected_ms"),
|
||||
[
|
||||
# (8 + 4.25 + 8) symbols of 8.192 ms, then 50 ms for the reply to start, with a quarter added
|
||||
(_lora(spreading_factor=10), 270),
|
||||
(_lora(spreading_factor=7), 89),
|
||||
# 6 bytes of preamble, sync word and length at 4800 bit/s
|
||||
(
|
||||
{
|
||||
"modulation": "FSK",
|
||||
"preamble_size": 4,
|
||||
"bitrate": 4800,
|
||||
"sync_value": [0x12],
|
||||
},
|
||||
75,
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_turnaround_covers_reply_header(radio: dict, expected_ms: int) -> None:
|
||||
"""After sending, the link waits until the peer's reply would have started and its header been received."""
|
||||
assert radio_link.turnaround_ms(radio) == expected_ms
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("config", "expected"),
|
||||
[
|
||||
({}, False),
|
||||
# A session's peer sends keep-alives on its own schedule, not a reply to each packet
|
||||
({"session": {}}, False),
|
||||
({"reliability": {}}, True),
|
||||
({"addressing": {"peers": [{"session": {}}, {"reliability": {}}]}}, True),
|
||||
],
|
||||
)
|
||||
def test_takes_turns_only_with_reliability(config: dict, expected: bool) -> None:
|
||||
"""Only a reliability layer's peer answers each packet, and it sends again what turn taking refuses."""
|
||||
assert radio_link.takes_turns(config) is expected
|
||||
|
||||
|
||||
def _fsk(**options: object) -> dict:
|
||||
return {
|
||||
"modulation": "FSK",
|
||||
"packet_mode": True,
|
||||
"payload_length": 0,
|
||||
"sync_value": [0x12],
|
||||
"preamble_size": 4,
|
||||
"bitrate": 4800,
|
||||
**options,
|
||||
}
|
||||
|
||||
|
||||
class _FullConfig(dict):
|
||||
"""Enough of the full configuration for the radio links' final validation."""
|
||||
|
||||
def __init__(self, domain: str, radio: dict) -> None:
|
||||
super().__init__({domain: [{"rx_start": True, **radio}]})
|
||||
self.domain = domain
|
||||
|
||||
def get_path_for_id(self, id_: object) -> list:
|
||||
return [self.domain, 0, "id"]
|
||||
|
||||
def get_config_for_path(self, path: list) -> dict:
|
||||
return self[path[0]][path[1]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("module", "domain", "radio", "link", "match", "fix"),
|
||||
[
|
||||
(
|
||||
sx126x_packet_interface,
|
||||
"sx126x",
|
||||
_lora(payload_length=10),
|
||||
{},
|
||||
"payload_length",
|
||||
{"payload_length": 0},
|
||||
),
|
||||
(
|
||||
sx127x_packet_interface,
|
||||
"sx127x",
|
||||
_lora(payload_length=10),
|
||||
{},
|
||||
"payload_length",
|
||||
{"payload_length": 0},
|
||||
),
|
||||
(
|
||||
sx127x_packet_interface,
|
||||
"sx127x",
|
||||
{
|
||||
**_fsk(modulation="FSK", packet_mode=False),
|
||||
},
|
||||
{},
|
||||
"packet_mode",
|
||||
{"packet_mode": True},
|
||||
),
|
||||
(
|
||||
sx127x_packet_interface,
|
||||
"sx127x",
|
||||
{
|
||||
**_fsk(modulation="OOK"),
|
||||
},
|
||||
{CONF_MAX_PACKET_SIZE: 64},
|
||||
"at most 63",
|
||||
None,
|
||||
),
|
||||
(
|
||||
sx127x_packet_interface,
|
||||
"sx127x",
|
||||
{
|
||||
**_fsk(sync_value=[]),
|
||||
},
|
||||
{},
|
||||
"sync_value",
|
||||
{"sync_value": [0x12]},
|
||||
),
|
||||
(
|
||||
sx126x_packet_interface,
|
||||
"sx126x",
|
||||
_fsk(sync_value=[]),
|
||||
{},
|
||||
"sync_value",
|
||||
{"sync_value": [0x12, 0x34]},
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_radio_settings_checked(
|
||||
module: object,
|
||||
domain: str,
|
||||
radio: dict,
|
||||
link: dict,
|
||||
match: str,
|
||||
fix: dict | None,
|
||||
) -> None:
|
||||
"""Packets vary in length; FSK or OOK on an SX127x needs packet mode and a sync word, and holds at most 63 bytes;
|
||||
FSK on an SX126x needs a sync word.
|
||||
"""
|
||||
config = {f"{domain}_id": "radio", CONF_MAX_PACKET_SIZE: 63, **link}
|
||||
token = fv.full_config.set(_FullConfig(domain, radio))
|
||||
try:
|
||||
with pytest.raises(cv.Invalid, match=match):
|
||||
module.FINAL_VALIDATE_SCHEMA(config)
|
||||
if fix is not None:
|
||||
fv.full_config.set(_FullConfig(domain, {**radio, **fix}))
|
||||
assert module.FINAL_VALIDATE_SCHEMA(config)
|
||||
finally:
|
||||
fv.full_config.reset(token)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("link", "match"),
|
||||
[
|
||||
({}, None),
|
||||
({"encryption": "secret"}, None),
|
||||
({"reliability": {}}, "reliability"),
|
||||
(
|
||||
{
|
||||
"addressing": {
|
||||
"address": 1,
|
||||
"peers": [
|
||||
{"id": "a", "destination": 2, "session": {}},
|
||||
{"id": "b", "destination": 3},
|
||||
],
|
||||
}
|
||||
},
|
||||
"session",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_rx_start_needed_for_replies(setup_core, link: dict, match: str | None) -> None:
|
||||
"""A radio that sleeps after sending suits only a link whose layers need no packets back."""
|
||||
sleeping = _lora(payload_length=0, rx_start=False)
|
||||
config = sx126x_packet_interface.CONFIG_SCHEMA({"sx126x_id": "radio", **link})
|
||||
token = fv.full_config.set(_FullConfig("sx126x", sleeping))
|
||||
try:
|
||||
if match is None:
|
||||
assert sx126x_packet_interface.FINAL_VALIDATE_SCHEMA(config)
|
||||
return
|
||||
with pytest.raises(cv.Invalid, match=f"'{match}'.*rx_start"):
|
||||
sx126x_packet_interface.FINAL_VALIDATE_SCHEMA(config)
|
||||
finally:
|
||||
fv.full_config.reset(token)
|
||||
|
||||
|
||||
def _final_validate(radio: dict, link: dict) -> None:
|
||||
config = sx126x_packet_interface.CONFIG_SCHEMA({"sx126x_id": "radio", **link})
|
||||
token = fv.full_config.set(_FullConfig("sx126x", {"payload_length": 0, **radio}))
|
||||
try:
|
||||
sx126x_packet_interface.FINAL_VALIDATE_SCHEMA(config)
|
||||
finally:
|
||||
fv.full_config.reset(token)
|
||||
|
||||
|
||||
def test_packet_must_fit_transmit_time(setup_core) -> None:
|
||||
"""The radios give up on a packet that takes longer than 4 s to send, so the largest packet must take less."""
|
||||
slow = _lora(spreading_factor=12)
|
||||
with pytest.raises(cv.Invalid, match=r"255 bytes takes 9019 ms.*to 7\d or less"):
|
||||
_final_validate(slow, {})
|
||||
_final_validate(slow, {CONF_MAX_PACKET_SIZE: 64})
|
||||
_final_validate(_lora(), {})
|
||||
|
||||
|
||||
def test_reliability_timeout_follows_airtime(setup_core) -> None:
|
||||
"""A reliability timeout must allow for the peer's largest packet and the acknowledgement."""
|
||||
needed = radio_link.reliability_timeout_ms(_lora(), 255, 0)
|
||||
assert needed == math.ceil(
|
||||
(
|
||||
radio_link.packet_airtime_ms(_lora(), 255)
|
||||
+ radio_link.packet_airtime_ms(_lora(), radio_link.ACK_PACKET_SIZE)
|
||||
)
|
||||
* radio_link.MARGIN
|
||||
)
|
||||
assert radio_link.reliability_timeout_ms(_lora(), 255, 100) == needed + 100
|
||||
with pytest.raises(cv.Invalid, match=f"at least {needed}ms"):
|
||||
_final_validate(_lora(), {"reliability": {"timeout": "200ms"}})
|
||||
# Left out, the link works it out when generating code
|
||||
_final_validate(_lora(), {"reliability": {}})
|
||||
_final_validate(_lora(), {"reliability": {"timeout": f"{needed}ms"}})
|
||||
# The worked out timeout allows for any ack_delay, so one longer than 200 ms needs no timeout set
|
||||
_final_validate(_lora(spreading_factor=9), {"reliability": {"ack_delay": "250ms"}})
|
||||
|
||||
|
||||
def test_suggested_size_is_allowed(setup_core) -> None:
|
||||
"""The suggested max_packet_size is never below the smallest one allowed."""
|
||||
slowest = _lora(spreading_factor=12, bandwidth="7_8kHz")
|
||||
with pytest.raises(cv.Invalid, match="use faster radio settings"):
|
||||
_final_validate(slowest, {CONF_MAX_PACKET_SIZE: 16})
|
||||
|
||||
|
||||
def test_channel_packet_size(setup_core) -> None:
|
||||
"""Other devices can send no more than the radio receives, nor more than their own checks allow."""
|
||||
assert radio_link.channel_packet_size(_lora(), 255) == 255
|
||||
assert radio_link.channel_packet_size(_lora(spreading_factor=12), 255) == 77
|
||||
assert radio_link.channel_packet_size(_fsk(), 63) == 63
|
||||
|
||||
|
||||
def test_reliability_timeout_allows_for_other_devices(setup_core) -> None:
|
||||
"""A small max_packet_size still needs a timeout that allows for the largest packet another device can send."""
|
||||
needed = radio_link.reliability_timeout_ms(_lora(), 255, 0)
|
||||
with pytest.raises(cv.Invalid, match=f"at least {needed}ms"):
|
||||
_final_validate(
|
||||
_lora(),
|
||||
{
|
||||
CONF_MAX_PACKET_SIZE: 16,
|
||||
"reliability": {
|
||||
"timeout": f"{radio_link.reliability_timeout_ms(_lora(), 16, 0)}ms"
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def test_long_sends_warned(setup_core, caplog: pytest.LogCaptureFixture) -> None:
|
||||
"""A send of many radio packets, each waited for, is warned about."""
|
||||
slow = _lora(spreading_factor=10)
|
||||
_final_validate(slow, {CONF_MAX_PACKET_SIZE: 64})
|
||||
assert "radio packets in a row" not in caplog.text
|
||||
_final_validate(slow, {CONF_MAX_PACKET_SIZE: 64, "fragmentation": {}})
|
||||
# 1024 bytes in fragments of 62
|
||||
assert "can send 17 radio packets in a row" in caplog.text
|
||||
@@ -62,8 +62,7 @@ def test_reliability_defaults(setup_core) -> None:
|
||||
config = uart_packet_interface.CONFIG_SCHEMA({CONF_RELIABILITY: None})
|
||||
reliability = config[CONF_RELIABILITY]
|
||||
assert reliability["type"] == "hdlc"
|
||||
# Left out, so that the link can choose one; otherwise the layer uses 200 ms
|
||||
assert "timeout" not in reliability
|
||||
assert reliability["timeout"].total_milliseconds == 200
|
||||
assert reliability[CONF_MAX_QUEUE_LENGTH] == 4
|
||||
assert reliability["max_retries"] == 5
|
||||
|
||||
@@ -93,9 +92,6 @@ def test_ack_delay_shorter_than_timeout(setup_core) -> None:
|
||||
uart_packet_interface.CONFIG_SCHEMA(
|
||||
{CONF_RELIABILITY: {"timeout": "500ms", "ack_delay": "500ms"}}
|
||||
)
|
||||
# Without a timeout, the link's default of 200 ms applies
|
||||
with pytest.raises(cv.Invalid, match="shorter than 'timeout', which is 200ms"):
|
||||
uart_packet_interface.CONFIG_SCHEMA({CONF_RELIABILITY: {"ack_delay": "250ms"}})
|
||||
|
||||
|
||||
def test_layer_types_registered() -> None:
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
"""Unit tests for the tcp packet interface platform."""
|
||||
|
||||
import pytest
|
||||
|
||||
from esphome import config_validation as cv
|
||||
from esphome.components import socket
|
||||
from esphome.components.packet_interface import CONF_MAX_PACKET_SIZE
|
||||
from esphome.components.tcp import packet_interface as tcp_packet_interface
|
||||
from esphome.core import CORE
|
||||
|
||||
|
||||
def test_client_and_server(setup_core) -> None:
|
||||
"""A client needs a host; a server listens on its port."""
|
||||
client = tcp_packet_interface.CONFIG_SCHEMA({"host": "192.168.1.50", "port": 6060})
|
||||
assert client["role"] == "client"
|
||||
assert client[CONF_MAX_PACKET_SIZE] == 1022
|
||||
assert tcp_packet_interface.CONFIG_SCHEMA({"role": "server", "port": 6060})
|
||||
with pytest.raises(cv.Invalid):
|
||||
tcp_packet_interface.CONFIG_SCHEMA({"port": 6060})
|
||||
|
||||
|
||||
@pytest.mark.parametrize("layer", ["reliability", "session", "addressing"])
|
||||
def test_provided_layers_rejected(setup_core, layer: str) -> None:
|
||||
"""TCP provides these itself, so they cannot be configured on it."""
|
||||
with pytest.raises(cv.Invalid, match=f"provides '{layer}' itself"):
|
||||
tcp_packet_interface.CONFIG_SCHEMA(
|
||||
{"host": "192.168.1.50", "port": 6060, layer: {"address": 1}}
|
||||
)
|
||||
|
||||
|
||||
def test_rolling_code_rejected(setup_core) -> None:
|
||||
"""The rolling code needs the session's keep-alives, which TCP does not send through the layers."""
|
||||
with pytest.raises(cv.Invalid, match="cannot be used on this link"):
|
||||
tcp_packet_interface.CONFIG_SCHEMA(
|
||||
{
|
||||
"host": "192.168.1.50",
|
||||
"port": 6060,
|
||||
"encryption": {"key": "secret", "rolling_code_enable": True},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_auto_load_binary_sensor_for_connected(setup_core) -> None:
|
||||
"""The binary_sensor component is loaded only for the connected sensor."""
|
||||
assert tcp_packet_interface.AUTO_LOAD() == ["socket", "binary_sensor"]
|
||||
assert tcp_packet_interface.AUTO_LOAD({}) == ["socket", "binary_sensor"]
|
||||
config = {"host": "192.168.1.50", "port": 6060}
|
||||
assert tcp_packet_interface.AUTO_LOAD(config) == ["socket"]
|
||||
config["connected"] = {"name": "Link"}
|
||||
assert tcp_packet_interface.AUTO_LOAD(config) == ["socket", "binary_sensor"]
|
||||
|
||||
|
||||
def test_timeout(setup_core) -> None:
|
||||
"""The timeout follows keep_alive unless given, and must be longer than it."""
|
||||
base = {"host": "192.168.1.50", "port": 6060}
|
||||
config = tcp_packet_interface.CONFIG_SCHEMA({**base, "keep_alive": "2s"})
|
||||
assert config["timeout"].total_milliseconds == 6000
|
||||
# A peer that sends keep-alives less often is matched with a longer timeout
|
||||
config = tcp_packet_interface.CONFIG_SCHEMA({**base, "timeout": "90s"})
|
||||
assert config["timeout"].total_milliseconds == 90000
|
||||
with pytest.raises(cv.Invalid, match="longer than keep_alive"):
|
||||
tcp_packet_interface.CONFIG_SCHEMA(
|
||||
{**base, "keep_alive": "5s", "timeout": "5s"}
|
||||
)
|
||||
with pytest.raises(cv.Invalid):
|
||||
tcp_packet_interface.CONFIG_SCHEMA({**base, "keep_alive": "2h"})
|
||||
with pytest.raises(cv.Invalid):
|
||||
tcp_packet_interface.CONFIG_SCHEMA({**base, "timeout": "4h"})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("role", "tcp", "listen"), [("client", 1, 0), ("server", 1, 1)]
|
||||
)
|
||||
def test_sockets_reserved(setup_core, role: str, tcp: int, listen: int) -> None:
|
||||
"""A client reserves one socket; a server also reserves a listener."""
|
||||
config = {"role": role, "port": 6060}
|
||||
if role == "client":
|
||||
config["host"] = "192.168.1.50"
|
||||
tcp_packet_interface.CONFIG_SCHEMA(config)
|
||||
name = "tcp.packet_interface"
|
||||
assert CORE.data[socket.KEY_SOCKET_CONSUMERS_TCP][name] == tcp
|
||||
assert (
|
||||
CORE.data.get(socket.KEY_SOCKET_CONSUMERS_TCP_LISTEN, {}).get(name, 0) == listen
|
||||
)
|
||||
Reference in new issue
Block a user