Fixed CreateObject service list-of-initial-values encoding and decoding. (#1199)

Fixes the CreateObject service list-of-initial-values encoding and decoding by refactoring the data structure to be similar to WriteProperty. The implementation changes from using a linked list of property values to using a flat buffer approach with delayed decoding.

Changes:

* Refactored BACNET_CREATE_OBJECT_DATA structure to use an application_data buffer instead of a linked list for initial values
* Added a new create_object_process() and create_object_initializer_list_process() functions to centralize object creation logic and error reporting
* Updated all device implementations to use the new centralized creation functions
* Enhanced the create-object example application to support command-line specification of initial property values
* Added comprehensive test coverage for the new encoding/decoding and processing functions
This commit is contained in:
Steve Karg
2026-01-15 14:50:59 -06:00
committed by GitHub
parent 21daea9eb4
commit 5802bd0ecc
18 changed files with 921 additions and 212 deletions
@@ -92,7 +92,9 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/datalink/bvlc.c
${SRC_DIR}/bacnet/datalink/bvlc6.c
${SRC_DIR}/bacnet/cov.c
${SRC_DIR}/bacnet/create_object.c
${SRC_DIR}/bacnet/datetime.c
${SRC_DIR}/bacnet/delete_object.c
${SRC_DIR}/bacnet/dcc.c
${SRC_DIR}/bacnet/indtext.c
${SRC_DIR}/bacnet/hostnport.c
@@ -97,7 +97,9 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/datalink/bvlc.c
${SRC_DIR}/bacnet/datalink/bvlc6.c
${SRC_DIR}/bacnet/cov.c
${SRC_DIR}/bacnet/create_object.c
${SRC_DIR}/bacnet/datetime.c
${SRC_DIR}/bacnet/delete_object.c
${SRC_DIR}/bacnet/dcc.c
${SRC_DIR}/bacnet/indtext.c
${SRC_DIR}/bacnet/hostnport.c
@@ -376,7 +376,7 @@ static void testDevice(void)
count = Device_Object_List_Count();
create_data.object_type = OBJECT_ANALOG_VALUE;
create_data.object_instance = BACNET_MAX_INSTANCE;
create_data.list_of_initial_values = NULL;
create_data.application_data_len = 0;
create_data.error_class = ERROR_CLASS_DEVICE;
create_data.error_code = ERROR_CODE_SUCCESS;
create_data.first_failed_element_number = 0;
@@ -410,7 +410,7 @@ static void testDevice(void)
/* known object without create */
create_data.object_type = OBJECT_DEVICE;
create_data.object_instance = BACNET_MAX_INSTANCE;
create_data.list_of_initial_values = NULL;
create_data.application_data_len = 0;
create_data.error_class = ERROR_CLASS_DEVICE;
create_data.error_code = ERROR_CODE_SUCCESS;
create_data.first_failed_element_number = 0;
+2
View File
@@ -53,6 +53,7 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/indtext.c
${SRC_DIR}/bacnet/hostnport.c
${SRC_DIR}/bacnet/lighting.c
${SRC_DIR}/bacnet/proplist.c
${SRC_DIR}/bacnet/shed_level.c
${SRC_DIR}/bacnet/timer_value.c
${SRC_DIR}/bacnet/timestamp.c
@@ -64,6 +65,7 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/special_event.c
${SRC_DIR}/bacnet/channel_value.c
${SRC_DIR}/bacnet/secure_connect.c
${SRC_DIR}/bacnet/wp.c
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c
+196 -3
View File
@@ -10,15 +10,64 @@
#include <zephyr/ztest.h>
#include <bacnet/bacdest.h>
#include <bacnet/create_object.h>
#include <bacnet/bactext.h>
static uint32_t Test_Create_Object_Returned_Instance = BACNET_MAX_INSTANCE;
static uint32_t Test_Delete_Object_Instance = BACNET_MAX_INSTANCE;
static BACNET_WRITE_PROPERTY_DATA Test_Write_Property_Data;
static bool Test_Write_Property_Return_Status;
/**
* @brief CreateObject service handler for an object
* @ingroup ObjHelpers
* @param object_instance [in] instance number of the object to create,
* or BACNET_MAX_INSTANCE to create the next free object instance
* @return object instance number created, or BACNET_MAX_INSTANCE if not
*/
static uint32_t test_create_object_function(uint32_t object_instance)
{
(void)object_instance;
return Test_Create_Object_Returned_Instance;
}
/**
* @brief DeleteObject service handler for an object
* @ingroup ObjHelpers
* @param object_instance [in] instance number of the object to delete
* @return true if the object instance is deleted
*/
static bool test_delete_object_function(uint32_t object_instance)
{
Test_Delete_Object_Instance = object_instance;
return true;
}
/** Attempts to write a new value to one property for this object type
* of a given instance.
* A function template; @see device.c for assignment to object types.
* @ingroup ObjHelpers
*
* @param wp_data [in] Pointer to the BACnet_Write_Property_Data structure,
* which is packed with the information from the WP request.
* @return The length of the apdu encoded or -1 for error or
* -2 for abort message.
*/
static bool test_write_property_function(BACNET_WRITE_PROPERTY_DATA *wp_data)
{
Test_Write_Property_Data = *wp_data;
return Test_Write_Property_Return_Status;
}
static void test_CreateObjectCodec(BACNET_CREATE_OBJECT_DATA *data)
{
uint8_t apdu[MAX_APDU] = { 0 };
BACNET_CREATE_OBJECT_DATA test_data = { 0 };
int len = 0, apdu_len = 0, null_len = 0, test_len = 0;
bool object_supported = false, object_exists = false, status = false;
null_len = create_object_encode_service_request(NULL, data);
apdu_len = create_object_encode_service_request(apdu, data);
test_len = create_object_service_request_encode(NULL, 0, data);
zassert_equal(test_len, 0, NULL);
null_len = create_object_service_request_encode(NULL, sizeof(apdu), data);
apdu_len = create_object_service_request_encode(apdu, sizeof(apdu), data);
zassert_equal(apdu_len, null_len, NULL);
zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL);
null_len = create_object_decode_service_request(apdu, apdu_len, NULL);
@@ -29,9 +78,101 @@ static void test_CreateObjectCodec(BACNET_CREATE_OBJECT_DATA *data)
while (test_len) {
test_len--;
len = create_object_decode_service_request(apdu, test_len, &test_data);
if (data->object_instance == BACNET_MAX_INSTANCE) {
if (test_len == 4) {
/* optional list-of-values */
continue;
}
} else {
if (test_len == 7) {
/* optional list-of-values */
continue;
}
}
zassert_equal(
len, BACNET_STATUS_REJECT, "len=%d test_len=%d", len, test_len);
}
/* test service processing */
apdu_len = create_object_service_request_encode(apdu, sizeof(apdu), data);
test_len = create_object_decode_service_request(apdu, apdu_len, &test_data);
zassert_equal(test_len, apdu_len, NULL);
/* processing - error case */
status = create_object_process(
NULL, object_supported, object_exists, NULL, NULL, NULL);
zassert_equal(status, false, NULL);
/* processing - error case */
status = create_object_process(
data, object_supported, object_exists, NULL, NULL, NULL);
zassert_equal(status, false, NULL);
zassert_equal(data->error_class, ERROR_CLASS_OBJECT, NULL);
zassert_equal(data->error_code, ERROR_CODE_UNSUPPORTED_OBJECT_TYPE, NULL);
/* processing - error case */
object_supported = true;
object_exists = true;
status = create_object_process(
data, object_supported, object_exists, test_create_object_function,
test_delete_object_function, test_write_property_function);
zassert_equal(status, false, NULL);
zassert_equal(data->error_class, ERROR_CLASS_OBJECT, NULL);
zassert_equal(
data->error_code, ERROR_CODE_OBJECT_IDENTIFIER_ALREADY_EXISTS, NULL);
/* processing - error case */
object_supported = true;
object_exists = false;
status = create_object_process(
data, object_supported, object_exists, NULL,
test_delete_object_function, test_write_property_function);
zassert_equal(status, false, NULL);
zassert_equal(data->error_class, ERROR_CLASS_OBJECT, NULL);
zassert_equal(
data->error_code, ERROR_CODE_DYNAMIC_CREATION_NOT_SUPPORTED, NULL);
/* processing - error case */
object_supported = true;
object_exists = false;
Test_Create_Object_Returned_Instance = BACNET_MAX_INSTANCE;
status = create_object_process(
data, object_supported, object_exists, test_create_object_function,
test_delete_object_function, test_write_property_function);
zassert_equal(status, false, NULL);
zassert_equal(data->error_class, ERROR_CLASS_RESOURCES, NULL);
zassert_equal(data->error_code, ERROR_CODE_NO_SPACE_FOR_OBJECT, NULL);
/* processing - error case */
object_supported = true;
object_exists = false;
Test_Create_Object_Returned_Instance = 12345;
Test_Write_Property_Return_Status = false;
status = create_object_process(
data, object_supported, object_exists, test_create_object_function,
test_delete_object_function, test_write_property_function);
if (data->application_data_len) {
zassert_equal(status, false, NULL);
zassert_equal(
data->error_class, ERROR_CLASS_PROPERTY, "error_class=%s",
bactext_error_class_name(data->error_class));
zassert_equal(
data->error_code, ERROR_CODE_WRITE_ACCESS_DENIED, "error_code=%s",
bactext_error_code_name(data->error_code));
} else {
zassert_equal(status, true, NULL);
}
/* processing - successful case */
object_supported = true;
object_exists = false;
if (data->object_instance != BACNET_MAX_INSTANCE) {
Test_Create_Object_Returned_Instance = data->object_instance;
} else {
Test_Create_Object_Returned_Instance = 12345;
}
Test_Write_Property_Return_Status = true;
status = create_object_process(
data, object_supported, object_exists, test_create_object_function,
test_delete_object_function, test_write_property_function);
zassert_equal(status, true, NULL);
zassert_equal(
data->object_instance, Test_Create_Object_Returned_Instance, NULL);
zassert_equal(
data->first_failed_element_number, 0, "first_failed_element_number=%u",
data->first_failed_element_number);
}
#if defined(CONFIG_ZTEST_NEW_API)
@@ -40,8 +181,55 @@ ZTEST(create_object_tests, test_CreateObject)
static void test_CreateObject(void)
#endif
{
BACNET_CREATE_OBJECT_DATA data = { 0 };
BACNET_CREATE_OBJECT_DATA data = {
.object_instance = 1,
.object_type = OBJECT_ANALOG_OUTPUT,
.application_data_len = 0,
.application_data = { 0 },
.error_class = ERROR_CLASS_PROPERTY,
.error_code = ERROR_CODE_SUCCESS,
};
BACNET_PROPERTY_VALUE value[2] = {
{ .next = NULL,
.priority = BACNET_NO_PRIORITY,
.propertyArrayIndex = BACNET_ARRAY_ALL,
.propertyIdentifier = PROP_OBJECT_NAME,
.value = { .tag = BACNET_APPLICATION_TAG_CHARACTER_STRING,
.type.Character_String = { .encoding = CHARACTER_UTF8,
.length = 4,
.value = "Test" } } },
{ .next = NULL,
.priority = 1,
.propertyArrayIndex = BACNET_ARRAY_ALL,
.propertyIdentifier = PROP_PRESENT_VALUE,
.value = { .tag = BACNET_APPLICATION_TAG_REAL, .type.Real = 42.0 } },
};
int len = 0;
/* encode two initial values */
len = create_object_encode_initial_value(
data.application_data, len, &value[0]);
data.application_data_len += len;
len = create_object_encode_initial_value(
data.application_data, len, &value[1]);
data.application_data_len += len;
/* test encoding and decoding of CreateObject service */
test_CreateObjectCodec(&data);
data.object_instance = BACNET_MAX_INSTANCE;
test_CreateObjectCodec(&data);
/* validate the last write */
zassert_equal(
Test_Write_Property_Data.object_instance, data.object_instance, NULL);
zassert_equal(Test_Write_Property_Data.object_type, data.object_type, NULL);
zassert_equal(
Test_Write_Property_Data.array_index, value[1].propertyArrayIndex,
NULL);
zassert_equal(
Test_Write_Property_Data.object_property, value[1].propertyIdentifier,
NULL);
/* test with no initial values */
data.object_instance = 1;
data.application_data_len = 0;
test_CreateObjectCodec(&data);
data.object_instance = BACNET_MAX_INSTANCE;
test_CreateObjectCodec(&data);
@@ -105,6 +293,11 @@ static void test_CreateObjectError(void)
null_len = create_object_error_ack_service_encode(NULL, &data);
apdu_len = create_object_error_ack_service_encode(apdu, &data);
zassert_equal(apdu_len, null_len, NULL);
null_len =
create_object_error_ack_service_decode(NULL, apdu_len, &test_data);
zassert_equal(null_len, BACNET_STATUS_REJECT, NULL);
null_len = create_object_error_ack_service_decode(apdu, apdu_len, NULL);
zassert_equal(apdu_len, null_len, NULL);
test_len =
create_object_error_ack_service_decode(apdu, apdu_len, &test_data);
zassert_equal(apdu_len, test_len, NULL);