1 /* -*- c++ -*- */
2 /*
3 * Copyright 2015-2017 Josh Blum <josh@joshknows.com>
4 * Copyright 2013 Dimitri Stolnikov <horiz0n@gmx.net>
5 *
6 * GNU Radio is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3, or (at your option)
9 * any later version.
10 *
11 * GNU Radio is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Radio; see the file COPYING. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /*
23 * config.h is generated by configure. It contains the results
24 * of probing for features, options etc. It should be the first
25 * file included in your .cc file.
26 */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <iostream>
32 #include <algorithm> //find
33
34 #include <boost/assign.hpp>
35 #include <boost/format.hpp>
36 #include <boost/lexical_cast.hpp>
37
38 #include <gnuradio/io_signature.h>
39
40 #include "arg_helpers.h"
41 #include "soapy_sink_c.h"
42 #include "soapy_common.h"
43 #include <SoapySDR/Device.hpp>
44 #include <SoapySDR/Version.hpp>
45
46 using namespace boost::assign;
47
48 /*
49 * Create a new instance of soapy_sink_c and return
50 * a boost shared_ptr. This is effectively the public constructor.
51 */
make_soapy_sink_c(const std::string & args)52 soapy_sink_c_sptr make_soapy_sink_c (const std::string &args)
53 {
54 return gnuradio::get_initial_sptr(new soapy_sink_c (args));
55 }
56
57 /*
58 * The private constructor
59 */
soapy_sink_c(const std::string & args)60 soapy_sink_c::soapy_sink_c (const std::string &args)
61 : gr::sync_block ("soapy_sink_c",
62 args_to_io_signature(args),
63 gr::io_signature::make (0, 0, 0))
64 {
65 {
66 std::lock_guard<std::mutex> l(get_soapy_maker_mutex());
67 _device = SoapySDR::Device::make(params_to_dict(args));
68 }
69 _nchan = std::max(1, args_to_io_signature(args)->max_streams());
70 std::vector<size_t> channels;
71 for (size_t i = 0; i < _nchan; i++) channels.push_back(i);
72 _stream = _device->setupStream(SOAPY_SDR_TX, "CF32", channels);
73 }
74
~soapy_sink_c(void)75 soapy_sink_c::~soapy_sink_c(void)
76 {
77 _device->closeStream(_stream);
78 std::lock_guard<std::mutex> l(get_soapy_maker_mutex());
79 SoapySDR::Device::unmake(_device);
80 }
81
start()82 bool soapy_sink_c::start()
83 {
84 return _device->activateStream(_stream) == 0;
85 }
86
stop()87 bool soapy_sink_c::stop()
88 {
89 return _device->deactivateStream(_stream) == 0;
90 }
91
work(int noutput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)92 int soapy_sink_c::work( int noutput_items,
93 gr_vector_const_void_star &input_items,
94 gr_vector_void_star &output_items )
95 {
96 int flags = 0;
97 long long timeNs = 0;
98 int ret = _device->writeStream(
99 _stream, &input_items[0],
100 noutput_items, flags, timeNs);
101
102 if (ret < 0) return 0; //call again
103 return ret;
104 }
105
get_devices()106 std::vector<std::string> soapy_sink_c::get_devices()
107 {
108 std::vector<std::string> result;
109 int i = 0;
110 for (SoapySDR::Kwargs kw : SoapySDR::Device::enumerate())
111 {
112 kw["soapy"] = boost::lexical_cast<std::string>(i++);
113 result.push_back(dict_to_args_string(kw));
114 }
115 return result;
116 }
117
get_num_channels(void)118 size_t soapy_sink_c::get_num_channels( void )
119 {
120 return _nchan;
121 }
122
get_sample_rates(void)123 osmosdr::meta_range_t soapy_sink_c::get_sample_rates( void )
124 {
125 osmosdr::meta_range_t result;
126 #ifdef SOAPY_SDR_API_HAS_GET_SAMPLE_RATE_RANGE
127 for (const SoapySDR::Range &r : _device->getSampleRateRange(SOAPY_SDR_TX, 0))
128 {
129 result.push_back(osmosdr::range_t(r.minimum(), r.maximum()));
130 }
131 #else
132 for (const double rate : _device->listSampleRates(SOAPY_SDR_TX, 0))
133 {
134 result.push_back(osmosdr::range_t(rate));
135 }
136 #endif
137 return result;
138 }
139
set_sample_rate(double rate)140 double soapy_sink_c::set_sample_rate( double rate )
141 {
142 _device->setSampleRate(SOAPY_SDR_TX, 0, rate);
143 return this->get_sample_rate();
144 }
145
get_sample_rate(void)146 double soapy_sink_c::get_sample_rate( void )
147 {
148 return _device->getSampleRate(SOAPY_SDR_TX, 0);
149 }
150
get_freq_range(size_t chan)151 osmosdr::freq_range_t soapy_sink_c::get_freq_range( size_t chan)
152 {
153 osmosdr::meta_range_t result;
154 for (const SoapySDR::Range r : _device->getFrequencyRange(SOAPY_SDR_TX, 0))
155 {
156 result.push_back(osmosdr::range_t(r.minimum(), r.maximum()));
157 }
158 return result;
159 }
160
set_center_freq(double freq,size_t chan)161 double soapy_sink_c::set_center_freq( double freq, size_t chan)
162 {
163 _device->setFrequency(SOAPY_SDR_TX, chan, freq);
164 return this->get_center_freq(chan);
165 }
166
get_center_freq(size_t chan)167 double soapy_sink_c::get_center_freq( size_t chan)
168 {
169 return _device->getFrequency(SOAPY_SDR_TX, chan);
170 }
171
set_freq_corr(double ppm,size_t chan)172 double soapy_sink_c::set_freq_corr( double ppm, size_t chan)
173 {
174 #ifdef SOAPY_SDR_API_HAS_FREQUENCY_CORRECTION_API
175 _device->setFrequencyCorrection(SOAPY_SDR_TX, chan, ppm);
176 #else
177 std::vector<std::string> components = _device->listFrequencies(SOAPY_SDR_TX, chan);
178 if (std::find(components.begin(), components.end(), "CORR") != components.end())
179 {
180 _device->setFrequency(SOAPY_SDR_TX, chan, "CORR", ppm);
181 }
182 #endif
183 return this->get_freq_corr(chan);
184 }
185
get_freq_corr(size_t chan)186 double soapy_sink_c::get_freq_corr( size_t chan)
187 {
188 #ifdef SOAPY_SDR_API_HAS_FREQUENCY_CORRECTION_API
189 return _device->getFrequencyCorrection(SOAPY_SDR_TX, chan);
190 #else
191 std::vector<std::string> components = _device->listFrequencies(SOAPY_SDR_TX, chan);
192 if (std::find(components.begin(), components.end(), "CORR") != components.end())
193 {
194 return _device->getFrequency(SOAPY_SDR_TX, chan, "CORR");
195 }
196 return 0.0;
197 #endif
198 }
199
get_gain_names(size_t chan)200 std::vector<std::string> soapy_sink_c::get_gain_names( size_t chan)
201 {
202 return _device->listGains(SOAPY_SDR_TX, chan);
203 }
204
get_gain_range(size_t chan)205 osmosdr::gain_range_t soapy_sink_c::get_gain_range( size_t chan)
206 {
207 SoapySDR::Range r = _device->getGainRange(SOAPY_SDR_TX, chan);
208 return soapy_range_to_gain_range(r);
209 }
210
get_gain_range(const std::string & name,size_t chan)211 osmosdr::gain_range_t soapy_sink_c::get_gain_range( const std::string & name,
212 size_t chan)
213 {
214 SoapySDR::Range r = _device->getGainRange(SOAPY_SDR_TX, chan, name);
215 return soapy_range_to_gain_range(r);
216 }
217
set_gain_mode(bool automatic,size_t chan)218 bool soapy_sink_c::set_gain_mode( bool automatic, size_t chan)
219 {
220 _device->setGainMode(SOAPY_SDR_TX, chan, automatic);
221 return this->get_gain_mode(chan);
222 }
223
get_gain_mode(size_t chan)224 bool soapy_sink_c::get_gain_mode( size_t chan)
225 {
226 return _device->getGainMode(SOAPY_SDR_TX, chan);
227 }
228
set_gain(double gain,size_t chan)229 double soapy_sink_c::set_gain( double gain, size_t chan)
230 {
231 _device->setGain(SOAPY_SDR_TX, chan, gain);
232 return this->get_gain(chan);
233 }
234
set_gain(double gain,const std::string & name,size_t chan)235 double soapy_sink_c::set_gain( double gain,
236 const std::string & name,
237 size_t chan)
238 {
239 _device->setGain(SOAPY_SDR_TX, chan, name, gain);
240 return this->get_gain(name, chan);
241 }
242
get_gain(size_t chan)243 double soapy_sink_c::get_gain( size_t chan)
244 {
245 return _device->getGain(SOAPY_SDR_TX, chan);
246 }
247
get_gain(const std::string & name,size_t chan)248 double soapy_sink_c::get_gain( const std::string & name, size_t chan)
249 {
250 return _device->getGain(SOAPY_SDR_TX, chan, name);
251 }
252
set_if_gain(double gain,size_t chan)253 double soapy_sink_c::set_if_gain( double gain, size_t chan)
254 {
255 //docs specify RF gain is the first element
256 const std::string name = this->get_gain_names(chan).front();
257 return this->set_gain(gain, name, chan);
258 }
259
set_bb_gain(double gain,size_t chan)260 double soapy_sink_c::set_bb_gain( double gain, size_t chan)
261 {
262 //docs specify baseband gain is the last element
263 const std::string name = this->get_gain_names(chan).back();
264 return this->set_gain(gain, name, chan);
265 }
266
get_antennas(size_t chan)267 std::vector< std::string > soapy_sink_c::get_antennas( size_t chan)
268 {
269 return _device->listAntennas(SOAPY_SDR_TX, chan);
270 }
271
set_antenna(const std::string & antenna,size_t chan)272 std::string soapy_sink_c::set_antenna( const std::string & antenna,
273 size_t chan)
274 {
275 _device->setAntenna(SOAPY_SDR_TX, chan, antenna);
276 return this->get_antenna(chan);
277 }
278
get_antenna(size_t chan)279 std::string soapy_sink_c::get_antenna( size_t chan)
280 {
281 return _device->getAntenna(SOAPY_SDR_TX, chan);
282 }
283
set_dc_offset(const std::complex<double> & offset,size_t chan)284 void soapy_sink_c::set_dc_offset( const std::complex<double> &offset, size_t chan)
285 {
286 _device->setDCOffset(SOAPY_SDR_TX, chan, offset);
287 }
288
set_iq_balance(const std::complex<double> & balance,size_t chan)289 void soapy_sink_c::set_iq_balance( const std::complex<double> &balance, size_t chan)
290 {
291 _device->setIQBalance(SOAPY_SDR_TX, chan, balance);
292 }
293
set_bandwidth(double bandwidth,size_t chan)294 double soapy_sink_c::set_bandwidth( double bandwidth, size_t chan)
295 {
296 if ( bandwidth == 0.0 ) /* bandwidth of 0 means automatic filter selection */
297 set_bandwidth(get_sample_rate() * 0.75, chan); /* select narrower filters to prevent aliasing */
298
299 _device->setBandwidth(SOAPY_SDR_TX, chan, bandwidth);
300 return this->get_bandwidth(chan);
301 }
302
get_bandwidth(size_t chan)303 double soapy_sink_c::get_bandwidth( size_t chan)
304 {
305 return _device->getBandwidth(SOAPY_SDR_TX, chan);
306 }
307
get_bandwidth_range(size_t chan)308 osmosdr::freq_range_t soapy_sink_c::get_bandwidth_range( size_t chan)
309 {
310 osmosdr::meta_range_t result;
311 #ifdef SOAPY_SDR_API_HAS_GET_BANDWIDTH_RANGE
312 for (const SoapySDR::Range &r : _device->getBandwidthRange(SOAPY_SDR_TX, 0))
313 {
314 result.push_back(osmosdr::range_t(r.minimum(), r.maximum()));
315 }
316 #else
317 for (const double bw : _device->listBandwidths(SOAPY_SDR_TX, 0))
318 {
319 result.push_back(osmosdr::range_t(bw));
320 }
321 #endif
322 return result;
323 }
324
set_time_source(const std::string & source,const size_t)325 void soapy_sink_c::set_time_source(const std::string &source,
326 const size_t)
327 {
328 _device->setTimeSource(source);
329 }
330
get_time_source(const size_t)331 std::string soapy_sink_c::get_time_source(const size_t)
332 {
333 return _device->getTimeSource();
334 }
335
get_time_sources(const size_t)336 std::vector<std::string> soapy_sink_c::get_time_sources(const size_t)
337 {
338 return _device->listTimeSources();
339 }
340
set_clock_source(const std::string & source,const size_t)341 void soapy_sink_c::set_clock_source(const std::string &source,
342 const size_t)
343 {
344 _device->setClockSource(source);
345 }
346
get_clock_source(const size_t)347 std::string soapy_sink_c::get_clock_source(const size_t)
348 {
349 return _device->getClockSource();
350 }
351
get_clock_sources(const size_t)352 std::vector<std::string> soapy_sink_c::get_clock_sources(const size_t)
353 {
354 return _device->listClockSources();
355 }
356
get_clock_rate(size_t)357 double soapy_sink_c::get_clock_rate(size_t)
358 {
359 return _device->getMasterClockRate();
360 }
361
set_clock_rate(double rate,size_t)362 void soapy_sink_c::set_clock_rate(double rate, size_t)
363 {
364 _device->setMasterClockRate(rate);
365 }
366
get_time_now(size_t)367 ::osmosdr::time_spec_t soapy_sink_c::get_time_now(size_t)
368 {
369 return ::osmosdr::time_spec_t::from_ticks(_device->getHardwareTime(), 1e6);
370 }
371
get_time_last_pps(size_t)372 ::osmosdr::time_spec_t soapy_sink_c::get_time_last_pps(size_t)
373 {
374 return ::osmosdr::time_spec_t::from_ticks(_device->getHardwareTime("PPS"), 1e6);
375 }
376
set_time_now(const::osmosdr::time_spec_t & time_spec,size_t)377 void soapy_sink_c::set_time_now(const ::osmosdr::time_spec_t &time_spec,
378 size_t)
379 {
380 _device->setHardwareTime(time_spec.to_ticks(1e9));
381 }
382
set_time_next_pps(const::osmosdr::time_spec_t & time_spec)383 void soapy_sink_c::set_time_next_pps(const ::osmosdr::time_spec_t &time_spec)
384 {
385 _device->setHardwareTime(time_spec.to_ticks(1e9), "PPS");
386 }
387
set_time_unknown_pps(const::osmosdr::time_spec_t & time_spec)388 void soapy_sink_c::set_time_unknown_pps(const ::osmosdr::time_spec_t &time_spec)
389 {
390 _device->setHardwareTime(time_spec.to_ticks(1e9), "UNKNOWN_PPS");
391 }
392
393