mirror of
https://github.com/esphome/esphome.git
synced 2026-05-27 20:53:46 +08:00
[e131] Fix E1.31 on ESP8266 and RP2040 by restoring WiFiUDP support (#14086)
This commit is contained in:
committed by
Jonathan Swoboda
parent
2491b4f85c
commit
25b14f9953
@@ -14,12 +14,17 @@ static const int PORT = 5568;
|
|||||||
E131Component::E131Component() {}
|
E131Component::E131Component() {}
|
||||||
|
|
||||||
E131Component::~E131Component() {
|
E131Component::~E131Component() {
|
||||||
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
if (this->socket_) {
|
if (this->socket_) {
|
||||||
this->socket_->close();
|
this->socket_->close();
|
||||||
}
|
}
|
||||||
|
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||||
|
this->udp_.stop();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void E131Component::setup() {
|
void E131Component::setup() {
|
||||||
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
this->socket_ = socket::socket_ip(SOCK_DGRAM, IPPROTO_IP);
|
this->socket_ = socket::socket_ip(SOCK_DGRAM, IPPROTO_IP);
|
||||||
|
|
||||||
int enable = 1;
|
int enable = 1;
|
||||||
@@ -50,6 +55,13 @@ void E131Component::setup() {
|
|||||||
this->mark_failed();
|
this->mark_failed();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||||
|
if (!this->udp_.begin(PORT)) {
|
||||||
|
ESP_LOGW(TAG, "Cannot bind E1.31 to port %d.", PORT);
|
||||||
|
this->mark_failed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
join_igmp_groups_();
|
join_igmp_groups_();
|
||||||
}
|
}
|
||||||
@@ -59,19 +71,36 @@ void E131Component::loop() {
|
|||||||
int universe = 0;
|
int universe = 0;
|
||||||
uint8_t buf[1460];
|
uint8_t buf[1460];
|
||||||
|
|
||||||
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
ssize_t len = this->socket_->read(buf, sizeof(buf));
|
ssize_t len = this->socket_->read(buf, sizeof(buf));
|
||||||
if (len == -1) {
|
if (len == -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->packet_(buf, (size_t) len, universe, packet)) {
|
if (!this->packet_(buf, (size_t) len, universe, packet)) {
|
||||||
ESP_LOGV(TAG, "Invalid packet received of size %zd.", len);
|
ESP_LOGV(TAG, "Invalid packet received of size %d.", (int) len);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this->process_(universe, packet)) {
|
if (!this->process_(universe, packet)) {
|
||||||
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
|
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
|
||||||
}
|
}
|
||||||
|
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||||
|
while (auto packet_size = this->udp_.parsePacket()) {
|
||||||
|
auto len = this->udp_.read(buf, sizeof(buf));
|
||||||
|
if (len <= 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!this->packet_(buf, (size_t) len, universe, packet)) {
|
||||||
|
ESP_LOGV(TAG, "Invalid packet received of size %d.", (int) len);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this->process_(universe, packet)) {
|
||||||
|
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void E131Component::add_effect(E131AddressableLightEffect *light_effect) {
|
void E131Component::add_effect(E131AddressableLightEffect *light_effect) {
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "esphome/core/defines.h"
|
#include "esphome/core/defines.h"
|
||||||
#ifdef USE_NETWORK
|
#ifdef USE_NETWORK
|
||||||
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
#include "esphome/components/socket/socket.h"
|
#include "esphome/components/socket/socket.h"
|
||||||
|
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||||
|
#include <WiFiUdp.h>
|
||||||
|
#endif
|
||||||
#include "esphome/core/component.h"
|
#include "esphome/core/component.h"
|
||||||
|
|
||||||
#include <cinttypes>
|
#include <cinttypes>
|
||||||
@@ -45,7 +49,11 @@ class E131Component : public esphome::Component {
|
|||||||
void leave_(int universe);
|
void leave_(int universe);
|
||||||
|
|
||||||
E131ListenMethod listen_method_{E131_MULTICAST};
|
E131ListenMethod listen_method_{E131_MULTICAST};
|
||||||
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
std::unique_ptr<socket::Socket> socket_;
|
std::unique_ptr<socket::Socket> socket_;
|
||||||
|
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
|
||||||
|
WiFiUDP udp_;
|
||||||
|
#endif
|
||||||
std::vector<E131AddressableLightEffect *> light_effects_;
|
std::vector<E131AddressableLightEffect *> light_effects_;
|
||||||
std::map<int, int> universe_consumers_;
|
std::map<int, int> universe_consumers_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -62,8 +62,10 @@ const size_t E131_MIN_PACKET_SIZE = reinterpret_cast<size_t>(&((E131RawPacket *)
|
|||||||
bool E131Component::join_igmp_groups_() {
|
bool E131Component::join_igmp_groups_() {
|
||||||
if (listen_method_ != E131_MULTICAST)
|
if (listen_method_ != E131_MULTICAST)
|
||||||
return false;
|
return false;
|
||||||
|
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
|
||||||
if (this->socket_ == nullptr)
|
if (this->socket_ == nullptr)
|
||||||
return false;
|
return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
for (auto universe : universe_consumers_) {
|
for (auto universe : universe_consumers_) {
|
||||||
if (!universe.second)
|
if (!universe.second)
|
||||||
|
|||||||
Reference in New Issue
Block a user