New state machine, local UDP route

This commit is contained in:
Christoph Hagen
2024-04-22 12:58:18 +02:00
parent 3afe8b16a9
commit da22f8a37c
7 changed files with 289 additions and 145 deletions

View File

@@ -6,17 +6,46 @@
#include "configurations/EthernetConfiguration.h"
#include "configurations/KeyConfiguration.h"
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,
};
class SesameController: public ServerConnectionCallbacks {
public:
SesameController();
void configure(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig);
SesameController(ServoConfiguration servoConfig, ServerConfiguration serverConfig, EthernetConfiguration ethernetConfig, KeyConfiguration keyConfig);
void loop(uint32_t millis);
private:
SesameDeviceStatus status = SesameDeviceStatus::initial;
uint32_t currentTime = 0;
ServerConnection server;
@@ -26,7 +55,7 @@ private:
// An EthernetUDP instance to send and receive packets over UDP
EthernetUDP udp;
EthernetHardwareStatus ethernetStatus;
EthernetConfiguration ethernetConfig;
bool ethernetIsConfigured = false;
@@ -42,6 +71,31 @@ private:
uint32_t currentServerChallenge;
SignedMessage outgoingMessage;
// 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();
bool hasCurrentChallenge() {
return currentChallengeExpiry > currentTime;

View File

@@ -46,14 +46,16 @@ class ServerConnection {
public:
ServerConnection();
ServerConnection(ServerConfiguration configuration);
/**
* @brief Set the configuration and the callback handler
*
* @param callback The handler to handle messages and errors
*/
void configure(ServerConfiguration configuration, ServerConnectionCallbacks* callbacks);
void setCallbacks(ServerConnectionCallbacks* callbacks);
void shouldConnect(bool connect);
/**
* @brief Call this function regularly to handle socket operations.
@@ -73,7 +75,8 @@ public:
private:
uint32_t currentTime;
uint32_t currentTime = 0;
bool shouldBeConnected = false;
bool socketIsConnected() {
return webSocket.isConnected();
@@ -81,15 +84,12 @@ private:
void connect();
void disconnect();
bool shouldReconnect = true;
bool isConnecting = false;
bool isDisconnecting = false;
uint32_t connectionTimeout = 0;
uint32_t nextReconnectAttemptMs = 0;
void didDisconnect();
void didConnect();
void didChangeConnectionState(bool isConnected);
ServerConfiguration configuration;

View File

@@ -52,15 +52,10 @@ public:
/**
* @brief Construct a new servo controller
*/
ServoController();
/**
* @brief Configure the servo
*
* @param The configuration for the servo
*/
void configure(ServoConfiguration configuration);
ServoController(ServoConfiguration configuration);
/**
* @brief Update the servo state periodically