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