MIP_SDK  v4.0.0-234-g2b06a940
MicroStrain Communications Library for embedded systems
Classes | Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
mip::PacketView Class Reference

C++ class representing a view of a MIP packet. More...

#include <mip_packet.hpp>

Inheritance diagram for mip::PacketView:
mip::C::mip_packet_view mip::SizedPacketBuf< BufferSize >

Classes

class  AllocatedField
 
class  FieldIterator
 

Public Types

enum  Index : uint8_t {
  Index::SYNC_1 = C::MIP_PACKET_INDEX_SYNC_1, Index::SYNC_2 = C::MIP_PACKET_INDEX_SYNC_2, Index::DESC_SET = C::MIP_PACKET_INDEX_DESC_SET, Index::LENGTH = C::MIP_PACKET_INDEX_LENGTH,
  Index::PAYLOAD = C::MIP_PACKET_INDEX_PAYLOAD
}
 

Public Member Functions

 PacketView (uint8_t *buffer, size_t bufferSize, uint8_t descriptorSet)
 Create a brand-new MIP packet in the given buffer. More...
 
 PacketView (const uint8_t *buffer, size_t length)
 Initializes a MIP packet from an existing buffer. More...
 
 PacketView (const C::mip_packet_view *other)
 Constructs a C++ PacketRef class from the base C object. More...
 
 PacketView (const C::mip_packet_view &other)
 Constructs a C++ PacketRef class from the base C object. More...
 
 PacketView (microstrain::U8ArrayView buffer, uint8_t descriptorSet)
 Create a new MIP packet in an existing buffer. More...
 
 PacketView (microstrain::ConstU8ArrayView buffer)
 Create a reference to an existing MIP packet. More...
 
microstrain::ConstU8ArrayView buffer () const
 
microstrain::U8ArrayView buffer_w ()
 Gets the entire storage buffer for the packet. More...
 
microstrain::ConstU8ArrayView data () const
 
microstrain::ConstU8ArrayView payload () const
 
microstrain::U8ArrayView payload_w ()
 Get the payload as raw bytes. More...
 
Serializer createField (uint8_t fieldDescriptor, uint8_t length)
 
uint8_t dataAt (const size_t i) const
 
uint8_t & dataAt (const size_t i)
 
uint8_t dataAt (const Index i) const
 
uint8_t & dataAt (const Index i)
 
uint8_t payloadAt (const size_t i) const
 
uint8_t & payloadAt (const size_t i)
 Get payload byte at index i. More...
 
bool addField (uint8_t fieldDescriptor, microstrain::ConstU8ArrayView payload)
 Get writable payload byte at index i. More...
 
bool addField (const FieldView &field)
 Copies the given mip field to the packet. More...
 
AllocatedField createField (uint8_t fieldDescriptor)
 
FieldIterator begin () const
 
FieldIterator end () const
 
FieldView firstField () const
 Returns the first field in the packet. More...
 
template<class FieldType >
bool addField (const FieldType &field, uint8_t fieldDescriptor=INVALID_FIELD_DESCRIPTOR)
 Adds a field of the given type to the packet. More...
 
bool copyPacketTo (microstrain::U8ArrayView buffer) const
 Copies this packet to an external buffer (span version). More...
 

Static Public Member Functions

template<class FieldType >
static PacketView createFromField (microstrain::U8ArrayView packetBuffer, const FieldType &field, uint8_t fieldDescriptor=INVALID_FIELD_DESCRIPTOR)
 Creates a new PacketRef containing a single MIP field from an instance of the field type. More...
 

Static Public Attributes

static constexpr uint8_t SYNC_1 = C::MIP_SYNC_1
 
static constexpr uint8_t SYNC_2 = C::MIP_SYNC_2
 
static constexpr size_t HEADER_LENGTH = C::MIP_PACKET_HEADER_LENGTH
 
static constexpr size_t CHECKSUM_LENGTH = C::MIP_PACKET_CHECKSUM_LENGTH
 
static constexpr size_t LENGTH_MIN = C::MIP_PACKET_LENGTH_MIN
 
static constexpr size_t LENGTH_MAX = C::MIP_PACKET_LENGTH_MAX
 
static constexpr size_t PAYLOAD_LENGTH_MIN = C::MIP_PACKET_PAYLOAD_LENGTH_MIN
 
static constexpr size_t PAYLOAD_LENGTH_MAX = C::MIP_PACKET_PAYLOAD_LENGTH_MAX
 

Detailed Description

This is a thin wrapper around the mip_packet_view C structure. Like the C version, it does not contain or own the data buffer. Any of the C functions can be used with the C++ packet class because it inherits from the C struct.

Fields may be iterated over using the C-style methods, with an iterator, or with a range-based for loop:

for(PacketRef::Iterator iter = packet.begin(); iter != packet.end(); ++iter) { ... }
for(Field field : packet) { ... }

Member Enumeration Documentation

◆ Index

enum mip::PacketView::Index : uint8_t
strong
Enumerator
SYNC_1 
SYNC_2 
DESC_SET 
LENGTH 
PAYLOAD 

Constructor & Destructor Documentation

◆ PacketView() [1/6]

mip::PacketView::PacketView ( uint8_t *  buffer,
size_t  bufferSize,
uint8_t  descriptorSet 
)
inline

Use this along with the packet building functions to create MIP packets.

Parameters
packet
bufferThis is where the packet bytes will be stored. Must be at least MIP_PACKET_LENGTH_MIN bytes in size.
buffer_sizeThe size of buffer, in bytes.
descriptor_setThe MIP descriptor set for the packet.

◆ PacketView() [2/6]

mip::PacketView::PacketView ( const uint8_t *  buffer,
size_t  length 
)
inline

Use this when receiving or parsing MIP packets.

The data in the buffer should be a valid or suspected MIP packet.

Parameters
packet
bufferThe data buffer containing the bytes for a MIP packet. Must be at least MIP_PACKET_LENGTH_MIN bytes in size.
lengthThe length of the data pointed to by buffer.
Note
The data does not need to be a valid MIP packet, for instance to use the mip_packet_is_sane() or mip_packet_is_valid() functions. However, if it is NOT a valid MIP packet, the result of calling any accessor function is unpredictable. In particular, if length is less than MIP_PACKET_LENGTH_MIN bytes, calling the accessor functions is undefined behavior.

◆ PacketView() [3/6]

mip::PacketView::PacketView ( const C::mip_packet_view other)
inline

◆ PacketView() [4/6]

mip::PacketView::PacketView ( const C::mip_packet_view other)
inline

◆ PacketView() [5/6]

mip::PacketView::PacketView ( microstrain::U8ArrayView  buffer,
uint8_t  descriptorSet 
)
inline
Parameters
bufferPlace to store the MIP packet bytes.
descriptorSetInitializes the packet to this descriptor set.

◆ PacketView() [6/6]

mip::PacketView::PacketView ( microstrain::ConstU8ArrayView  buffer)
inline
Parameters
bufferBuffer containing an existing MIP packet.
Warning
Do not call functions which modify the packet (addField, finalize, reset, etc) unless you know the buffer is not const.

Member Function Documentation

◆ buffer()

microstrain::ConstU8ArrayView mip::PacketView::buffer ( ) const
inline

◆ buffer_w()

microstrain::U8ArrayView mip::PacketView::buffer_w ( )
inline

◆ data()

microstrain::ConstU8ArrayView mip::PacketView::data ( ) const
inline

◆ payload()

microstrain::ConstU8ArrayView mip::PacketView::payload ( ) const
inline

◆ payload_w()

microstrain::U8ArrayView mip::PacketView::payload_w ( )
inline

◆ createField() [1/2]

Serializer mip::PacketView::createField ( uint8_t  fieldDescriptor,
uint8_t  length 
)
inline

◆ dataAt() [1/4]

uint8_t mip::PacketView::dataAt ( const size_t  i) const
inline

◆ dataAt() [2/4]

uint8_t& mip::PacketView::dataAt ( const size_t  i)
inline

◆ dataAt() [3/4]

uint8_t mip::PacketView::dataAt ( const Index  i) const
inline

◆ dataAt() [4/4]

uint8_t& mip::PacketView::dataAt ( const Index  i)
inline

◆ payloadAt() [1/2]

uint8_t mip::PacketView::payloadAt ( const size_t  i) const
inline

◆ payloadAt() [2/2]

uint8_t& mip::PacketView::payloadAt ( const size_t  i)
inline

◆ addField() [1/3]

bool mip::PacketView::addField ( uint8_t  fieldDescriptor,
microstrain::ConstU8ArrayView  payload 
)
inline

Creates a mip field with the given descriptor and a copy of the payload.

◆ addField() [2/3]

bool mip::PacketView::addField ( const FieldView field)
inline

◆ createField() [2/2]

AllocatedField mip::PacketView::createField ( uint8_t  fieldDescriptor)
inline

◆ begin()

FieldIterator mip::PacketView::begin ( ) const
inline

Returns a forward iterator to the first field in the packet.

◆ end()

FieldIterator mip::PacketView::end ( ) const
inline

Returns a sentry object representing the end of fields in the packet.

◆ firstField()

FieldView mip::PacketView::firstField ( ) const
inline

Subsequent fields can be obtained via the returned Field class, but iteration is best done with begin()/end() or the range-based for loop.

Note
Packets can be empty, so make sure that the returned field is valid before using it.
Returns
A Field instance representing the first field (if any).

◆ addField() [3/3]

template<class FieldType >
bool mip::PacketView::addField ( const FieldType &  field,
uint8_t  fieldDescriptor = INVALID_FIELD_DESCRIPTOR 
)
inline
Template Parameters
FieldTypeAny field class from a file in the mip/definitions directory.
Parameters
fieldInstance of the field to add to the packet.
fieldDescriptorIf specified, overrides the field descriptor.
Returns
True if the field was added, false if the packet has insufficient space.

◆ createFromField()

template<class FieldType >
static PacketView mip::PacketView::createFromField ( microstrain::U8ArrayView  packetBuffer,
const FieldType &  field,
uint8_t  fieldDescriptor = INVALID_FIELD_DESCRIPTOR 
)
inlinestatic

This works just like the addField<FieldType>() function but also initializes and finalizes the packet. It is assumed that the field will fit in an empty packet; otherwise the field can't ever be used. The field classes are predefined so this doesn't need runtime checking.

Template Parameters
FieldTypeAny field class from a file in the mip/definitions directory.
Parameters
packetBufferBuffer to hold the packet bytes.
fieldInstance of the field to add to the packet.
fieldDescriptorIf specified, overrides the field descriptor.
Returns
A PacketRef object containing the field.

◆ copyPacketTo()

bool mip::PacketView::copyPacketTo ( microstrain::U8ArrayView  buffer) const
inline

This packet must be sane (see isSane()). Undefined behavior otherwise due to lookup of totalLength().

Parameters
bufferData is copied to this buffer.
Returns
true if successful.
false if maxLength is too short.

Member Data Documentation

◆ SYNC_1

constexpr uint8_t mip::PacketView::SYNC_1 = C::MIP_SYNC_1
staticconstexpr

◆ SYNC_2

constexpr uint8_t mip::PacketView::SYNC_2 = C::MIP_SYNC_2
staticconstexpr

◆ HEADER_LENGTH

constexpr size_t mip::PacketView::HEADER_LENGTH = C::MIP_PACKET_HEADER_LENGTH
staticconstexpr

◆ CHECKSUM_LENGTH

constexpr size_t mip::PacketView::CHECKSUM_LENGTH = C::MIP_PACKET_CHECKSUM_LENGTH
staticconstexpr

◆ LENGTH_MIN

constexpr size_t mip::PacketView::LENGTH_MIN = C::MIP_PACKET_LENGTH_MIN
staticconstexpr

◆ LENGTH_MAX

constexpr size_t mip::PacketView::LENGTH_MAX = C::MIP_PACKET_LENGTH_MAX
staticconstexpr

◆ PAYLOAD_LENGTH_MIN

constexpr size_t mip::PacketView::PAYLOAD_LENGTH_MIN = C::MIP_PACKET_PAYLOAD_LENGTH_MIN
staticconstexpr

◆ PAYLOAD_LENGTH_MAX

constexpr size_t mip::PacketView::PAYLOAD_LENGTH_MAX = C::MIP_PACKET_PAYLOAD_LENGTH_MAX
staticconstexpr

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