2010-01-17 00:29:31 +00:00
|
|
|
//
|
2015-02-27 18:44:26 +00:00
|
|
|
// Copyright 2010-2011,2014-2015 Ettus Research LLC
|
2010-01-17 00:29:31 +00:00
|
|
|
//
|
2017-12-22 17:45:24 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2010-01-30 00:40:23 +00:00
|
|
|
//
|
2010-01-17 00:29:31 +00:00
|
|
|
|
2010-03-15 19:15:33 +00:00
|
|
|
#include <uhd/device.hpp>
|
2010-03-27 21:27:55 +00:00
|
|
|
#include <uhd/types/dict.hpp>
|
2011-02-24 22:54:24 +00:00
|
|
|
#include <uhd/exception.hpp>
|
2011-05-05 03:40:36 +00:00
|
|
|
#include <uhd/utils/log.hpp>
|
2017-02-08 00:37:25 +00:00
|
|
|
|
2010-03-27 08:02:58 +00:00
|
|
|
#include <uhd/utils/static.hpp>
|
2010-06-14 17:44:50 +00:00
|
|
|
#include <uhd/utils/algorithm.hpp>
|
2010-01-17 00:29:31 +00:00
|
|
|
#include <boost/format.hpp>
|
2010-02-26 02:32:32 +00:00
|
|
|
#include <boost/weak_ptr.hpp>
|
|
|
|
|
#include <boost/functional/hash.hpp>
|
2010-03-15 19:15:33 +00:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2011-04-26 19:04:07 +00:00
|
|
|
#include <boost/thread/mutex.hpp>
|
2010-01-17 00:29:31 +00:00
|
|
|
|
2010-02-10 00:28:16 +00:00
|
|
|
using namespace uhd;
|
2010-01-17 00:29:31 +00:00
|
|
|
|
2011-04-26 19:04:07 +00:00
|
|
|
static boost::mutex _device_mutex;
|
|
|
|
|
|
2010-03-15 19:15:33 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Helper Functions
|
|
|
|
|
**********************************************************************/
|
2010-02-26 02:32:32 +00:00
|
|
|
/*!
|
|
|
|
|
* Make a device hash that maps 1 to 1 with a device address.
|
|
|
|
|
* The hash will be used to identify created devices.
|
|
|
|
|
* \param dev_addr the device address
|
|
|
|
|
* \return the hash number
|
|
|
|
|
*/
|
|
|
|
|
static size_t hash_device_addr(
|
|
|
|
|
const device_addr_t &dev_addr
|
|
|
|
|
){
|
|
|
|
|
//combine the hashes of sorted keys/value pairs
|
|
|
|
|
size_t hash = 0;
|
2014-10-11 00:24:40 +00:00
|
|
|
|
2017-11-11 01:01:24 +00:00
|
|
|
// The device addr can contain all sorts of stuff, which sometimes gets in
|
|
|
|
|
// the way of hashing reliably. TODO: Make this a whitelist
|
|
|
|
|
const std::vector<std::string> hash_key_blacklist = {
|
|
|
|
|
"claimed",
|
|
|
|
|
"skip_dram",
|
|
|
|
|
"skip_ddc",
|
|
|
|
|
"skip_duc"
|
|
|
|
|
};
|
|
|
|
|
|
2014-10-11 00:24:40 +00:00
|
|
|
if(dev_addr.has_key("resource")) {
|
|
|
|
|
boost::hash_combine(hash, "resource");
|
2016-04-13 22:46:25 +00:00
|
|
|
boost::hash_combine(hash, dev_addr["resource"]);
|
2014-10-11 00:24:40 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2017-11-11 01:01:24 +00:00
|
|
|
for (const std::string &key: uhd::sorted(dev_addr.keys())) {
|
|
|
|
|
if (std::find(hash_key_blacklist.begin(),
|
|
|
|
|
hash_key_blacklist.end(),
|
|
|
|
|
key) == hash_key_blacklist.end()) {
|
|
|
|
|
boost::hash_combine(hash, key);
|
|
|
|
|
boost::hash_combine(hash, dev_addr[key]);
|
|
|
|
|
}
|
2014-10-11 00:24:40 +00:00
|
|
|
}
|
2010-02-26 02:32:32 +00:00
|
|
|
}
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-15 19:15:33 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Registration
|
|
|
|
|
**********************************************************************/
|
2014-07-17 18:50:50 +00:00
|
|
|
typedef boost::tuple<device::find_t, device::make_t, device::device_filter_t> dev_fcn_reg_t;
|
2010-03-15 19:15:33 +00:00
|
|
|
|
|
|
|
|
// instantiate the device function registry container
|
2010-03-27 08:02:58 +00:00
|
|
|
UHD_SINGLETON_FCN(std::vector<dev_fcn_reg_t>, get_dev_fcn_regs)
|
2010-03-15 19:15:33 +00:00
|
|
|
|
|
|
|
|
void device::register_device(
|
2010-03-30 22:11:23 +00:00
|
|
|
const find_t &find,
|
2014-07-17 18:50:50 +00:00
|
|
|
const make_t &make,
|
|
|
|
|
const device_filter_t filter
|
2010-03-15 19:15:33 +00:00
|
|
|
){
|
2017-05-20 00:43:26 +00:00
|
|
|
// UHD_LOGGER_TRACE("UHD") << "registering device";
|
2014-07-17 18:50:50 +00:00
|
|
|
get_dev_fcn_regs().push_back(dev_fcn_reg_t(find, make, filter));
|
2010-03-15 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-13 15:44:31 +00:00
|
|
|
device::~device(void){
|
|
|
|
|
/* NOP */
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Discover
|
|
|
|
|
**********************************************************************/
|
2014-07-17 18:50:50 +00:00
|
|
|
device_addrs_t device::find(const device_addr_t &hint, device_filter_t filter){
|
2011-04-26 19:04:07 +00:00
|
|
|
boost::mutex::scoped_lock lock(_device_mutex);
|
|
|
|
|
|
2010-02-13 02:07:55 +00:00
|
|
|
device_addrs_t device_addrs;
|
2010-02-26 02:32:32 +00:00
|
|
|
|
2017-02-10 07:19:55 +00:00
|
|
|
for(const dev_fcn_reg_t &fcn: get_dev_fcn_regs()) {
|
2015-02-27 18:44:26 +00:00
|
|
|
try {
|
|
|
|
|
if (filter == ANY or fcn.get<2>() == filter) {
|
2014-07-17 18:50:50 +00:00
|
|
|
device_addrs_t discovered_addrs = fcn.get<0>()(hint);
|
|
|
|
|
device_addrs.insert(
|
|
|
|
|
device_addrs.begin(),
|
|
|
|
|
discovered_addrs.begin(),
|
|
|
|
|
discovered_addrs.end()
|
|
|
|
|
);
|
|
|
|
|
}
|
2010-10-01 18:54:22 +00:00
|
|
|
}
|
2015-02-27 18:44:26 +00:00
|
|
|
catch (const std::exception &e) {
|
2017-02-08 00:37:25 +00:00
|
|
|
UHD_LOGGER_ERROR("UHD") << "Device discovery error: " << e.what();
|
2010-10-01 18:54:22 +00:00
|
|
|
}
|
2010-01-30 03:22:40 +00:00
|
|
|
}
|
2010-02-26 02:32:32 +00:00
|
|
|
|
2010-01-17 00:29:31 +00:00
|
|
|
return device_addrs;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
/***********************************************************************
|
|
|
|
|
* Make
|
|
|
|
|
**********************************************************************/
|
2014-07-17 18:50:50 +00:00
|
|
|
device::sptr device::make(const device_addr_t &hint, device_filter_t filter, size_t which){
|
2011-04-26 19:04:07 +00:00
|
|
|
boost::mutex::scoped_lock lock(_device_mutex);
|
|
|
|
|
|
2010-03-15 19:15:33 +00:00
|
|
|
typedef boost::tuple<device_addr_t, make_t> dev_addr_make_t;
|
|
|
|
|
std::vector<dev_addr_make_t> dev_addr_makers;
|
|
|
|
|
|
2017-02-10 07:19:55 +00:00
|
|
|
for(const dev_fcn_reg_t &fcn: get_dev_fcn_regs()){
|
2015-02-27 18:44:26 +00:00
|
|
|
try{
|
|
|
|
|
if(filter == ANY or fcn.get<2>() == filter){
|
2017-02-10 07:19:55 +00:00
|
|
|
for(device_addr_t dev_addr: fcn.get<0>()(hint)){
|
2015-02-27 18:44:26 +00:00
|
|
|
//append the discovered address and its factory function
|
|
|
|
|
dev_addr_makers.push_back(dev_addr_make_t(dev_addr, fcn.get<1>()));
|
|
|
|
|
}
|
2014-07-17 18:50:50 +00:00
|
|
|
}
|
2010-03-15 19:15:33 +00:00
|
|
|
}
|
2015-02-27 18:44:26 +00:00
|
|
|
catch(const std::exception &e){
|
2017-02-08 00:37:25 +00:00
|
|
|
UHD_LOGGER_ERROR("UHD") << "Device discovery error: " << e.what() ;
|
2015-02-27 18:44:26 +00:00
|
|
|
}
|
2010-03-15 19:15:33 +00:00
|
|
|
}
|
2010-01-22 03:05:30 +00:00
|
|
|
|
2010-01-17 00:29:31 +00:00
|
|
|
//check that we found any devices
|
2010-03-15 19:15:33 +00:00
|
|
|
if (dev_addr_makers.size() == 0){
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::key_error(str(
|
2010-05-03 08:20:11 +00:00
|
|
|
boost::format("No devices found for ----->\n%s") % hint.to_pp_string()
|
2010-01-17 00:29:31 +00:00
|
|
|
));
|
|
|
|
|
}
|
2010-01-22 03:05:30 +00:00
|
|
|
|
2010-01-17 00:29:31 +00:00
|
|
|
//check that the which index is valid
|
2010-03-15 19:15:33 +00:00
|
|
|
if (dev_addr_makers.size() <= which){
|
2011-02-25 00:35:29 +00:00
|
|
|
throw uhd::index_error(str(
|
2010-05-03 08:20:11 +00:00
|
|
|
boost::format("No device at index %d for ----->\n%s") % which % hint.to_pp_string()
|
2010-01-17 00:29:31 +00:00
|
|
|
));
|
|
|
|
|
}
|
2010-01-22 03:05:30 +00:00
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
//create a unique hash for the device address
|
2010-03-15 19:15:33 +00:00
|
|
|
device_addr_t dev_addr; make_t maker;
|
|
|
|
|
boost::tie(dev_addr, maker) = dev_addr_makers.at(which);
|
2010-02-26 02:32:32 +00:00
|
|
|
size_t dev_hash = hash_device_addr(dev_addr);
|
2017-04-07 06:34:16 +00:00
|
|
|
UHD_LOGGER_TRACE("UHD") << boost::format("Device hash: %u") % dev_hash ;
|
2010-01-17 00:29:31 +00:00
|
|
|
|
2010-04-27 22:20:53 +00:00
|
|
|
//copy keys that were in hint but not in dev_addr
|
|
|
|
|
//this way, we can pass additional transport arguments
|
2017-02-10 07:19:55 +00:00
|
|
|
for(const std::string &key: hint.keys()){
|
2010-04-27 22:20:53 +00:00
|
|
|
if (not dev_addr.has_key(key)) dev_addr[key] = hint[key];
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
//map device address hash to created devices
|
|
|
|
|
static uhd::dict<size_t, boost::weak_ptr<device> > hash_to_device;
|
2010-01-17 00:29:31 +00:00
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
//try to find an existing device
|
2016-11-18 22:50:36 +00:00
|
|
|
if (hash_to_device.has_key(dev_hash) and not hash_to_device[dev_hash].expired()){
|
2010-02-26 02:32:32 +00:00
|
|
|
return hash_to_device[dev_hash].lock();
|
|
|
|
|
}
|
2016-11-18 22:50:36 +00:00
|
|
|
else {
|
|
|
|
|
//create and register a new device
|
2010-03-15 19:15:33 +00:00
|
|
|
device::sptr dev = maker(dev_addr);
|
2010-02-26 02:32:32 +00:00
|
|
|
hash_to_device[dev_hash] = dev;
|
|
|
|
|
return dev;
|
|
|
|
|
}
|
2010-01-17 00:29:31 +00:00
|
|
|
}
|
2014-02-04 19:04:07 +00:00
|
|
|
|
|
|
|
|
uhd::property_tree::sptr
|
|
|
|
|
device::get_tree(void) const
|
|
|
|
|
{
|
|
|
|
|
return _tree;
|
|
|
|
|
}
|
2014-07-17 18:50:50 +00:00
|
|
|
|
|
|
|
|
device::device_filter_t device::get_device_type() const {
|
|
|
|
|
return _type;
|
|
|
|
|
}
|