Bus Messages 1.0
Bus Message Serialization Library
Loading...
Searching...
No Matches
bus::CanDataFrame Class Reference

Implements an interface against a CAN Data Frame message. More...

#include "bus/candataframe.h"

Inheritance diagram for bus::CanDataFrame:
Collaboration diagram for bus::CanDataFrame:

Public Member Functions

 CanDataFrame ()
 Deafult constructor.
 CanDataFrame (CanErrorType type)=delete
 CanDataFrame (const std::shared_ptr< IBusMessage > &message)
 Create a message from a raw byte array.
void MessageId (uint32_t msg_id)
 DBC message ID. Note that bit 31 indicate extended ID.
uint32_t MessageId () const
 DBC message ID. Note that bit 31 indicate extended ID.
void CanId (uint32_t can_id)
 Returns the 11/27 bit CAN ID.
uint32_t CanId () const
 29/11 bit CAN message ID. Note that bit 31 is not used.
void ExtendedId (bool extended)
 Set true if the CAN ID uses 20-bit addressing.
bool ExtendedId () const
 Returns true if the CAN ID uses 29-bit addressing,.
void Dlc (uint8_t dlc)
 Sets the CAN message data length code.
uint8_t Dlc () const
 Returns the data length code (DLC).
void Crc (uint32_t crc)
 Sets the CRC code.
uint32_t Crc () const
 Returns the CRC code.
void DataLength (uint8_t data_length)
 Sets number of data bytes.
uint8_t DataLength () const
 Returns number of data bytes.
void DataBytes (const std::vector< uint8_t > &data)
 Sets the payload data bytes.
const std::vector< uint8_t > & DataBytes () const
 Returns a reference to the payload data bytes.
void Dir (bool transmit)
 If set true, the message was transmitted.
bool Dir () const
 Returns true if the message was transmitted.
void Srr (bool srr)
 Sets the SRR bit. *‍/.
bool Srr () const
 Returns the SRR bit.
void Edl (bool edl)
 Extended (CAN FD) data length.
bool Edl () const
 Extended (CAN FD) data length.
void Brs (bool brs)
 Bit rate switch (CAN FD).
bool Brs () const
 Bit rate switch (CAN FD).
void Esi (bool esi)
 Error state indicator (CAN FD).
bool Esi () const
 Error state indicator (CAN FD).
void Rtr (bool rtr)
 Sets the RTR bit (remote frame).
bool Rtr () const
 Returns the RTR bit.
void WakeUp (bool wake_up)
 Indicate a CAN bus wake up status.
bool WakeUp () const
 Indicate a CAN bus wake up message.
void SingleWire (bool single_wire)
 Indicate a single wire CAN bus.
bool SingleWire () const
 Indicate a single wire CAN bus.
void R0 (bool flag)
 Optional R0 flag.
bool R0 () const
 Optional R0 flag.
void R1 (bool flag)
 Optional R1 flag.
bool R1 () const
 Optional R1 flag.
void FrameDuration (uint32_t duration)
 Frame duration in nano-seconds.
uint32_t FrameDuration () const
 Frame duration in nano-seconds.
void ToRaw (std::vector< uint8_t > &dest) const override
 Serialize the message.
void FromRaw (const std::vector< uint8_t > &source) override
 Deserialize the message.
Public Member Functions inherited from bus::IBusMessage
 IBusMessage (BusMessageType type)
BusMessageType Type () const
 Returns type of message.
void Version (uint16_t version)
 Sets the vewrsion number for a message.
uint16_t Version () const
 Returns the version number of the message.
uint32_t Size () const
 Returns the total size of the message.
bool Valid () const
 Returns true if the message is valid.
void Timestamp (uint64_t timestamp)
 Sets the absolute time.
uint64_t Timestamp () const
 Retuns the timestampe.
void BusChannel (uint16_t channel)
 Sets the source channel.
uint16_t BusChannel () const
 Returns the bus channel number.

Static Public Member Functions

static size_t DlcToLength (uint8_t dlc)
 Return the data length by DLC.
Static Public Member Functions inherited from bus::IBusMessage
static std::shared_ptr< IBusMessageCreate (BusMessageType type)
 Creates a message by its type.

Additional Inherited Members

Protected Member Functions inherited from bus::IBusMessage
void Size (uint32_t size) const
 Sets the total size of the message.
void Valid (bool valid) const
 Sets the message valid or invalid.

Detailed Description

The class implements an interface against a CAN Data Frame message. This message is the normal CAN messages while the other messages indicates some sort of exception or error.

The serialization is according to table below and uses little endian byte order.

CAN Data Frame Message Layout
Byte (Bits) OffsetDescriptionSize
0-17Type of Message (enumerate)18 bytes
18Message ID (CAN Id + IDE)uint32_t
22DLCuint8_t
23Data Lengthuint8_t
24CRCuint32_t
28:0Direction (Rx=0, Tx=1)1-bit
28:1SRR1-bit
28:2EDL1-bit
28:3BRS1-bit
28:4ESI1-bit
28:5RTR1-bit
28:6R01-bit
28:7R11-bit
29:0Wake Up1-bit
29:1Single Wire1-bit
30Frame Duration (ns)/td>uint32_t
34Data BytesData Length bytes

Member Function Documentation

◆ CanId()

uint32_t bus::CanDataFrame::CanId ( ) const
nodiscard

The CAN message ID identifies the message on a CAN bus. Note that there is a DBC message ID which is the CAN ID + highest bit 31 set if the CAN ID uses 29-bit addressing. This is a confusing naming that causes invalid handling of messages.

Returns
29-bit CAN ID.

◆ DataBytes()

void bus::CanDataFrame::DataBytes ( const std::vector< uint8_t > & data)

This function sets the payload data bytes in the message. Note that this function also set the data length and DLC code.

Parameters
data

◆ DataLength()

void bus::CanDataFrame::DataLength ( uint8_t data_length)

The data length is not sent on the bus. Instead is it calculated from the DLC code. Note that the DataBytes() function fix both data length and the DLC code so this function is normally not used.

Parameters
data_lengthNumber of payload data bytes.

◆ Dlc() [1/2]

uint8_t bus::CanDataFrame::Dlc ( ) const
inlinenodiscard

The DLC is equal to number of bytes for CAN messages but not for CAN FD messages.

Returns
Data length code for the message.

◆ Dlc() [2/2]

void bus::CanDataFrame::Dlc ( uint8_t dlc)
inline

Sets the data length code (DLC). The DLC is the same as data length for CAN but not for CAN FD. Note that the DataBytes() function fix both data length and the DLC code so this function is normally not used.

Parameters
dlcData length code.

◆ ExtendedId() [1/2]

bool bus::CanDataFrame::ExtendedId ( ) const
nodiscard
Returns
True if CAN ID uses extended addressing. Returns the extended CAN ID.

◆ ExtendedId() [2/2]

void bus::CanDataFrame::ExtendedId ( bool extended)
Parameters
extendedTrue if CAN ID uses 29-bit addressing.

◆ FromRaw()

void bus::CanDataFrame::FromRaw ( const std::vector< uint8_t > & source)
overridevirtual

Reads in the byte array and desrialize the message.

Parameters
sourceSource buffer.

Reimplemented from bus::IBusMessage.

◆ MessageId() [1/2]

uint32_t bus::CanDataFrame::MessageId ( ) const
nodiscard

The message ID is the CAN ID with the highest bit 31 set if the CAN ID uses a 29-bit address.

Returns
DBC message ID.

◆ MessageId() [2/2]

void bus::CanDataFrame::MessageId ( uint32_t msg_id)

The message ID is the CAN ID + highest bit 31 set if the CAN ID uses extended 29-bit ID. The message ID is actually used in a DBC file and not sent on the CAN bus, well the extended bit is sent.

Parameters
msg_idDBC Message ID

◆ ToRaw()

void bus::CanDataFrame::ToRaw ( std::vector< uint8_t > & dest) const
overridevirtual

Serialize the message to a destination byte array. The destination array size is set by the function.

Parameters
destDestination buffer.

Reimplemented from bus::IBusMessage.


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