Chore/bacnet-sc-unrelated-cleanup (#620)

* Added required linux Ethernet library for ethernet build

* Added .obj to gitignore

* Fixed BACnet port for APPLE to use BSD in CMake

* Changed format in CMake to enable cleaner SC merge

* Added create-object and delete-object recipes in GCC Makefile

* Added datalink timer to all example OS apps

* Changed most microcontroller ports to use BACAPP_MINIMAL to specify which datatypes can be written.

* Fixed zephyr OS for BACnet/IP warning

* Fixed zephyr OS log to not require log_strdup

* Added writefile API to file object example

* Added API to device-client to make it more robust.

* Added API in network-port object for getting the ASCII object-name

* Added debug print with a timestamp option

* Added debug print with hex dump print

* Added API to network port object for activate and discard

* Added default define for debug with timestamp

* Added prototype in header for disabled debug printf.

* Added fifo peek ahead function to peek at more than one byte.

* Added get-mac value for network port that uses buffer rather than octetstring
This commit is contained in:
Steve Karg
2024-04-19 12:54:56 -05:00
committed by GitHub
parent 600508c357
commit 770be70688
47 changed files with 687 additions and 152 deletions
+151 -27
View File
@@ -37,81 +37,205 @@
#include <stdio.h> /* Standard I/O */
#include <stdlib.h> /* Standard Library */
#include <stdarg.h>
#if DEBUG_ENABLED
#include <string.h>
#include <ctype.h>
#endif
#include "bacnet/basic/sys/debug.h"
#if DEBUG_PRINTF_WITH_TIMESTAMP
#include "bacnet/datetime.h"
#endif
/** @file debug.c Debug print function */
#if DEBUG_ENABLED
#if DEBUG_PRINTF_WITH_TIMESTAMP
/**
* @brief Print timestamp with a printf string
* @param format - printf format string
* @param ... - variable arguments
* @note This function is only available if
* DEBUG_PRINTF_WITH_TIMESTAMP is non-zero
* and DEBUG_ENABLED is non-zero
*/
void debug_printf(const char *format, ...)
{
#if DEBUG_ENABLED
va_list ap;
char stamp_str[64];
char buf[1024];
BACNET_DATE date;
BACNET_TIME time;
datetime_local(&date, &time, NULL, NULL);
sprintf(stamp_str, "[%02d:%02d:%02d.%03d]: ", time.hour, time.min,
time.sec, time.hundredths * 10);
va_start(ap, format);
vsprintf(buf, format, ap);
va_end(ap);
printf("%s%s", stamp_str, buf);
fflush(stdout);
#else
(void)format;
#endif
}
#else
/**
* @brief Print with a printf string
* @param format - printf format string
* @param ... - variable arguments
* @note This function is only available if
* DEBUG_ENABLED is non-zero
*/
void debug_printf(const char *format, ...)
{
#if DEBUG_ENABLED
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
fflush(stdout);
return;
}
#else
void debug_printf(const char *format, ...)
{
(void)format;
#endif
}
#endif
#if PRINT_ENABLED
/**
* @brief print format with HEX dump of a buffer
* @param offset - starting address to print to the left side
* @param buffer - buffer from which to print hex from
* @param buffer_length - number of bytes from the buffer to print
* @param format - printf format string
* @param ... - variable arguments
* @note This function is only available if DEBUG_ENABLED is non-zero
*/
void debug_printf_hex(
uint32_t offset,
const uint8_t *buffer,
size_t buffer_length,
const char *format, ...)
{
#if DEBUG_ENABLED
size_t i = 0;
bool new_line = true;
char line[16+1] = {0};
size_t remainder = 0;
va_list ap;
va_start(ap, format);
vfprintf(stdout, format, ap);
va_end(ap);
/* print the buffer after the formatted text */
if (buffer && buffer_length) {
for (i = 0; i < buffer_length; i++) {
if (new_line) {
new_line = false;
printf("%08x ", (unsigned int)(offset+i));
memset(line, '.', sizeof(line)-1);
}
printf("%02x ", buffer[i]);
if (isprint(buffer[i])) {
line[i%16] = buffer[i];
}
if ((i != 0) && (!((i+1)%16))) {
printf(" %s\n", line);
new_line = true;
}
}
remainder = buffer_length%16;
if (remainder) {
for (i = 0; i < (16-remainder); i++) {
printf(" ");
}
printf(" %s\n", line);
}
}
fflush(stdout);
#else
(void)offset;
(void)buffer;
(void)buffer_length;
(void)format;
#endif
}
/**
* @brief Print with a printf string
* @param format - printf format string
* @param ... - variable arguments
* @note This function is only available if
* PRINT_ENABLED is non-zero
* @return number of characters printed
*/
int debug_aprintf(const char *format, ...)
{
int length = 0;
#if PRINT_ENABLED
va_list ap;
va_start(ap, format);
length = vfprintf(stdout, format, ap);
va_end(ap);
fflush(stdout);
#else
(void)format;
#endif
return length;
}
/**
* @brief Print with a printf string
* @param stream - file stream to print to
* @param format - printf format string
* @param ... - variable arguments
* @note This function is only available if
* PRINT_ENABLED is non-zero
* @return number of characters printed
*/
int debug_fprintf(FILE *stream, const char *format, ...)
{
int length = 0;
#if PRINT_ENABLED
va_list ap;
va_start(ap, format);
length = vfprintf(stream, format, ap);
va_end(ap);
fflush(stream);
#else
(void)format;
#endif
return length;
}
/**
* @brief Print with a perror string
* @param format - printf format string
* @param ... - variable arguments
* @note This function is only available if
* PRINT_ENABLED is non-zero
*/
void debug_perror(const char *format, ...)
{
#if PRINT_ENABLED
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
fflush(stderr);
}
#else
int debug_aprintf(const char *format, ...)
{
(void)format;
return 0;
}
int debug_fprintf(FILE *stream, const char *format, ...)
{
(void)stream;
(void)format;
return 0;
}
void debug_perror(const char *format, ...)
{
(void)format;
}
#endif
}
/**
* @brief Print with a printf string that does nothing
* @param format - printf format string
* @param ... - variable arguments
* @note useful when used with defines such as PRINTF
*/
void debug_printf_disabled(const char *format, ...)
{
(void)format;
}
+16 -11
View File
@@ -34,6 +34,10 @@
#define DEBUG_ENABLED 0
#endif
#ifndef DEBUG_PRINTF_WITH_TIMESTAMP
#define DEBUG_PRINTF_WITH_TIMESTAMP 0
#endif
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -55,17 +59,18 @@ extern "C" {
void debug_perror(
const char *format,
...);
#if DEBUG_ENABLED
/* Nothing more here */
#else
/* If your compiler supports it, this is more compact:
inline void debug_printf(
const char *format,
...) {
format = format;
}
*/
#endif
BACNET_STACK_EXPORT
void debug_printf_hex(
uint32_t offset,
const uint8_t *buffer,
size_t buffer_length,
const char *format, ...);
BACNET_STACK_EXPORT
void debug_printf_disabled(
const char *format,
...);
#ifdef __cplusplus
}
#endif /* __cplusplus */
+33
View File
@@ -172,6 +172,39 @@ uint8_t FIFO_Peek(FIFO_BUFFER const *b)
return 0;
}
/**
* Peeks ahead from the front of the FIFO without removing any data.
* Limit the number of bytes peeked to the number of bytes available.
*
* @param b - pointer to FIFO_BUFFER structure
* @param buffer [out] - buffer to hold the peeked bytes
* @param length [in] - number of bytes to peek from the FIFO
* @return number of bytes peeked
*/
unsigned FIFO_Peek_Ahead(FIFO_BUFFER const *b, uint8_t* buffer, unsigned length)
{
unsigned count = 0;
unsigned index;
unsigned tail;
unsigned i;
if (b) {
count = FIFO_Count(b);
if (count > length) {
/* adjust to limit the number of bytes peeked */
count = length;
}
tail = b->tail;
for(i = 0; i < count; i++) {
index = tail % b->buffer_len;
buffer[i] = b->buffer[index];
tail++;
}
}
return count;
}
/**
* Gets a byte from the front of the FIFO, and removes it.
* Use FIFO_Empty() or FIFO_Available() function to see if there is
+6
View File
@@ -77,6 +77,12 @@ extern "C" {
uint8_t FIFO_Peek(
FIFO_BUFFER const *b);
BACNET_STACK_EXPORT
unsigned FIFO_Peek_Ahead(
FIFO_BUFFER const *b,
uint8_t* data_bytes,
unsigned length);
BACNET_STACK_EXPORT
uint8_t FIFO_Get(
FIFO_BUFFER * b);