This commit is contained in:
Christoph Hagen 2024-04-20 21:00:09 +02:00
parent 1504ce6b0c
commit 3afe8b16a9
2 changed files with 11 additions and 15 deletions

View File

@ -24,13 +24,8 @@ private:
// UDP // UDP
// buffers for receiving and sending data
char udpReceiveBuffer[SIGNED_MESSAGE_SIZE]; // buffer to hold incoming packet
// An EthernetUDP instance to send and receive packets over UDP // An EthernetUDP instance to send and receive packets over UDP
EthernetUDP Udp; EthernetUDP udp;
EthernetConfiguration ethernetConfig; EthernetConfiguration ethernetConfig;
bool ethernetIsConfigured = false; bool ethernetIsConfigured = false;

View File

@ -52,7 +52,7 @@ void SesameController::configure(ServoConfiguration servoConfig, ServerConfigura
server.configure(serverConfig, this); server.configure(serverConfig, this);
Serial.println("[INFO] Server connection configured"); Serial.println("[INFO] Server connection configured");
Udp.begin(ethernetConfig.udpPort); udp.begin(ethernetConfig.udpPort);
Serial.println("[INFO] Local UDP connection configured"); Serial.println("[INFO] Local UDP connection configured");
} }
@ -73,32 +73,33 @@ void SesameController::checkLocalMessage() {
bool SesameController::readLocalMessage() { bool SesameController::readLocalMessage() {
// if there's data available, read a packet // if there's data available, read a packet
int packetSize = Udp.parsePacket(); int packetSize = udp.parsePacket();
if (packetSize == 0) { if (packetSize == 0) {
return false; return false;
} }
if (packetSize != SIGNED_MESSAGE_SIZE) { if (packetSize != SIGNED_MESSAGE_SIZE) {
Serial.print("Received packet of invalid size "); Serial.print("[WARN] Received UDP packet of invalid size ");
Serial.println(packetSize); Serial.println(packetSize);
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote); prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
return true; return true;
} }
int bytesRead = Udp.read((uint8_t*) &receivedLocalMessage, SIGNED_MESSAGE_SIZE); int bytesRead = udp.read((uint8_t*) &receivedLocalMessage, SIGNED_MESSAGE_SIZE);
if (bytesRead != SIGNED_MESSAGE_SIZE) { if (bytesRead != SIGNED_MESSAGE_SIZE) {
Serial.println("Failed to read full local message"); Serial.println("[WARN] Failed to read full local message");
prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote); prepareResponseBuffer(MessageResult::InvalidMessageSizeFromRemote);
return true; return true;
} }
Serial.println("Received local message"); Serial.println("[INFO] Received local message");
processMessage(&receivedLocalMessage, true); processMessage(&receivedLocalMessage, true);
return true; return true;
} }
void SesameController::sendPreparedLocalResponse() { void SesameController::sendPreparedLocalResponse() {
// send a reply to the IP address and port that sent us the packet we received // send a reply to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); udp.beginPacket(udp.remoteIP(), udp.remotePort());
Udp.write((uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE); udp.write((uint8_t*) &outgoingMessage, SIGNED_MESSAGE_SIZE);
Udp.endPacket(); udp.endPacket();
Serial.println("[INFO] Sent local response");
} }
// MARK: Server // MARK: Server