refactor BACnet Address utility functions (#390)

* refactor BACnet Address utility functions

---------

Co-authored-by: Steve Karg <skarg@users.sourceforge.net>
This commit is contained in:
Steve Karg
2023-02-04 08:46:31 -06:00
committed by GitHub
parent 85976ee1f8
commit 236c2d1003
17 changed files with 490 additions and 221 deletions
+2 -2
View File
@@ -160,7 +160,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -173,7 +173,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
+3 -3
View File
@@ -259,7 +259,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -272,7 +272,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
@@ -435,4 +435,4 @@ int main(int argc, char *argv[])
}
return 0;
}
}
+2 -2
View File
@@ -163,7 +163,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -176,7 +176,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
+3 -3
View File
@@ -285,7 +285,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -298,7 +298,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
@@ -635,4 +635,4 @@ int main(int argc, char *argv[])
}
return 0;
}
}
+2 -2
View File
@@ -188,7 +188,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -201,7 +201,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
+3 -2
View File
@@ -289,7 +289,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -302,7 +302,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
@@ -349,6 +349,7 @@ int main(int argc, char *argv[])
}
address_init();
if (specific_address) {
bacnet_address_init(&dest, &mac, dnet, &adr);
if (adr.len && mac.len) {
memcpy(&dest.mac[0], &mac.adr[0], mac.len);
dest.mac_len = mac.len;
+2 -2
View File
@@ -384,7 +384,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -397,7 +397,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
+3 -2
View File
@@ -31,6 +31,7 @@
#include <ctype.h>
#include <errno.h>
#include "bacnet/basic/binding/address.h"
#include "bacnet/bacaddr.h"
#include "bacnet/bactext.h"
#include "bacnet/config.h"
#include "bacnet/bacdef.h"
@@ -185,7 +186,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
global_broadcast = false;
}
}
@@ -200,7 +201,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
global_broadcast = false;
}
}
+2 -2
View File
@@ -138,7 +138,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
specific_address = true;
}
}
@@ -151,7 +151,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
specific_address = true;
}
}
+2 -2
View File
@@ -385,7 +385,7 @@ int main(int argc, char *argv[])
}
if (strcmp(argv[argi], "--mac") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&mac, argv[argi])) {
if (bacnet_address_mac_from_ascii(&mac, argv[argi])) {
global_broadcast = false;
}
}
@@ -398,7 +398,7 @@ int main(int argc, char *argv[])
}
} else if (strcmp(argv[argi], "--dadr") == 0) {
if (++argi < argc) {
if (address_mac_from_ascii(&adr, argv[argi])) {
if (bacnet_address_mac_from_ascii(&adr, argv[argi])) {
global_broadcast = false;
}
}
+188 -27
View File
@@ -34,12 +34,20 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include "bacnet/config.h"
#include "bacnet/bacdef.h"
#include "bacnet/bacint.h"
#include "bacnet/bacaddr.h"
/** @file bacaddr.c BACnet Address structure utilities */
/**
* @brief Copy a #BACNET_ADDRESS value to another
* @param dest - #BACNET_ADDRESS to be copied into
* @param src - #BACNET_ADDRESS to be copied from
*/
void bacnet_address_copy(BACNET_ADDRESS *dest, BACNET_ADDRESS *src)
{
int i = 0;
@@ -57,45 +65,198 @@ void bacnet_address_copy(BACNET_ADDRESS *dest, BACNET_ADDRESS *src)
}
}
/**
* @brief Compare two #BACNET_ADDRESS values
* @param dest - #BACNET_ADDRESS to be compared
* @param src - #BACNET_ADDRESS to be compared
* @return true if the same values
*/
bool bacnet_address_same(BACNET_ADDRESS *dest, BACNET_ADDRESS *src)
{
uint8_t i = 0; /* loop counter */
uint8_t max_len = 0; /* used for dynamic max */
if (dest == src) { /* same ? */
if (!dest || !src) {
return false;
}
if (dest == src) {
return true;
}
if (dest->net != src->net) {
if (dest->mac_len != src->mac_len) {
return false;
}
if (dest->len != src->len) {
return false;
}
max_len = dest->len;
if (max_len > MAX_MAC_LEN) {
max_len = MAX_MAC_LEN;
}
for (i = 0; i < max_len; i++) {
if (dest->adr[i] != src->adr[i]) {
return false;
}
}
if (dest->net == 0) {
if (dest->mac_len != src->mac_len) {
return false;
}
max_len = dest->mac_len;
if (max_len > MAX_MAC_LEN) {
max_len = MAX_MAC_LEN;
}
for (i = 0; i < max_len; i++) {
for (i = 0; i < MAX_MAC_LEN; i++) {
if (i < dest->mac_len) {
if (dest->mac[i] != src->mac[i]) {
return false;
}
}
}
if (dest->net != src->net) {
return false;
}
/* if local, ignore remaining fields */
if (dest->net == 0) {
return true;
}
if (dest->len != src->len) {
return false;
}
for (i = 0; i < MAX_MAC_LEN; i++) {
if (i < dest->len) {
if (dest->adr[i] != src->adr[i]) {
return false;
}
}
}
return true;
}
/**
* @brief Configure a #BACNET_ADDRESS from mac, dnet, and adr
* @param dest - #BACNET_ADDRESS to be configured
* @param mac - #BACNET_MAC_ADDRESS used in configuration
* @param dnet - remote network number 0..65535 (0=local, 65535=broadcast)
* @param adr - #BACNET_MAC_ADDRESS behind the remote network
* @return true if configured
*/
bool bacnet_address_init(BACNET_ADDRESS *dest,
BACNET_MAC_ADDRESS *mac,
uint16_t dnet,
BACNET_MAC_ADDRESS *adr)
{
uint8_t i = 0; /* loop counter */
if (!dest) {
return false;
}
if (adr && adr->len && mac && mac->len) {
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->mac[i] = mac->adr[i];
}
dest->mac_len = mac->len;
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = adr->adr[i];
}
dest->len = adr->len;
dest->net = dnet;
} else if (mac && mac->len) {
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->mac[i] = mac->adr[i];
}
dest->mac_len = mac->len;
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0;
}
dest->len = 0;
dest->net = dnet;
} else {
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->mac[i] = 0;
}
dest->mac_len = mac->len;
for (i = 0; i < MAX_MAC_LEN; i++) {
dest->adr[i] = 0;
}
dest->len = 0;
dest->net = dnet;
}
return true;
}
/**
* @brief Compare two #BACNET_MAC_ADDRESS values
* @param dest - #BACNET_MAC_ADDRESS to be compared
* @param src - #BACNET_MAC_ADDRESS to be compared
* @return true if the same values
*/
bool bacnet_address_mac_same(BACNET_MAC_ADDRESS *dest, BACNET_MAC_ADDRESS *src)
{
uint8_t i = 0; /* loop counter */
if (!dest || !src) {
return false;
}
if (dest->len != src->len) {
return false;
}
for (i = 0; i < MAX_MAC_LEN; i++) {
if (i < dest->len) {
if (dest->adr[i] != src->adr[i]) {
return false;
}
}
}
return true;
}
/**
* @brief Initialize a BACNET_MAC_ADDRESS
* @param mac [out] BACNET_MAC_ADDRESS structure
* @param adr [in] address to initialize, null if empty
* @param len [in] length of address in bytes
*/
void bacnet_address_mac_init(BACNET_MAC_ADDRESS *mac, uint8_t *adr, uint8_t len)
{
uint8_t i = 0;
if (mac) {
if (adr && (len <= sizeof(mac->adr))) {
for (i = 0; i < len; i++) {
mac->adr[i] = adr[i];
}
mac->len = len;
} else {
mac->len = 0;
}
}
}
/**
* @brief Parse an ASCII string for a bacnet-address
* @param mac [out] BACNET_MAC_ADDRESS structure to store the results
* @param arg [in] nul terminated ASCII string to parse
* @return true if the address was parsed
*/
bool bacnet_address_mac_from_ascii(BACNET_MAC_ADDRESS *mac, const char *arg)
{
unsigned a[6] = { 0 }, p = 0;
uint16_t port = 0;
int c, i;
bool status = false;
if (!(mac && arg)) {
return false;
}
c = sscanf(arg, "%3u.%3u.%3u.%3u:%5u", &a[0], &a[1], &a[2], &a[3], &p);
if ((c == 4) || (c == 5)) {
mac->adr[0] = a[0];
mac->adr[1] = a[1];
mac->adr[2] = a[2];
mac->adr[3] = a[3];
if (c == 4) {
port = 0xBAC0U;
} else {
port = (uint16_t)p;
}
encode_unsigned16(&mac->adr[4], port);
mac->len = 6;
status = true;
} else {
c = sscanf(arg, "%2x:%2x:%2x:%2x:%2x:%2x", &a[0], &a[1], &a[2], &a[3],
&a[4], &a[5]);
if (c > 0) {
for (i = 0; i < c; i++) {
mac->adr[i] = a[i];
}
mac->len = c;
status = true;
}
}
return status;
}
+40 -30
View File
@@ -1,26 +1,26 @@
/**************************************************************************
*
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************/
*
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************/
#ifndef BACADDR_H
#define BACADDR_H
@@ -34,14 +34,24 @@
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
void bacnet_address_copy(
BACNET_ADDRESS * dest,
BACNET_ADDRESS * src);
BACNET_STACK_EXPORT
bool bacnet_address_same(
BACNET_ADDRESS * dest,
BACNET_ADDRESS * src);
BACNET_STACK_EXPORT
void bacnet_address_copy(BACNET_ADDRESS *dest, BACNET_ADDRESS *src);
BACNET_STACK_EXPORT
bool bacnet_address_same(BACNET_ADDRESS *dest, BACNET_ADDRESS *src);
BACNET_STACK_EXPORT
bool bacnet_address_init(BACNET_ADDRESS *dest,
BACNET_MAC_ADDRESS *mac,
uint16_t dnet,
BACNET_MAC_ADDRESS *adr);
BACNET_STACK_EXPORT
bool bacnet_address_mac_same(
BACNET_MAC_ADDRESS *dest, BACNET_MAC_ADDRESS *src);
BACNET_STACK_EXPORT
void bacnet_address_mac_init(
BACNET_MAC_ADDRESS *mac, uint8_t *adr, uint8_t len);
BACNET_STACK_EXPORT
bool bacnet_address_mac_from_ascii(BACNET_MAC_ADDRESS *mac, const char *arg);
#ifdef __cplusplus
}
+2 -126
View File
@@ -120,60 +120,6 @@ void address_own_device_id_set(uint32_t own_id)
Own_Device_ID = own_id;
}
/**
* @brief Check if the given source and destination address can be matched
* by checking the length, net and MAC address.
*
* @param dest Destination address
* @param src Source address
*
* @return true for a match, false otherwise
*/
bool address_match(BACNET_ADDRESS *dest, BACNET_ADDRESS *src)
{
uint8_t i = 0;
uint8_t max_len = 0;
if (dest == src) {
return (true);
}
if (dest->mac_len != src->mac_len) {
return false;
}
max_len = dest->mac_len;
if (max_len > MAX_MAC_LEN) {
max_len = MAX_MAC_LEN;
}
for (i = 0; i < max_len; i++) {
if (dest->mac[i] != src->mac[i]) {
return false;
}
}
if (dest->net != src->net) {
return false;
}
/* if local, ignore remaining fields */
if (dest->net == 0) {
return true;
}
if (dest->len != src->len) {
return false;
}
max_len = dest->len;
if (max_len > MAX_MAC_LEN) {
max_len = MAX_MAC_LEN;
}
for (i = 0; i < max_len; i++) {
if (dest->adr[i] != src->adr[i]) {
return false;
}
}
return true;
}
/**
* @brief Remove a device from the address list.
*
@@ -268,76 +214,6 @@ static struct Address_Cache_Entry *address_remove_oldest(void)
return (pCandidate);
}
/**
* Initialize a BACNET_MAC_ADDRESS
*
* @param mac [out] BACNET_MAC_ADDRESS structure
* @param adr [in] address to initialize, null if empty
* @param len [in] length of address in bytes
*/
void address_mac_init(BACNET_MAC_ADDRESS *mac, uint8_t *adr, uint8_t len)
{
uint8_t i = 0;
if (mac) {
if (adr && (len <= sizeof(mac->adr))) {
for (i = 0; i < len; i++) {
mac->adr[i] = adr[i];
}
mac->len = len;
} else {
mac->len = 0;
}
}
}
/** Parse an ASCII string for a bacnet-address
*
* @param mac [out] BACNET_MAC_ADDRESS structure to store the results
* @param arg [in] nul terminated ASCII string to parse
*
* @return true if the address was parsed
*/
bool address_mac_from_ascii(BACNET_MAC_ADDRESS *mac, const char *arg)
{
unsigned a[6] = { 0 }, p = 0;
uint16_t port = 0;
int c, i;
bool status = false;
if (!(mac && arg)) {
return false;
}
c = sscanf(arg, "%3u.%3u.%3u.%3u:%5u", &a[0], &a[1], &a[2], &a[3], &p);
if ((c == 4) || (c == 5)) {
mac->adr[0] = a[0];
mac->adr[1] = a[1];
mac->adr[2] = a[2];
mac->adr[3] = a[3];
if (c == 4) {
port = 0xBAC0U;
} else {
port = (uint16_t)p;
}
encode_unsigned16(&mac->adr[4], port);
mac->len = 6;
status = true;
} else {
c = sscanf(arg, "%2x:%2x:%2x:%2x:%2x:%2x", &a[0], &a[1], &a[2], &a[3],
&a[4], &a[5]);
if (c > 0) {
for (i = 0; i < c; i++) {
mac->adr[i] = a[i];
}
mac->len = c;
status = true;
}
}
return status;
}
#ifdef BACNET_ADDRESS_CACHE_FILE
/* File format:
DeviceID MAC SNET SADR MAX-APDU
@@ -367,7 +243,7 @@ static void address_file_init(const char *pFilename)
if (sscanf(line, "%7ld %79s %5u %79s %4u", &device_id,
&mac_string[0], &snet, &sadr_string[0],
&max_apdu) == 5) {
if (address_mac_from_ascii(&mac, mac_string)) {
if (bacnet_address_mac_from_ascii(&mac, mac_string)) {
src.mac_len = mac.len;
for (index = 0; index < MAX_MAC_LEN; index++) {
src.mac[index] = mac.adr[index];
@@ -375,7 +251,7 @@ static void address_file_init(const char *pFilename)
}
src.net = (uint16_t)snet;
if (snet) {
if (address_mac_from_ascii(&mac, sadr_string)) {
if (bacnet_address_mac_from_ascii(&mac, sadr_string)) {
src.len = mac.len;
for (index = 0; index < MAX_MAC_LEN; index++) {
src.adr[index] = mac.adr[index];
+6 -16
View File
@@ -29,8 +29,14 @@
#include <stdbool.h>
#include "bacnet/bacnet_stack_exports.h"
#include "bacnet/bacdef.h"
#include "bacnet/bacaddr.h"
#include "bacnet/readrange.h"
/* refactored utility functions - see bacaddr.c module */
#define address_mac_init(m,a,l) bacnet_address_mac_init(m,a,l)
#define address_mac_from_ascii(m,a) bacnet_address_mac_from_ascii(m,a)
#define address_match(d,s) bacnet_address_same(d,s)
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
@@ -83,11 +89,6 @@ extern "C" {
unsigned address_count(
void);
BACNET_STACK_EXPORT
bool address_match(
BACNET_ADDRESS * dest,
BACNET_ADDRESS * src);
BACNET_STACK_EXPORT
bool address_bind_request(
uint32_t device_id,
@@ -127,17 +128,6 @@ extern "C" {
void address_cache_timer(
uint16_t uSeconds);
BACNET_STACK_EXPORT
void address_mac_init(
BACNET_MAC_ADDRESS *mac,
uint8_t *adr,
uint8_t len);
BACNET_STACK_EXPORT
bool address_mac_from_ascii(
BACNET_MAC_ADDRESS *mac,
const char *arg);
BACNET_STACK_EXPORT
void address_protected_entry_index_set(uint32_t top_protected_entry_index);
BACNET_STACK_EXPORT
+1
View File
@@ -41,6 +41,7 @@ list(APPEND testdirs
bacnet/alarm_ack
bacnet/arf
bacnet/awf
bacnet/bacaddr
bacnet/bacapp
bacnet/bacdcode
bacnet/bacdevobjpropref
+43
View File
@@ -0,0 +1,43 @@
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
get_filename_component(basename ${CMAKE_CURRENT_SOURCE_DIR} NAME)
project(test_${basename}
VERSION 1.0.0
LANGUAGES C)
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z_/-]*$"
"/src"
SRC_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/bacnet/[a-zA-Z_/-]*$"
"/test"
TST_DIR
${CMAKE_CURRENT_SOURCE_DIR})
set(ZTST_DIR "${TST_DIR}/ztest/src")
add_compile_definitions(
BIG_ENDIAN=0
CONFIG_ZTEST=1
BACAPP_ALL
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
add_executable(${PROJECT_NAME}
# File(s) under test
${SRC_DIR}/bacnet/bacaddr.c
# Support files and stubs (pathname alphabetical)
${SRC_DIR}/bacnet/bacint.c
# Test and test library files
./src/main.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
+186
View File
@@ -0,0 +1,186 @@
/**
* @file
* @brief Unit test for BACNET_ADDRESS copy, init, compare
* @author Steve Karg <skarg@users.sourceforge.net>
* @date January 2023
*
* SPDX-License-Identifier: MIT
*/
#include <zephyr/ztest.h>
#include <bacnet/bacaddr.h>
/**
* @addtogroup bacnet_tests
* @{
*/
static void test_BACNET_ADDRESS(void)
{
BACNET_ADDRESS src = { 0 }, dest = { 0 };
BACNET_ADDRESS test_src = { 0 }, test_dest = { 0 };
BACNET_MAC_ADDRESS mac = { 0 }, adr = { 0 };
uint16_t dnet = 0;
bool status = false;
/* invalid parameters */
status = bacnet_address_same(NULL, &dest);
zassert_false(status, NULL);
status = bacnet_address_same(&dest, NULL);
zassert_false(status, NULL);
status = bacnet_address_same(NULL, NULL);
zassert_false(status, NULL);
/* happy day cases */
status = bacnet_address_same(&dest, &dest);
zassert_true(status, NULL);
bacnet_address_copy(&dest, &src);
status = bacnet_address_same(&dest, &src);
zassert_true(status, NULL);
status = bacnet_address_init(&dest, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_init(&src, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_same(&dest, &src);
zassert_true(status, NULL);
/* remote dnet is non-zero */
dnet = 1;
status = bacnet_address_init(&dest, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_init(&src, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_same(&dest, &src);
zassert_true(status, NULL);
/* different src->len */
bacnet_address_copy(&dest, &src);
src.len = 4;
status = bacnet_address_same(&dest, &src);
zassert_false(status, NULL);
/* large src->len */
src.len = MAX_MAC_LEN;
status = bacnet_address_same(&dest, &src);
zassert_false(status, NULL);
bacnet_address_copy(&dest, &src);
/* different src->dnet */
dnet = 12;
status = bacnet_address_init(&src, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_same(&dest, &src);
zassert_false(status, NULL);
/* init checking */
status = bacnet_address_init(NULL, &mac, dnet, &adr);
zassert_false(status, NULL);
mac.len = MAX_MAC_LEN;
status = bacnet_address_init(&dest, &mac, dnet, &adr);
zassert_true(status, NULL);
adr.len = MAX_MAC_LEN;
status = bacnet_address_init(&dest, &mac, dnet, &adr);
zassert_true(status, NULL);
/* remote dnet adr is different */
dnet = 1;
status = bacnet_address_init(&dest, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_init(&src, &mac, dnet, &adr);
src.adr[MAX_MAC_LEN-1] = 1;
status = bacnet_address_same(&dest, &src);
zassert_false(status, NULL);
/* mac_len is different */
dnet = 0;
status = bacnet_address_init(&dest, &mac, dnet, &adr);
zassert_true(status, NULL);
status = bacnet_address_init(&src, &mac, dnet, &adr);
src.mac_len = MAX_MAC_LEN;
dest.mac_len = MAX_MAC_LEN;
status = bacnet_address_same(&dest, &src);
zassert_true(status, NULL);
dest.mac_len = 1;
status = bacnet_address_same(&dest, &src);
zassert_false(status, NULL);
}
static void test_BACNET_MAC_ADDRESS(void)
{
BACNET_MAC_ADDRESS dest = { 0 }, src = { 0 };
uint8_t adr[MAX_MAC_LEN] = { 0 };
uint8_t len = MAX_MAC_LEN;
bool status = false;
const char *bip_ascii = "255.255.255.255:47808";
const char *bip_ascii_no_port = "255.255.255.255";
const char *ethernet_ascii = "f0:f1:f2:f3:f4:f5";
const char *mstp_ascii = "7F";
const char *vmac_ascii = "12:34:56";
bacnet_address_mac_init(&src, adr, len);
bacnet_address_mac_init(&dest, adr, len);
status = bacnet_address_mac_same(&dest, &src);
zassert_true(status, NULL);
status = bacnet_address_mac_same(NULL, &src);
zassert_false(status, NULL);
status = bacnet_address_mac_same(&dest, NULL);
zassert_false(status, NULL);
bacnet_address_mac_init(&src, NULL, 0);
bacnet_address_mac_init(&dest, NULL, 0);
status = bacnet_address_mac_same(&dest, &src);
zassert_true(status, NULL);
bacnet_address_mac_init(&src, adr, 1);
bacnet_address_mac_init(&dest, adr, 2);
status = bacnet_address_mac_same(&dest, &src);
zassert_false(status, NULL);
status = bacnet_address_mac_from_ascii(&dest, bip_ascii);
zassert_true(status, NULL);
zassert_equal(dest.len, 6, NULL);
zassert_equal(dest.adr[0], 255, NULL);
zassert_equal(dest.adr[1], 255, NULL);
zassert_equal(dest.adr[2], 255, NULL);
zassert_equal(dest.adr[3], 255, NULL);
status = bacnet_address_mac_from_ascii(&dest, bip_ascii_no_port);
zassert_true(status, NULL);
status = bacnet_address_mac_from_ascii(&dest, ethernet_ascii);
zassert_equal(dest.len, 6, NULL);
zassert_equal(dest.adr[0], 0xf0, NULL);
zassert_equal(dest.adr[1], 0xf1, NULL);
zassert_equal(dest.adr[2], 0xf2, NULL);
zassert_equal(dest.adr[3], 0xf3, NULL);
zassert_equal(dest.adr[4], 0xf4, NULL);
zassert_equal(dest.adr[5], 0xf5, NULL);
zassert_true(status, NULL);
status = bacnet_address_mac_from_ascii(&dest, mstp_ascii);
zassert_true(status, NULL);
zassert_equal(dest.len, 1, NULL);
zassert_equal(dest.adr[0], 0x7f, NULL);
status = bacnet_address_mac_from_ascii(&dest, vmac_ascii);
zassert_true(status, NULL);
zassert_equal(dest.len, 3, NULL);
zassert_equal(dest.adr[0], 0x12, NULL);
zassert_equal(dest.adr[1], 0x34, NULL);
zassert_equal(dest.adr[2], 0x56, NULL);
/* different MAC of same len */
status = bacnet_address_mac_from_ascii(&src, vmac_ascii);
zassert_true(status, NULL);
dest.adr[1] = 0x11;
dest.adr[2] = 0x22;
dest.adr[3] = 0x33;
status = bacnet_address_mac_same(&dest, &src);
zassert_false(status, NULL);
/* NULL parameters */
status = bacnet_address_mac_from_ascii(NULL, vmac_ascii);
zassert_false(status, NULL);
status = bacnet_address_mac_from_ascii(&dest, NULL);
zassert_false(status, NULL);
}
/**
* @}
*/
void test_main(void)
{
ztest_test_suite(bacnet_address_tests,
ztest_unit_test(test_BACNET_ADDRESS),
ztest_unit_test(test_BACNET_MAC_ADDRESS)
);
ztest_run_test_suite(bacnet_address_tests);
}