Skip to main content

HdlcLayer

Struct HdlcLayer 

Source
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: HdlcAddress

Address of the peer this layer sends frames to.

§own: HdlcAddress

This station’s own address (used as the source address).

§is_client: bool§send_seq: u8§recv_seq: u8§connected: bool

Whether the data link is in NRM (connected) as opposed to NDM.

§inter_octet_timeout: Duration

Maximum 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: XidParams

This station’s configured XID ceiling, restored at the start of every SNRM/UA negotiation.

§xid: XidParams

The XID parameters actually negotiated with the peer (tightened from xid_configured by the last SNRM/UA exchange).

Implementations§

Source§

impl<T: PhysicalTransport> HdlcLayer<T>

Source

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.

Source

pub fn new_server( transport: T, server: HdlcAddress, client: HdlcAddress, ) -> Self

Creates a server-side HDLC layer.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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).

Source

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).

Source

pub fn is_connected(&self) -> bool

Returns whether the data link is connected (NRM).

Source

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).

Source

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).

Source

fn send_unnumbered(&mut self, control: Control) -> Result<()>

Sends an unnumbered response frame (server side).

Source

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).

Source

pub fn transport_mut(&mut self) -> &mut T

Returns a mutable reference to the underlying transport.

Source

pub fn into_inner(self) -> T

Consumes the layer and returns the underlying transport.

Source

fn llc(&self) -> [u8; 3]

Source

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.

Source

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>

Source§

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.

Source§

fn receive_apdu(&mut self) -> Result<Vec<u8>>

Receives and de-frames one APDU.
Source§

fn client_sap(&self) -> Option<u8>

Client SAP learned from the data-link peer address, when known.
Source§

impl<T: Debug + PhysicalTransport> Debug for HdlcLayer<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for HdlcLayer<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for HdlcLayer<T>
where T: RefUnwindSafe,

§

impl<T> Send for HdlcLayer<T>
where T: Send,

§

impl<T> Sync for HdlcLayer<T>
where T: Sync,

§

impl<T> Unpin for HdlcLayer<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for HdlcLayer<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for HdlcLayer<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more