Debian Codegraph @

3rdparty/rplidar_sdk/sdk/src/sl_udp_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 class UdpChannel : public IChannel
40 {
41 public:
42 UdpChannel(const std::string& ip, int port) : _binded_socket(rp::net::DGramSocket::CreateSocket()) {
43 _ip = ip;
44 _port = port;
45 }
46
47 bool bind(const std::string & ip, sl_s32 port)
48 {
49 _socket = rp::net::SocketAddress(ip.c_str(), port);
50 return true;
51 }
52
53 bool open()
54 {
55 if(!bind(_ip, _port))
56 return false;
57 return SL_IS_OK(_binded_socket->setPairAddress(&_socket));
58 }
59
60 void close()
61 {
62 _binded_socket->dispose();
63 _binded_socket = NULL;
64 }
65 void flush()
66 {
67 clearReadCache();
68 }
69
70 sl_result waitForDataExt(size_t& size_hint, sl_u32 timeoutInMs)
71 {
72 u_result ans;
73 size_hint = 0;
74 ans = _binded_socket->waitforData(timeoutInMs);
75
76 switch (ans) {
77 case RESULT_OK:
78 size_hint = 1024; //dummy value
79 break;
80 }
81
82 return ans;
83 }
84
85 bool waitForData(size_t size, sl_u32 timeoutInMs, size_t* actualReady)
86 {
87 if (actualReady)
88 *actualReady = size;
89 return (_binded_socket->waitforData(timeoutInMs) == RESULT_OK);
90
91 }
92
93 int write(const void* data, size_t size)
94 {
95 return _binded_socket->sendTo(nullptr, data, size);
96 }
97
98 int read(void* buffer, size_t size)
99 {
100 size_t actualGet;
101
102 u_result ans = _binded_socket->recvFrom(buffer, size, actualGet);
103 if (IS_FAIL(ans)) return 0;
104 return actualGet;
105
106 }
107
108 void clearReadCache() {
109 _binded_socket->clearRxCache();
110 }
111
112 void setStatus(_u32 flag){}
113
114 int getChannelType() {
115 return CHANNEL_TYPE_UDP;
116 }
117
118 private:
119 rp::net::DGramSocket * _binded_socket;
120 rp::net::SocketAddress _socket;
121 std::string _ip;
122 int _port;
123 };
124
125 Result<IChannel*> createUdpChannel(const std::string& ip, int port)
126 {
127 return new UdpChannel(ip, port);
128 }
129 }