Improve configuration, refactoring

This commit is contained in:
Christoph Hagen
2023-08-09 15:02:24 +02:00
parent b99245085e
commit e84e388521
10 changed files with 235 additions and 165 deletions

View File

@ -7,21 +7,50 @@
#include "fresh.h"
#include <ESPAsyncWebServer.h>
struct WifiConfiguration {
// The WiFi network to connect to
const char* ssid;
// The WiFi password to connect to the above network
const char* password;
// The name of the device on the network
const char* networkName;
// The interval to reconnect to WiFi if the connection is broken
uint32_t reconnectInterval;
};
struct KeyConfiguration {
const uint8_t* remoteKey;
const uint8_t* localKey;
};
class SesameController: public ServerConnectionCallbacks {
public:
SesameController(ServerConnection* server, ServoController* servo, AsyncWebServer* local, TimeCheck* timeCheck, uint8_t remoteDeviceCount);
SesameController(uint16_t localWebServerPort, uint8_t remoteDeviceCount);
void configure();
void configure(ServoConfiguration servoConfig, ServerConfiguration serverConfig, TimeConfiguration timeConfig, WifiConfiguration wifiConfig, KeyConfiguration keyConfig);
void loop(uint32_t millis);
private:
ServerConnection* server;
ServoController* servo;
AsyncWebServer* local;
TimeCheck* timeCheck;
ServerConnection server;
ServoController servo;
AsyncWebServer localWebServer;
TimeCheck timeCheck;
Storage storage;
WifiConfiguration wifiConfig;
KeyConfiguration keyConfig;
bool isReconnecting = false;
// The buffer to hold a received message while it is read
uint8_t receivedMessageBuffer[AUTHENTICATED_MESSAGE_SIZE];
@ -31,6 +60,9 @@ private:
AuthenticatedMessage* responseMessage;
uint16_t responseSize = 0;
void ensureWiFiConnection(uint32_t time);
void ensureWebSocketConnection();
void handleLocalMessage(AsyncWebServerRequest *request);
// Based on https://stackoverflow.com/a/23898449/266720
bool convertHexMessageToBinary(const char* str);

View File

@ -52,11 +52,7 @@ constexpr uint32_t wifiReconnectInterval = 10000;
/* Local server */
// The port for the local server to directly receive messages over WiFi
constexpr uint16_t localPort = 80;
// The url parameter to send the message to the local server
constexpr char messageUrlParameter[] = "m";
constexpr uint16_t localWebServerPort = 80;
/* Server */

View File

@ -1,7 +1,34 @@
#pragma once
#include <stdint.h>
#include "config.h"
struct TimeConfiguration {
/**
* @brief The timezone offset in seconds
*/
int32_t offsetToGMT;
/**
* @brief The daylight savings offset in seconds
*/
int32_t offsetDaylightSavings;
/**
* @brief The url of the NTP server
*/
const char* ntpServerUrl;
/**
* @brief The allowed discrepancy between the time of a received message
* and the device time (in seconds)
*
* A stricter (lower) value better prevents against replay attacks,
* but may lead to issues when dealing with slow networks and other
* routing delays.
*/
uint32_t allowedTimeOffset;
};
class TimeCheck {
@ -9,26 +36,18 @@ public:
/**
* @brief Create a time checker instance
*
* Specify the allowed discrepancy between the time of a received message
* and the device time (in seconds).
*
* A stricter (lower) value better prevents against replay attacks,
* but may lead to issues when dealing with slow networks and other
* routing delays.
*
* @param offset The allowed time discrepancy in both directions (seconds)
*/
TimeCheck(uint32_t allowedTimeOffset = 60);
TimeCheck();
/**
* @brief Configure an NTP server to get the current time
*
* @param offsetToGMT The timezone offset in seconds
* @param offsetDaylightSavings The daylight savings offset in seconds
* @param serverUrl The url of the NTP server
* @brief Set the configuration
*/
void configureNTP(int32_t offsetToGMT, int32_t offsetDaylightSavings, const char* serverUrl);
void configure(TimeConfiguration configuration);
/**
* @brief Configure the NTP server to get the current time
*/
void startNTP();
/**
* @brief Print the current time to the serial output
@ -42,20 +61,6 @@ public:
*/
uint32_t getEpochTime();
/**
* @brief The allowed time discrepancy (in seconds)
*
* Specifies the allowed discrepancy between the time of a received message
* and the device time (in seconds).
*
* A stricter (lower) value better prevents against replay attacks,
* but may lead to issues when dealing with slow networks and other
* routing delays.
*
* @param offset The offset in both directions (seconds)
*/
void setMessageTimeAllowedOffset(uint32_t offset);
/**
* @brief Check wether the time of a message is within the allowed bounds regarding freshness.
*
@ -70,14 +75,6 @@ public:
bool isMessageTimeAcceptable(uint32_t messageTime);
private:
/**
* @brief The allowed discrepancy between the time of a received message
* and the device time (in seconds)
*
* A stricter (lower) value better prevents against replay attacks,
* but may lead to issues when dealing with slow networks and other
* routing delays.
*/
uint32_t allowedOffset;
TimeConfiguration config;
};

View File

@ -6,6 +6,32 @@
#include <WiFiClientSecure.h>
#include <WebSocketsClient.h>
struct ServerConfiguration {
/**
* @brief The url of the remote server to connect to
*/
const char* url;
/**
* @brief The server port
*/
int port;
/**
* @brief The path on the server
*/
const char* path;
/**
* @brief The authentication key for the server
*/
const char* key;
uint32_t reconnectTime;
};
class ServerConnectionCallbacks {
public:
@ -18,18 +44,18 @@ class ServerConnection {
public:
ServerConnection(const char* url, int port, const char* path);
void connect(const char* key, uint32_t reconnectTime = 5000);
void loop();
ServerConnection();
/**
* @brief Set the handler
* @brief Set the configuration and the callback handler
*
* @param callback The handler to handle messages and errors
*/
void setCallbackHandler(ServerConnectionCallbacks* callbacks);
void configure(ServerConfiguration configuration, ServerConnectionCallbacks* callbacks);
void connect();
void loop();
/**
* @brief Send a response message over the socket
@ -45,13 +71,7 @@ public:
private:
const char* url;
int port;
const char* path;
const char* key = NULL;
ServerConfiguration configuration;
// Indicator that the socket is connected.
bool socketIsConnected = false;

View File

@ -3,7 +3,7 @@
#include <stdint.h>
#include <ESP32Servo.h> // To control the servo
typedef struct ServoConfiguration {
struct ServoConfiguration {
/**
* @brief The timer to use for the servo control
* number 0-3 indicating which timer to allocate in this library
@ -34,6 +34,7 @@ typedef struct ServoConfiguration {
* @brief The servo value (in µs) that specifies the 'released' state
*/
int releasedValue;
};
/**
@ -70,7 +71,7 @@ public:
* There is no required interval to call this function, but the accuracy of
* the opening interval is dependent on the calling frequency.
*/
void loop();
void loop(uint32_t millis);
/**
* Push the door opener button down by moving the servo arm.