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];
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:31:11 +01:00
|
|
|
// MARK: Local client callbacks
|
2023-08-09 15:02:24 +02:00
|
|
|
|
2023-08-09 12:55:11 +02:00
|
|
|
void handleLocalMessage(AsyncWebServerRequest *request);
|
2023-12-05 20:46:41 +01:00
|
|
|
|
2023-12-05 21:31:11 +01:00
|
|
|
// MARK: Socket Callbacks
|
2023-08-09 12:55:11 +02:00
|
|
|
|
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);
|
|
|
|
|
2023-12-05 21:31:11 +01:00
|
|
|
void handleServerMessage(uint8_t* payload, size_t length);
|
|
|
|
|
|
|
|
// MARK: Message processing
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Process a received message (local or socket).
|
|
|
|
*
|
|
|
|
* @param message The message to process.
|
|
|
|
*
|
|
|
|
* Note: Prepares the response in the outgoing message buffer.
|
|
|
|
*/
|
2023-12-05 20:46:41 +01:00
|
|
|
void processMessage(SignedMessage* message);
|
2023-08-09 12:55:11 +02:00
|
|
|
|
2023-12-05 21:31:11 +01:00
|
|
|
/**
|
|
|
|
* @brief Prepare a server challenge for a local or socket message.
|
|
|
|
*
|
|
|
|
* @param message The message to respond to
|
|
|
|
*
|
|
|
|
* Note: Prepares the response in the outgoing message buffer.
|
|
|
|
*/
|
|
|
|
void prepareChallenge(Message* message);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Complete an unlock request for a local or socket message.
|
|
|
|
*
|
|
|
|
* @param message The message to respond to
|
|
|
|
*
|
|
|
|
* Note: Prepares the response in the outgoing message buffer.
|
|
|
|
*/
|
|
|
|
void completeUnlockRequest(Message* message);
|
|
|
|
|
|
|
|
// MARK: Responses
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Prepare the outgoing message buffer for both socket and local responses.
|
|
|
|
*
|
|
|
|
* @param event The resulting state to transmit
|
|
|
|
* @param message An optional message to echo
|
|
|
|
*/
|
2023-12-05 20:46:41 +01:00
|
|
|
void prepareResponseBuffer(MessageResult event, Message* message = NULL);
|
2023-12-05 21:31:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send the prepared outgoing message to a locally connected client
|
|
|
|
*
|
|
|
|
* @param request The original request of the client
|
|
|
|
*/
|
2023-08-09 12:55:11 +02:00
|
|
|
void sendPreparedLocalResponse(AsyncWebServerRequest *request);
|
2023-12-05 21:31:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Send the prepared outgoing message to the server
|
|
|
|
*/
|
2023-12-05 20:46:41 +01:00
|
|
|
void sendPreparedResponseToServer();
|
2023-11-04 11:14:40 +01:00
|
|
|
|
2023-12-05 21:31:11 +01:00
|
|
|
// MARK: Helper
|
|
|
|
|
|
|
|
bool convertHexMessageToBinary(const char* str);
|
2023-08-09 12:55:11 +02:00
|
|
|
};
|