Skip to main content

Module sim

Module sim 

Source
Available on crate feature oms only.
Expand description

Write OMS-secured wireless telegrams: for gateway simulators and tests (feature oms).

Telegram is what a meter transmits: link layer, an optional ELL, the AFL for mode 7, a short transport header and the records, protected by mode 5, 7 or 10, with an optional plaintext tail after the protected part. A simulated gas meter with a rising counter:

use mbus_kit::app::Authenticity;
use mbus_kit::sim::{Security, Telegram};
use mbus_kit::wireless::{self, Format};
use mbus_kit::{Address, Manufacturer, Medium, oms::Key};

let meter = Address {
    manufacturer: Manufacturer::from_letters(*b"ELS").unwrap(),
    id: [0x78, 0x56, 0x34, 0x12], // 12345678, least significant byte first
    version: 0x33,
    medium: Medium::GAS,
};
let key = Key::new([0x2B; 16]);
for counter in 1..=3u32 {
    // One record: DIF 04 (32-bit integer), VIF 13 (volume in litres).
    let [a, b, c, d] = (1_250 * counter).to_le_bytes();
    let records = [0x04, 0x13, a, b, c, d];
    let telegram = Telegram {
        access_number: counter as u8,
        ..Telegram::new(meter, Security::Mode7 { counter, mac: true }, &records)
    };
    let mut radio = [0u8; 256];
    let n = telegram.write(&key, Format::A, &mut radio)?;

    let mut buf = [0u8; 512];
    let (t, _) = wireless::open(&radio[..n], Format::A, &key, &mut buf)?.unwrap();
    assert_eq!(t.app.authenticity, Authenticity::Authenticated { counter });
    let volume = t.app.records().unwrap().next().unwrap()?;
    assert_eq!(volume.quantity().unwrap().mantissa, i128::from(1_250 * counter));
}

Records are their wire bytes (DIF, VIF, data), as a meter’s firmware holds them. A Telegram::tail travels after the protected part: in mode 5 and in mode 7 without MAC nothing protects it, and the decoder says so per record (Record::authenticity); in mode 7 with MAC and in mode 10 the MAC or tag covers it.

A round trip through this writer and the crate’s decoder proves that they agree, not that either is right. The independent evidence is that the writer reproduces the published OMS Annex N frames N.2.1, N.2.3 and N.2.6 byte for byte (tests/sim.rs). Short transport headers only (CI 7A), mode 7 with KDF-A and an 8-byte CMAC, mode 10 with an 8-byte tag; no fragments.

Structs§

Telegram
A telegram as a meter transmits it. Build it with Telegram::new and change fields with struct update syntax.

Enums§

Security
How the records are protected.