Introduce controller, refactoring
This commit is contained in:
42
include/controller.h
Normal file
42
include/controller.h
Normal file
@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include "server.h"
|
||||
#include "servo.h"
|
||||
#include "message.h"
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
class SesameController: public ServerConnectionCallbacks {
|
||||
|
||||
public:
|
||||
SesameController(ServerConnection* server, ServoController* servo, AsyncWebServer* local);
|
||||
|
||||
private:
|
||||
|
||||
ServerConnection* server;
|
||||
ServoController* servo;
|
||||
AsyncWebServer* local;
|
||||
|
||||
// The buffer to hold a received message while it is read
|
||||
uint8_t receivedMessageBuffer[AUTHENTICATED_MESSAGE_SIZE];
|
||||
|
||||
// The buffer to hold a response while it is sent
|
||||
uint8_t responseBuffer[AUTHENTICATED_MESSAGE_SIZE+1];
|
||||
SesameEvent* responseStatus;
|
||||
AuthenticatedMessage* responseMessage;
|
||||
uint16_t responseSize = 0;
|
||||
|
||||
void handleLocalMessage(AsyncWebServerRequest *request);
|
||||
// Based on https://stackoverflow.com/a/23898449/266720
|
||||
bool convertHexMessageToBinary(const char* str);
|
||||
|
||||
void handleServerMessage(uint8_t* payload, size_t length);
|
||||
void sendServerError(SesameEvent event);
|
||||
|
||||
void processMessage(AuthenticatedMessage* message);
|
||||
SesameEvent verifyAndProcessReceivedMessage(AuthenticatedMessage* message);
|
||||
|
||||
uint16_t prepareResponseBuffer(SesameEvent event, uint8_t deviceId = 0);
|
||||
void sendPreparedLocalResponse(AsyncWebServerRequest *request);
|
||||
void sendLocalResponse(AsyncWebServerRequest *request, SesameEvent event, uint8_t deviceId = 0);
|
||||
void sendPreparedServerResponse();
|
||||
};
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* @brief The size of a message authentication code
|
||||
@ -87,9 +88,9 @@ typedef struct {
|
||||
} AuthenticatedMessage;
|
||||
#pragma pack(pop)
|
||||
|
||||
#define MESSAGE_CONTENT_SIZE sizeof(Message)
|
||||
constexpr int MESSAGE_CONTENT_SIZE = sizeof(Message);
|
||||
|
||||
#define AUTHENTICATED_MESSAGE_SIZE sizeof(AuthenticatedMessage)
|
||||
constexpr int AUTHENTICATED_MESSAGE_SIZE = sizeof(AuthenticatedMessage);
|
||||
|
||||
/**
|
||||
* An event signaled from the device
|
||||
@ -97,19 +98,27 @@ typedef struct {
|
||||
enum class SesameEvent {
|
||||
TextReceived = 1,
|
||||
UnexpectedSocketEvent = 2,
|
||||
InvalidMessageData = 3,
|
||||
InvalidMessageSize = 3,
|
||||
MessageAuthenticationFailed = 4,
|
||||
MessageTimeMismatch = 5,
|
||||
MessageCounterInvalid = 6,
|
||||
MessageAccepted = 7,
|
||||
MessageDeviceInvalid = 8,
|
||||
InvalidUrlParameter = 9,
|
||||
InvalidResponseAuthentication = 10,
|
||||
DeviceSetupIncomplete = 11,
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief A callback for messages received over the socket
|
||||
*
|
||||
* The first parameter is the received message.
|
||||
* The second parameter is the response to the remote.
|
||||
* The return value is the type of event to respond with.
|
||||
* The first parameter is a pointer to the byte buffer.
|
||||
* The second parameter indicates the number of received bytes.
|
||||
*/
|
||||
typedef SesameEvent (*MessageCallback)(AuthenticatedMessage*, AuthenticatedMessage*);
|
||||
typedef void (*MessageCallback)(uint8_t* payload, size_t length);
|
||||
|
||||
/**
|
||||
* @brief A callback for socket errors
|
||||
*/
|
||||
typedef void (*ErrorCallback)(SesameEvent event);
|
||||
|
||||
|
@ -6,6 +6,14 @@
|
||||
#include <WiFiClientSecure.h>
|
||||
#include <WebSocketsClient.h>
|
||||
|
||||
class ServerConnectionCallbacks {
|
||||
|
||||
public:
|
||||
|
||||
virtual void sendServerError(SesameEvent event) = 0;
|
||||
virtual void handleServerMessage(uint8_t* payload, size_t length) = 0;
|
||||
};
|
||||
|
||||
class ServerConnection {
|
||||
|
||||
public:
|
||||
@ -14,14 +22,26 @@ public:
|
||||
|
||||
void connect(const char* key, uint32_t reconnectTime = 5000);
|
||||
|
||||
void connectSSL(const char* key, uint32_t reconnectTime = 5000);
|
||||
|
||||
void loop();
|
||||
|
||||
void onMessage(MessageCallback callback);
|
||||
/**
|
||||
* @brief Set the handler
|
||||
*
|
||||
* @param callback The handler to handle messages and errors
|
||||
*/
|
||||
void setCallbackHandler(ServerConnectionCallbacks* callbacks);
|
||||
|
||||
// Indicator that the socket is connected.
|
||||
bool socketIsConnected = false;
|
||||
/**
|
||||
* @brief Send a response message over the socket
|
||||
*
|
||||
* @param buffer The data buffer
|
||||
* @param length The number of bytes to send
|
||||
*/
|
||||
void sendResponse(uint8_t* buffer, uint16_t length);
|
||||
|
||||
bool isSocketConnected() {
|
||||
return socketIsConnected;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@ -33,15 +53,14 @@ private:
|
||||
|
||||
const char* key = NULL;
|
||||
|
||||
MessageCallback messageCallback = NULL;
|
||||
// Indicator that the socket is connected.
|
||||
bool socketIsConnected = false;
|
||||
|
||||
ServerConnectionCallbacks* controller = NULL;
|
||||
|
||||
// WebSocket to connect to the control server
|
||||
WebSocketsClient webSocket;
|
||||
|
||||
void reconnectAfter(uint32_t reconnectTime);
|
||||
|
||||
void registerEventCallback();
|
||||
|
||||
/**
|
||||
* Callback for WebSocket events.
|
||||
*
|
||||
@ -51,35 +70,4 @@ private:
|
||||
* @param length The number of bytes received
|
||||
*/
|
||||
void webSocketEventHandler(WStype_t type, uint8_t * payload, size_t length);
|
||||
|
||||
/**
|
||||
* Process received binary data.
|
||||
*
|
||||
* Checks whether the received data is a valid and unused key,
|
||||
* and then signals that the motor should move.
|
||||
* Sends the event id to the server as a response to the request.
|
||||
*
|
||||
* If the key is valid, then `shouldStartOpening` is set to true.
|
||||
*
|
||||
* @param payload The pointer to the received data.
|
||||
* @param length The number of bytes received.
|
||||
*/
|
||||
void processReceivedBytes(uint8_t* payload, size_t length);
|
||||
|
||||
/**
|
||||
* Send a response event to the server and include the next key index.
|
||||
*
|
||||
* Sends the event type as three byte.
|
||||
* @param event The event type
|
||||
*/
|
||||
void sendFailureResponse(SesameEvent event);
|
||||
|
||||
/**
|
||||
* Send a response event to the server and include the next key index.
|
||||
*
|
||||
* Sends the event type as three byte.
|
||||
* @param event The event type
|
||||
*/
|
||||
void sendResponse(SesameEvent event, AuthenticatedMessage* message);
|
||||
|
||||
};
|
Reference in New Issue
Block a user