Statistics
| Branch: | Tag: | Revision:

root / host / lib / device.cpp @ ed5cb33e

History | View | Annotate | Download (4.99 KB)

1 5b42b774 Josh Blum
//
2
// Copyright 2010 Ettus Research LLC
3
//
4 aa2c904d Josh Blum
// This program is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15 5715b2c4 Josh Blum
// asize_t with this program.  If not, see <http://www.gnu.org/licenses/>.
16 aa2c904d Josh Blum
//
17 5b42b774 Josh Blum
18 7590f187 Josh Blum
#include <uhd/device.hpp>
19 b71d0cbe Josh Blum
#include <uhd/types/dict.hpp>
20 52df9afd Josh Blum
#include <uhd/utils/assert.hpp>
21
#include <uhd/utils/static.hpp>
22 5715b2c4 Josh Blum
#include <boost/foreach.hpp>
23 5b42b774 Josh Blum
#include <boost/format.hpp>
24 5715b2c4 Josh Blum
#include <boost/weak_ptr.hpp>
25
#include <boost/functional/hash.hpp>
26 7590f187 Josh Blum
#include <boost/tuple/tuple.hpp>
27 e3483c74 Josh Blum
#include <stdexcept>
28 5715b2c4 Josh Blum
#include <algorithm>
29 5b42b774 Josh Blum
30 c5480830 Josh Blum
using namespace uhd;
31 5b42b774 Josh Blum
32 7590f187 Josh Blum
/***********************************************************************
33
 * Helper Functions
34
 **********************************************************************/
35 5715b2c4 Josh Blum
/*!
36
 * Make a device hash that maps 1 to 1 with a device address.
37
 * The hash will be used to identify created devices.
38
 * \param dev_addr the device address
39
 * \return the hash number
40
 */
41
static size_t hash_device_addr(
42
    const device_addr_t &dev_addr
43
){
44
    //sort the keys of the device address
45 ed5cb33e Josh Blum
    std::vector<std::string> keys = dev_addr.keys();
46 5715b2c4 Josh Blum
    std::sort(keys.begin(), keys.end());
47
48
    //combine the hashes of sorted keys/value pairs
49
    size_t hash = 0;
50 1295df8c Josh Blum
    BOOST_FOREACH(const std::string &key, keys){
51 5715b2c4 Josh Blum
        boost::hash_combine(hash, key);
52
        boost::hash_combine(hash, dev_addr[key]);
53
    }
54
    return hash;
55
}
56
57
/***********************************************************************
58 7590f187 Josh Blum
 * Registration
59
 **********************************************************************/
60 28130783 Josh Blum
typedef boost::tuple<device::find_t, device::make_t> dev_fcn_reg_t;
61 7590f187 Josh Blum
62
// instantiate the device function registry container
63 52df9afd Josh Blum
UHD_SINGLETON_FCN(std::vector<dev_fcn_reg_t>, get_dev_fcn_regs)
64 7590f187 Josh Blum
65
void device::register_device(
66 28130783 Josh Blum
    const find_t &find,
67 7590f187 Josh Blum
    const make_t &make
68
){
69
    //std::cout << "registering device" << std::endl;
70 28130783 Josh Blum
    get_dev_fcn_regs().push_back(dev_fcn_reg_t(find, make));
71 7590f187 Josh Blum
}
72
73
/***********************************************************************
74 5715b2c4 Josh Blum
 * Discover
75
 **********************************************************************/
76 28130783 Josh Blum
device_addrs_t device::find(const device_addr_t &hint){
77 9fff25f4 Josh Blum
    device_addrs_t device_addrs;
78 5715b2c4 Josh Blum
79 1295df8c Josh Blum
    BOOST_FOREACH(const dev_fcn_reg_t &fcn, get_dev_fcn_regs()){
80 7590f187 Josh Blum
        device_addrs_t discovered_addrs = fcn.get<0>()(hint);
81
        device_addrs.insert(
82
            device_addrs.begin(),
83
            discovered_addrs.begin(),
84
            discovered_addrs.end()
85
        );
86 5e455ca9 Josh Blum
    }
87 5715b2c4 Josh Blum
88 5b42b774 Josh Blum
    return device_addrs;
89
}
90
91 5715b2c4 Josh Blum
/***********************************************************************
92
 * Make
93
 **********************************************************************/
94 9fff25f4 Josh Blum
device::sptr device::make(const device_addr_t &hint, size_t which){
95 7590f187 Josh Blum
    typedef boost::tuple<device_addr_t, make_t> dev_addr_make_t;
96
    std::vector<dev_addr_make_t> dev_addr_makers;
97
98 1295df8c Josh Blum
    BOOST_FOREACH(const dev_fcn_reg_t &fcn, get_dev_fcn_regs()){
99 7590f187 Josh Blum
        BOOST_FOREACH(device_addr_t dev_addr, fcn.get<0>()(hint)){
100
            //copy keys that were in hint but not in dev_addr
101
            //this way, we can pass additional transport arguments
102 ed5cb33e Josh Blum
            BOOST_FOREACH(const std::string &key, hint.keys()){
103 7590f187 Josh Blum
                if (not dev_addr.has_key(key)) dev_addr[key] = hint[key];
104
            }
105
            //append the discovered address and its factory function
106
            dev_addr_makers.push_back(dev_addr_make_t(dev_addr, fcn.get<1>()));
107
        }
108
    }
109 069a7ce0 Josh Blum
110 5b42b774 Josh Blum
    //check that we found any devices
111 7590f187 Josh Blum
    if (dev_addr_makers.size() == 0){
112 5b42b774 Josh Blum
        throw std::runtime_error(str(
113 b71d0cbe Josh Blum
            boost::format("No devices found for ----->\n%s") % hint.to_string()
114 5b42b774 Josh Blum
        ));
115
    }
116 069a7ce0 Josh Blum
117 5b42b774 Josh Blum
    //check that the which index is valid
118 7590f187 Josh Blum
    if (dev_addr_makers.size() <= which){
119 5b42b774 Josh Blum
        throw std::runtime_error(str(
120 b71d0cbe Josh Blum
            boost::format("No device at index %d for ----->\n%s") % which % hint.to_string()
121 5b42b774 Josh Blum
        ));
122
    }
123 069a7ce0 Josh Blum
124 5715b2c4 Josh Blum
    //create a unique hash for the device address
125 7590f187 Josh Blum
    device_addr_t dev_addr; make_t maker;
126
    boost::tie(dev_addr, maker) = dev_addr_makers.at(which);
127 5715b2c4 Josh Blum
    size_t dev_hash = hash_device_addr(dev_addr);
128
    //std::cout << boost::format("Hash: %u") % dev_hash << std::endl;
129 5b42b774 Josh Blum
130 5715b2c4 Josh Blum
    //map device address hash to created devices
131
    static uhd::dict<size_t, boost::weak_ptr<device> > hash_to_device;
132 5b42b774 Josh Blum
133 5715b2c4 Josh Blum
    //try to find an existing device
134
    try{
135
        ASSERT_THROW(hash_to_device.has_key(dev_hash));
136
        ASSERT_THROW(not hash_to_device[dev_hash].expired());
137
        return hash_to_device[dev_hash].lock();
138
    }
139
    //create and register a new device
140 52df9afd Josh Blum
    catch(const uhd::assert_error &){
141 7590f187 Josh Blum
        device::sptr dev = maker(dev_addr);
142 5715b2c4 Josh Blum
        hash_to_device[dev_hash] = dev;
143
        return dev;
144
    }
145 5b42b774 Josh Blum
}