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:
@@ -44,6 +44,20 @@
|
||||
#include "bacnet/basic/bbmd6/vmac.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 */
|
||||
static uint16_t BVLC6_Result_Code = BVLC6_RESULT_SUCCESSFUL_COMPLETION;
|
||||
/** 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];
|
||||
#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.
|
||||
*
|
||||
* @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);
|
||||
if (vmac) {
|
||||
/* 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)) {
|
||||
/* new entry - add it! */
|
||||
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) {
|
||||
vmac = VMAC_Find_By_Key(device_id);
|
||||
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);
|
||||
status = bbmd6_address_from_vmac(addr, vmac);
|
||||
if (vmac_src) {
|
||||
@@ -264,13 +298,13 @@ int bvlc6_send_pdu(BACNET_ADDRESS *dest,
|
||||
vmac_src = Device_Object_Instance_Number();
|
||||
mtu_len = bvlc6_encode_distribute_broadcast_to_network(
|
||||
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 {
|
||||
bip6_get_broadcast_addr(&bvlc_dest);
|
||||
vmac_src = Device_Object_Instance_Number();
|
||||
mtu_len = bvlc6_encode_original_broadcast(
|
||||
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)) {
|
||||
/* 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();
|
||||
mtu_len = bvlc6_encode_original_broadcast(
|
||||
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) {
|
||||
/* valid unicast */
|
||||
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();
|
||||
mtu_len = bvlc6_encode_original_unicast(
|
||||
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 {
|
||||
debug_printf("BVLC6: Send failure. Invalid Address.\n");
|
||||
PRINTF("BVLC6: Send failure. Invalid Address.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -479,7 +513,7 @@ static void bbmd6_virtual_address_resolution_handler(
|
||||
uint32_t vmac_me = 0;
|
||||
|
||||
if (addr && pdu) {
|
||||
debug_printf("BIP6: Received Virtual-Address-Resolution.\n");
|
||||
PRINTF("BIP6: Received Virtual-Address-Resolution.\n");
|
||||
if (bbmd6_address_match_self(addr)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
} else {
|
||||
@@ -513,7 +547,7 @@ static void bbmd6_virtual_address_resolution_ack_handler(
|
||||
uint32_t vmac_dst = 0;
|
||||
|
||||
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)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
} else {
|
||||
@@ -542,7 +576,7 @@ static void bbmd6_address_resolution_handler(
|
||||
uint32_t vmac_me = 0;
|
||||
|
||||
if (addr && pdu) {
|
||||
debug_printf("BIP6: Received Address-Resolution.\n");
|
||||
PRINTF("BIP6: Received Address-Resolution.\n");
|
||||
if (bbmd6_address_match_self(addr)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
} else {
|
||||
@@ -577,7 +611,7 @@ static void bbmd6_address_resolution_ack_handler(
|
||||
uint32_t vmac_dst = 0;
|
||||
|
||||
if (addr && pdu) {
|
||||
debug_printf("BIP6: Received Address-Resolution-ACK.\n");
|
||||
PRINTF("BIP6: Received Address-Resolution-ACK.\n");
|
||||
if (bbmd6_address_match_self(addr)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
} else {
|
||||
@@ -638,7 +672,7 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
incoming messages. */
|
||||
bbmd6_add_vmac(vmac_src, addr);
|
||||
bvlc6_vmac_address_set(src, vmac_src);
|
||||
debug_printf(
|
||||
PRINTF(
|
||||
"BIP6: Received Result Code=%d\n", BVLC6_Result_Code);
|
||||
}
|
||||
break;
|
||||
@@ -657,10 +691,10 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
case BVLC6_ORIGINAL_UNICAST_NPDU:
|
||||
/* This message is used to send directed NPDUs to
|
||||
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)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
debug_printf("BIP6: Original-Unicast-NPDU is me!.\n");
|
||||
PRINTF("BIP6: Original-Unicast-NPDU is me!.\n");
|
||||
} else {
|
||||
function_len = bvlc6_decode_original_unicast(
|
||||
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);
|
||||
offset = header_len + (function_len - npdu_len);
|
||||
} else {
|
||||
debug_printf("BIP6: Original-Unicast-NPDU: "
|
||||
PRINTF("BIP6: Original-Unicast-NPDU: "
|
||||
"VMAC is not me!\n");
|
||||
}
|
||||
} else {
|
||||
debug_printf(
|
||||
PRINTF(
|
||||
"BIP6: Original-Unicast-NPDU: Unable to decode!\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
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)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
debug_printf("BIP6: Original-Broadcast-NPDU is me!\n");
|
||||
PRINTF("BIP6: Original-Broadcast-NPDU is me!\n");
|
||||
} else {
|
||||
function_len = bvlc6_decode_original_broadcast(
|
||||
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];
|
||||
if (npdu_confirmed_service(npdu, npdu_len)) {
|
||||
offset = 0;
|
||||
debug_printf(
|
||||
PRINTF(
|
||||
"BIP6: Original-Broadcast-NPDU: "
|
||||
"Confirmed Service! Discard!");
|
||||
}
|
||||
} else {
|
||||
debug_printf("BIP6: Original-Broadcast-NPDU: Unable to "
|
||||
PRINTF("BIP6: Original-Broadcast-NPDU: Unable to "
|
||||
"decode!\n");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case BVLC6_FORWARDED_NPDU:
|
||||
debug_printf("BIP6: Received Forwarded-NPDU.\n");
|
||||
PRINTF("BIP6: Received Forwarded-NPDU.\n");
|
||||
if (bbmd6_address_match_self(addr)) {
|
||||
/* ignore messages from my IPv6 address */
|
||||
debug_printf("BIP6: Forwarded-NPDU is me!\n");
|
||||
PRINTF("BIP6: Forwarded-NPDU is me!\n");
|
||||
} else {
|
||||
function_len = bvlc6_decode_forwarded_npdu(pdu, pdu_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);
|
||||
offset = header_len + (function_len - npdu_len);
|
||||
} else {
|
||||
debug_printf(
|
||||
PRINTF(
|
||||
"BIP6: Forwarded-NPDU: Unable to decode!\n");
|
||||
}
|
||||
}
|
||||
@@ -761,7 +795,7 @@ int bvlc6_bbmd_disabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
if (send_result) {
|
||||
vmac_src = Device_Object_Instance_Number();
|
||||
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. */
|
||||
bbmd6_add_vmac(vmac_src, addr);
|
||||
bvlc6_vmac_address_set(src, vmac_src);
|
||||
debug_printf(
|
||||
PRINTF(
|
||||
"BIP6: Received Result Code=%d\n", BVLC6_Result_Code);
|
||||
}
|
||||
break;
|
||||
@@ -839,9 +873,9 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
case BVLC6_ORIGINAL_UNICAST_NPDU:
|
||||
/* This message is used to send directed NPDUs to
|
||||
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)) {
|
||||
debug_printf("BIP6: Dropped Original-Unicast-NPDU.\n");
|
||||
PRINTF("BIP6: Dropped Original-Unicast-NPDU.\n");
|
||||
/* ignore messages from my IPv6 address */
|
||||
offset = 0;
|
||||
} else {
|
||||
@@ -860,7 +894,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
}
|
||||
break;
|
||||
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(
|
||||
pdu, pdu_len, &vmac_src, NULL, 0, &npdu_len);
|
||||
if (function_len) {
|
||||
@@ -873,7 +907,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
network layer. */
|
||||
if (npdu_confirmed_service(npdu, npdu_len)) {
|
||||
offset = 0;
|
||||
debug_printf(
|
||||
PRINTF(
|
||||
"BIP6: Original-Broadcast-NPDU: "
|
||||
"Confirmed Service! Discard!");
|
||||
} else {
|
||||
@@ -900,7 +934,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
}
|
||||
break;
|
||||
case BVLC6_FORWARDED_NPDU:
|
||||
debug_printf("BIP6: Received Forwarded-NPDU.\n");
|
||||
PRINTF("BIP6: Received Forwarded-NPDU.\n");
|
||||
function_len = bvlc6_decode_forwarded_npdu(
|
||||
pdu, pdu_len, &vmac_src, &fwd_address, NULL, 0, &npdu_len);
|
||||
if (function_len) {
|
||||
@@ -959,7 +993,7 @@ int bvlc6_bbmd_enabled_handler(BACNET_IP6_ADDRESS *addr,
|
||||
if (send_result) {
|
||||
vmac_src = Device_Object_Instance_Number();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,10 @@ extern "C" {
|
||||
void bvlc6_maintenance_timer(
|
||||
uint16_t seconds);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
void bvlc6_debug_enable(
|
||||
void);
|
||||
|
||||
BACNET_STACK_EXPORT
|
||||
void bvlc6_cleanup(void);
|
||||
|
||||
|
||||
@@ -42,6 +42,28 @@
|
||||
/* me! */
|
||||
#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
|
||||
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);
|
||||
if (index >= 0) {
|
||||
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)
|
||||
{
|
||||
struct vmac_data *pVMAC;
|
||||
uint32_t device_id;
|
||||
const int index = 0;
|
||||
|
||||
if (VMAC_List) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
} while (pVMAC);
|
||||
@@ -255,7 +286,7 @@ void VMAC_Init(void)
|
||||
VMAC_List = Keylist_Create();
|
||||
if (VMAC_List) {
|
||||
atexit(VMAC_Cleanup);
|
||||
printf("VMAC List initialized.\n");
|
||||
PRINTF("VMAC List initialized.\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,8 @@ extern "C" {
|
||||
void VMAC_Cleanup(void);
|
||||
BACNET_STACK_EXPORT
|
||||
void VMAC_Init(void);
|
||||
BACNET_STACK_EXPORT
|
||||
void VMAC_Debug_Enable(void);
|
||||
|
||||
#ifdef BAC_TEST
|
||||
#include "ctest.h"
|
||||
|
||||
Reference in New Issue
Block a user