Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Slamtec LIDAR SDK
3 *
4 * Copyright (c) 2014 - 2020 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 #include "sl_lidar_driver.h"
34 #include "hal/abs_rxtx.h"
35 #include "hal/socket.h"
36
37
38 namespace sl {
39
40 class SerialPortChannel : public ISerialPortChannel
41 {
42 public:
43 SerialPortChannel(const std::string& device, int baudrate) :_rxtxSerial(rp::hal::serial_rxtx::CreateRxTx())
44 {
45 _device = device;
46 _baudrate = baudrate;
47 }
48
49 ~SerialPortChannel()
50 {
51 if (_rxtxSerial)
52 delete _rxtxSerial;
53 }
54
55 bool bind(const std::string& device, sl_s32 baudrate)
56 {
57 _closePending = false;
58 return _rxtxSerial->bind(device.c_str(), baudrate);
59 }
60
61 bool open()
62 {
63 if(!bind(_device, _baudrate))
64 return false;
65 return _rxtxSerial->open();
66 }
67
68 void close()
69 {
70 _closePending = true;
71 _rxtxSerial->cancelOperation();
72 _rxtxSerial->close();
73 }
74 void flush()
75 {
76 _rxtxSerial->flush(0);
77 }
78
79 sl_result waitForDataExt(size_t& size_hint, sl_u32 timeoutInMs)
80 {
81 _word_size_t result;
82 size_t size_holder;
83 size_hint = 0;
84
85 if (_closePending) return RESULT_OPERATION_TIMEOUT;
86
87 if (!_rxtxSerial->isOpened()) {
88 return RESULT_OPERATION_FAIL;
89 }
90
91 result = _rxtxSerial->waitfordata(1, timeoutInMs, &size_holder);
92 size_hint = size_holder;
93 if (result == (_word_size_t)rp::hal::serial_rxtx::ANS_DEV_ERR)
94 return RESULT_OPERATION_FAIL;
95 if (result == (_word_size_t)rp::hal::serial_rxtx::ANS_TIMEOUT)
96 return RESULT_OPERATION_TIMEOUT;
97
98 return RESULT_OK;
99 }
100
101 bool waitForData(size_t size, sl_u32 timeoutInMs, size_t* actualReady)
102 {
103 if (_closePending) return false;
104 return (_rxtxSerial->waitfordata(size, timeoutInMs, actualReady) == rp::hal::serial_rxtx::ANS_OK);
105 }
106
107 int write(const void* data, size_t size)
108 {
109 return _rxtxSerial->senddata((const sl_u8 * )data, size);
110 }
111
112 int read(void* buffer, size_t size)
113 {
114 size_t lenRec = 0;
115 lenRec = _rxtxSerial->recvdata((sl_u8 *)buffer, size);
116 return (int)lenRec;
117 }
118
119 void clearReadCache()
120 {
121
122 }
123
124 void setDTR(bool dtr)
125 {
126 dtr ? _rxtxSerial->setDTR() : _rxtxSerial->clearDTR();
127 }
128
129 int getChannelType() {
130 return CHANNEL_TYPE_SERIALPORT;
131 }
132
133 private:
134 rp::hal::serial_rxtx * _rxtxSerial;
135 bool _closePending;
136 std::string _device;
137 int _baudrate;
138
139 };
140
141 Result<IChannel*> createSerialPortChannel(const std::string& device, int baudrate)
142 {
143 return new SerialPortChannel(device, baudrate);
144 }
145
146 }