aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/device.cpp
blob: d0aa0f20081ff574948196982d75ca7b9c31fc73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Copyright 2010-2011,2014-2015 Ettus Research LLC
//
// SPDX-License-Identifier: GPL-3.0
//

#include <uhd/device.hpp>
#include <uhd/types/dict.hpp>
#include <uhd/exception.hpp>
#include <uhd/utils/log.hpp>

#include <uhd/utils/static.hpp>
#include <uhd/utils/algorithm.hpp>
#include <boost/format.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/functional/hash.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/thread/mutex.hpp>

using namespace uhd;

static boost::mutex _device_mutex;

/***********************************************************************
 * Helper Functions
 **********************************************************************/
/*!
 * 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;

    // 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"
    };

    if(dev_addr.has_key("resource")) {
        boost::hash_combine(hash, "resource");
        boost::hash_combine(hash, dev_addr["resource"]);
    }
    else {
        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]);
            }
        }
    }
    return hash;
}

/***********************************************************************
 * Registration
 **********************************************************************/
typedef boost::tuple<device::find_t, device::make_t, device::device_filter_t> dev_fcn_reg_t;

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

void device::register_device(
    const find_t &find,
    const make_t &make,
    const device_filter_t filter
){
    // UHD_LOGGER_TRACE("UHD") << "registering device";
    get_dev_fcn_regs().push_back(dev_fcn_reg_t(find, make, filter));
}

device::~device(void){
    /* NOP */
}

/***********************************************************************
 * Discover
 **********************************************************************/
device_addrs_t device::find(const device_addr_t &hint, device_filter_t filter){
    boost::mutex::scoped_lock lock(_device_mutex);

    device_addrs_t device_addrs;

    for(const dev_fcn_reg_t &fcn:  get_dev_fcn_regs()) {
        try {
            if (filter == ANY or fcn.get<2>() == filter) {
                device_addrs_t discovered_addrs = fcn.get<0>()(hint);
                device_addrs.insert(
                    device_addrs.begin(),
                    discovered_addrs.begin(),
                    discovered_addrs.end()
                );
            }
        }
        catch (const std::exception &e) {
            UHD_LOGGER_ERROR("UHD") << "Device discovery error: " << e.what();
        }
    }

    return device_addrs;
}

/***********************************************************************
 * Make
 **********************************************************************/
device::sptr device::make(const device_addr_t &hint, device_filter_t filter, size_t which){
    boost::mutex::scoped_lock lock(_device_mutex);

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

    for(const dev_fcn_reg_t &fcn:  get_dev_fcn_regs()){
        try{
            if(filter == ANY or fcn.get<2>() == filter){
                for(device_addr_t dev_addr:  fcn.get<0>()(hint)){
                    //append the discovered address and its factory function
                    dev_addr_makers.push_back(dev_addr_make_t(dev_addr, fcn.get<1>()));
                }
            }
        }
        catch(const std::exception &e){
            UHD_LOGGER_ERROR("UHD") << "Device discovery error: " << e.what() ;
        }
    }

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

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

    //create a unique hash for the device address
    device_addr_t dev_addr; make_t maker;
    boost::tie(dev_addr, maker) = dev_addr_makers.at(which);
    size_t dev_hash = hash_device_addr(dev_addr);
    UHD_LOGGER_TRACE("UHD") << boost::format("Device hash: %u") % dev_hash ;

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

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

    //try to find an existing device
    if (hash_to_device.has_key(dev_hash) and not hash_to_device[dev_hash].expired()){
        return hash_to_device[dev_hash].lock();
    }
    else {
        //create and register a new device
        device::sptr dev = maker(dev_addr);
        hash_to_device[dev_hash] = dev;
        return dev;
    }
}

uhd::property_tree::sptr
device::get_tree(void) const
{
    return _tree;
}

device::device_filter_t device::get_device_type() const {
    return _type;
}