Debian Codegraph @

3rdparty/xspublic/xstypes/xsmessage.c

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 #include "xsmessage.h"
34 #include <stdlib.h>
35 #include <memory.h> // memset
36 #include "xsbusid.h"
37 #include <stdio.h>
38
39 //////////////////////////////////////////////////////////////////////////////////////////
40 // Field message indices
41 #define XS_IND_PREAMBLE 0
42 #define XS_IND_BID 1
43 #define XS_IND_MID 2
44 #define XS_IND_LEN 3
45 #define XS_IND_DATA0 4
46 #define XS_IND_LENEXTH 4
47 #define XS_IND_LENEXTL 5
48 #define XS_IND_DATAEXT0 6
49
50 #define XS_SELFTEST_OK 0x1FF
51
52 // message data lengths
53 #define XS_LEN_TRANSPORTMODE 1
54 #define XS_LEN_DEVICEID 4
55 #define XS_LEN_INITBUSRESULTS 4
56 #define XS_LEN_PERIOD 2
57 #define XS_LEN_BUSPWR 2
58 #define XS_LEN_DATALENGTH 2
59 #define XS_LEN_CONFIGURATION 118
60 #define XS_LEN_FIRMWAREREV 3
61 #define XS_LEN_BTDISABLE 1
62 #define XS_LEN_OPMODE 1
63 #define XS_LEN_BAUDRATE 1
64 #define XS_LEN_SYNCMODE 1
65 #define XS_LEN_PRODUCTCODE 20
66 #define XS_LEN_PROCESSINGFLAGS 2
67 #define XS_LEN_XMPWROFF 0
68 #define XS_LEN_OUTPUTMODE 2
69 #define XS_LEN_OUTPUTSETTINGS 4
70 #define XS_LEN_OUTPUTSKIPFACTOR 2
71 #define XS_LEN_SYNCINMODE 2
72 #define XS_LEN_SYNCINSKIPFACTOR 2
73 #define XS_LEN_SYNCINOFFSET 4
74 #define XS_LEN_SYNCOUTMODE 2
75 #define XS_LEN_SYNCOUTSKIPFACTOR 2
76 #define XS_LEN_SYNCOUTOFFSET 4
77 #define XS_LEN_SYNCOUTPULSEWIDTH 4
78 #define XS_LEN_ERRORMODE 2
79 #define XS_LEN_TRANSMITDELAY 2
80 #define XS_LEN_OBJECTALIGNMENT 36
81 #define XS_LEN_XMERRORMODE 2
82 #define XS_LEN_BUFFERSIZE 2
83 #define XS_LEN_HEADING 4
84 #define XS_LEN_MAGNETICFIELD 12
85 #define XS_LEN_LOCATIONID 2
86 #define XS_LEN_EXTOUTPUTMODE 2
87 #define XS_LEN_INITTRACKMODE 2
88 #define XS_LEN_STOREFILTERSTATE 0
89 #define XS_LEN_UTCTIME 12
90 #define XS_LEN_FILTERPROFILELABEL 20
91 #define XS_LEN_FILTERPROFILEFULL (1+1+XS_LEN_FILTERPROFILELABEL)
92 #define XS_LEN_AVAILABLEFILTERPROFILES (XS_MAX_FILTERPROFILES_IN_MT*XS_LEN_FILTERPROFILEFULL)
93 #define XS_LEN_REQFILTERPROFILEACK 2
94 #define XS_LEN_SETFILTERPROFILE 2
95 #define XS_LEN_GRAVITYMAGNITUDE 4
96 #define XS_LEN_GNSSLEVERARM 12
97 #define XS_LEN_LATLONALT 18
98 #define XS_LEN_SETNOROTATION 2
99 #define XS_LEN_FILTERSETTINGS 4
100 #define XS_LEN_AMD 2
101 #define XS_LEN_RESETORIENTATION 2
102 #define XS_LEN_GNSSSTATUS (1+5*16)
103 #define XS_LEN_CLIENTUSAGE 1
104 #define XS_LEN_CLIENTPRIORITY 1
105 #define XS_LEN_WIRELESSCONFIG 6
106 #define XS_LEN_INFOREQUEST 1
107 #define XS_LEN_SETOUTPUTTRIGGER 10
108 #define XS_LEN_SETINPUTTRIGGER 10
109
110 // MTData defines
111 // Length of data blocks in bytes
112 #define XS_LEN_RAWDATA 20
113 #define XS_LEN_CALIBDATA 36
114 #define XS_LEN_CALIB_ACCDATA 12
115 #define XS_LEN_CALIB_GYRDATA 12
116 #define XS_LEN_CALIB_MAGDATA 12
117 #define XS_LEN_ORIENT_QUATDATA 16
118 #define XS_LEN_ORIENT_EULERDATA 12
119 #define XS_LEN_ORIENT_MATRIXSTA 36
120 #define XS_LEN_SAMPLECNT 2
121 #define XS_LEN_TEMPDATA 4
122
123 // Length of data blocks in floats
124 #define XS_LEN_CALIBDATA_FLT 9
125 #define XS_LEN_TEMPDATA_FLT 1
126 #define XS_LEN_ORIENT_QUATDATA_FLT 4
127 #define XS_LEN_ORIENT_EULERDATA_FLT 3
128 #define XS_LEN_ORIENT_MATRIXSTA_FLT 9
129
130 #pragma pack(push, 1)
131 // little endian
132 union Itypes {
133 int64_t i64;
134 struct {
135 int32_t i1,i0;
136 } i32;
137 struct {
138 int16_t s3,s2,s1,s0;
139 } i16;
140 struct {
141 signed char b7,b6,b5,b4,b3,b2,b1,b0;
142 } i8;
143
144 double d;
145 struct {
146 float f1,f0;
147 } f32;
148 };
149 #pragma pack(pop)
150
151
152 /*! \addtogroup cinterface C Interface
153 @{
154 */
155
156 typedef union Itypes Itypes;
157
158 /*! \brief Calculate the sum of the values in the buffer
159 \details This function calculates the sum of the byte values for the first \a count bytes in the \a buffer
160 \param buffer An array of (unsigned) bytes
161 \param count The number of bytes in the buffer
162 \returns The unsigned sum of the byte values in the buffer modulo 256
163 */
164 uint8_t byteSum(const uint8_t* buffer, XsSize count)
165 {
166 register uint8_t sum = 0;
167 for (; count; --count, sum += *(buffer++));
168 return sum;
169 }
170
171 /*! \brief Get the buffer at offset \a offset */
172 static inline uint8_t *XsMessage_dataAtOffset(XsMessage *thisPtr, XsSize offset)
173 {
174 XsMessageHeader* hdr;
175
176 assert(thisPtr->m_message.m_data);
177 assert(offset < XsMessage_dataSize(thisPtr));
178
179 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
180 if (hdr->m_length != 255)
181 return hdr->m_datlen.m_data + offset;
182 return hdr->m_datlen.m_extended.m_data + offset;
183 }
184
185 /*! \brief return the const data at offset \a offset */
186 static inline const uint8_t *XsMessage_cdataAtOffset(XsMessage const *thisPtr, XsSize offset)
187 {
188 return XsMessage_dataAtOffset((XsMessage*)thisPtr, offset);
189 }
190
191 /*! \brief Make sure the data buffer is large enough to hold a new data item of \a sizeofValue */
192 static inline void XsMessage_ensureDataSize(XsMessage *thisPtr, XsSize offset, XsSize sizeofValue)
193 {
194 if (XsMessage_dataSize(thisPtr) < offset + sizeofValue)
195 XsMessage_resizeData(thisPtr, offset + sizeofValue);
196 }
197
198 /*! \brief Update the message checksum with the passed value */
199 static inline void XsMessage_updateChecksumWithValue(XsMessage *thisPtr, const void *value, XsSize sizeofValue, XsSize offset)
200 {
201 if (thisPtr->m_autoUpdateChecksum)
202 {
203 thisPtr->m_checksum[0] += byteSum(XsMessage_getDataBuffer(thisPtr, offset), sizeofValue);
204 thisPtr->m_checksum[0] -= byteSum(value, sizeofValue);
205 }
206 }
207
208 /*! \brief Swap the endianness based on the data size */
209 static inline void swapEndian(void *data, const XsSize size)
210 {
211 uint16_t *i16;
212 uint32_t *i32;
213 uint64_t *i64;
214
215 switch (size)
216 {
217 case sizeof(char):
218 break;
219 case sizeof(*i16):
220 i16 = data;
221 *i16 = swapEndian16(*i16);
222 break;
223 case sizeof(*i32):
224 i32 = data;
225 *i32 = swapEndian32(*i32);
226 break;
227 case sizeof(*i64):
228 i64 = data;
229 *i64 = swapEndian64(*i64);
230 break;
231 default:
232 assert(0);
233 }
234 }
235
236 /*! \brief Get data of size \a size at \a offset, and put it byteswapped into \a value */
237 void XsMessage_getEndianCorrectData(XsMessage const* thisPtr, void *value, XsSize size, XsSize offset)
238 {
239 memcpy(value, (void const*) XsMessage_cdataAtOffset(thisPtr, offset), size);
240 swapEndian(value, size);
241 }
242
243 /*! \brief Set value \a value of size \a size byteswapped at \a offset */
244 void XsMessage_setEndianCorrectData(XsMessage *thisPtr, void const *value, XsSize size, XsSize offset)
245 {
246 void* dest;
247 XsMessage_ensureDataSize(thisPtr, offset, size);
248 XsMessage_updateChecksumWithValue(thisPtr, value, size, offset);
249 dest = XsMessage_dataAtOffset(thisPtr, offset);
250 memcpy(dest, value, size);
251 swapEndian(dest, size);
252 }
253
254 /*! \brief This function initializes the %XsMessage object and reserves \a dataSize bytes for data
255
256 \param[in] dataSize the expected size of the message payload
257 */
258 void XsMessage_constructSized(XsMessage* thisPtr, XsSize dataSize)
259 {
260 XsSize msgSize;
261 XsMessageHeader* hdr;
262
263 if (dataSize < 255)
264 msgSize = dataSize + 5;
265 else
266 msgSize = dataSize + 7;
267
268 XsByteArray_construct(&thisPtr->m_message, msgSize, 0);
269 memset(thisPtr->m_message.m_data, 0, msgSize);
270 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
271 hdr->m_preamble = XS_PREAMBLE;
272 hdr->m_messageId = 0;
273 hdr->m_busId = XS_BID_MASTER;
274
275 if (dataSize < XS_EXTLENCODE)
276 {
277 hdr->m_length = (uint8_t) dataSize;
278 *((uint8_t**) &thisPtr->m_checksum) = &hdr->m_datlen.m_data[dataSize];
279 thisPtr->m_checksum[0] = (uint8_t)-(int8_t)(uint8_t)dataSize;
280 }
281 else
282 {
283 hdr->m_length = XS_EXTLENCODE;
284 hdr->m_datlen.m_extended.m_length.m_high = (uint8_t) (dataSize >> 8);
285 hdr->m_datlen.m_extended.m_length.m_low = (uint8_t) dataSize;
286 *((uint8_t**) &thisPtr->m_checksum) = &hdr->m_datlen.m_extended.m_data[dataSize];
287 thisPtr->m_checksum[0] = (uint8_t) -(hdr->m_datlen.m_extended.m_length.m_high + hdr->m_datlen.m_extended.m_length.m_low + XS_EXTLENCODE);
288 }
289 thisPtr->m_checksum[0] -= hdr->m_busId;
290 }
291
292 /*! \brief This function initializes the %XsMessage object
293 */
294 void XsMessage_construct(XsMessage* thisPtr)
295 {
296 XsMessage_constructSized(thisPtr, 0);
297 }
298
299 /*! \brief Construct an XsMessage as a copy of XsMessage \a src
300 */
301 void XsMessage_copyConstruct(XsMessage* thisPtr, XsMessage const* src)
302 {
303 if (!src)
304 XsMessage_construct(thisPtr);
305 else
306 {
307 XsMessageHeader* hdr;
308 XsSize dataSize;
309 XsArray_copyConstruct(&thisPtr->m_message, &src->m_message);
310 thisPtr->m_autoUpdateChecksum = src->m_autoUpdateChecksum;
311 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
312 dataSize = XsMessage_dataSize(thisPtr);
313 if (dataSize >= 255)
314 *((uint8_t**) &thisPtr->m_checksum) = &hdr->m_datlen.m_extended.m_data[dataSize];
315 else
316 *((uint8_t**) &thisPtr->m_checksum) = &hdr->m_datlen.m_data[dataSize];
317 }
318 }
319
320 /*! \brief This function reinitializes the %XsMessage object and reserves \a dataSize bytes for data
321 \param[in] dataSize the expected size of the message payload
322 */
323 void XsMessage_assign(XsMessage* thisPtr, XsSize dataSize)
324 {
325 XsMessage_destruct(thisPtr);
326 XsMessage_constructSized(thisPtr, dataSize);
327 }
328
329 /*! \brief This function initializes the %XsMessage object and reserves \a msgSize bytes for data, it then copies in the data from \a src
330 \param msgSize the size of the data pointed to by src
331 \param src the data to load the message from
332 \note This is a constructor! Previous contents will be overwritten without first freeing them!
333 */
334 void XsMessage_load(XsMessage* thisPtr, XsSize msgSize, unsigned char const* src)
335 {
336 XsByteArray_construct(&thisPtr->m_message, msgSize, src);
337 *((uint8_t**) &thisPtr->m_checksum) = &thisPtr->m_message.m_data[XsMessage_getTotalMessageSize(thisPtr)-1];
338 }
339
340 /*! \brief This function clears the data in the message
341
342 */
343 void XsMessage_destruct(XsMessage* thisPtr)
344 {
345 XsArray_destruct(&thisPtr->m_message);
346 *((uint8_t**) &thisPtr->m_checksum) = 0;
347 }
348
349 /*! \brief This function copies from \a thisPtr to \a copy
350 \param copy the object to copy to
351 */
352 void XsMessage_copy(XsMessage* copy, XsMessage const* thisPtr)
353 {
354 XsArray_copy(&copy->m_message, &thisPtr->m_message);
355 *((uint8_t**) &copy->m_checksum) = &copy->m_message.m_data[XsMessage_getTotalMessageSize(copy)-1];
356 copy->m_autoUpdateChecksum = thisPtr->m_autoUpdateChecksum;
357 }
358
359 /*! \brief This function returns the datasize of the message in \a thisptr
360 \returns the size of the message payload
361 */
362 XsSize XsMessage_dataSize(XsMessage const* thisPtr)
363 {
364 XsMessageHeader* hdr;
365
366 if (!thisPtr->m_message.m_data)
367 return 0;
368
369 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
370 if (hdr->m_length == 255)
371 return (((XsSize) hdr->m_datlen.m_extended.m_length.m_high) << 8) + hdr->m_datlen.m_extended.m_length.m_low;
372 else
373 return ((XsSize) hdr->m_length);
374 }
375
376 /*! \brief This function returns a const pointer to the \a offset in the data of the message in \a thisptr
377
378 \param offset the offset of the data to be returned
379
380 \returns a pointer to the data at offset \a offset
381 */
382 const uint8_t* XsMessage_constData(XsMessage const* thisPtr, XsSize offset)
383 {
384 if (!thisPtr->m_message.m_data)
385 return 0;
386 return XsMessage_cdataAtOffset(thisPtr, offset);
387 }
388
389 /*! \brief This function returns a const pointer to the header of the message in \a thisptr
390
391
392 \returns a pointer to the start of the message
393 */
394 const uint8_t* XsMessage_getMessageStart(XsMessage const* thisPtr)
395 {
396 return thisPtr->m_message.m_data;
397 }
398
399 /*! \brief Return the length of the message buffer.
400
401 The function returns the total size of the message, including the checksum. This
402 is in effect the number of bytes that would be transferred if the message were to
403 be sent over a communications channel.
404
405 \returns the total message size
406 */
407 XsSize XsMessage_getTotalMessageSize(XsMessage const* thisPtr)
408 {
409 XsMessageHeader* hdr;
410
411 if (!thisPtr->m_message.m_data)
412 return 0;
413
414 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
415 if (hdr->m_length == 255)
416 return (((XsSize) hdr->m_datlen.m_extended.m_length.m_high) << 8) + hdr->m_datlen.m_extended.m_length.m_low + 7;
417 else
418 return ((XsSize) hdr->m_length) + 5;
419 }
420
421 /*! \brief Returns the byte value at \a offset in the data of the message
422
423 \param offset the offset in the payload at which to read data
424
425 \returns the byte at offset \a offset in the message payload
426 */
427 uint8_t XsMessage_getDataByte(XsMessage const* thisPtr, XsSize offset)
428 {
429 return *XsMessage_cdataAtOffset(thisPtr, offset);
430 }
431
432 /*! \brief Returns the short value at \a offset in the data of the message
433
434 \param offset the offset in the payload at which to read data
435
436 \returns the 16-bit integer value at offset \a offset in the message payload
437 */
438 uint16_t XsMessage_getDataShort(XsMessage const* thisPtr, XsSize offset)
439 {
440 uint16_t ret;
441 XsMessage_getEndianCorrectData(thisPtr, &ret, sizeof(ret), offset);
442 return ret;
443 }
444
445 /*! \brief Returns the long value at \a offset in the data of the message
446
447 \param offset the offset in the payload at which to read data
448
449 \returns the 32-bit integer value at offset \a offset in the message payload
450 */
451 uint32_t XsMessage_getDataLong(XsMessage const* thisPtr, XsSize offset)
452 {
453 uint32_t ret;
454 XsMessage_getEndianCorrectData(thisPtr, &ret, sizeof(ret), offset);
455 return ret;
456 }
457
458 /*! \brief Returns the long value at \a offset in the data of the message
459
460 \param offset the offset in the payload at which to read data
461
462 \returns the 64-bit integer value at offset \a offset in the message payload
463 */
464 uint64_t XsMessage_getDataLongLong(XsMessage const* thisPtr, XsSize offset)
465 {
466 uint64_t ret;
467 XsMessage_getEndianCorrectData(thisPtr, &ret, sizeof(ret), offset);
468 return ret;
469 }
470
471 /*! \brief Returns the float value at \a offset in the data of the message
472
473 \param offset the offset in the payload at which to read data
474
475 \returns the single precision float value at offset \a offset in the message payload
476 */
477 float XsMessage_getDataFloat(XsMessage const* thisPtr, XsSize offset)
478 {
479 float ret;
480 XsMessage_getEndianCorrectData(thisPtr, &ret, sizeof(ret), offset);
481 return ret;
482 }
483
484 /*! \brief Returns the double at \a offset in the data of the message
485 \param offset the offset in the payload at which to read data
486
487 \returns the double precision floating point value at offset \a offset in the message payload
488 */
489 double XsMessage_getDataDouble(XsMessage const* thisPtr, XsSize offset)
490 {
491 double ret;
492 XsMessage_getEndianCorrectData(thisPtr, &ret, sizeof(ret), offset);
493 return ret;
494 }
495
496 /*! \brief Returns the F12.20 value at \a offset in the data of the message
497 \param offset the offset in the payload at which to read data
498
499 \returns the 12.20 fixed point value at offset \a offset in the message payload
500 */
501 double XsMessage_getDataF1220(XsMessage const* thisPtr, XsSize offset)
502 {
503 int32_t tmp;
504 Itypes rv;
505 tmp = (int32_t) XsMessage_getDataLong(thisPtr, offset);
506
507 rv.d = ((double) tmp)/1048576.0;
508 rv.i64 = (rv.i64 & ~1LL) | (tmp & 1);
509 return rv.d;
510 }
511
512 /*! \brief Returns the F16.32 value at \a offset in the data of the message
513 \param offset the offset in the payload at which to read data
514
515 \returns the 16.32 fixed point value at offset \a offset in the message payload
516 */
517 double XsMessage_getDataFP1632(XsMessage const* thisPtr, XsSize offset)
518 {
519 int16_t fpint;
520 int32_t fpfrac;
521 Itypes fp, rv;
522
523 fpfrac = (int32_t) XsMessage_getDataLong(thisPtr, offset);
524 fpint = (int16_t) XsMessage_getDataShort(thisPtr, offset+4);
525
526 fp.i32.i0 = fpint;
527 fp.i32.i1 = fpfrac;
528
529 rv.d = (double) fp.i64 / 4294967296.0;
530 rv.i64 = (rv.i64 & ~1LL) | (fpfrac & 1);
531 return rv.d;
532 }
533
534 /*! \brief Returns a const pointer to the data buffer of the message
535
536 \param offset the offset in the payload at which to read data
537
538
539 \returns a const pointer to the data buffer of the message
540 */
541 const uint8_t* XsMessage_getDataBuffer(XsMessage const* thisPtr, XsSize offset)
542 {
543 return XsMessage_constData(thisPtr, offset);
544 }
545
546 /*! \brief Set the byte at \a offset in the message to \a value
547
548 \param value the 8-bit value to set
549 \param offset the offset in the message payload at which to write the data
550 */
551 void XsMessage_setDataByte(XsMessage* thisPtr, uint8_t value, XsSize offset)
552 {
553 XsMessage_setEndianCorrectData(thisPtr, &value, sizeof(value), offset);
554 }
555
556 /*! \brief Sets the short at \a offset in the message to \a value
557
558 \param value the 16-bit value to set
559 \param offset the offset in the message payload at which to write the data
560 */
561 void XsMessage_setDataShort(XsMessage* thisPtr, uint16_t value, XsSize offset)
562 {
563 XsMessage_setEndianCorrectData(thisPtr, &value, sizeof(value), offset);
564 }
565
566 /*! \brief Sets the long at \a offset in the message to \a value
567
568 \param value the 32-bit value to set
569 \param offset the offset in the message payload at which to write the data
570 */
571 void XsMessage_setDataLong(XsMessage* thisPtr, uint32_t value, XsSize offset)
572 {
573 XsMessage_setEndianCorrectData(thisPtr, &value, sizeof(value), offset);
574 }
575
576 /*! \brief Sets the long at \a offset in the message to \a value
577
578 \param value the 64-bit value to set
579 \param offset the offset in the message payload at which to write the data
580 */
581 void XsMessage_setDataLongLong(XsMessage* thisPtr, uint64_t value, XsSize offset)
582 {
583 XsMessage_setEndianCorrectData(thisPtr, &value, sizeof(value), offset);
584 }
585
586
587 /*! \brief Sets the float at \a offset in the message to \a value
588
589 \param value the single precision floating point value to set
590 \param offset the offset in the message payload at which to write the data
591 */
592 void XsMessage_setDataFloat(XsMessage* thisPtr, float value, XsSize offset)
593 {
594 XsMessage_setEndianCorrectData(thisPtr, &value, sizeof(value), offset);
595 }
596
597 /*! \brief Sets the double at \a offset in the message to \a value
598
599 \param value the double precision floating point value to set
600 \param offset the offset in the message payload at which to write the data
601 */
602 void XsMessage_setDataDouble(XsMessage* thisPtr, double value, XsSize offset)
603 {
604 XsMessage_setEndianCorrectData(thisPtr, &value, sizeof(value), offset);
605 }
606
607 /*! \brief Sets the F12.20 at \a offset in the message to \a value
608
609 \param value the 12.20 fixed point value to set
610 \param offset the offset in the message payload at which to write the data
611 */
612 void XsMessage_setDataF1220(XsMessage* thisPtr, double value, XsSize offset)
613 {
614 Itypes fp;
615 uint32_t val;
616
617 fp.d = value;
618 val = (uint32_t) (int32_t) (value*1048576.0);
619
620 XsMessage_setDataLong(thisPtr, (val & ~1UL) | (fp.i64 & 1), offset);
621 }
622
623 /*! \brief Sets the F16.32 at \a offset in the message to \a value
624
625 \param value the 16.32 fixed point value to set
626 \param offset the offset in the message payload at which to write the data
627 */
628 void XsMessage_setDataFP1632(XsMessage* thisPtr, double value, XsSize offset)
629 {
630 Itypes fp;
631 int16_t fpint;
632 int32_t fpfrac;
633 int32_t dexp;
634 uint32_t b;
635
636 fp.d = value;
637 b = (uint32_t) (fp.i64 & 1);
638 dexp = ((fp.i32.i0 & (0x7fffffffL)) >> 20) - 1023;
639
640 if (dexp <= 14)
641 {
642 fp.i16.s0 = (fp.i16.s0 & 0x000F) | 0x0010;
643 if (value < 0)
644 fp.i64 = -fp.i64;
645 if (dexp > -32)
646 fp.i64 = fp.i64 >> (20-dexp); // 52-32 - exponent
647 else
648 fp.i64 = fp.i64 >> 52; // this could be optimized?
649 fpint = fp.i16.s1;
650 fpfrac = fp.i32.i1;
651 }
652 else
653 {
654 if (value < 0)
655 {
656 fpint = ((int16_t) (uint16_t) (0x8000));
657 fpfrac = 0;
658 }
659 else
660 {
661 fpint = 0x7fff;
662 fpfrac = -1;
663 }
664 }
665
666 XsMessage_setDataLong(thisPtr, (fpfrac & ~1L) | b, offset);
667 XsMessage_setDataShort(thisPtr, (uint16_t) fpint, offset+(XsSize)4);
668 }
669
670 /*! \brief Puts \a size number of bytes from \a buffer into the message at \a offset
671 \remarks The buffersize of will be increased if \a buffer is too large
672
673 \param buffer the buffer to copy
674 \param size the size of the buffer
675 \param offset the offset at which to copy the data
676 */
677 void XsMessage_setDataBuffer(XsMessage* thisPtr, const uint8_t* buffer, XsSize size, XsSize offset)
678 {
679 XsMessage_ensureDataSize(thisPtr, offset, size);
680 XsMessage_updateChecksumWithValue(thisPtr, buffer, size, offset);
681 memcpy(XsMessage_dataAtOffset(thisPtr, offset), buffer, size);
682 }
683
684 /*! \relates XsMessage
685 \brief Returns the byte size of \a id if the format is a floating point format
686
687 \param id : The XsDataIdentifier to query
688 \returns Returns the byte size of XsDataIdentifier \a id
689 */
690 uint8_t XsMessage_getFPValueSize(XsDataIdentifier id)
691 {
692 switch (id & XDI_SubFormatMask)
693 {
694 case XDI_SubFormatFloat:
695 return 4;
696
697 case XDI_SubFormatDouble:
698 return 8;
699
700 case XDI_SubFormatFp1632:
701 return 6;
702
703 case XDI_SubFormatFp1220:
704 return 4;
705
706 default:
707 return 0;
708 }
709 }
710
711 /*! \returns a double converted from float \a f
712
713 \param[in] f The float to convert
714 */
715 static double convertFromFloat(float f)
716 {
717 Itypes rv, tmp;
718 tmp.f32.f0 = f;
719 rv.d = (double) f;
720 rv.i64 = (rv.i64 & ~1LL) | (tmp.i32.i0 & 1);
721 return rv.d;
722 }
723
724 /*! \returns a float converted from double \a d
725
726 \param[in] d The double to cenvert
727 */
728 static float convertToFloat(double d)
729 {
730 Itypes rv, tmp;
731 tmp.d = d;
732 rv.f32.f0 = (float) d;
733 rv.i32.i0 = (rv.i32.i0 & ~1L) | (tmp.i64 & 1);
734 return rv.f32.f0;
735 }
736
737 /*! \brief Return current data values as double, conversion depends on outputSetting.
738
739 \param dest destination array
740 \param dataIdentifier Data identifier containing data precision
741 \param offset offset in the data buffer from where to start reading.
742 \param numValues number of values to be read
743 */
744 void XsMessage_getDataFPValuesById(XsMessage const* thisPtr, XsDataIdentifier dataIdentifier, double *dest, XsSize offset, XsSize numValues)
745 {
746 XsSize i;
747 for (i=0; i<numValues; i++)
748 {
749 switch (dataIdentifier & XDI_SubFormatMask)
750 {
751 case XDI_SubFormatFloat:
752 *dest++ = convertFromFloat(XsMessage_getDataFloat(thisPtr, offset));
753 offset += 4;
754 break;
755
756 case XDI_SubFormatDouble:
757 *dest++ = XsMessage_getDataDouble(thisPtr, offset);
758 offset += 8;
759 break;
760
761 case XDI_SubFormatFp1632:
762 *dest++ = XsMessage_getDataFP1632(thisPtr, offset);
763 offset += 6;
764 break;
765
766 case XDI_SubFormatFp1220:
767 *dest++ = XsMessage_getDataF1220(thisPtr, offset);
768 offset += 4;
769 break;
770
771 default:
772 *dest++ = 0;
773 break;
774 }
775 }
776 }
777
778 /*! \brief Write a number of floating/fixed point values into to the data buffer, conversion depends on outputSettings
779
780 \param dataIdentifier Data Identifier
781 \param data The data array to be written to the buffer.
782 \param offset Offset in the data buffer from where to start writing.
783 \param numValues number of values to be written
784 */
785 void XsMessage_setDataFPValuesById(XsMessage* thisPtr, XsDataIdentifier dataIdentifier, double const*data, XsSize offset, XsSize numValues)
786 {
787 XsSize i;
788 for (i=0; i<numValues; i++)
789 {
790 switch (dataIdentifier & XDI_SubFormatMask)
791 {
792 case XDI_SubFormatFloat:
793 XsMessage_setDataFloat(thisPtr, convertToFloat(data[i]), offset);
794 offset += 4;
795 break;
796
797 case XDI_SubFormatDouble:
798 XsMessage_setDataDouble(thisPtr, data[i], offset);
799 offset += 8;
800 break;
801
802 case XDI_SubFormatFp1632:
803 XsMessage_setDataFP1632(thisPtr, data[i], offset);
804 offset += 6;
805 break;
806
807 case XDI_SubFormatFp1220:
808 XsMessage_setDataF1220(thisPtr, data[i], offset);
809 offset += 4;
810 break;
811
812 default:
813 break;
814 }
815 }
816 }
817
818 /*! \brief Return current data values as XsReal, conversion is done automatically based on data identifier.
819
820 This function behaves exactly like XsMessage_getDataFPValuesById, with the exception that it expects an XsReal array.
821
822 \param dest destination array
823 \param dataIdentifier Data identifier containing data precision
824 \param offset offset in the data buffer from where to start reading.
825 \param numValues number of values to be read
826 */
827 void XsMessage_getDataRealValuesById(XsMessage const* thisPtr, XsDataIdentifier dataIdentifier, XsReal *dest, XsSize offset, XsSize numValues)
828 {
829 XsSize i;
830 for (i=0; i<numValues; i++)
831 {
832 switch (dataIdentifier & XDI_SubFormatMask)
833 {
834 case XDI_SubFormatFloat:
835 #ifdef XSENS_SINGLE_PRECISION
836 *dest++ = XsMessage_getDataFloat(thisPtr, offset);
837 #else
838 *dest++ = convertFromFloat(XsMessage_getDataFloat(thisPtr, offset));
839 #endif
840 offset += 4;
841 break;
842
843 case XDI_SubFormatDouble:
844 #ifdef XSENS_SINGLE_PRECISION
845 *dest++ = convertToFloat(XsMessage_getDataDouble(thisPtr, offset));
846 #else
847 *dest++ = XsMessage_getDataDouble(thisPtr, offset);
848 #endif
849 offset += 8;
850 break;
851
852 case XDI_SubFormatFp1632:
853 #ifdef XSENS_SINGLE_PRECISION
854 *dest++ = convertToFloat(XsMessage_getDataFP1632(thisPtr, offset));
855 #else
856 *dest++ = XsMessage_getDataFP1632(thisPtr, offset);
857 #endif
858 offset += 6;
859 break;
860
861 case XDI_SubFormatFp1220:
862 #ifdef XSENS_SINGLE_PRECISION
863 *dest++ = convertToFloat(XsMessage_getDataF1220(thisPtr, offset));
864 #else
865 *dest++ = XsMessage_getDataF1220(thisPtr, offset);
866 #endif
867 offset += 4;
868 break;
869
870 default:
871 *dest++ = 0;
872 break;
873 }
874 }
875 }
876
877 /*! \brief Write a number of floating/fixed point values into to the data buffer, conversion depends on data identifier
878
879 \param dataIdentifier Data Identifier
880 \param data The data array to be written to the buffer.
881 \param offset Offset in the data buffer from where to start writing.
882 \param numValues number of values to be written
883 */
884 void XsMessage_setDataRealValuesById(XsMessage* thisPtr, XsDataIdentifier dataIdentifier, XsReal const*data, XsSize offset, XsSize numValues)
885 {
886 XsSize i;
887 for (i=0; i<numValues; i++)
888 {
889 switch (dataIdentifier & XDI_SubFormatMask)
890 {
891 case XDI_SubFormatFloat:
892 XsMessage_setDataFloat(thisPtr, convertToFloat(data[i]), offset);
893 offset += 4;
894 break;
895
896 case XDI_SubFormatDouble:
897 XsMessage_setDataDouble(thisPtr, data[i], offset);
898 offset += 8;
899 break;
900
901 case XDI_SubFormatFp1632:
902 XsMessage_setDataFP1632(thisPtr, data[i], offset);
903 offset += 6;
904 break;
905
906 case XDI_SubFormatFp1220:
907 XsMessage_setDataF1220(thisPtr, data[i], offset);
908 offset += 4;
909 break;
910
911 default:
912 break;
913 }
914 }
915 }
916
917 /*! \brief Computes the checksum for the message */
918 uint8_t XsMessage_computeChecksum(XsMessage const* thisPtr)
919 {
920 XsSize i, msgSize;
921 uint8_t cs = 0;
922
923 msgSize = XsMessage_getTotalMessageSize(thisPtr)-1;
924 for (i = 1; i < msgSize; ++i)
925 cs -= thisPtr->m_message.m_data[i];
926
927 return cs;
928 }
929
930 /*! \brief Update the checksum for the message
931 */
932 void XsMessage_recomputeChecksum(XsMessage* thisPtr)
933 {
934 assert(thisPtr->m_checksum);
935 thisPtr->m_checksum[0] = XsMessage_computeChecksum(thisPtr);
936 }
937
938 /*! \brief Returns non-zero if the checksum inside the message is correct for the message, zero otherwise
939 \returns true (non-zero) if the checksum inside the message is correct, false (zero) otherwise
940 */
941 int XsMessage_isChecksumOk(XsMessage const* thisPtr)
942 {
943 assert(thisPtr->m_checksum);
944 return thisPtr->m_checksum[0] == XsMessage_computeChecksum(thisPtr);
945 }
946
947 /*! \brief Returns a pointer to the message header for this message */
948 XsMessageHeader* XsMessage_getHeader(XsMessage* thisPtr)
949 {
950 return (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
951 }
952
953 /*! \brief \returns a const pointer to the message header */
954 const XsMessageHeader* XsMessage_getConstHeader(XsMessage const* thisPtr)
955 {
956 return (const XsMessageHeader*) (void*) thisPtr->m_message.m_data;
957 }
958
959 /*! \brief Test if this message is empty
960 \returns non-zero if this message is empty, zero otherwise
961 */
962 int XsMessage_empty(XsMessage const* thisPtr)
963 {
964 XsMessageHeader* hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
965 if (!hdr)
966 return 1;
967 return (hdr->m_messageId == 0 && hdr->m_busId == XS_BID_MASTER);
968 }
969
970 /*! \brief Resize the buffer of message to \a newSize bytes
971
972 \param newSize the new size of the message payload buffer
973 */
974 void XsMessage_resizeData(XsMessage* thisPtr, XsSize newSize)
975 {
976 XsSize index, oldSize;
977 XsByteArray old = XSBYTEARRAY_INITIALIZER;
978 XsMessageHeader* oldHdr, *newHdr;
979 uint8_t* oldData, *newData;
980
981 oldSize = XsMessage_dataSize(thisPtr);
982 if (oldSize == newSize)
983 return;
984
985 XsArray_swap(&thisPtr->m_message, &old);
986 XsMessage_constructSized(thisPtr, newSize);
987
988 newHdr = (XsMessageHeader*) thisPtr->m_message.m_data;
989 oldHdr = (XsMessageHeader*) old.m_data;
990 if (!oldHdr) // our original message may have been empty / uninitialized
991 return;
992
993 if (thisPtr->m_autoUpdateChecksum)
994 thisPtr->m_checksum[0] += newHdr->m_busId;
995
996 newHdr->m_busId = oldHdr->m_busId;
997 newHdr->m_preamble = oldHdr->m_preamble;
998 newHdr->m_messageId = oldHdr->m_messageId;
999
1000 if (thisPtr->m_autoUpdateChecksum)
1001 thisPtr->m_checksum[0] -= newHdr->m_busId + newHdr->m_messageId;
1002
1003 if (oldSize >= XS_EXTLENCODE)
1004 oldData = oldHdr->m_datlen.m_extended.m_data;
1005 else
1006 oldData = oldHdr->m_datlen.m_data;
1007
1008 if (newSize >= XS_EXTLENCODE)
1009 newData = newHdr->m_datlen.m_extended.m_data;
1010 else
1011 newData = newHdr->m_datlen.m_data;
1012
1013 if (oldSize > newSize)
1014 oldSize = newSize; // speed up the loop
1015 for (index = 0; index < oldSize; ++index)
1016 newData[index] = oldData[index];
1017
1018 if (thisPtr->m_autoUpdateChecksum)
1019 thisPtr->m_checksum[0] -= byteSum(oldData, oldSize);
1020
1021 XsArray_destruct(&old);
1022 }
1023
1024 /*! \brief Set the bus id for this message to \a busId
1025
1026 \param busId the bus identifier
1027
1028 \sa XS_BID_BROADCAST XS_BID_MASTER XS_BID_MT
1029 */
1030 void XsMessage_setBusId(XsMessage* thisPtr, uint8_t busId)
1031 {
1032 XsMessageHeader* hdr;
1033 if (!thisPtr->m_message.m_data)
1034 XsMessage_construct(thisPtr);
1035
1036 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
1037 if (thisPtr->m_autoUpdateChecksum)
1038 thisPtr->m_checksum[0] += hdr->m_busId;
1039 hdr->m_busId = busId;
1040 if (thisPtr->m_autoUpdateChecksum)
1041 thisPtr->m_checksum[0] -= hdr->m_busId;
1042 }
1043
1044 /*! \brief Set the message id for this message to \a msgId
1045
1046 \param msgId the message identifier
1047 */
1048 void XsMessage_setMessageId(XsMessage* thisPtr, enum XsXbusMessageId msgId)
1049 {
1050 XsMessageHeader* hdr;
1051 if (!thisPtr->m_message.m_data)
1052 XsMessage_construct(thisPtr);
1053
1054 hdr = (XsMessageHeader*) (void*) thisPtr->m_message.m_data;
1055 if (thisPtr->m_autoUpdateChecksum)
1056 thisPtr->m_checksum[0] += hdr->m_messageId;
1057 hdr->m_messageId = (uint8_t) msgId;
1058 if (thisPtr->m_autoUpdateChecksum)
1059 thisPtr->m_checksum[0] -= hdr->m_messageId;
1060 }
1061
1062 /*! \brief Create \a count bytes of empty space at \a offset in this message
1063
1064 \param count the number of bytes to reserve
1065 \param offset the offset at which to allocate the space
1066 */
1067 void XsMessage_insertData(XsMessage* thisPtr, XsSize count, XsSize offset)
1068 {
1069 XsSize index, oldSize, newSize;
1070 XsByteArray old = XSBYTEARRAY_INITIALIZER;
1071 XsMessageHeader* oldHdr, *newHdr;
1072 uint8_t* oldData, *newData;
1073
1074 if (!count)
1075 return;
1076
1077 oldSize = XsMessage_dataSize(thisPtr);
1078 newSize = oldSize+count;
1079
1080 if (newSize < offset+count)
1081 newSize = offset+count;
1082
1083 XsArray_swap(&thisPtr->m_message, &old);
1084 XsMessage_constructSized(thisPtr, newSize);
1085
1086 newHdr = (XsMessageHeader*) thisPtr->m_message.m_data;
1087 oldHdr = (XsMessageHeader*) old.m_data;
1088 if (!oldHdr) // our original message may have been empty / uninitialized
1089 return;
1090
1091 newHdr->m_busId = oldHdr->m_busId;
1092 newHdr->m_preamble = oldHdr->m_preamble;
1093 newHdr->m_messageId = oldHdr->m_messageId;
1094
1095 if (oldSize >= XS_EXTLENCODE)
1096 oldData = &oldHdr->m_datlen.m_extended.m_data[0];
1097 else
1098 oldData = &oldHdr->m_datlen.m_data[0];
1099
1100 if (newSize >= XS_EXTLENCODE)
1101 newData = &newHdr->m_datlen.m_extended.m_data[0];
1102 else
1103 newData = &newHdr->m_datlen.m_data[0];
1104
1105 if (offset <= oldSize)
1106 {
1107 for (index = 0; index < offset; ++index)
1108 newData[index] = oldData[index];
1109 }
1110 else
1111 {
1112 for (index = 0; index < oldSize; ++index)
1113 newData[index] = oldData[index];
1114 for (; index < offset; ++index)
1115 newData[index] = 0;
1116 }
1117 for (; index < oldSize; ++index)
1118 newData[index+count] = oldData[index];
1119
1120 if (thisPtr->m_autoUpdateChecksum)
1121 thisPtr->m_checksum[0] -= byteSum(oldData, oldSize);
1122
1123 XsArray_destruct(&old);
1124 }
1125
1126 /*! \brief Remove \a count bytes of data from the message at \a offset
1127
1128 \param count the number of bytes to remove
1129 \param offset the offset at which to remove the bytes
1130 */
1131 void XsMessage_deleteData(XsMessage* thisPtr, XsSize count, XsSize offset)
1132 {
1133 XsSize index, oldSize, newSize;
1134 XsByteArray old = XSBYTEARRAY_INITIALIZER;
1135 XsMessageHeader* oldHdr, *newHdr;
1136 uint8_t* oldData, *newData;
1137
1138 oldSize = XsMessage_dataSize(thisPtr);
1139 if (!count || offset >= oldSize)
1140 return;
1141
1142 if (offset + count >= oldSize)
1143 {
1144 XsMessage_resizeData(thisPtr, offset);
1145 return;
1146 }
1147 newSize = oldSize-count;
1148
1149 XsArray_swap(&thisPtr->m_message, &old);
1150 XsMessage_constructSized(thisPtr, newSize);
1151
1152 newHdr = (XsMessageHeader*) thisPtr->m_message.m_data;
1153 oldHdr = (XsMessageHeader*) old.m_data;
1154 if (!oldHdr) // our original message may have been empty / uninitialized
1155 return;
1156
1157 newHdr->m_busId = oldHdr->m_busId;
1158 newHdr->m_preamble = oldHdr->m_preamble;
1159 newHdr->m_messageId = oldHdr->m_messageId;
1160
1161 if (oldSize >= XS_EXTLENCODE)
1162 oldData = oldHdr->m_datlen.m_extended.m_data;
1163 else
1164 oldData = oldHdr->m_datlen.m_data;
1165
1166 if (newSize >= XS_EXTLENCODE)
1167 newData = newHdr->m_datlen.m_extended.m_data;
1168 else
1169 newData = newHdr->m_datlen.m_data;
1170
1171 for (index = 0; index < offset; ++index)
1172 newData[index] = oldData[index];
1173 for (; index < newSize; ++index)
1174 newData[index] = oldData[index+count];
1175
1176 if (thisPtr->m_autoUpdateChecksum)
1177 thisPtr->m_checksum[0] -= byteSum(newData, newSize);
1178
1179 XsArray_destruct(&old);
1180 }
1181
1182 /*! \brief Swap the contents of \a a and \a b
1183
1184 \details This function swaps the internal buffers so no actual data is moved around.
1185 A result is that it won't work for unmanaged data such as fixed size vectors
1186
1187 \param a the object to receive \a b's contents
1188 \param b the object to receive \a a's contents
1189 */
1190 void XsMessage_swap(XsMessage* a, XsMessage* b)
1191 {
1192 XsMessage tmp;
1193
1194 *((uint8_t**)&tmp.m_checksum) = a->m_checksum;
1195 tmp.m_autoUpdateChecksum = a->m_autoUpdateChecksum;
1196
1197 *((uint8_t**)&a->m_checksum) = b->m_checksum;
1198 a->m_autoUpdateChecksum = b->m_autoUpdateChecksum;
1199
1200 *((uint8_t**)&b->m_checksum) = tmp.m_checksum;
1201 b->m_autoUpdateChecksum = tmp.m_autoUpdateChecksum;
1202
1203 XsArray_swap(&a->m_message, &b->m_message);
1204 }
1205
1206 /*! \brief Compare the contents of the messages \a a and \a b, returning non-0 if they are different
1207 */
1208 int XsMessage_compare(XsMessage const* a, XsMessage const* b)
1209 {
1210 return XsArray_compare(&a->m_message, &b->m_message);
1211 }
1212
1213 /*! \brief Return a string containing the first \a maxBytes bytes of the message in hex format
1214 \param maxBytes the maximum number of bytes to include in the string, when set to 0, the full message will be used
1215 \param resultValue the resulting string
1216 */
1217 void XsMessage_toHexString(XsMessage const* thisPtr, XsSize maxBytes, XsString* resultValue)
1218 {
1219 char* s;
1220 XsSize i = XsMessage_getTotalMessageSize(thisPtr);
1221 if (maxBytes == 0 || maxBytes > i)
1222 maxBytes = i;
1223
1224 if (maxBytes)
1225 {
1226 XsString_resize(resultValue, maxBytes*3-1);
1227 s = (char*) resultValue->m_data;
1228 for (i = 0; i < maxBytes-1; ++i)
1229 sprintf(s+(i*3), "%02X ", (unsigned int) ((uint8_t const*) thisPtr->m_message.m_data)[i]);
1230 sprintf(s+((maxBytes-1)*3), "%02X", (unsigned int) ((uint8_t const*) thisPtr->m_message.m_data)[maxBytes-1]);
1231 }
1232 else
1233 XsString_resize(resultValue, 0);
1234 }
1235
1236 /*! @} */