nmea_protocolhandler.cpp
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 "nmea_protocolhandler.h"
66 #include "xscontrollerconfig.h"
67 
68 namespace nmea
69 {
70 
71 const char PREAMBLE = '$';
72 const char PREAMBLE_TSS2 = ':';
73 const char PREAMBLE_EM1000 = '\0';
74 const unsigned char AFTER_PREAMBLE_EM1000 = 0x90;
75 const char CR = '\r';
76 const char LF = '\n';
77 
85 {
86 }
87 
91 {
92 }
93 
97 {
98  type = XPT_Nmea;
99  MessageLocation location(-1, 0, -1, 0);
100 
101  int bufferSize = (int)raw.size();
102  if (bufferSize == 0)
103  return location;
104 
105  const unsigned char* buffer = raw.data();
106 
107  bool foundPreamble = false;
108  bool foundEnd = false;
109  int start = 0;
110  int end = 0;
111  for (int i = 0; i < bufferSize; i++)
112  {
113  if (!foundPreamble)
114  {
115  if ((buffer[i] == PREAMBLE) || (buffer[i] == PREAMBLE_TSS2) || (buffer[i] == PREAMBLE_EM1000))
116  {
117  foundPreamble = true;
118  start = i;
119  }
120  }
121  if (foundPreamble)
122  {
123  if ((buffer[i] == LF) && (buffer[i - 1] == CR))
124  {
125  foundEnd = true;
126  end = i + 1;
127  }
128  else if ((buffer[i] == AFTER_PREAMBLE_EM1000) && (buffer[i - 1] == PREAMBLE_EM1000)) // special case EM1000
129  {
130  if ((i - start) == 1 && (i + 9) < bufferSize)
131  {
132  foundEnd = true;
133  end = i + 9;
134  }
135  else
136  foundPreamble = false;
137  }
138  }
139  if (foundPreamble && foundEnd)
140  break;
141  }
142 
143  location.m_startPos = start;
144  location.m_size = end - start;
145 
146  return location;
147 }
148 
152 {
153  (void)location;
154  (void)raw;
155  return XsMessage();
156 }
157 
161 {
162  return MINIMUM_MESSAGE_SIZE;
163 }
164 
168 {
169  return MAXIMUM_MESSAGE_SIZE;
170 }
171 
174 {
175  return XPT_Nmea;
176 }
177 
178 }
XsMessage
struct XsMessage XsMessage
Definition: xsmessage.h:85
XsByteArray
A list of uint8_t values.
nmea::AFTER_PREAMBLE_EM1000
const unsigned char AFTER_PREAMBLE_EM1000
Definition: nmea_protocolhandler.cpp:74
nmea::ProtocolHandler::findMessage
MessageLocation findMessage(XsProtocolType &type, const XsByteArray &raw) const override
Find the first message in the raw byte stream.
Definition: nmea_protocolhandler.cpp:96
nmea
Definition: nmea_protocolhandler.cpp:68
nmea::PREAMBLE
const char PREAMBLE
Definition: nmea_protocolhandler.cpp:71
nmea::ProtocolHandler::MAXIMUM_MESSAGE_SIZE
static const int MAXIMUM_MESSAGE_SIZE
Definition: nmea_protocolhandler.h:90
nmea_protocolhandler.h
nmea::LF
const char LF
Definition: nmea_protocolhandler.cpp:76
XPT_Nmea
@ XPT_Nmea
The NMEA protocol, only the messages that can be sent from Xsens devices are recognized.
Definition: xsprotocoltype.h:75
nmea::ProtocolHandler::type
int type() const override
Definition: nmea_protocolhandler.cpp:173
MessageLocation::m_size
int m_size
The size of the message, when less than 0 it indicates the expected message size.
Definition: messagelocation.h:74
nmea::CR
const char CR
Definition: nmea_protocolhandler.cpp:75
XsMessage
Structure for storing a single message.
Definition: xsmessage.h:202
start
ROSCPP_DECL void start()
nmea::PREAMBLE_EM1000
const char PREAMBLE_EM1000
Definition: nmea_protocolhandler.cpp:73
nmea::ProtocolHandler::MINIMUM_MESSAGE_SIZE
static const int MINIMUM_MESSAGE_SIZE
Definition: nmea_protocolhandler.h:89
nmea::ProtocolHandler::ProtocolHandler
ProtocolHandler()
Default constructor.
Definition: nmea_protocolhandler.cpp:84
nmea::ProtocolHandler::~ProtocolHandler
virtual ~ProtocolHandler()
Deafult destructor.
Definition: nmea_protocolhandler.cpp:90
XsProtocolType
XsProtocolType
Protocol types (XsDevice::enableProtocol())
Definition: xsprotocoltype.h:72
nmea::ProtocolHandler::minimumMessageSize
int minimumMessageSize() const override
Definition: nmea_protocolhandler.cpp:160
nmea::ProtocolHandler::convertToMessage
XsMessage convertToMessage(MessageLocation &location, const XsByteArray &raw) const override
Converts raw data using location into a XsMessage object.
Definition: nmea_protocolhandler.cpp:151
MessageLocation
Stores the location of a message in a buffer using a start position and a size.
Definition: messagelocation.h:70
MessageLocation::m_startPos
int m_startPos
The offset of the first byte of the message or -1 if no message.
Definition: messagelocation.h:73
nmea::PREAMBLE_TSS2
const char PREAMBLE_TSS2
Definition: nmea_protocolhandler.cpp:72
xscontrollerconfig.h
nmea::ProtocolHandler::maximumMessageSize
int maximumMessageSize() const override
Definition: nmea_protocolhandler.cpp:167


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