Files
Vermix-Gate/lib/data/session.hpp
T
2025-10-06 18:27:50 +02:00

33 lines
987 B
C++

#pragma once
#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h> // Required for mutex
// Represents an active session.
// An empty username (username[0] == '\0') indicates the slot is free.
struct Session {
char username[33];
char token[17]; // 16 chars + null terminator
unsigned long expiry_time; // Expiration timestamp from millis()
};
class SessionManager {
public:
SessionManager();
~SessionManager(); // Destructor to clean up the mutex
const char* createSession(const char* username);
const char* validateSession(const char* token);
bool endSession(const char* token);
void cleanupExpiredSessions();
private:
static constexpr size_t MAX_SESSIONS = 3;
Session sessions_[MAX_SESSIONS];
SemaphoreHandle_t session_mutex_; // Mutex to protect access to sessions_
void generateToken(char* buffer, size_t buffer_size);
};
// Central, global instance.
extern SessionManager sessionManager;