use std::io::{Read, Write};
use std::net::{TcpListener, TcpStream};
use dlms_cosem_kit::acse::AuthMechanism;
use dlms_cosem_kit::axdr::{Data, DateTime, Unit};
use dlms_cosem_kit::codec::{Encode, Writer};
use dlms_cosem_kit::cosem::{AttributeAccess, MethodAccess};
use dlms_cosem_kit::obis::Obis;
use dlms_cosem_kit::security::{
KeyRing, OsRandom, RustCryptoProvider, SecurityPolicy, SecuritySuite, SystemTitle,
};
use dlms_cosem_kit::server::{AuditEvent, ObjectStore, Server, ServerConfig, StoreResult};
use dlms_cosem_kit::transport::wrapper::{DEFAULT_PORT, StreamReassembler, Wpdu};
use dlms_cosem_kit::xdlms::{ActionResult, DataAccessResult, SelectiveAccess};
const GUEK: [u8; 16] =
[0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F];
const GAK: [u8; 16] =
[0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF];
const METER_TITLE: SystemTitle = SystemTitle::new(*b"MMM\x00\x00\xbcaN");
const ENERGY: Obis = Obis::new(1, 0, 1, 8, 0, 255);
const ENERGY_EXPORT: Obis = Obis::new(1, 0, 2, 8, 0, 255);
const POWER: Obis = Obis::new(1, 0, 1, 7, 0, 255);
const CLOCK: Obis = Obis::new(0, 0, 1, 0, 0, 255);
const BREAKER: Obis = Obis::new(0, 0, 96, 3, 10, 255);
const PROFILE: Obis = Obis::new(1, 0, 99, 1, 0, 255);
const PROFILE_ROWS: u32 = 240;
struct Meter {
energy_wh: u32,
export_wh: u32,
power_w: u32,
time: DateTime,
breaker_closed: bool,
}
impl ObjectStore for Meter {
fn get_attribute(
&self,
class_id: u16,
logical_name: Obis,
attribute_id: i8,
selective_access: Option<SelectiveAccess<'_>>,
w: &mut dyn Writer,
) -> StoreResult<()> {
let bad = |_| DataAccessResult::OtherReason;
match (class_id, logical_name, attribute_id) {
(_, name, 1) => Data::OctetString(name.as_bytes()).encode(w).map_err(bad),
(3, ENERGY, 2) => Data::DoubleLongUnsigned(self.energy_wh).encode(w).map_err(bad),
(3, ENERGY_EXPORT, 2) => Data::DoubleLongUnsigned(self.export_wh).encode(w).map_err(bad),
(3, POWER, 2) => Data::DoubleLongUnsigned(self.power_w).encode(w).map_err(bad),
(3, ENERGY | ENERGY_EXPORT, 3) => {
w.write_bytes(&[0x02, 0x02, 0x0F, 0x00, 0x16, Unit::WATT_HOUR.0]).map_err(bad)
}
(3, POWER, 3) => w.write_bytes(&[0x02, 0x02, 0x0F, 0x00, 0x16, Unit::WATT.0]).map_err(bad),
(8, CLOCK, 2) => Data::DateTime(self.time).encode(w).map_err(bad),
(70, BREAKER, 2) => Data::Boolean(self.breaker_closed).encode(w).map_err(bad),
(7, PROFILE, 2) => self.profile(selective_access, w),
_ => Err(DataAccessResult::ObjectUndefined),
}
}
fn set_attribute(
&mut self,
class_id: u16,
logical_name: Obis,
attribute_id: i8,
_selective_access: Option<SelectiveAccess<'_>>,
value: Data<'_>,
) -> StoreResult<()> {
match (class_id, logical_name, attribute_id, value) {
(8, CLOCK, 2, Data::DateTime(dt)) => {
self.time = dt;
Ok(())
}
(8, CLOCK, 2, _) => Err(DataAccessResult::TypeUnmatched),
_ => Err(DataAccessResult::ReadWriteDenied),
}
}
fn invoke_method(
&mut self,
class_id: u16,
logical_name: Obis,
method_id: i8,
_parameters: Option<Data<'_>>,
_w: &mut dyn Writer,
) -> Result<bool, ActionResult> {
match (class_id, logical_name, method_id) {
(70, BREAKER, 1) => {
self.breaker_closed = false;
Ok(false)
}
(70, BREAKER, 2) => {
self.breaker_closed = true;
Ok(false)
}
_ => Err(ActionResult::ObjectUndefined),
}
}
fn attribute_access(&self, _: u16, logical_name: Obis, attribute_id: i8) -> AttributeAccess {
if logical_name == CLOCK && attribute_id == 2 {
return AttributeAccess::READ | AttributeAccess::WRITE;
}
AttributeAccess::READ
}
fn method_access(&self, _: u16, logical_name: Obis, _: i8) -> MethodAccess {
if logical_name == BREAKER { MethodAccess::ACCESS } else { MethodAccess::empty() }
}
fn audit(&mut self, event: AuditEvent) {
println!(" {event:?}");
}
}
impl Meter {
fn profile(&self, access: Option<SelectiveAccess<'_>>, w: &mut dyn Writer) -> StoreResult<()> {
let bad = |_| DataAccessResult::OtherReason;
let (from, to) = match access {
None => (0, PROFILE_ROWS),
Some(sa) if sa.selector == 2 => {
let s = sa.parameters.as_structure().ok_or(DataAccessResult::TypeUnmatched)?;
let first = s.get(0).map_err(|_| DataAccessResult::TypeUnmatched)?;
let last = s.get(1).map_err(|_| DataAccessResult::TypeUnmatched)?;
let first = first.as_u64().ok_or(DataAccessResult::TypeUnmatched)? as u32;
let last = last.as_u64().ok_or(DataAccessResult::TypeUnmatched)? as u32;
if first == 0 {
return Err(DataAccessResult::TypeUnmatched);
}
(first - 1, if last == 0 { PROFILE_ROWS } else { last.min(PROFILE_ROWS) })
}
Some(_) => return Err(DataAccessResult::ScopeOfAccessViolated),
};
if from > to {
return Err(DataAccessResult::TypeUnmatched);
}
w.write_u8(0x01).map_err(bad)?; w.write_length((to - from) as usize).map_err(bad)?;
for i in from..to {
w.write_bytes(&[0x02, 0x02]).map_err(bad)?; Data::DoubleLongUnsigned(1_000_000 + i * 17).encode(w).map_err(bad)?;
Data::LongUnsigned((i * 7 % 1000) as u16).encode(w).map_err(bad)?;
}
Ok(())
}
}
type Simulator = Server<Meter, RustCryptoProvider<OsRandom>, 8192>;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let ciphered = std::env::args().any(|a| a == "--ciphered");
let port = std::env::args()
.skip(1)
.find(|arg| arg != "--ciphered")
.map(|port| port.parse::<u16>())
.transpose()?
.unwrap_or(DEFAULT_PORT);
let listener = TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, port))?;
let addr = listener.local_addr()?;
println!("meter listening on {addr}, {} association", if ciphered { "ciphered HLS" } else { "plain" });
println!(" objects: 1-0:1.8.0, 1-0:2.8.0, 1-0:1.7.0, 0-0:1.0.0, 0-0:96.3.10, 1-0:99.1.0");
println!(" try: cargo run --example read -- {addr}{}\n", if ciphered { " --ciphered" } else { "" });
for stream in listener.incoming() {
let mut stream = stream?;
println!("── client {}", stream.peer_addr()?);
if let Err(e) = serve(&mut stream, ciphered) {
println!(" link closed: {e}");
}
println!();
}
Ok(())
}
fn serve(stream: &mut TcpStream, ciphered: bool) -> Result<(), Box<dyn std::error::Error>> {
let policy = if ciphered {
SecurityPolicy::authenticated_encrypted(SecuritySuite::Suite0)
} else {
SecurityPolicy::NONE
};
let mut server: Simulator = Server::new(
ServerConfig {
system_title: Some(METER_TITLE),
mechanism: if ciphered { AuthMechanism::HighGmac } else { AuthMechanism::None },
security: policy,
max_pdu_size: 512,
invocation_counter: 0,
..Default::default()
},
Meter {
energy_wh: 12_345_678,
export_wh: 98_765,
power_w: 1_193,
time: DateTime::from_civil(2026, 9, 6, 12, 0, 0, 120).expect("a real date"),
breaker_closed: true,
},
RustCryptoProvider::os(if ciphered { KeyRing::new(GUEK, GAK) } else { KeyRing::default() }),
);
let reassembler = StreamReassembler::new(2048);
let mut buffered = Vec::new();
let mut chunk = [0u8; 512];
let mut out = [0u8; 2048];
loop {
let n = stream.read(&mut chunk)?;
if n == 0 {
server.reset();
return Ok(());
}
buffered.extend_from_slice(&chunk[..n]);
while let Some((wpdu, used)) = reassembler.next_pdu(&buffered)? {
let (from, to) = (wpdu.header.destination, wpdu.header.source);
let mut reply = server.handle(wpdu.apdu, &mut out)?;
buffered.drain(..used);
loop {
if reply > 0 {
let mut framed = Vec::new();
Wpdu::new(from, to, &out[..reply])?.encode(&mut framed)?;
stream.write_all(&framed)?;
}
match server.poll_block(&mut out)? {
Some(n) => reply = n,
None => break,
}
}
}
}
}