-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoapyLeecher.cpp
870 lines (670 loc) · 24.2 KB
/
SoapyLeecher.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
#include <SoapySDR/Device.hpp>
#include <SoapySDR/Registry.hpp>
#include <unistd.h>
#include <cstring> // memcpy
#include <clocale>
#include <complex>
#include <stdexcept>
#include <iostream>
#include <string>
#include <memory> // unique_ptr
#include <liquid/liquid.h>
#include "SharedRingBuffer.hpp"
#include "SimpleSharedRingBuffer.hpp"
#include "TimestampedSharedRingBuffer.hpp"
#include "SoapyLeecher.hpp"
#include "Utils.hpp"
//#define DEBUG
//#define DEBUG_RESAMPLING
#if 0
#define TRACE_API_CALLS(x) cerr << x
#else
#define TRACE_API_CALLS(x)
#endif
using namespace std;
static SoapySDR::Stream* const TX_STREAM = (SoapySDR::Stream*) 0x81;
static SoapySDR::Stream* const RX_STREAM = (SoapySDR::Stream*) 0x82;
SoapyLeecher::SoapyLeecher(const SoapySDR::Kwargs &args):
shm("/soapy"),
rx_frequency(100e6), rx_sample_rate(1e6),
lo_nco(NULL), resampler(NULL),
tx_frequency(100e6), tx_sample_rate(1e6)
{
// Try to the SHM name argument
auto i = args.find("shm");
if (i != args.end())
shm = i->second;
// Open shared memory buffer
// TODO: R&W right required for some reason..
if (TimestampedSharedRingBuffer::checkSHM(shm))
rx_buffer = TimestampedSharedRingBuffer::open(shm, boost::interprocess::read_write);
else
rx_buffer = SimpleSharedRingBuffer::open(shm, SharedRingBuffer::BufferMode::OneToMany, boost::interprocess::read_only);
#ifdef DEBUG
cerr << endl;
cerr << "##############" << endl;
cerr << " RX Buffer " << endl;
cerr << "##############" << endl;
cerr << *rx_buffer;
#endif
// If TX with same name is found open it also
if (SimpleSharedRingBuffer::checkSHM(shm + "_tx")) {
tx_buffer = SimpleSharedRingBuffer::open(shm + "_tx", SharedRingBuffer::BufferMode::ManyToOne, boost::interprocess::read_write);
#ifdef DEBUG
cerr << endl;
cerr << "##############" << endl;
cerr << " TX Buffer " << endl;
cerr << "##############" << endl;
cerr << *tx_buffer;
#endif
}
}
#if 0
SoapyLeecher::SoapyLeecher(const SoapyLeecher& other) // copy constructor
: SoapyLeecher()
{
TRACE_API_CALLS("copy constructor" << endl);
}
#endif
#if 0
SoapyLeecher::SoapyLeecher(SoapyLeecher&& other) noexcept // move constructor
{
TRACE_API_CALLS("move constructor" << endl);
}
#if 0
SoapyLeecher& operator=(const SoapyLeecher& other) // copy assignment
{
TRACE_API_CALLS("copy assignment" << endl);
return *this = SoapyLeecher(other);
}
#endif
SoapyLeecher& operator=(SoapyLeecher&& other) noexcept // move assignment
{
TRACE_API_CALLS("move assignment" << endl);
std::swap(rx_buffer, other.rx_buffer);
std::swap(tx_buffer, other.tx_buffer);
return *this;
}
#endif
SoapyLeecher::~SoapyLeecher() {
if (lo_nco != NULL)
nco_crcf_destroy(lo_nco);
if (resampler != NULL)
resamp_crcf_destroy(resampler);
}
void SoapyLeecher::update_resampler() {
TRACE_API_CALLS("update_resampler()" << endl);
// Calculate new resampling rate and frequency offset
resampl_rate = rx_sample_rate / rx_buffer->getSampleRate();
if (abs(resampl_rate - 1.0) < 1e-4) {
if (resampler != NULL) // Disable resampling
resamp_crcf_destroy(resampler);
resampler = NULL;
return;
}
if (resampl_rate > 1) {
cerr << "IN:" << rx_buffer->getSampleRate() << " OUT " << rx_sample_rate << endl;
throw runtime_error("Interpolating is not supported!");
}
#ifdef DEBUG
cerr << "New resampling rate: " << resampl_rate << endl;
#endif
if (resampl_rate != 1 && rx_buffer->getFormat() != "CF32")
throw runtime_error("Mixing is not supported with non-CF32 streams!");
if (resampler != NULL)
resamp_crcf_destroy(resampler);
float bw = 0.4f * resampl_rate;
int semilen = roundf(3.0f / bw);
resampler = resamp_crcf_create(resampl_rate, semilen, bw, 60.0f, 16);
if (resampler == NULL)
throw runtime_error("Failed to create resampler!");
}
/*
* Update NCO
*/
void SoapyLeecher::update_mixer() {
TRACE_API_CALLS("update_mixer()" << endl);
double offset = rx_frequency - rx_buffer->getCenterFrequency();
#ifdef DEBUG
cerr << "Frequency offset: " << offset << "Hz" << endl;
#endif
if (lo_nco == NULL)
lo_nco = nco_crcf_create(LIQUID_VCO);
nco_crcf_set_frequency(lo_nco, 2*M_PI * offset / rx_buffer->getSampleRate());
}
string SoapyLeecher::getDriverKey(void) const {
return "Leecher";
}
string SoapyLeecher::getHardwareKey(void) const {
return "Leecher" + shm;
}
SoapySDR::Kwargs SoapyLeecher::getHardwareInfo(void) const {
SoapySDR::Kwargs args;
args["shm"] = shm;
// More?
return args;
}
size_t SoapyLeecher::getNumChannels(const int dir) const {
if (dir == SOAPY_SDR_RX && rx_buffer.get() != nullptr)
return rx_buffer->getNumChannels();
else if (dir == SOAPY_SDR_TX && tx_buffer.get() != nullptr)
return tx_buffer->getNumChannels();
return 0;
}
bool SoapyLeecher::getFullDuplex(const int direction, const size_t channel) const {
(void) direction; (void) channel;
return (tx_buffer.get() != nullptr);
}
std::vector<std::string> SoapyLeecher::getStreamFormats(const int direction, const size_t channel) const {
vector<string> formats;
if (direction == SOAPY_SDR_RX && rx_buffer.get() != nullptr)
formats.push_back(rx_buffer->getFormat());
else if (direction == SOAPY_SDR_TX && tx_buffer.get() != nullptr)
formats.push_back(tx_buffer->getFormat());
return formats;
}
string SoapyLeecher::getNativeStreamFormat(const int direction, const size_t channel, double &fullScale) const {
(void)direction; (void)channel; (void)fullScale;
return "CF32";
throw runtime_error("Invalid format!");
fullScale = rx_buffer->getSampleRate();
return rx_buffer->getFormat();
}
SoapySDR::Stream* SoapyLeecher::setupStream(const int direction, const std::string &format,
const std::vector<size_t> &channels, const SoapySDR::Kwargs &args)
{
TRACE_API_CALLS("setupStream(" << (direction == SOAPY_SDR_RX ? "RX" : "TX") << ", " << format << ")" << endl);
if (direction == SOAPY_SDR_RX) {
if (rx_buffer->getFormat() != format)
throw runtime_error("Invalid format!");
return RX_STREAM; // Return RX-handle
}
else if (direction == SOAPY_SDR_TX) {
if (tx_buffer.get() == nullptr)
return NULL;
if (tx_buffer->getFormat() != format)
throw runtime_error("Invalid format!");
return TX_STREAM; // Return TX-handle
}
return NULL;
}
void SoapyLeecher::closeStream(SoapySDR::Stream *stream) {
TRACE_API_CALLS("closeStream(" << (stream == RX_STREAM ? "RX" : "TX") << ")" << endl);
if (stream == RX_STREAM) {
rx_buffer.release();
}
else if (stream == TX_STREAM) {
tx_buffer.release();
}
}
int SoapyLeecher::activateStream(SoapySDR::Stream *stream, const int flags, const long long timeNs, const size_t numElems) {
(void) flags; (void)timeNs; (void)numElems;
TRACE_API_CALLS("activateStream(" << (stream == RX_STREAM ? "RX" : "TX") << ", " << flags << ")" << endl);
// The last setup for the stream before actual data!
if (stream == RX_STREAM) {
if (rx_buffer.get() == nullptr)
return SOAPY_SDR_STREAM_ERROR;
// Sync the receiver
rx_buffer->sync();
update_resampler();
update_mixer();
return 0;
}
else if (stream == TX_STREAM) {
if (tx_buffer.get() == nullptr)
return SOAPY_SDR_STREAM_ERROR;
#ifdef LOCK_WHEN_ACTIVATED
// Wait for the TX buffer to became available
boost::posix_time::ptime abs_timeout = boost::get_system_time() + boost::posix_time::milliseconds(10);
while (tx_buffer->getState() != SharedRingBuffer::Ready)
tx_buffer->wait_tail(abs_timeout);
if (tx_buffer->getState() != SharedRingBuffer::Ready)
return SOAPY_SDR_TIMEOUT;
cerr << "writeStream: acquiring the write lock" << endl;
try {
tx_buffer->acquireWriteLock();
}
catch (boost::interprocess::interprocess_exception &ex) {
return SOAPY_SDR_TIMEOUT;
}
// Set configs
tx_buffer->setCenterFrequency(tx_frequency);
tx_buffer->setSampleRate(tx_sample_rate);
// tx_buffer->setState(SharedRingBuffer::Streaming);
#endif
return 0;
}
return SOAPY_SDR_STREAM_ERROR;
}
int SoapyLeecher::deactivateStream(SoapySDR::Stream *stream, const int flags, const long long timeNs) {
(void) flags; (void) timeNs;
TRACE_API_CALLS("deactivateStream(" << ")" << endl);
if (stream == RX_STREAM) {
// Nothing to do
}
else if (stream == TX_STREAM) {
if (tx_buffer.get() == nullptr)
return SOAPY_SDR_STREAM_ERROR;
if (tx_buffer->ownsWriteLock() == false)
{
if (tx_buffer->getState() == SharedRingBuffer::Streaming)
tx_buffer->setState(SharedRingBuffer::EndOfBurst);
cerr << "writeStream: releasing the write lock" << endl;
// Release the write lock
tx_buffer->releaseWriteLock();
}
}
return 0;
}
int SoapyLeecher::resampledReadStream(SoapySDR::Stream *stream, void *const *buffs, const size_t numElems, int &flags, long long &timeNs, const long timeoutUs) {
(void) flags; (void) timeNs;
long long timestamp;
liquid_float_complex s;
unsigned new_samples = 0;
unsigned int wanted_samples = ceil(numElems / resampl_rate);
TRACE_API_CALLS("resampledReadStream(" << dec << numElems << ", " << resampl_rate << ", " << wanted_samples << ")" << endl);
// Limit wanted sample count if its crazy
//if (wanted_samples > rx_buffer->getCtrl().buffer_size / 2)
// wanted_samples = rx_buffer->getCtrl().buffer_size / 2;
// Calculate absolute timeout time
boost::posix_time::ptime abs_timeout = boost::get_system_time() + boost::posix_time::microseconds(timeoutUs);
const size_t n_channels = rx_buffer->getNumChannels();
void* shm_buffs[n_channels];
// Copy host apps buffer
void* host_buffs[n_channels];
for (unsigned ch = 0; ch < n_channels; ch++)
host_buffs[ch] = buffs[ch];
while (wanted_samples > 0) {
// How much new data is available?
rx_buffer->getReadPointers(shm_buffs);
size_t samples_available = rx_buffer->read(wanted_samples, timestamp);
#ifdef DEBUG_RESAMPLING
cerr << dec << "Wanted: " << wanted_samples << " Found. " << samples_available << endl;
#endif
// First iterate for each rx channel (more cache friendly)
unsigned int original_sample_pos = new_samples;
double original_phase = nco_crcf_get_phase(lo_nco);
for (size_t ch = 0; ch < n_channels; ch++) {
// Reset some of the variables for each channel
new_samples = original_sample_pos;
nco_crcf_set_phase(lo_nco, original_phase);
// Cast buffer pointers
const liquid_float_complex* input = static_cast<const liquid_float_complex*>(shm_buffs[ch]);
liquid_float_complex* output = static_cast<liquid_float_complex*>(host_buffs[ch]);
// Process the samples one by one
unsigned int new_resamples = 0;
for (size_t k = 0; k < samples_available; k++) {
// Up/downconvert
nco_crcf_step(lo_nco);
nco_crcf_mix_down(lo_nco, input[k], &s);
// Resample and store the new samples
unsigned int n_resamples;
resamp_crcf_execute(resampler, s, &output[new_resamples], &n_resamples);
// TODO: BUG!!! same resampler cannot be used for multiple channels!
new_samples += n_resamples;
new_resamples += n_resamples;
}
host_buffs[ch] = reinterpret_cast<void*>(reinterpret_cast<size_t>(host_buffs[ch]) + rx_buffer->getDatasize() * new_resamples);
#ifdef DEBUG_RESAMPLING
cerr << "Resampled: " << samples_available << " -> " << new_resamples << endl;
#endif
}
wanted_samples -= samples_available;
#ifdef DEBUG_RESAMPLING
cerr << dec << "new_samples " << new_samples << endl;
#endif
// Wait for new data and check the timeout condition
try {
if (new_samples < numElems)
rx_buffer->wait_head(abs_timeout);
}
catch (boost::interprocess::interprocess_exception &ex) {
return (new_samples != 0) ? new_samples : SOAPY_SDR_TIMEOUT;
}
if (abs_timeout < boost::get_system_time())
return (new_samples != 0) ? new_samples : SOAPY_SDR_TIMEOUT;
}
assert(new_samples <= numElems); // Over production!!!
return new_samples;
}
int SoapyLeecher::readStream(SoapySDR::Stream *stream, void *const *buffs, const size_t numElems, int &flags, long long &timeNs, const long timeoutUs) {
(void) flags; (void) timeNs;
TRACE_API_CALLS("ReadStream(" << dec << numElems << ")" << endl);
if (stream == RX_STREAM) {
if (rx_buffer.get() == nullptr)
return SOAPY_SDR_STREAM_ERROR;
if (rx_buffer->getState() != SharedRingBuffer::Streaming)
return SOAPY_SDR_STREAM_ERROR;
// Have the stream settings changed?
if (rx_buffer->settingsChanged()) {
update_resampler();
update_mixer();
}
if (resampler != NULL)
return resampledReadStream(stream, buffs, numElems, flags, timeNs, timeoutUs);
#ifdef TIMESTAMPING
if (numElems % rx_buffer->getCtrl().block_size != 0)
cerr << "numElems " << numElems << " is not a nice number!" << endl;
#endif
// Calculate absolute timeout time
boost::posix_time::ptime abs_timeout = boost::get_system_time() + boost::posix_time::microseconds(timeoutUs);
const size_t n_channels = rx_buffer->getNumChannels();
long long timestamp;
unsigned int wanted_samples = numElems;
unsigned int new_samples = 0;
void* shm_buffs[n_channels];
// Copy host apps buffer
void* host_buffs[n_channels];
for (unsigned ch = 0; ch < n_channels; ch++)
host_buffs[ch] = buffs[ch];
while (1) {
// How much new data is available?
rx_buffer->getReadPointers(shm_buffs);
size_t samples_available = rx_buffer->read(wanted_samples, timestamp);
#ifdef DEBUG
cerr << "# " << samples_available << endl;
#endif
assert(samples_available <= wanted_samples);
wanted_samples -= samples_available;
if (samples_available > 0) {
if (0) { // && (flags & SOAPY_SDR_HAS_TIME) == 0 // Only on the first read
flags |= SOAPY_SDR_HAS_TIME;
timeNs = timestamp;
}
// Copy new samples to outputs
for (size_t ch = 0; ch < n_channels; ch++) {
memcpy(host_buffs[ch], shm_buffs[ch], samples_available * rx_buffer->getDatasize());
host_buffs[ch] = reinterpret_cast<void*>(reinterpret_cast<size_t>(host_buffs[ch]) + rx_buffer->getDatasize() * samples_available);
}
new_samples += samples_available;
// Enough?
if (new_samples >= numElems)
break;
}
// Wait for new data and check the timeout condition
try {
if (new_samples < numElems)
rx_buffer->wait_head(abs_timeout);
}
catch (boost::interprocess::interprocess_exception &ex) {
return (new_samples != 0) ? new_samples : SOAPY_SDR_TIMEOUT;
}
if (abs_timeout < boost::get_system_time())
return (new_samples != 0) ? new_samples : SOAPY_SDR_TIMEOUT;
}
#ifdef DEBUG
cerr << "new_samples " << new_samples << endl;
#endif
return new_samples;
}
return SOAPY_SDR_STREAM_ERROR;
}
int SoapyLeecher::writeStream(SoapySDR::Stream *stream, const void *const *buffs, const size_t numElems, int &flags, const long long timeNs, const long timeoutUs) {
(void) flags; (void) timeNs;
cerr << "writeStream(" << numElems << ")" << endl;
if (stream == TX_STREAM) {
if (tx_buffer.get() == nullptr)
return SOAPY_SDR_STREAM_ERROR;
boost::posix_time::ptime abs_timeout = boost::get_system_time() + boost::posix_time::microseconds(timeoutUs);
#ifdef LOCK_WHEN_ACTIVATED
//
if (tx_buffer->ownsWriteLock() == false)
{
cerr << "writeStream: TX stream has not been activated!" << endl;
return SOAPY_SDR_STREAM_ERROR;
}
#else
// If we don't have tx write lock try to acquire it
if (tx_buffer->ownsWriteLock() == false) {
// Wait for the TX buffer to became available
while (tx_buffer->getState() != SharedRingBuffer::Ready) {
cerr << "writeStream: Buffer is busy" << endl;
tx_buffer->wait_tail(abs_timeout);
if (boost::get_system_time() > abs_timeout) {
cout << "timeout" << endl;
return SOAPY_SDR_TIMEOUT;
}
}
if (tx_buffer->getState() != SharedRingBuffer::Ready)
return SOAPY_SDR_TIMEOUT;
cerr << "writeStream: acquiring the write lock" << endl;
try {
tx_buffer->acquireWriteLock();
}
catch (boost::interprocess::interprocess_exception &ex) {
return SOAPY_SDR_TIMEOUT;
}
// Reset the buffer state for our use.
tx_buffer->reset();
tx_buffer->setCenterFrequency(tx_frequency);
tx_buffer->setSampleRate(tx_sample_rate);
tx_buffer->setState(SharedRingBuffer::Streaming);
}
#endif
// If the buffer is not anymore in streaming mode, an error has occurred in the other end!
if (tx_buffer->getState() != SharedRingBuffer::Streaming) {
cerr << "writeStream: Buffer state out of sync!" << endl;
return SOAPY_SDR_UNDERFLOW;
}
// TODO: if (numElems > tx_buffer->buffer_size / 2);
unsigned int x = tx_buffer->getSamplesLeft();
cout << "samples left " << x << " " << numElems << endl;
// Wait for enough space becomes available in the ring buffer
try {
while (tx_buffer->getSamplesLeft() < numElems) { // TODO: Fails if the looping is not supported
tx_buffer->wait_tail(abs_timeout);
if (boost::get_system_time() > abs_timeout)
return SOAPY_SDR_TIMEOUT;
}
}
catch (boost::interprocess::interprocess_exception &ex) {
return SOAPY_SDR_TIME_ERROR;
}
// Copy samples to ring buffer
const size_t n_channels = tx_buffer->getNumChannels();
void* shm_buffs[n_channels];
tx_buffer->getWritePointers(shm_buffs);
// TODO: Allow partial writes?
size_t samples_written = numElems; // min(tx_buffer->getSamplesLeft(), numElems);
for (size_t ch = 0; ch < n_channels; ch++)
memcpy(shm_buffs[ch], buffs[ch], samples_written * tx_buffer->getDatasize());
if (flags & SOAPY_SDR_END_BURST)
tx_buffer->setState(SharedRingBuffer::EndOfBurst);
tx_buffer->write(samples_written, timeNs);
#ifndef LOCK_WHEN_ACTIVATED
if (flags & SOAPY_SDR_END_BURST) {
cerr << "writeStream: releasing the write lock" << endl;
tx_buffer->releaseWriteLock();
#endif
#if 0
// Wait the end of the transmission
while (tx_buffer->getState() == SharedRingBuffer::Ready)
tx_buffer->wait(abs_timeout);
if (tx_buffer->getState() != SharedRingBuffer::Ready)
return SOAPY_SDR_TIMEOUT;
#endif
}
return samples_written;
}
return SOAPY_SDR_STREAM_ERROR;
}
int SoapyLeecher::readStreamStatus(SoapySDR::Stream *stream, size_t &chanMask, int &flags, long long &timeNs, const long timeoutUs) {
TRACE_API_CALLS("readStreamStatus(" << (stream == RX_STREAM ? "RX": "TX") << endl);
boost::posix_time::ptime abs_timeout = boost::get_system_time() + boost::posix_time::microseconds(timeoutUs);
if (stream == RX_STREAM) {
while (1) {
if (abs_timeout < boost::get_system_time())
return SOAPY_SDR_TIMEOUT;
// TODO
tx_buffer->wait_head(abs_timeout);
}
return 0;
}
else if (stream == TX_STREAM) {
while (1) {
if (abs_timeout < boost::get_system_time())
return SOAPY_SDR_TIMEOUT;
// TODO
tx_buffer->wait_tail(abs_timeout);
}
if (tx_buffer->getState() != SharedRingBuffer::Ready)
flags |= 1;
return 0;
}
return SOAPY_SDR_STREAM_ERROR;
}
std::vector<std::string> SoapyLeecher::listAntennas(const int direction, const size_t channel) const {
(void)direction; (void)channel;
return std::vector<std::string>({"RX", "TX"});
}
void SoapyLeecher::setAntenna(const int direction, const size_t channel, const std::string &name) {
(void)direction; (void)channel; (void)name;
}
std::string SoapyLeecher::getAntenna(const int direction, const size_t channel) const {
(void)channel;
return (direction == SOAPY_SDR_RX) ? "RX" : "TX";
}
void SoapyLeecher::setFrequency(const int direction, const size_t channel, const double frequency, const SoapySDR::Kwargs &args) {
TRACE_API_CALLS("setFrequency(" << direction << ", " << channel << ", " << frequency << ")" << endl);
if (direction == SOAPY_SDR_RX) {
if (rx_buffer.get() == nullptr)
throw runtime_error("RX not available");
rx_frequency = frequency;
update_mixer();
}
else if (direction == SOAPY_SDR_TX) {
if (tx_buffer.get() == nullptr)
throw runtime_error("TX not available");
tx_frequency = frequency;
if (0 /*tx_enabled*/)
tx_buffer->setCenterFrequency(frequency);
}
}
double SoapyLeecher::getFrequency(const int direction, const size_t channel) const {
if (direction == SOAPY_SDR_RX) {
return rx_frequency; // Return mixed center frequency
}
else if (direction == SOAPY_SDR_TX) {
if (tx_buffer.get() == nullptr)
throw runtime_error("TX not available");
return tx_frequency; // tx_buffer->getCenterFrequency();
}
return 0.0;
}
void SoapyLeecher::setSampleRate(const int direction, const size_t channel, const double rate) {
TRACE_API_CALLS("setSampleRate(" << direction << ", " << channel << ", " << rate << ")" << endl);
if (direction == SOAPY_SDR_RX) {
if (rx_buffer.get() == nullptr)
throw runtime_error("RX not available");
if (rx_buffer->getSampleRate() < rate)
throw runtime_error("Interpolation not supported!");
rx_sample_rate = rate;
update_resampler();
}
else if (direction == SOAPY_SDR_TX) {
if (tx_buffer.get() == nullptr)
throw runtime_error("TX not available");
tx_sample_rate = rate;
}
}
double SoapyLeecher::getSampleRate(const int direction, const size_t channel) const {
if (direction == SOAPY_SDR_RX) {
return rx_sample_rate; // Return sample rate after decimation
}
else if (direction == SOAPY_SDR_TX) {
if (tx_buffer.get() == nullptr)
throw runtime_error("TX not available");
return tx_sample_rate; // tx_buffer->getSampleRate();
}
return 0.0;
}
// #ifdef SOAPY_SDR_API_HAS_GET_SAMPLE_RATE_RANGE
SoapySDR::RangeList SoapyLeecher::getSampleRateRange(const int direction, const size_t channel) const {
TRACE_API_CALLS("getSampleRateRange(" << direction << ", " << channel << ")" << endl);
SoapySDR::RangeList list;
list.push_back(SoapySDR::Range(10000, 4000000));
return list;
}
std::vector<double> SoapyLeecher::listSampleRates(const int direction, const size_t channel) const {
TRACE_API_CALLS("listSampleRates(" << direction << ", " << channel << ")" << endl);
if (rx_buffer.get() == nullptr)
throw runtime_error("RX not available");
std::vector<double> list;
double base = rx_buffer->getSampleRate();
for (int i = 1; i < 16; i++)
list.push_back(base / i);
return list;
}
void SoapyLeecher::setBandwidth(const int direction, const size_t channel, const double bw) {
TRACE_API_CALLS("setBandwidth(" << direction << "," << channel << ", " << bw << ")" << endl);
// TODO
}
double SoapyLeecher::getBandwidth(const int direction, const size_t channel) const {
TRACE_API_CALLS("getBandwidth(" << ")" << endl);
if (direction == SOAPY_SDR_RX) {
return rx_sample_rate;
}
else if (direction == SOAPY_SDR_TX) {
return tx_sample_rate;
}
return 0.0;
}
std::vector<double> SoapyLeecher::listBandwidths(const int direction, const size_t channel) const {
TRACE_API_CALLS("listBandwidths(" << ")" << endl);
std::vector<double> list;
return list;
}
SoapySDR::RangeList SoapyLeecher::getBandwidthRange(const int direction, const size_t channel) const {
TRACE_API_CALLS("getBandwidthRange(" << ")" << endl);
SoapySDR::RangeList list;
return list;
}
/***********************************************************************
* Find available devices
**********************************************************************/
/*
* Try to find a shared memory buffer
*/
SoapySDR::KwargsList findLeecher(const SoapySDR::Kwargs &args)
{
// Check for "shm" filter
string shm("soapy");
auto i = args.find("shm");
if (i != args.end())
shm = i->second;
SoapySDR::KwargsList results;
// Foreach all SHMs
for (string shm_name: SHMRegistry::list()) {
// Check shm name
if (shm_name.compare(0, shm.size(), shm))
continue;
// Filter out TX streams
if (shm_name.substr(shm_name.size() - 3, 3) == "_tx")
continue;
// Try to open the Shared Memory buffer to get details
if (SimpleSharedRingBuffer::checkSHM(shm_name) == true) {
SoapySDR::Kwargs resultArgs;
resultArgs["shm"] = shm_name;
resultArgs["label"] = "SoapyLeecher: " + shm_name; // Readable label for the Leetcher driver
results.push_back(resultArgs);
}
else if (TimestampedSharedRingBuffer::checkSHM(shm_name) == true) {
SoapySDR::Kwargs resultArgs;
resultArgs["shm"] = shm_name;
resultArgs["timestamped"] = "true";
resultArgs["label"] = "SoapyLeecher: " + shm_name; // Readable label for the Leetcher driver
results.push_back(resultArgs);
}
}
return results;
}
/***********************************************************************
* Make device instance
**********************************************************************/
SoapySDR::Device *makeLeecher(const SoapySDR::Kwargs &args) {
return new SoapyLeecher(args);
}
/***********************************************************************
* Registration
**********************************************************************/
static SoapySDR::Registry registerLeecher("leecher", &findLeecher, &makeLeecher, SOAPY_SDR_ABI_VERSION);