Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ build/

# Python bytecode from tests/ and wiretest/
__pycache__/
build-*/

# --datalink sc: the device's certificates and private key (--sc-cert-dir,
# default ./certs) and the CARI request/response zips. Never commit them.
certs/
*-cari-request.zip
*-cari-response.zip
67 changes: 65 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,72 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.3.0] - 2026-10-28

_No changes yet._
Built on CAS BACnet Stack 6.x (`d9f39406`, 6.0.23). One executable now runs on any
one of four BACnet data links, chosen at start-up with `--datalink`. The default,
`ipv4`, is the same device as 1.2.0.

### Added

- **`--datalink ipv4|ipv6|mstp|sc`**, with each data link's own options. Every
option is checked, and an option for a data link other than the chosen one is
an error rather than silently ignored. `--help` lists them all.
- **One Network Port object per data link**, only the active one's: 1 "Vermilion"
(BACnet/IP), 2 "Cerulean" (BACnet/IPv6), 3 "Amber" (MS/TP), 4 "Teal" (BACnet/SC).
- **BACnet/IPv6** (`--datalink ipv6`, `--port`, `--interface`). Network Port 2
serves the host's IPv6 address, prefix length, default gateway, DNS server and
link speed, and the stack serves the VMAC. The start-up I-Am and Who-Is go to the
multicast group FF02::BAC0. `tests/ipv6_probe.py` checks it from outside.
- **MS/TP** (`--datalink mstp`, `--mstp-port`, `--mstp-baud`, `--mstp-mac`,
`--mstp-max-master`, `--mstp-max-info-frames`) on a USB-to-RS-485 adapter. The
CAS BACnet Stack runs the MS/TP state machines; the example only moves bytes on
and off the serial port. Network Port 3's Max_Master and Max_Info_Frames are
writable as pending changes. The start-up I-Am and Who-Is wait until the station
has joined the token ring. The `m` key prints the MS/TP counters. The Device's
Max_APDU_Length_Accepted is 480 on MS/TP.
- **BACnet/SC node** (`--datalink sc`, `--sc-hub`, `--sc-failover-hub`,
`--sc-cert-dir`, `--import-cari`), built when libwebsockets, OpenSSL and zlib are
found (`-D BBC_SC=OFF` leaves it out). The certificates follow the B-SCHUB
example's CARI flow: the first start writes a certificate request and stops, and
`--import-cari` installs the Certificate Authority's response, printing each CA's
SHA-256 fingerprint to check out of band. Files 2-5 expose the certificates over
BACnet; ReinitializeDevice ACTIVATE_CHANGES installs new ones only if the device
could still connect with them, and the Network Port Command GENERATE_CSR_FILE
makes a new key and request. `tools/sign_cari_request.py` signs a request with a
test CA.
- ReinitializeDevice ACTIVATE_CHANGES on every data link, and the Network Port
Command DISCARD_CHANGES.
- A start-up warning while `DCC_PASSWORD` is empty: any BACnet client can then
silence or restart the device, and on BACnet/SC replace its certificates.
- `common/`: `SimpleUDP6` (a BACnet/IPv6 socket) and `SimpleSerial` (a serial port
for MS/TP), see `common/CHANGELOG.md`.

### Changed

- Reading the peer device (DS-RP-A, DS-RPM-A) runs on `ipv4` and `ipv6` only: on
MS/TP and BACnet/SC the CAS BACnet Stack does not hand the application the
answers. See `TODO.md`.
- The Notification Class recipient is preset on `ipv4` only; on the other data
links a client writes Recipient_List.
- `--deviceName` sets the name in the BACnet/SC certificate request as well.

### Fixed

- **Linux: BACnet/SC is built with the distribution's libwebsockets.** CMake now
finds Ubuntu's `LibwebsocketsConfig.cmake` as well as `libwebsockets-config.cmake`,
and the hub connector builds against libwebsockets 4.0 (Ubuntu 22.04), which
lacks `LWS_PROTOCOL_LIST_TERM`.
- `tools/sign_cari_request.py` says it needs `cryptography` 42 or later.

### Known limitations

- MS/TP was run on a live bus at 38400 baud only. 76800 baud is refused on Linux,
where termios has no such rate.
- The BACnet/SC failover hub, the certificate procedures over BACnet and writing
Max_Master / Max_Info_Frames have not been exercised against this example.
- As a BACnet/IPv6 BBMD this example would not pass its peers' broadcasts on: it
gives the stack no destination address.

## [1.2.0] - 2026-10-27

Expand Down
127 changes: 116 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,45 @@
# STATIC and DLL modes - see its CMakeLists.txt - but they need a prebuilt library
# and this example does not use them.)
#
# ONE executable, four data links: BACnet/IP (default), BACnet/IPv6, MS/TP and
# BACnet/SC, chosen at run time with --datalink. The stack's default (full) build
# already compiles all four. BACnet/IP, BACnet/IPv6 and MS/TP need nothing but the
# operating system. BACnet/SC also needs libwebsockets, OpenSSL and zlib (the
# WebSocket/TLS connection to the hub, and the certificate tools); they are found
# through vcpkg (VCPKG_ROOT) or the system. Without them the example still builds,
# and --datalink sc says how to rebuild with them. -D BBC_SC=OFF leaves them out on
# purpose.
#
# Build:
# cmake -B build -S .
# cmake --build build --config Release --parallel
# Run (from the example folder):
# ./build/BACnetExampleBBC (Linux/macOS)
# .\build\Release\BACnetExampleBBC.exe (Windows)
# ./build/BACnetExampleBBC [--datalink ipv4|ipv6|mstp|sc] (Linux/macOS)
# .\build\Release\BACnetExampleBBC.exe [--datalink ipv4|ipv6|mstp|sc] (Windows)
# =============================================================================

cmake_minimum_required(VERSION 3.15)

# --- BACnet/SC (optional) --------------------------------------------------------
# ON (the default) means "build BACnet/SC if its libraries can be found". It is
# not an error when they cannot: the example is then built without it.
option(BBC_SC "Build the BACnet/SC data link (needs libwebsockets, OpenSSL and zlib)" ON)

# vcpkg toolchain auto-detection - only when BACnet/SC is wanted, and only if the
# caller did not choose a toolchain. Must happen BEFORE project(). With VCPKG_ROOT
# set (Visual Studio bundles a vcpkg), vcpkg.json's libraries are installed into
# the build folder at configure time. A machine without vcpkg skips all of this.
if(BBC_SC AND NOT DEFINED CMAKE_TOOLCHAIN_FILE AND DEFINED ENV{VCPKG_ROOT}
AND EXISTS "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "Vcpkg toolchain file")
# On Windows, the static triplet: libwebsockets/OpenSSL linked into the one
# executable, against the static C runtime (see CMAKE_MSVC_RUNTIME_LIBRARY below).
if(WIN32 AND NOT DEFINED VCPKG_TARGET_TRIPLET)
set(VCPKG_TARGET_TRIPLET "x64-windows-static" CACHE STRING "Vcpkg target triplet")
endif()
endif()

project(BACnetProfileExample_CPP_B_BC CXX C)


Expand Down Expand Up @@ -57,25 +87,95 @@ if(NOT EXISTS "${CAS_STACK_DIR}/adapters/cpp/CMakeLists.txt")
"Or pass -D CAS_STACK_DIR=/path/to/cas-bacnet-stack to use a stack elsewhere.")
endif()

# --- BACnet/SC libraries ---------------------------------------------------------
set(BBC_SC_FOUND OFF)
if(BBC_SC)
# vcpkg installs libwebsocketsConfig.cmake; Debian and Ubuntu install
# LibwebsocketsConfig.cmake, and Linux file names are case-sensitive.
find_package(libwebsockets CONFIG QUIET NAMES libwebsockets Libwebsockets)
find_package(OpenSSL QUIET)
find_package(ZLIB QUIET)
find_package(Threads QUIET)
if(libwebsockets_FOUND AND OpenSSL_FOUND AND ZLIB_FOUND AND Threads_FOUND)
set(BBC_SC_FOUND ON)
else()
message(STATUS "B-BC: BACnet/SC is NOT built - libwebsockets, OpenSSL or zlib was not found "
"(set VCPKG_ROOT, or install them). The other data links are built.")
endif()
endif()
if(BBC_SC_FOUND)
message(STATUS "B-BC: BACnet/SC is built (libwebsockets, OpenSSL, zlib found)")
endif()

# --- MSVC runtime library (must match across the whole link) ----------------
# vcpkg's x64-windows-static triplet builds libwebsockets/OpenSSL against the
# static CRT (/MT). When this build links them, compile the stack and this example
# the same way - set BEFORE add_subdirectory(adapter) so the stack's own SOURCE-mode
# compile picks it up - or the link fails with LNK2038 MD-vs-MT mismatches.
if(MSVC AND BBC_SC_FOUND AND VCPKG_TARGET_TRIPLET MATCHES "-static$")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()

# Compile each target's files in parallel with MSVC too (/MP): the stack is ~600
# files. Before add_subdirectory() so the stack's target gets it.
if(MSVC)
add_compile_options(/MP)
endif()

# --- The CAS BACnet Stack, via its CMake adapter target ---------------------
# CAS_BACNET_STACK_LINK is the adapter's own cache variable (SOURCE/STATIC/DLL,
# default SOURCE, which is what this example builds with). To override it, set it
# BEFORE add_subdirectory() so the adapter target picks it up. See
# submodules/cas-bacnet-stack/adapters/cpp/CMakeLists.txt for what each mode does.
# The stack is built with its default options (source/CIBuildSettingsDefaults.h):
# every object type and every data link - BACnet/IP, BACnet/IPv6, MS/TP, BACnet/SC.
add_subdirectory("${CAS_STACK_DIR}/adapters/cpp" cas-bacnet-stack-adapter)

# --- The shared example helper ----------------------------------------------
set(COMMON_SOURCES
"${EXAMPLES_COMMON_DIR}/CASExampleHelper.cpp"
"${EXAMPLES_COMMON_DIR}/SimpleUDP.cpp")
"${EXAMPLES_COMMON_DIR}/SimpleUDP.cpp" # BACnet/IP (--datalink ipv4)
"${EXAMPLES_COMMON_DIR}/SimpleUDP6.cpp" # BACnet/IPv6 (--datalink ipv6)
"${EXAMPLES_COMMON_DIR}/SimpleSerial.cpp") # MS/TP (--datalink mstp)

# --- The executable ---------------------------------------------------------
add_executable(BACnetExampleBBC
# --- This example's own sources ---------------------------------------------
set(EXAMPLE_SOURCES
main.cpp
${COMMON_SOURCES})
command_line.cpp # --datalink and every other option, --help
ipv6/LocalIPv6Address.cpp # the host's IPv6 address for the IPv6 Network Port
mstp/MstpDataLink.cpp) # the stack's MS/TP byte callbacks on a serial port

# BACnet/SC: the hub connection (libwebsockets client) and the certificate files
# (CARI request/response, the certificate File objects) - see sc/README.md.
set(SC_SOURCES
sc/ScNode.cpp
sc/ScHubConnector.cpp
sc/KeyPassword.cpp
sc/cari.cpp
sc/cert_layout.cpp
sc/cert_tool.cpp
sc/cert_store.cpp)

# --- The executable ---------------------------------------------------------
if(BBC_SC_FOUND)
add_executable(BACnetExampleBBC ${EXAMPLE_SOURCES} ${COMMON_SOURCES} ${SC_SOURCES})
target_compile_definitions(BACnetExampleBBC PRIVATE BBC_WITH_SC)
# The libwebsockets package names its target "websockets" (static) or
# "websockets_shared" (some Linux distributions ship only that one).
if(TARGET websockets)
set(BBC_WEBSOCKETS_TARGET websockets)
else()
set(BBC_WEBSOCKETS_TARGET websockets_shared)
endif()
target_link_libraries(BACnetExampleBBC PRIVATE
${BBC_WEBSOCKETS_TARGET} OpenSSL::SSL OpenSSL::Crypto ZLIB::ZLIB Threads::Threads)
else()
add_executable(BACnetExampleBBC ${EXAMPLE_SOURCES} ${COMMON_SOURCES})
endif()

target_include_directories(BACnetExampleBBC PRIVATE
"${EXAMPLES_COMMON_DIR}")
"${EXAMPLES_COMMON_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}")

target_link_libraries(BACnetExampleBBC PRIVATE CASBACnetStack::Adapter)

Expand All @@ -88,11 +188,16 @@ if(MSVC)
else()
set(EXAMPLE_WARNINGS "-Wall;-Wextra")
endif()
set_source_files_properties(main.cpp ${COMMON_SOURCES}
set_source_files_properties(${EXAMPLE_SOURCES} ${COMMON_SOURCES} ${SC_SOURCES}
PROPERTIES COMPILE_OPTIONS "${EXAMPLE_WARNINGS}")

# --- Platform sockets -------------------------------------------------------
# ws2_32 = Winsock (UDP); iphlpapi = GetAdaptersInfo (local broadcast address).
# --- Platform libraries -----------------------------------------------------
# ws2_32 = Winsock (UDP); iphlpapi = GetAdaptersInfo / GetAdaptersAddresses (the
# local IPv4 and IPv6 addresses); winmm = timeBeginPeriod (1 ms sleeps for MS/TP).
# dbghelp is needed by libuv, which a static libwebsockets pulls in (BACnet/SC).
if(WIN32)
target_link_libraries(BACnetExampleBBC PRIVATE ws2_32 iphlpapi)
target_link_libraries(BACnetExampleBBC PRIVATE ws2_32 iphlpapi winmm)
if(BBC_SC_FOUND)
target_link_libraries(BACnetExampleBBC PRIVATE dbghelp)
endif()
endif()
Loading
Loading