Added postpone reply because transmission of the segmented ComplexACK cannot begin until the node holds the token. (#1116)

This commit is contained in:
Steve Karg
2025-11-15 13:08:27 -06:00
committed by GitHub
parent 51358e83f9
commit 9d1abbeba5
17 changed files with 539 additions and 1105 deletions
+18 -121
View File
@@ -116,125 +116,6 @@ uint16_t MSTP_Get_Send(struct mstp_port_struct_t *mstp_port, unsigned timeout)
return pdu_len;
}
/**
* @brief Determine if the reply packet is the data expected
* @param mstp_port - specific MSTP port that is used for this datalink
* @param reply_pdu - PDU of the data
* @param reply_pdu_len - number of bytes of PDU data
* @param dest_address - the destination address for this data
* @return true if the reply packet is the data expected
*/
static bool MSTP_Compare_Data_Expecting_Reply(
volatile struct mstp_port_struct_t *mstp_port,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
const BACNET_ADDRESS *dest_address)
{
uint16_t offset;
/* One way to check the message is to compare NPDU
src, dest, along with the APDU type, invoke id.
Seems a bit overkill */
struct DER_compare_t {
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS address;
uint8_t pdu_type;
uint8_t invoke_id;
uint8_t service_choice;
};
struct DER_compare_t request;
struct DER_compare_t reply;
uint8_t *request_pdu;
uint16_t request_pdu_len;
uint8_t src_address;
request_pdu = &mstp_port->InputBuffer[0];
request_pdu_len = mstp_port->DataLength;
src_address = mstp_port->SourceAddress;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(
&request_pdu[0], request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
request.pdu_type = request_pdu[offset] & 0xF0;
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
return false;
}
request.invoke_id = request_pdu[offset + 2];
/* segmented message? */
if (request_pdu[offset] & BIT(3)) {
request.service_choice = request_pdu[offset + 5];
} else {
request.service_choice = request_pdu[offset + 3];
}
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = bacnet_npdu_decode(
&reply_pdu[0], reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
return false;
}
/* reply could be a lot of things:
confirmed, simple ack, abort, reject, error */
reply.pdu_type = reply_pdu[offset] & 0xF0;
switch (reply.pdu_type) {
case PDU_TYPE_SIMPLE_ACK:
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_COMPLEX_ACK:
reply.invoke_id = reply_pdu[offset + 1];
/* segmented message? */
if (reply_pdu[offset] & BIT(3)) {
reply.service_choice = reply_pdu[offset + 4];
} else {
reply.service_choice = reply_pdu[offset + 2];
}
break;
case PDU_TYPE_ERROR:
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_REJECT:
case PDU_TYPE_ABORT:
case PDU_TYPE_SEGMENT_ACK:
reply.invoke_id = reply_pdu[offset + 1];
break;
default:
return false;
}
/* these don't have service choice included */
if ((reply.pdu_type == PDU_TYPE_REJECT) ||
(reply.pdu_type == PDU_TYPE_ABORT) ||
(reply.pdu_type == PDU_TYPE_SEGMENT_ACK)) {
if (request.invoke_id != reply.invoke_id) {
return false;
}
} else {
if (request.invoke_id != reply.invoke_id) {
return false;
}
if (request.service_choice != reply.service_choice) {
return false;
}
}
if (request.npdu_data.protocol_version !=
reply.npdu_data.protocol_version) {
return false;
}
if (request.npdu_data.priority != reply.npdu_data.priority) {
return false;
}
if (!bacnet_address_same(&request.address, &reply.address)) {
return false;
}
return true;
}
/**
* @brief The MS/TP state machine uses this function for getting data to send
* as the reply to a DATA_EXPECTING_REPLY frame, or nothing
@@ -263,11 +144,27 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
/* look at next PDU in queue without removing it */
pkt = (struct dlmstp_packet *)(void *)Ringbuf_Peek(&user->PDU_Queue);
/* is this the reply to the DER? */
matched = MSTP_Compare_Data_Expecting_Reply(
mstp_port, pkt->pdu, pkt->pdu_len, &pkt->address);
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, &pkt->pdu[0], pkt->pdu_len,
pkt->address.mac[0]);
if (!matched) {
return 0;
}
if (npdu_is_segmented_complex_ack_reply(pkt->pdu, pkt->pdu_len)) {
/* In Clause 5.4.5.3, AWAIT_RESPONSE, in the transition
SendSegmentedComplexACK, the text "transmit a BACnet-
ComplexACK-PDU..." shall be replaced by "direct the
MS/TP data link to transmit a Reply Postponed frame;
transmit a BACnet-ComplexACK-PDU...."
It is necessary to postpone the reply because
transmission of the segmented ComplexACK
cannot begin until the node holds the token.*/
return MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], mstp_port->OutputBufferSize,
FRAME_TYPE_REPLY_POSTPONED, mstp_port->SourceAddress,
mstp_port->This_Station, NULL, 0);
}
/* convert the PDU into the MSTP Frame */
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], mstp_port->OutputBufferSize,
+191
View File
@@ -10,6 +10,7 @@
/* BACnet Stack defines - first */
#include "bacnet/bacdef.h"
/* BACnet Stack API */
#include "bacnet/bacaddr.h"
#include "bacnet/bacdcode.h"
#include "bacnet/bacint.h"
#include "bacnet/npdu.h"
@@ -616,3 +617,193 @@ bool npdu_confirmed_service(const uint8_t *pdu, uint16_t pdu_len)
return status;
}
/**
* @brief Helper for datalink detecting an segmented complex ack reply
* @param pdu [in] Buffer containing the NPDU and APDU of the received packet.
* @param pdu_len [in] The size of the received message in the pdu[] buffer.
* @return true if the PDU is a segmented complex ack reply
*/
bool npdu_is_segmented_complex_ack_reply(const uint8_t *pdu, uint16_t pdu_len)
{
bool status = false;
int apdu_offset = 0;
BACNET_NPDU_DATA npdu_data = { 0 };
if (pdu_len > 0) {
if (pdu[0] == BACNET_PROTOCOL_VERSION) {
/* only handle the version that we know how to handle */
apdu_offset =
bacnet_npdu_decode(pdu, pdu_len, NULL, NULL, &npdu_data);
if ((!npdu_data.network_layer_message) && (apdu_offset > 0) &&
(apdu_offset < pdu_len)) {
if ((pdu[apdu_offset] & 0xF0) == PDU_TYPE_COMPLEX_ACK) {
/* segmented message? */
if (pdu[apdu_offset] & BIT(3)) {
status = true;
}
}
}
}
}
return status;
}
/**
* @brief Determine if the reply PDU is expected by the request PDU
* @param request_pdu - packet containing the NDPU and APDU of the request
* @param request_pdu_len - number of bytes of PDU data
* @param request_address - address of the sender of the request
* @param reply_pdu - packet containing the NDPU and APDU of the reply
* @param reply_pdu_len - number of bytes of PDU data
* @param reply_address - address of the sender of the reply
* @return true if the reply PDU is the data expected by the request PDU
* @note This function is used by the DLMSTP datalink layer to match confirmed
* service requests with their replies in the ANSWER_DATA_REQUEST state.
*/
bool npdu_is_expected_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
BACNET_ADDRESS *request_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
BACNET_ADDRESS *reply_address)
{
int16_t offset;
/* One way to check the message is to compare NPDU
src, dest, along with the APDU type, invoke id. */
struct DER_compare_t {
BACNET_NPDU_DATA npdu_data;
uint8_t pdu_type;
uint8_t invoke_id;
uint8_t service_choice;
};
struct DER_compare_t request = { 0 };
struct DER_compare_t reply = { 0 };
if (!request_pdu || !reply_pdu || !request_address || !reply_address) {
return false;
}
if ((request_pdu_len > 0) && (request_pdu[0] != BACNET_PROTOCOL_VERSION)) {
/* we don't know how to decode any other protocol versions */
return false;
}
/* decode the request NPDU and the source address */
offset = (int16_t)bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, request_address,
&request.npdu_data);
if (offset <= 0) {
return false;
}
if (request.npdu_data.network_layer_message) {
return false;
}
request.pdu_type = request_pdu[offset] & 0xF0;
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
return false;
}
request.invoke_id = request_pdu[offset + 2];
/* segmented request? */
if (request_pdu[offset] & BIT(3)) {
request.service_choice = request_pdu[offset + 5];
} else {
request.service_choice = request_pdu[offset + 3];
}
if ((reply_pdu_len > 0) && (reply_pdu[0] != BACNET_PROTOCOL_VERSION)) {
/* we don't know how to decode any other protocol versions */
return false;
}
/* decode the reply NPDU and the destination address */
offset = (int16_t)bacnet_npdu_decode(
reply_pdu, reply_pdu_len, reply_address, NULL, &reply.npdu_data);
if (offset <= 0) {
return false;
}
if (reply.npdu_data.network_layer_message) {
return false;
}
/* reply could be a lot of things:
confirmed, simple ack, abort, reject, error */
reply.pdu_type = reply_pdu[offset] & 0xF0;
switch (reply.pdu_type) {
case PDU_TYPE_SIMPLE_ACK:
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_COMPLEX_ACK:
reply.invoke_id = reply_pdu[offset + 1];
/* segmented message? */
if (reply_pdu[offset] & BIT(3)) {
reply.service_choice = reply_pdu[offset + 4];
} else {
reply.service_choice = reply_pdu[offset + 2];
}
break;
case PDU_TYPE_ERROR:
reply.invoke_id = reply_pdu[offset + 1];
reply.service_choice = reply_pdu[offset + 2];
break;
case PDU_TYPE_REJECT:
case PDU_TYPE_ABORT:
case PDU_TYPE_SEGMENT_ACK:
reply.invoke_id = reply_pdu[offset + 1];
break;
default:
/* A queued request, just look for another */
return false;
}
if (request.invoke_id != reply.invoke_id) {
/* Normal to have multiple replies queued, just look for another */
return false;
}
/* these reply messages don't have service choice included */
if ((reply.pdu_type != PDU_TYPE_REJECT) &&
(reply.pdu_type != PDU_TYPE_ABORT) &&
(reply.pdu_type != PDU_TYPE_SEGMENT_ACK)) {
if (request.service_choice != reply.service_choice) {
return false;
}
}
if (request.npdu_data.priority != reply.npdu_data.priority) {
return false;
}
if (!bacnet_address_same(request_address, reply_address)) {
return false;
}
return true;
}
/**
* @brief Determine if the reply PDU is the data expected by the request PDU
* @param request_pdu - packet containing the NDPU and APDU of the request
* @param request_pdu_len - number of bytes of PDU data
* @param request_mac - MS/TP MAC address of the sender of the request
* @param reply_pdu - packet containing the NDPU and APDU of the reply
* @param reply_pdu_len - number of bytes of PDU data
* @param reply_mac - MS/TP MAC address of the sender of the reply
* @return true if the reply PDU is the data expected by the request PDU
* @note This function is used by the DLMSTP datalink layer to match confirmed
* service requests with their replies in the ANSWER_DATA_REQUEST state.
*/
bool npdu_is_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t request_mac,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t reply_mac)
{
BACNET_ADDRESS request_address = { 0 };
BACNET_ADDRESS reply_address = { 0 };
request_address.len = 1;
request_address.adr[0] = request_mac;
reply_address.len = 1;
reply_address.adr[0] = reply_mac;
return npdu_is_expected_reply(
request_pdu, request_pdu_len, &request_address, reply_pdu,
reply_pdu_len, &reply_address);
}
+17
View File
@@ -113,6 +113,23 @@ int bacnet_npdu_decode(
BACNET_STACK_EXPORT
bool npdu_confirmed_service(const uint8_t *pdu, uint16_t pdu_len);
BACNET_STACK_EXPORT
bool npdu_is_segmented_complex_ack_reply(const uint8_t *pdu, uint16_t pdu_len);
BACNET_STACK_EXPORT
bool npdu_is_expected_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
BACNET_ADDRESS *request_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
BACNET_ADDRESS *reply_address);
bool npdu_is_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t request_mac,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t reply_mac);
#ifdef __cplusplus
}