Merge pull request #55 from lategoodbye/master

Improve multi reply binaries
This commit is contained in:
Robert Johansson 2013-07-01 07:09:18 -07:00
commit 655e3c897d
3 changed files with 120 additions and 41 deletions

View File

@ -22,9 +22,9 @@ B<mbus-serial-request-data> [-d] [-b BAUDRATE] device mbus-address
B<mbus-tcp-request-data> [-d] host port mbus-address B<mbus-tcp-request-data> [-d] host port mbus-address
B<mbus-serial-request-data-multi-reply> [-d] [-b BAUDRATE] device mbus-address B<mbus-serial-request-data-multi-reply> [-d] [-b BAUDRATE] [-f FRAMES] device mbus-address
B<mbus-tcp-request-data-multi-reply> [-d] host port mbus-address B<mbus-tcp-request-data-multi-reply> [-d] [-f FRAMES] host port mbus-address
B<mbus-serial-select-secondary> [-b BAUDRATE] device secondary-mbus-address B<mbus-serial-select-secondary> [-b BAUDRATE] device secondary-mbus-address
@ -47,8 +47,8 @@ B<mbus-serial-request-data>, B<mbus-tcp-request-data> - read data from given dev
Supports both primary and secondary address types. Supports both primary and secondary address types.
B<mbus-serial-request-data-multi-reply>, B<mbus-tcp-request-data-multi-reply> - read B<mbus-serial-request-data-multi-reply>, B<mbus-tcp-request-data-multi-reply> - read
data from given device supporting multi-telegram (up to 16 frames) reply. Can be also data from given device supporting multi-telegram (default: up to 16 frames) reply.
used for single telegram as mbus-serial-request-data or mbus-tcp-request-data. Can be also used for single telegram as mbus-serial-request-data or mbus-tcp-request-data.
Supports both primary and secondary address types. Supports both primary and secondary address types.
B<mbus-serial-select-secondary>, B<mbus-tcp-select-secondary> - perform single secondary B<mbus-serial-select-secondary>, B<mbus-tcp-select-secondary> - perform single secondary
@ -79,6 +79,10 @@ the response is erroneous, the MBus master can send a retransmissions.
libmbus supports the following range of retransmission: 0 until 9 libmbus supports the following range of retransmission: 0 until 9
=item B<-f> I<FRAMES>
Maximum response frames.
=item B<-d> =item B<-d>
Enable debugging messages. Enable debugging messages.
@ -95,6 +99,7 @@ For TCP communciation, the host which represents the MBus (gateway).
=item B<port> =item B<port>
For TCP communciation, the port on the host which represents the MBus (gateway). For TCP communciation, the port on the host which represents the MBus (gateway).
An integer between 0 and 65535.
=item B<address> =item B<address>

View File

@ -20,7 +20,7 @@ static int debug = 0;
// //
// init slave to get really the beginning of the records // init slave to get really the beginning of the records
// //
int static int
init_slaves(mbus_handle *handle) init_slaves(mbus_handle *handle)
{ {
if (debug) if (debug)
@ -49,7 +49,7 @@ init_slaves(mbus_handle *handle)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Wrapper for argument parsing errors // Wrapper for argument parsing errors
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
int static void
parse_abort(char **argv) parse_abort(char **argv)
{ {
fprintf(stderr, "usage: %s [-d] [-b BAUDRATE] [-f FRAMES] device mbus-address\n", argv[0]); fprintf(stderr, "usage: %s [-d] [-b BAUDRATE] [-f FRAMES] device mbus-address\n", argv[0]);
@ -65,8 +65,7 @@ parse_abort(char **argv)
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
mbus_frame *frame, reply; mbus_frame reply;
mbus_frame_data reply_data;
mbus_handle *handle = NULL; mbus_handle *handle = NULL;
char *device, *addr_str, *xml_result; char *device, *addr_str, *xml_result;
@ -75,7 +74,6 @@ main(int argc, char **argv)
int maxframes = MAXFRAMES; int maxframes = MAXFRAMES;
memset((void *)&reply, 0, sizeof(mbus_frame)); memset((void *)&reply, 0, sizeof(mbus_frame));
memset((void *)&reply_data, 0, sizeof(mbus_frame_data));
int c; int c;
for (c=1; c<argc-2; c++) for (c=1; c<argc-2; c++)
{ {
@ -121,17 +119,22 @@ main(int argc, char **argv)
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr,"Failed to setup connection to M-bus gateway\n"); fprintf(stderr,"Failed to setup connection to M-bus gateway\n");
mbus_context_free(handle);
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");
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
if (init_slaves(handle) == 0) if (init_slaves(handle) == 0)
{ {
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
@ -146,16 +149,22 @@ main(int argc, char **argv)
if (ret == MBUS_PROBE_COLLISION) if (ret == MBUS_PROBE_COLLISION)
{ {
fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str); fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str);
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
else if (ret == MBUS_PROBE_NOTHING) else if (ret == MBUS_PROBE_NOTHING)
{ {
fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str); fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str);
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
else if (ret == MBUS_PROBE_ERROR) else if (ret == MBUS_PROBE_ERROR)
{ {
fprintf(stderr, "%s: Error: Failed to select secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str); fprintf(stderr, "%s: Error: Failed to select secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str);
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
// else MBUS_PROBE_SINGLE // else MBUS_PROBE_SINGLE
@ -173,6 +182,9 @@ main(int argc, char **argv)
if (mbus_sendrecv_request(handle, address, &reply, maxframes) != 0) if (mbus_sendrecv_request(handle, address, &reply, maxframes) != 0)
{ {
fprintf(stderr, "Failed to send/receive M-Bus request.\n"); fprintf(stderr, "Failed to send/receive M-Bus request.\n");
mbus_disconnect(handle);
mbus_context_free(handle);
mbus_frame_free(reply.next);
return 1; return 1;
} }
@ -190,6 +202,9 @@ main(int argc, char **argv)
if ((xml_result = mbus_frame_xml(&reply)) == NULL) if ((xml_result = mbus_frame_xml(&reply)) == NULL)
{ {
fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str()); fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str());
mbus_disconnect(handle);
mbus_context_free(handle);
mbus_frame_free(reply.next);
return 1; return 1;
} }
@ -198,6 +213,8 @@ main(int argc, char **argv)
mbus_disconnect(handle); mbus_disconnect(handle);
mbus_context_free(handle); mbus_context_free(handle);
mbus_frame_free(reply.next);
return 0; return 0;
} }

View File

@ -15,43 +15,89 @@
static int debug = 0; static int debug = 0;
// Default value for the maximum number of frames
#define MAXFRAMES 16
//
// init slave to get really the beginning of the records
//
static 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;
}
//------------------------------------------------------------------------------
// Wrapper for argument parsing errors
//------------------------------------------------------------------------------
static void
parse_abort(char **argv)
{
fprintf(stderr, "usage: %s [-d] [-f FRAMES] host port mbus-address\n", argv[0]);
fprintf(stderr, " optional flag -d for debug printout\n");
fprintf(stderr, " optional flag -f for selecting the maximal number of frames\n");
exit(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)
{ {
mbus_frame *frame, reply; mbus_frame reply;
mbus_frame_data reply_data;
mbus_handle *handle = NULL; mbus_handle *handle = NULL;
char *host, *addr_str, matching_addr[16], *xml_result; char *host, *addr_str, *xml_result;
int address; int address;
long port; long port;
int maxframes = MAXFRAMES;
int c = 0;
memset((void *)&reply, 0, sizeof(mbus_frame)); memset((void *)&reply, 0, sizeof(mbus_frame));
memset((void *)&reply_data, 0, sizeof(mbus_frame_data));
for (c=1; c<argc-3; c++)
if (argc == 4)
{ {
host = argv[1]; if (strcmp(argv[c], "-d") == 0)
port = atol(argv[2]); {
addr_str = argv[3]; debug = 1;
debug = 0; }
else if (strcmp(argv[c], "-f") == 0)
{
c++;
maxframes = atoi(argv[c]);
}
else
{
parse_abort(argv);
}
} }
else if (argc == 5 && strcmp(argv[1], "-d") == 0) if (c > argc-3)
{ {
host = argv[2]; parse_abort(argv);
port = atol(argv[3]);
addr_str = argv[4];
debug = 1;
}
else
{
fprintf(stderr, "usage: %s [-d] host port mbus-address\n", argv[0]);
fprintf(stderr, " optional flag -d for debug printout\n");
return 0;
} }
host = argv[c];
port = atol(argv[c+1]);
addr_str = argv[c+2];
if ((port < 0) || (port > 0xFFFF)) if ((port < 0) || (port > 0xFFFF))
{ {
@ -73,18 +119,15 @@ main(int argc, char **argv)
if (mbus_connect(handle) == -1) if (mbus_connect(handle) == -1)
{ {
fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); fprintf(stderr,"Failed to setup connection to M-bus gateway\n");
mbus_context_free(handle);
return 1; return 1;
} }
// if (init_slaves(handle) == 0)
// init slave to get really the beginning of the records
//
if (debug)
printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__);
if (mbus_send_ping_frame(handle, MBUS_ADDRESS_BROADCAST_NOREPLY, 1) == -1)
{ {
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
@ -99,20 +142,26 @@ main(int argc, char **argv)
if (ret == MBUS_PROBE_COLLISION) if (ret == MBUS_PROBE_COLLISION)
{ {
fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str); fprintf(stderr, "%s: Error: The address mask [%s] matches more than one device.\n", __PRETTY_FUNCTION__, addr_str);
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
else if (ret == MBUS_PROBE_NOTHING) else if (ret == MBUS_PROBE_NOTHING)
{ {
fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str); fprintf(stderr, "%s: Error: The selected secondary address does not match any device [%s].\n", __PRETTY_FUNCTION__, addr_str);
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
else if (ret == MBUS_PROBE_ERROR) else if (ret == MBUS_PROBE_ERROR)
{ {
fprintf(stderr, "%s: Error: Failed to select secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str); fprintf(stderr, "%s: Error: Failed to select secondary address [%s].\n", __PRETTY_FUNCTION__, addr_str);
mbus_disconnect(handle);
mbus_context_free(handle);
return 1; return 1;
} }
// else MBUS_PROBE_SINGLE // else MBUS_PROBE_SINGLE
address = MBUS_ADDRESS_NETWORK_LAYER; address = MBUS_ADDRESS_NETWORK_LAYER;
} }
else else
@ -123,9 +172,12 @@ main(int argc, char **argv)
// instead of the send and recv, use this sendrecv function that // instead of the send and recv, use this sendrecv function that
// takes care of the possibility of multi-telegram replies (limit = 16 frames) // takes care of the possibility of multi-telegram replies (limit = 16 frames)
if (mbus_sendrecv_request(handle, address, &reply, 16) != 0) if (mbus_sendrecv_request(handle, address, &reply, maxframes) != 0)
{ {
fprintf(stderr, "Failed to send/receive M-Bus request.\n"); fprintf(stderr, "Failed to send/receive M-Bus request.\n");
mbus_disconnect(handle);
mbus_context_free(handle);
mbus_frame_free(reply.next);
return 1; return 1;
} }
@ -143,6 +195,9 @@ main(int argc, char **argv)
if ((xml_result = mbus_frame_xml(&reply)) == NULL) if ((xml_result = mbus_frame_xml(&reply)) == NULL)
{ {
fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str()); fprintf(stderr, "Failed to generate XML representation of MBUS frames: %s\n", mbus_error_str());
mbus_disconnect(handle);
mbus_context_free(handle);
mbus_frame_free(reply.next);
return 1; return 1;
} }
@ -151,6 +206,8 @@ main(int argc, char **argv)
mbus_disconnect(handle); mbus_disconnect(handle);
mbus_context_free(handle); mbus_context_free(handle);
mbus_frame_free(reply.next);
return 0; return 0;
} }