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