protocolmanager.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 "protocolmanager.h"
66 #include "iprotocolhandler.h"
67 #include "communicator.h"
68 #include <algorithm>
69 
70 // Remove windows global min/max macros
71 #undef min
72 #undef max
73 
84  : m_communicator(communicator)
85 {
86 }
87 
89 {
90 }
91 
95 {
96  return m_protocolHandlers.begin();
97 }
98 
102 {
103  return m_protocolHandlers.end();
104 }
105 
112 {
113  MessageLocation bestMessageLocation;
114  XsProtocolType bestProtocolType = XPT_Xbus;
115  container_type::iterator bestHandlerIter = m_protocolHandlers.end();
116 
117  for (container_type::iterator i = m_protocolHandlers.begin(); i != m_protocolHandlers.end(); ++i)
118  {
119  assert((*i).operator->() != 0);
120  IProtocolHandler const& handler = **i;
121  XsProtocolType currentProtocolType = static_cast<XsProtocolType>(handler.type());
122  MessageLocation currentMessageLocation = handler.findMessage(type, raw);
123 
124  int currentPosition = currentMessageLocation.m_incompletePos >= 0 ? currentMessageLocation.m_incompletePos : currentMessageLocation.m_startPos;
125  if (currentPosition < 0)
126  continue;
127 
128  int bestPosition = bestMessageLocation.m_incompletePos >= 0 ? bestMessageLocation.m_incompletePos : bestMessageLocation.m_startPos;
129  if ( (bestPosition < 0)
130  || (currentPosition < bestPosition)
131  || (currentPosition == bestPosition && currentMessageLocation.m_startPos >= 0 && (bestMessageLocation.m_startPos < 0 || currentMessageLocation.m_startPos < bestMessageLocation.m_startPos))
132  )
133  {
134  // Message is a better match
135  bestMessageLocation = currentMessageLocation;
136  bestProtocolType = currentProtocolType;
137  bestHandlerIter = i;
138  }
139 
140  // Stop searching if the location is as good as it gets
141  if (bestMessageLocation.m_startPos == 0)
142  break;
143  }
144 
145  // Move the best handler to the front of the list to speed up future searches
146  if (bestHandlerIter != m_protocolHandlers.end() && bestHandlerIter != m_protocolHandlers.begin())
147  {
148  value_type bestHandler = *bestHandlerIter;
150  cont.erase(bestHandlerIter);
151  cont.push_front(bestHandler);
152  }
153  type = bestProtocolType;
154  return bestMessageLocation;
155 }
156 
164 {
165  for (auto const& handler : m_protocolHandlers)
166  if (handler->type() == type)
167  return handler->convertToMessage(location, raw);
168 
169  return XsMessage();
170 }
171 
177 {
178  bool result = false;
179  container_type::iterator i = m_protocolHandlers.begin();
180  while (i != m_protocolHandlers.end())
181  {
182  if ((*i)->type() == type)
183  {
184  // Increment the incrementing iterator before removing the pointed to element to
185  // prevent icrementing an invalidated iterator
186  container_type::iterator toErase = i;
187  ++i;
188  m_protocolHandlers.erase(toErase);
189  result = true;
190  }
191  else
192  ++i;
193  }
194  return result;
195 }
196 
197 
203 {
204  for (auto i : m_protocolHandlers)
205  {
206  if (i->type() == type)
207  return i;
208  }
209  return value_type();
210 }
211 
212 
217 {
218  bool result = false;
219  for (container_type::const_iterator i = m_protocolHandlers.begin(); i != m_protocolHandlers.end(); ++i)
220  {
221  if ((*i)->type() == type)
222  result = true;
223  }
224  return result;
225 }
226 
227 
229 {
230  assert(handler != 0);
231  // check for duplicates first
232  for (container_type::iterator i = m_protocolHandlers.begin(); i != m_protocolHandlers.end(); ++i)
233  if (handler->type() == (*i)->type())
234  return (*i);
235 
236  m_protocolHandlers.emplace_back(value_type(handler));
237  value_type inserted = m_protocolHandlers.back();
238  return inserted;
239 }
240 
244 {
245  m_protocolHandlers.clear();
246 }
247 
252 bool ProtocolManager::validateMessage(XsMessage const& message) const
253 {
254  return m_communicator.sanityCheck(message);
255 }
256 
260 {
261  if (m_protocolHandlers.empty())
262  return 0;
263  return (*m_protocolHandlers.begin())->minimumMessageSize();
264 }
265 
ProtocolManager::clear
virtual void clear()
Clears the protocol handlers list.
Definition: protocolmanager.cpp:243
XsMessage
struct XsMessage XsMessage
Definition: xsmessage.h:85
XsByteArray
A list of uint8_t values.
ProtocolManager::m_communicator
Communicator const & m_communicator
Definition: protocolmanager.h:116
ProtocolManager::begin
const_iterator begin() const
Definition: protocolmanager.cpp:94
Communicator::sanityCheck
bool sanityCheck(XsMessage const &msg) const
Do a sanity check on a potential message.
Definition: communicator.cpp:262
IProtocolHandler::type
virtual int type() const =0
Returns the type of the protocol handler.
iprotocolhandler.h
ProtocolManager::container_type
std::list< value_type > container_type
A typedef for a container type.
Definition: protocolmanager.h:89
Communicator
A base struct for a communication interface.
Definition: communicator.h:95
protocolmanager.h
ProtocolManager::add
virtual value_type add(IProtocolHandler *handler)
Adds the protocol handler.
Definition: protocolmanager.cpp:228
ProtocolManager::findMessage
MessageLocation findMessage(XsProtocolType &type, const XsByteArray &raw) override
Searches for a raw message in a raw data.
Definition: protocolmanager.cpp:111
ProtocolManager::const_iterator
container_type::const_iterator const_iterator
A typedef for a const iterator.
Definition: protocolmanager.h:92
ProtocolManager::find
virtual value_type find(XsProtocolType type)
Searches the registered protocol handlers for a handler that matches the given protocol type and retu...
Definition: protocolmanager.cpp:202
IProtocolHandler
Interface class for protocol handlers.
Definition: iprotocolhandler.h:78
ProtocolManager::m_protocolHandlers
container_type m_protocolHandlers
Definition: protocolmanager.h:120
ProtocolManager::likelyMinimumMessageSize
int likelyMinimumMessageSize() const
Definition: protocolmanager.cpp:259
XsMessage
Structure for storing a single message.
Definition: xsmessage.h:202
XPT_Xbus
@ XPT_Xbus
The Xsens Xbus protocol, enabled by default, always 0.
Definition: xsprotocoltype.h:74
ProtocolManager::value_type
std::shared_ptr< IProtocolHandler > value_type
A typedef for a value type.
Definition: protocolmanager.h:86
MessageLocation::m_incompletePos
int m_incompletePos
Definition: messagelocation.h:82
ProtocolManager::hasProtocol
virtual bool hasProtocol(XsProtocolType type) const
Definition: protocolmanager.cpp:216
ProtocolManager::validateMessage
bool validateMessage(XsMessage const &message) const override
Check a message for a validity.
Definition: protocolmanager.cpp:252
ProtocolManager::end
const_iterator end() const
Definition: protocolmanager.cpp:101
XsProtocolType
XsProtocolType
Protocol types (XsDevice::enableProtocol())
Definition: xsprotocoltype.h:72
ProtocolManager::ProtocolManager
ProtocolManager(Communicator const &)
Default constructor.
Definition: protocolmanager.cpp:83
ProtocolManager::convertToMessage
XsMessage convertToMessage(XsProtocolType &type, MessageLocation &location, const XsByteArray &raw) override
Converts raw data using location into a XsMessage object.
Definition: protocolmanager.cpp:163
ProtocolManager::~ProtocolManager
virtual ~ProtocolManager()
Definition: protocolmanager.cpp:88
communicator.h
ProtocolManager::remove
virtual bool remove(XsProtocolType type)
Removes a protocol handler of a specified type.
Definition: protocolmanager.cpp:176
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


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