Feature/add create object service (#476)

Added BACnet CreateObject and DeleteObject services

* refactored codec for BACnetPropertyValue into bacapp module
* added unit tests for BACnetPropertyValue
* refactored COV and Events to use BACnetPropertyValue codec API
* added unit tests for COV
* added overrun safe decoders for tag numbers and boolean context
* added unit tests and codecs for CreateObject and DeleteObject services
* added APDU service handers and senders for CreateObject and DeleteObject services
* added command line apps bacco and bacdo for CreateObject and DeleteObject services
* added CreateObject and DeleteObject service handling in example server app and device object
* added new BACnetRejectReason, Error Class, and BACnetAbortReason enumerations and conversions

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-08-28 13:02:35 -05:00
committed by GitHub
parent 8184afea12
commit f61f4300be
60 changed files with 3946 additions and 942 deletions
+258 -29
View File
@@ -39,7 +39,7 @@
#include <ctype.h> /* for isalnum */
#include <errno.h>
#include <math.h>
#if (__STDC_VERSION__ >= 199901L) && defined (__STDC_ISO_10646__)
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
#include <wchar.h>
#include <wctype.h>
#endif
@@ -198,8 +198,8 @@ int bacapp_encode_application_data(
break;
case BACNET_APPLICATION_TAG_DESTINATION:
/* BACnetDestination */
apdu_len = bacnet_destination_encode(
apdu, &value->type.Destination);
apdu_len =
bacnet_destination_encode(apdu, &value->type.Destination);
break;
#endif
default:
@@ -351,8 +351,8 @@ int bacapp_decode_data(uint8_t *apdu,
break;
case BACNET_APPLICATION_TAG_DESTINATION:
/* BACnetDestination */
len = bacnet_destination_decode(apdu, len_value_type,
&value->type.Destination);
len = bacnet_destination_decode(
apdu, len_value_type, &value->type.Destination);
break;
#endif
default:
@@ -684,8 +684,8 @@ int bacapp_encode_context_data_value(uint8_t *apdu,
break;
case BACNET_APPLICATION_TAG_DESTINATION:
/* BACnetDestination */
apdu_len = bacnet_destination_context_encode(apdu,
context_tag_number, &value->type.Destination);
apdu_len = bacnet_destination_context_encode(
apdu, context_tag_number, &value->type.Destination);
break;
#endif
default:
@@ -1535,8 +1535,8 @@ int bacapp_data_len(
}
do {
if (bacnet_is_opening_tag(apdu, apdu_len_max)) {
len = bacnet_tag_number_and_value_decode(apdu,
apdu_len_max - apdu_len, &tag_number, &value);
len = bacnet_tag_number_and_value_decode(
apdu, apdu_len_max - apdu_len, &tag_number, &value);
if (opening_tag_number_counter == 0) {
opening_tag_number = tag_number;
opening_tag_number_counter = 1;
@@ -1546,8 +1546,8 @@ int bacapp_data_len(
opening_tag_number_counter++;
}
} else if (bacnet_is_closing_tag(apdu, apdu_len_max)) {
len = bacnet_tag_number_and_value_decode(apdu,
apdu_len_max - apdu_len, &tag_number, &value);
len = bacnet_tag_number_and_value_decode(
apdu, apdu_len_max - apdu_len, &tag_number, &value);
if (tag_number == opening_tag_number) {
if (opening_tag_number_counter > 0) {
opening_tag_number_counter--;
@@ -1585,7 +1585,6 @@ int bacapp_data_len(
}
} while (opening_tag_number_counter > 0);
return total_len;
}
@@ -1603,13 +1602,14 @@ static int bacapp_snprintf_date(char *str, size_t str_len, BACNET_DATE *bdate)
{
int ret_val = 0;
int slen = 0;
const char *weekday_text, *month_text;
weekday_text = bactext_day_of_week_name(bdate->wday);
month_text = bactext_month_name(bdate->month);
/* false positive cppcheck - snprintf allows null pointers */
/* cppcheck-suppress nullPointer */
/* cppcheck-suppress ctunullpointer */
slen = snprintf(str, str_len, "%s, %s",
bactext_day_of_week_name(bdate->wday),
bactext_month_name(bdate->month));
slen = snprintf(str, str_len, "%s, %s", weekday_text, month_text);
if (str) {
str += slen;
if (str_len >= slen) {
@@ -1873,7 +1873,7 @@ int bacapp_snprintf_value(
#if defined(BACAPP_OCTET_STRING) || defined(BACAPP_TYPES_EXTRA)
uint8_t *octet_str;
#endif
#if (__STDC_VERSION__ >= 199901L) && defined (__STDC_ISO_10646__)
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
/* Wide character (decoded from multi-byte character). */
wchar_t wc;
/* Wide character length in bytes. */
@@ -1953,7 +1953,7 @@ int bacapp_snprintf_value(
}
}
ret_val += slen;
#if (__STDC_VERSION__ >= 199901L) && defined (__STDC_ISO_10646__)
#if (__STDC_VERSION__ >= 199901L) && defined(__STDC_ISO_10646__)
if (characterstring_encoding(&value->type.Character_String) ==
CHARACTER_UTF8) {
while (len > 0) {
@@ -2201,14 +2201,16 @@ int bacapp_snprintf_value(
break;
case BACNET_APPLICATION_TAG_TIMESTAMP:
/*ISO 8601 format */
slen = snprintf(str, str_len, "%04u-%02u-%02uT%02u:%02u:%02u.%03u",
(unsigned) value->type.Time_Stamp.value.dateTime.date.year,
(unsigned) value->type.Time_Stamp.value.dateTime.date.month,
(unsigned) value->type.Time_Stamp.value.dateTime.date.day,
(unsigned) value->type.Time_Stamp.value.dateTime.time.hour,
(unsigned) value->type.Time_Stamp.value.dateTime.time.min,
(unsigned) value->type.Time_Stamp.value.dateTime.time.sec,
(unsigned) value->type.Time_Stamp.value.dateTime.time.hundredths);
slen = snprintf(str, str_len,
"%04u-%02u-%02uT%02u:%02u:%02u.%03u",
(unsigned)value->type.Time_Stamp.value.dateTime.date.year,
(unsigned)value->type.Time_Stamp.value.dateTime.date.month,
(unsigned)value->type.Time_Stamp.value.dateTime.date.day,
(unsigned)value->type.Time_Stamp.value.dateTime.time.hour,
(unsigned)value->type.Time_Stamp.value.dateTime.time.min,
(unsigned)value->type.Time_Stamp.value.dateTime.time.sec,
(unsigned)
value->type.Time_Stamp.value.dateTime.time.hundredths);
ret_val += slen;
break;
case BACNET_APPLICATION_TAG_LIGHTING_COMMAND:
@@ -2279,7 +2281,8 @@ int bacapp_snprintf_value(
break;
case BACNET_APPLICATION_TAG_DESTINATION:
/* BACnetWeeklySchedule */
ret_val = bacnet_destination_to_ascii(&value->type.Destination, str, str_len);
ret_val = bacnet_destination_to_ascii(
&value->type.Destination, str, str_len);
break;
case BACNET_APPLICATION_TAG_HOST_N_PORT:
if (value->type.Host_Address.host_ip_address) {
@@ -2803,8 +2806,8 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number,
}
break;
case BACNET_APPLICATION_TAG_DESTINATION:
status = bacnet_destination_from_ascii(&value->type.Destination,
argv);
status = bacnet_destination_from_ascii(
&value->type.Destination, argv);
break;
#endif
default:
@@ -2868,6 +2871,231 @@ void bacapp_property_value_list_init(BACNET_PROPERTY_VALUE *value, size_t count)
}
}
/**
* @brief Link an array of BACNET_PROPERTY_VALUE elements.
* The linked-list is used prior to encoding or decoding
* the APDU data into the structure.
*
* @param value_list - Pointer to the first BACNET_PROPERTY_VALUE element in
* an array
* @param count - number of BACNET_PROPERTY_VALUE elements to link
*/
void bacapp_property_value_list_link(
BACNET_PROPERTY_VALUE *value_list, size_t count)
{
BACNET_PROPERTY_VALUE *current_value_list = NULL;
if (value_list) {
while (count) {
if (count > 1) {
current_value_list = value_list;
value_list++;
current_value_list->next = value_list;
} else {
value_list->next = NULL;
}
count--;
}
}
}
/**
* @brief Encode one BACnetPropertyValue value
*
* BACnetPropertyValue ::= SEQUENCE {
* property-identifier [0] BACnetPropertyIdentifier,
* property-array-index [1] Unsigned OPTIONAL,
* -- used only with array datatypes
* -- if omitted with an array the entire array is referenced
* property-value [2] ABSTRACT-SYNTAX.&Type,
* -- any datatype appropriate for the specified property
* priority [3] Unsigned (1..16) OPTIONAL
* -- used only when property is commandable
* }
*
* @param apdu Pointer to the buffer for encoded values, or NULL for length
* @param value Pointer to the service data used for encoding values
*
* @return Bytes encoded or zero on error.
*/
int bacapp_property_value_encode(uint8_t *apdu, BACNET_PROPERTY_VALUE *value)
{
int len = 0; /* length of each encoding */
int apdu_len = 0; /* total length of the apdu, return value */
BACNET_APPLICATION_DATA_VALUE *app_data = NULL;
if (value) {
/* tag 0 - propertyIdentifier */
len = encode_context_enumerated(apdu, 0, value->propertyIdentifier);
apdu_len += len;
if (apdu) {
apdu += len;
}
/* tag 1 - propertyArrayIndex OPTIONAL */
if (value->propertyArrayIndex != BACNET_ARRAY_ALL) {
len = encode_context_unsigned(apdu, 1, value->propertyArrayIndex);
apdu_len += len;
if (apdu) {
apdu += len;
}
}
/* tag 2 - value */
/* abstract syntax gets enclosed in a context tag */
len = encode_opening_tag(apdu, 2);
apdu_len += len;
if (apdu) {
apdu += len;
}
app_data = &value->value;
while (app_data != NULL) {
len = bacapp_encode_application_data(apdu, app_data);
apdu_len += len;
if (apdu) {
apdu += len;
}
app_data = app_data->next;
}
len = encode_closing_tag(apdu, 2);
apdu_len += len;
if (apdu) {
apdu += len;
}
/* tag 3 - priority OPTIONAL */
if (value->priority != BACNET_NO_PRIORITY) {
len = encode_context_unsigned(apdu, 3, value->priority);
apdu_len += len;
}
}
return apdu_len;
}
/**
* @brief Decode one BACnetPropertyValue value
*
* BACnetPropertyValue ::= SEQUENCE {
* property-identifier [0] BACnetPropertyIdentifier,
* property-array-index [1] Unsigned OPTIONAL,
* -- used only with array datatypes
* -- if omitted with an array the entire array is referenced
* property-value [2] ABSTRACT-SYNTAX.&Type,
* -- any datatype appropriate for the specified property
* priority [3] Unsigned (1..16) OPTIONAL
* -- used only when property is commandable
* }
*
* @param apdu Pointer to the buffer of encoded value
* @param apdu_size Size of the buffer holding the encode value
* @param value Pointer to the service data used for encoding values
*
* @return Bytes decoded or BACNET_STATUS_ERROR on error.
*/
int bacapp_property_value_decode(
uint8_t *apdu, uint32_t apdu_size, BACNET_PROPERTY_VALUE *value)
{
int len = 0;
int apdu_len = 0;
int tag_len = 0;
uint32_t enumerated_value = 0;
BACNET_UNSIGNED_INTEGER unsigned_value = 0;
BACNET_PROPERTY_ID property_identifier = PROP_ALL;
BACNET_APPLICATION_DATA_VALUE *app_data = NULL;
/* property-identifier [0] BACnetPropertyIdentifier */
len = bacnet_enumerated_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 0, &enumerated_value);
if (len == BACNET_STATUS_ERROR) {
return BACNET_STATUS_ERROR;
}
property_identifier = enumerated_value;
if (value) {
value->propertyIdentifier = property_identifier;
}
apdu_len += len;
/* property-array-index [1] Unsigned OPTIONAL */
if (bacnet_is_context_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 1, NULL)) {
len = bacnet_unsigned_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 1, &unsigned_value);
if (len == BACNET_STATUS_ERROR) {
return BACNET_STATUS_ERROR;
} else if (unsigned_value > UINT32_MAX) {
return BACNET_STATUS_ERROR;
} else {
apdu_len += len;
if (value) {
value->propertyArrayIndex = unsigned_value;
}
}
} else {
if (value) {
value->propertyArrayIndex = BACNET_ARRAY_ALL;
}
}
/* property-value [2] ABSTRACT-SYNTAX.&Type */
if (bacnet_is_opening_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 2, &len)) {
if (value) {
apdu_len += len;
app_data = &value->value;
while (app_data != NULL) {
len = bacapp_decode_application_data(
&apdu[apdu_len], apdu_size - apdu_len, app_data);
if (len < 0) {
return BACNET_STATUS_ERROR;
}
apdu_len += len;
if (bacnet_is_closing_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 2, &len)) {
break;
}
app_data = app_data->next;
}
} else {
/* this len function needs to start at the opening tag
to match opening/closing tags like a stack.
However, it returns the len between the tags.
Therefore, store the length of the opening tag first */
tag_len = len;
len = bacapp_data_len(&apdu[apdu_len], apdu_size - apdu_len,
(BACNET_PROPERTY_ID)property_identifier);
apdu_len += len;
/* add the opening tag length to the totals */
apdu_len += tag_len;
}
if (bacnet_is_closing_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 2, &len)) {
apdu_len += len;
} else {
return BACNET_STATUS_ERROR;
}
} else {
return BACNET_STATUS_ERROR;
}
/* priority [3] Unsigned (1..16) OPTIONAL */
if (bacnet_is_context_tag_number(
&apdu[apdu_len], apdu_size - apdu_len, 3, NULL)) {
len = bacnet_unsigned_context_decode(
&apdu[apdu_len], apdu_size - apdu_len, 3, &unsigned_value);
if (len == BACNET_STATUS_ERROR) {
return BACNET_STATUS_ERROR;
} else if (unsigned_value > UINT8_MAX) {
return BACNET_STATUS_ERROR;
} else {
apdu_len += len;
if (value) {
value->priority = unsigned_value;
}
}
} else {
if (value) {
value->priority = BACNET_NO_PRIORITY;
}
}
return apdu_len;
}
/* generic - can be used by other unit tests
returns true if matching or same, false if different */
bool bacapp_same_value(BACNET_APPLICATION_DATA_VALUE *value,
@@ -2922,7 +3150,8 @@ bool bacapp_same_value(BACNET_APPLICATION_DATA_VALUE *value,
#endif
#if defined(BACAPP_DOUBLE)
case BACNET_APPLICATION_TAG_DOUBLE:
if (!islessgreater(test_value->type.Double,value->type.Double)) {
if (!islessgreater(
test_value->type.Double, value->type.Double)) {
status = true;
}
break;