m_bus_parser/
lib.rs

1//! Brief summary
2//! * is a library for parsing M-Bus frames and user data.
3//! * aims to be a modern, open source decoder for wired m-bus protocol decoder for EN 13757-2 physical and link layer, EN 13757-3 application layer of m-bus
4//! * was implemented using the publicly available documentation available at <https://m-bus.com/>
5//! # Example
6//! ```rust
7//! use m_bus_parser::frames::{ Address, Frame, Function };
8//! use m_bus_parser::user_data::{ DataRecords, UserDataBlock };
9//! use m_bus_parser::mbus_data::MbusData;
10//! use std::io;
11//!
12//!     let example = vec![
13//!         0x68, 0x4D, 0x4D, 0x68, 0x08, 0x01, 0x72, 0x01,
14//!         0x00, 0x00, 0x00, 0x96, 0x15, 0x01, 0x00, 0x18,
15//!         0x00, 0x00, 0x00, 0x0C, 0x78, 0x56, 0x00, 0x00,
16//!         0x00, 0x01, 0xFD, 0x1B, 0x00, 0x02, 0xFC, 0x03,
17//!         0x48, 0x52, 0x25, 0x74, 0x44, 0x0D, 0x22, 0xFC,
18//!         0x03, 0x48, 0x52, 0x25, 0x74, 0xF1, 0x0C, 0x12,
19//!         0xFC, 0x03, 0x48, 0x52, 0x25, 0x74, 0x63, 0x11,
20//!         0x02, 0x65, 0xB4, 0x09, 0x22, 0x65, 0x86, 0x09,
21//!         0x12, 0x65, 0xB7, 0x09, 0x01, 0x72, 0x00, 0x72,
22//!         0x65, 0x00, 0x00, 0xB2, 0x01, 0x65, 0x00, 0x00,
23//!         0x1F, 0xB3, 0x16
24//!     ];
25//!
26//!     // Parse the frame
27//!     let frame = Frame::try_from(example.as_slice()).unwrap();
28//!
29//!     if let Frame::LongFrame { function, address, data } = frame {
30//!         assert_eq!(function, Function::RspUd { acd: false, dfc: false });
31//!         assert_eq!(address, Address::Primary(1));
32//!         if let Ok(UserDataBlock::VariableDataStructure { fixed_data_header, variable_data_block }) = UserDataBlock::try_from(data) {
33//!             let data_records = DataRecords::try_from(variable_data_block).unwrap();
34//!             println!("data_records: {:#?}", data_records);
35//!             let data_records = DataRecords::try_from(variable_data_block).unwrap();
36//!         }
37//!     }
38//!
39//!     // Parse everything at once
40//!     let parsed_data = m_bus_parser::mbus_data::MbusData::try_from(example.as_slice()).unwrap();
41//!     println!("parsed_data: {:#?}", parsed_data);
42//!
43//! ```
44
45#![cfg_attr(not(feature = "std"), no_std)]
46
47use frames::FrameError;
48use user_data::ApplicationLayerError;
49
50pub mod frames;
51pub mod mbus_data;
52pub mod user_data;
53
54#[cfg(feature = "std")]
55pub use mbus_data::serialize_mbus_data;
56
57#[derive(Debug)]
58#[cfg_attr(feature = "serde", derive(serde::Serialize))]
59pub enum MbusError {
60    FrameError(FrameError),
61    ApplicationLayerError(ApplicationLayerError),
62}
63
64impl From<FrameError> for MbusError {
65    fn from(error: FrameError) -> Self {
66        Self::FrameError(error)
67    }
68}
69
70impl From<ApplicationLayerError> for MbusError {
71    fn from(error: ApplicationLayerError) -> Self {
72        Self::ApplicationLayerError(error)
73    }
74}