Merge pull request #48 from lategoodbye/master
parseable for splint, fix memleaks, minor fixes
This commit is contained in:
commit
b0dff87093
@ -29,7 +29,7 @@ main(int argc, char **argv)
|
||||
int port, address, result;
|
||||
FILE *fp = NULL;
|
||||
size_t buff_len, len;
|
||||
u_char raw_buff[4096], buff[4096];
|
||||
unsigned char raw_buff[4096], buff[4096];
|
||||
|
||||
memset((void *)&reply, 0, sizeof(mbus_frame));
|
||||
memset((void *)&reply_data, 0, sizeof(mbus_frame_data));
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
/*@ignore@*/
|
||||
#define MBUS_ERROR(...) fprintf (stderr, __VA_ARGS__)
|
||||
|
||||
#ifdef _DEBUG_
|
||||
@ -27,6 +28,7 @@
|
||||
#else
|
||||
#define MBUS_DEBUG(...)
|
||||
#endif
|
||||
/*@end@*/
|
||||
|
||||
static int debug = 0;
|
||||
|
||||
@ -766,7 +768,6 @@ int mbus_fixed_normalize(int medium_unit, long medium_value, char **unit_out, do
|
||||
exponent = 0.0;
|
||||
*value_out = 0.0;
|
||||
return -1;
|
||||
break;
|
||||
}
|
||||
|
||||
return -2;
|
||||
@ -776,11 +777,11 @@ int mbus_fixed_normalize(int medium_unit, long medium_value, char **unit_out, do
|
||||
int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real, char **value_out_str, int *value_out_str_size)
|
||||
{
|
||||
int result = 0;
|
||||
unsigned char vif, vife;
|
||||
struct tm time;
|
||||
*value_out_real = 0.0;
|
||||
*value_out_str = NULL;
|
||||
*value_out_str_size = 0;
|
||||
u_char vif, vife;
|
||||
struct tm time;
|
||||
|
||||
if (record)
|
||||
{
|
||||
@ -908,7 +909,7 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = record->data_len;
|
||||
mbus_data_str_decode((u_char*)(*value_out_str), record->data, record->data_len);
|
||||
mbus_data_str_decode((unsigned char*)(*value_out_str), record->data, record->data_len);
|
||||
result = 0;
|
||||
break;
|
||||
}
|
||||
@ -929,7 +930,7 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
return -1;
|
||||
}
|
||||
*value_out_str_size = 3 * record->data_len;
|
||||
mbus_data_bin_decode((u_char*)(*value_out_str), record->data, record->data_len, (3 * record->data_len + 1));
|
||||
mbus_data_bin_decode((unsigned char*)(*value_out_str), record->data, record->data_len, (3 * record->data_len + 1));
|
||||
result = 0;
|
||||
break;
|
||||
|
||||
@ -951,13 +952,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
|
||||
int
|
||||
mbus_vif_unit_normalize(int vif, double value, char **unit_out, double *value_out, char **quantity_out)
|
||||
{
|
||||
MBUS_DEBUG("vif_unit_normalize = 0x%03X \n", vif);
|
||||
|
||||
double exponent = 1.0;
|
||||
|
||||
unsigned newVif = vif & 0xF7F; /* clear extension bit */
|
||||
|
||||
int i;
|
||||
double exponent = 1.0;
|
||||
unsigned newVif = vif & 0xF7F; /* clear extension bit */
|
||||
|
||||
MBUS_DEBUG("vif_unit_normalize = 0x%03X \n", vif);
|
||||
|
||||
if (unit_out == NULL || value_out == NULL || quantity_out == NULL)
|
||||
{
|
||||
@ -988,6 +987,8 @@ mbus_vif_unit_normalize(int vif, double value, char **unit_out, double *value_ou
|
||||
int
|
||||
mbus_vib_unit_normalize(mbus_value_information_block *vib, double value, char **unit_out, double *value_out, char **quantity_out)
|
||||
{
|
||||
int code;
|
||||
|
||||
if (vib == NULL || unit_out == NULL || value_out == NULL || quantity_out == NULL)
|
||||
{
|
||||
MBUS_ERROR("%s: Invalid parameter.\n", __PRETTY_FUNCTION__);
|
||||
@ -1004,7 +1005,7 @@ mbus_vib_unit_normalize(mbus_value_information_block *vib, double value, char **
|
||||
return -1;
|
||||
}
|
||||
|
||||
int code = ((vib->vife[0]) & MBUS_DIB_VIF_WITHOUT_EXTENSION) | 0x100;
|
||||
code = ((vib->vife[0]) & MBUS_DIB_VIF_WITHOUT_EXTENSION) | 0x100;
|
||||
if (mbus_vif_unit_normalize(code, value, unit_out, value_out, quantity_out) != 0)
|
||||
{
|
||||
MBUS_ERROR("%s: Error mbus_vif_unit_normalize\n", __PRETTY_FUNCTION__);
|
||||
@ -1019,7 +1020,7 @@ mbus_vib_unit_normalize(mbus_value_information_block *vib, double value, char **
|
||||
return -1;
|
||||
}
|
||||
|
||||
int code = ((vib->vife[0]) & MBUS_DIB_VIF_WITHOUT_EXTENSION) | 0x200;
|
||||
code = ((vib->vife[0]) & MBUS_DIB_VIF_WITHOUT_EXTENSION) | 0x200;
|
||||
if (0 != mbus_vif_unit_normalize(code, value, unit_out, value_out, quantity_out))
|
||||
{
|
||||
MBUS_ERROR("%s: Error mbus_vif_unit_normalize\n", __PRETTY_FUNCTION__);
|
||||
@ -1036,7 +1037,7 @@ mbus_vib_unit_normalize(mbus_value_information_block *vib, double value, char **
|
||||
}
|
||||
else
|
||||
{
|
||||
int code = (vib->vif) & MBUS_DIB_VIF_WITHOUT_EXTENSION;
|
||||
code = (vib->vif) & MBUS_DIB_VIF_WITHOUT_EXTENSION;
|
||||
if (0 != mbus_vif_unit_normalize(code, value, unit_out, value_out, quantity_out))
|
||||
{
|
||||
MBUS_ERROR("%s: Error mbus_vif_unit_normalize\n", __PRETTY_FUNCTION__);
|
||||
@ -1049,7 +1050,7 @@ mbus_vib_unit_normalize(mbus_value_information_block *vib, double value, char **
|
||||
(vib->vif != 0xFD) &&
|
||||
(vib->vif != 0xFB)) /* codes for VIF extention: see table 8.4.5 */
|
||||
{
|
||||
int code = (vib->vif) & 0x7f;
|
||||
code = (vib->vif) & 0x7f;
|
||||
switch (code)
|
||||
{
|
||||
case 0x70:
|
||||
@ -1134,8 +1135,9 @@ mbus_record_free(mbus_record * rec)
|
||||
|
||||
|
||||
mbus_record *
|
||||
mbus_parse_fixed_record(char status_byte, char medium_unit, u_char *data)
|
||||
mbus_parse_fixed_record(char status_byte, char medium_unit, unsigned char *data)
|
||||
{
|
||||
long value = 0;
|
||||
mbus_record * record = NULL;
|
||||
|
||||
if (!(record = mbus_record_new()))
|
||||
@ -1154,7 +1156,6 @@ mbus_parse_fixed_record(char status_byte, char medium_unit, u_char *data)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
long value = 0;
|
||||
if ((status_byte & MBUS_DATA_FIXED_STATUS_FORMAT_MASK) == MBUS_DATA_FIXED_STATUS_FORMAT_BCD)
|
||||
{
|
||||
value = mbus_data_bcd_decode(data, 4);
|
||||
@ -1294,7 +1295,7 @@ mbus_data_variable_xml_normalized(mbus_data_variable *data)
|
||||
{
|
||||
mbus_data_record *record;
|
||||
mbus_record *norm_record;
|
||||
char *buff = NULL;
|
||||
char *buff = NULL, *new_buff = NULL;
|
||||
char str_encoded[768];
|
||||
size_t len = 0, buff_size = 8192;
|
||||
size_t i;
|
||||
@ -1317,10 +1318,16 @@ mbus_data_variable_xml_normalized(mbus_data_variable *data)
|
||||
if ((buff_size - len) < 1024)
|
||||
{
|
||||
buff_size *= 2;
|
||||
buff = (char*) realloc(buff,buff_size);
|
||||
new_buff = (char*) realloc(buff,buff_size);
|
||||
|
||||
if (buff == NULL)
|
||||
if (new_buff == NULL)
|
||||
{
|
||||
mbus_record_free(norm_record);
|
||||
free(buff);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buff = new_buff;
|
||||
}
|
||||
|
||||
len += snprintf(&buff[len], buff_size - len, " <DataRecord id=\"%zd\">\n", i);
|
||||
@ -2298,11 +2305,11 @@ mbus_scan_2nd_address_range(mbus_handle * handle, int pos, char *addr_mask)
|
||||
// - whitespaces will be ignored
|
||||
//------------------------------------------------------------------------------
|
||||
size_t
|
||||
mbus_hex2bin(u_char * dst, size_t dst_len, const u_char * src, size_t src_len)
|
||||
mbus_hex2bin(unsigned char * dst, size_t dst_len, const unsigned char * src, size_t src_len)
|
||||
{
|
||||
size_t i, result = 0;
|
||||
unsigned long val;
|
||||
u_char *ptr, *end, buf[3];
|
||||
unsigned char *ptr, *end, buf[3];
|
||||
|
||||
if (!src || !dst)
|
||||
{
|
||||
@ -2333,7 +2340,7 @@ mbus_hex2bin(u_char * dst, size_t dst_len, const u_char * src, size_t src_len)
|
||||
if (result >= dst_len)
|
||||
break;
|
||||
|
||||
dst[result++] = (u_char) val;
|
||||
dst[result++] = (unsigned char) val;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -368,7 +368,7 @@ void mbus_record_free(mbus_record *rec);
|
||||
*
|
||||
* @return Newly allocated record if succesful, NULL otherwise. Later on need to use #mbus_record_free
|
||||
*/
|
||||
mbus_record *mbus_parse_fixed_record(char statusByte, char medium_unit_byte, u_char *data);
|
||||
mbus_record *mbus_parse_fixed_record(char statusByte, char medium_unit_byte, unsigned char *data);
|
||||
|
||||
|
||||
/**
|
||||
@ -477,7 +477,7 @@ int mbus_scan_2nd_address_range(mbus_handle * handle, int pos, char *addr_mask);
|
||||
*
|
||||
* @return byte count of successful converted values
|
||||
*/
|
||||
size_t mbus_hex2bin(u_char * dst, size_t dst_len, const u_char * src, size_t src_len);
|
||||
size_t mbus_hex2bin(unsigned char * dst, size_t dst_len, const unsigned char * src, size_t src_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@ -29,20 +29,20 @@ static mbus_slave_data slave_data[MBUS_MAX_PRIMARY_SLAVES];
|
||||
//
|
||||
// init event callback
|
||||
//
|
||||
void (*_mbus_recv_event)(u_char src_type, const char *buff, size_t len) = NULL;
|
||||
void (*_mbus_send_event)(u_char src_type, const char *buff, size_t len) = NULL;
|
||||
void (*_mbus_recv_event)(unsigned char src_type, const char *buff, size_t len) = NULL;
|
||||
void (*_mbus_send_event)(unsigned char src_type, const char *buff, size_t len) = NULL;
|
||||
|
||||
//
|
||||
// trace callbacks
|
||||
//
|
||||
void
|
||||
mbus_dump_recv_event(u_char src_type, const char *buff, size_t len)
|
||||
mbus_dump_recv_event(unsigned char src_type, const char *buff, size_t len)
|
||||
{
|
||||
mbus_hex_dump("RECV", buff, len);
|
||||
}
|
||||
|
||||
void
|
||||
mbus_dump_send_event(u_char src_type, const char *buff, size_t len)
|
||||
mbus_dump_send_event(unsigned char src_type, const char *buff, size_t len)
|
||||
{
|
||||
mbus_hex_dump("SEND", buff, len);
|
||||
}
|
||||
@ -51,7 +51,7 @@ mbus_dump_send_event(u_char src_type, const char *buff, size_t len)
|
||||
/// Register a function for receive events.
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_register_recv_event(void (*event)(u_char src_type, const char *buff, size_t len))
|
||||
mbus_register_recv_event(void (*event)(unsigned char src_type, const char *buff, size_t len))
|
||||
{
|
||||
_mbus_recv_event = event;
|
||||
}
|
||||
@ -60,7 +60,7 @@ mbus_register_recv_event(void (*event)(u_char src_type, const char *buff, size_t
|
||||
/// Register a function for send events.
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_register_send_event(void (*event)(u_char src_type, const char *buff, size_t len))
|
||||
mbus_register_send_event(void (*event)(unsigned char src_type, const char *buff, size_t len))
|
||||
{
|
||||
_mbus_send_event = event;
|
||||
}
|
||||
@ -182,11 +182,11 @@ mbus_frame_free(mbus_frame *frame)
|
||||
//------------------------------------------------------------------------------
|
||||
/// Caclulate the checksum of the M-Bus frame. Internal.
|
||||
//------------------------------------------------------------------------------
|
||||
u_char
|
||||
unsigned char
|
||||
calc_checksum(mbus_frame *frame)
|
||||
{
|
||||
size_t i;
|
||||
u_char cksum;
|
||||
unsigned char cksum;
|
||||
|
||||
assert(frame != NULL);
|
||||
switch(frame->type)
|
||||
@ -258,7 +258,7 @@ mbus_frame_calc_checksum(mbus_frame *frame)
|
||||
///
|
||||
/// Calculate the values of the lengths fields in the M-Bus frame. Internal.
|
||||
///
|
||||
u_char
|
||||
unsigned char
|
||||
calc_length(const mbus_frame *frame)
|
||||
{
|
||||
assert(frame != NULL);
|
||||
@ -335,7 +335,7 @@ mbus_frame_direction(mbus_frame *frame)
|
||||
int
|
||||
mbus_frame_verify(mbus_frame *frame)
|
||||
{
|
||||
u_char checksum;
|
||||
unsigned char checksum;
|
||||
|
||||
if (frame)
|
||||
{
|
||||
@ -446,20 +446,20 @@ mbus_frame_verify(mbus_frame *frame)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_data_bcd_encode(u_char *bcd_data, size_t bcd_data_size, int value)
|
||||
mbus_data_bcd_encode(unsigned char *bcd_data, size_t bcd_data_size, int value)
|
||||
{
|
||||
int v0, v1, v2, x1, x2;
|
||||
size_t i;
|
||||
|
||||
v2 = value;
|
||||
|
||||
if (bcd_data)
|
||||
if (bcd_data && bcd_data_size && (value >= 0))
|
||||
{
|
||||
v2 = value;
|
||||
|
||||
for (i = 0; i < bcd_data_size; i++)
|
||||
{
|
||||
v0 = v2;
|
||||
v1 = (int)(v0 / 10.0);
|
||||
v2 = (int)(v1 / 10.0);
|
||||
v1 = (int)(v0 / 10);
|
||||
v2 = (int)(v1 / 10);
|
||||
|
||||
x1 = v0 - v1 * 10;
|
||||
x2 = v1 - v2 * 10;
|
||||
@ -479,7 +479,7 @@ mbus_data_bcd_encode(u_char *bcd_data, size_t bcd_data_size, int value)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
long long
|
||||
mbus_data_bcd_decode(u_char *bcd_data, size_t bcd_data_size)
|
||||
mbus_data_bcd_decode(unsigned char *bcd_data, size_t bcd_data_size)
|
||||
{
|
||||
long long val = 0;
|
||||
size_t i;
|
||||
@ -504,7 +504,7 @@ mbus_data_bcd_decode(u_char *bcd_data, size_t bcd_data_size)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_data_int_decode(u_char *int_data, size_t int_data_size)
|
||||
mbus_data_int_decode(unsigned char *int_data, size_t int_data_size)
|
||||
{
|
||||
int val = 0;
|
||||
size_t i;
|
||||
@ -523,7 +523,7 @@ mbus_data_int_decode(u_char *int_data, size_t int_data_size)
|
||||
}
|
||||
|
||||
long
|
||||
mbus_data_long_decode(u_char *int_data, size_t int_data_size)
|
||||
mbus_data_long_decode(unsigned char *int_data, size_t int_data_size)
|
||||
{
|
||||
long val = 0;
|
||||
size_t i;
|
||||
@ -542,7 +542,7 @@ mbus_data_long_decode(u_char *int_data, size_t int_data_size)
|
||||
}
|
||||
|
||||
long long
|
||||
mbus_data_long_long_decode(u_char *int_data, size_t int_data_size)
|
||||
mbus_data_long_long_decode(unsigned char *int_data, size_t int_data_size)
|
||||
{
|
||||
long long val = 0;
|
||||
size_t i;
|
||||
@ -566,7 +566,7 @@ mbus_data_long_long_decode(u_char *int_data, size_t int_data_size)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_data_int_encode(u_char *int_data, size_t int_data_size, int value)
|
||||
mbus_data_int_encode(unsigned char *int_data, size_t int_data_size, int value)
|
||||
{
|
||||
int i;
|
||||
|
||||
@ -591,7 +591,7 @@ mbus_data_int_encode(u_char *int_data, size_t int_data_size, int value)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
float
|
||||
mbus_data_float_decode(u_char *float_data)
|
||||
mbus_data_float_decode(unsigned char *float_data)
|
||||
{
|
||||
float val = 0.0f;
|
||||
long temp = 0, fraction;
|
||||
@ -636,7 +636,7 @@ mbus_data_float_decode(u_char *float_data)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_data_str_decode(u_char *dst, const u_char *src, size_t len)
|
||||
mbus_data_str_decode(unsigned char *dst, const unsigned char *src, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
@ -657,7 +657,7 @@ mbus_data_str_decode(u_char *dst, const u_char *src, size_t len)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_data_bin_decode(u_char *dst, const u_char *src, size_t len, size_t max_len)
|
||||
mbus_data_bin_decode(unsigned char *dst, const unsigned char *src, size_t len, size_t max_len)
|
||||
{
|
||||
size_t i, pos;
|
||||
|
||||
@ -687,18 +687,25 @@ mbus_data_bin_decode(u_char *dst, const u_char *src, size_t len, size_t max_len)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_data_tm_decode(struct tm *t, u_char *t_data, size_t t_data_size)
|
||||
mbus_data_tm_decode(struct tm *t, unsigned char *t_data, size_t t_data_size)
|
||||
{
|
||||
if (t && t_data)
|
||||
if (t == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
t->tm_sec = 0;
|
||||
t->tm_min = 0;
|
||||
t->tm_hour = 0;
|
||||
t->tm_mday = 0;
|
||||
t->tm_mon = 0;
|
||||
t->tm_year = 0;
|
||||
t->tm_wday = 0;
|
||||
t->tm_yday = 0;
|
||||
t->tm_isdst = 0;
|
||||
|
||||
if (t_data)
|
||||
{
|
||||
t->tm_sec = 0;
|
||||
t->tm_min = 0;
|
||||
t->tm_hour = 0;
|
||||
t->tm_mday = 0;
|
||||
t->tm_mon = 0;
|
||||
t->tm_year = 0;
|
||||
t->tm_isdst = 0;
|
||||
|
||||
if (t_data_size == 4) // Type F = Compound CP32: Date and Time
|
||||
{
|
||||
if ((t_data[0] & 0x80) == 0) // Time valid ?
|
||||
@ -728,7 +735,7 @@ mbus_data_tm_decode(struct tm *t, u_char *t_data, size_t t_data_size)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_data_manufacturer_encode(u_char *m_data, u_char *m_code)
|
||||
mbus_data_manufacturer_encode(unsigned char *m_data, unsigned char *m_code)
|
||||
{
|
||||
int m_val;
|
||||
|
||||
@ -750,7 +757,7 @@ mbus_data_manufacturer_encode(u_char *m_data, u_char *m_code)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
const char *
|
||||
mbus_decode_manufacturer(u_char byte1, u_char byte2)
|
||||
mbus_decode_manufacturer(unsigned char byte1, unsigned char byte2)
|
||||
{
|
||||
static char m_str[4];
|
||||
|
||||
@ -1396,7 +1403,7 @@ mbus_data_fixed_unit(int medium_unit_byte)
|
||||
/// For variable-length frames, returns a string describing the medium.
|
||||
///
|
||||
const char *
|
||||
mbus_data_variable_medium_lookup(u_char medium)
|
||||
mbus_data_variable_medium_lookup(unsigned char medium)
|
||||
{
|
||||
static char buff[256];
|
||||
|
||||
@ -1561,8 +1568,8 @@ mbus_unit_prefix(int exp)
|
||||
///
|
||||
/// See the table on page 41 the M-BUS specification.
|
||||
//------------------------------------------------------------------------------
|
||||
u_char
|
||||
mbus_dif_datalength_lookup(u_char dif)
|
||||
unsigned char
|
||||
mbus_dif_datalength_lookup(unsigned char dif)
|
||||
{
|
||||
switch (dif&0x0F)
|
||||
{
|
||||
@ -1614,7 +1621,7 @@ mbus_dif_datalength_lookup(u_char dif)
|
||||
/// See section 8.4.3 Codes for Value Information Field (VIF) in the M-BUS spec
|
||||
//------------------------------------------------------------------------------
|
||||
const char *
|
||||
mbus_vif_unit_lookup(u_char vif)
|
||||
mbus_vif_unit_lookup(unsigned char vif)
|
||||
{
|
||||
static char buff[256];
|
||||
int n;
|
||||
@ -2172,7 +2179,7 @@ const char *
|
||||
mbus_data_record_decode(mbus_data_record *record)
|
||||
{
|
||||
static char buff[768];
|
||||
u_char vif, vife;
|
||||
unsigned char vif, vife;
|
||||
|
||||
// ignore extension bit
|
||||
vif = (record->drh.vib.vif & MBUS_DIB_VIF_WITHOUT_EXTENSION);
|
||||
@ -2363,7 +2370,7 @@ mbus_data_record_decode(mbus_data_record *record)
|
||||
mbus_data_str_decode(buff, record->data, record->data_len);
|
||||
break;
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
/*@fallthrough@*/
|
||||
|
||||
default:
|
||||
|
||||
@ -2476,7 +2483,7 @@ mbus_data_fixed_function(int status)
|
||||
/// PARSE M-BUS frame data structures from binary data.
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_parse(mbus_frame *frame, u_char *data, size_t data_size)
|
||||
mbus_parse(mbus_frame *frame, unsigned char *data, size_t data_size)
|
||||
{
|
||||
size_t i, len;
|
||||
|
||||
@ -2658,7 +2665,7 @@ mbus_data_fixed_parse(mbus_frame *frame, mbus_data_fixed *data)
|
||||
int
|
||||
mbus_data_variable_parse(mbus_frame *frame, mbus_data_variable *data)
|
||||
{
|
||||
mbus_data_record *record;
|
||||
mbus_data_record *record = NULL;
|
||||
size_t i, j;
|
||||
|
||||
if (frame && data)
|
||||
@ -2728,7 +2735,7 @@ mbus_data_variable_parse(mbus_frame *frame, mbus_data_variable *data)
|
||||
while (frame->data[i] & MBUS_DIB_DIF_EXTENSION_BIT &&
|
||||
record->drh.dib.ndife < NITEMS(record->drh.dib.dife))
|
||||
{
|
||||
u_char dife = frame->data[i+1];
|
||||
unsigned char dife = frame->data[i+1];
|
||||
record->drh.dib.dife[record->drh.dib.ndife] = dife;
|
||||
|
||||
record->drh.dib.ndife++;
|
||||
@ -2766,7 +2773,7 @@ mbus_data_variable_parse(mbus_frame *frame, mbus_data_variable *data)
|
||||
while (frame->data[i] & MBUS_DIB_VIF_EXTENSION_BIT &&
|
||||
record->drh.vib.nvife < NITEMS(record->drh.vib.vife))
|
||||
{
|
||||
u_char vife = frame->data[i+1];
|
||||
unsigned char vife = frame->data[i+1];
|
||||
record->drh.vib.vife[record->drh.vib.nvife] = vife;
|
||||
|
||||
record->drh.vib.nvife++;
|
||||
@ -2878,12 +2885,8 @@ mbus_frame_data_parse(mbus_frame *frame, mbus_frame_data *data)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(error_str, sizeof(error_str), "Wrong direction in frame (master to slave)");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
snprintf(error_str, sizeof(error_str), "Wrong direction in frame (master to slave)");
|
||||
|
||||
return -1;
|
||||
}
|
||||
@ -2894,7 +2897,7 @@ mbus_frame_data_parse(mbus_frame *frame, mbus_frame_data *data)
|
||||
/// of M-bus frames.
|
||||
//------------------------------------------------------------------------------
|
||||
int
|
||||
mbus_frame_pack(mbus_frame *frame, u_char *data, size_t data_size)
|
||||
mbus_frame_pack(mbus_frame *frame, unsigned char *data, size_t data_size)
|
||||
{
|
||||
size_t i, offset = 0;
|
||||
|
||||
@ -3121,7 +3124,7 @@ int
|
||||
mbus_frame_print(mbus_frame *frame)
|
||||
{
|
||||
mbus_frame *iter;
|
||||
u_char data_buff[256];
|
||||
unsigned char data_buff[256];
|
||||
int len, i;
|
||||
|
||||
if (frame == NULL)
|
||||
@ -3245,7 +3248,7 @@ mbus_data_variable_print(mbus_data_variable *data)
|
||||
// DIFE
|
||||
for (j = 0; j < record->drh.dib.ndife; j++)
|
||||
{
|
||||
u_char dife = record->drh.dib.dife[j];
|
||||
unsigned char dife = record->drh.dib.dife[j];
|
||||
|
||||
printf("DIFE[%zd] = %.2X\n", j, dife);
|
||||
printf("DIFE[%zd].Extension = %s\n", j, (dife & MBUS_DIB_DIF_EXTENSION_BIT) ? "Yes" : "No");
|
||||
@ -3261,7 +3264,7 @@ mbus_data_variable_print(mbus_data_variable *data)
|
||||
// VIFE
|
||||
for (j = 0; j < record->drh.vib.nvife; j++)
|
||||
{
|
||||
u_char vife = record->drh.vib.vife[j];
|
||||
unsigned char vife = record->drh.vib.vife[j];
|
||||
|
||||
printf("VIFE[%zd] = %.2X\n", j, vife);
|
||||
printf("VIFE[%zd].Extension = %s\n", j, (vife & MBUS_DIB_VIF_EXTENSION_BIT) ? "Yes" : "No");
|
||||
@ -3330,7 +3333,7 @@ mbus_hex_dump(const char *label, const char *buff, size_t len)
|
||||
|
||||
for (i = 0; i < len; i++)
|
||||
{
|
||||
fprintf(stderr, " %02X", (u_char) buff[i]);
|
||||
fprintf(stderr, " %02X", (unsigned char) buff[i]);
|
||||
}
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
@ -3356,7 +3359,7 @@ mbus_data_error_print(int error)
|
||||
///
|
||||
//------------------------------------------------------------------------------
|
||||
void
|
||||
mbus_str_xml_encode(u_char *dst, const u_char *src, size_t max_len)
|
||||
mbus_str_xml_encode(unsigned char *dst, const unsigned char *src, size_t max_len)
|
||||
{
|
||||
size_t i, len;
|
||||
|
||||
@ -3461,7 +3464,6 @@ mbus_data_variable_record_xml(mbus_data_record *record, int record_cnt, int fram
|
||||
size_t len = 0;
|
||||
struct tm * timeinfo;
|
||||
char timestamp[21];
|
||||
int val;
|
||||
|
||||
if (record)
|
||||
{
|
||||
@ -3520,7 +3522,7 @@ char *
|
||||
mbus_data_variable_xml(mbus_data_variable *data)
|
||||
{
|
||||
mbus_data_record *record;
|
||||
char *buff = NULL;
|
||||
char *buff = NULL, *new_buff;
|
||||
size_t len = 0, buff_size = 8192;
|
||||
int i;
|
||||
|
||||
@ -3541,10 +3543,15 @@ mbus_data_variable_xml(mbus_data_variable *data)
|
||||
if ((buff_size - len) < 1024)
|
||||
{
|
||||
buff_size *= 2;
|
||||
buff = (char*) realloc(buff,buff_size);
|
||||
new_buff = (char*) realloc(buff,buff_size);
|
||||
|
||||
if (buff == NULL)
|
||||
if (new_buff == NULL)
|
||||
{
|
||||
free(buff);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buff = new_buff;
|
||||
}
|
||||
|
||||
len += snprintf(&buff[len], buff_size - len, "%s",
|
||||
@ -3696,7 +3703,7 @@ mbus_frame_xml(mbus_frame *frame)
|
||||
mbus_frame *iter;
|
||||
|
||||
mbus_data_record *record;
|
||||
char *buff = NULL;
|
||||
char *buff = NULL, *new_buff;
|
||||
|
||||
size_t len = 0, buff_size = 8192;
|
||||
int record_cnt = 0, frame_cnt;
|
||||
@ -3736,7 +3743,10 @@ mbus_frame_xml(mbus_frame *frame)
|
||||
buff = (char*) malloc(buff_size);
|
||||
|
||||
if (buff == NULL)
|
||||
return NULL;
|
||||
{
|
||||
mbus_data_record_free(frame_data.data_var.record);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// include frame counter in XML output if more than one frame
|
||||
// is available (frame_cnt = -1 => not included in output)
|
||||
@ -3757,10 +3767,16 @@ mbus_frame_xml(mbus_frame *frame)
|
||||
if ((buff_size - len) < 1024)
|
||||
{
|
||||
buff_size *= 2;
|
||||
buff = (char*) realloc(buff,buff_size);
|
||||
|
||||
if (buff == NULL)
|
||||
new_buff = (char*) realloc(buff,buff_size);
|
||||
|
||||
if (new_buff == NULL)
|
||||
{
|
||||
free(buff);
|
||||
mbus_data_record_free(frame_data.data_var.record);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buff = new_buff;
|
||||
}
|
||||
|
||||
len += snprintf(&buff[len], buff_size - len, "%s",
|
||||
@ -3790,10 +3806,16 @@ mbus_frame_xml(mbus_frame *frame)
|
||||
if ((buff_size - len) < 1024)
|
||||
{
|
||||
buff_size *= 2;
|
||||
buff = (char*) realloc(buff,buff_size);
|
||||
new_buff = (char*) realloc(buff,buff_size);
|
||||
|
||||
if (buff == NULL)
|
||||
if (new_buff == NULL)
|
||||
{
|
||||
free(buff);
|
||||
mbus_data_record_free(frame_data.data_var.record);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buff = new_buff;
|
||||
}
|
||||
|
||||
len += snprintf(&buff[len], buff_size - len, "%s",
|
||||
|
@ -71,18 +71,18 @@ extern "C" {
|
||||
|
||||
typedef struct _mbus_frame {
|
||||
|
||||
u_char start1;
|
||||
u_char length1;
|
||||
u_char length2;
|
||||
u_char start2;
|
||||
u_char control;
|
||||
u_char address;
|
||||
u_char control_information;
|
||||
unsigned char start1;
|
||||
unsigned char length1;
|
||||
unsigned char length2;
|
||||
unsigned char start2;
|
||||
unsigned char control;
|
||||
unsigned char address;
|
||||
unsigned char control_information;
|
||||
// variable data field
|
||||
u_char checksum;
|
||||
u_char stop;
|
||||
unsigned char checksum;
|
||||
unsigned char stop;
|
||||
|
||||
u_char data[252];
|
||||
unsigned char data[252];
|
||||
size_t data_size;
|
||||
|
||||
int type;
|
||||
@ -134,19 +134,19 @@ typedef struct _mbus_slave_data {
|
||||
|
||||
typedef struct _mbus_data_information_block {
|
||||
|
||||
u_char dif;
|
||||
u_char dife[10];
|
||||
unsigned char dif;
|
||||
unsigned char dife[10];
|
||||
size_t ndife;
|
||||
|
||||
} mbus_data_information_block;
|
||||
|
||||
typedef struct _mbus_value_information_block {
|
||||
|
||||
u_char vif;
|
||||
u_char vife[10];
|
||||
unsigned char vif;
|
||||
unsigned char vife[10];
|
||||
size_t nvife;
|
||||
|
||||
u_char custom_vif[128];
|
||||
unsigned char custom_vif[128];
|
||||
|
||||
} mbus_value_information_block;
|
||||
|
||||
@ -161,7 +161,7 @@ typedef struct _mbus_data_record {
|
||||
|
||||
mbus_data_record_header drh;
|
||||
|
||||
u_char data[234];
|
||||
unsigned char data[234];
|
||||
size_t data_len;
|
||||
|
||||
time_t timestamp;
|
||||
@ -181,13 +181,13 @@ typedef struct _mbus_data_variable_header {
|
||||
// ex
|
||||
// 88 63 80 09 82 4D 02 04 15 00 00 00
|
||||
|
||||
u_char id_bcd[4]; // 88 63 80 09
|
||||
u_char manufacturer[2]; // 82 4D
|
||||
u_char version; // 02
|
||||
u_char medium; // 04
|
||||
u_char access_no; // 15
|
||||
u_char status; // 00
|
||||
u_char signature[2]; // 00 00
|
||||
unsigned char id_bcd[4]; // 88 63 80 09
|
||||
unsigned char manufacturer[2]; // 82 4D
|
||||
unsigned char version; // 02
|
||||
unsigned char medium; // 04
|
||||
unsigned char access_no; // 15
|
||||
unsigned char status; // 00
|
||||
unsigned char signature[2]; // 00 00
|
||||
|
||||
} mbus_data_variable_header;
|
||||
|
||||
@ -201,14 +201,14 @@ typedef struct _mbus_data_variable {
|
||||
mbus_data_record *record;
|
||||
size_t nrecords;
|
||||
|
||||
u_char *data;
|
||||
unsigned char *data;
|
||||
size_t data_len;
|
||||
|
||||
u_char more_records_follow;
|
||||
unsigned char more_records_follow;
|
||||
|
||||
// are these needed/used?
|
||||
u_char mdh;
|
||||
u_char *mfg_data;
|
||||
unsigned char mdh;
|
||||
unsigned char *mfg_data;
|
||||
size_t mfg_data_len;
|
||||
|
||||
} mbus_data_variable;
|
||||
@ -230,13 +230,13 @@ typedef struct _mbus_data_fixed {
|
||||
// 01 00 00 00 counter 1 = 1l (actual value)
|
||||
// 35 01 00 00 counter 2 = 135 l (historic value)
|
||||
|
||||
u_char id_bcd[4];
|
||||
u_char tx_cnt;
|
||||
u_char status;
|
||||
u_char cnt1_type;
|
||||
u_char cnt2_type;
|
||||
u_char cnt1_val[4];
|
||||
u_char cnt2_val[4];
|
||||
unsigned char id_bcd[4];
|
||||
unsigned char tx_cnt;
|
||||
unsigned char status;
|
||||
unsigned char cnt1_type;
|
||||
unsigned char cnt2_type;
|
||||
unsigned char cnt1_val[4];
|
||||
unsigned char cnt2_val[4];
|
||||
|
||||
} mbus_data_fixed;
|
||||
|
||||
@ -268,10 +268,10 @@ typedef struct _mbus_data_secondary_address {
|
||||
// ex
|
||||
// 14 49 10 01 10 57 01 06
|
||||
|
||||
u_char id_bcd[4]; // 14 49 10 01
|
||||
u_char manufacturer[2]; // 10 57
|
||||
u_char version; // 01
|
||||
u_char medium; // 06
|
||||
unsigned char id_bcd[4]; // 14 49 10 01
|
||||
unsigned char manufacturer[2]; // 10 57
|
||||
unsigned char version; // 01
|
||||
unsigned char medium; // 06
|
||||
|
||||
} mbus_data_secondary_address;
|
||||
|
||||
@ -506,17 +506,17 @@ typedef struct _mbus_data_secondary_address {
|
||||
//
|
||||
// Event callback functions
|
||||
//
|
||||
extern void (*_mbus_recv_event)(u_char src_type, const char *buff, size_t len);
|
||||
extern void (*_mbus_send_event)(u_char src_type, const char *buff, size_t len);
|
||||
extern void (*_mbus_recv_event)(unsigned char src_type, const char *buff, size_t len);
|
||||
extern void (*_mbus_send_event)(unsigned char src_type, const char *buff, size_t len);
|
||||
|
||||
void mbus_dump_recv_event(u_char src_type, const char *buff, size_t len);
|
||||
void mbus_dump_send_event(u_char src_type, const char *buff, size_t len);
|
||||
void mbus_dump_recv_event(unsigned char src_type, const char *buff, size_t len);
|
||||
void mbus_dump_send_event(unsigned char src_type, const char *buff, size_t len);
|
||||
|
||||
//
|
||||
// Event register functions
|
||||
//
|
||||
void mbus_register_recv_event(void (*event)(u_char src_type, const char *buff, size_t len));
|
||||
void mbus_register_send_event(void (*event)(u_char src_type, const char *buff, size_t len));
|
||||
void mbus_register_recv_event(void (*event)(unsigned char src_type, const char *buff, size_t len));
|
||||
void mbus_register_send_event(void (*event)(unsigned char src_type, const char *buff, size_t len));
|
||||
|
||||
//
|
||||
// variable length records
|
||||
@ -543,14 +543,14 @@ int mbus_frame_calc_length (mbus_frame *frame);
|
||||
//
|
||||
// Parse/Pack to bin
|
||||
//
|
||||
int mbus_parse(mbus_frame *frame, u_char *data, size_t data_size);
|
||||
int mbus_parse(mbus_frame *frame, unsigned char *data, size_t data_size);
|
||||
|
||||
int mbus_data_fixed_parse (mbus_frame *frame, mbus_data_fixed *data);
|
||||
int mbus_data_variable_parse(mbus_frame *frame, mbus_data_variable *data);
|
||||
|
||||
int mbus_frame_data_parse (mbus_frame *frame, mbus_frame_data *data);
|
||||
|
||||
int mbus_frame_pack(mbus_frame *frame, u_char *data, size_t data_size);
|
||||
int mbus_frame_pack(mbus_frame *frame, unsigned char *data, size_t data_size);
|
||||
|
||||
int mbus_frame_verify(mbus_frame *frame);
|
||||
|
||||
@ -576,7 +576,7 @@ mbus_slave_data *mbus_slave_data_get(size_t i);
|
||||
//
|
||||
// XML generating functions
|
||||
//
|
||||
void mbus_str_xml_encode(u_char *dst, const u_char *src, size_t max_len);
|
||||
void mbus_str_xml_encode(unsigned char *dst, const unsigned char *src, size_t max_len);
|
||||
char *mbus_data_xml(mbus_frame_data *data);
|
||||
char *mbus_data_variable_xml(mbus_data_variable *data);
|
||||
char *mbus_data_fixed_xml(mbus_data_fixed *data);
|
||||
@ -607,37 +607,37 @@ void mbus_hex_dump(const char *label, const char *buff, size_t len);
|
||||
//
|
||||
// data encode/decode functions
|
||||
//
|
||||
int mbus_data_manufacturer_encode(u_char *m_data, u_char *m_code);
|
||||
const char *mbus_decode_manufacturer(u_char byte1, u_char byte2);
|
||||
int mbus_data_manufacturer_encode(unsigned char *m_data, unsigned char *m_code);
|
||||
const char *mbus_decode_manufacturer(unsigned char byte1, unsigned char byte2);
|
||||
const char *mbus_data_product_name(mbus_data_variable_header *header);
|
||||
|
||||
int mbus_data_bcd_encode(u_char *bcd_data, size_t bcd_data_size, int value);
|
||||
int mbus_data_int_encode(u_char *int_data, size_t int_data_size, int value);
|
||||
int mbus_data_bcd_encode(unsigned char *bcd_data, size_t bcd_data_size, int value);
|
||||
int mbus_data_int_encode(unsigned char *int_data, size_t int_data_size, int value);
|
||||
|
||||
long long mbus_data_bcd_decode(u_char *bcd_data, size_t bcd_data_size);
|
||||
int mbus_data_int_decode(u_char *int_data, size_t int_data_size);
|
||||
long mbus_data_long_decode(u_char *int_data, size_t int_data_size);
|
||||
long long mbus_data_long_long_decode(u_char *int_data, size_t int_data_size);
|
||||
long long mbus_data_bcd_decode(unsigned char *bcd_data, size_t bcd_data_size);
|
||||
int mbus_data_int_decode(unsigned char *int_data, size_t int_data_size);
|
||||
long mbus_data_long_decode(unsigned char *int_data, size_t int_data_size);
|
||||
long long mbus_data_long_long_decode(unsigned char *int_data, size_t int_data_size);
|
||||
|
||||
float mbus_data_float_decode(u_char *float_data);
|
||||
float mbus_data_float_decode(unsigned char *float_data);
|
||||
|
||||
void mbus_data_tm_decode(struct tm *t, u_char *t_data, size_t t_data_size);
|
||||
void mbus_data_tm_decode(struct tm *t, unsigned char *t_data, size_t t_data_size);
|
||||
|
||||
void mbus_data_str_decode(u_char *dst, const u_char *src, size_t len);
|
||||
void mbus_data_str_decode(unsigned char *dst, const unsigned char *src, size_t len);
|
||||
|
||||
void mbus_data_bin_decode(u_char *dst, const u_char *src, size_t len, size_t max_len);
|
||||
void mbus_data_bin_decode(unsigned char *dst, const unsigned char *src, size_t len, size_t max_len);
|
||||
|
||||
const char *mbus_data_fixed_medium(mbus_data_fixed *data);
|
||||
const char *mbus_data_fixed_unit(int medium_unit_byte);
|
||||
const char *mbus_data_variable_medium_lookup(u_char medium);
|
||||
const char *mbus_data_variable_medium_lookup(unsigned char medium);
|
||||
const char *mbus_unit_prefix(int exp);
|
||||
|
||||
const char *mbus_data_error_lookup(int error);
|
||||
|
||||
const char *mbus_vib_unit_lookup(mbus_value_information_block *vib);
|
||||
const char *mbus_vif_unit_lookup(u_char vif);
|
||||
const char *mbus_vif_unit_lookup(unsigned char vif);
|
||||
|
||||
u_char mbus_dif_datalength_lookup(u_char dif);
|
||||
unsigned char mbus_dif_datalength_lookup(unsigned char dif);
|
||||
|
||||
char *mbus_frame_get_secondary_address(mbus_frame *frame);
|
||||
int mbus_frame_select_secondary_pack(mbus_frame *frame, char *address);
|
||||
|
@ -98,11 +98,12 @@ int
|
||||
mbus_serial_set_baudrate(mbus_handle *handle, int baudrate)
|
||||
{
|
||||
speed_t speed;
|
||||
mbus_serial_data *serial_data;
|
||||
|
||||
if (handle == NULL)
|
||||
return -1;
|
||||
|
||||
mbus_serial_data *serial_data = (mbus_serial_data *) handle->auxdata;
|
||||
serial_data = (mbus_serial_data *) handle->auxdata;
|
||||
|
||||
switch (baudrate)
|
||||
{
|
||||
@ -207,7 +208,7 @@ mbus_serial_data_free(mbus_handle *handle)
|
||||
int
|
||||
mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame)
|
||||
{
|
||||
u_char buff[PACKET_BUFF_SIZE];
|
||||
unsigned char buff[PACKET_BUFF_SIZE];
|
||||
int len, ret;
|
||||
|
||||
if (handle == NULL || frame == NULL)
|
||||
|
@ -124,7 +124,7 @@ mbus_tcp_disconnect(mbus_handle *handle)
|
||||
int
|
||||
mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame)
|
||||
{
|
||||
u_char buff[PACKET_BUFF_SIZE];
|
||||
unsigned char buff[PACKET_BUFF_SIZE];
|
||||
int len, ret;
|
||||
|
||||
if (handle == NULL || frame == NULL)
|
||||
|
@ -8,7 +8,7 @@
|
||||
//
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
#include <mbus/mbus-protocol.h>
|
||||
#include "mbus-protocol.h"
|
||||
|
||||
//
|
||||
//
|
||||
|
@ -33,10 +33,10 @@
|
||||
#ifndef _MBUS_H_
|
||||
#define _MBUS_H_
|
||||
|
||||
#include <mbus/mbus-protocol.h>
|
||||
#include <mbus/mbus-protocol-aux.h>
|
||||
#include <mbus/mbus-tcp.h>
|
||||
#include <mbus/mbus-serial.h>
|
||||
#include "mbus-protocol.h"
|
||||
#include "mbus-protocol-aux.h"
|
||||
#include "mbus-tcp.h"
|
||||
#include "mbus-serial.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -19,7 +19,7 @@ main(int argc, char *argv[])
|
||||
{
|
||||
FILE *fp = NULL;
|
||||
size_t buff_len, len;
|
||||
u_char buf[1024];
|
||||
unsigned char buf[1024];
|
||||
mbus_frame reply;
|
||||
mbus_frame_data frame_data;
|
||||
char *xml_result = NULL;
|
||||
|
@ -20,7 +20,7 @@ main(int argc, char *argv[])
|
||||
FILE *fp = NULL;
|
||||
size_t buff_len, len;
|
||||
int result;
|
||||
u_char raw_buff[4096], buff[4096];
|
||||
unsigned char raw_buff[4096], buff[4096];
|
||||
mbus_frame reply;
|
||||
mbus_frame_data frame_data;
|
||||
char *xml_result = NULL;
|
||||
@ -80,7 +80,7 @@ main(int argc, char *argv[])
|
||||
}
|
||||
printf("%s", xml_result);
|
||||
free(xml_result);
|
||||
|
||||
mbus_data_record_free(frame_data.data_var.record);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ hex_to_bin(char *buff, size_t buff_size, char *hex_buff)
|
||||
while ((ptr != endptr) && i < buff_size-1)
|
||||
{
|
||||
ptr = endptr;
|
||||
buff[i++] = (u_char)strtol(ptr, (char **)&endptr, 16);
|
||||
buff[i++] = (unsigned char)strtol(ptr, (char **)&endptr, 16);
|
||||
}
|
||||
|
||||
return i;
|
||||
|
Loading…
x
Reference in New Issue
Block a user