Skip to main content

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//!
8//! use m_bus_core::{FrameError, Function};
9//! use wired_mbus_link_layer::{Address, WiredFrame};
10//! use m_bus_parser::user_data::{DataRecords, UserDataBlock};
11//! use m_bus_parser::mbus_data::MbusData;
12//!
13//! fn try_parse() -> Result<(), m_bus_parser::MbusError> {
14//!     let example = vec![
15//!         0x68, 0x4D, 0x4D, 0x68, 0x08, 0x01, 0x72, 0x01,
16//!         0x00, 0x00, 0x00, 0x96, 0x15, 0x01, 0x00, 0x18,
17//!         0x00, 0x00, 0x00, 0x0C, 0x78, 0x56, 0x00, 0x00,
18//!         0x00, 0x01, 0xFD, 0x1B, 0x00, 0x02, 0xFC, 0x03,
19//!         0x48, 0x52, 0x25, 0x74, 0x44, 0x0D, 0x22, 0xFC,
20//!         0x03, 0x48, 0x52, 0x25, 0x74, 0xF1, 0x0C, 0x12,
21//!         0xFC, 0x03, 0x48, 0x52, 0x25, 0x74, 0x63, 0x11,
22//!         0x02, 0x65, 0xB4, 0x09, 0x22, 0x65, 0x86, 0x09,
23//!         0x12, 0x65, 0xB7, 0x09, 0x01, 0x72, 0x00, 0x72,
24//!         0x65, 0x00, 0x00, 0xB2, 0x01, 0x65, 0x00, 0x00,
25//!         0x1F, 0xB3, 0x16
26//!     ];
27//!
28//!     // Parse the frame
29//!     let frame = WiredFrame::try_from(example.as_slice())?;
30//!
31//!     if let WiredFrame::LongFrame { function, address, data } = frame {
32//!         assert_eq!(function, Function::RspUd { acd: false, dfc: false });
33//!         assert_eq!(address, Address::Primary(1));
34//!         if let Ok(UserDataBlock::VariableDataStructureWithLongTplHeader { long_tpl_header, variable_data_block, .. }) = UserDataBlock::try_from(data) {
35//!             let data_records = DataRecords::from((variable_data_block, &long_tpl_header));
36//!             println!("data_records: {:#?}", data_records.collect::<Result<Vec<_>, _>>()?);
37//!         }
38//!     }
39//!
40//!     // Parse everything at once
41//!     let parsed_data = MbusData::<WiredFrame>::try_from(example.as_slice())?;
42//!     println!("parsed_data: {:#?}", parsed_data);
43//!     Ok(())
44//! }
45//! ```
46
47#![cfg_attr(not(feature = "std"), no_std)]
48
49#[cfg(feature = "std")]
50#[allow(clippy::indexing_slicing)]
51pub mod annotate;
52#[cfg(feature = "std")]
53pub mod manufacturers;
54pub mod mbus_data;
55pub use m_bus_application_layer as user_data;
56
57pub use m_bus_core::decryption;
58use m_bus_core::ApplicationLayerError;
59// Re-export link layer types for convenience
60pub use m_bus_core::{FrameError, Function};
61pub use wired_mbus_link_layer::{Address, WiredFrame};
62pub use wireless_mbus_link_layer::{ManufacturerId, WirelessFrame};
63
64#[cfg(feature = "std")]
65pub use mbus_data::serialize_mbus_data;
66
67#[derive(Debug, Clone, Copy, PartialEq)]
68#[cfg_attr(feature = "serde", derive(serde::Serialize))]
69#[non_exhaustive]
70pub enum MbusError {
71    FrameError(FrameError),
72    WirelessFrameError(wireless_mbus_link_layer::FrameError),
73    ApplicationLayerError(ApplicationLayerError),
74    DataRecordError(user_data::variable_user_data::DataRecordError),
75}
76
77#[cfg(feature = "std")]
78impl std::fmt::Display for MbusError {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            MbusError::FrameError(e) => write!(f, "{e}"),
82            MbusError::WirelessFrameError(e) => write!(f, "{:?}", e),
83            MbusError::ApplicationLayerError(e) => write!(f, "{e}"),
84            MbusError::DataRecordError(e) => write!(f, "{e}"),
85        }
86    }
87}
88
89#[cfg(feature = "std")]
90impl std::error::Error for MbusError {}
91
92impl From<FrameError> for MbusError {
93    fn from(error: FrameError) -> Self {
94        Self::FrameError(error)
95    }
96}
97
98impl From<ApplicationLayerError> for MbusError {
99    fn from(error: ApplicationLayerError) -> Self {
100        Self::ApplicationLayerError(error)
101    }
102}
103
104impl From<user_data::variable_user_data::DataRecordError> for MbusError {
105    fn from(error: user_data::variable_user_data::DataRecordError) -> Self {
106        Self::DataRecordError(error)
107    }
108}
109
110impl From<wireless_mbus_link_layer::FrameError> for MbusError {
111    fn from(error: wireless_mbus_link_layer::FrameError) -> Self {
112        Self::WirelessFrameError(error)
113    }
114}