xbus.c
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 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 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #include "xbus.h"
66 
67 
70 bool Xbus_checkPreamble(const uint8_t* xbusMessage)
71 {
72  return xbusMessage[OFFSET_TO_PREAMBLE] == XBUS_PREAMBLE;
73 }
74 
77 int Xbus_getBusId(const uint8_t* xbusMessage)
78 {
79  return (xbusMessage[OFFSET_TO_BID] & 0xff);
80 }
81 
84 void Xbus_setBusId(uint8_t* xbusMessage, uint8_t busId)
85 {
86  xbusMessage[OFFSET_TO_BID] = busId & 0xff;
87 }
88 
91 int Xbus_getMessageId(const uint8_t* xbusMessage)
92 {
93  return (xbusMessage[OFFSET_TO_MID] & 0xff);
94 }
95 
98 void Xbus_setMessageId(uint8_t* xbusMessage, uint8_t messageId)
99 {
100  xbusMessage[OFFSET_TO_MID] = messageId & 0xff;
101 }
102 
105 int Xbus_getPayloadLength(const uint8_t* xbusMessage)
106 {
107  int length = xbusMessage[OFFSET_TO_LEN] & 0xff;
109  return length;
110  else
111  {
112  int result = (xbusMessage[OFFSET_TO_LEN + 2] & 0xff);
113  result += (xbusMessage[OFFSET_TO_LEN + 1] & 0xff) << 8;
114  return result;
115  }
116 }
117 
120 void Xbus_setPayloadLength(uint8_t* xbusMessage, uint16_t payloadLength)
121 {
122  if (payloadLength < 255)
123  xbusMessage[OFFSET_TO_LEN] = payloadLength & 0xff;
124  else
125  {
126  xbusMessage[OFFSET_TO_LEN] = LENGTH_EXTENDER_BYTE;
127  xbusMessage[OFFSET_TO_LEN + 1] = (payloadLength >> 8) & 0xff;
128  xbusMessage[OFFSET_TO_LEN + 2] = payloadLength & 0xff;
129  }
130 }
131 
134 void Xbus_message(uint8_t* xbusMessage, uint8_t bid, uint8_t mid, uint16_t len)
135 {
136  xbusMessage[0] = 0xFA;
137  Xbus_setBusId(xbusMessage, bid);
138  Xbus_setMessageId(xbusMessage, mid);
139  Xbus_setPayloadLength(xbusMessage, len);
140 }
141 
144 int Xbus_getRawLength(const uint8_t* xbusMessage)
145 {
146  int rtrn = Xbus_getPayloadLength(xbusMessage);
147 
148  if ((xbusMessage[OFFSET_TO_LEN] & 0xff) == LENGTH_EXTENDER_BYTE)
149  rtrn += 7;
150  else
151  rtrn += 5;
152  return rtrn;
153 }
154 
157 uint8_t* Xbus_getPointerToPayload(uint8_t* xbusMessage)
158 {
159  if ((xbusMessage[OFFSET_TO_LEN] & 0xff) == LENGTH_EXTENDER_BYTE)
160  return xbusMessage + OFFSET_TO_PAYLOAD_EXT;
161  else
162  return xbusMessage + OFFSET_TO_PAYLOAD;
163 }
164 
167 uint8_t const* Xbus_getConstPointerToPayload(uint8_t const* xbusMessage)
168 {
169  return Xbus_getPointerToPayload((uint8_t*)xbusMessage);
170 }
171 
172 
175 void Xbus_insertChecksum(uint8_t* xbusMessage)
176 {
177  int nBytes = Xbus_getRawLength(xbusMessage);
178 
179  uint8_t checksum = 0;
180  for (int i = 0; i < nBytes - 2; i++)
181  checksum -= xbusMessage[1 + i];
182 
183  xbusMessage[nBytes - 1] = checksum;
184 }
185 
188 bool Xbus_verifyChecksum(const uint8_t* xbusMessage)
189 {
190  int nBytes = Xbus_getRawLength(xbusMessage);
191  uint8_t checksum = 0;
192  for (int n = 1; n < nBytes; n++)
193  checksum += (xbusMessage[n] & 0xff);
194  checksum &= 0xff;
195  return (checksum == 0);
196 }
197 
198 
Xbus_getBusId
int Xbus_getBusId(const uint8_t *xbusMessage)
Returns xbus Bus identifier.
Definition: xbus.c:77
Xbus_checkPreamble
bool Xbus_checkPreamble(const uint8_t *xbusMessage)
Returns true if the preamble equeals 0xFA, false othersise.
Definition: xbus.c:70
Xbus_setBusId
void Xbus_setBusId(uint8_t *xbusMessage, uint8_t busId)
Sets xbus Bus identifier.
Definition: xbus.c:84
OFFSET_TO_PAYLOAD
#define OFFSET_TO_PAYLOAD
Definition: xbus.h:83
LENGTH_EXTENDER_BYTE
#define LENGTH_EXTENDER_BYTE
Definition: xbus.h:86
OFFSET_TO_MID
#define OFFSET_TO_MID
Definition: xbus.h:79
OFFSET_TO_PREAMBLE
#define OFFSET_TO_PREAMBLE
Definition: xbus.h:77
Xbus_verifyChecksum
bool Xbus_verifyChecksum(const uint8_t *xbusMessage)
Verifies the checksum of aon xbus message.
Definition: xbus.c:188
xbus.h
Xbus_setMessageId
void Xbus_setMessageId(uint8_t *xbusMessage, uint8_t messageId)
Sets xbus Message identifier.
Definition: xbus.c:98
Xbus_setPayloadLength
void Xbus_setPayloadLength(uint8_t *xbusMessage, uint16_t payloadLength)
Sets xbus message (payload) length.
Definition: xbus.c:120
Xbus_insertChecksum
void Xbus_insertChecksum(uint8_t *xbusMessage)
Inserts the correct checksum in xbus message.
Definition: xbus.c:175
Xbus_getConstPointerToPayload
uint8_t const * Xbus_getConstPointerToPayload(uint8_t const *xbusMessage)
Returns a const pointer to payload of an xbus message.
Definition: xbus.c:167
OFFSET_TO_PAYLOAD_EXT
#define OFFSET_TO_PAYLOAD_EXT
Definition: xbus.h:84
OFFSET_TO_LEN
#define OFFSET_TO_LEN
Definition: xbus.h:80
Xbus_getRawLength
int Xbus_getRawLength(const uint8_t *xbusMessage)
Returns total length of xbus message (header + payload + checksum)
Definition: xbus.c:144
length
TF2SIMD_FORCE_INLINE tf2Scalar length(const Quaternion &q)
Xbus_message
void Xbus_message(uint8_t *xbusMessage, uint8_t bid, uint8_t mid, uint16_t len)
Initialize a xbus message with BID, MID and Length.
Definition: xbus.c:134
OFFSET_TO_BID
#define OFFSET_TO_BID
Definition: xbus.h:78
Xbus_getPointerToPayload
uint8_t * Xbus_getPointerToPayload(uint8_t *xbusMessage)
Returns pointer to payload of an xbus message.
Definition: xbus.c:157
XBUS_PREAMBLE
#define XBUS_PREAMBLE
Definition: xbus.h:87
Xbus_getPayloadLength
int Xbus_getPayloadLength(const uint8_t *xbusMessage)
Returns xbus message (payload) length.
Definition: xbus.c:105
Xbus_getMessageId
int Xbus_getMessageId(const uint8_t *xbusMessage)
Returns xbus Message identifier.
Definition: xbus.c:91


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20