ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
espnow_transport.cpp
Go to the documentation of this file.
1#include "espnow_transport.h"
2
3#if defined(USE_ESP32) || defined(USE_ESP8266)
4
6#include "esphome/core/log.h"
7
8#include <span>
9
10namespace esphome::espnow {
11
12ESPHOME_LOG_TAG(TAG, "espnow.transport");
13
14bool ESPNowTransport::should_send() { return this->parent_ != nullptr && !this->parent_->is_failed(); }
15
17 PacketTransport::setup();
18
19 if (this->parent_ == nullptr) {
20 ESP_LOGE(TAG, "ESPNow component not set");
21 this->mark_failed();
22 return;
23 }
24
25 ESP_LOGI(TAG, "Registering ESP-NOW handlers, peer: %02X:%02X:%02X:%02X:%02X:%02X", this->peer_address_[0],
26 this->peer_address_[1], this->peer_address_[2], this->peer_address_[3], this->peer_address_[4],
27 this->peer_address_[5]);
28
29 // Register received handler
30 this->parent_->register_receive_handler(this);
31
32 // Register broadcast handler
33 this->parent_->register_broadcast_handler(this);
34}
35
36void ESPNowTransport::send_packet(const std::vector<uint8_t> &buf) const {
37 if (this->parent_ == nullptr) {
38 ESP_LOGE(TAG, "ESPNow component not set");
39 return;
40 }
41
42 if (buf.empty()) {
43 ESP_LOGW(TAG, "Attempted to send empty packet");
44 return;
45 }
46
47 if (buf.size() > ESPNOW_MAX_DATA_LEN) {
48 ESP_LOGE(TAG, "Packet too large: %zu bytes (max %u)", buf.size(), (unsigned) ESPNOW_MAX_DATA_LEN);
49 return;
50 }
51
52 // Send to configured peer address
53 this->parent_->send(this->peer_address_.data(), buf.data(), buf.size(), [](esp_err_t err) {
54 if (err != ESP_OK) {
55 ESP_LOGW(TAG, "Send failed: %d", err);
56 }
57 });
58}
59
60bool ESPNowTransport::on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
61 ESP_LOGV(TAG, "Received packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", (unsigned) size, info.src_addr[0],
62 info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
63
64 if (data == nullptr || size == 0) {
65 ESP_LOGW(TAG, "Received empty or null packet");
66 return false;
67 }
68
69 this->process_(std::span<const uint8_t>(data, size));
70 return false; // Allow other handlers to run
71}
72
73bool ESPNowTransport::on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint16_t size) {
74 ESP_LOGV(TAG, "Received broadcast packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", (unsigned) size,
75 info.src_addr[0], info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
76
77 if (data == nullptr || size == 0) {
78 ESP_LOGW(TAG, "Received empty or null broadcast packet");
79 return false;
80 }
81
82 this->process_(std::span<const uint8_t>(data, size));
83 return false; // Allow other handlers to run
84}
85
86} // namespace esphome::espnow
87
88#endif // USE_ESP32 || USE_ESP8266
void mark_failed()
Mark this component as failed.
void send_packet(const std::vector< uint8_t > &buf) const override
ESPHOME_LOG_TAG(TAG, "espnow")
uint16_t size
Definition helpers.cpp:34
uint8_t src_addr[ESP_NOW_ETH_ALEN]
Source address of ESPNOW packet.