Sesame-Device/include/controller.h

79 lines
2.2 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"
2023-08-09 13:13:38 +02:00
#include "storage.h"
2023-08-09 13:25:19 +02:00
#include "fresh.h"
2023-08-09 12:55:11 +02:00
#include <ESPAsyncWebServer.h>
2023-08-09 15:02:24 +02:00
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;
};
2023-08-09 12:55:11 +02:00
class SesameController: public ServerConnectionCallbacks {
public:
2023-08-09 15:02:24 +02:00
SesameController(uint16_t localWebServerPort, uint8_t remoteDeviceCount);
void configure(ServoConfiguration servoConfig, ServerConfiguration serverConfig, TimeConfiguration timeConfig, WifiConfiguration wifiConfig, 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-08-09 15:02:24 +02:00
ServerConnection server;
ServoController servo;
AsyncWebServer localWebServer;
TimeCheck timeCheck;
2023-08-09 13:13:38 +02:00
Storage storage;
2023-08-09 12:55:11 +02:00
2023-08-09 15:02:24 +02:00
WifiConfiguration wifiConfig;
KeyConfiguration keyConfig;
bool isReconnecting = false;
2023-08-09 12:55:11 +02:00
// 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;
2023-08-09 15:02:24 +02:00
void ensureWiFiConnection(uint32_t time);
void ensureWebSocketConnection();
2023-08-09 12:55:11 +02:00
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);
2023-08-09 17:13:03 +02:00
void prepareResponseBuffer(SesameEvent event, uint8_t deviceId = 0);
2023-08-09 12:55:11 +02:00
void sendPreparedLocalResponse(AsyncWebServerRequest *request);
void sendPreparedServerResponse();
};