From 55c2bf0deeff5c57ed1f6e32e805b1b984f04aa4 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 17 Jul 2013 22:21:07 +0200 Subject: [PATCH 1/4] Add normalized values option -n Add file pointer check after fread --- test/mbus_parse.c | 34 +++++++++++++++++++++++++++------- test/mbus_parse_hex.c | 39 +++++++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 17 deletions(-) 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; From fb84fd11a88352e0185271085c338d62a664b803 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sat, 3 Aug 2013 15:51:34 +0200 Subject: [PATCH 2/4] Add additional check if directory is empty or hex file doesn't exists --- test/generate-xml.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/generate-xml.sh b/test/generate-xml.sh index e82b8b8..a0ee783 100755 --- a/test/generate-xml.sh +++ b/test/generate-xml.sh @@ -33,6 +33,10 @@ if [ ! -d "$directory" ]; then fi for hexfile in "$directory"/*.hex; do + if [ ! -f "$hexfile" ]; then + continue + fi + filename=`basename $hexfile .hex` # Parse hex file and write XML in file From c8aaa8dc320b580fc2778e5a4b13ea1c0a3955fe Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sat, 3 Aug 2013 16:23:20 +0200 Subject: [PATCH 3/4] Add new test frames from Elster Falcon and Relay Padpuls Signed-off-by: Stefan Wahren --- test/test-frames/els_falcon.hex | 1 + test/test-frames/rel_padpuls2.hex | 1 + test/test-frames/rel_padpuls3.hex | 1 + 3 files changed, 3 insertions(+) create mode 100644 test/test-frames/els_falcon.hex create mode 100644 test/test-frames/rel_padpuls2.hex create mode 100644 test/test-frames/rel_padpuls3.hex diff --git a/test/test-frames/els_falcon.hex b/test/test-frames/els_falcon.hex new file mode 100644 index 0000000..467cd6e --- /dev/null +++ b/test/test-frames/els_falcon.hex @@ -0,0 +1 @@ +68 4A 4A 68 08 01 72 45 23 11 70 93 15 0A 07 02 00 00 00 0C 13 67 45 23 01 04 6D 3A 0D E6 02 42 6C E1 01 4C 13 51 69 45 00 42 EC 7E 01 11 12 3B 39 17 42 6C 01 11 02 3B F9 17 0F 0E 42 20 01 01 01 00 05 08 5E 01 20 3D 12 08 3D 12 08 00 C0 16 \ No newline at end of file diff --git a/test/test-frames/rel_padpuls2.hex b/test/test-frames/rel_padpuls2.hex new file mode 100644 index 0000000..65c8738 --- /dev/null +++ b/test/test-frames/rel_padpuls2.hex @@ -0,0 +1 @@ +68 2F 2F 68 08 04 72 04 00 00 00 AC 48 12 00 01 00 00 00 0C 00 00 00 00 00 04 6D 10 0D 34 09 42 6C 1F 0C 4C 00 00 00 00 00 42 EC 7E 3F 0C 0F 43 01 01 00 D0 16 \ No newline at end of file diff --git a/test/test-frames/rel_padpuls3.hex b/test/test-frames/rel_padpuls3.hex new file mode 100644 index 0000000..ba73bd0 --- /dev/null +++ b/test/test-frames/rel_padpuls3.hex @@ -0,0 +1 @@ +68 2F 2F 68 08 01 72 01 01 03 01 AC 48 40 08 1E 00 00 00 0C 6E 87 19 00 00 04 6D 29 0A 1F 0C 42 6C 1F 0C 4C 6E 02 13 00 00 42 EC 7E 3F 0C 0F C0 01 01 0C 40 16 \ No newline at end of file From d9dd055bea5f0fb3aba4e46430c718087839f56b Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sat, 3 Aug 2013 16:35:09 +0200 Subject: [PATCH 4/4] Fix mbus_vib_unit_lookup remove unreachable code add missing unit password fix typo Signed-off-by: Stefan Wahren --- mbus/mbus-protocol.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/mbus/mbus-protocol.c b/mbus/mbus-protocol.c index cec73bb..7c7daee 100755 --- a/mbus/mbus-protocol.c +++ b/mbus/mbus-protocol.c @@ -2072,6 +2072,11 @@ mbus_vib_unit_lookup(mbus_value_information_block *vib) // E000 1101 snprintf(buff, sizeof(buff), "Software version"); } + else if (vib->vife[0] == 0x16) + { + // VIFE = E001 0110 Password + snprintf(buff, sizeof(buff), "Password"); + } else if (vib->vife[0] == 0x17 || vib->vife[0] == 0x97) { // VIFE = E001 0111 Error flags @@ -2082,11 +2087,6 @@ mbus_vib_unit_lookup(mbus_value_information_block *vib) // VIFE = E001 0000 Customer location snprintf(buff, sizeof(buff), "Customer location"); } - else if (vib->vife[0] == 0x0C) - { - // E000 1100 Model / Version - snprintf(buff, sizeof(buff), "Model / Version"); - } else if (vib->vife[0] == 0x11) { // VIFE = E001 0001 Customer @@ -2102,16 +2102,6 @@ mbus_vib_unit_lookup(mbus_value_information_block *vib) // VIFE = E001 1011 Digital input (binary) snprintf(buff, sizeof(buff), "Digital input (binary)"); } - else if (vib->vife[0] == 0x09) - { - // VIFE = E001 0110 Password - snprintf(buff, sizeof(buff), "Password"); - } - else if (vib->vife[0] == 0x0B) - { - // VIFE = E000 1011 Parameter set identification - snprintf(buff, sizeof(buff), "Parameter set identification"); - } else if ((vib->vife[0] & 0x70) == 0x40) { // VIFE = E100 nnnn 10^(nnnn-9) V @@ -2131,7 +2121,7 @@ mbus_vib_unit_lookup(mbus_value_information_block *vib) } else { - snprintf(buff, sizeof(buff), "Unrecongized VIF extension: 0x%.2x", vib->vife[0]); + snprintf(buff, sizeof(buff), "Unrecognized VIF extension: 0x%.2x", vib->vife[0]); } return buff; }