diff --git a/stm-firmware/shell.c b/stm-firmware/shell.c index 5d1a9fe..4b865d7 100644 --- a/stm-firmware/shell.c +++ b/stm-firmware/shell.c @@ -567,8 +567,6 @@ static char *get_safety_mem_dump(size_t *used_bytes) static shellmatta_retCode_t shell_cmd_dump_safety_mem(const shellmatta_handle_t handle, const char *arguments, uint32_t length) { - (void)arguments; - (void)length; static char *buffer; static const char *ptr; size_t used_bytes; @@ -576,6 +574,45 @@ static shellmatta_retCode_t shell_cmd_dump_safety_mem(const shellmatta_handle_t static size_t current_line; size_t remainder; static const char *hline = "----------------------------------------------------------------"; + char string[200]; + const char *token; + const char * const token_delim = "\t "; + FRESULT fres; + FIL file; + UINT bw; + + /* Check if the dump shall be stored to disk */ + strncpy(string, arguments, MIN(sizeof(string), length+1)); + string[sizeof(string) - 1] = '\0'; + + token = strtok(string, token_delim); + token = strtok(NULL, token_delim); + + if (token) { + buffer = get_safety_mem_dump(&used_bytes); + if (!buffer) { + shellmatta_printf(handle, "Error generating dump"); + return SHELLMATTA_OK; + } + fres = f_open(&file, token, FA_CREATE_NEW | FA_WRITE); + if (fres == FR_EXIST) { + free(buffer); + shellmatta_printf(handle, "File already esists!\r\n"); + return SHELLMATTA_OK; + } else if (fres != FR_OK) { + free(buffer); + shellmatta_printf(handle, "Error opening file %s\r\n", token); + return SHELLMATTA_OK; + } + + fres = f_write(&file, buffer, used_bytes - 1, &bw); + if (fres != FR_OK) { + shellmatta_printf(handle, "Error writing to file %s\r\n", token); + } + free(buffer); + f_close(&file); + return SHELLMATTA_OK; + } if (full_lines == 0) { shellmatta_printf(handle, "Safety memory content\r\n%s\r\n", hline); @@ -761,7 +798,7 @@ static shellmatta_cmd_t cmd[18] = { .cmd = "safety-mem-dump", .cmdAlias = NULL, .helpText = "", - .usageText = "", + .usageText = "safety-mem-dump [output-file]", .cmdFct = shell_cmd_dump_safety_mem, .next = NULL, },