diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 6f96d87..ead7cd0 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1811,6 +1811,57 @@ mbus_send_request_frame(mbus_handle * handle, int address) return retval; } +//------------------------------------------------------------------------------ +// send a user data packet from master to slave +//------------------------------------------------------------------------------ +int +mbus_send_user_data_frame(mbus_handle * handle, int address, const unsigned char *data, size_t data_size) +{ + int retval = 0; + mbus_frame *frame; + + if (mbus_is_primary_address(address) == 0) + { + MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, address); + return -1; + } + + if (data == NULL) + { + MBUS_ERROR("%s: Invalid data\n", __PRETTY_FUNCTION__); + return -1; + } + + if ((data_size > MBUS_FRAME_DATA_LENGTH) || (data_size == 0)) + { + MBUS_ERROR("%s: illegal data_size %d\n", __PRETTY_FUNCTION__, data_size); + return -1; + } + + frame = mbus_frame_new(MBUS_FRAME_TYPE_LONG); + + if (frame == NULL) + { + MBUS_ERROR("%s: failed to allocate mbus frame.\n", __PRETTY_FUNCTION__); + return -1; + } + + frame->control = MBUS_CONTROL_MASK_SND_UD | MBUS_CONTROL_MASK_DIR_M2S; + frame->address = address; + frame->control_information = MBUS_CONTROL_INFO_DATA_SEND; + frame->data_size = data_size; + memcpy(frame->data, data, data_size); + + if (mbus_send_frame(handle, frame) == -1) + { + MBUS_ERROR("%s: failed to send mbus frame.\n", __PRETTY_FUNCTION__); + retval = -1; + } + + mbus_frame_free(frame); + return retval; +} + //------------------------------------------------------------------------------ // send a request from master to slave and collect the reply (replies) // from the slave. diff --git a/mbus/mbus-protocol-aux.h b/mbus/mbus-protocol-aux.h index c2a255a..08093a6 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -287,6 +287,18 @@ int mbus_send_switch_baudrate_frame(mbus_handle * handle, int address, long baud */ int mbus_send_request_frame(mbus_handle * handle, int address); +/** + * Sends user data frame (SND_UD) to given slave using "unified" handle + * + * @param handle Initialized handle + * @param address Address (0-255) + * @param data User data + * @param data_size Byte count of user data + * + * @return Zero when successful. + */ +int mbus_send_user_data_frame(mbus_handle * handle, int address, const unsigned char *data, size_t data_size); + /** * Sends a request and read replies until no more records available * or limit is reached.