Skip to content

Commit

Permalink
Improved robustness
Browse files Browse the repository at this point in the history
Enhanced code documentation
Fixed indexed text processing bug
  • Loading branch information
RTEdbg committed Feb 16, 2025
1 parent 6fc65f8 commit d27fc31
Show file tree
Hide file tree
Showing 45 changed files with 368 additions and 211 deletions.
10 changes: 8 additions & 2 deletions Code/cmd_line.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -83,7 +83,7 @@ void check_timestamp_diff_values(void)
{
if (g_msg.param.max_negative_tstamp_diff_f == 0.0)
{
return; // No -ts command line argument
return; // No -ts command-line argument was found.
}

double frequency =
Expand Down Expand Up @@ -370,6 +370,12 @@ static void process_parameter_file(char *file_name)
}
}

if (ferror(par_file))
{
report_error_and_exit(get_message_text(FATAL_READ_FROM_CMD_LINE_PARAM_FILE),
EXIT_FATAL_FMT_PARSING_ERRORS);
}

fclose(par_file);
}

Expand Down
2 changes: 1 addition & 1 deletion Code/cmd_line.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down
8 changes: 6 additions & 2 deletions Code/decoder.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -87,7 +87,10 @@ static void print_message_type_and_date(uint32_t msg_index)

static void process_streaming_mode_messages(void)
{
g_msg.message_cnt--; // Do not count internal messages
if (g_msg.message_cnt > 0)
{
g_msg.message_cnt--; // Do not count internal messages
}

// Check if the current message has the correct type and length
// and reset the statistics if a new snapshot or single-shot logging code is detected
Expand Down Expand Up @@ -413,6 +416,7 @@ static char *convert_octal_number(char *message, size_t *chars_processed, unsign
{
message++;
result = (result * 8u) + data - '0';
// If the user provides a value that is too large, it could result in an overflow.
(*chars_processed)++;
data = *message;

Expand Down
2 changes: 1 addition & 1 deletion Code/decoder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down
12 changes: 6 additions & 6 deletions Code/errors.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -97,9 +97,9 @@ __declspec(noreturn) void report_error_and_show_instructions(const char *error_m
__declspec(noreturn) void report_fatal_error_and_exit(uint32_t error_code,
const char *additional_text, size_t additional_data)
{
if (error_code > TOTAL_ERRORS)
if ((error_code >= TOTAL_ERRORS) || (error_code < FIRST_FATAL_ERROR))
{
error_code = TOTAL_ERRORS;
error_code = FATAL_LAST; // Unknown error
}

if (additional_text == NULL)
Expand Down Expand Up @@ -340,9 +340,9 @@ static void report_problem_worker(FILE *out, uint32_t error_code, int additional

void report_problem(uint32_t error_code, int additional_data)
{
if (error_code >= TOTAL_ERRORS)
if ((error_code >= TOTAL_ERRORS) || (error_code < FIRST_FATAL_ERROR))
{
error_code = FATAL_LAST;
error_code = FATAL_LAST; // Unknown error
}

if (g_msg.file.error_log != NULL)
Expand Down Expand Up @@ -389,7 +389,7 @@ void report_decode_error_summary(void)
{
if (g_msg.error_counter[i] > 0)
{
fprintf(g_msg.file.error_log, "\n");
fputc('\n', g_msg.file.error_log);
fprintf(g_msg.file.error_log,
get_message_text(MSG_ERROR_COUNTER),
g_msg.error_counter[i],
Expand Down
4 changes: 2 additions & 2 deletions Code/errors.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand All @@ -17,7 +17,7 @@
#include "main.h"

// Exit codes for various error scenarios
#define EXIT_FATAL_FMT_PARSING_ERRORS 1u // Errors during format definition processing
#define EXIT_FATAL_FMT_PARSING_ERRORS 1u // Errors during command file or format definition processing
#define EXIT_FATAL_DECODING_ERRORS_DETECTED 2u // Fatal errors during binary file processing
#define EXIT_NON_FATAL_DECODING_ERRORS_DETECTED 3u // Non-fatal errors during binary file processing
#define EXIT_FAST_FAIL_INCORRECT_STACK 4u // Incorrect program stack (stack space exhausted)
Expand Down
11 changes: 7 additions & 4 deletions Code/files.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -167,7 +167,10 @@ void create_main_log_file(void)
}

#if defined STREAM_BUFF_SIZE
// Allocate a buffer for the stream; a larger buffer may not significantly increase write speed
/* Allocate a buffer for the stream; a larger buffer may not significantly increase write speed
* The setvbuf function optimizes I/O performance by controlling stream buffering, reducing
* frequent operations and improving efficiency.
*/
char *stream_buffer = allocate_memory(STREAM_BUFF_SIZE, "strBuff");
int rez = setvbuf(g_msg.file.main_log, stream_buffer, _IOFBF, STREAM_BUFF_SIZE);
#endif
Expand Down Expand Up @@ -268,9 +271,9 @@ char *prepare_folder_name(char *name, unsigned error_code)
len--;
}

if (name[len] == '"')
if (name[len - 1u] == '"')
{
name[len] = '\0';
name[len - 1u] = '\0';
len--;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Code/files.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down
26 changes: 18 additions & 8 deletions Code/format.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -49,7 +49,7 @@ unsigned assign_fmt_id(unsigned no_fmt_ids, msg_data_t *p_msg_data)
{
if (no_fmt_ids == 0)
{
return 0xFFFFFFFF;
return 0xFFFFFFFF; // Error
}

// Locate the first unassigned format ID and update the index to skip already assigned IDs.
Expand Down Expand Up @@ -92,7 +92,17 @@ unsigned assign_fmt_id(unsigned no_fmt_ids, msg_data_t *p_msg_data)

if (fmt_id_assigned)
{
uint32_t new_limit = fmt_id + no_fmt_ids;
uint32_t new_limit = 0;

if (((fmt_id + no_fmt_ids) < g_msg.hdr_data.topmost_fmt_id)
&& (no_fmt_ids < g_msg.hdr_data.topmost_fmt_id))
{
new_limit = fmt_id + no_fmt_ids;
}
else
{
return 0xFFFFFFFF; // Error
}

if (new_limit > g_msg.fmt_ids_defined)
{
Expand Down Expand Up @@ -183,7 +193,7 @@ static void print_indexed_text(FILE *out, unsigned index, const char *text)
break;
}

text++; // Skip ','
text++; // Move to the next text after the comma
}

if (*text == 0) // Text with the specified index was not found.
Expand Down Expand Up @@ -238,15 +248,15 @@ static const char *get_enums_name(unsigned index)

static void print_single_value_formatting_data(FILE *out, value_format_t *p_val_fmt)
{
const char *fmt_s = p_val_fmt->fmt_string;
const char *fmt_string = p_val_fmt->fmt_string;

if (p_val_fmt->fmt_string == NULL)
{
fmt_s = "undefined";
fmt_string = "undefined";
}
else
{
fmt_s = strip_newlines_and_shorten_string(fmt_s, '"');
fmt_string = strip_newlines_and_shorten_string(fmt_string, '"');
}

const char *enums_name = "";
Expand All @@ -268,7 +278,7 @@ static void print_single_value_formatting_data(FILE *out, value_format_t *p_val_
copy_to_main = ">>";
}

fprintf(out, "%s\t%s%s\t", fmt_s, copy_to_main, enums_name);
fprintf(out, "%s\t%s%s\t", fmt_string, copy_to_main, enums_name);

switch (p_val_fmt->fmt_type)
{
Expand Down
2 changes: 1 addition & 1 deletion Code/format.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down
73 changes: 70 additions & 3 deletions Code/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -38,6 +38,8 @@
#include "cmd_line.h"
#include "utf8_helpers.h"

#pragma comment(linker, "/STACK:8388608") // Increase stack size to 8 MB


/**********************************************/
/***** G L O B A L V A R I A B L E S ******/
Expand Down Expand Up @@ -140,6 +142,60 @@ static void print_cmd_line_parameters(int argc, char *argv[])
}


/**
* @brief Report the file error in the file 'errors.msg' if detected.
*
* @param file Pointer to the file structure
* @param file_name Pointer to the file name
*/

static void check_file_errors(FILE * file, const char* file_name)
{
static bool error_reported = false;

if ((file == NULL) || (file_name == NULL))
{
return;
}

if (!ferror(file))
{
return;
}

if (!error_reported)
{
fprintf(g_msg.file.error_log, get_message_text(MSG_PROBLEMS_WRITING_TO_OUTPUT_FILES));
error_reported = true;
}

fprintf(g_msg.file.error_log, "\n %s", file_name);
}


/**
* @brief Check if any error was detected while printing to the output files.
* If error were detected, report in the file 'errors.msg'.
*/

static void check_print_errors(void)
{
// Check the default output files
check_file_errors(g_msg.file.main_log, RTE_MAIN_LOG_FILE);
check_file_errors(g_msg.file.statistics_log, RTE_STAT_MAIN_FILE);
check_file_errors(g_msg.file.timestamps, RTE_MSG_TIMESTAMPS_FILE);

// Check the user defined output files
for (size_t i = 32U; i < g_msg.enums_found; i++)
{
if (g_msg.enums[i].type == OUT_FILE_TYPE)
{
check_file_errors(g_msg.enums[i].p_file, g_msg.enums[i].file_name);
}
}
}


/**
* @brief Prints the full binary file name and creation date, and prepares the date string
* for the special format type "%D".
Expand All @@ -160,6 +216,10 @@ static void print_data_file_name_and_date(void)
strftime(g_msg.date_string, BIN_FILE_DATE_LENGTH, "%Y-%m-%d %H:%M:%S", tmp);
fprintf(out, "\"%s\" %s\n", g_msg.param.data_file_name, g_msg.date_string);
}
else
{
fprintf(out, get_message_text(ERR_NO_BIN_FILE_INFO));
}
}


Expand Down Expand Up @@ -386,8 +446,14 @@ static void Process_binary_data_file(int argc, char *argv[])
}

// Allocate buffer for message processing
g_msg.assembled_msg = (uint32_t *)allocate_memory(
sizeof(uint32_t) * 4u * (1u + (size_t)g_msg.hdr_data.max_msg_blocks) + 20u, "Asm_msg");
size_t buffer_size = sizeof(uint32_t) * 4u * (1u + (size_t)g_msg.hdr_data.max_msg_blocks) + 20u;

if (buffer_size < (256U + 16U))
{
buffer_size = 256U + 16U; // The buffer should accommodate at least the MSGX-style message
}

g_msg.assembled_msg = (uint32_t *)allocate_memory(buffer_size, "Asm_msg");
prepare_sys_msg_fmt_structure();

print_msg_intro();
Expand Down Expand Up @@ -483,6 +549,7 @@ int main(int argc, char *argv[])
}
}

check_print_errors();
print_execution_time(begin_parsing, end_parsing);
_fcloseall();
(void)_wchdir(g_msg.file.start_folder); // Return to the initial working directory.
Expand Down
8 changes: 5 additions & 3 deletions Code/main.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2024 Branko Premzel.
* Copyright (c) Branko Premzel.
*
* SPDX-License-Identifier: MIT
*/
Expand Down Expand Up @@ -216,7 +216,7 @@ typedef struct _rte_msg_t
uint32_t index; /*!< Index to the rte_buffer */
uint32_t message_cnt; /*!< Counter of all messages found - including messages with problems */
uint32_t multiple_logging; /*!< Number of separate snapshots in the binary data file */
size_t already_processed_and_skipped_data; /*!< Total number of data already processed and overwritten in the working buffer */
size_t already_processed_data; /*!< Total number of data already processed in the working buffer */
uint32_t in_size; /*!< Total size of the loaded buffer [number of words] */
uint32_t error_warning_in_msg; /*!< Number of message in which a warning is displayed after the error(s) - if any */
uint32_t *rte_buffer; /*!< Pointer to data from the embedded system circular data logging buffer */
Expand All @@ -236,7 +236,9 @@ typedef struct _rte_msg_t
uint32_t fmt_align_value; /*!< Minimal value of the next format ID */

/* Error counting during a single message decoding */
uint32_t unfinished_words; /*!< Number of consecutive words with a value of 0xFFFFFFFF */
uint32_t unfinished_words; /*!< Number of consecutive words with a value of 0xFFFFFFFF.
* Such a value indicates that the default value in the buffer was not
* overwritten with the logged one during writing to the circular buffer.*/
uint32_t bad_packet_words; /*!< Number of DATA words in a packet without a FMT word */

/* Error information logged during a single message decoding */
Expand Down
Loading

0 comments on commit d27fc31

Please sign in to comment.