Feature/bip6 debug enable (#209)

* Added BACNET_BIP6_DEBUG environment variable to enhance BACnet/IPv6 debugging

* Added BACNET_BIP6_DEBUG environment variable to enhance BACnet/IPv6 debugging

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2022-01-08 11:34:11 -06:00
committed by GitHub
parent d77925a252
commit 889b7f9357
8 changed files with 188 additions and 50 deletions
+43 -12
View File
@@ -40,24 +40,52 @@
#include "bacnet/bacdcode.h" #include "bacnet/bacdcode.h"
#include "bacnet/config.h" #include "bacnet/config.h"
#include "bacnet/datalink/bip6.h" #include "bacnet/datalink/bip6.h"
#include "bacnet/basic/sys/debug.h"
#include "bacnet/basic/object/device.h" #include "bacnet/basic/object/device.h"
#include "bacnet/basic/bbmd6/h_bbmd6.h" #include "bacnet/basic/bbmd6/h_bbmd6.h"
#include "bacport.h" #include "bacport.h"
/* enable debugging */
static bool BIP6_Debug = false;
#if PRINT_ENABLED
#include <stdarg.h>
#include <stdio.h>
#define PRINTF(...) \
if (BIP6_Debug) { \
fprintf(stderr,__VA_ARGS__); \
fflush(stderr); \
}
#else
#define PRINTF(...)
#endif
/**
* @brief Print the IPv6 address with debug info
* @param str - debug info string
* @param addr - IPv4 address
*/
static void debug_print_ipv6(const char *str, const struct in6_addr *addr) static void debug_print_ipv6(const char *str, const struct in6_addr *addr)
{ {
debug_printf("BIP6: %s " PRINTF("BIP6: %s "
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%" "%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
"02x:%02x%02x\n", "%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
str, (int)addr->s6_addr[0], (int)addr->s6_addr[1], str, (int)addr->s6_addr[0], (int)addr->s6_addr[1],
(int)addr->s6_addr[2], (int)addr->s6_addr[3], (int)addr->s6_addr[4], (int)addr->s6_addr[2], (int)addr->s6_addr[3],
(int)addr->s6_addr[5], (int)addr->s6_addr[6], (int)addr->s6_addr[7], (int)addr->s6_addr[4], (int)addr->s6_addr[5],
(int)addr->s6_addr[8], (int)addr->s6_addr[9], (int)addr->s6_addr[10], (int)addr->s6_addr[6], (int)addr->s6_addr[7],
(int)addr->s6_addr[11], (int)addr->s6_addr[12], (int)addr->s6_addr[13], (int)addr->s6_addr[8], (int)addr->s6_addr[9],
(int)addr->s6_addr[10], (int)addr->s6_addr[11],
(int)addr->s6_addr[12], (int)addr->s6_addr[13],
(int)addr->s6_addr[14], (int)addr->s6_addr[15]); (int)addr->s6_addr[14], (int)addr->s6_addr[15]);
} }
/**
* @brief Enabled debug printing of BACnet/IPv4
*/
void bip6_debug_enable(void)
{
BIP6_Debug = true;
}
/** @file linux/bip6.c Initializes BACnet/IPv6 interface (Linux). */ /** @file linux/bip6.c Initializes BACnet/IPv6 interface (Linux). */
/* unix socket */ /* unix socket */
@@ -82,10 +110,12 @@ void bip6_set_interface(char *ifname)
exit(1); exit(1);
} }
ifa_tmp = ifa; ifa_tmp = ifa;
debug_printf("BIP6: seeking interface: %s\n", ifname); if (BIP6_Debug) {
PRINTF("BIP6: seeking interface: %s\n", ifname);
}
while (ifa_tmp) { while (ifa_tmp) {
if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6)) { if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6)) {
debug_printf("BIP6: found interface: %s\n", ifa_tmp->ifa_name); PRINTF("BIP6: found interface: %s\n", ifa_tmp->ifa_name);
} }
if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6) && if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6) &&
(strcasecmp(ifa_tmp->ifa_name, ifname) == 0)) { (strcasecmp(ifa_tmp->ifa_name, ifname) == 0)) {
@@ -105,7 +135,7 @@ void bip6_set_interface(char *ifname)
ifa_tmp = ifa_tmp->ifa_next; ifa_tmp = ifa_tmp->ifa_next;
} }
if (!found) { if (!found) {
debug_printf("BIP6: unable to set interface: %s\n", ifname); PRINTF("BIP6: unable to set interface: %s\n", ifname);
exit(1); exit(1);
} }
} }
@@ -350,6 +380,7 @@ uint16_t bip6_receive(
*/ */
void bip6_cleanup(void) void bip6_cleanup(void)
{ {
bvlc6_cleanup();
if (BIP6_Socket != -1) { if (BIP6_Socket != -1) {
close(BIP6_Socket); close(BIP6_Socket);
} }
@@ -391,7 +422,7 @@ bool bip6_init(char *ifname)
if (BIP6_Addr.port == 0) { if (BIP6_Addr.port == 0) {
bip6_set_port(0xBAC0U); bip6_set_port(0xBAC0U);
} }
debug_printf("BIP6: IPv6 UDP port: 0x%04X\n", htons(BIP6_Addr.port)); PRINTF("BIP6: IPv6 UDP port: 0x%04X\n", htons(BIP6_Addr.port));
if (BIP6_Broadcast_Addr.address[0] == 0) { if (BIP6_Broadcast_Addr.address[0] == 0) {
bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_SITE_LOCAL, 0, 0, bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_SITE_LOCAL, 0, 0,
0, 0, 0, 0, BIP6_MULTICAST_GROUP_ID); 0, 0, 0, 0, BIP6_MULTICAST_GROUP_ID);
+30 -3
View File
@@ -50,9 +50,28 @@ static SOCKET BIP6_Socket = INVALID_SOCKET;
static BACNET_IP6_ADDRESS BIP6_Addr; static BACNET_IP6_ADDRESS BIP6_Addr;
static BACNET_IP6_ADDRESS BIP6_Broadcast_Addr; static BACNET_IP6_ADDRESS BIP6_Broadcast_Addr;
/* enable debugging */
static bool BIP6_Debug = false;
#if PRINT_ENABLED
#include <stdarg.h>
#include <stdio.h>
#define PRINTF(...) \
if (BIP6_Debug) { \
fprintf(stderr,__VA_ARGS__); \
fflush(stderr); \
}
#else
#define PRINTF(...)
#endif
/**
* @brief Print the IPv6 address with debug info
* @param str - debug info string
* @param addr - IPv4 address
*/
static void debug_print_ipv6(const char *str, const struct in6_addr *addr) static void debug_print_ipv6(const char *str, const struct in6_addr *addr)
{ {
debug_printf("BIP6: %s " PRINTF("BIP6: %s "
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%" "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%"
"02x:%02x%02x\n", "02x:%02x%02x\n",
str, (int)addr->s6_addr[0], (int)addr->s6_addr[1], str, (int)addr->s6_addr[0], (int)addr->s6_addr[1],
@@ -63,6 +82,14 @@ static void debug_print_ipv6(const char *str, const struct in6_addr *addr)
(int)addr->s6_addr[14], (int)addr->s6_addr[15]); (int)addr->s6_addr[14], (int)addr->s6_addr[15]);
} }
/**
* @brief Enabled debug printing of BACnet/IPv4
*/
void bip6_debug_enable(void)
{
BIP6_Debug = true;
}
static LPSTR PrintError(int ErrorCode) static LPSTR PrintError(int ErrorCode)
{ {
static char Message[1024]; static char Message[1024];
@@ -104,7 +131,7 @@ void bip6_set_interface(char *ifname)
Hints.ai_protocol = IPPROTO_UDP; Hints.ai_protocol = IPPROTO_UDP;
Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE; Hints.ai_flags = AI_NUMERICHOST | AI_PASSIVE;
snprintf(port, sizeof(port), "%u", BIP6_Addr.port); snprintf(port, sizeof(port), "%u", BIP6_Addr.port);
debug_printf("BIP6: getaddrinfo - IPv6 address %s port %s\n", ifname, port); PRINTF("BIP6: getaddrinfo - IPv6 address %s port %s\n", ifname, port);
RetVal = getaddrinfo(ifname, &port[0], &Hints, &AddrInfo); RetVal = getaddrinfo(ifname, &port[0], &Hints, &AddrInfo);
if (RetVal != 0) { if (RetVal != 0) {
fprintf(stderr, "BIP6: getaddrinfo failed with error %d: %s\n", RetVal, fprintf(stderr, "BIP6: getaddrinfo failed with error %d: %s\n", RetVal,
@@ -512,7 +539,7 @@ bool bip6_init(char *ifname)
if (BIP6_Addr.port == 0) { if (BIP6_Addr.port == 0) {
bip6_set_port(0xBAC0U); bip6_set_port(0xBAC0U);
} }
debug_printf("BIP6: IPv6 UDP port: 0x%04X\n", BIP6_Addr.port); PRINTF("BIP6: IPv6 UDP port: 0x%04X\n", BIP6_Addr.port);
bip6_set_interface(ifname); bip6_set_interface(ifname);
if (BIP6_Broadcast_Addr.address[0] == 0) { if (BIP6_Broadcast_Addr.address[0] == 0) {
bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_LINK_LOCAL, 0, 0, bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_LINK_LOCAL, 0, 0,
+66 -32
View File
@@ -44,6 +44,20 @@
#include "bacnet/basic/bbmd6/vmac.h" #include "bacnet/basic/bbmd6/vmac.h"
#include "bacnet/basic/bbmd6/h_bbmd6.h" #include "bacnet/basic/bbmd6/h_bbmd6.h"
static bool BVLC6_Debug;
#if PRINT_ENABLED
#include <stdarg.h>
#include <stdio.h>
#define PRINTF(...) \
if (BVLC6_Debug) { \
fprintf(stderr,__VA_ARGS__); \
fflush(stderr); \
}
#else
#define PRINTF(...)
#endif
/** result from a client request */ /** result from a client request */
static uint16_t BVLC6_Result_Code = BVLC6_RESULT_SUCCESSFUL_COMPLETION; static uint16_t BVLC6_Result_Code = BVLC6_RESULT_SUCCESSFUL_COMPLETION;
/** incoming function */ /** incoming function */
@@ -68,6 +82,15 @@ static BACNET_IP6_BROADCAST_DISTRIBUTION_TABLE_ENTRY
static BACNET_IP6_FOREIGN_DEVICE_TABLE_ENTRY FD_Table[MAX_FD6_ENTRIES]; static BACNET_IP6_FOREIGN_DEVICE_TABLE_ENTRY FD_Table[MAX_FD6_ENTRIES];
#endif #endif
/**
* @brief Enable debugging if print is enabled
*/
void bvlc6_debug_enable(void)
{
BVLC6_Debug = true;
VMAC_Debug_Enable();
}
/** A timer function that is called about once a second. /** A timer function that is called about once a second.
* *
* @param seconds - number of elapsed seconds since the last call * @param seconds - number of elapsed seconds since the last call
@@ -163,10 +186,21 @@ static bool bbmd6_add_vmac(uint32_t device_id, BACNET_IP6_ADDRESS *addr)
vmac = VMAC_Find_By_Key(device_id); vmac = VMAC_Find_By_Key(device_id);
if (vmac) { if (vmac) {
/* already exists - replace? */ /* already exists - replace? */
PRINTF("VMAC existing %u [", (unsigned int)device_id);
for (unsigned i = 0; i < vmac->mac_len; i++) {
PRINTF("%02X", vmac->mac[i]);
}
PRINTF("]\n");
PRINTF("VMAC ignoring %u [", (unsigned int)device_id);
for (unsigned i = 0; i < IP6_ADDRESS_MAX; i++) {
PRINTF("%02X", addr->address[i]);
}
PRINTF("%04X", addr->port);
PRINTF("]\n");
} else if (bbmd6_address_to_vmac(&new_vmac, addr)) { } else if (bbmd6_address_to_vmac(&new_vmac, addr)) {
/* new entry - add it! */ /* new entry - add it! */
status = VMAC_Add(device_id, &new_vmac); status = VMAC_Add(device_id, &new_vmac);
debug_printf("BVLC6: Adding VMAC %lu.\n", (unsigned long)device_id); PRINTF("BVLC6: Adding VMAC %lu.\n", (unsigned long)device_id);
} }
} }
@@ -216,7 +250,7 @@ static bool bbmd6_address_from_bacnet_address(
if (status) { if (status) {
vmac = VMAC_Find_By_Key(device_id); vmac = VMAC_Find_By_Key(device_id);
if (vmac) { if (vmac) {
debug_printf("BVLC6: Found VMAC %lu (len=%u).\n", PRINTF("BVLC6: Found VMAC %lu (len=%u).\n",
(unsigned long)device_id, (unsigned)vmac->mac_len); (unsigned long)device_id, (unsigned)vmac->mac_len);
status = bbmd6_address_from_vmac(addr, vmac); status = bbmd6_address_from_vmac(addr, vmac);
if (vmac_src) { if (vmac_src) {
@@ -264,13 +298,13 @@ int bvlc6_send_pdu(BACNET_ADDRESS *dest,
vmac_src = Device_Object_Instance_Number(); vmac_src = Device_Object_Instance_Number();
mtu_len = bvlc6_encode_distribute_broadcast_to_network( mtu_len = bvlc6_encode_distribute_broadcast_to_network(
mtu, sizeof(mtu), vmac_src, pdu, pdu_len); mtu, sizeof(mtu), vmac_src, pdu, pdu_len);
debug_printf("BVLC6: Sent Distribute-Broadcast-to-Network.\n"); PRINTF("BVLC6: Sent Distribute-Broadcast-to-Network.\n");
} else { } else {
bip6_get_broadcast_addr(&bvlc_dest); bip6_get_broadcast_addr(&bvlc_dest);
vmac_src = Device_Object_Instance_Number(); vmac_src = Device_Object_Instance_Number();
mtu_len = bvlc6_encode_original_broadcast( mtu_len = bvlc6_encode_original_broadcast(
mtu, sizeof(mtu), vmac_src, pdu, pdu_len); mtu, sizeof(mtu), vmac_src, pdu, pdu_len);
debug_printf("BVLC6: Sent Original-Broadcast-NPDU.\n"); PRINTF("BVLC6: Sent Original-Broadcast-NPDU.\n");
} }
} else if ((dest->net > 0) && (dest->len == 0)) { } else if ((dest->net > 0) && (dest->len == 0)) {
/* net > 0 and net < 65535 are network specific broadcast if len = 0 */ /* net > 0 and net < 65535 are network specific broadcast if len = 0 */
@@ -283,17 +317,17 @@ int bvlc6_send_pdu(BACNET_ADDRESS *dest,
vmac_src = Device_Object_Instance_Number(); vmac_src = Device_Object_Instance_Number();
mtu_len = bvlc6_encode_original_broadcast( mtu_len = bvlc6_encode_original_broadcast(
mtu, sizeof(mtu), vmac_src, pdu, pdu_len); mtu, sizeof(mtu), vmac_src, pdu, pdu_len);
debug_printf("BVLC6: Sent Original-Broadcast-NPDU.\n"); PRINTF("BVLC6: Sent Original-Broadcast-NPDU.\n");
} else if (dest->mac_len == 3) { } else if (dest->mac_len == 3) {
/* valid unicast */ /* valid unicast */
bbmd6_address_from_bacnet_address(&bvlc_dest, &vmac_dst, dest); bbmd6_address_from_bacnet_address(&bvlc_dest, &vmac_dst, dest);
debug_printf("BVLC6: Sending to VMAC %lu.\n", (unsigned long)vmac_dst); PRINTF("BVLC6: Sending to VMAC %lu.\n", (unsigned long)vmac_dst);
vmac_src = Device_Object_Instance_Number(); vmac_src = Device_Object_Instance_Number();
mtu_len = bvlc6_encode_original_unicast( mtu_len = bvlc6_encode_original_unicast(
mtu, sizeof(mtu), vmac_src, vmac_dst, pdu, pdu_len); mtu, sizeof(mtu), vmac_src, vmac_dst, pdu, pdu_len);
debug_printf("BVLC6: Sent Original-Unicast-NPDU.\n"); PRINTF("BVLC6: Sent Original-Unicast-NPDU.\n");
} else { } else {
debug_printf("BVLC6: Send failure. Invalid Address.\n"); PRINTF("BVLC6: Send failure. Invalid Address.\n");
return -1; return -1;
} }
@@ -479,7 +513,7 @@ static void bbmd6_virtual_address_resolution_handler(
uint32_t vmac_me = 0; uint32_t vmac_me = 0;
if (addr && pdu) { if (addr && pdu) {
debug_printf("BIP6: Received Virtual-Address-Resolution.\n"); PRINTF("BIP6: Received Virtual-Address-Resolution.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
} else { } else {
@@ -513,7 +547,7 @@ static void bbmd6_virtual_address_resolution_ack_handler(
uint32_t vmac_dst = 0; uint32_t vmac_dst = 0;
if (addr && pdu) { if (addr && pdu) {
debug_printf("BIP6: Received Virtual-Address-Resolution-ACK.\n"); PRINTF("BIP6: Received Virtual-Address-Resolution-ACK.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
} else { } else {
@@ -542,7 +576,7 @@ static void bbmd6_address_resolution_handler(
uint32_t vmac_me = 0; uint32_t vmac_me = 0;
if (addr && pdu) { if (addr && pdu) {
debug_printf("BIP6: Received Address-Resolution.\n"); PRINTF("BIP6: Received Address-Resolution.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
} else { } else {
@@ -577,7 +611,7 @@ static void bbmd6_address_resolution_ack_handler(
uint32_t vmac_dst = 0; uint32_t vmac_dst = 0;
if (addr && pdu) { if (addr && pdu) {
debug_printf("BIP6: Received Address-Resolution-ACK.\n"); PRINTF("BIP6: Received Address-Resolution-ACK.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
} else { } else {
@@ -638,7 +672,7 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
incoming messages. */ incoming messages. */
bbmd6_add_vmac(vmac_src, addr); bbmd6_add_vmac(vmac_src, addr);
bvlc6_vmac_address_set(src, vmac_src); bvlc6_vmac_address_set(src, vmac_src);
debug_printf( PRINTF(
"BIP6: Received Result Code=%d\n", BVLC6_Result_Code); "BIP6: Received Result Code=%d\n", BVLC6_Result_Code);
} }
break; break;
@@ -657,10 +691,10 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
case BVLC6_ORIGINAL_UNICAST_NPDU: case BVLC6_ORIGINAL_UNICAST_NPDU:
/* This message is used to send directed NPDUs to /* This message is used to send directed NPDUs to
another B/IPv6 node or router. */ another B/IPv6 node or router. */
debug_printf("BIP6: Received Original-Unicast-NPDU.\n"); PRINTF("BIP6: Received Original-Unicast-NPDU.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
debug_printf("BIP6: Original-Unicast-NPDU is me!.\n"); PRINTF("BIP6: Original-Unicast-NPDU is me!.\n");
} else { } else {
function_len = bvlc6_decode_original_unicast( function_len = bvlc6_decode_original_unicast(
pdu, pdu_len, &vmac_src, &vmac_dst, NULL, 0, &npdu_len); pdu, pdu_len, &vmac_src, &vmac_dst, NULL, 0, &npdu_len);
@@ -673,20 +707,20 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
bvlc6_vmac_address_set(src, vmac_src); bvlc6_vmac_address_set(src, vmac_src);
offset = header_len + (function_len - npdu_len); offset = header_len + (function_len - npdu_len);
} else { } else {
debug_printf("BIP6: Original-Unicast-NPDU: " PRINTF("BIP6: Original-Unicast-NPDU: "
"VMAC is not me!\n"); "VMAC is not me!\n");
} }
} else { } else {
debug_printf( PRINTF(
"BIP6: Original-Unicast-NPDU: Unable to decode!\n"); "BIP6: Original-Unicast-NPDU: Unable to decode!\n");
} }
} }
break; break;
case BVLC6_ORIGINAL_BROADCAST_NPDU: case BVLC6_ORIGINAL_BROADCAST_NPDU:
debug_printf("BIP6: Received Original-Broadcast-NPDU.\n"); PRINTF("BIP6: Received Original-Broadcast-NPDU.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
debug_printf("BIP6: Original-Broadcast-NPDU is me!\n"); PRINTF("BIP6: Original-Broadcast-NPDU is me!\n");
} else { } else {
function_len = bvlc6_decode_original_broadcast( function_len = bvlc6_decode_original_broadcast(
pdu, pdu_len, &vmac_src, NULL, 0, &npdu_len); pdu, pdu_len, &vmac_src, NULL, 0, &npdu_len);
@@ -705,21 +739,21 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
npdu = &mtu[offset]; npdu = &mtu[offset];
if (npdu_confirmed_service(npdu, npdu_len)) { if (npdu_confirmed_service(npdu, npdu_len)) {
offset = 0; offset = 0;
debug_printf( PRINTF(
"BIP6: Original-Broadcast-NPDU: " "BIP6: Original-Broadcast-NPDU: "
"Confirmed Service! Discard!"); "Confirmed Service! Discard!");
} }
} else { } else {
debug_printf("BIP6: Original-Broadcast-NPDU: Unable to " PRINTF("BIP6: Original-Broadcast-NPDU: Unable to "
"decode!\n"); "decode!\n");
} }
} }
break; break;
case BVLC6_FORWARDED_NPDU: case BVLC6_FORWARDED_NPDU:
debug_printf("BIP6: Received Forwarded-NPDU.\n"); PRINTF("BIP6: Received Forwarded-NPDU.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
debug_printf("BIP6: Forwarded-NPDU is me!\n"); PRINTF("BIP6: Forwarded-NPDU is me!\n");
} else { } else {
function_len = bvlc6_decode_forwarded_npdu(pdu, pdu_len, function_len = bvlc6_decode_forwarded_npdu(pdu, pdu_len,
&vmac_src, &fwd_address, NULL, 0, &npdu_len); &vmac_src, &fwd_address, NULL, 0, &npdu_len);
@@ -731,7 +765,7 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
bvlc6_vmac_address_set(src, vmac_src); bvlc6_vmac_address_set(src, vmac_src);
offset = header_len + (function_len - npdu_len); offset = header_len + (function_len - npdu_len);
} else { } else {
debug_printf( PRINTF(
"BIP6: Forwarded-NPDU: Unable to decode!\n"); "BIP6: Forwarded-NPDU: Unable to decode!\n");
} }
} }
@@ -761,7 +795,7 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
if (send_result) { if (send_result) {
vmac_src = Device_Object_Instance_Number(); vmac_src = Device_Object_Instance_Number();
bvlc6_send_result(addr, vmac_src, result_code); bvlc6_send_result(addr, vmac_src, result_code);
debug_printf("BIP6: sent result code=%d\n", result_code); PRINTF("BIP6: sent result code=%d\n", result_code);
} }
} }
@@ -820,7 +854,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
incoming messages. */ incoming messages. */
bbmd6_add_vmac(vmac_src, addr); bbmd6_add_vmac(vmac_src, addr);
bvlc6_vmac_address_set(src, vmac_src); bvlc6_vmac_address_set(src, vmac_src);
debug_printf( PRINTF(
"BIP6: Received Result Code=%d\n", BVLC6_Result_Code); "BIP6: Received Result Code=%d\n", BVLC6_Result_Code);
} }
break; break;
@@ -839,9 +873,9 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
case BVLC6_ORIGINAL_UNICAST_NPDU: case BVLC6_ORIGINAL_UNICAST_NPDU:
/* This message is used to send directed NPDUs to /* This message is used to send directed NPDUs to
another B/IPv6 node or router. */ another B/IPv6 node or router. */
debug_printf("BIP6: Received Original-Unicast-NPDU.\n"); PRINTF("BIP6: Received Original-Unicast-NPDU.\n");
if (bbmd6_address_match_self(addr)) { if (bbmd6_address_match_self(addr)) {
debug_printf("BIP6: Dropped Original-Unicast-NPDU.\n"); PRINTF("BIP6: Dropped Original-Unicast-NPDU.\n");
/* ignore messages from my IPv6 address */ /* ignore messages from my IPv6 address */
offset = 0; offset = 0;
} else { } else {
@@ -860,7 +894,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
} }
break; break;
case BVLC6_ORIGINAL_BROADCAST_NPDU: case BVLC6_ORIGINAL_BROADCAST_NPDU:
debug_printf("BIP6: Received Original-Broadcast-NPDU.\n"); PRINTF("BIP6: Received Original-Broadcast-NPDU.\n");
function_len = bvlc6_decode_original_broadcast( function_len = bvlc6_decode_original_broadcast(
pdu, pdu_len, &vmac_src, NULL, 0, &npdu_len); pdu, pdu_len, &vmac_src, NULL, 0, &npdu_len);
if (function_len) { if (function_len) {
@@ -873,7 +907,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
network layer. */ network layer. */
if (npdu_confirmed_service(npdu, npdu_len)) { if (npdu_confirmed_service(npdu, npdu_len)) {
offset = 0; offset = 0;
debug_printf( PRINTF(
"BIP6: Original-Broadcast-NPDU: " "BIP6: Original-Broadcast-NPDU: "
"Confirmed Service! Discard!"); "Confirmed Service! Discard!");
} else { } else {
@@ -900,7 +934,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
} }
break; break;
case BVLC6_FORWARDED_NPDU: case BVLC6_FORWARDED_NPDU:
debug_printf("BIP6: Received Forwarded-NPDU.\n"); PRINTF("BIP6: Received Forwarded-NPDU.\n");
function_len = bvlc6_decode_forwarded_npdu( function_len = bvlc6_decode_forwarded_npdu(
pdu, pdu_len, &vmac_src, &fwd_address, NULL, 0, &npdu_len); pdu, pdu_len, &vmac_src, &fwd_address, NULL, 0, &npdu_len);
if (function_len) { if (function_len) {
@@ -959,7 +993,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
if (send_result) { if (send_result) {
vmac_src = Device_Object_Instance_Number(); vmac_src = Device_Object_Instance_Number();
bvlc6_send_result(addr, vmac_src, result_code); bvlc6_send_result(addr, vmac_src, result_code);
debug_printf("BIP6: sent result code=%d\n", result_code); PRINTF("BIP6: sent result code=%d\n", result_code);
} }
} }
+4
View File
@@ -84,6 +84,10 @@ extern "C" {
void bvlc6_maintenance_timer( void bvlc6_maintenance_timer(
uint16_t seconds); uint16_t seconds);
BACNET_STACK_EXPORT
void bvlc6_debug_enable(
void);
BACNET_STACK_EXPORT BACNET_STACK_EXPORT
void bvlc6_cleanup(void); void bvlc6_cleanup(void);
+34 -3
View File
@@ -42,6 +42,28 @@
/* me! */ /* me! */
#include "bacnet/basic/bbmd6/vmac.h" #include "bacnet/basic/bbmd6/vmac.h"
/* enable debugging */
static bool VMAC_Debug = false;
#if PRINT_ENABLED
#include <stdarg.h>
#include <stdio.h>
#define PRINTF(...) \
if (VMAC_Debug) { \
fprintf(stderr,__VA_ARGS__); \
fflush(stderr); \
}
#else
#define PRINTF(...)
#endif
/**
* @brief Enable debugging if print is enabled
*/
void VMAC_Debug_Enable(void)
{
VMAC_Debug = true;
}
/** @file /** @file
Handle VMAC address binding */ Handle VMAC address binding */
@@ -90,7 +112,7 @@ bool VMAC_Add(uint32_t device_id, struct vmac_data *src)
index = Keylist_Data_Add(VMAC_List, device_id, pVMAC); index = Keylist_Data_Add(VMAC_List, device_id, pVMAC);
if (index >= 0) { if (index >= 0) {
status = true; status = true;
printf("VMAC %u added.\n", (unsigned int)device_id); PRINTF("VMAC %u added.\n", (unsigned int)device_id);
} }
} }
} }
@@ -234,11 +256,20 @@ bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id)
void VMAC_Cleanup(void) void VMAC_Cleanup(void)
{ {
struct vmac_data *pVMAC; struct vmac_data *pVMAC;
uint32_t device_id;
const int index = 0;
if (VMAC_List) { if (VMAC_List) {
do { do {
pVMAC = Keylist_Data_Pop(VMAC_List); device_id = Keylist_Key(VMAC_List, index);
pVMAC = Keylist_Data_Delete_By_Index(VMAC_List, index);
if (pVMAC) { if (pVMAC) {
PRINTF("VMAC List: %lu [", (unsigned long)device_id);
/* print the MAC */
for (unsigned i = 0; i < pVMAC->mac_len; i++) {
PRINTF("%02X", pVMAC->mac[i]);
}
PRINTF("]\n");
free(pVMAC); free(pVMAC);
} }
} while (pVMAC); } while (pVMAC);
@@ -255,7 +286,7 @@ void VMAC_Init(void)
VMAC_List = Keylist_Create(); VMAC_List = Keylist_Create();
if (VMAC_List) { if (VMAC_List) {
atexit(VMAC_Cleanup); atexit(VMAC_Cleanup);
printf("VMAC List initialized.\n"); PRINTF("VMAC List initialized.\n");
} }
} }
+2
View File
@@ -49,6 +49,8 @@ extern "C" {
void VMAC_Cleanup(void); void VMAC_Cleanup(void);
BACNET_STACK_EXPORT BACNET_STACK_EXPORT
void VMAC_Init(void); void VMAC_Init(void);
BACNET_STACK_EXPORT
void VMAC_Debug_Enable(void);
#ifdef BAC_TEST #ifdef BAC_TEST
#include "ctest.h" #include "ctest.h"
+4
View File
@@ -102,6 +102,10 @@ extern "C" {
void bip6_receive_callback( void bip6_receive_callback(
void); void);
BACNET_STACK_EXPORT
void bip6_debug_enable(
void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif /* __cplusplus */ #endif /* __cplusplus */
+5
View File
@@ -431,6 +431,11 @@ void dlenv_init(void)
#endif #endif
#if defined(BACDL_BIP6) #if defined(BACDL_BIP6)
BACNET_IP6_ADDRESS addr; BACNET_IP6_ADDRESS addr;
pEnv = getenv("BACNET_BIP6_DEBUG");
if (pEnv) {
bip6_debug_enable();
bvlc6_debug_enable();
}
pEnv = getenv("BACNET_BIP6_BROADCAST"); pEnv = getenv("BACNET_BIP6_BROADCAST");
if (pEnv) { if (pEnv) {
bvlc6_address_set(&addr, (uint16_t)strtol(pEnv, NULL, 0), 0, 0, 0, 0, 0, bvlc6_address_set(&addr, (uint16_t)strtol(pEnv, NULL, 0), 0, 0, 0, 0, 0,