PROFINET IO Controller Stack 1.0.0
Modern C++ implementation of a PROFINET IO Controller stack
Loading...
Searching...
No Matches
profinet::wire::Reader Class Reference

Lightweight cursor for parsing a big-endian byte buffer without copies. More...

#include <wire.h>

+ Collaboration diagram for profinet::wire::Reader:

Public Member Functions

 Reader (const std::uint8_t *data, std::size_t len)
 Construct a reader over a raw pointer/length pair.
 
 Reader (std::span< const std::uint8_t > bytes)
 Construct a reader over a byte span.
 
 Reader (const std::vector< std::uint8_t > &v)
 Construct a reader over an existing byte vector.
 
std::size_t Remaining () const
 Number of bytes not yet consumed.
 
std::size_t Position () const
 Current read offset from the start of the buffer.
 
const std::uint8_t * Cursor () const
 Pointer to the next unread byte.
 
void Need (std::size_t n) const
 Assert that at least n bytes remain.
 
std::uint8_t U8 ()
 Read and consume one byte.
 
std::uint16_t U16 ()
 Read and consume a big-endian 16-bit value.
 
std::uint32_t U32 ()
 Read and consume a big-endian 32-bit value.
 
std::uint16_t U16LittleEndian ()
 Read and consume a little-endian 16-bit value.
 
std::uint32_t U32LittleEndian ()
 Read and consume a little-endian 32-bit value.
 
std::vector< std::uint8_t > ReadBytes (std::size_t n)
 Read and consume a variable-length byte range.
 
template<std::size_t N>
std::array< std::uint8_t, N > Fixed ()
 Read and consume a fixed-size byte array.
 
void Skip (std::size_t n)
 Advance the read position without returning the skipped bytes.
 
void SkipTo (std::size_t offset)
 Move the read position to an absolute byte offset.
 

Private Attributes

const std::uint8_t * data
 Pointer to the start of the underlying buffer (not owned).
 
std::size_t len
 Total length of the underlying buffer in bytes.
 
std::size_t pos = 0
 Current read offset from the start of the buffer.
 

Detailed Description

Lightweight cursor for parsing a big-endian byte buffer without copies.

Every read method advances the internal position and throws std::out_of_range if the buffer doesn't have enough remaining bytes.

Definition at line 191 of file wire.h.

Constructor & Destructor Documentation

◆ Reader() [1/3]

profinet::wire::Reader::Reader ( const std::uint8_t *  data,
std::size_t  len 
)
inline

Construct a reader over a raw pointer/length pair.

Parameters
dataPointer to the first byte of the buffer. Must outlive the Reader.
lenNumber of bytes available starting at Data.

Definition at line 197 of file wire.h.

◆ Reader() [2/3]

profinet::wire::Reader::Reader ( std::span< const std::uint8_t >  bytes)
inlineexplicit

Construct a reader over a byte span.

Parameters
bytesSpan containing the bytes to read. Must outlive the Reader.

Definition at line 204 of file wire.h.

◆ Reader() [3/3]

profinet::wire::Reader::Reader ( const std::vector< std::uint8_t > &  v)
inlineexplicit

Construct a reader over an existing byte vector.

Parameters
vVector to read from. Must outlive the Reader.

Definition at line 211 of file wire.h.

Member Function Documentation

◆ Cursor()

const std::uint8_t * profinet::wire::Reader::Cursor ( ) const
inline

Pointer to the next unread byte.

Returns
Pointer into the underlying buffer at the current position.

Definition at line 232 of file wire.h.

References data, and pos.

Referenced by profinet::wire::TEST().

+ Here is the caller graph for this function:

◆ Fixed()

template<std::size_t N>
std::array< std::uint8_t, N > profinet::wire::Reader::Fixed ( )
inline

Read and consume a fixed-size byte array.

Template Parameters
NNumber of bytes to read.
Returns
A copy of the next N bytes as a fixed-size array.

Definition at line 316 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::dcp::DCPDeviceDescription::DCPDeviceDescription(), profinet::rpc::RPCCon::EpmLookup(), profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNIODReleaseBlock::Parse(), profinet::PNInM0::Parse(), profinet::PNInM1::Parse(), profinet::PNInM2::Parse(), profinet::PNInM3::Parse(), profinet::PNInM4::Parse(), profinet::PNInM5::Parse(), profinet::PNInMReserved< Idx >::Parse(), profinet::PNIOCRBlockRes::Parse(), profinet::PNIOCRBlockReqHeader::Parse(), profinet::PNAlarmCRBlockReq::Parse(), profinet::PNAlarmCRBlockRes::Parse(), profinet::PNAlarmNotificationPDU::Parse(), profinet::PNIODWriteRes::Parse(), profinet::sim::VirtualProfinetRpcDevice::ParseArIdentity(), profinet::rpc::ParseEpmTower(), profinet::rpc::raw::ParseRawRpcHeader(), profinet::TEST(), profinet::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::TEST_F(), and profinet::rpc::UuidBytesToString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Need()

void profinet::wire::Reader::Need ( std::size_t  n) const
inline

Assert that at least n bytes remain.

Parameters
nNumber of bytes required.

Throws std::out_of_range if fewer than n bytes remain.

Definition at line 240 of file wire.h.

References Remaining().

Referenced by Fixed(), ReadBytes(), Skip(), profinet::wire::TEST(), profinet::wire::TEST(), U16(), U16LittleEndian(), U32(), U32LittleEndian(), and U8().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Position()

◆ ReadBytes()

std::vector< std::uint8_t > profinet::wire::Reader::ReadBytes ( std::size_t  n)
inline

Read and consume a variable-length byte range.

Parameters
nNumber of bytes to read.
Returns
A copy of the next n bytes.

Definition at line 304 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::rt::RTFrame::FromBytes(), profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNNRDData::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNInM0::Parse(), profinet::PNInM1::Parse(), profinet::PNInM2::Parse(), profinet::PNInM3::Parse(), profinet::PNInM5::Parse(), profinet::PNInMReserved< Idx >::Parse(), profinet::PNAlarmNotificationPDU::Parse(), profinet::PNRTAHeader::Parse(), profinet::rpc::ParseEpmTower(), profinet::TEST(), profinet::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), and profinet::wire::TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ Remaining()

std::size_t profinet::wire::Reader::Remaining ( ) const
inline

Number of bytes not yet consumed.

Returns
Byte count remaining between the current position and the end of the buffer.

Definition at line 218 of file wire.h.

References len, and pos.

Referenced by profinet::rt::RTFrame::FromBytes(), Need(), profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNNRDData::Parse(), profinet::PNIODHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNInMReserved< Idx >::Parse(), profinet::PNAlarmNotificationPDU::Parse(), profinet::PNRTAHeader::Parse(), profinet::rpc::ParseEpmTower(), profinet::blocks::ParseModuleDiffBlock(), profinet::blocks::ParseRealIdentificationData(), profinet::SkipVlanTags(), profinet::TEST(), profinet::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), and profinet::wire::TEST().

+ Here is the caller graph for this function:

◆ Skip()

void profinet::wire::Reader::Skip ( std::size_t  n)
inline

Advance the read position without returning the skipped bytes.

Parameters
nNumber of bytes to skip.

Definition at line 330 of file wire.h.

References Need(), and pos.

Referenced by profinet::dcp::DCPDeviceDescription::DCPDeviceDescription(), profinet::rt::ParseEthernetFrame(), profinet::blocks::ParseModuleDiffBlock(), profinet::blocks::ParseMultipleBlockHeader(), profinet::dcp::ParseSetResponse(), profinet::blocks::ParseWriteMultipleResponse(), profinet::dcp::ReadResponse(), profinet::dcp::ReceiveHello(), profinet::dcp::RecvSetResponse(), profinet::SkipVlanTags(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), TEST_F(), TEST_F(), and TEST_F().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ SkipTo()

void profinet::wire::Reader::SkipTo ( std::size_t  offset)
inline

Move the read position to an absolute byte offset.

Parameters
offsetZero-based byte offset from the start of the buffer.

The requested offset must be within the buffer. Moving to exactly the end of the buffer is valid and leaves no bytes remaining.

Exceptions
std::out_of_rangeif offset is greater than the buffer length.

Definition at line 343 of file wire.h.

References len, and pos.

Referenced by profinet::rpc::RPCCon::ApplicationReady(), profinet::rpc::RPCCon::EpmLookup(), profinet::sim::VirtualProfinetRpcDevice::HandleControl(), profinet::AlarmListener::HandleLayer2Frame(), profinet::PNBlockHeader::Parse(), profinet::ParseAlarmItem(), profinet::sim::VirtualProfinetRpcDevice::ParseArIdentity(), profinet::diagnosis::ParseDiagnosisBlock(), profinet::diagnosis::ParseDiagnosisSimple(), profinet::rpc::ParseEpmTower(), profinet::rt::ParseEthernetFrame(), profinet::blocks::ParsePdPortDataReal(), profinet::rpc::raw::ParseRawRpcHeader(), profinet::blocks::ParseWriteMultipleResponse(), profinet::rpc::PeekBlockHeader(), profinet::cyclic::CyclicController::ProcessInputFrame(), profinet::SkipVlanTags(), profinet::CyclicCaptureTests::TEST(), profinet::dcp::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), profinet::rpc::TEST_F(), and profinet::rpc::TEST_F().

+ Here is the caller graph for this function:

◆ U16()

std::uint16_t profinet::wire::Reader::U16 ( )
inline

Read and consume a big-endian 16-bit value.

Returns
The decoded value.

Definition at line 258 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::dcp::DCPDeviceDescription::DCPDeviceDescription(), profinet::rpc::RPCCon::ApplicationReady(), profinet::rt::RTFrame::FromBytes(), profinet::sim::VirtualProfinetRpcDevice::HandleControl(), profinet::AlarmListener::HandleLayer2Frame(), profinet::EthernetHeader::Parse(), profinet::EthernetVLANHeader::Parse(), profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNIODHeader::Parse(), profinet::PNBlockHeader::Parse(), profinet::PNARBlockRequest::Parse(), profinet::PNIODReleaseBlock::Parse(), profinet::PNInM0::Parse(), profinet::IOCRAPIObject::Parse(), profinet::PNIOCRBlockRes::Parse(), profinet::PNIOCRBlockReqHeader::Parse(), profinet::PNAlarmCRBlockReq::Parse(), profinet::PNAlarmCRBlockRes::Parse(), profinet::PNAlarmNotificationPDU::Parse(), profinet::PNRTAHeader::Parse(), profinet::PNIODWriteRes::Parse(), profinet::ParseAlarmItem(), profinet::ParseAlarmNotification(), profinet::sim::VirtualProfinetRpcDevice::ParseArIdentity(), profinet::blocks::ParseBlockHeader(), profinet::diagnosis::ParseDiagnosisBlock(), profinet::diagnosis::ParseDiagnosisSimple(), profinet::rpc::ParseEpmTower(), profinet::rt::ParseEthernetFrame(), profinet::blocks::ParseModuleDiffBlock(), profinet::blocks::ParseMultipleBlockHeader(), profinet::blocks::ParsePdPortDataReal(), profinet::blocks::ParsePortStatistics(), profinet::rpc::raw::ParseRawRpcHeader(), profinet::blocks::ParseRealIdentificationData(), profinet::dcp::ParseSetResponse(), profinet::blocks::ParseWriteMultipleResponse(), profinet::rpc::PeekBlockHeader(), profinet::cyclic::CyclicController::ProcessInputFrame(), profinet::dcp::ReadResponse(), profinet::dcp::ReceiveHello(), profinet::dcp::RecvSetResponse(), profinet::SkipVlanTags(), profinet::CyclicCaptureTests::TEST(), profinet::dcp::TEST(), profinet::TEST(), profinet::TEST(), profinet::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), profinet::device::TEST_F(), profinet::device::TEST_F(), profinet::device::TEST_F(), and profinet::rpc::UuidBytesToString().

+ Here is the call graph for this function:

◆ U16LittleEndian()

std::uint16_t profinet::wire::Reader::U16LittleEndian ( )
inline

Read and consume a little-endian 16-bit value.

Returns
The decoded value.

Definition at line 279 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::rpc::RPCCon::EpmLookup(), profinet::rpc::ParseEpmTower(), profinet::rpc::raw::ParseRawRpcHeader(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::rpc::TEST_F(), profinet::rpc::TEST_F(), and profinet::rpc::UuidBytesToString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ U32()

◆ U32LittleEndian()

std::uint32_t profinet::wire::Reader::U32LittleEndian ( )
inline

Read and consume a little-endian 32-bit value.

Returns
The decoded value.

Definition at line 291 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::rpc::RPCCon::ApplicationReady(), profinet::rpc::RPCCon::EpmLookup(), profinet::rpc::raw::ParseRawRpcHeader(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::rpc::TEST_F(), profinet::rpc::TEST_F(), and profinet::rpc::UuidBytesToString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ U8()

std::uint8_t profinet::wire::Reader::U8 ( )
inline

Read and consume one byte.

Returns
The byte at the current position.

Definition at line 250 of file wire.h.

References data, Need(), and pos.

Referenced by profinet::dcp::DCPDeviceDescription::DCPDeviceDescription(), profinet::rt::RTFrame::FromBytes(), profinet::PNDCPHeader::Parse(), profinet::PNDCPBlockRequest::Parse(), profinet::PNDCPBlock::Parse(), profinet::PNRPCHeader::Parse(), profinet::PNBlockHeader::Parse(), profinet::PNInM0::Parse(), profinet::PNRTAHeader::Parse(), profinet::blocks::ParseBlockHeader(), profinet::rpc::raw::ParseRawRpcHeader(), profinet::TEST(), profinet::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), profinet::wire::TEST(), and profinet::wire::TEST().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ data

const std::uint8_t* profinet::wire::Reader::data
private

Pointer to the start of the underlying buffer (not owned).

Definition at line 355 of file wire.h.

Referenced by Cursor(), Fixed(), ReadBytes(), U16(), U16LittleEndian(), U32(), U32LittleEndian(), and U8().

◆ len

std::size_t profinet::wire::Reader::len
private

Total length of the underlying buffer in bytes.

Definition at line 358 of file wire.h.

Referenced by Remaining(), and SkipTo().

◆ pos

std::size_t profinet::wire::Reader::pos = 0
private

Current read offset from the start of the buffer.

Definition at line 361 of file wire.h.

Referenced by Cursor(), Fixed(), Position(), ReadBytes(), Remaining(), Skip(), SkipTo(), U16(), U16LittleEndian(), U32(), U32LittleEndian(), and U8().


The documentation for this class was generated from the following file: