ESPHome 2026.1.0-dev
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, "Registering ESP-NOW handlers");
25 ESP_LOGI(TAG, "Peer address: %02X:%02X:%02X:%02X:%02X:%02X", this->peer_address_[0], this->peer_address_[1],
26 this->peer_address_[2], this->peer_address_[3], this->peer_address_[4], this->peer_address_[5]);
27
28 // Register received handler
29 this->parent_->register_received_handler(this);
30
31 // Register broadcasted handler
32 this->parent_->register_broadcasted_handler(this);
33}
34
35void ESPNowTransport::send_packet(const std::vector<uint8_t> &buf) const {
36 if (this->parent_ == nullptr) {
37 ESP_LOGE(TAG, "ESPNow component not set");
38 return;
39 }
40
41 if (buf.empty()) {
42 ESP_LOGW(TAG, "Attempted to send empty packet");
43 return;
44 }
45
46 if (buf.size() > ESP_NOW_MAX_DATA_LEN) {
47 ESP_LOGE(TAG, "Packet too large: %zu bytes (max %d)", buf.size(), ESP_NOW_MAX_DATA_LEN);
48 return;
49 }
50
51 // Send to configured peer address
52 this->parent_->send(this->peer_address_.data(), buf.data(), buf.size(), [](esp_err_t err) {
53 if (err != ESP_OK) {
54 ESP_LOGW(TAG, "Send failed: %d", err);
55 }
56 });
57}
58
59bool ESPNowTransport::on_received(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
60 ESP_LOGV(TAG, "Received packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
61 info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
62
63 if (data == nullptr || size == 0) {
64 ESP_LOGW(TAG, "Received empty or null packet");
65 return false;
66 }
67
68 this->packet_buffer_.resize(size);
69 memcpy(this->packet_buffer_.data(), data, size);
70 this->process_(this->packet_buffer_);
71 return false; // Allow other handlers to run
72}
73
74bool ESPNowTransport::on_broadcasted(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
75 ESP_LOGV(TAG, "Received broadcast packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
76 info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
77
78 if (data == nullptr || size == 0) {
79 ESP_LOGW(TAG, "Received empty or null broadcast packet");
80 return false;
81 }
82
83 this->packet_buffer_.resize(size);
84 memcpy(this->packet_buffer_.data(), data, size);
85 this->process_(this->packet_buffer_);
86 return false; // Allow other handlers to run
87}
88
89} // namespace espnow
90} // namespace esphome
91
92#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
uint8_t src_addr[ESP_NOW_ETH_ALEN]
Source address of ESPNOW packet.