diff options
| -rw-r--r-- | host/include/uhd/utils/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | host/include/uhd/utils/cast.hpp | 43 | ||||
| -rw-r--r-- | host/lib/usrp/b100/b100_impl.cpp | 7 | ||||
| -rw-r--r-- | host/lib/usrp/b200/b200_impl.cpp | 11 | ||||
| -rw-r--r-- | host/lib/usrp/usrp1/usrp1_impl.cpp | 7 | ||||
| -rw-r--r-- | host/tests/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | host/tests/cast_test.cpp | 33 | 
7 files changed, 92 insertions, 11 deletions
| diff --git a/host/include/uhd/utils/CMakeLists.txt b/host/include/uhd/utils/CMakeLists.txt index c0991b3ce..e9633286f 100644 --- a/host/include/uhd/utils/CMakeLists.txt +++ b/host/include/uhd/utils/CMakeLists.txt @@ -22,6 +22,7 @@ UHD_INSTALL(FILES      atomic.hpp      byteswap.hpp      byteswap.ipp +    cast.hpp      csv.hpp      gain_group.hpp      images.hpp diff --git a/host/include/uhd/utils/cast.hpp b/host/include/uhd/utils/cast.hpp new file mode 100644 index 000000000..9db92c526 --- /dev/null +++ b/host/include/uhd/utils/cast.hpp @@ -0,0 +1,43 @@ +// +// Copyright 2014 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_UTILS_CAST_HPP +#define INCLUDED_UHD_UTILS_CAST_HPP + +#include <uhd/config.hpp> +#include <string> +#include <sstream> + +namespace uhd{ namespace cast{ +    //! Convert a hexadecimal string into a value. +    // +    // Example: +    //     boost::uint16_t x = hexstr_cast<boost::uint16_t>("0xDEADBEEF"); +    // Uses stringstream. +    template<typename T> inline T hexstr_cast(const std::string &in) +    { +        T x; +        std::stringstream ss; +        ss << std::hex << in; +        ss >> x; +        return x; +    } + +}} //namespace uhd::cast + +#endif /* INCLUDED_UHD_UTILS_CAST_HPP */ + diff --git a/host/lib/usrp/b100/b100_impl.cpp b/host/lib/usrp/b100/b100_impl.cpp index cb32a596c..baf2b6ae3 100644 --- a/host/lib/usrp/b100/b100_impl.cpp +++ b/host/lib/usrp/b100/b100_impl.cpp @@ -20,6 +20,7 @@  #include "b100_regs.hpp"  #include <uhd/transport/usb_control.hpp>  #include <uhd/utils/msg.hpp> +#include <uhd/utils/cast.hpp>  #include <uhd/exception.hpp>  #include <uhd/utils/static.hpp>  #include <uhd/utils/images.hpp> @@ -56,11 +57,11 @@ static device_addrs_t b100_find(const device_addr_t &hint)      //since an address and resource is intended for a different, non-USB, device.      if (hint.has_key("addr") || hint.has_key("resource")) return b100_addrs; -    unsigned int vid, pid; +    boost::uint16_t vid, pid;      if(hint.has_key("vid") && hint.has_key("pid") && hint.has_key("type") && hint["type"] == "b100") { -        sscanf(hint.get("vid").c_str(), "%x", &vid); -        sscanf(hint.get("pid").c_str(), "%x", &pid); +        vid = uhd::cast::hexstr_cast<boost::uint16_t>(hint.get("vid")); +        pid = uhd::cast::hexstr_cast<boost::uint16_t>(hint.get("pid"));      } else {          vid = B100_VENDOR_ID;          pid = B100_PRODUCT_ID; diff --git a/host/lib/usrp/b200/b200_impl.cpp b/host/lib/usrp/b200/b200_impl.cpp index 84310a9fe..98141dbaa 100644 --- a/host/lib/usrp/b200/b200_impl.cpp +++ b/host/lib/usrp/b200/b200_impl.cpp @@ -19,6 +19,7 @@  #include "b200_regs.hpp"  #include <uhd/transport/usb_control.hpp>  #include <uhd/utils/msg.hpp> +#include <uhd/utils/cast.hpp>  #include <uhd/exception.hpp>  #include <uhd/utils/static.hpp>  #include <uhd/utils/images.hpp> @@ -58,11 +59,11 @@ static device_addrs_t b200_find(const device_addr_t &hint)      //since an address and resource is intended for a different, non-USB, device.      if (hint.has_key("addr") || hint.has_key("resource")) return b200_addrs; -    unsigned int vid, pid; +    boost::uint16_t vid, pid;      if(hint.has_key("vid") && hint.has_key("pid") && hint.has_key("type") && hint["type"] == "b200") { -        sscanf(hint.get("vid").c_str(), "%x", &vid); -        sscanf(hint.get("pid").c_str(), "%x", &pid); +        vid = uhd::cast::hexstr_cast<boost::uint16_t>(hint.get("vid")); +        pid = uhd::cast::hexstr_cast<boost::uint16_t>(hint.get("pid"));      } else {          vid = B200_VENDOR_ID;          pid = B200_PRODUCT_ID; @@ -160,9 +161,9 @@ b200_impl::b200_impl(const device_addr_t &device_addr)      boost::uint16_t vid = B200_VENDOR_ID;      boost::uint16_t pid = B200_PRODUCT_ID;      if (device_addr.has_key("vid")) -            sscanf(device_addr.get("vid").c_str(), "%hx", &vid); +        vid = uhd::cast::hexstr_cast<boost::uint16_t>(device_addr.get("vid"));      if (device_addr.has_key("pid")) -            sscanf(device_addr.get("pid").c_str(), "%hx", &pid); +        pid = uhd::cast::hexstr_cast<boost::uint16_t>(device_addr.get("pid"));      std::vector<usb_device_handle::sptr> device_list =          usb_device_handle::get_device_list(vid, pid); diff --git a/host/lib/usrp/usrp1/usrp1_impl.cpp b/host/lib/usrp/usrp1/usrp1_impl.cpp index 3eaac9839..0ba2e1e4a 100644 --- a/host/lib/usrp/usrp1/usrp1_impl.cpp +++ b/host/lib/usrp/usrp1/usrp1_impl.cpp @@ -20,6 +20,7 @@  #include <uhd/utils/safe_call.hpp>  #include <uhd/transport/usb_control.hpp>  #include <uhd/utils/msg.hpp> +#include <uhd/utils/cast.hpp>  #include <uhd/exception.hpp>  #include <uhd/utils/static.hpp>  #include <uhd/utils/images.hpp> @@ -59,11 +60,11 @@ static device_addrs_t usrp1_find(const device_addr_t &hint)      //since an address and resource is intended for a different, non-USB, device.      if (hint.has_key("addr") || hint.has_key("resource")) return usrp1_addrs; -    unsigned int vid, pid; +    boost::uint16_t vid, pid;      if(hint.has_key("vid") && hint.has_key("pid") && hint.has_key("type") && hint["type"] == "usrp1") { -        sscanf(hint.get("vid").c_str(), "%x", &vid); -        sscanf(hint.get("pid").c_str(), "%x", &pid); +        vid = uhd::cast::hexstr_cast<boost::uint16_t>(hint.get("vid")); +        pid = uhd::cast::hexstr_cast<boost::uint16_t>(hint.get("pid"));      } else {          vid = USRP1_VENDOR_ID;          pid = USRP1_PRODUCT_ID; diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 2a40d0050..7c4815004 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -28,6 +28,7 @@ SET(test_sources      buffer_test.cpp      byteswap_test.cpp      convert_test.cpp +    cast_test.cpp      dict_test.cpp      error_test.cpp      gain_group_test.cpp diff --git a/host/tests/cast_test.cpp b/host/tests/cast_test.cpp new file mode 100644 index 000000000..6b8a4c527 --- /dev/null +++ b/host/tests/cast_test.cpp @@ -0,0 +1,33 @@ +// +// Copyright 2014 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 <iostream> +#include <boost/test/unit_test.hpp> +#include <boost/cstdint.hpp> +#include <uhd/utils/cast.hpp> + +BOOST_AUTO_TEST_CASE(test_mac_addr){ +    std::string in = "0x0100"; +    boost::uint16_t correct_result = 256; +    boost::uint16_t x = uhd::cast::hexstr_cast<boost::uint16_t>(in); +    //boost::uint16_t x = uhd::cast::hexstr_cast(in); +    std::cout +        << "Testing hex -> uint16_t conversion. " +        << in << " == " << std::hex << x << "?" << std::endl; +    BOOST_CHECK_EQUAL(x, correct_result); +} + | 
