mirror of
https://github.com/esphome/esphome.git
synced 2024-11-24 07:58:09 +01:00
remove reference parameters and replace ESPNowPacketPtr with the real type.
This commit is contained in:
parent
4453d75943
commit
87f3f238b9
2 changed files with 36 additions and 34 deletions
|
@ -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<ESPNowPacket>(peer, data, len, this->get_protocol_id());
|
||||
std::shared_ptr<ESPNowPacket> packet = std::make_shared<ESPNowPacket>(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<ESPNowPacket> 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<ESPNowPacket> 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<ESPNowPacket> 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<ESPNowPacket>((uint64_t) *addr, data, (uint8_t) size);
|
||||
std::shared_ptr<ESPNowPacket> packet = std::make_shared<ESPNowPacket>((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<ESPNowPacket> 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<ESPNowPacket> packet{nullptr};
|
||||
|
||||
for (;;) {
|
||||
packet = std::make_shared<ESPNowPacket>();
|
||||
|
@ -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<ESPNowPacket>();
|
||||
std::shared_ptr<ESPNowPacket> packet = std::make_shared<ESPNowPacket>();
|
||||
if (!global_esp_now->use_sent_check_) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -139,7 +139,7 @@ struct ESPNowPacket {
|
|||
std::shared_ptr<ByteBuffer> payload_buffer_;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ESPNowPacket> ESPNowPacketPtr;
|
||||
// using ESPNowPacketPtr = std::shared_ptr<ESPNowPacket>;
|
||||
|
||||
class ESPNowComponent;
|
||||
|
||||
|
@ -147,9 +147,9 @@ class ESPNowProtocol : public Parented<ESPNowComponent> {
|
|||
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<ESPNowPacket> packet){};
|
||||
virtual void on_sent(const std::shared_ptr<ESPNowPacket> packet, bool status){};
|
||||
virtual void on_new_peer(const std::shared_ptr<ESPNowPacket> 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<void(const ESPNowPacketPtr &)> &&callback) {
|
||||
void add_on_receive_callback(std::function<void(const std::shared_ptr<ESPNowPacket>)> &&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<ESPNowPacket> packet) override { this->on_receive_.call(packet); };
|
||||
|
||||
void add_on_sent_callback(std::function<void(const ESPNowPacketPtr &, bool status)> &&callback) {
|
||||
void add_on_sent_callback(std::function<void(const std::shared_ptr<ESPNowPacket>, 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<ESPNowPacket> packet, bool status) override {
|
||||
this->on_sent_.call(packet, status);
|
||||
};
|
||||
|
||||
void add_on_peer_callback(std::function<void(const ESPNowPacketPtr &)> &&callback) {
|
||||
void add_on_peer_callback(std::function<void(const std::shared_ptr<ESPNowPacket>)> &&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<ESPNowPacket> packet) override { this->on_new_peer_.call(packet); };
|
||||
|
||||
protected:
|
||||
CallbackManager<void(const ESPNowPacketPtr &, bool)> on_sent_;
|
||||
CallbackManager<void(const ESPNowPacketPtr &)> on_receive_;
|
||||
CallbackManager<void(const ESPNowPacketPtr &)> on_new_peer_;
|
||||
CallbackManager<void(const std::shared_ptr<ESPNowPacket>, bool)> on_sent_;
|
||||
CallbackManager<void(const std::shared_ptr<ESPNowPacket>)> on_receive_;
|
||||
CallbackManager<void(const std::shared_ptr<ESPNowPacket>)> 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<ESPNowPacket> 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<ESPNowPacket> packet);
|
||||
void on_sent_(const std::shared_ptr<ESPNowPacket> packet, bool status);
|
||||
void on_new_peer_(const std::shared_ptr<ESPNowPacket> packet);
|
||||
|
||||
QueueHandle_t receive_queue_{};
|
||||
QueueHandle_t send_queue_{};
|
||||
|
@ -328,7 +330,7 @@ class ESPNowSentTrigger : public Trigger<ESPNowPacketPtr, bool> {
|
|||
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<ESPNowPacket> packet, bool status) { this->trigger(packet, status); });
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -336,7 +338,7 @@ class ESPNowReceiveTrigger : public Trigger<ESPNowPacketPtr> {
|
|||
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<ESPNowPacket> packet) { this->trigger(packet); });
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -344,7 +346,7 @@ class ESPNowNewPeerTrigger : public Trigger<ESPNowPacketPtr> {
|
|||
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<ESPNowPacket> packet) { this->trigger(packet); });
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue