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

Abstract base class representing a generic bus message. More...

#include "bus/ibusmessage.h"

Inheritance diagram for bus::IBusMessage:

Public Member Functions

 IBusMessage (BusMessageType type)
virtual void ToRaw (std::vector< uint8_t > &dest) const
 Serialize the message.
virtual void FromRaw (const std::vector< uint8_t > &source)
 Deserialize the message.
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 std::shared_ptr< IBusMessageCreate (BusMessageType type)
 Creates a message by its type.

Protected Member Functions

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

This abstract base class is used to implement generic bus messages. A message must have a specific type and a version. The type and version, defines how the message is serialized. The serialization are done with the ToRaw() and FromRaw() functions. These functions are abstract and have to be implemented by the derived class.

The serialization of a bus message have a header which has the same length. The header is according to table below and uses little endian byte order.

Bus Message Header
Byte (Bits) OffsetDescriptionSize
0Type of Message (enumerate)uint16_t
2Version Numberuint16_t
4Length of the messageuint32_t
8Timestamp ns since 1970uint64_t
16Bus Channeluint16_t

Constructor & Destructor Documentation

◆ IBusMessage()

bus::IBusMessage::IBusMessage ( BusMessageType type)
explicit

Contructor that creates a type of message.

This constructor should not be used by end-user but it is used for testing purpose.

Parameters
typeType of message

Member Function Documentation

◆ BusChannel() [1/2]

uint16_t bus::IBusMessage::BusChannel ( ) const
inlinenodiscard

The bus channel is a number that defines the source of the message. The source is typical a physical driver channel and not defining any ECU device.

Note that many manufactures of CAN communication cards, name the first channel as 'Channel 1' but uses channel 0 in its software. This causes a lot of problems and it doesn't help that the channel number randomly changes between computer restarts. It is recommended to start the channel number at 1 and assign the first channel, the 'Channel 1' name. The channel name should be assoiated with the device driver serial number instead of its internal channel number.

Returns
Bus channel number.

◆ BusChannel() [2/2]

void bus::IBusMessage::BusChannel ( uint16_t channel)
inline

The bus channel is a number that defines the source of the message. The source is typical a physical driver channel and not defining any ECU device.

Note that many manufactures of CAN communication card, name the first channel as 'Channel 1' but uses channel 0 in its software. This causes a lot of problems and it doesn't help that the channel number randomly changes between computer restarts. It is recommended to start the channel number at 1 and assign the first channel, the 'Channel 1' name. The channel name should be assoiated with the device driver serial number instead of its internal channel number.

Parameters
channelBus channel number.

◆ Create()

std::shared_ptr< IBusMessage > bus::IBusMessage::Create ( BusMessageType type)
static

Creates a message by its type. This fucntion is used by subscriber when deserialize a message.

Parameters
typeType of message.
Returns
Smart pointer to IBussMessage object.

◆ FromRaw()

virtual void bus::IBusMessage::FromRaw ( const std::vector< uint8_t > & source)
virtual

This function desrialize a message from an byte array.

Parameters
sourceSource buffer.

Reimplemented in bus::CanDataFrame.

◆ Size() [1/2]

uint32_t bus::IBusMessage::Size ( ) const
inlinenodiscard

The function return number of total message bytes, not the paylod data size.

Returns
Number of total message bytes.

◆ Size() [2/2]

void bus::IBusMessage::Size ( uint32_t size) const
inlineprotected

This function set the messsage size when serialize and deserialize the message.

Parameters
sizeMessage size (butes).

◆ Timestamp() [1/2]

uint64_t bus::IBusMessage::Timestamp ( ) const
inlinenodiscard

The timestamp is defined as nanoseconds since midnight 1970-01-01. The timezone is always UTC.

Returns
Time since 1970.

◆ Timestamp() [2/2]

void bus::IBusMessage::Timestamp ( uint64_t timestamp)
inline

Sets the timestamp for the message. The time is number of nanoseconds from midnight 1970-01-01. The timezone is always UTC.

Parameters
timestampNanoseconds sinncs 1970.

◆ ToRaw()

virtual void bus::IBusMessage::ToRaw ( std::vector< uint8_t > & dest) const
virtual

The function serialize a message to a byte array. This array is later used by the communication interfaces for transfer the message in shared ememroy or TCP/IP. The destination array is sized by the function.

Parameters
destDestination buffer.

Reimplemented in bus::CanDataFrame.

◆ Type()

BusMessageType bus::IBusMessage::Type ( ) const
inlinenodiscard
Returns
Type of message.

◆ Valid() [1/2]

bool bus::IBusMessage::Valid ( ) const
inlinenodiscard

The message can be set invalid if the serialization or deserialization fails. olle

Returns
True if the message is valid.

◆ Valid() [2/2]

void bus::IBusMessage::Valid ( bool valid) const
inlineprotected

Sets the message valid or invalid. By default the message is set valid (true).

Parameters
validValid is true while invalid is false.

◆ Version() [1/2]

uint16_t bus::IBusMessage::Version ( ) const
inlinenodiscard

Rarely used function but used by the serialization and deserialization functions.

Returns
Version number.

◆ Version() [2/2]

void bus::IBusMessage::Version ( uint16_t version)
inline

This function should rarely be used but defines the version of the message. This is used by the serialization and deserialization functions. The version is changed when properties are added.

Parameters
versionVersion number.

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