AssetTrackerRK
|
Class for reading or writing a u-blox command. More...
#include <UbloxGPS.h>
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. | |
Class for reading or writing a u-blox command.
You normally won't use this class directly, but will do so indirectly two ways:
|
strongprotected |
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.
buffer | Pointer a buffer. Must remain valid for the life of this object. Must not be NULL. |
bufferSize | Size of the buffer, a minimum of HEADER_PLUS_CRC_LEN (8) bytes. You will overwrite memory if the buffer is smaller than that. |
void UbloxCommandBase::addHandler | ( | UbloxMessageHandler * | handler | ) |
Add a message handler.
handler | A 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.
bool UbloxCommandBase::appendData | ( | const void * | data, |
size_t | dataLen | ||
) |
Appends data into the buffer at the current payloadLen.
data | Pointer to the data to copy from. |
dataLen | The length of the data in bytes. |
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.
|
inline |
Append a int8_t value to the current end of the payload.
value | The value to append |
The data must fit in the underlying buffer or false will be returned if it can't fit.
|
inline |
Append an int16_t value to the current end of the payload.
value | The 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.
|
inline |
Append a int32_t value to the current end of the payload.
value | The 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.
|
inline |
Append a float (4 byte floating point) value to the current end of the payload.
value | The 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.
|
inline |
Append a double (8 byte floating point) value to the current end of the payload.
value | The 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.
|
inline |
Append a uint8_t value to the current end of the payload.
value | The value to append |
The data must fit in the underlying buffer or false will be returned if it can't fit.
|
inline |
Append a uint16_t value to the current end of the payload.
value | The 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.
|
inline |
Append a uint32_t value to the current end of the payload.
value | The 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.
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.
bool UbloxCommandBase::decode | ( | char | ch | ) |
Decode a single character.
This is called after reading data from the GPS by serial or I2C.
bool UbloxCommandBase::fillData | ( | uint8_t | value, |
size_t | dataLen | ||
) |
Works like appendData(), but fill with a value instead.
value | The uint8_t value to set. Often 0 to initialize to zero. |
dataLen | The number of bytes to fill with value. |
Handy for pre-creating zero-filled command buffers that are zeroed out.
|
inline |
Get a pointer to the buffer, typically to send data.
This is a pointer to the first sync byte.
|
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..
bool UbloxCommandBase::getData | ( | size_t | offset, |
void * | data, | ||
size_t | dataLen | ||
) | const |
Copies data from the buffer.
offset | The 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) |
data | Buffer to copy data into |
dataLen | The length of the data. This must be <= (payloadLen - offset) |
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.
|
inline |
Get a int8_t value (signed) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
|
inline |
Get a int16_t value (little endian, signed) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
|
inline |
Get a int32_t value (little endian, 4 bytes, signed) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
|
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.
|
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.
float UbloxCommandBase::getR4 | ( | size_t | offset | ) | const |
Get a float value (4 bytes) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
double UbloxCommandBase::getR8 | ( | size_t | offset | ) | const |
Get a double value (8 bytes) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
|
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.
uint8_t UbloxCommandBase::getU1 | ( | size_t | offset | ) | const |
Get a uint8_t value at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
uint16_t UbloxCommandBase::getU2 | ( | size_t | offset | ) | const |
Get a uint16_t value (little endian) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
uint32_t UbloxCommandBase::getU4 | ( | size_t | offset | ) | const |
Get a uint32_t value (little endian, 4 bytes) at offset.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
void UbloxCommandBase::removeHandler | ( | UbloxMessageHandler * | handler | ) |
Remove a message handler.
handler | The 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!
void UbloxCommandBase::setClassId | ( | uint8_t | msgClass, |
uint8_t | msgId | ||
) |
When sending a message, sets the message class and ID.
msgClass | The class of the message |
msgId | The ID of the message |
bool UbloxCommandBase::setData | ( | size_t | offset, |
const void * | data, | ||
size_t | dataLen | ||
) |
Set data in the buffer.
offset | The offset to write to. This is relative to the data payload, so 0 is the first byte of payload data (not the sync byte) |
data | Pointer to the data to copy from. |
dataLen | The length of the data in bytes. |
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.
|
inline |
Set a int8_t value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set an int16_t value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set an int16_t value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set an float (4 byte floating point) value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set an double (8 byte floating point) value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set a uint8_t value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set a uint16_t value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
|
inline |
Set a uint32_t value at offset in the payload.
offset | The offset relative to the payload, so 0 is the first byte of the payload |
value | The 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.
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.