aboutsummaryrefslogtreecommitdiffstats
path: root/lib/osmosdr_source_c_impl.cc
blob: c6aada3811f9052861e465ee82b1344ed221aa4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/* -*- c++ -*- */
/*
 * Copyright 2012 Dimitri Stolnikov <horiz0n@gmx.net>
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

/*
 * config.h is generated by configure.  It contains the results
 * of probing for features, options etc.  It should be the first
 * file included in your .cc file.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <osmosdr_source_c_impl.h>
#include <gr_io_signature.h>
#include <gr_noise_source_c.h>
#include <gr_throttle.h>
#include <gnuradio/gr_constants.h>

#ifdef ENABLE_OSMOSDR
#include <osmosdr_src_c.h>
#endif

#ifdef ENABLE_FCD
#include <fcd_source.h>
#endif

#ifdef ENABLE_FILE
#include <file_source_c.h>
#endif

#ifdef ENABLE_RTL
#include <rtl_source_c.h>
#endif

#ifdef ENABLE_RTL_TCP
#include <rtl_tcp_source_c.h>
#endif

#ifdef ENABLE_UHD
#include <uhd_source_c.h>
#endif

#ifdef ENABLE_MIRI
#include <miri_source_c.h>
#endif

#ifdef ENABLE_HACKRF
#include <hackrf_source_c.h>
#endif

#include <osmosdr_arg_helpers.h>

/* This avoids throws in ctor of gr_hier_block2, as gnuradio is unable to deal
 with this behavior in a clean way. The GR maintainer Rondeau has been informed. */
#define WORKAROUND_GR_HIER_BLOCK2_BUG

/*
 * Create a new instance of osmosdr_source_c_impl and return
 * a boost shared_ptr.  This is effectively the public constructor.
 */
osmosdr_source_c_sptr
osmosdr_make_source_c (const std::string &args)
{
  return gnuradio::get_initial_sptr(new osmosdr_source_c_impl (args));
}

/*
 * The private constructor
 */
osmosdr_source_c_impl::osmosdr_source_c_impl (const std::string &args)
  : gr_hier_block2 ("osmosdr_source_c_impl",
        gr_make_io_signature (0, 0, 0),
        args_to_io_signature(args))
{
  size_t channel = 0;
  bool device_specified = false;

  std::vector< std::string > arg_list = args_to_vector(args);

  std::vector< std::string > dev_types;

#ifdef ENABLE_FILE
  dev_types.push_back("file");
#endif
#ifdef ENABLE_OSMOSDR
  dev_types.push_back("osmosdr");
#endif
#ifdef ENABLE_FCD
  dev_types.push_back("fcd");
#endif
#ifdef ENABLE_RTL
  dev_types.push_back("rtl");
#endif
#ifdef ENABLE_RTL_TCP
  dev_types.push_back("rtl_tcp");
#endif
#ifdef ENABLE_UHD
  dev_types.push_back("uhd");
#endif
#ifdef ENABLE_MIRI
  dev_types.push_back("miri");
#endif
#ifdef ENABLE_HACKRF
  dev_types.push_back("hackrf");
#endif

  std::cerr << "gr-osmosdr "
            << GR_OSMOSDR_VERSION " (" GR_OSMOSDR_LIBVER ") "
            << "gnuradio " << gr_version() << std::endl;
  std::cerr << "built-in source types: ";
  BOOST_FOREACH(std::string dev_type, dev_types)
    std::cerr << dev_type << " ";
  std::cerr << std::endl << std::flush;

  BOOST_FOREACH(std::string arg, arg_list) {
    dict_t dict = params_to_dict(arg);
    BOOST_FOREACH(std::string dev_type, dev_types) {
      if ( dict.count( dev_type ) ) {
        device_specified = true;
        break;
      }
    }
  }
#ifdef WORKAROUND_GR_HIER_BLOCK2_BUG
  try {
#endif
  std::vector< std::string > dev_list;
#ifdef ENABLE_OSMOSDR
  BOOST_FOREACH( std::string dev, osmosdr_src_c::get_devices() )
    dev_list.push_back( dev );
#endif
#ifdef ENABLE_FCD
  BOOST_FOREACH( std::string dev, fcd_source::get_devices() )
    dev_list.push_back( dev );
#endif
#ifdef ENABLE_RTL
  BOOST_FOREACH( std::string dev, rtl_source_c::get_devices() )
    dev_list.push_back( dev );
#endif
#ifdef ENABLE_UHD
  BOOST_FOREACH( std::string dev, uhd_source_c::get_devices() )
    dev_list.push_back( dev );
#endif
#ifdef ENABLE_MIRI
  BOOST_FOREACH( std::string dev, miri_source_c::get_devices() )
    dev_list.push_back( dev );
#endif
#ifdef ENABLE_HACKRF
  BOOST_FOREACH( std::string dev, hackrf_source_c::get_devices() )
    dev_list.push_back( dev );
#endif
//  std::cerr << std::endl;
//  BOOST_FOREACH( std::string dev, dev_list )
//    std::cerr << "'" << dev << "'" << std::endl;

  if (!device_specified) {
    if ( dev_list.size() )
      arg_list.push_back( dev_list.front() );
    else
      throw std::runtime_error("No supported devices found to pick from.");
  }

  BOOST_FOREACH(std::string arg, arg_list) {

    dict_t dict = params_to_dict(arg);

//    std::cerr << std::endl;
//    BOOST_FOREACH( dict_t::value_type &entry, dict )
//      std::cerr << "'" << entry.first << "' = '" << entry.second << "'" << std::endl;

    osmosdr_src_iface *iface = NULL;
    gr_basic_block_sptr block;

#ifdef ENABLE_OSMOSDR
    if ( dict.count("osmosdr") ) {
      osmosdr_src_c_sptr src = osmosdr_make_src_c( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_FCD
    if ( dict.count("fcd") ) {
      fcd_source_sptr src = make_fcd_source( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_FILE
    if ( dict.count("file") ) {
      file_source_c_sptr src = make_file_source_c( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_RTL
    if ( dict.count("rtl") ) {
      rtl_source_c_sptr src = make_rtl_source_c( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_RTL_TCP
    if ( dict.count("rtl_tcp") ) {
      rtl_tcp_source_c_sptr src = make_rtl_tcp_source_c( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_UHD
    if ( dict.count("uhd") ) {
      uhd_source_c_sptr src = make_uhd_source_c( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_MIRI
    if ( dict.count("miri") ) {
      miri_source_c_sptr src = make_miri_source_c( arg );
      block = src; iface = src.get();
    }
#endif

#ifdef ENABLE_HACKRF
    if ( dict.count("hackrf") ) {
      hackrf_source_c_sptr src = make_hackrf_source_c( arg );
      block = src; iface = src.get();
    }
#endif

    if ( iface != NULL && long(block.get()) != 0 ) {
      _devs.push_back( iface );

      for (size_t i = 0; i < iface->get_num_channels(); i++) {
#ifdef HAVE_IQBALANCE
        iqbalance_optimize_c_sptr iq_opt = iqbalance_make_optimize_c( 0 );
        iqbalance_fix_cc_sptr iq_fix = iqbalance_make_fix_cc();

        connect(block, i, iq_fix, 0);
        connect(iq_fix, 0, self(), channel++);

        connect(block, i, iq_opt, 0);
        msg_connect(iq_opt, "iqbal_corr", iq_fix, "iqbal_corr");

        _iq_opt.push_back( iq_opt.get() );
        _iq_fix.push_back( iq_fix.get() );
#else
        connect(block, i, self(), channel++);
#endif
      }
    } else if ( (iface != NULL) || (long(block.get()) != 0) )
      throw std::runtime_error("Eitner iface or block are NULL.");

  }

  if (!_devs.size())
    throw std::runtime_error("No devices specified via device arguments.");
#ifdef WORKAROUND_GR_HIER_BLOCK2_BUG
  } catch ( std::exception &ex ) {
    std::cerr << std::endl << "FATAL: " << ex.what() << std::endl << std::endl;

    /* we try to prevent the whole application from crashing by faking
     * the missing hardware (channels) with a gaussian noise source */
    gr_noise_source_c_sptr noise_source = \
        gr_make_noise_source_c( GR_GAUSSIAN, 10 );

    gr_throttle::sptr throttle = gr_make_throttle(sizeof(gr_complex), 1e6);

    connect(noise_source, 0, throttle, 0);

    size_t missing_chans = output_signature()->max_streams() - channel;

    std::cerr << "Trying to fill up " << missing_chans
              << " missing channel(s) with gaussian noise.\n"
              << "This is being done to prevent the application from crashing\n"
              << "due to a gnuradio bug. The maintainers have been informed.\n"
              << std::endl;

    for (size_t i = 0; i < missing_chans; i++)
      connect(throttle, 0, self(), channel++);
  }
#endif
}

size_t osmosdr_source_c_impl::get_num_channels()
{
  size_t channels = 0;

  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    channels += dev->get_num_channels();

  return channels;
}

#define NO_DEVICES_MSG  "FATAL: No device(s) available to work with."

osmosdr::meta_range_t osmosdr_source_c_impl::get_sample_rates()
{
  if ( ! _devs.empty() )
    return _devs[0]->get_sample_rates(); // assume same devices used in the group
#if 0
  else
    throw std::runtime_error(NO_DEVICES_MSG);
#endif
  return osmosdr::meta_range_t();;
}

double osmosdr_source_c_impl::set_sample_rate(double rate)
{
  double sample_rate = 0;

  if (_sample_rate != rate) {
#if 0
    if (_devs.empty())
      throw std::runtime_error(NO_DEVICES_MSG);
#endif
    BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
      sample_rate = dev->set_sample_rate(rate);

#ifdef HAVE_IQBALANCE
    size_t channel = 0;
    BOOST_FOREACH( osmosdr_src_iface *dev, _devs ) {
      for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++) {
        if ( channel < _iq_opt.size() ) {
          iqbalance_optimize_c *opt = _iq_opt[channel];

          if ( opt->period() > 0 ) { /* optimize is enabled */
            opt->set_period( dev->get_sample_rate() / 5 );
            opt->reset();
          }
        }

        channel++;
      }
    }
#endif

    _sample_rate = sample_rate;
  }

  return sample_rate;
}

double osmosdr_source_c_impl::get_sample_rate()
{
  double sample_rate = 0;

  if (!_devs.empty())
    sample_rate = _devs[0]->get_sample_rate(); // assume same devices used in the group
#if 0
  else
    throw std::runtime_error(NO_DEVICES_MSG);
#endif
  return sample_rate;
}

osmosdr::freq_range_t osmosdr_source_c_impl::get_freq_range( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_freq_range( dev_chan );

  return osmosdr::freq_range_t();
}

double osmosdr_source_c_impl::set_center_freq( double freq, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _center_freq[ chan ] != freq ) {
          _center_freq[ chan ] = freq;
          return dev->set_center_freq( freq, dev_chan );
        } else { return _center_freq[ chan ]; }
      }

  return 0;
}

double osmosdr_source_c_impl::get_center_freq( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_center_freq( dev_chan );

  return 0;
}

double osmosdr_source_c_impl::set_freq_corr( double ppm, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _freq_corr[ chan ] != ppm ) {
          _freq_corr[ chan ] = ppm;
          return dev->set_freq_corr( ppm, dev_chan );
        } else { return _freq_corr[ chan ]; }
      }

  return 0;
}

double osmosdr_source_c_impl::get_freq_corr( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_freq_corr( dev_chan );

  return 0;
}

std::vector<std::string> osmosdr_source_c_impl::get_gain_names( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_gain_names( dev_chan );

  return std::vector< std::string >();
}

osmosdr::gain_range_t osmosdr_source_c_impl::get_gain_range( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_gain_range( dev_chan );

  return osmosdr::gain_range_t();
}

osmosdr::gain_range_t osmosdr_source_c_impl::get_gain_range( const std::string & name, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_gain_range( name, dev_chan );

  return osmosdr::gain_range_t();
}

bool osmosdr_source_c_impl::set_gain_mode( bool automatic, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _gain_mode[ chan ] != automatic ) {
          _gain_mode[ chan ] = automatic;
          bool mode = dev->set_gain_mode( automatic, dev_chan );
          if (!automatic) // reapply gain value when switched to manual mode
            dev->set_gain( _gain[ chan ], dev_chan );
          return mode;
        } else { return _gain_mode[ chan ]; }
      }

  return false;
}

bool osmosdr_source_c_impl::get_gain_mode( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_gain_mode( dev_chan );

  return false;
}

double osmosdr_source_c_impl::set_gain( double gain, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _gain[ chan ] != gain ) {
          _gain[ chan ] = gain;
          return dev->set_gain( gain, dev_chan );
        } else { return _gain[ chan ]; }
      }

  return 0;
}

double osmosdr_source_c_impl::set_gain( double gain, const std::string & name, size_t chan)
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->set_gain( gain, name, dev_chan );

  return 0;
}

double osmosdr_source_c_impl::get_gain( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_gain( dev_chan );

  return 0;
}

double osmosdr_source_c_impl::get_gain( const std::string & name, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_gain( name, dev_chan );

  return 0;
}

double osmosdr_source_c_impl::set_if_gain( double gain, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _if_gain[ chan ] != gain ) {
          _if_gain[ chan ] = gain;
          return dev->set_if_gain( gain, dev_chan );
        } else { return _if_gain[ chan ]; }
      }

  return 0;
}

double osmosdr_source_c_impl::set_bb_gain( double gain, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _bb_gain[ chan ] != gain ) {
          _bb_gain[ chan ] = gain;
          return dev->set_bb_gain( gain, dev_chan );
        } else { return _bb_gain[ chan ]; }
      }

  return 0;
}

std::vector< std::string > osmosdr_source_c_impl::get_antennas( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_antennas( dev_chan );

  return std::vector< std::string >();
}

std::string osmosdr_source_c_impl::set_antenna( const std::string & antenna, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _antenna[ chan ] != antenna ) {
          _antenna[ chan ] = antenna;
          return dev->set_antenna( antenna, dev_chan );
        } else { return _antenna[ chan ]; }
      }

  return "";
}

std::string osmosdr_source_c_impl::get_antenna( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_antenna( dev_chan );

  return "";
}

void osmosdr_source_c_impl::set_dc_offset_mode( int mode, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->set_dc_offset_mode( mode, dev_chan );
}

void osmosdr_source_c_impl::set_dc_offset( const std::complex<double> &offset, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->set_dc_offset( offset, dev_chan );
}

void osmosdr_source_c_impl::set_iq_balance_mode( int mode, size_t chan )
{
  size_t channel = 0;
#ifdef HAVE_IQBALANCE
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs ) {
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++) {
      if ( chan == channel++ ) {
        if ( chan < _iq_opt.size() && chan < _iq_fix.size() ) {
          iqbalance_optimize_c *opt = _iq_opt[chan];
          iqbalance_fix_cc *fix = _iq_fix[chan];

          if ( IQBalanceOff == mode  ) {
            opt->set_period( 0 );
            /* store current values in order to be able to restore them later */
            _vals[ chan ] = std::pair< float, float >( fix->mag(), fix->phase() );
            fix->set_mag( 0.0f );
            fix->set_phase( 0.0f );
          } else if ( IQBalanceManual == mode ) {
            if ( opt->period() == 0 ) { /* transition from Off to Manual */
              /* restore previous values */
              std::pair< float, float > val = _vals[ chan ];
              fix->set_mag( val.first );
              fix->set_phase( val.second );
            }
            opt->set_period( 0 );
          } else if ( IQBalanceAutomatic == mode ) {
            opt->set_period( dev->get_sample_rate() / 5 );
            opt->reset();
          }
        }
      }
    }
  }
#else
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->set_iq_balance_mode( mode, dev_chan );
#endif
}

void osmosdr_source_c_impl::set_iq_balance( const std::complex<double> &balance, size_t chan )
{
  size_t channel = 0;
#ifdef HAVE_IQBALANCE
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs ) {
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++) {
      if ( chan == channel++ ) {
        if ( chan < _iq_opt.size() && chan < _iq_fix.size() ) {
          iqbalance_optimize_c *opt = _iq_opt[chan];
          iqbalance_fix_cc *fix = _iq_fix[chan];

          if ( opt->period() == 0 ) { /* automatic optimization desabled */
            fix->set_mag( balance.real() );
            fix->set_phase( balance.imag() );
          }
        }
      }
    }
  }
#else
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->set_iq_balance( balance, dev_chan );
#endif
}

double osmosdr_source_c_impl::set_bandwidth( double bandwidth, size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ ) {
        if ( _bandwidth[ chan ] != bandwidth ) {
          _bandwidth[ chan ] = bandwidth;
          return dev->set_bandwidth( bandwidth, dev_chan );
        } else { return _bandwidth[ chan ]; }
      }

  return 0;
}

double osmosdr_source_c_impl::get_bandwidth( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_bandwidth( dev_chan );

  return 0;
}

osmosdr::freq_range_t osmosdr_source_c_impl::get_bandwidth_range( size_t chan )
{
  size_t channel = 0;
  BOOST_FOREACH( osmosdr_src_iface *dev, _devs )
    for (size_t dev_chan = 0; dev_chan < dev->get_num_channels(); dev_chan++)
      if ( chan == channel++ )
        return dev->get_bandwidth_range( dev_chan );

  return osmosdr::freq_range_t();
}