Fix crash at udp.begin()

This commit is contained in:
Pascal Serrarens 2025-04-18 14:36:07 +02:00
parent a17c933908
commit 8ff0fdd78f
3 changed files with 21 additions and 23 deletions

View File

@ -19,6 +19,8 @@
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
WiFiUDP* udp;
void ParticipantUDP::Setup() { void ParticipantUDP::Setup() {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
GetBroadcastAddress(); GetBroadcastAddress();
@ -34,9 +36,9 @@ void ParticipantUDP::Setup() {
return; return;
} }
#endif #endif
std::cout << "UDP.begin " << this->port << std::endl;
udp.begin(this->port); udp = new WiFiUDP();
udp->begin(this->port);
std::cout << "Wifi sync started local " << this->port; std::cout << "Wifi sync started local " << this->port;
if (this->remoteSite != nullptr) if (this->remoteSite != nullptr)
@ -59,14 +61,14 @@ void ParticipantUDP::GetBroadcastAddress() {
void ParticipantUDP::Receive() { void ParticipantUDP::Receive() {
#if defined(ARDUINO) && defined(HAS_WIFI) #if defined(ARDUINO) && defined(HAS_WIFI)
int packetSize = udp.parsePacket(); int packetSize = udp->parsePacket();
while (packetSize > 0) { while (packetSize > 0) {
udp.read(buffer, packetSize); udp->read(buffer, packetSize);
String senderAddress = udp.remoteIP().toString(); String senderAddress = udp->remoteIP().toString();
char sender_ipAddress[16]; char sender_ipAddress[16];
senderAddress.toCharArray(sender_ipAddress, 16); senderAddress.toCharArray(sender_ipAddress, 16);
unsigned int sender_port = udp.remotePort(); unsigned int sender_port = udp->remotePort();
// Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, // Participant* remoteParticipant = this->GetParticipant(sender_ipAddress,
// sender_port); if (remoteParticipant == nullptr) { // sender_port); if (remoteParticipant == nullptr) {
@ -82,7 +84,7 @@ void ParticipantUDP::Receive() {
// ReceiveData(packetSize, remoteParticipant); // ReceiveData(packetSize, remoteParticipant);
ReceiveData(packetSize, sender_ipAddress, sender_port); ReceiveData(packetSize, sender_ipAddress, sender_port);
packetSize = udp.parsePacket(); packetSize = udp->parsePacket();
} }
#endif #endif
} }
@ -99,9 +101,9 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
delay(10); delay(10);
} }
n++; n++;
udp.beginPacket(remoteParticipant->ipAddress, remoteParticipant->port); udp->beginPacket(remoteParticipant->ipAddress, remoteParticipant->port);
udp.write((unsigned char*)buffer, bufferSize); udp->write((unsigned char*)buffer, bufferSize);
} while (udp.endPacket() == 0 && n < 10); } while (udp->endPacket() == 0 && n < 10);
#endif #endif
return true; return true;
@ -113,9 +115,9 @@ bool ParticipantUDP::Publish(IMessage* msg) {
if (bufferSize <= 0) if (bufferSize <= 0)
return true; return true;
udp.beginPacket(this->broadcastIpAddress, this->port); udp->beginPacket(this->broadcastIpAddress, this->port);
udp.write((unsigned char*)buffer, bufferSize); udp->write((unsigned char*)buffer, bufferSize);
udp.endPacket(); udp->endPacket();
// std::cout << "Publish to " << this->broadcastIpAddress << ":" // std::cout << "Publish to " << this->broadcastIpAddress << ":"
// << this->remotePort << "\n"; // << this->remotePort << "\n";

View File

@ -2,10 +2,6 @@
#include "Participants/ParticipantUDP.h" #include "Participants/ParticipantUDP.h"
#if defined(HAS_WIFI)
#include <WiFiUdp.h>
#endif
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
@ -17,12 +13,8 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected:
#if defined(HAS_WIFI)
char* broadcastIpAddress = nullptr; char* broadcastIpAddress = nullptr;
WiFiUDP udp;
#endif
void GetBroadcastAddress(); void GetBroadcastAddress();
}; };

View File

@ -104,14 +104,15 @@ void ParticipantUDP::Update(unsigned long currentTimeMs) {
if (thing == nullptr) if (thing == nullptr)
continue; continue;
if (this->isIsolated == false) { thing->Update(currentTimeMs, true);
if (this->isIsolated == false && thing->owner != this) {
PoseMsg* poseMsg = new PoseMsg(this->networkId, thing); PoseMsg* poseMsg = new PoseMsg(this->networkId, thing);
this->Send(thing->owner, poseMsg); this->Send(thing->owner, poseMsg);
BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing); BinaryMsg* binaryMsg = new BinaryMsg(this->networkId, thing);
this->Send(thing->owner, binaryMsg); this->Send(thing->owner, binaryMsg);
delete poseMsg; delete poseMsg;
} }
thing->Update(currentTimeMs, true);
} }
} }
@ -178,6 +179,8 @@ void ParticipantUDP::SendThingInfo(Participant* remoteParticipant,
} }
bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) { bool ParticipantUDP::Send(Participant* remoteParticipant, IMessage* msg) {
// std::cout << "send msg " << (int)this->buffer[0] << " to "
// << remoteParticipant->ipAddress << std::endl;
int bufferSize = msg->Serialize(this->buffer); int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
return true; return true;
@ -224,6 +227,7 @@ void ParticipantUDP::PublishThingInfo(Thing* thing) {
} }
bool ParticipantUDP::Publish(IMessage* msg) { bool ParticipantUDP::Publish(IMessage* msg) {
// std::cout << "publish msg\n";
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
Windows::ParticipantUDP* thisWindows = Windows::ParticipantUDP* thisWindows =
static_cast<Windows::ParticipantUDP*>(this); static_cast<Windows::ParticipantUDP*>(this);