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... | |
virtual | ~DirectRobotinoComMessage () |
Destructor. 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 86 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 96 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 109 of file direct_com_message.cpp.
|
virtual |
Destructor.
Definition at line 146 of file direct_com_message.cpp.
References fawkes::Exception::Exception().
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 222 of file direct_com_message.cpp.
Referenced by DirectRobotinoComMessage(), DirectRobotinoComThread::set_bumper_estop_enabled(), 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 311 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 259 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 283 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 237 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 271 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 297 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 248 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 784 of file direct_com_message.cpp.
References pack().
Referenced by DirectRobotinoComThread::set_bumper_estop_enabled(), and to_string().
uint16_t DirectRobotinoComMessage::checksum | ( | ) | const |
Get checksum of message.
Definition at line 810 of file direct_com_message.cpp.
References DirectRobotinoComMessage::ChecksumError::ChecksumError(), and parse_uint16().
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 359 of file direct_com_message.cpp.
size_t DirectRobotinoComMessage::data_size | ( | ) |
Get internal data buffer size.
Definition at line 676 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 657 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 515 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 418 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 469 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 384 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 537 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 436 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 492 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 401 of file direct_com_message.cpp.
DirectRobotinoComMessage::command_id_t DirectRobotinoComMessage::next_command | ( | ) |
Get next available command.
Definition at line 336 of file direct_com_message.cpp.
void DirectRobotinoComMessage::pack | ( | ) |
Pack message.
This escapes the data to be sent.
Definition at line 794 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 455 of file direct_com_message.cpp.
Referenced by checksum(), DirectRobotinoComThread::set_bumper_estop_enabled(), and unescape().
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 325 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 639 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 583 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 611 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 555 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 597 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 625 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 569 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 843 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 723 of file direct_com_message.cpp.
References fawkes::Exception::Exception(), and parse_uint16().
Referenced by DirectRobotinoComThread::set_bumper_estop_enabled().