Main MRPT website > C++ reference for MRPT 1.5.7
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 aligned.
27 */
28 #ifdef _MSC_VER
29  #pragma pack(push, 1)
30 #endif
31 /*! \brief A message header.
32 
33  DO NOT REORDER THE MEMBERS!
34 */
35 struct MessageHeader {
40  union _mdl {
41  struct _mextd {
42  struct _mlen {
45  } m_length;
47  } m_extended;
49  } m_datlen;
50 }
51 // different alignment commands for gcc / MSVS
52 #ifdef _MSC_VER
53  ;
54  #pragma pack(pop)
55 #else
56  /*! \cond NODOXYGEN */ __attribute__((__packed__)) /*! \endcond */;
57 #endif
58 
59 #if defined(_DEBUG) && defined(_CMT_CHECK_MSG_DATA_INTEGRITY)
60  #define CMT_CHECKVAR uint32_t m_checkvar;
61  #define CMT_CHECKVAL 10101010
62  #define CMT_CHECKASSIGN m_checkvar = CMT_CHECKVAL;
63  #define CMT_CHECKASSERT if (m_checkvar != CMT_CHECKVAL) throw "Message assertion failed!";
64 #else
65  #define CMT_CHECKVAR
66  #define CMT_CHECKVAL
67  #define CMT_CHECKASSIGN
68  #define CMT_CHECKASSERT
69 #endif
70 
71 #define swapEndian16(src) (((src) >> 8) | ((src) << 8))
72 #define swapEndian32(src) (((src) >> 24) | ((src) >> 8 & 0xFF00) | ((src) << 8 & 0xFF0000) | ((src) << 24))
73 
74 //////////////////////////////////////////////////////////////////////////////////////////
75 //! \brief Class for storing a single message.
76 class Message {
77 protected:
79 
80  //! The message header is the data buffer with interpretation
82  //! The checksum in the m_data or m_extendedData buffer
84  //! The maximum size of the message, including header and footer
86 
87  //! Internal checksum computation
88  uint8_t calcChecksum(void) const
89  { return computeChecksum(((uint8_t*)m_buffer) + 1, getTotalMessageSize()-2); }
90  //! Internal function to get the start of the data buffer.
91  uint8_t* getDataStart(void) const;
92 
93 public:
95 
96  /*! \brief Create a Message object with the given data length and message Id.
97 
98  The function allocates enough memory to hold an entire message with the given
99  data length.
100  \param msgId The message Id that will be assigend to the m_messageId field.
101  \param length The length of the data in the message. This value is stored in
102  \c m_createdLength as well as \c m_length or \c m_extendedLength.
103  \param maxLength The maximum data length that can be stored in the structure.
104  */
105  Message(const uint8_t msgId = 0, const uint16_t length = 0, const uint16_t maxLength = CMT_MAXMSGLEN);
106 
107  /*! \brief Create a message from the given source string
108 
109  This is done through a simple memory copy. The number of bytes copied is taken
110  from the data in the message (so the message is interpreted first).
111  Note that this does NOT recompute the checksum, nor is it checked.
112 
113  \param source The source string containing message data
114  \param size The size of the source string
115  \param maxLength The maximum data length that can be stored in the structure.
116  */
118 
119  Message(const Message& src);
120 
121  //! Destructor
122  ~Message();
123 
124  //! Clear all data in the message
125  void clear(void);
126 
127  //! Return the current value of the m_busId field.
128  uint8_t getBusId(void) const { return m_buffer->m_busId; }
129  /*! \brief Return a pointer to the data buffer.
130 
131  \param offset An optional offset in the data buffer from where to start reading.
132  */
134  { return &((getDataStart())[offset]); }
135  const uint8_t* getDataBuffer(const uint16_t offset = 0) const
136  { return &((getDataStart())[offset]); }
137  /*! \brief Return the current value of the data as an unsigned byte (8 bits).
138 
139  \param offset An optional offset in the data buffer from where to start reading.
140  */
142  { return (getDataStart()[offset]); }
143  /*! \brief Return the current value of the data as a double (64 bits).
144 
145  \param offset An optional offset in the data buffer from where to start reading.
146  */
147  double getDataDouble(const uint16_t offset=0) const;
148  /*! \brief Return the current value of the data as a float (32 bits).
149 
150  \param offset An optional offset in the data buffer from where to start reading.
151  */
152  float getDataFloat(const uint16_t offset=0) const;
153  /*! \brief Return the current value of the data as a double, converting it from FP 12.20
154 
155  \param offset An optional offset in the data buffer from where to start reading.
156  */
157  double getDataF1220(const uint16_t offset=0) const;
158  /*! \brief Return the current value of the data as a double, converting it from FP 16.32
159 
160  \param offset An optional offset in the data buffer from where to start reading.
161  */
162  double getDataFP1632(const uint16_t offset=0) const;
163  /*! \brief Return current data value as double, conversion depends on outputSettings.
164 
165  \param outputSettings MT output settings
166  \param offset An optional offset in the data buffer from where to start reading.
167  */
168  double getDataFPValue(const uint64_t outputSettings, const uint16_t offset = 0) const;
169  /*! \brief Return current data values as double, conversion depends on outputSetting.
170 
171  \param dest destination array
172  \param outputSettings MT output settings
173  \param offset offset in the data buffer from where to start reading.
174  \param numValues number of values to be read
175  */
176  void getDataFPValue(double *dest, const uint64_t outputSettings, uint16_t offset, const int16_t numValues) const;
177  /*! \brief Return the current value of the data as an uint32_t (32 bits).
178 
179  \param offset An optional offset in the data buffer from where to start reading.
180  */
181  uint32_t getDataLong(const uint16_t offset=0) const;
182  /*! \brief Return the current value of the data as an uint16_t (16 bits).
183 
184  \param offset An optional offset in the data buffer from where to start reading.
185  */
186  uint16_t getDataShort(const uint16_t offset=0) const;
187  //! Return the length of the data part of the message.
188  uint16_t getDataSize(void) const;
189  //! Return the current value of the m_messageId field.
190  uint8_t getMessageId(void) const { return m_buffer->m_messageId; }
191  /*! \brief Return the start of the message buffer.
192 
193  The function returns the address of the \c m_preamble member.
194  */
195  const uint8_t* getMessageStart(void) const
196  { return (uint8_t*) m_buffer; }
197  /*! \brief Return the length of the message buffer.
198 
199  The function returns the total size of the message, including the checksum. This
200  is in effect the number of bytes that would be transferred if the message were to
201  be sent over a communications channel.
202  */
203  uint16_t getTotalMessageSize(void) const;
204  //! Compute the checksum and compare it with the stored checksum. Equal is ok.
205  bool isChecksumOk(void) const;
206  /*! \brief Read the entire message from the given source string
207 
208  This is done through a simple memory copy. The number of bytes copied is \c
209  m_createdLength.
210  \param source The source string containing message data
211  \param size The size of the source string
212  */
214  /*! \brief Compute the checksum field and fill it.
215 
216  The checksum field should normally be correct at all times, but if you have
217  somehow managed to mess it up, this function can be used to recompute it.
218  */
220  //! Resize the data area to the given size
221  void resizeData(const uint16_t newSize);
222  //! Set the new value of the m_busId field and update the checksum.
223  void setBusId(const uint8_t busId);
224  /*! \brief Write a string of bytes into the data buffer.
225 
226  \param data The data to write to the buffer.
227  \param offset An optional offset in the data buffer from where to start writing.
228  \param count An optional number of bytes to write, if set to 0 (not set), as
229  many bytes as will fit into the buffer from the given point will
230  be written.
231  */
232  void setDataBuffer(const uint8_t* data, const uint16_t offset = 0,
233  const uint16_t count = 0);
234  /*! \brief Write an unsigned byte (8 bits) into the data buffer.
235 
236  \param data The data to write to the buffer.
237  \param offset An optional offset in the data buffer from where to start writing.
238  */
239  void setDataByte(const uint8_t data, const uint16_t offset = 0);
240  /*! \brief Write a double (64 bits) into the data buffer.
241 
242  \param data The data to write to the buffer.
243  \param offset An optional offset in the data buffer from where to start writing.
244  */
245  void setDataDouble(const double data, const uint16_t offset=0);
246  /*! \brief Write a float (32 bits) into the data buffer.
247 
248  \param data The data to write to the buffer.
249  \param offset An optional offset in the data buffer from where to start writing.
250  */
251  void setDataFloat(const float data, const uint16_t offset = 0);
252  /*! \brief Write a double (64 bits) into the data buffer, after converting it to F1220.
253 
254  \param data The data to write to the buffer.
255  \param offset An optional offset in the data buffer from where to start writing.
256  */
257  void setDataF1220(const double data, const uint16_t offset = 0);
258  /*! \brief Write a double (64 bits) into the data buffer, after converting it to FP1632.
259 
260  \param data The data to write to the buffer.
261  \param offset An optional offset in the data buffer from where to start writing.
262  */
263  void setDataFP1632(const double data, const uint16_t offset = 0);
264  /*! \brief Write a floating/fixed point value into to the data buffer, conversion depends on outputSettings
265 
266  \param outputSettings MT output settings
267  \param data The data to write to the buffer.
268  \param offset An optional offset in the data buffer from where to start writing.
269  */
270  void setDataFPValue(const uint64_t outputSettings, const double data, const uint16_t offset = 0);
271  /*! \brief Write a floating/fixed point value into to the data buffer, conversion depends on outputSettings
272 
273  \param outputSettings MT output settings
274  \param data The data array to be written to the buffer.
275  \param offset Offset in the data buffer from where to start writing.
276  \param numValues number of values to be written
277  */
278  void setDataFPValue(const uint64_t outputSettings, const double *data, uint16_t offset, const uint16_t numValues);
279  /*! \brief Write an uint32_t (32 bits) into the data buffer.
280 
281  \param data The data to write to the buffer.
282  \param offset An optional offset in the data buffer from where to start writing.
283  */
284  void setDataLong(const uint32_t data, const uint16_t offset = 0);
285  /*! \brief Write an uint16_t (16 bits) into the data buffer.
286 
287  \param data The data to write to the buffer.
288  \param offset An optional offset in the data buffer from where to start writing.
289  */
290  void setDataShort(const uint16_t data, const uint16_t offset = 0);
291  //! Set the new value of the m_messageId field and update the checksum.
292  void setMessageId(const uint8_t msgId);
293 
294  //! Copy message src into this
295  void operator = (const Message& src);
296 
297  //! Remove a number of bytes from the message (this will reduce the message size)
299  //! Insert a number of bytes into the message (this will increase the message size)
301 };
302 
303 } // end of xsens namespace
304 
305 #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:296
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:245
A message header.
Definition: cmtmessage.h:35
GLuint GLuint GLsizei count
Definition: glext.h:3512
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:606
const uint8_t * getDataBuffer(const uint16_t offset=0) const
Definition: cmtmessage.h:135
unsigned __int16 uint16_t
Definition: rptypes.h:46
#define CMT_MAXDATALEN
Definition: cmtdef.h:73
uint8_t * getDataStart(void) const
Internal function to get the start of the data buffer.
Definition: cmtmessage.cpp:337
uint8_t m_data[CMT_MAXDATALEN]
Definition: cmtmessage.h:46
void resizeData(const uint16_t newSize)
Resize the data area to the given size.
Definition: cmtmessage.cpp:394
#define CMT_CHECKVAR
Definition: cmtmessage.h:65
void operator=(const Message &src)
Copy message src into this.
Definition: cmtmessage.cpp:699
GLuint buffer
Definition: glext.h:3775
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:217
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:716
GLintptr offset
Definition: glext.h:3780
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:180
#define CMT_MAXMSGLEN
Definition: cmtdef.h:75
uint8_t getBusId(void) const
Return the current value of the m_busId field.
Definition: cmtmessage.h:128
void clear(void)
Clear all data in the message.
Definition: cmtmessage.cpp:137
uint16_t getDataSize(void) const
Return the length of the data part of the message.
Definition: cmtmessage.cpp:326
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:741
GLuint src
Definition: glext.h:6303
~Message()
Destructor.
Definition: cmtmessage.cpp:128
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:312
void recomputeChecksum(void)
Compute the checksum field and fill it.
Definition: cmtmessage.h:219
unsigned char uint8_t
Definition: rptypes.h:43
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:81
uint8_t computeChecksum(const uint8_t *buffer, uint32_t length)
Compute the checksum of the given byte string.
Definition: cmtmessage.cpp:756
__int16 int16_t
Definition: rptypes.h:45
float getDataFloat(const uint16_t offset=0) const
Return the current value of the data as a float (32 bits).
Definition: cmtmessage.cpp:164
XsensResultValue
Xsens return values.
Definition: xsens_std.h:30
void setDataFloat(const float data, const uint16_t offset=0)
Write a float (32 bits) into the data buffer.
Definition: cmtmessage.cpp:524
XsensResultValue loadFromString(const uint8_t *source, const uint16_t size)
Read the entire message from the given source string.
Definition: cmtmessage.cpp:369
void setDataLong(const uint32_t data, const uint16_t offset=0)
Write an uint32_t (32 bits) into the data buffer.
Definition: cmtmessage.cpp:653
void setDataDouble(const double data, const uint16_t offset=0)
Write a double (64 bits) into the data buffer.
Definition: cmtmessage.cpp:504
uint8_t * getDataBuffer(const uint16_t offset=0)
Return a pointer to the data buffer.
Definition: cmtmessage.h:133
unsigned __int64 uint64_t
Definition: rptypes.h:52
struct xsens::MessageHeader::_mdl::_mextd m_extended
uint8_t getMessageId(void) const
Return the current value of the m_messageId field.
Definition: cmtmessage.h:190
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:552
const uint8_t * getMessageStart(void) const
Return the start of the message buffer.
Definition: cmtmessage.h:195
uint8_t calcChecksum(void) const
Internal checksum computation.
Definition: cmtmessage.h:88
GLuint GLsizei GLsizei * length
Definition: glext.h:3900
GLsizei GLsizei GLchar * source
Definition: glext.h:3908
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:141
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:92
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:471
GLsizei maxLength
Definition: glext.h:4504
void setMessageId(const uint8_t msgId)
Set the new value of the m_messageId field and update the checksum.
Definition: cmtmessage.cpp:689
void setBusId(const uint8_t busId)
Set the new value of the m_busId field and update the checksum.
Definition: cmtmessage.cpp:461
GLsizeiptr size
Definition: glext.h:3779
struct xsens::MessageHeader::_mdl::_mextd::_mlen m_length
bool m_autoUpdateChecksum
Definition: cmtmessage.h:94
uint16_t getTotalMessageSize(void) const
Return the length of the message buffer.
Definition: cmtmessage.cpp:350
#define __attribute__(x)
Definition: rptypes.h:77
unsigned __int32 uint32_t
Definition: rptypes.h:49
bool isChecksumOk(void) const
Compute the checksum and compare it with the stored checksum. Equal is ok.
Definition: cmtmessage.cpp:360
uint8_t * m_checksum
The checksum in the m_data or m_extendedData buffer.
Definition: cmtmessage.h:83
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
double getDataDouble(const uint16_t offset=0) const
Return the current value of the data as a double (64 bits).
Definition: cmtmessage.cpp:150
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:544
void setDataByte(const uint8_t data, const uint16_t offset=0)
Write an unsigned byte (8 bits) into the data buffer.
Definition: cmtmessage.cpp:491
uint32_t m_maxLength
The maximum size of the message, including header and footer.
Definition: cmtmessage.h:85
Class for storing a single message.
Definition: cmtmessage.h:76
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:672



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019