From 5df087c88c0984c584705d029acd0fa5969552e4 Mon Sep 17 00:00:00 2001 From: skarg Date: Sun, 14 Sep 2008 16:12:59 +0000 Subject: [PATCH] Simplified the WhoIsRouterToNetwork and InitializeRouter demo. --- bacnet-stack/demo/initrouter/Makefile | 2 +- bacnet-stack/demo/initrouter/h_npdu.c | 88 -------------------------- bacnet-stack/demo/initrouter/main.c | 44 +++++++++++-- bacnet-stack/demo/whoisrouter/Makefile | 2 +- bacnet-stack/demo/whoisrouter/h_npdu.c | 88 -------------------------- bacnet-stack/demo/whoisrouter/main.c | 43 +++++++++++-- 6 files changed, 81 insertions(+), 186 deletions(-) delete mode 100644 bacnet-stack/demo/initrouter/h_npdu.c delete mode 100644 bacnet-stack/demo/whoisrouter/h_npdu.c diff --git a/bacnet-stack/demo/initrouter/Makefile b/bacnet-stack/demo/initrouter/Makefile index 59a0d051..d5ae87df 100644 --- a/bacnet-stack/demo/initrouter/Makefile +++ b/bacnet-stack/demo/initrouter/Makefile @@ -38,7 +38,7 @@ OPTIMIZATION = -O0 CFLAGS = -Wall $(DEBUGGING) $(OPTIMIZATION) $(INCLUDES) $(DEFINES) LFLAGS = -Wl,-Map=$(TARGET).map,$(LIBRARIES) -SRCS = main.c h_npdu.c +SRCS = main.c OBJS = ${SRCS:.c=.o} diff --git a/bacnet-stack/demo/initrouter/h_npdu.c b/bacnet-stack/demo/initrouter/h_npdu.c deleted file mode 100644 index b2e8162f..00000000 --- a/bacnet-stack/demo/initrouter/h_npdu.c +++ /dev/null @@ -1,88 +0,0 @@ -/*####COPYRIGHTBEGIN#### - ------------------------------------------- - Copyright (C) 2008 Steve Karg - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to: - The Free Software Foundation, Inc. - 59 Temple Place - Suite 330 - Boston, MA 02111-1307, USA. - - As a special exception, if other files instantiate templates or - use macros or inline functions from this file, or you compile - this file and link it with other works to produce a work based - on this file, this file does not by itself cause the resulting - work to be covered by the GNU General Public License. However - the source code for this file must still be made available in - accordance with section (3) of the GNU General Public License. - - This exception does not invalidate any other reasons why a work - based on this file might be covered by the GNU General Public - License. - ------------------------------------------- -####COPYRIGHTEND####*/ -#include -#include -#include "bacdef.h" -#include "bacdcode.h" -#include "bacint.h" -#include "bacenum.h" -#include "bits.h" -#include "npdu.h" -#include "apdu.h" -#define DEBUG_ENABLED 0 -#include "debug.h" - -#if PRINT_ENABLED -#include -#endif - -extern void router_handler( - BACNET_ADDRESS *src, - BACNET_NPDU_DATA *npdu_data, - uint8_t * npdu, /* PDU data */ - uint16_t npdu_len); - -void npdu_handler( - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t pdu_len) -{ /* length PDU */ - int apdu_offset = 0; - BACNET_ADDRESS dest = { 0 }; - BACNET_NPDU_DATA npdu_data = { 0 }; - - apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data); - if (npdu_data.network_layer_message) { - router_handler(src,&npdu_data,&pdu[apdu_offset], - (uint16_t) (pdu_len - apdu_offset)); - } else if ((apdu_offset > 0) && (apdu_offset <= pdu_len)) { - if ((npdu_data.protocol_version == BACNET_PROTOCOL_VERSION) && - ((dest.net == 0) || (dest.net == BACNET_BROADCAST_NETWORK))) { - /* only handle the version that we know how to handle */ - /* and we are not a router, so ignore messages with - routing information cause they are not for us */ - apdu_handler(src, &pdu[apdu_offset], - (uint16_t) (pdu_len - apdu_offset)); - } else { - if (dest.net) { - debug_printf("NPDU: DNET=%d. Discarded!\n", dest.net); - } else { - debug_printf("NPDU: BACnet Protocol Version=%d. Discarded!\n", - npdu_data.protocol_version); - } - } - } - - return; -} diff --git a/bacnet-stack/demo/initrouter/main.c b/bacnet-stack/demo/initrouter/main.c index 66478983..73e3e4b9 100644 --- a/bacnet-stack/demo/initrouter/main.c +++ b/bacnet-stack/demo/initrouter/main.c @@ -40,6 +40,8 @@ #include "device.h" #include "datalink.h" /* some demo stuff needed */ +#define DEBUG_ENABLED 0 +#include "debug.h" #include "filename.h" #include "handlers.h" #include "client.h" @@ -57,7 +59,7 @@ static BACNET_ROUTER_PORT *Target_Router_Port_List; static bool Error_Detected = false; -void MyAbortHandler( +static void MyAbortHandler( BACNET_ADDRESS * src, uint8_t invoke_id, uint8_t abort_reason, @@ -71,7 +73,7 @@ void MyAbortHandler( Error_Detected = true; } -void MyRejectHandler( +static void MyRejectHandler( BACNET_ADDRESS * src, uint8_t invoke_id, uint8_t reject_reason) @@ -83,7 +85,7 @@ void MyRejectHandler( Error_Detected = true; } -void router_handler( +static void My_Router_Handler( BACNET_ADDRESS *src, BACNET_NPDU_DATA *npdu_data, uint8_t * npdu, /* PDU data */ @@ -169,6 +171,40 @@ void router_handler( } } +static void My_NPDU_Handler( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t pdu_len) +{ /* length PDU */ + int apdu_offset = 0; + BACNET_ADDRESS dest = { 0 }; + BACNET_NPDU_DATA npdu_data = { 0 }; + + apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data); + if (npdu_data.network_layer_message) { + My_Router_Handler(src,&npdu_data,&pdu[apdu_offset], + (uint16_t) (pdu_len - apdu_offset)); + } else if ((apdu_offset > 0) && (apdu_offset <= pdu_len)) { + if ((npdu_data.protocol_version == BACNET_PROTOCOL_VERSION) && + ((dest.net == 0) || (dest.net == BACNET_BROADCAST_NETWORK))) { + /* only handle the version that we know how to handle */ + /* and we are not a router, so ignore messages with + routing information cause they are not for us */ + apdu_handler(src, &pdu[apdu_offset], + (uint16_t) (pdu_len - apdu_offset)); + } else { + if (dest.net) { + debug_printf("NPDU: DNET=%d. Discarded!\n", dest.net); + } else { + debug_printf("NPDU: BACnet Protocol Version=%d. Discarded!\n", + npdu_data.protocol_version); + } + } + } + + return; +} + static void Init_Service_Handlers( void) { @@ -379,7 +415,7 @@ int main(int argc, char *argv[]) { pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); /* process */ if (pdu_len) { - npdu_handler(&src, &Rx_Buf[0], pdu_len); + My_NPDU_Handler(&src, &Rx_Buf[0], pdu_len); } if (Error_Detected) break; diff --git a/bacnet-stack/demo/whoisrouter/Makefile b/bacnet-stack/demo/whoisrouter/Makefile index 42ac8092..08aad23a 100644 --- a/bacnet-stack/demo/whoisrouter/Makefile +++ b/bacnet-stack/demo/whoisrouter/Makefile @@ -38,7 +38,7 @@ OPTIMIZATION = -O0 CFLAGS = -Wall $(DEBUGGING) $(OPTIMIZATION) $(INCLUDES) $(DEFINES) LFLAGS = -Wl,-Map=$(TARGET).map,$(LIBRARIES) -SRCS = main.c h_npdu.c +SRCS = main.c OBJS = ${SRCS:.c=.o} diff --git a/bacnet-stack/demo/whoisrouter/h_npdu.c b/bacnet-stack/demo/whoisrouter/h_npdu.c deleted file mode 100644 index b2e8162f..00000000 --- a/bacnet-stack/demo/whoisrouter/h_npdu.c +++ /dev/null @@ -1,88 +0,0 @@ -/*####COPYRIGHTBEGIN#### - ------------------------------------------- - Copyright (C) 2008 Steve Karg - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License - as published by the Free Software Foundation; either version 2 - of the License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to: - The Free Software Foundation, Inc. - 59 Temple Place - Suite 330 - Boston, MA 02111-1307, USA. - - As a special exception, if other files instantiate templates or - use macros or inline functions from this file, or you compile - this file and link it with other works to produce a work based - on this file, this file does not by itself cause the resulting - work to be covered by the GNU General Public License. However - the source code for this file must still be made available in - accordance with section (3) of the GNU General Public License. - - This exception does not invalidate any other reasons why a work - based on this file might be covered by the GNU General Public - License. - ------------------------------------------- -####COPYRIGHTEND####*/ -#include -#include -#include "bacdef.h" -#include "bacdcode.h" -#include "bacint.h" -#include "bacenum.h" -#include "bits.h" -#include "npdu.h" -#include "apdu.h" -#define DEBUG_ENABLED 0 -#include "debug.h" - -#if PRINT_ENABLED -#include -#endif - -extern void router_handler( - BACNET_ADDRESS *src, - BACNET_NPDU_DATA *npdu_data, - uint8_t * npdu, /* PDU data */ - uint16_t npdu_len); - -void npdu_handler( - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t pdu_len) -{ /* length PDU */ - int apdu_offset = 0; - BACNET_ADDRESS dest = { 0 }; - BACNET_NPDU_DATA npdu_data = { 0 }; - - apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data); - if (npdu_data.network_layer_message) { - router_handler(src,&npdu_data,&pdu[apdu_offset], - (uint16_t) (pdu_len - apdu_offset)); - } else if ((apdu_offset > 0) && (apdu_offset <= pdu_len)) { - if ((npdu_data.protocol_version == BACNET_PROTOCOL_VERSION) && - ((dest.net == 0) || (dest.net == BACNET_BROADCAST_NETWORK))) { - /* only handle the version that we know how to handle */ - /* and we are not a router, so ignore messages with - routing information cause they are not for us */ - apdu_handler(src, &pdu[apdu_offset], - (uint16_t) (pdu_len - apdu_offset)); - } else { - if (dest.net) { - debug_printf("NPDU: DNET=%d. Discarded!\n", dest.net); - } else { - debug_printf("NPDU: BACnet Protocol Version=%d. Discarded!\n", - npdu_data.protocol_version); - } - } - } - - return; -} diff --git a/bacnet-stack/demo/whoisrouter/main.c b/bacnet-stack/demo/whoisrouter/main.c index c179911d..8afe1469 100644 --- a/bacnet-stack/demo/whoisrouter/main.c +++ b/bacnet-stack/demo/whoisrouter/main.c @@ -40,6 +40,7 @@ #include "device.h" #include "datalink.h" /* some demo stuff needed */ +#define DEBUG_ENABLED 0 #include "debug.h" #include "filename.h" #include "handlers.h" @@ -58,7 +59,7 @@ static BACNET_ADDRESS Target_Router_Address; static bool Error_Detected = false; -void MyAbortHandler( +static void MyAbortHandler( BACNET_ADDRESS * src, uint8_t invoke_id, uint8_t abort_reason, @@ -72,7 +73,7 @@ void MyAbortHandler( Error_Detected = true; } -void MyRejectHandler( +static void MyRejectHandler( BACNET_ADDRESS * src, uint8_t invoke_id, uint8_t reject_reason) @@ -84,7 +85,7 @@ void MyRejectHandler( Error_Detected = true; } -void router_handler( +static void My_Router_Handler( BACNET_ADDRESS *src, BACNET_NPDU_DATA *npdu_data, uint8_t * npdu, /* PDU data */ @@ -129,6 +130,40 @@ void router_handler( } } +void My_NPDU_Handler( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t pdu_len) +{ /* length PDU */ + int apdu_offset = 0; + BACNET_ADDRESS dest = { 0 }; + BACNET_NPDU_DATA npdu_data = { 0 }; + + apdu_offset = npdu_decode(&pdu[0], &dest, src, &npdu_data); + if (npdu_data.network_layer_message) { + My_Router_Handler(src,&npdu_data,&pdu[apdu_offset], + (uint16_t) (pdu_len - apdu_offset)); + } else if ((apdu_offset > 0) && (apdu_offset <= pdu_len)) { + if ((npdu_data.protocol_version == BACNET_PROTOCOL_VERSION) && + ((dest.net == 0) || (dest.net == BACNET_BROADCAST_NETWORK))) { + /* only handle the version that we know how to handle */ + /* and we are not a router, so ignore messages with + routing information cause they are not for us */ + apdu_handler(src, &pdu[apdu_offset], + (uint16_t) (pdu_len - apdu_offset)); + } else { + if (dest.net) { + debug_printf("NPDU: DNET=%d. Discarded!\n", dest.net); + } else { + debug_printf("NPDU: BACnet Protocol Version=%d. Discarded!\n", + npdu_data.protocol_version); + } + } + } + + return; +} + static void Init_Service_Handlers( void) { @@ -343,7 +378,7 @@ int main(int argc, char *argv[]) { pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); /* process */ if (pdu_len) { - npdu_handler(&src, &Rx_Buf[0], pdu_len); + My_NPDU_Handler(&src, &Rx_Buf[0], pdu_len); } if (Error_Detected) break;