Main MRPT website > C++ reference for MRPT 1.9.9
cmtmessage.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef _CMTMESSAGE_H_2006_05_24
11 #define _CMTMESSAGE_H_2006_05_24
12 
13 #ifndef _CMT_MONOLITHIC
14 #include "cmtdef.h"
15 #include "cmt1.h"
16 #endif
17 
18 namespace xsens
19 {
20 //////////////////////////////////////////////////////////////////////////////////////////
21 /*! \brief Compute the checksum of the given byte string.
22 */
24 
25 //////////////////////////////////////////////////////////////////////////////////////////
26 /* different alignment commands for gcc / MSVS, the structure needs to be 1-byte
27  * aligned.
28 */
29 #ifdef _MSC_VER
30 #pragma pack(push, 1)
31 #endif
32 /*! \brief A message header.
33 
34  DO NOT REORDER THE MEMBERS!
35 */
37 {
42  union _mdl {
43  struct _mextd
44  {
45  struct _mlen
46  {
49  } m_length;
51  } m_extended;
53  } m_datlen;
54 }
55 // different alignment commands for gcc / MSVS
56 #ifdef _MSC_VER
57 ;
58 #pragma pack(pop)
59 #else
60 /*! \cond NODOXYGEN */ __attribute__((__packed__)) /*! \endcond */;
61 #endif
62 
63 #if defined(_DEBUG) && defined(_CMT_CHECK_MSG_DATA_INTEGRITY)
64 #define CMT_CHECKVAR uint32_t m_checkvar;
65 #define CMT_CHECKVAL 10101010
66 #define CMT_CHECKASSIGN m_checkvar = CMT_CHECKVAL;
67 #define CMT_CHECKASSERT \
68  if (m_checkvar != CMT_CHECKVAL) throw "Message assertion failed!";
69 #else
70 #define CMT_CHECKVAR
71 #define CMT_CHECKVAL
72 #define CMT_CHECKASSIGN
73 #define CMT_CHECKASSERT
74 #endif
75 
76 #define swapEndian16(src) (((src) >> 8) | ((src) << 8))
77 #define swapEndian32(src) \
78  (((src) >> 24) | ((src) >> 8 & 0xFF00) | ((src) << 8 & 0xFF0000) | \
79  ((src) << 24))
80 
81 //////////////////////////////////////////////////////////////////////////////////////////
82 //! \brief Class for storing a single message.
83 class Message
84 {
85  protected:
87 
88  //! The message header is the data buffer with interpretation
90  //! The checksum in the m_data or m_extendedData buffer
92  //! The maximum size of the message, including header and footer
94 
95  //! Internal checksum computation
96  uint8_t calcChecksum(void) const
97  {
98  return computeChecksum(
99  ((uint8_t*)m_buffer) + 1, getTotalMessageSize() - 2);
100  }
101  //! Internal function to get the start of the data buffer.
102  uint8_t* getDataStart(void) const;
103 
104  public:
106 
107  /*! \brief Create a Message object with the given data length and message
108  Id.
109 
110  The function allocates enough memory to hold an entire message with the
111  given
112  data length.
113  \param msgId The message Id that will be assigend to the m_messageId
114  field.
115  \param length The length of the data in the message. This value is
116  stored in
117  \c m_createdLength as well as \c m_length or \c
118  m_extendedLength.
119  \param maxLength The maximum data length that can be stored in the
120  structure.
121  */
122  Message(
123  const uint8_t msgId = 0, const uint16_t length = 0,
125 
126  /*! \brief Create a message from the given source string
127 
128  This is done through a simple memory copy. The number of bytes copied is
129  taken
130  from the data in the message (so the message is interpreted first).
131  Note that this does NOT recompute the checksum, nor is it checked.
132 
133  \param source The source string containing message data
134  \param size The size of the source string
135  \param maxLength The maximum data length that can be stored in the
136  structure.
137  */
138  Message(
139  const uint8_t* source, const uint16_t size,
141 
142  Message(const Message& src);
143 
144  //! Destructor
145  ~Message();
146 
147  //! Clear all data in the message
148  void clear(void);
149 
150  //! Return the current value of the m_busId field.
151  uint8_t getBusId(void) const { return m_buffer->m_busId; }
152  /*! \brief Return a pointer to the data buffer.
153 
154  \param offset An optional offset in the data buffer from where to start
155  reading.
156  */
158  {
159  return &((getDataStart())[offset]);
160  }
161  const uint8_t* getDataBuffer(const uint16_t offset = 0) const
162  {
163  return &((getDataStart())[offset]);
164  }
165  /*! \brief Return the current value of the data as an unsigned byte (8
166  bits).
167 
168  \param offset An optional offset in the data buffer from where to start
169  reading.
170  */
172  {
173  return (getDataStart()[offset]);
174  }
175  /*! \brief Return the current value of the data as a double (64 bits).
176 
177  \param offset An optional offset in the data buffer from where to start
178  reading.
179  */
180  double getDataDouble(const uint16_t offset = 0) const;
181  /*! \brief Return the current value of the data as a float (32 bits).
182 
183  \param offset An optional offset in the data buffer from where to start
184  reading.
185  */
186  float getDataFloat(const uint16_t offset = 0) const;
187  /*! \brief Return the current value of the data as a double, converting it
188  from FP 12.20
189 
190  \param offset An optional offset in the data buffer from where to start
191  reading.
192  */
193  double getDataF1220(const uint16_t offset = 0) const;
194  /*! \brief Return the current value of the data as a double, converting it
195  from FP 16.32
196 
197  \param offset An optional offset in the data buffer from where to start
198  reading.
199  */
200  double getDataFP1632(const uint16_t offset = 0) const;
201  /*! \brief Return current data value as double, conversion depends on
202  outputSettings.
203 
204  \param outputSettings MT output settings
205  \param offset An optional offset in the data buffer from where to start
206  reading.
207  */
208  double getDataFPValue(
209  const uint64_t outputSettings, const uint16_t offset = 0) const;
210  /*! \brief Return current data values as double, conversion depends on
211  outputSetting.
212 
213  \param dest destination array
214  \param outputSettings MT output settings
215  \param offset offset in the data buffer from where to start reading.
216  \param numValues number of values to be read
217  */
218  void getDataFPValue(
219  double* dest, const uint64_t outputSettings, uint16_t offset,
220  const int16_t numValues) const;
221  /*! \brief Return the current value of the data as an uint32_t (32 bits).
222 
223  \param offset An optional offset in the data buffer from where to start
224  reading.
225  */
226  uint32_t getDataLong(const uint16_t offset = 0) const;
227  /*! \brief Return the current value of the data as an uint16_t (16 bits).
228 
229  \param offset An optional offset in the data buffer from where to start
230  reading.
231  */
232  uint16_t getDataShort(const uint16_t offset = 0) const;
233  //! Return the length of the data part of the message.
234  uint16_t getDataSize(void) const;
235  //! Return the current value of the m_messageId field.
236  uint8_t getMessageId(void) const { return m_buffer->m_messageId; }
237  /*! \brief Return the start of the message buffer.
238 
239  The function returns the address of the \c m_preamble member.
240  */
241  const uint8_t* getMessageStart(void) const { return (uint8_t*)m_buffer; }
242  /*! \brief Return the length of the message buffer.
243 
244  The function returns the total size of the message, including the
245  checksum. This
246  is in effect the number of bytes that would be transferred if the
247  message were to
248  be sent over a communications channel.
249  */
250  uint16_t getTotalMessageSize(void) const;
251  //! Compute the checksum and compare it with the stored checksum. Equal is
252  //! ok.
253  bool isChecksumOk(void) const;
254  /*! \brief Read the entire message from the given source string
255 
256  This is done through a simple memory copy. The number of bytes copied is
257  \c
258  m_createdLength.
259  \param source The source string containing message data
260  \param size The size of the source string
261  */
263  /*! \brief Compute the checksum field and fill it.
264 
265  The checksum field should normally be correct at all times, but if you
266  have
267  somehow managed to mess it up, this function can be used to recompute
268  it.
269  */
271  //! Resize the data area to the given size
272  void resizeData(const uint16_t newSize);
273  //! Set the new value of the m_busId field and update the checksum.
274  void setBusId(const uint8_t busId);
275  /*! \brief Write a string of bytes into the data buffer.
276 
277  \param data The data to write to the buffer.
278  \param offset An optional offset in the data buffer from where to
279  start writing.
280  \param count An optional number of bytes to write, if set to 0 (not
281  set), as
282  many bytes as will fit into the buffer from the given
283  point will
284  be written.
285  */
286  void setDataBuffer(
287  const uint8_t* data, const uint16_t offset = 0,
288  const uint16_t count = 0);
289  /*! \brief Write an unsigned byte (8 bits) into the data buffer.
290 
291  \param data The data to write to the buffer.
292  \param offset An optional offset in the data buffer from where to
293  start writing.
294  */
295  void setDataByte(const uint8_t data, const uint16_t offset = 0);
296  /*! \brief Write a double (64 bits) into the data buffer.
297 
298  \param data The data to write to the buffer.
299  \param offset An optional offset in the data buffer from where to start
300  writing.
301  */
302  void setDataDouble(const double data, const uint16_t offset = 0);
303  /*! \brief Write a float (32 bits) into the data buffer.
304 
305  \param data The data to write to the buffer.
306  \param offset An optional offset in the data buffer from where to start
307  writing.
308  */
309  void setDataFloat(const float data, const uint16_t offset = 0);
310  /*! \brief Write a double (64 bits) into the data buffer, after converting
311  it to F1220.
312 
313  \param data The data to write to the buffer.
314  \param offset An optional offset in the data buffer from where to
315  start writing.
316  */
317  void setDataF1220(const double data, const uint16_t offset = 0);
318  /*! \brief Write a double (64 bits) into the data buffer, after converting
319  it to FP1632.
320 
321  \param data The data to write to the buffer.
322  \param offset An optional offset in the data buffer from where to
323  start writing.
324  */
325  void setDataFP1632(const double data, const uint16_t offset = 0);
326  /*! \brief Write a floating/fixed point value into to the data buffer,
327  conversion depends on outputSettings
328 
329  \param outputSettings MT output settings
330  \param data The data to write to the buffer.
331  \param offset An optional offset in the data buffer from where to start
332  writing.
333  */
334  void setDataFPValue(
335  const uint64_t outputSettings, const double data,
336  const uint16_t offset = 0);
337  /*! \brief Write a floating/fixed point value into to the data buffer,
338  conversion depends on outputSettings
339 
340  \param outputSettings MT output settings
341  \param data The data array to be written to the buffer.
342  \param offset Offset in the data buffer from where to start writing.
343  \param numValues number of values to be written
344  */
345  void setDataFPValue(
346  const uint64_t outputSettings, const double* data, uint16_t offset,
347  const uint16_t numValues);
348  /*! \brief Write an uint32_t (32 bits) into the data buffer.
349 
350  \param data The data to write to the buffer.
351  \param offset An optional offset in the data buffer from where to start
352  writing.
353  */
354  void setDataLong(const uint32_t data, const uint16_t offset = 0);
355  /*! \brief Write an uint16_t (16 bits) into the data buffer.
356 
357  \param data The data to write to the buffer.
358  \param offset An optional offset in the data buffer from where to start
359  writing.
360  */
361  void setDataShort(const uint16_t data, const uint16_t offset = 0);
362  //! Set the new value of the m_messageId field and update the checksum.
363  void setMessageId(const uint8_t msgId);
364 
365  //! Copy message src into this
366  void operator=(const Message& src);
367 
368  //! Remove a number of bytes from the message (this will reduce the message
369  //! size)
371  //! Insert a number of bytes into the message (this will increase the
372  //! message size)
374 };
375 
376 } // end of xsens namespace
377 
378 #endif // _CMTMESSAGE_H_2006_05_24
uint32_t getDataLong(const uint16_t offset=0) const
Return the current value of the data as an uint32_t (32 bits).
Definition: cmtmessage.cpp:322
double getDataFPValue(const uint64_t outputSettings, const uint16_t offset=0) const
Return current data value as double, conversion depends on outputSettings.
Definition: cmtmessage.cpp:267
A message header.
Definition: cmtmessage.h:36
GLuint GLuint GLsizei count
Definition: glext.h:3528
void setDataFPValue(const uint64_t outputSettings, const double data, const uint16_t offset=0)
Write a floating/fixed point value into to the data buffer, conversion depends on outputSettings...
Definition: cmtmessage.cpp:639
const uint8_t * getDataBuffer(const uint16_t offset=0) const
Definition: cmtmessage.h:161
unsigned __int16 uint16_t
Definition: rptypes.h:44
#define CMT_MAXDATALEN
Definition: cmtdef.h:75
uint8_t * getDataStart(void) const
Internal function to get the start of the data buffer.
Definition: cmtmessage.cpp:365
uint8_t m_data[CMT_MAXDATALEN]
Definition: cmtmessage.h:50
void resizeData(const uint16_t newSize)
Resize the data area to the given size.
Definition: cmtmessage.cpp:424
#define CMT_CHECKVAR
Definition: cmtmessage.h:70
void operator=(const Message &src)
Copy message src into this.
Definition: cmtmessage.cpp:735
GLuint buffer
Definition: glext.h:3917
double getDataFP1632(const uint16_t offset=0) const
Return the current value of the data as a double, converting it from FP 16.32.
Definition: cmtmessage.cpp:239
void deleteData(uint16_t size, uint16_t offset=0)
Remove a number of bytes from the message (this will reduce the message size)
Definition: cmtmessage.cpp:752
GLintptr offset
Definition: glext.h:3925
double getDataF1220(const uint16_t offset=0) const
Return the current value of the data as a double, converting it from FP 12.20.
Definition: cmtmessage.cpp:197
#define CMT_MAXMSGLEN
Definition: cmtdef.h:77
uint8_t getBusId(void) const
Return the current value of the m_busId field.
Definition: cmtmessage.h:151
void clear(void)
Clear all data in the message.
Definition: cmtmessage.cpp:147
uint16_t getDataSize(void) const
Return the length of the data part of the message.
Definition: cmtmessage.cpp:352
void insertData(uint16_t size, uint16_t offset=0)
Insert a number of bytes into the message (this will increase the message size)
Definition: cmtmessage.cpp:775
GLuint src
Definition: glext.h:7278
~Message()
Destructor.
Definition: cmtmessage.cpp:138
uint16_t getDataShort(const uint16_t offset=0) const
Return the current value of the data as an uint16_t (16 bits).
Definition: cmtmessage.cpp:338
void recomputeChecksum(void)
Compute the checksum field and fill it.
Definition: cmtmessage.h:270
unsigned char uint8_t
Definition: rptypes.h:41
Message(const uint8_t msgId=0, const uint16_t length=0, const uint16_t maxLength=CMT_MAXMSGLEN)
Create a Message object with the given data length and message Id.
Definition: cmtmessage.cpp:41
CMT_CHECKVAR MessageHeader * m_buffer
The message header is the data buffer with interpretation.
Definition: cmtmessage.h:89
uint8_t computeChecksum(const uint8_t *buffer, uint32_t length)
Compute the checksum of the given byte string.
Definition: cmtmessage.cpp:789
__int16 int16_t
Definition: rptypes.h:43
float getDataFloat(const uint16_t offset=0) const
Return the current value of the data as a float (32 bits).
Definition: cmtmessage.cpp:180
XsensResultValue
Xsens return values.
Definition: xsens_std.h:31
void setDataFloat(const float data, const uint16_t offset=0)
Write a float (32 bits) into the data buffer.
Definition: cmtmessage.cpp:559
XsensResultValue loadFromString(const uint8_t *source, const uint16_t size)
Read the entire message from the given source string.
Definition: cmtmessage.cpp:398
void setDataLong(const uint32_t data, const uint16_t offset=0)
Write an uint32_t (32 bits) into the data buffer.
Definition: cmtmessage.cpp:690
void setDataDouble(const double data, const uint16_t offset=0)
Write a double (64 bits) into the data buffer.
Definition: cmtmessage.cpp:534
uint8_t * getDataBuffer(const uint16_t offset=0)
Return a pointer to the data buffer.
Definition: cmtmessage.h:157
unsigned __int64 uint64_t
Definition: rptypes.h:50
struct xsens::MessageHeader::_mdl::_mextd m_extended
uint8_t getMessageId(void) const
Return the current value of the m_messageId field.
Definition: cmtmessage.h:236
void setDataFP1632(const double data, const uint16_t offset=0)
Write a double (64 bits) into the data buffer, after converting it to FP1632.
Definition: cmtmessage.cpp:587
const uint8_t * getMessageStart(void) const
Return the start of the message buffer.
Definition: cmtmessage.h:241
uint8_t calcChecksum(void) const
Internal checksum computation.
Definition: cmtmessage.h:96
GLuint GLsizei GLsizei * length
Definition: glext.h:4064
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
uint8_t getDataByte(const uint16_t offset=0) const
Return the current value of the data as an unsigned byte (8 bits).
Definition: cmtmessage.h:171
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:95
void setDataBuffer(const uint8_t *data, const uint16_t offset=0, const uint16_t count=0)
Write a string of bytes into the data buffer.
Definition: cmtmessage.cpp:505
GLsizei maxLength
Definition: glext.h:4932
void setMessageId(const uint8_t msgId)
Set the new value of the m_messageId field and update the checksum.
Definition: cmtmessage.cpp:726
void setBusId(const uint8_t busId)
Set the new value of the m_busId field and update the checksum.
Definition: cmtmessage.cpp:496
GLsizeiptr size
Definition: glext.h:3923
struct xsens::MessageHeader::_mdl::_mextd::_mlen m_length
bool m_autoUpdateChecksum
Definition: cmtmessage.h:105
uint16_t getTotalMessageSize(void) const
Return the length of the message buffer.
Definition: cmtmessage.cpp:378
#define __attribute__(x)
Definition: rptypes.h:74
unsigned __int32 uint32_t
Definition: rptypes.h:47
bool isChecksumOk(void) const
Compute the checksum and compare it with the stored checksum.
Definition: cmtmessage.cpp:390
uint8_t * m_checksum
The checksum in the m_data or m_extendedData buffer.
Definition: cmtmessage.h:91
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3546
double getDataDouble(const uint16_t offset=0) const
Return the current value of the data as a double (64 bits).
Definition: cmtmessage.cpp:160
void setDataF1220(const double data, const uint16_t offset=0)
Write a double (64 bits) into the data buffer, after converting it to F1220.
Definition: cmtmessage.cpp:578
void setDataByte(const uint8_t data, const uint16_t offset=0)
Write an unsigned byte (8 bits) into the data buffer.
Definition: cmtmessage.cpp:523
uint32_t m_maxLength
The maximum size of the message, including header and footer.
Definition: cmtmessage.h:93
Class for storing a single message.
Definition: cmtmessage.h:83
union xsens::MessageHeader::_mdl m_datlen
void setDataShort(const uint16_t data, const uint16_t offset=0)
Write an uint16_t (16 bits) into the data buffer.
Definition: cmtmessage.cpp:710



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019