Merge pull request #21 from lategoodbye/master

Unified slave init and allocation bugfixes
This commit is contained in:
Robert Johansson 2012-09-04 17:18:27 -07:00
commit d05b7b6582
11 changed files with 155 additions and 127 deletions

View File

@ -19,6 +19,35 @@
static int debug = 0; static int debug = 0;
//
// init slave to get really the beginning of the records
//
int
init_slaves(mbus_handle *handle)
{
if (debug)
printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__);
if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1)
{
return 0;
}
//
// resend SND_NKE, maybe the first get lost
//
if (debug)
printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__);
if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1)
{
return 0;
}
return 1;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Scan for devices using secondary addressing. // Scan for devices using secondary addressing.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -91,51 +120,10 @@ main(int argc, char **argv)
return 1; return 1;
} }
// if (init_slaves(handle) == 0)
// init slave to get really the beginning of the records
//
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
if (frame == NULL)
{ {
fprintf(stderr, "Failed to allocate mbus frame.\n");
return 1; return 1;
} }
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (debug)
printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1)
{
fprintf(stderr, "Failed to send mbus frame.\n");
mbus_frame_free(frame);
return 1;
}
mbus_purge_frames(handle);
//
// resend SND_NKE, maybe the first get lost
//
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (debug)
printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1)
{
fprintf(stderr, "Failed to send mbus frame.\n");
mbus_frame_free(frame);
return 1;
}
mbus_purge_frames(handle);
if (strlen(addr_str) == 16) if (strlen(addr_str) == 16)
{ {

View File

@ -19,13 +19,43 @@
static int debug = 0; static int debug = 0;
//
// init slave to get really the beginning of the records
//
int
init_slaves(mbus_handle *handle)
{
if (debug)
printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__);
if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1)
{
return 0;
}
//
// resend SND_NKE, maybe the first get lost
//
if (debug)
printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__);
if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1)
{
return 0;
}
return 1;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Scan for devices using secondary addressing. // Scan for devices using secondary addressing.
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *device, *addr_mask; char *device, *addr_mask = NULL;
int baudrate = 9600; int baudrate = 9600;
mbus_handle *handle = NULL; mbus_handle *handle = NULL;
mbus_frame *frame = NULL, reply; mbus_frame *frame = NULL, reply;
@ -95,28 +125,38 @@ main(int argc, char **argv)
mbus_register_send_event(&mbus_dump_send_event); mbus_register_send_event(&mbus_dump_send_event);
mbus_register_recv_event(&mbus_dump_recv_event); mbus_register_recv_event(&mbus_dump_recv_event);
} }
if (addr_mask == NULL)
{
fprintf(stderr, "Failed to allocate address mask.\n");
return 1;
}
if (strlen(addr_mask) != 16) if (strlen(addr_mask) != 16)
{ {
fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n"); fprintf(stderr, "Misformatted secondary address mask. Must be 16 character HEX number.\n");
free(addr_mask);
return 1; return 1;
} }
if ((handle = mbus_context_serial(device)) == NULL) if ((handle = mbus_context_serial(device)) == NULL)
{ {
fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str());
free(addr_mask);
return 1; return 1;
} }
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
printf("Failed to setup connection to M-bus gateway\n"); printf("Failed to setup connection to M-bus gateway\n");
free(addr_mask);
return 1; return 1;
} }
if (mbus_serial_set_baudrate(handle, baudrate) == -1) if (mbus_serial_set_baudrate(handle, baudrate) == -1)
{ {
fprintf(stderr, "Failed to set baud rate.\n"); fprintf(stderr, "Failed to set baud rate.\n");
free(addr_mask);
return 1; return 1;
} }
@ -126,36 +166,16 @@ main(int argc, char **argv)
if (frame == NULL) if (frame == NULL)
{ {
fprintf(stderr, "Failed to allocate mbus frame.\n"); fprintf(stderr, "Failed to allocate mbus frame.\n");
free(addr_mask);
return 1; return 1;
} }
// if (init_slaves(handle) == 0)
// init slaves
//
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_NETWORK_LAYER;
if (mbus_send_frame(handle, frame) == -1)
{ {
fprintf(stderr, "Failed to send SND_NKE #1.\n"); free(addr_mask);
mbus_frame_free(frame);
return 1; return 1;
} }
(void) mbus_recv_frame(handle, &reply);
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
if (mbus_send_frame(handle, frame) == -1)
{
fprintf(stderr, "Failed to send SND_NKE #2.\n");
mbus_frame_free(frame);
return 1;
}
(void) mbus_recv_frame(handle, &reply);
mbus_scan_2nd_address_range(handle, 0, addr_mask); mbus_scan_2nd_address_range(handle, 0, addr_mask);
mbus_disconnect(handle); mbus_disconnect(handle);

View File

@ -95,7 +95,7 @@ main(int argc, char **argv)
fflush(stdout); fflush(stdout);
} }
if (mbus_send_ping_frame(handle, address) == -1) if (mbus_send_ping_frame(handle, address, 0) == -1)
{ {
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str()); printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -26,7 +26,7 @@ main(int argc, char **argv)
{ {
mbus_handle *handle; mbus_handle *handle;
mbus_frame reply; mbus_frame reply;
char *device, *addr; char *device, *addr = NULL;
int ret, baudrate = 9600; int ret, baudrate = 9600;
if (argc == 3) if (argc == 3)
@ -46,6 +46,12 @@ main(int argc, char **argv)
fprintf(stderr, " optional flag -b for selecting baudrate\n"); fprintf(stderr, " optional flag -b for selecting baudrate\n");
return 0; return 0;
} }
if (addr == NULL)
{
fprintf(stderr, "Failed to allocate address.\n");
return 1;
}
if (strlen(addr) != 16) if (strlen(addr) != 16)
{ {

View File

@ -77,29 +77,13 @@ main(int argc, char **argv)
// //
// init slave to get really the beginning of the records // init slave to get really the beginning of the records
// //
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
if (frame == NULL)
{
fprintf(stderr, "Failed to allocate mbus frame.\n");
return 1;
}
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
if (debug) if (debug)
printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1) if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1)
{ {
fprintf(stderr, "Failed to send mbus frame.\n");
mbus_frame_free(frame);
return 1; return 1;
} }
mbus_recv_frame(handle, &reply);
if (strlen(addr_str) == 16) if (strlen(addr_str) == 16)
{ {

View File

@ -25,7 +25,7 @@ static int debug = 0;
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
char *host, *addr_mask; char *host, *addr_mask = NULL;
int port; int port;
mbus_handle *handle = NULL; mbus_handle *handle = NULL;
mbus_frame *frame = NULL, reply; mbus_frame *frame = NULL, reply;
@ -50,6 +50,12 @@ main(int argc, char **argv)
{ {
addr_mask = strdup("FFFFFFFFFFFFFFFF"); addr_mask = strdup("FFFFFFFFFFFFFFFF");
} }
if (addr_mask == NULL)
{
fprintf(stderr, "Failed to allocate address mask.\n");
return 1;
}
if (strlen(addr_mask) != 16) if (strlen(addr_mask) != 16)
{ {
@ -68,42 +74,31 @@ main(int argc, char **argv)
fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); fprintf(stderr, "Failed to setup connection to M-bus gateway\n");
return 1; return 1;
} }
frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
if (frame == NULL)
{
fprintf(stderr, "Failed to allocate mbus frame.\n");
return 1;
}
// //
// init slaves // init slaves
// //
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; if (debug)
frame->address = MBUS_ADDRESS_NETWORK_LAYER; printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1) if (mbus_send_ping_frame(handle, MBUS_ADDRESS_NETWORK_LAYER, 1) == -1)
{ {
fprintf(stderr, "Failed to send SND_NKE #1.\n"); free(addr_mask);
mbus_frame_free(frame);
return 1; return 1;
} }
(void) mbus_recv_frame(handle, &reply); //
// resend SND_NKE, maybe the first get lost
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; //
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; if (debug)
printf("%s: debug: sending init frame #2\n", __PRETTY_FUNCTION__);
if (mbus_send_frame(handle, frame) == -1)
if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1)
{ {
fprintf(stderr, "Failed to send SND_NKE #2.\n"); free(addr_mask);
mbus_frame_free(frame);
return 1; return 1;
} }
(void) mbus_recv_frame(handle, &reply);
mbus_scan_2nd_address_range(handle, 0, addr_mask); mbus_scan_2nd_address_range(handle, 0, addr_mask);
mbus_disconnect(handle); mbus_disconnect(handle);

View File

@ -78,7 +78,7 @@ main(int argc, char **argv)
fflush(stdout); fflush(stdout);
} }
if (mbus_send_ping_frame(handle, address) == -1) if (mbus_send_ping_frame(handle, address, 0) == -1)
{ {
printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str()); printf("Scan failed. Could not send ping frame: %s\n", mbus_error_str());
return 1; return 1;

View File

@ -26,7 +26,7 @@ main(int argc, char **argv)
{ {
mbus_handle *handle; mbus_handle *handle;
mbus_frame reply; mbus_frame reply;
char *host, *addr; char *host, *addr = NULL;
int port, ret; int port, ret;
if (argc != 4) if (argc != 4)
@ -37,9 +37,14 @@ main(int argc, char **argv)
host = argv[1]; host = argv[1];
port = atoi(argv[2]); port = atoi(argv[2]);
addr = strdup(argv[3]);
if ((addr = strdup(argv[3])) == NULL)
{
fprintf(stderr, "Failed to allocate address.\n");
return 1;
}
if (strlen(argv[3]) != 16) if (strlen(addr) != 16)
{ {
printf("Misformatted secondary address. Must be 16 character HEX number.\n"); printf("Misformatted secondary address. Must be 16 character HEX number.\n");
return 1; return 1;

View File

@ -791,7 +791,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
switch (record->drh.dib.dif & 0x0F) switch (record->drh.dib.dif & 0x0F)
{ {
case 0x00: /* no data */ case 0x00: /* no data */
*value_out_str = (char*) malloc(1); if ((*value_out_str = (char*) malloc(1)) == NULL)
{
MBUS_ERROR("Unable to allocate memory");
return -1;
}
*value_out_str_size = 0; *value_out_str_size = 0;
result = 0; result = 0;
break; break;
@ -806,7 +810,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
if (vif == 0x6C) if (vif == 0x6C)
{ {
mbus_data_tm_decode(&time, record->data, 2); mbus_data_tm_decode(&time, record->data, 2);
*value_out_str = (char*) malloc(11); if ((*value_out_str = (char*) malloc(11)) == NULL)
{
MBUS_ERROR("Unable to allocate memory");
return -1;
}
*value_out_str_size = snprintf(*value_out_str, 11, "%04d-%02d-%02d", *value_out_str_size = snprintf(*value_out_str, 11, "%04d-%02d-%02d",
(time.tm_year + 2000), (time.tm_year + 2000),
(time.tm_mon + 1), (time.tm_mon + 1),
@ -834,7 +842,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
((record->drh.vib.vif == 0xFD) && (vife == 0x70))) ((record->drh.vib.vif == 0xFD) && (vife == 0x70)))
{ {
mbus_data_tm_decode(&time, record->data, 4); mbus_data_tm_decode(&time, record->data, 4);
*value_out_str = (char*) malloc(20); if ((*value_out_str = (char*) malloc(20)) == NULL)
{
MBUS_ERROR("Unable to allocate memory");
return -1;
}
*value_out_str_size = snprintf(*value_out_str, 20, "%04d-%02d-%02dT%02d:%02d:%02d", *value_out_str_size = snprintf(*value_out_str, 20, "%04d-%02d-%02dT%02d:%02d:%02d",
(time.tm_year + 2000), (time.tm_year + 2000),
(time.tm_mon + 1), (time.tm_mon + 1),
@ -888,7 +900,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
case 0x0D: /* variable length */ case 0x0D: /* variable length */
{ {
if (record->data_len <= 0xBF) { if (record->data_len <= 0xBF) {
*value_out_str = (char*) malloc(record->data_len + 1); if ((*value_out_str = (char*) malloc(record->data_len + 1)) == NULL)
{
MBUS_ERROR("Unable to allocate memory");
return -1;
}
*value_out_str_size = record->data_len; *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((u_char*)(*value_out_str), record->data, record->data_len);
result = 0; result = 0;
@ -905,7 +921,11 @@ int mbus_variable_value_decode(mbus_data_record *record, double *value_out_real,
break; break;
case 0x0F: /* Special functions */ case 0x0F: /* Special functions */
*value_out_str = (char*) malloc(3 * record->data_len + 1); if ((*value_out_str = (char*) malloc(3 * record->data_len + 1)) == NULL)
{
MBUS_ERROR("Unable to allocate memory");
return -1;
}
*value_out_str_size = 3 * record->data_len; *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((u_char*)(*value_out_str), record->data, record->data_len, (3 * record->data_len + 1));
result = 0; result = 0;
@ -1766,10 +1786,10 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// send a data request packet to from master to slave // send a data request packet to from master to slave and optional purge response
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int int
mbus_send_ping_frame(mbus_handle *handle, int address) mbus_send_ping_frame(mbus_handle *handle, int address, char purge_response)
{ {
int retval = 0; int retval = 0;
mbus_frame *frame; mbus_frame *frame;
@ -1788,8 +1808,14 @@ mbus_send_ping_frame(mbus_handle *handle, int address)
if (mbus_send_frame(handle, frame) == -1) if (mbus_send_frame(handle, frame) == -1)
{ {
MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__); MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__);
retval = -1; mbus_frame_free(frame);
} return -1;
}
if (purge_response)
{
mbus_purge_frames(handle);
}
mbus_frame_free(frame); mbus_frame_free(frame);
return retval; return retval;

View File

@ -265,12 +265,13 @@ int mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, i
/** /**
* Sends ping frame to given slave using "unified" handle * Sends ping frame to given slave using "unified" handle
* *
* @param handle Initialized handle * @param handle Initialized handle
* @param address Address (0-255) * @param address Address (0-255)
* @param purge_response Response flag (=0 don't receive response, >0 purge response)
* *
* @return Zero when successful. * @return Zero when successful.
*/ */
int mbus_send_ping_frame(mbus_handle *handle, int address); int mbus_send_ping_frame(mbus_handle *handle, int address, char purge_response);
/** /**
* Select slave by secondary address using "unified" handle * Select slave by secondary address using "unified" handle

View File

@ -897,6 +897,9 @@ mbus_data_product_name(mbus_data_variable_header *header)
{ {
switch (header->version) switch (header->version)
{ {
case 0x01:
strcpy(buff,"Kamstrup 382 (6850-005)");
break;
case 0x08: case 0x08:
strcpy(buff,"Kamstrup Multical 601"); strcpy(buff,"Kamstrup Multical 601");
break; break;