Hello list, the following patch rely on librtlsdr fork on github. I would like to receive feedback on what is the best option to backport the commit: https://github.com/librtlsdr/librtlsdr/commit/25d0e8e6737b93b3564ad04d0fd2cd...
or alternatively put a preprocessor switch in this patch in order to enable build against 2 different rtlsdr driver.
regards Luigi
--- a/lib/rtl/rtl_source_c.cc +++ b/lib/rtl/rtl_source_c.cc @@ -88,7 +88,11 @@ rtl_source_c::rtl_source_c (const std::string &args) _no_tuner(false), _auto_gain(false), _if_gain(0), - _skipped(0) + _lna_gain(0), + _mix_gain(0), + _vga_gain(0), + _skipped(0), + _extended_mode(0) { int ret; int index; @@ -150,6 +154,9 @@ rtl_source_c::rtl_source_c (const std::string &args) if (dict.count("offset_tune")) offset_tune = boost::lexical_cast< unsigned int >( dict["offset_tune"] );
+ if (dict.count("extended_mode")) + _extended_mode = boost::lexical_cast< bool >( dict["extended_mode"] ); + _buf_num = _buf_len = _buf_head = _buf_used = _buf_offset = 0;
if (dict.count("buffers")) @@ -528,6 +535,13 @@ std::vectorstd::string rtl_source_c::get_gain_names( size_t chan ) if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) { names += "IF"; } + + if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_R820T ) { + if ( _extended_mode ) { + names += "MIX"; + names += "IF"; + } + } }
return names; @@ -553,14 +567,31 @@ osmosdr::gain_range_t rtl_source_c::get_gain_range( size_t chan )
osmosdr::gain_range_t rtl_source_c::get_gain_range( const std::string & name, size_t chan ) { - if ( "IF" == name ) { - if ( _dev ) { - if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) { - return osmosdr::gain_range_t(3, 56, 1); - } else { + + if ( _dev ) { + if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_R820T ) { + if ( _extended_mode ) { + if ( "LNA" == name ) { + return osmosdr::gain_range_t( 0, 15, 1 ); + } + + if ( "MIX" == name ) { + return osmosdr::gain_range_t( 0, 15, 1 ); + } + + if ( "IF" == name ) { + return osmosdr::gain_range_t( 0, 15, 1 ); + } + return osmosdr::gain_range_t(); } } + + if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) { + if ( "IF" == name ) { + return osmosdr::gain_range_t(3, 56, 1); + } + } }
return get_gain_range( chan ); @@ -584,21 +615,86 @@ bool rtl_source_c::get_gain_mode( size_t chan ) return _auto_gain; }
+double rtl_source_c::set_bandwidth( double bandwidth, size_t chan ) +{ + if (_dev) { + return (double)rtlsdr_set_tuner_bandwidth( _dev, (uint32_t)bandwidth); + } + + return 0; +} + double rtl_source_c::set_gain( double gain, size_t chan ) { osmosdr::gain_range_t rf_gains = rtl_source_c::get_gain_range( chan );
- if (_dev) { + if ( _dev ) { rtlsdr_set_tuner_gain( _dev, int(rf_gains.clip(gain) * 10.0) ); }
return get_gain( chan ); }
+double rtl_source_c::set_lna_gain( double gain, size_t chan ) +{ + osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( "LNA", chan ); + + if ( _dev ) { + _lna_gain = gains.clip( gain ); + return rtlsdr_set_tuner_gain_ext( _dev, int(_lna_gain), int(_mix_gain), int(_vga_gain) ); + } + + return _lna_gain; +} + +double rtl_source_c::set_mix_gain( double gain, size_t chan ) +{ + osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( "MIX", chan ); + + if ( _dev ) { + _mix_gain = gains.clip( gain ); + return rtlsdr_set_tuner_gain_ext( _dev, int(_lna_gain), int(_mix_gain), int(_vga_gain) ); + } + + return _mix_gain; +} + +double rtl_source_c::set_vga_gain( double gain, size_t chan ) +{ + osmosdr::gain_range_t gains = rtl_source_c::get_gain_range( "IF", chan ); + + if ( _dev ) { + _vga_gain = gains.clip( gain ); + return rtlsdr_set_tuner_gain_ext( _dev, int(_lna_gain), int(_mix_gain), int(_vga_gain) ); + } + + return _vga_gain; +} + double rtl_source_c::set_gain( double gain, const std::string & name, size_t chan) { - if ( "IF" == name ) { - return set_if_gain( gain, chan ); + if ( _dev ) { + if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_R820T ) { + if ( _extended_mode ) { + if ( "LNA" == name ) { + return set_lna_gain( gain, chan ); + } + + if ( "MIX" == name ) { + return set_mix_gain( gain, chan ); + } + + if ( "IF" == name ) { + return set_vga_gain( gain, chan ); + } + } + } + + if ( rtlsdr_get_tuner_type(_dev) == RTLSDR_TUNER_E4000 ) { + if ( "IF" == name ) { + return set_if_gain( gain, chan ); + } + } }
return set_gain( gain, chan ); @@ -614,6 +710,20 @@ double rtl_source_c::get_gain( size_t chan )
double rtl_source_c::get_gain( const std::string & name, size_t chan ) { + if ( _extended_mode ) { + if ( "LNA" == name ) { + return _lna_gain; + } + + if ( "MIX" == name ) { + return _mix_gain; + } + + if ( "IF" == name ) { + return _vga_gain; + } + } + if ( "IF" == name ) { return _if_gain; } diff --git a/lib/rtl/rtl_source_c.h b/lib/rtl/rtl_source_c.h index 76de400..bb20e5c 100644 --- a/lib/rtl/rtl_source_c.h +++ b/lib/rtl/rtl_source_c.h @@ -101,12 +101,16 @@ public: osmosdr::gain_range_t get_gain_range( const std::string & name, size_t chan = 0 ); bool set_gain_mode( bool automatic, size_t chan = 0 ); bool get_gain_mode( size_t chan = 0 ); + double set_bandwidth( double bandwidth, size_t chan = 0 ); double set_gain( double gain, size_t chan = 0 ); double set_gain( double gain, const std::string & name, size_t chan = 0 ); double get_gain( size_t chan = 0 ); double get_gain( const std::string & name, size_t chan = 0 );
double set_if_gain( double gain, size_t chan = 0 ); + double set_lna_gain( double gain, size_t chan = 0 ); + double set_mix_gain( double gain, size_t chan = 0 ); + double set_vga_gain( double gain, size_t chan = 0 );
std::vector< std::string > get_antennas( size_t chan = 0 ); std::string set_antenna( const std::string & antenna, size_t chan = 0 ); @@ -141,7 +145,12 @@ private: bool _no_tuner; bool _auto_gain; double _if_gain; + double _lna_gain; + double _mix_gain; + double _vga_gain; unsigned int _skipped; + + bool _extended_mode; };
#endif /* INCLUDED_RTLSDR_SOURCE_C_H */
Hi,
the following patch rely on librtlsdr fork on github. I would like to receive feedback on what is the best option to backport the commit: https://github.com/librtlsdr/librtlsdr/commit/25d0e8e6737b93b3564ad04d0fd2cd...
There is so many things wrong with this patch that I don't even know where to start :
- Rename a function to _old and then not use it ... wtf ... if it's not used, remove it, don't leave it there for posterity's sake, that's why we have source revision control ! - r82xx_set_gain now has 3 completely distinct operating modes for no good reasons ... just make 3 distinct functions and call the right one. Everywhere that function is called the "mode" params are fixed/hardcoded ... - Introduces a new API rtlsdr_set_tuner_gain_ext which has hard coded calls to R820T functions without the appropriate tuner type indirections and without appropriate fallbacks or error code if the dongle uses a tuner not supporting the 'ext' mode.
Seriously people wonder why those don't get upstream, but I apply this and my E4k dongle might just not work or plain crash with any app using that new API ...
Cheers,
Sylvain
In my haste, I also forgot some other things wrong :
- TAB vs SPACE / indentation / code style errors - Gratuitous whitespace changes - Change the .gitignore completely unrelated to the commit but still present in it...