Debian Codegraph @

3rdparty/rplidar_sdk/sdk/src/sl_async_transceiver.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 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33
34
35 #include "sdkcommon.h"
36 #include "hal/abs_rxtx.h"
37 #include "hal/thread.h"
38 #include "hal/types.h"
39 #include "hal/assert.h"
40 #include "hal/locker.h"
41 #include "hal/socket.h"
42 #include "hal/event.h"
43
44 #include "sl_async_transceiver.h"
45
46
47
48 namespace sl { namespace internal {
49
50
51
52
53 ProtocolMessage::ProtocolMessage()
54 : len(0)
55 , cmd(0)
56 , data(NULL)
57 , _databufsize(0)
58 , _usingOutterData(false)
59 {
60 _changeBufSize();
61 }
62
63 ProtocolMessage::ProtocolMessage(_u8 _cmd, const void* buffer, size_t size)
64 : len(size)
65 , cmd(_cmd)
66 , data(NULL)
67 , _databufsize(0)
68 , _usingOutterData(false)
69 {
70 _changeBufSize();
71 if (buffer)
72 {
73 memcpy(data, buffer, size);
74 }
75 }
76
77 ProtocolMessage::ProtocolMessage(const ProtocolMessage& srcMsg)
78 : len(srcMsg.len)
79 , cmd(srcMsg.cmd)
80 , data(NULL)
81 , _databufsize(0)
82 , _usingOutterData(false)
83 {
84 _changeBufSize( true );
85 if (srcMsg.data && len)
86 {
87 memcpy(data, srcMsg.data, len);
88 }
89 }
90
91 ProtocolMessage::~ProtocolMessage()
92 {
93 this->cleanData();
94 }
95
96 ProtocolMessage& ProtocolMessage::operator =(const ProtocolMessage& srcMessage)
97 {
98 this->cleanData();
99
100
101 this->len = srcMessage.len;
102 this->cmd = srcMessage.cmd;
103
104 _changeBufSize( true );
105 if (srcMessage.data && len)
106 {
107 memcpy(data, srcMessage.data, len);
108 }
109
110 return *this;
111 }
112
113 void ProtocolMessage::setDataBuf(_u8 *buffer, size_t size)
114 {
115 this->cleanData();
116
117 len = size;
118 data = buffer;
119 _databufsize = size;
120 _usingOutterData = true;
121 }
122
123 void ProtocolMessage::fillData(const void * buffer, size_t size)
124 {
125 len = size;
126 _changeBufSize();
127 if (buffer)
128 memcpy(data, buffer, size);
129 }
130
131 void ProtocolMessage::cleanData()
132 {
133 if (data)
134 {
135 if (!_usingOutterData)
136 {
137 delete [] data;
138 }
139 data = NULL;
140 len = 1;
141 _databufsize = 0;
142 }
143 }
144
145 void ProtocolMessage::_changeBufSize( bool force_compact)
146 {
147 size_t actual_size = getPayloadSize();
148
149 size_t new_buf_size = actual_size;
150
151
152 if (!_usingOutterData)
153 {
154 // nothing to do
155 if ( new_buf_size == _databufsize ) return;
156
157 if ( new_buf_size < _databufsize){
158
159 if ( (_databufsize >> 1) < new_buf_size)
160 {
161 // reuse the current buffer
162 if (!force_compact) return;
163 }else
164 {
165 // the current buffer size is much bigger, we need to release it to save memory
166 }
167 }
168 }
169
170 // we need to change the buffer
171 cleanData();
172 // the cleanData() will reset the length info, so we need to restore it
173 len = actual_size;
174 data = new _u8[new_buf_size];
175 _databufsize = new_buf_size;
176 }
177
178
179 AsyncTransceiver::AsyncTransceiver(IAsyncProtocolCodec& codec)
180 : _bindedChannel(NULL)
181 , _codec(codec)
182 , _isWorking(false)
183 , _workingFlag(0)
184 {
185
186 }
187
188 AsyncTransceiver::~AsyncTransceiver()
189 {
190 unbindAndClose();
191 }
192
193 u_result AsyncTransceiver::openChannelAndBind(IChannel* channel)
194 {
195 if (!channel) return RESULT_INVALID_DATA;
196
197 unbindAndClose();
198 u_result ans = RESULT_OK;
199 do
200 {
201 rp::hal::AutoLocker l(_opLocker);
202
203 // try to open the channel ...
204 Result<std::nullptr_t> ans2 = SL_RESULT_OK;
205
206 if (!channel->open()) {
207 ans2= RESULT_OPERATION_FAIL;
208 break;
209 }
210
211
212 // force a flush to clear any pending data
213 channel->flush();
214
215 _dataEvt.set(false);
216
217 _isWorking = true;
218 _workingFlag = 0;
219 _bindedChannel = channel;
220
221
222 _decoderThread = CLASS_THREAD(AsyncTransceiver, _proc_decoderThread);
223 _rxThread = CLASS_THREAD(AsyncTransceiver, _proc_rxThread);
224
225
226
227
228 } while (0);
229
230 return ans;
231 }
232
233 void AsyncTransceiver::unbindAndClose()
234 {
235 rp::hal::AutoLocker l(_opLocker);
236 if (!_isWorking) return;
237
238 assert(_bindedChannel);
239
240
241 _isWorking = false;
242 _dataEvt.set(); // set signal to wake up threads
243
244 _decoderThread.join();
245 _rxThread.join();
246
247
248 _bindedChannel->close();
249
250 _bindedChannel = NULL;
251
252
253 for (std::list< Buffer* >::iterator itr = _rxQueue.begin(); itr != _rxQueue.end(); ++itr)
254 {
255 delete [] *itr;
256 }
257 _rxQueue.clear();
258
259 }
260
261 u_result AsyncTransceiver::sendMessage(message_autoptr_t& msg)
262 {
263 assert(msg);
264
265 if (!_isWorking) return RESULT_OPERATION_NOT_SUPPORT;
266
267 rp::hal::AutoLocker l(_opLocker);
268
269 size_t requiredBufferSize = _codec.estimateLength(msg);
270
271 if (requiredBufferSize == 0) {
272 // nothing to send
273 return RESULT_OK;
274 }
275
276 u_result ans = RESULT_OK;
277
278 _u8* txBuffer = new _u8[requiredBufferSize];
279
280 do {
281
282 if (!txBuffer) {
283 return RESULT_INSUFFICIENT_MEMORY;
284 }
285
286 _codec.onEncodeData(msg, txBuffer, &requiredBufferSize);
287
288 int txSize = _bindedChannel->write(txBuffer, requiredBufferSize);
289
290 if (txSize < 0) ans = RESULT_OPERATION_FAIL;
291
292 } while (0);
293
294
295 delete[] txBuffer;
296 return ans;
297 }
298
299 sl_result AsyncTransceiver::_proc_rxThread()
300 {
301 assert(_bindedChannel);
302
303 rp::hal::Thread::SetSelfPriority(rp::hal::Thread::PRIORITY_HIGH);
304
305 u_result result;
306 size_t hintedSize = 0;
307 while (_isWorking)
308 {
309 result = _bindedChannel->waitForDataExt(hintedSize, 1000);
310
311 if (IS_FAIL(result))
312 {
313 // timeout is allowed
314 if (result == RESULT_OPERATION_TIMEOUT) {
315 continue;
316 }
317 if (_isWorking) {
318 _workingFlag |= WORKING_FLAG_ERROR;
319 _codec.onChannelError(result);
320 break;
321 }
322 }
323
324 // no data in buffer, sleep and wait for the next round
325 if (!hintedSize)
326 {
327 continue;
328 }
329
330
331 Buffer* decodeBuffer = new Buffer();
332
333 decodeBuffer->data = new _u8[hintedSize];
334
335 decodeBuffer->size = _bindedChannel->read(decodeBuffer->data, hintedSize);
336 #ifdef _DEBUG_DUMP_PACKET
337 printf("Revc: %d\n", decodeBuffer->size);
338 #endif
339
340 if (!decodeBuffer->size) {
341 delete decodeBuffer;
342
343
344 _workingFlag |= WORKING_FLAG_ERROR;
345 _codec.onChannelError(RESULT_OPERATION_ABORTED);
346 break;
347 }
348
349 assert(hintedSize >= decodeBuffer->size);
350
351
352 #ifdef _DEBUG_DUMP_PACKET
353 printf("=== Dump RX Packet, size = %d ===\n", decodeBuffer->size);
354 for (int pos = 0; pos < decodeBuffer->size; pos++)
355 {
356 printf("%02x ", decodeBuffer->data[pos]);
357 }
358 printf("\n=== END ===\n");
359 #endif
360
361 _rxLocker.lock();
362 _rxQueue.push_back(decodeBuffer);
363 _dataEvt.set();
364 _rxLocker.unlock();
365
366
367 }
368 _workingFlag |= WORKING_FLAG_RX_DISABLED;
369 return RESULT_OK;
370 }
371
372 sl_result AsyncTransceiver::_proc_decoderThread()
373 {
374
375 assert(_bindedChannel);
376 rp::hal::Thread::SetSelfPriority(rp::hal::Thread::PRIORITY_HIGH);
377 _codec.onDecodeReset();
378
379
380 while (_isWorking)
381 {
382 _rxLocker.lock();
383
384 if (_rxQueue.empty())
385 {
386 _rxLocker.unlock();
387
388 if (_dataEvt.wait(1000))
389 continue;
390
391 _rxLocker.lock();
392 }
393 assert(!_rxQueue.empty());
394
395 Buffer * bufferToDecode = _rxQueue.front();
396 _rxQueue.pop_front();
397
398 _rxLocker.unlock();
399
400 //cout<<"decoding "<< bufferToDecode->size <<" bytes of data"<<endl;
401 _codec.onDecodeData(bufferToDecode->data, bufferToDecode->size);
402
403
404 delete bufferToDecode;
405 }
406
407 return RESULT_OK;
408
409 }
410
411
412 }}