forked from pothosware/SoapySDRPlay3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cpp
2281 lines (2068 loc) · 77 KB
/
Settings.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
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Charles J. Cliffe
* Copyright (c) 2020 Franco Venturi - changes for SDRplay API version 3
* and Dual Tuner for RSPduo
* Copyright (c) 2024 Marat Fayzullin - stability fixes and improvements
* 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 "SoapySDRPlay.hpp"
#if defined(_M_X64) || defined(_M_IX86)
#define strcasecmp _stricmp
#elif defined (__GNUC__)
#include <strings.h>
#endif
std::unordered_map<std::string, sdrplay_api_DeviceT*> SoapySDRPlay::selectedRSPDevices;
std::set<std::string> &SoapySDRPlay_getClaimedSerials(void)
{
static std::set<std::string> serials;
return serials;
}
SoapySDRPlay::SoapySDRPlay(const SoapySDR::Kwargs &args)
{
if (args.count("serial") == 0) throw std::runtime_error("no available RSP devices found");
selectDevice(args.at("serial"),
args.count("mode") ? args.at("mode") : "",
args.count("antenna") ? args.at("antenna") : "");
// keep all the default settings:
// - rf: 200MHz
// - fs: 2MHz
// - decimation: off
// - IF: 0kHz (zero IF)
// - bw: 200kHz
// - attenuation: 50dB
// - LNA state: 0
// - AGC: 50Hz
// - DC correction: on
// - IQ balance: on
// change the default AGC set point to -30dBfs
chParams->ctrlParams.agc.setPoint_dBfs = -30;
// process additional device string arguments
for (std::pair<std::string, std::string> arg : args) {
// ignore 'driver', 'label', 'mode', 'serial', and 'soapy'
if (arg.first == "driver" || arg.first == "label" ||
arg.first == "mode" || arg.first == "serial" ||
arg.first == "soapy") {
continue;
}
writeSetting(arg.first, arg.second);
}
// streaming settings
// this may change later according to format
shortsPerWord = 1;
bufferLength = bufferElems * elementsPerSample * shortsPerWord;
_streams[0] = 0;
_streams[1] = 0;
_streamsRefCount[0] = 0;
_streamsRefCount[1] = 0;
useShort = true;
useHdr = false;
streamActive = false;
device_unavailable = false;
cacheKey = serNo;
if (hwVer == SDRPLAY_RSPduo_ID) cacheKey += "@" + args.at("mode");
SoapySDRPlay_getClaimedSerials().insert(cacheKey);
}
SoapySDRPlay::~SoapySDRPlay(void)
{
SoapySDRPlay_getClaimedSerials().erase(cacheKey);
std::lock_guard <std::mutex> lock(_general_state_mutex);
try {
releaseDevice();
} catch(...) {
// Ignore exceptions, we are exiting
}
_streams[0] = 0;
_streams[1] = 0;
_streamsRefCount[0] = 0;
_streamsRefCount[1] = 0;
}
/*******************************************************************
* Identification API
******************************************************************/
std::string SoapySDRPlay::getDriverKey(void) const
{
return "SDRplay";
}
std::string SoapySDRPlay::getHardwareKey(void) const
{
if (hwVer == SDRPLAY_RSP1_ID) return "RSP1";
if (hwVer == SDRPLAY_RSP1A_ID) return "RSP1A";
if (hwVer == SDRPLAY_RSP1B_ID) return "RSP1B";
if (hwVer == SDRPLAY_RSP2_ID) return "RSP2";
if (hwVer == SDRPLAY_RSPduo_ID) return "RSPduo";
if (hwVer == SDRPLAY_RSPdx_ID) return "RSPdx";
if (hwVer == SDRPLAY_RSPdxR2_ID) return "RSPdx-R2";
return "UNKNOWN";
}
SoapySDR::Kwargs SoapySDRPlay::getHardwareInfo(void) const
{
// key/value pairs for any useful information
// this also gets printed in --probe
SoapySDR::Kwargs hwArgs;
float ver = SoapySDRPlay::sdrplay_api::get_version();
hwArgs["sdrplay_api_api_version"] = std::to_string(ver);
hwArgs["sdrplay_api_hw_version"] = std::to_string(device.hwVer);
return hwArgs;
}
/*******************************************************************
* Channels API
******************************************************************/
size_t SoapySDRPlay::getNumChannels(const int dir) const
{
if (device.hwVer == SDRPLAY_RSPduo_ID && device.rspDuoMode == sdrplay_api_RspDuoMode_Dual_Tuner) {
return (dir == SOAPY_SDR_RX) ? 2 : 0;
}
return (dir == SOAPY_SDR_RX) ? 1 : 0;
}
/*******************************************************************
* Antenna API
******************************************************************/
std::vector<std::string> SoapySDRPlay::listAntennas(const int direction, const size_t channel) const
{
std::vector<std::string> antennas;
if (direction == SOAPY_SDR_TX) {
return antennas;
}
if (device.hwVer == SDRPLAY_RSP1_ID || device.hwVer == SDRPLAY_RSP1A_ID || device.hwVer == SDRPLAY_RSP1B_ID) {
antennas.push_back("RX");
}
else if (device.hwVer == SDRPLAY_RSP2_ID) {
antennas.push_back("Antenna A");
antennas.push_back("Antenna B");
antennas.push_back("Hi-Z");
}
else if (device.hwVer == SDRPLAY_RSPdx_ID) {
antennas.push_back("Antenna A");
antennas.push_back("Antenna B");
antennas.push_back("Antenna C");
}
else if (device.hwVer == SDRPLAY_RSPdxR2_ID) {
antennas.push_back("Antenna A");
antennas.push_back("Antenna B");
antennas.push_back("Antenna C");
}
else if (device.hwVer == SDRPLAY_RSPduo_ID) {
if (device.rspDuoMode == sdrplay_api_RspDuoMode_Single_Tuner ||
device.rspDuoMode == sdrplay_api_RspDuoMode_Master) {
antennas.push_back("Tuner 1 50 ohm");
antennas.push_back("Tuner 1 Hi-Z");
antennas.push_back("Tuner 2 50 ohm");
}
else if (device.rspDuoMode == sdrplay_api_RspDuoMode_Dual_Tuner) {
if (channel == 0) {
// No Hi-Z antenna in Dual Tuner mode
// For diversity reception you would want the two tuner inputs
// to be the same otherwise there is a mismatch in the gain
// control.
antennas.push_back("Tuner 1 50 ohm");
}
else if (channel == 1) {
antennas.push_back("Tuner 2 50 ohm");
}
}
else if (device.rspDuoMode == sdrplay_api_RspDuoMode_Slave) {
if (device.tuner == sdrplay_api_Tuner_A) {
antennas.push_back("Tuner 1 50 ohm");
antennas.push_back("Tuner 1 Hi-Z");
}
else if (device.tuner == sdrplay_api_Tuner_B) {
antennas.push_back("Tuner 2 50 ohm");
}
}
}
return antennas;
}
void SoapySDRPlay::setAntenna(const int direction, const size_t channel, const std::string &name)
{
// Check direction
if ((direction != SOAPY_SDR_RX) || (device.hwVer == SDRPLAY_RSP1_ID) || (device.hwVer == SDRPLAY_RSP1A_ID) || (device.hwVer == SDRPLAY_RSP1B_ID)) {
return;
}
std::lock_guard <std::mutex> lock(_general_state_mutex);
if (device.hwVer == SDRPLAY_RSP2_ID)
{
bool changeToAntennaA_B = false;
if (name == "Antenna A")
{
chParams->rsp2TunerParams.antennaSel = sdrplay_api_Rsp2_ANTENNA_A;
changeToAntennaA_B = true;
}
else if (name == "Antenna B")
{
chParams->rsp2TunerParams.antennaSel = sdrplay_api_Rsp2_ANTENNA_B;
changeToAntennaA_B = true;
}
else if (name == "Hi-Z")
{
chParams->rsp2TunerParams.amPortSel = sdrplay_api_Rsp2_AMPORT_1;
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_Rsp2_AmPortSelect, sdrplay_api_Update_Ext1_None);
}
}
if (changeToAntennaA_B)
{
//if we are currently High_Z, make the switch first.
if (chParams->rsp2TunerParams.amPortSel == sdrplay_api_Rsp2_AMPORT_1)
{
chParams->rsp2TunerParams.amPortSel = sdrplay_api_Rsp2_AMPORT_2;
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_Rsp2_AmPortSelect, sdrplay_api_Update_Ext1_None);
}
}
else
{
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_Rsp2_AntennaControl, sdrplay_api_Update_Ext1_None);
}
}
}
}
else if (device.hwVer == SDRPLAY_RSPdx_ID)
{
if (name == "Antenna A")
{
deviceParams->devParams->rspDxParams.antennaSel = sdrplay_api_RspDx_ANTENNA_A;
}
else if (name == "Antenna B")
{
deviceParams->devParams->rspDxParams.antennaSel = sdrplay_api_RspDx_ANTENNA_B;
}
else if (name == "Antenna C")
{
deviceParams->devParams->rspDxParams.antennaSel = sdrplay_api_RspDx_ANTENNA_C;
}
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_None, sdrplay_api_Update_RspDx_AntennaControl);
}
}
else if (device.hwVer == SDRPLAY_RSPdxR2_ID)
{
if (name == "Antenna A")
{
deviceParams->devParams->rspDxParams.antennaSel = sdrplay_api_RspDx_ANTENNA_A;
}
else if (name == "Antenna B")
{
deviceParams->devParams->rspDxParams.antennaSel = sdrplay_api_RspDx_ANTENNA_B;
}
else if (name == "Antenna C")
{
deviceParams->devParams->rspDxParams.antennaSel = sdrplay_api_RspDx_ANTENNA_C;
}
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_None, sdrplay_api_Update_RspDx_AntennaControl);
}
}
else if (device.hwVer == SDRPLAY_RSPduo_ID)
{
bool changeToTunerA_B = false;
bool changeAmPort = false;
bool isTunerChangeAllowed = device.rspDuoMode == sdrplay_api_RspDuoMode_Single_Tuner || device.rspDuoMode == sdrplay_api_RspDuoMode_Master;
if (name == "Tuner 1 50 ohm")
{
changeAmPort = chParams->rspDuoTunerParams.tuner1AmPortSel != sdrplay_api_RspDuo_AMPORT_2;
chParams->rspDuoTunerParams.tuner1AmPortSel = sdrplay_api_RspDuo_AMPORT_2;
changeToTunerA_B = isTunerChangeAllowed && device.tuner != sdrplay_api_Tuner_A;
}
else if (name == "Tuner 2 50 ohm")
{
changeAmPort = chParams->rspDuoTunerParams.tuner1AmPortSel != sdrplay_api_RspDuo_AMPORT_2;
changeToTunerA_B = isTunerChangeAllowed && device.tuner != sdrplay_api_Tuner_B;
}
else if (name == "Tuner 1 Hi-Z")
{
changeAmPort = chParams->rspDuoTunerParams.tuner1AmPortSel != sdrplay_api_RspDuo_AMPORT_1;
chParams->rspDuoTunerParams.tuner1AmPortSel = sdrplay_api_RspDuo_AMPORT_1;
changeToTunerA_B = isTunerChangeAllowed && device.tuner != sdrplay_api_Tuner_A;
}
if (!changeToTunerA_B)
{
if (changeAmPort)
{
//if we are currently High_Z, make the switch first.
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_RspDuo_AmPortSelect, sdrplay_api_Update_Ext1_None);
}
}
}
else
{
if (streamActive)
{
if (device.rspDuoMode == sdrplay_api_RspDuoMode_Single_Tuner)
{
sdrplay_api_ErrT err;
err = sdrplay_api_SwapRspDuoActiveTuner(device.dev,
&device.tuner, chParams->rspDuoTunerParams.tuner1AmPortSel);
if (err != sdrplay_api_Success)
{
SoapySDR_logf(SOAPY_SDR_WARNING, "SwapRspDuoActiveTuner Error: %s", sdrplay_api_GetErrorString(err));
}
chParams = device.tuner == sdrplay_api_Tuner_B ?
deviceParams->rxChannelB : deviceParams->rxChannelA;
}
else if (device.rspDuoMode == sdrplay_api_RspDuoMode_Master)
{
// not sure what is the best way to handle this case - fv
SoapySDR_log(SOAPY_SDR_WARNING, "tuner change not allowed in RSPduo Master mode while the device is streaming");
}
}
else
{
// preserve all the device and tuner settings
// when changing tuner/antenna
sdrplay_api_DevParamsT devParams = *deviceParams->devParams;
sdrplay_api_RxChannelParamsT rxChannelParams = *chParams;
sdrplay_api_TunerSelectT other_tuner = (device.tuner == sdrplay_api_Tuner_A) ? sdrplay_api_Tuner_B : sdrplay_api_Tuner_A;
selectDevice(other_tuner, device.rspDuoMode,
device.rspDuoSampleFreq, nullptr);
// restore device and tuner settings
*deviceParams->devParams = devParams;
*chParams = rxChannelParams;
}
}
}
}
std::string SoapySDRPlay::getAntenna(const int direction, const size_t channel) const
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
if (direction == SOAPY_SDR_TX)
{
return "";
}
if (device.hwVer == SDRPLAY_RSP2_ID)
{
if (chParams->rsp2TunerParams.amPortSel == sdrplay_api_Rsp2_AMPORT_1) {
return "Hi-Z";
}
else if (chParams->rsp2TunerParams.antennaSel == sdrplay_api_Rsp2_ANTENNA_A) {
return "Antenna A";
}
else {
return "Antenna B";
}
}
else if (device.hwVer == SDRPLAY_RSPduo_ID)
{
if (device.tuner == sdrplay_api_Tuner_A ||
(device.tuner == sdrplay_api_Tuner_Both && channel == 0)) {
if (chParams->rspDuoTunerParams.tuner1AmPortSel == sdrplay_api_RspDuo_AMPORT_1) {
return "Tuner 1 Hi-Z";
} else {
return "Tuner 1 50 ohm";
}
} else if (device.tuner == sdrplay_api_Tuner_B ||
(device.tuner == sdrplay_api_Tuner_Both && channel == 1)) {
return "Tuner 2 50 ohm";
}
}
else if (device.hwVer == SDRPLAY_RSPdx_ID)
{
if (deviceParams->devParams->rspDxParams.antennaSel == sdrplay_api_RspDx_ANTENNA_A) {
return "Antenna A";
}
else if (deviceParams->devParams->rspDxParams.antennaSel == sdrplay_api_RspDx_ANTENNA_B) {
return "Antenna B";
}
else if (deviceParams->devParams->rspDxParams.antennaSel == sdrplay_api_RspDx_ANTENNA_C) {
return "Antenna C";
}
}
else if (device.hwVer == SDRPLAY_RSPdxR2_ID)
{
if (deviceParams->devParams->rspDxParams.antennaSel == sdrplay_api_RspDx_ANTENNA_A) {
return "Antenna A";
}
else if (deviceParams->devParams->rspDxParams.antennaSel == sdrplay_api_RspDx_ANTENNA_B) {
return "Antenna B";
}
else if (deviceParams->devParams->rspDxParams.antennaSel == sdrplay_api_RspDx_ANTENNA_C) {
return "Antenna C";
}
}
return "RX";
}
/*******************************************************************
* Frontend corrections API
******************************************************************/
bool SoapySDRPlay::hasDCOffsetMode(const int direction, const size_t channel) const
{
return true;
}
void SoapySDRPlay::setDCOffsetMode(const int direction, const size_t channel, const bool automatic)
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
//enable/disable automatic DC removal
chParams->ctrlParams.dcOffset.DCenable = (unsigned char)automatic;
chParams->ctrlParams.dcOffset.IQenable = (unsigned char)automatic;
}
bool SoapySDRPlay::getDCOffsetMode(const int direction, const size_t channel) const
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
return (bool)chParams->ctrlParams.dcOffset.DCenable;
}
bool SoapySDRPlay::hasDCOffset(const int direction, const size_t channel) const
{
//is a specific DC removal value configurable?
return false;
}
bool SoapySDRPlay::hasFrequencyCorrection(const int direction, const size_t channel) const {
return true;
}
void SoapySDRPlay::setFrequencyCorrection(const int direction, const size_t channel, const double value) {
setFrequency(direction, channel, "CORR", value);
}
double SoapySDRPlay::getFrequencyCorrection(const int direction, const size_t channel) const {
return getFrequency(direction, channel, "CORR");
}
/*******************************************************************
* Gain API
******************************************************************/
std::vector<std::string> SoapySDRPlay::listGains(const int direction, const size_t channel) const
{
//list available gain elements,
//the functions below have a "name" parameter
std::vector<std::string> results;
results.push_back("IFGR");
results.push_back("RFGR");
return results;
}
bool SoapySDRPlay::hasGainMode(const int direction, const size_t channel) const
{
return true;
}
void SoapySDRPlay::setGainMode(const int direction, const size_t channel, const bool automatic)
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
sdrplay_api_AgcControlT agc_control = automatic ? sdrplay_api_AGC_CTRL_EN : sdrplay_api_AGC_DISABLE;
if (chParams->ctrlParams.agc.enable != agc_control)
{
chParams->ctrlParams.agc.enable = agc_control;
chParams->ctrlParams.agc.attack_ms = 0;
chParams->ctrlParams.agc.decay_ms = 0;
chParams->ctrlParams.agc.decay_delay_ms = 0;
chParams->ctrlParams.agc.decay_threshold_dB = 0;
chParams->ctrlParams.agc.syncUpdate = 0;
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_Ctrl_Agc, sdrplay_api_Update_Ext1_None);
}
}
}
bool SoapySDRPlay::getGainMode(const int direction, const size_t channel) const
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
return chParams->ctrlParams.agc.enable != sdrplay_api_AGC_DISABLE;
}
void SoapySDRPlay::setGain(const int direction, const size_t channel, const double value)
{
// Defaulting to IFGR, since RFGR is not really a gain setting
setGain(direction, channel, "IFGR", value);
}
void SoapySDRPlay::setGain(const int direction, const size_t channel, const std::string &name, const double value)
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
bool doUpdate = false;
if (name == "IFGR")
{
//apply the change if the required value is different from gRdB
if (chParams->tunerParams.gain.gRdB != (int)value)
{
chParams->tunerParams.gain.gRdB = (int)value;
doUpdate = true;
}
}
else if (name == "RFGR")
{
if (chParams->tunerParams.gain.LNAstate != (int)value)
{
chParams->tunerParams.gain.LNAstate = (int)value;
doUpdate = true;
}
}
if ((doUpdate == true) && (streamActive))
{
gr_changed = 0;
sdrplay_api_ErrT err = tryUpdate(sdrplay_api_Update_Tuner_Gr);
if (err != sdrplay_api_Success)
{
SoapySDR_logf(SOAPY_SDR_WARNING, "sdrplay_api_Update(Tuner_Gr) Error: %s", sdrplay_api_GetErrorString(err));
return;
}
for (int i = 0; (i < updateTimeout) && (gr_changed == 0) ; ++i)
{
waitForDevice(1);
}
if (gr_changed == 0)
{
SoapySDR_log(SOAPY_SDR_WARNING, "Gain reduction update timeout.");
}
}
}
double SoapySDRPlay::getGain(const int direction, const size_t channel) const
{
return getGain(direction, channel, "IFGR");
}
double SoapySDRPlay::getGain(const int direction, const size_t channel, const std::string &name) const
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
if (name == "IFGR")
{
return chParams->tunerParams.gain.gRdB;
}
else if (name == "RFGR")
{
return chParams->tunerParams.gain.LNAstate;
}
return 0;
}
SoapySDR::Range SoapySDRPlay::getGainRange(const int direction, const size_t channel) const
{
return getGainRange(direction, channel, "IFGR");
}
SoapySDR::Range SoapySDRPlay::getGainRange(const int direction, const size_t channel, const std::string &name) const
{
if (name == "IFGR")
{
return SoapySDR::Range(20, 59);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSP1_ID))
{
return SoapySDR::Range(0, 3);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSP2_ID))
{
return SoapySDR::Range(0, 8);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSPduo_ID))
{
return SoapySDR::Range(0, 9);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSP1A_ID))
{
return SoapySDR::Range(0, 9);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSP1B_ID))
{
return SoapySDR::Range(0, 9);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSPdx_ID))
{
return SoapySDR::Range(0, 27);
}
else if ((name == "RFGR") && (device.hwVer == SDRPLAY_RSPdxR2_ID))
{
return SoapySDR::Range(0, 27);
}
return SoapySDR::Range(20, 59);
}
/*******************************************************************
* Frequency API
******************************************************************/
void SoapySDRPlay::setFrequency(const int direction,
const size_t channel,
const double frequency,
const SoapySDR::Kwargs &args)
{
// default to RF
setFrequency(direction, channel, "RF", frequency, args);
}
void SoapySDRPlay::setFrequency(const int direction,
const size_t channel,
const std::string &name,
const double frequency,
const SoapySDR::Kwargs &args)
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
if (direction == SOAPY_SDR_RX)
{
if ((name == "RF") && (chParams->tunerParams.rfFreq.rfHz != (uint32_t)frequency))
{
chParams->tunerParams.rfFreq.rfHz = (uint32_t)frequency;
if (streamActive)
{
rf_changed = 0;
sdrplay_api_ErrT err = tryUpdate(sdrplay_api_Update_Tuner_Frf);
if (err != sdrplay_api_Success)
{
SoapySDR_logf(SOAPY_SDR_WARNING, "sdrplay_api_Update(Tuner_FrF) Error: %s", sdrplay_api_GetErrorString(err));
return;
}
for (int i = 0; (i < updateTimeout) && (rf_changed == 0) ; ++i)
{
waitForDevice(1);
}
if (rf_changed == 0)
{
SoapySDR_log(SOAPY_SDR_WARNING, "RF center frequency update timeout.");
}
// Try enabling HDR mode, if applicable
tryHdr();
}
}
// can't set ppm for RSPduo slaves
else if ((name == "CORR") && deviceParams->devParams &&
(deviceParams->devParams->ppm != frequency))
{
deviceParams->devParams->ppm = frequency;
if (streamActive)
{
sdrplay_api_Update(device.dev, device.tuner, sdrplay_api_Update_Dev_Ppm, sdrplay_api_Update_Ext1_None);
}
}
}
}
double SoapySDRPlay::getFrequency(const int direction, const size_t channel) const
{
// default to RF
return getFrequency(direction, channel, "RF");
}
double SoapySDRPlay::getFrequency(const int direction, const size_t channel, const std::string &name) const
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
if (name == "RF")
{
return (double)chParams->tunerParams.rfFreq.rfHz;
}
else if (name == "CORR")
{
if (deviceParams->devParams)
{
return deviceParams->devParams->ppm;
} else {
return 0;
}
}
return 0;
}
std::vector<std::string> SoapySDRPlay::listFrequencies(const int direction, const size_t channel) const
{
std::vector<std::string> names;
names.push_back("RF");
names.push_back("CORR");
return names;
}
SoapySDR::RangeList SoapySDRPlay::getFrequencyRange(const int direction, const size_t channel) const
{
return getFrequencyRange(direction, channel, "RF");
}
SoapySDR::RangeList SoapySDRPlay::getFrequencyRange(const int direction, const size_t channel, const std::string &name) const
{
SoapySDR::RangeList results;
if (name == "RF")
{
if(device.hwVer == SDRPLAY_RSP1_ID)
{
results.push_back(SoapySDR::Range(10000, 2000000000));
}
else
{
results.push_back(SoapySDR::Range(1000, 2000000000));
}
}
return results;
}
SoapySDR::ArgInfoList SoapySDRPlay::getFrequencyArgsInfo(const int direction, const size_t channel) const
{
SoapySDR::ArgInfoList freqArgs;
return freqArgs;
}
/*******************************************************************
* Sample Rate API
******************************************************************/
/* input_sample_rate: sample rate used by the SDR
* output_sample_rate: sample rate as seen by the client app
* (<= input_sample_rate because of decimation)
*/
void SoapySDRPlay::setSampleRate(const int direction, const size_t channel, const double output_sample_rate)
{
std::lock_guard <std::mutex> lock(_general_state_mutex);
SoapySDR_logf(SOAPY_SDR_DEBUG, "Requested output sample rate: %lf", output_sample_rate);
if (direction == SOAPY_SDR_RX)
{
unsigned int decM;
unsigned int decEnable;
sdrplay_api_If_kHzT ifType;
double input_sample_rate = getInputSampleRateAndDecimation(output_sample_rate, &decM, &decEnable, &ifType);
if (input_sample_rate < 0) {
SoapySDR_logf(SOAPY_SDR_WARNING, "invalid sample rate. Sample rate unchanged.");
return;
}
sdrplay_api_Bw_MHzT bwType = getBwEnumForRate(output_sample_rate);
sdrplay_api_ReasonForUpdateT reasonForUpdate = (sdrplay_api_ReasonForUpdateT)(
sdrplay_api_Update_Ctrl_Decimation |
sdrplay_api_Update_Tuner_IfType |
sdrplay_api_Update_Tuner_BwType
);
bool waitForUpdate = false;
// update all parameters in case SDRPlay API requires them updated
// together
chParams->tunerParams.ifType = ifType;
chParams->tunerParams.bwType = bwType;
// always update input sample rate too
if (deviceParams->devParams)
{
deviceParams->devParams->fsFreq.fsHz = input_sample_rate;
reasonForUpdate = (sdrplay_api_ReasonForUpdateT)(reasonForUpdate | sdrplay_api_Update_Dev_Fs);
waitForUpdate = true;
}
// always update decimation since it depends on ifType, etc
chParams->ctrlParams.decimation.enable = decEnable;
chParams->ctrlParams.decimation.decimationFactor = decM;
chParams->ctrlParams.decimation.wideBandSignal =
ifType == sdrplay_api_IF_Zero? 1 : 0;
if (reasonForUpdate != sdrplay_api_Update_None)
{
if (_streams[0]) { _streams[0]->reset = true; }
if (_streams[1]) { _streams[1]->reset = true; }
if (streamActive)
{
// beware that when the fs change crosses the boundary between
// 2,685,312 and 2,685,313 the rx_callbacks stop for some
// reason
fs_changed = 0;
sdrplay_api_ErrT err = tryUpdate(reasonForUpdate);
if (err != sdrplay_api_Success)
{
SoapySDR_logf(SOAPY_SDR_WARNING, "sdrplay_api_Update(%08x) Error: %s", reasonForUpdate, sdrplay_api_GetErrorString(err));
return;
}
if (waitForUpdate)
{
for (int i = 0; (i < updateTimeout) && (fs_changed == 0) ; ++i)
{
waitForDevice(1);
}
if (fs_changed == 0)
{
SoapySDR_log(SOAPY_SDR_WARNING, "Sample rate update timeout.");
}
}
// Try enabling HDR mode, if applicable
tryHdr();
}
}
}
}
double SoapySDRPlay::getSampleRate(const int direction, const size_t channel) const
{
double fsHz = deviceParams->devParams ? deviceParams->devParams->fsFreq.fsHz : device.rspDuoSampleFreq;
if ((fsHz == 6.0e6 && chParams->tunerParams.ifType == sdrplay_api_IF_1_620) ||
(fsHz == 8.0e6 && chParams->tunerParams.ifType == sdrplay_api_IF_2_048))
{
fsHz = 2.0e6;
}
else if (!(fsHz >= 2.0e6 &&
chParams->tunerParams.ifType == sdrplay_api_IF_Zero &&
(device.hwVer != SDRPLAY_RSPduo_ID || device.rspDuoMode == sdrplay_api_RspDuoMode_Single_Tuner)
))
{
SoapySDR_logf(SOAPY_SDR_ERROR, "Invalid sample rate and/or IF setting - fsHz=%lf ifType=%d hwVer=%d rspDuoMode=%d rspDuoSampleFreq=%lf", fsHz, chParams->tunerParams.ifType, device.hwVer, device.rspDuoMode, device.rspDuoSampleFreq);
throw std::runtime_error("Invalid sample rate and/or IF setting");
}
if (!chParams->ctrlParams.decimation.enable)
{
return fsHz;
}
else
{
return fsHz / chParams->ctrlParams.decimation.decimationFactor;
}
}
std::vector<double> SoapySDRPlay::listSampleRates(const int direction, const size_t channel) const
{
std::vector<double> output_sample_rates;
if (device.hwVer == SDRPLAY_RSPduo_ID && device.rspDuoMode != sdrplay_api_RspDuoMode_Single_Tuner)
{
output_sample_rates.push_back(62500);
output_sample_rates.push_back(125000);
output_sample_rates.push_back(250000);
output_sample_rates.push_back(500000);
output_sample_rates.push_back(1000000);
output_sample_rates.push_back(2000000);
return output_sample_rates;
}
output_sample_rates.push_back(62500);
output_sample_rates.push_back(96000);
output_sample_rates.push_back(125000);
output_sample_rates.push_back(192000);
output_sample_rates.push_back(250000);
output_sample_rates.push_back(384000);
output_sample_rates.push_back(500000);
output_sample_rates.push_back(768000);
output_sample_rates.push_back(1000000);
output_sample_rates.push_back(1536000);
output_sample_rates.push_back(2000000);
output_sample_rates.push_back(2048000);
output_sample_rates.push_back(3000000);
output_sample_rates.push_back(4000000);
output_sample_rates.push_back(5000000);
output_sample_rates.push_back(6000000);
output_sample_rates.push_back(7000000);
output_sample_rates.push_back(8000000);
output_sample_rates.push_back(9000000);
output_sample_rates.push_back(10000000);
return output_sample_rates;
}
double SoapySDRPlay::getInputSampleRateAndDecimation(uint32_t output_sample_rate, unsigned int *decM, unsigned int *decEnable, sdrplay_api_If_kHzT *ifType) const
{
sdrplay_api_If_kHzT lif = sdrplay_api_IF_1_620;
double lif_input_sample_rate = 6000000;
if (device.hwVer == SDRPLAY_RSPduo_ID && device.rspDuoSampleFreq == 8000000)
{
lif = sdrplay_api_IF_2_048;
lif_input_sample_rate = 8000000;
}
// all RSPs should support these sample rates
switch (output_sample_rate) {
case 62500:
*ifType = lif; *decM = 32; *decEnable = 1;
return lif_input_sample_rate;
case 125000:
*ifType = lif; *decM = 16; *decEnable = 1;
return lif_input_sample_rate;
case 250000:
*ifType = lif; *decM = 8; *decEnable = 1;
return lif_input_sample_rate;
case 500000:
*ifType = lif; *decM = 4; *decEnable = 1;
return lif_input_sample_rate;
case 1000000:
*ifType = lif; *decM = 2; *decEnable = 1;
return lif_input_sample_rate;
case 2000000:
*ifType = lif; *decM = 1; *decEnable = 0;
return lif_input_sample_rate;
}
if (device.hwVer == SDRPLAY_RSPduo_ID && device.rspDuoMode != sdrplay_api_RspDuoMode_Single_Tuner)
{
return -1;
}
if (output_sample_rate <= 2000000)
{
switch (output_sample_rate) {
case 96000:
*ifType = sdrplay_api_IF_Zero; *decM = 32; *decEnable = 1;
return output_sample_rate * *decM;
case 192000:
*ifType = sdrplay_api_IF_Zero; *decM = 16; *decEnable = 1;
return output_sample_rate * *decM;
case 384000:
*ifType = sdrplay_api_IF_Zero; *decM = 8; *decEnable = 1;
return output_sample_rate * *decM;
case 768000:
*ifType = sdrplay_api_IF_Zero; *decM = 4; *decEnable = 1;