AssetTrackerRK
Public Member Functions | Protected Types | Protected Attributes | Static Protected Attributes
UbloxCommandBase Class Reference

Class for reading or writing a u-blox command. More...

#include <UbloxGPS.h>

Inheritance diagram for UbloxCommandBase:
UbloxCommand< PAYLOAD_SIZE > UbloxCommand< 100 > Ublox

Public Member Functions

 UbloxCommandBase (uint8_t *buffer, size_t bufferSize)
 Construct a UbloxCommandBase object. Normally you'd use UbloxCommand instead, which handles the buffer management for you. More...
 
virtual ~UbloxCommandBase ()
 Destructor.
 
bool decode (char ch)
 Decode a single character. More...
 
void discardToNextSync1 ()
 Used internally to discard invalid data. You probably won't need to call this.
 
void updateChecksum ()
 When preparing a command to send, updates the checksum, sync, and length bytes. More...
 
void calculateChecksum (uint8_t &ckA, uint8_t &ckB) const
 Calculates the checksum based on the data in the buffer. More...
 
void addHandler (UbloxMessageHandler *handler)
 Add a message handler. More...
 
void removeHandler (UbloxMessageHandler *handler)
 Remove a message handler. More...
 
void callHandlers ()
 Used internally to call all of the message handlers that match this class and id.
 
uint8_t getMsgClass () const
 Gets the message class of the current command. More...
 
uint8_t getMsgId () const
 Gets the message ID of the current command. More...
 
void setClassId (uint8_t msgClass, uint8_t msgId)
 When sending a message, sets the message class and ID. More...
 
size_t getPayloadLen () const
 Gets the payload length, the number of bytes available from getData()
 
const uint8_t * getData () const
 Get a const pointer to the data payload. More...
 
bool getData (size_t offset, void *data, size_t dataLen) const
 Copies data from the buffer. More...
 
bool setData (size_t offset, const void *data, size_t dataLen)
 Set data in the buffer. More...
 
bool appendData (const void *data, size_t dataLen)
 Appends data into the buffer at the current payloadLen. More...
 
bool fillData (uint8_t value, size_t dataLen)
 Works like appendData(), but fill with a value instead. More...
 
uint8_t getU1 (size_t offset) const
 Get a uint8_t value at offset. More...
 
bool setU1 (size_t offset, uint8_t value)
 Set a uint8_t value at offset in the payload. More...
 
bool appendU1 (uint8_t value)
 Append a uint8_t value to the current end of the payload. More...
 
int8_t getI1 (size_t offset) const
 Get a int8_t value (signed) at offset. More...
 
bool setI1 (size_t offset, int8_t value)
 Set a int8_t value at offset in the payload. More...
 
bool appendI1 (int8_t value)
 Append a int8_t value to the current end of the payload. More...
 
uint16_t getU2 (size_t offset) const
 Get a uint16_t value (little endian) at offset. More...
 
bool setU2 (size_t offset, uint16_t value)
 Set a uint16_t value at offset in the payload. More...
 
bool appendU2 (uint16_t value)
 Append a uint16_t value to the current end of the payload. More...
 
int16_t getI2 (size_t offset) const
 Get a int16_t value (little endian, signed) at offset. More...
 
bool setI2 (size_t offset, int16_t value)
 Set an int16_t value at offset in the payload. More...
 
bool appendI2 (int16_t value)
 Append an int16_t value to the current end of the payload. More...
 
uint32_t getU4 (size_t offset) const
 Get a uint32_t value (little endian, 4 bytes) at offset. More...
 
bool setU4 (size_t offset, uint32_t value)
 Set a uint32_t value at offset in the payload. More...
 
bool appendU4 (uint32_t value)
 Append a uint32_t value to the current end of the payload. More...
 
int32_t getI4 (size_t offset) const
 Get a int32_t value (little endian, 4 bytes, signed) at offset. More...
 
bool setI4 (size_t offset, int32_t value)
 Set an int16_t value at offset in the payload. More...
 
bool appendI4 (int32_t value)
 Append a int32_t value to the current end of the payload. More...
 
float getR4 (size_t offset) const
 Get a float value (4 bytes) at offset. More...
 
bool setR4 (size_t offset, float value)
 Set an float (4 byte floating point) value at offset in the payload. More...
 
bool appendR4 (float value)
 Append a float (4 byte floating point) value to the current end of the payload. More...
 
double getR8 (size_t offset) const
 Get a double value (8 bytes) at offset. More...
 
bool setR8 (size_t offset, double value)
 Set an double (8 byte floating point) value at offset in the payload. More...
 
bool appendR8 (double value)
 Append a double (8 byte floating point) value to the current end of the payload. More...
 
const uint8_t * getBuffer () const
 Get a pointer to the buffer, typically to send data. More...
 
size_t getSendLength () const
 Get the length of the data, typically to send it. More...
 

Protected Types

enum  State { State::LOOKING_FOR_START, State::LOOKING_FOR_LENGTH, State::LOOKING_FOR_MESSAGE }
 u-blox parsing state constants More...
 

Protected Attributes

uint8_t * buffer
 Buffer to hold data (received data or data for composing a packet to send)
 
size_t bufferSize
 Size of the buffer. Maximum payload is bufferSize - HEADER_PLUS_CRC_LEN.
 
size_t bufferOffset = 0
 Current offset being written to in buffer when using encode()
 
size_t payloadLen = 0
 Length of the data payload (0 = no data). This does not include the header or CRC.
 
State state = State::LOOKING_FOR_START
 Current parsing state.
 
std::vector< UbloxMessageHandler * > handlers
 Vector of message handler objects, contains filter and callback function to handle incoming messages.
 

Static Protected Attributes

static const uint8_t SYNC_1 = 0xb5
 value of the first sync byte at buffer[0]
 
static const uint8_t SYNC_2 = 0x62
 value of the second sync byte at buffer[1]
 
static const size_t CLASS_OFFSET = 2
 offset in buffer where the message class is stored
 
static const size_t ID_OFFSET = 3
 offset in buffer where the message ID is stored
 
static const size_t DATA_OFFSET = 6
 offset in buffer where data begins (after sync, class, id, and length)
 
static const size_t HEADER_PLUS_CRC_LEN = 8
 size of the header data (6 bytes) plus CRC (2 bytes). Minimum size of a valid packet with 0 bytes of payload data.
 
static const size_t CRC_START_OFFSET = 2
 offset in buffer where the CRC calculation starts (does not include sync bytes)
 
static const size_t CRC_HEADER_LEN = 4
 number of header bytes included in the CRC. The total CRC length is this plus payloadLen.
 

Detailed Description

Class for reading or writing a u-blox command.

You normally won't use this class directly, but will do so indirectly two ways:

Member Enumeration Documentation

◆ State

enum UbloxCommandBase::State
strongprotected

u-blox parsing state constants

Enumerator
LOOKING_FOR_START 

Looking for the first sync byte (SYNC_1, 0xb5)

LOOKING_FOR_LENGTH 

Waiting for DATA_OFFSET bytes to arrive so we can check the length.

LOOKING_FOR_MESSAGE 

Waiting for the rest of the message (HEADER_PLUS_CRC_LEN + payloadLen bytes)

Constructor & Destructor Documentation

◆ UbloxCommandBase()

UbloxCommandBase::UbloxCommandBase ( uint8_t *  buffer,
size_t  bufferSize 
)

Construct a UbloxCommandBase object. Normally you'd use UbloxCommand instead, which handles the buffer management for you.

Parameters
bufferPointer a buffer. Must remain valid for the life of this object. Must not be NULL.
bufferSizeSize of the buffer, a minimum of HEADER_PLUS_CRC_LEN (8) bytes. You will overwrite memory if the buffer is smaller than that.

Member Function Documentation

◆ addHandler()

void UbloxCommandBase::addHandler ( UbloxMessageHandler handler)

Add a message handler.

Parameters
handlerA pointer to a filled in UbloxMessageHandler structure.

Note the handler object must remain valid until removeHandler is called on it, so you probably want to make it a global variable or allocated with new. The exception is if you have a blocking function call and add and remove the handler within the scope of the function, then you can allocate the object on the stack.

◆ appendData()

bool UbloxCommandBase::appendData ( const void *  data,
size_t  dataLen 
)

Appends data into the buffer at the current payloadLen.

Parameters
dataPointer to the data to copy from.
dataLenThe length of the data in bytes.
Returns
true if it was copied or false if not

The data will be copied only if it will fit in the buffer entirely. The data will never be truncated. The payloadLen will be increased by dataLen bytes.

◆ appendI1()

bool UbloxCommandBase::appendI1 ( int8_t  value)
inline

Append a int8_t value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit.

◆ appendI2()

bool UbloxCommandBase::appendI2 ( int16_t  value)
inline

Append an int16_t value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied.

◆ appendI4()

bool UbloxCommandBase::appendI4 ( int32_t  value)
inline

Append a int32_t value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied.

◆ appendR4()

bool UbloxCommandBase::appendR4 ( float  value)
inline

Append a float (4 byte floating point) value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied.

◆ appendR8()

bool UbloxCommandBase::appendR8 ( double  value)
inline

Append a double (8 byte floating point) value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied.

◆ appendU1()

bool UbloxCommandBase::appendU1 ( uint8_t  value)
inline

Append a uint8_t value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit.

◆ appendU2()

bool UbloxCommandBase::appendU2 ( uint16_t  value)
inline

Append a uint16_t value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied.

◆ appendU4()

bool UbloxCommandBase::appendU4 ( uint32_t  value)
inline

Append a uint32_t value to the current end of the payload.

Parameters
valueThe value to append

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied.

◆ calculateChecksum()

void UbloxCommandBase::calculateChecksum ( uint8_t &  ckA,
uint8_t &  ckB 
) const

Calculates the checksum based on the data in the buffer.

The checksum is calculated not including the sync bytes or the CRC itself. This method returns the checksum values as parameters, so you can compare them.

The updateChecksum() method is typically used to calculate it for sending.

◆ decode()

bool UbloxCommandBase::decode ( char  ch)

Decode a single character.

This is called after reading data from the GPS by serial or I2C.

◆ fillData()

bool UbloxCommandBase::fillData ( uint8_t  value,
size_t  dataLen 
)

Works like appendData(), but fill with a value instead.

Parameters
valueThe uint8_t value to set. Often 0 to initialize to zero.
dataLenThe number of bytes to fill with value.

Handy for pre-creating zero-filled command buffers that are zeroed out.

◆ getBuffer()

const uint8_t* UbloxCommandBase::getBuffer ( ) const
inline

Get a pointer to the buffer, typically to send data.

This is a pointer to the first sync byte.

◆ getData() [1/2]

const uint8_t* UbloxCommandBase::getData ( ) const
inline

Get a const pointer to the data payload.

There are other methods to copy the data, and read out values like U2, R4, etc..

◆ getData() [2/2]

bool UbloxCommandBase::getData ( size_t  offset,
void *  data,
size_t  dataLen 
) const

Copies data from the buffer.

Parameters
offsetThe offset to being reading from. This is relative to the data payload, so 0 is the first byte of payload data (not the sync byte)
dataBuffer to copy data into
dataLenThe length of the data. This must be <= (payloadLen - offset)
Returns
true if the data could be retrieved or false if it could not

Will only return true if all of the data can be returned. If offset + dataLen > the payloadLen the false will be returned and no data will be returned.

◆ getI1()

int8_t UbloxCommandBase::getI1 ( size_t  offset) const
inline

Get a int8_t value (signed) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getI2()

int16_t UbloxCommandBase::getI2 ( size_t  offset) const
inline

Get a int16_t value (little endian, signed) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getI4()

int32_t UbloxCommandBase::getI4 ( size_t  offset) const
inline

Get a int32_t value (little endian, 4 bytes, signed) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getMsgClass()

uint8_t UbloxCommandBase::getMsgClass ( ) const
inline

Gets the message class of the current command.

This can be used from a message handler to find the message class of the message that is being handled.

◆ getMsgId()

uint8_t UbloxCommandBase::getMsgId ( ) const
inline

Gets the message ID of the current command.

This can be used from a message handler to find the message id of the message that is being handled.

◆ getR4()

float UbloxCommandBase::getR4 ( size_t  offset) const

Get a float value (4 bytes) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getR8()

double UbloxCommandBase::getR8 ( size_t  offset) const

Get a double value (8 bytes) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getSendLength()

size_t UbloxCommandBase::getSendLength ( ) const
inline

Get the length of the data, typically to send it.

It's the length from the first sync byte through the last CRC byte, including the data payload.

◆ getU1()

uint8_t UbloxCommandBase::getU1 ( size_t  offset) const

Get a uint8_t value at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getU2()

uint16_t UbloxCommandBase::getU2 ( size_t  offset) const

Get a uint16_t value (little endian) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ getU4()

uint32_t UbloxCommandBase::getU4 ( size_t  offset) const

Get a uint32_t value (little endian, 4 bytes) at offset.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload

◆ removeHandler()

void UbloxCommandBase::removeHandler ( UbloxMessageHandler handler)

Remove a message handler.

Parameters
handlerThe handler to remove (same value you passed to addHandler)

Note this will not free handler as it can't know if it was allocated on the stack, as a class member, global variable, or new. If you allocated the object with new, don't forget to delete it!

◆ setClassId()

void UbloxCommandBase::setClassId ( uint8_t  msgClass,
uint8_t  msgId 
)

When sending a message, sets the message class and ID.

Parameters
msgClassThe class of the message
msgIdThe ID of the message

◆ setData()

bool UbloxCommandBase::setData ( size_t  offset,
const void *  data,
size_t  dataLen 
)

Set data in the buffer.

Parameters
offsetThe offset to write to. This is relative to the data payload, so 0 is the first byte of payload data (not the sync byte)
dataPointer to the data to copy from.
dataLenThe length of the data in bytes.
Returns
true if it was copied or false if not

The data will be copied only if it will fit in the buffer entirely. The data will never be truncated. The payloadLen will be increased if necessary so the data fits within the payloadLen.

◆ setI1()

bool UbloxCommandBase::setI1 ( size_t  offset,
int8_t  value 
)
inline

Set a int8_t value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setI2()

bool UbloxCommandBase::setI2 ( size_t  offset,
int16_t  value 
)
inline

Set an int16_t value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setI4()

bool UbloxCommandBase::setI4 ( size_t  offset,
int32_t  value 
)
inline

Set an int16_t value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setR4()

bool UbloxCommandBase::setR4 ( size_t  offset,
float  value 
)
inline

Set an float (4 byte floating point) value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setR8()

bool UbloxCommandBase::setR8 ( size_t  offset,
double  value 
)
inline

Set an double (8 byte floating point) value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setU1()

bool UbloxCommandBase::setU1 ( size_t  offset,
uint8_t  value 
)
inline

Set a uint8_t value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setU2()

bool UbloxCommandBase::setU2 ( size_t  offset,
uint16_t  value 
)
inline

Set a uint16_t value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ setU4()

bool UbloxCommandBase::setU4 ( size_t  offset,
uint32_t  value 
)
inline

Set a uint32_t value at offset in the payload.

Parameters
offsetThe offset relative to the payload, so 0 is the first byte of the payload
valueThe value to set

The data must fit in the underlying buffer or false will be returned if it can't fit in its entirety. A partial value will never be copied. If the new data is larger than the current payloadLen, then the payload size is increased so it will contain the newly set data.

◆ updateChecksum()

void UbloxCommandBase::updateChecksum ( )

When preparing a command to send, updates the checksum, sync, and length bytes.

The length is generated from the payloadLen member variable, which should be correct when using the set or append methods, or after manually setting the length.


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