bip: decouple broadcast destination port from bind port (#1311)

Co-authored-by: Leunar Kalludra <Leunar.Kalludra@de.bosch.com>
This commit is contained in:
Leunar Kalludra
2026-04-24 13:40:32 +02:00
committed by GitHub
parent 8607b241e9
commit f616a8cc68
22 changed files with 469 additions and 17 deletions
@@ -0,0 +1,63 @@
# 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/ports/[a-zA-Z_/-]*$"
"/src"
SRC_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/ports/[a-zA-Z_/-]*$"
"/ports"
PORTS_DIR
${CMAKE_CURRENT_SOURCE_DIR})
string(REGEX REPLACE
"/test/ports/[a-zA-Z_/-]*$"
"/test"
TST_DIR
${CMAKE_CURRENT_SOURCE_DIR})
set(ZTST_DIR "${TST_DIR}/ztest/src")
add_compile_definitions(
BACNET_BIG_ENDIAN=0
CONFIG_ZTEST=1
BACNET_STACK_DEPRECATED_DISABLE=1
)
include_directories(
${SRC_DIR}
${TST_DIR}/ztest/include
)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
set(PORT_BIP_SOURCE ${PORTS_DIR}/linux/bip-init.c)
find_package(Threads REQUIRED)
elseif(WIN32)
set(PORT_BIP_SOURCE ${PORTS_DIR}/win32/bip-init.c)
add_compile_definitions(BACNET_PORT=win32)
elseif(APPLE)
set(PORT_BIP_SOURCE ${PORTS_DIR}/bsd/bip-init.c)
else()
message(FATAL_ERROR "bip_broadcast_port test is only supported on Linux, macOS, and Windows")
endif()
add_executable(${PROJECT_NAME}
${PORT_BIP_SOURCE}
${SRC_DIR}/bacnet/basic/sys/debug.c
./src/bvlc_stubs.c
./src/main.c
${ZTST_DIR}/ztest_mock.c
${ZTST_DIR}/ztest.c
)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_link_libraries(${PROJECT_NAME} Threads::Threads)
elseif(WIN32)
target_link_libraries(${PROJECT_NAME} ws2_32)
endif()
@@ -0,0 +1,48 @@
/* SPDX-License-Identifier: MIT */
#include "bacnet/basic/bbmd/h_bbmd.h"
int bvlc_handler(
BACNET_IP_ADDRESS *addr,
BACNET_ADDRESS *src,
uint8_t *npdu,
uint16_t npdu_len)
{
(void)addr;
(void)src;
(void)npdu;
(void)npdu_len;
return 0;
}
int bvlc_broadcast_handler(
BACNET_IP_ADDRESS *addr,
BACNET_ADDRESS *src,
uint8_t *npdu,
uint16_t npdu_len)
{
(void)addr;
(void)src;
(void)npdu;
(void)npdu_len;
return 0;
}
int bvlc_send_pdu(
const BACNET_ADDRESS *dest,
const BACNET_NPDU_DATA *npdu_data,
const uint8_t *pdu,
unsigned pdu_len)
{
(void)dest;
(void)npdu_data;
(void)pdu;
(void)pdu_len;
return 0;
}
void bvlc_init(void)
{
}
+62
View File
@@ -0,0 +1,62 @@
/**
* @file
* @brief Tests for separating BACnet/IP bind and broadcast destination ports
* @copyright SPDX-License-Identifier: MIT
*/
#include <zephyr/ztest.h>
#include <bacnet/datalink/bip.h>
static uint16_t bip_address_port(const BACNET_ADDRESS *address)
{
return ((uint16_t)address->mac[4] << 8) | address->mac[5];
}
static void test_broadcast_port_defaults_to_bind_port(void)
{
BACNET_ADDRESS broadcast = { 0 };
BACNET_IP_ADDRESS broadcast_addr = { 0 };
bip_cleanup();
bip_set_port(0xBAC1U);
zassert_equal(bip_get_port(), 0xBAC1U, NULL);
zassert_equal(bip_get_broadcast_port(), 0xBAC1U, NULL);
bip_get_broadcast_address(&broadcast);
zassert_equal(bip_address_port(&broadcast), 0xBAC1U, NULL);
zassert_true(bip_get_broadcast_addr(&broadcast_addr), NULL);
zassert_equal(broadcast_addr.port, 0xBAC1U, NULL);
}
static void test_broadcast_port_can_differ_from_bind_port(void)
{
BACNET_ADDRESS local = { 0 };
BACNET_ADDRESS broadcast = { 0 };
BACNET_IP_ADDRESS broadcast_addr = { 0 };
bip_cleanup();
bip_set_port(0xBAC1U);
bip_set_broadcast_port(0xBAC2U);
zassert_equal(bip_get_port(), 0xBAC1U, NULL);
zassert_equal(bip_get_broadcast_port(), 0xBAC2U, NULL);
bip_get_my_address(&local);
zassert_equal(bip_address_port(&local), 0xBAC1U, NULL);
bip_get_broadcast_address(&broadcast);
zassert_equal(bip_address_port(&broadcast), 0xBAC2U, NULL);
zassert_true(bip_get_broadcast_addr(&broadcast_addr), NULL);
zassert_equal(broadcast_addr.port, 0xBAC2U, NULL);
}
void test_main(void)
{
ztest_test_suite(
bip_broadcast_port_test,
ztest_unit_test(test_broadcast_port_defaults_to_bind_port),
ztest_unit_test(test_broadcast_port_can_differ_from_bind_port));
ztest_run_test_suite(bip_broadcast_port_test);
}