From e80db6185999c62496bfc624be4bbc9b1da08e37 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 3 Nov 2019 15:55:35 +0100 Subject: [PATCH] Implemented packet logging --- device.h | 12 ++++- hardware/Crypto.c | 3 +- hardware/Crypto.h | 5 -- protocol/KISS.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++ protocol/KISS.h | 4 ++ 5 files changed, 134 insertions(+), 7 deletions(-) diff --git a/device.h b/device.h index 7d1fda9..ab67164 100755 --- a/device.h +++ b/device.h @@ -5,7 +5,7 @@ // Version info #define MAJ_VERSION 0x01 -#define MIN_VERSION 0x00 +#define MIN_VERSION 0x01 // CPU settings #define TARGET_CPU m1284p @@ -99,4 +99,14 @@ #define USR_IO_4 4 #endif +// File system paths +#define PATH_BASE "OpenModem" +#define PATH_LOG "OpenModem/Log" +#define PATH_LOG_INDEX "OpenModem/Log/packet.index" + +#define PATH_ENTROPY_INDEX "OpenModem/entropy.index" +#define PATH_ENTROPY_SOURCE "OpenModem/entropy.source" +#define PATH_AES_128_KEY "OpenModem/aes128.key" +#define PATH_CRYPTO_DISABLE "OpenModem/aes128.disable" + #endif \ No newline at end of file diff --git a/hardware/Crypto.c b/hardware/Crypto.c index 8f85462..9ea1ca7 100644 --- a/hardware/Crypto.c +++ b/hardware/Crypto.c @@ -152,7 +152,8 @@ bool update_entropy_index(void) { UINT written = 0; crypto_fr = f_write(&crypto_fp, crypto_fb, sizeof(entropy_index), &written); - + f_close(&crypto_fp); + if (crypto_fr == FR_OK && written == sizeof(entropy_index)) { return true; } diff --git a/hardware/Crypto.h b/hardware/Crypto.h index e819b30..ed9036f 100644 --- a/hardware/Crypto.h +++ b/hardware/Crypto.h @@ -10,11 +10,6 @@ #include "hardware/LED.h" #include "hardware/SD.h" -#define PATH_ENTROPY_INDEX "OpenModem/entropy.index" -#define PATH_ENTROPY_SOURCE "OpenModem/entropy.source" -#define PATH_AES_128_KEY "OpenModem/aes128.key" -#define PATH_CRYPTO_DISABLE "OpenModem/aes128.disable" - #define CRYPTO_KEY_SIZE_BITS 128 #define CRYPTO_KEY_SIZE (CRYPTO_KEY_SIZE_BITS/8) #define CRYPTO_HMAC_SIZE_BITS 128 diff --git a/protocol/KISS.c b/protocol/KISS.c index ec2dcc6..e3864a5 100755 --- a/protocol/KISS.c +++ b/protocol/KISS.c @@ -36,6 +36,14 @@ bool ESCAPE; uint8_t command = CMD_UNKNOWN; +bool log_ready = false; +uint32_t log_index = 0; +FIL log_fp; +char log_fb[4]; +char log_filename[32]; +FRESULT log_fr; +FILINFO log_fi; + void kiss_init(AX25Ctx *ax25, Afsk *afsk, Serial *ser) { ax25ctx = ax25; serial = ser; @@ -125,6 +133,21 @@ void kiss_messageCallback(AX25Ctx *ctx) { } if (integrity_ok) { + bool log_write_ok = false; + if (config_log_packets && sd_mounted()) { + if (log_ready || log_init()) { + // TODO: Assertion here on max path length + memset(log_filename, 0x00, sizeof(log_filename)); + snprintf(log_filename, sizeof(log_filename), "%s/%lu.pkt", PATH_LOG, log_index); + + // Open file descriptor + log_fr = f_open(&log_fp, log_filename, FA_CREATE_NEW | FA_WRITE); + if (log_fr == FR_OK) { + log_write_ok = true; + } + } + } + fputc(FEND, &serial->uart0); fputc(0x00, &serial->uart0); for (unsigned i = 0; i < ctx->frame_len-2; i++) { @@ -138,12 +161,106 @@ void kiss_messageCallback(AX25Ctx *ctx) { } else { fputc(b, &serial->uart0); } + if (log_write_ok) { + UINT written = 0; + log_fr = f_write(&log_fp, &b, 1, &written); + } } fputc(FEND, &serial->uart0); + + if (config_log_packets && sd_mounted() && log_ready) { + f_close(&log_fp); + update_log_index(); + } } #endif } +bool log_init(void) { + if (sd_mounted()) { + // Check that base dir exists + log_fr = f_stat(PATH_BASE, &log_fi); + if (log_fr == FR_NO_PATH || log_fr == FR_NO_FILE) { + log_fr = f_mkdir(PATH_BASE); + if (log_fr != FR_OK) { + return false; + } else { + } + } + + // Check that log dir exists + log_fr = f_stat(PATH_LOG, &log_fi); + if (log_fr == FR_NO_PATH || log_fr == FR_NO_FILE) { + log_fr = f_mkdir(PATH_LOG); + if (log_fr != FR_OK) { + return false; + } + } + + if (load_log_index()) { + log_ready = true; + return true; + } else { + return false; + } + + } else { + return false; + } +} + +bool load_log_index(void) { + if (sd_mounted()) { + log_fr = f_open(&log_fp, PATH_LOG_INDEX, FA_READ); + if (log_fr == FR_NO_FILE) { + f_close(&log_fp); + log_fr = f_open(&log_fp, PATH_LOG_INDEX, FA_CREATE_NEW | FA_WRITE); + + if (log_fr == FR_OK) { + log_index = 0x00000000; + memcpy(log_fb, &log_index, sizeof(log_index)); + + UINT written = 0; + log_fr = f_write(&log_fp, log_fb, sizeof(log_index), &written); + f_close(&log_fp); + } + + log_fr = f_open(&log_fp, PATH_LOG_INDEX, FA_READ); + } + + if (log_fr == FR_OK) { + UINT read = 0; + log_fr = f_read(&log_fp, log_fb, sizeof(log_index), &read); + f_close(&log_fp); + if (log_fr == FR_OK && read == sizeof(log_index)) { + memcpy(&log_index, log_fb, sizeof(log_index)); + return true; + } + } + } + + f_close(&log_fp); + return false; +} + +bool update_log_index(void) { + log_fr = f_open(&log_fp, PATH_LOG_INDEX, FA_WRITE); + if (log_fr == FR_OK) { + log_index++; + memcpy(log_fb, &log_index, sizeof(log_index)); + + UINT written = 0; + log_fr = f_write(&log_fp, log_fb, sizeof(log_index), &written); + + f_close(&log_fp); + + if (log_fr == FR_OK && written == sizeof(log_index)) { + return true; + } + } + return false; +} + void kiss_csma(void) { if (queue_height > 0) { #if BITRATE == 2400 diff --git a/protocol/KISS.h b/protocol/KISS.h index 9f2dcc2..c65071c 100755 --- a/protocol/KISS.h +++ b/protocol/KISS.h @@ -48,4 +48,8 @@ void kiss_output_modem_mode(void); void kiss_output_config(uint8_t* data, size_t length); void kiss_output_afsk_peak(void); +bool log_init(void); +bool load_log_index(void); +bool update_log_index(void); + #endif \ No newline at end of file