libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
doip::DoIPMessage Class Reference

Represents a complete DoIP message with internal ByteArray representation. More...

#include <DoIPMessage.h>

Collaboration diagram for doip::DoIPMessage:

Public Member Functions

 DoIPMessage ()
 Default constructor - creates invalid empty message. More...
 
 DoIPMessage (DoIPPayloadType payloadType, const ByteArray &payload)
 Constructs a DoIP message with payload type and data. More...
 
 DoIPMessage (DoIPPayloadType payloadType, ByteArray &&payload)
 Constructs a DoIP message with payload type and data (move semantics). More...
 
 DoIPMessage (DoIPPayloadType payloadType, std::initializer_list< uint8_t > init_list)
 Constructs a DoIP message with payload type and initializer list. More...
 
 DoIPMessage (DoIPPayloadType payloadType, const uint8_t *data, size_t size)
 Constructs a DoIP message from raw byte array. More...
 
template<typename Iterator >
 DoIPMessage (DoIPPayloadType payloadType, Iterator first, Iterator last)
 Constructs a DoIP message from iterator range. More...
 
DoIPPayloadType getPayloadType () const
 Gets the payload type of this message. More...
 
ByteArrayRef getPayload () const
 Gets the payload data (without header). More...
 
ByteArrayRef getDiagnosticMessagePayload () const
 Gets the payload data (without header). More...
 
size_t getPayloadSize () const
 Gets the payload size in bytes (without header). More...
 
size_t getMessageSize () const
 Get the complete message size (header + payload). More...
 
ByteArrayRef getData () const
 Gets direct access to the complete message data for sending. More...
 
const uint8_t * data () const
 Gets direct pointer to the message data (for legacy APIs). More...
 
size_t size () const
 Gets the size for use with legacy APIs. More...
 
const ByteArrayasByteArray () const
 Get the complete message as ByteArray. More...
 
ByteArray copyAsByteArray () const
 Get a copy of the complete message as ByteArray. More...
 
bool hasSourceAddress () const
 Check if the message has a Source Address field. More...
 
std::optional< DoIPAddressgetSourceAddress () const
 Get the Source Address of the message (if message is a Diagnostic Message). More...
 
std::optional< DoIPAddressgetLogicalAddress () const
 Get the Logical Address of the message (if message is a Vehicle Identification Response). More...
 
std::optional< DoIPAddressgetTargetAddress () const
 Get the Target Address of the message (if message is a Diagnostic Message). More...
 
std::optional< DoIpVingetVin () const
 Get the vehicle identification number (VIN) if message is a Vehicle Identification Response. More...
 
std::optional< DoIpEidgetEid () const
 Get the entity id (EID) if message is a Vehicle Identification Response. More...
 
std::optional< DoIpGidgetGid () const
 Get the group id (GID) if message is a Vehicle Identification Response. More...
 
std::optional< DoIPFurtherActiongetFurtherActionRequest () const
 Get the Further Action Request object if message is a Vehicle Identification Response. More...
 
bool isValid () const
 Checks if the message is valid. More...
 

Static Public Member Functions

static bool isValidProtocolVersion (const uint8_t *data, size_t length, size_t offset=0)
 Checks if the protocol version is valid. More...
 
static std::optional< std::pair< DoIPPayloadType, uint32_t > > tryParseHeader (const uint8_t *data, size_t length)
 Gets the payload type from raw data. More...
 
static std::optional< DoIPMessagetryParse (const uint8_t *data, size_t length)
 Parse a DoIP message from raw data. More...
 

Protected Member Functions

void buildMessage (DoIPPayloadType payloadType, const ByteArray &payload)
 Builds the internal message representation. More...
 
bool isValidProtocolVersion () const
 Checks if the protocol version is valid. More...
 
uint32_t getPayloadLengthFromHeader () const
 Gets the payload length from the header. More...
 

Protected Attributes

ByteArray m_data
 Complete message data (header + payload) More...
 

Detailed Description

Represents a complete DoIP message with internal ByteArray representation.

The message is stored internally as a complete ByteArray including the 8-byte header and payload. This eliminates the need for copying when sending messages.

Memory layout: [0] Protocol Version [1] Inverse Protocol Version [2-3] Payload Type (big-endian uint16_t) [4-7] Payload Length (big-endian uint32_t) [8...] Payload data

The message types are listed in table 17.

Definition at line 82 of file DoIPMessage.h.

Constructor & Destructor Documentation

◆ DoIPMessage() [1/6]

doip::DoIPMessage::DoIPMessage ( )
inline

Default constructor - creates invalid empty message.

Definition at line 88 of file DoIPMessage.h.

◆ DoIPMessage() [2/6]

doip::DoIPMessage::DoIPMessage ( DoIPPayloadType  payloadType,
const ByteArray payload 
)
inlineexplicit

Constructs a DoIP message with payload type and data.

Parameters
payloadTypeThe payload type for this message
payloadThe payload data as a vector of bytes

Definition at line 96 of file DoIPMessage.h.

References buildMessage().

◆ DoIPMessage() [3/6]

doip::DoIPMessage::DoIPMessage ( DoIPPayloadType  payloadType,
ByteArray &&  payload 
)
inlineexplicit

Constructs a DoIP message with payload type and data (move semantics).

Parameters
payloadTypeThe payload type for this message
payloadThe payload data as a vector of bytes (moved)

Definition at line 106 of file DoIPMessage.h.

References buildMessage().

◆ DoIPMessage() [4/6]

doip::DoIPMessage::DoIPMessage ( DoIPPayloadType  payloadType,
std::initializer_list< uint8_t >  init_list 
)
inline

Constructs a DoIP message with payload type and initializer list.

Parameters
payloadTypeThe payload type for this message
init_listInitializer list of bytes for the payload

Definition at line 116 of file DoIPMessage.h.

References buildMessage().

◆ DoIPMessage() [5/6]

doip::DoIPMessage::DoIPMessage ( DoIPPayloadType  payloadType,
const uint8_t *  data,
size_t  size 
)
inlineexplicit

Constructs a DoIP message from raw byte array.

Parameters
payloadTypeThe payload type for this message
dataPointer to raw payload byte data
sizeNumber of bytes to copy from payload data

Definition at line 128 of file DoIPMessage.h.

References buildMessage(), data(), and size().

◆ DoIPMessage() [6/6]

template<typename Iterator >
doip::DoIPMessage::DoIPMessage ( DoIPPayloadType  payloadType,
Iterator  first,
Iterator  last 
)
inlineexplicit

Constructs a DoIP message from iterator range.

Template Parameters
IteratorInput iterator type
Parameters
payloadTypeThe payload type for this message
firstIterator to the beginning of the data range
lastIterator to the end of the data range

Definition at line 142 of file DoIPMessage.h.

References buildMessage().

Member Function Documentation

◆ asByteArray()

const ByteArray& doip::DoIPMessage::asByteArray ( ) const
inline

Get the complete message as ByteArray.

Returns
const ByteArray& Reference to the complete message data

Definition at line 240 of file DoIPMessage.h.

References m_data.

◆ buildMessage()

void doip::DoIPMessage::buildMessage ( DoIPPayloadType  payloadType,
const ByteArray payload 
)
inlineprotected

Builds the internal message representation.

Parameters
payloadTypeThe payload type
payloadThe payload data

Definition at line 452 of file DoIPMessage.h.

References doip::DOIP_HEADER_SIZE, m_data, doip::PROTOCOL_VERSION, doip::PROTOCOL_VERSION_INV, doip::ByteArray::writeEnum(), and doip::ByteArray::writeU32BE().

Referenced by DoIPMessage().

◆ copyAsByteArray()

ByteArray doip::DoIPMessage::copyAsByteArray ( ) const
inline

Get a copy of the complete message as ByteArray.

Returns
ByteArray A copy of the complete message data

Definition at line 249 of file DoIPMessage.h.

References m_data.

◆ data()

const uint8_t* doip::DoIPMessage::data ( ) const
inline

◆ getData()

ByteArrayRef doip::DoIPMessage::getData ( ) const
inline

Gets direct access to the complete message data for sending.

This method returns a pointer and size without copying, making it efficient for network transmission.

Returns
ByteArrayRef Reference to the complete message (header + payload)

Definition at line 213 of file DoIPMessage.h.

References m_data.

◆ getDiagnosticMessagePayload()

ByteArrayRef doip::DoIPMessage::getDiagnosticMessagePayload ( ) const
inline

Gets the payload data (without header).

Returns
ByteArrayRef pointing to the payload portion

Definition at line 177 of file DoIPMessage.h.

References doip::DOIP_DIAG_HEADER_SIZE, and m_data.

Referenced by doip::operator<<().

◆ getEid()

std::optional<DoIpEid> doip::DoIPMessage::getEid ( ) const
inline

Get the entity id (EID) if message is a Vehicle Identification Response.

Returns
std::optional<DoIPAddress> The VIN if present, std::nullopt otherwise

Definition at line 327 of file DoIPMessage.h.

References getPayload(), getPayloadType(), and doip::VehicleIdentificationResponse.

◆ getFurtherActionRequest()

std::optional<DoIPFurtherAction> doip::DoIPMessage::getFurtherActionRequest ( ) const
inline

Get the Further Action Request object if message is a Vehicle Identification Response.

Returns
std::optional<DoIPFurtherAction> The Further Action Request if present, std::nullopt otherwise

Definition at line 353 of file DoIPMessage.h.

References getPayload(), getPayloadType(), and doip::VehicleIdentificationResponse.

◆ getGid()

std::optional<DoIpGid> doip::DoIPMessage::getGid ( ) const
inline

Get the group id (GID) if message is a Vehicle Identification Response.

Returns
std::optional<DoIPAddress> The VIN if present, std::nullopt otherwise

Definition at line 340 of file DoIPMessage.h.

References getPayload(), getPayloadType(), and doip::VehicleIdentificationResponse.

◆ getLogicalAddress()

std::optional<DoIPAddress> doip::DoIPMessage::getLogicalAddress ( ) const
inline

Get the Logical Address of the message (if message is a Vehicle Identification Response).

Returns
std::optional<DoIPAddress> The logical address if present, std::nullopt otherwise

Definition at line 288 of file DoIPMessage.h.

References getPayload(), getPayloadType(), doip::readAddressFrom(), and doip::VehicleIdentificationResponse.

◆ getMessageSize()

size_t doip::DoIPMessage::getMessageSize ( ) const
inline

Get the complete message size (header + payload).

Returns
size_t Total number of bytes

Definition at line 201 of file DoIPMessage.h.

References m_data.

◆ getPayload()

ByteArrayRef doip::DoIPMessage::getPayload ( ) const
inline

Gets the payload data (without header).

Returns
ByteArrayRef pointing to the payload portion

Definition at line 165 of file DoIPMessage.h.

References doip::DOIP_HEADER_SIZE, and m_data.

Referenced by getEid(), getFurtherActionRequest(), getGid(), getLogicalAddress(), getSourceAddress(), getTargetAddress(), getVin(), hasSourceAddress(), and doip::operator<<().

◆ getPayloadLengthFromHeader()

uint32_t doip::DoIPMessage::getPayloadLengthFromHeader ( ) const
inlineprotected

Gets the payload length from the header.

Returns
size_t The payload length stored in the header

Definition at line 491 of file DoIPMessage.h.

References doip::DOIP_HEADER_SIZE, m_data, and doip::ByteArray::readU32BE().

Referenced by isValid().

◆ getPayloadSize()

size_t doip::DoIPMessage::getPayloadSize ( ) const
inline

Gets the payload size in bytes (without header).

Returns
size_t Number of bytes in the payload

Definition at line 189 of file DoIPMessage.h.

References doip::DOIP_HEADER_SIZE, and m_data.

Referenced by isValid(), and doip::operator<<().

◆ getPayloadType()

DoIPPayloadType doip::DoIPMessage::getPayloadType ( ) const
inline

Gets the payload type of this message.

Returns
The DoIP payload type

Definition at line 152 of file DoIPMessage.h.

References doip::DOIP_HEADER_SIZE, m_data, and doip::NegativeAck.

Referenced by getEid(), getFurtherActionRequest(), getGid(), getLogicalAddress(), getTargetAddress(), getVin(), hasSourceAddress(), doip::operator<<(), and doip::DoIPClient::receiveVehicleAnnouncement().

◆ getSourceAddress()

std::optional<DoIPAddress> doip::DoIPMessage::getSourceAddress ( ) const
inline

Get the Source Address of the message (if message is a Diagnostic Message).

Returns
std::optional<DoIPAddress> The source address if present, std::nullopt otherwise

Definition at line 274 of file DoIPMessage.h.

References getPayload(), hasSourceAddress(), and doip::readAddressFrom().

Referenced by doip::message::makeRoutingActivationResponse(), and doip::operator<<().

◆ getTargetAddress()

std::optional<DoIPAddress> doip::DoIPMessage::getTargetAddress ( ) const
inline

Get the Target Address of the message (if message is a Diagnostic Message).

Returns
std::optional<DoIPAddress> The target address if present, std::nullopt otherwise

Definition at line 301 of file DoIPMessage.h.

References doip::DiagnosticMessage, getPayload(), getPayloadType(), and doip::readAddressFrom().

Referenced by doip::operator<<().

◆ getVin()

std::optional<DoIpVin> doip::DoIPMessage::getVin ( ) const
inline

Get the vehicle identification number (VIN) if message is a Vehicle Identification Response.

Returns
std::optional<DoIPAddress> The VIN if present, std::nullopt otherwise

Definition at line 314 of file DoIPMessage.h.

References getPayload(), getPayloadType(), and doip::VehicleIdentificationResponse.

◆ hasSourceAddress()

bool doip::DoIPMessage::hasSourceAddress ( ) const
inline

Check if the message has a Source Address field.

Returns
Returns true in the case of success, false otherwise.

Definition at line 258 of file DoIPMessage.h.

References doip::AliveCheckResponse, doip::DiagnosticMessage, getPayload(), getPayloadType(), doip::RoutingActivationRequest, and doip::RoutingActivationResponse.

Referenced by getSourceAddress().

◆ isValid()

bool doip::DoIPMessage::isValid ( ) const
inline

Checks if the message is valid.

Returns
bool True if the message has a valid structure

Definition at line 366 of file DoIPMessage.h.

References doip::DOIP_HEADER_SIZE, getPayloadLengthFromHeader(), getPayloadSize(), isValidProtocolVersion(), and m_data.

◆ isValidProtocolVersion() [1/2]

bool doip::DoIPMessage::isValidProtocolVersion ( ) const
inlineprotected

Checks if the protocol version is valid.

Returns
bool True if valid

Definition at line 476 of file DoIPMessage.h.

References doip::ISO_13400_2010, doip::ISO_13400_2025, and m_data.

Referenced by isValid(), and tryParseHeader().

◆ isValidProtocolVersion() [2/2]

static bool doip::DoIPMessage::isValidProtocolVersion ( const uint8_t *  data,
size_t  length,
size_t  offset = 0 
)
inlinestatic

Checks if the protocol version is valid.

Parameters
dataPointer to the byte array
lengthLength of the data
offsetOffset to start checking from (default: 0)
Returns
bool True if valid

Definition at line 379 of file DoIPMessage.h.

References data(), doip::ISO_13400_2010, and doip::ISO_13400_2025.

◆ size()

◆ tryParse()

static std::optional<DoIPMessage> doip::DoIPMessage::tryParse ( const uint8_t *  data,
size_t  length 
)
inlinestatic

Parse a DoIP message from raw data.

Parameters
dataPointer to raw message data
lengthLength of the data
Returns
std::optional<DoIPMessage> The parsed message or std::nullopt if invalid

Definition at line 425 of file DoIPMessage.h.

References data(), doip::DOIP_HEADER_SIZE, m_data, and tryParseHeader().

Referenced by doip::DoIPClient::receiveMessage(), doip::DoIPClient::receiveUdpMessage(), and doip::DoIPClient::receiveVehicleAnnouncement().

◆ tryParseHeader()

static std::optional<std::pair<DoIPPayloadType, uint32_t> > doip::DoIPMessage::tryParseHeader ( const uint8_t *  data,
size_t  length 
)
inlinestatic

Gets the payload type from raw data.

Parameters
dataPointer to the byte array
lengthLength of the data
Returns
std::optional<DoIPPayloadType> The payload type if valid

Definition at line 399 of file DoIPMessage.h.

References data(), doip::DOIP_HEADER_SIZE, isValidProtocolVersion(), doip::util::readU32BE(), and doip::toPayloadType().

Referenced by doip::DoIPConnection::receiveTcpMessage(), and tryParse().

Member Data Documentation

◆ m_data


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