diff options
Diffstat (limited to 'host')
-rw-r--r-- | host/CMakeLists.txt | 25 | ||||
-rw-r--r-- | host/lib/include/uhdlib/utils/prefs.hpp | 54 | ||||
-rw-r--r-- | host/lib/utils/CMakeLists.txt | 7 | ||||
-rw-r--r-- | host/lib/utils/prefs.cpp | 101 |
4 files changed, 186 insertions, 1 deletions
diff --git a/host/CMakeLists.txt b/host/CMakeLists.txt index 72fafacbb..55f78ac30 100644 --- a/host/CMakeLists.txt +++ b/host/CMakeLists.txt @@ -104,7 +104,6 @@ ENDIF() SET(LIBUHD_PKG ${LIBUHD_PKG} CACHE BOOL "Build Debian libuhd003 package") SET(LIBUHDDEV_PKG ${LIBUHDDEV_PKG} CACHE BOOL "Build Debian libuhd-dev package") SET(UHDHOST_PKG ${UHDPOST_PKG} CACHE BOOL "Build Debian uhd-host package") - INCLUDE(UHDComponent) #enable components INCLUDE(UHDPackage) #setup cpack @@ -131,6 +130,30 @@ SET(PKG_DOC_DIR share/doc/uhd) SET(PKG_MAN_DIR share/man/man1) ######################################################################## +# UHD config files +######################################################################## +SET(UHD_USER_CONF_FILE + ".uhd/uhd.conf" + CACHE STRING + "Location of the user-specific UHD configuration file, relative to APPDATA (or HOME)" +) +IF(UNIX) + SET(UHD_SYS_CONF_FILE + "/etc/uhd/uhd.conf" + CACHE STRING + "Location of the system-wide UHD configuration file" + ) +ELSEIF(WIN32) + SET(UHD_SYS_CONF_FILE + "%programdata%/uhd/uhd.conf" + CACHE STRING + "Location of the system-wide UHD configuration file" + ) +ELSE() + MESSAGE(WARNING "Not setting UHD_SYS_CONF_FILE!") +ENDIF() + +######################################################################## # UHD Image Directories ######################################################################## IF(NOT DEFINED UHD_IMAGES_DIR) diff --git a/host/lib/include/uhdlib/utils/prefs.hpp b/host/lib/include/uhdlib/utils/prefs.hpp new file mode 100644 index 000000000..62831a875 --- /dev/null +++ b/host/lib/include/uhdlib/utils/prefs.hpp @@ -0,0 +1,54 @@ +// +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#ifndef INCLUDED_LIBUHD_UTILS_PREFS_HPP +#define INCLUDED_LIBUHD_UTILS_PREFS_HPP + +#include <uhd/types/device_addr.hpp> +#include <uhdlib/utils/config_parser.hpp> +#include <string> + +namespace uhd { namespace prefs { + + /*! Return a reference to an object representing the UHD config file + * state. + * + * Note: Don't call this in static initializers. + */ + config_parser& get_uhd_config(); + + /*! Convenience function to update device args with settings from + * config files. + * + * Assume the user has a configuration file as such: + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.ini} + * [type=b200] + * master_clock_rate=20e6 + * + * [serial=f42f9b] ; Let's assume this is another B200 + * master_clock_rate=10e6 + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * If get_usrp_args() gets called with "type" key equal to "b200", it will + * first apply the `master_clock_rate=20e6` settings, as if they had been + * passed in as device args into the initialization sequence. If the device + * happens to have the serial number listed above, i.e., "serial" equals + * "f42f9b", then the new value `master_clock_rate=10e6` will get applied. + * + * If the user actually specified their own value of `master_clock_rate`, + * that value would get applied. + * + * + * \param user_args After getting the device args from the config + * files, all of these key/value pairs will be applied + * and will overwrite the settings from config files + * if they exist. + */ + uhd::device_addr_t get_usrp_args(const uhd::device_addr_t &user_args); + +}} /* namespace uhd::prefs */ + +#endif /* INCLUDED_LIBUHD_UTILS_PREFS_HPP */ + diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index 62d1ba090..659c855a1 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -155,6 +155,12 @@ SET_SOURCE_FILES_PROPERTIES( "UHD_PKG_PATH=\"${UHD_PKG_PATH}\";UHD_LIB_DIR=\"${UHD_LIB_DIR}\"" ) +SET_SOURCE_FILES_PROPERTIES( + ${CMAKE_CURRENT_SOURCE_DIR}/prefs.cpp + PROPERTIES COMPILE_DEFINITIONS + "UHD_SYS_CONF_FILE=\"${UHD_SYS_CONF_FILE}\";UHD_USER_CONF_FILE=\"${UHD_USER_CONF_FILE}\"" +) + ######################################################################## # Append sources ######################################################################## @@ -170,6 +176,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp ${CMAKE_CURRENT_SOURCE_DIR}/pathslib.cpp ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/prefs.cpp ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp ${CMAKE_CURRENT_SOURCE_DIR}/thread.cpp diff --git a/host/lib/utils/prefs.cpp b/host/lib/utils/prefs.cpp new file mode 100644 index 000000000..5e5fa40f5 --- /dev/null +++ b/host/lib/utils/prefs.cpp @@ -0,0 +1,101 @@ +// +// Copyright 2017-2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#include <uhd/utils/log.hpp> +#include <uhd/utils/paths.hpp> +#include <uhdlib/utils/prefs.hpp> +#include <uhdlib/utils/paths.hpp> +#include <boost/filesystem.hpp> +#include <cstdlib> + +using namespace uhd; + +namespace { + constexpr char UHD_CONF_FILE_VAR[] = "UHD_CONFIG_FILE"; + + inline void _update_conf_file( + const std::string& path, + const std::string& config_type, + config_parser& conf_file + ) { + if (not path.empty()) { + UHD_LOG_TRACE("PREFS", "Trying to load " << path); + try { + conf_file.read_file(path); + UHD_LOG_DEBUG("PREFS", + "Loaded " << config_type << " config file " << path); + } catch (...) { + // nop + } + } + } + + void update_from_key( + const std::string& key, const std::string &value, + uhd::device_addr_t& user_args + ) { + if (value.empty()) { + return; + } + + const std::string key_str = key + "=" + value; + for (const auto& key : uhd::prefs::get_uhd_config().options(key_str)) { + user_args[key] = + uhd::prefs::get_uhd_config().get<std::string>(key_str, key); + } + } +} + +config_parser& uhd::prefs::get_uhd_config() +{ + static config_parser _conf_files{}; + static bool init_done = false; + if (not init_done) { + UHD_LOG_TRACE("CONF", "Initializing config file object..."); + const std::string sys_conf_file = path_expandvars(UHD_SYS_CONF_FILE); + _update_conf_file(sys_conf_file, "system", _conf_files); + const std::string user_conf_file = + (boost::filesystem::path(get_app_path()) + / std::string(UHD_USER_CONF_FILE)).string(); + _update_conf_file(user_conf_file, "user", _conf_files); + std::string env_conf_file; + try { // getenv into std::string can fail + if (std::getenv(UHD_CONF_FILE_VAR) != NULL) { + env_conf_file = std::string(std::getenv(UHD_CONF_FILE_VAR)); + } + _update_conf_file(env_conf_file, "ENV", _conf_files); + } catch (const std::exception &) { + // nop + } + UHD_LOG_TRACE("PREFS", "Done initializing."); + } + + return _conf_files; +} + + +device_addr_t uhd::prefs::get_usrp_args( + const uhd::device_addr_t &user_args +) { + device_addr_t usrp_args; + const std::vector<std::string> keys_to_update_from = { + "type", + "product", + "serial" + }; + + for (const auto& key : keys_to_update_from) { + update_from_key(key, user_args.get(key, ""), usrp_args); + } + + // Finally, copy over the original user args: + for (const auto &user_key : user_args.keys()) { + usrp_args[user_key] = user_args[user_key]; + } + + return usrp_args; +} + |