From e3d5697cc1dd494c678f92723e00d56c3caa9891 Mon Sep 17 00:00:00 2001 From: skarg Date: Tue, 23 May 2006 20:58:13 +0000 Subject: [PATCH] adding BVLC support - not working yet. --- bacnet-stack/bip.c | 9 ++++++-- bacnet-stack/bip.h | 1 + bacnet-stack/bvlc.c | 56 ++++++++++++++++++++++++++------------------- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/bacnet-stack/bip.c b/bacnet-stack/bip.c index 801081af..7554e9e4 100644 --- a/bacnet-stack/bip.c +++ b/bacnet-stack/bip.c @@ -51,6 +51,11 @@ void bip_set_socket(int sock_fd) BIP_Socket = sock_fd; } +int bip_socket(void) +{ + return BIP_Socket; +} + bool bip_valid(void) { return (BIP_Socket != -1); @@ -248,7 +253,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ /* the signature of a BACnet/IP packet */ if (buf[0] != BVLL_TYPE_BACNET_IP) return 0; - if ((buf[1] == BVLC_ORIGINAL_UNICAST_NPDU) || + if ((buf[1] == BVLC_ORIGINAL_UNICAST_NPDU) || (buf[1] == BVLC_ORIGINAL_BROADCAST_NPDU)) { /* ignore messages from me */ if (sin.sin_addr.s_addr == BIP_Address.s_addr) @@ -275,7 +280,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ } #ifdef BBMD_ENABLED if (buf[1] < MAX_BVLC_FUNCTION) { - bbmd_handler(&buf[0], received_bytes, &sin); + bbmd_handler(&buf[0], received_bytes, &sin); } #endif diff --git a/bacnet-stack/bip.h b/bacnet-stack/bip.h index 5884ca7d..6bb05688 100644 --- a/bacnet-stack/bip.h +++ b/bacnet-stack/bip.h @@ -56,6 +56,7 @@ extern "C" { /* normal functions... */ void bip_cleanup(void); void bip_set_socket(int sock_fd); + int bip_socket(void); bool bip_valid(void); void bip_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ void bip_get_my_address(BACNET_ADDRESS * my_address); diff --git a/bacnet-stack/bvlc.c b/bacnet-stack/bvlc.c index 4f29342e..be3b60aa 100644 --- a/bacnet-stack/bvlc.c +++ b/bacnet-stack/bvlc.c @@ -34,6 +34,7 @@ #include /* for standard integer types uint8_t etc. */ #include /* for the standard bool type. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bip.h" #include "net.h" /* custom per port */ @@ -43,19 +44,13 @@ Broadcast Distribution Table, and Foreign Device Registration */ -typedef struct -{ - /* IP Address - stored in host byte order */ - struct in_addr address; - uint16_t port; -} BIP_ADDRESS; - typedef struct { /* true if valid entry - false if not */ bool valid; /* BACnet/IP address */ - BIP_ADDRESS bip_address; + struct in_addr dest_address; + uint16_t dest_port; /* Broadcast Distribution Mask - stored in host byte order */ struct in_addr broadcast_mask; } BBMD_TABLE_ENTRY; @@ -76,14 +71,15 @@ typedef struct { bool valid; /* BACnet/IP address */ - BIP_ADDRESS bip_address; + struct in_addr dest_address; + uint16_t dest_port; /* seconds for valid entry lifetime */ uint16_t time_to_live; - time_t seconds_remaining; + time_t seconds_remaining; /* includes 30 second grace period */ } FD_TABLE_ENTRY; #define MAX_FD_ENTRIES 128 -static FOREIGN_DEVICE_TABLE_ENTRY FD_Table[MAX_FD_ENTRIES]; +static FD_TABLE_ENTRY FD_Table[MAX_FD_ENTRIES]; void bvlc_maintenance_timer(unsigned seconds) { @@ -92,14 +88,19 @@ void bvlc_maintenance_timer(unsigned seconds) for (i = 0; i < MAX_FD_ENTRIES; i++) { if (FD_Table[i].valid) { - if (FD_Table[i].time_to_live + if (FD_Table[i].seconds_remaining) + { + if (FD_Table[i].seconds_remaining < seconds) + FD_Table[i].seconds_remaining = 0; + else + FD_Table[i].seconds_remaining -= seconds; + if (FD_Table[i].seconds_remaining == 0) + { + FD_Table[i].valid = false; + } + } } } - - -static FOREIGN_DEVICE_TABLE_ENTRY FD_Table[MAX_FD_ENTRIES]; - - } int bvlc_encode_bip_address( @@ -401,7 +402,7 @@ int bvlc_encode_original_broadcast_npdu( /* copy the source internet address to the BACnet address */ /* FIXME: IPv6? */ /* FIXME: is sockaddr_in host or network order? */ -void bvlc_internet_to_bacnet_address +void bvlc_internet_to_bacnet_address( BACNET_ADDRESS * src, /* returns the BACnet source address */ struct sockaddr_in * sin) /* source internet address */ { @@ -422,7 +423,7 @@ void bvlc_internet_to_bacnet_address /* copy the source internet address to the BACnet address */ /* FIXME: IPv6? */ /* FIXME: is sockaddr_in host or network order? */ -void bvlc_bacnet_to_internet_address +void bvlc_bacnet_to_internet_address( struct sockaddr_in * sin, /* source internet address */ BACNET_ADDRESS * src) /* returns the BACnet source address */ { @@ -449,17 +450,20 @@ void bvlc_bdt_forward_npdu( int mtu_len = 0; int bytes_sent = 0; unsigned i = 0; /* loop counter */ + struct sockaddr_in bip_dest; /* assumes that the driver has already been initialized */ - if (BIP_Socket < 0) - return BIP_Socket; + if (bip_socket() < 0) + return; mtu_len = bvlc_encode_forwarded_npdu( &mtu[0], - src, + sin, npdu, npdu_length); + /* load destination IP address */ + bip_dest.sin_family = AF_INET; /* loop through the BDT and send one to each entry, except us */ for (i = 0; i < MAX_BBMD_ENTRIES; i++) { @@ -470,15 +474,19 @@ void bvlc_bdt_forward_npdu( mask in the BDT entry and logically ORing it with the BBMD address of the same entry. */ + bip_dest.sin_addr.s_addr = + htonl(((~BBMD_Table[i].broadcast_mask.s_addr) | + BBMD_Table[i].dest_address.s_addr)); + bip_dest.sin_port = htons(BBMD_Table[i].dest_port); + /* Send the packet */ - bytes_sent = sendto(BIP_Socket, (char *) mtu, mtu_len, 0, + bytes_sent = sendto(bip_socket(), (char *) mtu, mtu_len, 0, (struct sockaddr *) bip_dest, sizeof(struct sockaddr)); } return bytes_sent; } - void bvlc_fdt_forward_npdu( struct sockaddr_in *sin, /* the source address */ uint8_t * npdu, /* returns the NPDU */