forked from pothosware/SoapyAirspyHF
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Streaming.cpp
256 lines (199 loc) · 8.09 KB
/
Streaming.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Charles J. Cliffe
* Copyright (c) 2018 Corey Stotts
* Copyright (c) 2022 Albin Stigö
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "SoapyAirspyHF.hpp"
#include <SoapySDR/ConverterRegistry.hpp>
#include <SoapySDR/Formats.hpp>
#include <SoapySDR/Logger.hpp>
#include <algorithm>
#include <libairspyhf/airspyhf.h>
#include <memory>
#define AIRSPYHF_NATIVE_FORMAT SOAPY_SDR_CF32
std::vector<std::string>
SoapyAirspyHF::getStreamFormats(const int direction,
const size_t channel) const {
if (direction != SOAPY_SDR_RX or channel != 0) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"SoapyAirspyHF::getStreamFormats(%d, %d) invalid channel",
direction, channel);
return {};
}
// Allow all formats we can convert to
return SoapySDR::ConverterRegistry::listTargetFormats(AIRSPYHF_NATIVE_FORMAT);
}
std::string SoapyAirspyHF::getNativeStreamFormat(const int direction,
const size_t channel,
double &fullScale) const {
if (direction != SOAPY_SDR_RX or channel != 0) {
SoapySDR::logf(
SOAPY_SDR_ERROR,
"SoapyAirspyHF::getNativeStreamFormat(%d, %d) invalid channel",
direction, channel);
return {};
}
fullScale = 1.0;
return AIRSPYHF_NATIVE_FORMAT;
}
SoapySDR::ArgInfoList
SoapyAirspyHF::getStreamArgsInfo(const int direction,
const size_t channel) const {
SoapySDR::ArgInfoList streamArgs;
if (direction != SOAPY_SDR_RX or channel != 0) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"SoapyAirspyHF::getStreamArgsInfo(%d, %d) invalid channel",
direction, channel);
return streamArgs;
}
return streamArgs;
}
// Static trampoline for libairspyhf callback
static int rx_callback_(airspyhf_transfer_t *transfer) {
// Stream handle
SoapySDR::Stream *stream = (SoapySDR::Stream *)transfer->ctx;
const uint32_t timeout_us = 500'000; // 500ms
const auto written = stream->ringbuffer().write_at_least(
static_cast<size_t>(transfer->sample_count),
std::chrono::microseconds(timeout_us),
[&](airspyhf_complex_float_t *begin,
[[maybe_unused]] const uint32_t available) {
// Copy samples to ringbuffer, conversion is done in readStream if
// needed.
std::copy(transfer->samples, transfer->samples + transfer->sample_count,
begin);
return transfer->sample_count;
});
// Add ticks
stream->addTicks(transfer->sample_count);
if (written < 0) {
SoapySDR::logf(SOAPY_SDR_INFO,
"SoapyAirspyHF::rx_callback: ringbuffer write timeout");
return 0;
}
return 0; // anything else is an error.
}
/*******************************************************************
* Stream API
******************************************************************/
SoapySDR::Stream *
SoapyAirspyHF::setupStream(const int direction, const std::string &format,
const std::vector<size_t> &channels,
const SoapySDR::Kwargs &args) {
(void)args; // Currently unused
if (direction != SOAPY_SDR_RX or channels.size() != 1 or
channels.at(0) != 0) {
SoapySDR::logf(SOAPY_SDR_INFO,
"SoapyAirspyHF::setupStream(%d, %s, %d, %d) invalid channel",
direction, format.c_str(), channels.size(), channels.at(0));
}
const auto &sources = SoapySDR::ConverterRegistry::listSourceFormats(format);
// Check there is a convert function that can convert from our native format.
if (std::find(sources.begin(), sources.end(), AIRSPYHF_NATIVE_FORMAT) ==
sources.end()) {
throw std::runtime_error("setupStream invalid format '" + format + "'.");
}
// Find converter function
const auto converterFunction = SoapySDR::ConverterRegistry::getFunction(
AIRSPYHF_NATIVE_FORMAT, format, SoapySDR::ConverterRegistry::GENERIC);
SoapySDR::logf(SOAPY_SDR_INFO, "setupStream: format=%s", format.c_str());
// Get MTU
const auto mtu = airspyhf_get_output_size(device_);
// Create stream;
stream_ = std::make_unique<SoapySDR::Stream>(device_, sampleRate_,
converterFunction, mtu);
// Return point to stream
return stream_.get();
}
void SoapyAirspyHF::closeStream(SoapySDR::Stream *stream) {
// Check that stream is current
if (stream != stream_.get()) {
SoapySDR::logf(SOAPY_SDR_ERROR, "closeStream: invalid stream");
return;
}
stream_.reset();
}
size_t SoapyAirspyHF::getStreamMTU(SoapySDR::Stream *stream) const {
return stream->MTU();
}
int SoapyAirspyHF::activateStream(SoapySDR::Stream *stream, const int flags,
const long long timeNs,
const size_t numElems) {
int ret = 0;
// rate limit this log?
if (flags != 0) {
SoapySDR::logf(SOAPY_SDR_DEBUG, "activateStream: flags not supported");
}
// Clear buffer
stream->ringbuffer().clear();
// Reset ticks
stream->ticks_ = 0;
// Start the stream
ret = airspyhf_start(device_, &rx_callback_, (void *)stream);
if (ret != AIRSPYHF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR, "activateStream: airspyhf_start failed: %d",
ret);
return SOAPY_SDR_STREAM_ERROR;
}
SoapySDR::logf(SOAPY_SDR_DEBUG,
"activateStream: flags=%d, timeNs=%lld, numElems=%d", flags,
timeNs, numElems);
return 0;
}
int SoapyAirspyHF::deactivateStream(SoapySDR::Stream *stream, const int flags,
const long long timeNs) {
int ret = 0;
SoapySDR::logf(SOAPY_SDR_DEBUG, "deactivateStream: flags=%d, timeNs=%lld",
flags, timeNs);
if (flags != 0) {
SoapySDR::logf(SOAPY_SDR_DEBUG, "deactivateStream: flags not supported");
}
// Stop streaming
ret = airspyhf_stop(stream->device());
if (ret != AIRSPYHF_SUCCESS) {
SoapySDR::logf(SOAPY_SDR_ERROR,
"deactivateStream: airspyhf_stop() failed: %d", ret);
return SOAPY_SDR_STREAM_ERROR;
}
return 0;
}
int SoapyAirspyHF::readStream(SoapySDR::Stream *stream, void *const *buffs,
const size_t numElems, int &flags,
long long &timeNs, const long timeoutUs) {
// Flags are not used by this driver
flags = 0;
// Convert either requested number of elements or the MTU.
const auto to_convert = std::min(numElems, getStreamMTU(stream));
const auto converted = stream->ringbuffer().read_at_least(
to_convert, std::chrono::microseconds(timeoutUs),
[&](const airspyhf_complex_float_t *begin,
[[maybe_unused]] const uint32_t available) {
// Convert samples to output buffer
stream->converter()(begin, buffs[0], to_convert, 1.0);
// Consume from ringbuffer
return to_convert;
});
timeNs = stream->timeNs();
if (converted < 0) {
SoapySDR::logf(SOAPY_SDR_INFO, "readStream: ringbuffer read timeout.");
return SOAPY_SDR_TIMEOUT;
}
return static_cast<int>(converted);
}