From 304856ea0ae91b58384a191f7b64bc7ed99a91d9 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 11 Apr 2025 11:23:54 +0200 Subject: [PATCH] Improve ESP-IDF defines --- EspIdf/EspIdfParticipant.cpp | 38 +++++++++++++++++++++--------------- EspIdf/EspIdfParticipant.h | 9 ++++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/EspIdf/EspIdfParticipant.cpp b/EspIdf/EspIdfParticipant.cpp index f479a18..b0f7974 100644 --- a/EspIdf/EspIdfParticipant.cpp +++ b/EspIdf/EspIdfParticipant.cpp @@ -1,15 +1,17 @@ #include "EspIdfParticipant.h" +#if defined(IDF_VER) #include "esp_wifi.h" +#endif namespace RoboidControl { namespace EspIdf { void ParticipantUDP::Setup(int localPort, - const char* remoteIpAddress, - int remotePort) { + const char* remoteIpAddress, + int remotePort) { #if defined(IDF_VER) -std::cout << "Set up UDP\n"; + std::cout << "Set up UDP\n"; GetBroadcastAddress(); wifi_ap_record_t ap_info; @@ -35,9 +37,9 @@ std::cout << "Set up UDP\n"; local_addr.sin_addr.s_addr = htonl(INADDR_ANY); // Listen on all available network interfaces - // Bind the socket to the address and port - if (bind(this->sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) { + if (bind(this->sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) < + 0) { std::cout << "Unable to bind UDP socket: errno " << errno << "\n"; close(sockfd); vTaskDelete(NULL); @@ -45,19 +47,21 @@ std::cout << "Set up UDP\n"; } // Initialize the dest_addr structure - memset(&this->dest_addr, 0, sizeof(this->dest_addr)); // Clear the entire structure + memset(&this->dest_addr, 0, + sizeof(this->dest_addr)); // Clear the entire structure this->dest_addr.sin_family = AF_INET; this->dest_addr.sin_port = htons(this->remoteSite->port); - inet_pton(AF_INET, this->remoteSite->ipAddress, &this->dest_addr.sin_addr.s_addr); - + inet_pton(AF_INET, this->remoteSite->ipAddress, + &this->dest_addr.sin_addr.s_addr); std::cout << "Wifi sync started local " << this->port << ", remote " << this->remoteSite->ipAddress << ":" << this->remoteSite->port << "\n"; -#endif // IDF_VER +#endif // IDF_VER } void ParticipantUDP::GetBroadcastAddress() { +#if defined(IDF_VER) // SOMEHOW, THIS FUNCTION RESULTS IN MEMORY CORRUPION... esp_netif_ip_info_t ip_info; @@ -76,6 +80,7 @@ void ParticipantUDP::GetBroadcastAddress() { snprintf(this->broadcastIpAddress, INET_ADDRSTRLEN, IPSTR, IP2STR(&broadcast_addr.u_addr.ip4)); std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n"; +#endif // IDF_VER } void ParticipantUDP::Receive() { @@ -90,8 +95,8 @@ void ParticipantUDP::Receive() { std::cout << "poll() error\n"; return; } - - //char buffer[1024]; + + // char buffer[1024]; struct sockaddr_in source_addr; socklen_t addr_len = sizeof(source_addr); @@ -107,7 +112,8 @@ void ParticipantUDP::Receive() { break; } - // std::cout << "receiving " << packetSize << " bytes, msgId " << (int)this->buffer[0] << "\n"; + // std::cout << "receiving " << packetSize << " bytes, msgId " << + // (int)this->buffer[0] << "\n"; inet_ntoa_r(source_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN); unsigned int sender_port = ntohs(source_addr.sin_port); @@ -119,9 +125,9 @@ void ParticipantUDP::Receive() { return; } } - //std::cout << "no more messages\n"; + // std::cout << "no more messages\n"; -#endif // IDF_VER +#endif // IDF_VER } bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { @@ -129,8 +135,8 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) { std::cout << "Sending to " << remoteParticipant->ipAddress << ":" << remoteParticipant->port << "\n"; - int err = sendto(this->sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr, - sizeof(dest_addr)); + int err = sendto(this->sockfd, buffer, bufferSize, 0, + (struct sockaddr*)&dest_addr, sizeof(dest_addr)); if (errno != 0) std::cout << "Send error " << err << " or " << errno << "\n"; diff --git a/EspIdf/EspIdfParticipant.h b/EspIdf/EspIdfParticipant.h index 1e80f0e..cf1c6bf 100644 --- a/EspIdf/EspIdfParticipant.h +++ b/EspIdf/EspIdfParticipant.h @@ -2,7 +2,9 @@ #include "Participants/ParticipantUDP.h" +#if defined(IDF_VER) #include "lwip/sockets.h" +#endif namespace RoboidControl { namespace EspIdf { @@ -15,12 +17,13 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP { bool Publish(IMessage* msg); protected: - char broadcastIpAddress[INET_ADDRSTRLEN]; // IPv4 address can have a max of 15 characters - // + null terminator +#if defined(IDF_VER) + char broadcastIpAddress[INET_ADDRSTRLEN]; int sockfd; struct sockaddr_in dest_addr; - //struct sockaddr_in src_addr; + // struct sockaddr_in src_addr; +#endif void GetBroadcastAddress(); };