Feature/zeroing rx buffer remain (#90)
* Added zeroing rx buffer remain * Added zeroing rx buffer remain * Added safety margin for the rx-buffer in the different ports. * Added safety margin for the receive buffer. * Added DoxyGen comments. * Fixed checking return value when calculating distance between opening and closing tag on multiple properties.
This commit is contained in:
@@ -244,6 +244,7 @@ uint16_t bip_receive(BACNET_ADDRESS *src, /* source address */
|
||||
unsigned timeout)
|
||||
{
|
||||
int received_bytes = 0;
|
||||
int max = 0;
|
||||
uint16_t pdu_len = 0; /* return value */
|
||||
uint8_t src_addr[] = { 0, 0, 0, 0 };
|
||||
uint16_t src_port = 0;
|
||||
@@ -274,6 +275,18 @@ uint16_t bip_receive(BACNET_ADDRESS *src, /* source address */
|
||||
if (pdu[0] != BVLL_TYPE_BACNET_IP)
|
||||
return 0;
|
||||
|
||||
/* Erase up to 16 bytes after the received bytes as safety margin to
|
||||
* ensure that the decoding functions will run into a 'safe field'
|
||||
* of zero, if for any reason they would overrun, when parsing the
|
||||
* message. */
|
||||
max = (int)max_pdu - received_bytes;
|
||||
if (max > 0) {
|
||||
if (max > 16) {
|
||||
max = 16;
|
||||
}
|
||||
memset(&pdu[received_bytes], 0, max);
|
||||
}
|
||||
|
||||
if (bvlc_for_non_bbmd(src_addr, &src_port, pdu, received_bytes) > 0) {
|
||||
/* Handled, usually with a NACK. */
|
||||
#if PRINT_ENABLED
|
||||
|
||||
@@ -70,7 +70,17 @@ void setup()
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint8_t PDUBuffer[MAX_MPDU];
|
||||
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
|
||||
|
||||
static uint8_t PDUBuffer[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
|
||||
* so that in the rare case, the message
|
||||
* would be filled up to MAX_MPDU and some
|
||||
* decoding functions would overrun, these
|
||||
* decoding functions will just end up in
|
||||
* a safe field of static zeros. */];
|
||||
|
||||
/** Main */
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint16_t pdu_len = 0;
|
||||
@@ -84,7 +94,7 @@ int main(void)
|
||||
for (;;) {
|
||||
/* other tasks */
|
||||
/* BACnet handling */
|
||||
pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0);
|
||||
pdu_len = datalink_receive(&src, &PDUBuffer[0], MAX_MPDU, 0);
|
||||
if (pdu_len) {
|
||||
npdu_handler(&src, &PDUBuffer[0], pdu_len);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user