sl_udp_channel.cpp
Go to the documentation of this file.
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 SL_RESULT_OK;
51  }
52 
53  bool open()
54  {
55  if(SL_IS_FAIL(bind(_ip, _port)))
56  return false;
58  }
59 
60  void close()
61  {
63  _binded_socket = NULL;
64  }
65  void flush()
66  {
67 
68  }
69 
70  bool waitForData(size_t size, sl_u32 timeoutInMs, size_t* actualReady)
71  {
72  if (actualReady)
73  *actualReady = size;
74  return (_binded_socket->waitforData(timeoutInMs) == RESULT_OK);
75 
76  }
77 
78  int write(const void* data, size_t size)
79  {
80  return _binded_socket->sendTo(_socket, data, size);
81  }
82 
83  int read(void* buffer, size_t size)
84  {
85  u_result ans;
86  size_t recCnt = 0;
87  size_t lenRec = 0;
88  while (size > recCnt)
89  {
90  sl_u8 *temp = (sl_u8 *)buffer+recCnt;
91  ans = _binded_socket->recvFrom(temp, size, lenRec);
92  recCnt += lenRec;
93  if (ans)
94  break;
95  }
96  return recCnt;
97 
98  }
99 
100  void clearReadCache() {}
101 
103 
104  private:
107  std::string _ip;
108  int _port;
109  };
110 
111  Result<IChannel*> createUdpChannel(const std::string& ip, int port)
112  {
113  return new UdpChannel(ip, port);
114  }
115 }
void setStatus(_u32 flag)
Result< IChannel * > createUdpChannel(const std::string &ip, int port)
Definition: sl_crc.h:38
rp::net::SocketAddress _socket
UdpChannel(const std::string &ip, int port)
virtual u_result sendTo(const SocketAddress &target, const void *buffer, size_t len)=0
#define RESULT_OK
Definition: rptypes.h:102
rp::net::DGramSocket * _binded_socket
virtual void dispose()=0
bool bind(const std::string &ip, sl_s32 port)
sl_u8 size
int read(void *buffer, size_t size)
bool waitForData(size_t size, sl_u32 timeoutInMs, size_t *actualReady)
sl_u8 flag
Definition: sl_lidar_cmd.h:43
#define SL_IS_OK(x)
Definition: sl_types.h:82
int write(const void *data, size_t size)
virtual u_result waitforData(_u32 timeout=DEFAULT_SOCKET_TIMEOUT)=0
uint32_t _u32
Definition: rptypes.h:69
virtual u_result setPairAddress(const SocketAddress *pairAddress)=0
#define SL_IS_FAIL(x)
Definition: sl_types.h:83
virtual u_result recvFrom(void *buf, size_t len, size_t &recv_len, SocketAddress *sourceAddr=NULL)=0
#define SL_RESULT_OK
Definition: sl_types.h:71
sl_u8 data[0]
uint32_t u_result
Definition: rptypes.h:100


rplidar_ros
Author(s):
autogenerated on Tue Jun 13 2023 02:07:38