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
//! Attention numbers (`SML_Attention.Res`, TR-03109-1 Anlage IV Tab. 2 and 3).
//!
//! A device answers a request it will not or cannot execute with an attention number,
//! an OBIS-like six-byte code. `81 81 c7 c7 fd xx` are hints, `81 81 c7 c7 fe xx` are
//! errors, and `e0 00` to `fc ff` in the last two bytes are application specific.
//!
//! ```
//! use sml_kit::attention;
//!
//! assert_eq!(attention::describe(&attention::AUTHENTICATION), Some("insufficient authentication"));
//! ```

const fn code(e: u8, f: u8) -> [u8; 6] {
    [0x81, 0x81, 0xC7, 0xC7, e, f]
}

/// Positive acknowledgement.
pub const OK: [u8; 6] = code(0xFD, 0x00);
/// The order will be executed later; its result follows as a response without request.
pub const LATER: [u8; 6] = code(0xFD, 0x01);
/// Error not covered by another number.
pub const ERROR: [u8; 6] = code(0xFE, 0x00);
/// Unknown SML identifier.
pub const UNKNOWN_IDENTIFIER: [u8; 6] = code(0xFE, 0x01);
/// Insufficient authentication (user name or password).
pub const AUTHENTICATION: [u8; 6] = code(0xFE, 0x02);
/// Destination address (`serverId`) not available.
pub const SERVER_ID_UNAVAILABLE: [u8; 6] = code(0xFE, 0x03);
/// Request (`reqFileId`) not available.
pub const REQ_FILE_ID_UNAVAILABLE: [u8; 6] = code(0xFE, 0x04);
/// One or more destination attributes cannot be written.
pub const NOT_WRITABLE: [u8; 6] = code(0xFE, 0x05);
/// One or more destination attributes cannot be read.
pub const NOT_READABLE: [u8; 6] = code(0xFE, 0x06);
/// Communication with the measuring point is disturbed.
pub const MEASURING_POINT_DISTURBED: [u8; 6] = code(0xFE, 0x07);
/// Raw data cannot be interpreted.
pub const RAW_DATA_UNINTERPRETABLE: [u8; 6] = code(0xFE, 0x08);
/// Value out of permitted range.
pub const OUT_OF_RANGE: [u8; 6] = code(0xFE, 0x09);
/// Request not executed (e.g. tree path invalid).
pub const NOT_EXECUTED: [u8; 6] = code(0xFE, 0x0A);
/// Checksum faulty.
pub const CHECKSUM: [u8; 6] = code(0xFE, 0x0B);
/// Broadcast not supported.
pub const BROADCAST_UNSUPPORTED: [u8; 6] = code(0xFE, 0x0C);
/// Unexpected SML message.
pub const UNEXPECTED_MESSAGE: [u8; 6] = code(0xFE, 0x0D);
/// Unknown object in the load profile.
pub const UNKNOWN_PROFILE_OBJECT: [u8; 6] = code(0xFE, 0x0E);
/// Data type not supported in a set request.
pub const UNSUPPORTED_TYPE: [u8; 6] = code(0xFE, 0x0F);
/// Optional element not supported.
pub const OPTIONAL_UNSUPPORTED: [u8; 6] = code(0xFE, 0x10);
/// The requested load profile has no entry.
pub const PROFILE_EMPTY: [u8; 6] = code(0xFE, 0x11);
/// End limit before start limit in a load profile request.
pub const END_BEFORE_BEGIN: [u8; 6] = code(0xFE, 0x12);
/// The file ended without a close request.
pub const NO_CLOSE: [u8; 6] = code(0xFE, 0x14);
/// The load profile is temporarily unavailable.
pub const PROFILE_UNAVAILABLE: [u8; 6] = code(0xFE, 0x15);

const DESCRIPTIONS: &[([u8; 6], &str)] = &[
    (OK, "ok, positive acknowledgement"),
    (LATER, "will be executed later; result follows as response without request"),
    (ERROR, "error"),
    (UNKNOWN_IDENTIFIER, "unknown SML identifier"),
    (AUTHENTICATION, "insufficient authentication"),
    (SERVER_ID_UNAVAILABLE, "destination address (serverId) not available"),
    (REQ_FILE_ID_UNAVAILABLE, "request (reqFileId) not available"),
    (NOT_WRITABLE, "destination attribute(s) cannot be written"),
    (NOT_READABLE, "destination attribute(s) cannot be read"),
    (MEASURING_POINT_DISTURBED, "communication with measuring point disturbed"),
    (RAW_DATA_UNINTERPRETABLE, "raw data cannot be interpreted"),
    (OUT_OF_RANGE, "value out of permitted range"),
    (NOT_EXECUTED, "request not executed"),
    (CHECKSUM, "checksum faulty"),
    (BROADCAST_UNSUPPORTED, "broadcast not supported"),
    (UNEXPECTED_MESSAGE, "unexpected SML message"),
    (UNKNOWN_PROFILE_OBJECT, "unknown object in the load profile"),
    (UNSUPPORTED_TYPE, "data type not supported"),
    (OPTIONAL_UNSUPPORTED, "optional element not supported"),
    (PROFILE_EMPTY, "requested load profile has no entry"),
    (END_BEFORE_BEGIN, "end limit before start limit"),
    (NO_CLOSE, "file ended without close request"),
    (PROFILE_UNAVAILABLE, "load profile temporarily unavailable"),
];

/// What a standard attention number means. Application-specific and unknown numbers
/// have no description.
pub fn describe(number: &[u8]) -> Option<&'static str> {
    DESCRIPTIONS.iter().find(|(n, _)| n.as_slice() == number).map(|&(_, d)| d)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn standard_numbers_are_described() {
        assert_eq!(describe(&[0x81, 0x81, 0xC7, 0xC7, 0xFE, 0x0B]), Some("checksum faulty"));
        assert_eq!(describe(&OK), Some("ok, positive acknowledgement"));
        assert_eq!(describe(&[0x81, 0x81, 0xC7, 0xC7, 0xE0, 0x00]), None);
        assert_eq!(describe(&[1, 2]), None);
    }
}