-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoapySpyServerClient.hpp
202 lines (141 loc) · 6.22 KB
/
SoapySpyServerClient.hpp
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
// Copyright (c) 2022 Nicholas Corgan
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "spyserver_client.h"
#include <SoapySDR/Constants.h>
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Types.hpp>
#include <volk/volk_alloc.hh>
#include <atomic>
#include <cassert>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
struct SDRPPClient
{
static constexpr size_t MaxQueueSize = 128;
static constexpr size_t TimeoutMs = 1000;
std::unique_ptr<DSPComplexBufferQueue> bufferQueue;
spyserver::SpyServerClient client;
inline bool syncFields(void) const
{
assert(client);
assert(client->isOpen());
return (client->waitForDevInfo(TimeoutMs) and client->waitForClientSync(TimeoutMs));
}
};
struct SoapySpyServerStream
{
std::atomic_bool active{false};
};
class SoapySpyServerClient: public SoapySDR::Device
{
public:
SoapySpyServerClient(const SoapySDR::Kwargs &args);
virtual ~SoapySpyServerClient(void) = default;
/*******************************************************************
* Utility
******************************************************************/
static SDRPPClient makeSDRPPClient(const SoapySDR::Kwargs &args);
static std::string ParamsToSpyServerURL(
const std::string &host,
const std::string &port);
static std::string DeviceEnumToName(const uint32_t deviceType);
/*******************************************************************
* Identification API
******************************************************************/
std::string getDriverKey(void) const;
std::string getHardwareKey(void) const;
SoapySDR::Kwargs getHardwareInfo(void) const;
/*******************************************************************
* Channels API
******************************************************************/
inline bool validChannelParams(const int direction, const size_t channel) const
{
return (direction == SOAPY_SDR_RX) and (channel == 0);
}
size_t getNumChannels(const int direction) const;
SoapySDR::Kwargs getChannelInfo(const int direction, const size_t channel) const;
/*******************************************************************
* Stream API
******************************************************************/
inline bool validStream(SoapySDR::Stream* stream)
{
return (stream == (SoapySDR::Stream*)_stream.get());
}
std::vector<std::string> getStreamFormats(const int direction, const size_t channel) const;
SoapySDR::Stream *setupStream(
const int direction,
const std::string &format,
const std::vector<size_t> &channels,
const SoapySDR::Kwargs &args);
void closeStream(SoapySDR::Stream *stream);
int activateStream(
SoapySDR::Stream *stream,
const int flags,
const long long timeNs,
const size_t numElems);
int deactivateStream(
SoapySDR::Stream *stream,
const int flags,
const long long timeNs);
int readStream(
SoapySDR::Stream *stream,
void * const *buffs,
const size_t numElems,
int &flags,
long long &timeNs,
const long timeoutUs);
/*******************************************************************
* Antenna API
******************************************************************/
static const std::string AntennaName;
std::vector<std::string> listAntennas(const int direction, const size_t channel) const;
void setAntenna(const int direction, const size_t channel, const std::string &name);
std::string getAntenna(const int direction, const size_t channel) const;
/*******************************************************************
* Gain API
******************************************************************/
static const std::string GainName;
inline bool validGainParams(const int direction, const size_t channel, const std::string &name) const
{
return validChannelParams(direction, channel) and (name == GainName);
}
std::vector<std::string> listGains(const int direction, const size_t channel) const;
void setGain(const int direction, const size_t channel, const std::string &name, const double value);
double getGain(const int direction, const size_t channel, const std::string &name) const;
SoapySDR::Range getGainRange(const int direction, const size_t channel, const std::string &name) const;
/*******************************************************************
* Frequency API
******************************************************************/
static const std::string FrequencyName;
inline bool validFrequencyParams(const int direction, const size_t channel, const std::string &name) const
{
return validChannelParams(direction, channel) and (name == FrequencyName);
}
void setFrequency(const int direction, const size_t channel, const std::string &name, const double frequency, const SoapySDR::Kwargs &args);
double getFrequency(const int direction, const size_t channel, const std::string &name) const;
std::vector<std::string> listFrequencies(const int direction, const size_t channel) const;
SoapySDR::RangeList getFrequencyRange(const int direction, const size_t channel, const std::string &name) const;
/*******************************************************************
* Sample Rate API
******************************************************************/
void setSampleRate(const int direction, const size_t channel, const double rate);
double getSampleRate(const int direction, const size_t channel) const;
// Intentionally using deprecated API, no guaranteed step. Let Soapy deal with it.
std::vector<double> listSampleRates(const int direction, const size_t channel) const;
private:
//
// Fields
//
static constexpr int SDRPP_TIMEOUT_MS = 1000;
std::string _spyServerURL;
SDRPPClient _sdrppClient;
volk::vector<dsp::complex_t> _currentBuffer;
size_t _startIndex{0};
double _sampleRate{0.0};
std::vector<std::pair<uint32_t, double>> _sampleRates;
std::unique_ptr<SoapySpyServerStream> _stream;
mutable std::mutex _streamMutex;
};