Compare commits

..

2 Commits

Author SHA1 Message Date
fdd23604f5 Merge branch 'main' into Braccio 2025-04-11 11:24:19 +02:00
304856ea0a Improve ESP-IDF defines 2025-04-11 11:23:54 +02:00
2 changed files with 28 additions and 19 deletions

View File

@ -1,15 +1,17 @@
#include "EspIdfParticipant.h" #include "EspIdfParticipant.h"
#if defined(IDF_VER)
#include "esp_wifi.h" #include "esp_wifi.h"
#endif
namespace RoboidControl { namespace RoboidControl {
namespace EspIdf { namespace EspIdf {
void ParticipantUDP::Setup(int localPort, void ParticipantUDP::Setup(int localPort,
const char* remoteIpAddress, const char* remoteIpAddress,
int remotePort) { int remotePort) {
#if defined(IDF_VER) #if defined(IDF_VER)
std::cout << "Set up UDP\n"; std::cout << "Set up UDP\n";
GetBroadcastAddress(); GetBroadcastAddress();
wifi_ap_record_t ap_info; wifi_ap_record_t ap_info;
@ -35,9 +37,9 @@ std::cout << "Set up UDP\n";
local_addr.sin_addr.s_addr = local_addr.sin_addr.s_addr =
htonl(INADDR_ANY); // Listen on all available network interfaces htonl(INADDR_ANY); // Listen on all available network interfaces
// Bind the socket to the address and port // 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"; std::cout << "Unable to bind UDP socket: errno " << errno << "\n";
close(sockfd); close(sockfd);
vTaskDelete(NULL); vTaskDelete(NULL);
@ -45,19 +47,21 @@ std::cout << "Set up UDP\n";
} }
// Initialize the dest_addr structure // 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_family = AF_INET;
this->dest_addr.sin_port = htons(this->remoteSite->port); 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 " std::cout << "Wifi sync started local " << this->port << ", remote "
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port << this->remoteSite->ipAddress << ":" << this->remoteSite->port
<< "\n"; << "\n";
#endif // IDF_VER #endif // IDF_VER
} }
void ParticipantUDP::GetBroadcastAddress() { void ParticipantUDP::GetBroadcastAddress() {
#if defined(IDF_VER)
// SOMEHOW, THIS FUNCTION RESULTS IN MEMORY CORRUPION... // SOMEHOW, THIS FUNCTION RESULTS IN MEMORY CORRUPION...
esp_netif_ip_info_t ip_info; esp_netif_ip_info_t ip_info;
@ -76,6 +80,7 @@ void ParticipantUDP::GetBroadcastAddress() {
snprintf(this->broadcastIpAddress, INET_ADDRSTRLEN, IPSTR, snprintf(this->broadcastIpAddress, INET_ADDRSTRLEN, IPSTR,
IP2STR(&broadcast_addr.u_addr.ip4)); IP2STR(&broadcast_addr.u_addr.ip4));
std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n"; std::cout << "Broadcast address: " << this->broadcastIpAddress << "\n";
#endif // IDF_VER
} }
void ParticipantUDP::Receive() { void ParticipantUDP::Receive() {
@ -91,7 +96,7 @@ void ParticipantUDP::Receive() {
return; return;
} }
//char buffer[1024]; // char buffer[1024];
struct sockaddr_in source_addr; struct sockaddr_in source_addr;
socklen_t addr_len = sizeof(source_addr); socklen_t addr_len = sizeof(source_addr);
@ -107,7 +112,8 @@ void ParticipantUDP::Receive() {
break; 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); inet_ntoa_r(source_addr.sin_addr, sender_ipAddress, INET_ADDRSTRLEN);
unsigned int sender_port = ntohs(source_addr.sin_port); unsigned int sender_port = ntohs(source_addr.sin_port);
@ -119,9 +125,9 @@ void ParticipantUDP::Receive() {
return; 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) { bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
@ -129,8 +135,8 @@ bool ParticipantUDP::Send(Participant* remoteParticipant, int bufferSize) {
std::cout << "Sending to " << remoteParticipant->ipAddress << ":" std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
<< remoteParticipant->port << "\n"; << remoteParticipant->port << "\n";
int err = sendto(this->sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr, int err = sendto(this->sockfd, buffer, bufferSize, 0,
sizeof(dest_addr)); (struct sockaddr*)&dest_addr, sizeof(dest_addr));
if (errno != 0) if (errno != 0)
std::cout << "Send error " << err << " or " << errno << "\n"; std::cout << "Send error " << err << " or " << errno << "\n";

View File

@ -2,7 +2,9 @@
#include "Participants/ParticipantUDP.h" #include "Participants/ParticipantUDP.h"
#if defined(IDF_VER)
#include "lwip/sockets.h" #include "lwip/sockets.h"
#endif
namespace RoboidControl { namespace RoboidControl {
namespace EspIdf { namespace EspIdf {
@ -15,12 +17,13 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
protected: protected:
char broadcastIpAddress[INET_ADDRSTRLEN]; // IPv4 address can have a max of 15 characters #if defined(IDF_VER)
// + null terminator char broadcastIpAddress[INET_ADDRSTRLEN];
int sockfd; int sockfd;
struct sockaddr_in dest_addr; struct sockaddr_in dest_addr;
//struct sockaddr_in src_addr; // struct sockaddr_in src_addr;
#endif
void GetBroadcastAddress(); void GetBroadcastAddress();
}; };