From 8209673c43e0b3d9af7c133a45fd17b004cfbe78 Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Fri, 20 Feb 2026 08:55:40 -0600 Subject: [PATCH] Add Host-N-Port minimal encode and decode (#1239) * Added Host-N-Port minimal encode and decode which utilizes the octet string buffer and character string buffer encode and decode API. --- CHANGELOG.md | 2 + src/bacnet/hostnport.c | 460 +++++++++++++++++++++++++++++-- src/bacnet/hostnport.h | 26 ++ test/bacnet/hostnport/src/main.c | 184 ++++++++++++- 4 files changed, 639 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2d20f4f..06b7276b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,8 @@ The git repositories are hosted at the following sites: ### Added +* Add Host-N-Port minimal encode and decode which utilizes the octet + string and character string buffer encode and decode. (#1239) * Added API for extending the basic Device object and children with proprietary properties for ReadProperty and WriteProperty services. (#1238) * Added octet and character string buffer codecs to used with fixed diff --git a/src/bacnet/hostnport.c b/src/bacnet/hostnport.c index 5f2f5f2b..0bc2681c 100644 --- a/src/bacnet/hostnport.c +++ b/src/bacnet/hostnport.c @@ -361,19 +361,323 @@ bool host_n_port_copy(BACNET_HOST_N_PORT *dst, const BACNET_HOST_N_PORT *src) dst->host_ip_address = src->host_ip_address; dst->host_name = src->host_name; if (src->host_ip_address) { - status = - octetstring_copy(&dst->host.ip_address, &src->host.ip_address); + (void)octetstring_copy( + &dst->host.ip_address, &src->host.ip_address); } else if (src->host_name) { - status = characterstring_copy(&dst->host.name, &src->host.name); - } else { - status = true; + (void)characterstring_copy(&dst->host.name, &src->host.name); } dst->port = src->port; + status = true; } return status; } +/** + * @brief Encode a BACnetHostNPort complex data type + * + * BACnetHostNPort ::= SEQUENCE { + * host [0] BACnetHostAddress, + * BACnetHostAddress ::= CHOICE { + * none [0] NULL, + * ip-address [1] OCTET STRING, + * -- 4 octets for B/IP or 16 octets for B/IPv6 + * name [2] CharacterString + * -- Internet host name (see RFC 1123) + * } + * port [1] Unsigned16 + * } + * + * @param apdu - the APDU buffer, or NULL for length + * @param host - BACnetHostNPort SEQUENCE + * @return length of the encoded APDU buffer + */ +int host_n_port_minimal_encode( + uint8_t *apdu, const BACNET_HOST_N_PORT_MINIMAL *host) +{ + int len = 0; + int apdu_len = 0; + BACNET_CHARACTER_STRING_BUFFER name = { 0 }; + + if (host) { + /* host [0] BACnetHostAddress - opening */ + len = encode_opening_tag(apdu, 0); + apdu_len += len; + if (apdu) { + apdu += len; + } + /* BACnetHostAddress ::= CHOICE */ + if (host->tag == BACNET_HOST_ADDRESS_TAG_IP_ADDRESS) { + /* CHOICE - ip-address [1] OCTET STRING */ + len = encode_context_octet_string_buffer( + apdu, 1, host->host.ip_address.address, + host->host.ip_address.length); + } else if (host->tag == BACNET_HOST_ADDRESS_TAG_NAME) { + /* CHOICE - name [2] CharacterString */ + name.buffer = (char *)host->host.name.fqdn; + name.buffer_size = sizeof(host->host.name.fqdn); + name.buffer_length = host->host.name.length; + name.encoding = CHARACTER_ANSI_X34; + len = encode_context_character_string_buffer(apdu, 2, &name); + } else { + /* CHOICE - none [0] NULL */ + len = encode_context_null(apdu, 0); + } + apdu_len += len; + if (apdu) { + apdu += len; + } + /* host [0] BACnetHostAddress - closing */ + len = encode_closing_tag(apdu, 0); + apdu_len += len; + if (apdu) { + apdu += len; + } + /* port [1] Unsigned16 */ + len = encode_context_unsigned(apdu, 1, host->port); + apdu_len += len; + } + + return apdu_len; +} + +/** + * @brief Decode the BACnetHostAddress + * @param apdu - the APDU buffer + * @param apdu_size - the APDU buffer length + * @param error_code - error or reject or abort when error occurs + * @param address - BACnetHostAddress data + * @return length of the APDU buffer decoded, or BACNET_STATUS_REJECT + */ +int host_n_port_minimal_address_decode( + const uint8_t *apdu, + uint32_t apdu_size, + BACNET_ERROR_CODE *error_code, + BACNET_HOST_N_PORT_MINIMAL *address) +{ + int apdu_len = 0, len = 0; + uint8_t *buffer = NULL; + size_t buffer_size = 0; + BACNET_TAG tag = { 0 }; + BACNET_CHARACTER_STRING_BUFFER char_string = { 0 }; + + /* default reject code */ + if (error_code) { + *error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER; + } + len = bacnet_tag_decode(&apdu[apdu_len], apdu_size - apdu_len, &tag); + if (len <= 0) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_REJECT; + } + apdu_len += len; + if (tag.context && (tag.number == 0)) { + /* CHOICE - none [0] NULL */ + if (address) { + address->tag = BACNET_HOST_ADDRESS_TAG_NONE; + } + } else if (tag.context && (tag.number == 1)) { + /* CHOICE - ip-address [1] OCTET STRING */ + if (address) { + address->tag = BACNET_HOST_ADDRESS_TAG_IP_ADDRESS; + buffer = address->host.ip_address.address; + buffer_size = sizeof(address->host.ip_address.address); + } + len = bacnet_octet_string_buffer_decode( + &apdu[apdu_len], apdu_size - apdu_len, tag.len_value_type, buffer, + buffer_size); + if (len < 0) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_BUFFER_OVERFLOW; + } + return BACNET_STATUS_REJECT; + } + if (address) { + if (len > buffer_size) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_BUFFER_OVERFLOW; + } + return BACNET_STATUS_REJECT; + } + address->host.ip_address.length = len; + } + apdu_len += len; + } else if (tag.context && (tag.number == 2)) { + if (address) { + address->tag = BACNET_HOST_ADDRESS_TAG_NAME; + char_string.buffer = address->host.name.fqdn; + char_string.buffer_size = sizeof(address->host.name.fqdn); + char_string.encoding = CHARACTER_ANSI_X34; + char_string.buffer_length = 0; + } + len = bacnet_character_string_buffer_decode( + &apdu[apdu_len], apdu_size - apdu_len, tag.len_value_type, + &char_string); + if (len == 0) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_BUFFER_OVERFLOW; + } + return BACNET_STATUS_REJECT; + } + if (address) { + if (char_string.buffer_length > char_string.buffer_size) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_BUFFER_OVERFLOW; + } + return BACNET_STATUS_REJECT; + } + address->host.name.length = char_string.buffer_length; + } + apdu_len += len; + } else { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_REJECT; + } + + return apdu_len; +} + +/** + * @brief Decode the BACnetHostNPort complex data + * + * BACnetHostNPort ::= SEQUENCE { + * host [0] BACnetHostAddress, + * BACnetHostAddress ::= CHOICE { + * none [0] NULL, + * ip-address [1] OCTET STRING, + * -- 4 octets for B/IP or 16 octets for B/IPv6 + * name [2] CharacterString + * -- Internet host name (see RFC 1123) + * } + * port [1] Unsigned16 + * } + * + * @param apdu - the APDU buffer + * @param apdu_size - the APDU buffer length + * @param error_code - error or reject or abort when error occurs + * @param host - BACnetHostNPort SEQUENCE + * @return length of the APDU buffer decoded, or ERROR, REJECT, or ABORT + */ +int host_n_port_minimal_decode( + const uint8_t *apdu, + uint32_t apdu_size, + BACNET_ERROR_CODE *error_code, + BACNET_HOST_N_PORT_MINIMAL *host) +{ + int apdu_len = 0, len = 0; + BACNET_UNSIGNED_INTEGER unsigned_value = 0; + + /* default reject code */ + if (error_code) { + *error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER; + } + /* host [0] BACnetHostAddress - opening */ + if (!bacnet_is_opening_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, 0, &len)) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_REJECT; + } + apdu_len += len; + /* BACnetHostAddress ::= CHOICE */ + len = host_n_port_minimal_address_decode( + &apdu[apdu_len], apdu_size - apdu_len, error_code, host); + if (len > 0) { + apdu_len += len; + } else { + return BACNET_STATUS_REJECT; + } + /* host [0] BACnetHostAddress - closing */ + if (!bacnet_is_closing_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, 0, &len)) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_REJECT; + } + apdu_len += len; + /* port [1] Unsigned16 */ + len = bacnet_unsigned_context_decode( + &apdu[apdu_len], apdu_size - apdu_len, 1, &unsigned_value); + if (len > 0) { + if (unsigned_value <= UINT16_MAX) { + if (host) { + host->port = unsigned_value; + } + } else { + if (error_code) { + *error_code = ERROR_CODE_REJECT_PARAMETER_OUT_OF_RANGE; + } + return BACNET_STATUS_REJECT; + } + } else { + if (error_code) { + if (len == 0) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } else { + *error_code = ERROR_CODE_REJECT_OTHER; + } + } + return BACNET_STATUS_REJECT; + } + apdu_len += len; + + return apdu_len; +} + +/** + * @brief Decode a context encoded BACnetHostNPort_Minimal complex data + * @param apdu - the APDU buffer + * @param apdu_size - the APDU buffer length + * @param tag_number - context tag number to be decoded + * @param error_code - error or reject or abort when error occurs + * @param value - BACnetHostNPort_Minimal structure + * @return length of the APDU buffer decoded, or ERROR, REJECT, or ABORT + */ +int host_n_port_minimal_context_decode( + const uint8_t *apdu, + uint32_t apdu_size, + uint8_t tag_number, + BACNET_ERROR_CODE *error_code, + BACNET_HOST_N_PORT_MINIMAL *value) +{ + int len = 0; + int apdu_len = 0; + + if (!apdu) { + return BACNET_STATUS_ERROR; + } + if (!bacnet_is_opening_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_ERROR; + } + apdu_len += len; + len = host_n_port_minimal_decode( + &apdu[apdu_len], apdu_size - apdu_len, error_code, value); + if (len <= 0) { + return BACNET_STATUS_ERROR; + } + apdu_len += len; + if (!bacnet_is_closing_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_ERROR; + } + apdu_len += len; + + return apdu_len; +} + /** * @brief Initialize a BACnetHostNPort_Minimal structure for IP address * @param host - BACnetHostNPort_Minimal structure @@ -390,8 +694,8 @@ void host_n_port_minimal_ip_init( if (host) { host->tag = BACNET_HOST_ADDRESS_TAG_IP_ADDRESS; - host->host.ip_address.length = address_len; imax = min(address_len, sizeof(host->host.ip_address.address)); + host->host.ip_address.length = imax; if (address) { for (i = 0; i < imax; i++) { host->host.ip_address.address[i] = address[i]; @@ -405,6 +709,36 @@ void host_n_port_minimal_ip_init( } } +/** + * @brief Initialize a BACnetHostNPort_Minimal structure for a FQDN hostname + * @param host - BACnetHostNPort_Minimal structure + * @param port - TCP/IP UDP port number + * @param hostname - FQDN hostname + */ +void host_n_port_minimal_hostname_init( + BACNET_HOST_N_PORT_MINIMAL *host, uint16_t port, const char *hostname) +{ + unsigned i; + + if (host) { + host->tag = BACNET_HOST_ADDRESS_TAG_NAME; + if (hostname) { + host->host.name.length = + bacnet_strnlen(hostname, sizeof(host->host.name.fqdn)); + } else { + host->host.name.length = 0; + } + for (i = 0; i < sizeof(host->host.name.fqdn); i++) { + if (hostname && (i < host->host.name.length)) { + host->host.name.fqdn[i] = hostname[i]; + } else { + host->host.name.fqdn[i] = 0; + } + } + host->port = port; + } +} + /** * @brief copy the BACnetHostNPort_Minimal complex data * @param dest - destination structure @@ -509,27 +843,27 @@ bool host_n_port_to_minimal( min(sizeof(dst->host.ip_address.address), src->host.ip_address.length), &src->host.ip_address); - if (dst->host.ip_address.length > 0) { - status = true; - } } else if (src->host_name) { dst->tag = BACNET_HOST_ADDRESS_TAG_NAME; dst->host.name.length = characterstring_copy_value( dst->host.name.fqdn, sizeof(dst->host.name.fqdn), &src->host.name); - if (dst->host.name.length > 0) { - status = true; - } } else { dst->tag = BACNET_HOST_ADDRESS_TAG_NONE; - status = true; } dst->port = src->port; + status = true; } return status; } +/** + * @brief Compare the BACnetHostNPort_Minimal complex data of src and dst + * @param dst - destination structure + * @param src - source structure + * @return true if both are the same + */ bool host_n_port_minimal_same( const BACNET_HOST_N_PORT_MINIMAL *dst, const BACNET_HOST_N_PORT_MINIMAL *src) @@ -625,6 +959,12 @@ bool host_n_port_from_ascii(BACNET_HOST_N_PORT *value, const char *argv) unsigned a[4] = { 0 }, p = 0; int count; + if (!argv) { + return false; + } + if (!value) { + return false; + } count = sscanf(argv, "%3u.%3u.%3u.%3u:%5u", &a[0], &a[1], &a[2], &a[3], &p); if ((count == 4) || (count == 5)) { uint8_t address[4]; @@ -726,6 +1066,15 @@ int bacnet_bdt_entry_context_encode( return apdu_len; } +/** + * @brief Decode a BDT entry + * + * @param apdu - the APDU buffer + * @param apdu_size - the APDU buffer length + * @param error_code - error or reject or abort when error occurs + * @param address - BDT entry + * @return length of the APDU buffer decoded, or ERROR, REJECT, or ABORT + */ int bacnet_bdt_entry_decode( const uint8_t *apdu, uint32_t apdu_size, @@ -733,6 +1082,8 @@ int bacnet_bdt_entry_decode( BACNET_BDT_ENTRY *address) { int apdu_len = 0, len = 0; + BACNET_HOST_N_PORT *bbmd_address = NULL; + BACNET_OCTET_STRING *broadcast_mask = NULL; /* default reject code */ if (error_code) { @@ -747,9 +1098,12 @@ int bacnet_bdt_entry_decode( } apdu_len += len; /* BACnetHostNPort ::= SEQUENCE */ + if (address) { + bbmd_address = &address->bbmd_address; + broadcast_mask = &address->broadcast_mask; + } len = host_n_port_decode( - &apdu[apdu_len], apdu_size - apdu_len, error_code, - &address->bbmd_address); + &apdu[apdu_len], apdu_size - apdu_len, error_code, bbmd_address); if (len > 0) { apdu_len += len; } else { @@ -766,7 +1120,7 @@ int bacnet_bdt_entry_decode( apdu_len += len; /* broadcast-mask [1] OCTET STRING */ len = bacnet_octet_string_context_decode( - &apdu[apdu_len], apdu_size - apdu_len, 1, &address->broadcast_mask); + &apdu[apdu_len], apdu_size - apdu_len, 1, broadcast_mask); if (len > 0) { apdu_len += len; } @@ -774,6 +1128,54 @@ int bacnet_bdt_entry_decode( return apdu_len; } +/** + * @brief Decodes a context tagged value from APDU buffer + * @param apdu - the APDU buffer + * @param apdu_size - the APDU buffer size + * @param tag_number - context tag number to be encoded + * @param error_code - error or reject or abort when error occurs + * @param value - parameter to store the value after decoding + * @return length of the APDU buffer decoded, or BACNET_STATUS_ERROR + */ +int bacnet_bdt_entry_context_decode( + const uint8_t *apdu, + uint32_t apdu_size, + uint8_t tag_number, + BACNET_ERROR_CODE *error_code, + BACNET_BDT_ENTRY *value) +{ + int len = 0; + int apdu_len = 0; + + if (!apdu) { + return BACNET_STATUS_ERROR; + } + if (!bacnet_is_opening_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_ERROR; + } + apdu_len += len; + len = bacnet_bdt_entry_decode( + &apdu[apdu_len], apdu_size - apdu_len, error_code, value); + if (len <= 0) { + return BACNET_STATUS_ERROR; + } + apdu_len += len; + if (!bacnet_is_closing_tag_number( + &apdu[apdu_len], apdu_size - apdu_len, tag_number, &len)) { + if (error_code) { + *error_code = ERROR_CODE_REJECT_INVALID_TAG; + } + return BACNET_STATUS_ERROR; + } + apdu_len += len; + + return apdu_len; +} + /** * @brief Copy the BACnetBDTEntry complex data * @param dst - destination structure @@ -785,11 +1187,9 @@ bool bacnet_bdt_entry_copy(BACNET_BDT_ENTRY *dst, const BACNET_BDT_ENTRY *src) bool status = false; if (dst && src) { - status = host_n_port_copy(&dst->bbmd_address, &src->bbmd_address); - if (!status) { - status = - octetstring_copy(&dst->broadcast_mask, &src->broadcast_mask); - } + (void)host_n_port_copy(&dst->bbmd_address, &src->bbmd_address); + (void)octetstring_copy(&dst->broadcast_mask, &src->broadcast_mask); + status = true; } return status; @@ -1081,14 +1481,18 @@ int bacnet_fdt_entry_decode( { int apdu_len = 0, len = 0; BACNET_UNSIGNED_INTEGER unsigned_value = 0; + BACNET_OCTET_STRING *bacnetip_address = NULL; /* default reject code */ if (error_code) { *error_code = ERROR_CODE_REJECT_MISSING_REQUIRED_PARAMETER; } + if (entry) { + bacnetip_address = &entry->bacnetip_address; + } /* bacnetip-address [0] OCTET STRING */ len = bacnet_octet_string_context_decode( - &apdu[apdu_len], apdu_size - apdu_len, 0, &entry->bacnetip_address); + &apdu[apdu_len], apdu_size - apdu_len, 0, bacnetip_address); if (len > 0) { apdu_len += len; } else { @@ -1098,7 +1502,9 @@ int bacnet_fdt_entry_decode( len = bacnet_unsigned_context_decode( &apdu[apdu_len], apdu_size - apdu_len, 1, &unsigned_value); if ((len > 0) && (unsigned_value <= UINT16_MAX)) { - entry->time_to_live = unsigned_value; + if (entry) { + entry->time_to_live = unsigned_value; + } apdu_len += len; } else { if (error_code) { @@ -1110,7 +1516,9 @@ int bacnet_fdt_entry_decode( len = bacnet_unsigned_context_decode( &apdu[apdu_len], apdu_size - apdu_len, 2, &unsigned_value); if ((len > 0) && (unsigned_value <= UINT16_MAX)) { - entry->remaining_time_to_live = unsigned_value; + if (entry) { + entry->remaining_time_to_live = unsigned_value; + } apdu_len += len; } else { if (error_code) { @@ -1186,10 +1594,10 @@ bool bacnet_fdt_entry_copy(BACNET_FDT_ENTRY *dst, const BACNET_FDT_ENTRY *src) bool status = false; if (dst && src) { - status = - octetstring_copy(&dst->bacnetip_address, &src->bacnetip_address); + octetstring_copy(&dst->bacnetip_address, &src->bacnetip_address); dst->time_to_live = src->time_to_live; dst->remaining_time_to_live = src->remaining_time_to_live; + status = true; } return status; diff --git a/src/bacnet/hostnport.h b/src/bacnet/hostnport.h index 8eba16f4..3b0dc48d 100644 --- a/src/bacnet/hostnport.h +++ b/src/bacnet/hostnport.h @@ -133,11 +133,37 @@ BACNET_STACK_EXPORT bool host_n_port_minimal_same( const BACNET_HOST_N_PORT_MINIMAL *dst, const BACNET_HOST_N_PORT_MINIMAL *src); +BACNET_STACK_EXPORT void host_n_port_minimal_ip_init( BACNET_HOST_N_PORT_MINIMAL *host, uint16_t port, const uint8_t *address, size_t address_len); +BACNET_STACK_EXPORT +void host_n_port_minimal_hostname_init( + BACNET_HOST_N_PORT_MINIMAL *host, uint16_t port, const char *hostname); +BACNET_STACK_EXPORT +int host_n_port_minimal_encode( + uint8_t *apdu, const BACNET_HOST_N_PORT_MINIMAL *address); +BACNET_STACK_EXPORT +int host_n_port_minimal_address_decode( + const uint8_t *apdu, + uint32_t apdu_size, + BACNET_ERROR_CODE *error_code, + BACNET_HOST_N_PORT_MINIMAL *address); +BACNET_STACK_EXPORT +int host_n_port_minimal_decode( + const uint8_t *apdu, + uint32_t apdu_size, + BACNET_ERROR_CODE *error_code, + BACNET_HOST_N_PORT_MINIMAL *address); +BACNET_STACK_EXPORT +int host_n_port_minimal_context_decode( + const uint8_t *apdu, + uint32_t apdu_size, + uint8_t tag_number, + BACNET_ERROR_CODE *error_code, + BACNET_HOST_N_PORT_MINIMAL *address); BACNET_STACK_EXPORT bool host_n_port_same( diff --git a/test/bacnet/hostnport/src/main.c b/test/bacnet/hostnport/src/main.c index f7d89053..6274f5de 100644 --- a/test/bacnet/hostnport/src/main.c +++ b/test/bacnet/hostnport/src/main.c @@ -13,18 +13,22 @@ #include #if defined(CONFIG_ZTEST_NEW_API) -ZTEST(create_object_tests, test_HostNPortMinimal_Init) +ZTEST(host_n_port_tests, test_HostNPortMinimal_Init) #else -static void test_HostNPortMinimal_Init(void) +static void test_HostNPortMinimal_Codec(void) #endif { + uint8_t apdu[MAX_APDU] = { 0 }; + int len = 0, apdu_len = 0, null_len = 0, test_len = 0; BACNET_HOST_N_PORT_MINIMAL test_data_1 = { 0 }, test_data_2 = { 0 }; BACNET_HOST_N_PORT host_n_port = { 0 }; uint8_t address[IP6_ADDRESS_MAX] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + const char *hostname = "bacnet.org"; uint16_t port = 0xBAC0; + BACNET_ERROR_CODE error_code = ERROR_CODE_SUCCESS; - /* test the init */ + /* test TAG for BACnet/IP address */ host_n_port_minimal_ip_init(&test_data_1, port, address, sizeof(address)); zassert_equal(test_data_1.tag, BACNET_HOST_ADDRESS_TAG_IP_ADDRESS, NULL); zassert_equal(test_data_1.port, port, NULL); @@ -36,6 +40,47 @@ static void test_HostNPortMinimal_Init(void) zassert_true( host_n_port_minimal_same(&test_data_1, &test_data_2), "test_data_1 != test_data_2"); + + null_len = host_n_port_minimal_encode(NULL, &test_data_1); + apdu_len = host_n_port_minimal_encode(apdu, &test_data_1); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = host_n_port_minimal_decode(apdu, apdu_len, NULL, NULL); + test_len = + host_n_port_minimal_decode(apdu, apdu_len, &error_code, &test_data_2); + zassert_equal(test_len, null_len, NULL); + zassert_equal( + apdu_len, test_len, "apdu_len=%d test_len=%d", apdu_len, test_len); + zassert_true( + host_n_port_minimal_same(&test_data_1, &test_data_2), + "test_data_1 != test_data_2"); + while (test_len) { + test_len--; + len = host_n_port_minimal_decode(apdu, test_len, NULL, NULL); + zassert_true(len < 0, "len=%d test_len=%d", len, test_len); + } + + /* test TAG for BACnet/IP hostname */ + host_n_port_minimal_hostname_init(&test_data_1, port, hostname); + zassert_equal(test_data_1.tag, BACNET_HOST_ADDRESS_TAG_NAME, NULL); + null_len = host_n_port_minimal_encode(NULL, &test_data_1); + apdu_len = host_n_port_minimal_encode(apdu, &test_data_1); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = host_n_port_minimal_decode(apdu, apdu_len, NULL, NULL); + test_len = + host_n_port_minimal_decode(apdu, apdu_len, &error_code, &test_data_2); + zassert_equal(test_len, null_len, NULL); + zassert_equal( + apdu_len, test_len, "apdu_len=%d test_len=%d", apdu_len, test_len); + zassert_true( + host_n_port_minimal_same(&test_data_1, &test_data_2), + "test_data_1 != test_data_2"); + while (test_len) { + test_len--; + len = host_n_port_minimal_decode(apdu, test_len, NULL, NULL); + zassert_true(len < 0, "len=%d test_len=%d", len, test_len); + } } static void test_HostNPortMinimal_Copy(BACNET_HOST_N_PORT *data) @@ -94,12 +139,15 @@ static void test_HostNPortCodec(BACNET_HOST_N_PORT *data) } #if defined(CONFIG_ZTEST_NEW_API) -ZTEST(create_object_tests, test_HostNPort) +ZTEST(host_n_port_tests, test_HostNPort) #else static void test_HostNPort(void) #endif { BACNET_HOST_N_PORT data = { 0 }; + bool status = false; + const char *dotted_ip = "192.168.1.1"; + const char *dotted_ip_port = "192.168.1.1:47808"; /* none */ test_HostNPortCodec(&data); @@ -117,10 +165,23 @@ static void test_HostNPort(void) data.port = 0xBAC0; test_HostNPortCodec(&data); test_HostNPortMinimal_Copy(&data); + + status = host_n_port_from_ascii(NULL, NULL); + zassert_false(status, NULL); + status = host_n_port_from_ascii(NULL, dotted_ip); + zassert_false(status, NULL); + status = host_n_port_from_ascii(&data, NULL); + zassert_false(status, NULL); + status = host_n_port_from_ascii(&data, dotted_ip); + zassert_true(status, NULL); + status = host_n_port_from_ascii(&data, dotted_ip_port); + zassert_true(status, NULL); + test_HostNPortCodec(&data); + test_HostNPortMinimal_Copy(&data); } #if defined(CONFIG_ZTEST_NEW_API) -ZTEST(create_object_tests, test_is_valid_hostname) +ZTEST(host_n_port_tests, test_is_valid_hostname) #else static void test_is_valid_hostname(void) #endif @@ -187,6 +248,114 @@ static void test_is_valid_hostname(void) zassert_false(status, NULL); } +#if defined(CONFIG_ZTEST_NEW_API) +ZTEST(host_n_port_tests, test_fdt_entry) +#else +static void test_fdt_entry(void) +#endif +{ + bool status = false; + const char *fdt_string = "1.2.3.4:47808,60,30"; + const char *ipv6_fdt_string = + "fe80:0000:0000:0000:020c:29ff:fe50:745b:47808,60,30"; + const char *bdt_string = "1.2.3.4:47808"; + const char *ipv6_bdt_string = + "fe48:0000:000d:0000:0002:0c29:fffe:5074:47808"; + char str[128] = { 0 }; + + BACNET_FDT_ENTRY fdt_entry = { 0 }, test_fdt_entry = { 0 }; + BACNET_BDT_ENTRY bdt_entry = { 0 }, test_bdt_entry = { 0 }; + uint8_t apdu[MAX_APDU] = { 0 }; + int apdu_len = 0, null_len = 0, test_len = 0; + BACNET_ERROR_CODE error_code = ERROR_CODE_SUCCESS; + + status = bacnet_fdt_entry_from_ascii(NULL, NULL); + zassert_false(status, NULL); + status = bacnet_fdt_entry_from_ascii(NULL, fdt_string); + zassert_false(status, NULL); + status = bacnet_fdt_entry_from_ascii(&fdt_entry, NULL); + zassert_false(status, NULL); + status = bacnet_fdt_entry_from_ascii(&fdt_entry, fdt_string); + zassert_true(status, NULL); + status = bacnet_fdt_entry_copy(&test_fdt_entry, &fdt_entry); + zassert_true(status, NULL); + status = bacnet_fdt_entry_same(&test_fdt_entry, &fdt_entry); + zassert_true(status, NULL); + + null_len = bacnet_fdt_entry_context_encode(NULL, 0, &fdt_entry); + apdu_len = bacnet_fdt_entry_context_encode(apdu, 0, &fdt_entry); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = + bacnet_fdt_entry_context_decode(apdu, apdu_len, 0, &error_code, NULL); + test_len = bacnet_fdt_entry_context_decode( + apdu, apdu_len, 0, &error_code, &test_fdt_entry); + zassert_equal(test_len, null_len, NULL); + test_len = bacnet_fdt_entry_to_ascii(str, sizeof(str), &test_fdt_entry); + zassert_true(test_len > 0, "%s", str); + + status = bacnet_fdt_entry_from_ascii(&fdt_entry, ipv6_fdt_string); + zassert_true(status, NULL); + null_len = bacnet_fdt_entry_context_encode(NULL, 0, &fdt_entry); + apdu_len = bacnet_fdt_entry_context_encode(apdu, 0, &fdt_entry); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = + bacnet_fdt_entry_context_decode(apdu, apdu_len, 0, &error_code, NULL); + test_len = bacnet_fdt_entry_context_decode( + apdu, apdu_len, 0, &error_code, &test_fdt_entry); + zassert_equal(test_len, null_len, NULL); + test_len = bacnet_fdt_entry_to_ascii(str, sizeof(str), &test_fdt_entry); + zassert_true(test_len > 0, "%s", str); + + status = bacnet_bdt_entry_from_ascii(NULL, NULL); + zassert_false(status, NULL); + status = bacnet_bdt_entry_from_ascii(NULL, bdt_string); + zassert_false(status, NULL); + status = bacnet_bdt_entry_from_ascii(&bdt_entry, NULL); + zassert_false(status, NULL); + status = bacnet_bdt_entry_from_ascii(&bdt_entry, bdt_string); + zassert_true(status, NULL); + status = bacnet_bdt_entry_copy(&test_bdt_entry, &bdt_entry); + zassert_true(status, NULL); + status = bacnet_bdt_entry_same(&test_bdt_entry, &bdt_entry); + zassert_true(status, NULL); + + null_len = bacnet_bdt_entry_encode(NULL, &bdt_entry); + apdu_len = bacnet_bdt_entry_encode(apdu, &bdt_entry); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = bacnet_bdt_entry_decode(apdu, apdu_len, &error_code, NULL); + test_len = + bacnet_bdt_entry_decode(apdu, apdu_len, &error_code, &test_bdt_entry); + zassert_equal(test_len, null_len, NULL); + test_len = bacnet_bdt_entry_to_ascii(str, sizeof(str), &test_bdt_entry); + zassert_true(test_len > 0, "%s", str); + + status = bacnet_bdt_entry_from_ascii(&bdt_entry, ipv6_bdt_string); + zassert_true(status, NULL); + null_len = bacnet_bdt_entry_encode(NULL, &bdt_entry); + apdu_len = bacnet_bdt_entry_encode(apdu, &bdt_entry); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = bacnet_bdt_entry_decode(apdu, apdu_len, &error_code, NULL); + test_len = + bacnet_bdt_entry_decode(apdu, apdu_len, &error_code, &test_bdt_entry); + zassert_equal(test_len, null_len, NULL); + test_len = bacnet_bdt_entry_to_ascii(str, sizeof(str), &test_bdt_entry); + zassert_true(test_len > 0, "%s", str); + + null_len = bacnet_bdt_entry_context_encode(NULL, 0, &bdt_entry); + apdu_len = bacnet_bdt_entry_context_encode(apdu, 0, &bdt_entry); + zassert_equal(apdu_len, null_len, NULL); + zassert_true(apdu_len != BACNET_STATUS_ERROR, NULL); + null_len = + bacnet_bdt_entry_context_decode(apdu, apdu_len, 0, &error_code, NULL); + test_len = bacnet_bdt_entry_context_decode( + apdu, apdu_len, 0, &error_code, &test_bdt_entry); + zassert_equal(test_len, null_len, NULL); +} + #if defined(CONFIG_ZTEST_NEW_API) ZTEST_SUITE(host_n_port_tests, NULL, NULL, NULL, NULL, NULL); #else @@ -194,8 +363,9 @@ void test_main(void) { ztest_test_suite( host_n_port_tests, ztest_unit_test(test_HostNPort), - ztest_unit_test(test_HostNPortMinimal_Init), - ztest_unit_test(test_is_valid_hostname)); + ztest_unit_test(test_HostNPortMinimal_Codec), + ztest_unit_test(test_is_valid_hostname), + ztest_unit_test(test_fdt_entry)); ztest_run_test_suite(host_n_port_tests); }