Fawkes API Fawkes Development Version
|
Robotino communication message. More...
#include "direct_com_message.h"
Classes | |
class | ChecksumError |
Excpetion thrown on checksum errors. More... | |
Public Types | |
typedef std::shared_ptr< DirectRobotinoComMessage > | pointer |
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... | |
DirectRobotinoComMessage & | operator= (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... | |
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:
Definition at line 29 of file direct_com_message.h.
typedef std::shared_ptr<DirectRobotinoComMessage> DirectRobotinoComMessage::pointer |
shared pointer to direct com message
Definition at line 33 of file direct_com_message.h.
DirectRobotinoComMessage::DirectRobotinoComMessage | ( | ) |
Constructor.
Create empty message for writing.
Definition at line 93 of file direct_com_message.cpp.
DirectRobotinoComMessage::DirectRobotinoComMessage | ( | command_id_t | cmdid | ) |
Constructor for initial command.
Create message for writing and add command for given message ID.
cmdid | message ID of command to add |
Definition at line 125 of file direct_com_message.cpp.
References add_command().
DirectRobotinoComMessage::DirectRobotinoComMessage | ( | const unsigned char * | msg, |
size_t | msg_size | ||
) |
Constructor for incoming message.
Create message for reading from incoming buffer.
msg | the message of msg_size is expected to be escaped and to range from the including 0xAA head byte to the checksum. |
msg_size | size of msg buffer |
Definition at line 138 of file direct_com_message.cpp.
DirectRobotinoComMessage::DirectRobotinoComMessage | ( | const DirectRobotinoComMessage & | other | ) |
Copy Constructor.
other | instance to copy from |
Definition at line 101 of file direct_com_message.cpp.
|
virtual |
Destructor.
Definition at line 180 of file direct_com_message.cpp.
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.
cmdid | command 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().
void DirectRobotinoComMessage::add_float | ( | float | value | ) |
Add float to current command.
value | value to add |
Exception | thrown 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().
void DirectRobotinoComMessage::add_int16 | ( | int16_t | value | ) |
Add 16-bit signed integer to current command.
value | value to add |
Exception | thrown if no command has been added, yet |
Definition at line 337 of file direct_com_message.cpp.
void DirectRobotinoComMessage::add_int32 | ( | int32_t | value | ) |
Add 32-bit signed integer to current command.
value | value to add |
Exception | thrown if no command has been added, yet |
Definition at line 361 of file direct_com_message.cpp.
void DirectRobotinoComMessage::add_int8 | ( | int8_t | value | ) |
Add 8-bit signed integer to current command.
value | value to add |
Exception | thrown if no command has been added, yet |
Definition at line 315 of file direct_com_message.cpp.
void DirectRobotinoComMessage::add_uint16 | ( | uint16_t | value | ) |
Add 16-bit unsigned integer to current command.
value | value to add |
Exception | thrown if no command has been added, yet |
Definition at line 349 of file direct_com_message.cpp.
Referenced by DirectRobotinoComThread::set_speed_points().
void DirectRobotinoComMessage::add_uint32 | ( | uint32_t | value | ) |
Add 32-bit unsigned integer to current command.
value | value to add |
Exception | thrown if no command has been added, yet |
Definition at line 375 of file direct_com_message.cpp.
void DirectRobotinoComMessage::add_uint8 | ( | uint8_t | value | ) |
Add 8-bit unsigned integer to current command.
value | value to add |
Exception | thrown 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().
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.
Definition at line 874 of file direct_com_message.cpp.
References pack().
Referenced by to_string().
uint16_t DirectRobotinoComMessage::checksum | ( | ) | const |
Get checksum of message.
Definition at line 900 of file direct_com_message.cpp.
Referenced by pack().
DirectRobotinoComMessage::command_id_t DirectRobotinoComMessage::command_id | ( | ) | const |
uint8_t DirectRobotinoComMessage::command_length | ( | ) | const |
Get length of current command.
Definition at line 438 of file direct_com_message.cpp.
size_t DirectRobotinoComMessage::data_size | ( | ) |
Get internal data buffer size.
Definition at line 751 of file direct_com_message.cpp.
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().
Definition at line 733 of file direct_com_message.cpp.
float DirectRobotinoComMessage::get_float | ( | ) |
Get float from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 593 of file direct_com_message.cpp.
int16_t DirectRobotinoComMessage::get_int16 | ( | ) |
Get 16-bit signed integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 496 of file direct_com_message.cpp.
int32_t DirectRobotinoComMessage::get_int32 | ( | ) |
Get 32-bit signed integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 547 of file direct_com_message.cpp.
int8_t DirectRobotinoComMessage::get_int8 | ( | ) |
Get 8-bit signed integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 462 of file direct_com_message.cpp.
std::string DirectRobotinoComMessage::get_string | ( | ) |
Get string from current command.
This consumes all remaining data in the current command.
Exception | thrown if no data remains to be read |
Definition at line 615 of file direct_com_message.cpp.
uint16_t DirectRobotinoComMessage::get_uint16 | ( | ) |
Get 16-bit unsigned integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 514 of file direct_com_message.cpp.
uint32_t DirectRobotinoComMessage::get_uint32 | ( | ) |
Get 32-bit unsigned integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 570 of file direct_com_message.cpp.
uint8_t DirectRobotinoComMessage::get_uint8 | ( | ) |
Get 8-bit unsigned integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 479 of file direct_com_message.cpp.
DirectRobotinoComMessage::command_id_t DirectRobotinoComMessage::next_command | ( | ) |
Get next available command.
Definition at line 414 of file direct_com_message.cpp.
DirectRobotinoComMessage & DirectRobotinoComMessage::operator= | ( | const DirectRobotinoComMessage & | other | ) |
Assignment operator.
other | instance to copy from |
Definition at line 194 of file direct_com_message.cpp.
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().
|
static |
Parse 16-bit unsigned integer from given buffer.
buf | buffer at least of size 2 bytes |
Exception | thrown if not enough data remains to be read |
Definition at line 533 of file direct_com_message.cpp.
size_t DirectRobotinoComMessage::payload_size | ( | ) |
void DirectRobotinoComMessage::rewind | ( | ) |
Rewind to read again from start.
Exception | thrown if no not a reading message |
Definition at line 403 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_float | ( | ) |
Skip float from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 716 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_int16 | ( | ) |
Skip 16-bit signed integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 660 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_int32 | ( | ) |
Skip 32-bit signed integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 688 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_int8 | ( | ) |
Skip 8-bit signed integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 632 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_uint16 | ( | ) |
Skip 16-bit unsigned integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 674 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_uint32 | ( | ) |
Skip 32-bit unsigned integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 702 of file direct_com_message.cpp.
void DirectRobotinoComMessage::skip_uint8 | ( | ) |
Skip 8-bit unsigned integer from current command.
This also forwards the command-internal pointer appropriately.
Exception | thrown if not enough data remains to be read |
Definition at line 646 of file direct_com_message.cpp.
std::string DirectRobotinoComMessage::to_string | ( | bool | escaped = false | ) |
Generate string representation of message.
escaped | true to serialize escaped buffer, false to use unescaped buffer |
Definition at line 935 of file direct_com_message.cpp.
References buffer().
|
static |
Unescape a number of unescaped bytes.
unescaped | buffer to contain the unescaped data on return, must be at least of unescaped_size number of bytes |
unescaped_size | expected number of bytes to unescape from input buffer |
escaped | escaped buffer to process |
escaped_size | size of escaped_buffer |
Exception | if not enough bytes could be unescaped |
Definition at line 799 of file direct_com_message.cpp.