adding BVLC support - not working yet.

This commit is contained in:
skarg
2006-05-23 20:58:13 +00:00
parent 85d5ebe819
commit e3d5697cc1
3 changed files with 40 additions and 26 deletions
+5
View File
@@ -51,6 +51,11 @@ void bip_set_socket(int sock_fd)
BIP_Socket = sock_fd; BIP_Socket = sock_fd;
} }
int bip_socket(void)
{
return BIP_Socket;
}
bool bip_valid(void) bool bip_valid(void)
{ {
return (BIP_Socket != -1); return (BIP_Socket != -1);
+1
View File
@@ -56,6 +56,7 @@ extern "C" {
/* normal functions... */ /* normal functions... */
void bip_cleanup(void); void bip_cleanup(void);
void bip_set_socket(int sock_fd); void bip_set_socket(int sock_fd);
int bip_socket(void);
bool bip_valid(void); bool bip_valid(void);
void bip_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ void bip_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */
void bip_get_my_address(BACNET_ADDRESS * my_address); void bip_get_my_address(BACNET_ADDRESS * my_address);
+32 -24
View File
@@ -34,6 +34,7 @@
#include <stdint.h> /* for standard integer types uint8_t etc. */ #include <stdint.h> /* for standard integer types uint8_t etc. */
#include <stdbool.h> /* for the standard bool type. */ #include <stdbool.h> /* for the standard bool type. */
#include <time.h> /* for the standard bool type. */
#include "bacdcode.h" #include "bacdcode.h"
#include "bip.h" #include "bip.h"
#include "net.h" /* custom per port */ #include "net.h" /* custom per port */
@@ -43,19 +44,13 @@
Broadcast Distribution Table, and Broadcast Distribution Table, and
Foreign Device Registration */ Foreign Device Registration */
typedef struct
{
/* IP Address - stored in host byte order */
struct in_addr address;
uint16_t port;
} BIP_ADDRESS;
typedef struct typedef struct
{ {
/* true if valid entry - false if not */ /* true if valid entry - false if not */
bool valid; bool valid;
/* BACnet/IP address */ /* BACnet/IP address */
BIP_ADDRESS bip_address; struct in_addr dest_address;
uint16_t dest_port;
/* Broadcast Distribution Mask - stored in host byte order */ /* Broadcast Distribution Mask - stored in host byte order */
struct in_addr broadcast_mask; struct in_addr broadcast_mask;
} BBMD_TABLE_ENTRY; } BBMD_TABLE_ENTRY;
@@ -76,14 +71,15 @@ typedef struct
{ {
bool valid; bool valid;
/* BACnet/IP address */ /* BACnet/IP address */
BIP_ADDRESS bip_address; struct in_addr dest_address;
uint16_t dest_port;
/* seconds for valid entry lifetime */ /* seconds for valid entry lifetime */
uint16_t time_to_live; uint16_t time_to_live;
time_t seconds_remaining; time_t seconds_remaining; /* includes 30 second grace period */
} FD_TABLE_ENTRY; } FD_TABLE_ENTRY;
#define MAX_FD_ENTRIES 128 #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) void bvlc_maintenance_timer(unsigned seconds)
{ {
@@ -92,14 +88,19 @@ void bvlc_maintenance_timer(unsigned seconds)
for (i = 0; i < MAX_FD_ENTRIES; i++) { for (i = 0; i < MAX_FD_ENTRIES; i++) {
if (FD_Table[i].valid) 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( int bvlc_encode_bip_address(
@@ -401,7 +402,7 @@ int bvlc_encode_original_broadcast_npdu(
/* copy the source internet address to the BACnet address */ /* copy the source internet address to the BACnet address */
/* FIXME: IPv6? */ /* FIXME: IPv6? */
/* FIXME: is sockaddr_in host or network order? */ /* 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 */ BACNET_ADDRESS * src, /* returns the BACnet source address */
struct sockaddr_in * sin) /* source internet 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 */ /* copy the source internet address to the BACnet address */
/* FIXME: IPv6? */ /* FIXME: IPv6? */
/* FIXME: is sockaddr_in host or network order? */ /* 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 */ struct sockaddr_in * sin, /* source internet address */
BACNET_ADDRESS * src) /* returns the BACnet source address */ BACNET_ADDRESS * src) /* returns the BACnet source address */
{ {
@@ -449,17 +450,20 @@ void bvlc_bdt_forward_npdu(
int mtu_len = 0; int mtu_len = 0;
int bytes_sent = 0; int bytes_sent = 0;
unsigned i = 0; /* loop counter */ unsigned i = 0; /* loop counter */
struct sockaddr_in bip_dest;
/* assumes that the driver has already been initialized */ /* assumes that the driver has already been initialized */
if (BIP_Socket < 0) if (bip_socket() < 0)
return BIP_Socket; return;
mtu_len = bvlc_encode_forwarded_npdu( mtu_len = bvlc_encode_forwarded_npdu(
&mtu[0], &mtu[0],
src, sin,
npdu, npdu,
npdu_length); npdu_length);
/* load destination IP address */
bip_dest.sin_family = AF_INET;
/* loop through the BDT and send one to each entry, except us */ /* loop through the BDT and send one to each entry, except us */
for (i = 0; i < MAX_BBMD_ENTRIES; i++) 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 mask in the BDT entry and logically ORing it with the
BBMD address of the same entry. */ 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 */ /* 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)); (struct sockaddr *) bip_dest, sizeof(struct sockaddr));
} }
return bytes_sent; return bytes_sent;
} }
void bvlc_fdt_forward_npdu( void bvlc_fdt_forward_npdu(
struct sockaddr_in *sin, /* the source address */ struct sockaddr_in *sin, /* the source address */
uint8_t * npdu, /* returns the NPDU */ uint8_t * npdu, /* returns the NPDU */