2010-01-17 00:29:31 +00:00
|
|
|
//
|
|
|
|
|
// Copyright 2010 Ettus Research LLC
|
|
|
|
|
//
|
2010-01-30 00:40:23 +00:00
|
|
|
// 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
|
2010-02-26 02:32:32 +00:00
|
|
|
// asize_t with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-30 00:40:23 +00:00
|
|
|
//
|
2010-01-17 00:29:31 +00:00
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
#include <uhd/usrp/usrp1e.hpp>
|
2010-02-22 06:15:30 +00:00
|
|
|
#include <uhd/usrp/usrp2.hpp>
|
2010-02-26 02:32:32 +00:00
|
|
|
#include <uhd/dict.hpp>
|
|
|
|
|
#include <uhd/utils.hpp>
|
|
|
|
|
#include <boost/foreach.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-01-21 23:30:34 +00:00
|
|
|
#include <stdexcept>
|
2010-02-26 02:32:32 +00:00
|
|
|
#include <algorithm>
|
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
|
|
|
|
2010-02-26 02:32:32 +00:00
|
|
|
/*!
|
|
|
|
|
* Create a new device from a device address.
|
|
|
|
|
* Based on the address, call the appropriate make functions.
|
|
|
|
|
* \param dev_addr the device address
|
2010-03-12 02:37:34 +00:00
|
|
|
* \param hint the device address that was used to find the device
|
2010-02-26 02:32:32 +00:00
|
|
|
* \return a smart pointer to a device
|
|
|
|
|
*/
|
2010-03-12 02:37:34 +00:00
|
|
|
static device::sptr make_device(const device_addr_t &dev_addr_, const device_addr_t &hint){
|
|
|
|
|
//copy keys that were in hint but not in dev_addr
|
|
|
|
|
//this way, we can pass additional transport arguments
|
|
|
|
|
device_addr_t dev_addr = dev_addr_;
|
|
|
|
|
BOOST_FOREACH(std::string key, hint.get_keys()){
|
|
|
|
|
if (not dev_addr.has_key(key)) dev_addr[key] = hint[key];
|
|
|
|
|
}
|
2010-02-26 02:32:32 +00:00
|
|
|
|
|
|
|
|
//create a usrp1e
|
|
|
|
|
if (dev_addr["type"] == "usrp1e"){
|
|
|
|
|
return usrp::usrp1e::make(dev_addr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//create a usrp2
|
|
|
|
|
if (dev_addr["type"] == "usrp2"){
|
|
|
|
|
return usrp::usrp2::make(dev_addr);
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-12 02:37:34 +00:00
|
|
|
throw std::runtime_error(str(
|
|
|
|
|
boost::format("Cant make a device for %s") % device_addr::to_string(dev_addr)
|
|
|
|
|
));
|
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
|
|
|
|
|
){
|
|
|
|
|
//sort the keys of the device address
|
|
|
|
|
std::vector<std::string> keys = dev_addr.get_keys();
|
|
|
|
|
std::sort(keys.begin(), keys.end());
|
|
|
|
|
|
|
|
|
|
//combine the hashes of sorted keys/value pairs
|
|
|
|
|
size_t hash = 0;
|
|
|
|
|
BOOST_FOREACH(std::string key, keys){
|
|
|
|
|
boost::hash_combine(hash, key);
|
|
|
|
|
boost::hash_combine(hash, dev_addr[key]);
|
|
|
|
|
}
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
|
* Discover
|
|
|
|
|
**********************************************************************/
|
2010-02-13 02:07:55 +00:00
|
|
|
device_addrs_t device::discover(const device_addr_t &hint){
|
|
|
|
|
device_addrs_t device_addrs;
|
2010-02-26 02:32:32 +00:00
|
|
|
|
|
|
|
|
//discover the usrp1es
|
|
|
|
|
std::vector<device_addr_t> usrp2_addrs = usrp::usrp1e::discover(hint);
|
|
|
|
|
device_addrs.insert(device_addrs.begin(), usrp2_addrs.begin(), usrp2_addrs.end());
|
|
|
|
|
|
|
|
|
|
//discover the usrp2s
|
2010-02-25 20:48:51 +00:00
|
|
|
if (hint.has_key("addr")){
|
2010-02-21 20:59:41 +00:00
|
|
|
std::vector<device_addr_t> usrp2_addrs = usrp::usrp2::discover(hint);
|
2010-01-30 03:22:40 +00:00
|
|
|
device_addrs.insert(device_addrs.begin(), usrp2_addrs.begin(), usrp2_addrs.end());
|
|
|
|
|
}
|
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
|
|
|
|
|
**********************************************************************/
|
2010-02-13 02:07:55 +00:00
|
|
|
device::sptr device::make(const device_addr_t &hint, size_t which){
|
2010-01-17 00:29:31 +00:00
|
|
|
std::vector<device_addr_t> device_addrs = discover(hint);
|
2010-01-22 03:05:30 +00:00
|
|
|
|
2010-01-17 00:29:31 +00:00
|
|
|
//check that we found any devices
|
|
|
|
|
if (device_addrs.size() == 0){
|
|
|
|
|
throw std::runtime_error(str(
|
2010-02-26 02:32:32 +00:00
|
|
|
boost::format("No devices found for %s") % device_addr::to_string(hint)
|
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
|
|
|
|
|
if (device_addrs.size() <= which){
|
|
|
|
|
throw std::runtime_error(str(
|
2010-02-26 02:32:32 +00:00
|
|
|
boost::format("No device at index %d for %s") % which % device_addr::to_string(hint)
|
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
|
|
|
|
|
device_addr_t dev_addr = device_addrs.at(which);
|
|
|
|
|
size_t dev_hash = hash_device_addr(dev_addr);
|
|
|
|
|
//std::cout << boost::format("Hash: %u") % dev_hash << std::endl;
|
2010-01-17 00:29:31 +00:00
|
|
|
|
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
|
|
|
|
|
try{
|
|
|
|
|
ASSERT_THROW(hash_to_device.has_key(dev_hash));
|
|
|
|
|
ASSERT_THROW(not hash_to_device[dev_hash].expired());
|
|
|
|
|
return hash_to_device[dev_hash].lock();
|
|
|
|
|
}
|
|
|
|
|
//create and register a new device
|
|
|
|
|
catch(const std::assert_error &e){
|
2010-03-12 02:37:34 +00:00
|
|
|
device::sptr dev = make_device(dev_addr, hint);
|
2010-02-26 02:32:32 +00:00
|
|
|
hash_to_device[dev_hash] = dev;
|
|
|
|
|
return dev;
|
|
|
|
|
}
|
2010-01-17 00:29:31 +00:00
|
|
|
}
|