udp_receiver.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 /*
3  * @brief udp_receiver receives msgpack raw data by udp.
4  * It implements a udp client, connects to multiScan136 (or any other udp-) sender,
5  * receives and buffers msgpack raw data.
6  *
7  * Copyright (C) 2020 Ing.-Buero Dr. Michael Lehning, Hildesheim
8  * Copyright (C) 2020 SICK AG, Waldkirch
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  * All rights reserved.
23  *
24  * Redistribution and use in source and binary forms, with or without
25  * modification, are permitted provided that the following conditions are met:
26  *
27  * * Redistributions of source code must retain the above copyright
28  * notice, this list of conditions and the following disclaimer.
29  * * Redistributions in binary form must reproduce the above copyright
30  * notice, this list of conditions and the following disclaimer in the
31  * documentation and/or other materials provided with the distribution.
32  * * Neither the name of SICK AG nor the names of its
33  * contributors may be used to endorse or promote products derived from
34  * this software without specific prior written permission
35  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
36  * contributors may be used to endorse or promote products derived from
37  * this software without specific prior written permission
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  *
51  * Authors:
52  * Michael Lehning <michael.lehning@lehning.de>
53  *
54  * Copyright 2020 SICK AG
55  * Copyright 2020 Ing.-Buero Dr. Michael Lehning
56  *
57  */
58 #ifndef __SICK_SCANSEGMENT_XD_UDP_RECEIVER_H
59 #define __SICK_SCANSEGMENT_XD_UDP_RECEIVER_H
60 
64 
65 namespace sick_scansegment_xd
66 {
67  /*
68  * @brief forward declaration of an udp receiver socket implementation.
69  * Used internally in the UdpReceiver.
70  */
71  class UdpReceiverSocketImpl;
72 
73  /*
74  * @brief class UdpReceiver receives msgpack raw data by udp.
75  * It implements a udp client, connects to multiScan136 (or any other udp-) sender,
76  * receives and buffers msgpack raw data.
77  */
79  {
80  public:
81 
82  /*
83  * @brief Default constructor.
84  */
85  UdpReceiver();
86 
87  /*
88  * @brief Default destructor.
89  */
90  ~UdpReceiver();
91 
92  /*
93  * @brief Initializes an udp socket to a sender.
94  * @param[in] udp_sender ip address of the udp sender, f.e. "127.0.0.1" (localhost, loopback)
95  * @param[in] udp_port ip port, f.e. 2115 (default port for multiScan136 emulator)
96  * @param[in] udp_input_fifolength max. input fifo length (-1: unlimited, default: 20 for buffering 1 second at 20 Hz), elements will be removed from front if number of elements exceeds the fifo_length
97  * @param[in] verbose true: enable debug output, false: quiet mode (default)
98  * @param[in] export_udp_msg: true: export binary udp and msgpack data to file (*.udp and *.msg), default: false
99  * @param[in] scandataformat ScanDataFormat: 1 for msgpack or 2 for compact scandata, default: 1
100  * @param[in] PayloadFifo* fifo: Fifo to handle payload data
101  */
102  bool Init(const std::string& udp_sender, int udp_port, int udp_input_fifolength = 20, bool verbose = false, bool export_udp_msg = false, int scandataformat = 1, PayloadFifo* fifo = 0);
103 
104  /*
105  * @brief Starts receiving udp packages in a background thread and pops msgpack data packages to the fifo.
106  */
107  bool Start(void);
108 
109  /*
110  * @brief Stops the udp receiver thread
111  */
112  void Stop(bool do_join = true);
113 
114  /*
115  * @brief Stop to receive data and shutdown the udp socket
116  */
117  void Close(void);
118 
119  /*
120  * @brief Returns the Fifo storing the msgpack data received by this udp receiver.
121  */
122  PayloadFifo* Fifo(void) { return m_fifo_impl; }
123 
124  /*
125  * @brief Converts a payload to a hex string
126  * param[in] payload payload buffer
127  * param[in] bytes_received number of received bytes
128  */
129  static std::string ToHexString(const std::vector<uint8_t>& payload, size_t bytes_received);
130 
131  /*
132  * @brief Converts a payload to a printable string (alnum characters or '.' for non-printable bytes)
133  * param[in] payload payload buffer
134  * param[in] bytes_received number of received bytes
135  */
136  static std::string ToPrintableString(const std::vector<uint8_t>& payload, size_t bytes_received);
137 
138  private:
139 
140  /*
141  * @brief Thread callback, runs the receiver for udp packages and pops msgpack data packages to the fifo.
142  */
143  bool Run(void);
144 
145  /*
146  * Configuration and parameter
147  */
148  bool m_verbose; // true: enable debug output, false: quiet mode (default)
149  int m_udp_recv_buffer_size; // size of buffer to receive udp packages
150  std::vector<uint8_t> m_udp_msg_start_seq; // any udp message from multiScan136 starts with 15 byte ".....class.Scan"
151  double m_udp_timeout_recv_nonblocking; // in normal mode we receive udp datagrams non-blocking with timeout to enable sync with msgpack start
152  double m_udp_sender_timeout; // if no udp packages received within some seconds, we switch to blocking udp receive
153  bool m_export_udp_msg; // true : export binary udpand msgpack data to file(*.udpand* .msg), default: false
154  int m_scandataformat; // ScanDataFormat: 1 for msgpack or 2 for compact scandata, default: 1
155 
156  /*
157  * Member data to run a udp receiver
158  */
159  UdpReceiverSocketImpl* m_socket_impl; // implementation of the udp receiver socket
160  PayloadFifo* m_fifo_impl; // implementation of a thread safe fifo buffer to share the payload of udp packages
161  bool m_fifo_impl_created; // m_fifo_impl has been created and needs be deleted at exit
162  std::thread* m_receiver_thread; // background thread to receive udp packages
163  bool m_run_receiver_thread; // flag to start and stop the udp receiver thread
164 
165 
166  }; // class UdpReceiver
167 
168 } // namespace sick_scansegment_xd
169 #endif // __SICK_SCANSEGMENT_XD_UDP_RECEIVER_H
common.h
sick_scansegment_xd::PayloadFifo
Definition: fifo.h:187
sick_scansegment_xd::UdpReceiver::ToPrintableString
static std::string ToPrintableString(const std::vector< uint8_t > &payload, size_t bytes_received)
Definition: udp_receiver.cpp:398
sick_scansegment_xd::UdpReceiver::ToHexString
static std::string ToHexString(const std::vector< uint8_t > &payload, size_t bytes_received)
Definition: udp_receiver.cpp:383
sick_scansegment_xd
Definition: include/sick_scansegment_xd/common.h:138
sick_scansegment_xd::UdpReceiver::Start
bool Start(void)
Definition: udp_receiver.cpp:150
sick_scansegment_xd::UdpReceiver::m_scandataformat
int m_scandataformat
Definition: udp_receiver.h:154
fifo.h
sick_scansegment_xd::UdpReceiver::UdpReceiver
UdpReceiver()
Definition: udp_receiver.cpp:84
sick_scansegment_xd::UdpReceiver::Init
bool Init(const std::string &udp_sender, int udp_port, int udp_input_fifolength=20, bool verbose=false, bool export_udp_msg=false, int scandataformat=1, PayloadFifo *fifo=0)
Definition: udp_receiver.cpp:106
sick_ros_wrapper.h
sick_scansegment_xd::UdpReceiver::m_socket_impl
UdpReceiverSocketImpl * m_socket_impl
Definition: udp_receiver.h:159
sick_scansegment_xd::UdpReceiver::Close
void Close(void)
Definition: udp_receiver.cpp:172
multiscan_pcap_player.verbose
int verbose
Definition: multiscan_pcap_player.py:142
sick_scansegment_xd::UdpReceiver::m_run_receiver_thread
bool m_run_receiver_thread
Definition: udp_receiver.h:163
sick_scansegment_xd::UdpReceiver::m_verbose
bool m_verbose
Definition: udp_receiver.h:148
pcap_json_converter.payload
string payload
Definition: pcap_json_converter.py:130
sick_scansegment_xd::UdpReceiver::m_udp_msg_start_seq
std::vector< uint8_t > m_udp_msg_start_seq
Definition: udp_receiver.h:150
sick_scansegment_xd::UdpReceiver::m_receiver_thread
std::thread * m_receiver_thread
Definition: udp_receiver.h:162
sick_scansegment_xd::UdpReceiver::~UdpReceiver
~UdpReceiver()
Definition: udp_receiver.cpp:92
multiscan_pcap_player.udp_port
int udp_port
Definition: multiscan_pcap_player.py:137
sick_scansegment_xd::UdpReceiver::m_fifo_impl_created
bool m_fifo_impl_created
Definition: udp_receiver.h:161
sick_scansegment_xd::UdpReceiver::Run
bool Run(void)
Definition: udp_receiver.cpp:204
sick_scansegment_xd::UdpReceiver::m_udp_sender_timeout
double m_udp_sender_timeout
Definition: udp_receiver.h:152
sick_scan_base.h
sick_scansegment_xd::UdpReceiver::m_udp_recv_buffer_size
int m_udp_recv_buffer_size
Definition: udp_receiver.h:149
sick_scansegment_xd::UdpReceiver::m_export_udp_msg
bool m_export_udp_msg
Definition: udp_receiver.h:153
sick_scansegment_xd::UdpReceiver::m_fifo_impl
PayloadFifo * m_fifo_impl
Definition: udp_receiver.h:160
sick_scansegment_xd::UdpReceiverSocketImpl
Definition: udp_sockets.h:106
sick_scansegment_xd::UdpReceiver
Definition: udp_receiver.h:78
sick_scansegment_xd::UdpReceiver::Fifo
PayloadFifo * Fifo(void)
Definition: udp_receiver.h:122
sick_scansegment_xd::UdpReceiver::m_udp_timeout_recv_nonblocking
double m_udp_timeout_recv_nonblocking
Definition: udp_receiver.h:151
sick_scansegment_xd::UdpReceiver::Stop
void Stop(bool do_join=true)
Definition: udp_receiver.cpp:160


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:13