Fawkes API  Fawkes Development Version
DirectRobotinoComMessage Class Reference

#include "direct_com_message.h"

Classes

class  ChecksumError
 

Public Types

typedef std::shared_ptr< DirectRobotinoComMessagepointer
 shared pointer to direct com message More...
 

Public Member Functions

 DirectRobotinoComMessage ()
 Constructor. More...
 
 DirectRobotinoComMessage (command_id_t cmdid)
 Constructor for initial command. More...
 
 DirectRobotinoComMessage (const unsigned char *msg, size_t msg_size)
 Constructor for incoming message. More...
 
 DirectRobotinoComMessage (const DirectRobotinoComMessage &other)
 Copy Constructor. More...
 
virtual ~DirectRobotinoComMessage ()
 Destructor. More...
 
DirectRobotinoComMessageoperator= (const DirectRobotinoComMessage &other)
 Assignment operator. More...
 
void add_command (command_id_t cmdid)
 Add a command header. More...
 
void add_int8 (int8_t value)
 Add 8-bit signed integer to current command. More...
 
void add_uint8 (uint8_t value)
 Add 8-bit unsigned integer to current command. More...
 
void add_int16 (int16_t value)
 Add 16-bit signed integer to current command. More...
 
void add_uint16 (uint16_t value)
 Add 16-bit unsigned integer to current command. More...
 
void add_int32 (int32_t value)
 Add 32-bit signed integer to current command. More...
 
void add_uint32 (uint32_t value)
 Add 32-bit unsigned integer to current command. More...
 
void add_float (float value)
 Add float to current command. More...
 
void rewind ()
 Rewind to read again from start. More...
 
command_id_t next_command ()
 Get next available command. More...
 
uint8_t command_length () const
 Get length of current command. More...
 
command_id_t command_id () const
 Get ID of current command. More...
 
int8_t get_int8 ()
 Get 8-bit signed integer from current command. More...
 
uint8_t get_uint8 ()
 Get 8-bit unsigned integer from current command. More...
 
int16_t get_int16 ()
 Get 16-bit signed integer from current command. More...
 
uint16_t get_uint16 ()
 Get 16-bit unsigned integer from current command. More...
 
int32_t get_int32 ()
 Get 32-bit signed integer from current command. More...
 
uint32_t get_uint32 ()
 Get 32-bit unsigned integer from current command. More...
 
float get_float ()
 Get float from current command. More...
 
std::string get_string ()
 Get string from current command. More...
 
void skip_int8 ()
 Skip 8-bit signed integer from current command. More...
 
void skip_uint8 ()
 Skip 8-bit unsigned integer from current command. More...
 
void skip_int16 ()
 Skip 16-bit signed integer from current command. More...
 
void skip_uint16 ()
 Skip 16-bit unsigned integer from current command. More...
 
void skip_int32 ()
 Skip 32-bit signed integer from current command. More...
 
void skip_uint32 ()
 Skip 32-bit unsigned integer from current command. More...
 
void skip_float ()
 Skip float from current command. More...
 
boost::asio::const_buffer buffer ()
 Get access to buffer for sending. More...
 
void pack ()
 Pack message. More...
 
uint16_t checksum () const
 Get checksum of message. More...
 
std::string to_string (bool escaped=false)
 Generate string representation of message. More...
 
size_t escaped_data_size ()
 Size of escaped buffer. More...
 
size_t payload_size ()
 Get payload size. More...
 
size_t data_size ()
 Get internal data buffer size. More...
 

Static Public Member Functions

static size_t unescape (unsigned char *unescaped, size_t unescaped_size, const unsigned char *escaped, size_t escaped_size)
 Unescape a number of unescaped bytes. More...
 
static uint16_t parse_uint16 (const unsigned char *buf)
 Parse 16-bit unsigned integer from given buffer. More...
 

Detailed Description

Robotino communication message.

This object is used to create messages to send and parse messages to read. It is designed to be generic, i.e., it provides methods to add messages and its fields and to iterate over commands and read fields. These methods must be called in proper sequence, no command type specific processing is performed within the class. This approach was chosen since we do not strive for a user-facing generic transport, but rather for a minimal method to interact with Robotino's microcontroller.

A message strictly differentiates between a reading and a writing mode, depending on the constructor with which it was created. The reading mode is used to parse received messages and provide access to its commands, the writing mode to construct messages to send. Furthermore, the message assumes quick run-throughs, i.e., after requesting the buffer of a writing message once, it is fixed and will not be re-generated after further additions.

Terminology is a mix in part due to the naming in OpenRobotino:

  • Message: our representation of a message to send
  • Command: a command field within a message, this is called tag and also command in OpenRobotino. We chose the latter.
Author
Tim Niemueller

Definition at line 29 of file direct_com_message.h.

Member Typedef Documentation

◆ pointer

shared pointer to direct com message

Definition at line 38 of file direct_com_message.h.

Constructor & Destructor Documentation

◆ DirectRobotinoComMessage() [1/4]

DirectRobotinoComMessage::DirectRobotinoComMessage ( )

Constructor.

Create empty message for writing.

Definition at line 93 of file direct_com_message.cpp.

◆ DirectRobotinoComMessage() [2/4]

DirectRobotinoComMessage::DirectRobotinoComMessage ( command_id_t  cmdid)

Constructor for initial command.

Create message for writing and add command for given message ID.

Parameters
cmdidmessage ID of command to add

Definition at line 125 of file direct_com_message.cpp.

References add_command().

◆ DirectRobotinoComMessage() [3/4]

DirectRobotinoComMessage::DirectRobotinoComMessage ( const unsigned char *  msg,
size_t  msg_size 
)

Constructor for incoming message.

Create message for reading from incoming buffer.

Parameters
msgthe message of msg_size is expected to be escaped and to range from the including 0xAA head byte to the checksum.
msg_sizesize of msg buffer

Definition at line 138 of file direct_com_message.cpp.

◆ DirectRobotinoComMessage() [4/4]

DirectRobotinoComMessage::DirectRobotinoComMessage ( const DirectRobotinoComMessage other)

Copy Constructor.

Parameters
otherinstance to copy from

Definition at line 101 of file direct_com_message.cpp.

◆ ~DirectRobotinoComMessage()

DirectRobotinoComMessage::~DirectRobotinoComMessage ( )
virtual

Destructor.

Definition at line 180 of file direct_com_message.cpp.

Member Function Documentation

◆ add_command()

void DirectRobotinoComMessage::add_command ( command_id_t  cmdid)

Add a command header.

This only allocates the header. You must call the appropriate methods to add the required data fields afterwards or the message will be rejected/ignored by the Robotino.

Parameters
cmdidcommand ID to add.

Definition at line 300 of file direct_com_message.cpp.

Referenced by DirectRobotinoComMessage(), DirectRobotinoComThread::set_motor_accel_limits(), and DirectRobotinoComThread::set_speed_points().

◆ add_float()

void DirectRobotinoComMessage::add_float ( float  value)

Add float to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 389 of file direct_com_message.cpp.

Referenced by DirectRobotinoComThread::reset_odometry(), and DirectRobotinoComThread::set_motor_accel_limits().

◆ add_int16()

void DirectRobotinoComMessage::add_int16 ( int16_t  value)

Add 16-bit signed integer to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 337 of file direct_com_message.cpp.

◆ add_int32()

void DirectRobotinoComMessage::add_int32 ( int32_t  value)

Add 32-bit signed integer to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 361 of file direct_com_message.cpp.

◆ add_int8()

void DirectRobotinoComMessage::add_int8 ( int8_t  value)

Add 8-bit signed integer to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 315 of file direct_com_message.cpp.

◆ add_uint16()

void DirectRobotinoComMessage::add_uint16 ( uint16_t  value)

Add 16-bit unsigned integer to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 349 of file direct_com_message.cpp.

Referenced by DirectRobotinoComThread::set_speed_points().

◆ add_uint32()

void DirectRobotinoComMessage::add_uint32 ( uint32_t  value)

Add 32-bit unsigned integer to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 375 of file direct_com_message.cpp.

◆ add_uint8()

void DirectRobotinoComMessage::add_uint8 ( uint8_t  value)

Add 8-bit unsigned integer to current command.

Parameters
valuevalue to add
Exceptions
Exceptionthrown if no command has been added, yet

Definition at line 326 of file direct_com_message.cpp.

Referenced by DirectRobotinoComThread::init(), DirectRobotinoComThread::set_bumper_estop_enabled(), DirectRobotinoComThread::set_digital_output(), DirectRobotinoComThread::set_motor_accel_limits(), and DirectRobotinoComThread::set_speed_points().

◆ buffer()

boost::asio::const_buffer DirectRobotinoComMessage::buffer ( )

Get access to buffer for sending.

This implies packing. Note that after calling this methods later modifications to the message will be ignored. The buffer is invalidated on the destruction of the message.

Returns
buffer of escaped data.

Definition at line 874 of file direct_com_message.cpp.

References pack().

Referenced by to_string().

◆ checksum()

uint16_t DirectRobotinoComMessage::checksum ( ) const

Get checksum of message.

Returns
checksum

Definition at line 900 of file direct_com_message.cpp.

Referenced by pack().

◆ command_id()

DirectRobotinoComMessage::command_id_t DirectRobotinoComMessage::command_id ( ) const

Get ID of current command.

Returns
command ID

Definition at line 449 of file direct_com_message.cpp.

◆ command_length()

uint8_t DirectRobotinoComMessage::command_length ( ) const

Get length of current command.

Returns
length in bytes

Definition at line 438 of file direct_com_message.cpp.

◆ data_size()

size_t DirectRobotinoComMessage::data_size ( )

Get internal data buffer size.

Returns
data buffer size

Definition at line 751 of file direct_com_message.cpp.

◆ escaped_data_size()

size_t DirectRobotinoComMessage::escaped_data_size ( )

Size of escaped buffer.

In particular, after calling the parsing ctor this denotes how many bytes of the input buffer have been consumed. On message creation, only provides meaningful values after calling pack() or buffer().

Returns
size of escaped buffer

Definition at line 733 of file direct_com_message.cpp.

◆ get_float()

float DirectRobotinoComMessage::get_float ( )

Get float from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 593 of file direct_com_message.cpp.

◆ get_int16()

int16_t DirectRobotinoComMessage::get_int16 ( )

Get 16-bit signed integer from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 496 of file direct_com_message.cpp.

◆ get_int32()

int32_t DirectRobotinoComMessage::get_int32 ( )

Get 32-bit signed integer from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 547 of file direct_com_message.cpp.

◆ get_int8()

int8_t DirectRobotinoComMessage::get_int8 ( )

Get 8-bit signed integer from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 462 of file direct_com_message.cpp.

◆ get_string()

std::string DirectRobotinoComMessage::get_string ( )

Get string from current command.

This consumes all remaining data in the current command.

Returns
value
Exceptions
Exceptionthrown if no data remains to be read

Definition at line 615 of file direct_com_message.cpp.

◆ get_uint16()

uint16_t DirectRobotinoComMessage::get_uint16 ( )

Get 16-bit unsigned integer from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 514 of file direct_com_message.cpp.

◆ get_uint32()

uint32_t DirectRobotinoComMessage::get_uint32 ( )

Get 32-bit unsigned integer from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 570 of file direct_com_message.cpp.

◆ get_uint8()

uint8_t DirectRobotinoComMessage::get_uint8 ( )

Get 8-bit unsigned integer from current command.

This also forwards the command-internal pointer appropriately.

Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 479 of file direct_com_message.cpp.

◆ next_command()

DirectRobotinoComMessage::command_id_t DirectRobotinoComMessage::next_command ( )

Get next available command.

Returns
ID of next command, or CMDID_NONE if no more commands available

Definition at line 414 of file direct_com_message.cpp.

◆ operator=()

DirectRobotinoComMessage & DirectRobotinoComMessage::operator= ( const DirectRobotinoComMessage other)

Assignment operator.

Parameters
otherinstance to copy from
Returns
reference to this instance

Definition at line 194 of file direct_com_message.cpp.

◆ pack()

void DirectRobotinoComMessage::pack ( )

Pack message.

This escapes the data to be sent.

Definition at line 884 of file direct_com_message.cpp.

References checksum().

Referenced by buffer().

◆ parse_uint16()

uint16_t DirectRobotinoComMessage::parse_uint16 ( const unsigned char *  buf)
static

Parse 16-bit unsigned integer from given buffer.

Parameters
bufbuffer at least of size 2 bytes
Returns
value
Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 533 of file direct_com_message.cpp.

◆ payload_size()

size_t DirectRobotinoComMessage::payload_size ( )

Get payload size.

Returns
payload size

Definition at line 742 of file direct_com_message.cpp.

◆ rewind()

void DirectRobotinoComMessage::rewind ( )

Rewind to read again from start.

Exceptions
Exceptionthrown if no not a reading message

Definition at line 403 of file direct_com_message.cpp.

◆ skip_float()

void DirectRobotinoComMessage::skip_float ( )

Skip float from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 716 of file direct_com_message.cpp.

◆ skip_int16()

void DirectRobotinoComMessage::skip_int16 ( )

Skip 16-bit signed integer from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 660 of file direct_com_message.cpp.

◆ skip_int32()

void DirectRobotinoComMessage::skip_int32 ( )

Skip 32-bit signed integer from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 688 of file direct_com_message.cpp.

◆ skip_int8()

void DirectRobotinoComMessage::skip_int8 ( )

Skip 8-bit signed integer from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 632 of file direct_com_message.cpp.

◆ skip_uint16()

void DirectRobotinoComMessage::skip_uint16 ( )

Skip 16-bit unsigned integer from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 674 of file direct_com_message.cpp.

◆ skip_uint32()

void DirectRobotinoComMessage::skip_uint32 ( )

Skip 32-bit unsigned integer from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 702 of file direct_com_message.cpp.

◆ skip_uint8()

void DirectRobotinoComMessage::skip_uint8 ( )

Skip 8-bit unsigned integer from current command.

This also forwards the command-internal pointer appropriately.

Exceptions
Exceptionthrown if not enough data remains to be read

Definition at line 646 of file direct_com_message.cpp.

◆ to_string()

std::string DirectRobotinoComMessage::to_string ( bool  escaped = false)

Generate string representation of message.

Parameters
escapedtrue to serialize escaped buffer, false to use unescaped buffer
Returns
string representation of message in hexadecimal view with parantheses to indicate command boundaries.

Definition at line 935 of file direct_com_message.cpp.

References buffer().

◆ unescape()

size_t DirectRobotinoComMessage::unescape ( unsigned char *  unescaped,
size_t  unescaped_size,
const unsigned char *  escaped,
size_t  escaped_size 
)
static

Unescape a number of unescaped bytes.

Parameters
unescapedbuffer to contain the unescaped data on return, must be at least of unescaped_size number of bytes
unescaped_sizeexpected number of bytes to unescape from input buffer
escapedescaped buffer to process
escaped_sizesize of escaped_buffer
Returns
number of bytes consumed from escaped buffer
Exceptions
Exceptionif not enough bytes could be unescaped

Definition at line 799 of file direct_com_message.cpp.


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