Sesame-Device/include/controller.h

149 lines
3.8 KiB
C
Raw Normal View History

2023-08-09 12:55:11 +02:00
#pragma once
#include "server.h"
#include "servo.h"
#include "message.h"
#include "configurations/EthernetConfiguration.h"
#include "configurations/KeyConfiguration.h"
2023-08-09 15:02:24 +02:00
2023-08-09 12:55:11 +02:00
class SesameController: public ServerConnectionCallbacks {
public:
SesameController();
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;
// 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;
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
void checkLocalMessage();
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.
*/
void processMessage(SignedMessage* message, bool shouldPerformUnlock);
2023-08-09 12:55:11 +02:00
2023-12-08 00:24:15 +01:00
/**
* @brief Checks that the message is valid and prepares a challenge.
*
* This function is also called when a challenge response arrives too late.
*
* @param message The message to respond to
*
* Note: Prepares the response in the outgoing message buffer.
*/
void checkAndPrepareChallenge(Message* message);
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, bool shouldPerformUnlock);
2023-12-05 21:31:11 +01:00
// 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 Read a message from the UDP port
*
*/
bool readLocalMessage();
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
*/
void sendPreparedLocalResponse();
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
};