Summary
Implement aimdb-knx-connector to enable real-time monitoring and control of KNX building automation networks. The connector will support bidirectional communication with KNX/IP gateways using the proven ConnectorBuilder + Router pattern from the MQTT connector.
Motivation
KNX is the leading open standard for building automation, controlling lighting, HVAC, security, and energy management in commercial and residential installations worldwide. Integrating KNX with AimDB enables:
- Edge Analytics: Real-time monitoring on Raspberry Pi or embedded devices
- Cloud Integration: Seamless bridging between KNX and MQTT/HTTP/Kafka
- Type Safety: KNX telegrams become strongly-typed Rust records
- Unified Runtime: Same code runs on MCU (Embassy) and servers (Tokio)
Scope
Phase 1: Core Implementation (4-6 weeks)
Deliverables:
- ✅ KNXnet/IP Tunneling protocol support (UDP port 3671)
- ✅ Inbound monitoring: KNX bus → AimDB records (via Router)
- ✅ Outbound control: AimDB records → KNX bus (via publish)
- ✅ Group address parsing (3-level format: main/middle/sub)
- ✅ Basic DPT type parsing (bool, u8, u16, f32)
- ✅ Tokio runtime support (
tokio-runtime feature)
- ✅ Embassy runtime support (
embassy-runtime feature)
- ✅ Automatic reconnection on connection loss
- ✅ Example:
tokio-knx-demo (Tokio)
- ✅ Example:
embassy-knx-demo (Embassy)
Out of Scope (Future):
- Extended DPT type library (1-20 full coverage)
- Group address discovery
- ETS project import
- KNXnet/IP Secure
Technical Approach
Architecture Pattern
Follow the proven MQTT connector pattern exactly:
User Configuration:
.with_connector(KnxConnectorBuilder::new("knx://192.168.1.19:3671"))
.configure::<LightState>(|reg| {
reg.link_from("knx://1/0/7") // Inbound
reg.link_to("knx://1/0/8") // Outbound
})
Build Phase:
1. CollectorBuilder collects inbound routes
2. RouterBuilder creates Router (group_address → producer)
3. Spawn background task (UDP listener + router dispatch)
4. Spawn outbound publishers (consumer → serialize → publish)
Runtime:
Inbound: KNX telegram → parse → router.route() → producer → buffer
Outbound: Buffer → consumer → serializer → connector.publish() → GroupValueWrite
Protocol Implementation
Connection Sequence:
- UDP socket bind to ephemeral port
- Send
CONNECT_REQUEST to gateway:3671
- Receive
CONNECT_RESPONSE with channel_id
- Loop:
- Receive
TUNNELING_REQUEST (bus telegrams)
- Parse cEMI frame (message code 0x29 = L_Data.ind)
- Extract group address and payload
- Dispatch via
router.route(group_address, data)
- Send
TUNNELING_ACK (seq counter + channel_id)
- On error: reconnect with exponential backoff
Frame Parsing:
- Header: 6 bytes (length, version, service type, total length)
- Connection header: 4 bytes (length, channel_id, seq_counter, reserved)
- cEMI: variable (control fields, addresses, NPDU, payload)
Code Structure
aimdb-knx-connector/
├── Cargo.toml
├── src/
│ ├── lib.rs # GroupAddress, KnxValue, URL parsing
│ ├── tokio_client.rs # Tokio implementation (std)
│ └── embassy_client.rs # Embassy implementation (no_std)
└── examples/
├── tokio-knx-demo/ # Tokio demo
└── embassy-knx-demo/ # Embassy demo
Implementation Checklist
Core Types (lib.rs)
ConnectorBuilder (tokio_client.rs + embassy_client.rs)
Connector Implementation (both runtimes)
Protocol Implementation (shared logic)
Testing
Documentation
Testing Strategy
Manual Testing Requirements
Hardware:
- KNX/IP gateway (e.g., SCN-IP000.03, Gira X1, ABB IP Interface)
- Active KNX bus with devices (lights, switches, sensors)
Test Procedure:
# 1. Run monitor example
cd examples/tokio-knx-demo
cargo run
# Expected output:
# ✅ KNX connected, channel_id: 42
# Collected 1 inbound KNX routes
# KNX router has 1 group addresses
# 2. Trigger KNX bus activity (press switch, send telegram)
# Expected output:
# 💡 Light 1/0/7: ON
# 💡 Light 1/0/7: OFF
# 3. Test reconnection (disconnect gateway, reconnect)
# Expected output:
# ❌ Connection failed: ..., retrying in 5s
# ✅ KNX connected, channel_id: 43
Success Criteria
Reference Materials
Implementation Reference:
aimdb-mqtt-connector/ - Complete pattern reference (follow exactly)
aimdb-core/src/connector.rs - Connector trait
aimdb-core/src/router.rs - Router implementation
examples/tokio-mqtt-connector-demo/ - Tokio patterns
Protocol Specifications:
- KNXnet/IP Core v1.0 specification
- cEMI specification
- DPT (Data Point Types) reference
Design Documents:
docs/design/aimdb-knx-connector-task.md - Complete implementation guide
docs/design/012-M5-connector-development-guide.md - Generic connector patterns
Hardware Documentation:
- SCN-IP000.03 (MDT) - Primary test device
Dependencies
New Dependencies:
[features]
default = ["std"]
std = ["aimdb-core/std"]
tokio-runtime = ["std", "tokio", "aimdb-tokio-adapter"]
embassy-runtime = ["aimdb-core/alloc", "embassy-net", "embassy-sync", "aimdb-embassy-adapter"]
[dependencies]
aimdb-core = { path = "../aimdb-core" }
tokio = { workspace = true, optional = true }
embassy-net = { workspace = true, optional = true }
embassy-sync = { workspace = true, optional = true }
static-cell = { version = "2.0", optional = true }
[dev-dependencies]
aimdb-tokio-adapter = { path = "../aimdb-tokio-adapter" }
tokio = { workspace = true, features = ["full"] }
tracing-subscriber = "0.3"
No External KNX Crates:
- Rationale: Limited options with GPL licensing or poor maintenance
- Direct UDP implementation gives full control and no_std compatibility
- Protocol is well-documented and validated against hardware
Risks and Mitigations
| Risk |
Impact |
Mitigation |
| Protocol complexity |
High |
Reference implementation validated against hardware |
| Gateway compatibility |
Medium |
Test with multiple gateway models (SCN-IP000.03, Gira X1) |
| Connection stability |
Medium |
Robust reconnection logic in background task |
| Embassy network stack integration |
Medium |
Follow MQTT connector pattern, use SendFutureWrapper, test on real hardware |
Timeline Estimate
Phase 1: Core Implementation (4-6 weeks)
- Week 1: Core types, URL parsing, protocol frames (shared)
- Week 2: Tokio implementation (ConnectorBuilder, background task, connection)
- Week 3: Router integration, telegram parsing, dispatch (Tokio)
- Week 4: Embassy implementation (network stack, SendFutureWrapper patterns)
- Week 5: Outbound publishing (both runtimes), testing, refinement
- Week 6: Documentation, examples (both runtimes), polish
Acceptance Criteria
Summary
Implement
aimdb-knx-connectorto enable real-time monitoring and control of KNX building automation networks. The connector will support bidirectional communication with KNX/IP gateways using the provenConnectorBuilder+Routerpattern from the MQTT connector.Motivation
KNX is the leading open standard for building automation, controlling lighting, HVAC, security, and energy management in commercial and residential installations worldwide. Integrating KNX with AimDB enables:
Scope
Phase 1: Core Implementation (4-6 weeks)
Deliverables:
tokio-runtimefeature)embassy-runtimefeature)tokio-knx-demo(Tokio)embassy-knx-demo(Embassy)Out of Scope (Future):
Technical Approach
Architecture Pattern
Follow the proven MQTT connector pattern exactly:
Protocol Implementation
Connection Sequence:
CONNECT_REQUESTto gateway:3671CONNECT_RESPONSEwith channel_idTUNNELING_REQUEST(bus telegrams)router.route(group_address, data)TUNNELING_ACK(seq counter + channel_id)Frame Parsing:
Code Structure
Implementation Checklist
Core Types (lib.rs)
GroupAddressstruct with 3-level parsing and encodingKnxValuestruct with DPT type helpersparse_knx_url()function forknx://host:portformatKnxConfigstruct for gateway connection infoKnxErrorenum)ConnectorBuilder (tokio_client.rs + embassy_client.rs)
KnxConnectorBuilderstruct (both runtimes)ConnectorBuilder<R>trait:build(): collect routes, create router, spawn tasksscheme(): return "knx"db.collect_inbound_routes("knx")Routerfrom inbound routesdb.collect_outbound_routes("knx")spawn_outbound_publishers()Connector Implementation (both runtimes)
KnxConnectorImplstruct with routernew(): spawn background listener taskspawn_outbound_publishers(): spawn consumer tasksConnectortrait:publish(): send GroupValueWrite telegramtokio::net::UdpSocketandtokio::spawnembassy_net::udp::UdpSocketandSendFutureWrapperProtocol Implementation (shared logic)
KnxConnectionstruct (socket, channel_id, seq_counter)connect_and_listen(): full connection lifecyclebuild_connect_request(): construct CONNECT_REQUEST frameparse_connect_response(): extract channel_id and statusbuild_tunneling_ack(): construct ACK frameparse_telegram(): extract group address and payload from cEMIis_tunneling_request(): frame type detectionsend_group_write(): construct GroupValueWrite frame (outbound)Testing
tokio-knx-demowith tap() observer (Tokio)embassy-knx-demo(Embassy on STM32/ESP32)Documentation
Testing Strategy
Manual Testing Requirements
Hardware:
Test Procedure:
Success Criteria
Reference Materials
Implementation Reference:
aimdb-mqtt-connector/- Complete pattern reference (follow exactly)aimdb-core/src/connector.rs- Connector traitaimdb-core/src/router.rs- Router implementationexamples/tokio-mqtt-connector-demo/- Tokio patternsProtocol Specifications:
Design Documents:
docs/design/aimdb-knx-connector-task.md- Complete implementation guidedocs/design/012-M5-connector-development-guide.md- Generic connector patternsHardware Documentation:
Dependencies
New Dependencies:
No External KNX Crates:
Risks and Mitigations
SendFutureWrapper, test on real hardwareTimeline Estimate
Phase 1: Core Implementation (4-6 weeks)
Acceptance Criteria
cargo build --features tokio-runtimecargo build --features embassy-runtimecargo build --target thumbv7em-none-eabihf --features embassy-runtimecargo test --all-featurescargo clippy --all-targets --all-features -- -D warningscargo fmt --all -- --check