mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
[espnow] Cleanup method visibility and naming (#16109)
This commit is contained in:
@@ -26,9 +26,9 @@ espnow_ns = cg.esphome_ns.namespace("espnow")
|
|||||||
ESPNowComponent = espnow_ns.class_("ESPNowComponent", cg.Component)
|
ESPNowComponent = espnow_ns.class_("ESPNowComponent", cg.Component)
|
||||||
|
|
||||||
# Handler interfaces that other components can use to register callbacks
|
# Handler interfaces that other components can use to register callbacks
|
||||||
ESPNowReceivedPacketHandler = espnow_ns.class_("ESPNowReceivedPacketHandler")
|
ESPNowReceivePacketHandler = espnow_ns.class_("ESPNowReceivePacketHandler")
|
||||||
ESPNowUnknownPeerHandler = espnow_ns.class_("ESPNowUnknownPeerHandler")
|
ESPNowUnknownPeerHandler = espnow_ns.class_("ESPNowUnknownPeerHandler")
|
||||||
ESPNowBroadcastedHandler = espnow_ns.class_("ESPNowBroadcastedHandler")
|
ESPNowBroadcastHandler = espnow_ns.class_("ESPNowBroadcastHandler")
|
||||||
|
|
||||||
ESPNowRecvInfo = espnow_ns.class_("ESPNowRecvInfo")
|
ESPNowRecvInfo = espnow_ns.class_("ESPNowRecvInfo")
|
||||||
ESPNowRecvInfoConstRef = ESPNowRecvInfo.operator("const").operator("ref")
|
ESPNowRecvInfoConstRef = ESPNowRecvInfo.operator("const").operator("ref")
|
||||||
@@ -48,10 +48,10 @@ OnUnknownPeerTrigger = espnow_ns.class_(
|
|||||||
"OnUnknownPeerTrigger", ESPNowHandlerTrigger, ESPNowUnknownPeerHandler
|
"OnUnknownPeerTrigger", ESPNowHandlerTrigger, ESPNowUnknownPeerHandler
|
||||||
)
|
)
|
||||||
OnReceiveTrigger = espnow_ns.class_(
|
OnReceiveTrigger = espnow_ns.class_(
|
||||||
"OnReceiveTrigger", ESPNowHandlerTrigger, ESPNowReceivedPacketHandler
|
"OnReceiveTrigger", ESPNowHandlerTrigger, ESPNowReceivePacketHandler
|
||||||
)
|
)
|
||||||
OnBroadcastedTrigger = espnow_ns.class_(
|
OnBroadcastTrigger = espnow_ns.class_(
|
||||||
"OnBroadcastedTrigger", ESPNowHandlerTrigger, ESPNowBroadcastedHandler
|
"OnBroadcastTrigger", ESPNowHandlerTrigger, ESPNowBroadcastHandler
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -94,7 +94,7 @@ CONFIG_SCHEMA = cv.All(
|
|||||||
),
|
),
|
||||||
cv.Optional(CONF_ON_BROADCAST): automation.validate_automation(
|
cv.Optional(CONF_ON_BROADCAST): automation.validate_automation(
|
||||||
{
|
{
|
||||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnBroadcastedTrigger),
|
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(OnBroadcastTrigger),
|
||||||
cv.Optional(CONF_ADDRESS): cv.mac_address,
|
cv.Optional(CONF_ADDRESS): cv.mac_address,
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
@@ -140,11 +140,11 @@ async def to_code(config):
|
|||||||
|
|
||||||
for on_receive in config.get(CONF_ON_RECEIVE, []):
|
for on_receive in config.get(CONF_ON_RECEIVE, []):
|
||||||
trigger = await _trigger_to_code(on_receive)
|
trigger = await _trigger_to_code(on_receive)
|
||||||
cg.add(var.register_received_handler(trigger))
|
cg.add(var.register_receive_handler(trigger))
|
||||||
|
|
||||||
for on_receive in config.get(CONF_ON_BROADCAST, []):
|
for on_receive in config.get(CONF_ON_BROADCAST, []):
|
||||||
trigger = await _trigger_to_code(on_receive)
|
trigger = await _trigger_to_code(on_receive)
|
||||||
cg.add(var.register_broadcasted_handler(trigger))
|
cg.add(var.register_broadcast_handler(trigger))
|
||||||
|
|
||||||
|
|
||||||
# ========================================== A C T I O N S ================================================
|
# ========================================== A C T I O N S ================================================
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
void play(const Ts &...x) override { /* ignore - see play_complex */
|
void play(const Ts &...x) override { /* ignore - see play_complex */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +76,6 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
|
|||||||
this->error_.stop();
|
this->error_.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
|
||||||
ActionList<Ts...> sent_;
|
ActionList<Ts...> sent_;
|
||||||
ActionList<Ts...> error_;
|
ActionList<Ts...> error_;
|
||||||
|
|
||||||
@@ -89,7 +89,7 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
|
|||||||
template<typename... Ts> class AddPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
|
template<typename... Ts> class AddPeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
|
||||||
TEMPLATABLE_VALUE(peer_address_t, address);
|
TEMPLATABLE_VALUE(peer_address_t, address);
|
||||||
|
|
||||||
public:
|
protected:
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
peer_address_t address = this->address_.value(x...);
|
peer_address_t address = this->address_.value(x...);
|
||||||
this->parent_->add_peer(address.data());
|
this->parent_->add_peer(address.data());
|
||||||
@@ -99,7 +99,7 @@ template<typename... Ts> class AddPeerAction : public Action<Ts...>, public Pare
|
|||||||
template<typename... Ts> class DeletePeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
|
template<typename... Ts> class DeletePeerAction : public Action<Ts...>, public Parented<ESPNowComponent> {
|
||||||
TEMPLATABLE_VALUE(peer_address_t, address);
|
TEMPLATABLE_VALUE(peer_address_t, address);
|
||||||
|
|
||||||
public:
|
protected:
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
peer_address_t address = this->address_.value(x...);
|
peer_address_t address = this->address_.value(x...);
|
||||||
this->parent_->del_peer(address.data());
|
this->parent_->del_peer(address.data());
|
||||||
@@ -107,8 +107,9 @@ template<typename... Ts> class DeletePeerAction : public Action<Ts...>, public P
|
|||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class SetChannelAction : public Action<Ts...>, public Parented<ESPNowComponent> {
|
template<typename... Ts> class SetChannelAction : public Action<Ts...>, public Parented<ESPNowComponent> {
|
||||||
public:
|
|
||||||
TEMPLATABLE_VALUE(uint8_t, channel)
|
TEMPLATABLE_VALUE(uint8_t, channel)
|
||||||
|
|
||||||
|
protected:
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
if (this->parent_->is_wifi_enabled()) {
|
if (this->parent_->is_wifi_enabled()) {
|
||||||
return;
|
return;
|
||||||
@@ -125,9 +126,9 @@ class OnReceiveTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_t *,
|
|||||||
memcpy(this->address_, address.data(), ESP_NOW_ETH_ALEN);
|
memcpy(this->address_, address.data(), ESP_NOW_ETH_ALEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
explicit OnReceiveTrigger() : has_address_(false) {}
|
explicit OnReceiveTrigger() {}
|
||||||
|
|
||||||
bool on_received(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override {
|
bool on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override {
|
||||||
bool match = !this->has_address_ || (memcmp(this->address_, info.src_addr, ESP_NOW_ETH_ALEN) == 0);
|
bool match = !this->has_address_ || (memcmp(this->address_, info.src_addr, ESP_NOW_ETH_ALEN) == 0);
|
||||||
if (!match)
|
if (!match)
|
||||||
return false;
|
return false;
|
||||||
@@ -138,7 +139,7 @@ class OnReceiveTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_t *,
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool has_address_{false};
|
bool has_address_{false};
|
||||||
uint8_t address_[ESP_NOW_ETH_ALEN];
|
uint8_t address_[ESP_NOW_ETH_ALEN]{};
|
||||||
};
|
};
|
||||||
class OnUnknownPeerTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_t *, uint8_t>,
|
class OnUnknownPeerTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_t *, uint8_t>,
|
||||||
public ESPNowUnknownPeerHandler {
|
public ESPNowUnknownPeerHandler {
|
||||||
@@ -148,15 +149,15 @@ class OnUnknownPeerTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_
|
|||||||
return false; // Return false to continue processing other internal handlers
|
return false; // Return false to continue processing other internal handlers
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
class OnBroadcastedTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_t *, uint8_t>,
|
class OnBroadcastTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_t *, uint8_t>,
|
||||||
public ESPNowBroadcastedHandler {
|
public ESPNowBroadcastHandler {
|
||||||
public:
|
public:
|
||||||
explicit OnBroadcastedTrigger(std::array<uint8_t, ESP_NOW_ETH_ALEN> address) : has_address_(true) {
|
explicit OnBroadcastTrigger(std::array<uint8_t, ESP_NOW_ETH_ALEN> address) : has_address_(true) {
|
||||||
memcpy(this->address_, address.data(), ESP_NOW_ETH_ALEN);
|
memcpy(this->address_, address.data(), ESP_NOW_ETH_ALEN);
|
||||||
}
|
}
|
||||||
explicit OnBroadcastedTrigger() : has_address_(false) {}
|
explicit OnBroadcastTrigger() {}
|
||||||
|
|
||||||
bool on_broadcasted(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override {
|
bool on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override {
|
||||||
bool match = !this->has_address_ || (memcmp(this->address_, info.src_addr, ESP_NOW_ETH_ALEN) == 0);
|
bool match = !this->has_address_ || (memcmp(this->address_, info.src_addr, ESP_NOW_ETH_ALEN) == 0);
|
||||||
if (!match)
|
if (!match)
|
||||||
return false;
|
return false;
|
||||||
@@ -167,7 +168,7 @@ class OnBroadcastedTrigger : public Trigger<const ESPNowRecvInfo &, const uint8_
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool has_address_{false};
|
bool has_address_{false};
|
||||||
uint8_t address_[ESP_NOW_ETH_ALEN];
|
uint8_t address_[ESP_NOW_ETH_ALEN]{};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace esphome::espnow
|
} // namespace esphome::espnow
|
||||||
|
|||||||
@@ -299,13 +299,13 @@ void ESPNowComponent::loop() {
|
|||||||
format_hex_pretty_to(hex_buf, packet->packet_.receive.data, packet->packet_.receive.size));
|
format_hex_pretty_to(hex_buf, packet->packet_.receive.data, packet->packet_.receive.size));
|
||||||
#endif
|
#endif
|
||||||
if (memcmp(info.des_addr, ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0) {
|
if (memcmp(info.des_addr, ESPNOW_BROADCAST_ADDR, ESP_NOW_ETH_ALEN) == 0) {
|
||||||
for (auto *handler : this->broadcasted_handlers_) {
|
for (auto *handler : this->broadcast_handlers_) {
|
||||||
if (handler->on_broadcasted(info, packet->packet_.receive.data, packet->packet_.receive.size))
|
if (handler->on_broadcast(info, packet->packet_.receive.data, packet->packet_.receive.size))
|
||||||
break; // If a handler returns true, stop processing further handlers
|
break; // If a handler returns true, stop processing further handlers
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (auto *handler : this->received_handlers_) {
|
for (auto *handler : this->receive_handlers_) {
|
||||||
if (handler->on_received(info, packet->packet_.receive.data, packet->packet_.receive.size))
|
if (handler->on_receive(info, packet->packet_.receive.data, packet->packet_.receive.size))
|
||||||
break; // If a handler returns true, stop processing further handlers
|
break; // If a handler returns true, stop processing further handlers
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ using peer_address_t = std::array<uint8_t, ESP_NOW_ETH_ALEN>;
|
|||||||
enum class ESPNowTriggers : uint8_t {
|
enum class ESPNowTriggers : uint8_t {
|
||||||
TRIGGER_NONE = 0,
|
TRIGGER_NONE = 0,
|
||||||
ON_NEW_PEER = 1,
|
ON_NEW_PEER = 1,
|
||||||
ON_RECEIVED = 2,
|
ON_RECEIVE = 2,
|
||||||
ON_BROADCASTED = 3,
|
ON_BROADCAST = 3,
|
||||||
ON_SUCCEED = 10,
|
ON_SUCCEED = 10,
|
||||||
ON_FAILED = 11,
|
ON_FAILED = 11,
|
||||||
};
|
};
|
||||||
@@ -74,18 +74,18 @@ class ESPNowReceivedPacketHandler {
|
|||||||
/// @param data Pointer to the received data payload
|
/// @param data Pointer to the received data payload
|
||||||
/// @param size Size of the received data in bytes
|
/// @param size Size of the received data in bytes
|
||||||
/// @return true if the packet was handled, false otherwise
|
/// @return true if the packet was handled, false otherwise
|
||||||
virtual bool on_received(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) = 0;
|
virtual bool on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) = 0;
|
||||||
};
|
};
|
||||||
/// Handler interface for receiving broadcasted ESPNow packets
|
/// Handler interface for receiving ESPNow broadcast packets
|
||||||
/// Components should inherit from this class to handle incoming ESPNow data
|
/// Components should inherit from this class to handle incoming ESPNow data
|
||||||
class ESPNowBroadcastedHandler {
|
class ESPNowBroadcastHandler {
|
||||||
public:
|
public:
|
||||||
/// Called when a broadcasted ESPNow packet is received
|
/// Called when an ESPNow broadcast packet is received
|
||||||
/// @param info Information about the received packet (sender MAC, etc.)
|
/// @param info Information about the received packet (sender MAC, etc.)
|
||||||
/// @param data Pointer to the received data payload
|
/// @param data Pointer to the received data payload
|
||||||
/// @param size Size of the received data in bytes
|
/// @param size Size of the received data in bytes
|
||||||
/// @return true if the packet was handled, false otherwise
|
/// @return true if the packet was handled, false otherwise
|
||||||
virtual bool on_broadcasted(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) = 0;
|
virtual bool on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class ESPNowComponent : public Component {
|
class ESPNowComponent : public Component {
|
||||||
@@ -136,13 +136,11 @@ class ESPNowComponent : public Component {
|
|||||||
esp_err_t send(const uint8_t *peer_address, const uint8_t *payload, size_t size,
|
esp_err_t send(const uint8_t *peer_address, const uint8_t *payload, size_t size,
|
||||||
const send_callback_t &callback = nullptr);
|
const send_callback_t &callback = nullptr);
|
||||||
|
|
||||||
void register_received_handler(ESPNowReceivedPacketHandler *handler) { this->received_handlers_.push_back(handler); }
|
void register_receive_handler(ESPNowReceivedPacketHandler *handler) { this->receive_handlers_.push_back(handler); }
|
||||||
void register_unknown_peer_handler(ESPNowUnknownPeerHandler *handler) {
|
void register_unknown_peer_handler(ESPNowUnknownPeerHandler *handler) {
|
||||||
this->unknown_peer_handlers_.push_back(handler);
|
this->unknown_peer_handlers_.push_back(handler);
|
||||||
}
|
}
|
||||||
void register_broadcasted_handler(ESPNowBroadcastedHandler *handler) {
|
void register_broadcast_handler(ESPNowBroadcastHandler *handler) { this->broadcast_handlers_.push_back(handler); }
|
||||||
this->broadcasted_handlers_.push_back(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
friend void on_data_received(const esp_now_recv_info_t *info, const uint8_t *data, int size);
|
friend void on_data_received(const esp_now_recv_info_t *info, const uint8_t *data, int size);
|
||||||
@@ -156,8 +154,8 @@ class ESPNowComponent : public Component {
|
|||||||
void send_();
|
void send_();
|
||||||
|
|
||||||
std::vector<ESPNowUnknownPeerHandler *> unknown_peer_handlers_;
|
std::vector<ESPNowUnknownPeerHandler *> unknown_peer_handlers_;
|
||||||
std::vector<ESPNowReceivedPacketHandler *> received_handlers_;
|
std::vector<ESPNowReceivedPacketHandler *> receive_handlers_;
|
||||||
std::vector<ESPNowBroadcastedHandler *> broadcasted_handlers_;
|
std::vector<ESPNowBroadcastHandler *> broadcast_handlers_;
|
||||||
|
|
||||||
std::vector<ESPNowPeer> peers_{};
|
std::vector<ESPNowPeer> peers_{};
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,10 @@ void ESPNowTransport::setup() {
|
|||||||
this->peer_address_[5]);
|
this->peer_address_[5]);
|
||||||
|
|
||||||
// Register received handler
|
// Register received handler
|
||||||
this->parent_->register_received_handler(this);
|
this->parent_->register_receive_handler(this);
|
||||||
|
|
||||||
// Register broadcasted handler
|
// Register broadcast handler
|
||||||
this->parent_->register_broadcasted_handler(this);
|
this->parent_->register_broadcast_handler(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ESPNowTransport::send_packet(const std::vector<uint8_t> &buf) const {
|
void ESPNowTransport::send_packet(const std::vector<uint8_t> &buf) const {
|
||||||
@@ -56,7 +56,7 @@ void ESPNowTransport::send_packet(const std::vector<uint8_t> &buf) const {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPNowTransport::on_received(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
|
bool ESPNowTransport::on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
|
||||||
ESP_LOGV(TAG, "Received packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
|
ESP_LOGV(TAG, "Received packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
|
||||||
info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
|
info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ bool ESPNowTransport::on_received(const ESPNowRecvInfo &info, const uint8_t *dat
|
|||||||
return false; // Allow other handlers to run
|
return false; // Allow other handlers to run
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ESPNowTransport::on_broadcasted(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
|
bool ESPNowTransport::on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) {
|
||||||
ESP_LOGV(TAG, "Received broadcast packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
|
ESP_LOGV(TAG, "Received broadcast packet of size %u from %02X:%02X:%02X:%02X:%02X:%02X", size, info.src_addr[0],
|
||||||
info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
|
info.src_addr[1], info.src_addr[2], info.src_addr[3], info.src_addr[4], info.src_addr[5]);
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ namespace espnow {
|
|||||||
class ESPNowTransport : public packet_transport::PacketTransport,
|
class ESPNowTransport : public packet_transport::PacketTransport,
|
||||||
public Parented<ESPNowComponent>,
|
public Parented<ESPNowComponent>,
|
||||||
public ESPNowReceivedPacketHandler,
|
public ESPNowReceivedPacketHandler,
|
||||||
public ESPNowBroadcastedHandler {
|
public ESPNowBroadcastHandler {
|
||||||
public:
|
public:
|
||||||
void setup() override;
|
void setup() override;
|
||||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||||
@@ -25,8 +25,8 @@ class ESPNowTransport : public packet_transport::PacketTransport,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ESPNow handler interface
|
// ESPNow handler interface
|
||||||
bool on_received(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override;
|
bool on_receive(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override;
|
||||||
bool on_broadcasted(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override;
|
bool on_broadcast(const ESPNowRecvInfo &info, const uint8_t *data, uint8_t size) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void send_packet(const std::vector<uint8_t> &buf) const override;
|
void send_packet(const std::vector<uint8_t> &buf) const override;
|
||||||
|
|||||||
Reference in New Issue
Block a user