2011-10-01 18:45:20 +00:00
/*!
* \ file file_signal_source . cc
2011-12-28 21:36:45 +00:00
* \ brief Implementation of a class that reads signals samples from a file
* and adapts it to a SignalSourceInterface
2011-10-01 18:45:20 +00:00
* \ author Carlos Aviles , 2010. carlos . avilesr ( at ) googlemail . com
* Javier Arribas , 2011 jarribas ( at ) cttc . es
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*
2019-07-26 10:38:20 +00:00
* Copyright ( C ) 2010 - 2019 ( see AUTHORS file for a list of contributors )
2011-10-01 18:45:20 +00:00
*
* GNSS - SDR is a software defined Global Navigation
* Satellite Systems receiver
*
* This file is part of GNSS - SDR .
*
2020-02-08 00:20:02 +00:00
* SPDX - License - Identifier : GPL - 3.0 - or - later
2011-10-01 18:45:20 +00:00
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
# include "file_signal_source.h"
2014-01-14 23:22:54 +00:00
# include "configuration_interface.h"
2018-02-18 11:23:55 +00:00
# include "gnss_sdr_flags.h"
# include "gnss_sdr_valve.h"
2018-02-26 02:15:53 +00:00
# include <glog/logging.h>
2018-12-09 21:00:09 +00:00
# include <exception>
2018-02-26 02:15:53 +00:00
# include <fstream>
# include <iomanip>
2018-12-09 21:00:09 +00:00
# include <iostream> // for std::cerr
2018-12-03 14:43:43 +00:00
# include <utility>
2011-10-01 18:45:20 +00:00
2020-06-29 07:07:41 +00:00
FileSignalSource : : FileSignalSource ( const ConfigurationInterface * configuration ,
2018-12-03 18:01:47 +00:00
const std : : string & role , unsigned int in_streams , unsigned int out_streams ,
2020-06-18 09:49:28 +00:00
Concurrent_Queue < pmt : : pmt_t > * queue ) : role_ ( role ) , in_streams_ ( in_streams ) , out_streams_ ( out_streams )
2011-10-01 18:45:20 +00:00
{
2014-11-05 22:26:31 +00:00
std : : string default_filename = " ./example_capture.dat " ;
2013-02-04 00:16:22 +00:00
std : : string default_item_type = " short " ;
2014-11-05 22:26:31 +00:00
std : : string default_dump_filename = " ./my_capture.dat " ;
2013-02-04 00:16:22 +00:00
2015-08-06 08:31:39 +00:00
double default_seconds_to_skip = 0.0 ;
size_t header_size = 0 ;
2019-02-11 17:36:12 +00:00
samples_ = configuration - > property ( role + " .samples " , 0 ) ;
2019-02-11 18:27:14 +00:00
sampling_frequency_ = configuration - > property ( role + " .sampling_frequency " , 0 ) ;
2013-02-04 00:16:22 +00:00
filename_ = configuration - > property ( role + " .filename " , default_filename ) ;
// override value with commandline flag, if present
2019-02-11 20:13:02 +00:00
if ( FLAGS_signal_source ! = " - " )
{
filename_ = FLAGS_signal_source ;
}
if ( FLAGS_s ! = " - " )
{
filename_ = FLAGS_s ;
}
2013-02-04 00:16:22 +00:00
item_type_ = configuration - > property ( role + " .item_type " , default_item_type ) ;
repeat_ = configuration - > property ( role + " .repeat " , false ) ;
dump_ = configuration - > property ( role + " .dump " , false ) ;
dump_filename_ = configuration - > property ( role + " .dump_filename " , default_dump_filename ) ;
enable_throttle_control_ = configuration - > property ( role + " .enable_throttle_control " , false ) ;
2018-01-19 11:09:40 +00:00
2018-03-03 01:03:39 +00:00
double seconds_to_skip = configuration - > property ( role + " .seconds_to_skip " , default_seconds_to_skip ) ;
header_size = configuration - > property ( role + " .header_size " , 0 ) ;
2018-08-11 12:31:35 +00:00
int64_t samples_to_skip = 0 ;
2015-08-06 08:31:39 +00:00
bool is_complex = false ;
2013-02-04 00:16:22 +00:00
2018-12-03 11:52:17 +00:00
if ( item_type_ = = " gr_complex " )
2013-02-04 00:16:22 +00:00
{
item_size_ = sizeof ( gr_complex ) ;
}
2018-12-03 11:52:17 +00:00
else if ( item_type_ = = " float " )
2013-02-04 00:16:22 +00:00
{
item_size_ = sizeof ( float ) ;
}
2018-12-03 11:52:17 +00:00
else if ( item_type_ = = " short " )
2013-02-04 00:16:22 +00:00
{
2015-02-04 01:29:38 +00:00
item_size_ = sizeof ( int16_t ) ;
2013-02-04 00:16:22 +00:00
}
2018-12-03 11:52:17 +00:00
else if ( item_type_ = = " ishort " )
2015-02-04 12:06:44 +00:00
{
item_size_ = sizeof ( int16_t ) ;
2015-08-06 08:31:39 +00:00
is_complex = true ;
2015-02-04 12:06:44 +00:00
}
2018-12-03 11:52:17 +00:00
else if ( item_type_ = = " byte " )
2014-10-27 12:00:10 +00:00
{
2016-05-02 21:46:30 +00:00
item_size_ = sizeof ( int8_t ) ;
2014-10-27 12:00:10 +00:00
}
2018-12-03 11:52:17 +00:00
else if ( item_type_ = = " ibyte " )
2015-02-04 12:06:44 +00:00
{
2016-05-02 21:46:30 +00:00
item_size_ = sizeof ( int8_t ) ;
2015-08-06 08:31:39 +00:00
is_complex = true ;
2015-02-04 12:06:44 +00:00
}
2013-02-04 00:16:22 +00:00
else
{
2014-03-16 19:58:29 +00:00
LOG ( WARNING ) < < item_type_
2018-03-03 01:03:39 +00:00
< < " unrecognized item type. Using gr_complex. " ;
2013-02-04 00:16:22 +00:00
item_size_ = sizeof ( gr_complex ) ;
}
try
2018-03-03 01:03:39 +00:00
{
2013-07-04 13:47:40 +00:00
file_source_ = gr : : blocks : : file_source : : make ( item_size_ , filename_ . c_str ( ) , repeat_ ) ;
2018-03-03 01:03:39 +00:00
if ( seconds_to_skip > 0 )
2015-08-06 08:31:39 +00:00
{
2018-08-11 12:31:35 +00:00
samples_to_skip = static_cast < int64_t > ( seconds_to_skip * sampling_frequency_ ) ;
2018-01-19 11:09:40 +00:00
2018-03-03 01:03:39 +00:00
if ( is_complex )
2018-01-19 11:09:40 +00:00
{
samples_to_skip * = 2 ;
}
2015-08-06 08:31:39 +00:00
}
2018-03-03 01:03:39 +00:00
if ( header_size > 0 )
2018-01-19 11:09:40 +00:00
{
samples_to_skip + = header_size ;
}
2015-08-06 08:31:39 +00:00
2018-03-03 01:03:39 +00:00
if ( samples_to_skip > 0 )
2015-08-06 08:31:39 +00:00
{
2018-01-19 11:09:40 +00:00
LOG ( INFO ) < < " Skipping " < < samples_to_skip < < " samples of the input file " ;
2018-03-03 01:03:39 +00:00
if ( not file_source_ - > seek ( samples_to_skip , SEEK_SET ) )
2018-01-19 11:09:40 +00:00
{
LOG ( INFO ) < < " Error skipping bytes! " ;
}
2015-08-06 08:31:39 +00:00
}
2018-03-03 01:03:39 +00:00
}
catch ( const std : : exception & e )
{
2018-12-03 11:52:17 +00:00
if ( filename_ = = default_filename )
2014-11-04 20:28:01 +00:00
{
2014-11-05 22:26:31 +00:00
std : : cerr
2018-03-03 01:03:39 +00:00
< < " The configuration file has not been found. "
< < std : : endl
< < " Please create a configuration file based on the examples at the 'conf/' folder "
< < std : : endl
< < " and then generate your own GNSS Software Defined Receiver by doing: "
< < std : : endl
< < " $ gnss-sdr --config_file=/path/to/my_GNSS_SDR_configuration.conf "
< < std : : endl ;
2014-11-04 20:28:01 +00:00
}
else
{
2014-11-05 22:26:31 +00:00
std : : cerr
2018-03-03 01:03:39 +00:00
< < " The receiver was configured to work with a file signal source "
< < std : : endl
< < " but the specified file is unreachable by GNSS-SDR. "
< < std : : endl
< < " Please modify your configuration file "
< < std : : endl
< < " and point SignalSource.filename to a valid raw data file. Then: "
< < std : : endl
< < " $ gnss-sdr --config_file=/path/to/my_GNSS_SDR_configuration.conf "
< < std : : endl
< < " Examples of configuration files available at: "
< < std : : endl
< < GNSSSDR_INSTALL_DIR " /share/gnss-sdr/conf/ "
< < std : : endl ;
2014-11-04 20:28:01 +00:00
}
2014-03-16 19:58:29 +00:00
LOG ( INFO ) < < " file_signal_source: Unable to open the samples file "
2014-11-05 00:39:13 +00:00
< < filename_ . c_str ( ) < < " , exiting the program. " ;
2013-02-04 00:16:22 +00:00
throw ( e ) ;
2018-03-03 01:03:39 +00:00
}
2013-02-04 00:16:22 +00:00
DLOG ( INFO ) < < " file_source( " < < file_source_ - > unique_id ( ) < < " ) " ;
2018-03-03 01:03:39 +00:00
if ( samples_ = = 0 ) // read all file
2013-02-04 00:16:22 +00:00
{
/*!
* BUG workaround : The GNU Radio file source does not stop the receiver after reaching the End of File .
* A possible solution is to compute the file length in samples using file size , excluding the last 100 milliseconds , and enable always the
* valve block
*/
2018-03-03 01:03:39 +00:00
std : : ifstream file ( filename_ . c_str ( ) , std : : ios : : in | std : : ios : : binary | std : : ios : : ate ) ;
2013-02-04 00:16:22 +00:00
std : : ifstream : : pos_type size ;
if ( file . is_open ( ) )
{
size = file . tellg ( ) ;
2020-03-12 18:15:11 +00:00
DLOG ( INFO ) < < " Total samples in the file= " < < floor ( static_cast < double > ( size ) / static_cast < double > ( item_size_ ) ) ;
2013-02-04 00:16:22 +00:00
}
else
{
std : : cout < < " file_signal_source: Unable to open the samples file " < < filename_ . c_str ( ) < < std : : endl ;
2014-03-16 19:58:29 +00:00
LOG ( ERROR ) < < " file_signal_source: Unable to open the samples file " < < filename_ . c_str ( ) ;
2013-02-04 00:16:22 +00:00
}
2017-01-11 22:26:42 +00:00
std : : streamsize ss = std : : cout . precision ( ) ;
2013-02-04 00:16:22 +00:00
std : : cout < < std : : setprecision ( 16 ) ;
2015-01-31 11:16:46 +00:00
std : : cout < < " Processing file " < < filename_ < < " , which contains " < < static_cast < double > ( size ) < < " [bytes] " < < std : : endl ;
2018-03-03 01:03:39 +00:00
std : : cout . precision ( ss ) ;
2013-02-04 00:16:22 +00:00
if ( size > 0 )
{
2018-08-11 12:31:35 +00:00
int64_t bytes_to_skip = samples_to_skip * item_size_ ;
int64_t bytes_to_process = static_cast < int64_t > ( size ) - bytes_to_skip ;
2020-03-12 18:15:11 +00:00
samples_ = floor ( static_cast < double > ( bytes_to_process ) / static_cast < double > ( item_size_ ) - ceil ( 0.002 * static_cast < double > ( sampling_frequency_ ) ) ) ; // process all the samples available in the file excluding at least the last 1 ms
2013-02-04 00:16:22 +00:00
}
}
2013-07-30 10:53:45 +00:00
2013-02-04 00:16:22 +00:00
CHECK ( samples_ > 0 ) < < " File does not contain enough samples to process. " ;
double signal_duration_s ;
2018-03-03 01:03:39 +00:00
signal_duration_s = static_cast < double > ( samples_ ) * ( 1 / static_cast < double > ( sampling_frequency_ ) ) ;
2014-09-09 15:16:33 +00:00
2018-03-03 01:03:39 +00:00
if ( is_complex )
2017-01-11 22:26:42 +00:00
{
signal_duration_s / = 2.0 ;
}
2015-08-06 08:31:39 +00:00
2013-02-04 00:16:22 +00:00
DLOG ( INFO ) < < " Total number samples to be processed= " < < samples_ < < " GNSS signal duration= " < < signal_duration_s < < " [s] " ;
std : : cout < < " GNSS signal recorded time to be processed: " < < signal_duration_s < < " [s] " < < std : : endl ;
2020-06-18 09:49:28 +00:00
valve_ = gnss_sdr_make_valve ( item_size_ , samples_ , queue ) ;
2013-02-04 00:16:22 +00:00
DLOG ( INFO ) < < " valve( " < < valve_ - > unique_id ( ) < < " ) " ;
if ( dump_ )
{
2013-07-04 13:47:40 +00:00
sink_ = gr : : blocks : : file_sink : : make ( item_size_ , dump_filename_ . c_str ( ) ) ;
2013-02-04 00:16:22 +00:00
DLOG ( INFO ) < < " file_sink( " < < sink_ - > unique_id ( ) < < " ) " ;
}
if ( enable_throttle_control_ )
{
2013-07-04 13:47:40 +00:00
throttle_ = gr : : blocks : : throttle : : make ( item_size_ , sampling_frequency_ ) ;
2013-02-04 00:16:22 +00:00
}
2018-01-19 11:09:40 +00:00
2013-02-04 00:16:22 +00:00
DLOG ( INFO ) < < " File source filename " < < filename_ ;
DLOG ( INFO ) < < " Samples " < < samples_ ;
DLOG ( INFO ) < < " Sampling frequency " < < sampling_frequency_ ;
DLOG ( INFO ) < < " Item type " < < item_type_ ;
DLOG ( INFO ) < < " Item size " < < item_size_ ;
DLOG ( INFO ) < < " Repeat " < < repeat_ ;
DLOG ( INFO ) < < " Dump " < < dump_ ;
DLOG ( INFO ) < < " Dump filename " < < dump_filename_ ;
2018-06-03 17:11:16 +00:00
if ( in_streams_ > 0 )
{
LOG ( ERROR ) < < " A signal source does not have an input stream " ;
}
if ( out_streams_ > 1 )
{
LOG ( ERROR ) < < " This implementation only supports one output stream " ;
}
2011-10-01 18:45:20 +00:00
}
2012-01-23 00:52:05 +00:00
2013-07-04 13:47:40 +00:00
void FileSignalSource : : connect ( gr : : top_block_sptr top_block )
2011-10-01 18:45:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( samples_ > 0 )
2012-07-15 00:47:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( enable_throttle_control_ = = true )
{
top_block - > connect ( file_source_ , 0 , throttle_ , 0 ) ;
DLOG ( INFO ) < < " connected file source to throttle " ;
top_block - > connect ( throttle_ , 0 , valve_ , 0 ) ;
DLOG ( INFO ) < < " connected throttle to valve " ;
if ( dump_ )
{
top_block - > connect ( valve_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " connected valve to file sink " ;
}
}
else
{
top_block - > connect ( file_source_ , 0 , valve_ , 0 ) ;
DLOG ( INFO ) < < " connected file source to valve " ;
if ( dump_ )
{
top_block - > connect ( valve_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " connected valve to file sink " ;
}
}
2011-10-01 18:45:20 +00:00
}
2013-07-04 13:47:40 +00:00
else
2012-07-15 00:47:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( enable_throttle_control_ = = true )
{
top_block - > connect ( file_source_ , 0 , throttle_ , 0 ) ;
DLOG ( INFO ) < < " connected file source to throttle " ;
if ( dump_ )
{
top_block - > connect ( file_source_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " connected file source to sink " ;
}
}
else
{
if ( dump_ )
{
top_block - > connect ( file_source_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " connected file source to sink " ;
}
}
2012-07-15 00:47:20 +00:00
}
2011-10-01 18:45:20 +00:00
}
2012-01-23 00:52:05 +00:00
2013-07-04 13:47:40 +00:00
void FileSignalSource : : disconnect ( gr : : top_block_sptr top_block )
2011-10-01 18:45:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( samples_ > 0 )
2011-10-01 18:45:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( enable_throttle_control_ = = true )
{
top_block - > disconnect ( file_source_ , 0 , throttle_ , 0 ) ;
DLOG ( INFO ) < < " disconnected file source to throttle " ;
top_block - > disconnect ( throttle_ , 0 , valve_ , 0 ) ;
DLOG ( INFO ) < < " disconnected throttle to valve " ;
if ( dump_ )
{
top_block - > disconnect ( valve_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " disconnected valve to file sink " ;
}
}
else
{
top_block - > disconnect ( file_source_ , 0 , valve_ , 0 ) ;
DLOG ( INFO ) < < " disconnected file source to valve " ;
if ( dump_ )
{
top_block - > disconnect ( valve_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " disconnected valve to file sink " ;
}
}
2011-10-01 18:45:20 +00:00
}
2013-07-04 13:47:40 +00:00
else
2011-10-01 18:45:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( enable_throttle_control_ = = true )
{
top_block - > disconnect ( file_source_ , 0 , throttle_ , 0 ) ;
DLOG ( INFO ) < < " disconnected file source to throttle " ;
if ( dump_ )
{
top_block - > disconnect ( file_source_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " disconnected file source to sink " ;
}
}
else
{
if ( dump_ )
{
top_block - > disconnect ( file_source_ , 0 , sink_ , 0 ) ;
DLOG ( INFO ) < < " disconnected file source to sink " ;
}
}
2011-10-01 18:45:20 +00:00
}
}
2012-01-23 00:52:05 +00:00
2013-07-04 13:47:40 +00:00
gr : : basic_block_sptr FileSignalSource : : get_left_block ( )
2011-10-01 18:45:20 +00:00
{
2014-03-16 19:58:29 +00:00
LOG ( WARNING ) < < " Left block of a signal source should not be retrieved " ;
2013-07-04 13:47:40 +00:00
return gr : : blocks : : file_source : : sptr ( ) ;
2011-10-01 18:45:20 +00:00
}
2012-01-23 00:52:05 +00:00
2013-07-04 13:47:40 +00:00
gr : : basic_block_sptr FileSignalSource : : get_right_block ( )
2011-10-01 18:45:20 +00:00
{
2013-07-04 13:47:40 +00:00
if ( samples_ > 0 )
2012-01-23 00:52:05 +00:00
{
2013-07-04 13:47:40 +00:00
return valve_ ;
2012-01-23 00:52:05 +00:00
}
2018-12-03 21:08:19 +00:00
if ( enable_throttle_control_ = = true )
2012-01-23 00:52:05 +00:00
{
2018-12-03 21:08:19 +00:00
return throttle_ ;
2012-01-23 00:52:05 +00:00
}
2018-12-03 21:08:19 +00:00
return file_source_ ;
2011-10-01 18:45:20 +00:00
}