Feature/what is network number handling (#304)

* Add What-Is-Network-Number handling.

Add What-Is-Network-Number and Network-Number-Is network layer handling.
Refactor npdu_encode_npdu_network() from router specific code.
Add unit test for NDPU network message
Add app for What-Is-Network-Number
Add app for Network-Number-Is
Add send helper for What-Is-Network-Number
Add send helper for Network-Number-Is

* added sys/debug.c to ports builds for use of debug_printf() in npdu handler.

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2022-07-14 15:34:45 -05:00
committed by GitHub
parent 38d213b47c
commit 1f41341c09
28 changed files with 1080 additions and 46 deletions
+173 -7
View File
@@ -24,6 +24,7 @@
*********************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include "bacnet/bacaddr.h"
#include "bacnet/bacdef.h"
#include "bacnet/bacdcode.h"
#include "bacnet/bacint.h"
@@ -32,13 +33,176 @@
#include "bacnet/npdu.h"
#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
#include <stdio.h>
#endif
static uint16_t Local_Network_Number;
static uint8_t Local_Network_Number_Status = NETWORK_NUMBER_LEARNED;
/**
* @brief get the local network number
* @return local network number
*/
uint16_t npdu_network_number(void)
{
return Local_Network_Number;
}
/**
* @brief set the local network number
* @param net - local network number
*/
void npdu_network_number_set(uint16_t net)
{
Local_Network_Number = net;
}
/**
* @brief send the local network number is message
* @param dst - the destination address for the message
* @param net - local network number
* @param status - 0=learned, 1=assigned
* @return number of bytes sent
*/
int npdu_send_network_number_is(BACNET_ADDRESS *dst,
uint16_t net, uint8_t status)
{
uint16_t len = 0;
int pdu_len = 0;
int bytes_sent = 0;
bool data_expecting_reply = false;
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS my_address;
/* Upon receipt of a What-Is-Network-Number message,
a device that knows the local network number shall
transmit a local broadcast Network-Number-Is message
back to the source device. */
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);
return bytes_sent;
}
/**
* @brief send the local network number is message
* @param dst - the destination address for the message
* @param net - local network number
* @return number of bytes sent
*/
int npdu_send_what_is_network_number(BACNET_ADDRESS *dst)
{
int pdu_len = 0;
bool data_expecting_reply = false;
BACNET_NPDU_DATA npdu_data;
BACNET_ADDRESS daddr = { 0 };
BACNET_ADDRESS saddr = { 0 };
if (dst) {
bacnet_address_copy(&daddr, dst);
} else {
datalink_get_broadcast_address(&daddr);
}
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);
/* Now send the message */
return datalink_send_pdu(
dst, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len);
}
/** @file h_npdu.c Handles messages at the NPDU level of the BACnet stack. */
/** Handler to manage the Network Layer Control Messages received in a packet.
* This handler is called if the NCPI bit 7 indicates that this packet is a
* network layer message and there is no further DNET to pass it to.
* The NCPI has already been decoded into the npdu_data structure.
* @ingroup MISCHNDLR
*
* @param src [in] The routing source information, if any.
* If src->net and src->len are 0, there is no
* routing source information.
* @param npdu_data [in] Contains a filled-out structure with information
* decoded from the NCPI and other NPDU
* bytes.
* @param npdu [in] Buffer containing the rest of the NPDU, following the
* bytes that have already been decoded.
* @param npdu_len [in] The length of the remaining NPDU message in npdu[].
*/
static void network_control_handler(BACNET_ADDRESS *src,
BACNET_NPDU_DATA *npdu_data,
uint8_t *npdu,
uint16_t npdu_len)
{
uint16_t dnet = 0;
uint16_t len = 0;
uint8_t status = 0;
switch (npdu_data->network_message_type) {
case NETWORK_MESSAGE_WHAT_IS_NETWORK_NUMBER:
if (src->net == 0) {
if (Local_Network_Number) {
npdu_send_network_number_is(src, Local_Network_Number,
Local_Network_Number_Status);
} else {
/* Upon receipt of a What-Is-Network-Number message,
a device that does not know the local network number
shall discard the message. */
}
} else {
/* Devices shall ignore What-Is-Network-Number messages
that contain SNET/SADR or DNET/DADR information in
the NPCI. */
}
break;
case NETWORK_MESSAGE_NETWORK_NUMBER_IS:
if (src->net == 0) {
/* It shall be transmitted with a local broadcast address,
and shall never be routed. */
if (npdu_len >= 2) {
(void)decode_unsigned16(npdu, &dnet);
Local_Network_Number = dnet;
}
if (npdu_len >= 3) {
status = npdu[2];
/* Our net number is always learned, unless we
are a router. Ignore the learned/configured
status */
(void)status;
}
} else {
/* Devices shall ignore Network-Number-Is messages that
contain SNET/SADR or DNET/DADR information In the NPCI or
that are sent with a local unicast address. */
}
break;
default:
break;
}
}
/** Handler for the NPDU portion of a received packet.
* Aside from error-checking, if the NPDU doesn't contain routing info,
* this handler doesn't do much besides stepping over the NPDU header
@@ -62,10 +226,10 @@
* @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.
*/
void npdu_handler(BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
void npdu_handler(BACNET_ADDRESS *src,
uint8_t *pdu,
uint16_t pdu_len)
{ /* length PDU */
{
int apdu_offset = 0;
BACNET_ADDRESS dest = { 0 };
BACNET_NPDU_DATA npdu_data = { 0 };
@@ -79,10 +243,12 @@ void npdu_handler(BACNET_ADDRESS *src, /* source address */
apdu_offset =
bacnet_npdu_decode(&pdu[0], pdu_len, &dest, src, &npdu_data);
if (npdu_data.network_layer_message) {
/*FIXME: network layer message received! Handle it! */
#if PRINT_ENABLED
fprintf(stderr, "NPDU: Network Layer Message discarded!\n");
#endif
if ((dest.net == 0) || (dest.net == BACNET_BROADCAST_NETWORK)) {
network_control_handler(src, &npdu_data,
&pdu[apdu_offset], (uint16_t)(pdu_len - apdu_offset));
} else {
debug_printf("NPDU: message for router. Discarded!\n");
}
} else if ((apdu_offset > 0) && (apdu_offset < pdu_len)) {
if ((dest.net == 0) || (dest.net == BACNET_BROADCAST_NETWORK)) {
/* only handle the version that we know how to handle */
+14
View File
@@ -47,6 +47,20 @@ extern "C" {
BACNET_ADDRESS * src,
uint8_t * pdu,
uint16_t pdu_len);
BACNET_STACK_EXPORT
uint16_t npdu_network_number(void);
BACNET_STACK_EXPORT
void npdu_network_number_set(uint16_t net);
BACNET_STACK_EXPORT
int npdu_send_network_number_is(
BACNET_ADDRESS *dst,
uint16_t net,
uint8_t status);
BACNET_STACK_EXPORT
int npdu_send_what_is_network_number(
BACNET_ADDRESS *dst);
BACNET_STACK_EXPORT
void npdu_handler_cleanup(void);
BACNET_STACK_EXPORT
+6
View File
@@ -161,6 +161,12 @@ static void network_control_handler(BACNET_ADDRESS *src,
case NETWORK_MESSAGE_DISCONNECT_CONNECTION_TO_NETWORK:
/* Do nothing - don't support PTP half-router control */
break;
case NETWORK_MESSAGE_WHAT_IS_NETWORK_NUMBER:
/* FIXME: add procedure for this message */
break;
case NETWORK_MESSAGE_NETWORK_NUMBER_IS:
/* FIXME: add procedure for this message */
break;
default:
/* An unrecognized message is bad; send an error response. */
Send_Reject_Message_To_Network(
+33 -30
View File
@@ -29,6 +29,7 @@
#include "bacnet/config.h"
#include "bacnet/bacdef.h"
#include "bacnet/bacdcode.h"
#include "bacnet/bacaddr.h"
#include "bacnet/npdu.h"
#include "bacnet/apdu.h"
#include "bacnet/bactext.h"
@@ -43,36 +44,6 @@
/** @file s_router.c Methods to send various BACnet Router Network Layer
* Messages. */
/** Initialize an npdu_data structure with given parameters and good defaults,
* and add the Network Layer Message fields.
* The name is a misnomer, as it doesn't do any actual encoding here.
* @see npdu_encode_npdu_data for a simpler version to use when sending an
* APDU instead of a Network Layer Message.
*
* @param npdu_data [out] Returns a filled-out structure with information
* provided by the other arguments and
* good defaults.
* @param network_message_type [in] The type of Network Layer Message.
* @param data_expecting_reply [in] True if message should have a reply.
* @param priority [in] One of the 4 priorities defined in section 6.2.2,
* like B'11' = Life Safety message
*/
void npdu_encode_npdu_network(BACNET_NPDU_DATA *npdu_data,
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
bool data_expecting_reply,
BACNET_MESSAGE_PRIORITY priority)
{
if (npdu_data) {
npdu_data->data_expecting_reply = data_expecting_reply;
npdu_data->protocol_version = BACNET_PROTOCOL_VERSION;
npdu_data->network_layer_message = true; /* false if APDU */
npdu_data->network_message_type = network_message_type; /* optional */
npdu_data->vendor_id = 0; /* optional, if net message type is > 0x80 */
npdu_data->priority = priority;
npdu_data->hop_count = HOP_COUNT_DEFAULT;
}
}
/** Function to encode and send any supported Network Layer Message.
* The payload for the message is encoded from information in the iArgs[] array.
* The contents of iArgs are are, per message type:
@@ -150,6 +121,18 @@ int Send_Network_Layer_Message(BACNET_NETWORK_MESSAGE_TYPE network_message_type,
}
break;
case NETWORK_MESSAGE_NETWORK_NUMBER_IS:
/* Encode the DNET */
len = encode_unsigned16(
&Handler_Transmit_Buffer[pdu_len], (uint16_t)*pVal);
pdu_len += len;
pVal++;
/* Encode the Status byte */
Handler_Transmit_Buffer[pdu_len] = (uint8_t)*pVal;
pdu_len++;
pVal++;
break;
case NETWORK_MESSAGE_REJECT_MESSAGE_TO_NETWORK:
/* Encode the Reason byte, then the DNET */
Handler_Transmit_Buffer[pdu_len++] = (uint8_t)*pVal;
@@ -317,3 +300,23 @@ void Send_Initialize_Routing_Table_Ack(
Send_Network_Layer_Message(
NETWORK_MESSAGE_INIT_RT_TABLE_ACK, dst, (int *)DNET_list);
}
/**
* @brief Sets a BACnet network number for the local network
*
* @param dst [in] If NULL, request will be broadcast to the local BACnet
* network. Optionally may designate a particular router
* destination to respond.
* @param dnet [in] Which BACnet network to request for; if -1, no DNET
* will be sent and the receiving router(s) will send
* their full list of reachable BACnet networks.
*/
void Send_Network_Number_Is(BACNET_ADDRESS *dst, int dnet, int status)
{
int iArgs[2];
iArgs[0] = dnet;
iArgs[1] = status;
Send_Network_Layer_Message(
NETWORK_MESSAGE_NETWORK_NUMBER_IS, dst, iArgs);
}
+5 -7
View File
@@ -43,12 +43,6 @@ extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
void npdu_encode_npdu_network(
BACNET_NPDU_DATA *npdu_data,
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
bool data_expecting_reply,
BACNET_MESSAGE_PRIORITY priority);
BACNET_STACK_EXPORT
int Send_Network_Layer_Message(
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
BACNET_ADDRESS * dst,
@@ -73,7 +67,11 @@ extern "C" {
void Send_Initialize_Routing_Table_Ack(
BACNET_ADDRESS * dst,
const int DNET_list[]);
BACNET_STACK_EXPORT
void Send_Network_Number_Is(
BACNET_ADDRESS *dst,
int dnet,
int status);
#ifdef __cplusplus
}
+30
View File
@@ -286,6 +286,36 @@ void npdu_encode_npdu_data(BACNET_NPDU_DATA *npdu_data,
}
}
/** Initialize an npdu_data structure with given parameters and good defaults,
* and add the Network Layer Message fields.
* The name is a misnomer, as it doesn't do any actual encoding here.
* @see npdu_encode_npdu_data for a simpler version to use when sending an
* APDU instead of a Network Layer Message.
*
* @param npdu_data [out] Returns a filled-out structure with information
* provided by the other arguments and
* good defaults.
* @param network_message_type [in] The type of Network Layer Message.
* @param data_expecting_reply [in] True if message should have a reply.
* @param priority [in] One of the 4 priorities defined in section 6.2.2,
* like B'11' = Life Safety message
*/
void npdu_encode_npdu_network(BACNET_NPDU_DATA *npdu_data,
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
bool data_expecting_reply,
BACNET_MESSAGE_PRIORITY priority)
{
if (npdu_data) {
npdu_data->data_expecting_reply = data_expecting_reply;
npdu_data->protocol_version = BACNET_PROTOCOL_VERSION;
npdu_data->network_layer_message = true; /* false if APDU */
npdu_data->network_message_type = network_message_type; /* optional */
npdu_data->vendor_id = 0; /* optional, if net message type is > 0x80 */
npdu_data->priority = priority;
npdu_data->hop_count = HOP_COUNT_DEFAULT;
}
}
/** Decode the NPDU portion of a received message, particularly the NCPI byte.
* The Network Layer Protocol Control Information byte is described
* in section 6.2.2 of the BACnet standard.
+10
View File
@@ -61,6 +61,9 @@ typedef struct router_port_t {
struct router_port_t *next; /**< Point to next in linked list */
} BACNET_ROUTER_PORT;
#define NETWORK_NUMBER_LEARNED 0
#define NETWORK_NUMBER_CONFIGURED 1
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -83,6 +86,13 @@ extern "C" {
bool data_expecting_reply,
BACNET_MESSAGE_PRIORITY priority);
BACNET_STACK_EXPORT
void npdu_encode_npdu_network(
BACNET_NPDU_DATA *npdu_data,
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
bool data_expecting_reply,
BACNET_MESSAGE_PRIORITY priority);
BACNET_STACK_EXPORT
void npdu_copy_data(
BACNET_NPDU_DATA * dest,