ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
basic_addressing.cpp
Go to the documentation of this file.
1#include "basic_addressing.h"
2#ifdef USE_PACKET_INTERFACE_BASIC_ADDRESSING
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "packet_interface.addressing");
8
11 [this](const PacketBuffer &data, const PacketMetaData &meta_data) { this->receive_(data, meta_data); });
12}
13
15 ESP_LOGCONFIG(TAG,
16 "Basic Addressing on '%s':\n"
17 " Address: 0x%04X",
18 this->link_->get_name(), this->address_);
19}
20
21bool BasicAddressing::send(const PacketBuffer &data, const PacketMetaData &meta_data, uint16_t destination) {
22 uint8_t header[HEADER_SIZE];
23 put_le16(put_le16(header, destination), this->address_);
24 PacketBuffer packet(header, HEADER_SIZE, &data, nullptr, 0);
25 return this->link_->send_to_interface(packet, meta_data);
26}
27
28void BasicAddressing::receive_(const PacketBuffer &data, const PacketMetaData &meta_data) {
29 // Lower layers deliver packets in one piece; the payload is passed up without copying
30 if (!data.is_contiguous()) {
31 ESP_LOGW(TAG, "%s: Packet split across buffers is not supported", this->link_->get_name());
32 return;
33 }
34 auto packet = data.as_span();
35 if (packet.size() < HEADER_SIZE) {
36 ESP_LOGV(TAG, "%s: Packet of %zu bytes is shorter than the address header, dropped", this->link_->get_name(),
37 packet.size());
38 return;
39 }
40 uint16_t destination = encode_uint16(packet[1], packet[0]);
41 uint16_t source = encode_uint16(packet[3], packet[2]);
42 if (source == this->address_ || (destination != this->address_ && destination != BROADCAST_ADDRESS))
43 return;
44 AddressingPort *source_port = nullptr;
45 AddressingPort *broadcast_port = nullptr;
46 for (auto *port : this->ports_) {
47 if (port->destination_ == source) {
48 source_port = port;
49 } else if (port->destination_ == BROADCAST_ADDRESS) {
50 broadcast_port = port;
51 }
52 }
53 // A broadcast goes to the broadcast port, even from a device with a port of its own, as that port's layers expect
54 // only its point-to-point packets; anything else goes to the source's port, or to the broadcast port if it has none
55 AddressingPort *target = source_port;
56 if (broadcast_port != nullptr && (destination == BROADCAST_ADDRESS || source_port == nullptr))
57 target = broadcast_port;
58 if (target == nullptr) {
59 ESP_LOGV(TAG, "%s: Packet from 0x%04X dropped", this->link_->get_name(), source);
60 return;
61 }
62 PacketMetaData received = meta_data;
63 received.source_address = source;
64 received.destination_address = destination;
65 target->on_receive_from_interface_(PacketBuffer(packet.data() + HEADER_SIZE, packet.size() - HEADER_SIZE), received);
66}
67
68AddressingPort::AddressingPort(BasicAddressing *addressing, uint16_t destination, bool only_port)
70 remaining_packet_size(addressing->get_link()->get_max_packet_size(), BasicAddressing::HEADER_SIZE)),
71 addressing_(addressing),
72 destination_(destination) {
73 if (only_port) {
74 this->stack_on_(addressing->get_link(), CAPABILITY_ADDRESSING);
75 } else {
76 this->inherit_from_(addressing->get_link(), CAPABILITY_ADDRESSING);
77 }
78 // A link with no room leaves the layers above none either, so they fail their setup
79 addressing->add_port(this);
80}
81
83 uint16_t destination = this->destination_;
84 uint16_t chosen = meta_data.destination_address;
85 if (chosen != 0 && chosen != destination) {
86 if (destination != BasicAddressing::BROADCAST_ADDRESS || chosen == this->addressing_->get_address()) {
87 // The same call is made for every packet, so once is enough
88 if (!this->destination_warned_) {
89 this->destination_warned_ = true;
90 ESP_LOGW(TAG,
91 "%s: Packet for 0x%04X refused; only the broadcast port sends to a chosen address, and not to "
92 "this device's own",
93 this->name_, chosen);
94 }
95 return false;
96 }
97 destination = chosen;
98 }
99 return this->addressing_->send(data, meta_data, destination);
100}
101
103 ESP_LOGCONFIG(TAG,
104 "Addressing Port '%s':\n"
105 " Max Packet Size: %zu",
106 this->name_, this->max_packet_size_);
108 ESP_LOGCONFIG(TAG, " Destination: broadcast");
109 } else {
110 ESP_LOGCONFIG(TAG, " Destination: 0x%04X", this->destination_);
111 }
112}
113
114} // namespace esphome::packet_interface
115#endif
The bottom of the stack of layers for one destination, or for broadcast, on an addressed channel.
bool send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) override
Sends to the port's destination.
AddressingPort(BasicAddressing *addressing, uint16_t destination, bool only_port)
With only_port, the port is the top of the link, so top() on the link reaches this port's stack.
An addressing layer for channels shared by several devices, such as an RS485 bus or a LoRa radio.
static constexpr size_t HEADER_SIZE
Destination and source addresses.
bool send(const PacketBuffer &data, const PacketMetaData &meta_data, uint16_t destination)
Sends data to destination, which the port has checked.
void receive_(const PacketBuffer &data, const PacketMetaData &meta_data)
StaticVector< AddressingPort *, PACKET_INTERFACE_ADDRESSING_PORT_COUNT > ports_
A buffer class that efficiently represents a composite buffer with three sections:
A class that will transport data to and from an abstract channel.
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.
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.
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...
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.
ESPHOME_LOG_TAG(TAG, "packet_interface.addressing")
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.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1199
constexpr uint8_t * put_le16(uint8_t *dst, uint16_t value)
Definition helpers.h:1286