Implemented separate Who-Is and Who-Has handlers for the routed case, where we loop through all the Devices looking for matches.

Tested and improved the routing code.
Gateway demo now sends I-Am as each Device is added.
Added test for valid Network number when routing, and only send Reject message if this fails (and not if we just can't find a Device match).
Test the hop_count and discard the packet if would reach 0 and a destination Network is called for.
This commit is contained in:
tbrennan3
2010-12-02 20:33:43 +00:00
parent e850469da8
commit 31f440e070
8 changed files with 283 additions and 76 deletions
+15 -8
View File
@@ -157,9 +157,13 @@ static void Init_Service_Handlers(
{
Routing_Device_Init( first_object_instance );
/* we need to handle who-is to support dynamic device binding */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has);
/* we need to handle who-is to support dynamic device binding
* For the gateway, we want the routing handlers, and we will use the
* unicast variety so we can get back through switches to different subnets */
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS,
handler_who_is_unicast_for_routing);
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS,
handler_who_has_for_routing);
/* set the handler for all the services we don't implement */
/* It is required to send the proper reject message... */
apdu_set_unrecognized_service_handler_handler
@@ -225,7 +229,9 @@ void Initialize_Device_Addresses( )
#else
#error "No support for this Data Link Layer type "
#endif
/* broadcast an I-Am on startup */
Send_I_Am(&Handler_Transmit_Buffer[0]);
for (i = 1; i < MAX_NUM_DEVICES; i++ ) {
pDev = Get_Routed_Device_Object( i );
if ( pDev == NULL )
@@ -241,12 +247,15 @@ void Initialize_Device_Addresses( )
pDev->bacDevAddr.net = VIRTUAL_DNET;
memcpy( &pDev->bacDevAddr.adr[0], &pDev->bacDevAddr.mac[0], 6 );
pDev->bacDevAddr.len = 6;
printf( " - Routed device %d at %s \n", i, inet_ntoa( *netPtr ) );
printf( " - Routed device [%d] ID %u at %s \n", i,
pDev->bacObj.Object_Instance_Number, inet_ntoa( *netPtr ) );
#elif defined(BACDL_MSTP)
/* Todo: set MS/TP net and port #s */
pDev->bacDevAddr.mac_len = 2;
#endif
/* broadcast an I-Am for each routed Device now */
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
}
@@ -318,8 +327,6 @@ int main(
/* configure the timeout values */
last_seconds = time(NULL);
/* broadcast an I-Am on startup */
Send_I_Am(&Handler_Transmit_Buffer[0]);
/* broadcast an I-am-router-to-network on startup */
printf( "Remote Network DNET Number %d \n", DNET_list[0] );
+11 -4
View File
@@ -200,6 +200,12 @@ static void routed_apdu_handler(
int cursor = 0; /* Starting hint */
bool bGotOne = false;
if (!Routed_Device_Is_Valid_Network( dest->net, DNET_list ) ) {
/* We don't know how to reach this one */
Send_Reject_Message_To_Network( src, NETWORK_REJECT_NO_ROUTE, dest->net );
return;
}
while ( Routed_Device_GetNext( dest, DNET_list, &cursor ) ) {
apdu_handler(src, apdu, apdu_len);
bGotOne = true;
@@ -207,8 +213,7 @@ static void routed_apdu_handler(
break; /* We don't need to keep looking */
}
if ( !bGotOne ) {
/* We don't know how to reach this one */
Send_Reject_Message_To_Network( src, NETWORK_REJECT_NO_ROUTE, dest->net );
/* Just silently drop this packet. */
}
}
@@ -267,8 +272,10 @@ void routing_npdu_handler(
* since only routers can handle it (even if for our DNET) */
}
} else if (apdu_offset <= pdu_len) {
routed_apdu_handler(src, &dest, DNET_list, &pdu[apdu_offset],
(uint16_t) (pdu_len - apdu_offset));
if ( (dest.net == 0) || (npdu_data.hop_count > 1) )
routed_apdu_handler(src, &dest, DNET_list, &pdu[apdu_offset],
(uint16_t) (pdu_len - apdu_offset));
/* Else, hop_count bottomed out and we discard this one. */
}
} else {
/* Should we send NETWORK_MESSAGE_REJECT_MESSAGE_TO_NETWORK? */
+79 -34
View File
@@ -38,6 +38,42 @@
/** @file h_whohas.c Handles Who-Has requests. */
/** Local function which responds with either the requested object name
* or object ID, if the Device has a match.
* @param data [in] The decoded who-has payload from the request.
*/
static void match_name_or_object(
BACNET_WHO_HAS_DATA * data)
{
char *object_name = NULL;
int object_type = 0;
uint32_t object_instance = 0;
bool found = false;
/* do we have such an object? If so, send an I-Have.
note: we should have only 1 of such an object */
if (data->object_name) {
/* valid name in my device? */
object_name = characterstring_value(&data->object.name);
found =
Device_Valid_Object_Name(object_name, &object_type,
&object_instance);
if (found)
Send_I_Have(Device_Object_Instance_Number(),
(BACNET_OBJECT_TYPE) object_type, object_instance,
object_name);
} else {
/* valid object in my device? */
object_name =
Device_Valid_Object_Id(data->object.identifier.type,
data->object.identifier.instance);
if (object_name)
Send_I_Have(Device_Object_Instance_Number(),
(BACNET_OBJECT_TYPE) data->object.identifier.type,
data->object.identifier.instance, object_name);
}
}
/** Handler for Who-Has requests, with broadcast I-Have response.
* Will respond if the device Object ID matches, and we have
* the Object or Object Name requested.
@@ -55,49 +91,58 @@ void handler_who_has(
int len = 0;
BACNET_WHO_HAS_DATA data;
bool directed_to_me = false;
int object_type = 0;
uint32_t object_instance = 0;
char *object_name = NULL;
bool found = false;
(void) src;
len = whohas_decode_service_request(service_request, service_len, &data);
if (len > 0) {
if ((data.low_limit == -1) || (data.high_limit == -1))
directed_to_me = true;
#ifdef BAC_ROUTING
else if ((Routed_Device_Object_Instance_Number() >= (uint32_t) data.low_limit)
&& (Routed_Device_Object_Instance_Number() <= (uint32_t) data.high_limit))
#else
else if ((Device_Object_Instance_Number() >= (uint32_t) data.low_limit)
&& (Device_Object_Instance_Number() <= (uint32_t) data.high_limit))
#endif
&& (Device_Object_Instance_Number() <= (uint32_t) data.high_limit))
directed_to_me = true;
if (directed_to_me) {
/* do we have such an object? If so, send an I-Have.
note: we should have only 1 of such an object */
if (data.object_name) {
/* valid name in my device? */
object_name = characterstring_value(&data.object.name);
found =
Device_Valid_Object_Name(object_name, &object_type,
&object_instance);
if (found)
Send_I_Have(Device_Object_Instance_Number(),
(BACNET_OBJECT_TYPE) object_type, object_instance,
object_name);
} else {
/* valid object in my device? */
object_name =
Device_Valid_Object_Id(data.object.identifier.type,
data.object.identifier.instance);
if (object_name)
Send_I_Have(Device_Object_Instance_Number(),
(BACNET_OBJECT_TYPE) data.object.identifier.type,
data.object.identifier.instance, object_name);
}
match_name_or_object(&data);
}
}
return;
}
#ifdef BAC_ROUTING
/** Handler for Who-Has requests in the virtual routing setup,
* with broadcast I-Have response.
* Will respond if the device Object ID matches, and we have
* the Object or Object Name requested.
*
* @ingroup DMDOB
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source (ignored).
*/
void handler_who_has_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src)
{
int len = 0;
BACNET_WHO_HAS_DATA data;
int32_t dev_instance;
int cursor = 0; /* Starting hint */
int my_list[2] = {0, -1}; /* Not really used, so dummy values */
BACNET_ADDRESS bcast_net;
(void) src;
len = whohas_decode_service_request(service_request, service_len, &data);
if (len > 0) {
/* Go through all devices, starting with the root gateway Device */
memset( &bcast_net, 0, sizeof(BACNET_ADDRESS));
bcast_net.net = BACNET_BROADCAST_NETWORK; /* That's all we have to set */
while ( Routed_Device_GetNext( &bcast_net, my_list, &cursor ) ) {
dev_instance = Device_Object_Instance_Number();
if ( (data.low_limit == -1) || (data.high_limit == -1) ||
((dev_instance >= data.low_limit) &&
(dev_instance <= data.high_limit)))
match_name_or_object(&data);
}
}
}
#endif /* BAC_ROUTING */
+100 -21
View File
@@ -44,7 +44,7 @@
* @ingroup DMDDB
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source.
* @param src [in] The BACNET_ADDRESS of the message's source (ignored).
*/
void handler_who_is(
uint8_t * service_request,
@@ -63,21 +63,12 @@ void handler_who_is(
Send_I_Am(&Handler_Transmit_Buffer[0]);
else if (len != -1) {
/* is my device id within the limits? */
#ifdef BAC_ROUTING
if (((Routed_Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Routed_Device_Object_Instance_Number() <= (uint32_t) high_limit))
||
/* BACnet wildcard is the max instance number - everyone responds */
((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
(BACNET_MAX_INSTANCE <= (uint32_t) high_limit)))
#else
if (((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t) high_limit))
||
/* BACnet wildcard is the max instance number - everyone responds */
((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
(BACNET_MAX_INSTANCE <= (uint32_t) high_limit)))
#endif
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
@@ -88,7 +79,8 @@ void handler_who_is(
* @ingroup DMDDB
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source.
* @param src [in] The BACNET_ADDRESS of the message's source that the
* response will be sent back to.
*/
void handler_who_is_unicast(
uint8_t * service_request,
@@ -99,7 +91,6 @@ void handler_who_is_unicast(
int32_t low_limit = 0;
int32_t high_limit = 0;
(void) src;
len =
whois_decode_service_request(service_request, service_len, &low_limit,
&high_limit);
@@ -107,23 +98,111 @@ void handler_who_is_unicast(
Send_I_Am_Unicast(&Handler_Transmit_Buffer[0], src);
else if (len != -1) {
/* is my device id within the limits? */
#ifdef BAC_ROUTING
if (((Routed_Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Routed_Device_Object_Instance_Number() <= (uint32_t) high_limit))
||
/* BACnet wildcard is the max instance number - everyone responds */
((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
(BACNET_MAX_INSTANCE <= (uint32_t) high_limit)))
#else
if (((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t) high_limit))
||
/* BACnet wildcard is the max instance number - everyone responds */
((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
(BACNET_MAX_INSTANCE <= (uint32_t) high_limit)))
#endif
Send_I_Am_Unicast(&Handler_Transmit_Buffer[0], src);
}
return;
}
#ifdef BAC_ROUTING
/** Local function to check Who-Is requests against our Device IDs.
* Will check the gateway (root Device) and all virtual routed
* Devices against the range and respond for each that matches.
*
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source.
* @param is_unicast [in] True if should send unicast response(s)
* back to the src, else False if should broadcast response(s).
*/
static void check_who_is_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src,
bool is_unicast )
{
int len = 0;
int32_t low_limit = 0;
int32_t high_limit = 0;
int32_t dev_instance;
int cursor = 0; /* Starting hint */
int my_list[2] = {0, -1}; /* Not really used, so dummy values */
BACNET_ADDRESS bcast_net;
len =
whois_decode_service_request(service_request, service_len, &low_limit,
&high_limit);
if (len == -1) {
/* Invalid; just leave */
return;
}
/* If len == 0, then high_limit is untouched and still == 0 */
/* BACnet wildcard is the max instance number - everyone responds */
if ((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
(BACNET_MAX_INSTANCE <= (uint32_t) high_limit))
high_limit = 0;
/* This is the "always accept" case we will test for below */
/* Go through all devices, starting with the root gateway Device */
memset( &bcast_net, 0, sizeof(BACNET_ADDRESS));
bcast_net.net = BACNET_BROADCAST_NETWORK; /* That's all we have to set */
while ( Routed_Device_GetNext( &bcast_net, my_list, &cursor ) ) {
dev_instance = Device_Object_Instance_Number();
if ( (high_limit == 0) ||
((dev_instance >= low_limit) && (dev_instance <= high_limit))) {
if ( is_unicast )
Send_I_Am_Unicast(&Handler_Transmit_Buffer[0], src);
else
Send_I_Am(&Handler_Transmit_Buffer[0]);
}
}
}
/** Handler for Who-Is requests in the virtual routing setup,
* with broadcast I-Am response(s).
* Will check the gateway (root Device) and all virtual routed
* Devices against the range and respond for each that matches.
*
* @ingroup DMDDB
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source (ignored).
*/
void handler_who_is_bcast_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src)
{
check_who_is_for_routing(service_request, service_len, src, false );
}
/** Handler for Who-Is requests in the virtual routing setup,
* with unicast I-Am response(s) returned to the src.
* Will check the gateway (root Device) and all virtual routed
* Devices against the range and respond for each that matches.
*
* @ingroup DMDDB
* @param service_request [in] The received message to be handled.
* @param service_len [in] Length of the service_request message.
* @param src [in] The BACNET_ADDRESS of the message's source that the
* response will be sent back to.
*/
void handler_who_is_unicast_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src)
{
check_who_is_for_routing(service_request, service_len, src, true );
}
#endif /* BAC_ROUTING */
+1 -1
View File
@@ -53,7 +53,7 @@ object_functions_t Object_Table[] = {
};
/* buffer used for receive */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
/* static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; */
/* global variables used in this file */
#define MAX_ROUTER_DNETS 64
+58 -7
View File
@@ -142,6 +142,7 @@ uint16_t Add_Routed_Device(
* 0 is for the main, gateway Device entry.
* -1 is a special case meaning "whichever iCurrent_Device_Idx
* is currently set to"
* If valid idx, will set iCurrent_Device_Idx with the idx
* @return Pointer to the requested Device Object data, or NULL if the idx
* is for an invalid row entry (eg, after the last good Device).
*/
@@ -150,8 +151,10 @@ DEVICE_OBJECT_DATA * Get_Routed_Device_Object(
{
if ( idx == -1 )
return &Devices[iCurrent_Device_Idx];
else if ( (idx >= 0) && (idx < MAX_NUM_DEVICES) )
else if ( (idx >= 0) && (idx < MAX_NUM_DEVICES) ) {
iCurrent_Device_Idx = idx;
return &Devices[idx];
}
else
return NULL;
}
@@ -161,6 +164,7 @@ DEVICE_OBJECT_DATA * Get_Routed_Device_Object(
* 0 is for the main, gateway Device entry.
* -1 is a special case meaning "whichever iCurrent_Device_Idx
* is currently set to"
* If valid idx, will set iCurrent_Device_Idx with the idx
* @return Pointer to the requested Device Object BACnet address, or NULL if the idx
* is for an invalid row entry (eg, after the last good Device).
*/
@@ -169,8 +173,10 @@ BACNET_ADDRESS * Get_Routed_Device_Address(
{
if ( idx == -1 )
return &Devices[iCurrent_Device_Idx].bacDevAddr;
else if ( (idx >= 0) && (idx < MAX_NUM_DEVICES) )
else if ( (idx >= 0) && (idx < MAX_NUM_DEVICES) ) {
iCurrent_Device_Idx = idx;
return &Devices[idx].bacDevAddr;
}
else
return NULL;
}
@@ -188,7 +194,10 @@ void routed_get_my_address(
BACNET_ADDRESS * my_address)
{
my_address = &Devices[iCurrent_Device_Idx].bacDevAddr;
if (my_address) {
memcpy(my_address, &Devices[iCurrent_Device_Idx].bacDevAddr,
sizeof( BACNET_ADDRESS ));
}
}
@@ -259,8 +268,8 @@ bool Routed_Device_Address_Lookup(
* Otherwise, its returned value is implementation-dependent and the
* calling function should not alter or interpret it.
*
* @return True if the MAC addresses match (or the address_len is 0,
* meaning MAC broadcast, so it's an automatic match).
* @return True if the MAC addresses match (or if BACNET_BROADCAST_NETWORK and
* the dest->len is 0, meaning MAC bcast, so it's an automatic match).
* Else False if no match or invalid idx is given; the cursor will
* be returned as -1 in these cases.
*/
@@ -273,10 +282,16 @@ bool Routed_Device_GetNext(
int idx = *cursor;
bool bSuccess = false;
/* First, see if it's a BACnet broadcast.
/* First, see if the index is out of range.
* Eg, last call to GetNext may have been the last successful one.
*/
if ( (idx < 0) || (idx >= MAX_NUM_DEVICES) )
idx = -1;
/* Next, see if it's a BACnet broadcast.
* For broadcasts, all Devices get a chance at it.
*/
if (dest->net == BACNET_BROADCAST_NETWORK) {
else if (dest->net == BACNET_BROADCAST_NETWORK) {
/* Just take the entry indexed by the cursor */
bSuccess = Routed_Device_Address_Lookup( idx++,
dest->len, dest->adr );
@@ -319,6 +334,42 @@ bool Routed_Device_GetNext(
}
/** Check if the destination network is reachable - is it our virtual network,
* or local or else broadcast.
*
* @param dest_net [in] The BACnet network number of a message's destination.
* Success if it is our virtual network number, or 0 (local for the
* gateway, or 0xFFFF for a broadcast network number.
* @param DNET_list [in] List of our reachable downstream BACnet Network numbers.
* Normally just one valid entry; terminated with a -1 value.
* @return True if matches our virtual network, or is for the local network
* Device (the gateway), or is BACNET_BROADCAST_NETWORK, which is
* an automatic match.
* Else False if not a reachable network.
*/
bool Routed_Device_Is_Valid_Network(
uint16_t dest_net,
int * DNET_list )
{
int dnet = DNET_list[0]; /* Get the DNET of our virtual network */
bool bSuccess = false;
/* First, see if it's a BACnet broadcast (automatic pass). */
if ( dest_net == BACNET_BROADCAST_NETWORK)
bSuccess = true;
/* Or see if it's for the main Gateway Device, because
* there's no routing info.
*/
else if (dest_net == 0)
bSuccess = true;
/* Or see if matches our virtual DNET */
else if (dest_net == dnet)
bSuccess = true;
return bSuccess;
}
/* methods to override the normal Device objection functions */
uint32_t Routed_Device_Index_To_Instance(
+4 -1
View File
@@ -331,7 +331,10 @@ extern "C" {
BACNET_ADDRESS * dest,
int * DNET_list,
int * cursor );
bool Routed_Device_Is_Valid_Network(
uint16_t dest_net,
int * DNET_list );
uint32_t Routed_Device_Index_To_Instance(
unsigned index);
bool Routed_Device_Valid_Object_Instance_Number(
+15
View File
@@ -70,11 +70,26 @@ extern "C" {
uint16_t service_len,
BACNET_ADDRESS * src);
void handler_who_is_bcast_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src);
void handler_who_is_unicast_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src);
void handler_who_has(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src);
void handler_who_has_for_routing(
uint8_t * service_request,
uint16_t service_len,
BACNET_ADDRESS * src);
void handler_i_am_add(
uint8_t * service_request,
uint16_t service_len,