From 42ed80a5b03d20b520672d6899a63e834478a569 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Sat, 28 Apr 2012 14:09:58 +0200 Subject: [PATCH] - init slave to get always the beginning of the records - start read with FCB set - fixed double free in case of send failure --- mbus/mbus-protocol-aux.c | 41 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 0b36ff7..d04e4d3 100644 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1369,9 +1369,44 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m return -1; } - frame->control = MBUS_CONTROL_MASK_REQ_UD2 | MBUS_CONTROL_MASK_DIR_M2S | MBUS_CONTROL_MASK_FCV; + // + // init slave to get really the beginning of the records + // + + frame->control = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; frame->address = address; - + + if (debug) + printf("%s: debug: sending init 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; + } + + if (mbus_recv_frame(handle, reply) == -1) + { + MBUS_ERROR("%s: Failed to receive M-Bus response frame.\n", __PRETTY_FUNCTION__); + mbus_frame_free(frame); + return -1; + } + + if (mbus_frame_type(reply) != MBUS_FRAME_TYPE_ACK) + { + MBUS_ERROR("%s: Unknown reply.\n", __PRETTY_FUNCTION__); + mbus_frame_free(frame); + return -1; + } + + frame->control = MBUS_CONTROL_MASK_REQ_UD2 | + MBUS_CONTROL_MASK_DIR_M2S | + MBUS_CONTROL_MASK_FCV | + MBUS_CONTROL_MASK_FCB; + + frame->address = address; + if (debug) printf("%s: debug: sending request frame\n", __PRETTY_FUNCTION__); @@ -1379,7 +1414,7 @@ mbus_sendrecv_request(mbus_handle *handle, int address, mbus_frame *reply, int m { MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__); mbus_frame_free(frame); - retval = -1; + return -1; } //