diff options
-rw-r--r-- | host/lib/include/uhdlib/utils/serial_number.hpp | 29 | ||||
-rw-r--r-- | host/lib/usrp/mpmd/mpmd_find.cpp | 3 | ||||
-rw-r--r-- | host/lib/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/utils/serial_number.cpp | 23 | ||||
-rw-r--r-- | host/tests/CMakeLists.txt | 6 | ||||
-rw-r--r-- | host/tests/serial_number_test.cpp | 22 |
6 files changed, 83 insertions, 1 deletions
diff --git a/host/lib/include/uhdlib/utils/serial_number.hpp b/host/lib/include/uhdlib/utils/serial_number.hpp new file mode 100644 index 000000000..230f9657d --- /dev/null +++ b/host/lib/include/uhdlib/utils/serial_number.hpp @@ -0,0 +1,29 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#ifndef INCLUDED_UHDLIB_UTILS_SERIAL_NUMBER_HPP +# define INCLUDED_UHDLIB_UTILS_SERIAL_NUMBER_HPP + +#include <string> + +namespace uhd { namespace utils { + +/*! Convenience function to determine whether two serial numbers refer to the + * same device. Serial numbers are case-insensitive and ignore leading zeros, + * so e.g. the strings "0123abcd" and "123ABCD" are considered the same serial + * number. + * + * Serial numbers cannot be longer than 8 characters or have characters outside + * the range 0-9a-fA-F. + * + * \param serial_a The first serial number to compare + * \param serial_b The second serial number to compare + */ +bool serial_numbers_match(const std::string& serial_a, const std::string& serial_b); + +}} + +#endif /* INCLUDED_UHDLIB_UTILS_SERIAL_NUMBER_HPP */ diff --git a/host/lib/usrp/mpmd/mpmd_find.cpp b/host/lib/usrp/mpmd/mpmd_find.cpp index 501b250b0..c04615587 100644 --- a/host/lib/usrp/mpmd/mpmd_find.cpp +++ b/host/lib/usrp/mpmd/mpmd_find.cpp @@ -9,6 +9,7 @@ #include "mpmd_devices.hpp" #include "mpmd_impl.hpp" #include "mpmd_link_if_mgr.hpp" +#include <uhdlib/utils/serial_number.hpp> #include <uhd/transport/if_addrs.hpp> #include <uhd/transport/udp_simple.hpp> #include <uhd/types/device_addr.hpp> @@ -110,7 +111,7 @@ device_addrs_t mpmd_find_with_addr( } // filter the discovered device below by matching optional keys if ((not hint_.has_key("name") or hint_["name"] == new_addr["name"]) - and (not hint_.has_key("serial") or hint_["serial"] == new_addr["serial"]) + and (not hint_.has_key("serial") or uhd::serial_numbers_match(hint_["serial"], new_addr["serial"])) and (not hint_.has_key("type") or hint_["type"] == new_addr["type"] or hint_["type"] == MPM_CATCHALL_DEVICE_TYPE) and (not hint_.has_key("product") diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index 5d1d22e9b..aa06e2c45 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -214,6 +214,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/pathslib.cpp ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/prefs.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/serial_number.cpp ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp ${CMAKE_CURRENT_SOURCE_DIR}/system_time.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp diff --git a/host/lib/utils/serial_number.cpp b/host/lib/utils/serial_number.cpp new file mode 100644 index 000000000..7fd0863d6 --- /dev/null +++ b/host/lib/utils/serial_number.cpp @@ -0,0 +1,23 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhdlib/utils/serial_number.hpp> +#include <stdexcept> +#include <string> + +namespace uhd +{ + bool serial_numbers_match(const std::string& serial_a, const std::string& serial_b) + { + try { + const uint32_t a = std::stoi(serial_a, 0, 16); + const uint32_t b = std::stoi(serial_b, 0, 16); + return a == b; + } catch (std::out_of_range& e) { + return false; + } + } +} diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index f9f94e023..828729b79 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -269,6 +269,12 @@ UHD_ADD_NONAPI_TEST( ${CMAKE_SOURCE_DIR}/lib/transport/offload_io_service.cpp ) +UHD_ADD_NONAPI_TEST( + TARGET "serial_number_test.cpp" + EXTRA_SOURCES + ${CMAKE_SOURCE_DIR}/lib/utils/serial_number.cpp +) + ######################################################################## # demo of a loadable module ######################################################################## diff --git a/host/tests/serial_number_test.cpp b/host/tests/serial_number_test.cpp new file mode 100644 index 000000000..11c7f5372 --- /dev/null +++ b/host/tests/serial_number_test.cpp @@ -0,0 +1,22 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhdlib/utils/serial_number.hpp> +#include <boost/test/unit_test.hpp> + +BOOST_AUTO_TEST_CASE(test_serial_numbers_match) +{ + BOOST_CHECK_EQUAL(true, uhd::serial_numbers_match("abcd123", "abcd123")); + BOOST_CHECK_EQUAL(true, uhd::serial_numbers_match("0abcd123", "0abcd123")); + BOOST_CHECK_EQUAL(false, uhd::serial_numbers_match("0abcd123", "abcd1230")); + BOOST_CHECK_EQUAL(false, uhd::serial_numbers_match("abcd123", "abcd124")); + BOOST_CHECK_EQUAL(false, uhd::serial_numbers_match("abcd123", "321dcba")); + BOOST_CHECK_EQUAL(true, uhd::serial_numbers_match("abcd123", "0abcd123")); + BOOST_CHECK_EQUAL(true, uhd::serial_numbers_match("0abcd123", "abcd123")); + + // Out of range + BOOST_CHECK_EQUAL(false, uhd::serial_numbers_match("aaaaaaaaaaaaaa", "abcd123")); +} |