Files
2026-02-20 17:50:40 +00:00

91 lines
2.1 KiB
C

// sd_logger.h - SD Card Logger with PCAP format
#ifndef SD_LOGGER_H
#define SD_LOGGER_H
#include <Arduino.h>
#include <SD_MMC.h>
#include <FS.h>
#include "config.h"
#include "types.h"
// SD status
extern bool sdInitialized;
extern uint64_t sdCardSize;
extern uint64_t sdFreeSpace;
// Current log file
extern File currentLogFile;
extern char currentLogFileName[64];
extern uint32_t currentLogFileSize;
// Initialize SD card
bool initSDCard();
// Create log directories
bool createLogDirectories();
// Start new log file
bool startLogFile();
// Close current log file
void closeLogFile();
// Write CAN frame to SD (PCAP format)
bool writeCANFrameToSD(const CanFrame& frame);
// Flush write buffer to SD card
bool flushWriteBuffer();
// SD Write Task (runs on Core 1)
void sdWriteTask(void *pvParameters);
// Get SD card info
uint64_t getSDCardSize();
uint64_t getFreeSpace();
uint64_t getUsedSpace();
// List log files
int listLogFiles(char* buffer, size_t bufferSize);
// Get file info
bool getLogFileInfo(const char* filename, uint32_t& size, uint32_t& timestamp);
// Delete log file
bool deleteLogFile(const char* filename);
// Get current filename
const char* getCurrentLogFilename();
// PCAP file structures
#pragma pack(push, 1)
struct PCAPGlobalHeader {
uint32_t magicNumber; // 0xa1b2c3d4
uint16_t versionMajor; // 2
uint16_t versionMinor; // 4
int32_t thiszone; // GMT to local correction
uint32_t sigfigs; // accuracy of timestamps
uint32_t snaplen; // max length of captured packets
uint32_t network; // data link type (227 = LINKTYPE_CAN_SOCKETCAN)
};
struct PCAPPacketHeader {
uint32_t tsSec; // timestamp seconds
uint32_t tsUsec; // timestamp microseconds
uint32_t inclLen; // number of octets of packet saved in file
uint32_t origLen; // actual length of packet
};
struct CANFDFrame {
uint32_t canId; // CAN ID + flags
uint8_t len; // payload length
uint8_t flags; // FD flags
uint8_t reserved0;
uint8_t reserved1;
uint8_t data[64]; // payload
};
#pragma pack(pop)
#endif // SD_LOGGER_H