Implemented packet logging

This commit is contained in:
Mark Qvist 2019-11-03 15:55:35 +01:00
parent 768edd2f5d
commit e80db61859
5 changed files with 134 additions and 7 deletions

View File

@ -5,7 +5,7 @@
// Version info // Version info
#define MAJ_VERSION 0x01 #define MAJ_VERSION 0x01
#define MIN_VERSION 0x00 #define MIN_VERSION 0x01
// CPU settings // CPU settings
#define TARGET_CPU m1284p #define TARGET_CPU m1284p
@ -99,4 +99,14 @@
#define USR_IO_4 4 #define USR_IO_4 4
#endif #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 #endif

View File

@ -152,6 +152,7 @@ bool update_entropy_index(void) {
UINT written = 0; UINT written = 0;
crypto_fr = f_write(&crypto_fp, crypto_fb, sizeof(entropy_index), &written); 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)) { if (crypto_fr == FR_OK && written == sizeof(entropy_index)) {
return true; return true;

View File

@ -10,11 +10,6 @@
#include "hardware/LED.h" #include "hardware/LED.h"
#include "hardware/SD.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_BITS 128
#define CRYPTO_KEY_SIZE (CRYPTO_KEY_SIZE_BITS/8) #define CRYPTO_KEY_SIZE (CRYPTO_KEY_SIZE_BITS/8)
#define CRYPTO_HMAC_SIZE_BITS 128 #define CRYPTO_HMAC_SIZE_BITS 128

View File

@ -36,6 +36,14 @@ bool ESCAPE;
uint8_t command = CMD_UNKNOWN; 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) { void kiss_init(AX25Ctx *ax25, Afsk *afsk, Serial *ser) {
ax25ctx = ax25; ax25ctx = ax25;
serial = ser; serial = ser;
@ -125,6 +133,21 @@ void kiss_messageCallback(AX25Ctx *ctx) {
} }
if (integrity_ok) { 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(FEND, &serial->uart0);
fputc(0x00, &serial->uart0); fputc(0x00, &serial->uart0);
for (unsigned i = 0; i < ctx->frame_len-2; i++) { for (unsigned i = 0; i < ctx->frame_len-2; i++) {
@ -138,12 +161,106 @@ void kiss_messageCallback(AX25Ctx *ctx) {
} else { } else {
fputc(b, &serial->uart0); fputc(b, &serial->uart0);
} }
if (log_write_ok) {
UINT written = 0;
log_fr = f_write(&log_fp, &b, 1, &written);
}
} }
fputc(FEND, &serial->uart0); fputc(FEND, &serial->uart0);
if (config_log_packets && sd_mounted() && log_ready) {
f_close(&log_fp);
update_log_index();
}
} }
#endif #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) { void kiss_csma(void) {
if (queue_height > 0) { if (queue_height > 0) {
#if BITRATE == 2400 #if BITRATE == 2400

View File

@ -48,4 +48,8 @@ void kiss_output_modem_mode(void);
void kiss_output_config(uint8_t* data, size_t length); void kiss_output_config(uint8_t* data, size_t length);
void kiss_output_afsk_peak(void); void kiss_output_afsk_peak(void);
bool log_init(void);
bool load_log_index(void);
bool update_log_index(void);
#endif #endif