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