diff options
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/features/CMakeLists.txt | 2 | ||||
-rw-r--r-- | host/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/features/CMakeLists.txt | 12 | ||||
-rw-r--r-- | host/lib/features/discoverable_feature_registry.cpp | 34 | ||||
-rw-r--r-- | host/lib/include/uhdlib/features/discoverable_feature_registry.hpp | 42 | ||||
-rw-r--r-- | host/tests/CMakeLists.txt | 7 | ||||
-rw-r--r-- | host/tests/discoverable_feature_test.cpp | 51 |
7 files changed, 113 insertions, 36 deletions
diff --git a/host/include/uhd/features/CMakeLists.txt b/host/include/uhd/features/CMakeLists.txt index efba46dc2..798844077 100644 --- a/host/include/uhd/features/CMakeLists.txt +++ b/host/include/uhd/features/CMakeLists.txt @@ -6,7 +6,7 @@ UHD_INSTALL(FILES discoverable_feature.hpp - discoverable_feature_getter.hpp + discoverable_feature_getter_iface.hpp DESTINATION ${INCLUDE_DIR}/uhd/features COMPONENT headers ) diff --git a/host/lib/CMakeLists.txt b/host/lib/CMakeLists.txt index c5046420a..c0b39faea 100644 --- a/host/lib/CMakeLists.txt +++ b/host/lib/CMakeLists.txt @@ -83,6 +83,7 @@ LIBUHD_REGISTER_COMPONENT("DPDK" ENABLE_DPDK ON "ENABLE_MPMD;DPDK_FOUND" OFF OFF ######################################################################## INCLUDE_SUBDIRECTORY(include) INCLUDE_SUBDIRECTORY(cal) +INCLUDE_SUBDIRECTORY(features) INCLUDE_SUBDIRECTORY(ic_reg_maps) INCLUDE_SUBDIRECTORY(types) INCLUDE_SUBDIRECTORY(convert) diff --git a/host/lib/features/CMakeLists.txt b/host/lib/features/CMakeLists.txt new file mode 100644 index 000000000..2cdd1dd56 --- /dev/null +++ b/host/lib/features/CMakeLists.txt @@ -0,0 +1,12 @@ +# +# Copyright 2020 Ettus Research, a National Instruments Brand +# +# SPDX-License-Identifier: GPL-3.0-or-later +# + +######################################################################## +# This file included, use CMake directory variables +######################################################################## +LIBUHD_APPEND_SOURCES( + ${CMAKE_CURRENT_SOURCE_DIR}/discoverable_feature_registry.cpp +) diff --git a/host/lib/features/discoverable_feature_registry.cpp b/host/lib/features/discoverable_feature_registry.cpp new file mode 100644 index 000000000..5bed6a06e --- /dev/null +++ b/host/lib/features/discoverable_feature_registry.cpp @@ -0,0 +1,34 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhd/exception.hpp> +#include <uhd/features/discoverable_feature.hpp> +#include <uhdlib/features/discoverable_feature_registry.hpp> +#include <vector> + +namespace uhd { namespace features { + +std::vector<std::string> +discoverable_feature_registry::enumerate_features() +{ + std::vector<std::string> features; + for (auto& entry : _features) { + features.push_back(entry.second->get_feature_name()); + } + return features; +} + +discoverable_feature::sptr discoverable_feature_registry::get_feature_ptr( + discoverable_feature::feature_id_t feature_id) +{ + auto it = _features.find(feature_id); + if (it == _features.end()) { + return discoverable_feature::sptr(); + } + return it->second; +} + +}} // namespace uhd::features diff --git a/host/lib/include/uhdlib/features/discoverable_feature_registry.hpp b/host/lib/include/uhdlib/features/discoverable_feature_registry.hpp new file mode 100644 index 000000000..f27f1a4f1 --- /dev/null +++ b/host/lib/include/uhdlib/features/discoverable_feature_registry.hpp @@ -0,0 +1,42 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#pragma once + +#include <uhd/exception.hpp> +#include <uhd/features/discoverable_feature.hpp> +#include <uhd/features/discoverable_feature_getter_iface.hpp> +#include <map> +#include <memory> +#include <vector> + +namespace uhd { namespace features { + +/*! Map-based registry to implement discoverable_feature_getter_iface + */ +class discoverable_feature_registry : public virtual discoverable_feature_getter_iface +{ +public: + virtual ~discoverable_feature_registry() = default; + + std::vector<std::string> enumerate_features() override; + + template <typename T> + void register_feature(std::shared_ptr<T> feature) + { + if (!_features.emplace(T::get_feature_id(), feature).second) { + UHD_ASSERT_THROW(false); + } + } + +private: + discoverable_feature::sptr get_feature_ptr( + discoverable_feature::feature_id_t feature_id) override; + + std::map<discoverable_feature::feature_id_t, discoverable_feature::sptr> _features; +}; + +}} // namespace uhd::features diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 47ca5b79f..6626b9c3a 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -32,7 +32,6 @@ set(test_sources constrained_device_args_test.cpp convert_test.cpp dict_test.cpp - discoverable_feature_test.cpp eeprom_utils_test.cpp error_test.cpp fp_compare_delta_test.cpp @@ -339,6 +338,12 @@ UHD_ADD_NONAPI_TEST( ${CMAKE_SOURCE_DIR}/lib/usrp/common/pwr_cal_mgr.cpp ) +UHD_ADD_NONAPI_TEST( + TARGET "discoverable_feature_test.cpp" + EXTRA_SOURCES + ${CMAKE_SOURCE_DIR}/lib/features/discoverable_feature_registry.cpp +) + ######################################################################## # demo of a loadable module ######################################################################## diff --git a/host/tests/discoverable_feature_test.cpp b/host/tests/discoverable_feature_test.cpp index b75cb76f3..e35660392 100644 --- a/host/tests/discoverable_feature_test.cpp +++ b/host/tests/discoverable_feature_test.cpp @@ -5,6 +5,7 @@ // #include <uhd/features/discoverable_feature_getter_iface.hpp> +#include <uhdlib/features/discoverable_feature_registry.hpp> #include <boost/test/unit_test.hpp> #include <iostream> @@ -38,43 +39,16 @@ public: } }; -class test_feature_getter : public discoverable_feature_getter_iface +class test_feature_getter : public discoverable_feature_registry { public: test_feature_getter(bool feature0_enabled, bool feature1_enabled) { if (feature0_enabled) - _test_feature0.reset(new test_feature0()); + register_feature(std::make_shared<test_feature0>()); if (feature1_enabled) - _test_feature1.reset(new test_feature1); + register_feature(std::make_shared<test_feature1>()); } - - std::vector<std::string> enumerate_features() override - { - std::vector<std::string> features; - if (_test_feature0) - features.push_back(_test_feature0->get_feature_name()); - if (_test_feature1) - features.push_back(_test_feature1->get_feature_name()); - return features; - } - -private: - discoverable_feature::sptr get_feature_ptr( - discoverable_feature::feature_id_t feature_id) override - { - switch (feature_id) { - case discoverable_feature::RESERVED0: - return _test_feature0; - case discoverable_feature::RESERVED1: - return _test_feature1; - default: - return discoverable_feature::sptr(); - }; - } - - discoverable_feature::sptr _test_feature0; - discoverable_feature::sptr _test_feature1; }; BOOST_AUTO_TEST_CASE(test_has_feature_works) @@ -89,10 +63,19 @@ BOOST_AUTO_TEST_CASE(test_enumerate_feature_works) test_feature_getter feature_getter(true, true); const auto features = feature_getter.enumerate_features(); - // Note that ordering isn't strictly a requirement of the interface, - // we just leverage that here to demonstrate API usage concisely. - BOOST_CHECK_EQUAL(features[0], "test_feature0"); - BOOST_CHECK_EQUAL(features[1], "test_feature1"); + bool has_feature0 = false; + bool has_feature1 = false; + for (auto& feature : features) { + if (feature == "test_feature0") { + has_feature0 = true; + } + if (feature == "test_feature1") { + has_feature1 = true; + } + } + BOOST_CHECK_EQUAL(has_feature0, true); + BOOST_CHECK_EQUAL(has_feature1, true); + BOOST_CHECK_EQUAL(features.size(), 2); } BOOST_AUTO_TEST_CASE(test_get_feature_works) |