sml-kit 0.1.1

Read German smart meters in Rust: SML (Smart Message Language) decoder and encoder, sans-I/O, no_std, no allocator, no dependencies
Documentation
//! Write meter telegrams: for tests, simulators and fixtures.
//!
//! [`Telegram`] writes what a basic meter pushes on its optical interface: an
//! `OpenResponse`, one `GetListResponse` with the entries, and a `CloseResponse`, each
//! with a correct message CRC, through the same [`SmlFile`] a client uses. Frame the result with [`encode_frame`](crate::encode_frame).
//!
//! ```
//! use sml_kit::{ListEntry, Obis, Unit, encode_frame, readout, sim::Telegram};
//!
//! let entries = [
//!     ListEntry::bytes(Obis::MANUFACTURER, b"SIM"),
//!     ListEntry::number(Obis::ENERGY_IMPORT, Unit::WH, -1, 123_456_789),
//! ];
//! let telegram = Telegram { server_id: b"\x0a\x01SIM\x00\x00\x00\x00\x01", file_id: b"F1", entries: &entries };
//! let mut payload = [0u8; 256];
//! let n = telegram.write(&mut payload)?;
//! assert_eq!(readout(&payload[..n])?.quantity(Obis::ENERGY_IMPORT).unwrap().to_string(), "12345678.9 Wh");
//! # Ok::<(), sml_kit::Error>(())
//! ```
//!
//! This is a simulator, not a model of any particular meter. Agreement between it and
//! the decoder proves consistency only; the real-meter corpus (`tests/corpus/`) is the
//! evidence that real meters decode.

use crate::message::{Body, CloseResponse, GetListResponse, OpenResponse};
use crate::value::Time;
use crate::{ListEntries, ListEntry, Result, ServerId, SmlFile};

/// The list name basic meters use: `1-0:98.10.255*255`.
const LIST_NAME: [u8; 6] = [1, 0, 98, 10, 255, 255];

/// A simulated meter telegram.
#[derive(Clone, Copy, Debug)]
pub struct Telegram<'a> {
    /// The meter's server ID (10 bytes on German meters).
    pub server_id: &'a [u8],
    /// The file ID shared by the three messages.
    pub file_id: &'a [u8],
    /// The entries of the `GetListResponse`.
    pub entries: &'a [ListEntry<'a>],
}

impl Telegram<'_> {
    /// Write the three messages into `out`. Returns the number of bytes written: the
    /// unframed payload.
    pub fn write(&self, out: &mut [u8]) -> Result<usize> {
        let server_id = ServerId::new(self.server_id);
        let bodies = [
            Body::OpenResponse(OpenResponse {
                codepage: None,
                client_id: None,
                req_file_id: self.file_id,
                server_id,
                ref_time: None,
                sml_version: None,
            }),
            Body::GetListResponse(GetListResponse {
                client_id: None,
                server_id,
                list_name: Some(&LIST_NAME),
                act_sensor_time: Some(Time::SecIndex(0x0001_0203)),
                entries: ListEntries::from_slice(self.entries),
                list_signature: None,
                act_gateway_time: None,
            }),
            Body::CloseResponse(CloseResponse { global_signature: None }),
        ];
        SmlFile { file_id: self.file_id, messages: &bodies }.write(out)
    }
}