From 08a4c554435813440b8b103ce4e07890087cdd31 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sat, 14 Jul 2012 16:22:25 +0200 Subject: [PATCH] - Initialize maximum retry counter to 3 for serial and tcp connections - Retransmit request frame (just for mbus_sendrecv_request) in case of timeout or invalid response --- mbus/mbus-protocol-aux.c | 75 ++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index f7f9dc9..fc9a147 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1330,6 +1330,7 @@ mbus_context_serial(const char *device) return NULL; } + handle->max_retry = 3; handle->is_serial = 1; handle->auxdata = serial_data; handle->open = mbus_serial_connect; @@ -1371,6 +1372,7 @@ mbus_context_tcp(const char *host, int port) return NULL; } + handle->max_retry = 3; handle->is_serial = 0; handle->auxdata = tcp_data; handle->open = mbus_tcp_connect; @@ -1616,10 +1618,10 @@ mbus_send_request_frame(mbus_handle * handle, int address) int mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int max_frames) { - int retval = 0, more_frames = 1; + int retval = 0, more_frames = 1, retry = 0; mbus_frame_data reply_data; mbus_frame *frame, *next_frame; - int frame_count = 0; + int frame_count = 0, result; frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT); @@ -1635,16 +1637,6 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m MBUS_CONTROL_MASK_FCB; frame->address = address; - - if (debug) - printf("%s: debug: sending request frame\n", __PRETTY_FUNCTION__); - - if (mbus_send_frame(handle, frame) == -1) - { - MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__); - mbus_frame_free(frame); - return -1; - } // // continue to read until no more records are available (usually only one @@ -1656,18 +1648,54 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m while (more_frames) { - frame_count++; - + if (retry > handle->max_retry) + { + // Give up + retval = 1; + break; + } + + if (debug) + printf("%s: debug: sending request frame\n", __PRETTY_FUNCTION__); + + if (mbus_send_frame(handle, frame) == -1) + { + MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__); + retval = -1; + break; + } + if (debug) printf("%s: debug: receiving response frame #%d\n", __PRETTY_FUNCTION__, frame_count); - - if (mbus_recv_frame(handle, next_frame) != MBUS_RECV_RESULT_OK) + + result = mbus_recv_frame(handle, next_frame); + + if (result == MBUS_RECV_RESULT_OK) + { + mbus_purge_frames(handle); + } + else if (result == MBUS_RECV_RESULT_TIMEOUT) + { + MBUS_ERROR("%s: No M-Bus response frame received.\n", __PRETTY_FUNCTION__); + retry++; + continue; + } + else if (result == MBUS_RECV_RESULT_INVALID) + { + MBUS_ERROR("%s: Received invalid M-Bus response frame.\n", __PRETTY_FUNCTION__); + retry++; + mbus_purge_frames(handle); + continue; + } + else { MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); retval = 1; break; } + frame_count++; + // // We need to parse the data in the received frame to be able to tell // if more records are available or not. @@ -1713,20 +1741,9 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m } next_frame = next_frame->next; - - // need to send a new request and receive another reply - - if (debug) - printf("%s: debug: resending request frame\n", __PRETTY_FUNCTION__); - - // toogle FCB bit before + + // toogle FCB bit frame->control ^= MBUS_CONTROL_MASK_FCB; - if (mbus_send_frame(handle, frame) == -1) - { - MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__); - retval = -1; - more_frames = 0; - } } else {