Skip to main content

smgw_lmn/simulator/
mod.rs

1//! Loopback meter and gateway roles for the commissioned simulation channel, over
2//! DLMS/COSEM or SML.
3extern crate std;
4
5use crate::dlms::protocol::{cosem::CaptureObject, obis::Obis};
6use core::{fmt, str::FromStr};
7
8mod sml;
9mod tcp;
10
11#[cfg(feature = "loopback-client")]
12mod gateway;
13#[cfg(feature = "loopback-client")]
14pub use gateway::{SOURCE, new_profile, read_meter};
15pub use sml::SERVER_ID;
16#[cfg(feature = "loopback-client")]
17pub use sml::gateway::SML_SOURCE;
18#[cfg(feature = "loopback-meter")]
19mod meter;
20#[cfg(feature = "loopback-meter")]
21pub use meter::{History, Meter, VirtualMeter, serve_meter};
22
23/// Commissioned simulator meter identifier.
24pub const ID: &str = "fixture-meter";
25/// Profile Generic logical name for the three-slot fixture.
26pub const PROFILE: Obis = Obis::new(1, 0, 99, 1, 0, 255);
27const ENERGY: Obis = Obis::new(1, 0, 1, 8, 0, 255);
28const STATUS: CaptureObject = CaptureObject::new(1, Obis::new(0, 0, 96, 10, 1, 255), 2, 0);
29const MEASURED: CaptureObject = CaptureObject::new(1, Obis::new(0, 0, 96, 7, 1, 255), 2, 0);
30
31/// The protocol on the simulated meter link. Both sides must be commissioned alike.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
33pub enum Protocol {
34    /// DLMS/COSEM over the IEC 62056-47 wrapper: a selective-access profile read.
35    #[default]
36    Dlms,
37    /// SML transport frames: one `GetProfilePack` request and answer.
38    Sml,
39}
40
41impl Protocol {
42    /// The command-line spelling: `dlms` or `sml`.
43    pub const fn as_str(self) -> &'static str {
44        match self {
45            Self::Dlms => "dlms",
46            Self::Sml => "sml",
47        }
48    }
49
50    /// The protocol's display name.
51    pub const fn name(self) -> &'static str {
52        match self {
53            Self::Dlms => "DLMS/COSEM",
54            Self::Sml => "SML",
55        }
56    }
57}
58
59impl fmt::Display for Protocol {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        f.write_str(self.name())
62    }
63}
64
65impl FromStr for Protocol {
66    type Err = &'static str;
67    fn from_str(s: &str) -> Result<Self, Self::Err> {
68        match s {
69            "dlms" => Ok(Self::Dlms),
70            "sml" => Ok(Self::Sml),
71            _ => Err("expected dlms or sml"),
72        }
73    }
74}
75
76#[cfg(all(test, feature = "loopback-client", feature = "loopback-meter"))]
77mod tests;