SoDaRadio-12.2.0-cut_dependencies:6c82803
Loading...
Searching...
No Matches
RTLSDRRX.hxx
Go to the documentation of this file.
1#pragma once
2/*
3Copyright (c) 2026 Matthew H. Reilly (kb1vc)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9
10 Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
45
46#include "RadioRX.hxx"
47#include "Params.hxx"
49#include "RTLSDRDev.hxx"
50#include <SoDa/ReSampler.hxx>
51#include <vector>
52#include <complex>
53#include <memory>
54#include <mutex>
55#include <thread>
56#include <atomic>
57
58namespace SoDa {
59
60 class RTLSDRRX;
61 using RTLSDRRXPtr = std::shared_ptr<RTLSDRRX>;
62
63 class RTLSDRRX : public RadioRX {
64 protected:
66
67 public:
68 ~RTLSDRRX() = default;
69
71 auto ret = std::shared_ptr<RTLSDRRX>(new RTLSDRRX(dev_in, params));
72 ret->self = ret;
73 ret->registerThread(ret);
74 return ret;
75 }
76
77 void init() override;
78
79 void setNCOFreq(double freq) override;
80 bool downConvert() override;
81
82 void startStream() override;
83 void stopStream() override;
84 bool streamEnabled() override { return streaming; }
85 void enableIFStreamer(bool enable) override { if_streaming_enabled = enable; }
86
87 private:
88 void doMixer(std::vector<std::complex<float>> & buf);
89 static void asyncCallback(unsigned char* buf, uint32_t len, void* ctx);
90
92
93 static constexpr float HW_RATE = 2048000.0f; // RTL2832U actual USB delivery rate
94 static constexpr unsigned int READ_BYTES = 16384; // always divisible by 512
95 static constexpr unsigned int RAW_ACCU_MAX = 8; // max resampler blocks to buffer (~400ms)
96
97 unsigned int rs_in_size;
98 unsigned int rs_out_size;
99 unsigned int rf_buf_size;
100
102
103 std::atomic<bool> streaming{false};
105
106 std::atomic<unsigned int> diag_reads{0};
107 unsigned int diag_resamp_runs;
108 unsigned int diag_published;
109
110 SoDa::ReSamplerPtr hw_resampler;
111
112 std::mutex raw_accu_mutex;
113 std::vector<std::complex<float>> raw_accu;
114 std::vector<std::complex<float>> rs_in;
115 std::vector<std::complex<float>> rs_out;
116 std::vector<std::complex<float>> accu;
117
118 std::thread async_rx_thread;
119
121
122 std::weak_ptr<RTLSDRRX> self;
123 };
124
125} // namespace SoDa
Shared RAII wrapper for an rtlsdr_dev_t handle.
Thread class that provides the common IF receive functions for any supported radio (USRP/Adalm/Pluto/...
sin/cos oscillator to drive TX signal chain
unsigned int diag_resamp_runs
total resampler invocations
Definition RTLSDRRX.hxx:107
bool if_streaming_enabled
Definition RTLSDRRX.hxx:104
void startStream() override
flush the input rx data stream and set the stream_processing flag to on.
std::atomic< unsigned int > diag_reads
async callback invocations
Definition RTLSDRRX.hxx:106
std::vector< std::complex< float > > rs_in
exact-size input to resampler (rs_in_size)
Definition RTLSDRRX.hxx:114
std::thread async_rx_thread
Definition RTLSDRRX.hxx:118
static constexpr unsigned int READ_BYTES
Definition RTLSDRRX.hxx:94
unsigned int rf_buf_size
publish chunk size (30000 samples at 625 kSPS)
Definition RTLSDRRX.hxx:99
void doMixer(std::vector< std::complex< float > > &buf)
std::weak_ptr< RTLSDRRX > self
Definition RTLSDRRX.hxx:122
~RTLSDRRX()=default
static constexpr float HW_RATE
Definition RTLSDRRX.hxx:93
void stopStream() override
flush the input rx data stream and set the stream_processing flag to off
double rx_sample_rate
625 kSPS — used for IF NCO phase increment
Definition RTLSDRRX.hxx:101
std::vector< std::complex< float > > rs_out
resampler output (rs_out_size)
Definition RTLSDRRX.hxx:115
void init() override
perform initialization.
static void asyncCallback(unsigned char *buf, uint32_t len, void *ctx)
static constexpr unsigned int RAW_ACCU_MAX
Definition RTLSDRRX.hxx:95
QuadratureOscillator IF_osc
Definition RTLSDRRX.hxx:120
std::vector< std::complex< float > > raw_accu
IQ accumulator filled by async callback.
Definition RTLSDRRX.hxx:113
unsigned int rs_out_size
resampler output size (at 625 kSPS)
Definition RTLSDRRX.hxx:98
bool downConvert() override
A receiver must accept input data from the rx_stream and beat it against its NCO to produce output on...
std::mutex raw_accu_mutex
Definition RTLSDRRX.hxx:112
std::atomic< bool > streaming
Definition RTLSDRRX.hxx:103
RTLSDRDevPtr rtl
Definition RTLSDRRX.hxx:91
SoDa::ReSamplerPtr hw_resampler
2.048 MSPS → 625 kSPS
Definition RTLSDRRX.hxx:110
bool streamEnabled() override
test the processing stream flag
Definition RTLSDRRX.hxx:84
unsigned int diag_published
total rf_buf_size blocks published
Definition RTLSDRRX.hxx:108
unsigned int rs_in_size
exact resampler input size (at 2.048 MSPS)
Definition RTLSDRRX.hxx:97
void setNCOFreq(double freq) override
Methods that ALL radios must implement.
static RTLSDRRXPtr make(RTLSDRDevPtr dev_in, ParamsPtr params)
Definition RTLSDRRX.hxx:70
RTLSDRRX(RTLSDRDevPtr dev_in, ParamsPtr params)
void enableIFStreamer(bool enable) override
Enable IF streamer - This passes the incoming RF sample buffer onto the IF stream where it can be con...
Definition RTLSDRRX.hxx:85
std::vector< std::complex< float > > accu
accumulates resampled IQ for rf_buf_size publish
Definition RTLSDRRX.hxx:116
RadioRX(ParamsPtr params)
Constructor Build a RadioRX thread.
ParamsPtr params
Definition RadioRX.hxx:91
std::shared_ptr< RTLSDRDev > RTLSDRDevPtr
Definition RTLSDRDev.hxx:62
std::shared_ptr< Params > ParamsPtr
Definition Params.hxx:42
std::shared_ptr< RTLSDRRX > RTLSDRRXPtr
Definition RTLSDRRX.hxx:61