From d6c5529247d326967962895b34606f2b1e2874ca Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 3 Oct 2012 23:01:44 +0200 Subject: [PATCH] new function mbus_frame_direction to get the direction of a frame (slave to master or vice versa) extended mbus_frame_data_parse to check the direction of a frame --- mbus/mbus-protocol.c | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/mbus/mbus-protocol.c b/mbus/mbus-protocol.c index 59736e9..dee306e 100755 --- a/mbus/mbus-protocol.c +++ b/mbus/mbus-protocol.c @@ -300,6 +300,19 @@ mbus_frame_type(mbus_frame *frame) return -1; } +//------------------------------------------------------------------------------ +/// Return the M-Bus frame direction +//------------------------------------------------------------------------------ +int +mbus_frame_direction(mbus_frame *frame) +{ + if (frame) + { + return (frame->control & MBUS_CONTROL_MASK_DIR); + } + return -1; +} + //------------------------------------------------------------------------------ /// Verify that parsed frame is a valid M-bus frame. // @@ -2713,7 +2726,23 @@ mbus_data_variable_parse(mbus_frame *frame, mbus_data_variable *data) int mbus_frame_data_parse(mbus_frame *frame, mbus_frame_data *data) { - if (frame && data) + char direction; + + if (frame == NULL) + { + snprintf(error_str, sizeof(error_str), "Got null pointer to frame."); + return -1; + } + + if (data == NULL) + { + snprintf(error_str, sizeof(error_str), "Got null pointer to data."); + return -1; + } + + direction = (frame->control & MBUS_CONTROL_MASK_DIR); + + if (direction == MBUS_CONTROL_MASK_DIR_S2M) { if (frame->control_information == MBUS_CONTROL_INFO_ERROR_GENERAL) { @@ -2761,9 +2790,13 @@ mbus_frame_data_parse(mbus_frame *frame, mbus_frame_data *data) return -1; } } - - snprintf(error_str, sizeof(error_str), "Got null pointer to frame or data."); - + else + { + snprintf(error_str, sizeof(error_str), "Wrong direction in frame (master to slave)"); + + return -1; + } + return -1; }