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
// 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
EthernetUDP Udp;
EthernetUDP udp;
EthernetConfiguration ethernetConfig;
bool ethernetIsConfigured = false;

View File

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