diff options
author | Ashish Chaudhari <ashish@ettus.com> | 2015-03-30 16:56:01 -0700 |
---|---|---|
committer | Ashish Chaudhari <ashish@ettus.com> | 2015-03-30 16:56:01 -0700 |
commit | 700bf99bdc483fdcc9deb54abc29bd7f81e16089 (patch) | |
tree | 42d4f30e7a4abc9e47dcd01300a2f44ab1b91510 /host/lib/types | |
parent | 6a34824ad10eaa2d2b642b959f278f6c4e326d6d (diff) | |
parent | 61599b3eaadcc46ac8d24974176d7fd89778d06e (diff) | |
download | uhd-700bf99bdc483fdcc9deb54abc29bd7f81e16089.tar.gz uhd-700bf99bdc483fdcc9deb54abc29bd7f81e16089.tar.bz2 uhd-700bf99bdc483fdcc9deb54abc29bd7f81e16089.zip |
Merge branch 'master' into ashish/vivado
Diffstat (limited to 'host/lib/types')
-rw-r--r-- | host/lib/types/CMakeLists.txt | 3 | ||||
-rw-r--r-- | host/lib/types/byte_vector.cpp | 55 |
2 files changed, 57 insertions, 1 deletions
diff --git a/host/lib/types/CMakeLists.txt b/host/lib/types/CMakeLists.txt index 821754386..5e97628f0 100644 --- a/host/lib/types/CMakeLists.txt +++ b/host/lib/types/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright 2011-2013 Ettus Research LLC +# Copyright 2011-2013,2015 Ettus Research LLC # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -92,4 +92,5 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/types.cpp ${CMAKE_CURRENT_SOURCE_DIR}/wb_iface.cpp ${CMAKE_CURRENT_SOURCE_DIR}/filters.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/byte_vector.cpp ) diff --git a/host/lib/types/byte_vector.cpp b/host/lib/types/byte_vector.cpp new file mode 100644 index 000000000..36b311faf --- /dev/null +++ b/host/lib/types/byte_vector.cpp @@ -0,0 +1,55 @@ +// +// Copyright 2015 Ettus Research LLC +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. +// + +#include <boost/foreach.hpp> +#include <boost/lexical_cast.hpp> + +#include <uhd/types/byte_vector.hpp> + +namespace uhd{ + +std::string bytes_to_string(const byte_vector_t &bytes){ + std::string out; + BOOST_FOREACH(boost::uint8_t byte, bytes){ + if (byte < 32 or byte > 127) return out; + out += byte; + } + return out; +} + +std::string uint16_bytes_to_string(const byte_vector_t &bytes){ + const boost::uint16_t num = (boost::uint16_t(bytes.at(0)) << 0) | (boost::uint16_t(bytes.at(1)) << 8); + return (num == 0 or num == 0xffff)? "" : boost::lexical_cast<std::string>(num); +} + +byte_vector_t string_to_bytes(const std::string &str, size_t max_length){ + byte_vector_t bytes; + for (size_t i = 0; i < std::min(str.size(), max_length); i++){ + bytes.push_back(str[i]); + } + if (bytes.size() < max_length - 1) bytes.push_back('\0'); + return bytes; +} + +byte_vector_t string_to_uint16_bytes(const std::string &num_str){ + const boost::uint16_t num = boost::lexical_cast<boost::uint16_t>(num_str); + const byte_vector_t lsb_msb = boost::assign::list_of + (boost::uint8_t(num >> 0))(boost::uint8_t(num >> 8)); + return lsb_msb; +} + +} /* namespace uhd */ |