Improved wifi code (but still not working)

This commit is contained in:
Pascal Serrarens 2025-06-26 17:25:32 +02:00
parent 1add0647e1
commit 9a26e98b0f
5 changed files with 31 additions and 28 deletions

View File

@ -22,26 +22,25 @@ void ParticipantUDP::Setup(int localPort,
}
// Create a UDP socket
this->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (this->sockfd < 0) {
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (this->sock < 0) {
std::cout << "Unable to create UDP socket: errno " << errno << "\n";
vTaskDelete(NULL);
return;
}
// Set up the server address structure
// Set up the receiving address
struct sockaddr_in local_addr;
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(this->port);
local_addr.sin_port = htons(localPort);
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) {
// Bind the socket to the receiving address
if (bind(this->sock, (struct sockaddr*)&local_addr, sizeof(local_addr)) < 0) {
std::cout << "Unable to bind UDP socket: errno " << errno << "\n";
close(sockfd);
close(sock);
vTaskDelete(NULL);
return;
}
@ -54,7 +53,7 @@ void ParticipantUDP::Setup(int localPort,
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 " << localPort << ", remote "
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port
<< "\n";
#endif // IDF_VER
@ -86,7 +85,7 @@ void ParticipantUDP::GetBroadcastAddress() {
void ParticipantUDP::Receive() {
#if defined(IDF_VER)
struct pollfd fds[1];
fds[0].fd = sockfd;
fds[0].fd = sock;
fds[0].events = POLLIN; // We're looking for data available to read
// Use poll() with a timeout of 0 to return immediately
@ -103,7 +102,7 @@ void ParticipantUDP::Receive() {
char sender_ipAddress[INET_ADDRSTRLEN];
while (ret > 0 && fds[0].revents & POLLIN) {
int packetSize = recvfrom(this->sockfd, buffer, sizeof(buffer) - 1, 0,
int packetSize = recvfrom(this->sock, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr*)&source_addr, &addr_len);
if (packetSize < 0) {
std::cout << "recvfrom() error\n";
@ -132,10 +131,10 @@ void ParticipantUDP::Receive() {
bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
#if defined(IDF_VER)
// std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
// << remoteParticipant->port << "\n";
std::cout << "Sending to " << remoteParticipant->ipAddress << ":"
<< remoteParticipant->port << "\n";
int err = sendto(this->sockfd, buffer, bufferSize, 0,
int err = sendto(this->sock, buffer, bufferSize, 0,
(struct sockaddr*)&dest_addr, sizeof(dest_addr));
if (errno != 0)
std::cout << "Send error " << err << " or " << errno << "\n";
@ -154,7 +153,7 @@ bool ParticipantUDP::Publish(IMessage* msg) {
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(this->port);
inet_pton(AF_INET, this->broadcastIpAddress, &dest_addr.sin_addr.s_addr);
int err = sendto(sockfd, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
int err = sendto(sock, buffer, bufferSize, 0, (struct sockaddr*)&dest_addr,
sizeof(dest_addr));
if (err != 0)
std::cout << "Publish error\n";

View File

@ -20,7 +20,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
#if defined(IDF_VER)
char broadcastIpAddress[INET_ADDRSTRLEN];
int sockfd;
int sock;
struct sockaddr_in dest_addr;
// struct sockaddr_in src_addr;
#endif

View File

@ -112,8 +112,8 @@ void Participant::Add(Thing* thing, bool checkId) {
thing->id = highestIx + 1;
this->things.push_back(thing);
#endif
// std::cout << "Add thing with generated ID " << this->ipAddress << ":"
// << this->port << "[" << (int)thing->id << "]\n";
std::cout << "Add thing with generated ID " << this->ipAddress << ":"
<< this->port << "[" << (int)thing->id << "]\n";
} else {
Thing* foundThing = Get(thing->owner->networkId, thing->id);
if (foundThing == nullptr) {
@ -122,12 +122,12 @@ void Participant::Add(Thing* thing, bool checkId) {
#else
this->things.push_back(thing);
#endif
// std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
// "["
// << (int)thing->id << "]\n";
std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
"["
<< (int)thing->id << "]\n";
} else {
// std::cout << "Did not add, existing thing " << this->ipAddress << ":"
// << this->port << "[" << (int)thing->id << "]\n";
std::cout << "Did not add, existing thing " << this->ipAddress << ":"
<< this->port << "[" << (int)thing->id << "]\n";
}
}
}

View File

@ -8,9 +8,9 @@
#include "Posix/PosixParticipant.h"
#include "Windows/WindowsParticipant.h"
#include "Things/DifferentialDrive.h"
#include "Things/DistanceSensor.h"
#include "Things/TouchSensor.h"
#include "Things/DifferentialDrive.h"
#include <string.h>
@ -28,6 +28,7 @@ ParticipantUDP::ParticipantUDP(int port) : Participant("127.0.0.1", port) {
this->root = Thing::LocalRoot(); //::LocalParticipant->root;
this->root->owner = this;
this->root->name = "UDP Root";
std::cout << "P2 " << (int)this->root << std::endl;
this->Add(this->root);
Participant::ReplaceLocalParticipant(*this);
@ -45,6 +46,7 @@ ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
this->root = Thing::LocalRoot(); // Participant::LocalParticipant->root;
this->root->owner = this;
this->root->name = "UDP Root";
std::cout << "P1 " << (int)this->root << std::endl;
this->Add(this->root);
Participant::ReplaceLocalParticipant(*this);
@ -102,7 +104,6 @@ void ParticipantUDP::Update(bool recurse) {
this->Send(msg);
delete msg;
this->nextPublishMe = currentTimeMs + this->publishInterval;
}
@ -114,10 +115,13 @@ void ParticipantUDP::Update(bool recurse) {
}
void ParticipantUDP::UpdateMyThings() {
std::cout << "# things = " << this->things.size() << std::endl;
for (Thing* thing : this->things) {
std::cout << ".\n";
if (thing == nullptr) // || thing->GetParent() != nullptr)
continue;
std::cout << (int)this->root << std::endl;
std::cout << thing->name << std::endl;
// Why don't we do recursive?
// Because when a thing creates a thing in the update,
// that new thing is not sent out (because of hierarchyChanged)

View File

@ -42,7 +42,7 @@ Thing::Thing(Participant* owner) {
this->owner = owner;
this->owner->Add(this);
// std::cout << this->owner->name << ": New root thing " << std::endl;
std::cout << this->owner->name << ": New root thing " << std::endl;
}
void Thing::CreateRoot(Participant* owner) {
@ -262,7 +262,7 @@ void Thing::Update(bool recursive) {
this->nameChanged = false;
if (recursive) {
// std::cout << "# children: " << (int)this->childCount << std::endl;
std::cout << "# children: " << (int)this->childCount << std::endl;
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
Thing* child = this->children[childIx];
if (child == nullptr)