pub struct HdlcLayer<T: PhysicalTransport> {
transport: T,
peer: HdlcAddress,
own: HdlcAddress,
is_client: bool,
send_seq: u8,
recv_seq: u8,
connected: bool,
inter_octet_timeout: Duration,
inactivity_timeout: Option<Duration>,
xid_configured: XidParams,
xid: XidParams,
}Expand description
The HDLC data-link sub-layer over a physical transport.
Sends APDUs as information frames (prefixed with the DLMS LLC header) and
receives them, maintaining the send/receive sequence numbers. Connection
management frames (SNRM/UA/DISC) are available through HdlcFrame directly.
Fields§
§transport: T§peer: HdlcAddressAddress of the peer this layer sends frames to.
own: HdlcAddressThis station’s own address (used as the source address).
is_client: bool§send_seq: u8§recv_seq: u8§connected: boolWhether the data link is in NRM (connected) as opposed to NDM.
inter_octet_timeout: DurationMaximum gap between two octets of the same frame (IEC 62056-46 “межсимвольный” timeout) before the read is aborted.
inactivity_timeout: Option<Duration>Maximum time to wait for a new frame to start (IEC 62056-46
“межкадровый” / inactivity timeout); None waits indefinitely.
xid_configured: XidParamsThis station’s configured XID ceiling, restored at the start of every SNRM/UA negotiation.
xid: XidParamsThe XID parameters actually negotiated with the peer (tightened from
xid_configured by the last SNRM/UA exchange).
Implementations§
Source§impl<T: PhysicalTransport> HdlcLayer<T>
impl<T: PhysicalTransport> HdlcLayer<T>
Sourcepub fn new_client(
transport: T,
client: HdlcAddress,
server: HdlcAddress,
) -> Self
pub fn new_client( transport: T, client: HdlcAddress, server: HdlcAddress, ) -> Self
Creates a client-side HDLC layer that talks to the server at server
address using the given client address.
Sourcepub fn new_server(
transport: T,
server: HdlcAddress,
client: HdlcAddress,
) -> Self
pub fn new_server( transport: T, server: HdlcAddress, client: HdlcAddress, ) -> Self
Creates a server-side HDLC layer.
Sourcepub fn set_inter_octet_timeout_ms(&mut self, ms: u16)
pub fn set_inter_octet_timeout_ms(&mut self, ms: u16)
Sets the inter-octet timeout (IEC 62056-46 “межсимвольный”): the
maximum gap allowed between two octets of the same frame before the
read is aborted with a std::io::ErrorKind::TimedOut error.
Clamped to 20..=6000 ms per the standard; defaults to 25 ms.
Has an effect only if the underlying PhysicalTransport honours
PhysicalTransport::set_read_timeout.
Sourcepub fn set_inactivity_timeout_s(&mut self, seconds: u16)
pub fn set_inactivity_timeout_s(&mut self, seconds: u16)
Sets the inactivity timeout (IEC 62056-46 “межкадровый”): the maximum
time to wait for a new frame to start before
Self::receive_apdu-driven reads abort with a
std::io::ErrorKind::TimedOut error and the link is considered
dropped (see Self::is_connected). Clamped to 0..=120 s; 0
disables it (wait indefinitely), which is also the default.
Has an effect only if the underlying PhysicalTransport honours
PhysicalTransport::set_read_timeout.
Sourcepub fn set_xid_ceiling(&mut self, xid: XidParams)
pub fn set_xid_ceiling(&mut self, xid: XidParams)
Sets this station’s XID ceiling — the ceiling is what SNRM proposes
and what UA’s negotiated values are tightened from — and immediately
resets the negotiated Self::xid to it. Call before Self::connect
(client) or before the next SNRM is received (server).
Sourcepub fn xid(&self) -> XidParams
pub fn xid(&self) -> XidParams
Returns the XID parameters actually negotiated with the peer (the ceiling, tightened by the peer’s counter-proposal in the last SNRM/UA exchange). Before the first successful exchange this equals the configured ceiling.
Sourcepub fn connect(&mut self) -> Result<()>
pub fn connect(&mut self) -> Result<()>
Client: establishes the data link (NDM → NRM) with an SNRM/UA
exchange, negotiating XID parameters (IEC 62056-46 §6.4.4.4.3.2 —
see Self::set_xid_ceiling / Self::xid) and resetting both
sequence numbers (§6.4.4.2).
Sourcepub fn disconnect(&mut self) -> Result<()>
pub fn disconnect(&mut self) -> Result<()>
Client: releases the data link (NRM → NDM) with a DISC/UA exchange. A DM answer also completes the release (the peer was already disconnected).
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns whether the data link is connected (NRM).
Sourcefn read_decoded_frame(&mut self) -> Result<HdlcFrame>
fn read_decoded_frame(&mut self) -> Result<HdlcFrame>
Reads frames until one decodes cleanly. Bad FCS/HCS frames are dropped
silently; an unknown control field in NRM is answered with FRMR (W)
(ISO 13239 / Yellow Book HDLC_COMMAND — C++ OSP_ERR_UNSUPPORTED path).
Sourcefn wait_for_rr(&mut self, expected_nr: u8) -> Result<()>
fn wait_for_rr(&mut self, expected_nr: u8) -> Result<()>
Blocks until an RR/RNR acknowledges expected_nr (= next N(S) to send).
Partial acks with N(R) behind expected_nr are ignored; N(R) ahead of
V(S) yields FRMR (Z).
Sourcefn send_unnumbered(&mut self, control: Control) -> Result<()>
fn send_unnumbered(&mut self, control: Control) -> Result<()>
Sends an unnumbered response frame (server side).
Sourcefn send_unnumbered_with_info(
&mut self,
control: Control,
information: Vec<u8>,
) -> Result<()>
fn send_unnumbered_with_info( &mut self, control: Control, information: Vec<u8>, ) -> Result<()>
Sends an unnumbered frame carrying an information field (used for UA’s XID reply to SNRM).
Sourcepub fn transport_mut(&mut self) -> &mut T
pub fn transport_mut(&mut self) -> &mut T
Returns a mutable reference to the underlying transport.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the layer and returns the underlying transport.
fn llc(&self) -> [u8; 3]
Sourcefn read_frame(&mut self) -> Result<Vec<u8>>
fn read_frame(&mut self) -> Result<Vec<u8>>
Reads one complete frame (from an opening flag to a closing flag) from the transport.
Waits up to the configured inactivity timeout for the opening flag of a new frame, then up to the (tighter) inter-octet timeout for every subsequent octet of that same frame.
An inter-octet timeout mid-frame discards the incomplete octets and
keeps waiting for the next frame (C++ session_recv_frame parity) —
it is not surfaced as an error. An inactivity timeout with an empty
buffer marks the link as NDM and returns
std::io::ErrorKind::TimedOut.
Both timeouts require the transport to honour
PhysicalTransport::set_read_timeout; a transport that doesn’t
(the default) makes reads block indefinitely as before.
Sourcefn read_frame_body(&mut self) -> Result<Vec<u8>>
fn read_frame_body(&mut self) -> Result<Vec<u8>>
Reads the format field, payload and closing flag of a frame whose opening flag octet has already been consumed.
Trait Implementations§
Source§impl<T: PhysicalTransport> DataLinkLayer for HdlcLayer<T>
impl<T: PhysicalTransport> DataLinkLayer for HdlcLayer<T>
Source§fn send_apdu(&mut self, apdu: &[u8]) -> Result<()>
fn send_apdu(&mut self, apdu: &[u8]) -> Result<()>
Frames and sends apdu, splitting it into consecutive I-frames (the
format field’s segmentation bit set on every frame but the last) when
the LLC-prefixed payload exceeds the negotiated max_info_tx (see
Self::xid) — the mirror of the segmented-frame reassembly already
performed by Self::receive_apdu.
With window_tx == 1 the server waits for the peer’s RR after
each non-final segment (etalon behaviour). Blasting every segment
back-to-back leaves the client’s intermediate RRs (N(R) behind
send_seq) to be mis-classified as invalid N(R) → FRMR once
Self::receive_apdu resumes.