pub struct Frame<'a> {
pub header: FrameHeader,
pub data: &'a [u8],
}Expand description
A frame whose data borrows from the caller or from decoder storage.
Fields§
§header: FrameHeaderHeader fields.
data: &'a [u8]The data field as the sender’s client gave it: an NPDU, test data or proprietary content. An extended frame carries it COBS-encoded on the wire; here it is decoded.
Implementations§
Source§impl<'a> Frame<'a>
impl<'a> Frame<'a>
Sourcepub const fn new(
frame_type: FrameType,
destination: u8,
source: u8,
data: &'a [u8],
) -> Self
pub const fn new( frame_type: FrameType, destination: u8, source: u8, data: &'a [u8], ) -> Self
Construct a frame.
Sourcepub fn encode_into(self, output: &mut [u8]) -> Result<usize, FrameError>
pub fn encode_into(self, output: &mut [u8]) -> Result<usize, FrameError>
Encode this frame at the start of output and return its wire length.
The frame is checked first, as described in the crate docs:
a source other than 255; for a standard type, at most
MAX_STANDARD_MPDU_DATA octets of data, none on a type that never
carries any, and a vendor identifier’s two octets on a proprietary
type; for a COBS type, a Length within the COBS bounds and one of the
two extended data types. Nothing is written unless the frame passes
and fits.
use bacnet_mstp_codec::{Frame, FrameError, FrameType};
let mut wire = [0u8; 8];
let length = Frame::new(FrameType::TOKEN, 0x10, 0x05, &[]).encode_into(&mut wire)?;
assert_eq!(wire[..length], [0x55, 0xFF, 0x00, 0x10, 0x05, 0x00, 0x00, 0x8C]);
// A Token never carries data.
assert_eq!(
Frame::new(FrameType::TOKEN, 0x10, 0x05, &[1]).encode_into(&mut wire),
Err(FrameError::UnexpectedData { raw_type: 0, length: 1 }),
);Sourcepub fn encode_to_sink<S: FrameSink + ?Sized>(
self,
sink: &mut S,
) -> Result<usize, FrameError>
pub fn encode_to_sink<S: FrameSink + ?Sized>( self, sink: &mut S, ) -> Result<usize, FrameError>
Encode this frame into sink and return its wire length.
The checks are those of Frame::encode_into. When the sink reports a
fixed capacity, a frame that does not fit fails before anything is
written. A growable sink is written directly, so if one of its writes
fails, the octets written before it stay in the sink; see
FrameSink.