Statistics
| Branch: | Tag: | Revision:

root / host / lib / device.cpp @ 9734a743

History | View | Annotate | Download (5.25 KB)

1
//
2
// Copyright 2010-2011 Ettus Research LLC
3
//
4
// 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
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
//
17

    
18
#include <uhd/device.hpp>
19
#include <uhd/types/dict.hpp>
20
#include <uhd/exception.hpp>
21
#include <uhd/utils/log.hpp>
22
#include <uhd/utils/msg.hpp>
23
#include <uhd/utils/static.hpp>
24
#include <uhd/utils/algorithm.hpp>
25
#include <boost/foreach.hpp>
26
#include <boost/format.hpp>
27
#include <boost/weak_ptr.hpp>
28
#include <boost/functional/hash.hpp>
29
#include <boost/tuple/tuple.hpp>
30
#include <boost/thread/mutex.hpp>
31

    
32
using namespace uhd;
33

    
34
static boost::mutex _device_mutex;
35

    
36
/***********************************************************************
37
 * Helper Functions
38
 **********************************************************************/
39
/*!
40
 * Make a device hash that maps 1 to 1 with a device address.
41
 * The hash will be used to identify created devices.
42
 * \param dev_addr the device address
43
 * \return the hash number
44
 */
45
static size_t hash_device_addr(
46
    const device_addr_t &dev_addr
47
){
48
    //combine the hashes of sorted keys/value pairs
49
    size_t hash = 0;
50
    BOOST_FOREACH(const std::string &key, uhd::sorted(dev_addr.keys())){
51
        boost::hash_combine(hash, key);
52
        boost::hash_combine(hash, dev_addr[key]);
53
    }
54
    return hash;
55
}
56

    
57
/***********************************************************************
58
 * Registration
59
 **********************************************************************/
60
typedef boost::tuple<device::find_t, device::make_t> dev_fcn_reg_t;
61

    
62
// instantiate the device function registry container
63
UHD_SINGLETON_FCN(std::vector<dev_fcn_reg_t>, get_dev_fcn_regs)
64

    
65
void device::register_device(
66
    const find_t &find,
67
    const make_t &make
68
){
69
    UHD_LOGV(always) << "registering device" << std::endl;
70
    get_dev_fcn_regs().push_back(dev_fcn_reg_t(find, make));
71
}
72

    
73
/***********************************************************************
74
 * Discover
75
 **********************************************************************/
76
device_addrs_t device::find(const device_addr_t &hint){
77
    boost::mutex::scoped_lock lock(_device_mutex);
78

    
79
    device_addrs_t device_addrs;
80

    
81
    BOOST_FOREACH(const dev_fcn_reg_t &fcn, get_dev_fcn_regs()){
82
        try{
83
            device_addrs_t discovered_addrs = fcn.get<0>()(hint);
84
            device_addrs.insert(
85
                device_addrs.begin(),
86
                discovered_addrs.begin(),
87
                discovered_addrs.end()
88
            );
89
        }
90
        catch(const std::exception &e){
91
            UHD_MSG(error) << "Device discovery error: " << e.what() << std::endl;
92
        }
93
    }
94

    
95
    return device_addrs;
96
}
97

    
98
/***********************************************************************
99
 * Make
100
 **********************************************************************/
101
device::sptr device::make(const device_addr_t &hint, size_t which){
102
    boost::mutex::scoped_lock lock(_device_mutex);
103

    
104
    typedef boost::tuple<device_addr_t, make_t> dev_addr_make_t;
105
    std::vector<dev_addr_make_t> dev_addr_makers;
106

    
107
    BOOST_FOREACH(const dev_fcn_reg_t &fcn, get_dev_fcn_regs()){
108
        BOOST_FOREACH(device_addr_t dev_addr, fcn.get<0>()(hint)){
109
            //append the discovered address and its factory function
110
            dev_addr_makers.push_back(dev_addr_make_t(dev_addr, fcn.get<1>()));
111
        }
112
    }
113

    
114
    //check that we found any devices
115
    if (dev_addr_makers.size() == 0){
116
        throw uhd::key_error(str(
117
            boost::format("No devices found for ----->\n%s") % hint.to_pp_string()
118
        ));
119
    }
120

    
121
    //check that the which index is valid
122
    if (dev_addr_makers.size() <= which){
123
        throw uhd::index_error(str(
124
            boost::format("No device at index %d for ----->\n%s") % which % hint.to_pp_string()
125
        ));
126
    }
127

    
128
    //create a unique hash for the device address
129
    device_addr_t dev_addr; make_t maker;
130
    boost::tie(dev_addr, maker) = dev_addr_makers.at(which);
131
    size_t dev_hash = hash_device_addr(dev_addr);
132
    UHD_LOG << boost::format("Device hash: %u") % dev_hash << std::endl;
133

    
134
    //copy keys that were in hint but not in dev_addr
135
    //this way, we can pass additional transport arguments
136
    BOOST_FOREACH(const std::string &key, hint.keys()){
137
        if (not dev_addr.has_key(key)) dev_addr[key] = hint[key];
138
    }
139

    
140
    //map device address hash to created devices
141
    static uhd::dict<size_t, boost::weak_ptr<device> > hash_to_device;
142

    
143
    //try to find an existing device
144
    try{
145
        UHD_ASSERT_THROW(hash_to_device.has_key(dev_hash));
146
        UHD_ASSERT_THROW(not hash_to_device[dev_hash].expired());
147
        return hash_to_device[dev_hash].lock();
148
    }
149
    //create and register a new device
150
    catch(const uhd::assertion_error &){
151
        device::sptr dev = maker(dev_addr);
152
        hash_to_device[dev_hash] = dev;
153
        return dev;
154
    }
155
}