diff options
Diffstat (limited to 'host/lib/utils')
-rw-r--r-- | host/lib/utils/compat_check.cpp | 89 | ||||
-rw-r--r-- | host/lib/utils/config_parser.cpp | 27 | ||||
-rw-r--r-- | host/lib/utils/csv.cpp | 26 | ||||
-rw-r--r-- | host/lib/utils/eeprom_utils.cpp | 15 | ||||
-rw-r--r-- | host/lib/utils/gain_group.cpp | 5 | ||||
-rw-r--r-- | host/lib/utils/ihex.cpp | 100 | ||||
-rw-r--r-- | host/lib/utils/load_modules.cpp | 65 | ||||
-rw-r--r-- | host/lib/utils/log_c.cpp | 31 | ||||
-rw-r--r-- | host/lib/utils/paths.cpp | 259 | ||||
-rw-r--r-- | host/lib/utils/pathslib.cpp | 14 | ||||
-rw-r--r-- | host/lib/utils/platform.cpp | 50 | ||||
-rw-r--r-- | host/lib/utils/prefs.cpp | 126 | ||||
-rw-r--r-- | host/lib/utils/static.cpp | 13 | ||||
-rw-r--r-- | host/lib/utils/system_time.cpp | 5 | ||||
-rw-r--r-- | host/lib/utils/tasks.cpp | 116 | ||||
-rw-r--r-- | host/lib/utils/thread_priority_c.cpp | 16 |
16 files changed, 479 insertions, 478 deletions
diff --git a/host/lib/utils/compat_check.cpp b/host/lib/utils/compat_check.cpp index e340e5c67..834e23ee4 100644 --- a/host/lib/utils/compat_check.cpp +++ b/host/lib/utils/compat_check.cpp @@ -9,57 +9,50 @@ #include <uhdlib/utils/compat_check.hpp> #include <boost/format.hpp> -void uhd::assert_fpga_compat( - const size_t uhd_major, +void uhd::assert_fpga_compat(const size_t uhd_major, const size_t uhd_minor, const uint64_t fpga_compat, const std::string& fpga_component, const std::string& log_component, - const bool fail_on_minor_behind -) { + const bool fail_on_minor_behind) +{ const size_t fpga_major = fpga_compat >> 32; const size_t fpga_minor = fpga_compat & 0xFFFFFFFF; if (!log_component.empty()) { UHD_LOGGER_DEBUG(log_component) << "Checking compat number for FPGA component `" << fpga_component - << "': Expecting " << uhd_major << "." << uhd_minor << ", actual: " - << fpga_major << "." << fpga_minor << "." - ; + << "': Expecting " << uhd_major << "." << uhd_minor + << ", actual: " << fpga_major << "." << fpga_minor << "."; } if (uhd_major > fpga_major) { if (!log_component.empty()) { UHD_LOGGER_ERROR(log_component) - << "Major compat number mismatch for " << fpga_component << ":" - " Expecting " << uhd_major << ", got " << fpga_major << "." - ; + << "Major compat number mismatch for " << fpga_component + << ":" + " Expecting " + << uhd_major << ", got " << fpga_major << "."; } - throw uhd::runtime_error(str( - boost::format("FPGA component `%s' is revision %d and UHD supports" - " revision %d. Please either upgrade the FPGA" - " image (recommended) or downgrade UHD.") - % fpga_component - % fpga_major - % uhd_major - )); + throw uhd::runtime_error( + str(boost::format("FPGA component `%s' is revision %d and UHD supports" + " revision %d. Please either upgrade the FPGA" + " image (recommended) or downgrade UHD.") + % fpga_component % fpga_major % uhd_major)); } if (uhd_major < fpga_major) { if (!log_component.empty()) { UHD_LOGGER_ERROR(log_component) - << "Major compat number mismatch for " << fpga_component << ":" - " Expecting " << uhd_major << ", got " << fpga_major << "." - ; + << "Major compat number mismatch for " << fpga_component + << ":" + " Expecting " + << uhd_major << ", got " << fpga_major << "."; } - throw uhd::runtime_error(str( - boost::format("FPGA component `%s' is revision %d and UHD supports" - " revision %d. Please either upgrade UHD " - " (recommended) or downgrade the FPGA image.") - % fpga_component - % fpga_major - % uhd_major - )); - + throw uhd::runtime_error( + str(boost::format("FPGA component `%s' is revision %d and UHD supports" + " revision %d. Please either upgrade UHD " + " (recommended) or downgrade the FPGA image.") + % fpga_component % fpga_major % uhd_major)); } if (uhd_minor > fpga_minor) { if (fail_on_minor_behind) { @@ -67,34 +60,19 @@ void uhd::assert_fpga_compat( UHD_LOGGER_ERROR(log_component) << str( boost::format("Minor compat number mismatch for `%s':" " Expecting %d.%d, got %d.%d.") - % fpga_component - % uhd_major - % uhd_minor - % fpga_major - % fpga_minor - ); + % fpga_component % uhd_major % uhd_minor % fpga_major % fpga_minor); } - throw uhd::runtime_error(str( - boost::format("FPGA component `%s' is revision %d.%d and UHD supports" - " revision %d.%d. Please either upgrade UHD " - " (recommended) or downgrade the FPGA image.") - % fpga_component - % fpga_major - % fpga_minor - % uhd_major - % uhd_minor - )); + throw uhd::runtime_error( + str(boost::format("FPGA component `%s' is revision %d.%d and UHD supports" + " revision %d.%d. Please either upgrade UHD " + " (recommended) or downgrade the FPGA image.") + % fpga_component % fpga_major % fpga_minor % uhd_major % uhd_minor)); } else { if (!log_component.empty()) { UHD_LOGGER_WARNING(log_component) << str( boost::format("Non-critical minor compat number mismatch " "for `%s': Expecting %d.%d, got %d.%d.") - % fpga_component - % uhd_major - % uhd_minor - % fpga_major - % fpga_minor - ); + % fpga_component % uhd_major % uhd_minor % fpga_major % fpga_minor); } } } else if (uhd_minor < fpga_minor) { @@ -102,12 +80,7 @@ void uhd::assert_fpga_compat( UHD_LOGGER_WARNING(log_component) << str( boost::format("Non-critical minor compat number mismatch " "for `%s': Expecting %d.%d, got %d.%d.") - % fpga_component - % uhd_major - % uhd_minor - % fpga_major - % fpga_minor - ); + % fpga_component % uhd_major % uhd_minor % fpga_major % fpga_minor); } } // We got here? Then all is good. diff --git a/host/lib/utils/config_parser.cpp b/host/lib/utils/config_parser.cpp index c46671623..bf0cdf5ff 100644 --- a/host/lib/utils/config_parser.cpp +++ b/host/lib/utils/config_parser.cpp @@ -5,36 +5,30 @@ // #include <uhdlib/utils/config_parser.hpp> -#include <boost/format.hpp> #include <boost/filesystem.hpp> +#include <boost/format.hpp> #include <boost/property_tree/ini_parser.hpp> using namespace uhd; -config_parser::config_parser(const std::string &path) +config_parser::config_parser(const std::string& path) { if (not path.empty()) { try { boost::property_tree::ini_parser::read_ini(path, _pt); - } catch (const boost::property_tree::ini_parser_error &) { - throw uhd::runtime_error(str( - boost::format("Unable to parse file %s") - % path - )); + } catch (const boost::property_tree::ini_parser_error&) { + throw uhd::runtime_error( + str(boost::format("Unable to parse file %s") % path)); } } } -void config_parser::read_file(const std::string &path) +void config_parser::read_file(const std::string& path) { config_parser new_config(path); for (const auto& section : new_config.sections()) { for (const auto& key : new_config.options(section)) { - set<std::string>( - section, - key, - new_config.get<std::string>(section, key) - ); + set<std::string>(section, key, new_config.get<std::string>(section, key)); } } } @@ -43,17 +37,16 @@ std::vector<std::string> config_parser::sections() { try { return _options(_pt); - } catch (const boost::property_tree::ptree_bad_path &) { + } catch (const boost::property_tree::ptree_bad_path&) { return std::vector<std::string>{}; } } -std::vector<std::string> config_parser::options(const std::string §ion) +std::vector<std::string> config_parser::options(const std::string& section) { try { return _options(_pt.get_child(section)); - } catch (const boost::property_tree::ptree_bad_path &) { + } catch (const boost::property_tree::ptree_bad_path&) { return std::vector<std::string>{}; } } - diff --git a/host/lib/utils/csv.cpp b/host/lib/utils/csv.cpp index 29b219fef..752b2e881 100644 --- a/host/lib/utils/csv.cpp +++ b/host/lib/utils/csv.cpp @@ -9,30 +9,32 @@ using namespace uhd; -csv::rows_type csv::to_rows(std::istream &input){ +csv::rows_type csv::to_rows(std::istream& input) +{ csv::rows_type rows; std::string line; - //for each line in the input stream - while (std::getline(input, line)){ + // for each line in the input stream + while (std::getline(input, line)) { csv::row_type row(1, ""); bool in_quote = false; char last_ch, next_ch = ' '; - //for each character in the line - for(char ch: line){ + // for each character in the line + for (char ch : line) { last_ch = next_ch; next_ch = ch; - //catch a quote character and change the state - //we handle double quotes by checking last_ch - if (ch == '"'){ + // catch a quote character and change the state + // we handle double quotes by checking last_ch + if (ch == '"') { in_quote = not in_quote; - if (last_ch != '"') continue; + if (last_ch != '"') + continue; } - //a comma not inside quotes is a column delimiter - if (not in_quote and ch == ','){ + // a comma not inside quotes is a column delimiter + if (not in_quote and ch == ',') { row.push_back(""); continue; } - //if we got here we record the character + // if we got here we record the character row.back() += ch; } rows.push_back(row); diff --git a/host/lib/utils/eeprom_utils.cpp b/host/lib/utils/eeprom_utils.cpp index 6999827c0..2cb1c7571 100644 --- a/host/lib/utils/eeprom_utils.cpp +++ b/host/lib/utils/eeprom_utils.cpp @@ -7,16 +7,15 @@ #include <uhdlib/utils/eeprom_utils.hpp> #include <boost/lexical_cast.hpp> -uhd::byte_vector_t string_to_uint16_bytes(const std::string &num_str){ - const uint16_t num = boost::lexical_cast<uint16_t>(num_str); - const std::vector<uint8_t> lsb_msb = { - uint8_t(num >> 0), - uint8_t(num >> 8) - }; +uhd::byte_vector_t string_to_uint16_bytes(const std::string& num_str) +{ + const uint16_t num = boost::lexical_cast<uint16_t>(num_str); + const std::vector<uint8_t> lsb_msb = {uint8_t(num >> 0), uint8_t(num >> 8)}; return lsb_msb; } -std::string uint16_bytes_to_string(const uhd::byte_vector_t &bytes){ +std::string uint16_bytes_to_string(const uhd::byte_vector_t& bytes) +{ const uint16_t num = (uint16_t(bytes.at(0)) << 0) | (uint16_t(bytes.at(1)) << 8); - return (num == 0 or num == 0xffff)? "" : std::to_string(num); + return (num == 0 or num == 0xffff) ? "" : std::to_string(num); } diff --git a/host/lib/utils/gain_group.cpp b/host/lib/utils/gain_group.cpp index 32f14d062..c414bc5ff 100644 --- a/host/lib/utils/gain_group.cpp +++ b/host/lib/utils/gain_group.cpp @@ -130,7 +130,10 @@ public: } std::sort(indexes_step_size_dec.begin(), indexes_step_size_dec.end(), - std::bind(&compare_by_step_size, std::placeholders::_1, std::placeholders::_2, all_fcns)); + std::bind(&compare_by_step_size, + std::placeholders::_1, + std::placeholders::_2, + all_fcns)); UHD_ASSERT_THROW(all_fcns.at(indexes_step_size_dec.front()).get_range().step() >= all_fcns.at(indexes_step_size_dec.back()).get_range().step()); diff --git a/host/lib/utils/ihex.cpp b/host/lib/utils/ihex.cpp index 24d8cbdb6..bea03c2ac 100644 --- a/host/lib/utils/ihex.cpp +++ b/host/lib/utils/ihex.cpp @@ -22,7 +22,7 @@ using namespace uhd; */ static bool checksum(const std::string& record) { - size_t len = record.length(); + size_t len = record.length(); unsigned char sum = 0; unsigned int val; @@ -32,9 +32,9 @@ static bool checksum(const std::string& record) } if (sum == 0) - return true; + return true; else - return false; + return false; } @@ -48,13 +48,12 @@ static bool checksum(const std::string& record) * \param data output data * \return true if record is sucessfully read, false on error */ -static bool parse_record( - const std::string& record, - uint16_t &len, - uint16_t &addr, - uint16_t &type, - unsigned char* data -) { +static bool parse_record(const std::string& record, + uint16_t& len, + uint16_t& addr, + uint16_t& type, + unsigned char* data) +{ unsigned int i; unsigned int val; @@ -65,20 +64,19 @@ static bool parse_record( std::istringstream(record.substr(3, 4)) >> std::hex >> addr; std::istringstream(record.substr(7, 2)) >> std::hex >> type; - if (len > (2 * (record.length() - 9))) // sanity check to prevent buffer overrun + if (len > (2 * (record.length() - 9))) // sanity check to prevent buffer overrun return false; for (i = 0; i < len; i++) { std::istringstream(record.substr(9 + 2 * i, 2)) >> std::hex >> val; - data[i] = (unsigned char) val; + data[i] = (unsigned char)val; } return true; } -ihex_reader::ihex_reader(const std::string &ihex_filename) - : _ihex_filename(ihex_filename) +ihex_reader::ihex_reader(const std::string& ihex_filename) : _ihex_filename(ihex_filename) { // nop } @@ -86,12 +84,12 @@ ihex_reader::ihex_reader(const std::string &ihex_filename) void ihex_reader::read(ihex_reader::record_handle_type record_handler) { - const char *filename = _ihex_filename.c_str(); + const char* filename = _ihex_filename.c_str(); /* Fields used in every record. */ - uint16_t len = 0; - uint16_t type = 0; - uint16_t lower_address_bits = 0x0000; + uint16_t len = 0; + uint16_t type = 0; + uint16_t lower_address_bits = 0x0000; static const int MAX_RECORD_LENGTH = 255; unsigned char data[MAX_RECORD_LENGTH]; @@ -103,7 +101,7 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) std::ifstream file; file.open(filename, std::ifstream::in); - if(!file.good()) { + if (!file.good()) { throw uhd::io_error("ihex_reader::read(): cannot open firmware input file"); } @@ -126,14 +124,16 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) ret = record_handler(lower_address_bits, upper_address_bits, data, len); if (ret < 0) { - throw uhd::io_error("ihex_reader::read(): record hander returned failure code"); + throw uhd::io_error( + "ihex_reader::read(): record hander returned failure code"); } } /* Type 0x01: EOF. */ else if (type == 0x01) { - if (lower_address_bits != 0x0000 || len != 0 ) { - throw uhd::io_error("ihex_reader::read(): For EOF record, address must be 0, length must be 0."); + if (lower_address_bits != 0x0000 || len != 0) { + throw uhd::io_error("ihex_reader::read(): For EOF record, address must " + "be 0, length must be 0."); } /* Successful termination! */ @@ -143,35 +143,39 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) /* Type 0x04: Extended Linear Address Record. */ else if (type == 0x04) { - if (lower_address_bits != 0x0000 || len != 2 ) { - throw uhd::io_error("ihex_reader::read(): For ELA record, address must be 0, length must be 2."); + if (lower_address_bits != 0x0000 || len != 2) { + throw uhd::io_error("ihex_reader::read(): For ELA record, address must " + "be 0, length must be 2."); } - upper_address_bits = ((uint16_t)((data[0] & 0x00FF) << 8))\ - + ((uint16_t)(data[1] & 0x00FF)); + upper_address_bits = + ((uint16_t)((data[0] & 0x00FF) << 8)) + ((uint16_t)(data[1] & 0x00FF)); } /* Type 0x05: Start Linear Address Record. */ else if (type == 0x05) { - if (lower_address_bits != 0x0000 || len != 4 ) { - throw uhd::io_error("ihex_reader::read(): For SLA record, address must be 0, length must be 4."); + if (lower_address_bits != 0x0000 || len != 4) { + throw uhd::io_error("ihex_reader::read(): For SLA record, address must " + "be 0, length must be 4."); } /* The firmware load is complete. We now need to tell the CPU * to jump to an execution address start point, now contained within * the data field. Parse these address bits out, and then push the * instruction. */ - upper_address_bits = ((uint16_t)((data[0] & 0x00FF) << 8))\ - + ((uint16_t)(data[1] & 0x00FF)); - lower_address_bits = ((uint16_t)((data[2] & 0x00FF) << 8))\ - + ((uint16_t)(data[3] & 0x00FF)); + upper_address_bits = + ((uint16_t)((data[0] & 0x00FF) << 8)) + ((uint16_t)(data[1] & 0x00FF)); + lower_address_bits = + ((uint16_t)((data[2] & 0x00FF) << 8)) + ((uint16_t)(data[3] & 0x00FF)); record_handler(lower_address_bits, upper_address_bits, 0, 0); } /* If we receive an unknown record type, error out. */ else { - throw uhd::io_error(str(boost::format("ihex_reader::read(): unsupported record type: %X.") % type)); + throw uhd::io_error( + str(boost::format("ihex_reader::read(): unsupported record type: %X.") + % type)); } } @@ -181,33 +185,33 @@ void ihex_reader::read(ihex_reader::record_handle_type record_handler) // We need a functor for the cast, a lambda would be perfect... int _file_writer_callback( - std::shared_ptr<std::ofstream> output_file, - unsigned char *buff, - uint16_t len -) { - output_file->write((const char *) buff, len); + std::shared_ptr<std::ofstream> output_file, unsigned char* buff, uint16_t len) +{ + output_file->write((const char*)buff, len); return 0; } -void ihex_reader::to_bin_file(const std::string &bin_filename) +void ihex_reader::to_bin_file(const std::string& bin_filename) { std::shared_ptr<std::ofstream> output_file(std::make_shared<std::ofstream>()); output_file->open(bin_filename.c_str(), std::ios::out | std::ios::binary); if (not output_file->is_open()) { - throw uhd::io_error(str(boost::format("Could not open file for writing: %s") % bin_filename)); + throw uhd::io_error( + str(boost::format("Could not open file for writing: %s") % bin_filename)); } - this->read(std::bind(&_file_writer_callback, output_file, std::placeholders::_3, std::placeholders::_4)); + this->read(std::bind(&_file_writer_callback, + output_file, + std::placeholders::_3, + std::placeholders::_4)); output_file->close(); } // We need a functor for the cast, a lambda would be perfect... int _vector_writer_callback( - std::vector<uint8_t>& vector, - unsigned char *buff, - uint16_t len -) { + std::vector<uint8_t>& vector, unsigned char* buff, uint16_t len) +{ for (size_t i = 0; i < len; i++) { vector.push_back(buff[i]); } @@ -220,8 +224,10 @@ std::vector<uint8_t> ihex_reader::to_vector(const size_t size_estimate) std::vector<uint8_t> buf; buf.reserve(size_estimate == 0 ? DEFAULT_SIZE_ESTIMATE : size_estimate); - this->read(std::bind(&_vector_writer_callback, std::ref(buf), std::placeholders::_3, std::placeholders::_4)); + this->read(std::bind(&_vector_writer_callback, + std::ref(buf), + std::placeholders::_3, + std::placeholders::_4)); return buf; } - diff --git a/host/lib/utils/load_modules.cpp b/host/lib/utils/load_modules.cpp index c862a0abd..cac678975 100644 --- a/host/lib/utils/load_modules.cpp +++ b/host/lib/utils/load_modules.cpp @@ -5,11 +5,11 @@ // SPDX-License-Identifier: GPL-3.0-or-later // +#include <uhd/exception.hpp> #include <uhd/utils/paths.hpp> #include <uhd/utils/static.hpp> -#include <uhd/exception.hpp> -#include <boost/format.hpp> #include <boost/filesystem.hpp> +#include <boost/format.hpp> #include <iostream> #include <string> #include <vector> @@ -20,34 +20,34 @@ namespace fs = boost::filesystem; * Module Load Function **********************************************************************/ #ifdef HAVE_DLOPEN -#include <dlfcn.h> -static void load_module(const std::string &file_name){ - if (dlopen(file_name.c_str(), RTLD_LAZY) == NULL){ - throw uhd::os_error(str( - boost::format("dlopen failed to load \"%s\"") % file_name - )); +# include <dlfcn.h> +static void load_module(const std::string& file_name) +{ + if (dlopen(file_name.c_str(), RTLD_LAZY) == NULL) { + throw uhd::os_error( + str(boost::format("dlopen failed to load \"%s\"") % file_name)); } } #endif /* HAVE_DLOPEN */ #ifdef HAVE_LOAD_LIBRARY -#include <windows.h> -static void load_module(const std::string &file_name){ - if (LoadLibrary(file_name.c_str()) == NULL){ - throw uhd::os_error(str( - boost::format("LoadLibrary failed to load \"%s\"") % file_name - )); +# include <windows.h> +static void load_module(const std::string& file_name) +{ + if (LoadLibrary(file_name.c_str()) == NULL) { + throw uhd::os_error( + str(boost::format("LoadLibrary failed to load \"%s\"") % file_name)); } } #endif /* HAVE_LOAD_LIBRARY */ #ifdef HAVE_LOAD_MODULES_DUMMY -static void load_module(const std::string &file_name){ +static void load_module(const std::string& file_name) +{ throw uhd::not_implemented_error(str( - boost::format("Module loading not supported: Cannot load \"%s\"") % file_name - )); + boost::format("Module loading not supported: Cannot load \"%s\"") % file_name)); } #endif /* HAVE_LOAD_MODULES_DUMMY */ @@ -60,29 +60,27 @@ static void load_module(const std::string &file_name){ * Does not throw, prints to std error. * \param path the filesystem path */ -static void load_module_path(const fs::path &path){ - if (not fs::exists(path)){ - //std::cerr << boost::format("Module path \"%s\" not found.") % path.string() << std::endl; +static void load_module_path(const fs::path& path) +{ + if (not fs::exists(path)) { + // std::cerr << boost::format("Module path \"%s\" not found.") % path.string() << + // std::endl; return; } - //try to load the files in this path - if (fs::is_directory(path)){ - for( - fs::directory_iterator dir_itr(path); - dir_itr != fs::directory_iterator(); - ++dir_itr - ){ + // try to load the files in this path + if (fs::is_directory(path)) { + for (fs::directory_iterator dir_itr(path); dir_itr != fs::directory_iterator(); + ++dir_itr) { load_module_path(dir_itr->path()); } return; } - //its not a directory, try to load it - try{ + // its not a directory, try to load it + try { load_module(path.string()); - } - catch(const std::exception &err){ + } catch (const std::exception& err) { std::cerr << boost::format("Error: %s") % err.what() << std::endl; } } @@ -90,8 +88,9 @@ static void load_module_path(const fs::path &path){ /*! * Load all the modules given in the module paths. */ -UHD_STATIC_BLOCK(load_modules){ - for(const fs::path &path: uhd::get_module_paths()){ +UHD_STATIC_BLOCK(load_modules) +{ + for (const fs::path& path : uhd::get_module_paths()) { load_module_path(path); } } diff --git a/host/lib/utils/log_c.cpp b/host/lib/utils/log_c.cpp index 8ccc031a5..be08dbe01 100644 --- a/host/lib/utils/log_c.cpp +++ b/host/lib/utils/log_c.cpp @@ -9,16 +9,15 @@ #include <stdarg.h> -void UHD_API _uhd_log( - const uhd_log_severity_level_t log_level, - const char *filename, - const int lineno, - const char *component, - const char *format, - ... -) { - int size = 0; - char *c_str = NULL; +void UHD_API _uhd_log(const uhd_log_severity_level_t log_level, + const char* filename, + const int lineno, + const char* component, + const char* format, + ...) +{ + int size = 0; + char* c_str = NULL; va_list ap; /* figure out size */ @@ -32,7 +31,7 @@ void UHD_API _uhd_log( /* trailing '\0' */ size++; - c_str = (char *)malloc(size); + c_str = (char*)malloc(size); if (!c_str) { return; } @@ -45,14 +44,14 @@ void UHD_API _uhd_log( va_end(ap); try { - uhd::_log::log( - static_cast<uhd::log::severity_level>(log_level), + uhd::_log::log(static_cast<uhd::log::severity_level>(log_level), filename, unsigned(lineno), component, - boost::this_thread::get_id() - ) << c_str; - } catch (...) {} + boost::this_thread::get_id()) + << c_str; + } catch (...) { + } out_free: free(c_str); diff --git a/host/lib/utils/paths.cpp b/host/lib/utils/paths.cpp index 258722d79..326551c8c 100644 --- a/host/lib/utils/paths.cpp +++ b/host/lib/utils/paths.cpp @@ -9,29 +9,27 @@ #include <uhd/exception.hpp> #include <uhd/utils/paths.hpp> #include <uhdlib/utils/paths.hpp> - #include <boost/algorithm/string.hpp> -#include <functional> #include <boost/version.hpp> +#include <functional> #if BOOST_VERSION >= 106100 -#include <boost/dll/runtime_symbol_info.hpp> +# include <boost/dll/runtime_symbol_info.hpp> #endif #include <boost/filesystem.hpp> #include <boost/format.hpp> -#include <regex> #include <boost/tokenizer.hpp> - #include <cstdio> #include <cstdlib> #include <fstream> #include <iostream> +#include <regex> #include <streambuf> #include <string> #include <vector> #ifdef BOOST_MSVC -#define USE_GET_TEMP_PATH -#include <windows.h> //GetTempPath +# define USE_GET_TEMP_PATH +# include <windows.h> //GetTempPath #endif namespace fs = boost::filesystem; @@ -45,22 +43,22 @@ namespace fs = boost::filesystem; * \param default_val A default string value to use if the path isn't found. * \returns The string value of the environment variable. */ -static std::string get_env_var(const std::string &var_name, - const std::string &default_val = "") { - +static std::string get_env_var( + const std::string& var_name, const std::string& default_val = "") +{ std::string env_result = default_val; - char *env_var_str = NULL; + char* env_var_str = NULL; /* Some versions of MinGW don't expose `_dupenv_s` */ #if defined(UHD_PLATFORM_WIN32) && !defined(__MINGW32__) size_t len; errno_t err = _dupenv_s(&env_var_str, &len, var_name.c_str()); - if((not err) and (env_var_str != NULL)) + if ((not err) and (env_var_str != NULL)) env_result = std::string(env_var_str); free(env_var_str); #else env_var_str = std::getenv(var_name.c_str()); - if(env_var_str != NULL) + if (env_var_str != NULL) env_result = std::string(env_var_str); #endif @@ -75,25 +73,28 @@ static std::string get_env_var(const std::string &var_name, * \param var_name The environment variable name to read. * \returns The vector of paths from the environment variable. */ -static std::vector<std::string> get_env_paths(const std::string &var_name){ +static std::vector<std::string> get_env_paths(const std::string& var_name) +{ #ifdef UHD_PLATFORM_WIN32 static const std::string env_path_sep = ";"; #else static const std::string env_path_sep = ":"; #endif /*UHD_PLATFORM_WIN32*/ -#define path_tokenizer(inp) \ - boost::tokenizer<boost::char_separator<char> > \ - (inp, boost::char_separator<char>(env_path_sep.c_str())) +#define path_tokenizer(inp) \ + boost::tokenizer<boost::char_separator<char>>( \ + inp, boost::char_separator<char>(env_path_sep.c_str())) std::string var_value = get_env_var(var_name); std::vector<std::string> paths; - //convert to full filesystem path, filter blank paths - if (var_value.empty()) return paths; - for(const std::string &path_string: path_tokenizer(var_value)){ - if (path_string.empty()) continue; + // convert to full filesystem path, filter blank paths + if (var_value.empty()) + return paths; + for (const std::string& path_string : path_tokenizer(var_value)) { + if (path_string.empty()) + continue; paths.push_back(fs::system_complete(path_string).string()); } @@ -111,10 +112,11 @@ static std::vector<std::string> get_env_paths(const std::string &var_name){ * \param path The path starting with the tilde character * \returns The same path with the tilde expanded to contents of $HOME. */ -static std::string expand_home_directory(std::string path) { +static std::string expand_home_directory(std::string path) +{ boost::trim(path); - if(path.empty() || (path[0] != '~')) { + if (path.empty() || (path[0] != '~')) { return path; } @@ -130,80 +132,95 @@ static std::string expand_home_directory(std::string path) { **********************************************************************/ -std::string uhd::get_tmp_path(void){ - const char *tmp_path = NULL; +std::string uhd::get_tmp_path(void) +{ + const char* tmp_path = NULL; - //try the official uhd temp path environment variable + // try the official uhd temp path environment variable tmp_path = std::getenv("UHD_TEMP_PATH"); - if (tmp_path != NULL) return tmp_path; + if (tmp_path != NULL) + return tmp_path; - //try the windows function if available - #ifdef USE_GET_TEMP_PATH +// try the windows function if available +#ifdef USE_GET_TEMP_PATH char lpBuffer[2048]; - if (GetTempPath(sizeof(lpBuffer), lpBuffer)) return lpBuffer; - #endif + if (GetTempPath(sizeof(lpBuffer), lpBuffer)) + return lpBuffer; +#endif - //try windows environment variables + // try windows environment variables tmp_path = std::getenv("TMP"); - if (tmp_path != NULL) return tmp_path; + if (tmp_path != NULL) + return tmp_path; tmp_path = std::getenv("TEMP"); - if (tmp_path != NULL) return tmp_path; + if (tmp_path != NULL) + return tmp_path; - //try the stdio define if available - #ifdef P_tmpdir +// try the stdio define if available +#ifdef P_tmpdir return P_tmpdir; - #else +#else - //try unix environment variables + // try unix environment variables tmp_path = std::getenv("TMPDIR"); - if (tmp_path != NULL) return tmp_path; + if (tmp_path != NULL) + return tmp_path; - //give up and use the unix default + // give up and use the unix default return "/tmp"; - #endif +#endif } -std::string uhd::get_app_path(void){ +std::string uhd::get_app_path(void) +{ const std::string uhdcalib_path = get_env_var("UHD_CONFIG_DIR"); - if (not uhdcalib_path.empty()) return uhdcalib_path; + if (not uhdcalib_path.empty()) + return uhdcalib_path; const std::string appdata_path = get_env_var("APPDATA"); - if (not appdata_path.empty()) return appdata_path; + if (not appdata_path.empty()) + return appdata_path; const std::string home_path = get_env_var("HOME"); - if (not home_path.empty()) return home_path; + if (not home_path.empty()) + return home_path; return uhd::get_tmp_path(); } #if BOOST_VERSION >= 106100 -std::string uhd::get_pkg_path(void) { +std::string uhd::get_pkg_path(void) +{ fs::path pkg_path = fs::path(uhd::get_lib_path()).parent_path().lexically_normal(); return get_env_var("UHD_PKG_PATH", pkg_path.string()); } -std::string uhd::get_lib_path(void) { +std::string uhd::get_lib_path(void) +{ fs::path runtime_libfile_path = boost::dll::this_line_location(); return runtime_libfile_path.remove_filename().string(); } #else -std::string uhd::get_pkg_path(void) { +std::string uhd::get_pkg_path(void) +{ return get_env_var("UHD_PKG_PATH", UHD_PKG_PATH); } -std::string uhd::get_lib_path(void) { +std::string uhd::get_lib_path(void) +{ fs::path lib_path = fs::path(uhd::get_pkg_path()) / UHD_LIB_DIR; return lib_path.string(); } #endif -std::vector<fs::path> uhd::get_module_paths(void){ +std::vector<fs::path> uhd::get_module_paths(void) +{ std::vector<fs::path> paths; std::vector<std::string> env_paths = get_env_paths("UHD_MODULE_PATH"); - for(std::string &str_path: env_paths) { + for (std::string& str_path : env_paths) { paths.push_back(str_path); } @@ -214,52 +231,76 @@ std::vector<fs::path> uhd::get_module_paths(void){ } #ifdef UHD_PLATFORM_WIN32 -#include <windows.h> +# include <windows.h> /*! * On Windows, query the system registry for the UHD images install path. * If the key isn't found in the registry, an empty string is returned. * \param registry_key_path The registry key to look for. * \return The images path, formatted for windows. */ -std::string _get_images_path_from_registry(const std::string& registry_key_path) { +std::string _get_images_path_from_registry(const std::string& registry_key_path) +{ std::smatch reg_key_match; - //If a substring in the search path is enclosed in [] (square brackets) then it is interpreted as a registry path - if (not std::regex_search(registry_key_path, reg_key_match, std::regex("\\[(.+)\\](.*)", std::regex::icase))) + // If a substring in the search path is enclosed in [] (square brackets) then it is + // interpreted as a registry path + if (not std::regex_search(registry_key_path, + reg_key_match, + std::regex("\\[(.+)\\](.*)", std::regex::icase))) return std::string(); - std::string reg_key_path = std::string(reg_key_match[1].first, reg_key_match[1].second); - std::string path_suffix = std::string(reg_key_match[2].first, reg_key_match[2].second); + std::string reg_key_path = + std::string(reg_key_match[1].first, reg_key_match[1].second); + std::string path_suffix = + std::string(reg_key_match[2].first, reg_key_match[2].second); - //Split the registry path into parent, key-path and value. + // Split the registry path into parent, key-path and value. std::smatch reg_parent_match; - if (not std::regex_search(reg_key_path, reg_parent_match, std::regex("^(.+?)\\\\(.+)\\\\(.+)$", std::regex::icase))) + if (not std::regex_search(reg_key_path, + reg_parent_match, + std::regex("^(.+?)\\\\(.+)\\\\(.+)$", std::regex::icase))) return std::string(); - std::string reg_parent = std::string(reg_parent_match[1].first, reg_parent_match[1].second); - std::string reg_path = std::string(reg_parent_match[2].first, reg_parent_match[2].second); - std::string reg_val_name = std::string(reg_parent_match[3].first, reg_parent_match[3].second); + std::string reg_parent = + std::string(reg_parent_match[1].first, reg_parent_match[1].second); + std::string reg_path = + std::string(reg_parent_match[2].first, reg_parent_match[2].second); + std::string reg_val_name = + std::string(reg_parent_match[3].first, reg_parent_match[3].second); HKEY hkey_parent = HKEY_LOCAL_MACHINE; - if (reg_parent == "HKEY_LOCAL_MACHINE") hkey_parent = HKEY_LOCAL_MACHINE; - else if (reg_parent == "HKEY_CURRENT_USER") hkey_parent = HKEY_CURRENT_USER; - else if (reg_parent == "HKEY_CLASSES_ROOT") hkey_parent = HKEY_CLASSES_ROOT; - else if (reg_parent == "HKEY_CURRENT_CONFIG") hkey_parent = HKEY_CURRENT_CONFIG; - else if (reg_parent == "HKEY_USERS") hkey_parent = HKEY_CURRENT_USER; + if (reg_parent == "HKEY_LOCAL_MACHINE") + hkey_parent = HKEY_LOCAL_MACHINE; + else if (reg_parent == "HKEY_CURRENT_USER") + hkey_parent = HKEY_CURRENT_USER; + else if (reg_parent == "HKEY_CLASSES_ROOT") + hkey_parent = HKEY_CLASSES_ROOT; + else if (reg_parent == "HKEY_CURRENT_CONFIG") + hkey_parent = HKEY_CURRENT_CONFIG; + else if (reg_parent == "HKEY_USERS") + hkey_parent = HKEY_CURRENT_USER; TCHAR value_buff[1024]; - DWORD value_buff_size = 1024*sizeof(TCHAR); + DWORD value_buff_size = 1024 * sizeof(TCHAR); - //Get a handle to the key location + // Get a handle to the key location HKEY hkey_location; - if (RegOpenKeyExA(hkey_parent, reg_path.c_str(), 0, KEY_QUERY_VALUE, &hkey_location) != ERROR_SUCCESS) + if (RegOpenKeyExA(hkey_parent, reg_path.c_str(), 0, KEY_QUERY_VALUE, &hkey_location) + != ERROR_SUCCESS) return std::string(); - //Query key value + // Query key value DWORD dw_type = REG_SZ; - if(RegQueryValueExA(hkey_location, reg_val_name.c_str(), NULL, &dw_type, (LPBYTE)value_buff, &value_buff_size) == ERROR_SUCCESS) { + if (RegQueryValueExA(hkey_location, + reg_val_name.c_str(), + NULL, + &dw_type, + (LPBYTE)value_buff, + &value_buff_size) + == ERROR_SUCCESS) { RegCloseKey(hkey_location); - if (value_buff_size >= 1024*sizeof(TCHAR)) { + if (value_buff_size >= 1024 * sizeof(TCHAR)) { return std::string(); } else { - std::string return_value(value_buff, value_buff_size-1); //value_buff_size includes the null terminator + std::string return_value(value_buff, + value_buff_size - 1); // value_buff_size includes the null terminator return_value += path_suffix; return return_value; } @@ -267,10 +308,10 @@ std::string _get_images_path_from_registry(const std::string& registry_key_path) return std::string(); } } -#endif /*UHD_PLATFORM_WIN32*/ - -std::string uhd::get_images_dir(const std::string &search_paths) { +#endif /*UHD_PLATFORM_WIN32*/ +std::string uhd::get_images_dir(const std::string& search_paths) +{ /* This function will check for the existence of directories in this * order: * @@ -285,9 +326,9 @@ std::string uhd::get_images_dir(const std::string &search_paths) { /* We will start by looking for a path indicated by the `UHD_IMAGES_DIR` * environment variable. */ std::vector<std::string> env_paths = get_env_paths("UHD_IMAGES_DIR"); - for(auto possible_dir: env_paths) { + for (auto possible_dir : env_paths) { if (fs::is_directory(fs::path(possible_dir))) { - return possible_dir; + return possible_dir; } } @@ -306,18 +347,18 @@ std::string uhd::get_images_dir(const std::string &search_paths) { std::vector<std::string> search_paths_vector; boost::split(search_paths_vector, _search_paths, boost::is_any_of(",;")); - for(std::string& search_path: search_paths_vector) { - + for (std::string& search_path : search_paths_vector) { boost::algorithm::trim(search_path); - if (search_path.empty()) continue; + if (search_path.empty()) + continue; #ifdef UHD_PLATFORM_WIN32 possible_dir = _get_images_path_from_registry(search_path); if (possible_dir.empty()) { - //Could not read from the registry due to missing key, invalid - //values, etc Just use the search path. The is_directory check - //will fail if this is a registry path and we will move on to - //the next item in the list. + // Could not read from the registry due to missing key, invalid + // values, etc Just use the search path. The is_directory check + // will fail if this is a registry path and we will move on to + // the next item in the list. possible_dir = search_path; } #else @@ -340,10 +381,12 @@ std::string uhd::get_images_dir(const std::string &search_paths) { } } -std::string uhd::find_image_path(const std::string &image_name, const std::string &search_paths){ +std::string uhd::find_image_path( + const std::string& image_name, const std::string& search_paths) +{ /* If a path was provided on the command-line or as a hint from the caller, * we default to that. */ - if (fs::exists(image_name)){ + if (fs::exists(image_name)) { return fs::system_complete(image_name).string(); } @@ -354,33 +397,33 @@ std::string uhd::find_image_path(const std::string &image_name, const std::strin if (fs::exists(image_path)) { return image_path.string(); } else { - throw uhd::io_error( - "Could not find the image '" + image_name + "' in the image directory " + images_dir - + "\nFor more information regarding image paths, please refer to the UHD manual."); + throw uhd::io_error("Could not find the image '" + image_name + + "' in the image directory " + images_dir + + "\nFor more information regarding image paths, please " + "refer to the UHD manual."); } } /* If we made it this far, then we didn't find anything. */ images_dir = "<no images directory located>"; - throw uhd::io_error("Could not find path for image: " + image_name - + "\n\n" - + "Using images directory: " + images_dir - + "\n\n" - + "Set the environment variable 'UHD_IMAGES_DIR' appropriately or" - + " follow the below instructions to download the images package." - + "\n\n" - + uhd::print_utility_error("uhd_images_downloader.py")); + throw uhd::io_error("Could not find path for image: " + image_name + "\n\n" + + "Using images directory: " + images_dir + "\n\n" + + "Set the environment variable 'UHD_IMAGES_DIR' appropriately or" + + " follow the below instructions to download the images package." + + "\n\n" + uhd::print_utility_error("uhd_images_downloader.py")); } -std::string uhd::find_utility(const std::string &name) { - return fs::path(fs::path(uhd::get_lib_path()) / "uhd" / "utils" / name) - .string(); +std::string uhd::find_utility(const std::string& name) +{ + return fs::path(fs::path(uhd::get_lib_path()) / "uhd" / "utils" / name).string(); } -std::string uhd::print_utility_error(const std::string &name, const std::string &args){ - #ifdef UHD_PLATFORM_WIN32 - return "As an Administrator, please run:\n\n\"" + find_utility(name) + args + "\""; - #else - return "Please run:\n\n \"" + find_utility(name) + (args.empty() ? "" : (" " + args)) + "\""; - #endif +std::string uhd::print_utility_error(const std::string& name, const std::string& args) +{ +#ifdef UHD_PLATFORM_WIN32 + return "As an Administrator, please run:\n\n\"" + find_utility(name) + args + "\""; +#else + return "Please run:\n\n \"" + find_utility(name) + (args.empty() ? "" : (" " + args)) + + "\""; +#endif } diff --git a/host/lib/utils/pathslib.cpp b/host/lib/utils/pathslib.cpp index cbb0d0917..547c860b1 100644 --- a/host/lib/utils/pathslib.cpp +++ b/host/lib/utils/pathslib.cpp @@ -8,9 +8,9 @@ #include <uhdlib/utils/paths.hpp> #ifdef BOOST_MSVC -# include <windows.h> +# include <windows.h> #else -# include <wordexp.h> +# include <wordexp.h> #endif std::string uhd::path_expandvars(const std::string& path) @@ -21,15 +21,12 @@ std::string uhd::path_expandvars(const std::string& path) #ifdef BOOST_MSVC constexpr size_t max_pathlen = 4096; char result[max_pathlen]; - const size_t result_len = ExpandEnvironmentStrings( - path.c_str(), - &result[0], - max_pathlen - ); + const size_t result_len = + ExpandEnvironmentStrings(path.c_str(), &result[0], max_pathlen); if (result == 0) { return path; } - return std::string(result, result+result_len); + return std::string(result, result + result_len); #else wordexp_t p; std::string return_value; @@ -42,4 +39,3 @@ std::string uhd::path_expandvars(const std::string& path) return return_value; #endif } - diff --git a/host/lib/utils/platform.cpp b/host/lib/utils/platform.cpp index 859f3aa20..e12f7ad17 100644 --- a/host/lib/utils/platform.cpp +++ b/host/lib/utils/platform.cpp @@ -5,44 +5,52 @@ // SPDX-License-Identifier: GPL-3.0-or-later // -#include <uhd/utils/platform.hpp> #include <uhd/config.hpp> +#include <uhd/utils/platform.hpp> #include <boost/functional/hash.hpp> #ifdef UHD_PLATFORM_WIN32 -#include <windows.h> +# include <windows.h> #else -#include <unistd.h> +# include <unistd.h> #endif namespace uhd { - int32_t get_process_id() { +int32_t get_process_id() +{ #ifdef UHD_PLATFORM_WIN32 return int32_t(GetCurrentProcessId()); #else return int32_t(getpid()); #endif - } +} - uint32_t get_host_id() { +uint32_t get_host_id() +{ #ifdef UHD_PLATFORM_WIN32 - //extract volume serial number - char szVolName[MAX_PATH+1], szFileSysName[MAX_PATH+1]; - DWORD dwSerialNumber, dwMaxComponentLen, dwFileSysFlags; - GetVolumeInformation("C:\\", szVolName, MAX_PATH, - &dwSerialNumber, &dwMaxComponentLen, - &dwFileSysFlags, szFileSysName, sizeof(szFileSysName)); + // extract volume serial number + char szVolName[MAX_PATH + 1], szFileSysName[MAX_PATH + 1]; + DWORD dwSerialNumber, dwMaxComponentLen, dwFileSysFlags; + GetVolumeInformation("C:\\", + szVolName, + MAX_PATH, + &dwSerialNumber, + &dwMaxComponentLen, + &dwFileSysFlags, + szFileSysName, + sizeof(szFileSysName)); - return uint32_t(dwSerialNumber); + return uint32_t(dwSerialNumber); #else - return uint32_t(gethostid()); + return uint32_t(gethostid()); #endif - } +} - uint32_t get_process_hash() { - size_t hash = 0; - boost::hash_combine(hash, uhd::get_process_id()); - boost::hash_combine(hash, uhd::get_host_id()); - return uint32_t(hash); - } +uint32_t get_process_hash() +{ + size_t hash = 0; + boost::hash_combine(hash, uhd::get_process_id()); + boost::hash_combine(hash, uhd::get_host_id()); + return uint32_t(hash); } +} // namespace uhd diff --git a/host/lib/utils/prefs.cpp b/host/lib/utils/prefs.cpp index 63479c911..72a9c9eea 100644 --- a/host/lib/utils/prefs.cpp +++ b/host/lib/utils/prefs.cpp @@ -4,74 +4,69 @@ // SPDX-License-Identifier: GPL-3.0+ // -#include <config.h> #include <uhd/utils/log.hpp> #include <uhd/utils/paths.hpp> -#include <uhdlib/utils/prefs.hpp> #include <uhdlib/utils/paths.hpp> +#include <uhdlib/utils/prefs.hpp> +#include <config.h> #include <boost/filesystem.hpp> #include <cstdlib> using namespace uhd; namespace { - constexpr char UHD_CONF_FILE_VAR[] = "UHD_CONFIG_FILE"; +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); - if (boost::filesystem::exists(path)) { - try { - conf_file.read_file(path); - UHD_LOG_DEBUG("PREFS", - "Loaded " << config_type << " config file " << path); - } catch (...) { - UHD_LOG_DEBUG("PREFS", - "Failed to load " << config_type << " config file " << path); - } - } else { - UHD_LOG_TRACE("PREFS", - "No " << config_type << " config file found at " << path); +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); + if (boost::filesystem::exists(path)) { + try { + conf_file.read_file(path); + UHD_LOG_DEBUG( + "PREFS", "Loaded " << config_type << " config file " << path); + } catch (...) { + UHD_LOG_DEBUG( + "PREFS", "Failed to load " << config_type << " config file " << path); } + } else { + UHD_LOG_TRACE( + "PREFS", "No " << config_type << " config file found at " << path); } } +} - 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); - } +void update_from_key( + const std::string& key, const std::string& value, uhd::device_addr_t& user_args) +{ + if (value.empty()) { + return; } - device_addr_t get_args( - const uhd::device_addr_t& user_args, - const std::vector<std::string>& keys_to_update_from - ) { - device_addr_t args; - for (const auto& key : keys_to_update_from) { - update_from_key(key, user_args.get(key, ""), args); - } + 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); + } +} - // Finally, copy over the original user args: - for (const auto& user_key : user_args.keys()) { - args[user_key] = user_args[user_key]; - } +device_addr_t get_args(const uhd::device_addr_t& user_args, + const std::vector<std::string>& keys_to_update_from) +{ + device_addr_t args; + for (const auto& key : keys_to_update_from) { + update_from_key(key, user_args.get(key, ""), args); + } - return args; + // Finally, copy over the original user args: + for (const auto& user_key : user_args.keys()) { + args[user_key] = user_args[user_key]; } + + return args; } +} // namespace config_parser& uhd::prefs::get_uhd_config() { @@ -82,8 +77,8 @@ config_parser& uhd::prefs::get_uhd_config() 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(); + (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 @@ -91,7 +86,7 @@ config_parser& uhd::prefs::get_uhd_config() env_conf_file = std::string(std::getenv(UHD_CONF_FILE_VAR)); } _update_conf_file(env_conf_file, "ENV", _conf_files); - } catch (const std::exception &) { + } catch (const std::exception&) { // nop } init_done = true; @@ -102,31 +97,20 @@ config_parser& uhd::prefs::get_uhd_config() } -device_addr_t uhd::prefs::get_usrp_args( - const uhd::device_addr_t &user_args -) { - const std::vector<std::string> keys_to_update_from = { - "type", - "product", - "serial" - }; +device_addr_t uhd::prefs::get_usrp_args(const uhd::device_addr_t& user_args) +{ + const std::vector<std::string> keys_to_update_from = {"type", "product", "serial"}; return get_args(user_args, keys_to_update_from); } -device_addr_t uhd::prefs::get_dpdk_args( - const uhd::device_addr_t &user_args -) { - const std::vector<std::string> keys_to_update_from = { - "use_dpdk" - }; +device_addr_t uhd::prefs::get_dpdk_args(const uhd::device_addr_t& user_args) +{ + const std::vector<std::string> keys_to_update_from = {"use_dpdk"}; return get_args(user_args, keys_to_update_from); } -device_addr_t uhd::prefs::get_dpdk_nic_args( - const uhd::device_addr_t &user_args -) { - const std::vector<std::string> keys_to_update_from = { - "dpdk_mac" - }; +device_addr_t uhd::prefs::get_dpdk_nic_args(const uhd::device_addr_t& user_args) +{ + const std::vector<std::string> keys_to_update_from = {"dpdk_mac"}; return get_args(user_args, keys_to_update_from); } diff --git a/host/lib/utils/static.cpp b/host/lib/utils/static.cpp index 84ca992c8..22cd3a1b6 100644 --- a/host/lib/utils/static.cpp +++ b/host/lib/utils/static.cpp @@ -6,17 +6,16 @@ // #include <uhd/utils/static.hpp> -#include <stdexcept> #include <iostream> -_uhd_static_fixture::_uhd_static_fixture(void (*fcn)(void), const char *name){ - try{ +#include <stdexcept> +_uhd_static_fixture::_uhd_static_fixture(void (*fcn)(void), const char* name) +{ + try { fcn(); - } - catch(const std::exception &e){ + } catch (const std::exception& e) { std::cerr << "Exception in static block " << name << std::endl; std::cerr << " " << e.what() << std::endl; - } - catch(...){ + } catch (...) { std::cerr << "Exception in static block " << name << std::endl; } } diff --git a/host/lib/utils/system_time.cpp b/host/lib/utils/system_time.cpp index 71fcd3fff..6693a38d4 100644 --- a/host/lib/utils/system_time.cpp +++ b/host/lib/utils/system_time.cpp @@ -9,8 +9,9 @@ uhd::time_spec_t uhd::get_system_time(void) { - const auto now = std::chrono::steady_clock::now().time_since_epoch(); + const auto now = std::chrono::steady_clock::now().time_since_epoch(); const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now); - const auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(now-seconds); + const auto nanoseconds = + std::chrono::duration_cast<std::chrono::nanoseconds>(now - seconds); return uhd::time_spec_t(seconds.count(), nanoseconds.count(), 1e9); } diff --git a/host/lib/utils/tasks.cpp b/host/lib/utils/tasks.cpp index a2d7f82d3..ce6e1716c 100644 --- a/host/lib/utils/tasks.cpp +++ b/host/lib/utils/tasks.cpp @@ -21,19 +21,19 @@ using namespace uhd; -class task_impl : public task{ +class task_impl : public task +{ public: - - task_impl(const task_fcn_type &task_fcn, const std::string &name): - _exit(false) + task_impl(const task_fcn_type& task_fcn, const std::string& name) : _exit(false) { - _task = std::thread([this, task_fcn](){ this->task_loop(task_fcn); }); + _task = std::thread([this, task_fcn]() { this->task_loop(task_fcn); }); if (not name.empty()) { set_thread_name(&_task, name); } } - ~task_impl(void){ + ~task_impl(void) + { _exit = true; if (_task.joinable()) { _task.join(); @@ -41,37 +41,37 @@ public: } private: - void task_loop(const task_fcn_type &task_fcn){ - try{ - while (!_exit){ + void task_loop(const task_fcn_type& task_fcn) + { + try { + while (!_exit) { task_fcn(); } - } - catch(const std::exception &e){ + } catch (const std::exception& e) { do_error_msg(e.what()); - } - catch(...){ + } catch (...) { UHD_THROW_INVALID_CODE_PATH(); } } - void do_error_msg(const std::string &msg){ + void do_error_msg(const std::string& msg) + { UHD_LOGGER_ERROR("UHD") << "An unexpected exception was caught in a task loop." - << "The task loop will now exit, things may not work." - << msg - ; + << "The task loop will now exit, things may not work." << msg; } std::atomic<bool> _exit; std::thread _task; }; -task::sptr task::make(const task_fcn_type &task_fcn, const std::string &name){ +task::sptr task::make(const task_fcn_type& task_fcn, const std::string& name) +{ return task::sptr(new task_impl(task_fcn, name)); } -msg_task::~msg_task(void){ +msg_task::~msg_task(void) +{ /* NOP */ } @@ -81,17 +81,18 @@ msg_task::~msg_task(void){ * ctrl_cores can check this queue for stranded messages. */ -class msg_task_impl : public msg_task{ +class msg_task_impl : public msg_task +{ public: - - msg_task_impl(const task_fcn_type &task_fcn): - _spawn_barrier(2) + msg_task_impl(const task_fcn_type& task_fcn) : _spawn_barrier(2) { - (void)_thread_group.create_thread(std::bind(&msg_task_impl::task_loop, this, task_fcn)); + (void)_thread_group.create_thread( + std::bind(&msg_task_impl::task_loop, this, task_fcn)); _spawn_barrier.wait(); } - ~msg_task_impl(void){ + ~msg_task_impl(void) + { _running = false; _thread_group.interrupt_all(); _thread_group.join_all(); @@ -99,8 +100,8 @@ public: /* * Returns the first message for the given SID. - * This way a radio_ctrl_core doesn't have to die in timeout but can check for stranded messages here. - * This might happen during shutdown when dtors are called. + * This way a radio_ctrl_core doesn't have to die in timeout but can check for + * stranded messages here. This might happen during shutdown when dtors are called. * See also: comments in b200_io_impl->handle_async_task */ msg_payload_t get_msg_from_dump_queue(uint32_t sid) @@ -118,43 +119,40 @@ public: } private: - - void task_loop(const task_fcn_type &task_fcn){ + void task_loop(const task_fcn_type& task_fcn) + { _running = true; _spawn_barrier.wait(); - try{ - while (_running){ - boost::optional<msg_type_t> buff = task_fcn(); - if(buff != boost::none){ - /* - * If a message gets stranded it is returned by task_fcn and then pushed to the dump_queue. - * This way ctrl_cores can check dump_queue for missing messages. - */ - boost::mutex::scoped_lock lock(_mutex); - _dump_queue.push_back(buff.get() ); - } + try { + while (_running) { + boost::optional<msg_type_t> buff = task_fcn(); + if (buff != boost::none) { + /* + * If a message gets stranded it is returned by task_fcn and then + * pushed to the dump_queue. This way ctrl_cores can check dump_queue + * for missing messages. + */ + boost::mutex::scoped_lock lock(_mutex); + _dump_queue.push_back(buff.get()); + } } - } - catch(const boost::thread_interrupted &){ - //this is an ok way to exit the task loop - } - catch(const std::exception &e){ + } catch (const boost::thread_interrupted&) { + // this is an ok way to exit the task loop + } catch (const std::exception& e) { do_error_msg(e.what()); - } - catch(...){ - //FIXME - //Unfortunately, this is also an ok way to end a task, - //because on some systems boost throws uncatchables. + } catch (...) { + // FIXME + // Unfortunately, this is also an ok way to end a task, + // because on some systems boost throws uncatchables. } } - void do_error_msg(const std::string &msg){ + void do_error_msg(const std::string& msg) + { UHD_LOGGER_ERROR("UHD") - << "An unexpected exception was caught in a task loop." - << "The task loop will now exit, things may not work." - << msg - ; + << "An unexpected exception was caught in a task loop." + << "The task loop will now exit, things may not work." << msg; } boost::mutex _mutex; @@ -163,11 +161,13 @@ private: bool _running; /* - * This queue holds stranded messages until a radio_ctrl_core grabs them via 'get_msg_from_dump_queue'. + * This queue holds stranded messages until a radio_ctrl_core grabs them via + * 'get_msg_from_dump_queue'. */ - std::vector <msg_type_t> _dump_queue; + std::vector<msg_type_t> _dump_queue; }; -msg_task::sptr msg_task::make(const task_fcn_type &task_fcn){ +msg_task::sptr msg_task::make(const task_fcn_type& task_fcn) +{ return msg_task::sptr(new msg_task_impl(task_fcn)); } diff --git a/host/lib/utils/thread_priority_c.cpp b/host/lib/utils/thread_priority_c.cpp index 6f0c76fe9..3e9f1dbf4 100644 --- a/host/lib/utils/thread_priority_c.cpp +++ b/host/lib/utils/thread_priority_c.cpp @@ -6,18 +6,14 @@ // #include <uhd/error.h> -#include <uhd/utils/thread_priority.h> -#include <uhd/utils/thread.hpp> -#include <uhd/utils/log.hpp> #include <uhd/exception.hpp> +#include <uhd/utils/log.hpp> +#include <uhd/utils/thread.hpp> +#include <uhd/utils/thread_priority.h> #include <boost/format.hpp> #include <iostream> -uhd_error uhd_set_thread_priority( - float priority, - bool realtime -){ - UHD_SAFE_C( - uhd::set_thread_priority(priority, realtime); - ) +uhd_error uhd_set_thread_priority(float priority, bool realtime) +{ + UHD_SAFE_C(uhd::set_thread_priority(priority, realtime);) } |