Improved wifi code (but still not working)
This commit is contained in:
parent
1add0647e1
commit
9a26e98b0f
@ -22,26 +22,25 @@ void ParticipantUDP::Setup(int localPort,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a UDP socket
|
// Create a UDP socket
|
||||||
this->sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
if (this->sockfd < 0) {
|
if (this->sock < 0) {
|
||||||
std::cout << "Unable to create UDP socket: errno " << errno << "\n";
|
std::cout << "Unable to create UDP socket: errno " << errno << "\n";
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the server address structure
|
// Set up the receiving address
|
||||||
struct sockaddr_in local_addr;
|
struct sockaddr_in local_addr;
|
||||||
memset(&local_addr, 0, sizeof(local_addr));
|
memset(&local_addr, 0, sizeof(local_addr));
|
||||||
local_addr.sin_family = AF_INET;
|
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 =
|
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 receiving address
|
||||||
if (bind(this->sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr)) <
|
if (bind(this->sock, (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(sock);
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -54,7 +53,7 @@ void ParticipantUDP::Setup(int localPort,
|
|||||||
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 " << localPort << ", remote "
|
||||||
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port
|
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port
|
||||||
<< "\n";
|
<< "\n";
|
||||||
#endif // IDF_VER
|
#endif // IDF_VER
|
||||||
@ -86,7 +85,7 @@ void ParticipantUDP::GetBroadcastAddress() {
|
|||||||
void ParticipantUDP::Receive() {
|
void ParticipantUDP::Receive() {
|
||||||
#if defined(IDF_VER)
|
#if defined(IDF_VER)
|
||||||
struct pollfd fds[1];
|
struct pollfd fds[1];
|
||||||
fds[0].fd = sockfd;
|
fds[0].fd = sock;
|
||||||
fds[0].events = POLLIN; // We're looking for data available to read
|
fds[0].events = POLLIN; // We're looking for data available to read
|
||||||
|
|
||||||
// Use poll() with a timeout of 0 to return immediately
|
// Use poll() with a timeout of 0 to return immediately
|
||||||
@ -103,7 +102,7 @@ void ParticipantUDP::Receive() {
|
|||||||
char sender_ipAddress[INET_ADDRSTRLEN];
|
char sender_ipAddress[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
while (ret > 0 && fds[0].revents & POLLIN) {
|
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);
|
(struct sockaddr*)&source_addr, &addr_len);
|
||||||
if (packetSize < 0) {
|
if (packetSize < 0) {
|
||||||
std::cout << "recvfrom() error\n";
|
std::cout << "recvfrom() error\n";
|
||||||
@ -132,10 +131,10 @@ void ParticipantUDP::Receive() {
|
|||||||
|
|
||||||
bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
|
bool ParticipantUDP::SendTo(Participant* remoteParticipant, int bufferSize) {
|
||||||
#if defined(IDF_VER)
|
#if defined(IDF_VER)
|
||||||
// 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->sock, buffer, bufferSize, 0,
|
||||||
(struct sockaddr*)&dest_addr, 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";
|
||||||
@ -154,7 +153,7 @@ bool ParticipantUDP::Publish(IMessage* msg) {
|
|||||||
dest_addr.sin_family = AF_INET;
|
dest_addr.sin_family = AF_INET;
|
||||||
dest_addr.sin_port = htons(this->port);
|
dest_addr.sin_port = htons(this->port);
|
||||||
inet_pton(AF_INET, this->broadcastIpAddress, &dest_addr.sin_addr.s_addr);
|
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));
|
sizeof(dest_addr));
|
||||||
if (err != 0)
|
if (err != 0)
|
||||||
std::cout << "Publish error\n";
|
std::cout << "Publish error\n";
|
||||||
|
@ -20,7 +20,7 @@ class ParticipantUDP : public RoboidControl::ParticipantUDP {
|
|||||||
#if defined(IDF_VER)
|
#if defined(IDF_VER)
|
||||||
char broadcastIpAddress[INET_ADDRSTRLEN];
|
char broadcastIpAddress[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
int sockfd;
|
int sock;
|
||||||
struct sockaddr_in dest_addr;
|
struct sockaddr_in dest_addr;
|
||||||
// struct sockaddr_in src_addr;
|
// struct sockaddr_in src_addr;
|
||||||
#endif
|
#endif
|
||||||
|
@ -112,8 +112,8 @@ void Participant::Add(Thing* thing, bool checkId) {
|
|||||||
thing->id = highestIx + 1;
|
thing->id = highestIx + 1;
|
||||||
this->things.push_back(thing);
|
this->things.push_back(thing);
|
||||||
#endif
|
#endif
|
||||||
// std::cout << "Add thing with generated ID " << this->ipAddress << ":"
|
std::cout << "Add thing with generated ID " << this->ipAddress << ":"
|
||||||
// << this->port << "[" << (int)thing->id << "]\n";
|
<< this->port << "[" << (int)thing->id << "]\n";
|
||||||
} else {
|
} else {
|
||||||
Thing* foundThing = Get(thing->owner->networkId, thing->id);
|
Thing* foundThing = Get(thing->owner->networkId, thing->id);
|
||||||
if (foundThing == nullptr) {
|
if (foundThing == nullptr) {
|
||||||
@ -122,12 +122,12 @@ void Participant::Add(Thing* thing, bool checkId) {
|
|||||||
#else
|
#else
|
||||||
this->things.push_back(thing);
|
this->things.push_back(thing);
|
||||||
#endif
|
#endif
|
||||||
// std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
|
std::cout << "Add thing " << this->ipAddress << ":" << this->port <<
|
||||||
// "["
|
"["
|
||||||
// << (int)thing->id << "]\n";
|
<< (int)thing->id << "]\n";
|
||||||
} else {
|
} else {
|
||||||
// std::cout << "Did not add, existing thing " << this->ipAddress << ":"
|
std::cout << "Did not add, existing thing " << this->ipAddress << ":"
|
||||||
// << this->port << "[" << (int)thing->id << "]\n";
|
<< this->port << "[" << (int)thing->id << "]\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@
|
|||||||
#include "Posix/PosixParticipant.h"
|
#include "Posix/PosixParticipant.h"
|
||||||
#include "Windows/WindowsParticipant.h"
|
#include "Windows/WindowsParticipant.h"
|
||||||
|
|
||||||
|
#include "Things/DifferentialDrive.h"
|
||||||
#include "Things/DistanceSensor.h"
|
#include "Things/DistanceSensor.h"
|
||||||
#include "Things/TouchSensor.h"
|
#include "Things/TouchSensor.h"
|
||||||
#include "Things/DifferentialDrive.h"
|
|
||||||
|
|
||||||
#include <string.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 = Thing::LocalRoot(); //::LocalParticipant->root;
|
||||||
this->root->owner = this;
|
this->root->owner = this;
|
||||||
this->root->name = "UDP Root";
|
this->root->name = "UDP Root";
|
||||||
|
std::cout << "P2 " << (int)this->root << std::endl;
|
||||||
this->Add(this->root);
|
this->Add(this->root);
|
||||||
|
|
||||||
Participant::ReplaceLocalParticipant(*this);
|
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 = Thing::LocalRoot(); // Participant::LocalParticipant->root;
|
||||||
this->root->owner = this;
|
this->root->owner = this;
|
||||||
this->root->name = "UDP Root";
|
this->root->name = "UDP Root";
|
||||||
|
std::cout << "P1 " << (int)this->root << std::endl;
|
||||||
this->Add(this->root);
|
this->Add(this->root);
|
||||||
|
|
||||||
Participant::ReplaceLocalParticipant(*this);
|
Participant::ReplaceLocalParticipant(*this);
|
||||||
@ -102,7 +104,6 @@ void ParticipantUDP::Update(bool recurse) {
|
|||||||
this->Send(msg);
|
this->Send(msg);
|
||||||
|
|
||||||
delete msg;
|
delete msg;
|
||||||
|
|
||||||
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
this->nextPublishMe = currentTimeMs + this->publishInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,10 +115,13 @@ void ParticipantUDP::Update(bool recurse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ParticipantUDP::UpdateMyThings() {
|
void ParticipantUDP::UpdateMyThings() {
|
||||||
|
std::cout << "# things = " << this->things.size() << std::endl;
|
||||||
for (Thing* thing : this->things) {
|
for (Thing* thing : this->things) {
|
||||||
|
std::cout << ".\n";
|
||||||
if (thing == nullptr) // || thing->GetParent() != nullptr)
|
if (thing == nullptr) // || thing->GetParent() != nullptr)
|
||||||
continue;
|
continue;
|
||||||
|
std::cout << (int)this->root << std::endl;
|
||||||
|
std::cout << thing->name << std::endl;
|
||||||
// Why don't we do recursive?
|
// Why don't we do recursive?
|
||||||
// Because when a thing creates a thing in the update,
|
// Because when a thing creates a thing in the update,
|
||||||
// that new thing is not sent out (because of hierarchyChanged)
|
// that new thing is not sent out (because of hierarchyChanged)
|
||||||
|
@ -42,7 +42,7 @@ Thing::Thing(Participant* owner) {
|
|||||||
|
|
||||||
this->owner = owner;
|
this->owner = owner;
|
||||||
this->owner->Add(this);
|
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) {
|
void Thing::CreateRoot(Participant* owner) {
|
||||||
@ -262,7 +262,7 @@ void Thing::Update(bool recursive) {
|
|||||||
this->nameChanged = false;
|
this->nameChanged = false;
|
||||||
|
|
||||||
if (recursive) {
|
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++) {
|
for (unsigned char childIx = 0; childIx < this->childCount; childIx++) {
|
||||||
Thing* child = this->children[childIx];
|
Thing* child = this->children[childIx];
|
||||||
if (child == nullptr)
|
if (child == nullptr)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user