diff --git a/bin/build_tool.sh b/bin/build_tool.sh new file mode 100755 index 00000000..a8ba10c3 --- /dev/null +++ b/bin/build_tool.sh @@ -0,0 +1,2 @@ +#!/bin/bash +g++ mbus-inspect-frame.cpp -I../ -lmbus -L../mbus/.libs/ -static -o mbus-inspect-frame diff --git a/bin/mbus-inspect-frame.cpp b/bin/mbus-inspect-frame.cpp new file mode 100755 index 00000000..b4444d95 --- /dev/null +++ b/bin/mbus-inspect-frame.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +extern "C" { +#include +} + +int read_hex_formated_frame(unsigned char* data, int maxsize) { + std::string line; + if(!std::getline(std::cin, line)) { + return -1; + } + + std::istringstream hex_chars(line); + + // read from stdin and count bytes + int out = 0; + unsigned int c; + while( hex_chars >> std::hex >> c ){ + out++; + if ( out >= maxsize) { + return -1; + } + data[out - 1] = c; + } + + data[out] = '\0'; + return out; +} + + +// just format framedata +// for debug usage +int +main(int argc, char **argv) +{ + unsigned char data[500]; + mbus_frame reply; + mbus_frame_data reply_data; + char* xml_result; + + int line_length = read_hex_formated_frame(data, 500); + if (line_length <= 0) + { + std::cout << "line couldnt be parsed in any way, or was too long (500 bytes)" << std::endl; + return 0; + } + + if (mbus_parse(&reply, data, line_length) < 0) + { + fprintf(stderr, "M-bus parse_data error: %s %d\n", mbus_error_str(), line_length); + return 1; + } + // + // parse data + // + if (mbus_frame_data_parse(&reply, &reply_data) == -1) + { + fprintf(stderr, "M-bus data parse error: %s\n", mbus_error_str()); + return 1; + } + + // + // generate XML and print to standard output + // + if ((xml_result = mbus_frame_data_xml(&reply_data)) == NULL) + { + fprintf(stderr, "Failed to generate XML representation of MBUS frame: %s\n", mbus_error_str()); + return 1; + } + + printf("%s", xml_result); + free(xml_result); + + // manual free + if (reply_data.data_var.record) + { + mbus_data_record_free(reply_data.data_var.record); // free's up the whole list + } + + return 0; +} diff --git a/mbus/mbus-protocol.c b/mbus/mbus-protocol.c index 93123644..e2eab933 100755 --- a/mbus/mbus-protocol.c +++ b/mbus/mbus-protocol.c @@ -4649,9 +4649,11 @@ mbus_data_variable_record_xml(mbus_data_record *record, int record_cnt, int fram if (record->timestamp > 0) { timeinfo = gmtime (&(record->timestamp)); - strftime(timestamp,21,"%Y-%m-%dT%H:%M:%SZ",timeinfo); - len += snprintf(&buff[len], sizeof(buff) - len, + if(timeinfo) { + strftime(timestamp,21,"%Y-%m-%dT%H:%M:%SZ",timeinfo); + len += snprintf(&buff[len], sizeof(buff) - len, " %s\n", timestamp); + } } len += snprintf(&buff[len], sizeof(buff) - len, " \n\n");