Merge pull request #17 from lategoodbye/master

Retransmission of request frames
This commit is contained in:
Robert Johansson 2012-07-14 08:40:04 -07:00
commit 36c46d18c2
3 changed files with 47 additions and 31 deletions

View File

@ -91,7 +91,6 @@ main(int argc, char **argv)
}
(void) mbus_recv_frame(handle, &reply);
sleep(1);
frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S;
frame->address = MBUS_ADDRESS_BROADCAST_NOREPLY;
@ -104,7 +103,6 @@ main(int argc, char **argv)
}
(void) mbus_recv_frame(handle, &reply);
sleep(1);
mbus_scan_2nd_address_range(handle, 0, addr_mask);

View File

@ -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);
@ -1636,16 +1638,6 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m
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
// reply frame, but can be more for so-called multi-telegram replies)
@ -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.
@ -1714,19 +1742,8 @@ 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
{

View File

@ -75,6 +75,7 @@
*/
struct _mbus_handle {
int fd;
int max_retry;
char is_serial; /**< _handle type (non zero for serial) */
int (*open) (struct _mbus_handle *handle);
int (*close) (struct _mbus_handle *handle);