Debian Codegraph @

3rdparty/xspublic/xstypes/xsmessage.h

raw
1
2 // Copyright (c) 2003-2019 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32
33 #ifndef XSMESSAGE_H
34 #define XSMESSAGE_H
35
36 #include "xstypesconfig.h"
37 #include "pstdint.h"
38 #include "xsbytearray.h"
39 #include "xsdataidentifier.h"
40 #include "xsxbusmessageid.h"
41 #include "xsstring.h"
42 #include "xsresultvalue.h"
43 #include "xsbusid.h"
44
45 struct XsMessage;
46 struct XsMessageHeader;
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 #ifndef __cplusplus
52 #define XSMESSAGE_INITIALIZER { XSBYTEARRAY_INITIALIZER, 1, 0 }
53 typedef struct XsMessage XsMessage;
54 typedef struct XsMessageHeader XsMessageHeader;
55 #endif
56
57 XSTYPES_DLL_API void XsMessage_construct(XsMessage* thisPtr);
58 XSTYPES_DLL_API void XsMessage_constructSized(XsMessage* thisPtr, XsSize dataSize);
59 XSTYPES_DLL_API void XsMessage_copyConstruct(XsMessage* thisPtr, XsMessage const* src);
60 XSTYPES_DLL_API void XsMessage_assign(XsMessage* thisPtr, XsSize dataSize);
61 XSTYPES_DLL_API void XsMessage_load(XsMessage* thisPtr, XsSize msgSize, unsigned char const* src);
62 XSTYPES_DLL_API void XsMessage_destruct(XsMessage* thisPtr);
63 XSTYPES_DLL_API void XsMessage_copy(XsMessage* copy, XsMessage const* src);
64 XSTYPES_DLL_API void XsMessage_swap(XsMessage* a, XsMessage* b);
65 XSTYPES_DLL_API XsSize XsMessage_dataSize(XsMessage const* thisPtr);
66 XSTYPES_DLL_API const uint8_t* XsMessage_constData(XsMessage const* thisPtr, XsSize offset);
67 XSTYPES_DLL_API const uint8_t* XsMessage_getMessageStart(XsMessage const* thisPtr);
68 XSTYPES_DLL_API XsSize XsMessage_getTotalMessageSize(XsMessage const* thisPtr);
69 XSTYPES_DLL_API uint8_t XsMessage_getDataByte(XsMessage const* thisPtr, XsSize offset);
70 XSTYPES_DLL_API uint16_t XsMessage_getDataShort(XsMessage const* thisPtr, XsSize offset);
71 XSTYPES_DLL_API uint32_t XsMessage_getDataLong(XsMessage const* thisPtr, XsSize offset);
72 XSTYPES_DLL_API uint64_t XsMessage_getDataLongLong(XsMessage const* thisPtr, XsSize offset);
73 XSTYPES_DLL_API float XsMessage_getDataFloat(XsMessage const* thisPtr, XsSize offset);
74 XSTYPES_DLL_API double XsMessage_getDataDouble(XsMessage const* thisPtr, XsSize offset);
75 XSTYPES_DLL_API double XsMessage_getDataF1220(XsMessage const* thisPtr, XsSize offset);
76 XSTYPES_DLL_API double XsMessage_getDataFP1632(XsMessage const* thisPtr, XsSize offset);
77 XSTYPES_DLL_API const uint8_t* XsMessage_getDataBuffer(XsMessage const* thisPtr, XsSize offset);
78 XSTYPES_DLL_API void XsMessage_setDataByte(XsMessage* thisPtr, uint8_t value, XsSize offset);
79 XSTYPES_DLL_API void XsMessage_setDataShort(XsMessage* thisPtr, uint16_t value, XsSize offset);
80 XSTYPES_DLL_API void XsMessage_setDataLong(XsMessage* thisPtr, uint32_t value, XsSize offset);
81 XSTYPES_DLL_API void XsMessage_setDataLongLong(XsMessage* thisPtr, uint64_t value, XsSize offset);
82 XSTYPES_DLL_API void XsMessage_setDataFloat(XsMessage* thisPtr, float value, XsSize offset);
83 XSTYPES_DLL_API void XsMessage_setDataDouble(XsMessage* thisPtr, double value, XsSize offset);
84 XSTYPES_DLL_API void XsMessage_setDataF1220(XsMessage* thisPtr, double value, XsSize offset);
85 XSTYPES_DLL_API void XsMessage_setDataFP1632(XsMessage* thisPtr, double value, XsSize offset);
86 XSTYPES_DLL_API void XsMessage_setDataBuffer(XsMessage* thisPtr, const uint8_t* buffer, XsSize size, XsSize offset);
87 XSTYPES_DLL_API uint8_t XsMessage_computeChecksum(XsMessage const* thisPtr);
88 XSTYPES_DLL_API void XsMessage_recomputeChecksum(XsMessage* thisPtr);
89 XSTYPES_DLL_API int XsMessage_isChecksumOk(XsMessage const* thisPtr);
90 XSTYPES_DLL_API XsMessageHeader* XsMessage_getHeader(XsMessage* );
91 XSTYPES_DLL_API const XsMessageHeader* XsMessage_getConstHeader(XsMessage const* thisPtr);
92 XSTYPES_DLL_API int XsMessage_empty(XsMessage const* thisPtr);
93 XSTYPES_DLL_API void XsMessage_resizeData(XsMessage* thisPtr, XsSize newSize);
94 XSTYPES_DLL_API void XsMessage_setBusId(XsMessage* thisPtr, uint8_t busId);
95 XSTYPES_DLL_API void XsMessage_setMessageId(XsMessage* thisPtr, XsXbusMessageId msgId);
96 XSTYPES_DLL_API void XsMessage_insertData(XsMessage* thisPtr, XsSize count, XsSize offset);
97 XSTYPES_DLL_API void XsMessage_deleteData(XsMessage* thisPtr, XsSize count, XsSize offset);
98 XSTYPES_DLL_API uint8_t XsMessage_getFPValueSize(XsDataIdentifier id);
99 XSTYPES_DLL_API void XsMessage_getDataFPValuesById(XsMessage const* thisPtr, XsDataIdentifier dataIdentifier, double *dest, XsSize offset, XsSize numValues);
100 XSTYPES_DLL_API void XsMessage_setDataFPValuesById(XsMessage* thisPtr, XsDataIdentifier dataIdentifier, double const *data, XsSize offset, XsSize numValues);
101 XSTYPES_DLL_API void XsMessage_getDataRealValuesById(XsMessage const* thisPtr, XsDataIdentifier dataIdentifier, XsReal *dest, XsSize offset, XsSize numValues);
102 XSTYPES_DLL_API void XsMessage_setDataRealValuesById(XsMessage* thisPtr, XsDataIdentifier dataIdentifier, XsReal const *data, XsSize offset, XsSize numValues);
103 XSTYPES_DLL_API int XsMessage_compare(XsMessage const* a, XsMessage const* b);
104 XSTYPES_DLL_API void XsMessage_toHexString(XsMessage const* thisPtr, XsSize maxBytes, XsString* resultValue);
105 XSTYPES_DLL_API void XsMessage_getEndianCorrectData(XsMessage const* thisPtr, void *value, XsSize size, XsSize offset);
106 XSTYPES_DLL_API void XsMessage_setEndianCorrectData(XsMessage *thisPtr, void const *value, XsSize size, XsSize offset);
107
108 #ifdef __cplusplus
109 } // extern "C"
110 #endif
111
112 #define XS_PREAMBLE 0xFA
113 #define XS_EXTLENCODE 0xFF
114
115 #define XS_LEN_MSGHEADER 4
116 #define XS_LEN_MSGEXTHEADER 6
117 #define XS_LEN_MSGHEADERCS 5
118 #define XS_LEN_MSGEXTHEADERCS 7
119 #define XS_LEN_CHECKSUM 1
120 #define XS_LEN_UNSIGSHORT 2
121 #define XS_LEN_UNSIGINT 4
122 #define XS_LEN_FLOAT 4
123
124 // Maximum message/data length
125 #define XS_MAXDATALEN (8192-XS_LEN_MSGEXTHEADERCS)
126 #define XS_MAXSHORTDATALEN 254
127 #define XS_MAXMSGLEN (XS_MAXDATALEN+XS_LEN_MSGEXTHEADERCS)
128 #define XS_MAXSHORTMSGLEN (XS_MAXSHORTDATALEN+XS_LEN_MSGHEADERCS)
129 #define XS_MAXGARBAGE (XS_MAXMSGLEN+1)
130
131 //////////////////////////////////////////////////////////////////////////////////////////
132 // different alignment commands for gcc / MSVS, the structure needs to be 1-byte aligned.
133 #ifdef _MSC_VER
134 #pragma pack(push, 1)
135 #ifndef PACK_POST
136 #define PACK_POST
137 #endif
138 #else
139 #ifndef PACK_POST
140 #define PACK_POST __attribute__((__packed__))
141 #endif
142 #endif
143 /*! \brief A message header
144 \details This structure is used to interpret the header of a message.
145 */
146 struct XsMessageHeader {
147 uint8_t m_preamble; //!< \brief The message preamble (always 0xFA)
148 uint8_t m_busId; //!< \brief The bus ID \sa XS_BID_MASTER XS_BID_BROADCAST XS_BID_MT
149 uint8_t m_messageId; //!< \brief The message ID \sa XsXbusMessageId
150 uint8_t m_length; //!< \brief The length of the message \details A length of 255 means extended length is used
151 //! \brief Contains optional extended length of message and first byte of data buffer
152 union LengthData {
153 //! \brief Contains extended length information and first byte of data buffer if normal length is 255
154 struct ExtendedLength {
155 //! \brief The high and low byte of the extended length
156 struct ExtendedParts {
157 uint8_t m_high; //!< \brief High byte of extended length
158 uint8_t m_low; //!< \brief Low byte of extended length
159 } m_length; //!< \brief Extended length, only valid if normal length is 255
160 uint8_t m_data[1]; //!< \brief The first byte of the data buffer, the data buffer is always at least 1 byte since it has to contain the checksum, but it can be bigger.
161 } m_extended; //!< \brief The extended length, only valid if normal length is 255
162 uint8_t m_data[1]; //!< \brief The first byte of the data buffer if length < 255, the data buffer is always at least 1 byte since it has to contain the checksum, but it can be bigger.
163 } m_datlen; //!< \brief Data or length and data
164 #ifdef SWIG
165 };
166 #else
167 } PACK_POST;
168 #endif
169
170 #ifdef _MSC_VER
171 #pragma pack(pop)
172 #endif
173
174 //////////////////////////////////////////////////////////////////////////////////////////
175 //! \brief Structure for storing a single message.
176 struct XsMessage {
177 #ifdef __cplusplus
178 /*! \brief Create a XsMessage object with the given data length and message Id.
179
180 The function allocates enough memory to hold an entire message with the given
181 data length.
182 \param msgId The message Id that will be assigned to the m_messageId field.
183 \param dataLength The length of the data in the message.
184 */
185 explicit XsMessage(XsXbusMessageId msgId = XMID_InvalidMessage, XsSize dataLength = 0)
186 : m_autoUpdateChecksum(1)
187 , m_checksum(0)
188 {
189 XsMessage_constructSized(this, dataLength);
190 XsMessage_setMessageId(this, msgId);
191 }
192
193 /*! \brief Create a message from the given source byte array
194
195 This is done through a simple memory copy. The number of bytes copied is taken
196 from the data in the message (so the message is interpreted first).
197 Note that this does NOT recompute the checksum, nor is it checked.
198
199 \param source The source byte array containing message data
200 \param size The size of the source string
201 */
202 XsMessage(const uint8_t* source, XsSize size)
203 : m_autoUpdateChecksum(1)
204 , m_checksum(0)
205 {
206 XsMessage_load(this, size, source);
207 }
208
209 /*! \brief Create a message from the given source string
210
211 The string is interpreted as hex-encoded bytes. Example to request device ID: XsMessage("FAFF0000", true).
212
213 \param source The source string containing the full message.
214 \param computeChecksum When set to true (default), the checksum will be automatically computed and added or updated. When set to false, a valid checksum is assumed to be included in \a source.
215 */
216 XsMessage(const XsString& source, bool computeChecksum = true)
217 : m_autoUpdateChecksum(1)
218 , m_checksum(0)
219 {
220 XsSize szm = source.size()/2;
221 XsSize szmcc = szm + (computeChecksum ? 1 : 0);
222 XsByteArray tmp(szmcc);
223 auto tonibble = [](char a) -> uint8_t
224 {
225 if (a >= '0' && a <= '9')
226 return (uint8_t) (a-'0');
227 if (a >= 'a' && a <= 'f')
228 return (uint8_t) (10+a-'a');
229 if (a >= 'A' && a <= 'F')
230 return (uint8_t) (10+a-'A');
231 return 0;
232 };
233 for (XsSize i = 0; i < szm; ++i)
234 tmp[i] = tonibble(source[i*2])*(uint8_t)16+tonibble(source[i*2+1]); //lint !e734
235 XsMessage_load(this, tmp.size(), tmp.data());
236 if (computeChecksum)
237 XsMessage_recomputeChecksum(this);
238 }
239
240 //! \brief Copy constructor
241 XsMessage(const XsMessage& src)
242 : m_message(src.m_message)
243 , m_autoUpdateChecksum(src.m_autoUpdateChecksum)
244 , m_checksum(0)
245 {
246 updateChecksumPtr();
247 }
248
249 //! Destroy the message
250 ~XsMessage()
251 {
252 XsMessage_destruct(this);
253 }
254
255 //! \brief Clear all data in the message
256 void clear(void)
257 {
258 XsMessage_destruct(this);
259 }
260
261 /*! \brief Test if this message is empty
262
263 \returns true if this message is empty, false otherwise
264 */
265 bool empty(void) const
266 {
267 return 0 != XsMessage_empty(this);
268 }
269
270 //! Return the busId header field.
271 uint8_t getBusId(void) const
272 {
273 const XsMessageHeader* hdr = XsMessage_getConstHeader(this);
274 if (!hdr)
275 return 0;
276 return hdr->m_busId;
277 }
278
279 /*! \copydoc XsMessage_constData
280 */
281 const uint8_t* getDataBuffer(XsSize offset = 0) const
282 {
283 return XsMessage_constData(this, offset);
284 }
285
286 /*! \copydoc XsMessage_getDataByte
287 */
288 uint8_t getDataByte(XsSize offset = 0) const
289 {
290 return XsMessage_getDataByte(this, offset);
291 }
292
293 /*! \copydoc XsMessage_getDataDouble
294 */
295 double getDataDouble(XsSize offset = 0) const
296 {
297 return XsMessage_getDataDouble(this, offset);
298 }
299
300 /*! \copydoc XsMessage_getDataFloat
301 */
302 float getDataFloat(XsSize offset = 0) const
303 {
304 return XsMessage_getDataFloat(this, offset);
305 }
306
307 /*! \copydoc XsMessage_getDataF1220
308 */
309 double getDataF1220(XsSize offset = 0) const
310 {
311 return XsMessage_getDataF1220(this, offset);
312 }
313
314 /*! \copydoc XsMessage_getDataFP1632
315 */
316 double getDataFP1632(XsSize offset = 0) const
317 {
318 return XsMessage_getDataFP1632(this, offset);
319 }
320
321 /*! \copydoc XsMessage_getDataLong
322 */
323 uint32_t getDataLong(XsSize offset = 0) const
324 {
325 return XsMessage_getDataLong(this, offset);
326 }
327 /*! \copydoc XsMessage_getDataLongLong
328 */
329 uint64_t getDataLongLong(XsSize offset = 0) const
330 {
331 return XsMessage_getDataLongLong(this, offset);
332 }
333
334 /*! \copydoc XsMessage_getDataShort
335 */
336 uint16_t getDataShort(XsSize offset = 0) const
337 {
338 return XsMessage_getDataShort(this, offset);
339 }
340
341 /*! \copydoc XsMessage_dataSize
342 */
343 XsSize getDataSize(void) const
344 {
345 return XsMessage_dataSize(this);
346 }
347
348 //! Return the current value of the m_messageId field.
349 XsXbusMessageId getMessageId(void) const
350 {
351 const XsMessageHeader* hdr = XsMessage_getConstHeader(this);
352 if (!hdr)
353 return XMID_InvalidMessage;
354 return (XsXbusMessageId) hdr->m_messageId;
355 }
356
357 //! Returns XRV_OK if this is a normal message or an error code if the message was an XMID_Error message or XRV_NULLPTR if the message is invalid / empty
358 inline XsResultValue toResultValue(void) const
359 {
360 const XsMessageHeader* hdr = XsMessage_getConstHeader(this);
361 if (!hdr)
362 return XRV_NULLPTR;
363 if (hdr->m_messageId == 0 && hdr->m_busId == XS_BID_MASTER)
364 return XRV_TIMEOUTNODATA; // we assume that an empty message indicates a timeout
365 if ((XsXbusMessageId) hdr->m_messageId != XMID_Error)
366 return XRV_OK;
367 return (XsResultValue) getDataByte();
368 }
369
370 /*! \copydoc XsMessage_getMessageStart
371 */
372 const uint8_t* getMessageStart(void) const
373 {
374 return XsMessage_getMessageStart(this);
375 }
376
377 /*! \copydoc XsMessage_getTotalMessageSize
378 */
379 XsSize getTotalMessageSize(void) const
380 {
381 return XsMessage_getTotalMessageSize(this);
382 }
383
384 /*! \copydoc XsMessage_isChecksumOk
385 */
386 bool isChecksumOk(void) const
387 {
388 return 0 != XsMessage_isChecksumOk(this);
389 }
390
391 /*! \brief Initialize the %XsMessage with the data from \a src
392
393 \param msgSize the size of the data pointed to by src
394 \param src the data to load the message from
395
396 \returns true if the checksum of the loaded message is OK.
397 */
398 bool loadFromString(const uint8_t* src, XsSize msgSize)
399 {
400 XsArray_destruct(&m_message);
401 XsMessage_load(this, msgSize, src);
402 return isChecksumOk();
403 }
404
405 /*! \copydoc XsMessage_recomputeChecksum
406 */
407 void recomputeChecksum(void)
408 {
409 XsMessage_recomputeChecksum(this);
410 }
411
412 /*! \copydoc XsMessage_resizeData
413 */
414 void resizeData(XsSize newSize)
415 {
416 XsMessage_resizeData(this, newSize);
417 }
418
419 /*! \copydoc XsMessage_setBusId
420 */
421 void setBusId(uint8_t busId)
422 {
423 XsMessage_setBusId(this, busId);
424 }
425
426 /*! \copydoc XsMessage_setDataBuffer
427 */
428 void setDataBuffer(const uint8_t* buffer, XsSize size, XsSize offset = 0)
429 {
430 XsMessage_setDataBuffer(this, buffer, size, offset);
431 }
432
433 /*! \copydoc XsMessage_setDataByte
434 */
435 void setDataByte(const uint8_t value, XsSize offset = 0)
436 {
437 XsMessage_setDataByte(this, value, offset);
438 }
439
440 /*! \copydoc XsMessage_setDataDouble
441 */
442 void setDataDouble(const double value, XsSize offset=0)
443 {
444 XsMessage_setDataDouble(this, value, offset);
445 }
446
447 /*! \copydoc XsMessage_setDataFloat
448 */
449 void setDataFloat(const float value, XsSize offset = 0)
450 {
451 XsMessage_setDataFloat(this, value, offset);
452 }
453
454 /*! \copydoc XsMessage_setDataF1220
455 */
456 void setDataF1220(const double value, XsSize offset = 0)
457 {
458 XsMessage_setDataF1220(this, value, offset);
459 }
460
461 /*! \copydoc XsMessage_setDataFP1632
462 */
463 void setDataFP1632(const double value, XsSize offset = 0)
464 {
465 XsMessage_setDataFP1632(this, value, offset);
466 }
467
468 /*! \copydoc XsMessage_setDataLong
469 */
470 void setDataLong(const uint32_t value, XsSize offset = 0)
471 {
472 XsMessage_setDataLong(this, value, offset);
473 }
474 /*! \copydoc XsMessage_setDataLongLong
475 */
476 void setDataLongLong(const uint64_t value, XsSize offset = 0)
477 {
478 XsMessage_setDataLongLong(this, value, offset);
479 }
480
481 /*! \copydoc XsMessage_setDataShort
482 */
483 void setDataShort(const uint16_t value, XsSize offset = 0)
484 {
485 XsMessage_setDataShort(this, value, offset);
486 }
487
488 /*! \copydoc XsMessage_setMessageId
489 */
490 void setMessageId(const XsXbusMessageId msgId)
491 {
492 XsMessage_setMessageId(this, msgId);
493 }
494
495 //! Copy message src into this
496 XsMessage& operator = (const XsMessage& src)
497 {
498 if (this != &src)
499 XsMessage_copy(this, &src);
500 return *this;
501 }
502
503 /*! \copydoc XsMessage_deleteData */
504 void deleteData(XsSize count, XsSize offset = 0)
505 {
506 XsMessage_deleteData(this, count, offset);
507 }
508
509 /*! \copydoc XsMessage_insertData */
510 void insertData(XsSize count, XsSize offset = 0)
511 {
512 XsMessage_insertData(this, count, offset);
513 }
514
515 /*! \copydoc XsMessage_getFPValueSize */
516 inline static uint8_t getFPValueSize(XsDataIdentifier id)
517 {
518 return XsMessage_getFPValueSize(id);
519 }
520
521 /*! \copydoc XsMessage_getDataFPValuesById */
522 void getDataFPValue(XsDataIdentifier dataIdentifier, double *dest, XsSize offset = 0, XsSize numValues = 1) const
523 {
524 XsMessage_getDataFPValuesById(this, dataIdentifier, dest, offset, numValues);
525 }
526
527 /*! \brief Return current data values as double, conversion depends on outputSetting
528
529 \param dataIdentifier Data identifier containing data precision
530 \param offset offset in the data buffer from where to start reading.
531
532 \returns the current data value as double
533 */
534 double getDataFPValue(XsDataIdentifier dataIdentifier, XsSize offset = 0) const
535 {
536 double tmp;
537 XsMessage_getDataFPValuesById(this, dataIdentifier, &tmp, offset, 1);
538 return tmp;
539 }
540
541 /*! \copydoc XsMessage_setDataFPValuesById */
542 void setDataFPValue(XsDataIdentifier dataIdentifier, const double *data, XsSize offset = 0, XsSize numValues = 1)
543 {
544 XsMessage_setDataFPValuesById(this, dataIdentifier, data, offset, numValues);
545 }
546
547 /*! \brief Write a floating/fixed point value into to the data buffer, conversion depends on outputSettings
548
549 \param dataIdentifier Data Identifier
550 \param data The data array to be written to the buffer.
551 \param offset Offset in the data buffer from where to start writing.
552 */
553 void setDataFPValue(XsDataIdentifier dataIdentifier, double data, XsSize offset = 0)
554 {
555 XsMessage_setDataFPValuesById(this, dataIdentifier, &data, offset, 1);
556 }
557
558 /*! \brief Return true if \a other is identical to this */
559 inline bool operator == (const XsMessage& other) const
560 {
561 if (this == &other)
562 return true;
563 return m_message == other.m_message;
564 }
565
566 /*! \brief Return the message in its raw form */
567 inline XsByteArray const& rawMessage() const
568 {
569 return m_message;
570 };
571
572 /*! \brief Return a string containing the first \a maxBytes bytes of the message in hex format
573 \param maxBytes the maximum number of bytes to include in the string, when set to 0, the full message will be used
574 \return A string containing the first \a maxBytes bytes of the message in hex format
575 */
576 inline XsString toHexString(XsSize maxBytes = 0) const
577 {
578 XsString rv;
579 XsMessage_toHexString(this, maxBytes, &rv);
580 return rv;
581 }
582
583 /*! \brief Get data of type T from the message
584 \details This function can be used to get data of (simple) type T from the message.
585 \param data A pointer to a buffer with space for at least \a numValues of T
586 \param id Ignored for this function, but added here for generalization purposes
587 \param offset The offset of the first byte of the data in the message
588 \param numValues The number of consecutive values to read
589 */
590 template <typename T>
591 void getData(T* data, XsDataIdentifier id, XsSize offset = 0, int numValues = 1) const
592 {
593 (void) id;
594 for (int i = 0; i < numValues; ++i)
595 XsMessage_getEndianCorrectData(this, &data[i], sizeof(T), offset+((unsigned int)i)*sizeof(T));
596 }
597
598 /*! \brief Write data of type T to the message
599 \details This function can be used to write data of (simple) type T to the message.
600 \param data A pointer to a buffer with space for at least \a numValues of T
601 \param id Ignored for this function, but added here for generalization purposes
602 \param offset The offset of the first byte of the data in the message
603 \param numValues The number of consecutive values to write
604 */
605 template <typename T>
606 void setData(T const* data, XsDataIdentifier id, XsSize offset = 0, int numValues = 1)
607 {
608 (void) id;
609 for (int i = 0; i < numValues; ++i)
610 XsMessage_setEndianCorrectData(this, &data[i], sizeof(T), offset+((unsigned int)i)*sizeof(T));
611 }
612
613 /*! \brief Return the number of bytes that \a numValues items of type T will require in a message
614 \param id Ignored for this function, but added here for generalization purposes
615 \param numValues The desired number of items, used as a simple multiplier
616 \return The number of bytes that \a numValues items of type T will require in a message
617 */
618 template <typename T>
619 static int sizeInMsg(XsDataIdentifier id, int numValues = 1)
620 {
621 (void) id;
622 return numValues * (int) sizeof(T);
623 }
624
625 private:
626 /*! \brief Update the checksum pointer after changing the size of the message */
627 void updateChecksumPtr()
628 {
629 XsSize sz = XsMessage_getTotalMessageSize(this);
630 if (sz)
631 *const_cast<uint8_t**>(&m_checksum) = &m_message[sz-1];
632 else
633 *const_cast<uint8_t**>(&m_checksum) = 0;
634 }
635
636 #endif
637
638 XsByteArray m_message;
639 int m_autoUpdateChecksum;
640 uint8_t* const m_checksum; //! Points to the checksum to speed up automatic checksum updates
641 };
642
643 #ifdef __cplusplus
644 /*! \brief Get 'real' data from the message
645 \details This function can be used to get 'real' data from the message, possibly converting from an
646 underlying fixed or floating point type if necessary.
647 \param data A pointer to a buffer with space for at least \a numValues doubles
648 \param id The dataidentifer that will be used to determine the underlying data type
649 \param offset The offset of the first byte of the data in the message
650 \param numValues The number of consecutive values to read
651 */
652 template <>
653 inline void XsMessage::getData<double>(double* data, XsDataIdentifier id, XsSize offset, int numValues) const
654 {
655 getDataFPValue(id, data, offset, (XsSize)(ptrdiff_t)numValues);
656 }
657
658 /*! \brief Write 'real' data from the message
659 \details This function can be used to write 'real' data to the message, possibly converting to an
660 underlying fixed or floating point type if necessary.
661 \param data A pointer to a buffer with space for at least \a numValues doubles
662 \param id The dataidentifer that will be used to determine the underlying data type
663 \param offset The offset of the first byte of the data in the message
664 \param numValues The number of consecutive values to write
665 */
666 template <>
667 inline void XsMessage::setData<double>(double const* data, XsDataIdentifier id, XsSize offset, int numValues)
668 {
669 setDataFPValue(id, data, offset, (XsSize)(ptrdiff_t)numValues);
670 }
671
672 /*! \brief Return the number of bytes that \a numValues 'real' items will require in a message
673 \param id Specifies the desired underlying type to use
674 \param numValues The desired number of items, used as a simple multiplier
675 \return The number of bytes that \a numValues 'real' items will require in a message
676 */
677 template <>
678 inline int XsMessage::sizeInMsg<XsReal>(XsDataIdentifier id, int numValues)
679 {
680 return XsMessage_getFPValueSize(id) * numValues;
681 }
682 #endif
683
684 // some macros to help when constructing/parsing messages
685 #define swapEndian16(src) (((uint16_t)(src) >> 8) | ((uint16_t)(src) << 8))
686 #define swapEndian32(src) (((uint32_t)(src) >> 24) | (((uint32_t)(src) >> 8) & 0xFF00) | (((uint32_t)(src) << 8) & 0xFF0000) | ((uint32_t)(src) << 24))
687 #define swapEndian64(src) (((src >> 56) & 0xFFULL) | ((src >> 40) & 0xFF00ULL) | ((src >> 24) & 0xFF0000ULL) | ((src >> 8) & 0xFF000000ULL) | ((src << 8) & 0xFF00000000ULL) | ((src << 24) & 0xFF0000000000ULL) | ((src << 40) & 0xFF000000000000ULL) | ((src << 56)))
688
689 #endif // file guard