From 8901e6933a48120460ffcb5a4e9240fe6b5ee432 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 27 Jun 2025 15:00:54 +0200 Subject: [PATCH] ESP-IDF sending now works --- EspIdf/EspIdfParticipant.cpp | 72 ++++++++++++++++++++------------- EspIdf/EspIdfParticipant.h | 18 +++++---- Participants/ParticipantUDP.cpp | 6 +-- 3 files changed, 56 insertions(+), 40 deletions(-) diff --git a/EspIdf/EspIdfParticipant.cpp b/EspIdf/EspIdfParticipant.cpp index 774f6a3..44dc0e8 100644 --- a/EspIdf/EspIdfParticipant.cpp +++ b/EspIdf/EspIdfParticipant.cpp @@ -10,7 +10,6 @@ namespace RoboidControl { void ParticipantUDP::SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) { -#if defined(IDF_VER) std::cout << "Set up UDP\n"; GetBroadcastAddress(); @@ -56,23 +55,23 @@ void ParticipantUDP::SetupUDP(int localPort, // inet_pton(AF_INET, this->remoteSite->ipAddress, // &this->dest_addr.sin_addr.s_addr); + this->connected = true; + std::cout << "Wifi sync started local " << localPort << ", remote " << this->remoteSite->ipAddress << ":" << this->remoteSite->port << "\n"; - std::cout << "socket: " << (int)this->sock << std::endl; - ParticipantMsg* msg = new ParticipantMsg(this->networkId); - int bufferSize = msg->Serialize(this->buffer); - int err = sendto(this->sock, buffer, bufferSize, 0, - (struct sockaddr*)&dest_addr, sizeof(dest_addr)); - if (errno != 0) - std::cout << "AASend error " << err << " or " << errno << "\n"; - else - std::cout << "AASend SUCCESS\n"; + // std::cout << "socket: " << (int)this->sock << std::endl; + // ParticipantMsg* msg = new ParticipantMsg(this->networkId); + // int bufferSize = msg->Serialize(this->buffer); + // int err = sendto(this->sock, buffer, bufferSize, 0, + // (struct sockaddr*)&dest_addr, sizeof(dest_addr)); + // if (errno != 0) + // std::cout << "AASend error " << err << " or " << errno << "\n"; + // else + // std::cout << "AASend SUCCESS\n"; - SendTest(); - -#endif // IDF_VER + //SendTest(); } void ParticipantUDP::GetBroadcastAddress() { @@ -151,20 +150,36 @@ ParticipantUDP::ParticipantUDP(int port) : ParticipantUDPGeneric(port) {} ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) : ParticipantUDPGeneric(ipAddress, port, localPort) {} -bool ParticipantUDP::SendTest() { -#if defined(IDF_VER) - std::cout << "socket: " << (int)this->sock << std::endl; - ParticipantMsg* msg = new ParticipantMsg(this->networkId); - int bSize = msg->Serialize(this->buffer); - int err = sendto(this->sock, buffer, bSize, 0, (struct sockaddr*)&dest_addr, - sizeof(dest_addr)); - if (errno != 0) - std::cout << "BBSend error " << err << " or " << errno << "\n"; - else - std::cout << "BBSend SUCCESS\n"; +// bool ParticipantUDP::SendTest() { +// #if defined(IDF_VER) +// // std::cout << "socket: " << (int)this->sock << std::endl; +// // UBaseType_t stack_size = uxTaskGetStackHighWaterMark(NULL); // NULL to check the main task +// // size_t free_heap = xPortGetFreeHeapSize(); +// // std::cout << "Stack High Water Mark: " << stack_size << " heap " << free_heap << std::endl; -#endif - return true; +// ParticipantMsg* msg = new ParticipantMsg(this->networkId); +// int bSize = msg->Serialize(this->buffer); +// // std::cout << "buffer size " << bSize << std::endl; +// int err = sendto(this->sock, buffer, bSize, 0, (struct sockaddr*)&dest_addr, +// sizeof(dest_addr)); +// if (errno != 0) +// std::cout << "BBSend error " << err << " or " << errno << "\n"; +// else +// std::cout << "BBSend SUCCESS\n"; + +// #endif +// return true; +// } + +bool ParticipantUDP::Send(IMessage* msg) { + int bufferSize = msg->Serialize(this->buffer); + if (bufferSize <= 0) + return true; + + std::cout << "send msg " << (static_cast(this->buffer[0]) & 0xff) + << " to " << this->remoteSite->ipAddress << std::endl; + + return this->SendTo(this->remoteSite, bufferSize); } bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, @@ -174,7 +189,7 @@ bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, char ip_str[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &dest_addr.sin_addr, ip_str, sizeof(ip_str)); - std::cout << "Sending to " << ip_str << ":" << port << "\n"; + std::cout << "Sending " << bufferSize << " bytes to " << ip_str << ":" << port << "\n"; // Print the IP address and port // printf("IP Address: %s\n", ip_str); @@ -183,10 +198,9 @@ bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, this->dest_addr.sin_port = htons(remoteParticipant->port); this->dest_addr.sin_addr.s_addr = inet_addr(remoteParticipant->ipAddress); - std::cout << "socket: " << (int)this->sock << std::endl; int err = sendto(this->sock, buffer, bufferSize, 0, (struct sockaddr*)&this->dest_addr, sizeof(this->dest_addr)); - if (errno != 0) + if (errno < 0) std::cout << "Send error " << err << " or " << errno << "\n"; #endif diff --git a/EspIdf/EspIdfParticipant.h b/EspIdf/EspIdfParticipant.h index b465843..1c74ebf 100644 --- a/EspIdf/EspIdfParticipant.h +++ b/EspIdf/EspIdfParticipant.h @@ -9,7 +9,7 @@ namespace RoboidControl { class ParticipantUDP : public ParticipantUDPGeneric { public: - /// @brief Create a participant without connecting to a site + /// @brief Create a participant without connecting to a site /// @param port The port on which the participant communicates /// These participant typically broadcast Participant messages to let site /// servers on the local network know their presence. Alternatively they can @@ -19,19 +19,22 @@ class ParticipantUDP : public ParticipantUDPGeneric { /// @param ipAddress The IP address of the site /// @param port The port used by the site /// @param localPort The port used by the local participant - ParticipantUDP(const char* ipAddress, - int port = 7681, - int localPort = 7681); + ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681); void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Receive(); bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize); bool Publish(IMessage* msg); - bool SendTest(); + // bool SendTest(); - //bool Send(IMessage* msg) override; - void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) override; + /// @brief Sens a message to the remote site (if set) + /// @param msg The message to send + /// @return True if a message could be sent. + bool Send(IMessage* msg) override; + void SetupUDP(int localPort, + const char* remoteIpAddress, + int remotePort) override; void ReceiveUDP() override; protected: @@ -46,7 +49,6 @@ class ParticipantUDP : public ParticipantUDPGeneric { void GetBroadcastAddress(); }; - } // namespace RoboidControl #endif diff --git a/Participants/ParticipantUDP.cpp b/Participants/ParticipantUDP.cpp index 2670107..1fdb695 100644 --- a/Participants/ParticipantUDP.cpp +++ b/Participants/ParticipantUDP.cpp @@ -231,18 +231,18 @@ void ParticipantUDPGeneric::Update(bool recurse) { if (this->connected == false) begin(); - // EspIdf::ParticipantUDP* thisEspIdf = - // static_cast(this); + ParticipantUDP* thisEspIdf = + static_cast(this); // thisEspIdf->SendTest(); if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) { ParticipantMsg* msg = new ParticipantMsg(this->networkId); + // thisEspIdf->SendTest(); if (this->remoteSite == nullptr) this->Publish(msg); else this->Send(msg); - // thisEspIdf->SendTest(); delete msg; this->nextPublishMe = currentTimeMs + this->publishInterval;