tpt-protocol-tup
TPT Unified Protocol (TUP) — the canonical normalized schema for all TPT Protocol data.
This crate defines the TUP message envelope that all 19 protocol crates
normalize into. A DataPoint is a single named, timestamped, quality-tagged
observation from one device over one protocol. It carries:
- Rust structs that mirror the TUP CDDL schema documented in
SPEC-TUP.mdandschema/tup-v1.cddl - CBOR binary encoding/decoding via
minicbor(#![no_std]compatible; needsalloc) - A JSON mirror text encoding for daemon/UI/WebSocket use (requires the
stdfeature)
TUP is deliberately small and self-contained: every protocol parser in the
suite produces tpt_protocol_tup::DataPoint, and every UI (CLI, embedded, web)
consumes the same type. CBOR is the canonical wire format; the JSON mirror is
isomorphic and validated against schema/tup-v1.schema.json in tests.
Features
| Feature | Default | Description |
|---|---|---|
std |
no | Enables the JSON module (serde_json, hex) and minicbor/std. |
alloc |
no | Enables the CBOR encoder and decoder (they encode into a Vec) and minicbor/alloc. |
#![no_std]with default features empty. The CBOR encoder needsalloc. The JSON module needsstd.- Const
schema_version()(currently1) on everyDataPointfor forward compatibility; decoders reject a frame whoseschema_versionexceeds the compiled maximum. - Round-trip property tests for every value type and quality flag.
- A
jsonschemadev-dependency test that validates theSPEC-TUP.mdworked example againstschema/tup-v1.schema.json.
Architecture
protocol crate (e.g. tpt-protocol-modbus)
│ DataNormalizer::normalize(...)
▼
DataPoint { source_protocol, device_id, point_id,
timestamp, value, unit, quality, raw }
│
┌─────────────┴──────────────┐
▼ ▼
cbor::encode_to_vec json::to_json_string(_pretty)
cbor::decode_from_bytes json::from_json_str
(canonical wire format) (UI / WebSocket mirror)
The in-memory model (lib.rs) is independent of any codec. Each codec
(cbor.rs, json.rs) is a thin (de)serializer over the same struct set:
cbor.rsencodesDataPointas a CBOR map with stable integer keys (KEY_SCHEMA_VERSION = 0…KEY_RAW = 8).TupValueis encoded as a 2-element[type_tag, payload]array so the wire format stays self-describing.json.rs(behindstd) mapsDataPointto aserde_jsondocument with stringsource_protocol/qualityfields. The optionalrawfield is a0x-prefixed hex string. ABytesvalue is{"bytes": "0x…"}, and a non-finite float is{"float": "NaN" | "Infinity" | "-Infinity"}, because JSON has no bytes type and no NaN. A plain JSON string is alwaysText, so every value type survives the round trip. Invalid hex and unsupportedschema_versionvalues are errors. Known limit: the JSON parser reads integer literals above the u64 range as floats, so use CBOR for those.
DataPoint model
Construct it with DataPoint::new(...) and chain the builder helpers
with_unit, with_raw, with_timestamp, with_quality.
TUP value types (TupValue)
All five variants implement From<…> (From<i64>, From<f64>, From<bool>,
From<String>, From<&str>, From<Vec<u8>>, From<&[u8]>) so a DataPoint
can be built tersely: TupValue::from(42) or TupValue::Float(68.5).
Quality flags (Quality)
Each variant has an as_str() accessor and a FromStr / from_str_opt
parser; the string forms are exactly what the wire/JSON encodings emit.
Source protocol (SourceProtocol)
SourceProtocol enumerates all 19 supported protocols (Modbus … Coap,
including the private Mavlink/Ethercat) with as_str() and FromStr
round-tripping the kebab-case identifiers used on the wire
("modbus", "uds", "matter-thread", …).
Usage
Build, encode, and decode a DataPoint on the CBOR path:
use ;
let dp = new.with_unit;
let encoded = encode_to_vec.unwrap;
let decoded = decode_from_bytes.unwrap;
assert_eq!;
The same DataPoint through the JSON mirror (requires the std feature):
use ;
let json = to_json_string.unwrap;
let parsed = from_json_str.unwrap;
assert_eq!;
A runnable end-to-end round-trip example lives at
examples/roundtrip.rs:
no_std
This crate is #![no_std] and always requires alloc. With the std feature
you also get the JSON mirror. Without it, the crate builds with only the CBOR
codec and no serde_json or hex dependency:
Crate layout
tpt-protocol-tup/
├── Cargo.toml
├── README.md
├── examples/
│ └── roundtrip.rs # CBOR + JSON isomorphic round-trip demo
└── src/
├── lib.rs # DataPoint model, schema_version, re-exports
├── value.rs # TupValue enum + From impls + Display
├── quality.rs # Quality enum + str round-trip
├── source_protocol.rs # SourceProtocol enum + str round-trip (19 protocols)
├── cbor.rs # minicbor Encode/Decode + encode_to_vec/decode_from_bytes
└── json.rs # (std) serde_json mirror: to_json_string(_pretty)/from_json_str
Status / Conformance
- Schema version 1 (
SCHEMA_VERSIONinlib.rs). - A unit test (
schema_tests::worked_example_matches_json_schema) asserts theSPEC-TUP.md§4 worked example validates againstschema/tup-v1.schema.json. - CBOR codec rejects unknown
source_protocol/qualitystrings, unknownTupValuetype tags, schema versions greater than the compiled maximum, and truncated/garbage input (seecbor.rstestsreject_*).
Related crates
tpt-protocol-core— theDataNormalizer/FrameParser/StateValidatortraits every protocol crate implements to produceDataPoints.- The 19 protocol crates (
tpt-protocol-modbus,tpt-protocol-uds, …) which emit TUPDataPoints. - The consumer UIs —
tpt-protocol-cli,tpt-protocol-ui-embedded,tpt-protocol-ui-web— which decode TUP over the daemon's local WebSocket.
License
Licensed under either of MIT or Apache-2.0 at your option.