diff --git a/bacnet-stack/bacapp.c b/bacnet-stack/bacapp.c index 749f364b..bbef53f4 100644 --- a/bacnet-stack/bacapp.c +++ b/bacnet-stack/bacapp.c @@ -488,7 +488,7 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, value->type.Signed_Int = long_value; } else if (tag_number == BACNET_APPLICATION_TAG_REAL) { double_value = strtod(argv, NULL); - value->type.Real = (float)double_value; + value->type.Real = (float) double_value; } else if (tag_number == BACNET_APPLICATION_TAG_DOUBLE) { double_value = strtod(argv, NULL); value->type.Double = double_value; diff --git a/bacnet-stack/bacenum.h b/bacnet-stack/bacenum.h index 431395c7..784ba856 100644 --- a/bacnet-stack/bacenum.h +++ b/bacnet-stack/bacenum.h @@ -644,13 +644,12 @@ typedef enum { /* procedures and constraints described in Clause 23. */ } BACNET_LIFE_SAFETY_STATE; -typedef enum -{ +typedef enum { SILENCED_STATE_UNSILENCED = 0, SILENCED_STATE_AUDIBLE_SILENCED = 1, SILENCED_STATE_VISIBLE_SILENCED = 2, SILENCED_STATE_ALL_SILENCED = 3, - /* Enumerated values 0-63 are reserved for definition by ASHRAE.*/ + /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ /* Enumerated values 64-65535 may be used by others subject to */ /* procedures and constraints described in Clause 23. */ } BACNET_SILENCED_STATE; @@ -886,29 +885,29 @@ typedef enum { } BACNET_SERVICES_SUPPORTED; typedef enum { - BVLC_RESULT = 0, - BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE = 1, - BVLC_READ_BROADCAST_DISTRIBUTION_TABLE = 2, - BVLC_READ_BROADCAST_DISTRIBUTION_TABLE_ACK = 3, - BVLC_FORWARDED_NPDU = 4, - BVLC_REGISTER_FOREIGN_DEVICE = 5, - BVLC_READ_FOREIGN_DEVICE_TABLE = 6, - BVLC_READ_FOREIGN_DEVICE_TABLE_ACK = 7, - BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY = 8, - BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK = 9, - BVLC_ORIGINAL_UNICAST_NPDU = 10, - BVLC_ORIGINAL_BROADCAST_NPDU = 11, - MAX_BVLC_FUNCTION = 12 + BVLC_RESULT = 0, + BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE = 1, + BVLC_READ_BROADCAST_DISTRIBUTION_TABLE = 2, + BVLC_READ_BROADCAST_DISTRIBUTION_TABLE_ACK = 3, + BVLC_FORWARDED_NPDU = 4, + BVLC_REGISTER_FOREIGN_DEVICE = 5, + BVLC_READ_FOREIGN_DEVICE_TABLE = 6, + BVLC_READ_FOREIGN_DEVICE_TABLE_ACK = 7, + BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY = 8, + BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK = 9, + BVLC_ORIGINAL_UNICAST_NPDU = 10, + BVLC_ORIGINAL_BROADCAST_NPDU = 11, + MAX_BVLC_FUNCTION = 12 } BACNET_BVLC_FUNCTION; typedef enum { - BVLC_RESULT_SUCCESSFUL_COMPLETION = 0x0000, - BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK = 0x0010, - BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK = 0x0020, - BVLC_RESULT_REGISTER_FOREIGN_DEVICE_NAK = 0X0030, - BVLC_RESULT_READ_FOREIGN_DEVICE_TABLE_NAK = 0x0040, - BVLC_RESULT_DELETE_FOREIGN_DEVICE_TABLE_ENTRY_NAK = 0x0050, - BVLC_RESULT_DISTRIBUTE_BROADCAST_TO_NETWORK_NAK = 0x0060 + BVLC_RESULT_SUCCESSFUL_COMPLETION = 0x0000, + BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK = 0x0010, + BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK = 0x0020, + BVLC_RESULT_REGISTER_FOREIGN_DEVICE_NAK = 0X0030, + BVLC_RESULT_READ_FOREIGN_DEVICE_TABLE_NAK = 0x0040, + BVLC_RESULT_DELETE_FOREIGN_DEVICE_TABLE_ENTRY_NAK = 0x0050, + BVLC_RESULT_DISTRIBUTE_BROADCAST_TO_NETWORK_NAK = 0x0060 } BACNET_BVLC_RESULT; /* Bit String Enumerations */ diff --git a/bacnet-stack/bip.c b/bacnet-stack/bip.c index 7554e9e4..f41cf881 100644 --- a/bacnet-stack/bip.c +++ b/bacnet-stack/bip.c @@ -254,7 +254,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ if (buf[0] != BVLL_TYPE_BACNET_IP) return 0; if ((buf[1] == BVLC_ORIGINAL_UNICAST_NPDU) || - (buf[1] == BVLC_ORIGINAL_BROADCAST_NPDU)) { + (buf[1] == BVLC_ORIGINAL_BROADCAST_NPDU)) { /* ignore messages from me */ if (sin.sin_addr.s_addr == BIP_Address.s_addr) pdu_len = 0; @@ -278,11 +278,11 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ pdu_len = 0; } } - #ifdef BBMD_ENABLED +#ifdef BBMD_ENABLED if (buf[1] < MAX_BVLC_FUNCTION) { - bbmd_handler(&buf[0], received_bytes, &sin); + bbmd_handler(&buf[0], received_bytes, &sin); } - #endif +#endif return pdu_len; } diff --git a/bacnet-stack/bvlc.c b/bacnet-stack/bvlc.c index be3b60aa..9760b149 100644 --- a/bacnet-stack/bvlc.c +++ b/bacnet-stack/bvlc.c @@ -30,34 +30,35 @@ based on this file might be covered by the GNU General Public License. ------------------------------------------- -####COPYRIGHTEND####*/ - +####COPYRIGHTEND####*/ + #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 */ - + /* Handle the BACnet Virtual Link Control (BVLC), which includes: BACnet Broadcast Management Device, Broadcast Distribution Table, and - Foreign Device Registration */ - -typedef struct -{ - /* true if valid entry - false if not */ - bool valid; - /* BACnet/IP 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; - + Foreign Device Registration */ + typedef struct { + + /* true if valid entry - false if not */ + bool valid; + + /* BACnet/IP 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; + #define MAX_BBMD_ENTRIES 128 -static BBMD_TABLE_ENTRY BBMD_Table[MAX_BBMD_ENTRIES]; - +static BBMD_TABLE_ENTRY BBMD_Table[MAX_BBMD_ENTRIES]; + /*Each device that registers as a foreign device shall be placed in an entry in the BBMD's Foreign Device Table (FDT). Each entry shall consist of the 6-octet B/IP address of the registrant; @@ -66,621 +67,563 @@ registration; and a 2-octet value representing the number of seconds remaining before the BBMD will purge the registrant's FDT entry if no re-registration occurs. This value will be initialized to the 2-octet Time-to-Live value supplied at the time of -registration.*/ -typedef struct -{ - bool valid; - /* BACnet/IP address */ - struct in_addr dest_address; - uint16_t dest_port; - /* seconds for valid entry lifetime */ - uint16_t time_to_live; - time_t seconds_remaining; /* includes 30 second grace period */ -} FD_TABLE_ENTRY; - +registration.*/ +typedef struct { + bool valid; + + /* BACnet/IP address */ + struct in_addr dest_address; + uint16_t dest_port; + + /* seconds for valid entry lifetime */ + uint16_t time_to_live; + time_t seconds_remaining; /* includes 30 second grace period */ + } FD_TABLE_ENTRY; + #define MAX_FD_ENTRIES 128 -static FD_TABLE_ENTRY FD_Table[MAX_FD_ENTRIES]; - -void bvlc_maintenance_timer(unsigned seconds) -{ - unsigned i = 0; - - for (i = 0; i < MAX_FD_ENTRIES; i++) { - if (FD_Table[i].valid) - { - 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; - } - } - } - } -} - -int bvlc_encode_bip_address( - uint8_t *pdu, - struct in_addr *address, /* in host format */ - uint16_t port) -{ - int len = 0; - - if (pdu) { - len = encode_unsigned32(&pdu[0], address->s_addr); - len += encode_unsigned16(&pdu[len], port); - } - - return len; -} - -int bvlc_decode_bip_address( - uint8_t *pdu, - struct in_addr *address, /* in host format */ - uint16_t *port) -{ - -} - - -/* used for both read and write entries */ +static FD_TABLE_ENTRY FD_Table[MAX_FD_ENTRIES]; + void bvlc_maintenance_timer(unsigned seconds) +{ + unsigned i = 0; + for (i = 0; i < MAX_FD_ENTRIES; i++) { + if (FD_Table[i].valid) + { + 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; + } + } + } + } + } + int bvlc_encode_bip_address( uint8_t * pdu, struct in_addr *address, /* in host format */ + uint16_t port) +{ + int len = 0; + if (pdu) { + len = encode_unsigned32(&pdu[0], address->s_addr); + len += encode_unsigned16(&pdu[len], port); + } + return len; + } + int bvlc_decode_bip_address( uint8_t * pdu, struct in_addr *address, /* in host format */ + uint16_t * port) +{ + } + +/* used for both read and write entries */ int bvlc_encode_address_entry( - uint8_t *pdu, - struct in_addr *address, - uint16_t port, - struct in_addr *mask) -{ - int len = 0; - - if (pdu) { - len = bvlc_encode_bip_address(pdu, address, port); - len += encode_unsigned32(&pdu[len], mask->s_addr); - } - - return len; -} - -int bvlc_encode_bvlc_result( - uint8_t *pdu, - BACNET_BVLC_RESULT result_code) -{ - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_RESULT; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 6); - encode_unsigned16(&pdu[4], result_code); - } - - return 6; -} - -int bvlc_encode_write_bdt_init( - uint8_t *pdu, - unsigned entries) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 4 + entries * 10); - len = 4; - } - - return len; -} - -int bvlc_encode_read_bdt( - uint8_t *pdu) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_READ_BROADCAST_DISTRIBUTION_TABLE; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 4); - len = 4; - } - - return len; -} - -int bvlc_encode_read_bdt_ack_init( - uint8_t *pdu, - unsigned entries) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_READ_BROADCAST_DISTRIBUTION_TABLE_ACK; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 4 + entries * 10); - len = 4; - } - - return len; -} - -int bvlc_encode_forwarded_npdu( - uint8_t *pdu, - BACNET_ADDRESS *src, - uint8_t *npdu, - unsigned npdu_length) -{ - int len = 0; - unsigned i; /* for loop counter */ - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_FORWARDED_NPDU; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 4+6+npdu_length); - len = 4; - for (i = 0; i < 6; i++) { - pdu[len] = src->adr[i]; - len++; - } - for (i = 0; i < npdu_length; i++) { - pdu[len] = npdu[i]; - len++; - } - } - - return len; -} - -int bvlc_encode_register_foreign_device( - uint8_t *pdu, - uint16_t time_to_live_seconds) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_REGISTER_FOREIGN_DEVICE; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 6); - encode_unsigned16(&pdu[2], time_to_live_seconds); - len = 6; - } - - return len; -} - -int bvlc_encode_read_fdt( - uint8_t *pdu) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_READ_FOREIGN_DEVICE_TABLE; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 4); - len = 4; - } - - return len; -} - -int bvlc_encode_read_fdt_ack_init( - uint8_t *pdu, - unsigned entries) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_READ_FOREIGN_DEVICE_TABLE_ACK; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 4 + entries * 10); - len = 4; - } - - return len; -} - -int bvlc_encode_delete_fdt_entry( - uint8_t *pdu, - struct in_addr *address, - uint16_t port) -{ - int len = 0; - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_READ_FOREIGN_DEVICE_TABLE; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - encode_unsigned16(&pdu[2], 10); - /* FDT Entry */ - encode_unsigned32(&pdu[0], address->s_addr); - encode_unsigned16(&pdu[4], port); - len = 10; - } - - return len; -} - -int bvlc_encode_distribute_broadcast_to_network( - uint8_t *pdu, - uint8_t *npdu, - unsigned npdu_length) -{ - int len = 0; /* return value */ - unsigned i; /* for loop counter */ - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - len = encode_unsigned16(&pdu[2], 4+npdu_length) + 2; - for (i = 0; i < npdu_length; i++) { - pdu[len] = npdu[i]; - len++; - } - } - - return len; -} - -int bvlc_encode_original_unicast_npdu( - uint8_t * pdu, - uint8_t * npdu, - unsigned npdu_length) -{ - int len = 0; /* return value */ - unsigned i = 0; /* loop counter */ - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_ORIGINAL_UNICAST_NPDU; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - len = encode_unsigned16(&pdu[2], 4+npdu_length) + 2; - for (i = 0; i < npdu_length; i++) { - pdu[len] = npdu[i]; - len++; - } - } - - return len; -} - -int bvlc_encode_original_broadcast_npdu( - uint8_t * pdu, - uint8_t * npdu, - unsigned npdu_length) -{ - int len = 0; /* return value */ - unsigned i = 0; /* loop counter */ - - if (pdu) { - pdu[0] = BVLL_TYPE_BACNET_IP; - pdu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; - /* The 2-octet BVLC Length field is the length, in octets, - of the entire BVLL message, including the two octets of the - length field itself, most significant octet first. */ - len = encode_unsigned16(&pdu[2], 4+npdu_length) + 2; - for (i = 0; i < npdu_length; i++) { - pdu[len] = npdu[i]; - len++; - } - } - - return len; -} - -/* 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( - BACNET_ADDRESS * src, /* returns the BACnet source address */ - struct sockaddr_in * sin) /* source internet address */ -{ - int len = 0; - - if (src && sin) - { - len = encode_unsigned32(&src->mac[0], sin->sin_addr.s_addr); - len += encode_unsigned16(&src->mac[4], sin->sin_port); - src->mac_len = len; - src->net = 0; - src->len = 0; - } - - return; -} - -/* 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( - struct sockaddr_in * sin, /* source internet address */ - BACNET_ADDRESS * src) /* returns the BACnet source address */ -{ - int len = 0; - - if (src && sin) - { - if (src->mac_len == 6) - { - len = decode_unsigned32(&src->mac[0], &sin->sin_addr.s_addr); - len += decode_unsigned16(&src->mac[4], &sin->sin_port); - } - } - - return; -} - -void bvlc_bdt_forward_npdu( - struct sockaddr_in *sin, /* the source address */ - uint8_t * npdu, /* the NPDU */ - uint16_t npdu_length) /* length of the NPDU */ -{ - uint8_t mtu[MAX_MPDU] = { 0 }; - 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; - - mtu_len = bvlc_encode_forwarded_npdu( - &mtu[0], - 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++) - { - if (BBMD_Table[i].valid) - { - /* The B/IP address to which the Forwarded-NPDU message is - sent is formed by inverting the broadcast distribution - 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, - (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 */ - uint16_t max_npdu) /* amount of space available in the NPDU */ -{ -} - -uint16_t bvlc_handler( - BACNET_ADDRESS * src, /* returns the source address */ - uint8_t * npdu, /* returns the NPDU */ - uint16_t max_npdu, /* amount of space available in the NPDU */ - unsigned timeout) /* number of milliseconds to wait for a packet */ -{ - int received_bytes; - uint8_t buf[MAX_MPDU] = { 0 }; /* data */ - uint16_t pdu_len = 0; /* return value */ - fd_set read_fds; - int max; - struct timeval select_timeout; - struct sockaddr_in sin = { -1 }; - socklen_t sin_len = sizeof(sin); - int function_type = 0; - - /* Make sure the socket is open */ - if (BIP_Socket < 0) - return 0; - - /* we could just use a non-blocking socket, but that consumes all - the CPU time. We can use a timeout; it is only supported as - a select. */ - if (timeout >= 1000) { - select_timeout.tv_sec = timeout / 1000; - select_timeout.tv_usec = - 1000 * (timeout - select_timeout.tv_sec * 1000); - } else { - select_timeout.tv_sec = 0; - select_timeout.tv_usec = 1000 * timeout; - } - FD_ZERO(&read_fds); - FD_SET((unsigned int) BIP_Socket, &read_fds); - max = BIP_Socket; - /* see if there is a packet for us */ - if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) - received_bytes = recvfrom(BIP_Socket, - (char *) &buf[0], MAX_MPDU, 0, - (struct sockaddr *) &sin, &sin_len); - else - return 0; - - /* See if there is a problem */ - if (received_bytes < 0) { - return 0; - } - - /* no problem, just no bytes */ - if (received_bytes == 0) - return 0; - - /* the signature of a BACnet/IP packet */ - if (buf[0] != BVLL_TYPE_BACNET_IP) - return 0; - function_type = buf[1]; - /* decode the length of the PDU - length is inclusive of BVLC */ - (void)decode_unsigned16(&buf[2], &npdu_len); - /* subtract off the BVLC header */ - npdu_len -= 4; - switch (function_type) - { - case BVLC_RESULT: - break; - case BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE: - /* Upon receipt of a BVLL Write-Broadcast-Distribution-Table - message, a BBMD shall attempt to create or replace its BDT, - depending on whether or not a BDT has previously existed. - If the creation or replacement of the BDT is successful, the BBMD - shall return a BVLC-Result message to the originating device with - a result code of X'0000'. Otherwise, the BBMD shall return a - BVLC-Result message to the originating device with a result code - of X'0010' indicating that the write attempt has failed. */ - break; - case BVLC_READ_BROADCAST_DISTRIBUTION_TABLE: - break; - case BVLC_READ_BROADCAST_DISTRIBUTION_TABLE_ACK: - break; - case BVLC_FORWARDED_NPDU: - /* Upon receipt of a BVLL Forwarded-NPDU message, a BBMD shall - process it according to whether it was received from a peer - BBMD as the result of a directed broadcast or a unicast - transmission. A BBMD may ascertain the method by which Forwarded- - NPDU messages will arrive by inspecting the broadcast distribution - mask field in its own BDT entry since all BDTs are required - to be identical. If the message arrived via directed broadcast, - it was also received by the other devices on the BBMD's subnet. In - this case the BBMD merely retransmits the message directly to each - foreign device currently in the BBMD's FDT. If the - message arrived via a unicast transmission it has not yet been - received by the other devices on the BBMD's subnet. In this case, - the message is sent to the devices on the BBMD's subnet using the - B/IP broadcast address as well as to each foreign device - currently in the BBMD's FDT. A BBMD on a subnet with no other - BACnet devices may omit the broadcast using the B/IP - broadcast address. The method by which a BBMD determines whether - or not other BACnet devices are present is a local matter. */ - bvlc_broadcast_npdu(&sin, &buf[4], npdu_len); - bvlc_fdt_forward_npdu(&sin, &buf[4], npdu_len); - break; - case BVLC_REGISTER_FOREIGN_DEVICE: - break; - case BVLC_READ_FOREIGN_DEVICE_TABLE: - break; - case BVLC_READ_FOREIGN_DEVICE_TABLE_ACK: - break; - case BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY: - break; - case BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK: - bvlc_broadcast_forward_npdu(&sin, &buf[4], npdu_len); - bvlc_fdt_forward_npdu(&sin, &buf[4], npdu_len); - break; - case BVLC_ORIGINAL_UNICAST_NPDU: - /* ignore messages from me */ - if (sin.sin_addr.s_addr == BIP_Address.s_addr) - npdu_len = 0; - else { - bvlc_internet_to_bacnet_address(src,&sin); - /* copy the buffer into the PDU */ - if (npdu_len < max_npdu) - memmove(&npdu[0], &buf[4], npdu_len); - /* ignore packets that are too large */ - /* clients should check my max-apdu first */ - else - npdu_len = 0; - } - break; - case BVLC_ORIGINAL_BROADCAST_NPDU: - /* Upon receipt of a BVLL Original-Broadcast-NPDU message, - a BBMD shall construct a BVLL Forwarded-NPDU message and - send it to each IP subnet in its BDT with the exception - of its own. The B/IP address to which the Forwarded-NPDU - message is sent is formed by inverting the broadcast - distribution mask in the BDT entry and logically ORing it - with the BBMD address of the same entry. This process - produces either the directed broadcast address of the remote - subnet or the unicast address of the BBMD on that subnet - depending on the contents of the broadcast distribution - mask. See J.4.3.2.. In addition, the received BACnet NPDU - shall be sent directly to each foreign device currently in - the BBMD's FDT also using the BVLL Forwarded-NPDU message. */ - bvlc_internet_to_bacnet_address(src,&sin); - /* copy the buffer into the PDU */ - if (npdu_len < max_npdu) - memmove(&npdu[0], &buf[4], npdu_len); - /* ignore packets that are too large */ - /* clients should check my max-apdu first */ - else - npdu_len = 0; - /* if BDT or FDT entries exist, Forward the NPDU */ - bvlc_bdt_forward_npdu(&sin, &buf[4], npdu_len); - bvlc_fdt_forward_npdu(&sin, &buf[4], npdu_len); - break; - default: - break; - } - - return npdu_len; -} - + uint8_t * pdu, + struct in_addr *address, uint16_t port, struct in_addr *mask) +{ + int len = 0; + if (pdu) { + len = bvlc_encode_bip_address(pdu, address, port); + len += encode_unsigned32(&pdu[len], mask->s_addr); + } + return len; + } + int bvlc_encode_bvlc_result( + uint8_t * pdu, BACNET_BVLC_RESULT result_code) +{ + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_RESULT; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 6); + encode_unsigned16(&pdu[4], result_code); + } + return 6; + } + int bvlc_encode_write_bdt_init( uint8_t * pdu, unsigned entries) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 4 + entries * 10); + len = 4; + } + return len; + } + int bvlc_encode_read_bdt( uint8_t * pdu) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_READ_BROADCAST_DISTRIBUTION_TABLE; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 4); + len = 4; + } + return len; + } + int bvlc_encode_read_bdt_ack_init( uint8_t * pdu, unsigned entries) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_READ_BROADCAST_DISTRIBUTION_TABLE_ACK; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 4 + entries * 10); + len = 4; + } + return len; + } + int bvlc_encode_forwarded_npdu( + uint8_t * pdu, + BACNET_ADDRESS * src, uint8_t * npdu, unsigned npdu_length) +{ + int len = 0; + unsigned i; /* for loop counter */ + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_FORWARDED_NPDU; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 4 + 6 + npdu_length); + len = 4; + for (i = 0; i < 6; i++) { + pdu[len] = src->adr[i]; + len++; + } + for (i = 0; i < npdu_length; i++) { + pdu[len] = npdu[i]; + len++; + } + } + return len; + } + int bvlc_encode_register_foreign_device( + uint8_t * pdu, uint16_t time_to_live_seconds) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_REGISTER_FOREIGN_DEVICE; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 6); + encode_unsigned16(&pdu[2], time_to_live_seconds); + len = 6; + } + return len; + } + int bvlc_encode_read_fdt( uint8_t * pdu) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_READ_FOREIGN_DEVICE_TABLE; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 4); + len = 4; + } + return len; + } + int bvlc_encode_read_fdt_ack_init( uint8_t * pdu, unsigned entries) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_READ_FOREIGN_DEVICE_TABLE_ACK; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 4 + entries * 10); + len = 4; + } + return len; + } + int bvlc_encode_delete_fdt_entry( + uint8_t * pdu, struct in_addr *address, uint16_t port) +{ + int len = 0; + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_READ_FOREIGN_DEVICE_TABLE; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + encode_unsigned16(&pdu[2], 10); + + /* FDT Entry */ + encode_unsigned32(&pdu[0], address->s_addr); + encode_unsigned16(&pdu[4], port); + len = 10; + } + return len; + } + int bvlc_encode_distribute_broadcast_to_network( + uint8_t * pdu, uint8_t * npdu, unsigned npdu_length) +{ + int len = 0; /* return value */ + unsigned i; /* for loop counter */ + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + len = encode_unsigned16(&pdu[2], 4 + npdu_length) + 2; + for (i = 0; i < npdu_length; i++) { + pdu[len] = npdu[i]; + len++; + } + } + return len; + } + int bvlc_encode_original_unicast_npdu( + uint8_t * pdu, uint8_t * npdu, unsigned npdu_length) +{ + int len = 0; /* return value */ + unsigned i = 0; /* loop counter */ + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_ORIGINAL_UNICAST_NPDU; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + len = encode_unsigned16(&pdu[2], 4 + npdu_length) + 2; + for (i = 0; i < npdu_length; i++) { + pdu[len] = npdu[i]; + len++; + } + } + return len; + } + int bvlc_encode_original_broadcast_npdu( + uint8_t * pdu, uint8_t * npdu, unsigned npdu_length) +{ + int len = 0; /* return value */ + unsigned i = 0; /* loop counter */ + if (pdu) { + pdu[0] = BVLL_TYPE_BACNET_IP; + pdu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; + + /* The 2-octet BVLC Length field is the length, in octets, + of the entire BVLL message, including the two octets of the + length field itself, most significant octet first. */ + len = encode_unsigned16(&pdu[2], 4 + npdu_length) + 2; + for (i = 0; i < npdu_length; i++) { + pdu[len] = npdu[i]; + len++; + } + } + return len; + } + + +/* 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( BACNET_ADDRESS * src, /* returns the BACnet source address */ + struct sockaddr_in *sin) /* source internet address */ +{ + int len = 0; + if (src && sin) + { + len = encode_unsigned32(&src->mac[0], sin->sin_addr.s_addr); + len += encode_unsigned16(&src->mac[4], sin->sin_port); + src->mac_len = len; + src->net = 0; + src->len = 0; + } + return; + } + + +/* 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( struct sockaddr_in *sin, /* source internet address */ + BACNET_ADDRESS * src) /* returns the BACnet source address */ +{ + int len = 0; + if (src && sin) + { + if (src->mac_len == 6) + { + len = decode_unsigned32(&src->mac[0], &sin->sin_addr.s_addr); + len += decode_unsigned16(&src->mac[4], &sin->sin_port); + } + } + return; + } + void bvlc_bdt_forward_npdu( struct sockaddr_in *sin, /* the source address */ + uint8_t * npdu, /* the NPDU */ + uint16_t npdu_length) /* length of the NPDU */ +{ + uint8_t mtu[MAX_MPDU] = { + 0}; + 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; + mtu_len = + bvlc_encode_forwarded_npdu( &mtu[0], 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++) + { + if (BBMD_Table[i].valid) + { + + /* The B/IP address to which the Forwarded-NPDU message is + sent is formed by inverting the broadcast distribution + 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, + (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 */ + uint16_t max_npdu) /* amount of space available in the NPDU */ + { + } uint16_t bvlc_handler( BACNET_ADDRESS * src, /* returns the source address */ + uint8_t * npdu, /* returns the NPDU */ + uint16_t max_npdu, /* amount of space available in the NPDU */ + unsigned timeout) /* number of milliseconds to wait for a packet */ + { + int received_bytes; + uint8_t buf[MAX_MPDU] = { + 0}; /* data */ + uint16_t pdu_len = 0; /* return value */ + fd_set read_fds; + int max; + struct timeval select_timeout; + struct sockaddr_in sin = { -1 }; + socklen_t sin_len = sizeof(sin); + int function_type = 0; + + /* Make sure the socket is open */ + if (BIP_Socket < 0) + return 0; + + /* we could just use a non-blocking socket, but that consumes all + the CPU time. We can use a timeout; it is only supported as + a select. */ + if (timeout >= 1000) { + select_timeout.tv_sec = timeout / 1000; + select_timeout.tv_usec = + 1000 * (timeout - select_timeout.tv_sec * 1000); + } else { + select_timeout.tv_sec = 0; + select_timeout.tv_usec = 1000 * timeout; + } + FD_ZERO(&read_fds); + FD_SET((unsigned int) BIP_Socket, &read_fds); + max = BIP_Socket; + + /* see if there is a packet for us */ + if (select(max + 1, &read_fds, NULL, NULL, + &select_timeout) > 0) + received_bytes = + recvfrom(BIP_Socket, (char *) &buf[0], MAX_MPDU, 0, + (struct sockaddr *) &sin, &sin_len); + + else + return 0; + + /* See if there is a problem */ + if (received_bytes < 0) { + return 0; + } + + /* no problem, just no bytes */ + if (received_bytes == 0) + return 0; + + /* the signature of a BACnet/IP packet */ + if (buf[0] != BVLL_TYPE_BACNET_IP) + return 0; + function_type = buf[1]; + + /* decode the length of the PDU - length is inclusive of BVLC */ + (void) decode_unsigned16(&buf[2], &npdu_len); + + /* subtract off the BVLC header */ + npdu_len -= 4; + switch (function_type) + { + case BVLC_RESULT: + break; + case BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE: + + /* Upon receipt of a BVLL Write-Broadcast-Distribution-Table + message, a BBMD shall attempt to create or replace its BDT, + depending on whether or not a BDT has previously existed. + If the creation or replacement of the BDT is successful, the BBMD + shall return a BVLC-Result message to the originating device with + a result code of X'0000'. Otherwise, the BBMD shall return a + BVLC-Result message to the originating device with a result code + of X'0010' indicating that the write attempt has failed. */ + break; + case BVLC_READ_BROADCAST_DISTRIBUTION_TABLE: + break; + case BVLC_READ_BROADCAST_DISTRIBUTION_TABLE_ACK: + break; + case BVLC_FORWARDED_NPDU: + + /* Upon receipt of a BVLL Forwarded-NPDU message, a BBMD shall + process it according to whether it was received from a peer + BBMD as the result of a directed broadcast or a unicast + transmission. A BBMD may ascertain the method by which Forwarded- + NPDU messages will arrive by inspecting the broadcast distribution + mask field in its own BDT entry since all BDTs are required + to be identical. If the message arrived via directed broadcast, + it was also received by the other devices on the BBMD's subnet. In + this case the BBMD merely retransmits the message directly to each + foreign device currently in the BBMD's FDT. If the + message arrived via a unicast transmission it has not yet been + received by the other devices on the BBMD's subnet. In this case, + the message is sent to the devices on the BBMD's subnet using the + B/IP broadcast address as well as to each foreign device + currently in the BBMD's FDT. A BBMD on a subnet with no other + BACnet devices may omit the broadcast using the B/IP + broadcast address. The method by which a BBMD determines whether + or not other BACnet devices are present is a local matter. */ + bvlc_broadcast_npdu(&sin, &buf[4], npdu_len); + bvlc_fdt_forward_npdu(&sin, &buf[4], npdu_len); + break; + case BVLC_REGISTER_FOREIGN_DEVICE: + break; + case BVLC_READ_FOREIGN_DEVICE_TABLE: + break; + case BVLC_READ_FOREIGN_DEVICE_TABLE_ACK: + break; + case BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY: + break; + case BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK: + bvlc_broadcast_forward_npdu(&sin, &buf[4], npdu_len); + bvlc_fdt_forward_npdu(&sin, &buf[4], npdu_len); + break; + case BVLC_ORIGINAL_UNICAST_NPDU: + + /* ignore messages from me */ + if (sin.sin_addr.s_addr == BIP_Address.s_addr) + npdu_len = 0; + + else { + bvlc_internet_to_bacnet_address(src, &sin); + + /* copy the buffer into the PDU */ + if (npdu_len < max_npdu) + memmove(&npdu[0], &buf[4], npdu_len); + + /* ignore packets that are too large */ + /* clients should check my max-apdu first */ + else + npdu_len = 0; + } + break; + case BVLC_ORIGINAL_BROADCAST_NPDU: + + /* Upon receipt of a BVLL Original-Broadcast-NPDU message, + a BBMD shall construct a BVLL Forwarded-NPDU message and + send it to each IP subnet in its BDT with the exception + of its own. The B/IP address to which the Forwarded-NPDU + message is sent is formed by inverting the broadcast + distribution mask in the BDT entry and logically ORing it + with the BBMD address of the same entry. This process + produces either the directed broadcast address of the remote + subnet or the unicast address of the BBMD on that subnet + depending on the contents of the broadcast distribution + mask. See J.4.3.2.. In addition, the received BACnet NPDU + shall be sent directly to each foreign device currently in + the BBMD's FDT also using the BVLL Forwarded-NPDU message. */ + bvlc_internet_to_bacnet_address(src, &sin); + + /* copy the buffer into the PDU */ + if (npdu_len < max_npdu) + memmove(&npdu[0], &buf[4], npdu_len); + + /* ignore packets that are too large */ + /* clients should check my max-apdu first */ + else + npdu_len = 0; + + /* if BDT or FDT entries exist, Forward the NPDU */ + bvlc_bdt_forward_npdu(&sin, &buf[4], npdu_len); + bvlc_fdt_forward_npdu(&sin, &buf[4], npdu_len); + break; + default: + break; + } + return npdu_len; + } + #ifdef TEST #include #include #include "ctest.h" - -void testBVLC(Test * pTest) -{ - (void)pTest; -} - + void testBVLC(Test * pTest) { + (void) pTest; + } #ifdef TEST_BBMD -int main(void) -{ - Test *pTest; - bool rc; - - pTest = ct_create("BACnet Virtual Link Control", NULL); - /* individual tests */ - rc = ct_addTestFunction(pTest, testBVLC); - assert(rc); - - /* configure output */ - ct_setStream(pTest, stdout); - ct_run(pTest); - (void) ct_report(pTest); - ct_destroy(pTest); - - return 0; -} -#endif /* TEST_BBMD */ -#endif /* TEST */ + int main(void) { + Test * pTest; + bool rc; + pTest = ct_create("BACnet Virtual Link Control", NULL); + + /* individual tests */ + rc = ct_addTestFunction(pTest, testBVLC); + assert(rc); + + /* configure output */ + ct_setStream(pTest, stdout); + ct_run(pTest); + (void) ct_report(pTest); + ct_destroy(pTest); + return 0; + } + +#endif /* TEST_BBMD */ +#endif /* TEST */ diff --git a/bacnet-stack/bvlc.h b/bacnet-stack/bvlc.h index 82971473..aa961d10 100644 --- a/bacnet-stack/bvlc.h +++ b/bacnet-stack/bvlc.h @@ -30,23 +30,24 @@ based on this file might be covered by the GNU General Public License. ------------------------------------------- -####COPYRIGHTEND####*/ +####COPYRIGHTEND####*/ #ifndef BVLC_H #define BVLC_H - + #include /* for standard integer types uint8_t etc. */ #include /* for the standard bool type. */ #include "bip.h" - + #ifdef __cplusplus -extern "C" { -#endif /* __cplusplus */ - -/* called from BACnet/IP handler */ -void bvlc_handler(uint8_t *buf, int len, struct sockaddr_in *sin); - +extern "C" { + +#endif /* __cplusplus */ + +/* called from BACnet/IP handler */ + void bvlc_handler(uint8_t * buf, int len, struct sockaddr_in *sin); + #ifdef __cplusplus -} -#endif /* __cplusplus */ +} +#endif /* __cplusplus */ -#endif +#endif /* */ diff --git a/bacnet-stack/dcc.c b/bacnet-stack/dcc.c index 4eec825d..afea71bb 100644 --- a/bacnet-stack/dcc.c +++ b/bacnet-stack/dcc.c @@ -174,7 +174,7 @@ int dcc_decode_service_request(uint8_t * apdu, &len_value_type); len += decode_unsigned(&apdu[len], len_value_type, &value32); if (timeDuration) - *timeDuration = (uint16_t)value32; + *timeDuration = (uint16_t) value32; } else if (timeDuration) *timeDuration = 0; /* Tag 1: enable_disable */ diff --git a/bacnet-stack/demo/object/ai.c b/bacnet-stack/demo/object/ai.c index 185248ce..c37c47f3 100644 --- a/bacnet-stack/demo/object/ai.c +++ b/bacnet-stack/demo/object/ai.c @@ -121,7 +121,7 @@ int Analog_Input_Encode_Property_APDU(uint8_t * apdu, case PROP_UNITS: apdu_len = encode_tagged_enumerated(&apdu[0], UNITS_PERCENT); break; - /* test case for signed encoding and decoding negative value correctly */ + /* test case for signed encoding and decoding negative value correctly */ case 9999: apdu_len = encode_tagged_signed(&apdu[0], -200); break; diff --git a/bacnet-stack/demo/object/bo.c b/bacnet-stack/demo/object/bo.c index 0bb5fcd2..4d40e04b 100644 --- a/bacnet-stack/demo/object/bo.c +++ b/bacnet-stack/demo/object/bo.c @@ -104,7 +104,8 @@ unsigned Binary_Output_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t object_instance) +static BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t + object_instance) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; unsigned index = 0; diff --git a/bacnet-stack/demo/object/device.c b/bacnet-stack/demo/object/device.c index dfa27c6b..093519a5 100644 --- a/bacnet-stack/demo/object/device.c +++ b/bacnet-stack/demo/object/device.c @@ -35,7 +35,7 @@ #include "bi.h" /* object list dependency */ #include "bo.h" /* object list dependency */ #include "ao.h" /* object list dependency */ -#include "lsp.h" /* object list dependency */ +#include "lsp.h" /* object list dependency */ #include "wp.h" /* write property handling */ #include "device.h" /* me */ #if BACFILE @@ -67,11 +67,11 @@ static char Description[16] = "server"; /* static uint8_t Max_Segments_Accepted = 0; */ /* VT_Classes_Supported */ /* Active_VT_Sessions */ -BACNET_TIME Local_Time; /* rely on OS, if there is one */ -BACNET_DATE Local_Date; /* rely on OS, if there is one */ +BACNET_TIME Local_Time; /* rely on OS, if there is one */ +BACNET_DATE Local_Date; /* rely on OS, if there is one */ /* BACnet UTC is inverse of standard offset - i.e. relative to local */ -static int UTC_Offset = 5; -static bool Daylight_Savings_Status = false; /* rely on OS */ +static int UTC_Offset = 5; +static bool Daylight_Savings_Status = false; /* rely on OS */ /* APDU_Segment_Timeout */ static uint16_t APDU_Timeout = 3000; static uint8_t Number_Of_APDU_Retries = 3; @@ -405,7 +405,7 @@ bool Device_Object_List_Identifier(unsigned array_index, } /* file objects */ #if BACFILE - if (!status) { + if (!status) { /* normalize the index since we know it is not the previous objects */ object_index -= Life_Safety_Point_Count(); @@ -541,32 +541,32 @@ int Device_Encode_Property_APDU(uint8_t * apdu, Application_Software_Version); apdu_len = encode_tagged_character_string(&apdu[0], &char_string); break; - /* FIXME: if you support time */ + /* FIXME: if you support time */ case PROP_LOCAL_TIME: /* FIXME: get the actual value */ Local_Time.hour = 7; Local_Time.min = 0; Local_Time.sec = 3; Local_Time.hundredths = 1; - apdu_len = - encode_tagged_time(&apdu[0], &Local_Time); - break; - /* FIXME: if you support time */ + apdu_len = encode_tagged_time(&apdu[0], &Local_Time); + break; + /* FIXME: if you support time */ case PROP_UTC_OFFSET: /* NOTE: if your UTC offset is -5, then BACnet UTC offset is 5 */ apdu_len = encode_tagged_signed(&apdu[0], UTC_Offset); break; - /* FIXME: if you support date */ + /* FIXME: if you support date */ case PROP_LOCAL_DATE: /* FIXME: get the actual value instead of April Fool's Day */ - Local_Date.year = 2006; /* AD */ - Local_Date.month = 4; /* 1=Jan */ - Local_Date.day = 1; /* 1..31 */ - Local_Date.wday = 6; /* 1=Monday */ - apdu_len = encode_tagged_date(&apdu[0],&Local_Date); + Local_Date.year = 2006; /* AD */ + Local_Date.month = 4; /* 1=Jan */ + Local_Date.day = 1; /* 1..31 */ + Local_Date.wday = 6; /* 1=Monday */ + apdu_len = encode_tagged_date(&apdu[0], &Local_Date); break; case PROP_DAYLIGHT_SAVINGS_STATUS: - apdu_len = encode_tagged_boolean(&apdu[0], Daylight_Savings_Status); + apdu_len = + encode_tagged_boolean(&apdu[0], Daylight_Savings_Status); break; case PROP_PROTOCOL_VERSION: apdu_len = @@ -601,18 +601,18 @@ int Device_Encode_Property_APDU(uint8_t * apdu, /* FIXME: indicate the objects that YOU support */ bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); if (Analog_Input_Count()) - bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); if (Analog_Output_Count()) - bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true); if (Binary_Input_Count()) - bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); if (Binary_Output_Count()) - bitstring_set_bit(&bit_string, OBJECT_BINARY_OUTPUT, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_OUTPUT, true); if (Life_Safety_Point_Count()) - bitstring_set_bit(&bit_string, OBJECT_LIFE_SAFETY_POINT, true); + bitstring_set_bit(&bit_string, OBJECT_LIFE_SAFETY_POINT, true); #if BACFILE if (bacfile_count()) - bitstring_set_bit(&bit_string, OBJECT_FILE, true); + bitstring_set_bit(&bit_string, OBJECT_FILE, true); #endif apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string); break; @@ -681,8 +681,7 @@ int Device_Encode_Property_APDU(uint8_t * apdu, /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_tagged_unsigned(&apdu[0], Database_Revision); + apdu_len = encode_tagged_unsigned(&apdu[0], Database_Revision); break; default: *error_class = ERROR_CLASS_PROPERTY; diff --git a/bacnet-stack/demo/object/lsp.c b/bacnet-stack/demo/object/lsp.c index 9f574ddb..358ed8e2 100644 --- a/bacnet-stack/demo/object/lsp.c +++ b/bacnet-stack/demo/object/lsp.c @@ -61,7 +61,8 @@ void Life_Safety_Point_Init(void) for (i = 0; i < MAX_LIFE_SAFETY_POINTS; i++) { Life_Safety_Point_Mode[i] = LIFE_SAFETY_MODE_DEFAULT; Life_Safety_Point_State[i] = LIFE_SAFETY_STATE_QUIET; - Life_Safety_Point_Silenced_State[i] = SILENCED_STATE_UNSILENCED; + Life_Safety_Point_Silenced_State[i] = + SILENCED_STATE_UNSILENCED; Life_Safety_Point_Operation[i] = LIFE_SAFETY_OPERATION_NONE; } } @@ -112,7 +113,8 @@ unsigned Life_Safety_Point_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_LIFE_SAFETY_STATE Life_Safety_Point_Present_Value(uint32_t object_instance) +static BACNET_LIFE_SAFETY_STATE Life_Safety_Point_Present_Value(uint32_t + object_instance) { BACNET_LIFE_SAFETY_STATE present_value = LIFE_SAFETY_STATE_QUIET; unsigned index = 0; @@ -156,12 +158,12 @@ int Life_Safety_Point_Encode_Property_APDU(uint8_t * apdu, unsigned object_index = 0; bool state = false; BACNET_RELIABILITY reliability = RELIABILITY_NO_FAULT_DETECTED; - - (void)array_index; /* currently not used */ + (void) array_index; /* currently not used */ Life_Safety_Point_Init(); switch (property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_LIFE_SAFETY_POINT, + apdu_len = + encode_tagged_object_id(&apdu[0], OBJECT_LIFE_SAFETY_POINT, object_instance); break; case PROP_OBJECT_NAME: @@ -190,7 +192,8 @@ int Life_Safety_Point_Encode_Property_APDU(uint8_t * apdu, apdu_len = encode_tagged_enumerated(&apdu[0], EVENT_STATE_NORMAL); break; case PROP_OUT_OF_SERVICE: - object_index = Life_Safety_Point_Instance_To_Index(object_instance); + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); state = Life_Safety_Point_Out_Of_Service[object_index]; apdu_len = encode_tagged_boolean(&apdu[0], state); break; @@ -200,24 +203,27 @@ int Life_Safety_Point_Encode_Property_APDU(uint8_t * apdu, apdu_len = encode_tagged_enumerated(&apdu[0], reliability); break; case PROP_MODE: - object_index = Life_Safety_Point_Instance_To_Index(object_instance); + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); mode = Life_Safety_Point_Mode[object_index]; apdu_len = encode_tagged_enumerated(&apdu[0], mode); break; case PROP_ACCEPTED_MODES: - for (mode = MIN_LIFE_SAFETY_MODE; mode < MAX_LIFE_SAFETY_MODE; mode++) - { - len = encode_tagged_enumerated(&apdu[apdu_len], mode); - apdu_len += len; + for (mode = MIN_LIFE_SAFETY_MODE; mode < MAX_LIFE_SAFETY_MODE; + mode++) { + len = encode_tagged_enumerated(&apdu[apdu_len], mode); + apdu_len += len; } break; case PROP_SILENCED: - object_index = Life_Safety_Point_Instance_To_Index(object_instance); + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); silenced_state = Life_Safety_Point_Silenced_State[object_index]; apdu_len = encode_tagged_enumerated(&apdu[0], silenced_state); break; case PROP_OPERATION_EXPECTED: - object_index = Life_Safety_Point_Instance_To_Index(object_instance); + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); operation = Life_Safety_Point_Operation[object_index]; apdu_len = encode_tagged_enumerated(&apdu[0], operation); break; @@ -248,10 +254,13 @@ bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, switch (wp_data->object_property) { case PROP_MODE: if (wp_data->value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - if ((wp_data->value.type.Enumerated >= MIN_LIFE_SAFETY_MODE) && + if ((wp_data->value.type.Enumerated >= MIN_LIFE_SAFETY_MODE) && (wp_data->value.type.Enumerated <= MIN_LIFE_SAFETY_MODE)) { - object_index = Life_Safety_Point_Instance_To_Index(wp_data->object_instance); - Life_Safety_Point_Mode[object_index] = wp_data->value.type.Enumerated; + object_index = + Life_Safety_Point_Instance_To_Index(wp_data-> + object_instance); + Life_Safety_Point_Mode[object_index] = + wp_data->value.type.Enumerated; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; @@ -265,7 +274,8 @@ bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, case PROP_OUT_OF_SERVICE: if (wp_data->value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { object_index = - Life_Safety_Point_Instance_To_Index(wp_data->object_instance); + Life_Safety_Point_Instance_To_Index(wp_data-> + object_instance); Life_Safety_Point_Out_Of_Service[object_index] = wp_data->value.type.Boolean; status = true; diff --git a/bacnet-stack/demo/object/lsp.h b/bacnet-stack/demo/object/lsp.h index 35f274b8..4c3ef01f 100644 --- a/bacnet-stack/demo/object/lsp.h +++ b/bacnet-stack/demo/object/lsp.h @@ -46,8 +46,9 @@ extern "C" { int32_t array_index, BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); - bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * + wp_data, BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" diff --git a/bacnet-stack/ports/win32/bip-init.c b/bacnet-stack/ports/win32/bip-init.c index b011d98a..cf34a450 100644 --- a/bacnet-stack/ports/win32/bip-init.c +++ b/bacnet-stack/ports/win32/bip-init.c @@ -159,7 +159,7 @@ bool bip_init(void) bip_set_socket(-1); return false; } - #if 0 +#if 0 /* probably only for Apple... */ /* rebind a port that is already in use. Note: all users of the port must specify this flag */ @@ -171,7 +171,7 @@ bool bip_init(void) bip_set_socket(-1); return false; } - #endif +#endif /* bind the socket to the local port number and IP address */ sin.sin_family = AF_INET; #if USE_INADDR