Feature/oscbs 29 calendar time object (#440)

* Added basic Calendar object, unit tests, and integration with example device object.

* Added basic Time Value object, unit tests, and integration with example device object.

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Mikhail Antropov
2024-02-02 08:01:27 +03:00
committed by GitHub
parent 340bd09561
commit 8f576461a8
38 changed files with 2574 additions and 45 deletions
+4 -2
View File
@@ -27,17 +27,19 @@ static void testBinaryLightingOutput(void)
uint8_t apdu[MAX_APDU] = { 0 };
int len = 0, test_len = 0;
BACNET_READ_PROPERTY_DATA rpdata;
BACNET_WRITE_PROPERTY_DATA wpdata = { 0 };
BACNET_APPLICATION_DATA_VALUE value = { 0 };
const int *pRequired = NULL;
const int *pOptional = NULL;
const int *pProprietary = NULL;
const uint32_t instance = 123;
BACNET_WRITE_PROPERTY_DATA wpdata = { 0 };
uint32_t test_instance = 0;
bool status = false;
unsigned index;
Binary_Lighting_Output_Init();
Binary_Lighting_Output_Create(instance);
test_instance = Binary_Lighting_Output_Create(instance);
zassert_equal(test_instance, instance, NULL);
status = Binary_Lighting_Output_Valid_Instance(instance);
zassert_true(status, NULL);
index = Binary_Lighting_Output_Instance_To_Index(instance);
@@ -0,0 +1,69 @@
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(test_${basename}
VERSION 1.0.0
LANGUAGES C)
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z_/-]*$"
"/src"
SRC_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z_/-]*$"
"/test"
TST_DIR
${CMAKE_CURRENT_SOURCE_DIR})
set(ZTST_DIR "${TST_DIR}/ztest/src")
add_compile_definitions(
BIG_ENDIAN=0
CONFIG_ZTEST=1
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/basic/object/calendar.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacapp.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacdest.c
${SRC_DIR}/bacnet/bacdevobjpropref.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacreal.c
${SRC_DIR}/bacnet/bacstr.c
${SRC_DIR}/bacnet/bactext.c
${SRC_DIR}/bacnet/basic/sys/bigend.c
${SRC_DIR}/bacnet/basic/sys/keylist.c
${SRC_DIR}/bacnet/cov.c
${SRC_DIR}/bacnet/datetime.c
${SRC_DIR}/bacnet/basic/sys/days.c
${SRC_DIR}/bacnet/indtext.c
${SRC_DIR}/bacnet/hostnport.c
${SRC_DIR}/bacnet/lighting.c
${SRC_DIR}/bacnet/proplist.c
${SRC_DIR}/bacnet/timestamp.c
${SRC_DIR}/bacnet/memcopy.c
${SRC_DIR}/bacnet/wp.c
${SRC_DIR}/bacnet/weeklyschedule.c
${SRC_DIR}/bacnet/calendar_entry.c
${SRC_DIR}/bacnet/special_event.c
${SRC_DIR}/bacnet/bactimevalue.c
${SRC_DIR}/bacnet/dailyschedule.c
# Test and test library files
./src/main.c
./stubs.c
../mock/device_mock.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
@@ -0,0 +1,239 @@
/**
* @file
* @brief Unit test for BACnet Calendar object encode/decode APIs
* @author Mikhail Antropov <michail.antropov@dsr-corporation.com>
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2023
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/ztest.h>
#include <bacnet/basic/object/calendar.h>
#include <bacnet/bactext.h>
/**
* @addtogroup bacnet_tests
* @{
*/
/**
* @brief Test Calendar handling
*/
static void testCalendar(void)
{
uint8_t apdu[MAX_APDU] = { 0 };
int len = 0, test_len = 0;
BACNET_READ_PROPERTY_DATA rpdata = { 0 };
BACNET_WRITE_PROPERTY_DATA wpdata = { 0 };
BACNET_APPLICATION_DATA_VALUE value = {0};
const int *pRequired = NULL;
const int *pOptional = NULL;
const int *pProprietary = NULL;
const uint32_t instance = 1;
uint32_t test_instance = 0;
bool status = false;
unsigned index;
Calendar_Init();
test_instance = Calendar_Create(instance);
zassert_equal(test_instance, instance, NULL);
status = Calendar_Valid_Instance(instance);
zassert_true(status, NULL);
index = Calendar_Instance_To_Index(instance);
zassert_equal(index, 0, NULL);
rpdata.application_data = &apdu[0];
rpdata.application_data_len = sizeof(apdu);
rpdata.object_type = OBJECT_CALENDAR;
rpdata.object_instance = instance;
rpdata.array_index = BACNET_ARRAY_ALL;
Calendar_Property_Lists(&pRequired, &pOptional, &pProprietary);
while ((*pRequired) >= 0) {
rpdata.object_property = *pRequired;
rpdata.array_index = BACNET_ARRAY_ALL;
len = Calendar_Read_Property(&rpdata);
zassert_not_equal(len, BACNET_STATUS_ERROR,
"property '%s': failed to ReadProperty!\n",
bactext_property_name(rpdata.object_property));
if (len >= 0) {
test_len = bacapp_decode_known_property(rpdata.application_data,
len, &value, rpdata.object_type, rpdata.object_property);
zassert_equal(len, test_len,
"property '%s': failed to decode!\n",
bactext_property_name(rpdata.object_property));
/* check WriteProperty properties */
wpdata.object_type = rpdata.object_type;
wpdata.object_instance = rpdata.object_instance;
wpdata.object_property = rpdata.object_property;
wpdata.array_index = rpdata.array_index;
memcpy(&wpdata.application_data, rpdata.application_data, MAX_APDU);
wpdata.application_data_len = len;
wpdata.error_code = ERROR_CODE_SUCCESS;
status = Calendar_Write_Property(&wpdata);
if (!status) {
/* verify WriteProperty property is known */
zassert_not_equal(wpdata.error_code,
ERROR_CODE_UNKNOWN_PROPERTY,
"property '%s': WriteProperty Unknown!\n",
bactext_property_name(rpdata.object_property));
}
}
pRequired++;
}
while ((*pOptional) != -1) {
rpdata.object_property = *pOptional;
rpdata.array_index = BACNET_ARRAY_ALL;
len = Calendar_Read_Property(&rpdata);
zassert_not_equal(len, BACNET_STATUS_ERROR,
"property '%s': failed to ReadProperty!\n",
bactext_property_name(rpdata.object_property));
if (len > 0) {
test_len = bacapp_decode_application_data(rpdata.application_data,
(uint8_t)rpdata.application_data_len, &value);
zassert_equal(len, test_len, "property '%s': failed to decode!\n",
bactext_property_name(rpdata.object_property));
/* check WriteProperty properties */
wpdata.object_type = rpdata.object_type;
wpdata.object_instance = rpdata.object_instance;
wpdata.object_property = rpdata.object_property;
wpdata.array_index = rpdata.array_index;
memcpy(&wpdata.application_data, rpdata.application_data, MAX_APDU);
wpdata.application_data_len = len;
wpdata.error_code = ERROR_CODE_SUCCESS;
status = Calendar_Write_Property(&wpdata);
if (!status) {
/* verify WriteProperty property is known */
zassert_not_equal(wpdata.error_code,
ERROR_CODE_UNKNOWN_PROPERTY,
"property '%s': WriteProperty Unknown!\n",
bactext_property_name(rpdata.object_property));
}
}
pOptional++;
}
/* check for unsupported property - use ALL */
rpdata.object_property = PROP_ALL;
len = Calendar_Read_Property(&rpdata);
zassert_equal(len, BACNET_STATUS_ERROR, NULL);
status = Calendar_Write_Property(&wpdata);
zassert_false(status, NULL);
/* check the delete function */
status = Calendar_Delete(instance);
zassert_true(status, NULL);
}
static void testPresentValue(void)
{
const uint32_t instance = 1;
BACNET_DATE date;
BACNET_TIME time;
BACNET_CALENDAR_ENTRY entry;
BACNET_CALENDAR_ENTRY *value;
uint32_t test_instance = 0;
Calendar_Init();
test_instance = Calendar_Create(instance);
zassert_equal(test_instance, instance, NULL);
datetime_local(&date, &time, NULL, NULL);
// test Date
zassert_false(Calendar_Present_Value(instance), NULL);
zassert_equal(0, Calendar_Date_List_Count(instance), NULL);
entry.tag = BACNET_CALENDAR_DATE;
entry.type.Date = date;
entry.type.Date.day++;
Calendar_Date_List_Add(instance, &entry);
zassert_equal(1, Calendar_Date_List_Count(instance), NULL);
zassert_false(Calendar_Present_Value(instance), NULL);
entry.type.Date = date;
Calendar_Date_List_Add(instance, &entry);
zassert_equal(2, Calendar_Date_List_Count(instance), NULL);
zassert_true(Calendar_Present_Value(instance), NULL);
value = Calendar_Date_List_Get(instance, 1);
value->type.Date.day += 2;
zassert_equal(2, Calendar_Date_List_Count(instance), NULL);
zassert_false(Calendar_Present_Value(instance), NULL);
// test Date Range
entry.tag = BACNET_CALENDAR_DATE_RANGE;
entry.type.DateRange.startdate = date;
entry.type.DateRange.enddate = date;
entry.type.DateRange.startdate.day += 2;
entry.type.DateRange.enddate.day += 10;
Calendar_Date_List_Add(instance, &entry);
zassert_equal(3, Calendar_Date_List_Count(instance), NULL);
zassert_false(Calendar_Present_Value(instance), NULL);
value = Calendar_Date_List_Get(instance, 2);
value->type.DateRange.startdate.day = date.day;
zassert_true(Calendar_Present_Value(instance), NULL);
if (date.day > 1) {
value->type.DateRange.startdate.day --;
value->type.DateRange.enddate.day = date.day;
zassert_true(Calendar_Present_Value(instance), NULL);
}
value->type.DateRange.startdate.day = date.day + 2;
value->type.DateRange.enddate.day = date.day + 2;
zassert_false(Calendar_Present_Value(instance), NULL);
// test WeekNDay
entry.tag = BACNET_CALENDAR_WEEK_N_DAY;
entry.type.WeekNDay.month = 0xff;
entry.type.WeekNDay.weekofmonth = 0xff;
entry.type.WeekNDay.dayofweek = 0xff;
Calendar_Date_List_Add(instance, &entry);
zassert_equal(4, Calendar_Date_List_Count(instance), NULL);
value = Calendar_Date_List_Get(instance, 3);
zassert_true(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.month = date.month;
zassert_true(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.month++;
zassert_false(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.month = (date.month % 2) ? 13 : 14;
zassert_true(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.month = (date.month % 2) ? 14 : 13;
zassert_false(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.month = 0xff;
value->type.WeekNDay.weekofmonth = (date.day - 1) % 7 + 1;
zassert_true(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.weekofmonth++;
if (value->type.WeekNDay.weekofmonth >5)
value->type.WeekNDay.weekofmonth = 1;
zassert_false(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.weekofmonth = 0xff;
value->type.WeekNDay.dayofweek = date.wday;
zassert_true(Calendar_Present_Value(instance), NULL);
value->type.WeekNDay.dayofweek++;
if (value->type.WeekNDay.dayofweek > 7)
value->type.WeekNDay.dayofweek = 1;
zassert_false(Calendar_Present_Value(instance), NULL);
Calendar_Date_List_Delete_All(instance);
zassert_equal(0, Calendar_Date_List_Count(instance), NULL);
zassert_true(Calendar_Delete(instance), NULL);
}
/**
* @}
*/
void test_main(void)
{
ztest_test_suite(calendar_tests,
ztest_unit_test(testCalendar),
ztest_unit_test(testPresentValue)
);
ztest_run_test_suite(calendar_tests);
}
+28
View File
@@ -0,0 +1,28 @@
/**
* @file
* @brief Stub functions for unit test of a BACnet object
* @author Steve Karg <skarg@users.sourceforge.net>
* @date December 2022
*
* SPDX-License-Identifier: MIT
*/
#include <stdbool.h>
#include <stdint.h>
#include "bacnet/datetime.h"
bool datetime_local(
BACNET_DATE * bdate,
BACNET_TIME * btime,
int16_t * utc_offset_minutes,
bool * dst_active)
{
bdate->year = 2023;
bdate->month = 6;
bdate->day = 26;
bdate->wday = 1;
(void)btime;
(void)utc_offset_minutes;
(void)dst_active;
return true;
}
@@ -55,6 +55,7 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/basic/object/blo.c
${SRC_DIR}/bacnet/basic/object/bo.c
${SRC_DIR}/bacnet/basic/object/bv.c
${SRC_DIR}/bacnet/basic/object/calendar.c
${SRC_DIR}/bacnet/basic/object/channel.c
${SRC_DIR}/bacnet/basic/object/color_object.c
${SRC_DIR}/bacnet/basic/object/color_temperature.c
@@ -71,6 +72,7 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/basic/object/osv.c
${SRC_DIR}/bacnet/basic/object/piv.c
${SRC_DIR}/bacnet/basic/object/schedule.c
${SRC_DIR}/bacnet/basic/object/time_value.c
${SRC_DIR}/bacnet/basic/object/trendlog.c
${SRC_DIR}/bacnet/basic/service/h_apdu.c
${SRC_DIR}/bacnet/basic/service/h_cov.c
@@ -0,0 +1,69 @@
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(test_${basename}
VERSION 1.0.0
LANGUAGES C)
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z_/-]*$"
"/src"
SRC_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z_/-]*$"
"/test"
TST_DIR
${CMAKE_CURRENT_SOURCE_DIR})
set(ZTST_DIR "${TST_DIR}/ztest/src")
add_compile_definitions(
BIG_ENDIAN=0
CONFIG_ZTEST=1
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/basic/object/time_value.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacapp.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacdest.c
${SRC_DIR}/bacnet/bacdevobjpropref.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacreal.c
${SRC_DIR}/bacnet/bacstr.c
${SRC_DIR}/bacnet/bactext.c
${SRC_DIR}/bacnet/basic/sys/bigend.c
${SRC_DIR}/bacnet/cov.c
${SRC_DIR}/bacnet/datetime.c
${SRC_DIR}/bacnet/basic/sys/days.c
${SRC_DIR}/bacnet/basic/sys/keylist.c
${SRC_DIR}/bacnet/indtext.c
${SRC_DIR}/bacnet/hostnport.c
${SRC_DIR}/bacnet/lighting.c
${SRC_DIR}/bacnet/proplist.c
${SRC_DIR}/bacnet/timestamp.c
${SRC_DIR}/bacnet/memcopy.c
${SRC_DIR}/bacnet/wp.c
${SRC_DIR}/bacnet/weeklyschedule.c
${SRC_DIR}/bacnet/bactimevalue.c
${SRC_DIR}/bacnet/dailyschedule.c
${SRC_DIR}/bacnet/calendar_entry.c
${SRC_DIR}/bacnet/special_event.c
# Test and test library files
./src/main.c
./stubs.c
../mock/device_mock.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
@@ -0,0 +1,141 @@
/**
* @file
* @brief Unit test for BACnet Time Value object encode/decode APIs
* @author Mikhail Antropov <michail.antropov@dsr-corporation.com>
* @author Steve Karg <skarg@users.sourceforge.net>
* @date June 2023
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/ztest.h>
#include <bacnet/basic/object/time_value.h>
#include <bacnet/bactext.h>
/**
* @addtogroup bacnet_tests
* @{
*/
/**
* @brief Test Time Value handling
*/
static void testTimeValue(void)
{
uint8_t apdu[MAX_APDU] = { 0 };
int len = 0, test_len = 0;
BACNET_READ_PROPERTY_DATA rpdata = { 0 };
BACNET_WRITE_PROPERTY_DATA wpdata = { 0 };
BACNET_APPLICATION_DATA_VALUE value = {0};
const int *pRequired = NULL;
const int *pOptional = NULL;
const int *pProprietary = NULL;
const uint32_t instance = 123;
uint32_t test_instance = 0;
bool status = false;
unsigned index;
Time_Value_Init();
test_instance = Time_Value_Create(instance);
zassert_equal(test_instance, instance, NULL);
status = Time_Value_Valid_Instance(instance);
zassert_true(status, NULL);
index = Time_Value_Instance_To_Index(instance);
zassert_equal(index, 0, NULL);
rpdata.application_data = &apdu[0];
rpdata.application_data_len = sizeof(apdu);
rpdata.object_type = OBJECT_TIME_VALUE;
rpdata.object_instance = instance;
rpdata.array_index = BACNET_ARRAY_ALL;
Time_Value_Property_Lists(
&pRequired, &pOptional, &pProprietary);
while ((*pRequired) >= 0) {
rpdata.object_property = *pRequired;
rpdata.array_index = BACNET_ARRAY_ALL;
len = Time_Value_Read_Property(&rpdata);
zassert_not_equal(len, BACNET_STATUS_ERROR,
"property '%s': failed to ReadProperty!\n",
bactext_property_name(rpdata.object_property));
if (len >= 0) {
test_len = bacapp_decode_known_property(rpdata.application_data,
len, &value, rpdata.object_type, rpdata.object_property);
if (rpdata.object_property != PROP_PRIORITY_ARRAY) {
zassert_equal(len, test_len,
"property '%s': failed to decode!\n",
bactext_property_name(rpdata.object_property));
}
/* check WriteProperty properties */
wpdata.object_type = rpdata.object_type;
wpdata.object_instance = rpdata.object_instance;
wpdata.object_property = rpdata.object_property;
wpdata.array_index = rpdata.array_index;
memcpy(&wpdata.application_data, rpdata.application_data, MAX_APDU);
wpdata.application_data_len = len;
wpdata.error_code = ERROR_CODE_SUCCESS;
status = Time_Value_Write_Property(&wpdata);
if (!status) {
/* verify WriteProperty property is known */
zassert_not_equal(wpdata.error_code,
ERROR_CODE_UNKNOWN_PROPERTY,
"property '%s': WriteProperty Unknown!\n",
bactext_property_name(rpdata.object_property));
}
}
pRequired++;
}
while ((*pOptional) != -1) {
rpdata.object_property = *pOptional;
rpdata.array_index = BACNET_ARRAY_ALL;
len = Time_Value_Read_Property(&rpdata);
zassert_not_equal(len, BACNET_STATUS_ERROR,
"property '%s': failed to ReadProperty!\n",
bactext_property_name(rpdata.object_property));
if (len > 0) {
test_len = bacapp_decode_application_data(rpdata.application_data,
(uint8_t)rpdata.application_data_len, &value);
zassert_equal(len, test_len, "property '%s': failed to decode!\n",
bactext_property_name(rpdata.object_property));
/* check WriteProperty properties */
wpdata.object_type = rpdata.object_type;
wpdata.object_instance = rpdata.object_instance;
wpdata.object_property = rpdata.object_property;
wpdata.array_index = rpdata.array_index;
memcpy(&wpdata.application_data, rpdata.application_data, MAX_APDU);
wpdata.application_data_len = len;
wpdata.error_code = ERROR_CODE_SUCCESS;
status = Time_Value_Write_Property(&wpdata);
if (!status) {
/* verify WriteProperty property is known */
zassert_not_equal(wpdata.error_code,
ERROR_CODE_UNKNOWN_PROPERTY,
"property '%s': WriteProperty Unknown!\n",
bactext_property_name(rpdata.object_property));
}
}
pOptional++;
}
/* check for unsupported property - use ALL */
rpdata.object_property = PROP_ALL;
len = Time_Value_Read_Property(&rpdata);
zassert_equal(len, BACNET_STATUS_ERROR, NULL);
status = Time_Value_Write_Property(&wpdata);
zassert_false(status, NULL);
/* check the delete function */
status = Time_Value_Delete(instance);
zassert_true(status, NULL);
return;
}
/**
* @}
*/
void test_main(void)
{
ztest_test_suite(tv_tests,
ztest_unit_test(testTimeValue)
);
ztest_run_test_suite(tv_tests);
}
@@ -0,0 +1,28 @@
/**
* @file
* @brief Stub functions for unit test of a BACnet object
* @author Steve Karg <skarg@users.sourceforge.net>
* @date December 2022
*
* SPDX-License-Identifier: MIT
*/
#include <stdbool.h>
#include <stdint.h>
#include "bacnet/datetime.h"
bool datetime_local(
BACNET_DATE * bdate,
BACNET_TIME * btime,
int16_t * utc_offset_minutes,
bool * dst_active)
{
bdate->year = 2023;
bdate->month = 6;
bdate->day = 26;
bdate->wday = 1;
(void)btime;
(void)utc_offset_minutes;
(void)dst_active;
return true;
}
-1
View File
@@ -45,7 +45,6 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/bacstr.c
${SRC_DIR}/bacnet/bactext.c
${SRC_DIR}/bacnet/basic/sys/bigend.c
${SRC_DIR}/bacnet/calendar_entry.c
${SRC_DIR}/bacnet/datetime.c
${SRC_DIR}/bacnet/basic/sys/days.c
${SRC_DIR}/bacnet/indtext.c
+9 -2
View File
@@ -634,8 +634,15 @@ ZTEST_SUITE(datetime_tests, NULL, NULL, NULL, NULL, NULL);
#else
void test_main(void)
{
ztest_test_suite(datetime_tests, ztest_unit_test(testBACnetDate),
ztest_unit_test(testBACnetTime), ztest_unit_test(testBACnetDateTime),
#if 0
ztest_unit_test(testDateEpoch),
ztest_unit_test(testBACnetDateTimeSeconds),
ztest_unit_test(testDayOfYear),
#endif
ztest_test_suite(datetime_tests,
ztest_unit_test(testBACnetDate),
ztest_unit_test(testBACnetTime),
ztest_unit_test(testBACnetDateTime),
ztest_unit_test(testBACnetDayOfWeek),
ztest_unit_test(testDateEpochConversion),
ztest_unit_test(testBACnetDateTimeAdd),