attempt to unify the XML output for sequences of multi-telegram frames (single XML document, global record counts, no repeat of frame headers)

This commit is contained in:
Robert Johansson 2012-04-21 08:34:01 +09:00
parent 1d0159c531
commit 281394c092
3 changed files with 151 additions and 46 deletions

View File

@ -112,24 +112,9 @@ main(int argc, char **argv)
}
//
// here, figure out how the list of frames should be merged into a single
// XML document, but for now let's just dump all frames as independent XMLs
// generate XML and print to standard output
//
for (reply_iter = &reply; reply_iter; reply_iter = reply_iter->next)
{
if (mbus_frame_data_parse(reply_iter, &reply_data) == -1)
{
fprintf(stderr, "M-bus data parse error.\n");
return 1;
}
printf("%s", mbus_frame_data_xml(&reply_data));
// manual free, all records in the list
if (reply_data.data_var.record)
{
mbus_data_record_free(reply_data.data_var.record);
}
}
printf("%s", mbus_frame_xml(&reply));
mbus_disconnect(handle);
return 0;

View File

@ -2877,6 +2877,62 @@ mbus_data_variable_header_xml(mbus_data_variable_header *header)
return "";
}
//------------------------------------------------------------------------------
/// Generate XML for a single variable-length data record
//------------------------------------------------------------------------------
char *
mbus_data_variable_record_xml(mbus_data_record *record, int record_cnt, int frame_cnt)
{
static char buff[8192];
char str_encoded[768];
size_t len = 0;
int val;
if (record)
{
if (frame_cnt >= 0)
{
len += snprintf(&buff[len], sizeof(buff) - len,
" <DataRecord id=\"%d\" frame=\"%d\">\n",
record_cnt, frame_cnt);
}
else
{
len += snprintf(&buff[len], sizeof(buff) - len,
" <DataRecord id=\"%d\">\n", record_cnt);
}
if (record->drh.dib.dif == 0x0F) // MBUS_DIB_DIF_VENDOR_SPECIFIC
{
len += snprintf(&buff[len], sizeof(buff) - len,
" <Function>Manufacturer specific</Function>\n");
}
else if (record->drh.dib.dif == 0x1F)
{
len += snprintf(&buff[len], sizeof(buff) - len,
" <Function>More records follow</Function>\n");
}
else
{
mbus_str_xml_encode(str_encoded, mbus_data_record_function(record), sizeof(str_encoded));
len += snprintf(&buff[len], sizeof(buff) - len,
" <Function>%s</Function>\n", str_encoded);
mbus_str_xml_encode(str_encoded, mbus_data_record_unit(record), sizeof(str_encoded));
len += snprintf(&buff[len], sizeof(buff) - len,
" <Unit>%s</Unit>\n", str_encoded);
}
mbus_str_xml_encode(str_encoded, mbus_data_record_value(record), sizeof(str_encoded));
len += snprintf(&buff[len], sizeof(buff) - len, " <Value>%s</Value>\n", str_encoded);
len += snprintf(&buff[len], sizeof(buff) - len, " </DataRecord>\n\n");
return buff;
}
return "";
}
//------------------------------------------------------------------------------
/// Generate XML for variable-length data
//------------------------------------------------------------------------------
@ -2887,40 +2943,20 @@ mbus_data_variable_xml(mbus_data_variable *data)
static char buff[8192];
char str_encoded[768];
size_t len = 0;
size_t i;
int i;
if (data)
{
len += snprintf(&buff[len], sizeof(buff) - len, "<MBusData>\n\n");
len += snprintf(&buff[len], sizeof(buff) - len, "%s", mbus_data_variable_header_xml(&(data->header)));
len += snprintf(&buff[len], sizeof(buff) - len, "%s",
mbus_data_variable_header_xml(&(data->header)));
for (record = data->record, i = 0; record; record = record->next, i++)
{
len += snprintf(&buff[len], sizeof(buff) - len, " <DataRecord id=\"%zd\">\n", i);
if (record->drh.dib.dif == 0x0F) //MBUS_DIB_DIF_VENDOR_SPECIFIC)
{
len += snprintf(&buff[len], sizeof(buff) - len, " <Function>Manufacturer specific</Function>\n");
len += snprintf(&buff[len], sizeof(buff) - len, "%s",
mbus_data_variable_record_xml(record, i, -1));
}
else if (record->drh.dib.dif == 0x1F)
{
len += snprintf(&buff[len], sizeof(buff) - len, " <Function>More records follow</Function>\n");
}
else
{
mbus_str_xml_encode(str_encoded, mbus_data_record_function(record), sizeof(str_encoded));
len += snprintf(&buff[len], sizeof(buff) - len, " <Function>%s</Function>\n", str_encoded);
mbus_str_xml_encode(str_encoded, mbus_data_record_unit(record), sizeof(str_encoded));
len += snprintf(&buff[len], sizeof(buff) - len, " <Unit>%s</Unit>\n", str_encoded);
}
mbus_str_xml_encode(str_encoded, mbus_data_record_value(record), sizeof(str_encoded));
len += snprintf(&buff[len], sizeof(buff) - len, " <Value>%s</Value>\n", str_encoded);
len += snprintf(&buff[len], sizeof(buff) - len, " </DataRecord>\n\n");
}
len += snprintf(&buff[len], sizeof(buff) - len, "</MBusData>\n");
return buff;
@ -2929,9 +2965,9 @@ mbus_data_variable_xml(mbus_data_variable *data)
return "";
}
///
//------------------------------------------------------------------------------
/// Generate XML representation of fixed-length frame.
///
//------------------------------------------------------------------------------
char *
mbus_data_fixed_xml(mbus_data_fixed *data)
{
@ -2996,7 +3032,7 @@ mbus_data_fixed_xml(mbus_data_fixed *data)
}
//------------------------------------------------------------------------------
/// Return a string containing an XML representation of the M-BUS frame.
/// Return a string containing an XML representation of the M-BUS frame data.
//------------------------------------------------------------------------------
char *
mbus_frame_data_xml(mbus_frame_data *data)
@ -3018,6 +3054,89 @@ mbus_frame_data_xml(mbus_frame_data *data)
}
//------------------------------------------------------------------------------
/// Return an XML representation of the M-BUS frame.
//------------------------------------------------------------------------------
char *
mbus_frame_xml(mbus_frame *frame)
{
mbus_frame_data frame_data;
mbus_frame *iter;
mbus_data_record *record;
static char buff[8192];
size_t len = 0;
int record_cnt = 0, frame_cnt;
if (frame)
{
if (frame->type == MBUS_DATA_TYPE_FIXED)
{
//
// generate XML for fixed data
//
if (mbus_frame_data_parse(iter, &frame_data) == -1)
{
mbus_error_str_set("M-bus data parse error.");
return NULL;
}
return mbus_data_fixed_xml(&(frame_data.data_fix));
}
if (frame->type == MBUS_DATA_TYPE_VARIABLE)
{
//
// generate XML for a sequence of variable data frames
//
// include frame counter in XML output if more than one frame
// is available (frame_cnt = -1 => not included in output)
frame_cnt = (frame->next == NULL) ? -1 : 0;
len += snprintf(&buff[len], sizeof(buff) - len, "<MBusData>\n\n");
for (iter = frame; iter; iter = iter->next, frame_cnt++)
{
if (mbus_frame_data_parse(iter, &frame_data) == -1)
{
mbus_error_str_set("M-bus variable data parse error.");
return NULL;
}
// only print the header info for the first frame (should be
// the same for each frame in a sequence of a multi-telegram
// transfer.
if (frame_cnt < 1)
{
len += snprintf(&buff[len], sizeof(buff) - len, "%s",
mbus_data_variable_header_xml(&(frame_data.data_var.header)));
}
// loop through all records in the current frame, using a global
// record count as record ID in the XML output
for (record = frame_data.data_var.record; record; record = record->next, record_cnt++)
{
len += snprintf(&buff[len], sizeof(buff) - len, "%s",
mbus_data_variable_record_xml(record, record_cnt, frame_cnt));
}
// free all records in the list
if (frame_data.data_var.record)
{
mbus_data_record_free(frame_data.data_var.record);
}
}
len += snprintf(&buff[len], sizeof(buff) - len, "</MBusData>\n");
return buff;
}
}
return "";
}
//------------------------------------------------------------------------------
/// Allocate and initialize a new frame data structure

View File

@ -492,6 +492,7 @@ char *mbus_frame_data_xml(mbus_frame_data *data);
char *mbus_data_variable_header_xml(mbus_data_variable_header *header);
char *mbus_frame_xml(mbus_frame *frame);
//
// Debug/dump