OBECA Receive Process
LimeSdrReader.h
1 // OBECA - Open Broadcast Edge Cache Appliance
2 // Receive 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 <lime/LimeSuite.h>
23 #include <ring_buffer.h>
24 #include <string>
25 #include <thread>
26 #include <cstdint>
27 #include <libconfig.h++>
28 #include "srslte/srslte.h"
29 
37  public:
43  explicit LimeSdrReader(const libconfig::Config& cfg)
44  : _buffer(bev::linear_ringbuffer::delayed_init {})
45  , _overflows(0)
46  , _underflows(0)
47  , _cfg(cfg)
48  , _readerThread{} {}
49 
53  virtual ~LimeSdrReader();
54 
58  bool init(uint8_t device_index, const char* sample_file, const char* write_sample_file);
59 
63  bool setSampleRate(unsigned sample_rate);
64 
68  bool tune(uint32_t frequency, uint32_t sample_rate, uint32_t bandwidth, double gain, const std::string& antenna);
69 
73  void start();
74 
78  void stop();
79 
83  void clear_buffer();
84 
92  int getSamples(cf_t* data, uint32_t nsamples, srslte_timestamp_t* rx_time);
93 
97  double get_sample_rate() { return _sampleRate; }
98 
102  double get_frequency() { return _frequency; }
103 
107  unsigned get_filter_bw() { return _filterBw; }
108 
112  double get_gain() { return _gain; }
113 
117  double get_buffer_level() { if (!_buffer_ready) { return 0; } return static_cast<double>(_buffer.size()) / static_cast<double>(_buffer.capacity()); }
118 
122  std::string get_antenna() { return _antenna; }
123 
127  uint32_t rssi() { return _rssi; }
128 
129  private:
130  void read();
131  void* _sdr = nullptr;
132 
133  const libconfig::Config& _cfg;
134  lms_stream_t _stream;
135  bev::linear_ringbuffer _buffer;
136  std::thread _readerThread;
137  bool _running;
138 
139  double _sampleRate;
140  double _frequency;
141  unsigned _filterBw;
142  double _gain;
143  std::string _antenna;
144  unsigned _overflows;
145  unsigned _underflows;
146 
147  cf_t* _read_buffer;
148 
149  srslte_filesource_t file_source;
150  srslte_filesink_t file_sink;
151 
152  std::chrono::steady_clock::time_point _last_read;
153 
154  bool _high_watermark_reached = false;
155  int _sleep_adjustment = 0;
156 
157  unsigned _buffer_ms = 200;
158  bool _buffer_ready = false;
159  bool _reading_from_file = false;
160  bool _writing_to_file = false;
161 
162  uint32_t _rssi = 0;
163 };
LimeSdrReader::get_buffer_level
double get_buffer_level()
Get current ringbuffer level (0 = empty .
Definition: LimeSdrReader.h:117
LimeSdrReader::get_frequency
double get_frequency()
Get current center frequency.
Definition: LimeSdrReader.h:102
LimeSdrReader::get_antenna
std::string get_antenna()
Get current antenna port.
Definition: LimeSdrReader.h:122
LimeSdrReader::tune
bool tune(uint32_t frequency, uint32_t sample_rate, uint32_t bandwidth, double gain, const std::string &antenna)
Tune the SDR to the desired frequency, and set gain, filter and antenna parameters.
Definition: LimeSdrReader.cpp:129
LimeSdrReader::getSamples
int getSamples(cf_t *data, uint32_t nsamples, srslte_timestamp_t *rx_time)
Store nsamples count samples into the buffer at data.
Definition: LimeSdrReader.cpp:306
LimeSdrReader::get_sample_rate
double get_sample_rate()
Get current sample rate.
Definition: LimeSdrReader.h:97
LimeSdrReader::init
bool init(uint8_t device_index, 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: LimeSdrReader.cpp:42
LimeSdrReader::setSampleRate
bool setSampleRate(unsigned sample_rate)
Adjust the sample rate.
Definition: LimeSdrReader.cpp:125
LimeSdrReader::~LimeSdrReader
virtual ~LimeSdrReader()
Default destructor.
Definition: LimeSdrReader.cpp:26
LimeSdrReader::start
void start()
Start reading samples from the SDR.
Definition: LimeSdrReader.cpp:238
LimeSdrReader::get_filter_bw
unsigned get_filter_bw()
Get current filter bandwidth.
Definition: LimeSdrReader.h:107
LimeSdrReader::rssi
uint32_t rssi()
Get RSSI estimate (disabled at the moment)
Definition: LimeSdrReader.h:127
LimeSdrReader::clear_buffer
void clear_buffer()
Clear all samples from the rx buffers.
Definition: LimeSdrReader.cpp:120
LimeSdrReader::stop
void stop()
Stop reading samples from the SDR.
Definition: LimeSdrReader.cpp:256
LimeSdrReader::LimeSdrReader
LimeSdrReader(const libconfig::Config &cfg)
Default constructor.
Definition: LimeSdrReader.h:43
LimeSdrReader::get_gain
double get_gain()
Get current gain.
Definition: LimeSdrReader.h:112
LimeSdrReader
Interface to LimeSDR via LimeSuite.
Definition: LimeSdrReader.h:36