xbusparser.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 "xbusparser.h"
66 #include <stdio.h>
67 #include <stdbool.h>
68 #include "xbus.h"
69 
70 
73 void XbusParser_init(XbusParser* obj, unsigned char* buffer, int bufferSize)
74 {
75  obj->m_rxBufferSize = bufferSize;
76  obj->m_rxBuffer = buffer;
77  obj->m_idxWrite = 0;
78  obj->m_state = haveNothing;
79  obj->m_lastError = RES_Ok;
80 }
81 
82 
88 enum Result XbusParser_insertByte(XbusParser* obj, unsigned char byte, int* messageSize)
89 {
90  enum Result result = RES_Ok;
91  bool incrementIdxWrite = true;
92 
93  if (obj->m_state != haveNothing) // if we have at least a preamble,
94  obj->m_rxBuffer[obj->m_idxWrite] = byte; // store the received byte in the buffer.
95 
96  switch (obj->m_state)
97  {
98  case haveNothing:
99  if (byte == XBUS_PREAMBLE) // if we have a preamble,
100  {
101  obj->m_idxWrite = 0; // reset the write index
102  obj->m_rxBuffer[obj->m_idxWrite] = byte; // store the byte
103  obj->m_state = havePreamble; // update our state
104  obj->m_totalLength = 1; // reset m_totalLength
105  obj->m_checksum = 0; // reset m_checksum
106  result = RES_FoundPreamble;
107  }
108  else
109  {
110  incrementIdxWrite = false; // don't increment m_idxWrite if we are not in a xbus message
111  result = RES_SpuriousByte;
112  }
113  break;
114 
115  case havePreamble:
116  obj->m_totalLength++;
117  obj->m_checksum += obj->m_rxBuffer[obj->m_idxWrite];
118  if (obj->m_totalLength == OFFSET_TO_LEN + 1)
119  {
120  if (obj->m_rxBuffer[obj->m_idxWrite] != LENGTH_EXTENDER_BYTE)
121  {
122  obj->m_state = haveLength;
123  obj->m_payloadLength = obj->m_rxBuffer[obj->m_idxWrite];
125  }
126  else
127  {
130  }
131  }
132  break;
133 
135  obj->m_totalLength++;
136  obj->m_checksum += obj->m_rxBuffer[obj->m_idxWrite];
137  if (obj->m_totalLength == OFFSET_TO_LEN + 2)
138  obj->m_payloadLength = 256 * obj->m_rxBuffer[obj->m_idxWrite];
139  if (obj->m_totalLength == OFFSET_TO_LEN + 3)
140  {
141  obj->m_payloadLength += obj->m_rxBuffer[obj->m_idxWrite];
142  obj->m_state = haveLength;
143  }
144  break;
145 
146  case haveLength:
147  obj->m_totalLength++;
148  obj->m_checksum += obj->m_rxBuffer[obj->m_idxWrite];
149  if (obj->m_totalLength == (obj->m_offsetToPayload + obj->m_payloadLength + 1)) // if the current byte is the checksum
150  {
151  if ((obj->m_checksum & 0x00FF) == 0) // if checksum ok
152  obj->m_state = haveMessage;
153  else // if checksum error,
154  {
155  obj->m_state = haveNothing;
156  return RES_CheckSumError;
157  }
158  }
159  break;
160 
161  case haveMessage:
162  break;
163 
164  case haveError:
165  return obj->m_lastError;
166 
167  default:
168  break;
169  }
170 
171  if (obj->m_state == haveMessage)
172  {
173  *messageSize = obj->m_totalLength;
174  return RES_MessageReceived;
175  }
176 
177  if (incrementIdxWrite)
178  {
179  if (++obj->m_idxWrite >= obj->m_rxBufferSize)
180  {
181  //Error condition if no packet received yet (report buffer overflow, restart ? Keep parser in error state)
182  //User must restart the parser to clear error state
183  obj->m_state = haveError;
185  return obj->m_lastError;
186  }
187  }
188 
189  return result;
190 }
191 
192 
XbusParser::m_payloadLength
int m_payloadLength
Definition: xbusparser.h:101
OFFSET_TO_PAYLOAD
#define OFFSET_TO_PAYLOAD
Definition: xbus.h:83
RES_SpuriousByte
@ RES_SpuriousByte
Definition: xbusparser.h:90
LENGTH_EXTENDER_BYTE
#define LENGTH_EXTENDER_BYTE
Definition: xbus.h:86
XbusParser::m_lastError
enum Result m_lastError
Definition: xbusparser.h:104
XbusParser_insertByte
enum Result XbusParser_insertByte(XbusParser *obj, unsigned char byte, int *messageSize)
Insert a new byte in the parser.
Definition: xbusparser.c:88
haveNothing
@ haveNothing
Definition: xbusparser.h:74
RES_CheckSumError
@ RES_CheckSumError
Definition: xbusparser.h:87
XbusParser::m_state
enum State m_state
Definition: xbusparser.h:105
xbusparser.h
xbus.h
XbusParser::m_checksum
unsigned short m_checksum
Definition: xbusparser.h:103
XbusParser::m_rxBufferSize
int m_rxBufferSize
Definition: xbusparser.h:98
XbusParser::m_idxWrite
int m_idxWrite
Definition: xbusparser.h:99
XbusParser_init
void XbusParser_init(XbusParser *obj, unsigned char *buffer, int bufferSize)
Initializes an XbusParser instance.
Definition: xbusparser.c:73
XbusParser::m_rxBuffer
unsigned char * m_rxBuffer
Definition: xbusparser.h:97
RES_MessageReceived
@ RES_MessageReceived
Definition: xbusparser.h:85
XbusParser::m_offsetToPayload
int m_offsetToPayload
Definition: xbusparser.h:102
haveLengthExtenderByte
@ haveLengthExtenderByte
Definition: xbusparser.h:76
RES_BufferOverflow
@ RES_BufferOverflow
Definition: xbusparser.h:86
OFFSET_TO_PAYLOAD_EXT
#define OFFSET_TO_PAYLOAD_EXT
Definition: xbus.h:84
RES_FoundPreamble
@ RES_FoundPreamble
Definition: xbusparser.h:89
OFFSET_TO_LEN
#define OFFSET_TO_LEN
Definition: xbus.h:80
XbusParser
XbusParser object definition.
Definition: xbusparser.h:95
haveLength
@ haveLength
Definition: xbusparser.h:77
haveMessage
@ haveMessage
Definition: xbusparser.h:78
havePreamble
@ havePreamble
Definition: xbusparser.h:75
Result
Result
Definition: xbusparser.h:82
RES_Ok
@ RES_Ok
Definition: xbusparser.h:84
XbusParser::m_totalLength
int m_totalLength
Definition: xbusparser.h:100
haveError
@ haveError
Definition: xbusparser.h:79
XBUS_PREAMBLE
#define XBUS_PREAMBLE
Definition: xbus.h:87


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