ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
espnow_packet_interface.cpp
Go to the documentation of this file.
2
3#if defined(USE_ESP32) || defined(USE_ESP8266)
4
5#include <cstring>
6
9#include "esphome/core/log.h"
10
11namespace esphome::espnow {
12
13using packet_interface::PacketBuffer;
14using packet_interface::PacketMetaData;
15
16ESPHOME_LOG_TAG(TAG, "espnow.packet_interface");
17
18static constexpr uint32_t WARNING_INTERVAL_MS = 60000;
19
20ESPNowPacketInterface::ESPNowPacketInterface(ESPNowComponent *parent, std::span<uint8_t> tx_buffer,
21 peer_address_t peer_address)
22 : packet_interface::PacketInterface(tx_buffer.size()),
23 parent_(parent),
24 tx_buffer_(tx_buffer),
25 peer_address_(peer_address) {
26 if (!this->is_broadcast_())
28}
29
31 // Each mode hears only its own kind of packet, so a peer link and a broadcast link can share the component
32 if (!this->is_broadcast_() || !ESPNOW_REPORTS_DESTINATION)
33 this->parent_->register_receive_handler(this);
34 if (this->is_broadcast_()) {
35 this->parent_->register_broadcast_handler(this);
36 this->parent_->register_unknown_peer_handler(this);
37 }
38}
39
41 char peer_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
42 format_mac_addr_upper(this->peer_address_.data(), peer_buf);
43 ESP_LOGCONFIG(TAG,
44 "ESP-NOW Packet Interface '%s':\n"
45 " Peer: %s\n"
46 " Max Packet Size: %zu",
47 this->name_, peer_buf, this->max_packet_size_);
48}
49
51 // Layers above are set up first, and may send while setup waits for another component
52 if (!this->is_ready())
53 return false;
54 size_t size = data.size();
55 if (size > this->max_packet_size_) {
56 ESP_LOGW(TAG, "%s: Packet of %zu bytes is larger than %zu", this->name_, size, this->max_packet_size_);
57 return false;
58 }
59 const uint8_t *payload;
60 if (data.is_contiguous()) {
61 payload = data.as_span().data();
62 } else {
63 data.copy_to(this->tx_buffer_.data(), size);
64 payload = this->tx_buffer_.data();
65 }
66 // Only a unicast send learns whether the peer received it
67 esp_err_t err = this->is_broadcast_()
68 ? this->parent_->send(this->peer_address_.data(), payload, size)
69 : this->parent_->send(this->peer_address_.data(), payload, size, [this](esp_err_t status) {
70 if (status != ESP_OK)
71 this->delivery_failed_();
72 });
73 if (err != ESP_OK) {
74 this->send_failed_(err);
75 return false;
76 }
77 return true;
78}
79
80// A full send queue is logged by the espnow component, and ESP-NOW is not running before setup or while disabled.
81// Other errors, such as a peer that could not be added, repeat on every send, so warn now and then
84 if (err == ESP_ERR_ESPNOW_NO_MEM || err == ESP_ERR_ESPNOW_NOT_INIT ||
85 (this->send_warned_ && now - this->last_send_warning_ms_ < WARNING_INTERVAL_MS)) {
86 ESP_LOGV(TAG, "%s: Send failed: %s", this->name_, LOG_STR_ARG(espnow_error_to_str(err)));
87 return;
88 }
89 this->send_warned_ = true;
90 this->last_send_warning_ms_ = now;
91 ESP_LOGW(TAG, "%s: Send failed: %s", this->name_, LOG_STR_ARG(espnow_error_to_str(err)));
92}
93
94// A peer that is off, out of range or has another address never acknowledges; without a reliability layer above,
95// nothing else would report that, so warn now and then
98 if (this->delivery_warned_ && now - this->last_delivery_warning_ms_ < WARNING_INTERVAL_MS) {
99 ESP_LOGV(TAG, "%s: Packet not acknowledged by the peer", this->name_);
100 return;
101 }
102 this->delivery_warned_ = true;
103 this->last_delivery_warning_ms_ = now;
104 ESP_LOGW(TAG, "%s: Packet not acknowledged by the peer; it may be off, out of range, or have another address",
105 this->name_);
106}
107
108// Every larger packet from a peer whose max_packet_size is larger is lost, so warn now and then
111 if (this->size_warned_ && now - this->last_size_warning_ms_ < WARNING_INTERVAL_MS) {
112 ESP_LOGV(TAG, "%s: Packet of %u bytes is larger than %zu, dropped", this->name_, size, this->max_packet_size_);
113 return;
114 }
115 this->size_warned_ = true;
116 this->last_size_warning_ms_ = now;
117 ESP_LOGW(TAG, "%s: Packet of %u bytes is larger than %zu, dropped; set the same max_packet_size on both devices",
118 this->name_, size, this->max_packet_size_);
119}
120
121bool ESPNowPacketInterface::on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
122 this->receive_(info, data, size);
123 return false;
124}
125
126bool ESPNowPacketInterface::on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
127 this->receive_(info, data, size);
128 return false;
129}
130
131bool ESPNowPacketInterface::on_unknown_peer(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
132 // Only known peers reach the other handlers; with auto_add_peer this sender is added and arrives there
133 if (this->parent_->get_auto_add_peer())
134 return false;
135 if (!ESPNOW_REPORTS_DESTINATION || memcmp(info.des_addr, ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0)
136 this->receive_(info, data, size);
137 return false;
138}
139
140void ESPNowPacketInterface::receive_(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
141 if (size == 0)
142 return;
143 if (!this->is_broadcast_() && memcmp(info.src_addr, this->peer_address_.data(), ESP_NOW_ETH_ALEN) != 0)
144 return;
145 if (size > this->max_packet_size_) {
146 this->oversized_(size);
147 return;
148 }
149 PacketMetaData meta_data;
150 memcpy(meta_data.mac_address, info.src_addr, ESP_NOW_ETH_ALEN);
151#ifdef USE_ESP32
152 meta_data.rssi = info.rx_ctrl->rssi;
153#endif
154 this->on_receive_from_interface_(PacketBuffer(data, size), meta_data);
155}
156
157} // namespace esphome::espnow
158
159#endif // USE_ESP32 || USE_ESP8266
uint8_t status
Definition bl0942.h:8
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
bool is_ready() const
void receive_(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size)
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
bool send_to_interface(const packet_interface::PacketBuffer &data, const packet_interface::PacketMetaData &meta_data) override
bool on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) override
ESPNowPacketInterface(ESPNowComponent *parent, std::span< uint8_t > tx_buffer, peer_address_t peer_address)
A buffer class that efficiently represents a composite buffer with three sections:
size_t size() const
Get the total size of the buffer.
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.
const LogString * espnow_error_to_str(esp_err_t error)
ESPHOME_LOG_TAG(TAG, "espnow")
uint16_t size
Definition helpers.cpp:44
Application App
Global storage of Application pointer - only one Application can exist.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:2001
static void uint32_t
rx_ctrl_t * rx_ctrl
Rx control info of ESPNOW packet.
uint8_t des_addr[ESP_NOW_ETH_ALEN]
Destination address of ESPNOW packet.
uint8_t src_addr[ESP_NOW_ETH_ALEN]
Source address of ESPNOW packet.