Changed npdu_encode function to return length when given a NULL buffer. (#549)

* Changed npdu_encode function to return length when given a NULL buffer.

* reduce TSM dependency in NPDU handler and use local buffer

* add bacnet API with additional size of PDU argument.

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-12-27 10:29:42 -06:00
committed by GitHub
parent 0aa6b242b3
commit 73ee75635b
4 changed files with 192 additions and 94 deletions
+23 -10
View File
@@ -25,34 +25,47 @@ ZTEST(npdu_tests, test_NPDU_Network)
static void test_NPDU_Network(void)
#endif
{
uint8_t pdu[480] = { 0 };
uint8_t pdu[MAX_NPDU] = { 0 };
BACNET_ADDRESS dest = { 0 };
BACNET_ADDRESS src = { 0 };
BACNET_ADDRESS npdu_dest = { 0 };
BACNET_ADDRESS npdu_src = { 0 };
int len = 0;
int len = 0, null_len = 0, test_len = 0;
bool data_expecting_reply = true;
BACNET_NETWORK_MESSAGE_TYPE network_message_type =
NETWORK_MESSAGE_NETWORK_NUMBER_IS;
BACNET_MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL;
BACNET_NPDU_DATA npdu_data = { 0 };
int npdu_len = 0;
bool network_layer_message = true;
uint16_t vendor_id = 0; /* optional, if net message type is > 0x80 */
npdu_encode_npdu_network(&npdu_data,
network_message_type,
data_expecting_reply, priority);
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
null_len = bacnet_npdu_encode_pdu(NULL, 0, &dest, &src, &npdu_data);
len = bacnet_npdu_encode_pdu(&pdu[0], sizeof(pdu), &dest, &src, &npdu_data);
zassert_equal(len, null_len, NULL);
zassert_not_equal(len, 0, NULL);
/* can we get the info back? */
npdu_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
zassert_not_equal(npdu_len, 0, NULL);
test_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
zassert_not_equal(test_len, 0, NULL);
zassert_equal(len, test_len, NULL);
zassert_equal(npdu_data.data_expecting_reply, data_expecting_reply, NULL);
zassert_equal(npdu_data.network_layer_message, network_layer_message, NULL);
zassert_equal(npdu_data.network_message_type, network_message_type, NULL);
zassert_equal(npdu_data.vendor_id, vendor_id, NULL);
zassert_equal(npdu_data.priority, priority, NULL);
/* test for invalid short PDU */
while (len) {
len--;
test_len = bacnet_npdu_decode(pdu, len, &npdu_dest, &npdu_src, &npdu_data);
if (len == 2) {
/* special case with no NPDU options */
zassert_equal(len, test_len, NULL);
} else {
zassert_true(test_len <= 0, "len=%d test_len=%d\n", len, test_len);
}
}
}
/**
@@ -64,7 +77,7 @@ ZTEST(npdu_tests, testNPDU2)
static void testNPDU2(void)
#endif
{
uint8_t pdu[480] = { 0 };
uint8_t pdu[MAX_NPDU] = { 0 };
BACNET_ADDRESS dest = { 0 };
BACNET_ADDRESS src = { 0 };
BACNET_ADDRESS npdu_dest = { 0 };
@@ -100,7 +113,7 @@ static void testNPDU2(void)
src.adr[i] = 0x40;
}
npdu_encode_npdu_data(&npdu_data, true, priority);
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
len = bacnet_npdu_encode_pdu(&pdu[0], sizeof(pdu), &dest, &src, &npdu_data);
zassert_not_equal(len, 0, NULL);
/* can we get the info back? */
npdu_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);
@@ -132,7 +145,7 @@ ZTEST(npdu_tests, testNPDU1)
static void testNPDU1(void)
#endif
{
uint8_t pdu[480] = { 0 };
uint8_t pdu[MAX_NPDU] = { 0 };
BACNET_ADDRESS dest = { 0 };
BACNET_ADDRESS src = { 0 };
BACNET_ADDRESS npdu_dest = { 0 };
@@ -169,7 +182,7 @@ static void testNPDU1(void)
src.adr[i] = 0;
}
npdu_encode_npdu_data(&npdu_data, false, priority);
len = npdu_encode_pdu(&pdu[0], &dest, &src, &npdu_data);
len = bacnet_npdu_encode_pdu(&pdu[0], sizeof(pdu), &dest, &src, &npdu_data);
zassert_not_equal(len, 0, NULL);
/* can we get the info back? */
npdu_len = bacnet_npdu_decode(pdu, sizeof(pdu), &npdu_dest, &npdu_src, &npdu_data);