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