5G-MAG Reference Tools - MBMS Modem
SdrReader.h
Go to the documentation of this file.
1 // 5G-MAG Reference Tools
2 // MBMS Modem Process
3 //
4 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Affero General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program 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 Affero General Public License for more details.
15 //
16 // You should have received a copy of the GNU Affero General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #pragma once
21 
22 #include <string>
23 #include <vector>
24 #include <thread>
25 #include <map>
26 #include <cstdint>
27 #include <libconfig.h++>
28 #include "srsran/srsran.h"
29 #include "MultichannelRingbuffer.h"
30 
37 class SdrReader {
38 public:
44  explicit SdrReader(const libconfig::Config &cfg, size_t rx_channels)
45  : _overflows(0), _underflows(0), _cfg(cfg), _rx_channels(rx_channels), _readerThread{} {}
46 
50  virtual ~SdrReader();
51 
55  void enumerateDevices();
56 
60  bool init(const std::string &device_args, const char *sample_file, const char *write_sample_file);
61 
65  bool tune(uint32_t frequency, uint32_t sample_rate, uint32_t bandwidth, double gain, const std::string &antenna,
66  bool use_agc);
67 
71  void start();
72 
76  void stop();
77 
81  void clear_buffer();
82 
90  int get_samples(cf_t *data[SRSRAN_MAX_CHANNELS], uint32_t nsamples, srsran_timestamp_t *rx_time);
91 
95  double get_sample_rate() { return _sampleRate; }
96 
100  double get_frequency() { return _frequency; }
101 
105  unsigned get_filter_bw() { return _filterBw; }
106 
110  double get_gain() { return _gain; }
111 
115  double get_buffer_level();
116 
120  std::string get_antenna() { return _antenna; }
121 
125  uint32_t rssi() { return _rssi; }
126 
127  double min_gain() { return _min_gain; }
128 
129  double max_gain() { return _max_gain; }
130 
135 
140 
141 private:
142  void init_buffer();
143 
144  bool set_gain(bool use_agc, double gain, uint8_t idx);
145 
146  bool set_sample_rate(uint32_t rate, uint8_t idx);
147 
148  bool set_filter_bw(uint32_t bandwidth, uint8_t idx);
149 
150  bool set_antenna(const std::string &antenna, uint8_t idx);
151 
152  bool set_frequency(uint32_t frequency, uint8_t idx);
153 
154  void read();
155 
156  void *_sdr = nullptr;
157  void *_stream = nullptr;
158 
159  const libconfig::Config &_cfg;
160 
161  std::unique_ptr<MultichannelRingbuffer> _buffer;
162 
163  std::thread _readerThread;
164  bool _running;
165 
166  unsigned _rx_channels = 1;
167  double _sampleRate;
168  double _frequency;
169  unsigned _filterBw;
170  double _gain;
171  bool _use_agc;
172  double _min_gain;
173  double _max_gain;
174  std::string _antenna;
175  unsigned _overflows;
176  unsigned _underflows;
177 
179 
180  srsran_filesource_t file_source;
181  srsran_filesink_t file_sink;
182 
183  std::chrono::steady_clock::time_point _last_read;
184 
187 
188  unsigned _buffer_ms = 200;
189  bool _buffer_ready = false;
190  bool _reading_from_file = false;
191  bool _writing_to_file = false;
192  bool _write_samples = false;
193 
194  uint32_t _rssi = 0;
195 
197  std::string _temp_sensor_key = {};
198 
199  std::map<std::string, std::string> _device_args;
200 };
Interface to the SDR stick.
Definition: SdrReader.h:37
bool _reading_from_file
Definition: SdrReader.h:190
double get_frequency()
Get current center frequency.
Definition: SdrReader.h:100
void stop()
Stop reading samples from the SDR.
Definition: SdrReader.cpp:245
const libconfig::Config & _cfg
Definition: SdrReader.h:159
unsigned _filterBw
Definition: SdrReader.h:169
void start()
Start reading samples from the SDR.
Definition: SdrReader.cpp:213
double get_buffer_level()
Get current ringbuffer level (0 = empty .
Definition: SdrReader.cpp:369
bool _high_watermark_reached
Definition: SdrReader.h:185
std::string _temp_sensor_key
Definition: SdrReader.h:197
bool init(const std::string &device_args, const char *sample_file, const char *write_sample_file)
Initializes the SDR interface and creates a ring buffer according to the params from Cfg.
Definition: SdrReader.cpp:65
uint32_t _rssi
Definition: SdrReader.h:194
void disableSampleFileWriting()
If sample file creation is enabled, writing samples stops after this call.
Definition: SdrReader.h:139
SdrReader(const libconfig::Config &cfg, size_t rx_channels)
Default constructor.
Definition: SdrReader.h:44
void enumerateDevices()
Prints a list of all available SDR devices.
Definition: SdrReader.cpp:48
std::thread _readerThread
Definition: SdrReader.h:163
int get_samples(cf_t *data[SRSRAN_MAX_CHANNELS], uint32_t nsamples, srsran_timestamp_t *rx_time)
Store nsamples count samples into the buffer at data.
Definition: SdrReader.cpp:315
unsigned get_filter_bw()
Get current filter bandwidth.
Definition: SdrReader.h:105
std::string get_antenna()
Get current antenna port.
Definition: SdrReader.h:120
srsran_filesource_t file_source
Definition: SdrReader.h:180
bool set_gain(bool use_agc, double gain, uint8_t idx)
Definition: SdrReader.cpp:143
uint32_t rssi()
Get RSSI estimate (disabled at the moment)
Definition: SdrReader.h:125
srsran_filesink_t file_sink
Definition: SdrReader.h:181
void * _stream
Definition: SdrReader.h:157
unsigned _rx_channels
Definition: SdrReader.h:166
double max_gain()
Definition: SdrReader.h:129
bool _writing_to_file
Definition: SdrReader.h:191
bool _running
Definition: SdrReader.h:164
void clear_buffer()
Clear all samples from the rx buffers.
Definition: SdrReader.cpp:107
double _max_gain
Definition: SdrReader.h:173
std::map< std::string, std::string > _device_args
Definition: SdrReader.h:199
bool _buffer_ready
Definition: SdrReader.h:189
double min_gain()
Definition: SdrReader.h:127
bool set_sample_rate(uint32_t rate, uint8_t idx)
Definition: SdrReader.cpp:137
double _min_gain
Definition: SdrReader.h:172
bool set_antenna(const std::string &antenna, uint8_t idx)
Definition: SdrReader.cpp:112
void init_buffer()
Definition: SdrReader.cpp:101
bool _use_agc
Definition: SdrReader.h:171
int _sleep_adjustment
Definition: SdrReader.h:186
bool tune(uint32_t frequency, uint32_t sample_rate, uint32_t bandwidth, double gain, const std::string &antenna, bool use_agc)
Tune the SDR to the desired frequency, and set gain, filter and antenna parameters.
Definition: SdrReader.cpp:166
std::unique_ptr< MultichannelRingbuffer > _buffer
Definition: SdrReader.h:161
bool set_filter_bw(uint32_t bandwidth, uint8_t idx)
Definition: SdrReader.cpp:131
virtual ~SdrReader()
Default destructor.
Definition: SdrReader.cpp:31
unsigned _overflows
Definition: SdrReader.h:175
std::chrono::steady_clock::time_point _last_read
Definition: SdrReader.h:183
void enableSampleFileWriting()
If sample file creation is enabled, writing samples starts after this call.
Definition: SdrReader.h:134
double get_gain()
Get current gain.
Definition: SdrReader.h:110
double _gain
Definition: SdrReader.h:170
void * _sdr
Definition: SdrReader.h:156
bool _temp_sensor_available
Definition: SdrReader.h:196
bool set_frequency(uint32_t frequency, uint8_t idx)
Definition: SdrReader.cpp:125
bool _write_samples
Definition: SdrReader.h:192
unsigned _underflows
Definition: SdrReader.h:176
double _sampleRate
Definition: SdrReader.h:167
unsigned _buffer_ms
Definition: SdrReader.h:188
void read()
Definition: SdrReader.cpp:258
double _frequency
Definition: SdrReader.h:168
cf_t * _read_buffer
Definition: SdrReader.h:178
double get_sample_rate()
Get current sample rate.
Definition: SdrReader.h:95
std::string _antenna
Definition: SdrReader.h:174
static Config cfg
Global configuration object.
Definition: main.cpp:165
static std::string antenna
Antenna input to be used.
Definition: main.cpp:172
static bool use_agc
Definition: main.cpp:173
static unsigned sample_rate
Sample rate of the SDR.
Definition: main.cpp:167
static uint32_t bandwidth
Low pass filter bandwidth for the SDR.
Definition: main.cpp:170
static unsigned frequency
Center freqeuncy the SDR is tuned to.
Definition: main.cpp:169
static double gain
Overall system gain for the SDR.
Definition: main.cpp:171