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