Statistics
| Branch: | Tag: | Revision:

root / host / lib / device.cpp @ b31cefc5

History | View | Annotate | Download (5.2 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/static.hpp>
22
#include <uhd/utils/algorithm.hpp>
23
#include <boost/foreach.hpp>
24
#include <boost/format.hpp>
25
#include <boost/weak_ptr.hpp>
26
#include <boost/functional/hash.hpp>
27
#include <boost/tuple/tuple.hpp>
28
#include <boost/thread/mutex.hpp>
29
#include <iostream>
30

    
31
using namespace uhd;
32

    
33
static boost::mutex _device_mutex;
34

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

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

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

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

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

    
78
    device_addrs_t device_addrs;
79

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

    
94
    return device_addrs;
95
}
96

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

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

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

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

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

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

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

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

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