dataunpacker.cpp
Go to the documentation of this file.
1 /*
2  * Slamtec LIDAR SDK
3  *
4  * Copyright (c) 2014 - 2023 Shanghai Slamtec Co., Ltd.
5  * http://www.slamtec.com
6  *
7  */
8 
9  /*
10  * Sample Data Unpacker System
11  *
12  */
13 
14  /*
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  * this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright notice,
22  * this list of conditions and the following disclaimer in the documentation
23  * and/or other materials provided with the distribution.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
40 #include "dataunpacker.h"
41 #include "dataunnpacker_internal.h"
42 
43 
44 #include <map>
45 
46 
47 #define REGISTER_HANDLER(_c_) { \
48  auto newBorn = new unpacker::_c_(); \
49  if (!newBorn) return false; \
50  handlerList.push_back(newBorn); \
51  }
52 
53 // How to include new handlers?
54 // 1. add extra include line below if a new handle is to be included
55 // 2. update the code in function _registerDataUnpackerHandlers
59 
60 
61 #define DEF_REGISTER_HANDLER_LIST
62 
63 
65 
66 
67 static bool _registerDataUnpackerHandlers(std::vector<IDataUnpackerHandler *> & handlerList)
68 {
69  REGISTER_HANDLER(UnpackerHandler_NormalNode);
70  REGISTER_HANDLER(UnpackerHandler_HQNode);
71  REGISTER_HANDLER(UnpackerHandler_CapsuleNode);
72  REGISTER_HANDLER(UnpackerHandler_UltraCapsuleNode);
73  REGISTER_HANDLER(UnpackerHandler_DenseCapsuleNode);
74  REGISTER_HANDLER(UnpackerHandler_UltraDenseCapsuleNode);
75  return true;
76 }
77 
78 
80 {
81 public:
82 
83  void registerHandler(_u8 ansType, IDataUnpackerHandler* handler)
84  {
85  _handlerMap[ansType] = handler;
86  }
87 
88 
90  {
91  for (auto itr = _handlerMap.begin(); itr != _handlerMap.end(); ++itr)
92  {
93  delete itr->second;
94  }
95  _handlerMap.clear();
96  }
97 
100  , _enabled(false)
101  , _lastActiveAnsType(0)
102  , _lastActiveHandler(nullptr)
103  {
104 
105  }
106 
108  {
110  }
111 
112 
113  virtual void updateUnpackerContext(UnpackerContextType type, const void* data, size_t size)
114  {
115 
116  // notify the handlers ...
117  for (auto itr = _handlerMap.begin(); itr != _handlerMap.end(); ++itr)
118  {
119  itr->second->onUnpackerContextSet(type, data, size);
120  }
121  }
122 
123  virtual bool onSampleData(_u8 ansType, const void* buffer, size_t size) {
124  if (!_enabled) return false;
125 
126 
127  if (_lastActiveAnsType != ansType) {
129 
130  auto itr = _handlerMap.find(ansType);
131  if (itr != _handlerMap.end()) {
132  onSelectHandler(ansType, itr->second);
133  }
134  else {
135  onSelectHandler(ansType, nullptr);
136  }
137 
138  }
139 
140  if (_lastActiveHandler) {
141  _lastActiveHandler->onData(this, reinterpret_cast<const _u8 *>(buffer), size);
142  return true;
143  }
144  else {
145  return false;
146  }
147  }
148 
149  virtual void reset()
150  {
151  clearCache();
152  _lastActiveHandler = nullptr;
153  _lastActiveAnsType = 0;
154 
155  }
156 
157  virtual void enable()
158  {
159  _enabled = true;
160  reset();
161  }
162 
163  virtual void disable()
164  {
165  _enabled = false;
166  reset();
167 
168  }
169 
170  virtual void clearCache()
171  {
172  if (_lastActiveHandler) {
174  }
175  }
176 
178  return getus();
179  }
180 
181  virtual void publishHQNode(_u64 timestamp_uS, const rplidar_response_measurement_node_hq_t* node)
182  {
183  _listener.onHQNodeDecoded(timestamp_uS, node);
184  }
185 
186 
187  virtual void publishDecodingErrorMsg(int errorType, _u8 ansType, const void* payload, size_t size)
188  {
189  _listener.onDecodingError(errorType, ansType, payload, size);
190 
191  }
192 
193  virtual void publishCustomData(_u8 ansType, _u32 customCode, const void* payload, size_t size)
194  {
195  _listener.onCustomSampleDataDecoded(ansType, customCode, payload, size);
196  }
197 
198 
199  virtual void publishNewScanReset()
200  {
202  }
203 protected:
204 
205  void onSelectHandler(_u8 ansType, IDataUnpackerHandler* handler)
206  {
207  _lastActiveHandler = handler;
208  _lastActiveAnsType = ansType;
209  }
210 
212  {
213  reset();
214  }
215 
216 
217 protected:
218  bool _enabled;
219  std::map<_u8, IDataUnpackerHandler*> _handlerMap;
220 
223 };
224 
226 {
228 
229  std::vector<IDataUnpackerHandler*> list;
230  if (!_registerDataUnpackerHandlers(list)) {
231  delete impl;
232  for (auto itr = list.begin(); itr != list.end(); ++itr) {
233  delete* itr;
234  }
235  impl = nullptr;
236  }
237 
238  for (auto itr = list.begin(); itr != list.end(); ++itr) {
239  impl->registerHandler((*itr)->getSampleAnswerType(), (*itr));
240  }
241  return impl;
242 }
243 
245  delete unpacker;
246 }
247 
249 
250 }
251 
253  : _listener(l)
254 {
255 
256 }
257 
258 
LIDARSampleDataUnpackerImpl::LIDARSampleDataUnpackerImpl
LIDARSampleDataUnpackerImpl(LIDARSampleDataListener &l)
Definition: dataunpacker.cpp:98
LIDARSampleDataUnpackerInner
Definition: dataunnpacker_internal.h:44
dataunnpacker_internal.h
LIDARSampleDataUnpackerImpl::_enabled
bool _enabled
Definition: dataunpacker.cpp:218
sl_lidar_response_measurement_node_hq_t
Definition: sl_lidar_cmd.h:272
BEGIN_DATAUNPACKER_NS
#define BEGIN_DATAUNPACKER_NS()
Definition: dataupacker_namespace.h:4
type
sl_u32 type
Definition: sl_lidar_cmd.h:2
LIDARSampleDataUnpackerImpl::clearCache
virtual void clearCache()
Definition: dataunpacker.cpp:170
LIDARSampleDataUnpackerImpl::registerHandler
void registerHandler(_u8 ansType, IDataUnpackerHandler *handler)
Definition: dataunpacker.cpp:83
LIDARSampleDataUnpackerImpl::_lastActiveHandler
IDataUnpackerHandler * _lastActiveHandler
Definition: dataunpacker.cpp:222
LIDARSampleDataUnpackerImpl::publishCustomData
virtual void publishCustomData(_u8 ansType, _u32 customCode, const void *payload, size_t size)
Definition: dataunpacker.cpp:193
LIDARSampleDataUnpackerImpl::updateUnpackerContext
virtual void updateUnpackerContext(UnpackerContextType type, const void *data, size_t size)
Definition: dataunpacker.cpp:113
LIDARSampleDataUnpackerImpl::reset
virtual void reset()
Definition: dataunpacker.cpp:149
LIDARSampleDataUnpackerImpl::onSampleData
virtual bool onSampleData(_u8 ansType, const void *buffer, size_t size)
Definition: dataunpacker.cpp:123
_u8
uint8_t _u8
Definition: rptypes.h:63
size
sl_u8 size
Definition: sl_lidar_protocol.h:4
LIDARSampleDataListener::onCustomSampleDataDecoded
virtual void onCustomSampleDataDecoded(_u8 ansType, _u32 customCode, const void *data, size_t size)
Definition: dataunpacker.h:54
LIDARSampleDataUnpacker::~LIDARSampleDataUnpacker
virtual ~LIDARSampleDataUnpacker()
Definition: dataunpacker.cpp:248
LIDARSampleDataListener::onDecodingError
virtual void onDecodingError(int errMsg, _u8 ansType, const void *payload, size_t size)
Definition: dataunpacker.h:56
handler_capsules.h
payload
sl_u8 payload[0]
Definition: sl_lidar_cmd.h:3
LIDARSampleDataUnpackerImpl::~LIDARSampleDataUnpackerImpl
virtual ~LIDARSampleDataUnpackerImpl()
Definition: dataunpacker.cpp:107
LIDARSampleDataUnpackerImpl::publishNewScanReset
virtual void publishNewScanReset()
Definition: dataunpacker.cpp:199
LIDARSampleDataUnpacker
Definition: dataunpacker.h:59
_registerDataUnpackerHandlers
static bool _registerDataUnpackerHandlers(std::vector< IDataUnpackerHandler * > &handlerList)
Definition: dataunpacker.cpp:67
handler_normalnode.h
LIDARSampleDataUnpacker::_listener
LIDARSampleDataListener & _listener
Definition: dataunpacker.h:89
LIDARSampleDataUnpacker::UnpackerContextType
UnpackerContextType
Definition: dataunpacker.h:67
IDataUnpackerHandler::onData
virtual void onData(LIDARSampleDataUnpackerInner *engine, const _u8 *data, size_t size)=0
LIDARSampleDataListener::onHQNodeScanResetReq
virtual void onHQNodeScanResetReq()=0
LIDARSampleDataUnpackerImpl::publishDecodingErrorMsg
virtual void publishDecodingErrorMsg(int errorType, _u8 ansType, const void *payload, size_t size)
Definition: dataunpacker.cpp:187
LIDARSampleDataUnpackerImpl::enable
virtual void enable()
Definition: dataunpacker.cpp:157
LIDARSampleDataUnpackerImpl::publishHQNode
virtual void publishHQNode(_u64 timestamp_uS, const rplidar_response_measurement_node_hq_t *node)
Definition: dataunpacker.cpp:181
dataunpacker.h
LIDARSampleDataUnpackerImpl::onSelectHandler
void onSelectHandler(_u8 ansType, IDataUnpackerHandler *handler)
Definition: dataunpacker.cpp:205
LIDARSampleDataListener
Definition: dataunpacker.h:47
IDataUnpackerHandler::reset
virtual void reset()=0
LIDARSampleDataUnpackerImpl::onDeselectHandler
void onDeselectHandler()
Definition: dataunpacker.cpp:211
LIDARSampleDataListener::onHQNodeDecoded
virtual void onHQNodeDecoded(_u64 timestamp_uS, const rplidar_response_measurement_node_hq_t *node)=0
unpacker
Definition: handler_capsules.cpp:49
LIDARSampleDataUnpackerImpl
Definition: dataunpacker.cpp:79
std
REGISTER_HANDLER
#define REGISTER_HANDLER(_c_)
Definition: dataunpacker.cpp:47
_u64
uint64_t _u64
Definition: rptypes.h:72
LIDARSampleDataUnpacker::LIDARSampleDataUnpacker
LIDARSampleDataUnpacker(LIDARSampleDataListener &)
Definition: dataunpacker.cpp:252
LIDARSampleDataUnpackerImpl::disable
virtual void disable()
Definition: dataunpacker.cpp:163
LIDARSampleDataUnpackerImpl::_handlerMap
std::map< _u8, IDataUnpackerHandler * > _handlerMap
Definition: dataunpacker.cpp:219
data
sl_u8 data[0]
Definition: sl_lidar_protocol.h:5
LIDARSampleDataUnpackerImpl::getCurrentTimestamp_uS
virtual _u64 getCurrentTimestamp_uS()
Definition: dataunpacker.cpp:177
LIDARSampleDataUnpackerImpl::_lastActiveAnsType
_u8 _lastActiveAnsType
Definition: dataunpacker.cpp:221
END_DATAUNPACKER_NS
#define END_DATAUNPACKER_NS()
Definition: dataupacker_namespace.h:5
getus
#define getus()
Definition: linux/timer.h:58
_u32
uint32_t _u32
Definition: rptypes.h:69
IDataUnpackerHandler
Definition: dataunnpacker_internal.h:59
LIDARSampleDataUnpacker::ReleaseInstance
static void ReleaseInstance(LIDARSampleDataUnpacker *)
Definition: dataunpacker.cpp:244
LIDARSampleDataUnpackerImpl::unregisterAllHandlers
void unregisterAllHandlers()
Definition: dataunpacker.cpp:89
handler_hqnode.h
dataunnpacker_commondef.h
LIDARSampleDataUnpacker::CreateInstance
static LIDARSampleDataUnpacker * CreateInstance(LIDARSampleDataListener &listener)
Definition: dataunpacker.cpp:225


rplidar_ros
Author(s):
autogenerated on Fri Aug 2 2024 08:42:13