Refactored all the sprintf to use snprintf instead. (#628)

This commit is contained in:
Steve Karg
2024-04-27 12:41:45 -05:00
committed by GitHub
parent 70c54817fd
commit bb276e2431
46 changed files with 308 additions and 272 deletions
+3 -3
View File
@@ -94,12 +94,12 @@ uint32_t Analog_Input_Index_To_Instance(unsigned index)
bool Analog_Input_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
if (object_instance < MAX_ANALOG_INPUTS) {
sprintf(text_string, "AI-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "AI-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+3 -3
View File
@@ -158,14 +158,14 @@ bool Analog_Value_Present_Value_Set(
bool Analog_Value_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32] = ""; /* okay for single thread */
static char text[32] = ""; /* okay for single thread */
unsigned index = 0;
bool status = false;
index = Analog_Value_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_VALUES) {
sprintf(text_string, "AV-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "AV-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+1 -1
View File
@@ -64,7 +64,7 @@ void SerialPort::openChannel()
/* For COM ports greater than 9 you have to use a special syntax
for CreateFile. The syntax also works for COM ports 1-9. */
/* http://support.microsoft.com/kb/115831 */
sprintf(comName, "\\\\.\\COM%ld", portNumber);
snprintf(comName, sizeof(comName), "\\\\.\\COM%ld", portNumber);
}
serialHandle = CreateFile( comName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
+3 -3
View File
@@ -139,12 +139,12 @@ bool Binary_Input_Present_Value_Set(
bool Binary_Input_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
if (object_instance < MAX_BINARY_INPUTS) {
sprintf(text_string, "BI-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "BI-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+3 -3
View File
@@ -209,12 +209,12 @@ bool Binary_Output_Out_Of_Service(uint32_t instance)
bool Binary_Output_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
if (object_instance < MAX_BINARY_OUTPUTS) {
sprintf(text_string, "BO-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "BO-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+4 -3
View File
@@ -284,11 +284,12 @@ uint32_t Device_Index_To_Instance(unsigned index)
static char *Device_Name_Default(void)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
sprintf(text_string, "DEVICE-%lu", Object_Instance_Number);
snprintf(text, sizeof(text), "DEVICE-%lu",
(unsigned long)Object_Instance_Number);
return text_string;
return text;
}
bool Device_Object_Name(
+5 -5
View File
@@ -154,7 +154,7 @@ void test_task(void)
if (mstimer_expired(&Test_Timer)) {
mstimer_reset(&Test_Timer);
sprintf(Send_Buffer, "BACnet: 0000000\r\n");
snprintf(Send_Buffer, sizeof(Send_Buffer), "BACnet: 0000000\r\n");
MSTP_MAC_Address = input_address();
Send_Buffer[8] = (MSTP_MAC_Address & BIT(0)) ? '1' : '0';
Send_Buffer[9] = (MSTP_MAC_Address & BIT(1)) ? '1' : '0';
@@ -198,17 +198,17 @@ void test_task(void)
break;
case 'e':
seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *)&id, 2);
sprintf(Send_Buffer, "\r\n%04X", id);
snprintf(Send_Buffer, sizeof(Send_Buffer), "\r\n%04X", id);
serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer));
break;
case 'b':
sprintf(Send_Buffer, "\r\n%lubps",
snprintf(Send_Buffer, sizeof(Send_Buffer), "\r\n%lubps",
(unsigned long)rs485_baud_rate());
serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer));
break;
case 'm':
sprintf(
Send_Buffer, "\r\nMax:%u", (unsigned)dlmstp_max_master());
snprintf(Send_Buffer, sizeof(Send_Buffer),
"\r\nMax:%u", (unsigned)dlmstp_max_master());
serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer));
break;
default:
+3 -3
View File
@@ -204,7 +204,7 @@ void Analog_Input_Present_Value_Set(uint32_t object_instance, float value)
bool Analog_Input_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32] = "";
static char text[32] = "";
unsigned int index;
bool status = false;
@@ -215,8 +215,8 @@ bool Analog_Input_Object_Name(
else {
index = Analog_Input_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_INPUTS) {
sprintf(text_string, "ANALOG INPUT %lu", (unsigned long)index);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "ANALOG INPUT %lu", (unsigned long)index);
status = characterstring_init_ansi(object_name, text);
}
}
return status;
+3 -3
View File
@@ -171,16 +171,16 @@ bool Binary_Output_Out_Of_Service(uint32_t object_instance)
bool Binary_Output_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32] = "";
static char text[32] = "";
bool status = false;
if (object_instance == 0)
status = characterstring_init_ansi(object_name, "Led");
else {
if (object_instance < MAX_BINARY_OUTPUTS) {
sprintf(text_string, "BINARY OUTPUT %lu",
snprintf(text, sizeof(text), "BINARY OUTPUT %lu",
(unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text_string);
status = characterstring_init_ansi(object_name, text);
}
}
return status;
+1 -1
View File
@@ -597,7 +597,7 @@ static char *ntoa(uint32_t addr)
{
static char buffer[18];
sprintf(buffer, "%d.%d.%d.%d", (addr & 0x000000FF),
snprintf(buffer, sizeof(buffer), "%d.%d.%d.%d", (addr & 0x000000FF),
(addr & 0x0000FF00) >> 8, (addr & 0x00FF0000) >> 16,
(addr & 0xFF000000) >> 24);
+3 -3
View File
@@ -66,11 +66,11 @@ uint32_t Analog_Input_Index_To_Instance(unsigned index)
char *Analog_Input_Name(uint32_t object_instance)
{
static char text_string[16] = ""; /* okay for single thread */
static char text[16] = ""; /* okay for single thread */
if (object_instance < MAX_ANALOG_INPUTS) {
sprintf(text_string, "AI-%lu", (unsigned long)object_instance);
return text_string;
snprintf(text, sizeof(text), "AI-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -131,11 +131,11 @@ float Analog_Value_Present_Value(uint32_t object_instance)
/* note: the object name must be unique within this device */
char *Analog_Value_Name(uint32_t object_instance)
{
static char text_string[32] = ""; /* okay for single thread */
static char text[32] = ""; /* okay for single thread */
if (object_instance < MAX_ANALOG_VALUES) {
sprintf(text_string, "AV-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "AV-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -103,11 +103,11 @@ BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t object_instance)
char *Binary_Input_Name(uint32_t object_instance)
{
static char text_string[16] = ""; /* okay for single thread */
static char text[16] = ""; /* okay for single thread */
if (object_instance < MAX_BINARY_INPUTS) {
sprintf(text_string, "BI-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "BI-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -100,11 +100,11 @@ BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance)
/* note: the object name must be unique within this device */
char *Binary_Value_Name(uint32_t object_instance)
{
static char text_string[16] = ""; /* okay for single thread */
static char text[16] = ""; /* okay for single thread */
if (object_instance < MAX_BINARY_VALUES) {
sprintf(text_string, "BV-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "BV-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -66,11 +66,11 @@ uint32_t Analog_Input_Index_To_Instance(unsigned index)
char *Analog_Input_Name(uint32_t object_instance)
{
static char text_string[16] = ""; /* okay for single thread */
static char text[16] = ""; /* okay for single thread */
if (object_instance < MAX_ANALOG_INPUTS) {
sprintf(text_string, "AI-%lu", (unsigned long)object_instance);
return text_string;
snprintf(text, sizeof(text), "AI-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -131,11 +131,11 @@ float Analog_Value_Present_Value(uint32_t object_instance)
/* note: the object name must be unique within this device */
char *Analog_Value_Name(uint32_t object_instance)
{
static char text_string[32] = ""; /* okay for single thread */
static char text[32] = ""; /* okay for single thread */
if (object_instance < MAX_ANALOG_VALUES) {
sprintf(text_string, "AV-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "AV-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -103,11 +103,11 @@ BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t object_instance)
char *Binary_Input_Name(uint32_t object_instance)
{
static char text_string[16] = ""; /* okay for single thread */
static char text[16] = ""; /* okay for single thread */
if (object_instance < MAX_BINARY_INPUTS) {
sprintf(text_string, "BI-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "BI-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -100,11 +100,11 @@ BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance)
/* note: the object name must be unique within this device */
char *Binary_Value_Name(uint32_t object_instance)
{
static char text_string[16] = ""; /* okay for single thread */
static char text[16] = ""; /* okay for single thread */
if (object_instance < MAX_BINARY_VALUES) {
sprintf(text_string, "BV-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "BV-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+3 -3
View File
@@ -194,11 +194,11 @@ bool Binary_Output_Out_Of_Service(uint32_t instance)
/* note: the object name must be unique within this device */
char *Binary_Output_Name(uint32_t object_instance)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
if (object_instance < MAX_BINARY_OUTPUTS) {
sprintf(text_string, "BO-%lu", object_instance);
return text_string;
snprintf(text, sizeof(text), "BO-%lu", (unsigned long)object_instance);
return text;
}
return NULL;
+51 -36
View File
@@ -59,8 +59,9 @@
#include "remote-ext.h"
/* commonly used comparison address for ethernet */
uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF };
uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
/* commonly used empty address for ethernet quick compare */
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
@@ -134,7 +135,7 @@ bool ethernet_init(char *if_name)
BOOLEAN result;
CHAR str[sizeof(PACKET_OID_DATA) + 128];
int i;
char msgBuf[200];
char msg[200];
if (ethernet_valid())
ethernet_cleanup();
@@ -144,9 +145,10 @@ bool ethernet_init(char *if_name)
*/
/* Retrieve the device list */
if (pcap_findalldevs(&pcap_all_if, pcap_errbuf) == -1) {
sprintf(
msgBuf, "ethernet.c: error in pcap_findalldevs: %s\n", pcap_errbuf);
LogError(msgBuf);
snprintf(
msg, sizeof(msg), "ethernet.c: error in pcap_findalldevs: %s\n",
pcap_errbuf);
LogError(msg);
return false;
}
/* Scan the list printing every entry */
@@ -156,9 +158,10 @@ bool ethernet_init(char *if_name)
}
pcap_freealldevs(pcap_all_if); /* we don't need it anymore */
if (dev == NULL) {
sprintf(
msgBuf, "ethernet.c: specified interface not found: %s\n", if_name);
LogError(msgBuf);
snprintf(
msg, sizeof(msg), "ethernet.c: specified interface not found: %s\n",
if_name);
LogError(msg);
return false;
}
@@ -169,9 +172,10 @@ bool ethernet_init(char *if_name)
lpAdapter = PacketOpenAdapter(if_name);
if (lpAdapter == NULL) {
ethernet_cleanup();
sprintf(msgBuf, "ethernet.c: error in PacketOpenAdapter(\"%s\")\n",
if_name);
LogError(msgBuf);
snprintf(
msg, sizeof(msg),
"ethernet.c: error in PacketOpenAdapter(\"%s\")\n", if_name);
LogError(msg);
return false;
}
pOidData = (PPACKET_OID_DATA)str;
@@ -192,7 +196,8 @@ bool ethernet_init(char *if_name)
* Open interface for subsequent sending and receiving
*/
/* Open the output device */
pcap_eth802_fp = pcap_open(if_name, /* name of the device */
pcap_eth802_fp = pcap_open(
if_name, /* name of the device */
ETHERNET_MPDU_MAX, /* portion of the packet to capture */
PCAP_OPENFLAG_PROMISCUOUS, /* promiscuous mode */
eth_timeout, /* read timeout */
@@ -202,11 +207,12 @@ bool ethernet_init(char *if_name)
if (pcap_eth802_fp == NULL) {
PacketCloseAdapter(lpAdapter);
ethernet_cleanup();
sprintf(msgBuf,
snprintf(
msg, sizeof(msg),
"ethernet.c: unable to open the adapter. %s is not supported by "
"WinPcap\n",
if_name);
LogError(msgBuf);
LogError(msg);
return false;
}
@@ -219,7 +225,8 @@ bool ethernet_init(char *if_name)
/* function to send a packet out the 802.2 socket */
/* returns bytes sent success, negative on failure */
int ethernet_send(BACNET_ADDRESS *dest, /* destination address */
int ethernet_send(
BACNET_ADDRESS *dest, /* destination address */
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* any data to be sent - may be null */
unsigned pdu_len /* number of bytes of data */
@@ -272,10 +279,11 @@ int ethernet_send(BACNET_ADDRESS *dest, /* destination address */
/* Send the packet */
if (pcap_sendpacket(pcap_eth802_fp, mtu, mtu_len) != 0) {
/* did it get sent? */
char msgBuf[200];
sprintf(msgBuf, "ethernet.c: error sending packet: %s\n",
char msg[200];
snprintf(
msg, sizeof(msg), "ethernet.c: error sending packet: %s\n",
pcap_geterr(pcap_eth802_fp));
LogError(msgBuf);
LogError(msg);
return -5;
}
@@ -284,7 +292,8 @@ int ethernet_send(BACNET_ADDRESS *dest, /* destination address */
/* function to send a packet out the 802.2 socket */
/* returns number of bytes sent on success, negative on failure */
int ethernet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
int ethernet_send_pdu(
BACNET_ADDRESS *dest, /* destination address */
uint8_t *pdu, /* any data to be sent - may be null */
unsigned pdu_len /* number of bytes of data */
)
@@ -298,7 +307,8 @@ int ethernet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
}
/* function to send a packet out the 802.2 socket */
/* returns 1 on success, 0 on failure */
return ethernet_send(dest, /* destination address */
return ethernet_send(
dest, /* destination address */
&src, /* source address */
pdu, /* any data to be sent - may be null */
pdu_len /* number of bytes of data */
@@ -307,7 +317,8 @@ int ethernet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
/* receives an 802.2 framed packet */
/* returns the number of octets in the PDU, or zero on failure */
uint16_t ethernet_receive(BACNET_ADDRESS *src, /* source address */
uint16_t ethernet_receive(
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout /* number of milliseconds to wait for a packet. we ommit it
@@ -328,8 +339,9 @@ uint16_t ethernet_receive(BACNET_ADDRESS *src, /* source address */
/* Capture a packet */
res = pcap_next_ex(pcap_eth802_fp, &header, &pkt_data);
if (res < 0) {
char msgBuf[200];
sprintf(msgBuf, "ethernet.c: error in receiving packet: %s\n",
char msg[200];
snprintf(
msg, sizeof(), "ethernet.c: error in receiving packet: %s\n",
pcap_geterr(pcap_eth802_fp));
return 0;
} else if (res == 0)
@@ -418,27 +430,30 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS *dest)
void ethernet_debug_address(const char *info, BACNET_ADDRESS *dest)
{
int i = 0; /* counter */
char msgBuf[200];
char msg[200];
if (info) {
sprintf(msgBuf, "%s", info);
LogError(msgBuf);
snprintf(msg, sizeof(msg), "%s", info);
LogError(msg);
}
/* if */
if (dest) {
sprintf(
msgBuf, "Address:\n MAC Length=%d\n MAC Address=", dest->mac_len);
LogInfo(msgBuf);
snprintf(
msg, sizeof(msg),
"Address:\n MAC Length=%d\n MAC Address=", dest->mac_len);
LogInfo(msg);
for (i = 0; i < MAX_MAC_LEN; i++) {
sprintf(msgBuf, "%02X ", (unsigned)dest->mac[i]);
LogInfo(msgBuf);
snprintf(msg, sizeof(msg), "%02X ", (unsigned)dest->mac[i]);
LogInfo(msg);
} /* for */
LogInfo("\n");
sprintf(msgBuf, " Net=%hu\n Len=%d\n Adr=", dest->net, dest->len);
LogInfo(msgBuf);
snprintf(
msg, sizeof(msg), " Net=%hu\n Len=%d\n Adr=", dest->net,
dest->len);
LogInfo(msg);
for (i = 0; i < MAX_MAC_LEN; i++) {
sprintf(msgBuf, "%02X ", (unsigned)dest->adr[i]);
LogInfo(msgBuf);
snprintf(msg, sizeof(msg), "%02X ", (unsigned)dest->adr[i]);
LogInfo(msg);
} /* for */
LogInfo("\n");
}
+6 -3
View File
@@ -115,8 +115,11 @@ void RS485_Set_Interface(char *ifname)
strupper(ifname);
if (strncmp("COM", ifname, 3) == 0) {
if (strlen(ifname) > 3) {
sprintf(RS485_Port_Name, "\\\\.\\COM%i", atoi(ifname + 3));
fprintf(stdout, "Adjusted interface name to %s\r\n",
snprintf(
RS485_Port_Name, sizeof(RS485_Port_Name), "\\\\.\\COM%i",
atoi(ifname + 3));
fprintf(
stdout, "Adjusted interface name to %s\r\n",
RS485_Port_Name);
}
}
@@ -136,7 +139,7 @@ bool RS485_Interface_Valid(unsigned port_number)
bool status = false;
char ifname[255] = "";
sprintf(ifname, "\\\\.\\COM%u", port_number);
snprintf(ifname, sizeof(ifname), "\\\\.\\COM%u", port_number);
h = CreateFile(
ifname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE) {
+3 -3
View File
@@ -106,14 +106,14 @@ unsigned Analog_Input_Count(void)
bool Analog_Input_Object_Name(
uint32_t object_instance, BACNET_CHARACTER_STRING *object_name)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
bool status = false;
unsigned index = 0;
index = Analog_Input_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_INPUTS) {
sprintf(text_string, "AI-%lu", object_instance);
status = characterstring_init_ansi(object_name, text_string);
snprintf(text, sizeof(text), "AI-%lu", (unsigned long)object_instance);
status = characterstring_init_ansi(object_name, text);
}
return status;
+4 -3
View File
@@ -241,11 +241,12 @@ uint32_t Device_Index_To_Instance(unsigned index)
static char *Device_Name_Default(void)
{
static char text_string[32]; /* okay for single thread */
static char text[32]; /* okay for single thread */
sprintf(text_string, "DEVICE-%lu", Object_Instance_Number);
snprintf(text, sizeof(text), "DEVICE-%lu",
(unsigned long)Object_Instance_Number);
return text_string;
return text;
}
bool Device_Object_Name(
+97 -92
View File
@@ -58,8 +58,10 @@ LOG_MODULE_DECLARE(bacnet, CONFIG_BACNETSTACK_LOG_LEVEL);
#define THIS_FILE "bip6-init.c"
#if (MAX_MAC_LEN < BIP6_ADDRESS_MAX) /* Make sure an 18 byte address can be stored */
#error "BACNET_ADDRESS.mac (bacdef.h) is not large enough to store an IPv6 address."
#if (MAX_MAC_LEN < BIP6_ADDRESS_MAX) /* Make sure an 18 byte address can be \
stored */
#error \
"BACNET_ADDRESS.mac (bacdef.h) is not large enough to store an IPv6 address."
#endif
/* zephyr socket */
@@ -77,48 +79,45 @@ static struct in6_addr BIP6_Multicast_Addr;
/* Used by inet6_ntoa */
#if CONFIG_BACNETSTACK_LOG_LEVEL
static char ipv6_addr_str[42]={0};
static char ipv6_addr_str[42] = { 0 };
#else
static char ipv6_addr_str[]="";
static char ipv6_addr_str[] = "";
#endif
/**
* @brief Return a string representation of an IPv6 address
* @param a - IPv6 address
* @return Pointer to global string
*/
static char* inet6_ntoa(struct in6_addr *a)
* @brief Return a string representation of an IPv6 address
* @param a - IPv6 address
* @return Pointer to global string
*/
static char *inet6_ntoa(struct in6_addr *a)
{
#if CONFIG_BACNETSTACK_LOG_LEVEL
uint8_t x=0;
uint8_t d=0;
#if CONFIG_BACNETSTACK_LOG_LEVEL
uint8_t x = 0;
uint8_t d = 0;
uint8_t non_zero_count = 0;
/* Avoid overwhelming the logging system */
while(log_buffered_cnt())
{
while (log_buffered_cnt()) {
k_sleep(K_MSEC(1));
}
for(x=0; x<IP6_ADDRESS_MAX; x+=2)
{
if(a->s6_addr[x] | a->s6_addr[x+1])
{
for (x = 0; x < IP6_ADDRESS_MAX; x += 2) {
if (a->s6_addr[x] | a->s6_addr[x + 1]) {
non_zero_count++;
d+=sprintf(&ipv6_addr_str[d], "%02X%02X", a->s6_addr[x], a->s6_addr[x+1]);
d += snprintf(
&ipv6_addr_str[d], sizeof(ipv6_addr_str), "%02X%02X",
a->s6_addr[x], a->s6_addr[x + 1]);
}
if(x<14)
{
d+=sprintf(&ipv6_addr_str[d], ":");
if (x < 14) {
d += snprintf(&ipv6_addr_str[d], sizeof(ipv6_addr_str), ":");
}
}
if(!non_zero_count)
{
sprintf(&ipv6_addr_str[0], "undefined");
if (!non_zero_count) {
snprintf(&ipv6_addr_str[0], sizeof(ipv6_addr_str), "undefined");
}
#endif
#endif
return &ipv6_addr_str[0];
}
@@ -179,7 +178,7 @@ void bip6_get_broadcast_address(BACNET_ADDRESS *dest)
if (dest) {
dest->mac_len = BIP6_ADDRESS_MAX;
memcpy(&dest->mac[0], &BIP6_Multicast_Addr.s6_addr, IP6_ADDRESS_MAX);
memcpy(&dest->mac[IP6_ADDRESS_MAX], &BIP6_Port, sizeof(BIP6_Port) );
memcpy(&dest->mac[IP6_ADDRESS_MAX], &BIP6_Port, sizeof(BIP6_Port));
memset(&dest->adr, 0, sizeof(dest->adr));
dest->net = BACNET_BROADCAST_NETWORK;
dest->len = 0;
@@ -225,7 +224,8 @@ bool bip6_get_addr(BACNET_IP6_ADDRESS *addr)
bool bip6_set_broadcast_addr(BACNET_IP6_ADDRESS *addr)
{
if (addr) {
memcpy(&BIP6_Multicast_Addr.s6_addr, &addr->address[0], IP6_ADDRESS_MAX);
memcpy(
&BIP6_Multicast_Addr.s6_addr, &addr->address[0], IP6_ADDRESS_MAX);
return true;
}
return false;
@@ -239,7 +239,8 @@ bool bip6_set_broadcast_addr(BACNET_IP6_ADDRESS *addr)
bool bip6_get_broadcast_addr(BACNET_IP6_ADDRESS *addr)
{
if (addr) {
memcpy(&addr->address[0], &BIP6_Multicast_Addr.s6_addr, IP6_ADDRESS_MAX);
memcpy(
&addr->address[0], &BIP6_Multicast_Addr.s6_addr, IP6_ADDRESS_MAX);
addr->port = ntohs(BIP6_Port);
return true;
}
@@ -293,8 +294,9 @@ int bip6_send_mpdu(BACNET_IP6_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len)
bip6_dest.sin6_port = htons(dest->port);
/* Send the packet */
return zsock_sendto(BIP6_Socket, (char *)mtu, mtu_len, 0,
(struct sockaddr *)&bip6_dest, sizeof(struct sockaddr));
return zsock_sendto(
BIP6_Socket, (char *)mtu, mtu_len, 0, (struct sockaddr *)&bip6_dest,
sizeof(struct sockaddr));
}
uint16_t bip6_receive(
@@ -334,17 +336,18 @@ uint16_t bip6_receive(
/* see if there is a packet for us */
if (zsock_select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = zsock_recvfrom(BIP6_Socket, (char *)&npdu[0], max_npdu,
0, (struct sockaddr *)&sin, &sin_len);
}
else
{
received_bytes = zsock_recvfrom(
BIP6_Socket, (char *)&npdu[0], max_npdu, 0, (struct sockaddr *)&sin,
&sin_len);
} else {
return 0;
}
/* See if there is a problem */
if (received_bytes < 0) {
LOG_WRN("%s:%d - RX zsock_recvfrom() error: %d", THIS_FILE, __LINE__, received_bytes);
LOG_WRN(
"%s:%d - RX zsock_recvfrom() error: %d", THIS_FILE, __LINE__,
received_bytes);
return 0;
}
/* no problem, just no bytes */
@@ -382,7 +385,8 @@ uint16_t bip6_receive(
return npdu_len;
}
int bip6_send_pdu(BACNET_ADDRESS *dest,
int bip6_send_pdu(
BACNET_ADDRESS *dest,
BACNET_NPDU_DATA *npdu_data,
uint8_t *pdu,
unsigned pdu_len)
@@ -395,10 +399,10 @@ void bip6_set_interface(char *ifname)
{
struct net_if *interface = 0;
int index = -1;
int x=0;
int x = 0;
BACNET_IP6_ADDRESS unicast = {0};
BACNET_IP6_ADDRESS multicast = {0};
BACNET_IP6_ADDRESS unicast = { 0 };
BACNET_IP6_ADDRESS multicast = { 0 };
unicast.port = BIP6_Port;
multicast.port = BIP6_Port;
@@ -406,81 +410,84 @@ void bip6_set_interface(char *ifname)
LOG_INF("bip6_set_interface()");
LOG_INF("UDP port: %d", ntohs(BIP6_Port));
if(ifname)
{
if (ifname) {
index = atoi(ifname);
/* if index is zero, discern between "0" and a parse error */
if(!index && strcmp(ifname,"0"))
{
LOG_ERR("%s:%d - Argument must parse to an integer", THIS_FILE, __LINE__);
}
else
{
if (!index && strcmp(ifname, "0")) {
LOG_ERR(
"%s:%d - Argument must parse to an integer", THIS_FILE,
__LINE__);
} else {
interface = net_if_get_by_index(index);
if(interface)
{
if (interface) {
LOG_INF("Using interface %d", index);
}
else
{
LOG_ERR("%s:%d - No interface at index %d", THIS_FILE, __LINE__, index);
} else {
LOG_ERR(
"%s:%d - No interface at index %d", THIS_FILE, __LINE__,
index);
}
}
}
if(index == -1)
{
LOG_WRN("%s:%d - No valid interface specified - using default ",THIS_FILE, __LINE__);
if (index == -1) {
LOG_WRN(
"%s:%d - No valid interface specified - using default ", THIS_FILE,
__LINE__);
interface = net_if_get_default();
}
if(interface)
{
if (interface) {
LOG_INF("Interface set - Configured addresses:");
for(x=0; x<NET_IF_MAX_IPV6_ADDR; x++)
{
inet6_ntoa(&interface->config.ip.ipv6->unicast[x].address.in6_addr );
for (x = 0; x < NET_IF_MAX_IPV6_ADDR; x++) {
inet6_ntoa(&interface->config.ip.ipv6->unicast[x].address.in6_addr);
LOG_INF(" unicast[%d]: %s", x, ipv6_addr_str);
}
for(x=0; x<NET_IF_MAX_IPV6_MADDR; x++)
{
inet6_ntoa(&interface->config.ip.ipv6->mcast[x].address.in6_addr );
for (x = 0; x < NET_IF_MAX_IPV6_MADDR; x++) {
inet6_ntoa(&interface->config.ip.ipv6->mcast[x].address.in6_addr);
LOG_INF(" multicast[%d]: %s", x, ipv6_addr_str);
}
if(CONFIG_BACDL_BIP6_ADDRESS_INDEX >= NET_IF_MAX_IPV6_ADDR)
{
LOG_ERR("%s:%d - IPv6 address index of %d is out of range (0-%d)", THIS_FILE,
__LINE__, CONFIG_BACDL_BIP6_ADDRESS_INDEX, NET_IF_MAX_IPV6_ADDR-1);
if (CONFIG_BACDL_BIP6_ADDRESS_INDEX >= NET_IF_MAX_IPV6_ADDR) {
LOG_ERR(
"%s:%d - IPv6 address index of %d is out of range (0-%d)",
THIS_FILE, __LINE__, CONFIG_BACDL_BIP6_ADDRESS_INDEX,
NET_IF_MAX_IPV6_ADDR - 1);
return;
}
LOG_INF("Using IPv6 address at index %d", CONFIG_BACDL_BIP6_ADDRESS_INDEX);
LOG_INF(
"Using IPv6 address at index %d", CONFIG_BACDL_BIP6_ADDRESS_INDEX);
memcpy(&unicast.address, &interface->config.ip.ipv6->unicast
[CONFIG_BACDL_BIP6_ADDRESS_INDEX].address.in6_addr, IP6_ADDRESS_MAX);
memcpy(
&unicast.address,
&interface->config.ip.ipv6->unicast[CONFIG_BACDL_BIP6_ADDRESS_INDEX]
.address.in6_addr,
IP6_ADDRESS_MAX);
if(net_addr_pton(AF_INET6, CONFIG_BACDL_BIP6_MCAST_ADDRESS, &multicast.address))
{
LOG_ERR("%s:%d - Failed to parse IPv6 multicast address: %s", THIS_FILE, __LINE__, CONFIG_BACDL_BIP6_MCAST_ADDRESS);
if (net_addr_pton(
AF_INET6, CONFIG_BACDL_BIP6_MCAST_ADDRESS,
&multicast.address)) {
LOG_ERR(
"%s:%d - Failed to parse IPv6 multicast address: %s", THIS_FILE,
__LINE__, CONFIG_BACDL_BIP6_MCAST_ADDRESS);
}
bip6_set_addr(&unicast);
bip6_set_broadcast_addr(&multicast);
LOG_INF(" Unicast: %s", inet6_ntoa((struct in6_addr*)&unicast.address));
LOG_INF(" Multicast: %s", inet6_ntoa((struct in6_addr*)&multicast.address));
}
else
{
LOG_INF(
" Unicast: %s", inet6_ntoa((struct in6_addr *)&unicast.address));
LOG_INF(
" Multicast: %s",
inet6_ntoa((struct in6_addr *)&multicast.address));
} else {
LOG_ERR("%s:%d - Failed to set interface", THIS_FILE, __LINE__);
}
}
bool bip6_init(char *ifname)
{
LOG_INF("bip6_init()");
@@ -493,19 +500,19 @@ bool bip6_init(char *ifname)
bip6_set_interface(ifname);
if (BIP6_Address.s6_addr == 0) {
LOG_ERR("%s:%d - Failed to get an IPv6 address on interface: %s\n", THIS_FILE, __LINE__, ifname ? ifname : "[default]");
LOG_ERR(
"%s:%d - Failed to get an IPv6 address on interface: %s\n",
THIS_FILE, __LINE__, ifname ? ifname : "[default]");
return false;
}
/* assumes that the driver has already been initialized */
sock_fd = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
BIP6_Socket = sock_fd;
if (sock_fd < 0) {
if (sock_fd < 0) {
LOG_ERR("%s:%d - Failed to create socket", THIS_FILE, __LINE__);
return false;
}
else
{
} else {
LOG_INF("Socket created");
}
@@ -526,16 +533,14 @@ bool bip6_init(char *ifname)
LOG_INF("Binding to port %d", ntohs(BIP6_Port));
status =
zsock_bind(sock_fd, (const struct sockaddr*)&sin6, sizeof(struct sockaddr));
status = zsock_bind(
sock_fd, (const struct sockaddr *)&sin6, sizeof(struct sockaddr));
if (status < 0) {
zsock_close(sock_fd);
BIP6_Socket = -1;
LOG_ERR("%s:%d - zsock_bind() failure", THIS_FILE, __LINE__);
return false;
}
else
{
} else {
LOG_INF("Socket bound");
}