Skip to content

Commit 4084ef6

Browse files
committed
SDRPLAY: balance API refcount via running, fix Open() bounds check and antenna validation
1 parent 1287d85 commit 4084ef6

1 file changed

Lines changed: 14 additions & 16 deletions

File tree

Source/Device/SDRPLAY.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,15 @@ namespace Device {
3131
int SDRPLAY::API_count = 0;
3232

3333
SDRPLAY::SDRPLAY() : Device(Format::CF32, 2304000, Type::SDRPLAY, "SDRPLAY") {
34-
float version = 0.0;
35-
3634
if (API_count == 0 && sdrplay_api_Open() != sdrplay_api_Success)
3735
return;
3836

3937
API_count++;
40-
41-
if (sdrplay_api_ApiVersion(&version) != sdrplay_api_Success)
42-
return;
43-
44-
if ((int)version != 3)
45-
return;
46-
4738
running = true;
4839
}
4940

5041
SDRPLAY::~SDRPLAY() {
51-
if (--API_count == 0) sdrplay_api_Close();
42+
if (running && --API_count == 0) sdrplay_api_Close();
5243
}
5344

5445
void SDRPLAY::Open(uint64_t h) {
@@ -61,7 +52,7 @@ namespace Device {
6152
sdrplay_api_DeviceT devices[SDRPLAY_MAX_DEVICES];
6253
sdrplay_api_GetDevices(devices, &DeviceCount, SDRPLAY_MAX_DEVICES);
6354

64-
if (DeviceCount < h) throw std::runtime_error("SDRPLAY: cannot open device, handle not available.");
55+
if (h >= DeviceCount) throw std::runtime_error("SDRPLAY: cannot open device, handle not available.");
6556

6657
device = devices[h];
6758

@@ -190,7 +181,13 @@ namespace Device {
190181
void SDRPLAY::getDeviceList(std::vector<Description>& DeviceList) {
191182
unsigned int DeviceCount;
192183
if (!running) {
193-
Warning() << "SDRPLAY: API v3.x not running, skipping SDRplay device detection.";
184+
Warning() << "SDRPLAY: API v3.x not running, no SDRplay devices available.";
185+
return;
186+
}
187+
188+
float version = 0.0;
189+
if (sdrplay_api_ApiVersion(&version) != sdrplay_api_Success || (int)version != 3) {
190+
Warning() << "SDRPLAY: API version is not 3.x, no SDRplay devices available.";
194191
return;
195192
}
196193

@@ -230,13 +227,14 @@ namespace Device {
230227
case AIS::KEY_SETTING_GRDB:
231228
gRdB = Util::Parse::Integer(arg, 0, 59);
232229
break;
233-
case AIS::KEY_SETTING_ANTENNA:
234-
if (antenna == 'A' || antenna == 'B') {
235-
antenna = arg[0];
236-
}
230+
case AIS::KEY_SETTING_ANTENNA: {
231+
char a = arg.empty() ? 0 : (arg[0] & ~0x20);
232+
if (a == 'A' || a == 'B' || a == 'C')
233+
antenna = a;
237234
else
238235
throw std::runtime_error("SDRPLAY: invalid antenna.");
239236
break;
237+
}
240238
default:
241239
Device::SetKey(key, arg);
242240
break;

0 commit comments

Comments
 (0)