Sesame-Device/include/fresh.h

80 lines
1.8 KiB
C
Raw Normal View History

#pragma once
#include <stdint.h>
2023-08-09 15:02:24 +02:00
struct TimeConfiguration {
2023-08-09 13:25:19 +02:00
2023-08-09 15:02:24 +02:00
/**
* @brief The timezone offset in seconds
*/
int32_t offsetToGMT;
2023-08-09 13:25:19 +02:00
/**
2023-08-09 15:02:24 +02:00
* @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)
2023-08-09 13:25:19 +02:00
*
* A stricter (lower) value better prevents against replay attacks,
* but may lead to issues when dealing with slow networks and other
* routing delays.
*/
2023-08-09 15:02:24 +02:00
uint32_t allowedTimeOffset;
};
class TimeCheck {
public:
2023-08-09 13:25:19 +02:00
/**
2023-08-09 15:02:24 +02:00
* @brief Create a time checker instance
2023-08-09 13:25:19 +02:00
*/
2023-08-09 15:02:24 +02:00
TimeCheck();
/**
* @brief Set the configuration
*/
void configure(TimeConfiguration configuration);
/**
* @brief Configure the NTP server to get the current time
*/
void startNTP();
2023-08-09 13:25:19 +02:00
/**
* @brief Print the current time to the serial output
*
* The time must be initialized by calling `configureNTP()` before use.
*/
void printLocalTime();
/**
* Gets the current epoch time
*/
uint32_t getEpochTime();
/**
* @brief Check wether the time of a message is within the allowed bounds regarding freshness.
*
* The timestamp is used to ensure 'freshness' of the messages,
* i.e. that they are not unreasonably delayed or captured and
* later replayed by an attacker.
*
* @param messageTime The timestamp of the message (seconds since epoch)
* @return true The time is within the acceptable offset of the local time
* @return false The message time is invalid
*/
bool isMessageTimeAcceptable(uint32_t messageTime);
private:
2023-08-09 15:02:24 +02:00
TimeConfiguration config;
2023-08-09 13:25:19 +02:00
};