test_stream.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // Copyright 2022 Universal Robots A/S
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of the {copyright_holder} nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 // -- END LICENSE BLOCK ------------------------------------------------
30 
31 #include <gtest/gtest.h>
32 #include <chrono>
33 #include <condition_variable>
34 
39 
40 using namespace urcl;
41 
42 class StreamTest : public ::testing::Test
43 {
44 protected:
45  void SetUp()
46  {
47  server_.reset(new comm::TCPServer(60003));
48  server_->setConnectCallback(std::bind(&StreamTest::connectionCallback, this, std::placeholders::_1));
49  server_->setMessageCallback(std::bind(&StreamTest::messageCallback, this, std::placeholders::_1,
50  std::placeholders::_2, std::placeholders::_3));
51  server_->start();
52  }
53 
54  void teardown()
55  {
56  // Clean up
57  server_.reset();
58  }
59 
60  // callback functions for the tcp server
61  void messageCallback(const socket_t filedescriptor, char* buffer, size_t nbytesrecv)
62  {
63  std::lock_guard<std::mutex> lk(message_mutex_);
64  read_ = nbytesrecv;
65  received_message_ = std::string(buffer);
66  message_cv_.notify_one();
67  message_callback_ = true;
68  }
69 
70  void connectionCallback(const socket_t filedescriptor)
71  {
72  std::lock_guard<std::mutex> lk(connect_mutex_);
73  client_fd_ = filedescriptor;
74  connect_cv_.notify_one();
75  connection_callback_ = true;
76  }
77 
78  bool waitForMessageCallback(int milliseconds = 100)
79  {
80  std::unique_lock<std::mutex> lk(message_mutex_);
81  if (message_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
82  message_callback_ == true)
83  {
84  message_callback_ = false;
85  return true;
86  }
87  return false;
88  }
89 
90  bool waitForConnectionCallback(int milliseconds = 100)
91  {
92  std::unique_lock<std::mutex> lk(connect_mutex_);
93  if (connect_cv_.wait_for(lk, std::chrono::milliseconds(milliseconds)) == std::cv_status::no_timeout ||
94  connection_callback_ == true)
95  {
96  connection_callback_ = false;
97  return true;
98  }
99  return false;
100  }
101 
102  std::unique_ptr<comm::TCPServer> server_;
104  std::string received_message_;
105  size_t read_;
106 
107 private:
108  std::condition_variable message_cv_;
109  std::mutex message_mutex_;
110 
111  std::condition_variable connect_cv_;
112  std::mutex connect_mutex_;
113 
114  bool connection_callback_ = false;
115  bool message_callback_ = false;
116 };
117 
118 TEST_F(StreamTest, closed_stream)
119 {
120  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 60003);
121  stream.connect();
122 
123  EXPECT_TRUE(waitForConnectionCallback());
124  EXPECT_FALSE(stream.closed());
125 
126  stream.close();
127 
128  EXPECT_TRUE(stream.closed());
129 }
130 
131 TEST_F(StreamTest, connect_stream)
132 {
133  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 60003);
134 
135  // The socket state should be invalid until connect has been called
137  EXPECT_EQ(stream.getState(), expected_state);
138 
139  stream.connect();
140  EXPECT_TRUE(waitForConnectionCallback());
141 
142  expected_state = comm::SocketState::Connected;
143  EXPECT_EQ(stream.getState(), expected_state);
144 }
145 
146 TEST_F(StreamTest, read_buffer_to_small)
147 {
148  // RTDE package with timestamp
149  uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xcf, 0x8f, 0xf9, 0xdb, 0x22, 0xd0, 0xe5 };
150 
151  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 60003);
152  stream.connect();
153 
154  EXPECT_TRUE(waitForConnectionCallback());
155 
156  size_t written;
157  server_->write(client_fd_, data_package, sizeof(data_package), written);
158 
159  uint8_t buf[10];
160  size_t read = 0;
161 
162  // When the buffer is to small read should return false and the written bytes should be larger thant the read
163  EXPECT_FALSE(stream.read(buf, sizeof(buf), read));
164  EXPECT_GT(written, read);
165 }
166 
167 TEST_F(StreamTest, read_rtde_data_package)
168 {
169  // RTDE package with timestamp
170  uint8_t data_package[] = { 0x00, 0x0c, 0x55, 0x01, 0x40, 0xcf, 0x8f, 0xf9, 0xdb, 0x22, 0xd0, 0xe5 };
171 
172  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 60003);
173  stream.connect();
174 
175  EXPECT_TRUE(waitForConnectionCallback());
176 
177  size_t written;
178  server_->write(client_fd_, data_package, sizeof(data_package), written);
179 
180  uint8_t buf[4096];
181  size_t read = 0;
182  stream.read(buf, sizeof(buf), read);
183 
184  EXPECT_EQ(written, read);
185  for (unsigned int i = 0; i < read; ++i)
186  {
187  EXPECT_EQ(data_package[i], buf[i]);
188  }
189 }
190 
191 TEST_F(StreamTest, read_primary_data_package)
192 {
193  /* First RobotState of UR5e from URSim v5.8
194  *
195  * This package contains:
196  * - ROBOT_MODE_DATA
197  * - JOINT_DATA
198  * - CARTESIAN_INFO
199  * - KINEMATICS_INFO
200  * - NEEDED_FOR_CALIB_DATA
201  * - MASTERBOARD_DATA
202  * - TOOL_DATA
203  * - CONFIGURATION_DATA
204  * - FORCE_MODE_DATA
205  * - ADDITIONAL_INFO
206  * - SAFETY_DATA
207  * - TOOL_COMM_INFO
208  * - TOOL_MODE_INFO
209  */
210  unsigned char data_package[] = {
211  0x00, 0x00, 0x05, 0x6a, 0x10, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8, 0x7d, 0x17, 0x40, 0x01,
212  0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x3f, 0xef, 0x0a, 0x3d, 0x70, 0xa3, 0xd7, 0x0a, 0x00, 0x00, 0x00,
213  0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb, 0x01,
214  0xbf, 0xf9, 0x9c, 0x77, 0x9a, 0x6b, 0x50, 0xb0, 0xbf, 0xf9, 0x9c, 0x77, 0x9a, 0x6b, 0x50, 0xb0, 0x00, 0x00, 0x00,
215  0x00, 0x00, 0x00, 0x00, 0x00, 0xa3, 0xa8, 0x79, 0x38, 0x00, 0x00, 0x00, 0x00, 0x41, 0xcc, 0x00, 0x00, 0x00, 0x00,
216  0x00, 0x00, 0xfd, 0xbf, 0xfb, 0xa2, 0x33, 0x9c, 0x0e, 0xbe, 0xe0, 0xbf, 0xfb, 0xa2, 0x33, 0x9c, 0x0e, 0xbe, 0xe0,
217  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x09, 0x06, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x41, 0xc8, 0x00,
218  0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0xc0, 0x01, 0x9f, 0xbe, 0x76, 0xc8, 0xb4, 0x38, 0xc0, 0x01, 0x9f, 0xbe, 0x76,
219  0xc8, 0xb4, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xbf, 0x0f, 0xf7, 0x00, 0x00, 0x00, 0x00,
220  0x41, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0xbf, 0xe9, 0xdb, 0x22, 0xd0, 0xe5, 0x60, 0x40, 0xbf, 0xe9,
221  0xdb, 0x22, 0xd0, 0xe5, 0x60, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x6e, 0xbb, 0xe2, 0x00,
222  0x00, 0x00, 0x00, 0x41, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x3f, 0xf9, 0x85, 0x87, 0x93, 0xdd, 0x97,
223  0xf6, 0x3f, 0xf9, 0x85, 0x87, 0x93, 0xdd, 0x97, 0xf6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0xe6,
224  0x05, 0x69, 0x00, 0x00, 0x00, 0x00, 0x41, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0xbf, 0x9f, 0xbe, 0x76,
225  0xc8, 0xb4, 0x39, 0x00, 0xbf, 0x9f, 0xbe, 0x76, 0xc8, 0xb4, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0xbc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x00,
227  0x00, 0x00, 0x65, 0x04, 0xbf, 0xc2, 0x6d, 0x90, 0xa0, 0x1d, 0xe7, 0x77, 0xbf, 0xdb, 0xe1, 0x32, 0xf6, 0xa8, 0x66,
228  0x44, 0x3f, 0xc9, 0xdc, 0x1e, 0xb0, 0x03, 0x31, 0xe6, 0xbf, 0x54, 0x02, 0xc0, 0xf8, 0xe6, 0xe6, 0x79, 0x40, 0x08,
229  0xee, 0x22, 0x63, 0x78, 0xfa, 0xe0, 0x3f, 0xa3, 0xe9, 0xa4, 0x23, 0x7a, 0x7b, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
230  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
231  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe1, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
233  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00,
234  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xdb, 0x33, 0x33, 0x33,
237  0x33, 0x33, 0x33, 0xbf, 0xd9, 0x19, 0xce, 0x07, 0x5f, 0x6f, 0xd2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc4, 0xcc,
239  0xcc, 0xcc, 0xcc, 0xcc, 0xcd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240  0x00, 0x00, 0x3f, 0xc1, 0x0f, 0xf9, 0x72, 0x47, 0x45, 0x39, 0x3f, 0xb9, 0x85, 0xf0, 0x6f, 0x69, 0x44, 0x67, 0x3f,
241  0xb9, 0x7f, 0x62, 0xb6, 0xae, 0x7d, 0x56, 0x3f, 0xf9, 0x21, 0xfb, 0x54, 0x52, 0x45, 0x50, 0x00, 0x00, 0x00, 0x00,
242  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf9, 0x21, 0xfb, 0x54, 0x52, 0x45,
243  0x50, 0xbf, 0xf9, 0x21, 0xfb, 0x54, 0x52, 0x45, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244  0x00, 0x00, 0x00, 0x00, 0x00, 0x35, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247  0x00, 0x4b, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0x70, 0x62, 0x4d, 0xe0, 0x00, 0x00,
249  0x00, 0x3f, 0x70, 0x62, 0x4d, 0xe0, 0x00, 0x00, 0x00, 0x41, 0xc0, 0x00, 0x00, 0x42, 0x40, 0x00, 0x00, 0x00, 0x00,
250  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x57, 0xf4, 0x28, 0x5b, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
251  0x25, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x00, 0x01, 0xbd,
253  0x06, 0xc0, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0x40, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0xc0, 0x19,
254  0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0x40, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0xc0, 0x19, 0x57, 0x99, 0x28,
255  0x2c, 0x2f, 0x63, 0x40, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0xc0, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63,
256  0x40, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0xc0, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0x40, 0x19, 0x57,
257  0x99, 0x28, 0x2c, 0x2f, 0x63, 0xc0, 0x19, 0x57, 0x99, 0x28, 0x2c, 0x2f, 0x63, 0x40, 0x19, 0x57, 0x99, 0x28, 0x2c,
258  0x2f, 0x63, 0x40, 0x0a, 0xbb, 0x94, 0xed, 0xdd, 0xc6, 0xb1, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
259  0x0a, 0xbb, 0x94, 0xed, 0xdd, 0xc6, 0xb1, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0a, 0xbb, 0x94,
260  0xed, 0xdd, 0xc6, 0xb1, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0a, 0xbb, 0x94, 0xed, 0xdd, 0xc6,
261  0xb1, 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0a, 0xbb, 0x94, 0xed, 0xdd, 0xc6, 0xb1, 0x40, 0x44,
262  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x0a, 0xbb, 0x94, 0xed, 0xdd, 0xc6, 0xb1, 0x40, 0x44, 0x00, 0x00, 0x00,
263  0x00, 0x00, 0x00, 0x3f, 0xf0, 0xc1, 0x52, 0x38, 0x2d, 0x73, 0x65, 0x3f, 0xf6, 0x57, 0x18, 0x4a, 0xe7, 0x44, 0x87,
264  0x3f, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf3, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3f, 0xd0, 0x00,
265  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xdb, 0x33, 0x33, 0x33, 0x33,
266  0x33, 0x33, 0xbf, 0xd9, 0x19, 0xce, 0x07, 0x5f, 0x6f, 0xd2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
267  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xc4, 0xcc, 0xcc,
268  0xcc, 0xcc, 0xcc, 0xcd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
269  0x00, 0x3f, 0xc1, 0x0f, 0xf9, 0x72, 0x47, 0x45, 0x39, 0x3f, 0xb9, 0x85, 0xf0, 0x6f, 0x69, 0x44, 0x67, 0x3f, 0xb9,
270  0x7f, 0x62, 0xb6, 0xae, 0x7d, 0x56, 0x3f, 0xf9, 0x21, 0xfb, 0x54, 0x52, 0x45, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
271  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xf9, 0x21, 0xfb, 0x54, 0x52, 0x45, 0x50,
272  0xbf, 0xf9, 0x21, 0xfb, 0x54, 0x52, 0x45, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
273  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
274  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
275  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01,
276  0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x3d, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
277  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
278  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
279  0x3f, 0x6c, 0xf5, 0xac, 0x1d, 0xb9, 0xa1, 0x08, 0x00, 0x00, 0x00, 0x09, 0x08, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
280  0x00, 0x2b, 0x0a, 0x57, 0xf4, 0x28, 0x5b, 0x01, 0x01, 0xbf, 0xb8, 0x4d, 0xc2, 0x84, 0x9f, 0xed, 0xcf, 0xbf, 0xb0,
281  0x37, 0x9e, 0xd0, 0x87, 0xba, 0x97, 0x3f, 0xe2, 0xa2, 0x5b, 0x78, 0xc3, 0x9f, 0x6c, 0x3f, 0xc0, 0xa3, 0xd7, 0x0a,
282  0x3d, 0x70, 0xa4, 0x00, 0x00, 0x00, 0x1a, 0x0b, 0x00, 0x00, 0x01, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
283  0x00, 0x01, 0x3f, 0xc0, 0x00, 0x00, 0x40, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0c, 0x00, 0x01, 0x01
284  };
285  comm::URStream<primary_interface::PrimaryPackage> stream("127.0.0.1", 60003);
286  stream.connect();
287 
288  EXPECT_TRUE(waitForConnectionCallback());
289 
290  size_t written;
291  server_->write(client_fd_, data_package, sizeof(data_package), written);
292 
293  uint8_t buf[4096];
294  size_t read = 0;
295  stream.read(buf, sizeof(buf), read);
296 
297  EXPECT_EQ(written, read);
298  for (unsigned int i = 0; i < read; ++i)
299  {
300  EXPECT_EQ(data_package[i], buf[i]);
301  }
302 }
303 
304 TEST_F(StreamTest, write_data_package)
305 {
306  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 60003);
307  stream.connect();
308 
309  EXPECT_TRUE(waitForConnectionCallback());
310 
311  std::string send_message = "test message";
312  const uint8_t* data = reinterpret_cast<const uint8_t*>(send_message.c_str());
313  size_t len = send_message.size();
314  size_t written;
315  stream.write(data, len, written);
316 
317  EXPECT_TRUE(waitForMessageCallback());
318 
319  // Test that the message and the size of the message are equal
320  EXPECT_EQ(written, read_);
321  EXPECT_EQ(send_message, received_message_);
322 }
323 
324 TEST_F(StreamTest, connect_non_connected_robot)
325 {
326  comm::URStream<rtde_interface::RTDEPackage> stream("127.0.0.1", 12321);
327  auto start = std::chrono::system_clock::now();
328  EXPECT_FALSE(stream.connect(2, std::chrono::milliseconds(500)));
329  auto end = std::chrono::system_clock::now();
330  auto elapsed = end - start;
331  // This is only a rough estimate, obviously
332  EXPECT_LT(elapsed, std::chrono::milliseconds(7500));
333 }
334 
335 int main(int argc, char* argv[])
336 {
337  ::testing::InitGoogleTest(&argc, argv);
338 
339  return RUN_ALL_TESTS();
340 }
data_package.h
StreamTest::teardown
void teardown()
Definition: test_stream.cpp:54
urcl::comm::URStream
The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the s...
Definition: stream.h:40
socket_t
int socket_t
Definition: socket_t.h:57
StreamTest::server_
std::unique_ptr< comm::TCPServer > server_
Definition: test_stream.cpp:102
urcl::comm::URStream::connect
bool connect(const size_t max_num_tries=0, const std::chrono::milliseconds reconnection_time=std::chrono::seconds(10))
Connects to the configured socket.
Definition: stream.h:63
urcl
Definition: bin_parser.h:36
urcl::comm::URStream::write
bool write(const uint8_t *buf, const size_t buf_len, size_t &written)
Writes directly to the underlying socket (with a mutex guard)
Definition: stream.h:127
main
int main(int argc, char *argv[])
Definition: test_stream.cpp:335
StreamTest
Definition: test_stream.cpp:42
StreamTest::read_
size_t read_
Definition: test_stream.cpp:105
TEST_F
TEST_F(StreamTest, closed_stream)
Definition: test_stream.cpp:118
urcl::comm::SocketState::Invalid
@ Invalid
Socket is initialized or setup failed.
StreamTest::waitForMessageCallback
bool waitForMessageCallback(int milliseconds=100)
Definition: test_stream.cpp:78
primary_package.h
StreamTest::connect_cv_
std::condition_variable connect_cv_
Definition: test_stream.cpp:111
urcl::comm::URStream::read
bool read(uint8_t *buf, const size_t buf_len, size_t &read)
Reads a full UR package out of a socket. For this, it looks into the package and reads the byte lengt...
Definition: stream.h:134
stream.h
urcl::comm::TCPServer
Wrapper class for a TCP socket server.
Definition: tcp_server.h:58
StreamTest::message_cv_
std::condition_variable message_cv_
Definition: test_stream.cpp:108
StreamTest::client_fd_
socket_t client_fd_
Definition: test_stream.cpp:103
urcl::comm::TCPSocket::close
void close()
Closes the connection to the socket.
Definition: tcp_socket.cpp:151
StreamTest::message_mutex_
std::mutex message_mutex_
Definition: test_stream.cpp:109
StreamTest::connect_mutex_
std::mutex connect_mutex_
Definition: test_stream.cpp:112
StreamTest::waitForConnectionCallback
bool waitForConnectionCallback(int milliseconds=100)
Definition: test_stream.cpp:90
StreamTest::received_message_
std::string received_message_
Definition: test_stream.cpp:104
urcl::comm::SocketState::Connected
@ Connected
Socket is connected and ready to use.
StreamTest::messageCallback
void messageCallback(const socket_t filedescriptor, char *buffer, size_t nbytesrecv)
Definition: test_stream.cpp:61
StreamTest::SetUp
void SetUp()
Definition: test_stream.cpp:45
urcl::comm::URStream::closed
bool closed()
Returns whether the underlying socket is currently closed.
Definition: stream.h:81
urcl::comm::SocketState
SocketState
State the socket can be in.
Definition: tcp_socket.h:37
StreamTest::connectionCallback
void connectionCallback(const socket_t filedescriptor)
Definition: test_stream.cpp:70
tcp_server.h
urcl::comm::TCPSocket::getState
SocketState getState()
Getter for the state of the socket.
Definition: tcp_socket.h:82


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58