diff --git a/esphome/components/espnow/espnow.cpp b/esphome/components/espnow/espnow.cpp index 00a82c1371..fc4edfdf92 100644 --- a/esphome/components/espnow/espnow.cpp +++ b/esphome/components/espnow/espnow.cpp @@ -39,7 +39,7 @@ std::string format_mac_addr(const uint8_t *mac) { sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return buf; } -void show_packet(std::string title, ESPNowPacketPtr packet) { +void show_packet(std::string title, const ESPNowPacketPtr &packet) { ESP_LOGVV(TAG, "%s packet: M:%s H:%cx%cx%c P:%c%c%c 0x%02x S:%02x C:ox%02x~0x%02x S:%02d V:%s", "test", format_mac_addr(packet->peer_as_bytes()).c_str(), packet->content_at(0), packet->content_at(1), packet->content_at(2), packet->content_at(3), packet->content_at(4), packet->content_at(5), @@ -215,21 +215,21 @@ ESPNowProtocol *ESPNowComponent::get_protocol_(uint32_t protocol) { return this->protocols_[protocol]; } -void ESPNowComponent::on_receive_(ESPNowPacketPtr packet) { +void ESPNowComponent::on_receive_(const ESPNowPacketPtr &packet) { ESPNowProtocol *protocol = this->get_protocol_(packet->get_protocol()); if (protocol != nullptr) { protocol->on_receive(packet); } } -void ESPNowComponent::on_sent_(ESPNowPacketPtr packet, bool status) { +void ESPNowComponent::on_sent_(const ESPNowPacketPtr &packet, bool status) { ESPNowProtocol *protocol = this->get_protocol_(packet->get_protocol()); if (protocol != nullptr) { protocol->on_sent(packet, status); } } -void ESPNowComponent::on_new_peer_(ESPNowPacketPtr packet) { +void ESPNowComponent::on_new_peer_(const ESPNowPacketPtr &packet) { ESPNowProtocol *protocol = this->get_protocol_(packet->get_protocol()); if (protocol != nullptr) { protocol->on_new_peer(packet); @@ -272,7 +272,7 @@ void ESPNowComponent::on_data_received(const uint8_t *addr, const uint8_t *data, } } -bool ESPNowComponent::write(ESPNowPacketPtr packet) { +bool ESPNowComponent::write(const ESPNowPacketPtr &packet) { uint8_t *mac = packet->peer_as_bytes(); show_packet("Write", packet); if (this->is_failed()) { @@ -293,7 +293,7 @@ bool ESPNowComponent::write(ESPNowPacketPtr packet) { esp_err_t err = esp_now_send((uint8_t *) &mac, packet->content_bytes(), packet->get_size()); ESP_LOGVV(TAG, "S: 0x%04x.%d B: %d%s.", packet->get_sequents(), packet->attempts, this->send_queue_used(), (err == ESP_OK) ? "" : " FAILED"); - this->defer([this, packet, err]() { this->on_sent_(packet, err == ESP_OK); }); + this->defer([this, packet, err]() { this->on_sent_(std::move(packet), err == ESP_OK); }); return true; } @@ -310,13 +310,13 @@ void ESPNowComponent::runner() { if (!esp_now_is_peer_exist(mac)) { if (!this->auto_add_peer_) { - this->defer([this, packet]() { this->on_new_peer_(packet); }); + this->defer([this, packet]() { this->on_new_peer_(std::move(packet)); }); continue; } else { this->add_peer(packet->peer); } } - this->defer([this, packet]() { this->on_receive_(packet); }); + this->defer([this, packet]() { this->on_receive_(std::move(packet)); }); } packet = std::make_shared(); if (xQueueReceive(this->send_queue_, packet.get(), (TickType_t) 1) == pdTRUE) { @@ -366,7 +366,7 @@ void ESPNowComponent::on_data_sent(const uint8_t *mac_addr, esp_now_send_status_ ESP_LOGV(TAG, "Confirm sent (0x%04x.%d)", packet->get_sequents(), packet->attempts); global_esp_now->defer([packet]() { packet->reload(); - global_esp_now->on_sent_(packet, true); + global_esp_now->on_sent_(std::move(packet), true); auto tmp = std::make_shared(); xQueueReceive(global_esp_now->send_queue_, tmp.get(), 10 / portTICK_PERIOD_MS); @@ -376,7 +376,7 @@ void ESPNowComponent::on_data_sent(const uint8_t *mac_addr, esp_now_send_status_ return; } global_esp_now->defer([packet]() { - global_esp_now->on_sent_(packet, false); + global_esp_now->on_sent_(std::move(packet), false); auto tmp = std::make_shared(); xQueueReceive(global_esp_now->send_queue_, tmp.get(), 10 / portTICK_PERIOD_MS); xQueueSendToFront(global_esp_now->send_queue_, tmp.get(), 10 / portTICK_PERIOD_MS); diff --git a/esphome/components/espnow/espnow.h b/esphome/components/espnow/espnow.h index c49e629578..917b3c0eb0 100644 --- a/esphome/components/espnow/espnow.h +++ b/esphome/components/espnow/espnow.h @@ -147,9 +147,9 @@ class ESPNowProtocol : public Parented { public: ESPNowProtocol(){}; - virtual void on_receive(ESPNowPacketPtr packet){}; - virtual void on_sent(ESPNowPacketPtr packet, bool status){}; - virtual void on_new_peer(ESPNowPacketPtr packet){}; + virtual void on_receive(const ESPNowPacketPtr &packet){}; + virtual void on_sent(const ESPNowPacketPtr &packet, bool status){}; + virtual void on_new_peer(const ESPNowPacketPtr &packet){}; virtual uint32_t get_protocol_id() = 0; uint8_t get_next_sequents() { @@ -181,20 +181,20 @@ class ESPNowDefaultProtocol : public ESPNowProtocol { public: uint32_t get_protocol_id() override { return ESPNOW_MAIN_PROTOCOL_ID; }; - void add_on_receive_callback(std::function &&callback) { + void add_on_receive_callback(std::function &&callback) { this->on_receive_.add(std::move(callback)); } - void on_receive(ESPNowPacketPtr packet) override { this->on_receive_.call(packet); }; + void on_receive(const ESPNowPacketPtr &packet) override { this->on_receive_.call(std::move(packet)); }; - void add_on_sent_callback(std::function &&callback) { + void add_on_sent_callback(std::function &&callback) { this->on_sent_.add(std::move(callback)); } - void on_sent(ESPNowPacketPtr packet, bool status) override { this->on_sent_.call(packet, status); }; + void on_sent(const ESPNowPacketPtr &packet, bool status) override { this->on_sent_.call(std::move(packet), status); }; - void add_on_peer_callback(std::function &&callback) { + void add_on_peer_callback(std::function &&callback) { this->on_new_peer_.add(std::move(callback)); } - void on_new_peer(ESPNowPacketPtr packet) override { this->on_new_peer_.call(packet); }; + void on_new_peer(const ESPNowPacketPtr &packet) override { this->on_new_peer_.call(std::move(packet)); }; protected: CallbackManager on_sent_; @@ -226,7 +226,7 @@ class ESPNowComponent : public Component { void runner(); - bool write(ESPNowPacketPtr packet); + bool write(const ESPNowPacketPtr &packet); void register_protocol(ESPNowProtocol *protocol) { protocol->set_parent(this); @@ -257,9 +257,9 @@ class ESPNowComponent : public Component { bool use_sent_check_{true}; bool lock_{false}; - void on_receive_(ESPNowPacketPtr packet); - void on_sent_(ESPNowPacketPtr packet, bool status); - void on_new_peer_(ESPNowPacketPtr packet); + void on_receive_(const ESPNowPacketPtr &packet); + void on_sent_(const ESPNowPacketPtr &packet, bool status); + void on_new_peer_(const ESPNowPacketPtr &packet); QueueHandle_t receive_queue_{}; QueueHandle_t send_queue_{}; @@ -328,21 +328,23 @@ class ESPNowSentTrigger : public Trigger { public: explicit ESPNowSentTrigger(ESPNowComponent *parent) { parent->get_default_protocol()->add_on_sent_callback( - [this](ESPNowPacketPtr packet, bool status) { this->trigger(packet, status); }); + [this](ESPNowPacketPtr packet, bool status) { this->trigger(std::move(packet), status); }); } }; class ESPNowReceiveTrigger : public Trigger { public: explicit ESPNowReceiveTrigger(ESPNowComponent *parent) { - parent->get_default_protocol()->add_on_receive_callback([this](ESPNowPacketPtr packet) { this->trigger(packet); }); + parent->get_default_protocol()->add_on_receive_callback( + [this](ESPNowPacketPtr packet) { this->trigger(std::move(packet)); }); } }; class ESPNowNewPeerTrigger : public Trigger { public: explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) { - parent->get_default_protocol()->add_on_peer_callback([this](ESPNowPacketPtr packet) { this->trigger(packet); }); + parent->get_default_protocol()->add_on_peer_callback( + [this](ESPNowPacketPtr packet) { this->trigger(std::move(packet)); }); } };