Debian Codegraph @

3rdparty/rplidar_sdk/sdk/src/sl_async_transceiver.h

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 #pragma once
34
35 #include <list>
36 #include <memory>
37
38 namespace sl { namespace internal {
39
40
41 class _single_thread ProtocolMessage {
42
43 public:
44 size_t len;
45 _u8 cmd;
46 protected:
47 _u8* data;
48 size_t _databufsize;
49
50 public:
51 ProtocolMessage();
52 ProtocolMessage(_u8 _cmd, const void* buffer, size_t size);
53 ProtocolMessage(const ProtocolMessage& srcMsg);
54 virtual ~ProtocolMessage();
55
56 ProtocolMessage& operator=(const ProtocolMessage& srcMessage);
57
58 // avoid use this method, pls. use fillData instead
59 void setDataBuf(_u8* buffer, size_t size);
60
61 _u8* getDataBuf() { return data; }
62
63 void fillData(const void* buffer, size_t size);
64 void cleanData();
65
66 size_t getPayloadSize() const
67 {
68 return len;
69 }
70
71 protected:
72
73 // change the data buffer to fix the new payload size
74 // the existing buffer will be reused if possible.
75 // all the existing payload data will lose
76 void _changeBufSize(bool force_compact = false);
77 bool _usingOutterData;
78 };
79
80
81
82 typedef std::shared_ptr<ProtocolMessage> message_autoptr_t;
83
84
85 class IAsyncProtocolCodec {
86 public:
87 IAsyncProtocolCodec() {}
88 virtual ~IAsyncProtocolCodec() {}
89
90 virtual void onChannelError(u_result errCode) {}
91
92 virtual void onDecodeReset() {}
93 virtual void onDecodeData(const void* buffer, size_t size) = 0;
94
95
96 virtual size_t estimateLength(message_autoptr_t& message) = 0;
97 virtual void onEncodeData(message_autoptr_t& message, _u8* txbuffer, size_t* size) = 0;
98
99 };
100
101 class AsyncTransceiver {
102 public:
103
104 enum working_flag_t
105 {
106 WORKING_FLAG_RX_DISABLED = 0x1L << 0,
107 WORKING_FLAG_TX_DISABLED = 0x1L << 1,
108
109 WORKING_FLAG_ERROR = 0x1L << 31,
110 };
111
112
113 AsyncTransceiver(IAsyncProtocolCodec& codec);
114 ~AsyncTransceiver();
115
116
117
118 u_result openChannelAndBind(IChannel* channel);
119 void unbindAndClose();
120
121 IChannel* getBindedChannel() const {
122 return _bindedChannel;
123 }
124
125 u_result sendMessage(message_autoptr_t& msg);
126
127 protected:
128
129 sl_result _proc_rxThread();
130 sl_result _proc_decoderThread();
131
132 protected:
133
134
135 rp::hal::Locker _opLocker;
136 rp::hal::Locker _rxLocker;
137 rp::hal::Event _dataEvt;
138
139 IChannel* _bindedChannel;
140 IAsyncProtocolCodec& _codec;
141
142
143 bool _isWorking;
144 _u32 _workingFlag;
145
146 rp::hal::Thread _rxThread;
147 rp::hal::Thread _decoderThread;
148
149 struct Buffer {
150 size_t size;
151 _u8* data;
152
153
154 Buffer() : size(0), data(NULL){}
155
156 ~Buffer() {
157 if (data) {
158 delete[] data;
159 data = NULL;
160 }
161 }
162 };
163 std::list< Buffer * > _rxQueue;
164 };
165
166
167 }}