Aria  2.8.0
ArBasePacket Class Reference

Base packet class. More...

#include <ArBasePacket.h>

Inherited by ArAMPTUPacket, ArDPPTUPacket, ArLMS1XXPacket, ArLMS2xxPacket, ArRobotPacket, ArRVisionPacket, ArS3SeriesPacket, ArSonyPacket, ArSZSeriesPacket, and ArVCC4Packet.

Public Member Functions

 ArBasePacket (ArTypes::UByte2 bufferSize=0, ArTypes::UByte2 headerLength=0, char *buf=NULL, ArTypes::UByte2 footerLength=0)
 Constructor. More...
 
 ArBasePacket (const ArBasePacket &other)
 Copy constructor.
 
virtual ArTypes::Byte bufToByte (void)
 Gets a ArTypes::Byte from the buffer.
 
virtual ArTypes::Byte2 bufToByte2 (void)
 Gets a ArTypes::Byte2 from the buffer.
 
virtual ArTypes::Byte4 bufToByte4 (void)
 Gets a ArTypes::Byte4 from the buffer.
 
virtual void bufToData (char *data, int length)
 Gets length bytes from buffer and puts them into data. More...
 
virtual void bufToData (unsigned char *data, int length)
 Gets length bytes from buffer and puts them into data. More...
 
virtual void bufToStr (char *buf, int len)
 Gets a string from the buffer. More...
 
virtual ArTypes::UByte bufToUByte (void)
 Gets a ArTypes::UByte from the buffer.
 
virtual ArTypes::UByte2 bufToUByte2 (void)
 Gets a ArTypes::UByte2 from the buffer.
 
virtual ArTypes::UByte4 bufToUByte4 (void)
 Gets a ArTypes::UByte4 from the buffer.
 
virtual void byte2ToBuf (ArTypes::Byte2 val)
 Puts ArTypes::Byte2 into packets buffer.
 
virtual void byte4ToBuf (ArTypes::Byte4 val)
 Puts ArTypes::Byte4 into packets buffer.
 
virtual void byteToBuf (ArTypes::Byte val)
 Puts ArTypes::Byte into packets buffer.
 
virtual void dataToBuf (const char *data, int length)
 Copies length bytes from data into packet buffer. More...
 
virtual void dataToBuf (const unsigned char *data, int length)
 Copies length bytes from data into packet buffer. More...
 
virtual void duplicatePacket (ArBasePacket *packet)
 Makes this packet a duplicate of another packet. More...
 
virtual void empty (void)
 resets the length for more data to be added More...
 
virtual void finalizePacket (void)
 MakeFinals the packet in preparation for sending, must be done.
 
virtual const char * getBuf (void) const
 Gets a const pointer to the buffer the packet uses.
 
virtual char * getBuf (void)
 Gets a pointer to the buffer the packet uses.
 
virtual ArTypes::UByte2 getDataLength (void) const
 Gets the length of the data in the packet.
 
virtual ArTypes::UByte2 getDataReadLength (void) const
 Gets how far into the data of the packet that has been read.
 
virtual ArTypes::UByte2 getFooterLength (void) const
 Gets the length of the header.
 
virtual ArTypes::UByte2 getHeaderLength (void) const
 Gets the length of the header.
 
virtual ArTypes::UByte2 getLength (void) const
 Gets the total length of the packet.
 
virtual ArTypes::UByte2 getMaxLength (void) const
 Gets the maximum length packet.
 
virtual ArTypes::UByte2 getReadLength (void) const
 Gets how far into the packet that has been read.
 
virtual bool isValid (void)
 Returns whether the packet is valid, i.e. no error has occurred when reading/writing. More...
 
virtual void log (void)
 ArLogs the hex and decimal values of each byte of the packet, and possibly extra metadata as well.
 
ArBasePacketoperator= (const ArBasePacket &other)
 Assignment operator.
 
virtual void printHex (void)
 ArLogs the hex value of each byte in the packet.
 
virtual void resetRead (void)
 Restart the reading process. More...
 
virtual void resetValid ()
 Resets the valid state of the packet. More...
 
virtual void setBuf (char *buf, ArTypes::UByte2 bufferSize)
 Sets the buffer the packet is using.
 
virtual bool setHeaderLength (ArTypes::UByte2 length)
 Sets the length of the header.
 
virtual bool setLength (ArTypes::UByte2 length)
 Sets the length of the packet.
 
virtual void setMaxLength (ArTypes::UByte2 bufferSize)
 Sets the maximum buffer size (if new size is <= current does nothing)
 
virtual void setReadLength (ArTypes::UByte2 readLength)
 Sets the read length.
 
virtual void strNToBuf (const char *str, int length)
 Copies the given number of bytes from str into packet buffer. More...
 
virtual void strToBuf (const char *str)
 Puts a NULL-terminated string into packet buffer. More...
 
virtual void strToBufPadded (const char *str, int length)
 Copies length bytes from str, if str ends before length, pads data with 0s. More...
 
virtual void uByte2ToBuf (ArTypes::UByte2 val)
 Puts ArTypes::UByte2 into packet buffer.
 
virtual void uByte4ToBuf (ArTypes::UByte4 val)
 Puts ArTypes::UByte 4 into packet buffer.
 
virtual void uByteToBuf (ArTypes::UByte val)
 Puts ArTypes::UByte into packets buffer.
 
virtual ~ArBasePacket ()
 Destructor.
 

Protected Member Functions

bool hasWriteCapacity (int bytes)
 Returns true if there is enough room in the packet to add the specified number of bytes.
 
bool isNextGood (int bytes)
 

Protected Attributes

char * myBuf
 
ArTypes::UByte2 myFooterLength
 
ArTypes::UByte2 myHeaderLength
 
bool myIsValid
 
ArTypes::UByte2 myLength
 
ArTypes::UByte2 myMaxLength
 
bool myOwnMyBuf
 
ArTypes::UByte2 myReadLength
 

Detailed Description

Base packet class.

This class is a base class for specific packet types implemented by base classes. In most cases, you would not instantiate this class directly, but instead use a subclass. However, ArBasePacket contains many of the functions used to access the packet's data.

A packet is a sequence of values stored in a buffer. The contents of a packet's data buffer is read from a device or other program or written to the device (for example, a serial port or TCP port using an ArDeviceConnection or using ArNetworking), optionally preceded by a header with some identifying data and a length, and optionally followed by a footer with a checksum of the data. (If the header length of a particular packet type is 0, no header is written or expected on read, and likewise with footer.)

Values are added to the buffer or removed from the buffer in sequence. The "bufTo" methods are used to remove values from the buffer, and the "ToBuf" methods are used to add values to the buffer. There are different methods for different sized values.

ArBasePacket keeps a current position index in the buffer, which is the position at which new values are added or values are removed.

A buffer may be statically allocated externally and supplied to the constructor (also give a buffer size to determine the maximum amount of data that can be placed in that bufer), or automatically and dynamically allocated by ArBasePacket as needed (the default behavior).

When it is time to write out a packet, call finalizePacket() to set up the footer if neccesary. To reuse a packet, use empty() to reset the buffer; new data will then be added to the beginning of the buffer again.

Constructor & Destructor Documentation

◆ ArBasePacket()

ArBasePacket::ArBasePacket ( ArTypes::UByte2  bufferSize = 0,
ArTypes::UByte2  headerLength = 0,
char *  buf = NULL,
ArTypes::UByte2  footerLength = 0 
)

Constructor.

Parameters
bufferSizesize of the buffer
headerLengthlength of the header
bufbuffer packet uses, if NULL, instance will allocate memory
footerLengthlength of the footer following the data

Member Function Documentation

◆ bufToData() [1/2]

void ArBasePacket::bufToData ( char *  data,
int  length 
)
virtual

Gets length bytes from buffer and puts them into data.

copies length bytes from the buffer into data, length is passed in, not read from packet

Parameters
datacharacter array to copy the data into
lengthnumber of bytes to copy into data

◆ bufToData() [2/2]

void ArBasePacket::bufToData ( unsigned char *  data,
int  length 
)
virtual

Gets length bytes from buffer and puts them into data.

This was added to get around having to cast data you put in, since the data shouldn't really matter if its signed or unsigned.

copies length bytes from the buffer into data, length is passed in, not read from packet

Parameters
datacharacter array to copy the data into
lengthnumber of bytes to copy into data

◆ bufToStr()

void ArBasePacket::bufToStr ( char *  buf,
int  len 
)
virtual

Gets a string from the buffer.

Copy a string from the packet buffer into the given buffer, stopping when the end of the packet buffer is reached, the given length is reached, or a NUL character ('\0') is reached.

If the given length is not large enough, then the remainder of the string is flushed from the packet. A NUL character ('\0') is appended to buf if there is sufficient room after copying the sting from the packet, otherwise no NUL is added (i.e. if len bytes are copied).

Parameters
bufDestination buffer
lenMaximum number of characters to copy into the destination buffer

◆ dataToBuf() [1/2]

void ArBasePacket::dataToBuf ( const char *  data,
int  length 
)
virtual

Copies length bytes from data into packet buffer.

Parameters
datachacter array to copy into buffer
lengthhow many bytes to copy from data into packet

◆ dataToBuf() [2/2]

void ArBasePacket::dataToBuf ( const unsigned char *  data,
int  length 
)
virtual

Copies length bytes from data into packet buffer.

This was added to get around having to cast data you put in, since the data shouldn't really matter if its signed or unsigned.

Parameters
datachacter array to copy into buffer
lengthhow many bytes to copy from data into packet

◆ duplicatePacket()

void ArBasePacket::duplicatePacket ( ArBasePacket packet)
virtual

Makes this packet a duplicate of another packet.

Copies the given packets buffer into the buffer of this packet, also sets this length and readlength to what the given packet has.

Parameters
packetthe packet to duplicate

◆ empty()

void ArBasePacket::empty ( void  )
virtual

resets the length for more data to be added

Sets the packet length back to be the packets header length again.

◆ isValid()

bool ArBasePacket::isValid ( void  )
virtual

Returns whether the packet is valid, i.e. no error has occurred when reading/writing.

A packet is considered "invalid" if an attempt is made to write too much data into the packet, or to read too much data from the packet.

Calls to empty() and resetRead() will restore the valid state.

◆ resetRead()

void ArBasePacket::resetRead ( void  )
virtual

Restart the reading process.

Sets the length read back to the header length so the packet can be reread using the other methods.

◆ resetValid()

void ArBasePacket::resetValid ( )
virtual

Resets the valid state of the packet.

Resets the packet to the "valid" state.

This method should generally only be called externally when the application has taken some recovery action. For example, if an attempt to write a long string to the packet fails (and isValid() returns false), then a smaller string may be written instead.

◆ strNToBuf()

void ArBasePacket::strNToBuf ( const char *  str,
int  length 
)
virtual

Copies the given number of bytes from str into packet buffer.

This method performs no bounds checking on the given length and the contents of the string.

Deprecated:
use strToBufPadded(), strToBuf(), or dataToBuf() instead

For string operations, strNToBufPadded() is preferred. For raw data operations, dataToBuf() is preferred.

Parameters
strcharacter array to copy into the packet buffer
lengthhow many characters to copy from str into the packet buffer

◆ strToBuf()

void ArBasePacket::strToBuf ( const char *  str)
virtual

Puts a NULL-terminated string into packet buffer.

Parameters
strstring to copy into buffer

◆ strToBufPadded()

void ArBasePacket::strToBufPadded ( const char *  str,
int  length 
)
virtual

Copies length bytes from str, if str ends before length, pads data with 0s.

If string ends before length it pads the string with NUL ('\0') characters.

Parameters
strcharacter array to copy into buffer
lengthhow many bytes to copy from the str into packet

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