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
+1
View File
@@ -9,6 +9,7 @@ SRCS = main.c \
${BACNET_PORT_DIR}/rs485.c \
${BACNET_PORT_DIR}/mstimer-init.c \
${BACNET_PORT_DIR}/datetime-init.c \
${BACNET_SRC_DIR}/bacnet/bacaddr.c \
${BACNET_SRC_DIR}/bacnet/bacdcode.c \
${BACNET_SRC_DIR}/bacnet/bacint.c \
${BACNET_SRC_DIR}/bacnet/bacreal.c \
+4 -104
View File
@@ -195,106 +195,6 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS *src, uint8_t mstp_address)
}
}
static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(request_pdu, 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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = bacnet_npdu_decode(
reply_pdu, 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;
}
if (request.invoke_id != reply.invoke_id) {
return false;
}
/* these services 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.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;
}
/* MS/TP Frame Format */
/* All frames are of the following format: */
/* */
@@ -1063,9 +963,9 @@ static bool MSTP_Master_Node_FSM(void)
case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST:
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue);
if (pkt != NULL) {
matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0],
DataLength, SourceAddress, &pkt->buffer[0], pkt->length,
pkt->destination_mac);
matched = npdu_is_data_expecting_reply(
&InputBuffer[0], DataLength, SourceAddress,
&pkt->buffer[0], pkt->length, pkt->destination_mac);
} else {
matched = false;
}
@@ -1161,7 +1061,7 @@ static void MSTP_Slave_Node_FSM(void)
} else if (MSTP_Flag.ReceivePacketPending) {
if (!Ringbuf_Empty(&PDU_Queue)) {
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue);
matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0],
matched = npdu_is_data_expecting_reply(&InputBuffer[0],
DataLength, SourceAddress, &pkt->buffer[0], pkt->length,
pkt->destination_mac);
if (matched) {
+2 -102
View File
@@ -196,106 +196,6 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS *src, uint8_t mstp_address)
}
}
static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(request_pdu, 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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = bacnet_npdu_decode(
reply_pdu, 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;
}
if (request.invoke_id != reply.invoke_id) {
return false;
}
/* these services 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.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;
}
/* MS/TP Frame Format */
/* All frames are of the following format: */
/* */
@@ -1092,7 +992,7 @@ static bool MSTP_Master_Node_FSM(void)
if (!timeout) {
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue);
if (pkt != NULL) {
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&InputBuffer[0], DataLength, SourceAddress,
&pkt->buffer[0], pkt->length, pkt->destination_mac);
} else {
@@ -1191,7 +1091,7 @@ static void MSTP_Slave_Node_FSM(void)
} else if (MSTP_Flag.ReceivePacketPending) {
if (!Ringbuf_Empty(&PDU_Queue)) {
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue);
matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0],
matched = npdu_is_data_expecting_reply(&InputBuffer[0],
DataLength, SourceAddress, &pkt->buffer[0], pkt->length,
pkt->destination_mac);
if (matched) {
+1 -141
View File
@@ -166,146 +166,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 request_pdu - PDU of the data
* @param request_pdu_len - number of bytes of PDU data
* @param src_address - source address of the request
* @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 dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* unused parameters */
(void)request_pdu_len;
(void)reply_pdu_len;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Request is Network message.\n");
return false;
}
request.pdu_type = request_pdu[offset] & 0xF0;
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
debug_printf("DLMSTP: DER Compare failed: "
"Not Confirmed Request.\n");
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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Reply is Network message.\n");
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) {
debug_printf("DLMSTP: DER Compare failed: "
"Invoke ID mismatch.\n");
return false;
}
} else {
if (request.invoke_id != reply.invoke_id) {
debug_printf("DLMSTP: DER Compare failed: "
"Invoke ID mismatch.\n");
return false;
}
if (request.service_choice != reply.service_choice) {
debug_printf("DLMSTP: DER Compare failed: "
"Service choice mismatch.\n");
return false;
}
}
if (request.npdu_data.protocol_version !=
reply.npdu_data.protocol_version) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Protocol Version mismatch.\n");
return false;
}
if (request.npdu_data.priority != reply.npdu_data.priority) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Priority mismatch.\n");
return false;
}
if (!bacnet_address_same(&request.address, &reply.address)) {
debug_printf("DLMSTP: DER Compare failed: "
"BACnet Address mismatch.\n");
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
@@ -326,7 +186,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
}
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue);
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0], pkt->length,
pkt->destination_mac);
+2 -132
View File
@@ -447,136 +447,6 @@ void MSTP_Send_Frame(
RS485_Send_Frame(mstp_port, buffer, nbytes);
}
static bool dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* unused parameters */
(void)request_pdu_len;
(void)reply_pdu_len;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Request is Network message.\n");
return false;
}
request.pdu_type = request_pdu[offset] & 0xF0;
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
debug_printf("DLMSTP: DER Compare failed: "
"Not Confirmed Request.\n");
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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Reply is Network message.\n");
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) {
debug_printf("DLMSTP: DER Compare failed: "
"Invoke ID mismatch.\n");
return false;
}
} else {
if (request.invoke_id != reply.invoke_id) {
debug_printf("DLMSTP: DER Compare failed: "
"Invoke ID mismatch.\n");
return false;
}
if (request.service_choice != reply.service_choice) {
debug_printf("DLMSTP: DER Compare failed: "
"Service choice mismatch.\n");
return false;
}
}
if (request.npdu_data.protocol_version !=
reply.npdu_data.protocol_version) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Protocol Version mismatch.\n");
return false;
}
if (request.npdu_data.priority != reply.npdu_data.priority) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Priority mismatch.\n");
return false;
}
if (!bacnet_address_same(&request.address, &reply.address)) {
debug_printf("DLMSTP: DER Compare failed: "
"BACnet Address mismatch.\n");
return false;
}
return true;
}
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
{ /* milliseconds to wait for a packet */
@@ -595,7 +465,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
}
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&poSharedData->PDU_Queue);
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0], pkt->length,
pkt->destination_mac);
@@ -604,7 +474,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
while (!matched &&
(pkt = (struct mstp_pdu_packet *)Ringbuf_Peek_Next(
&poSharedData->PDU_Queue, (uint8_t *)pkt)) != NULL) {
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0],
pkt->length, pkt->destination_mac);
+1 -135
View File
@@ -173,140 +173,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 request_pdu - PDU of the data
* @param request_pdu_len - number of bytes of PDU data
* @param src_address - source address of the request
* @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 dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* unused parameters */
(void)request_pdu_len;
(void)reply_pdu_len;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Request is Network message.\n");
return false;
}
request.pdu_type = request_pdu[offset] & 0xF0;
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
debug_printf("DLMSTP: DER Compare failed: "
"Not Confirmed Request.\n");
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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Reply is Network message.\n");
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 don't have service choice included */
if ((request.pdu_type != PDU_TYPE_REJECT) &&
(request.pdu_type != PDU_TYPE_ABORT) &&
(request.pdu_type != PDU_TYPE_SEGMENT_ACK)) {
if (request.service_choice != reply.service_choice) {
debug_printf("DLMSTP: DER Compare failed: "
"Service choice mismatch.\n");
return false;
}
}
if (request.npdu_data.protocol_version !=
reply.npdu_data.protocol_version) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Protocol Version mismatch.\n");
return false;
}
if (request.npdu_data.priority != reply.npdu_data.priority) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Priority mismatch.\n");
return false;
}
if (!bacnet_address_same(&request.address, &reply.address)) {
debug_printf("DLMSTP: DER Compare failed: "
"BACnet Address mismatch.\n");
return false;
}
return true;
}
/**
* Add a certain number of nanoseconds to the specified time.
*
@@ -374,7 +240,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek_Next(
&PDU_Queue, (uint8_t *)pkt)) {
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0], pkt->length,
pkt->destination_mac);
+2 -132
View File
@@ -391,136 +391,6 @@ void MSTP_Send_Frame(
RS485_Send_Frame(mstp_port, buffer, nbytes);
}
static bool dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* unused parameters */
(void)request_pdu_len;
(void)reply_pdu_len;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Request is Network message.\n");
return false;
}
request.pdu_type = request_pdu[offset] & 0xF0;
if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) {
debug_printf("DLMSTP: DER Compare failed: "
"Not Confirmed Request.\n");
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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = bacnet_npdu_decode(
reply_pdu, reply_pdu_len, &reply.address, NULL, &reply.npdu_data);
if (reply.npdu_data.network_layer_message) {
debug_printf("DLMSTP: DER Compare failed: "
"Reply is Network message.\n");
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) {
debug_printf("DLMSTP: DER Compare failed: "
"Invoke ID mismatch.\n");
return false;
}
} else {
if (request.invoke_id != reply.invoke_id) {
debug_printf("DLMSTP: DER Compare failed: "
"Invoke ID mismatch.\n");
return false;
}
if (request.service_choice != reply.service_choice) {
debug_printf("DLMSTP: DER Compare failed: "
"Service choice mismatch.\n");
return false;
}
}
if (request.npdu_data.protocol_version !=
reply.npdu_data.protocol_version) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Protocol Version mismatch.\n");
return false;
}
if (request.npdu_data.priority != reply.npdu_data.priority) {
debug_printf("DLMSTP: DER Compare failed: "
"NPDU Priority mismatch.\n");
return false;
}
if (!bacnet_address_same(&request.address, &reply.address)) {
debug_printf("DLMSTP: DER Compare failed: "
"BACnet Address mismatch.\n");
return false;
}
return true;
}
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
{ /* milliseconds to wait for a packet */
@@ -539,7 +409,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
}
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&poSharedData->PDU_Queue);
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0], pkt->length,
pkt->destination_mac);
@@ -548,7 +418,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
while (!matched &&
(pkt = (struct mstp_pdu_packet *)Ringbuf_Peek_Next(
&poSharedData->PDU_Queue, (uint8_t *)pkt)) != NULL) {
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0],
pkt->length, pkt->destination_mac);
+2 -112
View File
@@ -304,116 +304,6 @@ void MSTP_Send_Frame(
RS485_Send_Frame(mstp_port, buffer, nbytes);
}
bool dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
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;
/* unused parameters */
request_pdu_len = request_pdu_len;
reply_pdu_len = reply_pdu_len;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(
request_pdu, 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, 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;
}
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
{ /* milliseconds to wait for a packet */
@@ -435,10 +325,10 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
return 0;
}
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len, &Transmit_Packet.address);
Transmit_Packet.pdu_len, destination);
if (!matched) {
return 0;
}
+1 -123
View File
@@ -156,128 +156,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 request_pdu - PDU of the data
* @param request_pdu_len - number of bytes of PDU data
* @param src_address - source address of the request
* @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 dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
uint16_t reply_pdu_len,
uint8_t 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;
/* unused parameters */
(void)request_pdu_len;
(void)reply_pdu_len;
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(
request_pdu, 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 */
reply.address.mac[0] = dest_address;
reply.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(
reply_pdu, 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
@@ -298,7 +176,7 @@ uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
}
pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue);
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(
matched = npdu_is_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0], pkt->length,
pkt->destination_mac);
+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
}
+1
View File
@@ -31,6 +31,7 @@ add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/basic/bbmd/h_bbmd.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacstr.c
+1
View File
@@ -32,6 +32,7 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/basic/bbmd6/h_bbmd6.c
${SRC_DIR}/bacnet/basic/bbmd6/vmac.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacstr.c
+1
View File
@@ -42,6 +42,7 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/basic/sys/fifo.c
${SRC_DIR}/bacnet/indtext.c
# core files needed
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacdcode.c
${SRC_DIR}/bacnet/bacint.c
${SRC_DIR}/bacnet/bacstr.c
+5
View File
@@ -34,6 +34,7 @@ add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/npdu.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/abort.c
${SRC_DIR}/bacnet/bacaction.c
${SRC_DIR}/bacnet/bacaddr.c
${SRC_DIR}/bacnet/bacdcode.c
@@ -46,6 +47,10 @@ add_executable(${PROJECT_NAME}
${SRC_DIR}/bacnet/basic/sys/debug.c
${SRC_DIR}/bacnet/basic/tsm/tsm.c
${SRC_DIR}/bacnet/dcc.c
${SRC_DIR}/bacnet/proplist.c
${SRC_DIR}/bacnet/reject.c
${SRC_DIR}/bacnet/rp.c
${SRC_DIR}/bacnet/whois.c
./stubs.c
# Test and test library files
./src/main.c
+289 -3
View File
@@ -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);
}