diff options
| -rw-r--r-- | host/include/uhd/types/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | host/include/uhd/types/byte_vector.hpp | 48 | ||||
| -rw-r--r-- | host/lib/types/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | host/lib/types/byte_vector.cpp | 55 | ||||
| -rw-r--r-- | host/lib/usrp/dboard_eeprom.cpp | 27 | ||||
| -rw-r--r-- | host/lib/usrp/mboard_eeprom.cpp | 29 | ||||
| -rw-r--r-- | host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp | 23 | 
7 files changed, 112 insertions, 74 deletions
| diff --git a/host/include/uhd/types/CMakeLists.txt b/host/include/uhd/types/CMakeLists.txt index b82c2b7f2..2a25df35f 100644 --- a/host/include/uhd/types/CMakeLists.txt +++ b/host/include/uhd/types/CMakeLists.txt @@ -17,6 +17,7 @@  UHD_INSTALL(FILES +    byte_vector.hpp      clock_config.hpp      device_addr.hpp      dict.ipp diff --git a/host/include/uhd/types/byte_vector.hpp b/host/include/uhd/types/byte_vector.hpp new file mode 100644 index 000000000..b7637fb5d --- /dev/null +++ b/host/include/uhd/types/byte_vector.hpp @@ -0,0 +1,48 @@ +// +// 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/>. +// + +#ifndef INCLUDED_UHD_TYPES_BYTE_VECTOR_HPP +#define INCLUDED_UHD_TYPES_BYTE_VECTOR_HPP + +#include <algorithm> +#include <string> +#include <vector> + +#include <boost/assign.hpp> +#include <boost/cstdint.hpp> + +#include <uhd/config.hpp> + +namespace uhd{ + +    //! Byte vector used for I2C data passing and EEPROM parsing. +    typedef std::vector<boost::uint8_t> byte_vector_t; + +    template<typename RangeSrc, typename RangeDst> UHD_INLINE +    void byte_copy(const RangeSrc &src, RangeDst &dst){ +        std::copy(boost::begin(src), boost::end(src), boost::begin(dst)); +    } + +    //! Create a string from a byte vector, terminate when invalid ASCII encountered +    UHD_API std::string bytes_to_string(const byte_vector_t &bytes); + +    //! Create a byte vector from a string, end at null terminator or max length +    UHD_API byte_vector_t string_to_bytes(const std::string &str, size_t max_length); + +} //namespace uhd + +#endif /* INCLUDED_UHD_TYPES_BYTE_VECTOR_HPP */ 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 */ diff --git a/host/lib/usrp/dboard_eeprom.cpp b/host/lib/usrp/dboard_eeprom.cpp index f2bee47a9..3b56ae19a 100644 --- a/host/lib/usrp/dboard_eeprom.cpp +++ b/host/lib/usrp/dboard_eeprom.cpp @@ -1,5 +1,5 @@  // -// Copyright 2010-2011 Ettus Research LLC +// Copyright 2010-2011,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 @@ -15,6 +15,7 @@  // along with this program.  If not, see <http://www.gnu.org/licenses/>.  // +#include <uhd/types/byte_vector.hpp>  #include <uhd/usrp/dboard_eeprom.hpp>  #include <uhd/exception.hpp>  #include <uhd/utils/log.hpp> @@ -27,30 +28,6 @@  using namespace uhd;  using namespace uhd::usrp; -/*********************************************************************** - * Utility functions - **********************************************************************/ - -//! create a string from a byte vector, return empty if invalid ascii -static const 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; -} - -//! create a byte vector from a string, null terminate unless max length -static const byte_vector_t string_to_bytes(const std::string &string, size_t max_length){ -    byte_vector_t bytes; -    for (size_t i = 0; i < std::min(string.size(), max_length); i++){ -        bytes.push_back(string[i]); -    } -    if (bytes.size() < max_length - 1) bytes.push_back('\0'); -    return bytes; -} -  ////////////////////////////////////////////////////////////////////////  // format of daughterboard EEPROM  // 00: 0xDB code for ``I'm a daughterboard'' diff --git a/host/lib/usrp/mboard_eeprom.cpp b/host/lib/usrp/mboard_eeprom.cpp index 68c084589..9c92fe252 100644 --- a/host/lib/usrp/mboard_eeprom.cpp +++ b/host/lib/usrp/mboard_eeprom.cpp @@ -1,5 +1,5 @@  // -// Copyright 2010-2013 Ettus Research LLC +// Copyright 2010-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 @@ -16,6 +16,7 @@  //  #include <uhd/usrp/mboard_eeprom.hpp> +#include <uhd/types/byte_vector.hpp>  #include <uhd/types/mac_addr.hpp>  #include <uhd/utils/byteswap.hpp>  #include <boost/asio/ip/address_v4.hpp> @@ -39,32 +40,6 @@ static const size_t NAME_MAX_LEN = 32 - SERIAL_LEN;   * Utility functions   **********************************************************************/ -//! A wrapper around std::copy that takes ranges instead of iterators. -template<typename RangeSrc, typename RangeDst> inline -void byte_copy(const RangeSrc &src, RangeDst &dst){ -    std::copy(boost::begin(src), boost::end(src), boost::begin(dst)); -} - -//! create a string from a byte vector, return empty if invalid ascii -static const 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; -} - -//! create a byte vector from a string, null terminate unless max length -static const byte_vector_t string_to_bytes(const std::string &string, size_t max_length){ -    byte_vector_t bytes; -    for (size_t i = 0; i < std::min(string.size(), max_length); i++){ -        bytes.push_back(string[i]); -    } -    if (bytes.size() < max_length - 1) bytes.push_back('\0'); -    return bytes; -} -  //! convert a string to a byte vector to write to eeprom  static 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); diff --git a/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp b/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp index 93c317191..49d1a0442 100644 --- a/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp +++ b/host/lib/usrp_clock/octoclock/octoclock_eeprom.cpp @@ -1,5 +1,5 @@  // -// Copyright 2014 Ettus Research LLC +// Copyright 2014-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 @@ -19,6 +19,7 @@  #include <uhd/usrp_clock/octoclock_eeprom.hpp>  #include <uhd/transport/udp_simple.hpp>  #include <uhd/usrp/mboard_eeprom.hpp> +#include <uhd/types/byte_vector.hpp>  #include <uhd/types/mac_addr.hpp>  #include <uhd/utils/byteswap.hpp>  #include <boost/assign/list_of.hpp> @@ -37,26 +38,6 @@ using namespace uhd::usrp_clock;  using namespace uhd::transport;  /*********************************************************************** - * Utility functions - **********************************************************************/ - -//! A wrapper around std::copy that takes ranges instead of iterators. -template<typename RangeSrc, typename RangeDst> inline -void byte_copy(const RangeSrc &src, RangeDst &dst){ -    std::copy(boost::begin(src), boost::end(src), boost::begin(dst)); -} - -//! create a string from a byte vector, return empty if invalid ascii -static const 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; -} - -/***********************************************************************   * Implementation   **********************************************************************/  void octoclock_eeprom_t::_load(){ | 
