Feature/refactor bacnet ipv4 add unit tests (#64)
* refactor BACnet/IPv4 BVLC into encode-decode library with unit tests Added Read-Broadcast-Distribution-Table encoding and unit test. Added Read-Broadcast-Distribution-Table-Ack encoding and unit test. Added Read-Foreign-Device-Table-Ack encoding and unit test. Added some BDT/FDT diff copy functions with unit tests Added some FDT add and delete entry functions with unit tests Added some BDT set append and clear entry functions with unit tests Added some BIPv4 address conversion functions with unit tests Added non-BBMD handling unit test Added basic unit test for BBMD handler Added BBMD broadcast mask get set and unit tests Added IPv6 maintenance timer. Added ReadFDT app Fixed ReadBDT app Added Who-Is to Makefile for individual app build Fixed debugging code blocks projects by swapping bip.c for h_bbmd.c module. Ported BACnet/IPv4 to refactored BVLC for Linux, BSD, Windows Fix datalink debug for DLENV module Improve BIPv4 linux driver debug info Added BDT mask functions Reduce debug info clutter in Who-Is app by using environment option Fix TTL seconds upper bounds addition Fix CIDR prefix calculation on Linux BIPv4. Convert BSD BIPv4 to BVLCv4 Fix CMake build for BIPv4 (Linux, BSD, Windows) Added [U]nsigned to 0xBAC0 constants Cleanup POSIX and Win32 API sockets Remove unnecessary file scope variable initialization Fix routed NPDU to depend on datalink; fix warning Remove OS dependent network code from gateway Enable BBMD client in library by default Co-authored-by: Steve Karg <skarg@users.sourceforge.net> Co-authored-by: Steve Karg <steve.karg@legrand.us>
This commit is contained in:
+16
-5
@@ -32,15 +32,14 @@ BACDL_DEFINE=-DBACDL_BIP6=1
|
||||
endif
|
||||
ifeq (${BACDL},)
|
||||
BACDL_DEFINE ?= -DBACDL_BIP=1
|
||||
BBMD_DEFINE ?= -DBBMD_ENABLED=1
|
||||
BBMD_DEFINE ?= -DBBMD_ENABLED=1 -DBBMD_CLIENT_ENABLED
|
||||
endif
|
||||
|
||||
ifeq (${BBMD},server)
|
||||
BBMD_DEFINE=-DBBMD_ENABLED=1
|
||||
endif
|
||||
ifeq (${BBMD},client)
|
||||
BBMD_DEFINE=-DBBMD_ENABLED=1
|
||||
BBMD_DEFINE=-DBBMD_CLIENT_ENABLED
|
||||
BBMD_DEFINE = -DBBMD_ENABLED=1 -DBBMD_CLIENT_ENABLED
|
||||
endif
|
||||
endif
|
||||
|
||||
@@ -131,7 +130,7 @@ PORT_ETHERNET_SRC = \
|
||||
PORT_BIP_SRC = \
|
||||
$(BACNET_PORT_DIR)/bip-init.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/datalink/bvlc.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/datalink/bip.c
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/bbmd/h_bbmd.c
|
||||
|
||||
PORT_BIP6_SRC = \
|
||||
$(BACNET_PORT_DIR)/bip6.c \
|
||||
@@ -189,7 +188,7 @@ SUBDIRS = readprop writeprop readfile writefile reinit server dcc \
|
||||
writepropm uptransfer getevent uevent abort error
|
||||
|
||||
ifeq (${BACDL_DEFINE},-DBACDL_BIP=1)
|
||||
SUBDIRS += whoisrouter iamrouter initrouter readbdt
|
||||
SUBDIRS += whoisrouter iamrouter initrouter readbdt readfdt
|
||||
endif
|
||||
|
||||
ifeq (${BACNET_PORT},linux)
|
||||
@@ -258,10 +257,22 @@ iamrouter:
|
||||
initrouter:
|
||||
$(MAKE) -b -C $@
|
||||
|
||||
.PHONY: readbdt
|
||||
readbdt:
|
||||
$(MAKE) -b -C $@
|
||||
|
||||
.PHONY: readfdt
|
||||
readfdt:
|
||||
$(MAKE) -b -C $@
|
||||
|
||||
.PHONY: uevent
|
||||
uevent:
|
||||
$(MAKE) -b -C $@
|
||||
|
||||
.PHONY: whois
|
||||
whois:
|
||||
$(MAKE) -b -C $@
|
||||
|
||||
.PHONY: router
|
||||
router:
|
||||
$(MAKE) -s -b -C $@
|
||||
|
||||
+3
-2
@@ -1432,7 +1432,7 @@ int main(int argc, char *argv[])
|
||||
* My_BIP_Port will be non-zero in this case.
|
||||
*/
|
||||
if (My_BIP_Port > 0) {
|
||||
bip_set_port(htons(My_BIP_Port));
|
||||
bip_set_port(My_BIP_Port);
|
||||
}
|
||||
#endif
|
||||
address_init();
|
||||
@@ -1446,7 +1446,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
#if defined(BACDL_BIP)
|
||||
if (My_BIP_Port > 0) {
|
||||
bip_set_port(htons(0xBAC0)); /* Set back to std BACnet/IP port */
|
||||
/* Set back to std BACnet/IP port */
|
||||
bip_set_port(0xBAC0);
|
||||
}
|
||||
#endif
|
||||
/* try to bind with the target device */
|
||||
|
||||
+20
-46
@@ -31,6 +31,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include "bacnet/config.h"
|
||||
@@ -172,19 +173,20 @@ static void Initialize_Device_Addresses()
|
||||
DEVICE_OBJECT_DATA *pDev = NULL;
|
||||
/* Setup info for the main gateway device first */
|
||||
pDev = Get_Routed_Device_Object(i);
|
||||
|
||||
/* we can't use datalink_get_my_address() since it is
|
||||
mapped to routed_get_my_address() in this app
|
||||
to get the parent device address */
|
||||
#if defined(BACDL_BIP)
|
||||
uint16_t myPort;
|
||||
struct in_addr *netPtr; /* Lets us cast to this type */
|
||||
uint8_t *gatewayMac = NULL;
|
||||
uint32_t myAddr = bip_get_addr();
|
||||
gatewayMac = pDev->bacDevAddr.mac; /* Keep pointer to the main MAC */
|
||||
memcpy(pDev->bacDevAddr.mac, &myAddr, 4);
|
||||
myPort = bip_get_port();
|
||||
memcpy(&pDev->bacDevAddr.mac[4], &myPort, 2);
|
||||
pDev->bacDevAddr.mac_len = 6;
|
||||
bip_get_my_address(&pDev->bacDevAddr);
|
||||
#elif defined(BACDL_MSTP)
|
||||
pDev->bacDevAddr.mac_len = 1;
|
||||
pDev->bacDevAddr.mac[0] = dlmstp_mac_address();
|
||||
dlmstp_get_my_address(&pDev->bacDevAddr);
|
||||
#elif defined(BACDL_ARCNET)
|
||||
arcnet_get_my_address(&pDev->bacDevAddr);
|
||||
#elif defined(BACDL_ETHERNET)
|
||||
ethernet_get_my_address(&pDev->bacDevAddr);
|
||||
#elif defined(BACDL_BIP6)
|
||||
bip6_get_my_address&pDev->bacDevAddr);
|
||||
#else
|
||||
#error "No support for this Data Link Layer type "
|
||||
#endif
|
||||
@@ -193,38 +195,12 @@ static void Initialize_Device_Addresses()
|
||||
|
||||
for (i = 1; i < MAX_NUM_DEVICES; i++) {
|
||||
pDev = Get_Routed_Device_Object(i);
|
||||
if (pDev == NULL)
|
||||
if (pDev == NULL) {
|
||||
continue;
|
||||
#if defined(BACDL_BIP)
|
||||
virtual_mac = i;
|
||||
netPtr = (struct in_addr *)pDev->bacDevAddr.mac;
|
||||
#if (MAX_NUM_DEVICES > 0xFFFFFF)
|
||||
pDev->bacDevAddr.mac[0] = ((virtual_mac & 0xff000000) >> 24);
|
||||
#else
|
||||
pDev->bacDevAddr.mac[0] = gatewayMac[3];
|
||||
#endif
|
||||
#if (MAX_NUM_DEVICES > 0xFFFF)
|
||||
pDev->bacDevAddr.mac[1] = ((virtual_mac & 0xff0000) >> 16);
|
||||
#else
|
||||
pDev->bacDevAddr.mac[1] = gatewayMac[2];
|
||||
#endif
|
||||
#if (MAX_NUM_DEVICES > 0xFF)
|
||||
pDev->bacDevAddr.mac[2] = ((virtual_mac & 0xff00) >> 8);
|
||||
#else
|
||||
pDev->bacDevAddr.mac[2] = gatewayMac[1];
|
||||
#endif
|
||||
pDev->bacDevAddr.mac[3] = (virtual_mac & 0xff);
|
||||
memcpy(&pDev->bacDevAddr.mac[4], &myPort, 2);
|
||||
pDev->bacDevAddr.mac_len = 6;
|
||||
pDev->bacDevAddr.net = VIRTUAL_DNET;
|
||||
memcpy(&pDev->bacDevAddr.adr[0], &pDev->bacDevAddr.mac[0], 6);
|
||||
pDev->bacDevAddr.len = 6;
|
||||
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
|
||||
}
|
||||
virtual_mac = pDev->bacObj.Object_Instance_Number;
|
||||
encode_unsigned24(&pDev->bacDevAddr.adr[0], virtual_mac);
|
||||
pDev->bacDevAddr.len = 3;
|
||||
/* broadcast an I-Am for each routed Device now */
|
||||
Send_I_Am(&Handler_Transmit_Buffer[0]);
|
||||
}
|
||||
@@ -234,7 +210,7 @@ static void Initialize_Device_Addresses()
|
||||
*
|
||||
* @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
|
||||
* datalink_receive, npdu_handler,
|
||||
* dcc_timer_seconds, bvlc_maintenance_timer,
|
||||
* dcc_timer_seconds, datalink_maintenance_timer,
|
||||
* Load_Control_State_Machine_Handler, handler_cov_task,
|
||||
* tsm_timer_milliseconds
|
||||
*
|
||||
@@ -309,9 +285,7 @@ int main(int argc, char *argv[])
|
||||
if (elapsed_seconds) {
|
||||
last_seconds = current_seconds;
|
||||
dcc_timer_seconds(elapsed_seconds);
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
dlenv_maintenance_timer(elapsed_seconds);
|
||||
Load_Control_State_Machine_Handler();
|
||||
elapsed_milliseconds = elapsed_seconds * 1000;
|
||||
|
||||
@@ -368,9 +368,7 @@ int main(int argc, char *argv[])
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds = current_seconds - last_seconds;
|
||||
if (elapsed_seconds) {
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
}
|
||||
total_seconds += elapsed_seconds;
|
||||
if (total_seconds > timeout_seconds) {
|
||||
|
||||
+2
-4
@@ -197,7 +197,7 @@ static void piface_task(void)
|
||||
*
|
||||
* @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
|
||||
* datalink_receive, npdu_handler,
|
||||
* dcc_timer_seconds, bvlc_maintenance_timer,
|
||||
* dcc_timer_seconds, datalink_maintenance_timer,
|
||||
* handler_cov_task,
|
||||
* tsm_timer_milliseconds
|
||||
*
|
||||
@@ -254,9 +254,7 @@ int main(int argc, char *argv[])
|
||||
if (elapsed_seconds) {
|
||||
last_seconds = current_seconds;
|
||||
dcc_timer_seconds(elapsed_seconds);
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
dlenv_maintenance_timer(elapsed_seconds);
|
||||
elapsed_milliseconds = elapsed_seconds * 1000;
|
||||
handler_cov_timer_seconds(elapsed_seconds);
|
||||
|
||||
+6
-10
@@ -56,8 +56,7 @@
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
|
||||
|
||||
/* targets interpreted from the command line options */
|
||||
static uint32_t Target_BBMD_Address;
|
||||
static uint16_t Target_BBMD_Port;
|
||||
static BACNET_IP_ADDRESS Target_BBMD_Address;
|
||||
|
||||
static bool Error_Detected = false;
|
||||
|
||||
@@ -134,8 +133,7 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
/* decode the command line parameters */
|
||||
if (argc > 1) {
|
||||
Target_BBMD_Address = inet_addr(argv[1]);
|
||||
if (Target_BBMD_Address == (-1)) {
|
||||
if (!bip_get_addr_by_name(argv[1], &Target_BBMD_Address)) {
|
||||
fprintf(stderr, "IP=%s - failed to convert address.\r\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
@@ -143,14 +141,14 @@ int main(int argc, char *argv[])
|
||||
if (argc > 2) {
|
||||
port = strtol(argv[2], NULL, 0);
|
||||
if ((port > 0) && (port <= 65535)) {
|
||||
Target_BBMD_Port = htons(port);
|
||||
Target_BBMD_Address.port = (uint16_t)port;
|
||||
} else {
|
||||
fprintf(
|
||||
stderr, "port=%ld - port must be between 0-65535.\r\n", port);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
Target_BBMD_Port = htons(47808);
|
||||
Target_BBMD_Address.port = 0xBAC0U;
|
||||
}
|
||||
/* setup my info */
|
||||
Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
|
||||
@@ -162,7 +160,7 @@ int main(int argc, char *argv[])
|
||||
last_seconds = time(NULL);
|
||||
timeout_seconds = apdu_timeout() / 1000;
|
||||
/* send the request */
|
||||
bvlc_bbmd_read_bdt(Target_BBMD_Address, Target_BBMD_Port);
|
||||
bvlc_bbmd_read_bdt(&Target_BBMD_Address);
|
||||
/* loop forever */
|
||||
for (;;) {
|
||||
/* increment timer - exit if timed out */
|
||||
@@ -178,9 +176,7 @@ int main(int argc, char *argv[])
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds = current_seconds - last_seconds;
|
||||
if (elapsed_seconds) {
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
}
|
||||
total_seconds += elapsed_seconds;
|
||||
if (total_seconds > timeout_seconds) {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#Makefile to build BACnet Application for the GCC Port
|
||||
|
||||
TARGET = bacrfdt
|
||||
# BACnet objects that are used with this app
|
||||
BACNET_OBJECT_DIR = $(BACNET_SRC_DIR)/bacnet/basic/object
|
||||
SRC = main.c \
|
||||
$(BACNET_OBJECT_DIR)/client/device-client.c \
|
||||
$(BACNET_OBJECT_DIR)/netport.c
|
||||
BACNET_BASIC_SRC += \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/h_apdu.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/h_iam.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/h_noserv.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/h_rp.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/h_whois.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/s_iam.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/service/s_whois.c
|
||||
|
||||
# TARGET_EXT is defined in apps/Makefile as .exe or nothing
|
||||
TARGET_BIN = ${TARGET}$(TARGET_EXT)
|
||||
|
||||
SRCS = $(SRC) $(BACNET_SRC) $(BACNET_BASIC_SRC) $(BACNET_PORT_SRC)
|
||||
|
||||
OBJS += ${SRCS:.c=.o}
|
||||
|
||||
.PHONY: all
|
||||
all: Makefile ${TARGET_BIN}
|
||||
|
||||
${TARGET_BIN}: ${OBJS}
|
||||
${CC} ${PFLAGS} ${OBJS} ${LFLAGS} -o $@
|
||||
size $@
|
||||
cp $@ ../../bin
|
||||
|
||||
.c.o:
|
||||
${CC} -c ${CFLAGS} $*.c -o $@
|
||||
|
||||
.PHONY: depend
|
||||
depend:
|
||||
rm -f .depend
|
||||
${CC} -MM ${CFLAGS} *.c >> .depend
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f core ${TARGET_BIN} ${OBJS} $(TARGET).map
|
||||
|
||||
.PHONY: include
|
||||
include: .depend
|
||||
|
||||
@@ -0,0 +1,190 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
/* command line tool that sends a BACnet BVLC message, and displays the reply */
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h> /* for time */
|
||||
#include <errno.h>
|
||||
#include <ctype.h> /* for toupper */
|
||||
#include "bacnet/bactext.h"
|
||||
#include "bacnet/iam.h"
|
||||
#include "bacnet/basic/binding/address.h"
|
||||
#include "bacnet/config.h"
|
||||
#include "bacnet/bacdef.h"
|
||||
#include "bacnet/npdu.h"
|
||||
#include "bacnet/apdu.h"
|
||||
#include "bacnet/basic/object/device.h"
|
||||
#include "bacnet/datalink/datalink.h"
|
||||
#include "bacnet/datalink/bvlc.h"
|
||||
/* some demo stuff needed */
|
||||
#ifndef DEBUG_ENABLED
|
||||
#define DEBUG_ENABLED 0
|
||||
#endif
|
||||
#include "bacnet/basic/sys/debug.h"
|
||||
#include "bacnet/basic/sys/filename.h"
|
||||
#include "bacnet/basic/services.h"
|
||||
#include "bacnet/basic/services.h"
|
||||
#include "bacnet/basic/tsm/tsm.h"
|
||||
#include "bacnet/datalink/dlenv.h"
|
||||
|
||||
/* buffer used for receive */
|
||||
static uint8_t Rx_Buf[MAX_MPDU];
|
||||
|
||||
/* targets interpreted from the command line options */
|
||||
static BACNET_IP_ADDRESS Target_BBMD_Address;
|
||||
|
||||
static bool Error_Detected;
|
||||
|
||||
static void MyAbortHandler(
|
||||
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t abort_reason, bool server)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
(void)server;
|
||||
printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
static void MyRejectHandler(
|
||||
BACNET_ADDRESS *src, uint8_t invoke_id, uint8_t reject_reason)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
static void Init_Service_Handlers(void)
|
||||
{
|
||||
Device_Init(NULL);
|
||||
/* we need to handle who-is
|
||||
to support dynamic device binding to us */
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is);
|
||||
/* 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(handler_unrecognized_service);
|
||||
/* we must implement read property - it's required! */
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property);
|
||||
/* handle the reply (request) coming back */
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_add);
|
||||
/* handle any errors coming back */
|
||||
apdu_set_abort_handler(MyAbortHandler);
|
||||
apdu_set_reject_handler(MyRejectHandler);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BACNET_ADDRESS src = { 0 }; /* address where message came from */
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 100; /* milliseconds */
|
||||
time_t total_seconds = 0;
|
||||
time_t elapsed_seconds = 0;
|
||||
time_t last_seconds = 0;
|
||||
time_t current_seconds = 0;
|
||||
time_t timeout_seconds = 0;
|
||||
long port = 0;
|
||||
|
||||
if (argc < 2) {
|
||||
printf("Usage: %s IP [port]\r\n", filename_remove_path(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
if ((argc > 1) && (strcmp(argv[1], "--help") == 0)) {
|
||||
printf(
|
||||
"Send a Read-Foreign-Device-Table message to a BBMD.\r\n"
|
||||
"\r\n"
|
||||
"IP:\r\n"
|
||||
"IP address of the BBMD in dotted decimal notation\r\n"
|
||||
"[port]\r\n"
|
||||
"optional BACnet/IP port number (default=47808=0xBAC0)\r\n"
|
||||
"\r\n"
|
||||
"To send a Read-Foreign-Device-Table message to a BBMD\r\n"
|
||||
"at 192.168.0.1 using port 47808:\r\n"
|
||||
"%s 192.168.0.1 47808\r\n",
|
||||
filename_remove_path(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
/* decode the command line parameters */
|
||||
if (argc > 1) {
|
||||
if (!bip_get_addr_by_name(argv[1], &Target_BBMD_Address)) {
|
||||
fprintf(stderr, "IP=%s - failed to convert address.\r\n", argv[1]);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (argc > 2) {
|
||||
port = strtol(argv[2], NULL, 0);
|
||||
if ((port > 0) && (port <= 65535)) {
|
||||
Target_BBMD_Address.port = (uint16_t)port;
|
||||
} else {
|
||||
fprintf(
|
||||
stderr, "port=%ld - port must be between 0-65535.\r\n", port);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
Target_BBMD_Address.port = 0xBAC0U;
|
||||
}
|
||||
/* setup my info */
|
||||
Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
|
||||
Init_Service_Handlers();
|
||||
address_init();
|
||||
dlenv_init();
|
||||
atexit(datalink_cleanup);
|
||||
/* configure the timeout values */
|
||||
last_seconds = time(NULL);
|
||||
timeout_seconds = apdu_timeout() / 1000;
|
||||
/* send the request */
|
||||
bvlc_bbmd_read_fdt(&Target_BBMD_Address);
|
||||
/* loop forever */
|
||||
for (;;) {
|
||||
/* increment timer - exit if timed out */
|
||||
current_seconds = time(NULL);
|
||||
/* returns 0 bytes on timeout */
|
||||
pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
|
||||
/* process */
|
||||
if (pdu_len) {
|
||||
npdu_handler(&src, &Rx_Buf[0], pdu_len);
|
||||
}
|
||||
if (Error_Detected)
|
||||
break;
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds = current_seconds - last_seconds;
|
||||
if (elapsed_seconds) {
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
}
|
||||
total_seconds += elapsed_seconds;
|
||||
if (total_seconds > timeout_seconds) {
|
||||
break;
|
||||
}
|
||||
/* keep track of time for next check */
|
||||
last_seconds = current_seconds;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -25,7 +25,7 @@ PORT_BIP6_SRC = \
|
||||
PORT_BIP_SRC = \
|
||||
$(BACNET_PORT_DIR)/bip-init.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/datalink/bvlc.c \
|
||||
$(BACNET_SRC_DIR)/bacnet/datalink/bip.c
|
||||
$(BACNET_SRC_DIR)/bacnet/basic/bbmd/h_bbmd.c
|
||||
|
||||
# WARNINGS, DEBUGGING, OPTIMIZATION are defined in common apps Makefile
|
||||
# BACNET_DEFINES is defined in common apps Makefile
|
||||
|
||||
@@ -998,13 +998,12 @@ static void datalink_init(void)
|
||||
{
|
||||
char *pEnv = NULL;
|
||||
BACNET_ADDRESS my_address = { 0 };
|
||||
extern bool BIP_Debug;
|
||||
|
||||
/* BACnet/IP Initialization */
|
||||
BIP_Debug = true;
|
||||
bip_debug_enable();
|
||||
pEnv = getenv("BACNET_IP_PORT");
|
||||
if (pEnv) {
|
||||
bip_set_port(htons((uint16_t)strtol(pEnv, NULL, 0)));
|
||||
bip_set_port((uint16_t)strtol(pEnv, NULL, 0));
|
||||
} else {
|
||||
/* BIP_Port is statically initialized to 0xBAC0,
|
||||
* so if it is different, then it was programmatically altered,
|
||||
@@ -1012,8 +1011,8 @@ static void datalink_init(void)
|
||||
* Unless it is set below 1024, since:
|
||||
* "The range for well-known ports managed by the IANA is 0-1023."
|
||||
*/
|
||||
if (ntohs(bip_get_port()) < 1024) {
|
||||
bip_set_port(htons(0xBAC0));
|
||||
if (bip_get_port() < 1024) {
|
||||
bip_set_port(0xBAC0U);
|
||||
}
|
||||
}
|
||||
if (!bip_init(getenv("BACNET_IFACE"))) {
|
||||
@@ -1169,6 +1168,7 @@ int main(int argc, char *argv[])
|
||||
if (elapsed_seconds) {
|
||||
last_seconds = current_seconds;
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
bvlc6_maintenance_timer(elapsed_seconds);
|
||||
}
|
||||
if (Exit_Requested) {
|
||||
break;
|
||||
|
||||
+2
-2
@@ -299,7 +299,7 @@ bool read_config(char *filepath)
|
||||
if (result) {
|
||||
current->params.bip_params.port = param;
|
||||
} else {
|
||||
current->params.bip_params.port = 0xBAC0;
|
||||
current->params.bip_params.port = 0xBAC0U;
|
||||
}
|
||||
result =
|
||||
config_setting_lookup_int(port, "network", (int *)¶m);
|
||||
@@ -477,7 +477,7 @@ bool parse_cmd(int argc, char *argv[])
|
||||
}
|
||||
|
||||
/* setup default parameters */
|
||||
current->params.bip_params.port = 0xBAC0; /* 47808 */
|
||||
current->params.bip_params.port = 0xBAC0U; /* 47808 */
|
||||
current->route_info.net = get_next_free_dnet();
|
||||
|
||||
/* check if interface is valid */
|
||||
|
||||
+2
-4
@@ -173,7 +173,7 @@ static void print_help(const char *filename)
|
||||
*
|
||||
* @see Device_Set_Object_Instance_Number, dlenv_init, Send_I_Am,
|
||||
* datalink_receive, npdu_handler,
|
||||
* dcc_timer_seconds, bvlc_maintenance_timer,
|
||||
* dcc_timer_seconds, datalink_maintenance_timer,
|
||||
* Load_Control_State_Machine_Handler, handler_cov_task,
|
||||
* tsm_timer_milliseconds
|
||||
*
|
||||
@@ -275,9 +275,7 @@ int main(int argc, char *argv[])
|
||||
if (elapsed_seconds) {
|
||||
last_seconds = current_seconds;
|
||||
dcc_timer_seconds(elapsed_seconds);
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
dlenv_maintenance_timer(elapsed_seconds);
|
||||
Load_Control_State_Machine_Handler();
|
||||
elapsed_milliseconds = elapsed_seconds * 1000;
|
||||
|
||||
+27
-23
@@ -60,6 +60,8 @@ static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
|
||||
static int32_t Target_Object_Instance_Min = -1;
|
||||
static int32_t Target_Object_Instance_Max = -1;
|
||||
static bool Error_Detected = false;
|
||||
/* debug info printing */
|
||||
static bool BACnet_Debug_Enabled;
|
||||
|
||||
#define BAC_ADDRESS_MULT 1
|
||||
|
||||
@@ -130,32 +132,32 @@ static void my_i_am_handler(
|
||||
(void)service_len;
|
||||
len = iam_decode_service_request(
|
||||
service_request, &device_id, &max_apdu, &segmentation, &vendor_id);
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "Received I-Am Request");
|
||||
#endif
|
||||
if (BACnet_Debug_Enabled) {
|
||||
fprintf(stderr, "Received I-Am Request");
|
||||
}
|
||||
if (len != -1) {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, " from %lu, MAC = ", (unsigned long)device_id);
|
||||
if ((src->mac_len == 6) && (src->len == 0)) {
|
||||
fprintf(stderr, "%u.%u.%u.%u %02X%02X\n", (unsigned)src->mac[0],
|
||||
(unsigned)src->mac[1], (unsigned)src->mac[2],
|
||||
(unsigned)src->mac[3], (unsigned)src->mac[4],
|
||||
(unsigned)src->mac[5]);
|
||||
} else {
|
||||
for (i = 0; i < src->mac_len; i++) {
|
||||
fprintf(stderr, "%02X", (unsigned)src->mac[i]);
|
||||
if (i < (src->mac_len - 1)) {
|
||||
fprintf(stderr, ":");
|
||||
if (BACnet_Debug_Enabled) {
|
||||
fprintf(stderr, " from %lu, MAC = ", (unsigned long)device_id);
|
||||
if ((src->mac_len == 6) && (src->len == 0)) {
|
||||
fprintf(stderr, "%u.%u.%u.%u %02X%02X\n", (unsigned)src->mac[0],
|
||||
(unsigned)src->mac[1], (unsigned)src->mac[2],
|
||||
(unsigned)src->mac[3], (unsigned)src->mac[4],
|
||||
(unsigned)src->mac[5]);
|
||||
} else {
|
||||
for (i = 0; i < src->mac_len; i++) {
|
||||
fprintf(stderr, "%02X", (unsigned)src->mac[i]);
|
||||
if (i < (src->mac_len - 1)) {
|
||||
fprintf(stderr, ":");
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
#endif
|
||||
address_table_add(device_id, max_apdu, src);
|
||||
} else {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, ", but unable to decode it.\n");
|
||||
#endif
|
||||
if (BACnet_Debug_Enabled) {
|
||||
fprintf(stderr, ", but unable to decode it.\n");
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
@@ -342,6 +344,10 @@ int main(int argc, char *argv[])
|
||||
unsigned int target_args = 0;
|
||||
char *filename = NULL;
|
||||
|
||||
/* check for local environment settings */
|
||||
if (getenv("BACNET_DEBUG")) {
|
||||
BACnet_Debug_Enabled = true;
|
||||
}
|
||||
/* decode any command line parameters */
|
||||
filename = filename_remove_path(argv[0]);
|
||||
for (argi = 1; argi < argc; argi++) {
|
||||
@@ -461,9 +467,7 @@ int main(int argc, char *argv[])
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds = current_seconds - last_seconds;
|
||||
if (elapsed_seconds) {
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
}
|
||||
total_seconds += elapsed_seconds;
|
||||
if (total_seconds > timeout_seconds) {
|
||||
|
||||
@@ -296,9 +296,7 @@ int main(int argc, char *argv[])
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds = current_seconds - last_seconds;
|
||||
if (elapsed_seconds) {
|
||||
#if defined(BACDL_BIP) && BBMD_ENABLED
|
||||
bvlc_maintenance_timer(elapsed_seconds);
|
||||
#endif
|
||||
datalink_maintenance_timer(elapsed_seconds);
|
||||
}
|
||||
total_seconds += elapsed_seconds;
|
||||
if (total_seconds > timeout_seconds) {
|
||||
|
||||
Reference in New Issue
Block a user