Skip to content

sx126x and sx127x Listeners Receive Packets as a Span

The sx126x and sx127x radios now receive each packet into a fixed buffer and hand listeners a std::span<const uint8_t> instead of a std::vector<uint8_t>. External components that implement SX126xListener or SX127xListener should change their on_packet() signature; the old one keeps compiling until 2027.6.0.

This is a developer breaking change for external components in ESPHome 2026.12.0 and later.

PR #23493: Keep received packets in a fixed buffer

Each received packet was built in a std::vector<uint8_t>, and the on_packet trigger copied it again for every automation. A packet from these chips is at most 255 bytes, so each radio now keeps one 255-byte buffer and passes views of it. On a simulated radio this took a receive test from 6,764 heap allocations to one, and saved about 210 bytes of flash.

  1. SX126xListener::on_packet() and SX127xListener::on_packet() take std::span<const uint8_t> packet instead of const std::vector<uint8_t> &packet.
  2. The old on_packet(const std::vector<uint8_t> &, float, float) is deprecated. A listener that still overrides it keeps working: the default span method copies the packet into a vector and calls it, and logs one warning the first time it does. GCC and clang do not warn at compile time about overriding a deprecated virtual, so look for that log line.
  3. packet_interface::RadioLinkBase::receive_() takes a std::span<const uint8_t>. There is no deprecation path for it. A RadioLink subclass that still overrides the vector on_packet() no longer builds (“overriding final function”), since the radios only call the span form; override the span form instead. Calling on_packet() on a link with a vector still works.
  4. x in the on_packet automation is a ByteView: a std::span<const uint8_t> that also converts to std::vector<uint8_t> and compares with one.
  • External components that implement SX126xListener or SX127xListener
  • External components that derive from RadioLink

Most YAML configurations are not affected. x.size(), x[i], x.data(), for (auto b : x), std::string(x.begin(), x.end()), format_hex(x), format_hex_pretty(x), format_hex_to(buf, x), x == std::vector<uint8_t>{...}, return x; and passing x to a function or script that takes a vector all still work. A lambda breaks only if it:

  • changes x (x.push_back(...), x.clear())
  • calls x.at(i)
  • keeps x past a delay or other wait: the buffer holds the next packet by then
// Before
void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) override;
// After
void on_packet(std::span<const uint8_t> packet, float rssi, float snr) override;

The body usually needs no change: packet.size(), packet[i], packet.data() and range-for work on a span. Code that needs a vector makes the copy itself: std::vector<uint8_t> data(packet.begin(), packet.end());. A helper that took const std::vector<uint8_t> & can take std::span<const uint8_t> instead; a vector still converts to it.

In a YAML lambda, copy x first when you need to change or keep it:

on_packet:
then:
- lambda: |-
std::vector<uint8_t> data = x;
data.push_back(0x00);

Use the version macro to override the right form on each version, which avoids the copy per packet and the warning on 2026.12.0 and later:

#include "esphome/core/version.h"
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2026, 12, 0)
void on_packet(std::span<const uint8_t> packet, float rssi, float snr) override;
#else
void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) override;
#endif

Keeping only the vector override also works on both until 2027.6.0: it is called directly before 2026.12.0, and through the deprecated default from 2026.12.0, at the cost of one copy per packet and the one-time warning.

  • ESPHome 2026.12.0 (December 2026): listeners receive a span; the vector override is deprecated and still called.
  • ESPHome 2027.6.0 (June 2027): the vector override is removed.
Terminal window
grep -rnE 'SX12[67]xListener|on_packet\(const std::vector' your_component/

If you have questions about migrating your external component, please ask in:

Comments

Feel free to leave a comment here to discuss this post with others. You can ask questions, share your experience, or suggest improvements. If you have a question about a specific feature or issue, please consider using the ESPHome Discord. Stick to English and follow ESPHome's code of conduct. These comments exist on a discussion on GitHub, so you can also comment there directly if you prefer.