ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
packet_interface.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <cstddef>
5#include <cstdint>
6#include <utility>
7
11#include "packet_buffer.h"
12
14
16constexpr size_t remaining_packet_size(size_t size, size_t used) { return size > used ? size - used : 0; }
17
26
38 public:
40 uint16_t source_address{0};
42 float rssi{NAN};
43 float snr{NAN};
44 uint8_t mac_address[MAC_ADDRESS_SIZE]{};
45};
56class PacketInterface : public Component {
57 public:
66 virtual bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) = 0;
67
69 size_t get_max_packet_size() const { return this->max_packet_size_; }
71 virtual size_t get_send_capacity() const { return SIZE_MAX; }
73 uint8_t get_capabilities() const { return this->capabilities_; }
74 bool has_capability(Capability capability) const { return (this->capabilities_ & capability) != 0; }
81 template<typename F> void add_on_session_state_callback(F &&callback) {
82 this->session_callback_.add(std::forward<F>(callback));
83 }
84
87 PacketInterface *layer = this;
88 while (layer->upper_ != nullptr)
89 layer = layer->upper_;
90 return layer;
91 }
93 void set_name(const char *name) { this->name_ = name; }
94 const char *get_name() const { return this->name_; }
95
101 template<typename F> void add_packet_interface_listener(F &&callback) {
102 this->callback_.add(std::forward<F>(callback));
103 }
104
105 void dump_config() override;
106
107 protected:
109 explicit PacketInterface(size_t max_packet_size) : max_packet_size_(max_packet_size) {}
110
111 virtual ~PacketInterface() = default;
118 void on_receive_from_interface_(const PacketBuffer &data, const PacketMetaData &meta_data);
123 void note_received_(bool understood);
125 void stack_on_(PacketInterface *lower, Capability capability) {
126 lower->upper_ = this;
127 this->inherit_from_(lower, capability);
128 }
130 void inherit_from_(PacketInterface *lower, Capability capability) {
131 this->name_ = lower->name_;
132 this->capabilities_ = lower->capabilities_ | capability;
133 // A link that has a session of its own, such as TCP, reports it to whatever is stacked above, as does a
134 // reliability layer that discards data; the session layer handles the latter itself
136 capability != CAPABILITY_SESSION) {
137 lower->add_on_session_state_callback([this](bool connected) { this->session_callback_.call(connected); });
138 }
139 }
140
144 const char *name_{""};
145 const size_t max_packet_size_;
146 uint8_t capabilities_{0};
148};
149} // namespace esphome::packet_interface
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
A buffer class that efficiently represents a composite buffer with three sections:
A class that will transport data to and from an abstract channel.
bool has_capability(Capability capability) const
void add_on_session_state_callback(F &&callback)
With CAPABILITY_SESSION, the callback is called with true when the peer can be reached,...
size_t get_max_packet_size() const
The largest packet this layer accepts; fixed when the layer is constructed.
void note_received_(bool understood)
Called by a layer for each received packet it understood, or could not make sense of.
void stack_on_(PacketInterface *lower, Capability capability)
Called by a layer's constructor to record itself as the layer above lower, adding its own capability.
PacketInterface * top()
The topmost layer stacked on this one, or this one if nothing is stacked on it.
virtual bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data)=0
Method to be implemented by child classes to send data to the interface.
LazyCallbackManager< void(const PacketBuffer &, const PacketMetaData &)> callback_
uint8_t get_capabilities() const
The capabilities of this layer and every layer below it.
void set_name(const char *name)
Name shown in log messages; a layer takes the name of the link it is stacked on.
void add_packet_interface_listener(F &&callback)
Add a listener that will be called when data is received.
void inherit_from_(PacketInterface *lower, Capability capability)
Takes the name and capabilities of lower without becoming its top, for one of several stacks on a lin...
LazyCallbackManager< void(bool)> session_callback_
virtual size_t get_send_capacity() const
How many packets send_to_interface() would accept now, so a layer above can tell before sending a ser...
void on_receive_from_interface_(const PacketBuffer &data, const PacketMetaData &meta_data)
This function should be called by implementing classes with received data to be passed to any listene...
A struct to hold metadata about a packet.
constexpr size_t remaining_packet_size(size_t size, size_t used)
The room left for a packet of size bytes after used bytes, or 0 if it is too small.
Capability
What a stack of layers provides, as bits of PacketInterface::get_capabilities().
uint16_t size
Definition helpers.cpp:34