Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / host / lib / usrp2.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009 Free Software Foundation, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <usrp2/usrp2.h>
24 #include "usrp2_impl.h"
25 #include <vector>
26 #include <boost/thread.hpp>
27 #include <boost/weak_ptr.hpp>
28 #include <string>
29 #include <stdexcept>
30 #include <cstdio>
31
32 namespace usrp2 {
33
34   // --- Table of weak pointers to usrps we know about ---
35
36   // (Could be cleaned up and turned into a template)
37
38   struct usrp_table_entry {
39     // inteface + normalized mac addr ("eth0:01:23:45:67:89:ab")
40     std::string key;
41     boost::weak_ptr<usrp2::usrp2>  value;
42
43     usrp_table_entry(const std::string &_key, boost::weak_ptr<usrp2::usrp2> _value)
44       : key(_key), value(_value) {}
45   };
46
47   typedef std::vector<usrp_table_entry> usrp_table;
48
49   static boost::mutex s_table_mutex;
50   static usrp_table s_table;
51
52   usrp2::sptr
53   usrp2::find_existing_or_make_new(const std::string &ifc, props *pr, size_t rx_bufsize)
54   {
55     std::string key = ifc + ":" + pr->addr;
56
57     boost::mutex::scoped_lock   guard(s_table_mutex);
58
59     for (usrp_table::iterator p = s_table.begin(); p != s_table.end();){
60       if (p->value.expired())   // weak pointer is now dead
61         p = s_table.erase(p);   // erase it
62       else {
63         if (key == p->key)      // found it
64           return usrp2::sptr(p->value);
65         else                    
66           ++p;                  // keep looking
67       }
68     }
69
70     // We don't have the USRP2 we're looking for
71
72     // create a new one and stick it in the table.
73     usrp2::sptr r(new usrp2::usrp2(ifc, pr, rx_bufsize));
74     usrp_table_entry t(key, r);
75     s_table.push_back(t);
76
77     return r;
78   }
79
80   // --- end of table code ---
81
82   static bool
83   parse_mac_addr(const std::string &s, std::string &ns)
84   {
85     u2_mac_addr_t p;
86
87     p.addr[0] = 0x00;           // Matt's IAB
88     p.addr[1] = 0x50;
89     p.addr[2] = 0xC2;
90     p.addr[3] = 0x85;
91     p.addr[4] = 0x30;
92     p.addr[5] = 0x00;
93     
94     int len = s.size();
95     switch (len) {
96       
97     case 5:
98       if (sscanf(s.c_str(), "%hhx:%hhx", &p.addr[4], &p.addr[5]) != 2)
99         return false;
100       break;
101       
102     case 17:
103       if (sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
104                  &p.addr[0], &p.addr[1], &p.addr[2],
105                  &p.addr[3], &p.addr[4], &p.addr[5]) != 6)
106         return false;
107       break;
108
109     default:
110       return false;
111     }
112     
113     char buf[128];
114     snprintf(buf, sizeof(buf),
115              "%02x:%02x:%02x:%02x:%02x:%02x",
116              p.addr[0],p.addr[1],p.addr[2],
117              p.addr[3],p.addr[4],p.addr[5]);
118     ns = std::string(buf);
119     return true;
120   }
121
122   usrp2::sptr
123   usrp2::make(const std::string &ifc, const std::string &addr, size_t rx_bufsize)
124   {
125     std::string naddr = "";
126     if (addr != "" && !parse_mac_addr(addr, naddr))
127       throw std::runtime_error("Invalid MAC address");
128
129     props_vector_t u2s = find(ifc, naddr);
130     int n = u2s.size();
131
132     if (n == 0) {
133       if (addr == "")
134         throw std::runtime_error("No USRPs found on interface " + ifc);
135       else
136         throw std::runtime_error("No USRP found with addr " + addr + " on interface " + ifc);
137     }
138
139     if (n > 1)
140       throw std::runtime_error("Multiple USRPs found on interface; must select by MAC address.");
141
142     return find_existing_or_make_new(ifc, &u2s[0], rx_bufsize);
143   }
144
145   // Private constructor.  Sole function is to create an impl.
146   usrp2::usrp2(const std::string &ifc, props *p, size_t rx_bufsize)
147     : d_impl(new usrp2::impl(ifc, p, rx_bufsize))
148   {
149     // NOP
150   }
151   
152   // Public class destructor.  d_impl will auto-delete.
153   usrp2::~usrp2()
154   {
155     // NOP
156   }
157   
158   std::string
159   usrp2::mac_addr()
160   {
161     return d_impl->mac_addr();
162   }
163
164   std::string
165   usrp2::interface_name()
166   {
167     return d_impl->interface_name();
168   }
169
170   // Receive
171
172   bool 
173   usrp2::set_rx_gain(double gain)
174   {
175     return d_impl->set_rx_gain(gain);
176   }
177   
178   double
179   usrp2::rx_gain_min()
180   {
181     return d_impl->rx_gain_min();
182   }
183
184   double
185   usrp2::rx_gain_max()
186   {
187     return d_impl->rx_gain_max();
188   }
189
190   double
191   usrp2::rx_gain_db_per_step()
192   {
193     return d_impl->rx_gain_db_per_step();
194   }
195
196   bool
197   usrp2::set_rx_lo_offset(double frequency)
198   {
199     return d_impl->set_rx_lo_offset(frequency);
200   }
201
202   bool
203   usrp2::set_rx_center_freq(double frequency, tune_result *result)
204   {
205     return d_impl->set_rx_center_freq(frequency, result);
206   }
207   
208   double
209   usrp2::rx_freq_min()
210   {
211     return d_impl->rx_freq_min();
212   }
213
214   double
215   usrp2::rx_freq_max()
216   {
217     return d_impl->rx_freq_max();
218   }
219
220   bool
221   usrp2::set_rx_decim(int decimation_factor)
222   {
223     return d_impl->set_rx_decim(decimation_factor);
224   }
225   
226   int
227   usrp2::rx_decim()
228   {
229     return d_impl->rx_decim();
230   }
231
232   bool
233   usrp2::set_rx_scale_iq(int scale_i, int scale_q)
234   {
235     return d_impl->set_rx_scale_iq(scale_i, scale_q);
236   }
237   
238   bool
239   usrp2::start_rx_streaming(unsigned int channel, unsigned int items_per_frame)
240   {
241     return d_impl->start_rx_streaming(channel, items_per_frame);
242   }
243   
244   bool
245   usrp2::rx_samples(unsigned int channel, rx_sample_handler *handler)
246   {
247     return d_impl->rx_samples(channel, handler);
248   }
249
250   bool
251   usrp2::stop_rx_streaming(unsigned int channel)
252   {
253     return d_impl->stop_rx_streaming(channel);
254   }
255
256   unsigned int
257   usrp2::rx_overruns()
258   {
259     return d_impl->rx_overruns();
260   }
261   
262   unsigned int
263   usrp2::rx_missing()
264   {
265     return d_impl->rx_missing();
266   }
267
268   // Transmit
269
270   bool 
271   usrp2::set_tx_gain(double gain)
272   {
273     return d_impl->set_tx_gain(gain);
274   }
275   
276   double
277   usrp2::tx_gain_min()
278   {
279     return d_impl->tx_gain_min();
280   }
281
282   double
283   usrp2::tx_gain_max()
284   {
285     return d_impl->tx_gain_max();
286   }
287
288   double
289   usrp2::tx_gain_db_per_step()
290   {
291     return d_impl->tx_gain_db_per_step();
292   }
293
294   bool
295   usrp2::set_tx_lo_offset(double frequency)
296   {
297     return d_impl->set_tx_lo_offset(frequency);
298   }
299
300   bool
301   usrp2::set_tx_center_freq(double frequency, tune_result *result)
302   {
303     return d_impl->set_tx_center_freq(frequency, result);
304   }
305   
306   double
307   usrp2::tx_freq_min()
308   {
309     return d_impl->tx_freq_min();
310   }
311
312   double
313   usrp2::tx_freq_max()
314   {
315     return d_impl->tx_freq_max();
316   }
317
318
319   bool
320   usrp2::set_tx_interp(int interpolation_factor)
321   {
322     return d_impl->set_tx_interp(interpolation_factor);
323   }
324   
325   int
326   usrp2::tx_interp()
327   {
328     return d_impl->tx_interp();
329   }
330
331   void
332   usrp2::default_tx_scale_iq(int interpolation_factor, int *scale_i, int *scale_q)
333   {
334     d_impl->default_tx_scale_iq(interpolation_factor, scale_i, scale_q);
335   }
336
337   bool
338   usrp2::set_tx_scale_iq(int scale_i, int scale_q)
339   {
340     return d_impl->set_tx_scale_iq(scale_i, scale_q);
341   }
342   
343   bool
344   usrp2::tx_32fc(unsigned int channel,
345                  const std::complex<float> *samples,
346                  size_t nsamples,
347                  const tx_metadata *metadata)
348   {
349     return d_impl->tx_32fc(channel, samples, nsamples, metadata);
350   }
351
352   bool
353   usrp2::tx_16sc(unsigned int channel,
354                  const std::complex<int16_t> *samples,
355                  size_t nsamples,
356                  const tx_metadata *metadata)
357   {
358     return d_impl->tx_16sc(channel, samples, nsamples, metadata);
359   }
360
361   bool
362   usrp2::tx_raw(unsigned int channel,
363                 const uint32_t *items,
364                 size_t nitems,
365                 const tx_metadata *metadata)
366   {
367     return d_impl->tx_raw(channel, items, nitems, metadata);
368   }
369
370   // miscellaneous methods
371
372   bool
373   usrp2::config_mimo(int flags)
374   {
375     return d_impl->config_mimo(flags);
376   }
377
378   bool
379   usrp2::fpga_master_clock_freq(long *freq)
380   {
381     return d_impl->fpga_master_clock_freq(freq);
382   }
383
384   bool
385   usrp2::adc_rate(long *rate)
386   {
387     return d_impl->adc_rate(rate);
388   }
389
390   bool
391   usrp2::dac_rate(long *rate)
392   {
393     return d_impl->dac_rate(rate);
394   }
395
396   bool
397   usrp2::tx_daughterboard_id(int *dbid)
398   {
399     return d_impl->tx_daughterboard_id(dbid);
400   }
401
402   bool
403   usrp2::rx_daughterboard_id(int *dbid)
404   {
405     return d_impl->rx_daughterboard_id(dbid);
406   }
407   
408
409   // low level methods
410
411   bool
412   usrp2::burn_mac_addr(const std::string &new_addr)
413   {
414     return d_impl->burn_mac_addr(new_addr);
415   }
416
417   bool
418   usrp2::sync_to_pps()
419   {
420     return d_impl->sync_to_pps();
421   }
422
423   bool
424   usrp2::sync_every_pps(bool enable)
425   {
426     return d_impl->sync_every_pps(enable);
427   }
428
429   std::vector<uint32_t>
430   usrp2::peek32(uint32_t addr, uint32_t words)
431   {
432     return d_impl->peek32(addr, words);
433   }
434
435   bool
436   usrp2::poke32(uint32_t addr, const std::vector<uint32_t> &data)
437   {
438     return d_impl->poke32(addr, data);
439   }
440
441   bool
442   usrp2::set_gpio_ddr(int bank, uint16_t value, uint16_t mask)
443   {
444     return d_impl->set_gpio_ddr(bank, value, mask);
445   }
446
447   bool
448   usrp2::set_gpio_sels(int bank, std::string src)
449   {
450     return d_impl->set_gpio_sels(bank, src);
451   }
452
453   bool
454   usrp2::write_gpio(int bank, uint16_t value, uint16_t mask)
455   {
456     return d_impl->write_gpio(bank, value, mask);
457   }
458
459   bool
460   usrp2::read_gpio(int bank, uint16_t *value)
461   {
462     return d_impl->read_gpio(bank, value);
463   }
464
465   bool
466   usrp2::enable_gpio_streaming(int bank, int enable)
467   {
468     return d_impl->enable_gpio_streaming(bank, enable);
469   }
470
471 } // namespace usrp2
472
473 std::ostream& operator<<(std::ostream &os, const usrp2::props &x)
474 {
475   os << x.addr;
476
477   char buf[128];
478   snprintf(buf, sizeof(buf)," hw_rev = 0x%04x", x.hw_rev);
479
480   os << buf;
481   return os;
482 }