Compare commits
No commits in common. "fdd23604f5c4a29c9c0b8d757245b2063c4d3c8e" and "fe021e3b8aa65e545d97075110c5804decc0f1bc" have entirely different histories.
fdd23604f5
...
fe021e3b8a
@ -1,17 +1,15 @@
|
|||||||
#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;
|
||||||
@ -37,9 +35,9 @@ void ParticipantUDP::Setup(int localPort,
|
|||||||
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)) <
|
if (bind(this->sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) {
|
||||||
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);
|
||||||
@ -47,21 +45,19 @@ void ParticipantUDP::Setup(int localPort,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initialize the dest_addr structure
|
// Initialize the dest_addr structure
|
||||||
memset(&this->dest_addr, 0,
|
memset(&this->dest_addr, 0, sizeof(this->dest_addr)); // Clear the entire structure
|
||||||
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,
|
inet_pton(AF_INET, this->remoteSite->ipAddress, &this->dest_addr.sin_addr.s_addr);
|
||||||
&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;
|
||||||
@ -80,7 +76,6 @@ 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() {
|
||||||
@ -96,7 +91,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);
|
||||||
@ -112,8 +107,7 @@ void ParticipantUDP::Receive() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// std::cout << "receiving " << packetSize << " bytes, msgId " <<
|
// std::cout << "receiving " << packetSize << " bytes, msgId " << (int)this->buffer[0] << "\n";
|
||||||
// (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);
|
||||||
|
|
||||||
@ -125,9 +119,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) {
|
||||||
@ -135,8 +129,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,
|
int err = sendto(this->sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
|
||||||
(struct sockaddr*)&dest_addr, sizeof(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";
|
||||||
|
|
||||||
|
@ -2,9 +2,7 @@
|
|||||||
|
|
||||||
#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 {
|
||||||
@ -17,13 +15,12 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
|||||||
bool Publish(IMessage* msg);
|
bool Publish(IMessage* msg);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
#if defined(IDF_VER)
|
char broadcastIpAddress[INET_ADDRSTRLEN]; // IPv4 address can have a max of 15 characters
|
||||||
char broadcastIpAddress[INET_ADDRSTRLEN];
|
// + null terminator
|
||||||
|
|
||||||
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();
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user