ESPHome 2025.11.0b4
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
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(static_cast<ESPNowReceivedPacketHandler *>(this));
30
31 // Register broadcasted handler
32 this->parent_->register_broadcasted_handler(static_cast<ESPNowBroadcastedHandler *>(this));
33}
34
39
40void ESPNowTransport::send_packet(const std::vector<uint8_t> &buf) const {
41 if (this->parent_ == nullptr) {
42 ESP_LOGE(TAG, "ESPNow component not set");
43 return;
44 }
45
46 if (buf.empty()) {
47 ESP_LOGW(TAG, "Attempted to send empty packet");
48 return;
49 }
50
51 if (buf.size() > ESP_NOW_MAX_DATA_LEN) {
52 ESP_LOGE(TAG, "Packet too large: %zu bytes (max %d)", buf.size(), ESP_NOW_MAX_DATA_LEN);
53 return;
54 }
55
56 // Send to configured peer address
57 this->parent_->send(this->peer_address_.data(), buf.data(), buf.size(), [](esp_err_t err) {
58 if (err != ESP_OK) {
59 ESP_LOGW(TAG, "Send failed: %d", err);
60 }
61 });
62}
63
64bool ESPNowTransport::on_received(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
65 ESP_LOGV(TAG, "Received packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
66 info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
67
68 if (data == nullptr || size == 0) {
69 ESP_LOGW(TAG, "Received empty or null packet");
70 return false;
71 }
72
73 this->packet_buffer_.resize(size);
74 memcpy(this->packet_buffer_.data(), data, size);
75 this->process_(this->packet_buffer_);
76 return false; // Allow other handlers to run
77}
78
79bool ESPNowTransport::on_broadcasted(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
80 ESP_LOGV(TAG, "Received broadcast packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
81 info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
82
83 if (data == nullptr || size == 0) {
84 ESP_LOGW(TAG, "Received empty or null broadcast packet");
85 return false;
86 }
87
88 this->packet_buffer_.resize(size);
89 memcpy(this->packet_buffer_.data(), data, size);
90 this->process_(this->packet_buffer_);
91 return false; // Allow other handlers to run
92}
93
94} // namespace espnow
95} // namespace esphome
96
97#endif // USE_ESP32
virtual void mark_failed()
Mark this component as failed.
Handler interface for receiving broadcasted ESPNow packets Components should inherit from this class ...
Handler interface for receiving ESPNow packets Components should inherit from this class to handle in...
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.