3 * Copyright 2008 Free Software Foundation, Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <usrp2/usrp2.h>
24 #include "usrp2_impl.h"
26 #include <boost/thread.hpp>
27 #include <boost/weak_ptr.hpp>
33 // --- Table of weak pointers to usrps we know about ---
35 // (Could be cleaned up and turned into a template)
37 struct usrp_table_entry {
38 // inteface + normalized mac addr ("eth0:01:23:45:67:89:ab")
40 boost::weak_ptr<usrp2::usrp2> value;
42 usrp_table_entry(const std::string &_key, boost::weak_ptr<usrp2::usrp2> _value)
43 : key(_key), value(_value) {}
46 typedef std::vector<usrp_table_entry> usrp_table;
48 static boost::mutex s_table_mutex;
49 static usrp_table s_table;
52 usrp2::find_existing_or_make_new(const std::string &ifc, props *pr)
54 std::string key = ifc + ":" + pr->addr;
56 boost::mutex::scoped_lock guard(s_table_mutex);
58 for (usrp_table::iterator p = s_table.begin(); p != s_table.end();){
59 if (p->value.expired()) // weak pointer is now dead
60 p = s_table.erase(p); // erase it
62 if (key == p->key) // found it
63 return usrp2::sptr(p->value);
69 // We don't have the USRP2 we're looking for
71 // create a new one and stick it in the table.
72 usrp2::sptr r(new usrp2::usrp2(ifc, pr));
73 usrp_table_entry t(key, r);
79 // --- end of table code ---
82 parse_mac_addr(const std::string &s, std::string &ns)
86 p.addr[0] = 0x00; // Matt's IAB
97 if (sscanf(s.c_str(), "%hhx:%hhx", &p.addr[4], &p.addr[5]) != 2)
102 if (sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
103 &p.addr[0], &p.addr[1], &p.addr[2],
104 &p.addr[3], &p.addr[4], &p.addr[5]) != 6)
113 snprintf(buf, sizeof(buf),
114 "%02x:%02x:%02x:%02x:%02x:%02x",
115 p.addr[0],p.addr[1],p.addr[2],
116 p.addr[3],p.addr[4],p.addr[5]);
117 ns = std::string(buf);
122 usrp2::make(const std::string &ifc, const std::string &addr)
124 std::string naddr = "";
125 if (addr != "" && !parse_mac_addr(addr, naddr))
126 throw std::runtime_error("Invalid MAC address");
128 props_vector_t u2s = find(ifc, naddr);
133 throw std::runtime_error("No USRPs found on interface " + ifc);
135 throw std::runtime_error("No USRP found with addr " + addr + " on interface " + ifc);
139 throw std::runtime_error("Multiple USRPs found on interface; must select by MAC address.");
141 return find_existing_or_make_new(ifc, &u2s[0]);
144 // Private constructor. Sole function is to create an impl.
145 usrp2::usrp2(const std::string &ifc, props *p)
146 : d_impl(new usrp2::impl(ifc, p))
151 // Public class destructor. d_impl will auto-delete.
160 return d_impl->mac_addr();
166 usrp2::set_rx_gain(double gain)
168 return d_impl->set_rx_gain(gain);
174 return d_impl->rx_gain_min();
180 return d_impl->rx_gain_max();
184 usrp2::rx_gain_db_per_step()
186 return d_impl->rx_gain_db_per_step();
190 usrp2::set_rx_center_freq(double frequency, tune_result *result)
192 return d_impl->set_rx_center_freq(frequency, result);
198 return d_impl->rx_freq_min();
204 return d_impl->rx_freq_max();
208 usrp2::set_rx_decim(int decimation_factor)
210 return d_impl->set_rx_decim(decimation_factor);
216 return d_impl->rx_decim();
220 usrp2::set_rx_scale_iq(int scale_i, int scale_q)
222 return d_impl->set_rx_scale_iq(scale_i, scale_q);
226 usrp2::start_rx_streaming(unsigned int channel, unsigned int items_per_frame)
228 return d_impl->start_rx_streaming(channel, items_per_frame);
232 usrp2::rx_samples(unsigned int channel, rx_sample_handler *handler)
234 return d_impl->rx_samples(channel, handler);
238 usrp2::stop_rx_streaming(unsigned int channel)
240 return d_impl->stop_rx_streaming(channel);
246 return d_impl->rx_overruns();
252 return d_impl->rx_missing();
258 usrp2::set_tx_gain(double gain)
260 return d_impl->set_tx_gain(gain);
266 return d_impl->tx_gain_min();
272 return d_impl->tx_gain_max();
276 usrp2::tx_gain_db_per_step()
278 return d_impl->tx_gain_db_per_step();
282 usrp2::set_tx_center_freq(double frequency, tune_result *result)
284 return d_impl->set_tx_center_freq(frequency, result);
290 return d_impl->tx_freq_min();
296 return d_impl->tx_freq_max();
301 usrp2::set_tx_interp(int interpolation_factor)
303 return d_impl->set_tx_interp(interpolation_factor);
309 return d_impl->tx_interp();
313 usrp2::default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q)
315 d_impl->default_tx_scale_iq(interpolation_factor, scale_i, scale_q);
319 usrp2::set_tx_scale_iq(int scale_i, int scale_q)
321 return d_impl->set_tx_scale_iq(scale_i, scale_q);
325 usrp2::tx_32fc(unsigned int channel,
326 const std::complex<float> *samples,
328 const tx_metadata *metadata)
330 return d_impl->tx_32fc(channel, samples, nsamples, metadata);
334 usrp2::tx_16sc(unsigned int channel,
335 const std::complex<int16_t> *samples,
337 const tx_metadata *metadata)
339 return d_impl->tx_16sc(channel, samples, nsamples, metadata);
343 usrp2::tx_raw(unsigned int channel,
344 const uint32_t *items,
346 const tx_metadata *metadata)
348 return d_impl->tx_raw(channel, items, nitems, metadata);
351 // miscellaneous methods
354 usrp2::config_mimo(int flags)
356 return d_impl->config_mimo(flags);
360 usrp2::fpga_master_clock_freq(long *freq)
362 return d_impl->fpga_master_clock_freq(freq);
366 usrp2::adc_rate(long *rate)
368 return d_impl->adc_rate(rate);
372 usrp2::dac_rate(long *rate)
374 return d_impl->dac_rate(rate);
378 usrp2::tx_daughterboard_id(int *dbid)
380 return d_impl->tx_daughterboard_id(dbid);
384 usrp2::rx_daughterboard_id(int *dbid)
386 return d_impl->rx_daughterboard_id(dbid);
393 usrp2::burn_mac_addr(const std::string &new_addr)
395 return d_impl->burn_mac_addr(new_addr);
401 return d_impl->sync_to_pps();
404 std::vector<uint32_t>
405 usrp2::peek32(uint32_t addr, uint32_t words)
407 return d_impl->peek32(addr, words);
411 usrp2::poke32(uint32_t addr, const std::vector<uint32_t> &data)
413 return d_impl->poke32(addr, data);
419 std::ostream& operator<<(std::ostream &os, const usrp2::props &x)
424 snprintf(buf, sizeof(buf)," hw_rev = 0x%04x", x.hw_rev);