2023-08-09 12:55:11 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "server.h"
|
|
|
|
#include "servo.h"
|
|
|
|
#include "message.h"
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
struct EthernetConfiguration {
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
// The MAC address of the ethernet connection
|
|
|
|
uint8_t macAddress[6];
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
// The master-in slave-out pin of the SPI connection for the Ethernet module
|
|
|
|
int8_t spiPinMiso;
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
// The master-out slave-in pin of the SPI connection for the Ethernet module
|
|
|
|
int8_t spiPinMosi;
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
// The slave clock pin of the SPI connection for the Ethernet module
|
|
|
|
int8_t spiPinSclk;
|
2023-11-04 11:14:40 +01:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
// The slave-select pin of the SPI connection for the Ethernet module
|
|
|
|
int8_t spiPinSS;
|
|
|
|
|
|
|
|
unsigned long dhcpLeaseTimeoutMs;
|
|
|
|
unsigned long dhcpLeaseResponseTimeoutMs;
|
|
|
|
|
|
|
|
// The static IP address to assign if DHCP fails
|
|
|
|
uint8_t manualIp[4];
|
|
|
|
|
|
|
|
// The IP address of the DNS server, if DHCP fails
|
|
|
|
uint8_t manualDnsAddress[4];
|
|
|
|
|
|
|
|
uint32_t socketHeartbeatIntervalMs;
|
|
|
|
uint32_t socketHeartbeatTimeoutMs;
|
|
|
|
uint8_t socketHeartbeatFailureReconnectCount;
|
2023-08-09 15:02:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct KeyConfiguration {
|
|
|
|
|
|
|
|
const uint8_t* remoteKey;
|
|
|
|
|
|
|
|
const uint8_t* localKey;
|
2023-12-05 20:46:41 +01:00
|
|
|
|
|
|
|
uint32_t challengeExpiryMs;
|
2023-08-09 15:02:24 +02:00
|
|
|
};
|
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
class SesameController: public ServerConnectionCallbacks {
|
|
|
|
|
|
|
|
public:
|
2023-12-05 20:46:41 +01:00
|
|
|
SesameController(uint16_t localWebServerPort);
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
void configure(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig);
|
2023-08-09 13:13:38 +02:00
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
void loop(uint32_t millis);
|
2023-08-09 12:55:11 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
uint32_t currentTime = 0;
|
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
ServerConnection server;
|
|
|
|
ServoController servo;
|
|
|
|
AsyncWebServer localWebServer;
|
2023-08-09 12:55:11 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
EthernetConfiguration ethernetConfig;
|
|
|
|
bool ethernetIsConfigured = false;
|
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
KeyConfiguration keyConfig;
|
|
|
|
|
|
|
|
bool isReconnecting = false;
|
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
// Buffer to get local message
|
|
|
|
SignedMessage receivedLocalMessage;
|
|
|
|
|
|
|
|
uint32_t currentClientChallenge;
|
|
|
|
uint32_t currentChallengeExpiry = 0;
|
|
|
|
uint32_t currentServerChallenge;
|
|
|
|
|
|
|
|
SignedMessage outgoingMessage;
|
|
|
|
|
|
|
|
bool hasCurrentChallenge() {
|
|
|
|
return currentChallengeExpiry > currentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearCurrentChallenge() {
|
|
|
|
currentClientChallenge = 0;
|
|
|
|
currentServerChallenge = 0;
|
|
|
|
currentChallengeExpiry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send an error Response over the web socket.
|
|
|
|
*
|
|
|
|
* @param result The error result to send
|
|
|
|
* @param discardMessage Indicate if the stored message should be cleared.
|
|
|
|
*
|
|
|
|
* Note: Only clear the message if no other operation is in progress.
|
|
|
|
*/
|
|
|
|
void sendErrorResponseToServer(MessageResult result, bool discardMessage = true);
|
2023-08-09 12:55:11 +02:00
|
|
|
|
2023-08-09 15:02:24 +02:00
|
|
|
void ensureWebSocketConnection();
|
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
void handleLocalMessage(AsyncWebServerRequest *request);
|
2023-12-05 20:46:41 +01:00
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
bool convertHexMessageToBinary(const char* str);
|
|
|
|
|
|
|
|
void handleServerMessage(uint8_t* payload, size_t length);
|
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
/**
|
|
|
|
* @brief Callback to send an error back to the server via the web socket.
|
|
|
|
*
|
|
|
|
* This function is called when the socket get's an error.
|
|
|
|
*
|
|
|
|
* @param event The error to report back
|
|
|
|
*/
|
|
|
|
void sendServerError(MessageResult event);
|
|
|
|
|
|
|
|
void processMessage(SignedMessage* message);
|
|
|
|
MessageResult verifyAndProcessReceivedMessage(SignedMessage* message);
|
2023-08-09 12:55:11 +02:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
void prepareResponseBuffer(MessageResult event, Message* message = NULL);
|
2023-08-09 12:55:11 +02:00
|
|
|
void sendPreparedLocalResponse(AsyncWebServerRequest *request);
|
2023-12-05 20:46:41 +01:00
|
|
|
void sendPreparedResponseToServer();
|
2023-11-04 11:14:40 +01:00
|
|
|
|
2023-12-05 20:46:41 +01:00
|
|
|
void prepareChallenge(Message* message);
|
|
|
|
void completeUnlockRequest(Message* message);
|
2023-08-09 12:55:11 +02:00
|
|
|
};
|