GNSS-SDR  0.0.19
An Open Source GNSS Software Defined Receiver
file_source_base.h
Go to the documentation of this file.
1 /*!
2  * \file file_source_base.h
3  * \brief Header file of the base class to file-oriented signal_source GNSS blocks.
4  * \author Jim Melton, 2021. jim.melton(at)sncorp.com
5  *
6  *
7  * -----------------------------------------------------------------------------
8  *
9  * GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
10  * This file is part of GNSS-SDR.
11  *
12  * Copyright (C) 2010-2021 (see AUTHORS file for a list of contributors)
13  * SPDX-License-Identifier: GPL-3.0-or-later
14  *
15  * -----------------------------------------------------------------------------
16  */
17 
18 #ifndef GNSS_SDR_FILE_SOURCE_BASE_H
19 #define GNSS_SDR_FILE_SOURCE_BASE_H
20 
21 #include "concurrent_queue.h"
22 #include "signal_source_base.h"
23 #include <gnuradio/blocks/file_sink.h> // for dump
24 #include <gnuradio/blocks/file_source.h>
25 #include <gnuradio/blocks/throttle.h>
26 #include <pmt/pmt.h>
27 #include <cstddef>
28 #include <string>
29 #include <tuple>
30 
31 /** \addtogroup Signal_Source
32  * \{ */
33 /** \addtogroup Signal_Source_adapters
34  * \{ */
35 
37 
38 
39 //! \brief Base class to file-oriented SignalSourceBase GNSS blocks.
40 //!
41 //! This class supports the following properties:
42 //!
43 //! .filename - the path to the input file
44 //! - may be overridden by the -signal_source or -s command-line arguments
45 //!
46 //! .samples - number of samples to process (default 0)
47 //! - if not specified or 0, read the entire file; otherwise stop after that many samples
48 //!
49 //! .sampling_frequency - the frequency of the sampled data (samples/second)
50 //!
51 //! .item_type - data type of the samples (default "short")
52 //!
53 //! .header_size - the size of a prefixed header to skip in "samples" (default 0)
54 //!
55 //! .seconds_to_skip - number of seconds of lead-in data to skip over (default 0)
56 //!
57 //! .enable_throttle_control - whether to stop reading if the upstream buffer is full (default false)
58 //!
59 //! .repeat - whether to rewind and continue at end of file (default false)
60 //!
61 //! (probably abstracted to the base class)
62 //!
63 //! .dump - whether to archive input data
64 //!
65 //! .dump_filename - if dumping, path to file for output
67 {
68 public:
69  // Virtual overrides
70  void connect(gr::top_block_sptr top_block) override;
71  void disconnect(gr::top_block_sptr top_block) override;
72  gr::basic_block_sptr get_left_block() override;
73  gr::basic_block_sptr get_right_block() override;
74 
75  //! The file to read
76  std::string filename() const;
77 
78  //! The item type
79  std::string item_type() const;
80 
81  //! The configured size of each item
82  size_t item_size() override;
83  virtual size_t item_size() const; // what the interface **should** have declared
84 
85  //! Whether to repeat reading after end-of-file
86  bool repeat() const;
87 
88  //! The sampling frequency of the source file
89  int64_t sampling_frequency() const;
90 
91  //! The number of samples in the file
92  uint64_t samples() const;
93 
94 protected:
95  //! \brief Constructor
96  //!
97  //! Subclasses may want to assert default item types that are appropriate to the specific file
98  //! type supported. Rather than require the item type to be specified in the config file, allow
99  //! sub-classes to impose their will
100  FileSourceBase(ConfigurationInterface const* configuration, std::string const& role, std::string impl,
102  std::string default_item_type = "short");
103 
104  //! Perform post-construction initialization
105  void init();
106 
107  //! Compute the item size, from the item_type(). Subclasses may constrain types that don't make
108  // sense. The return of this method is a tuple of item_size and is_complex
109  virtual std::tuple<size_t, bool> itemTypeToSize();
110 
111  //! The number of (possibly unpacked) samples in a (raw) file sample (default=1)
112  virtual double packetsPerSample() const;
113 
114  //! Compute the number of samples to skip
115  virtual size_t samplesToSkip() const;
116 
117  //! Compute the number of samples in the file
118  size_t computeSamplesInFile() const;
119 
120  //! Abstracted front-end source. Sub-classes may override if they create specialized chains to
121  //! decode source files into a usable format
122  virtual gnss_shared_ptr<gr::block> source() const;
123 
124  //! For complex source chains, the size of the file item may not be the same as the size of the
125  // "source" (decoded) item. This method allows subclasses to handle these differences
126  virtual size_t source_item_size() const;
127  bool is_complex() const;
128 
129  // Generic access to created objects
130  gnss_shared_ptr<gr::block> file_source() const;
131  gnss_shared_ptr<gr::block> valve() const;
132  gnss_shared_ptr<gr::block> throttle() const;
133  gnss_shared_ptr<gr::block> sink() const;
134 
135  // The methods create the various blocks, if enabled, and return access to them. The created
136  // object is also held in this class
137  gr::blocks::file_source::sptr create_file_source();
138  gr::blocks::throttle::sptr create_throttle();
139  gnss_shared_ptr<gr::block> create_valve();
140  gr::blocks::file_sink::sptr create_sink();
141 
142  // Subclass hooks to augment created objects, as required
143  virtual void create_file_source_hook();
144  virtual void create_throttle_hook();
145  virtual void create_valve_hook();
146  virtual void create_sink_hook();
147 
148  // Subclass hooks for connection/disconnection
149  virtual void pre_connect_hook(gr::top_block_sptr top_block);
150  virtual void post_connect_hook(gr::top_block_sptr top_block);
151  virtual void pre_disconnect_hook(gr::top_block_sptr top_block);
152  virtual void post_disconnect_hook(gr::top_block_sptr top_block);
153 
154 private:
155  gr::blocks::file_source::sptr file_source_;
156  gr::blocks::throttle::sptr throttle_;
157  gr::blocks::file_sink::sptr sink_;
158 
159  // The valve allows only the configured number of samples through, then it closes.
160 
161  // The framework passes the queue as a naked pointer, rather than a shared pointer, so this
162  // class has two choices: construct the valve in the ctor, or hold onto the pointer, possibly
163  // beyond its lifetime. Fortunately, the queue is only used to create the valve, so the
164  // likelihood of holding a stale pointer is mitigated
165  gnss_shared_ptr<gr::block> valve_;
167 
168  std::string role_;
169  std::string filename_;
170  std::string dump_filename_;
171  std::string item_type_;
172  size_t item_size_;
173  size_t header_size_; // length (in samples) of the header (if any)
174  uint64_t samples_;
175  int64_t sampling_frequency_; // why is this signed
176  double minimum_tail_s_;
177  double seconds_to_skip_;
178  bool is_complex_; // a misnomer; if I/Q are interleaved as integer values
179  bool repeat_;
180  bool enable_throttle_control_;
181  bool dump_;
182 };
183 
184 /** \} */
185 /** \} */
186 #endif // GNSS_SDR_FILE_SOURCE_BASE_H
Interface of a thread-safe std::queue.
size_t computeSamplesInFile() const
Compute the number of samples in the file.
void init()
Perform post-construction initialization.
Header file of the base class to signal_source GNSS blocks.
std::string item_type() const
The item type.
virtual std::tuple< size_t, bool > itemTypeToSize()
Compute the item size, from the item_type(). Subclasses may constrain types that don&#39;t make...
FileSourceBase(ConfigurationInterface const *configuration, std::string const &role, std::string impl, Concurrent_Queue< pmt::pmt_t > *queue, std::string default_item_type="short")
Constructor.
This abstract class represents an interface to configuration parameters.
size_t item_size() override
The configured size of each item.
int64_t sampling_frequency() const
The sampling frequency of the source file.
bool repeat() const
Whether to repeat reading after end-of-file.
virtual gnss_shared_ptr< gr::block > source() const
Abstracted front-end source. Sub-classes may override if they create specialized chains to decode sou...
Base class to file-oriented SignalSourceBase GNSS blocks.
virtual size_t source_item_size() const
For complex source chains, the size of the file item may not be the same as the size of the...
virtual size_t samplesToSkip() const
Compute the number of samples to skip.
std::string filename() const
The file to read.
virtual double packetsPerSample() const
The number of (possibly unpacked) samples in a (raw) file sample (default=1)
uint64_t samples() const
The number of samples in the file.