Embedded builds
sml-kit on a microcontroller: no std, no allocator, a caller-owned buffer and no panic path.
No std, no allocator, no dependencies๐
cargo add sml-kit --no-default-featuresThe crate has no dependencies and never allocates, with or without std. The std feature adds std::error::Error for Error and the blocking io::FrameReader.
Memory๐
The decoder assembles each frame in a buffer you own. Size it to the largest frame you accept: a basic meter's telegram is typically a few hundred bytes. A larger frame is reported as Error::BufferFull and skipped; the decoder carries on with the next one. After a line timeout, decoder.reset() drops a half-received frame.
struct Driver {
decoder: Decoder<[u8; 512]>, // owns its buffer: no lifetime on your driver
}
let driver = Driver { decoder: Decoder::new([0; 512]) };
let mut buf = [0u8; 512]; // or borrow one
let decoder = Decoder::new(&mut buf[..]);Messages and list entries are parsed lazily over the frame: each ListEntry borrows from the buffer, so nothing is copied.
Async๐
The decoder is sans-I/O, so it works in any async runtime as it is: no feature flag, no dependency. Await your runtime's read, and push the bytes:
use sml_kit::{Decoder, Obis, readout};
let mut decoder = Decoder::new([0u8; 1024]);
let mut chunk = [0u8; 64];
loop {
let n = uart.read(&mut chunk).await?; // Embassy: embedded_io_async::Read; Tokio: AsyncReadExt
for &byte in &chunk[..n] {
if let Ok(Some(frame)) = decoder.push(byte) {
if let Some(a_plus) = readout(frame).ok().and_then(|l| l.quantity(Obis::ENERGY_IMPORT)) {
sender.send(a_plus).await; // awaiting here is fine
}
}
}
}push returns the frame only until the next byte, so the loop can await while handling it; nothing is copied.
No panic path๐
The library denies panic!, unwrap, expect and indexing by lint. CI builds it for thumbv7em-none-eabihf in release mode and fails if any panic symbol is linked in. A decoder that can abort firmware on a malformed telegram would be a denial-of-service bug, since anyone can shine an infrared LED at a meter's reading head.
Targets checked in CI๐
thumbv7em-none-eabihf(Cortex-M4/M7)riscv32imac-unknown-none-elf(e.g. ESP32-C3)wasm32-unknown-unknown
CI also fails on any f32 or f64 in the library: readings are exact integers with a decimal exponent, so a core without an FPU pays nothing for them.