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
+14 -15
View File
@@ -34,7 +34,6 @@
#include "bacnet/apdu.h"
#include "bacnet/basic/services.h"
#include "bacnet/basic/sys/debug.h"
#include "bacnet/basic/tsm/tsm.h"
#include "bacnet/datalink/datalink.h"
#if PRINT_ENABLED
@@ -76,8 +75,9 @@ int npdu_send_network_number_is(
int pdu_len = 0;
int bytes_sent = 0;
bool data_expecting_reply = false;
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS my_address;
BACNET_NPDU_DATA npdu_data = { 0 };
BACNET_ADDRESS my_address = { 0 };
uint8_t pdu[MAX_NPDU + 2 + 1] = { 0 };
/* Upon receipt of a What-Is-Network-Number message,
a device that knows the local network number shall
@@ -86,14 +86,14 @@ int npdu_send_network_number_is(
datalink_get_my_address(&my_address);
npdu_encode_npdu_network(&npdu_data, NETWORK_MESSAGE_NETWORK_NUMBER_IS,
data_expecting_reply, MESSAGE_PRIORITY_NORMAL);
pdu_len = npdu_encode_pdu(
&Handler_Transmit_Buffer[0], dst, &my_address, &npdu_data);
len = encode_unsigned16(&Handler_Transmit_Buffer[pdu_len], net);
pdu_len += len;
Handler_Transmit_Buffer[pdu_len] = status;
pdu_len++;
bytes_sent = datalink_send_pdu(
dst, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len);
pdu_len = npdu_encode_pdu(pdu, dst, &my_address, &npdu_data);
if ((pdu_len > 0) && (pdu_len <= MAX_NPDU)) {
len = encode_unsigned16(&pdu[pdu_len], net);
pdu_len += len;
pdu[pdu_len] = status;
pdu_len++;
bytes_sent = datalink_send_pdu(dst, &npdu_data, pdu, pdu_len);
}
return bytes_sent;
}
@@ -111,6 +111,7 @@ int npdu_send_what_is_network_number(BACNET_ADDRESS *dst)
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS daddr = { 0 };
BACNET_ADDRESS saddr = { 0 };
uint8_t pdu[MAX_NPDU] = { 0 };
if (dst) {
bacnet_address_copy(&daddr, dst);
@@ -120,12 +121,10 @@ int npdu_send_what_is_network_number(BACNET_ADDRESS *dst)
datalink_get_my_address(&saddr);
npdu_encode_npdu_network(&npdu_data, NETWORK_MESSAGE_WHAT_IS_NETWORK_NUMBER,
data_expecting_reply, MESSAGE_PRIORITY_NORMAL);
pdu_len = npdu_encode_pdu(
&Handler_Transmit_Buffer[0], &daddr, &saddr, &npdu_data);
pdu_len = npdu_encode_pdu(pdu, &daddr, &saddr, &npdu_data);
/* Now send the message */
return datalink_send_pdu(
dst, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len);
return datalink_send_pdu(dst, &npdu_data, pdu, pdu_len);
}
/** @file h_npdu.c Handles messages at the NPDU level of the BACnet stack. */