knust - a asynchronous KNX/IP Library for Rust
knust is a high-performance, memory-safe implementation of the KNX/IP protocol for building automation systems. It provides async/await support and strong type safety while maintaining compatibility with KNX standards.
Features
- Async/await support with tokio for non-blocking I/O
- Memory-safe protocol parsing with zero unsafe code in public API
- Multiple connection types: Tunneling (point-to-point) and Routing (multicast)
- KNX IP Secure and experimental KNX Data Security support
- Gateway discovery for automatic network configuration
- Comprehensive error handling with structured error types
- Property-based testing for correctness verification
- Performance monitoring and memory management
- Extensive logging with component-specific levels
Cargo Features
dpt(on by default) — datapoint-type encode/decode (DPT 1..251). Disable for a raw-frame-only client that never interprets group values.ets(off) — ETS CSV group-address import (parse_ets_csv). Impliesdpt.server(off) — act as a KNXnet/IP tunneling server (TunnelServer::bind). Most consumers are clients and don't need it — see Server below.secure(off) — KNX IP Secure (session handshake, verified against real hardware) and KNX Data Security (group encryption, experimental and unverified against a reference implementation), plus KNX keyring (.knxkeys) parsing/validation. Impliesets.
Quick Start
Add Knx to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["rt-multi-thread", "macros"] }
Basic Usage
There's no built-in device abstraction layer — you send telegrams and read
group values directly. See examples/custom_devices.rs
for a pattern to build a device layer on top of these primitives.
use ;
use ;
use ;
async
Builder Pattern
use Knx;
let knx = builder
.connection_type
.gateway_ip
.individual_address
.timeout_ms
.auto_reconnect
.build
.await?;
Connection Types
Tunneling (Point-to-Point)
Best for reliable communication with a single KNX/IP gateway:
let config = ConnectionConfig ;
Routing (Multicast)
Best for monitoring multiple devices on the network:
let config = ConnectionConfig ;
Secure Connections
For encrypted communication using KNX IP Secure (requires the secure
feature — without it, or without security set, connect() returns a
configuration error rather than silently falling back to plaintext):
use SecurityConfig;
let security_config = SecurityConfig ;
let config = ConnectionConfig ;
Reading Group Values
use Duration;
// Read a temperature sensor (DPT 9.001) and wait for the response
let payload = knx
.read_group_value
.await?;
let temperature = decode?;
println!;
Gateway Discovery
Automatically discover KNX/IP gateways on your network:
use GatewayScanner;
let scanner = new.await?;
let gateways = scanner.discover.await?;
for gateway in gateways
Security Features
The following require the secure feature.
KNX Data Security
Encrypt group communication using KNX Data Security. Experimental —
unlike KNX IP Secure (the session handshake above), this hasn't been
verified against a reference implementation or real hardware, and isn't
wired into the telegram send/receive path automatically; use
security::group::encrypt_group_payload/decrypt_group_payload directly
on APDU bytes:
use ;
let mut keyring = new;
// Add group keys
let group_key = from_hex?;
keyring.add_group_key;
// Check if group is secured
if keyring.is_group_secured
Sequence Number Validation
Protect against replay attacks:
let sender = new;
match keyring.validate_sequence
Logging and Debugging
Configure comprehensive logging for troubleshooting:
use ;
let mut logging_config = new;
logging_config.set_default_level;
logging_config.set_component_level;
logging_config.set_component_level;
logging_config.set_protocol_events;
logging_config.set_hex_dump;
init_logging;
Memory Management
Monitor and optimize memory usage:
// Get memory statistics
let stats = knx.memory_stats.await;
println!;
println!;
// Check memory bounds
if !knx.memory_within_bounds
// Force cleanup
let freed = knx.force_cleanup.await;
println!;
Error Handling
Knx provides comprehensive error types with context:
use KnxError;
match knx.connect.await
Examples
The repository includes examples covering common tasks (cargo run --example <name>,
some require the secure/ets features):
- custom_devices: recommended pattern for building a device layer on
send_telegram/read_group_value - send_telegrams: sending raw telegrams
- value_reader: reading and decoding group values
- gateway_discovery: discovering gateways on the network
- secure_connection: KNX IP Secure / Data Security
- configuration_examples:
ConnectionConfigvariations - logging_demo: component-level logging setup
- memory_optimization_demo: memory monitoring and cleanup
- tunneling_connection / tcp_connection_demo: UDP and TCP tunneling
- connection_handling: reconnect/backoff handling
- packet_listener / telegram_monitor: passive bus monitoring
- datetime_sync: sending DPT 10/11 time and date
- color_dpt_demo: RGB/RGBW/XYY color DPTs
- sequence_validation_demo: UDP sequence number handling
Run ls examples/ for the full, current list.
Configuration Files
The optional ets feature adds ETS CSV group-address import
(parse_ets_csv). The optional secure feature adds a KNX keyring
(.knxkeys) parser — see Configuration and KeyringParser in
knust::config. There is no ETS project (.knxproj/XML) import or generic
TOML/JSON application-config format.
Server
TunnelServer (this crate, server feature) acts as a KNXnet/IP
gateway: TunnelServer::bind(addr, individual_address).await? binds a
UDP+TCP tunneling endpoint and starts serving connecting clients
immediately (bind_secure additionally requires KNX IP Secure). It's a
software bridge endpoint, not a full line of real device addresses.
Testing
Knx includes comprehensive test coverage with both unit tests and property-based tests:
# Run all tests
# Run property-based tests
# Run integration tests
# Run with logging
RUST_LOG=debug
Performance
Knx is designed for high performance:
- Zero-copy parsing where possible
- Async I/O with tokio for scalability
- Memory pooling for connection management
- Hot path optimization for telegram processing
- Configurable memory limits and cleanup
Compatibility
- Rust Edition: 2024
- MSRV: 1.85.1
- KNX Standards: KNX/IP, KNX IP Secure; KNX Data Security is experimental (see crate docs)
- Platforms: Linux, macOS, Windows
- Async Runtime: tokio
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Based on the xknx Python library
- KNX Association for the protocol specifications
- Rust community for excellent async ecosystem