MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
mip_parser.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "mip_packet.hpp"
4 
5 #include <mip/mip_parser.h>
6 
7 
8 namespace mip
9 {
13 
19 class Parser : public C::mip_parser
20 {
21 public:
23  Parser(uint8_t* buffer, size_t bufferSize, C::mip_packet_callback callback, void* callbackObject, Timeout timeout) { C::mip_parser_init(this, buffer, bufferSize, callback, callbackObject, timeout); }
25  Parser(uint8_t* buffer, size_t bufferSize, bool (*callback)(void*,const PacketView*,Timestamp), void* callbackObject, Timeout timeout) { C::mip_parser_init(this, buffer, bufferSize, (C::mip_packet_callback)callback, callbackObject, timeout); }
26 
27  Parser(uint8_t* buffer, size_t bufferSize, Timeout timeout) { C::mip_parser_init(this, buffer, bufferSize, nullptr, nullptr, timeout); }
28 
29  template<class T, bool (T::*Callback)(const PacketView&, Timestamp)>
30  void setCallback(T& object);
31 
33  void reset() { C::mip_parser_reset(this); }
34 
36  size_t parse(const uint8_t* inputBuffer, size_t inputCount, Timestamp timestamp, unsigned int maxPackets=0) { return C::mip_parser_parse(this, inputBuffer, inputCount, timestamp, maxPackets); }
37 
39  Timeout timeout() const { return C::mip_parser_timeout(this); }
42 };
43 
44 
66 template<class T, bool (T::*Callback)(const PacketView&, Timestamp)>
67 void Parser::setCallback(T& object)
68 {
69  C::mip_packet_callback callback = [](void* obj, const C::mip_packet_view* pkt, Timestamp timestamp)->bool
70  {
71  return (static_cast<T*>(obj)->*Callback)(PacketView(pkt), timestamp);
72  };
73 
74  C::mip_parser_set_callback(this, callback, &object);
75 }
76 
102 template<class Function>
103 bool parseMipDataFromSource(C::mip_parser& parser, Function reader, size_t maxPackets)
104 {
105  uint8_t* ptr;
106  size_t maxCount = C::mip_parser_get_write_ptr(&parser, &ptr);
107 
108  size_t count;
109  Timestamp timestamp;
110  if( !reader(ptr, maxCount, &count, &timestamp) )
111  return false;
112 
113  assert(count <= maxCount);
114 
115  C::mip_parser_process_written(&parser, count, timestamp, maxPackets);
116 
117  return true;
118 }
119 
122 } // namespace mip
mip::Parser::Parser
Parser(uint8_t *buffer, size_t bufferSize, Timeout timeout)
Definition: mip_parser.hpp:27
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
mip::C::mip_parser_process_written
void mip_parser_process_written(mip_parser *parser, size_t count, mip_timestamp timestamp, unsigned int max_packets)
Notify the parser that data has been written to the pointer previously obtained via mip_parser_get_wr...
Definition: mip_parser.c:387
mip_parser.h
mip::C::mip_parser_set_callback
void mip_parser_set_callback(mip_parser *parser, mip_packet_callback callback, void *callback_object)
mip_parser_set_callback
Definition: mip_parser.c:287
mip::C::mip_parser_timeout
mip_timestamp mip_parser_timeout(const mip_parser *parser)
Returns the packet timeout of the parser.
Definition: mip_parser.c:263
mip::Parser::setCallback
void setCallback(T &object)
Initializes the MIP Parser.
Definition: mip_parser.hpp:67
mip::Parser::Parser
Parser(uint8_t *buffer, size_t bufferSize, bool(*callback)(void *, const PacketView *, Timestamp), void *callbackObject, Timeout timeout)
Initializes the MIP parser.
Definition: mip_parser.hpp:25
mip::parseMipDataFromSource
bool parseMipDataFromSource(C::mip_parser &parser, Function reader, size_t maxPackets)
Read data from a source into the internal parsing buffer.
Definition: mip_parser.hpp:103
mip::C::mip_parser_parse
size_t mip_parser_parse(mip_parser *parser, const uint8_t *input_buffer, size_t input_count, mip_timestamp timestamp, unsigned int max_packets)
Parses packets from the input data buffer.
Definition: mip_parser.c:121
mip::C::mip_packet_view
Structure representing a MIP Packet.
Definition: mip_packet.h:50
mip::Parser::Parser
Parser(uint8_t *buffer, size_t bufferSize, C::mip_packet_callback callback, void *callbackObject, Timeout timeout)
Initializes the MIP parser.
Definition: mip_parser.hpp:23
mip::C::mip_packet_callback
bool(* mip_packet_callback)(void *user, const mip_packet_view *packet, mip_timestamp timestamp)
Callback function which receives parsed MIP packets.
Definition: mip_parser.h:45
mip::Parser::parse
size_t parse(const uint8_t *inputBuffer, size_t inputCount, Timestamp timestamp, unsigned int maxPackets=0)
Parses packets from the input data buffer.
Definition: mip_parser.hpp:36
mip::C::mip_parser
MIP Parser state.
Definition: mip_parser.h:55
mip::C::mip_parser_get_write_ptr
size_t mip_parser_get_write_ptr(mip_parser *parser, uint8_t **const ptr_out)
Obtain a pointer into which data may be read for processing.
Definition: mip_parser.c:368
mip::C::mip_parser_reset
void mip_parser_reset(mip_parser *parser)
Resets the MIP parser.
Definition: mip_parser.c:59
mip::C::mip_parser_init
void mip_parser_init(mip_parser *parser, uint8_t *buffer, size_t buffer_size, mip_packet_callback callback, void *callback_object, mip_timestamp timeout)
Initializes the MIP parser.
Definition: mip_parser.c:38
mip::Timestamp
microstrain::EmbeddedTimestamp Timestamp
Definition: mip_types.h:50
mip::Timeout
microstrain::EmbeddedTimeout Timeout
Definition: mip_types.h:51
mip::Parser::reset
void reset()
Resets the MIP parser.
Definition: mip_parser.hpp:33
mip::Parser::timeout
Timeout timeout() const
Returns the packet timeout of the parser.
Definition: mip_parser.hpp:39
mip::C::mip_parser_set_timeout
void mip_parser_set_timeout(mip_parser *parser, mip_timestamp timeout)
Changes the timeout of the MIP parser.
Definition: mip_parser.c:275
mip_packet.hpp
mip::PacketView
C++ class representing a view of a MIP packet.
Definition: mip_packet.hpp:35
mip::Parser
C++ class representing a MIP parser.
Definition: mip_parser.hpp:19
mip::Parser::setTimeout
void setTimeout(Timeout timeout)
Changes the timeout of the MIP parser.
Definition: mip_parser.hpp:41