diff --git a/bacnet-stack/arcnet.h b/bacnet-stack/arcnet.h index 1d15ee1b..324a0b77 100644 --- a/bacnet-stack/arcnet.h +++ b/bacnet-stack/arcnet.h @@ -38,6 +38,7 @@ #include #include #include "bacdef.h" +#include "npdu.h" /* specific defines for ARCNET */ #define MAX_HEADER (1+1+2+2+1+1+1+1) @@ -51,16 +52,10 @@ extern "C" { void arcnet_cleanup(void); bool arcnet_init(char *interface_name); -/* function to send a packet out the 802.2 socket */ -/* returns 0 on success, non-zero on failure */ - int arcnet_send(BACNET_ADDRESS * dest, /* destination address */ - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len); /* number of bytes of data */ - /* function to send a packet out the 802.2 socket */ /* returns zero on success, non-zero on failure */ int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ diff --git a/bacnet-stack/bip.c b/bacnet-stack/bip.c index d4c2ad25..36d0eb88 100644 --- a/bacnet-stack/bip.c +++ b/bacnet-stack/bip.c @@ -147,7 +147,7 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; int bytes_sent = 0; - BACNET_ADDRESS src; + BACNET_ADDRESS src = { 0 }; /* assumes that the driver has already been initialized */ if (BIP_Socket < 0) diff --git a/bacnet-stack/config.h b/bacnet-stack/config.h index eae3ffd1..4cb16690 100644 --- a/bacnet-stack/config.h +++ b/bacnet-stack/config.h @@ -2,6 +2,7 @@ #define CONFIG_H /* declare a single physical layer */ +/*#include "bip.h" */ /*#include "ethernet.h" */ /*#include "arcnet.h" */ /*#include "mstp.h" */ diff --git a/bacnet-stack/datalink.h b/bacnet-stack/datalink.h index 61022ccc..d2c3877d 100644 --- a/bacnet-stack/datalink.h +++ b/bacnet-stack/datalink.h @@ -40,20 +40,17 @@ #include "bacdef.h" #include "npdu.h" -#ifdef BACDL_ETHERNET +#if defined(BACDL_ETHERNET) #include "ethernet.h" -#endif - -#ifdef BACDL_ARCNET +#elif defined(BACDL_ARCNET) #include "arcnet.h" -#endif - -#ifdef BACDL_MSTP +#elif defined(BACDL_MSTP) #include "dlmstp.h" -#endif - -#ifdef BACDL_BIP +#elif defined(BACDL_BIP) #include "bip.h" +#else + #define MAX_HEADER (1 + 1 + 2) + #define MAX_MPDU (MAX_HEADER+MAX_PDU) #endif #ifdef __cplusplus diff --git a/bacnet-stack/demo/handler/s_dcc.c b/bacnet-stack/demo/handler/s_dcc.c index 60f53a97..ef86613f 100644 --- a/bacnet-stack/demo/handler/s_dcc.c +++ b/bacnet-stack/demo/handler/s_dcc.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "config.h" #include "config.h" #include "txbuf.h" diff --git a/bacnet-stack/demo/handler/s_ihave.c b/bacnet-stack/demo/handler/s_ihave.c index 76ca627d..db26c150 100644 --- a/bacnet-stack/demo/handler/s_ihave.c +++ b/bacnet-stack/demo/handler/s_ihave.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "config.h" #include "config.h" #include "txbuf.h" diff --git a/bacnet-stack/demo/server/Makefile b/bacnet-stack/demo/server/Makefile index 7d8f8581..30e4a5a4 100644 --- a/bacnet-stack/demo/server/Makefile +++ b/bacnet-stack/demo/server/Makefile @@ -6,10 +6,10 @@ BASEDIR = . #CFLAGS = -Wall -I. -O2 -g # Note: you can strip out symbols using the strip command # to get an idea of how big the compile really is. -#DEFINES = -DBACFILE=1 -DBACDL_ETHERNET=1 -#DEFINES = -DBACFILE=1 -DBACDL_ARCNET=1 -#DEFINES = -DBACFILE=1 -DBACDL_MSTP=1 -DEFINES = -DBACFILE=1 -DBACDL_BIP=1 -DPRINT_ENABLED=1 -DBIG_ENDIAN=0 +DEFINES = -DBACFILE=1 -DPRINT_ENABLED=1 -DBIG_ENDIAN=0 -DBACDL_ETHERNET=1 +#DEFINES = -DBACFILE=1 -DPRINT_ENABLED=1 -DBIG_ENDIAN=0 -DBACDL_ARCNET=1 +#DEFINES = -DBACFILE=1 -DPRINT_ENABLED=1 -DBIG_ENDIAN=0 -DBACDL_MSTP=1 +#DEFINES = -DBACFILE=1 -DPRINT_ENABLED=1 -DBIG_ENDIAN=0 -DBACDL_BIP=1 BACNET_PORT = ../../ports/linux BACNET_OBJECT = ../object BACNET_HANDLER = ../handler @@ -22,6 +22,8 @@ TARGET = bacserv SRCS = server.c \ $(BACNET_PORT)/bip-init.c \ + $(BACNET_PORT)/ethernet.c \ + $(BACNET_PORT)/arcnet.c \ $(BACNET_ROOT)/bip.c \ $(BACNET_HANDLER)/txbuf.c \ $(BACNET_HANDLER)/noserv.c \ diff --git a/bacnet-stack/demo/server/server.c b/bacnet-stack/demo/server/server.c index d26e020a..b2b21d78 100644 --- a/bacnet-stack/demo/server/server.c +++ b/bacnet-stack/demo/server/server.c @@ -112,8 +112,10 @@ int main(int argc, char *argv[]) /* allow the device ID to be set */ if (argc > 1) Device_Set_Object_Instance_Number(strtol(argv[1], NULL, 0)); + #if defined(BACDL_BIP) if (argc > 2) bip_set_port(strtol(argv[2], NULL, 0)); + #endif printf("BACnet Server Demo - Device #%u\r\n", Device_Object_Instance_Number()); Init_Service_Handlers(); diff --git a/bacnet-stack/ethernet.h b/bacnet-stack/ethernet.h index e4401015..12ed8632 100644 --- a/bacnet-stack/ethernet.h +++ b/bacnet-stack/ethernet.h @@ -38,6 +38,7 @@ #include #include #include "bacdef.h" +#include "npdu.h" /* specific defines for Ethernet */ #define MAX_HEADER (6+6+2+1+1+1) @@ -51,16 +52,10 @@ extern "C" { void ethernet_cleanup(void); bool ethernet_init(char *interface_name); -/* function to send a packet out the 802.2 socket */ -/* returns number of bytes sent on success, negative on failure */ - int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len); /* number of bytes of data */ - /* function to send a packet out the 802.2 socket */ /* returns number of bytes sent on success, negative on failure */ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ diff --git a/bacnet-stack/ports/linux/arcnet.c b/bacnet-stack/ports/linux/arcnet.c index d92f2135..aa32b91c 100644 --- a/bacnet-stack/ports/linux/arcnet.c +++ b/bacnet-stack/ports/linux/arcnet.c @@ -34,6 +34,7 @@ #include #include "bacdef.h" +#include "npdu.h" #include "arcnet.h" #include "net.h" @@ -182,18 +183,23 @@ bool arcnet_init(char *interface_name) return arcnet_valid(); } -/* function to send a packet out the socket */ +/* function to send a PDU out the socket */ /* returns number of bytes sent on success, negative on failure */ -int arcnet_send(BACNET_ADDRESS * dest, /* destination address */ - BACNET_ADDRESS * src, /* source address */ +int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) { /* number of bytes of data */ + BACNET_ADDRESS src = { 0 }; /* source address */ int bytes = 0; uint8_t mtu[512] = { 0 }; int mtu_len = 0; + int npdu_len = 0; struct archdr *pkt = (struct archdr *) mtu; + src.mac[0] = ARCNET_MAC_Address; + src.mac_len = 1; + /* don't waste time if the socket is not valid */ if (ARCNET_Sock_FD < 0) { fprintf(stderr, "arcnet: socket is invalid!\n"); @@ -206,24 +212,25 @@ int arcnet_send(BACNET_ADDRESS * dest, /* destination address */ fprintf(stderr, "arcnet: invalid destination MAC address!\n"); return -2; } - if (src->mac_len == 1) - pkt->hard.source = src->mac[0]; + if (src.mac_len == 1) + pkt->hard.source = src.mac[0]; else { fprintf(stderr, "arcnet: invalid source MAC address!\n"); return -3; } - if ((ARC_HDR_SIZE + pdu_len) > 512) { - fprintf(stderr, "arcnet: PDU is too big to send!\n"); - return -4; - } /* Logical PDU portion */ pkt->soft.raw[0] = 0xCD; /* SC for BACnet */ pkt->soft.raw[1] = 0x82; /* DSAP for BACnet */ pkt->soft.raw[2] = 0x82; /* SSAP for BACnet */ - pkt->soft.raw[3] = 0x03; /* Control byte in header */ - memcpy(&pkt->soft.raw[4], pdu, pdu_len); + pkt->soft.raw[3] = 0x03; /* LLC Control byte in header */ + npdu_len = npdu_encode_pdu(&pkt->soft.raw[4], dest, &src, npdu_data); /* packet length */ - mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + pdu_len; + mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + npdu_len + pdu_len; + if (mtu_len > 512) { + fprintf(stderr, "arcnet: PDU is too big to send!\n"); + return -4; + } + memcpy(&pkt->soft.raw[4+npdu_len], pdu, pdu_len); /* Send the packet */ bytes = sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0, @@ -237,22 +244,6 @@ int arcnet_send(BACNET_ADDRESS * dest, /* destination address */ return bytes; } -/* function to send a PDU out the socket */ -/* returns number of bytes sent on success, negative on failure */ -int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len) -{ /* number of bytes of data */ - BACNET_ADDRESS src = { 0 }; /* source address */ - - src.mac[0] = ARCNET_MAC_Address; - src.mac_len = 1; - return arcnet_send(dest, /* destination address */ - &src, /* source address */ - pdu, /* any data to be sent - may be null */ - pdu_len); /* number of bytes of data */ -} - /* receives an framed packet */ /* returns the number of octets in the PDU, or zero on failure */ uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */ diff --git a/bacnet-stack/ports/linux/ethernet.c b/bacnet-stack/ports/linux/ethernet.c index 203aaa1b..52141e1b 100644 --- a/bacnet-stack/ports/linux/ethernet.c +++ b/bacnet-stack/ports/linux/ethernet.c @@ -177,16 +177,23 @@ bool ethernet_init(char *interface_name) } /* function to send a packet out the 802.2 socket */ -/* returns bytes sent success, negative on failure */ -int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ - BACNET_ADDRESS * src, /* source address */ +/* returns number of bytes sent on success, negative on failure */ +int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) { /* number of bytes of data */ + int i = 0; /* counter */ int bytes = 0; - uint8_t mtu[MAX_MPDU] = { 0 }; + BACNET_ADDRESS src = { 0 }; /* source address for npdu*/ + uint8_t mtu[MAX_MPDU] = { 0 }; /* our buffer */ int mtu_len = 0; - int i = 0; + int len = 0; + + for (i = 0; i < 6; i++) { + src.mac[i] = Ethernet_MAC_Address[i]; + src.mac_len++; + } /* don't waste time if the socket is not valid */ if (eth802_sockfd < 0) { @@ -196,8 +203,7 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ /* load destination ethernet MAC address */ if (dest->mac_len == 6) { for (i = 0; i < 6; i++) { - mtu[mtu_len] = dest->mac[i]; - mtu_len++; + mtu[i] = dest->mac[i]; } } else { fprintf(stderr, "ethernet: invalid destination MAC address!\n"); @@ -205,28 +211,28 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ } /* load source ethernet MAC address */ - if (src->mac_len == 6) { + if (src.mac_len == 6) { for (i = 0; i < 6; i++) { - mtu[mtu_len] = src->mac[i]; - mtu_len++; + mtu[6 + i] = src.mac[i]; } } else { fprintf(stderr, "ethernet: invalid source MAC address!\n"); return -3; } - if ((14 + 3 + pdu_len) > MAX_MPDU) { + /* Logical PDU portion */ + mtu[14] = 0x82; /* DSAP for BACnet */ + mtu[15] = 0x82; /* SSAP for BACnet */ + mtu[16] = 0x03; /* Control byte in header */ + len = npdu_encode_pdu(&mtu[17], dest, &src, npdu_data); + mtu_len = 17 + len; + if ((mtu_len + pdu_len) > MAX_MPDU) { fprintf(stderr, "ethernet: PDU is too big to send!\n"); return -4; } - /* packet length */ - mtu_len += encode_unsigned16(&mtu[12], - 3 /*DSAP,SSAP,LLC */ + pdu_len); - /* Logical PDU portion */ - mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ - mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ - mtu[mtu_len++] = 0x03; /* Control byte in header */ memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; + /* packet length */ + encode_unsigned16(&mtu[12],mtu_len); /* Send the packet */ bytes = @@ -240,27 +246,6 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ return bytes; } -/* function to send a packet out the 802.2 socket */ -/* returns number of bytes sent on success, negative on failure */ -int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len) -{ /* number of bytes of data */ - int i = 0; /* counter */ - BACNET_ADDRESS src = { 0 }; /* source address */ - - for (i = 0; i < 6; i++) { - src.mac[i] = Ethernet_MAC_Address[i]; - src.mac_len++; - } - /* function to send a packet out the 802.2 socket */ - /* returns 1 on success, 0 on failure */ - return ethernet_send(dest, /* destination address */ - &src, /* source address */ - pdu, /* any data to be sent - may be null */ - pdu_len); /* number of bytes of data */ -} - /* receives an 802.2 framed packet */ /* returns the number of octets in the PDU, or zero on failure */ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */