MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
Classes | 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 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::Span< uint8_t > buffer, uint8_t descriptorSet)
 Create a new MIP packet in an existing buffer. More...
 
 PacketView (microstrain::Span< const uint8_t > buffer)
 Create a reference to an existing MIP packet. More...
 
bool isData () const
 
Serializer createField (uint8_t fieldDescriptor, uint8_t length)
 
microstrain::Span< const uint8_t > totalSpan () const
 Gets a span over the whole packet. More...
 
microstrain::Span< const uint8_t > payloadSpan () const
 Gets a span over just the payload. More...
 
AllocatedField createField (uint8_t fieldDescriptor)
 
uint8_t operator[] (unsigned int index) const
 
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 (uint8_t *buffer, size_t maxLength)
 Copies this packet to an external buffer. More...
 
bool copyPacketTo (microstrain::Span< uint8_t > buffer)
 Copies this packet to an external buffer (span version). More...
 

Static Public Member Functions

template<class FieldType >
static PacketView createFromField (uint8_t *buffer, size_t bufferSize, 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 size_t PAYLOAD_LENGTH_MAX = C::MIP_PACKET_PAYLOAD_LENGTH_MAX
 
static constexpr size_t PACKET_SIZE_MIN = C::MIP_PACKET_LENGTH_MIN
 
static constexpr size_t PACKET_SIZE_MAX = C::MIP_PACKET_LENGTH_MAX
 

Detailed Description

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

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) { ... }

Constructor & Destructor Documentation

◆ PacketView() [1/6]

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

Create a brand-new MIP packet in the given buffer.

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

Initializes a MIP packet from an existing buffer.

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

Constructs a C++ PacketRef class from the base C object.

◆ PacketView() [4/6]

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

Constructs a C++ PacketRef class from the base C object.

◆ PacketView() [5/6]

mip::PacketView::PacketView ( microstrain::Span< uint8_t >  buffer,
uint8_t  descriptorSet 
)
inline

Create a new MIP packet in an existing buffer.

Parameters
bufferPlace to store the MIP packet bytes.
descriptorSetInitializes the packet to this descriptor set.

◆ PacketView() [6/6]

mip::PacketView::PacketView ( microstrain::Span< const uint8_t >  buffer)
inline

Create a reference to an existing MIP packet.

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

◆ addField()

template<class FieldType >
bool mip::PacketView::addField ( const FieldType &  field,
uint8_t  fieldDescriptor = INVALID_FIELD_DESCRIPTOR 
)
inline

Adds a field of the given type to the packet.

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.

◆ begin()

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

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

◆ copyPacketTo() [1/2]

bool mip::PacketView::copyPacketTo ( microstrain::Span< uint8_t >  buffer)
inline

Copies this packet to an external buffer (span version).

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.

◆ copyPacketTo() [2/2]

bool mip::PacketView::copyPacketTo ( uint8_t *  buffer,
size_t  maxLength 
)
inline

Copies this packet to an external buffer.

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

Parameters
bufferData is copied into this location.
maxLengthMaximum number of bytes to copy.
Returns
true if successful.
false if maxLength is too short.

◆ createField() [1/2]

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

◆ createField() [2/2]

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

◆ createFromField()

template<class FieldType >
static PacketView mip::PacketView::createFromField ( uint8_t *  buffer,
size_t  bufferSize,
const FieldType &  field,
uint8_t  fieldDescriptor = INVALID_FIELD_DESCRIPTOR 
)
inlinestatic

Creates a new PacketRef containing a single MIP field from an instance of the field type.

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
bufferBuffer to hold the packet bytes.
bufferSizeSize of buffer in bytes.
fieldInstance of the field to add to the packet.
fieldDescriptorIf specified, overrides the field descriptor.
Returns
A PacketRef object containing the field.

◆ 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

Returns the first field in the packet.

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).

◆ isData()

bool mip::PacketView::isData ( ) const
inline

◆ operator[]()

uint8_t mip::PacketView::operator[] ( unsigned int  index) const
inline

◆ payloadSpan()

microstrain::Span<const uint8_t> mip::PacketView::payloadSpan ( ) const
inline

Gets a span over just the payload.

◆ totalSpan()

microstrain::Span<const uint8_t> mip::PacketView::totalSpan ( ) const
inline

Gets a span over the whole packet.

Member Data Documentation

◆ PACKET_SIZE_MAX

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

◆ PACKET_SIZE_MIN

constexpr size_t mip::PacketView::PACKET_SIZE_MIN = C::MIP_PACKET_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: