1 // Copyright (c) 2015-2018 Josh Blum
2 // Copyright (c) 2016-2016 Bastille Networks
3 // SPDX-License-Identifier: BSL-1.0
4 
5 #pragma once
6 #include "SoapyRPCSocket.hpp"
7 #include <SoapySDR/Device.hpp>
8 #include <mutex>
9 
10 class SoapyLogAcceptor;
11 
12 class SoapyRemoteDevice : public SoapySDR::Device
13 {
14 public:
15     SoapyRemoteDevice(const std::string &url, const SoapySDR::Kwargs &args);
16 
17     ~SoapyRemoteDevice(void);
18 
19     //! Helper routine to cache server discovery
20     static std::vector<std::string> getServerURLs(const int ipVer, const long timeoutUs);
21 
22     /*******************************************************************
23      * Identification API
24      ******************************************************************/
25 
26     std::string getDriverKey(void) const;
27 
28     std::string getHardwareKey(void) const;
29 
30     SoapySDR::Kwargs getHardwareInfo(void) const;
31 
32     /*******************************************************************
33      * Channels API
34      ******************************************************************/
35 
36     void setFrontendMapping(const int direction, const std::string &mapping);
37 
38     std::string getFrontendMapping(const int direction) const;
39 
40     size_t getNumChannels(const int direction) const;
41 
42     SoapySDR::Kwargs getChannelInfo(const int direction, const size_t channel) const;
43 
44     bool getFullDuplex(const int direction, const size_t channel) const;
45 
46     /*******************************************************************
47      * Stream API
48      ******************************************************************/
49 
50     std::vector<std::string> __getRemoteOnlyStreamFormats(const int direction, const size_t channel) const;
51 
52     std::vector<std::string> getStreamFormats(const int direction, const size_t channel) const;
53 
54     std::string getNativeStreamFormat(const int direction, const size_t channel, double &fullScale) const;
55 
56     SoapySDR::ArgInfoList getStreamArgsInfo(const int direction, const size_t channel) const;
57 
58     SoapySDR::Stream *setupStream(
59         const int direction,
60         const std::string &format,
61         const std::vector<size_t> &channels,
62         const SoapySDR::Kwargs &args);
63 
64     void closeStream(SoapySDR::Stream *stream);
65 
66     size_t getStreamMTU(SoapySDR::Stream *stream) const;
67 
68     int activateStream(
69         SoapySDR::Stream *stream,
70         const int flags,
71         const long long timeNs,
72         const size_t numElems);
73 
74     int deactivateStream(
75         SoapySDR::Stream *stream,
76         const int flags,
77         const long long timeNs);
78 
79     int readStream(
80         SoapySDR::Stream *stream,
81         void * const *buffs,
82         const size_t numElems,
83         int &flags,
84         long long &timeNs,
85         const long timeoutUs);
86 
87     int writeStream(
88         SoapySDR::Stream *stream,
89         const void * const *buffs,
90         const size_t numElems,
91         int &flags,
92         const long long timeNs,
93         const long timeoutUs);
94 
95     int readStreamStatus(
96         SoapySDR::Stream *stream,
97         size_t &chanMask,
98         int &flags,
99         long long &timeNs,
100         const long timeoutUs);
101 
102     /*******************************************************************
103      * Direct buffer access API
104      ******************************************************************/
105 
106     size_t getNumDirectAccessBuffers(SoapySDR::Stream *stream);
107 
108     int getDirectAccessBufferAddrs(SoapySDR::Stream *stream, const size_t handle, void **buffs);
109 
110     int acquireReadBuffer(
111         SoapySDR::Stream *stream,
112         size_t &handle,
113         const void **buffs,
114         int &flags,
115         long long &timeNs,
116         const long timeoutUs);
117 
118     void releaseReadBuffer(
119         SoapySDR::Stream *stream,
120         const size_t handle);
121 
122     int acquireWriteBuffer(
123         SoapySDR::Stream *stream,
124         size_t &handle,
125         void **buffs,
126         const long timeoutUs);
127 
128     void releaseWriteBuffer(
129         SoapySDR::Stream *stream,
130         const size_t handle,
131         const size_t numElems,
132         int &flags,
133         const long long timeNs);
134 
135     /*******************************************************************
136      * Antenna API
137      ******************************************************************/
138 
139     std::vector<std::string> listAntennas(const int direction, const size_t channel) const;
140 
141     void setAntenna(const int direction, const size_t channel, const std::string &name);
142 
143     std::string getAntenna(const int direction, const size_t channel) const;
144 
145     /*******************************************************************
146      * Frontend corrections API
147      ******************************************************************/
148 
149     bool hasDCOffsetMode(const int direction, const size_t channel) const;
150 
151     void setDCOffsetMode(const int direction, const size_t channel, const bool automatic);
152 
153     bool getDCOffsetMode(const int direction, const size_t channel) const;
154 
155     bool hasDCOffset(const int direction, const size_t channel) const;
156 
157     void setDCOffset(const int direction, const size_t channel, const std::complex<double> &offset);
158 
159     std::complex<double> getDCOffset(const int direction, const size_t channel) const;
160 
161     bool hasIQBalance(const int direction, const size_t channel) const;
162 
163     void setIQBalance(const int direction, const size_t channel, const std::complex<double> &balance);
164 
165     std::complex<double> getIQBalance(const int direction, const size_t channel) const;
166 
167     bool hasFrequencyCorrection(const int direction, const size_t channel) const;
168 
169     void setFrequencyCorrection(const int direction, const size_t channel, const double value);
170 
171     double getFrequencyCorrection(const int direction, const size_t channel) const;
172 
173     /*******************************************************************
174      * Gain API
175      ******************************************************************/
176 
177     std::vector<std::string> listGains(const int direction, const size_t channel) const;
178 
179     bool hasGainMode(const int direction, const size_t channel) const;
180 
181     void setGainMode(const int direction, const size_t channel, const bool automatic);
182 
183     bool getGainMode(const int direction, const size_t channel) const;
184 
185     void setGain(const int direction, const size_t channel, const double value);
186 
187     void setGain(const int direction, const size_t channel, const std::string &name, const double value);
188 
189     double getGain(const int direction, const size_t channel) const;
190 
191     double getGain(const int direction, const size_t channel, const std::string &name) const;
192 
193     SoapySDR::Range getGainRange(const int direction, const size_t channel) const;
194 
195     SoapySDR::Range getGainRange(const int direction, const size_t channel, const std::string &name) const;
196 
197     /*******************************************************************
198      * Frequency API
199      ******************************************************************/
200 
201     void setFrequency(const int direction, const size_t channel, const double frequency, const SoapySDR::Kwargs &args);
202 
203     void setFrequency(const int direction, const size_t channel, const std::string &name, const double frequency, const SoapySDR::Kwargs &args);
204 
205     double getFrequency(const int direction, const size_t channel) const;
206 
207     double getFrequency(const int direction, const size_t channel, const std::string &name) const;
208 
209     std::vector<std::string> listFrequencies(const int direction, const size_t channel) const;
210 
211     SoapySDR::RangeList getFrequencyRange(const int direction, const size_t channel) const;
212 
213     SoapySDR::RangeList getFrequencyRange(const int direction, const size_t channel, const std::string &name) const;
214 
215     SoapySDR::ArgInfoList getFrequencyArgsInfo(const int direction, const size_t channel) const;
216 
217     /*******************************************************************
218      * Sample Rate API
219      ******************************************************************/
220 
221     void setSampleRate(const int direction, const size_t channel, const double rate);
222 
223     double getSampleRate(const int direction, const size_t channel) const;
224 
225     std::vector<double> listSampleRates(const int direction, const size_t channel) const;
226 
227     SoapySDR::RangeList getSampleRateRange(const int direction, const size_t channel) const;
228 
229     /*******************************************************************
230      * Bandwidth API
231      ******************************************************************/
232 
233     void setBandwidth(const int direction, const size_t channel, const double bw);
234 
235     double getBandwidth(const int direction, const size_t channel) const;
236 
237     std::vector<double> listBandwidths(const int direction, const size_t channel) const;
238 
239     SoapySDR::RangeList getBandwidthRange(const int direction, const size_t channel) const;
240 
241     /*******************************************************************
242      * Clocking API
243      ******************************************************************/
244 
245     void setMasterClockRate(const double rate);
246 
247     double getMasterClockRate(void) const;
248 
249     SoapySDR::RangeList getMasterClockRates(void) const;
250 
251     std::vector<std::string> listClockSources(void) const;
252 
253     void setClockSource(const std::string &source);
254 
255     std::string getClockSource(void) const;
256 
257     /*******************************************************************
258      * Time API
259      ******************************************************************/
260 
261     std::vector<std::string> listTimeSources(void) const;
262 
263     void setTimeSource(const std::string &source);
264 
265     std::string getTimeSource(void) const;
266 
267     bool hasHardwareTime(const std::string &what) const;
268 
269     long long getHardwareTime(const std::string &what) const;
270 
271     void setHardwareTime(const long long timeNs, const std::string &what);
272 
273     void setCommandTime(const long long timeNs, const std::string &what);
274 
275     /*******************************************************************
276      * Sensor API
277      ******************************************************************/
278 
279     std::vector<std::string> listSensors(void) const;
280 
281     SoapySDR::ArgInfo getSensorInfo(const std::string &name) const;
282 
283     std::string readSensor(const std::string &name) const;
284 
285     std::vector<std::string> listSensors(const int direction, const size_t channel) const;
286 
287     SoapySDR::ArgInfo getSensorInfo(const int direction, const size_t channel, const std::string &name) const;
288 
289     std::string readSensor(const int direction, const size_t channel, const std::string &name) const;
290 
291     /*******************************************************************
292      * Register API
293      ******************************************************************/
294 
295     std::vector<std::string> listRegisterInterfaces(void) const;
296 
297     void writeRegister(const std::string &name, const unsigned addr, const unsigned value);
298 
299     unsigned readRegister(const std::string &name, const unsigned addr) const;
300 
301     void writeRegister(const unsigned addr, const unsigned value);
302 
303     unsigned readRegister(const unsigned addr) const;
304 
305     void writeRegisters(const std::string &name, const unsigned addr, const std::vector<unsigned> &value);
306 
307     std::vector<unsigned> readRegisters(const std::string &name, const unsigned addr, const size_t length) const;
308 
309     /*******************************************************************
310      * Settings API
311      ******************************************************************/
312 
313     SoapySDR::ArgInfoList getSettingInfo(void) const;
314 
315     void writeSetting(const std::string &key, const std::string &value);
316 
317     std::string readSetting(const std::string &key) const;
318 
319     SoapySDR::ArgInfoList getSettingInfo(const int direction, const size_t channel) const;
320 
321     void writeSetting(const int direction, const size_t channel, const std::string &key, const std::string &value);
322 
323     std::string readSetting(const int direction, const size_t channel, const std::string &key) const;
324 
325     /*******************************************************************
326      * GPIO API
327      ******************************************************************/
328 
329     std::vector<std::string> listGPIOBanks(void) const;
330 
331     void writeGPIO(const std::string &bank, const unsigned value);
332 
333     void writeGPIO(const std::string &bank, const unsigned value, const unsigned mask);
334 
335     unsigned readGPIO(const std::string &bank) const;
336 
337     void writeGPIODir(const std::string &bank, const unsigned dir);
338 
339     void writeGPIODir(const std::string &bank, const unsigned dir, const unsigned mask);
340 
341     unsigned readGPIODir(const std::string &bank) const;
342 
343     /*******************************************************************
344      * I2C API
345      ******************************************************************/
346 
347     void writeI2C(const int addr, const std::string &data);
348 
349     std::string readI2C(const int addr, const size_t numBytes);
350 
351     /*******************************************************************
352      * SPI API
353      ******************************************************************/
354 
355     unsigned transactSPI(const int addr, const unsigned data, const size_t numBits);
356 
357     /*******************************************************************
358      * UART API
359      ******************************************************************/
360 
361     std::vector<std::string> listUARTs(void) const;
362 
363     void writeUART(const std::string &which, const std::string &data);
364 
365     std::string readUART(const std::string &which, const long timeoutUs) const;
366 
367 private:
368     SoapySocketSession _sess;
369     mutable SoapyRPCSocket _sock;
370     SoapyLogAcceptor *_logAcceptor;
371     mutable std::mutex _mutex;
372     std::string _defaultStreamProt;
373 };
374