From 87f3f238b96ab2d13dabe3454e32adfec0ce2970 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Mon, 23 Sep 2024 20:44:40 +0200 Subject: [PATCH] remove reference parameters and replace ESPNowPacketPtr with the real type. --- esphome/components/espnow/espnow.cpp | 28 +++++++++---------- esphome/components/espnow/espnow.h | 42 +++++++++++++++------------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/esphome/components/espnow/espnow.cpp b/esphome/components/espnow/espnow.cpp index 5be727ce65..96e9beb55a 100644 --- a/esphome/components/espnow/espnow.cpp +++ b/esphome/components/espnow/espnow.cpp @@ -39,12 +39,12 @@ 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(const std::string &title, const ESPNowPacketPtr &packet) { +void show_packet(const std::string &title, const ESPNowPacket &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), - packet->content_at(6), packet->content_at(7), packet->crc(), packet->calc_crc(), packet->get_size(), - packet->is_valid() ? "Yes" : "No"); + 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), + packet.content_at(6), packet.content_at(7), packet.crc(), packet.calc_crc(), packet.get_size(), + packet.is_valid() ? "Yes" : "No"); } /* ESPNowPacket ********************************************************************** */ @@ -78,7 +78,7 @@ bool ESPNowPacket::is_valid() { /* ESPNowProtocol ********************************************************************** */ bool ESPNowProtocol::write(uint64_t peer, const uint8_t *data, uint8_t len) { - ESPNowPacketPtr packet = std::make_shared(peer, data, len, this->get_protocol_id()); + std::shared_ptr packet = std::make_shared(peer, data, len, this->get_protocol_id()); packet->set_sequents(this->get_next_sequents()); return this->parent_->write(packet); } @@ -215,21 +215,21 @@ ESPNowProtocol *ESPNowComponent::get_protocol_(uint32_t protocol) { return this->protocols_[protocol]; } -void ESPNowComponent::on_receive_(const ESPNowPacketPtr &packet) { +void ESPNowComponent::on_receive_(const std::shared_ptr packet) { ESPNowProtocol *protocol = this->get_protocol_(packet->get_protocol()); if (protocol != nullptr) { protocol->on_receive(packet); } } -void ESPNowComponent::on_sent_(const ESPNowPacketPtr &packet, bool status) { +void ESPNowComponent::on_sent_(const std::shared_ptr packet, bool status) { ESPNowProtocol *protocol = this->get_protocol_(packet->get_protocol()); if (protocol != nullptr) { protocol->on_sent(packet, status); } } -void ESPNowComponent::on_new_peer_(const ESPNowPacketPtr &packet) { +void ESPNowComponent::on_new_peer_(const std::shared_ptr packet) { ESPNowProtocol *protocol = this->get_protocol_(packet->get_protocol()); if (protocol != nullptr) { protocol->on_new_peer(packet); @@ -255,7 +255,7 @@ void ESPNowComponent::on_data_received(const uint8_t *addr, const uint8_t *data, (wifi_promiscuous_pkt_t *) (data - sizeof(wifi_pkt_rx_ctrl_t) - 39); // = sizeof (espnow_frame_format_t) rx_ctrl = &promiscuous_pkt->rx_ctrl; #endif - ESPNowPacketPtr packet = std::make_shared((uint64_t) *addr, data, (uint8_t) size); + std::shared_ptr packet = std::make_shared((uint64_t) *addr, data, (uint8_t) size); packet->is_broadcast = broadcast; if (rx_ctrl != nullptr) { packet->rssi = rx_ctrl->rssi; @@ -272,7 +272,7 @@ void ESPNowComponent::on_data_received(const uint8_t *addr, const uint8_t *data, } } -bool ESPNowComponent::write(const ESPNowPacketPtr &packet) { +bool ESPNowComponent::write(const std::shared_ptr packet) { uint8_t *mac = packet->peer_as_bytes(); show_packet("Write", packet); if (this->is_failed()) { @@ -293,7 +293,7 @@ bool ESPNowComponent::write(const 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); }); // std::move(packet) + this->defer([this, packet, err]() { this->on_sent_(packet, err == ESP_OK); }); return true; } @@ -301,7 +301,7 @@ bool ESPNowComponent::write(const ESPNowPacketPtr &packet) { } void ESPNowComponent::runner() { - ESPNowPacketPtr packet{nullptr}; + std::shared_ptr packet{nullptr}; for (;;) { packet = std::make_shared(); @@ -351,7 +351,7 @@ void ESPNowComponent::runner() { } void ESPNowComponent::on_data_sent(const uint8_t *mac_addr, esp_now_send_status_t status) { - ESPNowPacketPtr packet = std::make_shared(); + std::shared_ptr packet = std::make_shared(); if (!global_esp_now->use_sent_check_) { return; } diff --git a/esphome/components/espnow/espnow.h b/esphome/components/espnow/espnow.h index 54870dd17c..4b3ab34dda 100644 --- a/esphome/components/espnow/espnow.h +++ b/esphome/components/espnow/espnow.h @@ -139,7 +139,7 @@ struct ESPNowPacket { std::shared_ptr payload_buffer_; }; -typedef std::shared_ptr ESPNowPacketPtr; +// using ESPNowPacketPtr = std::shared_ptr; class ESPNowComponent; @@ -147,9 +147,9 @@ class ESPNowProtocol : public Parented { public: ESPNowProtocol(){}; - 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 void on_receive(const std::shared_ptr packet){}; + virtual void on_sent(const std::shared_ptr packet, bool status){}; + virtual void on_new_peer(const std::shared_ptr packet){}; virtual uint32_t get_protocol_id() = 0; uint8_t get_next_sequents() { @@ -181,25 +181,27 @@ 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(const ESPNowPacketPtr &packet) override { this->on_receive_.call(packet); }; + void on_receive(const std::shared_ptr packet) override { this->on_receive_.call(packet); }; - void add_on_sent_callback(std::function &&callback) { + void add_on_sent_callback(std::function, bool status)> &&callback) { this->on_sent_.add(std::move(callback)); } - void on_sent(const ESPNowPacketPtr &packet, bool status) override { this->on_sent_.call(packet, status); }; + void on_sent(const std::shared_ptr packet, bool status) override { + this->on_sent_.call(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(const ESPNowPacketPtr &packet) override { this->on_new_peer_.call(packet); }; + void on_new_peer(const std::shared_ptr packet) override { this->on_new_peer_.call(packet); }; protected: - CallbackManager on_sent_; - CallbackManager on_receive_; - CallbackManager on_new_peer_; + CallbackManager, bool)> on_sent_; + CallbackManager)> on_receive_; + CallbackManager)> on_new_peer_; }; class ESPNowComponent : public Component { @@ -226,7 +228,7 @@ class ESPNowComponent : public Component { void runner(); - bool write(const ESPNowPacketPtr &packet); + bool write(const std::shared_ptr packet); void register_protocol(ESPNowProtocol *protocol) { protocol->set_parent(this); @@ -257,9 +259,9 @@ class ESPNowComponent : public Component { bool use_sent_check_{true}; bool lock_{false}; - void on_receive_(const ESPNowPacketPtr &packet); - void on_sent_(const ESPNowPacketPtr &packet, bool status); - void on_new_peer_(const ESPNowPacketPtr &packet); + void on_receive_(const std::shared_ptr packet); + void on_sent_(const std::shared_ptr packet, bool status); + void on_new_peer_(const std::shared_ptr packet); QueueHandle_t receive_queue_{}; QueueHandle_t send_queue_{}; @@ -328,7 +330,7 @@ class ESPNowSentTrigger : public Trigger { public: explicit ESPNowSentTrigger(ESPNowComponent *parent) { parent->get_default_protocol()->add_on_sent_callback( - [this](ESPNowPacketPtr packet, bool status) { this->trigger(std::move(packet), status); }); + [this](std::shared_ptr packet, bool status) { this->trigger(packet, status); }); } }; @@ -336,7 +338,7 @@ class ESPNowReceiveTrigger : public Trigger { public: explicit ESPNowReceiveTrigger(ESPNowComponent *parent) { parent->get_default_protocol()->add_on_receive_callback( - [this](ESPNowPacketPtr packet) { this->trigger(std::move(packet)); }); + [this](std::shared_ptr packet) { this->trigger(packet); }); } }; @@ -344,7 +346,7 @@ class ESPNowNewPeerTrigger : public Trigger { public: explicit ESPNowNewPeerTrigger(ESPNowComponent *parent) { parent->get_default_protocol()->add_on_peer_callback( - [this](ESPNowPacketPtr packet) { this->trigger(std::move(packet)); }); + [this](std::shared_ptr packet) { this->trigger(packet); }); } };