Compare commits

...
Author SHA1 Message Date
clydebarrowandClaude Opus 5.5 f12e235cdd [espnow][udp] Report ESP-NOW send room, and refuse addresses that name no one device
- The ESP-NOW link reports the free slots in the shared send queue, and
  addressing ports pass on their link's capacity, so fragmentation does
  not start a packet that the queue cannot take whole.
- An ESP-NOW peer_address must be a device's own address or broadcast,
  not a group or all-zero address.
- The broadcast address is added as a peer at boot, not at the first send.
- A UDP destination of 0.0.0.0 or 255.255.255.255 is not taken as one peer.
- A test for the once a minute warning of a dropped UDP sender.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:03:13 +11:00
clydebarrow 0f6e49e9c1 Merge dev into packet-interface-09 2026-11-16 15:41:30 +11:00
clydebarrow f861a6c9d2 Merge packet-interface-08 2026-11-16 15:17:48 +11:00
clydebarrowandClaude Opus 5.5 577a91ccb5 [packet_interface] Find the layers a link provides from the link, also for a peer
interface_has_layer() looked up the provided layers by the type of the
configuration holding the layers, which for an addressing peer is the
peer, so a peer never had what its link provides.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:17:46 +11:00
clydebarrow 6b82439a60 Merge packet-interface-08 2026-11-16 15:06:12 +11:00
clydebarrowandClaude Opus 5.5 af097bc1cd [packet_interface] Warn when a fragmented packet is cut short
A link that refuses a fragment after others were sent leaves the peer
with part of a packet, which it drops. The fragmentation layer now logs
a warning then. The TCP link's capacity comment no longer claims that
this cannot happen: a socket that stalls part way through still cuts a
packet short.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:06:06 +11:00
clydebarrow 8855c059af Merge packet-interface-08 2026-11-16 14:27:58 +11:00
clydebarrow f9f8fe4a4f Merge remote-tracking branch 'upstream/dev' into pi-08-local
# Conflicts:
#	esphome/components/packet_interface/__init__.py
2026-11-16 14:21:05 +11:00
clydebarrowandClaude Opus 5.5 d8b6f315dc Merge packet-interface-08
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:51 +11:00
clydebarrowandClaude Opus 5.5 76c5005fa0 Merge packet-interface-07
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:51 +11:00
clydebarrowandClaude Opus 5.5 551b7e2a40 [packet_interface] Take turns only with a reliability layer, and count a failed SX126x read as receiving
A session layer's peer does not answer each packet, so turn taking on a link without a
reliability layer only refused sends that nothing would send again. A failed interrupt
read in SX126xRadio::is_receiving() read as a free channel, so the link could send over
a reception it could not see; it now holds the send, and warns if the radio stays unreadable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 12:08:49 +11:00
clydebarrowandClaude Opus 5.5 b1187431fe [espnow][udp] Warn now and then about oversized ESP-NOW packets, and log a failed UDP read
An ESP-NOW peer with a larger max_packet_size has its larger packets dropped, which was
logged only at verbose level, and the size check now runs after the peer filter, so other
senders' packets are not counted. A UDP read that fails other than for want of data is
logged: the raw lwIP socket consumes a packet whose source address it cannot convert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:04:25 +11:00
clydebarrowandClaude Opus 5.5 0957ee51a5 Merge packet-interface-08
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:03:11 +11:00
clydebarrowandClaude Opus 5.5 054d9657dc [tcp] Pass the link's send capacity through encryption, and declare the connected sensor with SUB_BINARY_SENSOR
On TCP, encryption sits right below fragmentation, and reported an unlimited capacity
whatever the link had room for, so a fragmented packet could be started that the link
could not take in full.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:03:02 +11:00
clydebarrowandClaude Opus 5.5 4a71b15edb Merge packet-interface-07
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:01:36 +11:00
clydebarrowandClaude Opus 5.5 ca8137ca5f [packet_interface] Say in the radio busy warning whether the send went out
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:01:29 +11:00
clydebarrowandClaude Opus 5.5 30955abf51 Merge packet-interface-08, with dev merged in
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:53:53 +11:00
clydebarrowandClaude Opus 5.5 cc24f06a15 Merge packet-interface-07, with dev merged in
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:53:53 +11:00
clydebarrowandClaude Opus 5.5 3dc9595f08 Merge branch 'dev' into packet-interface-07
dev now has the SX127x async PR, so 07 adds only its own SX127x changes on top.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:53:52 +11:00
clydebarrowandClaude Opus 5.5 e4fd61538b Merge packet-interface-08
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:47:27 +11:00
clydebarrowandClaude Opus 5.5 73766dc622 Merge packet-interface-07
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:47:27 +11:00
clydebarrowandClaude Opus 5.5 366fe14053 Merge the SX127x async PR's shorter comments
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:47:26 +11:00
clydebarrowandClaude Opus 5.5 dbcb55ce92 [sx127x] Shorten the comments
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:46:46 +11:00
clydebarrowandClaude Opus 5.5 df54f8b7cb Merge packet-interface-08, with dev merged in
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:40:11 +11:00
clydebarrowandClaude Opus 5.5 d3e81fbb7c Merge packet-interface-07, with dev merged in
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:40:11 +11:00
clydebarrowandClaude Opus 5.5 034be60a11 Merge branch 'dev' into packet-interface-07
dev now packs the SX126x interrupt setup through set_dio_irq_params_(). It takes the
flagged interrupts and the DIO1 ones separately, so receive can flag a valid header
or sync word without signalling it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:40:10 +11:00
clydebarrowandClaude Opus 5.5 8a86bb98dc Merge packet-interface-08, now based on the SX127x async PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:29:13 +11:00
clydebarrowandClaude Opus 5.5 35929b3b2d Merge packet-interface-07, now based on the SX127x async PR
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:29:12 +11:00
clydebarrowandClaude Opus 5.5 856c2c1816 Merge the SX127x async PR into packet-interface-07, which brings in the SX126x refactor
07 is now based on clydebarrow/sx127x-async-transmit (esphome PR 22733), which is on dev
after the SX126x radio lifecycle refactor (PR 22197). 07's driver changes are ported:

- is_receiving() moves into SX126xRadio, and into the new SX127x radio state; both report
  a held packet and stay false while sending or asleep.
- SX126x flags a valid header or sync word for is_receiving() without signalling it on DIO1.
- SX127x FSK packets are at most 63 bytes, as the FIFO also holds the length byte.
- The radio link takes an Error value of 0 as success, as both radios' NONE values are renamed.
- 07's own FSK SNR change is dropped; both drivers now report NAN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:27:55 +11:00
clydebarrowandClaude Opus 5.5 7fa96b0ac4 [sx127x] Send and receive with the settings configure() last applied
configure() is refused while a send is on air, but the setters still changed the
modulation, payload length, packet mode, RX start and frequency that the end of
the send and later sends and packets used, before the chip had them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 07:53:18 +11:00
clydebarrowandClaude Opus 5.5 3df2e55176 [sx127x] Arm the configuration retry before setting the warning
The warning's log message can run user code that calls configure(). Armed
afterwards, the retry stayed set on a radio that had already recovered, and reset
it a second later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 07:46:06 +11:00
clydebarrowandClaude Opus 5.5 7f200e14ed [sx127x] Go through standby when a send ends early
An SX1276 stays in TX when asked for RX while a packet is still going out, so a
cancelled or timed out send set a warning until the radio was configured again.
The test chip now behaves the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:41:54 +11:00
clydebarrowandClaude Opus 5.5 d3d792d356 [sx127x] Define the interrupt handler in the header, as SX126x does
Out of line, it shares the RP2040 .time_critical section with inline functions such
as get_loop_component_start_time(), which the compiler rejects as a section type conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:41:53 +11:00
clydebarrowandClaude Opus 5.5 0ef6c41e02 [sx127x] Add asynchronous sending, and recover from failed mode changes
Matches the SX126x changes: start_transmit_async(), cancel_transmit_async() and
is_transmitting(), with SX127xTransmitListener. A received packet still waiting
when a send starts is kept, the send returns BUSY, and the next loop delivers
the packet. Mode changes are ignored while a send is on air.

A mode change that does not take now sets a warning and the radio is configured
again every second, instead of marking the component failed. The error values
are renamed to SX127X_ERROR_*, and FSK packets report NAN for SNR.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:34:04 +11:00
clydebarrow 65a6c41c6b Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-14 12:43:39 +11:00
clydebarrow 4bbd53bf7a Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-14 12:43:39 +11:00
clydebarrowandClaude Opus 5.5 d66faa3ba7 [packet_interface] Take turns on a radio link whose peer replies
After a send, the next frame of a burst went out at once, on top of the peer's
acknowledgement or reply, as the radio only notices a packet once its header
has arrived. Both packets were lost, and the reliability layer threw away the
rest of the burst. Now, on a link with a layer that needs replies, a send in a
later loop pass waits until the peer has had time to start a reply and its
header to be received, unless a packet arrives first. Sends in the same loop
pass, such as the fragments of one packet, are not held apart, and one-way
links are unchanged.

On a SX1262 and SX1276 pair at SF10, 45 minutes of counter updates both ways
went from 86 to 90 percent delivered, with a link reset every 9 minutes, to
all 540 delivered each way with no resends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 09:56:50 +11:00
clydebarrow 10f45748df Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-14 08:10:05 +11:00
clydebarrow ccaebe0dce Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-14 08:10:05 +11:00
clydebarrowandClaude Opus 5.5 33d0b19643 [packet_interface] Resend only the oldest frame after a reliability timeout
Resending every unacknowledged frame on each timeout let two radio peers whose
acknowledgements were lost keep sending long bursts that collided again, until
one side reset the link. Now only the oldest frame is resent; the peer's answer
acknowledges whatever it already has, and the remaining frames follow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 08:09:58 +11:00
clydebarrow 31de10509b Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-13 10:53:33 +11:00
clydebarrow d39519a424 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-13 10:53:32 +11:00
clydebarrowandClaude Opus 5.5 1e78fb08fa [sx126x][sx127x] Name the radio link's time source without the protected suffix, as it is virtual
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 10:53:32 +11:00
clydebarrow 88ddf2d0bb Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-13 09:03:27 +11:00
clydebarrow 51c1b7a9ba Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-13 09:03:27 +11:00
clydebarrowandClaude Opus 5.5 6c6e680828 [sx126x][sx127x] Time busy periods from the current time, and size radio timings for the largest packet on the channel
- Listen before talk reads the current time, not the loop's start: earlier sends in the same loop pass may have
  waited seconds for the radio, and a reception first seen then could be sent over at once.
- A send made over a busy flag that seemed stuck ends the busy period, so a reception that starts before loop()
  runs is waited for again.
- An accepted packet ends a run of dropped oversized ones.
- The reliability timeout and the listen before talk limit allow for the largest packet any device on the channel
  can send, not only this link's own; a refused resend still counts as a retry.
- The suggested max_packet_size is never below 16, the smallest allowed.
- A warning is logged when one send can become several radio packets that together hold up other components for
  more than 4 s, as the drivers wait for each packet to be sent.
- SX126x FSK packets report the SNR as NAN, as the radio does not measure it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 09:03:25 +11:00
clydebarrow 22d4f1564f Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 21:41:05 +11:00
clydebarrowandClaude Opus 5.5 2ddc1109c2 [tcp] Read at most 8 frames per loop pass, and report send capacity while frames wait for the socket
- A peer sending without a pause no longer holds the main loop in the receive loop; a frame being read carries over.
- While frames wait for the socket, the link reports how many more full frames fit, so a fragmentation layer above
  does not start a packet that would be cut short.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:41:05 +11:00
clydebarrow d26e3dc68c Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 21:35:05 +11:00
clydebarrow 5a1e5eb899 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08
# Conflicts:
#	esphome/components/packet_interface/__init__.py
2026-11-12 21:35:03 +11:00
clydebarrowandClaude Opus 5.5 22666de8aa [packet_interface] Check ack_delay against the reliability timeout that the link will use
Without a timeout, ack_delay was checked against the 200 ms default even on a radio link, which works out a longer
timeout that allows for the delay.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:34:39 +11:00
clydebarrow e6ddb7c669 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 21:01:50 +11:00
clydebarrow e1ff5028b4 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 21:01:50 +11:00
clydebarrowandClaude Opus 5.5 add9caa66d [sx126x][sx127x] Work out reliability timeouts and size limits from the radio settings, and fix SX127x TX_DONE and FSK handling
- A reliability layer on a radio link without a timeout gets one long enough for the peer's largest packet and an
  acknowledgement, from the radio's settings; a timeout set shorter is rejected.
- A max_packet_size whose packets take longer to send than the drivers wait for (4 s) is rejected, naming the largest
  that fits.
- Listen before talk holds sends back for as long as the largest packet the radio can receive takes, not the link's
  own limit.
- A received packet larger than the link's max_packet_size is dropped.
- SX127x: in LoRa mode DIO0 also signals TX_DONE when the radio sleeps after sending, which was taken for a received
  packet, both by is_receiving() and loop(); FSK and OOK packets report RSSI and SNR as NAN, as the radio does not
  measure them; variable length FSK and OOK packets hold at most 63 bytes, as the FIFO also holds the length byte.
- The radio's configuration is found with fv.get_declaring_config().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:01:47 +11:00
clydebarrow a96e323e4c Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 20:43:52 +11:00
clydebarrow 1a2ec29caa Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 20:43:52 +11:00
clydebarrow 16ee16f391 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 20:42:45 +11:00
clydebarrow 3e62f017af Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 20:42:44 +11:00
clydebarrow 2d67382a17 Merge remote-tracking branch 'upstream/dev' into clydebarrow/packet-interface-07 2026-11-12 20:42:44 +11:00
clydebarrowandClaude Opus 5.5 e71c4ed0d9 [sx126x][sx127x] Put the radio link's flag in the spare byte of the packet interface base
Declared first, it fills the byte the base class leaves at its end, which takes each radio link from 56 to 52 bytes
on ESP32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 27ee1540e6 [sx126x][sx127x] Warn about sending over a busy radio only once the radio has the packet
A log line can run an automation that sends again, which would overwrite the buffer of a layer above before the
radio had read it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 2671bad22b [sx126x][sx127x] Stop reading a radio that fails while sends are held back
Also drop a leftover preamble_detect key from a test case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 a3bf895895 [sx127x] Count only the sync word match as a packet arriving without LoRa
The preamble flag stays set when no sync word follows, so a packet from
another network held every send back until max_busy ran out. A link
without LoRa now needs a sync_value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 f400132b16 [sx126x] Require a sync word for a link with FSK
Without one the radio cannot tell that a packet is arriving, so listen
before talk does nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 d92883f0c3 [sx126x][sx127x] Fixes from code review
- Without LoRa, require the SX127x radio to have a sync word or preamble
  detection, as listen before talk cannot see a packet arriving otherwise
- Warn once when a send finds the radio failed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 61d8a52e2b [sx126x][sx127x] Fixes from code review
- Warn when a send goes ahead over a busy period at its limit, as that
  means a stuck busy indication or a jammed channel
- Log an empty packet at verbose level, and say why it counts as sent
- Name the radio id if code generation cannot find its configuration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 38ef34e006 [sx126x][sx127x] Say which callers send a refused packet again
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 a6ee2288be [sx126x][sx127x] Require rx_start for layers that need replies
- Reject a radio with rx_start false when the link or a peer has a layer
  that needs packets back from the peer, as the radio sleeps after sending
- Log a send over a long busy period at debug level, so a lost reception
  can be traced

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 5647a22c9a [sx126x][sx127x] Share the radio link code, and fixes from code review
- Make both links one RadioLink template in packet_interface, with shared
  code generation in radio_link.py, so a fix applies to both radios
- While sending is held back, watch the radio from loop() and end the
  busy period once it is free, so a later reception starts a new period
  instead of being sent over
- Reject an SX127x link without LoRa unless the radio is in packet mode

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Code 4f008b76e4 [sx126x][sx127x] Take the packet size at construction
The radio links have no buffer of their own, so code generation passes
the configured max_packet_size to the constructor.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 3a4ae0342d [sx126x][sx127x] Fixes from code review
- Hold a send back for at most the airtime of the largest packet, worked
  out from the radio settings and at least 2 s, instead of a fixed 2 s
  that a LoRa packet at a high spreading factor outlasts
- End a busy period when a send finds the radio free, so a busy flag that
  never clears is released however rarely sends are tried
- Detect an FSK reception on the SX127x from its preamble and sync word
  flags
- Test the radio links' final validation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrowandClaude Opus 5.5 a2c1fdf9a1 [sx126x][sx127x] Add packet interface links for SX126x and SX127x radios
Each packet is one radio packet, with the RSSI and SNR of received
packets in the metadata. A radio cannot hear while it sends, so a link
refuses to send while its radio is receiving a packet, for up to two
seconds, and the layers above send again shortly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 20:14:32 +11:00
clydebarrow fbd5c8097a Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 14:18:16 +11:00
clydebarrow eda4be7972 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 14:18:16 +11:00
clydebarrow 87d0e2370d Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 14:18:16 +11:00
clydebarrowandClaude Opus 5.5 596a0e6a4a [packet_interface] Warn about a refused nested send once the outer send is done, not from a deferred call
A deferred call could outlive the layer in the unit tests, which run many components in one program.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 14:14:10 +11:00
clydebarrow 8d89e2bc1d Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 13:15:38 +11:00
clydebarrow d1ad87f4e6 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 13:15:37 +11:00
clydebarrow b37748c2d3 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 13:15:37 +11:00
clydebarrowandClaude Opus 5.5 dd876c98fc [packet_interface] Warn about refused nested sends, keep destination warnings per port, and check sizes when validating
- A send refused because another is in progress is now logged once per layer, from the next loop, as the logger
  drops lines logged from an automation that a log line runs.
- Each addressing port warns about its own refused destinations, naming the port, so a refusal on one port does not
  hide a later one on another.
- The session layer reports its one byte header and its keep-alive size to config validation, so the sizes there
  match the C++ layers.
- A layer that the layers below leave no room for is rejected during validation, not when the device starts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 13:15:30 +11:00
clydebarrow 5c800279f7 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 12:39:30 +11:00
clydebarrow 1fa3dcb0f3 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08
# Conflicts:
#	esphome/components/entity_transport/__init__.py
2026-11-12 12:39:28 +11:00
clydebarrow bd04e4bf2e Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 12:39:01 +11:00
clydebarrowandClaude Opus 5.5 90f6d49b55 [packet_interface] Guard the UART link against nested sends, and give fragmentation and entity_transport generated buffers
- The UART link writes a frame in pieces, so a send made from a UART debug automation, possibly through another
  addressing peer, is now refused instead of landing inside the frame.
- Addressing peers whose encryption keeps state for one peer, such as the rolling code, must use different keys; a
  packet relabelled with another peer's source address would otherwise pass that peer's checks.
- An addressing port named with port_id is now found when an application refers to it.
- The reliability layer refuses a packet with a chosen destination, which it could not keep for a resend.
- The fragmentation layer and entity_transport take buffers from cg.static_buffer, in PSRAM when it is enabled;
  entity_transport's size comes from the configured layers, and setup() fails if the C++ layers need more.
- destination is validated with cv validators that schema tooling can describe, and the address header uses
  put_le16().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 12:38:52 +11:00
clydebarrow 1c725e37e2 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 09:00:23 +11:00
clydebarrow 8f8243c164 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 09:00:23 +11:00
clydebarrow 702f44e4dd Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 09:00:22 +11:00
clydebarrow bf503dc448 Merge remote-tracking branch 'upstream/dev' into clydebarrow/packet-interface-06
# Conflicts:
#	esphome/components/entity_transport/__init__.py
#	esphome/components/packet_interface/__init__.py
#	tests/unit_tests/test_entity_transport.py
2026-11-12 09:00:10 +11:00
clydebarrow 69e70b7da1 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 08:25:48 +11:00
clydebarrow 26a5003459 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 08:25:48 +11:00
clydebarrow a48da110ca Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 08:25:48 +11:00
clydebarrowandClaude Opus 5.5 bd26849dc3 [packet_interface] Name the test's queue length constant in upper case, as clang-tidy requires
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 08:25:47 +11:00
clydebarrowandClaude Opus 5.5 635f4d850d [espnow][udp] Order the ESP-NOW and UDP link members to avoid padding
The six byte peer address now sits with the two flags, and the UDP link's flag fills the spare byte at the end of
the packet interface base, which takes the ESP-NOW link from 76 to 72 bytes and the UDP link from 56 to 52 on
ESP32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 08:06:03 +11:00
clydebarrow dc428031aa Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 08:05:25 +11:00
clydebarrowandClaude Opus 5.5 03958a6c1b [tcp] Keep the server flag with the other small members of the TCP link
It sat alone after the listener, padded to a word, which took the link from 76 to 80 bytes on ESP32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 08:05:24 +11:00
clydebarrow fed10a9dc9 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 08:04:57 +11:00
clydebarrowandClaude Opus 5.5 5925f80e4d [sx126x][sx127x] Put the radio link's flag in the spare byte of the packet interface base
Declared first, it fills the byte the base class leaves at its end, which takes each radio link from 56 to 52 bytes
on ESP32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 08:04:56 +11:00
clydebarrow d1b40be6d4 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 08:04:36 +11:00
clydebarrowandClaude Opus 5.5 86ca922934 [packet_interface] Keep the fragmentation layer's members free of padding
The length of the packet being joined fits in 16 bits, as configuration limits it to 16384 bytes, and the two flags
share a byte, which takes the layer from 56 to 52 bytes on ESP32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 08:04:35 +11:00
clydebarrow b199e4eeb5 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 07:56:51 +11:00
clydebarrow 4beea465c7 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 07:56:36 +11:00
clydebarrow 3068b6adb7 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 07:56:20 +11:00
clydebarrow 32ac29fb21 Merge branch 'clydebarrow/packet-interface-05' into clydebarrow/packet-interface-06
# Conflicts:
#	esphome/components/packet_interface/__init__.py
#	esphome/components/packet_interface/layers/__init__.py
#	esphome/components/packet_interface/layers/hdlc_reliability.py
#	esphome/components/packet_interface/layers/xxtea_encryption.py
2026-11-12 07:56:02 +11:00
clydebarrow 98b41c4dc8 Merge remote-tracking branch 'upstream/dev' into clydebarrow/packet-interface-05 2026-11-12 07:48:17 +11:00
clydebarrow edf7d6e07e Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-12 07:42:56 +11:00
clydebarrow 64a9afd8b5 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-12 07:42:56 +11:00
clydebarrow 0ee08e9a79 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-12 07:42:56 +11:00
clydebarrow b92b0d01c0 Merge branch 'clydebarrow/packet-interface-05' into clydebarrow/packet-interface-06 2026-11-12 07:42:56 +11:00
clydebarrowandClaude Opus 5.5 5bcafec97b [entity_transport] Order the members to avoid padding, and keep the flags in one-bit fields
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 07:42:55 +11:00
clydebarrowandClaude Opus 5.5 7cc4eaa09b [espnow] Warn now and then when a unicast peer does not acknowledge packets
Without a reliability layer above, a peer that is off, out of range or has another address went unreported, as the
delivery result was logged only at verbose level by the espnow component.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:41:33 +11:00
clydebarrow cbf19d2d7e Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-10 16:39:20 +11:00
clydebarrowandClaude Opus 5.5 eeb6b2a6e5 [tcp] Report a packet as not sent when the write that sends it drops the connection
The failed write closes the link and discards the queued frame, so returning true lost the packet without the
sender knowing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:39:13 +11:00
clydebarrow fe15eec956 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-10 16:38:21 +11:00
clydebarrow d94705d359 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-10 16:38:21 +11:00
clydebarrowandClaude Opus 5.5 e4b8fa7f89 [packet_interface] Take a chosen destination only through the broadcast port, and never this device's own address
A port to one peer may have layers that keep state for that peer, such as reliability, a session or a rolling code,
which a packet sent elsewhere would break. A packet to this device's own address is dropped by every receiver, as
metadata copied from a received packet would give. Both are now refused with a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:38:14 +11:00
clydebarrow 5c24d1c66f Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-10 16:30:37 +11:00
clydebarrow 384982896e Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-10 16:30:37 +11:00
clydebarrow 0edc6fffec Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-10 16:30:37 +11:00
clydebarrowandClaude Opus 5.5 29da0e8e36 [entity_transport] Rate limit each kind of dropped packet warning separately, and log each new provider's dictionary
One limit covered every kind of drop, so a steady stream of one kind, such as a neighbour on other firmware, could
take nearly every warning and leave a rarer kind seldom reported. The name of the device that sent each new
dictionary is now logged at debug level, so a provider that matches no device heard is easy to spot.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 16:30:36 +11:00
clydebarrow 05a1d9a3cc Merge branch 'clydebarrow/packet-interface-05' into clydebarrow/packet-interface-06 2026-11-10 16:30:36 +11:00
clydebarrow 40f926cfaf Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-10 15:55:51 +11:00
clydebarrowandClaude Opus 5.5 d48756a858 [sx126x][sx127x] Warn about sending over a busy radio only once the radio has the packet
A log line can run an automation that sends again, which would overwrite the buffer of a layer above before the
radio had read it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:55:50 +11:00
clydebarrow 6852a909bf Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08 2026-11-10 15:55:50 +11:00
clydebarrow f49008ad2c Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-10 15:55:02 +11:00
clydebarrowandClaude Opus 5.5 a608cacc33 [packet_interface] Refuse a send through the encryption or fragmentation layer while one is in progress
A log line further down, such as the radio's warning that it is sending although busy, can run an automation that
sends again before the layer below has read the layer's buffer. The nested send overwrote that buffer, so the outer
packet went out with the nested one's bytes. PacketInterface gets a helper that refuses such a send, which callers
already handle as a send that was not accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:54:52 +11:00
clydebarrow 01478f7519 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-10 15:48:56 +11:00
clydebarrow dcc296afc9 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08
# Conflicts:
#	esphome/components/entity_transport/__init__.py
#	tests/unit_tests/test_entity_transport.py
2026-11-10 15:48:54 +11:00
clydebarrow e190c1cea6 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-10 15:48:19 +11:00
clydebarrow ce090adf57 Merge branch 'clydebarrow/packet-interface-05' into clydebarrow/packet-interface-06
# Conflicts:
#	esphome/components/entity_transport/__init__.py
#	esphome/components/packet_interface/__init__.py
#	tests/unit_tests/test_entity_transport.py
2026-11-10 15:48:17 +11:00
clydebarrowandClaude Opus 5.5 7a4e9a8e08 [entity_transport] Take the id of a packet interface layer as standing for its link
Applications send through top(), so the id of a layer still uses every layer of its link. Final validation now
finds the link from a layer's id and checks its layers, where the previous commit rejected the id with a wrong
reason.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:47:25 +11:00
clydebarrow c5aabcb910 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09 2026-11-10 15:13:33 +11:00
clydebarrow 4032f1aa21 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08
# Conflicts:
#	esphome/components/entity_transport/__init__.py
#	tests/unit_tests/test_entity_transport.py
2026-11-10 15:13:31 +11:00
clydebarrow 2df3d6c901 Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07 2026-11-10 15:13:01 +11:00
clydebarrow 98f94729d0 Merge branch 'clydebarrow/packet-interface-05' into clydebarrow/packet-interface-06
# Conflicts:
#	esphome/components/packet_interface/__init__.py
#	tests/unit_tests/test_entity_transport.py
2026-11-10 15:12:53 +11:00
clydebarrowandClaude Opus 5.5 dbfa5d212d [entity_transport] Reject the id of a packet interface layer, and log packets dropped for carrying our own dictionary id
An application given the id of a layer would leave out the layers above it, and final validation found no
interface for it, so its checks were skipped without an error. A packet with our own dictionary id that is not a
request is now logged at verbose level, as another device with the same name and entities sends such packets.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 15:12:05 +11:00
clydebarrowandClaude Opus 5.5 447e2a0fd5 [espnow] Use cg.static_buffer for the packet interface copy buffer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:02:14 +11:00
clydebarrowandClaude Opus 5.5 a9ffe139ca Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:02:02 +11:00
clydebarrowandClaude Opus 5.5 26a67736ed [tcp] Use cg.static_buffer for the packet interface receive buffer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:01:16 +11:00
clydebarrowandClaude Opus 5.5 629347dad0 Merge branch 'clydebarrow/packet-interface-07' into clydebarrow/packet-interface-08
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:01:01 +11:00
clydebarrowandClaude Opus 5.5 98d288e7bc Merge branch 'clydebarrow/packet-interface-06' into clydebarrow/packet-interface-07
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:00:59 +11:00
clydebarrowandClaude Opus 5.5 97f189578c Merge branch 'clydebarrow/packet-interface-05' into clydebarrow/packet-interface-06
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:00:57 +11:00
clydebarrowandClaude Opus 5.5 9255b8c43c Merge remote-tracking branch 'upstream/dev' into clydebarrow/packet-interface-05
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 15:00:56 +11:00
clydebarrowandClaude Opus 5.5 73a5d3a003 Merge branch 'clydebarrow/packet-interface-08' into clydebarrow/packet-interface-09
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 14:56:21 +11:00
clydebarrow 968a010448 Merge branch 'pi07' into pi08 2026-10-25 18:06:19 +11:00
clydebarrow 4d299da205 Merge branch 'pi06' into pi07 2026-10-25 18:06:18 +11:00
clydebarrow c691a71cc5 Merge branch 'pi05' into pi06 2026-10-25 18:06:18 +11:00
clydebarrow 07164fc8b1 Merge remote-tracking branch 'upstream/dev' into pi05
# Conflicts:
#	esphome/components/packet_interface/__init__.py
#	esphome/core/defines.h
2026-10-25 18:06:16 +11:00
clydebarrowandClaude Opus 5.5 1f67fdbd4c [espnow] Warn now and then when a packet interface link's sends keep failing
A failure that repeats on every send, such as a peer that could not be
added, was logged only at verbose level. The error name helper is now
shared so the link can name the error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydebarrowandClaude Opus 5.5 9f6bde2436 [espnow][udp] Log oversized ESP-NOW packets, and name any sender in the UDP drop warning
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydebarrowandClaude Opus 5.5 d77c30f0bc [espnow][udp] Fixes from code review
- Add point_to_point to the UDP link, to override the guess that an
  address ending in .255 is a broadcast address, for subnets other than
  /24
- Warn, at most once a minute, when a point to point UDP link drops a
  packet from another sender
- Say that on ESP8266 an ESP-NOW peer link also receives the peer's
  broadcasts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydebarrowandClaude Code 06a9153011 [espnow][udp] Take the packet size from the copy buffer
The ESP-NOW link derives max_packet_size from the span it is given, as
the UDP point-to-point link already does.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydebarrowandClaude Opus 5.5 3aebba2cdf [espnow][udp] Fixes from code review
- Reject reliability, session and the rolling code on a broadcast UDP or
  ESP-NOW link without addressing, as every device hears the link
- Test the UDP and ESP-NOW final validation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydebarrowandClaude Opus 5.5 e6e1cb7540 [espnow] Keep the copy buffer in static memory
Code generation declares the ESP-NOW link's buffer for packets split
across buffers as a static array of max_packet_size bytes, so it is not
allocated at run time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydebarrowandClaude Opus 5.5 0c60a15d12 [espnow][udp] Add ESP-NOW packet interface link, and UDP point to point
The ESP-NOW link sends to one peer, which also provides addressing, or
broadcasts to every device on the channel. The UDP link accepts packets
only from the peer when the udp component sends to one unicast IPv4
address, and then provides addressing; the udp component now reports the
sender of each packet to its listeners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:41 +11:00
clydeandClaude Opus 5.5 fe19e264ab [entity_transport] Reject a timeout on a link that does the session's job
TCP provides the session itself, so its config has no session key and
the timeout check let it through. update_interval then defaults to
never, and unchanged states timed out on a working link. Record the
layers each link class provides and check those too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 9f57216b9d [sx126x][sx127x] Stop reading a radio that fails while sends are held back
Also drop a leftover preamble_detect key from a test case.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 923893f6df [packet_interface] Name a peer in the error for a link used with peers
The link may have only one peer, so the error no longer says several.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydeandClaude Opus 5.5 a77653a0b3 [tcp] Apply clang-format to the packet interface test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 6d3b256191 [sx127x] Count only the sync word match as a packet arriving without LoRa
The preamble flag stays set when no sync word follows, so a packet from
another network held every send back until max_busy ran out. A link
without LoRa now needs a sync_value.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 72f767cb4f [packet_interface] Log packets and fragments too short for their header
Every other drop in these layers is logged at verbose level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 861e0aa0da [packet_interface] Share the search for a layer that needs a session between both checks
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 f3e727dc48 [sx126x] Require a sync word for a link with FSK
Without one the radio cannot tell that a packet is arriving, so listen
before talk does nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 681d93ddba [tcp] Explain why the rolling code cannot be used on a TCP link
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 36799051e4 [sx126x][sx127x] Fixes from code review
- Without LoRa, require the SX127x radio to have a sync word or preamble
  detection, as listen before talk cannot see a packet arriving otherwise
- Warn once when a send finds the radio failed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 dd26efb0ca [tcp] Fixes from code review
- Publish the connected sensor as off at setup, so a link that never
  comes up does not show as unknown
- Add a timeout option, defaulting to three keep-alive intervals and
  longer than one, as the peer's keep_alive sets how often packets
  arrive; limit keep_alive to 1 h and timeout to 3 h
- Load binary_sensor for an empty config from tooling
- Name the layer in the error when a provided layer is configured

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 d4d91bc9d1 [sx126x][sx127x] Fixes from code review
- Warn when a send goes ahead over a busy period at its limit, as that
  means a stuck busy indication or a jammed channel
- Log an empty packet at verbose level, and say why it counts as sent
- Name the radio id if code generation cannot find its configuration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Code 8b5e3a1739 [tcp] Take the packet size from the receive buffer
The link takes the buffer as a std::span and derives max_packet_size
from its size, so a length beyond the buffer is refused and the
connection is closed.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 ff6d33b39b [sx126x][sx127x] Say which callers send a refused packet again
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 df58794be0 [packet_interface] Keep App alive across tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 b0632892c5 [sx126x][sx127x] Require rx_start for layers that need replies
- Reject a radio with rx_start false when the link or a peer has a layer
  that needs packets back from the peer, as the radio sleeps after sending
- Log a send over a long busy period at debug level, so a lost reception
  can be traced

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 01e7827898 [packet_interface] Fix clang-tidy findings in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 dcc7c92a71 [sx126x][sx127x] Share the radio link code, and fixes from code review
- Make both links one RadioLink template in packet_interface, with shared
  code generation in radio_link.py, so a fix applies to both radios
- While sending is held back, watch the radio from loop() and end the
  busy period once it is free, so a later reception starts a new period
  instead of being sent over
- Reject an SX127x link without LoRa unless the radio is in packet mode

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 de5652f845 [tcp] Fixes from code review
- Reserve the link's sockets, so the socket pool is sized for them
- Test the server role: accepting a client, receiving from it, and
  accepting the next after it disconnects
- Test a frame assembled across several reads, after a keep-alive

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Code 4614562856 [sx126x][sx127x] Take the packet size at construction
The radio links have no buffer of their own, so code generation passes
the configured max_packet_size to the constructor.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 4a26a38cee [tcp] Keep the receive buffer in static memory
Code generation declares the TCP link's receive buffer as a static array
of max_packet_size bytes, so it is not allocated at run time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 de6a9d5820 [sx126x][sx127x] Fixes from code review
- Hold a send back for at most the airtime of the largest packet, worked
  out from the radio settings and at least 2 s, instead of a fixed 2 s
  that a LoRa packet at a high spreading factor outlasts
- End a busy period when a send finds the radio free, so a busy flag that
  never clears is released however rarely sends are tried
- Detect an FSK reception on the SX127x from its preamble and sync word
  flags
- Test the radio links' final validation

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 4e3f2b0e0b [packet_interface] Use a local to_vector() in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 cb28c2fc86 [sx126x][sx127x] Add packet interface links for SX126x and SX127x radios
Each packet is one radio packet, with the RSSI and SNR of received
packets in the metadata. A radio cannot hear while it sends, so a link
refuses to send while its radio is receiving a packet, for up to two
seconds, and the layers above send again shortly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 b04c8947e0 [tcp] Add TCP packet interface link
Packets go over one TCP connection, as a client that connects to a host
or a server that accepts one client at a time. TCP resends what is lost
and joins exactly two devices, so the link provides the reliability,
addressing and session layers itself, and those cannot be configured on
it. A link platform names such layers with the new provides argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:40 +11:00
clydebarrowandClaude Opus 5.5 b007bbb01c [packet_interface] Check at validation that fragments fit the reliability queue
- Work out each layer's packet size during validation, as the C++ does,
  and reject a fragmentation max_packet_size whose fragments do not all
  fit in the reliability queue at once, or need more than 128 fragments
- Fit the test peer's largest packet in its queue
- Reuse interface_layers() in final_validate_interface()
- Keep the earlier tests in place in test_packet_interface.py

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 7ac9ba6daf [packet_interface] Use the shared max_packet_size constant in the fragmentation layer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 8b752525f5 [entity_transport] Find an addressing peer's layers for the timeout check
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 07e1195ffa [packet_interface] Route broadcasts to the broadcast port, and keep a join
A broadcast from a device with its own port went to that port, whose
layers drop it, so a broadcast port could not reach reliable peers. It now
goes to the broadcast port when there is one.

Fragmentation allocates its buffers with RAMAllocator and fails setup when
they cannot be allocated. A fragment from another source no longer drops
the packet being joined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Code 5ef072d03d [packet_interface] Take the addressing and fragmentation sizes at construction
AddressingPort derives max_packet_size from the link in its constructor,
and the fragmentation layer passes its configured size to the base
constructor.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 4d61784ba6 [packet_interface] Keep App alive across tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 be4ea37f21 [packet_interface] Fix clang-tidy findings in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 9915cf6827 [packet_interface] Fixes from code review
- Reject an addressing destination or peer that is this device's own
  address, whose packets would be dropped as echoes
- Show 0 fragments, not SIZE_MAX, for a link with no room
- Test the check that applications use a peer's id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:39 +11:00
clydebarrowandClaude Opus 5.5 beeeee07e8 [packet_interface] Use a local to_vector() in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:21 +11:00
clydebarrowandClaude Opus 5.5 e9c386f57e [packet_interface] Leave no room, instead of underflowing, on a small link
As in the other layers, addressing ports and fragmentation get no room
when max_packet_size is smaller than their header, and fragmentation
fails its setup instead of dividing by zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:21 +11:00
clydebarrowandClaude Opus 5.5 01fbfeb16a [packet_interface] Add addressing and fragmentation layers
The addressing layer lets several devices share a channel such as an
RS485 bus or a radio, with 16-bit addresses. A link can carry several
peers, each with its own stack of layers and its own id. Layers that work
between two devices only are rejected with broadcast addressing. The
fragmentation layer splits packets larger than the layers below allow,
and joins them again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:21 +11:00
pre-commit-ci-lite[bot] 369354a116 apply automatic formatting fixes 2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 105d1bb088 [entity_transport] Warn when packets are refused for long, and check every complete dictionary
- Set a warning once the interface has accepted no packets for a minute,
  and log refused dictionary packets as data packets are
- Clear remote entities that a provider's dictionary does not name also
  when the provider goes back to a dictionary already seen

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 1488af2b32 [entity_transport] Fixes from code review
- Check a data packet before asking for its dictionary, so a damaged
  packet does not make the provider resend everything
- When a provider's complete dictionary does not name a remote entity,
  warn and clear that entity's state, as a typo in provider or remote_id,
  or an entity the provider no longer sends, was otherwise silent

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 74b8de8064 [entity_transport] Ask again for a dictionary with a lost part, and warn on dropped packets
A dictionary packet now carries its part index and a last-part flag. A dictionary counts as received, and is not
asked for again, only once its last part follows every other part in order. Until then, data under it sends a
request even when some remote entities are already known.

A refused request no longer starts the request interval. A short or malformed packet, or one with another format
version, is logged as a warning at most once a minute.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 5393d192b4 [entity_transport] Reject a timeout on a packet interface with a session layer
With a session and a reliability layer, the update interval defaults to
never, and unchanged states are not sent again, so a receiver's timeout
cleared them on a working link. The session layer already clears the
states when the other device cannot be reached.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 670934b3d8 [entity_transport] Tag each data record, and check whole packets before use
Each data record now starts with a 16-bit tag holding the entity's number
and type, and the form of its payload, so that more entity types can be
added and a receiver can skip records of types it does not know. A binary
sensor's state is in the tag; a sensor has a 4 byte state, or none.

Data and dictionary packets are checked in full before anything in them
is used, so a damaged or forged packet changes no state and teaches nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 f76dce46aa [entity_transport] Use the shared provider and remote id constants
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 d44bccba3f [entity_transport] Ask only for dictionaries not yet received
A dictionary that names none of the remote entities was asked for again
every 5 seconds while any remote entity was unresolved, and each request
made its provider resend everything. A provider's new dictionary was not
asked for once every remote entity was resolved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 5b35282546 [entity_transport] Fail when the packet buffer cannot be allocated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Code aa1d7fa26b [entity_transport] Set the test link's size at construction
The mock link takes max_packet_size in its constructor, so a test that
needs a different size builds a local link.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 3862504cdd [packet_interface] Keep App alive across tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 ee3efdf15e [entity_transport] Brace multi-line if bodies
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 db46ed33a2 [packet_interface] Fix clang-tidy findings in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 e09257a992 [packet_interface] Use a local to_vector() in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 1fa3dd37fe [entity_transport] Fixes from code review
- Compare received ids by length and bytes, so an id with an embedded NUL
  cannot read past the configured string
- Fail setup when max_packet_size cannot hold the device name and the
  longest entity id, instead of skipping the id at every resend
- Reject two entities of one type sent with the same id
- Avoid an unused variable without remote entities
- Use matching ids in the test configuration

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydebarrowandClaude Opus 5.5 a7ca6a9608 [entity_transport] Add entity transport component
Sends sensor and binary sensor states to other devices over a packet
interface. A dictionary sent on connect, on request and at an interval
names the entities, so data packets refer to them by number. The update
interval and the handling of a lost peer follow the layers the interface
has, and remote states can time out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:03 +11:00
clydeandClaude Opus 5.5 e046551825 [packet_interface] Stretch the XXTEA key text with PBKDF2
A single SHA-256 of the key text let a captured packet check about 2^30
guesses for every one that passes, so a weak key text fell quickly to
an offline search. Derive the key with PBKDF2-HMAC-SHA256 at 600,000
iterations instead; this runs at build time, so the device pays nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:39:00 +11:00
pre-commit-ci-lite[bot] 18b5b7a24a apply automatic formatting fixes 2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 179d20ba77 [packet_interface] Limit the session keep_alive to 1 h and timeout to 3 h
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 1bc4779d96 [packet_interface] Fail the XXTEA layer when its buffers cannot be allocated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 366713ba05 [packet_interface] Check the layers in the link schema
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 47b8aae1b5 [packet_interface] Require a session layer with the rolling code
The rolling code accepts a packet only once the peer has sent back this
device's boot number, so a device that only receives never gets a packet.
The session's keep-alives make sure packets go both ways.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
pre-commit-ci-lite[bot] a9c155e4b4 apply automatic formatting fixes 2026-10-23 12:28:10 +10:00
clydebarrowandClaude Code 708e117a20 [packet_interface] Take the session and encryption sizes at construction
Both layers derive max_packet_size from the layer below in their
constructors instead of assigning it in the body.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 c6d1c63884 [packet_interface] Keep App alive across tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 faef5a98bf [packet_interface] Fix clang-tidy findings in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 7c86318c1a [packet_interface] Use a local to_vector() in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 58ef6ef3d9 [packet_interface] Fixes from code review
- Fail setup, instead of underflowing, when max_packet_size leaves the
  encryption or session layer no room
- Warn when the boot number cannot be saved
- Warn once when the peer's packets carry an older boot number, which
  happens when its flash is erased
- Test the counter wrapping to a new boot number

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 a6d2ae15a1 [packet_interface] Add session and XXTEA encryption layers
The session layer sends keep-alives and reports whether the peer can be
reached, including after it restarts or a reliability layer below loses
data. The encryption layer encrypts and checks each packet with XXTEA,
with an optional rolling code against replays. Components that a layer
needs, such as binary_sensor, are loaded only when it is configured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 63cfdf2b4d [packet_interface] Reject a layer type registered after the schemas are built
Such a type would be missing from the link schema, and selecting it would
fail with an unrelated error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 6411a8062f [packet_interface] Log a full queue during a link reset only once
While the peer is away the layers above keep sending, so the message
repeated about once a second until the link came up again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 c18ac5bac9 [packet_interface] Count the link reset tries outside the condition
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 78396dd142 [packet_interface] Warn when the HDLC link cannot come up
A frame larger than max_packet_size, usually from a peer with a larger
setting, is warned about once. A link reset that the peer does not answer
is warned about after max_retries tries, and a full queue is logged at
debug level while the link is down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Code 557e3c3e9e [packet_interface] Reject a duplicate layer type name
Every module in layers/ is imported automatically, so two modules
registering the same name silently replaced one with the other, in an
order decided by pkgutil. Registration now raises, so the mistake is
found when the configuration is loaded.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Code 8fd05e93b4 [packet_interface] Answer a repeated SABM without resetting the link
A peer that has not heard the UA repeats its SABM, and cannot have sent a
numbered frame meanwhile. Treating that as a restart threw away queued
frames and reported the session lost for a single dropped UA. A SABM is
now a restart only if the peer has sent a numbered frame since the link
came up.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Code f81c6847f3 [packet_interface] Take the reliability layer's size at construction
max_packet_size is derived from the layer below in the constructor
instead of being assigned in the body.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 d555d64300 [packet_interface] Fixes from code review
- Keep App alive across the HDLC tests, as the test main requires
- Run a layer's loop in the tests only while it has asked for it, so
  the loop's disable and request bookkeeping is tested, and wait for the
  retry timer in the tests of refused sends
- Report no send capacity while the layer has no send queue

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 267e3d5d70 [packet_interface] Fix clang-tidy findings in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 2da760ea4f [packet_interface] Use a local to_vector() in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 0b34f811a4 [packet_interface] Fixes from code review
- Reset the link after max_retries even when it is 255, where the retry
  counter used to wrap
- Require a reliability timeout of at least 50 ms
- Drop received frames larger than the layer's max_packet_size
- Fail setup, instead of aborting, when the send queue cannot be
  allocated or max_packet_size leaves no room
- Test the ack delay timer and a refused REJ

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Opus 5.5 28833fe0f3 [packet_interface] Add layer registry and HDLC reliability layer
Layers stack on a link and are configured on its packet_interface entry.
Each layer type registers itself with its kind, so a new type needs only
a new module. The first is a reliability layer in the style of HDLC
asynchronous balanced mode, with go-back-N retransmission.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:28:10 +10:00
clydebarrowandClaude Code 929aac8fac [udp] Take the packet size from the copy buffer
The link derives max_packet_size from the span it is given, so a packet
larger than the copy buffer cannot be copied into it.

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 11cc5bb008 [udp] Limit a split packet to the copy buffer
The copy buffer is sized by code generation to max_packet_size, but
set_max_packet_size() can raise the limit afterwards, and copying a split
packet would then overflow it. The link now takes the buffer as a span
and refuses a split packet larger than it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 2d6be6c27a [udp] Drop datagrams longer than the receive buffer
A read cuts a datagram longer than the buffer to fit, so a 600 byte
datagram arrived as a valid looking 508 byte packet. The buffer is now one
byte longer, and a read that fills it is dropped. The split packet test
also checks the bytes joined in the copy buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 bfa6e68039 [packet_interface] Fix clang-tidy findings in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 c39566f312 [udp] Keep the copy buffer in static memory
Code generation declares the UDP link's buffer for packets split across
buffers as a static array of max_packet_size bytes, so it is not
allocated at run time.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 5197140f2c [packet_interface] Use a local to_vector() in tests
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 ee0c6a412e [udp] Fixes from code review
- Enforce max_packet_size on send and receive
- Copy split packets into a buffer allocated in setup, not on the stack
- Add unit tests for the link's send and receive paths
- Explain why the broadcast socket is always enabled

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
clydebarrowandClaude Opus 5.5 373c6398b2 [udp] Add UDP packet interface link
Each packet is one UDP datagram, using the settings of the udp component.
The udp component now reports whether a packet was sent, and an ENOMEM
refusal from lwIP is logged at verbose level as the packet can be sent
again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 13:28:08 +11:00
24 changed files with 792 additions and 20 deletions

No files matched your search

+1
View File
@@ -227,6 +227,7 @@ esphome/components/esp_ldo/* @clydebarrow
esphome/components/espectre/* @francescopace
esphome/components/esphome/ota/* @esphome/core
esphome/components/espnow/* @jesserockz
esphome/components/espnow/packet_interface/* @clydebarrow
esphome/components/espnow/packet_transport/* @EasilyBoredEngineer
esphome/components/ethernet_info/* @gtjadsonsantos
esphome/components/event/* @nohat
@@ -24,7 +24,7 @@ ESPHOME_LOG_TAG(TAG, "espnow");
ESPNowComponent *global_esp_now = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static const LogString *espnow_error_to_str(esp_err_t error) {
const LogString *espnow_error_to_str(esp_err_t error) {
switch (error) {
case ESP_ERR_ESPNOW_FAILED:
return LOG_STR("ESPNow is in fail mode");
@@ -25,6 +25,9 @@ namespace esphome::espnow {
using peer_address_t = std::array<uint8_t, ESP_NOW_ETH_ALEN>;
/// A short description of an ESP-NOW error code, for logging.
const LogString *espnow_error_to_str(esp_err_t error);
enum class ESPNowTriggers : uint8_t {
TRIGGER_NONE = 0,
ON_NEW_PEER = 1,
@@ -123,6 +126,7 @@ class ESPNowComponent final : public Component {
uint8_t get_wifi_channel();
void set_auto_add_peer(bool value) { this->auto_add_peer_ = value; }
bool get_auto_add_peer() const { return this->auto_add_peer_; }
#if defined(USE_WIFI) && defined(USE_WIFI_CONNECT_STATE_LISTENERS)
// WiFiConnectStateListener interface: refresh the cached channel after each (re)connect
@@ -148,6 +152,10 @@ class ESPNowComponent final : public Component {
return this->send(peer_address, payload.data(), payload.size(), std::move(callback));
}
esp_err_t send(const uint8_t *peer_address, const uint8_t *payload, size_t size, send_callback_t callback = nullptr);
/// How many more packets send() can queue now; they share one pool.
size_t get_send_queue_free() const {
return MAX_ESP_NOW_SEND_QUEUE_SIZE - 1 - this->send_packet_queue_.size() - (this->current_send_packet_ != nullptr);
}
void register_receive_handler(ESPNowReceivedPacketHandler *handler) { this->receive_handlers_.push_back(handler); }
void register_unknown_peer_handler(ESPNowUnknownPeerHandler *handler) {
@@ -0,0 +1,98 @@
import esphome.codegen as cg
from esphome.components import packet_interface
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,
check_broadcast_link,
)
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.core import HexInt, MACAddress
import esphome.final_validate as fv
from esphome.types import ConfigType
from .. import (
CONF_MAX_PAYLOAD_SIZE,
DOMAIN as ESPNOW_DOMAIN,
ESPNowComponent,
espnow_ns,
)
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["espnow"]
CONF_ESPNOW_ID = "espnow_id"
CONF_PEER_ADDRESS = "peer_address"
# The ESP-NOW v1 payload; a larger max_payload_size on the espnow component allows a larger max_packet_size
MAX_PACKET_SIZE = 250
BROADCAST_MAC = "FF:FF:FF:FF:FF:FF"
ESPNowPacketInterface = espnow_ns.class_(
"ESPNowPacketInterface", packet_interface.PacketInterface
)
def _is_broadcast(config: ConfigType) -> bool:
return str(config[CONF_PEER_ADDRESS]) == BROADCAST_MAC
def _peer_mac(value: object) -> MACAddress:
"""A device's own address, or the broadcast address; a group or all-zero address names no one device."""
mac = cv.mac_address(value)
if str(mac) != BROADCAST_MAC and (mac.parts[0] & 1 or not any(mac.parts)):
raise cv.Invalid(
f"'{mac}' is not the address of a device; use a device's MAC address, or {BROADCAST_MAC}"
)
return mac
def _validate_addressing(config: ConfigType) -> ConfigType:
if _is_broadcast(config):
check_broadcast_link(config)
elif CONF_ADDRESSING in config:
raise cv.Invalid(
f"Packets to one '{CONF_PEER_ADDRESS}' are already addressed, so '{CONF_ADDRESSING}' is only "
"for broadcast",
path=[CONF_ADDRESSING],
)
return config
CONFIG_SCHEMA = cv.All(
packet_interface_schema(ESPNowPacketInterface, MAX_PACKET_SIZE, 1470).extend(
{
cv.GenerateID(CONF_ESPNOW_ID): cv.use_id(ESPNowComponent),
cv.Optional(CONF_PEER_ADDRESS, default=BROADCAST_MAC): _peer_mac,
}
),
_validate_addressing,
)
def _validate_packet_size(config: ConfigType) -> ConfigType:
payload = fv.full_config.get()[ESPNOW_DOMAIN][CONF_MAX_PAYLOAD_SIZE]
if config[CONF_MAX_PACKET_SIZE] > payload:
raise cv.Invalid(
f"Must not be larger than the espnow {CONF_MAX_PAYLOAD_SIZE} of {payload}",
path=[CONF_MAX_PACKET_SIZE],
)
return config
FINAL_VALIDATE_SCHEMA = _validate_packet_size
async def to_code(config: ConfigType) -> None:
parent = await cg.get_variable(config[CONF_ESPNOW_ID])
peer = [HexInt(x) for x in config[CONF_PEER_ADDRESS].parts]
# Added now, also the broadcast address, so no peer is added after setup
cg.add(parent.add_peer(peer))
tx_buffer = cg.static_buffer(
config[CONF_ID], cg.uint8, config[CONF_MAX_PACKET_SIZE]
)
await new_packet_interface(config, parent, tx_buffer, peer)
@@ -0,0 +1,159 @@
#include "espnow_packet_interface.h"
#if defined(USE_ESP32) || defined(USE_ESP8266)
#include <cstring>
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome::espnow {
using packet_interface::PacketBuffer;
using packet_interface::PacketMetaData;
ESPHOME_LOG_TAG(TAG, "espnow.packet_interface");
static constexpr uint32_t WARNING_INTERVAL_MS = 60000;
ESPNowPacketInterface::ESPNowPacketInterface(ESPNowComponent *parent, std::span<uint8_t> tx_buffer,
peer_address_t peer_address)
: packet_interface::PacketInterface(tx_buffer.size()),
parent_(parent),
tx_buffer_(tx_buffer),
peer_address_(peer_address) {
if (!this->is_broadcast_())
this->capabilities_ = packet_interface::CAPABILITY_ADDRESSING;
}
void ESPNowPacketInterface::setup() {
// Each mode hears only its own kind of packet, so a peer link and a broadcast link can share the component
if (!this->is_broadcast_() || !ESPNOW_REPORTS_DESTINATION)
this->parent_->register_receive_handler(this);
if (this->is_broadcast_()) {
this->parent_->register_broadcast_handler(this);
this->parent_->register_unknown_peer_handler(this);
}
}
void ESPNowPacketInterface::dump_config() {
char peer_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
format_mac_addr_upper(this->peer_address_.data(), peer_buf);
ESP_LOGCONFIG(TAG,
"ESP-NOW Packet Interface '%s':\n"
" Peer: %s\n"
" Max Packet Size: %zu",
this->name_, peer_buf, this->max_packet_size_);
}
bool ESPNowPacketInterface::send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) {
// Layers above are set up first, and may send while setup waits for another component
if (!this->is_ready())
return false;
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;
}
const uint8_t *payload;
if (data.is_contiguous()) {
payload = data.as_span().data();
} else {
data.copy_to(this->tx_buffer_.data(), size);
payload = this->tx_buffer_.data();
}
// Only a unicast send learns whether the peer received it
esp_err_t err = this->is_broadcast_()
? this->parent_->send(this->peer_address_.data(), payload, size)
: this->parent_->send(this->peer_address_.data(), payload, size, [this](esp_err_t status) {
if (status != ESP_OK)
this->delivery_failed_();
});
if (err != ESP_OK) {
this->send_failed_(err);
return false;
}
return true;
}
// A full send queue is logged by the espnow component, and ESP-NOW is not running before setup or while disabled.
// Other errors, such as a peer that could not be added, repeat on every send, so warn now and then
void ESPNowPacketInterface::send_failed_(esp_err_t err) {
uint32_t now = App.get_loop_component_start_time();
if (err == ESP_ERR_ESPNOW_NO_MEM || err == ESP_ERR_ESPNOW_NOT_INIT ||
(this->send_warned_ && now - this->last_send_warning_ms_ < WARNING_INTERVAL_MS)) {
ESP_LOGV(TAG, "%s: Send failed: %s", this->name_, LOG_STR_ARG(espnow_error_to_str(err)));
return;
}
this->send_warned_ = true;
this->last_send_warning_ms_ = now;
ESP_LOGW(TAG, "%s: Send failed: %s", this->name_, LOG_STR_ARG(espnow_error_to_str(err)));
}
// A peer that is off, out of range or has another address never acknowledges; without a reliability layer above,
// nothing else would report that, so warn now and then
void ESPNowPacketInterface::delivery_failed_() {
uint32_t now = App.get_loop_component_start_time();
if (this->delivery_warned_ && now - this->last_delivery_warning_ms_ < WARNING_INTERVAL_MS) {
ESP_LOGV(TAG, "%s: Packet not acknowledged by the peer", this->name_);
return;
}
this->delivery_warned_ = true;
this->last_delivery_warning_ms_ = now;
ESP_LOGW(TAG, "%s: Packet not acknowledged by the peer; it may be off, out of range, or have another address",
this->name_);
}
// Every larger packet from a peer whose max_packet_size is larger is lost, so warn now and then
void ESPNowPacketInterface::oversized_(uint16_t size) {
uint32_t now = App.get_loop_component_start_time();
if (this->size_warned_ && now - this->last_size_warning_ms_ < WARNING_INTERVAL_MS) {
ESP_LOGV(TAG, "%s: Packet of %u bytes is larger than %zu, dropped", this->name_, size, this->max_packet_size_);
return;
}
this->size_warned_ = true;
this->last_size_warning_ms_ = now;
ESP_LOGW(TAG, "%s: Packet of %u bytes is larger than %zu, dropped; set the same max_packet_size on both devices",
this->name_, size, this->max_packet_size_);
}
bool ESPNowPacketInterface::on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
this->receive_(info, data, size);
return false;
}
bool ESPNowPacketInterface::on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
this->receive_(info, data, size);
return false;
}
bool ESPNowPacketInterface::on_unknown_peer(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
// Only known peers reach the other handlers; with auto_add_peer this sender is added and arrives there
if (this->parent_->get_auto_add_peer())
return false;
if (!ESPNOW_REPORTS_DESTINATION || memcmp(info.des_addr, ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0)
this->receive_(info, data, size);
return false;
}
void ESPNowPacketInterface::receive_(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
if (size == 0)
return;
if (!this->is_broadcast_() && memcmp(info.src_addr, this->peer_address_.data(), ESP_NOW_ETH_ALEN) != 0)
return;
if (size > this->max_packet_size_) {
this->oversized_(size);
return;
}
PacketMetaData meta_data;
memcpy(meta_data.mac_address, info.src_addr, ESP_NOW_ETH_ALEN);
#ifdef USE_ESP32
meta_data.rssi = info.rx_ctrl->rssi;
#endif
this->on_receive_from_interface_(PacketBuffer(data, size), meta_data);
}
} // namespace esphome::espnow
#endif // USE_ESP32 || USE_ESP8266
@@ -0,0 +1,67 @@
#pragma once
#include "../espnow_component.h"
#if defined(USE_ESP32) || defined(USE_ESP8266)
#include <cstring>
#include <span>
#include "esphome/components/packet_interface/packet_interface.h"
namespace esphome::espnow {
/**
* A packet interface link over ESP-NOW.
*
* With a peer address other than broadcast, packets go to that device only, and only packets sent to this device by
* it are received, so the link provides the addressing capability itself. With the broadcast address, packets go to
* every device on the channel and every broadcast packet is received, from known and unknown senders; an addressing
* layer then tells the devices apart. ESP8266 does not report a packet's destination, so there a broadcast link also
* receives packets sent to this device, and a peer link also receives the peer's broadcasts.
*
* ESP-NOW queues each packet and reports later whether it was delivered; a reliability layer above resends what is
* lost.
*/
class ESPNowPacketInterface : public packet_interface::PacketInterface,
public ESPNowReceivedPacketHandler,
public ESPNowBroadcastHandler,
public ESPNowUnknownPeerHandler {
public:
/// @param tx_buffer Joins packets split across buffers; its size is the largest packet, and it must outlive the link
ESPNowPacketInterface(ESPNowComponent *parent, std::span<uint8_t> tx_buffer, peer_address_t peer_address);
void setup() override;
void dump_config() override;
bool send_to_interface(const packet_interface::PacketBuffer &data,
const packet_interface::PacketMetaData &meta_data) override;
/// The free slots in the ESP-NOW send queue, which every link on this device shares.
size_t get_send_capacity() const override { return this->parent_->get_send_queue_free(); }
bool on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) override;
bool on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) override;
bool on_unknown_peer(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) override;
protected:
bool is_broadcast_() const {
return memcmp(this->peer_address_.data(), ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0;
}
void receive_(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size);
void send_failed_(esp_err_t err);
void delivery_failed_();
void oversized_(uint16_t size);
ESPNowComponent *parent_;
const std::span<uint8_t> tx_buffer_;
uint32_t last_send_warning_ms_{0};
uint32_t last_delivery_warning_ms_{0};
uint32_t last_size_warning_ms_{0};
peer_address_t peer_address_;
bool send_warned_{false};
bool delivery_warned_{false};
bool size_warned_{false};
};
} // namespace esphome::espnow
#endif // USE_ESP32 || USE_ESP8266
@@ -60,6 +60,7 @@ class AddressingPort : public PacketInterface {
* packet to this device's own address is refused, as every receiver drops it.
*/
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override;
size_t get_send_capacity() const override { return this->addressing_->get_link()->get_send_capacity(); }
uint16_t get_destination() const { return this->destination_; }
protected:
@@ -55,7 +55,7 @@ void BasicFragmentation::dump_config() {
" Fragments in the Largest Packet: %zu",
this->name_, this->max_packet_size_, this->fragment_size_(), fragments);
if (this->queue_too_short_) {
ESP_LOGW(TAG, " The reliability queue is too short for the largest packets, which cannot be sent");
ESP_LOGW(TAG, " The layer below has room for too few fragments for the largest packets, which cannot be sent");
}
}
@@ -240,6 +240,21 @@ def check_packet_sizes(config: ConfigType, size: int) -> int:
return size
def check_broadcast_link(config: ConfigType) -> None:
"""On a link that every device hears, layers that keep state for one peer need an addressing layer."""
if CONF_ADDRESSING in config:
return
for kind in STACK_ORDER:
if (layer := config.get(kind.key)) is not None and kind.get_type(
layer
).single_peer(layer):
raise cv.Invalid(
f"This '{kind.key}' works between two devices only, but every device hears this link, so it needs "
f"'{CONF_ADDRESSING}' with a '{CONF_DESTINATION}'",
path=[kind.key],
)
def stack_auto_load(config: ConfigType) -> set[str]:
"""The components that the layers configured in config need loaded."""
loads: set[str] = set()
@@ -1,3 +1,5 @@
from ipaddress import IPv4Address
import esphome.codegen as cg
from esphome.components import packet_interface
from esphome.components.packet_interface import (
@@ -5,15 +7,24 @@ from esphome.components.packet_interface import (
new_packet_interface,
packet_interface_schema,
)
from esphome.components.packet_interface.layers import (
CONF_ADDRESSING,
check_broadcast_link,
)
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.core import CORE, HexInt
import esphome.final_validate as fv
from esphome.types import ConfigType
from .. import CONF_UDP_ID, UDP_SCHEMA, udp_ns
from .. import CONF_ADDRESSES, CONF_UDP_ID, DOMAIN as UDP_DOMAIN, UDP_SCHEMA, udp_ns
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["udp"]
CONF_POINT_TO_POINT = "point_to_point"
# Size of the receive buffer of the udp component
MAX_PACKET_SIZE = 508
@@ -22,9 +33,62 @@ UdpPacketInterface = udp_ns.class_(
packet_interface.PacketInterface,
)
CONFIG_SCHEMA = packet_interface_schema(
UdpPacketInterface, MAX_PACKET_SIZE, MAX_PACKET_SIZE
).extend(UDP_SCHEMA)
CONFIG_SCHEMA = (
packet_interface_schema(UdpPacketInterface, MAX_PACKET_SIZE, MAX_PACKET_SIZE)
.extend(UDP_SCHEMA)
.extend(
{
# Overrides the guess from the udp component's addresses, which cannot see the subnet
cv.Optional(CONF_POINT_TO_POINT): cv.boolean,
}
)
)
def _peer(udp_config: ConfigType, config: ConfigType) -> IPv4Address | None:
"""The one peer, when the UDP component sends to a single unicast IPv4 address.
Unless point_to_point says otherwise, an address ending in .255 is taken as a subnet broadcast, as the subnet is
not known here.
"""
point_to_point = config.get(CONF_POINT_TO_POINT)
match udp_config[CONF_ADDRESSES]:
# Neither all zeros nor all ones is one device's address
case [IPv4Address() as address] if not (
address.is_multicast
or address.is_unspecified
or address == IPv4Address("255.255.255.255")
):
if point_to_point is None:
point_to_point = address.packed[3] != 255
return address if point_to_point else None
if point_to_point:
raise cv.Invalid(
f"'{CONF_POINT_TO_POINT}' needs the UDP component to send to one unicast IPv4 address",
path=[CONF_POINT_TO_POINT],
)
return None
def _udp_config(full_config: ConfigType, config: ConfigType) -> ConfigType:
return next(
udp for udp in full_config[UDP_DOMAIN] if udp[CONF_ID] == config[CONF_UDP_ID]
)
def _final_validate(config: ConfigType) -> ConfigType:
if (peer := _peer(_udp_config(fv.full_config.get(), config), config)) is None:
check_broadcast_link(config)
elif CONF_ADDRESSING in config:
raise cv.Invalid(
f"The UDP component sends only to {peer}, so packets are already addressed; "
f"'{CONF_ADDRESSING}' is only for broadcast or multicast",
path=[CONF_ADDRESSING],
)
return config
FINAL_VALIDATE_SCHEMA = _final_validate
async def to_code(config: ConfigType) -> None:
@@ -32,7 +96,11 @@ async def to_code(config: ConfigType) -> None:
cg.add(udp_var.set_should_listen())
# The udp component sends to its addresses through its broadcast socket
cg.add(udp_var.set_should_broadcast())
peer = _peer(_udp_config(CORE.config, config), config)
peer_bytes = peer.packed if peer is not None else bytes(4)
tx_buffer = cg.static_buffer(
config[CONF_ID], cg.uint8, config[CONF_MAX_PACKET_SIZE]
)
await new_packet_interface(config, udp_var, tx_buffer)
await new_packet_interface(
config, udp_var, tx_buffer, [HexInt(x) for x in peer_bytes]
)
@@ -1,6 +1,8 @@
#include "udp_packet_interface.h"
#ifdef USE_NETWORK
#include <cstring>
#include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/log.h"
namespace esphome::udp {
@@ -10,11 +12,23 @@ using packet_interface::PacketMetaData;
ESPHOME_LOG_TAG(TAG, "udp.packet_interface");
void UdpPacketInterface::setup() {
this->parent_->add_listener([this](std::span<const uint8_t> data) { this->receive_(data); });
UdpPacketInterface::UdpPacketInterface(UDPComponent *udp_component, std::span<uint8_t> tx_buffer,
std::array<uint8_t, 4> peer)
: packet_interface::PacketInterface(tx_buffer.size()), parent_(udp_component), tx_buffer_(tx_buffer), peer_(peer) {
if (this->peer_ != std::array<uint8_t, 4>{})
this->capabilities_ = packet_interface::CAPABILITY_ADDRESSING;
}
void UdpPacketInterface::receive_(std::span<const uint8_t> data) {
void UdpPacketInterface::setup() {
this->parent_->add_listener(
[this](std::span<const uint8_t> data) { this->receive_(data, this->parent_->get_rx_source()); });
}
void UdpPacketInterface::receive_(std::span<const uint8_t> data, const sockaddr *source) {
if (!this->accepts_from_(source)) {
this->warn_dropped_(source);
return;
}
if (data.size() > this->max_packet_size_) {
ESP_LOGV(TAG, "%s: Packet of %zu bytes is larger than %zu, dropped", this->name_, data.size(),
this->max_packet_size_);
@@ -23,6 +37,28 @@ void UdpPacketInterface::receive_(std::span<const uint8_t> data) {
this->on_receive_from_interface_(PacketBuffer(data.data(), data.size()), {});
}
bool UdpPacketInterface::accepts_from_(const sockaddr *source) const {
if (!this->has_capability(packet_interface::CAPABILITY_ADDRESSING))
return true;
uint32_t address;
return socket::sockaddr_to_ipv4(source, &address) && memcmp(&address, this->peer_.data(), sizeof(address)) == 0;
}
void UdpPacketInterface::warn_dropped_(const sockaddr *source) {
static constexpr uint32_t WARNING_INTERVAL_MS = 60000;
uint32_t now = App.get_loop_component_start_time();
if (this->drop_warned_ && now - this->last_drop_warning_ms_ < WARNING_INTERVAL_MS)
return;
this->drop_warned_ = true;
this->last_drop_warning_ms_ = now;
char from[socket::SOCKADDR_STR_LEN];
socket::format_sockaddr_to(source, sizeof(sockaddr_storage), from);
ESP_LOGW(TAG,
"%s: Packet from %s dropped, as only %u.%u.%u.%u is accepted; set point_to_point: false if that is a "
"broadcast address",
this->name_, from, this->peer_[0], this->peer_[1], this->peer_[2], this->peer_[3]);
}
bool UdpPacketInterface::send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) {
size_t size = data.size();
if (size > this->max_packet_size_) {
@@ -4,9 +4,11 @@
// udp_component.h declares nothing without USE_NETWORK
#ifdef USE_NETWORK
#include <array>
#include <span>
#include "esphome/components/packet_interface/packet_interface.h"
#include "esphome/components/socket/socket.h"
#include "../udp_component.h"
namespace esphome::udp {
@@ -14,24 +16,33 @@ namespace esphome::udp {
/**
* A packet interface link over UDP: each packet is one datagram, sent to every address of the UDP component.
*
* Every datagram received on the listen port is passed up, from any sender, so on a shared network an addressing
* or encryption layer is needed to tell the peer's packets from others.
* When the UDP component sends to one unicast IPv4 address, the link joins exactly two devices: it accepts packets
* only from that address, and provides the addressing capability itself. Otherwise every packet is accepted, and an
* addressing layer can tell the devices apart. Packets from other senders are dropped with a warning at most once a
* minute, as they suggest that the address is a broadcast address the link took for a device.
*/
class UdpPacketInterface : public packet_interface::PacketInterface {
public:
/// @param tx_buffer Joins packets split across buffers; its size is the largest packet, and it must outlive the link
UdpPacketInterface(UDPComponent *udp_component, std::span<uint8_t> tx_buffer)
: packet_interface::PacketInterface(tx_buffer.size()), parent_(udp_component), tx_buffer_(tx_buffer) {}
/// @param peer The IPv4 address of the one peer, in network byte order; all zeros if packets are broadcast
UdpPacketInterface(UDPComponent *udp_component, std::span<uint8_t> tx_buffer, std::array<uint8_t, 4> peer);
void setup() override;
bool send_to_interface(const packet_interface::PacketBuffer &data,
const packet_interface::PacketMetaData &meta_data) override;
protected:
void receive_(std::span<const uint8_t> data);
void receive_(std::span<const uint8_t> data, const sockaddr *source);
/// True if packets from \p source are accepted: from anywhere, or with a peer, only from the peer.
bool accepts_from_(const sockaddr *source) const;
void warn_dropped_(const sockaddr *source);
// First, so it fills the spare byte at the end of PacketInterface
bool drop_warned_{false};
UDPComponent *parent_;
const std::span<uint8_t> tx_buffer_;
std::array<uint8_t, 4> peer_;
uint32_t last_drop_warning_ms_{0};
};
} // namespace esphome::udp
#endif
+11 -1
View File
@@ -242,10 +242,18 @@ void UDPComponent::loop() {
}
// One byte more than the largest packet, as a read cuts a longer datagram to fit and so cannot report it
std::array<uint8_t, MAX_PACKET_SIZE + 1> buf;
struct sockaddr_storage source;
for (;;) {
if (!this->listen_socket_->ready())
break;
auto len = this->listen_socket_->read(buf.data(), buf.size());
socklen_t source_len = sizeof(source);
auto len =
this->listen_socket_->recvfrom(buf.data(), buf.size(), reinterpret_cast<sockaddr *>(&source), &source_len);
if (len < 0 && errno != EWOULDBLOCK && errno != EAGAIN) {
// The raw lwIP socket consumes a packet whose source address it cannot convert
ESP_LOGD(TAG, "Read failed: errno %d", errno);
break;
}
if (len <= 0)
break;
size_t packet_len = static_cast<size_t>(len);
@@ -254,7 +262,9 @@ void UDPComponent::loop() {
continue;
}
ESP_LOGV(TAG, "Received packet of length %zu", packet_len);
this->rx_source_ = reinterpret_cast<sockaddr *>(&source);
this->packet_listeners_.call(std::span<const uint8_t>(buf.data(), packet_len));
this->rx_source_ = nullptr;
}
}
+3
View File
@@ -47,6 +47,8 @@ class UDPComponent final : public Component {
void set_omr_source() { this->poll_pending_ |= POLL_SRC_BIND; }
#endif
template<typename F> void add_listener(F &&listener) { this->packet_listeners_.add(std::forward<F>(listener)); }
/// The sender of the packet being passed to the listeners; valid only during a listener call.
const sockaddr *get_rx_source() const { return this->rx_source_; }
void setup() override;
void loop() override;
void dump_config() override;
@@ -78,6 +80,7 @@ class UDPComponent final : public Component {
StaticFixedVector<SockaddrEntry> sockaddrs_;
const char *const *addresses_{nullptr};
const char *listen_address_{nullptr};
const sockaddr *rx_source_{nullptr};
#ifdef USE_SOCKET_IPV6
uint32_t mcast_ifindex_{0};
#endif
@@ -0,0 +1,36 @@
espnow:
id: espnow_component
channel: 1
packet_interface:
- platform: espnow
id: espnow_peer_link
peer_address: 10:22:33:44:55:66
encryption: Johnny Cash
reliability:
session:
- platform: espnow
id: espnow_broadcast_link
addressing:
address: 0x0001
peers:
- id: espnow_bus_link
destination: broadcast
- id: espnow_pump_link
destination: 0x0002
reliability:
sensor:
- platform: template
id: espnow_sensor
lambda: return 1.0;
packet_transport:
- platform: packet_interface
packet_interface: espnow_peer_link
sensors:
- espnow_sensor
- platform: packet_interface
packet_interface: espnow_bus_link
sensors:
- espnow_sensor
@@ -0,0 +1,2 @@
packages:
espnow: !include common-packet-interface.yaml
@@ -0,0 +1,2 @@
packages:
espnow: !include common-packet-interface.yaml
@@ -29,8 +29,10 @@ 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;
size_t capacity{SIZE_MAX};
};
static PacketMetaData destination(uint16_t address) {
@@ -92,6 +94,14 @@ TEST(BasicAddressingTest, HeaderAndCapability) {
EXPECT_EQ(a.link.sent, (std::vector<Packet>{{0x78, 0x56, 0x34, 0x12, 0xAA}}));
}
// A fragmentation layer above a port must see how much the link can take
TEST(BasicAddressingTest, PortsReportTheLinksSendCapacity) {
Device a(0x1234, {BROADCAST, 0x5678});
a.link.capacity = 7;
EXPECT_EQ(a.port(0).port->get_send_capacity(), 7u);
EXPECT_EQ(a.port(1).port->get_send_capacity(), 7u);
}
TEST(BasicAddressingTest, ChosenDestinationOnlyThroughBroadcastPort) {
Device hub(0x0001, {0x0002, BROADCAST});
Packet packet{0xBB};
@@ -4,6 +4,9 @@ wifi:
udp:
- id: packet_interface_udp
- id: packet_interface_udp_peer
port: 18512
addresses: ["192.168.1.60"]
packet_interface:
- platform: uart
@@ -45,6 +48,12 @@ packet_interface:
reliability:
fragmentation:
session:
# Sends to one unicast address, so the link provides addressing
- platform: udp
id: packet_interface_udp_peer_link
udp_id: packet_interface_udp_peer
reliability:
session:
sensor:
- platform: template
@@ -60,3 +69,7 @@ packet_transport:
packet_interface: packet_interface_network
sensors:
- packet_interface_sensor
- platform: packet_interface
packet_interface: packet_interface_udp_peer_link
sensors:
- packet_interface_sensor
+12
View File
@@ -1,6 +1,18 @@
from esphome.components.logger import request_log_listener
from esphome.types import ConfigType
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# benchmark.yaml configures a udp packet_interface, whose to_code waits on the udp component variable.
manifest.enable_codegen()
original_to_code = manifest.to_code
# The tests check warnings through a real logger callback
async def to_code(config: ConfigType) -> None:
await original_to_code(config)
request_log_listener()
if hasattr(original_to_code, "priority"):
to_code.priority = original_to_code.priority
manifest.to_code = to_code
+1 -1
View File
@@ -1,5 +1,5 @@
udp:
addresses: ["192.168.1.255"]
addresses: ["192.168.1.60"]
packet_interface:
- platform: udp
+81 -3
View File
@@ -1,9 +1,14 @@
#include <gtest/gtest.h>
#include <array>
#include <cstdint>
#include <cstring>
#include <string>
#include <vector>
#include "esphome/components/logger/logger.h"
#include "esphome/components/udp/packet_interface/udp_packet_interface.h"
#include "esphome/core/application.h"
#ifdef USE_NETWORK
@@ -25,29 +30,41 @@ struct UdpBufferStorage {
uint8_t tx_buffer_storage[8]{};
};
static constexpr std::array<uint8_t, 4> PEER{192, 168, 1, 60};
class TestUdp : public UdpBufferStorage, public UdpPacketInterface {
public:
explicit TestUdp(UDPComponent *udp) : UdpPacketInterface(udp, this->tx_buffer_storage) {
explicit TestUdp(UDPComponent *udp, std::array<uint8_t, 4> peer = {})
: UdpPacketInterface(udp, this->tx_buffer_storage, peer) {
this->add_packet_interface_listener(
[this](const PacketBuffer &data, const PacketMetaData &) { this->received.push_back(to_vector(data)); });
}
using UdpPacketInterface::accepts_from_;
using UdpPacketInterface::receive_;
std::vector<Packet> received;
};
static sockaddr_in ipv4(std::array<uint8_t, 4> address) {
sockaddr_in addr{};
addr.sin_family = AF_INET;
memcpy(&addr.sin_addr.s_addr, address.data(), address.size());
return addr;
}
// The udp component is not set up, so it has no destinations and a send does nothing
class UdpPacketInterfaceTest : public ::testing::Test {
protected:
UDPComponent udp_;
TestUdp link_{&this->udp_};
sockaddr_in sender_{ipv4({10, 0, 0, 1})};
};
TEST_F(UdpPacketInterfaceTest, ReceivedPacketsUpToMaxPassedUp) {
Packet packet{1, 2, 3, 4, 5, 6, 7, 8};
this->link_.receive_(packet);
this->link_.receive_(packet, reinterpret_cast<sockaddr *>(&this->sender_));
Packet oversized{1, 2, 3, 4, 5, 6, 7, 8, 9};
this->link_.receive_(oversized);
this->link_.receive_(oversized, reinterpret_cast<sockaddr *>(&this->sender_));
EXPECT_EQ(this->link_.received, std::vector<Packet>{packet});
}
@@ -73,6 +90,67 @@ TEST_F(UdpPacketInterfaceTest, OversizedPacketNotSent) {
EXPECT_FALSE(this->link_.send_to_interface(PacketBuffer(&header, 1, &body, nullptr, 0), {}));
}
TEST_F(UdpPacketInterfaceTest, WithoutPeerAcceptsAnySender) {
EXPECT_FALSE(this->link_.has_capability(packet_interface::CAPABILITY_ADDRESSING));
EXPECT_TRUE(this->link_.accepts_from_(reinterpret_cast<sockaddr *>(&this->sender_)));
}
TEST_F(UdpPacketInterfaceTest, WithPeerAcceptsOnlyPeer) {
TestUdp link(&this->udp_, PEER);
EXPECT_TRUE(link.has_capability(packet_interface::CAPABILITY_ADDRESSING));
auto peer = ipv4(PEER);
Packet packet{1, 2};
link.receive_(packet, reinterpret_cast<sockaddr *>(&this->sender_));
EXPECT_TRUE(link.received.empty());
link.receive_(packet, reinterpret_cast<sockaddr *>(&peer));
EXPECT_EQ(link.received, std::vector<Packet>{packet});
}
// 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};
};
TEST_F(UdpPacketInterfaceTest, DroppedSenderWarnedOnceAMinute) {
TestUdp link(&this->udp_, PEER);
Warnings warnings;
Packet packet{1, 2};
auto drop_at = [&](uint32_t now) {
LoopBlockingGuard guard{nullptr, nullptr, now};
link.receive_(packet, reinterpret_cast<sockaddr *>(&this->sender_));
};
drop_at(1000);
ASSERT_EQ(warnings.lines.size(), 1u);
EXPECT_NE(warnings.lines[0].find("dropped"), std::string::npos);
drop_at(30000);
drop_at(60999);
EXPECT_EQ(warnings.lines.size(), 1u);
drop_at(61000);
EXPECT_EQ(warnings.lines.size(), 2u);
EXPECT_TRUE(link.received.empty());
}
TEST_F(UdpPacketInterfaceTest, WithPeerDropsSenderNotIpv4) {
TestUdp link(&this->udp_, PEER);
sockaddr other{};
other.sa_family = AF_UNSPEC;
EXPECT_FALSE(link.accepts_from_(&other));
}
} // namespace esphome::udp::testing
#endif
@@ -0,0 +1,60 @@
"""Unit tests for the espnow packet interface platform."""
import pytest
from esphome import config_validation as cv
from esphome.components.espnow import packet_interface as espnow_packet_interface
from esphome.components.packet_interface import CONF_MAX_PACKET_SIZE
import esphome.final_validate as fv
ADDRESSING = {"address": 1, "destination": 2}
def test_broadcast_takes_addressing(setup_core) -> None:
"""Broadcast is the default, and an addressing layer tells the devices apart."""
config = espnow_packet_interface.CONFIG_SCHEMA({"addressing": ADDRESSING})
assert str(config["peer_address"]) == "FF:FF:FF:FF:FF:FF"
assert config[CONF_MAX_PACKET_SIZE] == 250
def test_peer_rejects_addressing(setup_core) -> None:
"""Packets to one peer are already addressed."""
assert espnow_packet_interface.CONFIG_SCHEMA({"peer_address": "10:22:33:44:55:66"})
with pytest.raises(cv.Invalid, match="addressing"):
espnow_packet_interface.CONFIG_SCHEMA(
{"peer_address": "10:22:33:44:55:66", "addressing": ADDRESSING}
)
@pytest.mark.parametrize(
"mac", ["11:22:33:44:55:66", "01:00:5E:00:00:01", "00:00:00:00:00:00"]
)
def test_peer_must_be_one_device(setup_core, mac: str) -> None:
"""A group or all-zero address names no one device, so source filtering would drop everything."""
with pytest.raises(cv.Invalid, match="not the address of a device"):
espnow_packet_interface.CONFIG_SCHEMA({"peer_address": mac})
def test_broadcast_needs_addressing_for_two_device_layers(setup_core) -> None:
"""Every device hears a broadcast link, so reliability needs an addressing layer there."""
with pytest.raises(cv.Invalid, match="every device hears this link"):
espnow_packet_interface.CONFIG_SCHEMA({"reliability": None})
assert espnow_packet_interface.CONFIG_SCHEMA(
{"reliability": None, "addressing": ADDRESSING}
)
assert espnow_packet_interface.CONFIG_SCHEMA(
{"peer_address": "10:22:33:44:55:66", "reliability": None}
)
def test_packet_size_within_payload_size(setup_core) -> None:
"""A packet cannot be larger than the espnow component's max_payload_size."""
token = fv.full_config.set({"espnow": {"max_payload_size": 250}})
try:
assert espnow_packet_interface._validate_packet_size(
{CONF_MAX_PACKET_SIZE: 250}
)
with pytest.raises(cv.Invalid, match="max_payload_size"):
espnow_packet_interface._validate_packet_size({CONF_MAX_PACKET_SIZE: 251})
finally:
fv.full_config.reset(token)
@@ -0,0 +1,82 @@
"""Unit tests for the udp packet interface platform."""
from ipaddress import IPv4Address, IPv6Address
import pytest
from esphome import config_validation as cv
from esphome.components.udp import (
CONF_ADDRESSES,
packet_interface as udp_packet_interface,
)
from esphome.core import ID
import esphome.final_validate as fv
@pytest.mark.parametrize(
("addresses", "point_to_point", "peer"),
[
([IPv4Address("192.168.1.60")], None, IPv4Address("192.168.1.60")),
([IPv4Address("255.255.255.255")], None, None),
([IPv4Address("0.0.0.0")], None, None),
([IPv4Address("192.168.1.255")], None, None),
([IPv4Address("239.0.60.53")], None, None),
([IPv4Address("192.168.1.60"), IPv4Address("192.168.1.61")], None, None),
([IPv6Address("2001:db8::1")], None, None),
# A directed broadcast on a /25, and a host ending in .255 on a /23
([IPv4Address("192.168.1.127")], False, None),
([IPv4Address("192.168.0.255")], True, IPv4Address("192.168.0.255")),
],
)
def test_peer(
addresses: list, point_to_point: bool | None, peer: IPv4Address | None
) -> None:
"""Only one unicast IPv4 destination makes the link point to point, unless point_to_point says otherwise."""
config = {} if point_to_point is None else {"point_to_point": point_to_point}
assert udp_packet_interface._peer({CONF_ADDRESSES: addresses}, config) == peer
@pytest.mark.parametrize(
"addresses",
[
[IPv4Address("239.0.60.53")],
[IPv4Address("192.168.1.60"), IPv4Address("192.168.1.61")],
[IPv4Address("255.255.255.255")],
[IPv4Address("0.0.0.0")],
],
)
def test_point_to_point_needs_one_unicast_address(addresses: list) -> None:
"""point_to_point cannot make a multicast or several addresses into one peer."""
with pytest.raises(cv.Invalid, match="one unicast IPv4 address"):
udp_packet_interface._peer(
{CONF_ADDRESSES: addresses}, {"point_to_point": True}
)
def _final_validate(addresses: list, layers: dict) -> None:
config = udp_packet_interface.CONFIG_SCHEMA({"udp_id": "udp_link", **layers})
token = fv.full_config.set(
{"udp": [{"id": ID("udp_link"), CONF_ADDRESSES: addresses}]}
)
try:
udp_packet_interface._final_validate(config)
finally:
fv.full_config.reset(token)
def test_point_to_point_rejects_addressing(setup_core) -> None:
"""Packets to one unicast address are already addressed."""
addressing = {"addressing": {"address": 1, "destination": 2}}
with pytest.raises(cv.Invalid, match="already addressed"):
_final_validate([IPv4Address("192.168.1.60")], addressing)
_final_validate([IPv4Address("192.168.1.255")], addressing)
_final_validate(
[IPv4Address("192.168.1.127")], {**addressing, "point_to_point": False}
)
def test_broadcast_needs_addressing_for_two_device_layers(setup_core) -> None:
"""Every device hears a broadcast link, so reliability needs an addressing layer there."""
with pytest.raises(cv.Invalid, match="every device hears this link"):
_final_validate([IPv4Address("192.168.1.255")], {"reliability": None})
_final_validate([IPv4Address("192.168.1.60")], {"reliability": None})