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")]
50pub mod manufacturers;
51pub mod mbus_data;
52pub use m_bus_application_layer as user_data;
53
54pub use m_bus_core::decryption;
55use m_bus_core::ApplicationLayerError;
56// Re-export link layer types for convenience
57pub use m_bus_core::{FrameError, Function};
58pub use wired_mbus_link_layer::{Address, WiredFrame};
59pub use wireless_mbus_link_layer::{ManufacturerId, WirelessFrame};
60
61#[cfg(feature = "std")]
62pub use mbus_data::serialize_mbus_data;
63
64#[derive(Debug, Clone, Copy, PartialEq)]
65#[cfg_attr(feature = "serde", derive(serde::Serialize))]
66#[non_exhaustive]
67pub enum MbusError {
68    FrameError(FrameError),
69    WirelessFrameError(wireless_mbus_link_layer::FrameError),
70    ApplicationLayerError(ApplicationLayerError),
71    DataRecordError(user_data::variable_user_data::DataRecordError),
72}
73
74#[cfg(feature = "std")]
75impl std::fmt::Display for MbusError {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        match self {
78            MbusError::FrameError(e) => write!(f, "{e}"),
79            MbusError::WirelessFrameError(e) => write!(f, "{:?}", e),
80            MbusError::ApplicationLayerError(e) => write!(f, "{e}"),
81            MbusError::DataRecordError(e) => write!(f, "{e}"),
82        }
83    }
84}
85
86#[cfg(feature = "std")]
87impl std::error::Error for MbusError {}
88
89impl From<FrameError> for MbusError {
90    fn from(error: FrameError) -> Self {
91        Self::FrameError(error)
92    }
93}
94
95impl From<ApplicationLayerError> for MbusError {
96    fn from(error: ApplicationLayerError) -> Self {
97        Self::ApplicationLayerError(error)
98    }
99}
100
101impl From<user_data::variable_user_data::DataRecordError> for MbusError {
102    fn from(error: user_data::variable_user_data::DataRecordError) -> Self {
103        Self::DataRecordError(error)
104    }
105}
106
107impl From<wireless_mbus_link_layer::FrameError> for MbusError {
108    fn from(error: wireless_mbus_link_layer::FrameError) -> Self {
109        Self::WirelessFrameError(error)
110    }
111}