Debian Codegraph @

3rdparty/xspublic/xstypes/datapacket_p.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 DATAPACKET_P_H
34 #define DATAPACKET_P_H
35
36 #include "xsmessage.h"
37 #include "xsdeviceid.h"
38 #include "xstimestamp.h"
39 #include <map>
40 #include <atomic>
41 #include "xsquaternion.h"
42 #include "xsushortvector.h"
43 #include "xsvector3.h"
44 #include "xsscrdata.h"
45 #include "xstriggerindicationdata.h"
46 #include "xseuler.h"
47 #include "xsmatrix3x3.h"
48 #include "xsrange.h"
49 #include "xstimeinfo.h"
50 #include "xsrawgnsspvtdata.h"
51 #include "xsrawgnsssatinfo.h"
52 #include "xsbytearray.h"
53 #include "xsglovesnapshot.h"
54 #include "xsglovedata.h"
55
56 #include "xssnapshot.h"
57
58 /*! \cond XS_INTERNAL */
59 namespace XsDataPacket_Private {
60 /*! \brief Abstract Variant class for handling contents of XsDataPacket */
61 class Variant {
62 public:
63 /*! \brief Constructor, sets the dataId to \a id */
64 Variant(XsDataIdentifier id) : m_id(id) {}
65 virtual ~Variant() {}
66 /*! \brief Read the data from message \a msg at \a offset and optionally using \a dSize */
67 virtual XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize dSize) = 0;
68 /*! \brief Write the data to message \a msg at \a offset */
69 virtual void writeToMessage(XsMessage& msg, XsSize offset) const = 0;
70 /*! \brief Return the size the Variant would have in a message */
71 virtual XsSize sizeInMsg() const = 0;
72 /*! \brief Create a copy of the Variant
73 \return A pointer to the newly created Variant
74 \note This needs to be reimplemented in each subclass!
75 */
76 virtual Variant* clone() const = 0;
77
78 /*! \brief Set the dataId to \a id */
79 void setDataId(XsDataIdentifier id)
80 {
81 assert((m_id & XDI_FullTypeMask) == (id & XDI_FullTypeMask));
82 m_id = id;
83 }
84 /*! \brief Return the dataId of the Variant */
85 XsDataIdentifier dataId() const { return m_id; }
86
87 /*! \brief Convert the Variant to a derived type if allowed, returns nullptr otherwise (and asserts in debug builds) */
88 template <typename U>
89 U& toDerived()
90 {
91 U* ptr = dynamic_cast<U*>(this);
92 assert(ptr);
93 return *ptr;
94 }
95
96 /*! \brief Convert the Variant to a derived type if allowed, returns nullptr otherwise (and asserts in debug builds) */
97 template <typename U>
98 U const& toDerived() const
99 {
100 U const* ptr = dynamic_cast<U const*>(this);
101 assert(ptr);
102 return *ptr;
103 }
104
105 private:
106 XsDataIdentifier m_id;
107 };
108
109 /*! \brief Generic base class template for Variant subclasses */
110 template <typename T, int C = 1>
111 class GenericVariant : public Variant {
112 public:
113 /*! \brief Constructor, sets the dataId to \a id */
114 GenericVariant(XsDataIdentifier id) : Variant(id) {}
115
116 /*! \brief Read the data from message \a msg at \a offset and optionally using \a dSize */
117 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
118 {
119 msg.getData<T>(data(), dataId(), offset, C);
120 return sz;
121 }
122 /*! \brief Write the data to message \a msg at \a offset */
123 void writeToMessage(XsMessage& msg, XsSize offset) const override
124 {
125 msg.setData<T>(constData(), dataId(), offset, C);
126 }
127 /*! \brief Return a typed pointer to the contained data */
128 virtual T* data() = 0;
129 /*! \brief Return a typed pointer to the contained data */
130 virtual T const* constData() const = 0;
131
132 /*! \brief Return the size the Variant would have in a message */
133 XsSize sizeInMsg() const override
134 {
135 return (XsSize)(ptrdiff_t) XsMessage::sizeInMsg<T>(dataId(), C);
136 }
137 };
138
139 /*! \brief Read the data from the message \a msg at the given \a offset */
140 template<>
141 inline XsSize GenericVariant<uint64_t, 1>::readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz)
142 {
143 *data() = 0;
144 *data() = ((uint64_t)XsMessage_getDataLong(&msg, offset)) << 32;
145 *data() += ((uint64_t)XsMessage_getDataLong(&msg, offset + 4));
146 return sz;
147 }
148
149 /*! \brief Write the data to the message \a msg at the given \a offset */
150 template<>
151 inline void GenericVariant<uint64_t, 1>::writeToMessage(XsMessage& msg, XsSize offset) const
152 {
153 XsMessage_setDataLong(&msg, (uint32_t) ((*constData()) >> 32), offset);
154 XsMessage_setDataLong(&msg, (uint32_t) ((*constData()) & 0xFFFFFFFF), offset);
155 }
156
157 /*! \brief Template base class for simple single value Variants. memcpy is assumed to result in valid objects */
158 template <typename T>
159 struct SimpleVariant : public GenericVariant<T, 1> {
160 using GenericVariant<T, 1>::dataId;
161
162 /*! \brief Constructor, sets the dataId to \a id */
163 SimpleVariant(XsDataIdentifier id) : GenericVariant<T, 1>(id), m_data() {}
164 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
165 SimpleVariant(XsDataIdentifier id, T const& val) : GenericVariant<T, 1>(id), m_data(val) {}
166
167 T m_data; //!< The contained data
168 /*! \brief Return a typed pointer to the contained data */
169 T* data() override
170 {
171 return &m_data;
172 }
173 /*! \brief Return a typed pointer to the contained data */
174 T const* constData() const override
175 {
176 return &m_data;
177 }
178
179 /*! \brief Create a copy of the Variant
180 \return A pointer to the newly created Variant
181 \note This needs to be reimplemented in each subclass!
182 */
183 Variant* clone() const override
184 {
185 return new SimpleVariant<T>(dataId(), m_data);
186 }
187
188 /*! \brief Return the size the Variant would have in a message */
189 XsSize sizeInMsg() const override
190 {
191 return XsMessage::sizeInMsg<T>(dataId(), 1);
192 }
193 };
194
195 /*! \brief Template base class for complex or multi-value Variants. memcpy is NOT assumed to result in valid objects */
196 template <typename U, typename T, int C>
197 struct ComplexVariant : public GenericVariant<T, C>
198 {
199 using GenericVariant<T, C>::dataId;
200 /*! \brief Constructor, sets the dataId to \a id */
201 ComplexVariant(XsDataIdentifier id) : GenericVariant<T, C>(id) {}
202 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
203 ComplexVariant(XsDataIdentifier id, U const& val) : GenericVariant<T, C>(id), m_data(val) {}
204
205 U m_data; //!< The contained data
206 /*! \brief Return a typed pointer to the contained data */
207 T* data() override
208 {
209 return const_cast<T*>(m_data.data());
210 }
211 /*! \brief Return a typed pointer to the contained data */
212 T const* constData() const override
213 {
214 return m_data.data();
215 }
216
217 /*! \brief Create a copy of the Variant
218 \return A pointer to the newly created Variant
219 \note This needs to be reimplemented in each subclass!
220 */
221 Variant* clone() const override
222 {
223 return new ComplexVariant<U, T, C>(dataId(), m_data);
224 }
225 };
226
227 /*! \brief Variant containing an XsQuaternion value */
228 struct XsQuaternionVariant : public ComplexVariant<XsQuaternion, XsReal, 4>
229 {
230 /*! \brief Constructor, sets the dataId to \a id */
231 XsQuaternionVariant(XsDataIdentifier id) : ComplexVariant<XsQuaternion, XsReal, 4>(id) {}
232 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
233 XsQuaternionVariant(XsDataIdentifier id, XsQuaternion const& val) : ComplexVariant<XsQuaternion, XsReal, 4>(id, val) {}
234
235 Variant* clone() const override
236 {
237 return new XsQuaternionVariant(dataId(), m_data);
238 }
239 };
240
241 /*! \brief Variant containing an XsUShortVector value */
242 struct XsUShortVectorVariant : public ComplexVariant<XsUShortVector, uint16_t, 3>
243 {
244 /*! \brief Constructor, sets the dataId to \a id */
245 XsUShortVectorVariant(XsDataIdentifier id) : ComplexVariant<XsUShortVector, unsigned short, 3>(id) {}
246 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
247 XsUShortVectorVariant(XsDataIdentifier id, XsUShortVector const& val) : ComplexVariant<XsUShortVector, unsigned short, 3>(id, val) {}
248 Variant* clone() const override
249 {
250 return new XsUShortVectorVariant(dataId(), m_data);
251 }
252 };
253
254 /*! \brief Variant containing an XsVector3 value */
255 struct XsVector3Variant : public ComplexVariant<XsVector3, XsReal, 3>
256 {
257 /*! \brief Constructor, sets the dataId to \a id */
258 XsVector3Variant(XsDataIdentifier id) : ComplexVariant<XsVector3, XsReal, 3>(id) {}
259 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
260 XsVector3Variant(XsDataIdentifier id, XsVector const& val) : ComplexVariant<XsVector3, XsReal, 3>(id, val)
261 {
262 assert(val.size() == 3);
263 }
264 Variant* clone() const override
265 {
266 return new XsVector3Variant(dataId(), m_data);
267 }
268 };
269
270 /*! \brief Variant containing an XsVector value */
271 struct XsVector2Variant : public ComplexVariant<XsVector, XsReal, 2>
272 {
273 /*! \brief Constructor, sets the dataId to \a id */
274 XsVector2Variant(XsDataIdentifier id) : ComplexVariant<XsVector, XsReal, 2>(id, XsVector(2,0)) {}
275 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
276 XsVector2Variant(XsDataIdentifier id, XsVector const& val) : ComplexVariant<XsVector, XsReal, 2>(id, val)
277 {
278 assert(val.size() == 2);
279 }
280 Variant* clone() const override
281 {
282 return new XsVector2Variant(dataId(), m_data);
283 }
284 };
285
286 /*! \brief Variant containing an XsScrData value */
287 struct XsScrDataVariant : public Variant
288 {
289 /*! \brief Constructor, sets the dataId to \a id */
290 XsScrDataVariant(XsDataIdentifier id) : Variant(id) {}
291 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
292 XsScrDataVariant(XsDataIdentifier id, XsScrData const& val) : Variant(id), m_data(val) {}
293 Variant* clone() const override
294 {
295 return new XsScrDataVariant(dataId(), m_data);
296 }
297
298 XsScrData m_data; //!< The contained data
299 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
300 {
301 for (XsSize i = 0; i < 3; ++i, offset +=2)
302 m_data.m_acc[i] = msg.getDataShort(offset);
303 for (XsSize i = 0; i < 3; ++i, offset +=2)
304 m_data.m_gyr[i] = msg.getDataShort(offset);
305 for (XsSize i = 0; i < 3; ++i, offset +=2)
306 m_data.m_mag[i] = msg.getDataShort(offset);
307 m_data.m_temp = msg.getDataShort(offset);
308 return sz;
309 }
310 void writeToMessage(XsMessage& msg, XsSize offset) const override
311 {
312 for (XsSize i = 0; i < 3; ++i, offset +=2)
313 msg.setDataShort(m_data.m_acc[i], offset);
314 for (XsSize i = 0; i < 3; ++i, offset +=2)
315 msg.setDataShort(m_data.m_gyr[i], offset);
316 for (XsSize i = 0; i < 3; ++i, offset +=2)
317 msg.setDataShort(m_data.m_mag[i], offset);
318 msg.setDataShort(m_data.m_temp, offset);
319 }
320
321 XsSize sizeInMsg() const override
322 {
323 return 10*sizeof(uint16_t);
324 }
325 };
326
327 /*! \brief Variant containing an XsTriggerIndication value */
328 struct XsTriggerIndicationDataVariant : public Variant
329 {
330 /*! \brief Constructor, sets the dataId to \a id */
331 XsTriggerIndicationDataVariant(XsDataIdentifier id) : Variant(id) {}
332 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
333 XsTriggerIndicationDataVariant(XsDataIdentifier id, XsTriggerIndicationData const& val) : Variant(id), m_data(val) {}
334 Variant* clone() const override
335 {
336 return new XsTriggerIndicationDataVariant(dataId(), m_data);
337 }
338
339 XsTriggerIndicationData m_data; //!< The contained data
340 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
341 {
342 m_data.m_line = XsMessage_getDataByte (&msg, offset + 0);
343 m_data.m_polarity = XsMessage_getDataByte (&msg, offset + 1);
344 m_data.m_timestamp = XsMessage_getDataLong (&msg, offset + 2);
345 m_data.m_frameNumber = XsMessage_getDataShort(&msg, offset + 6);
346 return sz;
347 }
348 void writeToMessage(XsMessage& msg, XsSize offset) const override
349 {
350 XsMessage_setDataByte (&msg, m_data.m_line, offset + 0);
351 XsMessage_setDataByte (&msg, m_data.m_polarity, offset + 1);
352 XsMessage_setDataLong (&msg, m_data.m_timestamp, offset + 2);
353 XsMessage_setDataShort(&msg, m_data.m_frameNumber, offset + 6);
354 }
355
356 XsSize sizeInMsg() const override
357 {
358 return 8;
359 }
360 };
361
362 /*! \brief Variant containing an XsEuler value */
363 struct XsEulerVariant : public ComplexVariant<XsEuler, XsReal, 3>
364 {
365 /*! \brief Constructor, sets the dataId to \a id */
366 XsEulerVariant(XsDataIdentifier id) : ComplexVariant<XsEuler, XsReal, 3>(id) {}
367 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
368 XsEulerVariant(XsDataIdentifier id, XsEuler const& val) : ComplexVariant<XsEuler, XsReal, 3>(id, val) {}
369 Variant* clone() const override
370 {
371 return new XsEulerVariant(dataId(), m_data);
372 }
373 };
374
375 /*! \brief Variant containing an XsMatrix value */
376 struct XsMatrixVariant : public ComplexVariant<XsMatrix3x3, XsReal, 9>
377 {
378 /*! \brief Constructor, sets the dataId to \a id */
379 XsMatrixVariant(XsDataIdentifier id) : ComplexVariant<XsMatrix3x3, XsReal, 9>(id) {}
380 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
381 XsMatrixVariant(XsDataIdentifier id, XsMatrix const& val) : ComplexVariant<XsMatrix3x3, XsReal, 9>(id, val) {}
382 Variant* clone() const override
383 {
384 return new XsMatrixVariant(dataId(), m_data);
385 }
386
387 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
388 {
389 XsSize ds = XsMessage_getFPValueSize(dataId());
390 XsSize k = 0;
391 for (int i=0 ; i<3 ; ++i)
392 for (XsSize j=0 ; j<3 ; ++j, k+=ds)
393 XsMessage_getDataRealValuesById(&msg, dataId(), &m_data[j][i], offset+k, 1);
394 return sz;
395 }
396 void writeToMessage(XsMessage& msg, XsSize offset) const override
397 {
398 XsSize ds = XsMessage_getFPValueSize(dataId());
399 XsSize k = 0;
400 for (int i=0 ; i<3 ; ++i)
401 for (XsSize j=0 ; j<3 ; ++j, k+=ds)
402 XsMessage_setDataRealValuesById(&msg, dataId(), &m_data[j][i], offset+k, 1);
403 }
404 };
405
406 /*! \brief Variant containing an XsRange value */
407 struct XsRangeVariant : public Variant
408 {
409 /*! \brief Constructor, sets the dataId to \a id */
410 XsRangeVariant(XsDataIdentifier id) : Variant(id) {}
411 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
412 XsRangeVariant(XsDataIdentifier id, XsRange const& val) : Variant(id), m_data(val) {}
413 Variant* clone() const override
414 {
415 return new XsRangeVariant(dataId(), m_data);
416 }
417
418 XsRange m_data; //!< The contained data
419 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
420 {
421 // unwrap
422 uint16_t first = (uint16_t) XsMessage_getDataShort(&msg, offset + 0);
423 uint16_t last = (uint16_t) XsMessage_getDataShort(&msg, offset + 2);
424 m_data.setRange(first, (int)((uint16_t)(last - first)) + (int)first);
425 return sz;
426 }
427 void writeToMessage(XsMessage& msg, XsSize offset) const override
428 {
429 XsMessage_setDataShort(&msg, (uint16_t) m_data.first(), offset + 0);
430 XsMessage_setDataShort(&msg, (uint16_t) m_data.last(), offset + 2);
431 }
432
433 XsSize sizeInMsg() const override
434 {
435 return 4;
436 }
437 };
438
439 /*! \brief Variant containing an XsTimeInfo value */
440 struct XsTimeInfoVariant : public Variant
441 {
442 /*! \brief Constructor, sets the dataId to \a id */
443 XsTimeInfoVariant(XsDataIdentifier id) : Variant(id), m_data() {}
444 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
445 XsTimeInfoVariant(XsDataIdentifier id, XsTimeInfo const& val) : Variant(id), m_data(val) {}
446 Variant* clone() const override
447 {
448 return new XsTimeInfoVariant(dataId(), m_data);
449 }
450
451 XsTimeInfo m_data; //!< The contained data
452 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
453 {
454 m_data.m_nano = XsMessage_getDataLong(&msg, offset);
455 m_data.m_year = XsMessage_getDataShort(&msg, offset+4);
456
457 // month, day, hour, minute, second and valid
458 uint8_t* bareByte = (uint8_t*) &m_data.m_month;
459 for (XsSize i=0; i < 6; ++i)
460 bareByte[i] = XsMessage_getDataByte(&msg, offset + 6 + i);
461
462 m_data.m_utcOffset = 0;
463 return sz;
464 }
465 void writeToMessage(XsMessage& msg, XsSize offset) const override
466 {
467 // update
468 XsMessage_setDataLong(&msg, m_data.m_nano, offset);
469 XsMessage_setDataShort(&msg, m_data.m_year, offset + 4);
470
471 // month, day, hour, minute, second and valid
472 uint8_t const* bareByte = (uint8_t const*) &m_data.m_month;
473 for (XsSize i=0; i<6;++i)
474 XsMessage_setDataByte(&msg, bareByte[i], offset + 6 + i);
475
476 // utcOffset is ignored and assumed to be 0, use makeUtc to ensure this if necessary
477 }
478
479 XsSize sizeInMsg() const override
480 {
481 return 12;
482 }
483 };
484
485 /*! \brief Variant containing an XsRawGnssPvtData value */
486 struct XsRawGnssPvtDataVariant : public Variant
487 {
488 /*! \brief Constructor, sets the dataId to \a id */
489 XsRawGnssPvtDataVariant(XsDataIdentifier id) : Variant(id), m_data() {}
490 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
491 XsRawGnssPvtDataVariant(XsDataIdentifier id, XsRawGnssPvtData const& val) : Variant(id), m_data(val) {}
492 Variant* clone() const override
493 {
494 return new XsRawGnssPvtDataVariant(dataId(), m_data);
495 }
496
497 XsRawGnssPvtData m_data; //!< The contained data
498 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
499 {
500 m_data.m_itow = XsMessage_getDataLong(&msg, offset + 0);
501 m_data.m_year = XsMessage_getDataShort(&msg, offset + 4);
502 m_data.m_month = XsMessage_getDataByte(&msg, offset + 6);
503 m_data.m_day = XsMessage_getDataByte(&msg, offset + 7);
504 m_data.m_hour = XsMessage_getDataByte(&msg, offset + 8);
505 m_data.m_min = XsMessage_getDataByte(&msg, offset + 9);
506 m_data.m_sec = XsMessage_getDataByte(&msg, offset + 10);
507 m_data.m_valid = XsMessage_getDataByte(&msg, offset + 11);
508 m_data.m_tAcc = XsMessage_getDataLong(&msg, offset + 12);
509 m_data.m_nano = XsMessage_getDataLong(&msg, offset + 16);
510 m_data.m_fixType = XsMessage_getDataByte(&msg, offset + 20);
511 m_data.m_flags = XsMessage_getDataByte(&msg, offset + 21);
512 m_data.m_numSv = XsMessage_getDataByte(&msg, offset + 22);
513 m_data.m_res1 = XsMessage_getDataByte(&msg, offset + 23);
514 m_data.m_lon = XsMessage_getDataLong(&msg, offset + 24);
515 m_data.m_lat = XsMessage_getDataLong(&msg, offset + 28);
516 m_data.m_height = XsMessage_getDataLong(&msg, offset + 32);
517 m_data.m_hMsl = XsMessage_getDataLong(&msg, offset + 36);
518 m_data.m_hAcc = XsMessage_getDataLong(&msg, offset + 40);
519 m_data.m_vAcc = XsMessage_getDataLong(&msg, offset + 44);
520 m_data.m_velN = XsMessage_getDataLong(&msg, offset + 48);
521 m_data.m_velE = XsMessage_getDataLong(&msg, offset + 52);
522 m_data.m_velD = XsMessage_getDataLong(&msg, offset + 56);
523 m_data.m_gSpeed = XsMessage_getDataLong(&msg, offset + 60);
524 m_data.m_headMot = XsMessage_getDataLong(&msg, offset + 64);
525 m_data.m_sAcc = XsMessage_getDataLong(&msg, offset + 68);
526 m_data.m_headAcc = XsMessage_getDataLong(&msg, offset + 72);
527 m_data.m_headVeh = XsMessage_getDataLong(&msg, offset + 76);
528 m_data.m_gdop = XsMessage_getDataShort(&msg, offset + 80);
529 m_data.m_pdop = XsMessage_getDataShort(&msg, offset + 82);
530 m_data.m_tdop = XsMessage_getDataShort(&msg, offset + 84);
531 m_data.m_vdop = XsMessage_getDataShort(&msg, offset + 86);
532 m_data.m_hdop = XsMessage_getDataShort(&msg, offset + 88);
533 m_data.m_ndop = XsMessage_getDataShort(&msg, offset + 90);
534 m_data.m_edop = XsMessage_getDataShort(&msg, offset + 92);
535 return sz;
536 }
537 void writeToMessage(XsMessage& msg, XsSize offset) const override
538 {
539 XsMessage_setDataLong (&msg, m_data.m_itow , offset + 0);
540 XsMessage_setDataShort(&msg, m_data.m_year , offset + 4);
541 XsMessage_setDataByte (&msg, m_data.m_month , offset + 6);
542 XsMessage_setDataByte (&msg, m_data.m_day , offset + 7);
543 XsMessage_setDataByte (&msg, m_data.m_hour , offset + 8);
544 XsMessage_setDataByte (&msg, m_data.m_min , offset + 9);
545 XsMessage_setDataByte (&msg, m_data.m_sec , offset + 10);
546 XsMessage_setDataByte (&msg, m_data.m_valid , offset + 11);
547 XsMessage_setDataLong (&msg, m_data.m_tAcc , offset + 12);
548 XsMessage_setDataLong (&msg, m_data.m_nano , offset + 16);
549 XsMessage_setDataByte (&msg, m_data.m_fixType, offset + 20);
550 XsMessage_setDataByte (&msg, m_data.m_flags , offset + 21);
551 XsMessage_setDataByte (&msg, m_data.m_numSv , offset + 22);
552 XsMessage_setDataByte (&msg, m_data.m_res1 , offset + 23);
553 XsMessage_setDataLong (&msg, m_data.m_lon , offset + 24);
554 XsMessage_setDataLong (&msg, m_data.m_lat , offset + 28);
555 XsMessage_setDataLong (&msg, m_data.m_height , offset + 32);
556 XsMessage_setDataLong (&msg, m_data.m_hMsl , offset + 36);
557 XsMessage_setDataLong (&msg, m_data.m_hAcc , offset + 40);
558 XsMessage_setDataLong (&msg, m_data.m_vAcc , offset + 44);
559 XsMessage_setDataLong (&msg, m_data.m_velN , offset + 48);
560 XsMessage_setDataLong (&msg, m_data.m_velE , offset + 52);
561 XsMessage_setDataLong (&msg, m_data.m_velD , offset + 56);
562 XsMessage_setDataLong (&msg, m_data.m_gSpeed , offset + 60);
563 XsMessage_setDataLong (&msg, m_data.m_headMot, offset + 64);
564 XsMessage_setDataLong (&msg, m_data.m_sAcc , offset + 68);
565 XsMessage_setDataLong (&msg, m_data.m_headAcc, offset + 72);
566 XsMessage_setDataLong (&msg, m_data.m_headVeh, offset + 76);
567 XsMessage_setDataShort(&msg, m_data.m_gdop , offset + 80);
568 XsMessage_setDataShort(&msg, m_data.m_pdop , offset + 82);
569 XsMessage_setDataShort(&msg, m_data.m_tdop , offset + 84);
570 XsMessage_setDataShort(&msg, m_data.m_vdop , offset + 86);
571 XsMessage_setDataShort(&msg, m_data.m_hdop , offset + 88);
572 XsMessage_setDataShort(&msg, m_data.m_ndop , offset + 90);
573 XsMessage_setDataShort(&msg, m_data.m_edop , offset + 92);
574 }
575
576 XsSize sizeInMsg() const override
577 {
578 return 94;
579 }
580 };
581
582 /*! \brief Variant containing an XsRawGnssSatInfo value */
583 struct XsRawGnssSatInfoVariant : public Variant
584 {
585 /*! \brief Constructor, sets the dataId to \a id */
586 XsRawGnssSatInfoVariant(XsDataIdentifier id) : Variant(id), m_data() {}
587 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
588 XsRawGnssSatInfoVariant(XsDataIdentifier id, XsRawGnssSatInfo const& val) : Variant(id), m_data(val) {}
589 Variant* clone() const override
590 {
591 return new XsRawGnssSatInfoVariant(dataId(), m_data);
592 }
593
594 XsRawGnssSatInfo m_data; //!< The contained data
595 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
596 {
597 m_data.m_itow = XsMessage_getDataLong(&msg, offset + 0);
598 m_data.m_numSvs = XsMessage_getDataByte(&msg, offset + 4);
599 m_data.m_res1 = XsMessage_getDataByte(&msg, offset + 5);
600 m_data.m_res2 = XsMessage_getDataByte(&msg, offset + 6);
601 m_data.m_res3 = XsMessage_getDataByte(&msg, offset + 7);
602
603 offset = offset + 8;
604 for (uint8_t i = 0; i < m_data.m_numSvs; ++i)
605 {
606 m_data.m_satInfos[i].m_gnssId = XsMessage_getDataByte(&msg, offset + 0);
607 m_data.m_satInfos[i].m_svId = XsMessage_getDataByte(&msg, offset + 1);
608 m_data.m_satInfos[i].m_cno = XsMessage_getDataByte(&msg, offset + 2);
609 m_data.m_satInfos[i].m_flags = XsMessage_getDataByte(&msg, offset + 3);
610 offset += 4;
611 }
612 return sz;
613 }
614 void writeToMessage(XsMessage& msg, XsSize offset) const override
615 {
616 XsMessage_setDataLong(&msg, m_data.m_itow , offset + 0);
617 XsMessage_setDataByte(&msg, m_data.m_numSvs , offset + 4);
618 XsMessage_setDataByte(&msg, m_data.m_res1 , offset + 5);
619 XsMessage_setDataByte(&msg, m_data.m_res2 , offset + 6);
620 XsMessage_setDataByte(&msg, m_data.m_res3 , offset + 7);
621
622 offset = offset + 8;
623 for (uint8_t i = 0; i < m_data.m_numSvs; ++i)
624 {
625 XsMessage_setDataByte(&msg, m_data.m_satInfos[i].m_gnssId , offset + 0);
626 XsMessage_setDataByte(&msg, m_data.m_satInfos[i].m_svId , offset + 1);
627 XsMessage_setDataByte(&msg, m_data.m_satInfos[i].m_cno , offset + 2);
628 XsMessage_setDataByte(&msg, m_data.m_satInfos[i].m_flags , offset + 3);
629 offset += 4;
630 }
631 }
632
633 XsSize sizeInMsg() const override
634 {
635 return 8 + 4*m_data.m_numSvs;
636 }
637 };
638
639 /*! \brief Variant containing an XsFullSnaphsot value */
640 struct XsFullSnapshotVariant : public Variant
641 {
642 /*! \brief Constructor, sets the dataId to \a id */
643 XsFullSnapshotVariant(XsDataIdentifier id) : Variant(id) {}
644 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
645 XsFullSnapshotVariant(XsDataIdentifier id, XsSnapshot const& val) : Variant(id), m_data(val) {}
646
647 Variant* clone() const override
648 {
649 return new XsFullSnapshotVariant(dataId(), m_data);
650 }
651
652 XsSnapshot m_data; //!< The contained data
653 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
654 {
655 m_data.m_frameNumber = XsMessage_getDataShort(&msg, offset); offset += 2;
656 m_data.m_timestamp = XsMessage_getDataLongLong(&msg, offset); offset += 8;
657 for (int i = 0; i < 4; ++i, offset += 4)
658 m_data.m_iQ[i] = XsMessage_getDataLong(&msg, offset);
659 for (int i = 0; i < 3; ++i, offset += 8)
660 m_data.m_iV[i] = XsMessage_getDataLongLong(&msg, offset);
661 for (int i = 0; i < 3; ++i, offset += 4)
662 m_data.m_mag[i] = XsMessage_getDataLong(&msg, offset);
663
664 m_data.m_baro = XsMessage_getDataLong(&msg, offset); offset += 4;
665 m_data.m_accClippingCounter = XsMessage_getDataByte(&msg, offset); offset += 1;
666 m_data.m_gyrClippingCounter = XsMessage_getDataByte(&msg, offset); offset += 1;
667 m_data.m_status = XsMessage_getDataShort(&msg, offset);
668 m_data.m_type = ST_Full;
669 return sz;
670 }
671
672 void writeToMessage(XsMessage& msg, XsSize offset) const override
673 {
674 assert(m_data.m_type == ST_Full);
675 XsMessage_setDataShort(&msg, (uint16_t) m_data.m_frameNumber, offset); offset += 2;
676 XsMessage_setDataLongLong(&msg, m_data.m_timestamp, offset); offset += 8;
677 for (int i = 0; i < 4; ++i, offset += 4)
678 XsMessage_setDataLong(&msg, m_data.m_iQ[i], offset);
679 for (int i = 0; i < 3; ++i, offset += 8)
680 XsMessage_setDataLongLong(&msg, m_data.m_iV[i], offset);
681 for (int i = 0; i < 3; ++i, offset += 4)
682 XsMessage_setDataLong(&msg, m_data.m_mag[i], offset);
683
684 XsMessage_setDataLong(&msg, m_data.m_baro, offset); offset += 4;
685 XsMessage_setDataByte(&msg, m_data.m_accClippingCounter, offset); offset += 1;
686 XsMessage_setDataByte(&msg, m_data.m_gyrClippingCounter, offset); offset += 1;
687 XsMessage_setDataShort(&msg, m_data.m_status, offset);
688 }
689
690 XsSize sizeInMsg() const override
691 {
692 return 70;
693 }
694 };
695
696 /*! \brief Variant containing an XsAwindaSnapshot value */
697 struct XsAwindaSnapshotVariant : public Variant
698 {
699 /*! \brief Constructor, sets the dataId to \a id */
700 XsAwindaSnapshotVariant(XsDataIdentifier id) : Variant(id)
701 {
702 m_data.m_type = ST_Awinda;
703 }
704 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
705 XsAwindaSnapshotVariant(XsDataIdentifier id, XsSnapshot const& val) : Variant(id), m_data(val)
706 {
707 m_data.m_type = ST_Awinda;
708 }
709 Variant* clone() const override
710 {
711 return new XsAwindaSnapshotVariant(dataId(), m_data);
712 }
713
714 XsSnapshot m_data; //!< The contained data
715 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
716 {
717 m_data.m_deviceId = XsMessage_getDataLong(&msg, offset); offset += 4;
718 m_data.m_frameNumber = XsMessage_getDataLong(&msg, offset); offset += 4;
719 for (int i = 0; i < 3; ++i, offset += 4)
720 m_data.m_iQ[i] = XsMessage_getDataLong(&msg, offset);
721 for (int i = 0; i < 3; ++i, offset += 4)
722 m_data.m_iV[i] = (int64_t)(int32_t) XsMessage_getDataLong(&msg, offset);
723 for (int i = 0; i < 3; ++i, offset += 2)
724 m_data.m_mag[i] = (int32_t)(int16_t) XsMessage_getDataShort(&msg, offset);
725 m_data.m_baro = XsMessage_getDataLong(&msg, offset); offset += 4;
726 m_data.m_status = XsMessage_getDataShort(&msg, offset); offset += 2;
727 m_data.m_accClippingCounter = XsMessage_getDataByte(&msg, offset); offset += 1;
728 m_data.m_gyrClippingCounter = XsMessage_getDataByte(&msg, offset);
729 m_data.m_type = ST_Awinda;
730 return sz;
731 }
732 void writeToMessage(XsMessage& msg, XsSize offset) const override
733 {
734 assert(m_data.m_type == ST_Awinda);
735 XsMessage_setDataLong(&msg, m_data.m_deviceId.legacyDeviceId(), offset); offset += 4;
736 XsMessage_setDataLong(&msg, m_data.m_frameNumber, offset); offset += 4;
737 for (int i = 0; i < 3; ++i, offset += 4)
738 XsMessage_setDataLong(&msg, m_data.m_iQ[i], offset);
739 for (int i = 0; i < 3; ++i, offset += 4)
740 XsMessage_setDataLong(&msg, (uint32_t)(int32_t) m_data.m_iV[i], offset);
741 for (int i = 0; i < 3; ++i, offset += 2)
742 XsMessage_setDataShort(&msg, (uint16_t)(int16_t) m_data.m_mag[i], offset);
743 XsMessage_setDataLong(&msg, m_data.m_baro, offset); offset += 4;
744 XsMessage_setDataShort(&msg, m_data.m_status, offset); offset += 2;
745 XsMessage_setDataByte(&msg, m_data.m_accClippingCounter, offset); offset += 1;
746 XsMessage_setDataByte(&msg, m_data.m_gyrClippingCounter, offset);
747 }
748
749 XsSize sizeInMsg() const override
750 {
751 return 46;
752 }
753 };
754
755 /*! \brief Variant containing an XsByteArray value */
756 struct XsByteArrayVariant : public Variant
757 {
758 /*! \brief Constructor, sets the dataId to \a id */
759 XsByteArrayVariant(XsDataIdentifier id) : Variant(id)
760 {
761 }
762 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
763 XsByteArrayVariant(XsDataIdentifier id, XsByteArray const& val) : Variant(id), m_data(val)
764 {
765 }
766 Variant* clone() const override
767 {
768 return new XsByteArrayVariant(dataId(), m_data);
769 }
770
771 XsByteArray m_data; //!< The contained data
772 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize dSize) override
773 {
774 if (dSize)
775 {
776 if (dSize < 255)
777 {
778 m_data.assign(dSize, XsMessage_getDataBuffer(&msg, offset));
779 return dSize;
780 }
781 else
782 {
783 const uint16_t exactId = XsMessage_getDataShort(&msg, offset-3);
784 XsSize msgSize = msg.getDataSize();
785 XsSize total = 0;
786
787 // look ahead to get total size
788 XsSize sSize = dSize;
789 XsSize sOffset = offset;
790 XsSize tSize = 255;
791 while (sSize == 255)
792 {
793 sOffset += 255;
794 if (sOffset >= msgSize)
795 break;
796 uint16_t nextId = XsMessage_getDataShort(&msg, sOffset);
797 if (nextId != exactId)
798 break;
799 sSize = XsMessage_getDataByte(&msg, sOffset+2);
800 sOffset += 3;
801 tSize += sSize;
802 }
803 m_data.setSize(tSize);
804
805 sOffset = 0;
806 while (dSize == 255)
807 {
808 memcpy(m_data.data()+sOffset, XsMessage_getDataBuffer(&msg, offset), 255);
809 total += 258;
810 offset += 255;
811 sOffset += 255;
812 if (offset >= msgSize)
813 {
814 dSize = 0;
815 break;
816 }
817 uint16_t nextId = XsMessage_getDataShort(&msg, offset);
818 if (nextId != exactId)
819 {
820 dSize = 0;
821 break;
822 }
823 dSize = XsMessage_getDataByte(&msg, offset+2);
824 offset += 3;
825 }
826 if (dSize)
827 {
828 memcpy(m_data.data()+sOffset, XsMessage_getDataBuffer(&msg, offset), dSize);
829 total += dSize;
830 }
831 return total;
832 }
833 }
834 else
835 {
836 m_data.clear();
837 return 0;
838 }
839 }
840 void writeToMessage(XsMessage& msg, XsSize offset) const override
841 {
842 if (m_data.size())
843 {
844 if (m_data.size() < 255)
845 XsMessage_setDataBuffer(&msg, m_data.data(), m_data.size(), offset);
846 else
847 {
848 XsSize dataOffset = 0;
849 XsSize remainingSize = m_data.size();
850 while (remainingSize >= 255)
851 {
852 XsMessage_setDataBuffer(&msg, m_data.data() + dataOffset, 255, offset);
853 offset += 258;
854 dataOffset += 255;
855 remainingSize -= 255;
856 }
857 if (remainingSize)
858 XsMessage_setDataBuffer(&msg, m_data.data() + dataOffset, remainingSize, offset);
859 }
860 }
861 }
862
863 XsSize sizeInMsg() const override
864 {
865 return m_data.size();
866 }
867 };
868
869 /*! \brief Variant containing an XsGloveSnapshotVariant value */
870 struct XsGloveSnapshotVariant : public Variant
871 {
872 /*! \brief Constructor, sets the dataId to \a id */
873 XsGloveSnapshotVariant(XsDataIdentifier id) : Variant(id)
874 {
875 }
876 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
877 XsGloveSnapshotVariant(XsDataIdentifier id, XsGloveSnapshot const& val) : Variant(id), m_data(val)
878 {
879 }
880 Variant* clone() const override
881 {
882 return new XsGloveSnapshotVariant(dataId(), m_data);
883 }
884
885 XsGloveSnapshot m_data; //!< The contained data
886 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize dSize) override
887 {
888 const uint16_t exactId = XsMessage_getDataShort(&msg, offset - 3);
889 assert(dSize == 255);
890 if (dSize != 255)
891 return 0;
892 memcpy((uint8_t*)&m_data, XsMessage_getDataBuffer(&msg, offset), 255);
893 offset += 255;
894 uint16_t nextId = XsMessage_getDataShort(&msg, offset);
895 assert(nextId == exactId);
896 if (nextId != exactId)
897 return 0;
898 dSize = XsMessage_getDataByte(&msg, offset + 2);
899 assert(dSize == 124);
900 if (dSize != 124)
901 return 0;
902 offset += 3;
903 memcpy(((uint8_t*)&m_data)+255, XsMessage_getDataBuffer(&msg, offset), 124);
904
905 // loop over finger / struct and swap bytes
906 m_data.m_snapshotCounter = (uint16_t)swapEndian16(m_data.m_snapshotCounter);
907 m_data.m_validSampleFlags = (uint16_t)swapEndian16(m_data.m_validSampleFlags);
908 m_data.m_timestamp = (uint16_t)swapEndian16(m_data.m_timestamp);
909
910 for (int i = 0; i < 12; ++i)
911 {
912 for (int j = 0; j < 3; ++j)
913 {
914 // JLBC for MRPT: "auto&"->"auto" to avoid alignment GCC error
915 {
916 auto tmp = m_data.m_fingers[i].m_iV[j];
917 m_data.m_fingers[i].m_iV[j] = (uint32_t)swapEndian32(tmp);
918 }
919 {
920 auto tmp = m_data.m_fingers[i].m_mag[j];
921 tmp = (int16_t)swapEndian16(tmp);
922 }
923 }
924 {
925 //const auto& tmp = swapEndian16(m_data.m_fingers[i].m_flags);
926 }
927 }
928
929 return 255+124;
930 }
931 void writeToMessage(XsMessage& msg, XsSize offset) const override
932 {
933 assert(0); //not expected to be called
934 XsMessage_setDataShort(&msg, m_data.m_snapshotCounter, offset);
935 }
936
937 XsSize sizeInMsg() const override
938 {
939 return sizeof(XsGloveSnapshot);
940 }
941 };
942
943 /*! \brief Variant containing an XsGloveDataVariant value */
944 struct XsGloveDataVariant : public Variant
945 {
946 /*! \brief Constructor, sets the dataId to \a id */
947 XsGloveDataVariant(XsDataIdentifier id) : Variant(id), m_data() {}
948 /*! \brief Constructor, sets the dataId to \a id and data value to \a val */
949 XsGloveDataVariant(XsDataIdentifier id, XsGloveData const& val) : Variant(id), m_data(val) {}
950 Variant* clone() const override
951 {
952 return new XsGloveDataVariant(dataId(), m_data);
953 }
954
955 XsGloveData m_data; //!< The contained data
956 XsSize readFromMessage(XsMessage const& msg, XsSize offset, XsSize sz) override
957 {
958 assert(0); //not expected to be called
959 XsMessage_getDataShort(&msg, offset);
960 return sz;
961 }
962 void writeToMessage(XsMessage& msg, XsSize offset) const override
963 {
964 assert(0); //not expected to be called
965 XsMessage_setDataShort(&msg, m_data.snapshotCounter(), offset);
966 }
967
968 XsSize sizeInMsg() const override
969 {
970 return sizeof(XsGloveData);
971 }
972 };
973 }
974
975 typedef std::map<XsDataIdentifier, XsDataPacket_Private::Variant*> MapType;
976
977 struct DataPacketPrivate : private MapType
978 {
979 DataPacketPrivate() : m_refCount(1) { ++m_created; }
980 DataPacketPrivate(DataPacketPrivate const&);
981 ~DataPacketPrivate();
982 DataPacketPrivate& operator = (const DataPacketPrivate& p);
983 void erase(XsDataIdentifier id);
984 void erase(MapType::const_iterator const& it);
985 MapType::iterator insert(XsDataIdentifier id, XsDataPacket_Private::Variant* var);
986
987 void clear();
988 void merge(DataPacketPrivate const& other, bool overwrite);
989
990 MapType::const_iterator find(XsDataIdentifier id) const;
991
992 using MapType::begin;
993 using MapType::end;
994 using MapType::size;
995 using MapType::empty;
996 mutable volatile std::atomic_int m_refCount; //!< The reference count for this DataPacketPrivate.
997 static volatile std::atomic_int m_created; //!< The number of DataPacketPrivate objects created so far. \sa creationDiff()
998 static volatile std::atomic_int m_destroyed; //!< The number of DataPacketPrivate objects destroyed so far. \sa creationDiff()
999
1000 static int creationDiff();
1001 };
1002
1003 /*! \endcond */
1004
1005 #endif