source: pacpussensors/trunk/Vislab/lib3dv-1.2.0/lib3dv/detail/device_impl.cc

Last change on this file was 136, checked in by ldecherf, 8 years ago

Doc

File size: 70.0 KB
Line 
1/* lib3dv/detail/device_impl.cc
2 *
3 * Copyright (C) 2013 VisLab
4 *
5 * This file is part of lib3dv; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20#include <lib3dv/detail/device_impl.h>
21
22#include <boost/asio/deadline_timer.hpp>
23#include <boost/bind/protect.hpp>
24#include <boost/ref.hpp>
25#include <boost/system/error_code.hpp>
26#include <boost/uuid/uuid_io.hpp>
27
28#include <cstring>
29#include <iostream>
30#include <vector>
31
32namespace lib3dv
33{
34 void on_deadline(boost::asio::deadline_timer& deadline,
35 boost::asio::ip::udp::socket& socket,
36 uint8_t log_level,
37 bool wait,
38 boost::asio::io_service& io_service,
39 const boost::function<void (void)>& cleanup,
40 const boost::system::error_code& error)
41 {
42 if(!error) // can be a direct invocation, or triggered by an expired timer
43 {
44 if(deadline.expires_at() < boost::asio::deadline_timer::traits_type::now()) // expired timer
45 {
46 if(log_level > 0) std::cout << "[II] lib3dv: deadline expired, cleaning up..." << std::endl;
47
48 cleanup();
49 }
50 else // direct (first) invocation
51 {
52 deadline.async_wait(boost::bind(on_deadline, boost::ref(deadline), boost::ref(socket), log_level, wait, boost::ref(io_service), cleanup, _1));
53 }
54 }
55 else if(error == boost::asio::error::operation_aborted) // can be triggered by cancel() or expires_from_now()
56 {
57 if(log_level > 1) std::cout << "[II] lib3dv: deadline canceled" << std::endl; // cancel(), nothing to do
58
59 if(wait) // expires_from_now() we start to wait again
60 {
61 if(log_level > 1) std::cout << "[II] lib3dv: data deadline extended" << std::endl;
62
63 deadline.async_wait(boost::bind(on_deadline, boost::ref(deadline), boost::ref(socket), log_level, wait, boost::ref(io_service), cleanup, _1));
64 }
65 }
66 else // another error occourred
67 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
68 }
69
70 lib3dv::device create(boost::shared_ptr<lib3dv::detail::device_impl> impl)
71 {
72 lib3dv::device device;
73 device.m_impl = impl;
74 return device;
75 }
76
77 namespace detail
78 {
79 device_impl::device_impl(const boost::asio::ip::address_v4& local_address,
80 const boost::asio::ip::address_v4& remote_address, unsigned short remote_commands_port, unsigned short remote_data_port,
81 const boost::uuids::uuid& guid,
82 const std::bitset<device::capability::NUM>& capabilities,
83 const device::version_info& version,
84 uint8_t log_level,
85 lib3dv::error& device_error) :
86 m_log_level(log_level),
87 m_timeout(device::DEFAULT_TIMEOUT),
88 m_local_address(local_address),
89 m_remote_address(remote_address),
90 m_remote_commands_port(remote_commands_port),
91 m_remote_data_port(remote_data_port),
92 m_data_socket(m_data_socket_io_service),
93 m_commands_socket(m_commands_socket_io_service),
94 m_commands_deadline(m_commands_socket_io_service),
95 m_data_deadline(m_data_socket_io_service),
96 m_guid(guid),
97 m_capabilities(capabilities),
98 m_version(version),
99 m_max_connection_id(0),
100 m_status(device::status::ONLINE)
101 {
102 boost::system::error_code error;
103
104 m_data_socket.open(boost::asio::ip::udp::v4(), error);
105
106 if(error)
107 {
108 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
109 device_error = lib3dv::error::NETWORK_FAILURE;
110 }
111
112 m_commands_socket.open(boost::asio::ip::udp::v4(), error);
113
114 if(error)
115 {
116 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
117 device_error = lib3dv::error::NETWORK_FAILURE;
118 }
119
120#ifdef _MSC_VER
121 boost::asio::socket_base::receive_buffer_size option(8192 * 1024);
122 m_data_socket.set_option(option);
123#endif
124
125 m_data_socket.set_option(boost::asio::socket_base::broadcast(true));
126 m_data_socket.bind(boost::asio::ip::udp::endpoint(local_address, 0), error);
127
128 if(error)
129 {
130 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
131 device_error = lib3dv::error::NETWORK_FAILURE;
132 }
133
134 m_commands_socket.set_option(boost::asio::socket_base::broadcast(true));
135 m_commands_socket.bind(boost::asio::ip::udp::endpoint(local_address, 0), error);
136
137 if(error)
138 {
139 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
140 device_error = lib3dv::error::NETWORK_FAILURE;
141 }
142
143 m_data_socket_io_service.post(boost::bind(&device_impl::on_data_received, this, boost::system::error_code(), 0));
144 m_data_socket_io_service_thread = boost::thread(boost::bind(&boost::asio::io_service::run, &m_data_socket_io_service));
145
146 if(log_level > 0) std::cout << "[II] lib3dv: device " << m_guid << std::endl;
147 if(log_level > 0) std::cout << "\tcontrol channel: " << m_commands_socket.local_endpoint() << " <-> " << m_remote_address << ':' << m_remote_commands_port << std::endl;
148 if(log_level > 0) std::cout << "\tdata channel " << m_data_socket.local_endpoint() << " <-> " << m_remote_address << ':' << m_remote_data_port << std::endl;
149
150 if(device_error != lib3dv::error::NONE)
151 m_status = device::status::OFFLINE;
152 }
153
154 device_impl::~device_impl()
155 {
156 if(m_log_level > 0) std::cout << "[II] lib3dv: destructor called" << std::endl;
157
158 boost::system::error_code error;
159
160 if(m_data_socket.is_open())
161 m_data_socket.close(error);
162
163 m_data_socket_io_service.stop();
164
165 if(m_commands_socket.is_open())
166 m_commands_socket.close(error);
167
168 m_commands_socket_io_service.stop();
169
170 m_data_socket_io_service_thread.join();
171
172 if(m_log_level > 0) std::cout << "[II] lib3dv: destructor complete" << std::endl;
173 }
174
175 uint64_t device_impl::connect_image_callback(const boost::function<void (boost::shared_ptr<const lib3dv::image>, uint32_t)>& function)
176 {
177 m_connections[m_max_connection_id] = m_image_signal.connect(function);
178 ++m_max_connection_id;
179 return (m_max_connection_id - 1);
180 }
181
182 uint64_t device_impl::connect_terrain_callback(const boost::function<void (boost::shared_ptr<const lib3dv::terrain>, uint32_t)>& function)
183 {
184 m_connections[m_max_connection_id] = m_terrain_signal.connect(function);
185 ++m_max_connection_id;
186 return (m_max_connection_id - 1);
187 }
188
189 uint64_t device_impl::connect_obstacles_callback(const boost::function<void (boost::shared_ptr<const std::vector<lib3dv::obstacle> >, uint32_t)>& function)
190 {
191 m_connections[m_max_connection_id] = m_obstacles_signal.connect(function);
192 ++m_max_connection_id;
193 return (m_max_connection_id - 1);
194 }
195
196 uint64_t device_impl::connect_motion_callback(const boost::function<void (boost::shared_ptr<const lib3dv::motion>, uint32_t)>& function)
197 {
198 m_connections[m_max_connection_id] = m_motion_signal.connect(function);
199 ++m_max_connection_id;
200 return (m_max_connection_id - 1);
201 }
202
203 uint64_t device_impl::connect_classification_callback(const boost::function<void (boost::shared_ptr<const lib3dv::classification>, uint32_t)>& function)
204 {
205 m_connections[m_max_connection_id] = m_classification_signal.connect(function);
206 ++m_max_connection_id;
207 return (m_max_connection_id - 1);
208 }
209
210
211 LIB3DV_DEPRECATED uint64_t device_impl::connect_image_callback(const boost::function<void (boost::shared_ptr<lib3dv::image>, uint32_t)>& function)
212 {
213 m_connections[m_max_connection_id] = m_deprecated_image_signal.connect(function);
214 ++m_max_connection_id;
215 return (m_max_connection_id - 1);
216 }
217
218 LIB3DV_DEPRECATED uint64_t device_impl::connect_terrain_callback(const boost::function<void (boost::shared_ptr<lib3dv::terrain>, uint32_t)>& function)
219 {
220 m_connections[m_max_connection_id] = m_deprecated_terrain_signal.connect(function);
221 ++m_max_connection_id;
222 return (m_max_connection_id - 1);
223 }
224
225 LIB3DV_DEPRECATED uint64_t device_impl::connect_obstacles_callback(const boost::function<void (boost::shared_ptr<std::vector<lib3dv::obstacle> >, uint32_t)>& function)
226 {
227 m_connections[m_max_connection_id] = m_deprecated_obstacles_signal.connect(function);
228 ++m_max_connection_id;
229 return (m_max_connection_id - 1);
230 }
231
232 LIB3DV_DEPRECATED uint64_t device_impl::connect_motion_callback(const boost::function<void (boost::shared_ptr<lib3dv::motion>, uint32_t)>& function)
233 {
234 m_connections[m_max_connection_id] = m_deprecated_motion_signal.connect(function);
235 ++m_max_connection_id;
236 return (m_max_connection_id - 1);
237 }
238
239 LIB3DV_DEPRECATED uint64_t device_impl::connect_classification_callback(const boost::function<void (boost::shared_ptr<lib3dv::classification>, uint32_t)>& function)
240 {
241 m_connections[m_max_connection_id] = m_deprecated_classification_signal.connect(function);
242 ++m_max_connection_id;
243 return (m_max_connection_id - 1);
244 }
245
246 uint64_t device_impl::connect_timeout_callback(const boost::function<void (void)>& function)
247 {
248 m_connections[m_max_connection_id] = m_timeout_signal.connect(function);
249 ++m_max_connection_id;
250 return (m_max_connection_id - 1);
251 }
252
253 void device_impl::disconnect_callback(uint64_t id)
254 {
255 std::map<uint64_t, boost::signals2::connection>::iterator cc = m_connections.find(id);
256
257 if(cc != m_connections.end())
258 {
259 cc->second.disconnect();
260 m_connections.erase(cc);
261 }
262 }
263
264
265
266 void on_device_info_received(const boost::system::error_code& error,
267 size_t bytes_received,
268 boost::asio::ip::udp::socket& socket,
269 boost::array<uint8_t, device_impl::MAX_UDP_PAYLOAD_SIZE>& receive_buffer,
270 boost::asio::ip::udp::endpoint& local_endpoint,
271 boost::asio::ip::udp::endpoint& remote_endpoint,
272 std::vector<device>& devices,
273 uint8_t log_level,
274 lib3dv::error& device_error)
275 {
276 if(!error)
277 {
278 if(bytes_received >= sizeof(protocol::packet_header) + sizeof(protocol::command_header))
279 {
280 if(!memcmp(receive_buffer.data(), "#3DV", 4) && receive_buffer.data()[4] <= protocol::version::MAX_SUPPORTED)
281 {
282 const protocol::packet_header* packet_header = reinterpret_cast<const protocol::packet_header*>(receive_buffer.data());
283 const protocol::command_header* command_header = reinterpret_cast<const protocol::command_header*>(receive_buffer.data() + sizeof(protocol::packet_header));
284 const protocol::device_info* device_info = reinterpret_cast<const protocol::device_info*>(receive_buffer.data() + sizeof(protocol::packet_header) + sizeof(protocol::command_header));
285
286 device_error = static_cast<lib3dv::error::types>(packet_header->m_error);
287
288 if(device_error == lib3dv::error::NONE)
289 {
290 boost::uuids::uuid guid;
291
292 std::copy(device_info->m_guid, device_info->m_guid + 16, guid.data);
293
294 std::bitset<device::capability::NUM> capabilities;
295
296 for(unsigned int i = 0; i < device::capability::NUM; ++i)
297 if((device_info->m_capabilities) & (1 << i))
298 capabilities.set(i);
299
300 device::version_info version;
301 const uint8_t* raw_framework_version = device_info->m_framework_version;
302 const uint8_t* raw_application_version = device_info->m_application_version;
303
304 version.m_protocol = packet_header->m_protocol_version;
305 std::copy(device_info->m_framework_version, raw_framework_version + 3, version.m_framework);
306 std::copy(raw_application_version, raw_application_version + 3, version.m_application);
307
308 device_impl* device = new device_impl(local_endpoint.address().to_v4(),
309 remote_endpoint.address().to_v4(), remote_endpoint.port(), 0,
310 guid,
311 capabilities,
312 version,
313 log_level, device_error);
314
315 if(device_error == lib3dv::error::NONE)
316 devices.push_back(create(boost::shared_ptr<device_impl>(device)));
317 else
318 delete device;
319 }
320 }
321 else
322 std::cerr << "[EE] lib3dv: unknown packet received" << std::endl;
323 }
324 else
325 {
326 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
327 device_error = lib3dv::error::NETWORK_FAILURE;
328 }
329
330 if(socket.is_open())
331 socket.async_receive_from(boost::asio::buffer(receive_buffer), remote_endpoint, boost::bind(on_device_info_received, _1, _2,
332 boost::ref(socket), boost::ref(receive_buffer),
333 boost::ref(local_endpoint), boost::ref(remote_endpoint),
334 boost::ref(devices), log_level, boost::ref(device_error)));
335 }
336 }
337
338 void cleanup(boost::asio::io_service& io_service, boost::asio::ip::udp::socket& socket, lib3dv::error& device_error)
339 {
340 boost::system::error_code error;
341
342 if(socket.is_open())
343 socket.close(error);
344
345 device_error = error ? lib3dv::error::NETWORK_FAILURE : lib3dv::error::NONE;
346
347 io_service.stop();
348 }
349
350 std::vector<device> device_impl::enumerate(const boost::asio::ip::address_v4& local_address, unsigned short remote_port,
351 uint8_t log_level,
352 lib3dv::error& device_error,
353 const boost::posix_time::time_duration& timeout)
354 {
355 std::vector<device> devices;
356 boost::asio::ip::udp::endpoint local_endpoint(local_address, 0);
357
358 boost::asio::io_service io_service;
359 boost::asio::ip::udp::socket socket(io_service);
360 boost::asio::deadline_timer deadline(io_service);
361 boost::system::error_code error;
362
363 socket.open(boost::asio::ip::udp::v4(), error);
364
365 if(error)
366 {
367 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
368 device_error = lib3dv::error::NETWORK_FAILURE;
369 return devices;
370 }
371
372 socket.set_option(boost::asio::socket_base::broadcast(true));
373 socket.bind(local_endpoint, error);
374
375 if(error)
376 {
377 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
378 device_error = lib3dv::error::NETWORK_FAILURE;
379 return devices;
380 }
381
382 boost::asio::ip::udp::endpoint remote_endpoint;
383 boost::array<uint8_t, MAX_UDP_PAYLOAD_SIZE> receive_buffer;
384
385 socket.async_receive_from(boost::asio::buffer(receive_buffer), remote_endpoint, boost::bind(on_device_info_received, _1, _2,
386 boost::ref(socket), boost::ref(receive_buffer),
387 boost::ref(local_endpoint), boost::ref(remote_endpoint),
388 boost::ref(devices), log_level, boost::ref(device_error)));
389
390 deadline.expires_from_now(timeout);
391 on_deadline(deadline, socket, log_level, false, io_service, boost::bind(cleanup, boost::ref(io_service), boost::ref(socket), boost::ref(device_error)), boost::system::error_code());
392
393 boost::thread io_service_thread(boost::bind(&boost::asio::io_service::run, &io_service));
394
395 boost::asio::ip::address_v4 remote_broadcast_address = boost::asio::ip::address_v4::broadcast(local_address, boost::asio::ip::address_v4::netmask(local_address));
396
397 boost::shared_ptr<protocol::packet_header> packet_header = boost::shared_ptr<protocol::packet_header>(new protocol::packet_header());
398 boost::shared_ptr<protocol::command_header> raw_command = boost::shared_ptr<protocol::command_header>(new protocol::command_header());
399 boost::shared_ptr<std::vector<uint8_t> > raw_command_data = boost::shared_ptr<std::vector<uint8_t> >(new std::vector<uint8_t>());
400
401 packet_header->m_magic[0] = '#';
402 packet_header->m_magic[1] = '3';
403 packet_header->m_magic[2] = 'D';
404 packet_header->m_magic[3] = 'V';
405 packet_header->m_protocol_version = 0;
406 packet_header->m_fragment = 0;
407 packet_header->m_total_fragments = 1;
408 packet_header->m_payload_type = protocol::payload::COMMAND;
409
410 raw_command->m_type = lib3dv::detail::command::ENUMERATE_DEVICES;
411 raw_command->m_size = 0;
412
413 boost::array<boost::asio::const_buffer, 3> buffers;
414
415 buffers[0] = boost::asio::buffer(packet_header.get(), sizeof(protocol::packet_header));
416 buffers[1] = boost::asio::buffer(raw_command.get(), sizeof(protocol::command_header));
417 buffers[2] = boost::asio::buffer(raw_command_data->data(), raw_command_data->size());
418
419 if(log_level > 0) std::cout << "[II] lib3dv: broadcasting discovery message to " << remote_broadcast_address << ':' << remote_port << std::endl;
420
421 socket.send_to(buffers, boost::asio::ip::udp::endpoint(remote_broadcast_address, remote_port), 0, error);
422
423 if(error)
424 {
425 std::cerr << "[EE] lib3dv: " << error.message() << std::endl;
426 device_error = lib3dv::error::NETWORK_FAILURE;
427 return devices;
428 }
429
430 if(log_level > 0) std::cout << "[II] lib3dv: broadcast complete" << std::endl;
431
432 io_service_thread.join();
433
434 if(log_level > 0) std::cout << "[II] lib3dv: device discovery complete" << std::endl;
435
436 device_error = lib3dv::error::NONE;
437
438 return devices;
439 }
440
441 void device_impl::on_data_received(const boost::system::error_code& error, size_t bytes_received)
442 {
443 if(m_log_level > 1) std::cout << std::endl << "[II] lib3dv: " << " " << bytes_received << " data bytes read" << std::endl;
444
445 m_data_deadline.expires_from_now(m_timeout);
446
447 if(bytes_received >= sizeof(protocol::packet_header) && !memcmp(m_data_payload.c_array(), "#3DV", 4))
448 {
449 protocol::packet_header* packet_header = reinterpret_cast<protocol::packet_header*>(m_data_payload.c_array());
450
451 if(m_log_level > 1)
452 {
453 std::cout << "\t" << "protocol version = " << (int)(packet_header->m_protocol_version) << std::endl;
454 std::cout << "\t" << "guid = " << packet_header->m_guid << std::endl;
455 std::cout << "\t" << "fragment = " << packet_header->m_fragment << " / " << packet_header->m_total_fragments << std::endl;
456 std::cout << "\t" << "payload_type = " << (int)packet_header->m_payload_type << std::endl;
457 }
458
459 cleanup_reassembly_container(m_images, m_image_fragments);
460 cleanup_reassembly_container(m_terrains, m_terrain_fragments);
461 cleanup_reassembly_container(m_obstacles, m_obstacle_fragments);
462 cleanup_reassembly_container(m_poses, m_pose_fragments);
463
464 switch(packet_header->m_payload_type)
465 {
466 case protocol::payload::IMAGE_INFO_HEADER :
467 {
468 protocol::image_info_header* image_info_header = reinterpret_cast<protocol::image_info_header*>(m_data_payload.c_array() + sizeof(protocol::packet_header));
469
470 if(m_log_level > 0 && image_info_header->m_type)
471 {
472 std::cout << (m_log_level == 1 ? "\n" : "");
473 std::cout << (m_log_level > 1 ? "\t" : "") << "image" << std::endl;
474 std::cout << (m_log_level > 1 ? "\t" : "") << " guid = " << packet_header->m_guid << std::endl;
475 std::cout << (m_log_level > 1 ? "\t" : "") << " header:" << std::endl;
476 std::cout << (m_log_level > 1 ? "\t" : "") << " bpp = " << (int)image_info_header->m_bpp << std::endl;
477 std::cout << (m_log_level > 1 ? "\t" : "") << " channels = " << (int)image_info_header->m_channels << std::endl;
478 std::cout << (m_log_level > 1 ? "\t" : "") << " width = " << image_info_header->m_width << std::endl;
479 std::cout << (m_log_level > 1 ? "\t" : "") << " height = " << image_info_header->m_height << std::endl;
480 std::cout << (m_log_level > 1 ? "\t" : "") << " size = " << image_info_header->m_size << std::endl;
481 std::cout << (m_log_level > 1 ? "\t" : "") << " type = " << (int)image_info_header->m_type << std::endl;
482 std::cout << (m_log_level > 1 ? "\t" : "") << " format = " << (int)image_info_header->m_format << std::endl;
483 std::cout << (m_log_level == 1 ? " data: " : "");
484 }
485
486 boost::shared_ptr<raw_image_type> image = init_reassembly_container(m_images, m_image_fragments, *image_info_header, packet_header->m_guid);
487
488 if(image)
489 {
490 boost::shared_ptr<lib3dv::image> output_image = boost::shared_ptr<lib3dv::image>(new lib3dv::image());
491
492 output_image->m_width = image->m_header.m_width;
493 output_image->m_height = image->m_header.m_height;
494 output_image->m_channels = image->m_header.m_channels;
495 output_image->m_bpp = image->m_header.m_bpp;
496 output_image->m_type = static_cast<lib3dv::image::type::types>(image->m_header.m_type);
497 output_image->m_format = static_cast<lib3dv::image::format::types>(image->m_header.m_format);
498 output_image->m_timestamp = boost::posix_time::ptime(boost::gregorian::date(1970,1,1)) +
499 boost::posix_time::seconds(image->m_header.m_timestamp / 1000000) + // this serves as a workaround to an integer overflow in boost versions <= 1.51
500 boost::posix_time::microseconds(image->m_header.m_timestamp % 1000000);
501 output_image->m_buffer.swap(image->m_buffer);
502
503 m_image_signal(output_image, packet_header->m_guid);
504 m_deprecated_image_signal(output_image, packet_header->m_guid);
505 }
506
507 break;
508 }
509
510 case protocol::payload::IMAGE_DATA :
511 {
512 boost::shared_ptr<raw_image_type> image = fill_reassembly_container(m_images, m_image_fragments,
513 bytes_received, packet_header->m_guid,
514 packet_header->m_fragment, packet_header->m_total_fragments);
515
516 if(image)
517 {
518 boost::shared_ptr<lib3dv::image> output_image = boost::shared_ptr<lib3dv::image>(new lib3dv::image());
519
520 output_image->m_width = image->m_header.m_width;
521 output_image->m_height = image->m_header.m_height;
522 output_image->m_channels = image->m_header.m_channels;
523 output_image->m_bpp = image->m_header.m_bpp;
524 output_image->m_type = static_cast<lib3dv::image::type::types>(image->m_header.m_type);
525 output_image->m_format = static_cast<lib3dv::image::format::types>(image->m_header.m_format);
526 output_image->m_timestamp = boost::posix_time::ptime(boost::gregorian::date(1970,1,1)) +
527 boost::posix_time::seconds(image->m_header.m_timestamp / 1000000) + // this serves as a workaround to an integer overflow in boost versions <= 1.51
528 boost::posix_time::microseconds(image->m_header.m_timestamp % 1000000);
529 output_image->m_buffer.swap(image->m_buffer);
530
531 m_image_signal(output_image, packet_header->m_guid);
532 m_deprecated_image_signal(output_image, packet_header->m_guid);
533 }
534
535 break;
536 }
537
538 case protocol::payload::TERRAIN_INFO_HEADER :
539 {
540 protocol::terrain_info_header* terrain_info_header = reinterpret_cast<protocol::terrain_info_header*>(m_data_payload.c_array() + sizeof(protocol::packet_header));
541
542 if(m_log_level > 0)
543 {
544 std::cout << (m_log_level == 1 ? "\n" : "");
545 std::cout << (m_log_level > 1 ? "\t" : "") << "terrain" << std::endl;
546 std::cout << (m_log_level > 1 ? "\t" : "") << " guid = " << packet_header->m_guid << std::endl;
547 std::cout << (m_log_level > 1 ? "\t" : "") << " header:" << std::endl;
548 std::cout << (m_log_level > 1 ? "\t" : "") << " x cells number = " << (int)terrain_info_header->m_x_cell_num << std::endl;
549 std::cout << (m_log_level > 1 ? "\t" : "") << " x cells size = " << (int)terrain_info_header->m_x_cell_size << std::endl;
550 std::cout << (m_log_level > 1 ? "\t" : "") << " x min = " << terrain_info_header->m_x_min << std::endl;
551 std::cout << (m_log_level > 1 ? "\t" : "") << " y cells number = " << (int)terrain_info_header->m_y_cell_num << std::endl;
552 std::cout << (m_log_level > 1 ? "\t" : "") << " y cells size = " << (int)terrain_info_header->m_y_cell_size << std::endl;
553 std::cout << (m_log_level > 1 ? "\t" : "") << " y min = " << terrain_info_header->m_y_min << std::endl;
554 std::cout << (m_log_level == 1 ? " data: " : "");
555 }
556
557 boost::shared_ptr<raw_terrain_type> terrain = init_reassembly_container(m_terrains, m_terrain_fragments, *terrain_info_header, packet_header->m_guid);
558
559 if(terrain)
560 decode_terrain(terrain, packet_header->m_guid);
561
562 break;
563 }
564
565 case protocol::payload::TERRAIN_DATA :
566 {
567 boost::shared_ptr<raw_terrain_type> terrain = fill_reassembly_container(m_terrains, m_terrain_fragments,
568 bytes_received, packet_header->m_guid,
569 packet_header->m_fragment, packet_header->m_total_fragments);
570
571 if(terrain)
572 decode_terrain(terrain, packet_header->m_guid);
573
574 break;
575 }
576
577 case protocol::payload::OBSTACLES_MAP_INFO_HEADER :
578 {
579 protocol::obstacles_map_info_header* obstacles_map_info_header = reinterpret_cast<protocol::obstacles_map_info_header*>(m_data_payload.c_array() + sizeof(protocol::packet_header));
580
581 if(m_log_level > 0)
582 {
583 std::cout << (m_log_level == 1 ? "\n" : "");
584 std::cout << (m_log_level > 1 ? "\t" : "") << "obstacles" << std::endl;
585 std::cout << (m_log_level > 1 ? "\t" : "") << " guid = " << packet_header->m_guid << std::endl;
586 std::cout << (m_log_level > 1 ? "\t" : "") << " header:" << std::endl;
587 std::cout << (m_log_level > 1 ? "\t" : "") << " obstacles number = " << (int)obstacles_map_info_header->m_obstacles_num << std::endl;
588 std::cout << (m_log_level == 1 ? " data: " : "");
589 }
590
591 boost::shared_ptr<raw_obstacles_type> obstacles = init_reassembly_container(m_obstacles, m_obstacle_fragments, *obstacles_map_info_header, packet_header->m_guid);
592
593 if(obstacles)
594 decode_obstacles(obstacles, packet_header->m_guid);
595
596 break;
597 }
598
599 case protocol::payload::OBSTACLES_MAP_DATA:
600 {
601 boost::shared_ptr<raw_obstacles_type> obstacles = fill_reassembly_container(m_obstacles, m_obstacle_fragments,
602 bytes_received, packet_header->m_guid,
603 packet_header->m_fragment, packet_header->m_total_fragments);
604 if(obstacles)
605 decode_obstacles(obstacles, packet_header->m_guid);
606
607 break;
608 }
609
610 case protocol::payload::MOTION_INFO_HEADER :
611 {
612 protocol::motion_info_header* motion_info_header = reinterpret_cast<protocol::motion_info_header*>(m_data_payload.c_array() + sizeof(protocol::packet_header));
613
614 if(m_log_level > 0)
615 {
616 std::cout << (m_log_level == 1 ? "\n" : "");
617 std::cout << (m_log_level > 1 ? "\t" : "") << "pose" << std::endl;
618 std::cout << (m_log_level > 1 ? "\t" : "") << " guid = " << packet_header->m_guid << std::endl;
619 std::cout << (m_log_level > 1 ? "\t" : "") << " header:" << std::endl;
620 std::cout << (m_log_level > 1 ? "\t" : "") << " size = " << motion_info_header->m_size << std::endl;
621 std::cout << (m_log_level > 1 ? "\t" : "") << " poses = " << (int) motion_info_header->m_poses_num << std::endl;
622 std::cout << (m_log_level == 1 ? " data: " : "");
623 }
624
625 boost::shared_ptr<raw_motion_type> motion = init_reassembly_container(m_poses, m_pose_fragments, *motion_info_header, packet_header->m_guid);
626
627 if(motion)
628 decode_motion(motion, packet_header->m_guid);
629
630 break;
631 }
632
633 case protocol::payload::MOTION_DATA :
634 {
635 boost::shared_ptr<raw_motion_type> motion = fill_reassembly_container(m_poses, m_pose_fragments,
636 bytes_received, packet_header->m_guid,
637 packet_header->m_fragment, packet_header->m_total_fragments);
638
639 if(motion)
640 decode_motion(motion, packet_header->m_guid);
641
642 break;
643 }
644
645 case protocol::payload::CLASSIFICATION_INFO_HEADER :
646 {
647 //std::cout << "classification header!"<<std::endl;
648
649 protocol::classifier_info_header* classifier_info_header = reinterpret_cast<protocol::classifier_info_header*>(m_data_payload.c_array() + sizeof(protocol::packet_header));
650
651 if(m_log_level > 0)
652 {
653 std::cout << (m_log_level == 1 ? "\n" : "");
654 std::cout << (m_log_level > 1 ? "\t" : "") << "candidate" << std::endl;
655 std::cout << (m_log_level > 1 ? "\t" : "") << " guid = " << packet_header->m_guid << std::endl;
656 std::cout << (m_log_level > 1 ? "\t" : "") << " header:" << std::endl;
657 std::cout << (m_log_level > 1 ? "\t" : "") << " size = " << classifier_info_header->m_size << std::endl;
658 std::cout << (m_log_level == 1 ? " data: " : "");
659 }
660
661 boost::shared_ptr<raw_candidate_type> classification = init_reassembly_container(m_candidates, m_candidates_fragments, *classifier_info_header, packet_header->m_guid);
662
663 if(classification)
664 decode_classification(classification, packet_header->m_guid);
665
666 break;
667 }
668
669 case protocol::payload::CLASSIFICATION_DATA :
670 {
671 //std::cout << "classification data!"<<std::endl;
672
673 boost::shared_ptr<raw_candidate_type> classification = fill_reassembly_container(m_candidates, m_candidates_fragments,
674 bytes_received, packet_header->m_guid,
675 packet_header->m_fragment, packet_header->m_total_fragments);
676
677 if(classification)
678 decode_classification(classification, packet_header->m_guid);
679
680 break;
681 }
682
683
684 case protocol::payload::KEEP_ALIVE:
685 {
686 break;
687 }
688
689 default:
690 std::cerr << "[EE] lib3dv: unknown payload type: " << (int)packet_header->m_payload_type << std::endl;
691 }
692 }
693
694 m_data_socket.async_receive_from(boost::asio::buffer(&m_data_payload, MAX_UDP_PAYLOAD_SIZE),
695 m_data_sender_endpoint,
696 boost::bind(&device_impl::on_data_received, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
697 }
698
699 void device_impl::decode_terrain(boost::shared_ptr<raw_terrain_type> terrain, uint32_t guid)
700 {
701 boost::shared_ptr<lib3dv::terrain> output_terrain = boost::shared_ptr<lib3dv::terrain>(new lib3dv::terrain());
702 output_terrain->m_data.reserve(terrain->m_buffer.size() / 2);
703
704 float x_min = terrain->m_header.m_x_min / 100.0f;
705 float y_min = terrain->m_header.m_y_min / 100.0f;
706
707 float x_cell_size = terrain->m_header.m_x_cell_size / 100.0f;
708 float y_cell_size = terrain->m_header.m_y_cell_size / 100.0f;
709
710 uint16_t x_cell_num = terrain->m_header.m_x_cell_num;
711 uint16_t y_cell_num = terrain->m_header.m_y_cell_num;
712
713 const int16_t* data = reinterpret_cast<const int16_t*>(terrain->m_buffer.data());
714 std::vector<lib3dv::point3>& output_terrain_data = output_terrain->m_data;
715
716 lib3dv::point3 p;
717 unsigned int t = 0;
718
719 for(unsigned int x = 0; x < x_cell_num; ++x)
720 {
721 p.m_x = x_min + x * x_cell_size;
722
723 for(unsigned int y = 0; y < y_cell_num; ++y)
724 {
725 if(data[t] != static_cast<int16_t>(0x8000))
726 {
727 p.m_y = y_min + y * y_cell_size;
728 p.m_z = data[t] / 100.0f;
729 output_terrain_data.push_back(p);
730 }
731
732 ++t;
733 }
734 }
735
736 m_terrain_signal(output_terrain, guid);
737 m_deprecated_terrain_signal(output_terrain, guid);
738 }
739
740 void device_impl::decode_obstacles(boost::shared_ptr<raw_obstacles_type> obstacles, uint32_t guid)
741 {
742 boost::shared_ptr<std::vector<lib3dv::obstacle> > output_obstacles = boost::shared_ptr<std::vector<lib3dv::obstacle> >(new std::vector<lib3dv::obstacle>(obstacles->m_header.m_obstacles_num));
743 uint8_t* raw_obstacle = obstacles->m_buffer.data();
744
745 for(std::vector<lib3dv::obstacle>::iterator oo = output_obstacles->begin(); oo != output_obstacles->end(); ++oo)
746 {
747 const protocol::obstacle_info_header* obstacle_header = reinterpret_cast<const protocol::obstacle_info_header*>(raw_obstacle);
748 const protocol::stixel* obstacle_data = reinterpret_cast<const protocol::stixel*>(raw_obstacle + sizeof(protocol::obstacle_info_header));
749
750 oo->m_guid = obstacle_header->m_guid;
751 oo->m_data.resize(obstacle_header->m_stixel_num);
752
753 for(unsigned int s = 0; s < obstacle_header->m_stixel_num; ++s)
754 {
755 lib3dv::stixel& stixel = oo->m_data[s];
756
757 stixel.m_dx = obstacle_data[s].m_dx / 100.0f;
758 stixel.m_dy = obstacle_data[s].m_dy / 100.0f;
759 stixel.m_x = obstacle_data[s].m_x / 100.0f;
760 stixel.m_y = obstacle_data[s].m_y / 100.0f;
761 stixel.m_z = obstacle_data[s].m_z / 100.0f;
762 stixel.m_height = obstacle_data[s].m_height / 100.0f;
763 }
764
765 raw_obstacle += (sizeof(protocol::obstacle_info_header) + obstacle_header->m_stixel_num * sizeof(protocol::stixel));
766 }
767
768 m_obstacles_signal(output_obstacles, guid);
769 m_deprecated_obstacles_signal(output_obstacles, guid);
770 }
771
772 void device_impl::decode_motion(boost::shared_ptr<raw_motion_type> motion, uint32_t guid)
773 {
774 boost::shared_ptr<lib3dv::motion> output_motion = boost::shared_ptr<lib3dv::motion>(new lib3dv::motion());
775 output_motion->m_poses.resize(motion->m_header.m_poses_num);
776
777 std::vector<lib3dv::pose>& output_poses = output_motion->m_poses;
778 uint8_t* raw_pose = motion->m_buffer.data();
779
780 for(std::vector<lib3dv::pose>::iterator pose = output_poses.begin(); pose != output_poses.end(); ++pose)
781 {
782 const protocol::pose_info_header* pose_header = reinterpret_cast<const protocol::pose_info_header*>(raw_pose);
783 raw_pose += sizeof(protocol::pose_info_header);
784
785 const double* pose_data = reinterpret_cast<const double*>(raw_pose);
786
787 pose->m_timestamp = boost::posix_time::ptime(boost::gregorian::date(1970,1,1)) +
788 boost::posix_time::seconds(pose_header->m_timestamp / 1000000) + // this serves as a workaround to an integer overflow in boost versions <= 1.51
789 boost::posix_time::microseconds(pose_header->m_timestamp % 1000000);
790
791 if ( pose_header->m_type == 0 )
792 pose->m_type = lib3dv::pose::type::CURRENT_POSE;
793 else if ( pose_header->m_type == 1 )
794 pose->m_type = lib3dv::pose::type::RELATIVE_POSE;
795
796
797 pose->m_data.resize(pose_header->m_pose_num);
798
799 for (uint8_t i = 0; i < pose->m_data.size(); i++)
800 pose->m_data[i] = pose_data[i];
801
802 raw_pose += (pose_header->m_pose_num * sizeof(double));
803 }
804
805 m_motion_signal(output_motion, guid);
806 m_deprecated_motion_signal(output_motion, guid);
807 }
808
809 void device_impl::decode_classification(boost::shared_ptr<raw_candidate_type> candidates, uint32_t guid)
810 {
811 boost::shared_ptr<lib3dv::classification> output_classification = boost::shared_ptr<lib3dv::classification>(new lib3dv::classification());
812 output_classification->m_candidates.resize(candidates->m_header.m_candidates_num);
813 uint8_t* raw_candidate = candidates->m_buffer.data();
814 output_classification->m_timestamp = boost::posix_time::ptime(boost::gregorian::date(1970,1,1)) +
815 boost::posix_time::seconds(candidates->m_header.m_timestamp / 1000000) + // this serves as a workaround to an integer overflow in boost versions <= 1.51
816 boost::posix_time::microseconds(candidates->m_header.m_timestamp % 1000000);
817 for(std::vector<lib3dv::candidate>::iterator oo = output_classification->m_candidates.begin(); oo != output_classification->m_candidates.end(); ++oo)
818 {
819 //const protocol::classifier_info_header * classifier_header = reinterpret_cast<const protocol::classifier_info_header*>(raw_candidate);
820 //const protocol::classifier_data* candidate_data = reinterpret_cast<const protocol::classifier_data*>(raw_candidate + sizeof(protocol::classifier_info_header));
821 const protocol::classifier_data* candidate_data = reinterpret_cast<const protocol::classifier_data*>(raw_candidate);
822 //oo->m_category = lib3dv::category_type::types (candidate_data->m_category);
823 //oo->m_confidence = candidate_data->m_confidence;
824 //oo->m_wp_lb.m_x = candidate_data->m_wp_lb_x;
825 //oo->m_wp_lb.m_y = candidate_data->m_wp_lb_y;
826 //oo->m_wp_lb.m_z = candidate_data->m_wp_lb_z;
827 oo->m_x0 = candidate_data->m_x0;
828 oo->m_y0 = candidate_data->m_y0;
829 oo->m_x1 = candidate_data->m_x1;
830 oo->m_y1 = candidate_data->m_y1;
831 oo->m_confidence = candidate_data->m_confidence/100.0;
832 oo->m_category = lib3dv::category_type::types (candidate_data->m_category);
833 oo->m_guid = candidate_data->m_guid;
834
835 //std::cout << oo->m_guid <<"("<<"\t"<< oo->m_x0<<","<<oo->m_y0<<","<<oo->m_x1<<","<<oo->m_y1 << ") "<<oo->m_confidence<< " "<< oo->m_category<<std::endl;
836 raw_candidate += sizeof(protocol::classifier_data);
837 }
838 boost::shared_ptr<lib3dv::classification> output_motion = boost::shared_ptr<lib3dv::classification>(new lib3dv::classification());
839 m_classification_signal(output_classification, guid);
840 m_deprecated_classification_signal(output_classification, guid);
841 }
842
843
844 void device_impl::save_properties(error& error)
845 {
846 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
847
848 command->m_type = detail::command::SAVE_PROPERTIES;
849
850 send_command(command, error);
851
852 m_commands_socket_io_service.run();
853 m_commands_socket_io_service.reset();
854 }
855
856 void device_impl::reset_properties(error& error)
857 {
858 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
859
860 command->m_type = detail::command::RESET_PROPERTIES;
861
862 send_command(command, error);
863
864 m_commands_socket_io_service.run();
865 m_commands_socket_io_service.reset();
866 }
867
868 std::vector<device::property> device_impl::enumerate_properties(lib3dv::error& error)
869 {
870 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
871 std::vector<device::property> properties;
872
873 command->m_type = detail::command::ENUMERATE_PROPERTIES;
874 command->m_result = boost::ref(properties);
875
876 send_command(command, error);
877
878 m_commands_socket_io_service.run();
879 m_commands_socket_io_service.reset();
880
881 return properties;
882 }
883
884 boost::any device_impl::get_property_value(uint16_t address, lib3dv::error& error)
885 {
886 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
887
888 command->m_type = detail::command::GET_PROPERTY;
889 command->m_data.resize(sizeof(uint16_t));
890 *(reinterpret_cast<uint16_t*>(command->m_data.data())) = address;
891
892 send_command(command, error);
893
894 m_commands_socket_io_service.run();
895 m_commands_socket_io_service.reset();
896
897 return command->m_result;
898 }
899
900 template<typename T>
901 size_t set_property_value_helper(uint8_t type, size_t size, protocol::param_info& info, boost::shared_ptr<detail::command> command, const boost::any& value)
902 {
903 info.m_value_type = type;
904 size += sizeof(T);
905 command->m_data.resize(size);
906
907 *(reinterpret_cast<T*>(command->m_data.data() + sizeof(protocol::param_info))) = boost::any_cast<T>(value);
908
909 return size;
910 }
911
912 template<>
913 size_t set_property_value_helper<bool>(uint8_t type, size_t size, protocol::param_info& info, boost::shared_ptr<detail::command> command, const boost::any& value)
914 {
915 info.m_value_type = type;
916 size += sizeof(uint8_t);
917 command->m_data.resize(size);
918
919 *(reinterpret_cast<uint8_t*>(command->m_data.data() + sizeof(protocol::param_info))) = static_cast<uint8_t>(boost::any_cast<bool>(value));
920
921 return size;
922 }
923
924 template<>
925 size_t set_property_value_helper<std::string>(uint8_t type, size_t size, protocol::param_info& info, boost::shared_ptr<detail::command> command, const boost::any& value)
926 {
927 const std::string& str_value = boost::any_cast<std::string>(value);
928 size_t str_size = std::min(str_value.size(), (size_t)255);
929
930 info.m_value_type = type;
931 size += (sizeof(uint8_t) + str_size);
932 command->m_data.resize(size);
933
934 uint8_t* data = command->m_data.data() + sizeof(protocol::param_info);
935
936 *(reinterpret_cast<uint8_t*>(data)) = str_size;
937 data += sizeof(uint8_t);
938
939 std::copy(str_value.data(), str_value.data() + str_size, data);
940 data += str_size;
941
942 return size;
943 }
944
945 void device_impl::set_property_value(uint16_t address, const boost::any& value, lib3dv::error& error)
946 {
947 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
948
949 size_t size = sizeof(protocol::param_info);
950
951 protocol::param_info info;
952
953 info.m_value_address = address;
954 info.m_readonly = 0;
955 info.m_category = 0;
956 info.m_value_description_size = 0;
957
958 const std::type_info& type = value.type();
959
960 if(type == typeid(bool)) size = set_property_value_helper<bool>(command::BOOL, size, info, command, value);
961 else if(type == typeid(int64_t)) size = set_property_value_helper<int64_t>(command::INT64, size, info, command, value);
962 else if(type == typeid(int32_t)) size = set_property_value_helper<int32_t>(command::INT32, size, info, command, value);
963 else if(type == typeid(int16_t)) size = set_property_value_helper<int16_t>(command::INT16, size, info, command, value);
964 else if(type == typeid(int8_t)) size = set_property_value_helper<int8_t>(command::INT8, size, info, command, value);
965 else if(type == typeid(uint64_t)) size = set_property_value_helper<uint64_t>(command::UINT64, size, info, command, value);
966 else if(type == typeid(uint32_t)) size = set_property_value_helper<uint32_t>(command::UINT32, size, info, command, value);
967 else if(type == typeid(uint16_t)) size = set_property_value_helper<uint16_t>(command::UINT16, size, info, command, value);
968 else if(type == typeid(uint8_t)) size = set_property_value_helper<uint8_t>(command::UINT8, size, info, command, value);
969 else if(type == typeid(float)) size = set_property_value_helper<float>(command::FLOAT32, size, info, command, value);
970 else if(type == typeid(double)) size = set_property_value_helper<double>(command::FLOAT64, size, info, command, value);
971 else if(type == typeid(std::string)) size = set_property_value_helper<std::string>(command::STRING, size, info, command, value);
972
973 command->m_type = detail::command::SET_PROPERTY;
974 *(reinterpret_cast<protocol::param_info*>(command->m_data.data())) = info;
975
976 send_command(command, error);
977
978 m_commands_socket_io_service.run();
979 m_commands_socket_io_service.reset();
980 }
981
982 void device_impl::start_transmission(lib3dv::error& error)
983 {
984 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
985
986 command->m_type = detail::command::START_TRANSMISSION;
987 command->m_data.resize(sizeof(uint16_t));
988 *(reinterpret_cast<uint16_t*>(command->m_data.data())) = m_data_socket.local_endpoint().port();
989
990 send_command(command, error);
991
992 m_data_deadline.expires_from_now(m_timeout);
993
994 on_deadline(m_data_deadline, m_data_socket, m_log_level, true, m_data_socket_io_service, boost::bind(&device_impl::on_timeout, this, boost::ref(error)), boost::system::error_code());
995
996 m_commands_socket_io_service.run();
997 m_commands_socket_io_service.reset();
998
999 if(error != lib3dv::error::NONE)
1000 m_status = device::status::OFFLINE;
1001 else
1002 m_status = device::status::TRANSMITTING;
1003 }
1004
1005 void device_impl::stop_transmission(lib3dv::error& error)
1006 {
1007 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
1008
1009 command->m_type = detail::command::STOP_TRANSMISSION;
1010
1011 send_command(command, error);
1012
1013 m_commands_socket_io_service.run();
1014 m_commands_socket_io_service.reset();
1015
1016 if(error != lib3dv::error::NONE)
1017 m_status = device::status::OFFLINE;
1018 else
1019 m_status = device::status::ONLINE;
1020 }
1021
1022 void device_impl::poweroff(lib3dv::error& error)
1023 {
1024 m_status = device::status::OFFLINE;
1025
1026 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
1027
1028 command->m_type = detail::command::POWEROFF;
1029
1030 send_command(command, error);
1031
1032 m_commands_socket_io_service.run();
1033 m_commands_socket_io_service.reset();
1034 }
1035
1036 void device_impl::reboot(lib3dv::error& error)
1037 {
1038 m_status = device::status::OFFLINE;
1039
1040 boost::shared_ptr<detail::command> command = boost::shared_ptr<detail::command>(new detail::command());
1041
1042 command->m_type = detail::command::REBOOT;
1043
1044 send_command(command, error);
1045
1046 m_commands_socket_io_service.run();
1047 m_commands_socket_io_service.reset();
1048 }
1049
1050 void device_impl::send_command(boost::shared_ptr<lib3dv::detail::command> command, lib3dv::error& error)
1051 {
1052 error = lib3dv::error::NONE;
1053
1054 boost::shared_ptr<protocol::packet_header> packet_header = boost::shared_ptr<protocol::packet_header>(new protocol::packet_header());
1055 boost::shared_ptr<protocol::command_header> raw_command = boost::shared_ptr<protocol::command_header>(new protocol::command_header());
1056 boost::shared_ptr<std::vector<uint8_t> > raw_command_data = boost::shared_ptr<std::vector<uint8_t> >(new std::vector<uint8_t>());
1057
1058 packet_header->m_magic[0] = '#';
1059 packet_header->m_magic[1] = '3';
1060 packet_header->m_magic[2] = 'D';
1061 packet_header->m_magic[3] = 'V';
1062 packet_header->m_protocol_version = 0;
1063 packet_header->m_fragment = 0;
1064 packet_header->m_total_fragments = 1;
1065 packet_header->m_payload_type = protocol::payload::COMMAND;
1066
1067 raw_command->m_type = command->m_type;
1068 raw_command->m_size = command->m_data.size();
1069
1070 raw_command_data->swap(command->m_data);
1071
1072 boost::array<boost::asio::const_buffer, 3> buffers;
1073
1074 buffers[0] = boost::asio::buffer(packet_header.get(), sizeof(protocol::packet_header));
1075 buffers[1] = boost::asio::buffer(raw_command.get(), sizeof(protocol::command_header));
1076 buffers[2] = boost::asio::buffer(raw_command_data->data(), raw_command_data->size());
1077
1078 m_commands_socket.async_send_to(buffers,
1079 boost::asio::ip::udp::endpoint(m_remote_address, m_remote_commands_port),
1080 boost::bind(&device_impl::on_command_sent, this,
1081 boost::asio::placeholders::error,
1082 boost::asio::placeholders::bytes_transferred,
1083 boost::ref(command->m_result), boost::ref(error)));
1084
1085 m_commands_deadline.expires_from_now(m_timeout);
1086 on_deadline(m_commands_deadline, m_commands_socket, m_log_level, false, m_commands_socket_io_service, boost::bind(&device_impl::on_timeout, this, boost::ref(error)), boost::system::error_code());
1087 }
1088
1089 void device_impl::on_timeout(lib3dv::error& device_error)
1090 {
1091 // we don't have to close the connections if the timeout has been triggered by a stop_transmission()
1092 if(m_status == device::status::TRANSMITTING)
1093 {
1094 boost::system::error_code error;
1095
1096 if(m_commands_socket.is_open())
1097 m_commands_socket.close(error);
1098
1099 m_commands_socket_io_service.stop();
1100
1101 if(m_data_socket.is_open())
1102 m_data_socket.close(error);
1103
1104 m_data_socket_io_service.stop();
1105
1106 m_status = device::status::OFFLINE;
1107
1108 device_error = error ? lib3dv::error::NETWORK_FAILURE : lib3dv::error::NETWORK_TIMEOUT;
1109
1110 m_timeout_signal();
1111 }
1112 }
1113
1114 void device_impl::on_command_sent(const boost::system::error_code& error, size_t bytes_sent, boost::any& result, lib3dv::error& device_error)
1115 {
1116 if(m_log_level > 1) std::cout << "[II] lib3dv: command sent " << error << " (" << bytes_sent << " bytes)" << std::endl;
1117
1118 m_commands_socket.async_receive_from(boost::asio::buffer(m_commands_payload), m_commands_sender_endpoint, boost::bind(&device_impl::on_command_received, this,
1119 boost::asio::placeholders::error,
1120 boost::asio::placeholders::bytes_transferred,
1121 boost::ref(result), boost::ref(device_error)));
1122 }
1123
1124 inline const uint8_t* decode_value(const uint8_t* value, command::values type, boost::any& result)
1125 {
1126 switch(type)
1127 {
1128 case command::BOOL : { result = static_cast<bool>(*reinterpret_cast<const uint8_t*>(value)); value += sizeof(uint8_t); break; }
1129
1130 case command::INT64 : { result = *reinterpret_cast<const int64_t*>(value); value += sizeof(int64_t); break; }
1131
1132 case command::INT32 : { result = *reinterpret_cast<const int32_t*>(value); value += sizeof(int32_t); break; }
1133
1134 case command::INT16 : { result = *reinterpret_cast<const int16_t*>(value); value += sizeof(int16_t); break; }
1135
1136 case command::INT8 : { result = *reinterpret_cast<const int8_t*>(value); value += sizeof(int8_t); break; }
1137
1138 case command::UINT64 : { result = *reinterpret_cast<const uint64_t*>(value); value += sizeof(uint64_t); break; }
1139
1140 case command::UINT32 : { result = *reinterpret_cast<const uint32_t*>(value); value += sizeof(uint32_t); break; }
1141
1142 case command::UINT16 : { result = *reinterpret_cast<const uint16_t*>(value); value += sizeof(uint16_t); break; }
1143
1144 case command::UINT8 : { result = *reinterpret_cast<const uint8_t*>(value); value += sizeof(uint8_t); break; }
1145
1146 case command::FLOAT32 : { result = *reinterpret_cast<const float*>(value); value += sizeof(float); break; }
1147
1148 case command::FLOAT64 : { result = *reinterpret_cast<const double*>(value); value += sizeof(double); break; }
1149
1150 case command::STRING :
1151 {
1152 size_t size = *value;
1153 value += sizeof(uint8_t);
1154
1155 result = std::string(value, value + size);
1156 value += size;
1157
1158 break;
1159 }
1160 }
1161
1162 return value;
1163 }
1164
1165 const uint8_t* device_impl::decode_property(device::property& property, const uint8_t* data)
1166 {
1167 const protocol::param_info* param_info = reinterpret_cast<const protocol::param_info*>(data);
1168
1169 data += sizeof(protocol::param_info);
1170
1171 property.m_address = param_info->m_value_address;
1172 property.m_readonly = param_info->m_readonly;
1173
1174 switch(param_info->m_category)
1175 {
1176 case(command::VALUE) :
1177 {
1178 property.m_category = device::property::VALUE;
1179 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_value);
1180
1181 break;
1182 }
1183
1184 case(command::RANGE) :
1185 {
1186 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_value);
1187
1188 property.m_category = device::property::RANGE;
1189 property.m_attributes.resize(3);
1190
1191 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_attributes[0].first);
1192 property.m_attributes[0].second = "min";
1193
1194 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_attributes[1].first);
1195 property.m_attributes[1].second = "max";
1196
1197 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_attributes[2].first);
1198 property.m_attributes[2].second = "step";
1199
1200 break;
1201 }
1202
1203 case(command::SELECTION) :
1204 {
1205 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_value);
1206
1207 property.m_category = device::property::SELECTION;
1208
1209 uint16_t num = *reinterpret_cast<const uint16_t*>(data);
1210 data += sizeof(uint16_t);
1211
1212 property.m_attributes.resize(num);
1213
1214 for(unsigned int a = 0; a < num; ++a)
1215 {
1216 data = decode_value(data, static_cast<command::values>(param_info->m_value_type), property.m_attributes[a].first);
1217
1218 size_t size = *data;
1219 data += sizeof(uint8_t);
1220
1221 property.m_attributes[a].second = std::string(data, data + size);
1222 data += size;
1223 }
1224
1225 break;
1226 }
1227 }
1228
1229 std::string description((const char*)data, param_info->m_value_description_size);
1230 data += param_info->m_value_description_size;
1231
1232 property.m_name = description;
1233
1234 return data;
1235 }
1236
1237 void device_impl::on_command_received(const boost::system::error_code& error, size_t bytes_received, boost::any& result, lib3dv::error& device_error)
1238 {
1239 if(m_log_level > 1) std::cout << std::endl << "[II] lib3dv: " << " " << bytes_received << " data bytes read" << std::endl;
1240
1241 m_commands_deadline.cancel();
1242
1243 if(bytes_received >= sizeof(protocol::packet_header) && !memcmp(m_commands_payload.c_array(), "#3DV", 4))
1244 {
1245 const uint8_t* data = m_commands_payload.c_array();
1246 const protocol::packet_header* packet_header = reinterpret_cast<const protocol::packet_header*>(data);
1247 data += sizeof(protocol::packet_header);
1248
1249 if(m_log_level > 1)
1250 {
1251 std::cout << "\t" << "protocol version = " << (int)(packet_header->m_protocol_version) << std::endl;
1252 std::cout << "\t" << "guid = " << packet_header->m_guid << std::endl;
1253 std::cout << "\t" << "fragment = " << packet_header->m_fragment << " / " << packet_header->m_total_fragments << std::endl;
1254 std::cout << "\t" << "payload_type = " << (int)packet_header->m_payload_type << std::endl;
1255 std::cout << "\t" << "error = " << (int)packet_header->m_error << std::endl;
1256 }
1257
1258 if(!packet_header->m_error)
1259 {
1260 device_error.m_type = static_cast<lib3dv::error::types>(packet_header->m_error);
1261
1262 switch(packet_header->m_payload_type)
1263 {
1264 case protocol::payload::COMMAND :
1265 {
1266 const protocol::command_header* command_header = reinterpret_cast<const protocol::command_header*>(data);
1267 data += sizeof(protocol::command_header);
1268
1269 switch(command_header->m_type)
1270 {
1271 case(command::ENUMERATE_PROPERTIES) :
1272 {
1273 std::vector<device::property>& properties = boost::any_cast<boost::reference_wrapper<std::vector<device::property> > >(result);
1274
1275 const protocol::params_info_header* params_info_header = reinterpret_cast<const protocol::params_info_header*>(data);
1276 data += sizeof(protocol::params_info_header);
1277
1278 for(unsigned int i = 0; i < params_info_header->m_params_num; ++i)
1279 {
1280 device::property property;
1281
1282 data = decode_property(property, data);
1283
1284 properties.push_back(property);
1285 }
1286
1287 break;
1288 }
1289
1290 case(command::GET_PROPERTY) :
1291 {
1292 device::property property;
1293
1294 decode_property(property, data);
1295
1296 result = property.m_value;
1297
1298 break;
1299 }
1300 }
1301
1302 break;
1303 }
1304
1305 default:
1306 std::cerr << "[EE] lib3dv: unknown payload type: " << (int)packet_header->m_payload_type << std::endl;
1307 }
1308 } else
1309 device_error.m_type = static_cast<lib3dv::error::types>(packet_header->m_error);
1310 }
1311 }
1312
1313 std::ostream& operator<< (std::ostream& output, const device_impl& device)
1314 {
1315 output << "guid: " << device.m_guid
1316 << " address: " << device.m_remote_address
1317 << " version: " << (int)device.version().m_protocol
1318 << " / " << (int)device.version().m_framework[device::version_info::MAJOR]
1319 << "." << (int)device.version().m_framework[device::version_info::MINOR]
1320 << "." << (int)device.version().m_framework[device::version_info::STEP]
1321 << " / " << (int)device.version().m_application[device::version_info::MAJOR]
1322 << "." << (int)device.version().m_application[device::version_info::MINOR]
1323 << "." << (int)device.version().m_application[device::version_info::STEP]
1324 << " capabilities:" << (device.capabilities()[device::capability::DEPTH_MAPPING] ? " DEPTH_MAPPING" : "")
1325 << (device.capabilities()[device::capability::OBSTACLE_DETECTION] ? " OBSTACLE_DETECTION" : "")
1326 << (device.capabilities()[device::capability::TERRAIN_MAPPING] ? " TERRAIN_MAPPING" : "")
1327 << (device.capabilities()[device::capability::MOTION_ESTIMATION] ? " MOTION_ESTIMATION" : "")
1328 << (device.capabilities()[device::capability::CLASSIFICATION] ? " CLASSIFICATION" : "");
1329
1330 return output;
1331 }
1332 }
1333}
Note: See TracBrowser for help on using the repository browser.