From 5d079254cc714864cc864ba9cb30a238f3784a52 Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Tue, 3 Jul 2012 14:35:07 +0200 Subject: [PATCH 01/10] Extended mbus_recv_frame() error codes, improved collision handling - handling TCP connection reset and read(2) timeout --- bin/mbus-serial-request-data.c | 2 +- bin/mbus-serial-scan.c | 13 ++---- bin/mbus-serial-select-secondary.c | 4 +- bin/mbus-serial-switch-baudrate.c | 2 +- bin/mbus-tcp-request-data.c | 6 +-- bin/mbus-tcp-scan.c | 17 ++------ bin/mbus-tcp-select-secondary.c | 4 +- mbus/mbus-protocol-aux.c | 36 ++++++++++------ mbus/mbus-protocol-aux.h | 9 ++++ mbus/mbus-tcp.c | 67 +++++++++++++----------------- 10 files changed, 78 insertions(+), 82 deletions(-) mode change 100644 => 100755 bin/mbus-serial-request-data.c mode change 100644 => 100755 bin/mbus-serial-scan.c mode change 100644 => 100755 bin/mbus-serial-select-secondary.c mode change 100644 => 100755 bin/mbus-serial-switch-baudrate.c mode change 100644 => 100755 bin/mbus-tcp-request-data.c mode change 100644 => 100755 bin/mbus-tcp-scan.c mode change 100644 => 100755 bin/mbus-tcp-select-secondary.c mode change 100644 => 100755 mbus/mbus-protocol-aux.c mode change 100644 => 100755 mbus/mbus-protocol-aux.h mode change 100644 => 100755 mbus/mbus-tcp.c diff --git a/bin/mbus-serial-request-data.c b/bin/mbus-serial-request-data.c old mode 100644 new mode 100755 index d60a777..219a49d --- a/bin/mbus-serial-request-data.c +++ b/bin/mbus-serial-request-data.c @@ -126,7 +126,7 @@ main(int argc, char **argv) } } - if (mbus_recv_frame(handle, &reply) == -1) + if (mbus_recv_frame(handle, &reply) != 0) { fprintf(stderr, "Failed to receive M-Bus response frame.\n"); return 1; diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c old mode 100644 new mode 100755 index 3d7c8bf..cfd325a --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -97,7 +97,7 @@ main(int argc, char **argv) ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { continue; } @@ -108,22 +108,15 @@ main(int argc, char **argv) if (ret == -2) { /* check for more data (collision) */ - while (mbus_recv_frame(handle, &reply) != -1); - + mbus_recv_frame_dummy(handle); printf("Collision at address %d\n", address); - continue; } if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ - while (mbus_recv_frame(handle, &reply) != -1) - { - ret = -2; - } - - if (ret == -2) + if (mbus_recv_frame_dummy(handle)) { printf("Collision at address %d\n", address); diff --git a/bin/mbus-serial-select-secondary.c b/bin/mbus-serial-select-secondary.c old mode 100644 new mode 100755 index a5b7067..6fe462b --- a/bin/mbus-serial-select-secondary.c +++ b/bin/mbus-serial-select-secondary.c @@ -73,7 +73,7 @@ main(int argc, char **argv) ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { printf("No reply from device with secondary address %s: %s\n", argv[2], mbus_error_str()); return 1; @@ -93,7 +93,7 @@ main(int argc, char **argv) return 1; } - if (mbus_recv_frame(handle, &reply) == -1) + if (mbus_recv_frame(handle, &reply) != 0) { printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str()); return 1; diff --git a/bin/mbus-serial-switch-baudrate.c b/bin/mbus-serial-switch-baudrate.c old mode 100644 new mode 100755 index 4a0d91e..249f5d5 --- a/bin/mbus-serial-switch-baudrate.c +++ b/bin/mbus-serial-switch-baudrate.c @@ -72,7 +72,7 @@ main(int argc, char **argv) ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { printf("No reply from device\n"); return 1; diff --git a/bin/mbus-tcp-request-data.c b/bin/mbus-tcp-request-data.c old mode 100644 new mode 100755 index 7b40f28..6024f4b --- a/bin/mbus-tcp-request-data.c +++ b/bin/mbus-tcp-request-data.c @@ -111,11 +111,11 @@ main(int argc, char **argv) } } - if (mbus_recv_frame(handle, &reply) == -1) + if (mbus_recv_frame(handle, &reply) != 0) { - fprintf(stderr, "Failed to receive M-Bus response frame.\n"); + fprintf(stderr, "Failed to receive M-Bus response frame: %s\n", mbus_error_str()); return 1; - } + } // // parse data and print in XML format diff --git a/bin/mbus-tcp-scan.c b/bin/mbus-tcp-scan.c old mode 100644 new mode 100755 index d6dcee4..4de525e --- a/bin/mbus-tcp-scan.c +++ b/bin/mbus-tcp-scan.c @@ -17,7 +17,6 @@ #include #include - //------------------------------------------------------------------------------ // Execution starts here: //------------------------------------------------------------------------------ @@ -81,7 +80,7 @@ main(int argc, char **argv) ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { continue; } @@ -92,28 +91,20 @@ main(int argc, char **argv) if (ret == -2) { /* check for more data (collision) */ - while (mbus_recv_frame(handle, &reply) != -1); - + mbus_recv_frame_dummy(handle); printf("Collision at address %d\n", address); - continue; } if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ - while (mbus_recv_frame(handle, &reply) != -1) - { - ret = -2; - } - - if (ret == -2) + if (mbus_recv_frame_dummy(handle)) { printf("Collision at address %d\n", address); - continue; } - + printf("Found a M-Bus device at address %d\n", address); } } diff --git a/bin/mbus-tcp-select-secondary.c b/bin/mbus-tcp-select-secondary.c old mode 100644 new mode 100755 index 712abe2..698ef9d --- a/bin/mbus-tcp-select-secondary.c +++ b/bin/mbus-tcp-select-secondary.c @@ -59,7 +59,7 @@ main(int argc, char **argv) ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { printf("No reply from device with secondary address %s: %s\n", argv[3], mbus_error_str()); return 1; @@ -79,7 +79,7 @@ main(int argc, char **argv) return 1; } - if (mbus_recv_frame(handle, &reply) == -1) + if (mbus_recv_frame(handle, &reply) != 0) { printf("Failed to recieve reply from selected secondary device: %s\n", mbus_error_str()); return 1; diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c old mode 100644 new mode 100755 index 2323bec..fa06bb5 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1407,6 +1407,24 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame) return result; } +int mbus_recv_frame_dummy(mbus_handle *handle) +{ + int err, received; + mbus_frame reply; + + received = 0; + while (1) + { + err = mbus_recv_frame(handle, &reply); + if (err != -2 && err != 0) + break; + + received = 1; + } + + return received; +} + int mbus_send_frame(mbus_handle * handle, mbus_frame *frame) { @@ -1601,7 +1619,7 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m if (debug) printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count); - if (mbus_recv_frame(handle, next_frame) == -1) + if (mbus_recv_frame(handle, next_frame) != 0) { MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); retval = 1; @@ -1743,7 +1761,7 @@ mbus_select_secondary_address(mbus_handle * handle, const char *mask) ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { return MBUS_PROBE_NOTHING; } @@ -1751,20 +1769,14 @@ mbus_select_secondary_address(mbus_handle * handle, const char *mask) if (ret == -2) { /* check for more data (collision) */ - while (mbus_recv_frame(handle, &reply) != -1); - + mbus_recv_frame_dummy(handle); return MBUS_PROBE_COLLISION; } if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ - while (mbus_recv_frame(handle, &reply) != -1) - { - ret = -2; - } - - if (ret == -2) + if (mbus_recv_frame_dummy(handle)) { return MBUS_PROBE_COLLISION; } @@ -1809,7 +1821,7 @@ mbus_probe_secondary_address(mbus_handle * handle, const char *mask, char *match ret = mbus_recv_frame(handle, &reply); - if (ret == -1) + if (ret == -3) { return MBUS_PROBE_NOTHING; } @@ -1905,7 +1917,7 @@ int mbus_read_slave(mbus_handle * handle, mbus_address *address, mbus_frame * re } } - if (mbus_recv_frame(handle, reply) == -1) + if (mbus_recv_frame(handle, reply) != 0) { MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h old mode 100644 new mode 100755 index 39cc454..f5be172 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -179,6 +179,15 @@ int mbus_disconnect(mbus_handle * handle); int mbus_recv_frame(mbus_handle * handle, mbus_frame *frame); /** + * Used for handling collisions. Blocks as long as receiving frames or corrupted data. + * + * @param handle Initialized handle + * + * @return Zero when nothing received, one otherwise. + */ +int mbus_recv_frame_dummy(mbus_handle * handle); + +/** * Sends frame using "unified" handle * * @param handle Initialized handle diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c old mode 100644 new mode 100755 index f29e8ce..cc14b18 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -161,69 +162,59 @@ mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame) //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ -int -mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame) +int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame) { char buff[PACKET_BUFF_SIZE]; - int len, remaining, nread, timeouts; - - if (handle == NULL || frame == NULL) - { + int len, remaining, nread; + + if (handle == NULL || frame == NULL) { fprintf(stderr, "%s: Invalid parameter.\n", __PRETTY_FUNCTION__); return -1; } - memset((void *)buff, 0, sizeof(buff)); + memset((void *) buff, 0, sizeof(buff)); // // read data until a packet is received // remaining = 1; // start by reading 1 byte len = 0; - timeouts = 0; do { - - if ((nread = read(handle->sock, &buff[len], remaining)) == -1) - { +retry: + nread = read(handle->sock, &buff[len], remaining); + switch (nread) { + case -1: + if (errno == EINTR) + goto retry; + + if (errno == EAGAIN || errno == EWOULDBLOCK) { + mbus_error_str_set("M-Bus tcp transport layer timeout has been reached."); + return -3; + } + mbus_error_str_set("M-Bus tcp transport layer failed to read data."); return -1; + case 0: + mbus_error_str_set("M-Bus tcp transport layer connection closed by remote host."); + return -4; + default: + len += nread; } - - if (nread == 0) - { - timeouts++; - - if (timeouts >= 3) - { - // abort to avoid endless loop - fprintf(stderr, "%s: Timeout\n", __PRETTY_FUNCTION__); - break; - } - } - - len += nread; - } while ((remaining = mbus_parse(frame, buff, len)) > 0); - - if (len == 0) - { - // No data received - return -1; - } - + // // call the receive event function, if the callback function is registered - // + // if (_mbus_recv_event) _mbus_recv_event(MBUS_HANDLE_TYPE_TCP, buff, len); - - if (remaining != 0) - { + + if (remaining < 0) { mbus_error_str_set("M-Bus layer failed to parse data."); return -2; } - + return 0; } + From 9f9e0fc008f7178e39cc1be3a2efa2aa7ae331a5 Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Tue, 3 Jul 2012 16:47:11 +0200 Subject: [PATCH 02/10] Fixed mbus_recv_frame() return value to -1 when invalid m-bus handle --- mbus/mbus-protocol-aux.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index fa06bb5..ccdb04f 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1386,7 +1386,7 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame) if (handle == NULL) { MBUS_ERROR("%s: Invalid M-Bus handle for receive.\n", __PRETTY_FUNCTION__); - return 0; + return -1; } if (handle->is_serial) From c848090f56ae3ab38d39f6076dec61a34c8c9886 Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Wed, 4 Jul 2012 17:18:36 +0200 Subject: [PATCH 03/10] Rebuilding M-Bus context structs in progress - changed local library file includes from <> to "" - get rid of 'if (is_serial)' conditions - mbus_context_serial() and mbus_context_tcp() allocates memory and initialize context specific function pointers - simply mbus_connect() called instead of mbus_connect_tcp() or mbus_connect_serial() - context specific data can still be accessed via (void* auxdata) pointer - strdup(3) to copy host IP into TCP context struct --- mbus/mbus-protocol-aux.c | 131 +++++++++++++++++++-------------- mbus/mbus-protocol-aux.h | 40 ++++++----- mbus/mbus-protocol.c | 2 +- mbus/mbus-protocol.h | 0 mbus/mbus-serial.c | 151 ++++++++++++++++++++------------------- mbus/mbus-serial.h | 21 +++--- mbus/mbus-tcp.c | 71 ++++++++---------- mbus/mbus-tcp.h | 19 +++-- 8 files changed, 223 insertions(+), 212 deletions(-) mode change 100644 => 100755 mbus/mbus-protocol.c mode change 100644 => 100755 mbus/mbus-protocol.h mode change 100644 => 100755 mbus/mbus-serial.c mode change 100644 => 100755 mbus/mbus-serial.h mode change 100644 => 100755 mbus/mbus-tcp.h diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index ccdb04f..c5e9fd6 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -12,6 +12,8 @@ //------------------------------------------------------------------------------ #include "mbus-protocol-aux.h" +#include "mbus-serial.h" +#include "mbus-tcp.h" #include #include @@ -1308,53 +1310,98 @@ mbus_frame_data_xml_normalized(mbus_frame_data *data) } mbus_handle * -mbus_connect_serial(const char * device) +mbus_context_serial(const char *device) { - mbus_serial_handle * serial_handle; - if ((serial_handle = mbus_serial_connect((char*)device)) == NULL) - { - MBUS_ERROR("%s: Failed to setup serial connection to M-bus gateway on %s.\n", - __PRETTY_FUNCTION__, - device); - return NULL; - } + mbus_handle *handle; + mbus_serial_handle *serial_data; + char error_str[128]; - mbus_handle * handle; - if ((handle = (mbus_handle * ) malloc(sizeof(mbus_handle))) == NULL) + if ((handle = (mbus_handle *) malloc(sizeof(mbus_handle))) == NULL) { MBUS_ERROR("%s: Failed to allocate handle.\n", __PRETTY_FUNCTION__); return NULL; } + + if ((serial_data = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL) + { + snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); + mbus_error_str_set(error_str); + free(handle); + return NULL; + } + handle->is_serial = 1; - handle->m_serial_handle = serial_handle; + handle->auxdata = serial_data; + handle->open = mbus_serial_connect; + handle->close = mbus_serial_disconnect; + handle->recv = mbus_serial_recv_frame; + handle->send = mbus_serial_send_frame; + + if ((serial_data->device = strdup(device)) == NULL) + { + snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for device\n", __PRETTY_FUNCTION__); + mbus_error_str_set(error_str); + free(serial_data); + free(handle); + return NULL; + } + return handle; } - mbus_handle * -mbus_connect_tcp(const char * host, int port) +mbus_context_tcp(const char *host, int port) { - mbus_tcp_handle * tcp_handle; - if ((tcp_handle = mbus_tcp_connect((char*)host, port)) == NULL) - { - MBUS_ERROR("%s: Failed to setup tcp connection to M-bus gateway on %s, port %d.\n", - __PRETTY_FUNCTION__, - host, - port); - return NULL; - } + mbus_handle *handle; + mbus_tcp_handle *tcp_data; + char error_str[128]; - mbus_handle * handle; - if ((handle = (mbus_handle * ) malloc(sizeof(mbus_handle))) == NULL) + if ((handle = (mbus_handle *) malloc(sizeof(mbus_handle))) == NULL) { MBUS_ERROR("%s: Failed to allocate handle.\n", __PRETTY_FUNCTION__); return NULL; } + + if ((tcp_data = (mbus_tcp_handle *)malloc(sizeof(mbus_tcp_handle))) == NULL) + { + snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); + mbus_error_str_set(error_str); + free(handle); + return NULL; + } + handle->is_serial = 0; - handle->m_tcp_handle = tcp_handle; + handle->auxdata = tcp_data; + handle->open = mbus_tcp_connect; + handle->close = mbus_tcp_disconnect; + handle->recv = mbus_tcp_recv_frame; + handle->send = mbus_tcp_send_frame; + + tcp_data->port = port; + if ((tcp_data->host = strdup(host)) == NULL) + { + snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for host\n", __PRETTY_FUNCTION__); + mbus_error_str_set(error_str); + free(tcp_data); + free(handle); + return NULL; + } + return handle; } +int +modbus_connect(mbus_handle * handle) +{ + if (handle == NULL) + { + MBUS_ERROR("%s: Invalid M-Bus handle for disconnect.\n", __PRETTY_FUNCTION__); + return 0; + } + + return handle->open(handle); +} + int mbus_disconnect(mbus_handle * handle) { @@ -1364,18 +1411,7 @@ mbus_disconnect(mbus_handle * handle) return 0; } - if (handle->is_serial) - { - mbus_serial_disconnect(handle->m_serial_handle); - handle->m_serial_handle = NULL; - } - else - { - mbus_tcp_disconnect(handle->m_tcp_handle); - handle->m_tcp_handle = NULL; - } - free(handle); - return 0; + return handle->close(handle); } int @@ -1389,14 +1425,7 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame) return -1; } - if (handle->is_serial) - { - result = mbus_serial_recv_frame(handle->m_serial_handle, frame); - } - else - { - result = mbus_tcp_recv_frame(handle->m_tcp_handle, frame); - } + result = handle->recv(handle, frame); if (frame != NULL) { @@ -1434,15 +1463,7 @@ mbus_send_frame(mbus_handle * handle, mbus_frame *frame) return 0; } - if (handle->is_serial) - { - return mbus_serial_send_frame(handle->m_serial_handle, frame); - } - else - { - return mbus_tcp_send_frame(handle->m_tcp_handle, frame); - } - return 0; + return handle->send(handle, frame); } //------------------------------------------------------------------------------ diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index f5be172..62f4d96 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -60,28 +60,27 @@ #ifndef __MBUS_PROTOCOL_AUX_H__ #define __MBUS_PROTOCOL_AUX_H__ -#include -#include -#include -#include +#include "mbus-protocol.h" #define MBUS_PROBE_NOTHING 0 #define MBUS_PROBE_SINGLE 1 #define MBUS_PROBE_COLLISION 2 #define MBUS_PROBE_ERROR -1 - /** * Unified MBus handle type encapsulating either Serial or TCP gateway. */ -typedef struct _mbus_handle { - char is_serial; /**< _handle type (non zero for serial) */ - union { - mbus_tcp_handle * m_tcp_handle; /**< TCP gateway handle */ - mbus_serial_handle * m_serial_handle; /**< Serial gateway handle */ - }; -} mbus_handle; +struct _mbus_handle { + int fd; + char is_serial; /**< _handle type (non zero for serial) */ + int (*open) (struct _mbus_handle *handle); + int (*close) (struct _mbus_handle *handle); + int (*send) (struct _mbus_handle *handle, mbus_frame *frame); + int (*recv) (struct _mbus_handle *handle, mbus_frame *frame); + void *auxdata; +}; +typedef struct _mbus_handle mbus_handle; /** * MBus slave address type (primary/secodary address) @@ -141,25 +140,34 @@ void mbus_register_scan_progress(void (*event)(mbus_handle * handle, const char void mbus_register_found_event(void (*event)(mbus_handle * handle, mbus_frame *frame)); /** - * Connects to serial gateway and initializes MBus handle + * Allocate and initialize M-Bus serial context. * * @param device Serial device (like /dev/ttyUSB0 or /dev/ttyS0) * * @return Initialized "unified" handler when successful, NULL otherwise; */ -mbus_handle * mbus_connect_serial(const char * device); +mbus_handle * mbus_context_serial(const char *device); /** - * Connects to TCP gateway and initializes MBus handle + * Allocate and initialize M-Bus TCP context. * * @param host Gateway host * @param port Gateway port * * @return Initialized "unified" handler when successful, NULL otherwise; */ -mbus_handle * mbus_connect_tcp(const char *host, int port); +mbus_handle * mbus_context_tcp(const char *host, int port); /** + * Connect to serial bus or TCP gateway depending on context. + * + * @param handle Initialized handle + * + * @return Zero when successful. + */ +int mbus_connect(mbus_handle * handle); + +/** * Disconnects the "unified" handle. * * @param handle Initialized handle diff --git a/mbus/mbus-protocol.c b/mbus/mbus-protocol.c old mode 100644 new mode 100755 index 45df1be..e11dfe9 --- a/mbus/mbus-protocol.c +++ b/mbus/mbus-protocol.c @@ -13,7 +13,7 @@ #include #include -#include +#include "mbus-protocol.h" static int parse_debug = 0, debug = 0; static char error_str[512]; diff --git a/mbus/mbus-protocol.h b/mbus/mbus-protocol.h old mode 100644 new mode 100755 diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c old mode 100644 new mode 100755 index 7a1aa28..eb02139 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -20,109 +20,112 @@ #include #include -#include -#include +#include "mbus-serial.h" +#include "mbus-protocol-aux.h" +#include "mbus-protocol.h" #define PACKET_BUFF_SIZE 2048 //------------------------------------------------------------------------------ /// Set up a serial connection handle. //------------------------------------------------------------------------------ -mbus_serial_handle * -mbus_serial_connect(char *device) +int +mbus_serial_connect(mbus_handle *handle) { - mbus_serial_handle *handle; +// mbus_serial_handle *serial_data; +// +// if (device == NULL) +// { +// return NULL; +// } +// +// if ((handle = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL) +// { +// fprintf(stderr, "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); +// return NULL; +// } +// +// handle->device = device; // strdup? +// +// // +// // create the SERIAL connection +// // +// +// // Use blocking read and handle it by serial port VMIN/VTIME setting +// if ((handle->fd = open(handle->device, O_RDWR | O_NOCTTY)) < 0) +// { +// fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__); +// return NULL; +// } +// +// memset(&(handle->t), 0, sizeof(handle->t)); +// handle->t.c_cflag |= (CS8|CREAD|CLOCAL); +// handle->t.c_cflag |= PARENB; +// +// // No received data still OK +// handle->t.c_cc[VMIN] = 0; +// +// // Wait at most 0.2 sec.Note that it starts after first received byte!! +// // I.e. if CMIN>0 and there are no data we would still wait forever... +// // +// // The specification mentions link layer response timeout this way: +// // The time structure of various link layer communication types is described in EN60870-5-1. The answer time +// // between the end of a master send telegram and the beginning of the response telegram of the slave shall be +// // between 11 bit times and (330 bit times + 50ms). +// // +// // For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte). +// // I.e. timeout of 0.2s seems appropriate for 2400Bd. +// +// handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec +// +// cfsetispeed(&(handle->t), B2400); +// cfsetospeed(&(handle->t), B2400); +// +//#ifdef MBUS_SERIAL_DEBUG +// printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_cflag); +// printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_oflag); +// printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_iflag); +// printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_lflag); +//#endif +// +// tcsetattr(handle->fd, TCSANOW, &(handle->t)); - if (device == NULL) - { - return NULL; - } - - if ((handle = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL) - { - fprintf(stderr, "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); - return NULL; - } - - handle->device = device; // strdup? - - // - // create the SERIAL connection - // - - // Use blocking read and handle it by serial port VMIN/VTIME setting - if ((handle->fd = open(handle->device, O_RDWR | O_NOCTTY)) < 0) - { - fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__); - return NULL; - } - - memset(&(handle->t), 0, sizeof(handle->t)); - handle->t.c_cflag |= (CS8|CREAD|CLOCAL); - handle->t.c_cflag |= PARENB; - - // No received data still OK - handle->t.c_cc[VMIN] = 0; - - // Wait at most 0.2 sec.Note that it starts after first received byte!! - // I.e. if CMIN>0 and there are no data we would still wait forever... - // - // The specification mentions link layer response timeout this way: - // The time structure of various link layer communication types is described in EN60870-5-1. The answer time - // between the end of a master send telegram and the beginning of the response telegram of the slave shall be - // between 11 bit times and (330 bit times + 50ms). - // - // For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte). - // I.e. timeout of 0.2s seems appropriate for 2400Bd. - - handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec - - cfsetispeed(&(handle->t), B2400); - cfsetospeed(&(handle->t), B2400); - -#ifdef MBUS_SERIAL_DEBUG - printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_cflag); - printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_oflag); - printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_iflag); - printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_lflag); -#endif - - tcsetattr(handle->fd, TCSANOW, &(handle->t)); - - return handle; + return 0; } //------------------------------------------------------------------------------ // Set baud rate for serial connection //------------------------------------------------------------------------------ int -mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate) +mbus_serial_set_baudrate(mbus_handle *handle, int baudrate) { speed_t speed; if (handle == NULL) return -1; + mbus_serial_handle *serial_data = (mbus_serial_handle *) handle->auxdata; + switch (baudrate) { case 300: speed = B300; - handle->t.c_cc[VTIME] = 12; // Timeout in 1/10 sec + serial_data->t.c_cc[VTIME] = 12; // Timeout in 1/10 sec break; case 1200: speed = B1200; - handle->t.c_cc[VTIME] = 4; // Timeout in 1/10 sec + serial_data->t.c_cc[VTIME] = 4; // Timeout in 1/10 sec break; case 2400: speed = B2400; - handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec + serial_data->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec break; case 9600: speed = B9600; - handle->t.c_cc[VTIME] = 1; // Timeout in 1/10 sec + serial_data->t.c_cc[VTIME] = 1; // Timeout in 1/10 sec break; default: @@ -130,19 +133,19 @@ mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate) } // Set input baud rate - if (cfsetispeed(&(handle->t), speed) != 0) + if (cfsetispeed(&(serial_data->t), speed) != 0) { return -1; } // Set output baud rate - if (cfsetospeed(&(handle->t), speed) != 0) + if (cfsetospeed(&(serial_data->t), speed) != 0) { return -1; } // Change baud rate immediately - if (tcsetattr(handle->fd, TCSANOW, &(handle->t)) != 0) + if (tcsetattr(handle->fd, TCSANOW, &(serial_data->t)) != 0) { return -1; } @@ -155,7 +158,7 @@ mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate) // //------------------------------------------------------------------------------ int -mbus_serial_disconnect(mbus_serial_handle *handle) +mbus_serial_disconnect(mbus_handle *handle) { if (handle == NULL) { @@ -163,8 +166,6 @@ mbus_serial_disconnect(mbus_serial_handle *handle) } close(handle->fd); - - free(handle); return 0; } @@ -173,7 +174,7 @@ mbus_serial_disconnect(mbus_serial_handle *handle) // //------------------------------------------------------------------------------ int -mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame) +mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame) { u_char buff[PACKET_BUFF_SIZE]; int len, ret; @@ -225,7 +226,7 @@ mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame) // //------------------------------------------------------------------------------ int -mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame) +mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame) { char buff[PACKET_BUFF_SIZE]; int len, remaining, nread, timeouts; diff --git a/mbus/mbus-serial.h b/mbus/mbus-serial.h old mode 100644 new mode 100755 index 5c2999a..25174e4 --- a/mbus/mbus-serial.h +++ b/mbus/mbus-serial.h @@ -19,23 +19,20 @@ #define MBUS_SERIAL_H #include -#include - -typedef struct _mbus_serial_handle { +#include "mbus-protocol-aux.h" +#include "mbus-protocol.h" +typedef struct _mbus_serial_handle +{ char *device; - - int fd; struct termios t; - } mbus_serial_handle; - -mbus_serial_handle *mbus_serial_connect(char *device); -int mbus_serial_disconnect(mbus_serial_handle *handle); -int mbus_serial_send_frame(mbus_serial_handle *handle, mbus_frame *frame); -int mbus_serial_recv_frame(mbus_serial_handle *handle, mbus_frame *frame); -int mbus_serial_set_baudrate(mbus_serial_handle *handle, int baudrate); +int mbus_serial_connect(mbus_handle *handle); +int mbus_serial_disconnect(mbus_handle *handle); +int mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame); +int mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame); +int mbus_serial_set_baudrate(mbus_handle *handle, int baudrate); #endif /* MBUS_SERIAL_H */ diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c index cc14b18..67e4c6f 100755 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -23,48 +23,41 @@ #include #include -#include -#include +#include "mbus-tcp.h" #define PACKET_BUFF_SIZE 2048 //------------------------------------------------------------------------------ /// Setup a TCP/IP handle. //------------------------------------------------------------------------------ -mbus_tcp_handle * -mbus_tcp_connect(char *host, int port) +int +mbus_tcp_connect(mbus_handle *handle) { - mbus_tcp_handle *handle; - + char error_str[128], *host; struct hostent *host_addr; struct sockaddr_in s; struct timeval time_out; + mbus_tcp_handle *tcp_data; + int port; - if (host == NULL) - { - return NULL; - } + if (handle == NULL) + return 0; - if ((handle = (mbus_tcp_handle *)malloc(sizeof(mbus_tcp_handle))) == NULL) - { - char error_str[128]; - snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); - mbus_error_str_set(error_str); - return NULL; - } + tcp_data = (mbus_tcp_handle *) handle->auxdata; + if (tcp_data == NULL || tcp_data->host == NULL) + return 0; - handle->host = host; // strdup ? - handle->port = port; + host = tcp_data->host; + port = tcp_data->port; // // create the TCP connection // - if ((handle->sock = socket(AF_INET,SOCK_STREAM, 0)) < 0) + if ((handle->fd = socket(AF_INET,SOCK_STREAM, 0)) < 0) { - char error_str[128]; snprintf(error_str, sizeof(error_str), "%s: failed to setup a socket.", __PRETTY_FUNCTION__); mbus_error_str_set(error_str); - return NULL; + return 0; } s.sin_family = AF_INET; @@ -73,47 +66,41 @@ mbus_tcp_connect(char *host, int port) /* resolve hostname */ if ((host_addr = gethostbyname(host)) == NULL) { - char error_str[128]; snprintf(error_str, sizeof(error_str), "%s: unknown host: %s", __PRETTY_FUNCTION__, host); mbus_error_str_set(error_str); - free(handle); - return NULL; + return 0; } memcpy((void *)(&s.sin_addr), (void *)(host_addr->h_addr), host_addr->h_length); - if (connect(handle->sock, (struct sockaddr *)&s, sizeof(s)) < 0) + if (connect(handle->fd, (struct sockaddr *)&s, sizeof(s)) < 0) { - char error_str[128]; snprintf(error_str, sizeof(error_str), "%s: Failed to establish connection to %s:%d", __PRETTY_FUNCTION__, host, port); mbus_error_str_set(error_str); - free(handle); - return NULL; + return 0; } // Set a timeout - time_out.tv_sec = 4; //seconds - time_out.tv_usec = 0; - setsockopt(handle->sock, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out)); - setsockopt(handle->sock, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out)); + time_out.tv_sec = 0; //seconds + time_out.tv_usec = 100000; + setsockopt(handle->fd, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out)); + setsockopt(handle->fd, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out)); - return handle; + return 1; } //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ int -mbus_tcp_disconnect(mbus_tcp_handle *handle) +mbus_tcp_disconnect(mbus_handle *handle) { if (handle == NULL) { return -1; } - close(handle->sock); - - free(handle); + close(handle->fd); return 0; } @@ -122,7 +109,7 @@ mbus_tcp_disconnect(mbus_tcp_handle *handle) // //------------------------------------------------------------------------------ int -mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame) +mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame) { u_char buff[PACKET_BUFF_SIZE]; int len, ret; @@ -140,7 +127,7 @@ mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame) return -1; } - if ((ret = write(handle->sock, buff, len)) == len) + if ((ret = write(handle->fd, buff, len)) == len) { // // call the send event function, if the callback function is registered @@ -162,7 +149,7 @@ mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame) //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ -int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame) +int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame) { char buff[PACKET_BUFF_SIZE]; int len, remaining, nread; @@ -182,7 +169,7 @@ int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame) do { retry: - nread = read(handle->sock, &buff[len], remaining); + nread = read(handle->fd, &buff[len], remaining); switch (nread) { case -1: if (errno == EINTR) diff --git a/mbus/mbus-tcp.h b/mbus/mbus-tcp.h old mode 100644 new mode 100755 index 73a8fc8..a6206a0 --- a/mbus/mbus-tcp.h +++ b/mbus/mbus-tcp.h @@ -18,22 +18,19 @@ #ifndef MBUS_TCP_H #define MBUS_TCP_H -#include - -typedef struct _mbus_tcp_handle { +#include "mbus-protocol.h" +#include "mbus-protocol-aux.h" +typedef struct _mbus_tcp_handle +{ char *host; - int port; - int sock; - } mbus_tcp_handle; - -mbus_tcp_handle *mbus_tcp_connect(char *host, int port); -int mbus_tcp_disconnect(mbus_tcp_handle *handle); -int mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame); -int mbus_tcp_recv_frame(mbus_tcp_handle *handle, mbus_frame *frame); +int mbus_tcp_connect(mbus_handle *handle); +int mbus_tcp_disconnect(mbus_handle *handle); +int mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame); +int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame); #endif /* MBUS_TCP_H */ From 645e5eb482f0607f74ca052b71c522ab8f35413a Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Wed, 4 Jul 2012 18:38:50 +0200 Subject: [PATCH 04/10] mbus_serial_connect() is ready --- mbus/mbus-serial.c | 114 ++++++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 58 deletions(-) diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c index eb02139..3ecd314 100755 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -32,65 +32,63 @@ int mbus_serial_connect(mbus_handle *handle) { -// mbus_serial_handle *serial_data; -// -// if (device == NULL) -// { -// return NULL; -// } -// -// if ((handle = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL) -// { -// fprintf(stderr, "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); -// return NULL; -// } -// -// handle->device = device; // strdup? -// -// // -// // create the SERIAL connection -// // -// -// // Use blocking read and handle it by serial port VMIN/VTIME setting -// if ((handle->fd = open(handle->device, O_RDWR | O_NOCTTY)) < 0) -// { -// fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__); -// return NULL; -// } -// -// memset(&(handle->t), 0, sizeof(handle->t)); -// handle->t.c_cflag |= (CS8|CREAD|CLOCAL); -// handle->t.c_cflag |= PARENB; -// -// // No received data still OK -// handle->t.c_cc[VMIN] = 0; -// -// // Wait at most 0.2 sec.Note that it starts after first received byte!! -// // I.e. if CMIN>0 and there are no data we would still wait forever... -// // -// // The specification mentions link layer response timeout this way: -// // The time structure of various link layer communication types is described in EN60870-5-1. The answer time -// // between the end of a master send telegram and the beginning of the response telegram of the slave shall be -// // between 11 bit times and (330 bit times + 50ms). -// // -// // For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte). -// // I.e. timeout of 0.2s seems appropriate for 2400Bd. -// -// handle->t.c_cc[VTIME] = 2; // Timeout in 1/10 sec -// -// cfsetispeed(&(handle->t), B2400); -// cfsetospeed(&(handle->t), B2400); -// -//#ifdef MBUS_SERIAL_DEBUG -// printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_cflag); -// printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_oflag); -// printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_iflag); -// printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, handle->t.c_lflag); -//#endif -// -// tcsetattr(handle->fd, TCSANOW, &(handle->t)); + mbus_serial_handle *serial_data; + const char *device; + struct termios *term; - return 0; + if (handle == NULL) + return 0; + + serial_data = (mbus_serial_handle *) handle->auxdata; + if (serial_data == NULL || serial_data->device == NULL) + return 0; + + device = serial_data->device; + term = &(serial_data->t); + // + // create the SERIAL connection + // + + // Use blocking read and handle it by serial port VMIN/VTIME setting + if ((handle->fd = open(device, O_RDWR | O_NOCTTY)) < 0) + { + fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__); + return 0; + } + + memset(term, 0, sizeof(*term)); + term->c_cflag |= (CS8|CREAD|CLOCAL); + term->c_cflag |= PARENB; + + // No received data still OK + term->c_cc[VMIN] = 0; + + // Wait at most 0.2 sec.Note that it starts after first received byte!! + // I.e. if CMIN>0 and there are no data we would still wait forever... + // + // The specification mentions link layer response timeout this way: + // The time structure of various link layer communication types is described in EN60870-5-1. The answer time + // between the end of a master send telegram and the beginning of the response telegram of the slave shall be + // between 11 bit times and (330 bit times + 50ms). + // + // For 2400Bd this means (330 + 11) / 2400 + 0.05 = 188.75 ms (added 11 bit periods to receive first byte). + // I.e. timeout of 0.2s seems appropriate for 2400Bd. + + term->c_cc[VTIME] = 2; // Timeout in 1/10 sec + + cfsetispeed(term, B2400); + cfsetospeed(term, B2400); + +#ifdef MBUS_SERIAL_DEBUG + printf("%s: t.c_cflag = %x\n", __PRETTY_FUNCTION__, term->c_cflag); + printf("%s: t.c_oflag = %x\n", __PRETTY_FUNCTION__, term->c_oflag); + printf("%s: t.c_iflag = %x\n", __PRETTY_FUNCTION__, term->c_iflag); + printf("%s: t.c_lflag = %x\n", __PRETTY_FUNCTION__, term->c_lflag); +#endif + + tcsetattr(handle->fd, TCSANOW, term); + + return 1; } //------------------------------------------------------------------------------ From 34255c7237d1cbfd7cf6bfbe94158336c0941413 Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Wed, 4 Jul 2012 19:08:51 +0200 Subject: [PATCH 05/10] free_auxdata() added to mbus_handle interface for freeing context specific data --- mbus/mbus-protocol-aux.c | 12 ++++++++++++ mbus/mbus-protocol-aux.h | 9 +++++++++ mbus/mbus-serial.c | 13 +++++++++++++ mbus/mbus-serial.h | 1 + mbus/mbus-tcp.c | 13 +++++++++++++ mbus/mbus-tcp.h | 1 + 6 files changed, 49 insertions(+) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index c5e9fd6..e2e759b 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1336,6 +1336,7 @@ mbus_context_serial(const char *device) handle->close = mbus_serial_disconnect; handle->recv = mbus_serial_recv_frame; handle->send = mbus_serial_send_frame; + handle->free_auxdata = mbus_tcp_data_free; if ((serial_data->device = strdup(device)) == NULL) { @@ -1376,6 +1377,7 @@ mbus_context_tcp(const char *host, int port) handle->close = mbus_tcp_disconnect; handle->recv = mbus_tcp_recv_frame; handle->send = mbus_tcp_send_frame; + handle->free_auxdata = mbus_tcp_data_free; tcp_data->port = port; if ((tcp_data->host = strdup(host)) == NULL) @@ -1390,6 +1392,16 @@ mbus_context_tcp(const char *host, int port) return handle; } +void +mbus_context_free(mbus_handle * handle) +{ + if (handle) + { + handle->free_auxdata(handle); + free(handle); + } +} + int modbus_connect(mbus_handle * handle) { diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index 62f4d96..7d45229 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -77,6 +77,7 @@ struct _mbus_handle { int (*close) (struct _mbus_handle *handle); int (*send) (struct _mbus_handle *handle, mbus_frame *frame); int (*recv) (struct _mbus_handle *handle, mbus_frame *frame); + void (*free_auxdata) (struct _mbus_handle *handle); void *auxdata; }; @@ -159,6 +160,14 @@ mbus_handle * mbus_context_serial(const char *device); mbus_handle * mbus_context_tcp(const char *host, int port); /** + * Deallocate memory used by M-Bus context. + * + * @param handle Initialized handle + * + */ +void mbus_context_free(mbus_handle * handle); + +/** * Connect to serial bus or TCP gateway depending on context. * * @param handle Initialized handle diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c index 3ecd314..2b9ef3d 100755 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -168,6 +168,19 @@ mbus_serial_disconnect(mbus_handle *handle) return 0; } +void +mbus_serial_data_free(mbus_handle *handle) +{ + mbus_serial_handle *serial_data; + + if (handle) + { + serial_data = (mbus_serial_handle *) handle->auxdata; + free(serial_data->device); + free(serial_data); + } +} + //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ diff --git a/mbus/mbus-serial.h b/mbus/mbus-serial.h index 25174e4..97bffae 100755 --- a/mbus/mbus-serial.h +++ b/mbus/mbus-serial.h @@ -33,6 +33,7 @@ int mbus_serial_disconnect(mbus_handle *handle); int mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame); int mbus_serial_recv_frame(mbus_handle *handle, mbus_frame *frame); int mbus_serial_set_baudrate(mbus_handle *handle, int baudrate); +void mbus_serial_data_free(mbus_handle *handle); #endif /* MBUS_SERIAL_H */ diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c index 67e4c6f..7bf95b8 100755 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -89,6 +89,19 @@ mbus_tcp_connect(mbus_handle *handle) return 1; } +void +mbus_tcp_data_free(mbus_handle *handle) +{ + mbus_tcp_handle *tcp_data; + + if (handle) + { + tcp_data = (mbus_tcp_handle *) handle->auxdata; + free(tcp_data->host); + free(tcp_data); + } +} + //------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ diff --git a/mbus/mbus-tcp.h b/mbus/mbus-tcp.h index a6206a0..56b9b8d 100755 --- a/mbus/mbus-tcp.h +++ b/mbus/mbus-tcp.h @@ -31,6 +31,7 @@ int mbus_tcp_connect(mbus_handle *handle); int mbus_tcp_disconnect(mbus_handle *handle); int mbus_tcp_send_frame(mbus_handle *handle, mbus_frame *frame); int mbus_tcp_recv_frame(mbus_handle *handle, mbus_frame *frame); +void mbus_tcp_data_free(mbus_handle *handle); #endif /* MBUS_TCP_H */ From ec50ab928698c39861eb245a177e0fc4dfc8bb9f Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Wed, 4 Jul 2012 19:49:54 +0200 Subject: [PATCH 06/10] Examples from bin directory updated. API usage updated in mbus-protocol-aux.h. --- bin/mbus-serial-request-data-multi-reply.c | 13 ++++++++++--- bin/mbus-serial-request-data.c | 13 ++++++++++--- bin/mbus-serial-scan-secondary.c | 14 ++++++++++---- bin/mbus-serial-scan.c | 11 +++++++++-- bin/mbus-serial-select-secondary.c | 13 ++++++++++--- bin/mbus-serial-switch-baudrate.c | 13 ++++++++++--- bin/mbus-tcp-request-data-multi-reply.c | 9 ++++++++- bin/mbus-tcp-request-data.c | 9 ++++++++- bin/mbus-tcp-scan-secondary.c | 11 +++++++++-- bin/mbus-tcp-scan.c | 9 ++++++++- bin/mbus-tcp-select-secondary.c | 11 +++++++++-- mbus/mbus-protocol-aux.c | 4 ++-- mbus/mbus-protocol-aux.h | 7 +++++-- mbus/mbus-tcp.c | 6 +++--- 14 files changed, 111 insertions(+), 32 deletions(-) mode change 100644 => 100755 bin/mbus-serial-request-data-multi-reply.c mode change 100644 => 100755 bin/mbus-serial-scan-secondary.c mode change 100644 => 100755 bin/mbus-tcp-request-data-multi-reply.c mode change 100644 => 100755 bin/mbus-tcp-scan-secondary.c diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c old mode 100644 new mode 100755 index 9dd1648..d0a1abe --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -73,13 +73,19 @@ main(int argc, char **argv) mbus_register_recv_event(&mbus_dump_recv_event); } - if ((handle = mbus_connect_serial(device)) == NULL) + if ((handle = mbus_context_serial(device)) == NULL) { - fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); return 1; } - if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1) + if (!mbus_connect(handle)) + { + printf("Failed to setup connection to M-bus gateway\n"); + return 1; + } + + if (mbus_serial_set_baudrate(handle, baudrate) == -1) { printf("Failed to set baud rate.\n"); return 1; @@ -174,6 +180,7 @@ main(int argc, char **argv) free(xml_result); mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-serial-request-data.c b/bin/mbus-serial-request-data.c index 219a49d..0e6147a 100755 --- a/bin/mbus-serial-request-data.c +++ b/bin/mbus-serial-request-data.c @@ -70,13 +70,19 @@ main(int argc, char **argv) mbus_register_recv_event(&mbus_dump_recv_event); } - if ((handle = mbus_connect_serial(device)) == NULL) + if ((handle = mbus_context_serial(device)) == NULL) { - fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); return 1; } - if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1) + if (!mbus_connect(handle)) + { + printf("Failed to setup connection to M-bus gateway\n"); + return 1; + } + + if (mbus_serial_set_baudrate(handle, baudrate) == -1) { printf("Failed to set baud rate.\n"); return 1; @@ -162,6 +168,7 @@ main(int argc, char **argv) } mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-serial-scan-secondary.c b/bin/mbus-serial-scan-secondary.c old mode 100644 new mode 100755 index 92dc3d3..7de7c87 --- a/bin/mbus-serial-scan-secondary.c +++ b/bin/mbus-serial-scan-secondary.c @@ -102,13 +102,19 @@ main(int argc, char **argv) return 1; } - if ((handle = mbus_connect_serial(device)) == NULL) + if ((handle = mbus_context_serial(device)) == NULL) { - fprintf(stderr, "Failed to setup connection to M-bus gateway: %s\n", mbus_error_str()); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); return 1; } - if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1) + if (!mbus_connect(handle)) + { + printf("Failed to setup connection to M-bus gateway\n"); + return 1; + } + + if (mbus_serial_set_baudrate(handle, baudrate) == -1) { fprintf(stderr, "Failed to set baud rate.\n"); return 1; @@ -153,7 +159,7 @@ main(int argc, char **argv) mbus_scan_2nd_address_range(handle, 0, addr_mask); mbus_disconnect(handle); - + mbus_context_free(handle); //printf("Summary: Tried %ld address masks and found %d devices.\n", probe_count, match_count); free(addr_mask); diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c index cfd325a..845b1ca 100755 --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -62,13 +62,19 @@ main(int argc, char **argv) mbus_register_recv_event(&mbus_dump_recv_event); } - if ((handle = mbus_connect_serial(device)) == NULL) + if ((handle = mbus_context_serial(device)) == NULL) + { + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) { printf("Failed to setup connection to M-bus gateway\n"); return 1; } - if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1) + if (mbus_serial_set_baudrate(handle, baudrate) == -1) { printf("Failed to set baud rate.\n"); return 1; @@ -128,6 +134,7 @@ main(int argc, char **argv) } mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-serial-select-secondary.c b/bin/mbus-serial-select-secondary.c index 6fe462b..6053056 100755 --- a/bin/mbus-serial-select-secondary.c +++ b/bin/mbus-serial-select-secondary.c @@ -53,13 +53,19 @@ main(int argc, char **argv) return 1; } - if ((handle = mbus_connect_serial(device)) == NULL) + if ((handle = mbus_context_serial(device)) == NULL) { - printf("Failed to setup connection to M-bus device: %s\n", mbus_error_str()); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); return 1; } - if (mbus_serial_set_baudrate(handle->m_serial_handle, baudrate) == -1) + if (!mbus_connect(handle)) + { + printf("Failed to setup connection to M-bus gateway\n"); + return 1; + } + + if (mbus_serial_set_baudrate(handle, baudrate) == -1) { printf("Failed to set baud rate.\n"); return 1; @@ -113,6 +119,7 @@ main(int argc, char **argv) free(addr); mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-serial-switch-baudrate.c b/bin/mbus-serial-switch-baudrate.c index 249f5d5..9a4e9bc 100755 --- a/bin/mbus-serial-switch-baudrate.c +++ b/bin/mbus-serial-switch-baudrate.c @@ -52,13 +52,19 @@ main(int argc, char **argv) return 0; } - if ((handle = mbus_connect_serial(device)) == NULL) + if ((handle = mbus_context_serial(device)) == NULL) { - printf("Failed to setup connection to M-bus device: %s\n", mbus_error_str()); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) + { + printf("Failed to setup connection to M-bus gateway\n"); return 1; } - if (mbus_serial_set_baudrate(handle->m_serial_handle, source_baudrate) == -1) + if (mbus_serial_set_baudrate(handle, source_baudrate) == -1) { printf("Failed to set baud rate.\n"); return 1; @@ -88,6 +94,7 @@ main(int argc, char **argv) } mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-tcp-request-data-multi-reply.c b/bin/mbus-tcp-request-data-multi-reply.c old mode 100644 new mode 100755 index e06c022..bdd5790 --- a/bin/mbus-tcp-request-data-multi-reply.c +++ b/bin/mbus-tcp-request-data-multi-reply.c @@ -62,7 +62,13 @@ main(int argc, char **argv) mbus_register_recv_event(&mbus_dump_recv_event); } - if ((handle = mbus_connect_tcp(host, port)) == NULL) + if ((handle = mbus_context_tcp(host, port)) == NULL) + { + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) { fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; @@ -157,6 +163,7 @@ main(int argc, char **argv) free(xml_result); mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-tcp-request-data.c b/bin/mbus-tcp-request-data.c index 6024f4b..d2c05fd 100755 --- a/bin/mbus-tcp-request-data.c +++ b/bin/mbus-tcp-request-data.c @@ -62,7 +62,13 @@ main(int argc, char **argv) mbus_register_recv_event(&mbus_dump_recv_event); } - if ((handle = mbus_connect_tcp(host, port)) == NULL) + if ((handle = mbus_context_tcp(host, port)) == NULL) + { + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) { fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; @@ -147,6 +153,7 @@ main(int argc, char **argv) } mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-tcp-scan-secondary.c b/bin/mbus-tcp-scan-secondary.c old mode 100644 new mode 100755 index e2cca2a..5b31472 --- a/bin/mbus-tcp-scan-secondary.c +++ b/bin/mbus-tcp-scan-secondary.c @@ -57,9 +57,15 @@ main(int argc, char **argv) return 1; } - if ((handle = mbus_connect_tcp(host, port)) == NULL) + if ((handle = mbus_context_tcp(host, port)) == NULL) { - fprintf(stderr, "Failed to setup connection to M-bus gateway: %s\n", mbus_error_str()); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) + { + fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; } @@ -103,6 +109,7 @@ main(int argc, char **argv) mbus_scan_2nd_address_range(handle, 0, addr_mask); mbus_disconnect(handle); + mbus_context_free(handle); //printf("Summary: Tried %ld address masks and found %d devices.\n", probe_count, match_count); diff --git a/bin/mbus-tcp-scan.c b/bin/mbus-tcp-scan.c index 4de525e..4f88bac 100755 --- a/bin/mbus-tcp-scan.c +++ b/bin/mbus-tcp-scan.c @@ -51,7 +51,13 @@ main(int argc, char **argv) mbus_register_recv_event(&mbus_dump_recv_event); } - if ((handle = mbus_connect_tcp(host, port)) == NULL) + if ((handle = mbus_context_tcp(host, port)) == NULL) + { + printf("Scan failed: Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) { printf("Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str()); return 1; @@ -110,6 +116,7 @@ main(int argc, char **argv) } mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/bin/mbus-tcp-select-secondary.c b/bin/mbus-tcp-select-secondary.c index 698ef9d..ab3cddc 100755 --- a/bin/mbus-tcp-select-secondary.c +++ b/bin/mbus-tcp-select-secondary.c @@ -45,9 +45,15 @@ main(int argc, char **argv) return 1; } - if ((handle = mbus_connect_tcp(host, port)) == NULL) + if ((handle = mbus_context_tcp(host, port)) == NULL) { - printf("Failed to setup connection to M-bus gateway: %s\n", mbus_error_str()); + fprintf(stderr, "Could not initialize M-Bus context: %s\n", mbus_error_str()); + return 1; + } + + if (!mbus_connect(handle)) + { + fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; } @@ -99,6 +105,7 @@ main(int argc, char **argv) free(addr); mbus_disconnect(handle); + mbus_context_free(handle); return 0; } diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index e2e759b..e402c66 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1336,7 +1336,7 @@ mbus_context_serial(const char *device) handle->close = mbus_serial_disconnect; handle->recv = mbus_serial_recv_frame; handle->send = mbus_serial_send_frame; - handle->free_auxdata = mbus_tcp_data_free; + handle->free_auxdata = mbus_serial_data_free; if ((serial_data->device = strdup(device)) == NULL) { @@ -1403,7 +1403,7 @@ mbus_context_free(mbus_handle * handle) } int -modbus_connect(mbus_handle * handle) +mbus_connect(mbus_handle * handle) { if (handle == NULL) { diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index 7d45229..f127c2a 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -20,9 +20,11 @@ * the data processing. * Typical use might be (in oversimplified "pseudocode"): * \verbatim - * mbus_handle = mbus_connect_serial(device); + * mbus_handle = mbus_context_serial(device); * or - * mbus_handle = mbus_connect_tcp(host, port); + * mbus_handle = mbus_context_tcp(host, port); + * + * mbus_connect(mbus_handle); * * ... * @@ -45,6 +47,7 @@ * ... * * mbus_disconnect(mbus_handle); + * mbus_context_free(mbus_handle); * \endverbatim * * Note that the quantity values are partially "normalized". For example energy diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c index 7bf95b8..001e70e 100755 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -81,8 +81,8 @@ mbus_tcp_connect(mbus_handle *handle) } // Set a timeout - time_out.tv_sec = 0; //seconds - time_out.tv_usec = 100000; + time_out.tv_sec = 4; //seconds + time_out.tv_usec = 0; setsockopt(handle->fd, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out)); setsockopt(handle->fd, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out)); @@ -189,7 +189,7 @@ retry: goto retry; if (errno == EAGAIN || errno == EWOULDBLOCK) { - mbus_error_str_set("M-Bus tcp transport layer timeout has been reached."); + mbus_error_str_set("M-Bus tcp transport layer response timeout has been reached."); return -3; } From 78a6013d752e242fccf7d2408c6f4f78b719228a Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Fri, 6 Jul 2012 11:21:24 +0200 Subject: [PATCH 07/10] mbus_recv_dummy() instead of while loop. --- bin/mbus-serial-request-data-multi-reply.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index edfe091..a719cab 100755 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -116,7 +116,7 @@ main(int argc, char **argv) return 1; } - while (mbus_recv_frame(handle, &reply) != -1); + mbus_recv_frame_dummy(handle); // // resend SND_NKE, maybe the first get lost @@ -135,7 +135,7 @@ main(int argc, char **argv) return 1; } - while (mbus_recv_frame(handle, &reply) != -1); + mbus_recv_frame_dummy(handle); if (strlen(addr_str) == 16) { From e70c07e8f3f2d45d0047b4c71c3e7308233cd2dc Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Fri, 6 Jul 2012 11:29:46 +0200 Subject: [PATCH 08/10] TCP and serial specific data structs renamed. --- mbus/mbus-protocol-aux.c | 8 ++++---- mbus/mbus-serial.c | 10 +++++----- mbus/mbus-serial.h | 4 ++-- mbus/mbus-tcp.c | 8 ++++---- mbus/mbus-tcp.h | 4 ++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 6132728..cbb614f 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1313,7 +1313,7 @@ mbus_handle * mbus_context_serial(const char *device) { mbus_handle *handle; - mbus_serial_handle *serial_data; + mbus_serial_data *serial_data; char error_str[128]; if ((handle = (mbus_handle *) malloc(sizeof(mbus_handle))) == NULL) @@ -1322,7 +1322,7 @@ mbus_context_serial(const char *device) return NULL; } - if ((serial_data = (mbus_serial_handle *)malloc(sizeof(mbus_serial_handle))) == NULL) + if ((serial_data = (mbus_serial_data *)malloc(sizeof(mbus_serial_data))) == NULL) { snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); mbus_error_str_set(error_str); @@ -1354,7 +1354,7 @@ mbus_handle * mbus_context_tcp(const char *host, int port) { mbus_handle *handle; - mbus_tcp_handle *tcp_data; + mbus_tcp_data *tcp_data; char error_str[128]; if ((handle = (mbus_handle *) malloc(sizeof(mbus_handle))) == NULL) @@ -1363,7 +1363,7 @@ mbus_context_tcp(const char *host, int port) return NULL; } - if ((tcp_data = (mbus_tcp_handle *)malloc(sizeof(mbus_tcp_handle))) == NULL) + if ((tcp_data = (mbus_tcp_data *)malloc(sizeof(mbus_tcp_data))) == NULL) { snprintf(error_str, sizeof(error_str), "%s: failed to allocate memory for handle\n", __PRETTY_FUNCTION__); mbus_error_str_set(error_str); diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c index 2b9ef3d..2599676 100755 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -32,14 +32,14 @@ int mbus_serial_connect(mbus_handle *handle) { - mbus_serial_handle *serial_data; + mbus_serial_data *serial_data; const char *device; struct termios *term; if (handle == NULL) return 0; - serial_data = (mbus_serial_handle *) handle->auxdata; + serial_data = (mbus_serial_data *) handle->auxdata; if (serial_data == NULL || serial_data->device == NULL) return 0; @@ -102,7 +102,7 @@ mbus_serial_set_baudrate(mbus_handle *handle, int baudrate) if (handle == NULL) return -1; - mbus_serial_handle *serial_data = (mbus_serial_handle *) handle->auxdata; + mbus_serial_data *serial_data = (mbus_serial_data *) handle->auxdata; switch (baudrate) { @@ -171,11 +171,11 @@ mbus_serial_disconnect(mbus_handle *handle) void mbus_serial_data_free(mbus_handle *handle) { - mbus_serial_handle *serial_data; + mbus_serial_data *serial_data; if (handle) { - serial_data = (mbus_serial_handle *) handle->auxdata; + serial_data = (mbus_serial_data *) handle->auxdata; free(serial_data->device); free(serial_data); } diff --git a/mbus/mbus-serial.h b/mbus/mbus-serial.h index 97bffae..6203784 100755 --- a/mbus/mbus-serial.h +++ b/mbus/mbus-serial.h @@ -22,11 +22,11 @@ #include "mbus-protocol-aux.h" #include "mbus-protocol.h" -typedef struct _mbus_serial_handle +typedef struct _mbus_serial_data { char *device; struct termios t; -} mbus_serial_handle; +} mbus_serial_data; int mbus_serial_connect(mbus_handle *handle); int mbus_serial_disconnect(mbus_handle *handle); diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c index 001e70e..4234cc0 100755 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -37,13 +37,13 @@ mbus_tcp_connect(mbus_handle *handle) struct hostent *host_addr; struct sockaddr_in s; struct timeval time_out; - mbus_tcp_handle *tcp_data; + mbus_tcp_data *tcp_data; int port; if (handle == NULL) return 0; - tcp_data = (mbus_tcp_handle *) handle->auxdata; + tcp_data = (mbus_tcp_data *) handle->auxdata; if (tcp_data == NULL || tcp_data->host == NULL) return 0; @@ -92,11 +92,11 @@ mbus_tcp_connect(mbus_handle *handle) void mbus_tcp_data_free(mbus_handle *handle) { - mbus_tcp_handle *tcp_data; + mbus_tcp_data *tcp_data; if (handle) { - tcp_data = (mbus_tcp_handle *) handle->auxdata; + tcp_data = (mbus_tcp_data *) handle->auxdata; free(tcp_data->host); free(tcp_data); } diff --git a/mbus/mbus-tcp.h b/mbus/mbus-tcp.h index 56b9b8d..6de12a7 100755 --- a/mbus/mbus-tcp.h +++ b/mbus/mbus-tcp.h @@ -21,11 +21,11 @@ #include "mbus-protocol.h" #include "mbus-protocol-aux.h" -typedef struct _mbus_tcp_handle +typedef struct _mbus_tcp_data { char *host; int port; -} mbus_tcp_handle; +} mbus_tcp_data; int mbus_tcp_connect(mbus_handle *handle); int mbus_tcp_disconnect(mbus_handle *handle); From 5f9052b284b04c40a73d2d3bf9f336bf8a42ff47 Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Mon, 9 Jul 2012 10:48:13 +0200 Subject: [PATCH 09/10] Renamed mbus_recv_frame_dummy() to more descriptive mbus_purge_frames() --- bin/mbus-serial-request-data-multi-reply.c | 4 ++-- bin/mbus-serial-scan.c | 4 ++-- bin/mbus-tcp-scan.c | 4 ++-- mbus/mbus-protocol-aux.c | 6 +++--- mbus/mbus-protocol-aux.h | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index a719cab..0f1fb7a 100755 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -116,7 +116,7 @@ main(int argc, char **argv) return 1; } - mbus_recv_frame_dummy(handle); + mbus_purge_frames(handle); // // resend SND_NKE, maybe the first get lost @@ -135,7 +135,7 @@ main(int argc, char **argv) return 1; } - mbus_recv_frame_dummy(handle); + mbus_purge_frames(handle); if (strlen(addr_str) == 16) { diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c index 845b1ca..ac5470a 100755 --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -114,7 +114,7 @@ main(int argc, char **argv) if (ret == -2) { /* check for more data (collision) */ - mbus_recv_frame_dummy(handle); + mbus_purge_frames(handle); printf("Collision at address %d\n", address); continue; } @@ -122,7 +122,7 @@ main(int argc, char **argv) if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ - if (mbus_recv_frame_dummy(handle)) + if (mbus_purge_frames(handle)) { printf("Collision at address %d\n", address); diff --git a/bin/mbus-tcp-scan.c b/bin/mbus-tcp-scan.c index 4f88bac..015c525 100755 --- a/bin/mbus-tcp-scan.c +++ b/bin/mbus-tcp-scan.c @@ -97,7 +97,7 @@ main(int argc, char **argv) if (ret == -2) { /* check for more data (collision) */ - mbus_recv_frame_dummy(handle); + mbus_purge_frames(handle); printf("Collision at address %d\n", address); continue; } @@ -105,7 +105,7 @@ main(int argc, char **argv) if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ - if (mbus_recv_frame_dummy(handle)) + if (mbus_purge_frames(handle)) { printf("Collision at address %d\n", address); continue; diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index cbb614f..f1b4018 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1454,7 +1454,7 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame) return result; } -int mbus_recv_frame_dummy(mbus_handle *handle) +int mbus_purge_frames(mbus_handle *handle) { int err, received; mbus_frame reply; @@ -1810,14 +1810,14 @@ mbus_select_secondary_address(mbus_handle * handle, const char *mask) if (ret == -2) { /* check for more data (collision) */ - mbus_recv_frame_dummy(handle); + mbus_purge_frames(handle); return MBUS_PROBE_COLLISION; } if (mbus_frame_type(&reply) == MBUS_FRAME_TYPE_ACK) { /* check for more data (collision) */ - if (mbus_recv_frame_dummy(handle)) + if (mbus_purge_frames(handle)) { return MBUS_PROBE_COLLISION; } diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index f127c2a..71eeecd 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -205,7 +205,7 @@ int mbus_recv_frame(mbus_handle * handle, mbus_frame *frame); * * @return Zero when nothing received, one otherwise. */ -int mbus_recv_frame_dummy(mbus_handle * handle); +int mbus_purge_frames(mbus_handle * handle); /** * Sends frame using "unified" handle From b95f29fcc889115316bfe7f728c111bccd1f10bb Mon Sep 17 00:00:00 2001 From: jakubovsky Date: Mon, 9 Jul 2012 12:33:32 +0200 Subject: [PATCH 10/10] mbus_connect() and mbus_disconnect() return 0 on success and -1 otherwise --- bin/mbus-serial-request-data.c | 2 +- bin/mbus-serial-scan.c | 2 +- bin/mbus-serial-select-secondary.c | 2 +- bin/mbus-serial-switch-baudrate.c | 2 +- bin/mbus-tcp-request-data-multi-reply.c | 2 +- bin/mbus-tcp-request-data.c | 2 +- bin/mbus-tcp-scan-secondary.c | 2 +- bin/mbus-tcp-scan.c | 2 +- bin/mbus-tcp-select-secondary.c | 2 +- mbus/mbus-protocol-aux.c | 4 ++-- mbus/mbus-serial.c | 8 ++++---- mbus/mbus-tcp.c | 12 ++++++------ 12 files changed, 21 insertions(+), 21 deletions(-) diff --git a/bin/mbus-serial-request-data.c b/bin/mbus-serial-request-data.c index 0e6147a..46526a7 100755 --- a/bin/mbus-serial-request-data.c +++ b/bin/mbus-serial-request-data.c @@ -76,7 +76,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { printf("Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-serial-scan.c b/bin/mbus-serial-scan.c index ac5470a..1d2d0f8 100755 --- a/bin/mbus-serial-scan.c +++ b/bin/mbus-serial-scan.c @@ -68,7 +68,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { printf("Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-serial-select-secondary.c b/bin/mbus-serial-select-secondary.c index 6053056..624a384 100755 --- a/bin/mbus-serial-select-secondary.c +++ b/bin/mbus-serial-select-secondary.c @@ -59,7 +59,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { printf("Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-serial-switch-baudrate.c b/bin/mbus-serial-switch-baudrate.c index 9a4e9bc..e67b98c 100755 --- a/bin/mbus-serial-switch-baudrate.c +++ b/bin/mbus-serial-switch-baudrate.c @@ -58,7 +58,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { printf("Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-tcp-request-data-multi-reply.c b/bin/mbus-tcp-request-data-multi-reply.c index 1a8ff58..7d421f0 100755 --- a/bin/mbus-tcp-request-data-multi-reply.c +++ b/bin/mbus-tcp-request-data-multi-reply.c @@ -68,7 +68,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-tcp-request-data.c b/bin/mbus-tcp-request-data.c index d2c05fd..1ed504f 100755 --- a/bin/mbus-tcp-request-data.c +++ b/bin/mbus-tcp-request-data.c @@ -68,7 +68,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-tcp-scan-secondary.c b/bin/mbus-tcp-scan-secondary.c index ab77f67..bd1c12f 100755 --- a/bin/mbus-tcp-scan-secondary.c +++ b/bin/mbus-tcp-scan-secondary.c @@ -63,7 +63,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/bin/mbus-tcp-scan.c b/bin/mbus-tcp-scan.c index 015c525..2776018 100755 --- a/bin/mbus-tcp-scan.c +++ b/bin/mbus-tcp-scan.c @@ -57,7 +57,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { printf("Scan failed: Could not setup connection to M-bus gateway: %s\n", mbus_error_str()); return 1; diff --git a/bin/mbus-tcp-select-secondary.c b/bin/mbus-tcp-select-secondary.c index ab3cddc..5268650 100755 --- a/bin/mbus-tcp-select-secondary.c +++ b/bin/mbus-tcp-select-secondary.c @@ -51,7 +51,7 @@ main(int argc, char **argv) return 1; } - if (!mbus_connect(handle)) + if (mbus_connect(handle) == -1) { fprintf(stderr, "Failed to setup connection to M-bus gateway\n"); return 1; diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index f1b4018..cb11257 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1408,7 +1408,7 @@ mbus_connect(mbus_handle * handle) if (handle == NULL) { MBUS_ERROR("%s: Invalid M-Bus handle for disconnect.\n", __PRETTY_FUNCTION__); - return 0; + return -1; } return handle->open(handle); @@ -1420,7 +1420,7 @@ mbus_disconnect(mbus_handle * handle) if (handle == NULL) { MBUS_ERROR("%s: Invalid M-Bus handle for disconnect.\n", __PRETTY_FUNCTION__); - return 0; + return -1; } return handle->close(handle); diff --git a/mbus/mbus-serial.c b/mbus/mbus-serial.c index 2599676..d28e7eb 100755 --- a/mbus/mbus-serial.c +++ b/mbus/mbus-serial.c @@ -37,11 +37,11 @@ mbus_serial_connect(mbus_handle *handle) struct termios *term; if (handle == NULL) - return 0; + return -1; serial_data = (mbus_serial_data *) handle->auxdata; if (serial_data == NULL || serial_data->device == NULL) - return 0; + return -1; device = serial_data->device; term = &(serial_data->t); @@ -53,7 +53,7 @@ mbus_serial_connect(mbus_handle *handle) if ((handle->fd = open(device, O_RDWR | O_NOCTTY)) < 0) { fprintf(stderr, "%s: failed to open tty.", __PRETTY_FUNCTION__); - return 0; + return -1; } memset(term, 0, sizeof(*term)); @@ -88,7 +88,7 @@ mbus_serial_connect(mbus_handle *handle) tcsetattr(handle->fd, TCSANOW, term); - return 1; + return 0; } //------------------------------------------------------------------------------ diff --git a/mbus/mbus-tcp.c b/mbus/mbus-tcp.c index 4234cc0..f81a1cd 100755 --- a/mbus/mbus-tcp.c +++ b/mbus/mbus-tcp.c @@ -41,11 +41,11 @@ mbus_tcp_connect(mbus_handle *handle) int port; if (handle == NULL) - return 0; + return -1; tcp_data = (mbus_tcp_data *) handle->auxdata; if (tcp_data == NULL || tcp_data->host == NULL) - return 0; + return -1; host = tcp_data->host; port = tcp_data->port; @@ -57,7 +57,7 @@ mbus_tcp_connect(mbus_handle *handle) { snprintf(error_str, sizeof(error_str), "%s: failed to setup a socket.", __PRETTY_FUNCTION__); mbus_error_str_set(error_str); - return 0; + return -1; } s.sin_family = AF_INET; @@ -68,7 +68,7 @@ mbus_tcp_connect(mbus_handle *handle) { snprintf(error_str, sizeof(error_str), "%s: unknown host: %s", __PRETTY_FUNCTION__, host); mbus_error_str_set(error_str); - return 0; + return -1; } memcpy((void *)(&s.sin_addr), (void *)(host_addr->h_addr), host_addr->h_length); @@ -77,7 +77,7 @@ mbus_tcp_connect(mbus_handle *handle) { snprintf(error_str, sizeof(error_str), "%s: Failed to establish connection to %s:%d", __PRETTY_FUNCTION__, host, port); mbus_error_str_set(error_str); - return 0; + return -1; } // Set a timeout @@ -86,7 +86,7 @@ mbus_tcp_connect(mbus_handle *handle) setsockopt(handle->fd, SOL_SOCKET, SO_SNDTIMEO, &time_out, sizeof(time_out)); setsockopt(handle->fd, SOL_SOCKET, SO_RCVTIMEO, &time_out, sizeof(time_out)); - return 1; + return 0; } void