diff --git a/test/mbus_parse.c b/test/mbus_parse.c index 453ee99..1659ca0 100644 --- a/test/mbus_parse.c +++ b/test/mbus_parse.c @@ -12,32 +12,50 @@ #include #include -#include +#include int main(int argc, char *argv[]) { FILE *fp = NULL; size_t buff_len, len; + int normalized = 0; unsigned char buf[1024]; mbus_frame reply; mbus_frame_data frame_data; - char *xml_result = NULL; + char *xml_result = NULL, *file = NULL; - if (argc != 2) + if (argc == 3 && strcmp(argv[1], "-n") == 0) { - fprintf(stderr, "usage: %s binary-file\n", argv[0]); + file = argv[2]; + normalized = 1; + } + else if (argc == 2) + { + file = argv[1]; + } + else + { + fprintf(stderr, "usage: %s [-n] binary-file\n", argv[0]); + fprintf(stderr, " optional flag -n for normalized values\n"); return 1; } - if ((fp = fopen(argv[1], "r")) == NULL) + if ((fp = fopen(file, "r")) == NULL) { - fprintf(stderr, "%s: failed to open '%s'\n", argv[0], argv[1]); + fprintf(stderr, "%s: failed to open '%s'\n", argv[0], file); return 1; } memset(buf, 0, sizeof(buf)); len = fread(buf, 1, sizeof(buf), fp); + + if (ferror(fp) != 0) + { + fprintf(stderr, "%s: failed to read '%s'\n", argv[0], file); + return 1; + } + fclose(fp); memset(&reply, 0, sizeof(reply)); @@ -46,7 +64,9 @@ main(int argc, char *argv[]) mbus_frame_data_parse(&reply, &frame_data); mbus_frame_print(&reply); - if ((xml_result = mbus_frame_data_xml(&frame_data)) == NULL) + xml_result = normalized ? mbus_frame_data_xml_normalized(&frame_data) : mbus_frame_data_xml(&frame_data); + + if (xml_result == NULL) { fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str()); return 1; diff --git a/test/mbus_parse_hex.c b/test/mbus_parse_hex.c index 2bf6180..e1eb04b 100644 --- a/test/mbus_parse_hex.c +++ b/test/mbus_parse_hex.c @@ -12,33 +12,50 @@ #include #include -#include +#include int main(int argc, char *argv[]) { FILE *fp = NULL; size_t buff_len, len; - int result; + int result, normalized = 0; unsigned char raw_buff[4096], buff[4096]; mbus_frame reply; mbus_frame_data frame_data; - char *xml_result = NULL; - - if (argc != 2) + char *xml_result = NULL, *file = NULL; + + if (argc == 3 && strcmp(argv[1], "-n") == 0) { - fprintf(stderr, "usage: %s hex-file\n", argv[0]); + file = argv[2]; + normalized = 1; + } + else if (argc == 2) + { + file = argv[1]; + } + else + { + fprintf(stderr, "usage: %s [-n] hex-file\n", argv[0]); + fprintf(stderr, " optional flag -n for normalized values\n"); return 1; } - - if ((fp = fopen(argv[1], "r")) == NULL) + + if ((fp = fopen(file, "r")) == NULL) { - fprintf(stderr, "%s: failed to open '%s'\n", argv[0], argv[1]); + fprintf(stderr, "%s: failed to open '%s'\n", argv[0], file); return 1; } memset(raw_buff, 0, sizeof(raw_buff)); len = fread(raw_buff, 1, sizeof(raw_buff), fp); + + if (ferror(fp) != 0) + { + fprintf(stderr, "%s: failed to read '%s'\n", argv[0], file); + return 1; + } + fclose(fp); buff_len = mbus_hex2bin(buff,sizeof(buff),raw_buff,sizeof(raw_buff)); @@ -73,7 +90,9 @@ main(int argc, char *argv[]) //mbus_frame_print(&reply); //mbus_frame_data_print(&frame_data); - if ((xml_result = mbus_frame_data_xml(&frame_data)) == NULL) + xml_result = normalized ? mbus_frame_data_xml_normalized(&frame_data) : mbus_frame_data_xml(&frame_data); + + if (xml_result == NULL) { fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str()); return 1;