tpt-protocol-core
Core traits for the TPT Protocol — FrameParser, StateValidator, DataNormalizer, and the shared Error type.
This crate defines the universal traits that every protocol crate in the
suite implements. They provide a single, consistent interface for frame
parsing, connection-state validation, and normalization into the TUP
envelope (tpt-protocol-tup), so that any protocol can be driven by the
daemon, the CLI, and the UI tools through one code path.
Status / Conformance
- Version:
0.1.0(initial release). - Memory safety:
#![no_std]. The only heap allocations are theVecs returned byDataNormalizer::normalize_all,normalize_batchandFrameEncoder::encode, which usealloc. - Conformance: implements the three core traits specified in
spec §7 of the project specification. The
ProtocolPlugintrait (used by the daemon's plugin architecture) is provided behind thestdfeature. - The crate ships with unit tests covering each trait and is part of the
workspace-wide
proptestfuzz harness that assertsFrameParser::parsenever panics on arbitrary input.
Traits
All three parser/normalizer traits carry an associated Frame type that
must be Debug + Clone + PartialEq, so parsed frames can be inspected,
compared, and batched.
FrameParser
Parses raw bytes into a protocol-specific frame.
parseis the only required method — it converts a byte slice intoFrameor returns anError.try_parsehas a default implementation that returns the whole frame and the number of bytes consumed (bytes.len()). Protocol crates may override it to report how many bytes of a larger buffer were actually consumed, enabling incremental/streaming decoding.
StateValidator
Validates the health/state of a protocol connection.
validate returns Ok(()) when the connection is healthy and an Error
otherwise. State is protocol-defined (e.g. an online/offline flag or a
link-status word).
DataNormalizer
Normalizes a parsed frame into a TUP DataPoint.
normalizeis required — it maps aFrameinto a TUPDataPoint(tpt-protocol-tup).normalize_allreturns every point a frame carries. Its default implementation returns the single point fromnormalize. Protocols whose frames carry several values override it: Sparkplug B (one per metric), Matter (one per attribute), NMEA (one per decoded field) and IEEE 11073 SDC (one per numeric observation).normalize_batchconcatenatesnormalize_allfor each frame, so a batch fans out. A frame with several values contributes all of them.
FrameEncoder
The inverse of FrameParser: it writes a frame as wire bytes.
Most protocol crates implement it, and their tests check that parse(encode(frame))
gives the frame back. Not every crate does: BACnet, Matter and MAVLink do not yet.
ProtocolPlugin (behind the std feature)
The plugin trait used by tpt-protocol-daemon. Implementations produce a
futures stream of TUP DataPoint values (from simulated or real hardware).
ProtocolPlugin is only available when the std feature is enabled, which
pulls in futures-util. The daemon wires one ProtocolPlugin per protocol
crate (see tpt-protocol-daemon/src/plugins/mod.rs).
The Error type
Every trait method returns Result<T, Error>. The enum is #[non_exhaustive]
so new variants can be added in a semver-compatible way:
Each string variant has a matching constructor for ergonomic construction without importing the enum variant:
| Constructor | Produces |
|---|---|
Error::parse(msg) |
Error::Parse |
Error::state(msg) |
Error::State |
Error::normalize(msg) |
Error::Normalize |
Error::cbor(msg) |
Error::Cbor |
Error::transport(msg) |
Error::Transport |
Error::config(msg) |
Error::Config |
Error::checksum(expected, actual, msg) |
Error::Checksum |
Error::overflow(max, actual, msg) |
Error::Overflow |
Error::invalid_frame(reason, offset) |
Error::InvalidFrame |
Error implements Display (and std::error::Error under the std
feature), and converts from core::str::Utf8Error. The structured variants
(Checksum, Overflow, InvalidFrame) carry machine-readable detail that
protocol crates use to report exactly where and why a frame was rejected.
no_std story
The crate is #![no_std], and its default features are empty. It requires only alloc
(no full std). The parser, validator, normalizer and encoder traits and the Error type
are all available in a bare-metal/no_std context. The std feature (off by default)
additionally enables tpt-protocol-tup/std and futures-util,
which brings in the ProtocolPlugin trait for the daemon's async streaming
model. To use the crate without std:
= { = "0.1", = false }
(Note: tpt-protocol-tup, which DataNormalizer/ProtocolPlugin depend
on for DataPoint, also has a no_std story of its own — see its README.)
Writing a custom protocol
To add a new protocol to the ecosystem, implement the three core traits for
your wire format. A complete, runnable example lives at
examples/custom_protocol.rs (cargo run -p tpt-protocol-core --example custom_protocol) — it defines a hypothetical "SensorX" protocol:
use ;
use ;
;
;
;
Once a protocol implements these traits, the daemon can drive it by adding a
ProtocolPlugin entry (behind the std feature) — see
tpt-protocol-daemon/src/plugins/mod.rs. cargo xtask new-protocol <name>
scaffolds the crate and does that wiring; see
docs/bring-your-own-protocol.md.
Crate layout
crates/tpt-protocol-core/
├── Cargo.toml
├── README.md
├── CHANGELOG.md
├── src/
│ ├── lib.rs # FrameParser, StateValidator, DataNormalizer, ProtocolPlugin
│ └── error.rs # Error enum + constructors + Display/From impls
└── examples/
└── custom_protocol.rs # Run a full custom-protocol example
Related crates
tpt-protocol-tup— the TPT Unified Protocol envelope (DataPoint,TupValue,Quality,SourceProtocol) thatDataNormalizerproduces.tpt-protocol-daemon— background service that drives every protocol crate through theProtocolPlugintrait.- The 19 protocol crates (
tpt-protocol-modbus,tpt-protocol-j1939, …) each implement the three core traits and register aProtocolPlugin.
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.