MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
mip_field.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "mip_descriptors.hpp"
4 #include "mip_serialization.hpp"
5 
7 
8 #include <mip/mip_field.h>
9 #include <mip/mip_offsets.h>
10 
11 #include <cstring>
12 
13 
14 namespace mip
15 {
19 
26 {
27 public:
29 
31  FieldView() { C::mip_field_view::_payload=nullptr; C::mip_field_view::_payload_length=0; C::mip_field_view::_field_descriptor=0x00; C::mip_field_view::_descriptor_set=0x00; C::mip_field_view::_remaining_length=0; }
33  FieldView(uint8_t descriptor_set, uint8_t field_descriptor, const uint8_t* payload, uint8_t payload_length) { C::mip_field_init(this, descriptor_set, field_descriptor, payload, payload_length); }
35  FieldView(const uint8_t* header, uint8_t total_length, uint8_t descriptor_set) { *this = C::mip_field_from_header_ptr(header, total_length, descriptor_set); }
37  FieldView(const C::mip_field_view& other) { std::memcpy(static_cast<C::mip_field_view*>(this), &other, sizeof(C::mip_field_view)); }
38 
39  //
40  // C function wrappers
41  //
42 
44  uint8_t descriptorSet() const { return C::mip_field_descriptor_set(this); }
46  uint8_t fieldDescriptor() const { return C::mip_field_field_descriptor(this); }
50  uint8_t payloadLength() const { return C::mip_field_payload_length(this); }
52  const uint8_t* payload() const { return C::mip_field_payload(this); }
53 
57  uint8_t payload(unsigned int index) const { return payload()[index]; }
58 
59  uint8_t operator[](unsigned int index) const { return payload(index); }
60 
62 
64  bool isValid() const { return C::mip_field_is_valid(this); }
65 
67  FieldView nextAfter() const { return C::mip_field_next_after(this); }
69  bool next() { return C::mip_field_next(this); }
70 
71  //
72  // Additional functions which have no C equivalent
73  //
74 
86  template<class FieldType>
87  bool extract(FieldType& field, bool exact_size=true) const { return microstrain::extract<microstrain::serialization::Endian::big>(field, payload(), payloadLength(), 0, exact_size); }
88 
89 
91  bool isData() const { return isDataDescriptorSet(descriptorSet()); }
92 
95 
98 
101 
104 };
105 
106 
109 } // namespace mip
mip::isCommandDescriptorSet
bool isCommandDescriptorSet(uint8_t descriptorSet)
Definition: mip_descriptors.hpp:80
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
mip::FieldView::isData
bool isData() const
Determines if the field holds data (and not a command, reply, or response).
Definition: mip_field.hpp:91
mip::FieldView::next
bool next()
Updates the mip_field to refer to the next field in a packet.
Definition: mip_field.hpp:69
mip_descriptors.hpp
mip::FieldView::payload
uint8_t payload(unsigned int index) const
Index the payload at the given location.
Definition: mip_field.hpp:57
mip::C::mip_field_descriptor_set
uint8_t mip_field_descriptor_set(const mip_field_view *field)
Returns the descriptor set of the packet containing this field._.
Definition: mip_field.c:50
mip::C::mip_field_from_header_ptr
mip_field_view mip_field_from_header_ptr(const uint8_t *header, uint8_t total_length, uint8_t descriptor_set)
Constructs a mip_field from a pointer to the heaader.
Definition: mip_field.c:122
mip::isCommandFieldDescriptor
bool isCommandFieldDescriptor(uint8_t fieldDescriptor)
Definition: mip_descriptors.hpp:85
mip::FieldView::extract
bool extract(FieldType &field, bool exact_size=true) const
Deserializes the field data to specific field struct.
Definition: mip_field.hpp:87
microstrain::Span
Implementation of std::span from C++20.
Definition: span.hpp:40
mip::isDataDescriptorSet
bool isDataDescriptorSet(uint8_t descriptorSet)
Definition: mip_descriptors.hpp:79
mip::C::MIP_FIELD_PAYLOAD_LENGTH_MAX
@ MIP_FIELD_PAYLOAD_LENGTH_MAX
Definition: mip_offsets.h:35
mip::C::mip_field_next_after
mip_field_view mip_field_next_after(const mip_field_view *field)
Gets the next field after the specified field.
Definition: mip_field.c:185
mip::C::mip_field_view
A structure representing a MIP field.
Definition: mip_field.h:52
mip::FieldView::isReply
bool isReply() const
Determines if the field holds an ack/nack reply code.
Definition: mip_field.hpp:100
mip::FieldView::payloadLength
uint8_t payloadLength() const
Returns the length of the payload.
Definition: mip_field.hpp:50
mip::isResponseFieldDescriptor
bool isResponseFieldDescriptor(uint8_t fieldDescriptor)
Definition: mip_descriptors.hpp:87
mip::FieldView::FieldView
FieldView(const uint8_t *header, uint8_t total_length, uint8_t descriptor_set)
Constructs a mip_field from a pointer to the heaader.
Definition: mip_field.hpp:35
mip::FieldView
C++ class representing a MIP field.
Definition: mip_field.hpp:25
mip::C::mip_field_next
bool mip_field_next(mip_field_view *field)
Updates the mip_field to refer to the next field in a packet.
Definition: mip_field.c:204
mip::FieldView::operator[]
uint8_t operator[](unsigned int index) const
Definition: mip_field.hpp:59
mip_field.h
mip::FieldView::fieldDescriptor
uint8_t fieldDescriptor() const
Returns the field descriptor.
Definition: mip_field.hpp:46
mip::isReplyFieldDescriptor
bool isReplyFieldDescriptor(uint8_t fieldDescriptor)
Definition: mip_descriptors.hpp:86
mip::C::mip_field_payload_length
uint8_t mip_field_payload_length(const mip_field_view *field)
Returns the length of the payload.
Definition: mip_field.c:66
mip::FieldView::FieldView
FieldView(const C::mip_field_view &other)
Creates a Field class from the mip_field C struct.
Definition: mip_field.hpp:37
mip::FieldView::FieldView
FieldView(uint8_t descriptor_set, uint8_t field_descriptor, const uint8_t *payload, uint8_t payload_length)
Constructs a field view given the parameters.
Definition: mip_field.hpp:33
mip::C::mip_field_is_valid
bool mip_field_is_valid(const mip_field_view *field)
Returns true if the field has a valid field descriptor.
Definition: mip_field.c:84
mip::FieldView::descriptor
CompositeDescriptor descriptor() const
Returns the descriptor set and field descriptor.
Definition: mip_field.hpp:48
mip::FieldView::isCommand
bool isCommand() const
Determines if the field holds a command.
Definition: mip_field.hpp:97
mip::FieldView::payloadSpan
microstrain::Span< const uint8_t > payloadSpan() const
Definition: mip_field.hpp:61
mip::FieldView::isValid
bool isValid() const
Returns true if the field has a valid field descriptor.
Definition: mip_field.hpp:64
mip::C::mip_field_field_descriptor
uint8_t mip_field_field_descriptor(const mip_field_view *field)
Returns the field descriptor.
Definition: mip_field.c:58
mip::FieldView::isCommandSet
bool isCommandSet() const
Determines if the field is from a command descriptor set (a command, reply, or response field).
Definition: mip_field.hpp:94
mip::C::mip_field_init
void mip_field_init(mip_field_view *field, uint8_t descriptor_set, uint8_t field_descriptor, const uint8_t *payload, uint8_t payload_length)
Constructs a field view given the parameters.
Definition: mip_field.c:34
mip::CompositeDescriptor
Convenience struct holding both descriptor set and field descriptor.
Definition: mip_descriptors.hpp:21
mip::FieldView::isResponse
bool isResponse() const
Determines if the field holds command response data (not an ack/nack reply).
Definition: mip_field.hpp:103
mip_serialization.hpp
mip::FieldView::FieldView
FieldView()
Construct an empty MIP field.
Definition: mip_field.hpp:31
span.hpp
mip_offsets.h
mip::FieldView::descriptorSet
uint8_t descriptorSet() const
Returns the descriptor set of the packet containing this field._.
Definition: mip_field.hpp:44
mip::FieldView::nextAfter
FieldView nextAfter() const
Gets the next field after the specified field.
Definition: mip_field.hpp:67
mip::C::mip_field_payload
const uint8_t * mip_field_payload(const mip_field_view *field)
Returns the payload pointer for the field data.
Definition: mip_field.c:76
mip::FieldView::MAX_PAYLOAD_LENGTH
static constexpr size_t MAX_PAYLOAD_LENGTH
Definition: mip_field.hpp:28
mip::FieldView::payload
const uint8_t * payload() const
Returns the payload pointer for the field data.
Definition: mip_field.hpp:52