[136] | 1 | /* 3dv-client/device.cc
|
---|
| 2 | *
|
---|
| 3 | * Copyright (C) 2013 VisLab
|
---|
| 4 | *
|
---|
| 5 | * This file is part of 3dv-client; 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 | #include <lib3dv/device.h>
|
---|
| 20 |
|
---|
| 21 | #include <lib3dv/detail/device_impl.h>
|
---|
| 22 |
|
---|
| 23 | #include <iomanip>
|
---|
| 24 | #include <typeinfo>
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | namespace lib3dv
|
---|
| 28 | {
|
---|
| 29 | const unsigned short device::DEFAULT_PORT = 2048;
|
---|
| 30 | const boost::posix_time::time_duration device::DEFAULT_TIMEOUT = boost::posix_time::milliseconds(500);
|
---|
| 31 |
|
---|
| 32 | template<typename T> inline void print(std::ostream& output, const T& value) { output << value; }
|
---|
| 33 | template<> inline void print<uint8_t>(std::ostream& output, const uint8_t& value) { output << static_cast<uint32_t>(value); }
|
---|
| 34 | template<> inline void print<int8_t>(std::ostream& output, const int8_t& value) { output << static_cast<int32_t>(value); }
|
---|
| 35 |
|
---|
| 36 | template<typename T>
|
---|
| 37 | void print(std::ostream& output, const std::string& type, const device::property& property)
|
---|
| 38 | {
|
---|
| 39 | output << "0x" << std::hex << std::setfill('0') << std::setw(3) << property.m_address
|
---|
| 40 | << std::dec << " " << type << (property.m_readonly ? " r " : " rw ")
|
---|
| 41 | << property.m_name << " = ";
|
---|
| 42 |
|
---|
| 43 | print(output, boost::any_cast<T>(property.m_value));
|
---|
| 44 |
|
---|
| 45 | output << " ";
|
---|
| 46 |
|
---|
| 47 | for(unsigned int a = 0; a < property.m_attributes.size(); ++a)
|
---|
| 48 | {
|
---|
| 49 | output << " [";
|
---|
| 50 | print(output, boost::any_cast<T>(property.m_attributes[a].first));
|
---|
| 51 |
|
---|
| 52 | if(property.m_attributes[a].second.empty())
|
---|
| 53 | output << "]";
|
---|
| 54 | else
|
---|
| 55 | output << " -> " << property.m_attributes[a].second << "]";
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | std::ostream& operator<< (std::ostream& output, const device::property& property)
|
---|
| 60 | {
|
---|
| 61 | const std::type_info& type = property.m_value.type();
|
---|
| 62 |
|
---|
| 63 | if(type == typeid(bool)) print<bool>(output, "bool ", property);
|
---|
| 64 | else if(type == typeid(int64_t)) print<int64_t>(output, "int64 ", property);
|
---|
| 65 | else if(type == typeid(int32_t)) print<int32_t>(output, "int32 ", property);
|
---|
| 66 | else if(type == typeid(int16_t)) print<int16_t>(output, "int16 ", property);
|
---|
| 67 | else if(type == typeid(int8_t)) print<int8_t>(output, "int8 ", property);
|
---|
| 68 | else if(type == typeid(uint64_t)) print<uint64_t>(output, "uint64 ", property);
|
---|
| 69 | else if(type == typeid(uint32_t)) print<uint32_t>(output, "uint32 ", property);
|
---|
| 70 | else if(type == typeid(uint16_t)) print<uint16_t>(output, "uint16 ", property);
|
---|
| 71 | else if(type == typeid(uint8_t)) print<uint8_t>(output, "uint8 ", property);
|
---|
| 72 | else if(type == typeid(float)) print<float>(output, "float32", property);
|
---|
| 73 | else if(type == typeid(double)) print<double>(output, "float64", property);
|
---|
| 74 | else if(type == typeid(std::string)) print<std::string>(output, "string ", property);
|
---|
| 75 |
|
---|
| 76 | return output;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | std::vector<device> device::enumerate(const boost::asio::ip::address_v4& local_address, uint8_t log_level, error& error, const boost::posix_time::time_duration& timeout, unsigned short remote_port) { return detail::device_impl::enumerate(local_address, remote_port, log_level, error, timeout); }
|
---|
| 80 |
|
---|
| 81 | bool device::valid() const { return m_impl->valid(); }
|
---|
| 82 |
|
---|
| 83 | device::status::types device::status() const { return m_impl->status(); }
|
---|
| 84 |
|
---|
| 85 | void device::log_level(uint8_t level) { m_impl->m_log_level = level; }
|
---|
| 86 |
|
---|
| 87 | uint8_t device::log_level() const { return m_impl->m_log_level; }
|
---|
| 88 |
|
---|
| 89 | void device::timeout(const boost::posix_time::time_duration& timeout) { m_impl->m_timeout = timeout; }
|
---|
| 90 |
|
---|
| 91 | boost::posix_time::time_duration device::timeout() const { return m_impl->m_timeout; }
|
---|
| 92 |
|
---|
| 93 | const boost::uuids::uuid& device::guid() const { return m_impl->guid(); }
|
---|
| 94 |
|
---|
| 95 | const std::bitset<device::capability::NUM>& device::capabilities() const { return m_impl->capabilities(); }
|
---|
| 96 |
|
---|
| 97 | const device::version_info& device::version() const { return m_impl->version(); }
|
---|
| 98 |
|
---|
| 99 | void device::start_transmission(error& error) { if(m_impl->valid()) m_impl->start_transmission(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 100 |
|
---|
| 101 | void device::stop_transmission(error& error) { if(m_impl->valid()) m_impl->stop_transmission(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 102 |
|
---|
| 103 | void device::save_properties(error& error) { if(m_impl->valid()) m_impl->save_properties(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 104 |
|
---|
| 105 | void device::reset_properties(error& error) { if(m_impl->valid()) m_impl->reset_properties(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 106 |
|
---|
| 107 | void device::poweroff(error& error) { if(m_impl->valid()) m_impl->poweroff(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 108 |
|
---|
| 109 | void device::reboot(error& error) { if(m_impl->valid()) m_impl->reboot(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 110 |
|
---|
| 111 | std::vector<device::property> device::enumerate_properties(error& error) { if(m_impl->valid()) return m_impl->enumerate_properties(error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 112 |
|
---|
| 113 | boost::any device::get_property_value(uint16_t address, error& error) { if(m_impl->valid()) return m_impl->get_property_value(address, error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 114 |
|
---|
| 115 | void device::set_property_value(uint16_t address, const boost::any& value, error& error) { if(m_impl->valid()) m_impl->set_property_value(address, value, error); else error = lib3dv::error::NETWORK_FAILURE; }
|
---|
| 116 |
|
---|
| 117 | uint64_t device::connect_image_callback(const boost::function<void (boost::shared_ptr<const image>, uint32_t)>& function) { return m_impl->connect_image_callback(function); }
|
---|
| 118 |
|
---|
| 119 | uint64_t device::connect_terrain_callback(const boost::function<void (boost::shared_ptr<const terrain>, uint32_t)>& function) { return m_impl->connect_terrain_callback(function); }
|
---|
| 120 |
|
---|
| 121 | uint64_t device::connect_obstacles_callback(const boost::function<void (boost::shared_ptr<const std::vector<obstacle> >, uint32_t)>& function) { return m_impl->connect_obstacles_callback(function); }
|
---|
| 122 |
|
---|
| 123 | uint64_t device::connect_motion_callback(const boost::function<void (boost::shared_ptr<const motion>, uint32_t)>& function) { return m_impl->connect_motion_callback(function); }
|
---|
| 124 |
|
---|
| 125 | uint64_t device::connect_classification_callback(const boost::function<void (boost::shared_ptr<const lib3dv::classification>, uint32_t)>& function) { return m_impl->connect_classification_callback(function); }
|
---|
| 126 |
|
---|
| 127 | LIB3DV_DEPRECATED uint64_t device::connect_image_callback(const boost::function<void (boost::shared_ptr<image>, uint32_t)>& function) { return m_impl->connect_image_callback(function); }
|
---|
| 128 |
|
---|
| 129 | LIB3DV_DEPRECATED uint64_t device::connect_terrain_callback(const boost::function<void (boost::shared_ptr<terrain>, uint32_t)>& function) { return m_impl->connect_terrain_callback(function); }
|
---|
| 130 |
|
---|
| 131 | LIB3DV_DEPRECATED uint64_t device::connect_obstacles_callback(const boost::function<void (boost::shared_ptr<std::vector<obstacle> >, uint32_t)>& function) { return m_impl->connect_obstacles_callback(function); }
|
---|
| 132 |
|
---|
| 133 | LIB3DV_DEPRECATED uint64_t device::connect_motion_callback(const boost::function<void (boost::shared_ptr<motion>, uint32_t)>& function) { return m_impl->connect_motion_callback(function); }
|
---|
| 134 |
|
---|
| 135 | LIB3DV_DEPRECATED uint64_t device::connect_classification_callback(const boost::function<void (boost::shared_ptr<lib3dv::classification>, uint32_t)>& function) { return m_impl->connect_classification_callback(function); }
|
---|
| 136 |
|
---|
| 137 | uint64_t device::connect_timeout_callback(const boost::function<void (void)>& function) { return m_impl->connect_timeout_callback(function); }
|
---|
| 138 |
|
---|
| 139 | void device::disconnect_callback(uint64_t id) { m_impl->disconnect_callback(id); }
|
---|
| 140 |
|
---|
| 141 | std::ostream& operator<< (std::ostream& output, const device& device)
|
---|
| 142 | {
|
---|
| 143 | return output << *(device.m_impl);
|
---|
| 144 | }
|
---|
| 145 | }
|
---|