Sesame-Device/include/controller.h

198 lines
4.9 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
2024-04-22 12:58:18 +02:00
enum class SesameDeviceStatus {
/**
* @brief The initial state of the device after boot
*/
initial,
/**
* @brief The device has configured the individual parts,
* but has no the ethernet hardware detected.
*/
configuredButNoEthernetHardware,
/**
* @brief The device has ethernet hardware, but no ethernet link
*/
ethernetHardwareButNoLink,
/**
* @brief The device has an ethernet link, but no IP address.
*/
ethernetLinkButNoIP,
/**
* @brief The device has an IP address, but no socket connection
*/
ipAddressButNoSocketConnection,
};
2023-08-09 12:55:11 +02:00
class SesameController: public ServerConnectionCallbacks {
public:
2024-04-22 12:58:18 +02:00
SesameController(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:
2024-04-22 12:58:18 +02:00
SesameDeviceStatus status = SesameDeviceStatus::initial;
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
// An EthernetUDP instance to send and receive packets over UDP
2024-04-20 21:00:09 +02:00
EthernetUDP udp;
2024-04-22 12:58:18 +02:00
EthernetHardwareStatus ethernetStatus;
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;
2024-04-22 12:58:18 +02:00
// MARK: Ethernet
void initializeSpiBusForEthernetModule();
/**
* @brief Checks to ensure that Ethernet hardware is available
*
* @return true The hardware is available
* @return false The hardware is missing
*/
bool hasAvailableEthernetHardware();
/**
* @brief Check that an active ethernet link is available
*
* @return true Link is available
* @return false Link is absent
*/
bool hasEthernetLink();
void configureEthernet();
void startUDP();
void stopUDP();
2023-12-05 20:46:41 +01:00
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
};