From 4d5df2376b204afb724684d0d864ce0d93fe83fb Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 3 May 2010 01:20:11 -0700 Subject: Expanded the dboard id API to create dboard id types from strings and ints. And created utility functions to go between representations. Created to_pp_string for pretty print strings for dboard ids and device addrs. Minor changes to the various classes that call these utilities. --- host/include/uhd/types/device_addr.hpp | 10 ++--- host/include/uhd/usrp/dboard_id.hpp | 64 +++++++++++++++++++++++++++--- host/lib/CMakeLists.txt | 3 +- host/lib/device.cpp | 4 +- host/lib/types.cpp | 4 +- host/lib/usrp/dboard/db_basic_and_lf.cpp | 4 +- host/lib/usrp/dboard/db_rfx.cpp | 4 +- host/lib/usrp/dboard/db_xcvr2450.cpp | 4 +- host/lib/usrp/dboard_base.cpp | 16 ++++---- host/lib/usrp/dboard_eeprom.cpp | 13 +++--- host/lib/usrp/dboard_id.cpp | 68 ++++++++++++++++++++++++++++++++ host/lib/usrp/dboard_manager.cpp | 23 ++++++----- host/test/addr_test.cpp | 17 +++++++- host/utils/uhd_burn_db_eeprom.cpp | 8 ++-- host/utils/uhd_find_devices.cpp | 2 +- 15 files changed, 191 insertions(+), 53 deletions(-) create mode 100644 host/lib/usrp/dboard_id.cpp diff --git a/host/include/uhd/types/device_addr.hpp b/host/include/uhd/types/device_addr.hpp index e8da2a1ab..e359d9467 100644 --- a/host/include/uhd/types/device_addr.hpp +++ b/host/include/uhd/types/device_addr.hpp @@ -40,7 +40,7 @@ namespace uhd{ * An arguments string, is a way to represent a device address * using a single string with delimiter characters. * - Ex: addr=192.168.10.2 - * - Ex: addr=192.168.10.2, rx_buff_size=1e6 + * - Ex: addr=192.168.10.2, recv_buff_size=1e6 */ class UHD_API device_addr_t : public dict{ public: @@ -51,17 +51,17 @@ namespace uhd{ device_addr_t(const std::string &args = ""); /*! - * Convert a device address into a printable string. - * \return string good for use with std::cout << + * Convert a device address into a pretty print string. + * \return a printable string representing the device address */ - std::string to_string(void) const; + std::string to_pp_string(void) const; /*! * Convert the device address into an args string. * The args string contains delimiter symbols. * \return a string with delimiter markup */ - std::string to_args_str(void) const; + std::string to_string(void) const; }; //handy typedef for a vector of device addresses diff --git a/host/include/uhd/usrp/dboard_id.hpp b/host/include/uhd/usrp/dboard_id.hpp index afacaf8ff..8b6eaf6bd 100644 --- a/host/include/uhd/usrp/dboard_id.hpp +++ b/host/include/uhd/usrp/dboard_id.hpp @@ -20,16 +20,70 @@ #include #include +#include #include namespace uhd{ namespace usrp{ -typedef boost::uint16_t dboard_id_t; + class UHD_API dboard_id_t : boost::equality_comparable1{ + public: + /*! + * Create a dboard id from an integer. + * \param id the integer representation + */ + dboard_id_t(boost::uint16_t id = 0xffff); -namespace dboard_id{ - static const dboard_id_t NONE = 0xffff; - UHD_API std::string to_string(const dboard_id_t &id); -} + /*! + * Obtain a dboard id that represents no dboard. + * \return the dboard id with the 0xffff id. + */ + static dboard_id_t none(void); + + /*! + * Create a new dboard id from an integer representation. + * \param uint16 an unsigned 16 bit integer + * \return a new dboard id containing the integer + */ + static dboard_id_t from_uint16(boost::uint16_t uint16); + + /*! + * Get the dboard id represented as an integer. + * \return an unsigned 16 bit integer representation + */ + boost::uint16_t to_uint16(void) const; + + /*! + * Create a new dboard id from a string representation. + * If the string has a 0x prefix, it will be parsed as hex. + * \param string a numeric string, possibly hex + * \return a new dboard id containing the integer + */ + static dboard_id_t from_string(const std::string &string); + + /*! + * Get the dboard id represented as an integer. + * \return a hex string representation with 0x prefix + */ + std::string to_string(void) const; + + /*! + * Get the pretty print representation of this dboard id. + * \return a string with the dboard name and id number + */ + std::string to_pp_string(void) const; + + private: + boost::uint16_t _id; //internal representation + }; + + /*! + * Comparator operator overloaded for dboard ids. + * The boost::equality_comparable provides the !=. + * \param lhs the dboard id to the left of the operator + * \param rhs the dboard id to the right of the operator + * \return true when the dboard ids are equal + */ + UHD_API bool operator==(const dboard_id_t &lhs, const dboard_id_t &rhs); }} //namespace diff --git a/host/lib/CMakeLists.txt b/host/lib/CMakeLists.txt index 5252eda8f..47080a4e0 100644 --- a/host/lib/CMakeLists.txt +++ b/host/lib/CMakeLists.txt @@ -57,8 +57,9 @@ SET(libuhd_sources transport/udp_simple.cpp usrp/dboard_base.cpp usrp/dboard_eeprom.cpp - usrp/simple_usrp.cpp + usrp/dboard_id.cpp usrp/dboard_manager.cpp + usrp/simple_usrp.cpp usrp/tune_helper.cpp ) diff --git a/host/lib/device.cpp b/host/lib/device.cpp index 88bd2cff4..f139ecb20 100644 --- a/host/lib/device.cpp +++ b/host/lib/device.cpp @@ -105,14 +105,14 @@ device::sptr device::make(const device_addr_t &hint, size_t which){ //check that we found any devices if (dev_addr_makers.size() == 0){ throw std::runtime_error(str( - boost::format("No devices found for ----->\n%s") % hint.to_string() + boost::format("No devices found for ----->\n%s") % hint.to_pp_string() )); } //check that the which index is valid if (dev_addr_makers.size() <= which){ throw std::runtime_error(str( - boost::format("No device at index %d for ----->\n%s") % which % hint.to_string() + boost::format("No device at index %d for ----->\n%s") % which % hint.to_pp_string() )); } diff --git a/host/lib/types.cpp b/host/lib/types.cpp index 14f7651d4..ec9c8ac01 100644 --- a/host/lib/types.cpp +++ b/host/lib/types.cpp @@ -143,7 +143,7 @@ device_addr_t::device_addr_t(const std::string &args){ } } -std::string device_addr_t::to_string(void) const{ +std::string device_addr_t::to_pp_string(void) const{ if (this->size() == 0) return "Empty Device Address"; std::stringstream ss; @@ -153,7 +153,7 @@ std::string device_addr_t::to_string(void) const{ return ss.str(); } -std::string device_addr_t::to_args_str(void) const{ +std::string device_addr_t::to_string(void) const{ std::string args_str; BOOST_FOREACH(const std::string &key, this->keys()){ args_str += key + pair_delim + (*this)[key] + arg_delim; diff --git a/host/lib/usrp/dboard/db_basic_and_lf.cpp b/host/lib/usrp/dboard/db_basic_and_lf.cpp index ebe3b2616..23ac98872 100644 --- a/host/lib/usrp/dboard/db_basic_and_lf.cpp +++ b/host/lib/usrp/dboard/db_basic_and_lf.cpp @@ -101,7 +101,7 @@ void basic_rx::rx_get(const wax::obj &key_, wax::obj &val){ switch(key.as()){ case SUBDEV_PROP_NAME: val = std::string(str(boost::format("%s - %s") - % dboard_id::to_string(get_rx_id()) + % get_rx_id().to_pp_string() % get_subdev_name() )); return; @@ -202,7 +202,7 @@ void basic_tx::tx_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case SUBDEV_PROP_NAME: - val = dboard_id::to_string(get_tx_id()); + val = get_tx_id().to_pp_string(); return; case SUBDEV_PROP_OTHERS: diff --git a/host/lib/usrp/dboard/db_rfx.cpp b/host/lib/usrp/dboard/db_rfx.cpp index 07159cd09..bbc9716b1 100644 --- a/host/lib/usrp/dboard/db_rfx.cpp +++ b/host/lib/usrp/dboard/db_rfx.cpp @@ -351,7 +351,7 @@ void rfx_xcvr::rx_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case SUBDEV_PROP_NAME: - val = dboard_id::to_string(get_rx_id()); + val = get_rx_id().to_pp_string(); return; case SUBDEV_PROP_OTHERS: @@ -448,7 +448,7 @@ void rfx_xcvr::tx_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case SUBDEV_PROP_NAME: - val = dboard_id::to_string(get_tx_id()); + val = get_tx_id().to_pp_string(); return; case SUBDEV_PROP_OTHERS: diff --git a/host/lib/usrp/dboard/db_xcvr2450.cpp b/host/lib/usrp/dboard/db_xcvr2450.cpp index 9b0fcbc5c..3bf866fc8 100644 --- a/host/lib/usrp/dboard/db_xcvr2450.cpp +++ b/host/lib/usrp/dboard/db_xcvr2450.cpp @@ -444,7 +444,7 @@ void xcvr2450::rx_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case SUBDEV_PROP_NAME: - val = dboard_id::to_string(get_rx_id()); + val = get_rx_id().to_pp_string(); return; case SUBDEV_PROP_OTHERS: @@ -542,7 +542,7 @@ void xcvr2450::tx_get(const wax::obj &key_, wax::obj &val){ //handle the get request conditioned on the key switch(key.as()){ case SUBDEV_PROP_NAME: - val = dboard_id::to_string(get_tx_id()); + val = get_tx_id().to_pp_string(); return; case SUBDEV_PROP_OTHERS: diff --git a/host/lib/usrp/dboard_base.cpp b/host/lib/usrp/dboard_base.cpp index 523da696a..bd4b37ef3 100644 --- a/host/lib/usrp/dboard_base.cpp +++ b/host/lib/usrp/dboard_base.cpp @@ -58,15 +58,15 @@ dboard_id_t dboard_base::get_tx_id(void){ * xcvr dboard dboard_base class **********************************************************************/ xcvr_dboard_base::xcvr_dboard_base(ctor_args_t args) : dboard_base(args){ - if (get_rx_id() == dboard_id::NONE){ + if (get_rx_id() == dboard_id_t::none()){ throw std::runtime_error(str(boost::format( "cannot create xcvr board when the rx id is \"%s\"" - ) % dboard_id::to_string(dboard_id::NONE))); + ) % dboard_id_t::none().to_pp_string())); } - if (get_tx_id() == dboard_id::NONE){ + if (get_tx_id() == dboard_id_t::none()){ throw std::runtime_error(str(boost::format( "cannot create xcvr board when the tx id is \"%s\"" - ) % dboard_id::to_string(dboard_id::NONE))); + ) % dboard_id_t::none().to_pp_string())); } } @@ -78,11 +78,11 @@ xcvr_dboard_base::~xcvr_dboard_base(void){ * rx dboard dboard_base class **********************************************************************/ rx_dboard_base::rx_dboard_base(ctor_args_t args) : dboard_base(args){ - if (get_tx_id() != dboard_id::NONE){ + if (get_tx_id() != dboard_id_t::none()){ throw std::runtime_error(str(boost::format( "cannot create rx board when the tx id is \"%s\"" " -> expected a tx id of \"%s\"" - ) % dboard_id::to_string(get_tx_id()) % dboard_id::to_string(dboard_id::NONE))); + ) % get_tx_id().to_pp_string() % dboard_id_t::none().to_pp_string())); } } @@ -102,11 +102,11 @@ void rx_dboard_base::tx_set(const wax::obj &, const wax::obj &){ * tx dboard dboard_base class **********************************************************************/ tx_dboard_base::tx_dboard_base(ctor_args_t args) : dboard_base(args){ - if (get_rx_id() != dboard_id::NONE){ + if (get_rx_id() != dboard_id_t::none()){ throw std::runtime_error(str(boost::format( "cannot create tx board when the rx id is \"%s\"" " -> expected a rx id of \"%s\"" - ) % dboard_id::to_string(get_rx_id()) % dboard_id::to_string(dboard_id::NONE))); + ) % get_rx_id().to_pp_string() % dboard_id_t::none().to_pp_string())); } } diff --git a/host/lib/usrp/dboard_eeprom.cpp b/host/lib/usrp/dboard_eeprom.cpp index 54e7a4fd9..fa3631948 100644 --- a/host/lib/usrp/dboard_eeprom.cpp +++ b/host/lib/usrp/dboard_eeprom.cpp @@ -80,19 +80,20 @@ dboard_eeprom_t::dboard_eeprom_t(const byte_vector_t &bytes){ UHD_ASSERT_THROW(bytes.size() >= DB_EEPROM_CLEN); UHD_ASSERT_THROW(bytes[DB_EEPROM_MAGIC] == DB_EEPROM_MAGIC_VALUE); UHD_ASSERT_THROW(bytes[DB_EEPROM_CHKSUM] == checksum(bytes)); - id = \ - (boost::uint16_t(bytes[DB_EEPROM_ID_LSB]) << 0) | - (boost::uint16_t(bytes[DB_EEPROM_ID_MSB]) << 8) ; + id = dboard_id_t::from_uint16(0 + | (boost::uint16_t(bytes[DB_EEPROM_ID_LSB]) << 0) + | (boost::uint16_t(bytes[DB_EEPROM_ID_MSB]) << 8) + ); }catch(const uhd::assert_error &){ - id = dboard_id::NONE; + id = dboard_id_t::none(); } } byte_vector_t dboard_eeprom_t::get_eeprom_bytes(void){ byte_vector_t bytes(DB_EEPROM_CLEN, 0); //defaults to all zeros bytes[DB_EEPROM_MAGIC] = DB_EEPROM_MAGIC_VALUE; - bytes[DB_EEPROM_ID_LSB] = boost::uint8_t(id >> 0); - bytes[DB_EEPROM_ID_MSB] = boost::uint8_t(id >> 8); + bytes[DB_EEPROM_ID_LSB] = boost::uint8_t(id.to_uint16() >> 0); + bytes[DB_EEPROM_ID_MSB] = boost::uint8_t(id.to_uint16() >> 8); bytes[DB_EEPROM_CHKSUM] = checksum(bytes); return bytes; } diff --git a/host/lib/usrp/dboard_id.cpp b/host/lib/usrp/dboard_id.cpp new file mode 100644 index 000000000..3028d2a3b --- /dev/null +++ b/host/lib/usrp/dboard_id.cpp @@ -0,0 +1,68 @@ +// +// Copyright 2010 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 . +// + +#include +#include +#include +#include +#include + +using namespace uhd::usrp; + +dboard_id_t::dboard_id_t(boost::uint16_t id){ + _id = id; +} + +dboard_id_t dboard_id_t::none(void){ + return dboard_id_t(); +} + +dboard_id_t dboard_id_t::from_uint16(boost::uint16_t uint16){ + return dboard_id_t(uint16); +} + +boost::uint16_t dboard_id_t::to_uint16(void) const{ + return _id; +} + +//used with lexical cast to parse a hex string +template struct to_hex{ + T value; + operator T() const {return value;} + friend std::istream& operator>>(std::istream& in, to_hex& out){ + in >> std::hex >> out.value; + return in; + } +}; + +dboard_id_t dboard_id_t::from_string(const std::string &string){ + if (string.substr(0, 2) == "0x"){ + return dboard_id_t::from_uint16(boost::lexical_cast >(string)); + } + return dboard_id_t::from_uint16(boost::lexical_cast(string)); +} + +std::string dboard_id_t::to_string(void) const{ + return str(boost::format("0x%04x") % this->to_uint16()); +} + +//Note: to_pp_string is implemented in the dboard manager +//because it needs access to the dboard registration table + +bool uhd::usrp::operator==(const dboard_id_t &lhs, const dboard_id_t &rhs){ + return lhs.to_uint16() == rhs.to_uint16(); +} diff --git a/host/lib/usrp/dboard_manager.cpp b/host/lib/usrp/dboard_manager.cpp index 80bf2dccb..4f5d0a473 100644 --- a/host/lib/usrp/dboard_manager.cpp +++ b/host/lib/usrp/dboard_manager.cpp @@ -51,15 +51,18 @@ void dboard_manager::register_dboard( //std::cout << "registering: " << name << std::endl; if (get_id_to_args_map().has_key(dboard_id)){ throw std::runtime_error(str(boost::format( - "The dboard id 0x%04x is already registered to %s." - ) % dboard_id % dboard_id::to_string(dboard_id))); + "The dboard id %s is already registered to %s." + ) % dboard_id.to_string() % dboard_id.to_pp_string())); } get_id_to_args_map()[dboard_id] = args_t(dboard_ctor, name, subdev_names); } -std::string dboard_id::to_string(const dboard_id_t &id){ - std::string name = (get_id_to_args_map().has_key(id))? get_id_to_args_map()[id].get<1>() : "unknown"; - return str(boost::format("%s (0x%04x)") % name % id); +std::string dboard_id_t::to_pp_string(void) const{ + std::string name = "unknown"; + if (get_id_to_args_map().has_key(*this)){ + name = get_id_to_args_map()[*this].get<1>(); + } + return str(boost::format("%s (%s)") % name % this->to_string()); } /*********************************************************************** @@ -168,11 +171,11 @@ static args_t get_dboard_args( dboard_id_t dboard_id ){ //special case, the none id was provided, use the following ids - if (dboard_id == dboard_id::NONE){ + if (dboard_id == dboard_id_t::none()){ std::cerr << boost::format( "Warning: unregistered dboard id: %s" " -> defaulting to a basic board" - ) % dboard_id::to_string(dboard_id) << std::endl; + ) % dboard_id.to_pp_string() << std::endl; UHD_ASSERT_THROW(get_id_to_args_map().has_key(0x0001)); UHD_ASSERT_THROW(get_id_to_args_map().has_key(0x0000)); switch(unit){ @@ -184,7 +187,7 @@ static args_t get_dboard_args( //verify that there is a registered constructor for this id if (not get_id_to_args_map().has_key(dboard_id)){ - return get_dboard_args(unit, dboard_id::NONE); + return get_dboard_args(unit, dboard_id_t::none()); } //return the dboard args for this id @@ -235,7 +238,7 @@ dboard_manager_impl::dboard_manager_impl( //make the rx subdevs BOOST_FOREACH(const std::string &subdev, rx_subdevs){ db_ctor_args.sd_name = subdev; - db_ctor_args.tx_id = dboard_id::NONE; + db_ctor_args.tx_id = dboard_id_t::none(); dboard_base::sptr rx_dboard = rx_dboard_ctor(&db_ctor_args); //create a rx proxy for this rx board _rx_dboards[subdev] = subdev_proxy::sptr( @@ -245,7 +248,7 @@ dboard_manager_impl::dboard_manager_impl( //make the tx subdevs BOOST_FOREACH(const std::string &subdev, tx_subdevs){ db_ctor_args.sd_name = subdev; - db_ctor_args.rx_id = dboard_id::NONE; + db_ctor_args.rx_id = dboard_id_t::none(); dboard_base::sptr tx_dboard = tx_dboard_ctor(&db_ctor_args); //create a tx proxy for this tx board _tx_dboards[subdev] = subdev_proxy::sptr( diff --git a/host/test/addr_test.cpp b/host/test/addr_test.cpp index 93b7cc0df..0c50200d6 100644 --- a/host/test/addr_test.cpp +++ b/host/test/addr_test.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -41,8 +42,8 @@ BOOST_AUTO_TEST_CASE(test_device_addr){ dev_addr["key2"] = "val2"; //convert to and from args string - std::cout << "Pretty Print: " << std::endl << dev_addr.to_string(); - std::string args_str = dev_addr.to_args_str(); + std::cout << "Pretty Print: " << std::endl << dev_addr.to_pp_string(); + std::string args_str = dev_addr.to_string(); std::cout << "Args String: " << args_str << std::endl; uhd::device_addr_t new_dev_addr(args_str); @@ -65,3 +66,15 @@ BOOST_AUTO_TEST_CASE(test_device_addr){ new_dev_addr_vals.begin(), new_dev_addr_vals.end() ); } + +BOOST_AUTO_TEST_CASE(test_dboard_id){ + std::cout << "Testing dboard id..." << std::endl; + + using namespace uhd::usrp; + + BOOST_CHECK(dboard_id_t() == dboard_id_t::none()); + BOOST_CHECK_EQUAL(dboard_id_t().to_uint16(), dboard_id_t::none().to_uint16()); + BOOST_CHECK_EQUAL(dboard_id_t::from_string("0x1234").to_uint16(), 0x1234); + BOOST_CHECK_EQUAL(dboard_id_t::from_string("1234").to_uint16(), 1234); + std::cout << "Pretty Print: " << std::endl << dboard_id_t::none().to_pp_string(); +} diff --git a/host/utils/uhd_burn_db_eeprom.cpp b/host/utils/uhd_burn_db_eeprom.cpp index c07b43f16..dfd9decba 100644 --- a/host/utils/uhd_burn_db_eeprom.cpp +++ b/host/utils/uhd_burn_db_eeprom.cpp @@ -24,11 +24,9 @@ #include #include #include -#include #include #include #include -#include using namespace uhd; using namespace uhd::usrp; @@ -89,14 +87,14 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ if (vm.count("id") == 0){ std::cout << boost::format("Getting dbid on %s dboard...") % prefix << std::endl; dboard_id_t id = dboard[DBOARD_PROP_DBOARD_ID].as(); - std::cout << boost::format(" Current dbid: %s") % dboard_id::to_string(id) << std::endl; + std::cout << boost::format(" Current dbid: %s") % id.to_pp_string() << std::endl; } //write a new dboard id to eeprom else{ - dboard_id_t id = boost::lexical_cast >(vm["id"].as()); + dboard_id_t id = dboard_id_t::from_string(vm["id"].as()); std::cout << boost::format("Setting dbid on %s dboard...") % prefix << std::endl; - std::cout << boost::format(" New dbid: %s") % dboard_id::to_string(id) << std::endl; + std::cout << boost::format(" New dbid: %s") % id.to_pp_string() << std::endl; dboard[DBOARD_PROP_DBOARD_ID] = id; } diff --git a/host/utils/uhd_find_devices.cpp b/host/utils/uhd_find_devices.cpp index 69e550fd4..b778eeb68 100644 --- a/host/utils/uhd_find_devices.cpp +++ b/host/utils/uhd_find_devices.cpp @@ -52,7 +52,7 @@ int UHD_SAFE_MAIN(int argc, char *argv[]){ std::cout << "--------------------------------------------------" << std::endl; std::cout << "-- UHD Device " << i << std::endl; std::cout << "--------------------------------------------------" << std::endl; - std::cout << device_addrs[i].to_string() << std::endl << std::endl; + std::cout << device_addrs[i].to_pp_string() << std::endl << std::endl; //uhd::device::make(device_addrs[i]); //test make } -- cgit v1.2.3