mbus-kit 0.4.0

M-Bus and wireless M-Bus (EN 13757-2/-3/-4) for Rust: heat, water and gas meter readout with a sans-I/O, no_std, allocation-free core
Documentation

mbus-kit

M-Bus and wireless M-Bus (EN 13757) for Rust: read heat, water and gas meters with a sans-I/O, no_std, allocation-free core.

CI License unsafe

Documentation Β· API reference

🚧 Pre-release. Checked against a corpus of 41 real and published telegrams from 20 manufacturers (libmbus, wmbusmeters, jMBus, OMS Annex N), wired and wireless, plain and encrypted. The API may still change before 1.0.

Heat, water, gas and heat-cost-allocator meters across Europe speak M-Bus: wired (EN 13757-2, two-wire bus) or by radio (wireless M-Bus, EN 13757-4, and its OMS profile). Both carry the same application layer (EN 13757-3). mbus-kit turns those bytes into exact, fully described readings:

use mbus_kit::wired::Frame;
use mbus_kit::Application;

fn on_response(bytes: &[u8]) -> Result<(), mbus_kit::Error> {
    let Some((Frame::Long { ci, data, .. }, _)) = Frame::parse(bytes)? else { return Ok(()) };
    for record in Application::parse(ci, data)?.records().into_iter().flatten() {
        println!("{}", record?); // "volume flow Β· maximum Β· storage 5 β€” 0.113 mΒ³/h"
    }
    Ok(())
}

And by radio, encrypted (feature oms):

use mbus_kit::oms::Key;
use mbus_kit::wireless::{self, Format};

let key = Key::from_hex("0102030405060708090A0B0C0D0E0F11").unwrap();
let mut buf = [0u8; 512];
if let Some((telegram, _)) = wireless::open(&raw, Format::A, &key, &mut buf)? {
    println!("{} Β· {:?}", telegram.address, telegram.app.authenticity); // Encrypted, or Authenticated { counter }
    for record in telegram.app.records().into_iter().flatten() {
        println!("{}", record?); // "volume β€” 28504.27 mΒ³"
    }
}

What it does

Layer Status
Wired link layer (EN 13757-2): ACK, short, control and long frames, checksum; resynchronising scan for noisy lines βœ… parse and encode, incremental (asks for more bytes)
Wired master (sans-I/O): complete readout with FCB and retries, secondary addressing with wildcards, bus search, set address, baud rate, clock, application reset; replies only from the addressed meter (optionally bound to its printed ID), and every readout says whether it is complete, or what it heard instead βœ… tested against a simulated bus of 250 meters; fuzzed
Real converters (serial::Bus, std, no dependency): the master over any Read + Write β€” a USB level converter or a TCP gateway; every reply read until the line is quiet, so collisions show; echo and noise handled, every wait bounded βœ… simulated converter and a real socket
JSON (json, no_std, no allocation): one object per telegram β€” meter, authenticity, every record with exact value, unit, storage, tariff βœ… every corpus telegram checked by a strict parser; fuzzed
mbus tool (feature cli, no dependency): decode, read, search, listen, text or --json, sets up serial ports itself βœ…
Receiver sticks (stick, no_std): IMST iM871A, IMST iU891A, Amber AMB8465 β€” telegrams with RSSI, receive-mode commands βœ… checked against real captures
Wireless link layer (EN 13757-4): frame formats A and B with CRC-16/EN-13757 per block, or CRCs already removed by the receiver βœ… decode and encode
Extended link layer (CI 8C–8F) and authentication and fragmentation layer (CI 90), with fragment reassembly into your buffer βœ… checked against OMS Annex N N.3–N.5
Compact frames (CI 79/7B/73) and format frames: values without their layout, expanded with a layout cache you own, checked by the full-frame CRC βœ…
Transport layer: CI 72/7A/78/51, commands 5A/5B, errors 6E–70, alarms 71/74/75, 80/8A/8B βœ…
OMS security (feature oms): mode 5 (AES-CBC), mode 7 (KDF, AES-CBC, CMAC verified), mode 10 (AES-CCM, tag verified), ELL AES-CTR; keys zeroized, never printed βœ… checked against OMS Annex N and real meters
Simulator (sim, feature oms): secured telegrams for gateway tests β€” modes 5, 7 and 10, ELL, rising counters, an unprotected tail βœ… writes OMS Annex N N.2.1, N.2.3, N.2.6 byte for byte
Data records: DIF/DIFE, VIF/VIFE, plain-text VIF, LVAR, filler, manufacturer data βœ… lazily, without allocating
Value information: primary, FB, FD, FD FD tables and every combinable VIFE (EN 13757-3:2018), typed βœ… Kind, Modifier; correction factors applied exactly
Values: integers (signed and unsigned), BCD (incl. negative and invalid), IEEE reals (exactly, as decimals), dates and times (types G, J, F, I), time offsets (type M) βœ… exact Quantity, never floats
Compact profiles (VIFE 13/1E/1F): monthly or interval history as readings with their dates or offsets βœ… checked against OMS Annex G and N.13
Mode 13 (TLS), data types K/L Not yet

Design

  • Sans-I/O. No serial port, no radio driver, no clock. Feed bytes from an M-Bus level converter, a wM-Bus receiver stick or a capture file.
  • no_std, no allocator. The default build has no dependencies; oms adds only the RustCrypto AES modes and zeroize. Records borrow from the frame. The library's release build for a Cortex-M contains no panic path, checked in CI.
  • Exact. Readings are mantissa Γ— 10^exponent with their unit, never floats. Correction factors are applied exactly; a VIFE the crate cannot model yields no quantity rather than a wrong one.
  • Honest about radio. Every telegram, and every record in it, says what was verified: nothing, decrypted, or authenticated by CMAC or CCM tag. A CRC or a mode-5 decryption is not authentication: both can be forged or modified, and the tests show how.
  • Never panics on input. Malformed, truncated and random bytes are errors. Fuzzed.

Try it

cargo run --example readout                                   # the reference telegram
cargo run --example master                                    # read a meter, search a bus (simulated)
brew install hupe1980/tap/mbus                                 # the `mbus` tool (macOS, Linux)
cargo install mbus-kit --features cli                         # …or from source; binaries on the releases page
mbus read /dev/ttyUSB0 5                                       # a wired meter (port set up for you)
mbus listen /dev/ttyUSB0 --stick im871a --mode t1 --key <32 hex>   # radio meters through a stick
mbus decode captures.hex --json                                # captures as JSON lines
just decode tests/corpus/wireless/oms-n13-mode7-profile.hex    # 46-step load profile, type M

mbus decode takes one telegram per line, recognises wired and wireless frames and the radio format (A, B, CRCs removed) by itself, learns record layouts from full frames to expand later compact frames, and joins AFL fragments on consecutive lines. With --json, every command writes one JSON object per telegram:

{"meter":{"manufacturer":"ELS","id":"12345678","version":51,"medium":"gas","medium_code":3},"ci":"7A","access":117,"status":0,"authenticity":"authenticated","counter":2739,"body":"records","records":[{"name":"volume","function":"instantaneous","storage":0,"tariff":0,"subunit":0,"modifiers":[],"value":28504.27,"unit":"m3","authenticity":"authenticated","text":"volume β€” 28504.27 mΒ³"}, …]}

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Not affiliated with CEN, the OMS Group or any meter manufacturer.