From 0d610b6a27ec586d5148f9a1a0f3c53b60b6f477 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 20 Jun 2012 00:21:28 +0200 Subject: [PATCH 1/5] - fixed check of mbus_sendrecv_request and mbus_recv_frame - initialize memory in mbus_sendrecv_request - used defines for special address instead of magic numbers --- mbus/mbus-protocol-aux.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 2323bec..bba0cbc 100644 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1389,6 +1389,12 @@ mbus_recv_frame(mbus_handle * handle, mbus_frame *frame) return 0; } + if (frame == NULL) + { + MBUS_ERROR("%s: Invalid frame.\n", __PRETTY_FUNCTION__); + return 0; + } + if (handle->is_serial) { result = mbus_serial_recv_frame(handle->m_serial_handle, frame); @@ -1594,6 +1600,8 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m // next_frame = reply; + memset((void *)&reply_data, 0, sizeof(mbus_frame_data)); + while (more_frames) { frame_count++; @@ -1601,7 +1609,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; @@ -1905,7 +1913,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__); From d025b27b07ee4823a34651cc62762437279d16c7 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 20 Jun 2012 00:23:25 +0200 Subject: [PATCH 2/5] - fixed check of mbus_sendrecv_request and mbus_recv_frame - initialized memory in mbus_sendrecv_request - used defines for special address instead of magic numbers --- bin/mbus-serial-request-data-multi-reply.c | 4 ++-- bin/mbus-serial-request-data.c | 2 +- bin/mbus-serial-scan-secondary.c | 4 ++-- bin/mbus-tcp-request-data-multi-reply.c | 2 +- bin/mbus-tcp-request-data.c | 2 +- bin/mbus-tcp-scan-secondary.c | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index 9dd1648..5666527 100644 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -98,7 +98,7 @@ main(int argc, char **argv) } frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (debug) printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); @@ -147,7 +147,7 @@ main(int argc, char **argv) // instead of the send and recv, use this sendrecv function that // takes care of the possibility of multi-telegram replies (limit = 16 frames) - if (mbus_sendrecv_request(handle, address, &reply, 16) == -1) + if (mbus_sendrecv_request(handle, address, &reply, 16) != 0) { fprintf(stderr, "Failed to send/receive M-Bus request.\n"); return 1; diff --git a/bin/mbus-serial-request-data.c b/bin/mbus-serial-request-data.c index d60a777..219a49d 100644 --- 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-secondary.c b/bin/mbus-serial-scan-secondary.c index 92dc3d3..960c066 100644 --- a/bin/mbus-serial-scan-secondary.c +++ b/bin/mbus-serial-scan-secondary.c @@ -127,7 +127,7 @@ main(int argc, char **argv) // init slaves // frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFD; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (mbus_send_frame(handle, frame) == -1) { @@ -139,7 +139,7 @@ main(int argc, char **argv) (void) mbus_recv_frame(handle, &reply); frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFF; + frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; if (mbus_send_frame(handle, frame) == -1) { diff --git a/bin/mbus-tcp-request-data-multi-reply.c b/bin/mbus-tcp-request-data-multi-reply.c index e06c022..7c165c0 100644 --- a/bin/mbus-tcp-request-data-multi-reply.c +++ b/bin/mbus-tcp-request-data-multi-reply.c @@ -130,7 +130,7 @@ main(int argc, char **argv) // instead of the send and recv, use this sendrecv function that // takes care of the possibility of multi-telegram replies (limit = 16 frames) - if (mbus_sendrecv_request(handle, address, &reply, 16) == -1) + if (mbus_sendrecv_request(handle, address, &reply, 16) != 0) { fprintf(stderr, "Failed to send/receive M-Bus request.\n"); return 1; diff --git a/bin/mbus-tcp-request-data.c b/bin/mbus-tcp-request-data.c index 7b40f28..fce7b7b 100644 --- a/bin/mbus-tcp-request-data.c +++ b/bin/mbus-tcp-request-data.c @@ -111,7 +111,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-tcp-scan-secondary.c b/bin/mbus-tcp-scan-secondary.c index e2cca2a..6a97484 100644 --- a/bin/mbus-tcp-scan-secondary.c +++ b/bin/mbus-tcp-scan-secondary.c @@ -75,7 +75,7 @@ main(int argc, char **argv) // init slaves // frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFD; + frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (mbus_send_frame(handle, frame) == -1) { @@ -88,7 +88,7 @@ main(int argc, char **argv) sleep(1); frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; - frame->address = 0xFF; + frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY; if (mbus_send_frame(handle, frame) == -1) { From 1fa5e202fb5af6c343d0597693185aa406dda7a9 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 4 Jul 2012 21:14:00 +0200 Subject: [PATCH 3/5] Fix problem with multiple selected devices (secondary address) resend --- bin/mbus-serial-request-data-multi-reply.c | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index 5666527..36831bc 100644 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -101,7 +101,7 @@ main(int argc, char **argv) frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (debug) - printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); + printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); if (mbus_send_frame(handle, frame) == -1) { @@ -110,8 +110,27 @@ main(int argc, char **argv) return 1; } - mbus_recv_frame(handle, &reply); + while (mbus_recv_frame(handle, &reply) != -1); + // + // 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; + } + + while (mbus_recv_frame(handle, &reply) != -1); + if (strlen(addr_str) == 16) { // secondary addressing From 736181b35e98247d985f451ea9b52f6ea8f0562a Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 4 Jul 2012 21:15:55 +0200 Subject: [PATCH 4/5] Revert "Fix problem with multiple selected devices (secondary address) resend " This reverts commit 1fa5e202fb5af6c343d0597693185aa406dda7a9. --- bin/mbus-serial-request-data-multi-reply.c | 23 ++-------------------- 1 file changed, 2 insertions(+), 21 deletions(-) diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index 36831bc..5666527 100644 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -101,7 +101,7 @@ main(int argc, char **argv) frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (debug) - printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); + printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); if (mbus_send_frame(handle, frame) == -1) { @@ -110,27 +110,8 @@ main(int argc, char **argv) return 1; } - while (mbus_recv_frame(handle, &reply) != -1); + 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_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; - } - - while (mbus_recv_frame(handle, &reply) != -1); - if (strlen(addr_str) == 16) { // secondary addressing From 433744c80a4043a2ab47d66efdbb9bc69ce9c153 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 4 Jul 2012 21:21:01 +0200 Subject: [PATCH 5/5] Fix problem with multiple selected devices (secondary address) - resend SND_NKE to ensure all devices are deselected, because the first SND_NKE could get lost --- bin/mbus-serial-request-data-multi-reply.c | 23 ++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/bin/mbus-serial-request-data-multi-reply.c b/bin/mbus-serial-request-data-multi-reply.c index 5666527..36831bc 100644 --- a/bin/mbus-serial-request-data-multi-reply.c +++ b/bin/mbus-serial-request-data-multi-reply.c @@ -101,7 +101,7 @@ main(int argc, char **argv) frame->address = MBUS_ADDRESS_NETWORK_LAYER; if (debug) - printf("%s: debug: sending init frame\n", __PRETTY_FUNCTION__); + printf("%s: debug: sending init frame #1\n", __PRETTY_FUNCTION__); if (mbus_send_frame(handle, frame) == -1) { @@ -110,8 +110,27 @@ main(int argc, char **argv) return 1; } - mbus_recv_frame(handle, &reply); + while (mbus_recv_frame(handle, &reply) != -1); + // + // 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; + } + + while (mbus_recv_frame(handle, &reply) != -1); + if (strlen(addr_str) == 16) { // secondary addressing