]> git.gag.com Git - debian/gnuradio/blob - usrp2/host/lib/usrp2.cc
Renamed identifiers for consistency: s/complex_float/32fc/ s/complex_16/16sc/.
[debian/gnuradio] / usrp2 / host / lib / usrp2.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 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   bool
164   usrp2::burn_mac_addr(const std::string &new_addr)
165   {
166     return d_impl->burn_mac_addr(new_addr);
167   }
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   bool
179   usrp2::set_rx_center_freq(double frequency, tune_result *result)
180   {
181     return d_impl->set_rx_center_freq(frequency, result);
182   }
183   
184   bool
185   usrp2::set_rx_decim(int decimation_factor)
186   {
187     return d_impl->set_rx_decim(decimation_factor);
188   }
189   
190   bool
191   usrp2::set_rx_scale_iq(int scale_i, int scale_q)
192   {
193     return d_impl->set_rx_scale_iq(scale_i, scale_q);
194   }
195   
196   bool
197   usrp2::start_rx_streaming(unsigned int channel, unsigned int items_per_frame)
198   {
199     return d_impl->start_rx_streaming(channel, items_per_frame);
200   }
201   
202   bool
203   usrp2::rx_samples(unsigned int channel, rx_sample_handler *handler)
204   {
205     return d_impl->rx_samples(channel, handler);
206   }
207
208   bool
209   usrp2::stop_rx_streaming(unsigned int channel)
210   {
211     return d_impl->stop_rx_streaming(channel);
212   }
213
214   unsigned int
215   usrp2::rx_overruns()
216   {
217     return d_impl->rx_overruns();
218   }
219   
220   unsigned int
221   usrp2::rx_missing()
222   {
223     return d_impl->rx_missing();
224   }
225
226   // Transmit
227
228   bool 
229   usrp2::set_tx_gain(double gain)
230   {
231     return d_impl->set_tx_gain(gain);
232   }
233   
234   bool
235   usrp2::set_tx_center_freq(double frequency, tune_result *result)
236   {
237     return d_impl->set_tx_center_freq(frequency, result);
238   }
239   
240   bool
241   usrp2::set_tx_interp(int interpolation_factor)
242   {
243     return d_impl->set_tx_interp(interpolation_factor);
244   }
245   
246   bool
247   usrp2::set_tx_scale_iq(int scale_i, int scale_q)
248   {
249     return d_impl->set_tx_scale_iq(scale_i, scale_q);
250   }
251   
252   bool
253   usrp2::tx_32fc(unsigned int channel,
254                  const std::complex<float> *samples,
255                  size_t nsamples,
256                  const tx_metadata *metadata)
257   {
258     return d_impl->tx_32fc(channel, samples, nsamples, metadata);
259   }
260
261   bool
262   usrp2::tx_16sc(unsigned int channel,
263                  const std::complex<int16_t> *samples,
264                  size_t nsamples,
265                  const tx_metadata *metadata)
266   {
267     return d_impl->tx_16sc(channel, samples, nsamples, metadata);
268   }
269
270   bool
271   usrp2::tx_raw(unsigned int channel,
272                 const uint32_t *items,
273                 size_t nitems,
274                 const tx_metadata *metadata)
275   {
276     return d_impl->tx_raw(channel, items, nitems, metadata);
277   }
278
279 } // namespace usrp2
280
281
282 std::ostream& operator<<(std::ostream &os, const usrp2::props &x)
283 {
284   os << x.addr;
285
286   char buf[128];
287   snprintf(buf, sizeof(buf)," hw_rev = 0x%04x", x.hw_rev);
288
289   os << buf;
290   return os;
291 }