From e3098bbaa2792b544f3e4925343dc4a8ecc96b27 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Fri, 16 Aug 2024 14:13:56 -0500 Subject: [PATCH] Added device WriteProperty callback for non-volatile storing in basic device examples. (#728) --- src/bacnet/basic/object/device.c | 24 +++++++++++++++++ src/bacnet/basic/object/device.h | 3 +++ zephyr/subsys/bacnet_basic/device.c | 40 ++++++++++++----------------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/src/bacnet/basic/object/device.c b/src/bacnet/basic/object/device.c index afef656c..c9c09ecf 100644 --- a/src/bacnet/basic/object/device.c +++ b/src/bacnet/basic/object/device.c @@ -591,6 +591,7 @@ static uint32_t Database_Revision = 0; /* Profile_Name */ static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; static const char *Reinit_Password = "filister"; +static write_property_function Device_Write_Property_Store_Callback; /** * @brief Sets the ReinitializeDevice password @@ -1901,6 +1902,26 @@ static bool Device_Write_Property_Object_Name( return status; } +/** + * @brief Set the callback for a WriteProperty successful operation + * @param cb [in] The function to be called, or NULL to disable + */ +void Device_Write_Property_Store_Callback_Set( + write_property_function cb) +{ + Device_Write_Property_Store_Callback = cb; +} + +/** + * @brief Store the value of a property when WriteProperty is successful + */ +static void Device_Write_Property_Store(BACNET_WRITE_PROPERTY_DATA *wp_data) +{ + if (Device_Write_Property_Store_Callback) { + (void)Device_Write_Property_Store_Callback(wp_data); + } +} + /** Looks up the requested Object and Property, and set the new Value in it, * if allowed. * If the Object or Property can't be found, sets the error class and code. @@ -1936,6 +1957,9 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) } else { status = pObject->Object_Write_Property(wp_data); } + if (status) { + Device_Write_Property_Store(wp_data); + } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; diff --git a/src/bacnet/basic/object/device.h b/src/bacnet/basic/object/device.h index fa91db0f..6bf1c839 100644 --- a/src/bacnet/basic/object/device.h +++ b/src/bacnet/basic/object/device.h @@ -471,6 +471,9 @@ extern "C" { BACNET_STACK_EXPORT bool Device_Write_Property_Local( BACNET_WRITE_PROPERTY_DATA * wp_data); + BACNET_STACK_EXPORT + void Device_Write_Property_Store_Callback_Set( + write_property_function cb); #if defined(INTRINSIC_REPORTING) BACNET_STACK_EXPORT diff --git a/zephyr/subsys/bacnet_basic/device.c b/zephyr/subsys/bacnet_basic/device.c index cd310a46..29655f6b 100644 --- a/zephyr/subsys/bacnet_basic/device.c +++ b/zephyr/subsys/bacnet_basic/device.c @@ -350,6 +350,7 @@ static uint32_t Database_Revision; static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; static BACNET_CHARACTER_STRING Reinit_Password; static const char *BACnet_Version = BACNET_VERSION_TEXT; +static write_property_function Device_Write_Property_Store_Callback; /* These three arrays are used by the ReadPropertyMultiple handler */ static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, @@ -1386,33 +1387,24 @@ static bool Device_Write_Property_Object_Name( return status; } -void Device_Write_Property_Store(BACNET_WRITE_PROPERTY_DATA *wp_data) +/** + * @brief Set the callback for a WriteProperty successful operation + * @param cb [in] The function to be called, or NULL to disable + */ +void Device_Write_Property_Store_Callback_Set( + write_property_function cb) { - BACNET_ARRAY_INDEX array_index; - BACNET_PROPERTY_ID object_property; + Device_Write_Property_Store_Callback = cb; +} - if (property_list_bacnet_array_member(wp_data->object_type, - wp_data->object_property)) { - array_index = wp_data->array_index; - object_property = wp_data->object_property; - } else if (wp_data->object_property == PROP_PRESENT_VALUE) { - /* indirect Priority_Array write */ - if (Device_Objects_Property_List_Member(wp_data->object_type, - wp_data->object_instance, - PROP_PRIORITY_ARRAY)) { - array_index = wp_data->priority; - object_property = PROP_PRIORITY_ARRAY; - } else { - object_property = wp_data->object_property; - array_index = BACNET_ARRAY_ALL; - } - } else { - object_property = wp_data->object_property; - array_index = wp_data->array_index; +/** + * @brief Store the value of a property when WriteProperty is successful + */ +static void Device_Write_Property_Store(BACNET_WRITE_PROPERTY_DATA *wp_data) +{ + if (Device_Write_Property_Store_Callback) { + (void)Device_Write_Property_Store_Callback(wp_data); } - //bacnet_data_store(wp_data->object_type, wp_data->object_instance, - // object_property, array_index, wp_data->application_data, - // wp_data->application_data_len); } /** Looks up the requested Object and Property, and set the new Value in it,