Skip to main content

Crate sml_kit

Crate sml_kit 

Source
Expand description

§sml-kit

Read German smart meters in Rust. sml-kit decodes SML (Smart Message Language) from a meter’s optical interface or the meter bus behind a smart-meter gateway into exact readings: sans-I/O, no_std, no allocator, no dependencies.

crates.io docs.rs CI MSRV 1.85 License

Documentation · What is SML? · API reference

use sml_kit::{Decoder, Obis, readout};

// Call with every chunk of bytes from the reading head. Keep one decoder per meter
// (`Decoder::new([0; 1024])`): a frame often arrives across several reads.
fn on_bytes(decoder: &mut Decoder<[u8; 1024]>, bytes: &[u8]) {
    decoder.push_slice(bytes, |frame| {
        let Ok(list) = frame.and_then(readout) else { return };
        if let Some(a_plus) = list.quantity(Obis::ENERGY_IMPORT) {
            println!("{}: A+ = {a_plus}", list.server_id); // "1 EMH00 04321349: A+ = 15338555.7 Wh"
        }
    });
}

§Install

[dependencies]
sml-kit = "0.1"                                             # desktop, Raspberry Pi, server
# sml-kit = { version = "0.1", default-features = false }   # microcontroller: no std, no alloc

To read a real meter you need an infrared reading head on its optical interface (9600 baud, 8N1) and, for power and full-precision energy, the PIN from your meter operator. What is SML? explains both.

§Tested against real meters

Every CRC-valid frame of the public libsml-testing collection reads: 216 frames from EMH, ISKRA, DZG, eBZ, Holley, Itron, EasyMeter and Dr. Neuhaus meters, strictly for every meter but one. Three meters deviate from the specification; sml-kit reads them when you name the deviation:

MeterDeviationOption
Holley DTZ541 (some)CRC-16/KERMIT instead of X-25ParseOptions::lenient()
Holley DTZ541-ZDBA, DDZ285SML_Time without its choice tagParseOptions::lenient()
DZG DVS74 (affected serials)356.24 W sent as −299.12 W.with_dzg_power(true)

The capture tool tells you which one your meter needs:

cargo run --example decode -- capture.bin   # raw or hex: readings, and what your meter needs

§Features

Transport v1sans-I/O Decoder over an owned or borrowed buffer; encode_frame
Readings in one callreadout, GetListResponse::quantity; exact Quantity with unit
ValuesOBIS (parse, names), the DLMS unit table, SML_Time, ServerId as printed on the meter
Energy directiondirection, signed_power from the status word, for meters that send power without a sign
Every message bodyOpen, Close, GetList, Get/SetProcParameter, GetProfileList/Pack, Attention: typed, read and written
Request filesSmlFile: open, requests, close with transaction IDs and CRCs in one call
Blocking I/O (std)io::FrameReader over any std::io::Read
Asyncthe same Decoder in Embassy, Tokio or any runtime, no dependency
Simulatorsim::Telegram for tests

Not supported: verifying SML signatures (they are exposed as bytes), transport version 2 (no device sends it), and IEC 62056-21 text (an older protocol).

§Design

  • Sans-I/O. No ports or clocks inside: feed bytes from a reading head, a meter bus or a file.
  • no_std, no allocator, no dependencies. CI checks the Cortex-M release build for panic paths.
  • Exact. Readings are mantissa × 10^scaler with their unit, never floats: a meter reading is evidence for an invoice.
  • Strict by default. Both CRCs are checked; every deviation a real meter needs is a named option.
  • Never panics on input. Malformed, truncated and random bytes are errors; the parsers are fuzzed.
  • No secrets in logs. Request passwords print as <redacted>.

§Examples

cargo run --example readout                                  # a simulated telegram
cargo run --example decode -- tests/corpus/emh-ehz-apollon77.hex
cargo run --example serial -- /dev/ttyUSB0                   # a USB reading head

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option. The captures in tests/corpus/ keep their own MIT notices.

Not affiliated with the BSI or any meter manufacturer. SML is specified in BSI TR-03109-1, Annex IV.

Re-exports§

pub use crc::CrcCheck;
pub use message::Attention;
pub use message::Body;
pub use message::CloseRequest;
pub use message::CloseResponse;
pub use message::GetListRequest;
pub use message::GetListResponse;
pub use message::GetProcParameterRequest;
pub use message::GetProcParameterResponse;
pub use message::GetProfileListResponse;
pub use message::GetProfileRequest;
pub use message::ListEntries;
pub use message::ListEntry;
pub use message::Message;
pub use message::Messages;
pub use message::OpenRequest;
pub use message::OpenResponse;
pub use message::ParseOptions;
pub use message::SetProcParameterRequest;
pub use message::readout_with;
pub use tl::Kind;
pub use tl::Reader;
pub use tl::Writer;
pub use transport::Decoder;
pub use transport::encode_frame;

Modules§

attention
Attention numbers (SML_Attention.Res, TR-03109-1 Anlage IV Tab. 2 and 3).
crc
CRC-16/X-25, the checksum SML uses for both the transport frame and each message, and CRC-16/KERMIT, the checksum some meters use instead.
iostd
A blocking frame reader over std::io::Read: a serial port, a USB infrared head, a TCP socket or a capture file.
message
SML messages: SML_Message, the bodies a meter and a client exchange, and the options that decide how strictly they are read.
sim
Write meter telegrams: for tests, simulators and fixtures.
tl
The SML type-length encoding.
transport
SML transport protocol version 1: framing, escaping, padding and the frame CRC.

Structs§

GetProfilePackResponse
SML_GetProfilePack.Res: a profile as one header and many rows.
Obis
An OBIS code (IEC 62056-61): the six-byte name of a measured quantity.
PeriodEntry
SML_PeriodEntry: one value of a profile period, or a historic value.
ProfileColumn
SML_ProfObjHeaderEntry: what one column of a packed profile holds.
ProfilePeriod
SML_ProfObjPeriodEntry: one row of a packed profile.
ProfileValue
SML_ValueEntry: one value of a row; its unit and scaler are in the column header.
Quantity
An exact measured quantity: mantissa × 10^exponent in unit.
Register
One register of a TupelEntry: value × 10^scaler in unit, as on the wire (Unsigned8, Integer8, Integer64).
Seq
An SML SEQUENCE OF T, parsed lazily.
SeqIter
Iterator over a Seq.
ServerId
A device’s server ID: the bytes a meter names itself with in OpenResponse, GetListResponse and Attention.
SmlFile
An SML file: an open, the requests (or responses) and a close, sharing one file ID.
Tree
SML_Tree: one named parameter, with a value, children, or both.
TupelEntry
SML_TupelEntry: a signed set of registers (A+, R1, R4 and A−, R2, R3). 23 fields on the wire.
Unit
A unit code from the DLMS/COSEM unit table (IEC 62056-62), as SML carries it.

Enums§

Direction
The direction of energy flow, from the meter’s point of view.
Error
Everything that can go wrong between the meter’s bytes and a typed reading.
ProcParValue
SML_ProcParValue: the value of a parameter.
SmlValue
The value of a list entry: SML_Value.
StatusWord
Where a meter’s status word carries the energy direction.
Time
A point in time as SML encodes it: SML_Time.

Traits§

Element
An element of a Seq: ListEntry, PeriodEntry, Tree or a name (&[u8]). Sealed: the encoding of each is fixed by the specification.

Functions§

messages
Parse every SML message in one unescaped transport payload (one SML file), with the default, strict options.
readout
The readings in one transport payload, strictly: every message must parse, and the first GetListResponse is returned. Error::NoReadings if there is none.

Type Aliases§

NameList
A sequence of names: an SML_TreePath, or a profile’s object list.
PeriodEntries
Period entries of a profile.
ProfileColumns
The column headers of a packed profile.
ProfilePeriods
The rows of a packed profile.
ProfileValues
The values of one row, in column order.
Result
Result type of this crate.
Trees
The children of a Tree.