ESP_WifiTest/logs.h

61 lines
1.4 KiB
C++

#ifndef LOG_MODULE_H
#define LOG_MODULE_H
#include <Arduino.h>
#include <esp_partition.h>
#include <Preferences.h>
#define PAYLOAD_SIZE 16
// Используем partition для логов
#define LOG_PARTITION_SUBTYPE 0x70
//#define META_PRINT
//#define FLASH_PRINT
//#define LOGGED_PRINT
// Размер партиции для логов - строго 2 МБ
#define LOG_PARTITION_SIZE 0x200000 // 2 MB = 2097152 bytes
struct LogEntry {
uint32_t seq;
uint64_t ts;
uint8_t event_type;
uint8_t crc;
char payload[PAYLOAD_SIZE];
uint8_t reserved[2];
} __attribute__((packed));
#define ENTRY_SIZE sizeof(LogEntry)
class LogModule {
public:
LogModule();
bool begin();
bool writeLog(const LogEntry &entry);
void dumpLogs();
void clearLogs();
void handleUART(char cmd, char dumpCmd = 'd', char clearCmd = 'c');
private:
const esp_partition_t* partition;
uint32_t write_pos;
uint32_t total_entries;
uint32_t partition_size;
Preferences prefs;
uint8_t calculateCRC(const LogEntry &entry);
bool verifyEntry(const LogEntry &entry);
void recoverLog();
uint32_t findLastValidEntry();
bool validateAllEntries();
bool readRaw(uint32_t offset, uint8_t* data, size_t size);
bool writeRaw(uint32_t offset, const uint8_t* data, size_t size);
void saveMetadata();
bool loadMetadata();
};
#endif