From 73d58a9f7dcf2e3877a51aa083bbbbd0952568f3 Mon Sep 17 00:00:00 2001 From: Stefan Wahren Date: Wed, 21 Mar 2018 21:29:44 +0100 Subject: [PATCH] Implement mbus_set_primary_address() This function sends a frame to change primary address of a slave. --- mbus/mbus-protocol-aux.c | 27 +++++++++++++++++++++++++++ mbus/mbus-protocol-aux.h | 11 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index ed29562..ea21e74 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -1981,6 +1981,33 @@ mbus_send_user_data_frame(mbus_handle * handle, int address, const unsigned char return retval; } +//------------------------------------------------------------------------------ +// send a request from master to slave in order to change the primary address +//------------------------------------------------------------------------------ +int +mbus_set_primary_address(mbus_handle * handle, int old_address, int new_address) +{ + /* primary address record, see chapter 6.4.2 */ + unsigned char buffer[3] = { 0x01, 0x7A, new_address }; + + if (mbus_is_primary_address(new_address) == 0) + { + MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, new_address); + return -1; + } + + switch (new_address) + { + case MBUS_ADDRESS_NETWORK_LAYER: + case MBUS_ADDRESS_BROADCAST_REPLY: + case MBUS_ADDRESS_BROADCAST_NOREPLY: + MBUS_ERROR("%s: invalid address %d\n", __PRETTY_FUNCTION__, new_address); + return -1; + } + + return mbus_send_user_data_frame(handle, old_address, buffer, sizeof(buffer)); +} + //------------------------------------------------------------------------------ // 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 703bc23..4dacef4 100755 --- a/mbus/mbus-protocol-aux.h +++ b/mbus/mbus-protocol-aux.h @@ -307,6 +307,17 @@ int mbus_send_request_frame(mbus_handle * handle, int address); */ int mbus_send_user_data_frame(mbus_handle * handle, int address, const unsigned char *data, size_t data_size); +/** + * Sends frame to change primary address of given slave using "unified" handle + * + * @param handle Initialized handle + * @param old_address Old Address (0-255) + * @param new_address New Address (0-250) + * + * @return Zero when successful. + */ +int mbus_set_primary_address(mbus_handle * handle, int old_address, int new_address); + /** * Sends a request and read replies until no more records available * or limit is reached.