|
|
|
@@ -6,7 +6,13 @@
|
|
|
|
|
* @copyright SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
|
|
|
|
#include <zephyr/ztest.h>
|
|
|
|
|
#include <bacnet/abort.h>
|
|
|
|
|
#include <bacnet/bacerror.h>
|
|
|
|
|
#include <bacnet/bacdcode.h>
|
|
|
|
|
#include <bacnet/npdu.h>
|
|
|
|
|
#include <bacnet/reject.h>
|
|
|
|
|
#include <bacnet/rp.h>
|
|
|
|
|
#include <bacnet/whois.h>
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @addtogroup bacnet_tests
|
|
|
|
@@ -27,7 +33,7 @@ static void test_NPDU_Network(void)
|
|
|
|
|
BACNET_ADDRESS src = { 0 };
|
|
|
|
|
BACNET_ADDRESS npdu_dest = { 0 };
|
|
|
|
|
BACNET_ADDRESS npdu_src = { 0 };
|
|
|
|
|
int len = 0, null_len = 0, test_len = 0;
|
|
|
|
|
int len = 0, null_len = 0, test_len = 0, legacy_len;
|
|
|
|
|
bool data_expecting_reply = true;
|
|
|
|
|
BACNET_NETWORK_MESSAGE_TYPE network_message_type =
|
|
|
|
|
NETWORK_MESSAGE_NETWORK_NUMBER_IS;
|
|
|
|
@@ -43,8 +49,10 @@ static void test_NPDU_Network(void)
|
|
|
|
|
zassert_equal(len, null_len, NULL);
|
|
|
|
|
zassert_not_equal(len, 0, NULL);
|
|
|
|
|
/* can we get the info back? */
|
|
|
|
|
legacy_len = npdu_decode(pdu, &npdu_dest, &npdu_src, &npdu_data);
|
|
|
|
|
test_len =
|
|
|
|
|
bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
|
|
|
|
|
zassert_equal(test_len, legacy_len, NULL);
|
|
|
|
|
zassert_not_equal(test_len, 0, NULL);
|
|
|
|
|
zassert_equal(len, test_len, NULL);
|
|
|
|
|
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
|
|
|
|
@@ -199,9 +207,284 @@ static void testNPDU1(void)
|
|
|
|
|
zassert_equal(npdu_dest.mac_len, src.mac_len, NULL);
|
|
|
|
|
zassert_equal(npdu_src.mac_len, dest.mac_len, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
|
|
|
ZTEST(npdu_tests, test_NPDU_Copy)
|
|
|
|
|
#else
|
|
|
|
|
static void test_NPDU_Copy(void)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
BACNET_NPDU_DATA npdu_data = { 0 };
|
|
|
|
|
BACNET_NPDU_DATA npdu_data_copy = { 0 };
|
|
|
|
|
bool data_expecting_reply = true;
|
|
|
|
|
BACNET_MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_LIFE_SAFETY;
|
|
|
|
|
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, data_expecting_reply, priority);
|
|
|
|
|
npdu_copy_data(&npdu_data_copy, &npdu_data);
|
|
|
|
|
zassert_equal(
|
|
|
|
|
npdu_data_copy.data_expecting_reply, npdu_data.data_expecting_reply,
|
|
|
|
|
NULL);
|
|
|
|
|
zassert_equal(
|
|
|
|
|
npdu_data_copy.network_layer_message, npdu_data.network_layer_message,
|
|
|
|
|
NULL);
|
|
|
|
|
if (npdu_data_copy.network_layer_message) {
|
|
|
|
|
zassert_equal(
|
|
|
|
|
npdu_data_copy.network_message_type, npdu_data.network_message_type,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
|
|
|
|
zassert_equal(npdu_data_copy.vendor_id, npdu_data.vendor_id, NULL);
|
|
|
|
|
zassert_equal(npdu_data_copy.priority, npdu_data.priority, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @}
|
|
|
|
|
* @brief Initialize the a data link broadcast address
|
|
|
|
|
* @param dest - address to be filled with broadcast designator
|
|
|
|
|
*/
|
|
|
|
|
static void mstp_address_init(BACNET_ADDRESS *dest, uint8_t mac)
|
|
|
|
|
{
|
|
|
|
|
int i = 0; /* counter */
|
|
|
|
|
|
|
|
|
|
if (dest) {
|
|
|
|
|
dest->mac_len = 1;
|
|
|
|
|
dest->mac[0] = mac;
|
|
|
|
|
dest->net = 0; /* local only, no routing */
|
|
|
|
|
dest->len = 0; /* not routed */
|
|
|
|
|
for (i = 0; i < MAX_MAC_LEN; i++) {
|
|
|
|
|
dest->adr[i] = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
|
|
|
ZTEST(npdu_tests, test_NPDU_Confirmed_Service)
|
|
|
|
|
#else
|
|
|
|
|
static void test_NPDU_Confirmed_Service(void)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
BACNET_READ_PROPERTY_DATA rpdata = { 0 };
|
|
|
|
|
BACNET_NPDU_DATA npdu_data = { 0 };
|
|
|
|
|
BACNET_ADDRESS test_address = { 0 };
|
|
|
|
|
uint8_t apdu[MAX_APDU] = { 0 };
|
|
|
|
|
uint8_t pdu[MAX_NPDU + MAX_APDU] = { 0 };
|
|
|
|
|
int pdu_len = 0, npdu_len = 0, apdu_len = 0;
|
|
|
|
|
uint8_t invoke_id = 1;
|
|
|
|
|
bool status;
|
|
|
|
|
|
|
|
|
|
mstp_address_init(&test_address, 1);
|
|
|
|
|
rpdata.object_type = OBJECT_DEVICE;
|
|
|
|
|
rpdata.object_instance = 12345;
|
|
|
|
|
rpdata.object_property = PROP_OBJECT_NAME;
|
|
|
|
|
rpdata.array_index = BACNET_ARRAY_ALL;
|
|
|
|
|
rpdata.application_data = &apdu[0];
|
|
|
|
|
rpdata.application_data_len = sizeof(apdu);
|
|
|
|
|
rpdata.error_class = ERROR_CLASS_SERVICES;
|
|
|
|
|
rpdata.error_code = ERROR_CODE_OTHER;
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, true, MESSAGE_PRIORITY_NORMAL);
|
|
|
|
|
npdu_len =
|
|
|
|
|
npdu_encode_pdu(&pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
zassert_not_equal(npdu_len, 0, NULL);
|
|
|
|
|
/* confirmed service */
|
|
|
|
|
apdu_len = rp_encode_apdu(&pdu[npdu_len], invoke_id, &rpdata);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
pdu_len = npdu_len + apdu_len;
|
|
|
|
|
status = npdu_confirmed_service(pdu, pdu_len);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
/* unconfirmed service */
|
|
|
|
|
apdu_len = whois_encode_apdu(&pdu[npdu_len], -1, -1);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
pdu_len = npdu_len + apdu_len;
|
|
|
|
|
status = npdu_confirmed_service(pdu, pdu_len);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
|
|
|
ZTEST(npdu_tests, test_NPDU_Segmented_Complex_Ack_Reply)
|
|
|
|
|
#else
|
|
|
|
|
static void test_NPDU_Segmented_Complex_Ack_Reply(void)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
BACNET_READ_PROPERTY_DATA rpdata = { 0 };
|
|
|
|
|
BACNET_NPDU_DATA npdu_data = { 0 };
|
|
|
|
|
BACNET_ADDRESS test_address = { 0 };
|
|
|
|
|
uint8_t apdu[MAX_APDU] = { 0 };
|
|
|
|
|
uint8_t pdu[MAX_NPDU + MAX_APDU] = { 0 };
|
|
|
|
|
int pdu_len = 0, npdu_len = 0, apdu_len = 0;
|
|
|
|
|
uint8_t invoke_id = 1;
|
|
|
|
|
bool status;
|
|
|
|
|
|
|
|
|
|
mstp_address_init(&test_address, 1);
|
|
|
|
|
rpdata.object_type = OBJECT_DEVICE;
|
|
|
|
|
rpdata.object_instance = 12345;
|
|
|
|
|
rpdata.object_property = PROP_OBJECT_NAME;
|
|
|
|
|
rpdata.array_index = BACNET_ARRAY_ALL;
|
|
|
|
|
rpdata.application_data = &apdu[0];
|
|
|
|
|
rpdata.application_data_len = sizeof(apdu);
|
|
|
|
|
rpdata.error_class = ERROR_CLASS_SERVICES;
|
|
|
|
|
rpdata.error_code = ERROR_CODE_OTHER;
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, true, MESSAGE_PRIORITY_NORMAL);
|
|
|
|
|
npdu_len =
|
|
|
|
|
npdu_encode_pdu(&pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
zassert_not_equal(npdu_len, 0, NULL);
|
|
|
|
|
/* confirmed service */
|
|
|
|
|
pdu_len = npdu_len;
|
|
|
|
|
apdu_len = rp_ack_encode_apdu_init(&pdu[pdu_len], invoke_id, &rpdata);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
pdu_len += apdu_len;
|
|
|
|
|
apdu_len = rp_ack_encode_apdu_object_property_end(&pdu[pdu_len]);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
pdu_len += apdu_len;
|
|
|
|
|
status = npdu_is_segmented_complex_ack_reply(pdu, pdu_len);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
/* make it look segmented */
|
|
|
|
|
pdu[npdu_len] |= BIT(3);
|
|
|
|
|
status = npdu_is_segmented_complex_ack_reply(pdu, pdu_len);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
|
|
|
ZTEST(npdu_tests, test_NPDU_Data_Expecting_Reply)
|
|
|
|
|
#else
|
|
|
|
|
static void test_NPDU_Data_Expecting_Reply(void)
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
BACNET_READ_PROPERTY_DATA rpdata = { 0 };
|
|
|
|
|
BACNET_NPDU_DATA npdu_data = { 0 };
|
|
|
|
|
BACNET_ADDRESS test_address = { 0 }, reply_address = { 0 };
|
|
|
|
|
uint8_t apdu[MAX_APDU] = { 0 };
|
|
|
|
|
uint8_t request_pdu[MAX_NPDU + MAX_APDU] = { 0 };
|
|
|
|
|
uint8_t reply_pdu[MAX_NPDU + MAX_APDU] = { 0 };
|
|
|
|
|
int request_pdu_len = 0, reply_pdu_len = 0, npdu_len = 0, apdu_len = 0;
|
|
|
|
|
uint8_t invoke_id = 1;
|
|
|
|
|
bool status;
|
|
|
|
|
|
|
|
|
|
/* request */
|
|
|
|
|
mstp_address_init(&test_address, 1);
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, true, MESSAGE_PRIORITY_NORMAL);
|
|
|
|
|
npdu_len = npdu_encode_pdu(
|
|
|
|
|
&request_pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
zassert_not_equal(npdu_len, 0, NULL);
|
|
|
|
|
rpdata.object_type = OBJECT_DEVICE;
|
|
|
|
|
rpdata.object_instance = 12345;
|
|
|
|
|
rpdata.object_property = PROP_OBJECT_NAME;
|
|
|
|
|
rpdata.array_index = BACNET_ARRAY_ALL;
|
|
|
|
|
rpdata.application_data = &apdu[0];
|
|
|
|
|
rpdata.application_data_len = sizeof(apdu);
|
|
|
|
|
rpdata.error_class = ERROR_CLASS_SERVICES;
|
|
|
|
|
rpdata.error_code = ERROR_CODE_OTHER;
|
|
|
|
|
apdu_len = rp_encode_apdu(&request_pdu[npdu_len], invoke_id, &rpdata);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
request_pdu_len = npdu_len + apdu_len;
|
|
|
|
|
/* reply */
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL);
|
|
|
|
|
npdu_len = npdu_encode_pdu(
|
|
|
|
|
&reply_pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
zassert_not_equal(npdu_len, 0, NULL);
|
|
|
|
|
reply_pdu_len = npdu_len;
|
|
|
|
|
apdu_len =
|
|
|
|
|
rp_ack_encode_apdu_init(&reply_pdu[reply_pdu_len], invoke_id, &rpdata);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
reply_pdu_len += apdu_len;
|
|
|
|
|
apdu_len =
|
|
|
|
|
rp_ack_encode_apdu_object_property_end(&reply_pdu[reply_pdu_len]);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
reply_pdu_len += apdu_len;
|
|
|
|
|
/* is this the reply? */
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
/* using the MAC version of the function */
|
|
|
|
|
status = npdu_is_data_expecting_reply(
|
|
|
|
|
request_pdu, request_pdu_len, test_address.mac[0], reply_pdu,
|
|
|
|
|
reply_pdu_len, test_address.mac[0]);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
/* different address */
|
|
|
|
|
mstp_address_init(&reply_address, 4);
|
|
|
|
|
npdu_len = npdu_encode_pdu(
|
|
|
|
|
&reply_pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&reply_address);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
/* different protocol version*/
|
|
|
|
|
request_pdu[0] = BACNET_PROTOCOL_VERSION + 1;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
request_pdu[0] = BACNET_PROTOCOL_VERSION;
|
|
|
|
|
reply_pdu[0] = BACNET_PROTOCOL_VERSION + 1;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
reply_pdu[0] = BACNET_PROTOCOL_VERSION;
|
|
|
|
|
/* different network priority */
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_LIFE_SAFETY);
|
|
|
|
|
npdu_len = npdu_encode_pdu(
|
|
|
|
|
&reply_pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
zassert_not_equal(npdu_len, 0, NULL);
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL);
|
|
|
|
|
npdu_len = npdu_encode_pdu(
|
|
|
|
|
&reply_pdu[0], &test_address, &test_address, &npdu_data);
|
|
|
|
|
zassert_not_equal(npdu_len, 0, NULL);
|
|
|
|
|
/* different reply PDU type */
|
|
|
|
|
reply_pdu[npdu_len + 2] = SERVICE_CONFIRMED_WRITE_PROPERTY;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
reply_pdu[npdu_len + 2] = SERVICE_CONFIRMED_READ_PROPERTY;
|
|
|
|
|
/* change the invoke ID in the reply */
|
|
|
|
|
reply_pdu[npdu_len + 1] = 2;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_false(status, NULL);
|
|
|
|
|
reply_pdu[npdu_len + 1] = invoke_id;
|
|
|
|
|
/* reply with ERROR PDU */
|
|
|
|
|
apdu_len = bacerror_encode_apdu(
|
|
|
|
|
&reply_pdu[npdu_len], invoke_id, SERVICE_CONFIRMED_READ_PROPERTY,
|
|
|
|
|
ERROR_CLASS_OBJECT, ERROR_CODE_UNKNOWN_OBJECT);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
reply_pdu_len = npdu_len + apdu_len;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
/* reply with REJECT PDU */
|
|
|
|
|
apdu_len = reject_encode_apdu(
|
|
|
|
|
&reply_pdu[npdu_len], invoke_id, REJECT_REASON_OTHER);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
reply_pdu_len = npdu_len + apdu_len;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
/* reply with ABORT PDU */
|
|
|
|
|
apdu_len = abort_encode_apdu(
|
|
|
|
|
&reply_pdu[npdu_len], invoke_id, ABORT_REASON_OTHER, true);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
reply_pdu_len = npdu_len + apdu_len;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
zassert_true(status, NULL);
|
|
|
|
|
/* reply with simple ack - note this is totally fake! */
|
|
|
|
|
apdu_len = encode_simple_ack(
|
|
|
|
|
&reply_pdu[npdu_len], invoke_id, SERVICE_CONFIRMED_READ_PROPERTY);
|
|
|
|
|
zassert_true(apdu_len > 0, NULL);
|
|
|
|
|
reply_pdu_len = npdu_len + apdu_len;
|
|
|
|
|
status = npdu_is_expected_reply(
|
|
|
|
|
request_pdu, request_pdu_len, &test_address, reply_pdu, reply_pdu_len,
|
|
|
|
|
&test_address);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(CONFIG_ZTEST_NEW_API)
|
|
|
|
|
ZTEST_SUITE(npdu_tests, NULL, NULL, NULL, NULL, NULL);
|
|
|
|
@@ -210,7 +493,10 @@ void test_main(void)
|
|
|
|
|
{
|
|
|
|
|
ztest_test_suite(
|
|
|
|
|
npdu_tests, ztest_unit_test(testNPDU1), ztest_unit_test(testNPDU2),
|
|
|
|
|
ztest_unit_test(test_NPDU_Network));
|
|
|
|
|
ztest_unit_test(test_NPDU_Network), ztest_unit_test(test_NPDU_Copy),
|
|
|
|
|
ztest_unit_test(test_NPDU_Confirmed_Service),
|
|
|
|
|
ztest_unit_test(test_NPDU_Segmented_Complex_Ack_Reply),
|
|
|
|
|
ztest_unit_test(test_NPDU_Data_Expecting_Reply));
|
|
|
|
|
|
|
|
|
|
ztest_run_test_suite(npdu_tests);
|
|
|
|
|
}
|
|
|
|
|