ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
radio_link.h
Go to the documentation of this file.
1#pragma once
2
3#include <span>
4#include <vector>
5
8#include "esphome/core/hal.h"
9
11
14 public:
15 void set_max_busy(uint32_t max_busy_ms) { this->listen_before_talk_.set_max_busy(max_busy_ms); }
16 void set_turnaround(uint32_t turnaround_ms) { this->listen_before_talk_.set_turnaround(turnaround_ms); }
17
18 protected:
19 explicit RadioLinkBase(size_t max_packet_size) : PacketInterface(max_packet_size) {}
21 virtual uint32_t current_time_ms() const { return millis(); }
23 bool check_size_(size_t size) const;
26 bool may_send_(bool busy);
32 bool sent_(bool sent, bool busy);
34 bool skip_empty_() const;
36 void radio_not_ready_(bool failed);
38 void radio_free_();
39 void receive_(std::span<const uint8_t> packet, float rssi, float snr);
40
41 // First, so it fills the spare byte at the end of PacketInterface
42 bool failed_warned_{false};
44};
45
57// While a radio's listener still has the deprecated vector on_packet, a link subclass overriding it would build but
58// never be called: make that a build error. Goes away with the vector form in 2027.6.0.
59template<typename Listener> class RadioLinkListener : public Listener {};
60template<typename Listener>
61 requires requires {
62 static_cast<void (Listener::*)(const std::vector<uint8_t> &, float, float)>(&Listener::on_packet);
63 }
64class RadioLinkListener<Listener> : public Listener {
65 public:
66 // The radios call the span form; a direct vector call still reaches the link through it
67 void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) final {
68 static_cast<Listener *>(this)->on_packet(std::span<const uint8_t>(packet), rssi, snr);
69 }
70};
71
72template<typename Radio, typename Listener, typename Error, size_t MaxPacketSize>
73class RadioLink : public RadioLinkBase, public RadioLinkListener<Listener> {
74 public:
75 RadioLink(Radio *radio, size_t max_packet_size) : RadioLinkBase(max_packet_size), radio_(radio) {}
76 using RadioLinkListener<Listener>::on_packet;
77
78 void setup() override {
79 this->radio_->register_listener(this);
80 this->disable_loop();
81 }
82 // Runs only during a busy period; a busy flag that never clears keeps it running. A radio that failed meanwhile
83 // is not read again, as reading it may wait on the radio every time
84 void loop() override {
85 if (!this->radio_->is_ready() || !this->radio_->is_receiving())
86 this->radio_free_();
87 }
88
89 bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override {
90 // The radio is set up after the layers above, which may send from their own setup
91 if (!this->radio_->is_ready()) {
92 this->radio_not_ready_(this->radio_->is_failed());
93 return false;
94 }
95 size_t size = data.size();
96 if (size == 0)
97 return this->skip_empty_();
98 bool busy = this->radio_->is_receiving();
99 if (!this->check_size_(size) || !this->may_send_(busy))
100 return false;
101 if (data.is_contiguous()) {
102 auto span = data.as_span();
103 return this->sent_(this->radio_->transmit_packet(span.data(), span.size()) == Error{}, busy);
104 }
105 uint8_t buf[MaxPacketSize];
106 data.copy_to(buf, size);
107 return this->sent_(this->radio_->transmit_packet(buf, size) == Error{}, busy);
108 }
109
110 void on_packet(std::span<const uint8_t> packet, float rssi, float snr) override { this->receive_(packet, rssi, snr); }
111
112 protected:
113 Radio *radio_;
114};
115
116} // namespace esphome::packet_interface
void disable_loop()
Disable this component's loop.
Decides whether a half-duplex radio link may send while its radio reports that it is receiving a pack...
A buffer class that efficiently represents a composite buffer with three sections:
size_t size() const
Get the total size of the buffer.
A class that will transport data to and from an abstract channel.
A struct to hold metadata about a packet.
uint16_t size
Definition helpers.cpp:44
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t