Debian Codegraph @

3rdparty/rplidar_sdk/sdk/src/dataunpacker/dataunpacker.cpp

raw symbols SCIP
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
39 #include "dataunnpacker_commondef.h"
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
56 #include "unpacker/handler_capsules.h"
57 #include "unpacker/handler_hqnode.h"
58 #include "unpacker/handler_normalnode.h"
59
60
61 #define DEF_REGISTER_HANDLER_LIST
62
63
64 BEGIN_DATAUNPACKER_NS()
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
79 class LIDARSampleDataUnpackerImpl : public LIDARSampleDataUnpackerInner
80 {
81 public:
82
83 void registerHandler(_u8 ansType, IDataUnpackerHandler* handler)
84 {
85 _handlerMap[ansType] = handler;
86 }
87
88
89 void unregisterAllHandlers()
90 {
91 for (auto itr = _handlerMap.begin(); itr != _handlerMap.end(); ++itr)
92 {
93 delete itr->second;
94 }
95 _handlerMap.clear();
96 }
97
98 LIDARSampleDataUnpackerImpl(LIDARSampleDataListener& l)
99 : LIDARSampleDataUnpackerInner(l)
100 , _enabled(false)
101 , _lastActiveAnsType(0)
102 , _lastActiveHandler(nullptr)
103 {
104
105 }
106
107 virtual ~LIDARSampleDataUnpackerImpl()
108 {
109 unregisterAllHandlers();
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) {
128 onDeselectHandler();
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) {
173 _lastActiveHandler->reset();
174 }
175 }
176
177 virtual _u64 getCurrentTimestamp_uS() {
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 {
201 _listener.onHQNodeScanResetReq();
202 }
203 protected:
204
205 void onSelectHandler(_u8 ansType, IDataUnpackerHandler* handler)
206 {
207 _lastActiveHandler = handler;
208 _lastActiveAnsType = ansType;
209 }
210
211 void onDeselectHandler()
212 {
213 reset();
214 }
215
216
217 protected:
218 bool _enabled;
219 std::map<_u8, IDataUnpackerHandler*> _handlerMap;
220
221 _u8 _lastActiveAnsType;
222 IDataUnpackerHandler* _lastActiveHandler;
223 };
224
225 LIDARSampleDataUnpacker* LIDARSampleDataUnpacker::CreateInstance(LIDARSampleDataListener& listener)
226 {
227 LIDARSampleDataUnpackerImpl* impl = new LIDARSampleDataUnpackerImpl(listener);
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
244 void LIDARSampleDataUnpacker::ReleaseInstance(LIDARSampleDataUnpacker* unpacker) {
245 delete unpacker;
246 }
247
248 LIDARSampleDataUnpacker::~LIDARSampleDataUnpacker() {
249
250 }
251
252 LIDARSampleDataUnpacker::LIDARSampleDataUnpacker(LIDARSampleDataListener& l)
253 : _listener(l)
254 {
255
256 }
257
258
259 END_DATAUNPACKER_NS()