Skip to content

Commit 34566c7

Browse files
committed
Simplified SdrDeviceReader.
1 parent 2409ce7 commit 34566c7

File tree

5 files changed

+45
-63
lines changed

5 files changed

+45
-63
lines changed

sources/config.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ spdlog::level::level_enum parseLogLevel(const std::string& level) {
2626
return spdlog::level::level_enum::off;
2727
}
2828

29-
Config::Config(const nlohmann::json& json, const ArgConfig& argConfig) : m_id(!argConfig.id.empty() ? argConfig.id : randomHex(8)), m_json(json), m_argConfig(argConfig), m_fileConfig(json) {}
29+
Config::Config(const ArgConfig& argConfig, const FileConfig& fileConfig) : m_id(!argConfig.id.empty() ? argConfig.id : randomHex(8)), m_argConfig(argConfig), m_fileConfig(fileConfig) {}
3030

3131
Config Config::loadFromFile(const std::string& path, const ArgConfig& argConfig) {
3232
std::ifstream stream(path);
3333
if (stream) {
3434
try {
3535
auto json = nlohmann::json::parse(stream);
3636
ConfigMigrator::update(json);
37-
SdrDeviceReader::scanSoapyDevices(json);
38-
ConfigMigrator::sort(json);
39-
return Config(json, argConfig);
37+
FileConfig fileConfig(json);
38+
SdrDeviceReader::updateDevices(fileConfig.devices);
39+
return Config(argConfig, fileConfig);
4040
} catch (const nlohmann::json::parse_error& exception) {
4141
throw std::runtime_error(fmt::format("can not parse config file, invalid json format: {}", path));
4242
}
@@ -50,6 +50,7 @@ void Config::saveToFile(const std::string& path, const nlohmann::json& json) {
5050
if (stream) {
5151
auto tmp = json;
5252
SdrDeviceReader::clearDevices(tmp);
53+
ConfigMigrator::sort(tmp);
5354
stream << std::setw(4) << tmp << std::endl;
5455
} else {
5556
Logger::warn(LABEL, "save new config failed");
@@ -71,7 +72,7 @@ nlohmann::json Config::hideSensitiveData(const nlohmann::json& json) {
7172
return config;
7273
}
7374

74-
nlohmann::json Config::json() const { return m_json; }
75+
nlohmann::json Config::json() const { return static_cast<nlohmann::json>(m_fileConfig); }
7576
std::string Config::mqtt() const { return fmt::format("{}@{}", m_argConfig.mqttUser, m_argConfig.mqttUrl); };
7677

7778
std::string Config::getId() const { return m_id; }

sources/config.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,9 @@ class Config {
7878
int altitude() const;
7979

8080
private:
81-
Config(const nlohmann::json& json, const ArgConfig& argConfig);
81+
Config(const ArgConfig& argConfig, const FileConfig& fileConfig);
8282

8383
const std::string m_id;
84-
const nlohmann::json m_json;
8584
const ArgConfig& m_argConfig;
8685
const FileConfig m_fileConfig;
8786
};

sources/radio/help_structures.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Satellite, id, name, frequency, bandwidth, mo
4444

4545
struct Gain {
4646
std::string name;
47-
float value;
47+
double value;
4848
};
4949
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Gain, name, value)
5050

@@ -58,7 +58,8 @@ struct Device {
5858
float start_recording_level{};
5959
float stop_recording_level{};
6060
std::vector<Satellite> satellites{};
61+
std::vector<Frequency> sample_rates{};
6162

6263
std::string getName() const { return driver + "_" + serial; }
6364
};
64-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Device, enabled, gains, serial, driver, sample_rate, ranges, start_recording_level, stop_recording_level, satellites)
65+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Device, enabled, gains, serial, driver, sample_rate, ranges, start_recording_level, stop_recording_level, satellites, sample_rates)

sources/radio/sdr_device_reader.cpp

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44
#include <logger.h>
55
#include <utils/utils.h>
66

7-
#include <set>
8-
97
constexpr auto LABEL = "config";
108

11-
std::set<Frequency> getSampleRates(SoapySDR::Device* sdr) {
12-
std::set<Frequency> sampleRates;
9+
std::vector<Frequency> getSampleRates(SoapySDR::Device* sdr) {
10+
std::vector<Frequency> sampleRates;
1311
for (const auto value : sdr->listSampleRates(SOAPY_SDR_RX, 0)) {
1412
const auto sampleRate = static_cast<Frequency>(value);
1513
Logger::info(LABEL, " supported sample rate: {}", formatFrequency(sampleRate));
16-
sampleRates.insert(sampleRate);
14+
sampleRates.emplace_back(sampleRate);
1715
}
16+
std::sort(sampleRates.begin(), sampleRates.end());
1817
return sampleRates;
1918
}
2019

21-
std::vector<nlohmann::json> getGains(SoapySDR::Device* sdr) {
22-
std::vector<nlohmann::json> gains;
20+
std::vector<Gain> getGains(SoapySDR::Device* sdr) {
21+
std::vector<Gain> gains;
2322
for (const auto& gain : sdr->listGains(SOAPY_SDR_RX, 0)) {
2423
const auto gainRange = sdr->getGainRange(SOAPY_SDR_RX, 0, gain);
2524
Logger::info(
@@ -29,12 +28,12 @@ std::vector<nlohmann::json> getGains(SoapySDR::Device* sdr) {
2928
colored(GREEN, "{}", gainRange.minimum()),
3029
colored(GREEN, "{}", gainRange.maximum()),
3130
colored(GREEN, "{}", gainRange.step()));
32-
gains.push_back({{"name", gain}, {"value", gainRange.maximum()}});
31+
gains.emplace_back(gain, gainRange.maximum());
3332
}
3433
return gains;
3534
}
3635

37-
void SdrDeviceReader::updateSoapyDevice(nlohmann::json& json, const SoapySDR::Kwargs args) {
36+
void SdrDeviceReader::updateDevice(Device& device, const SoapySDR::Kwargs args) {
3837
const auto serial = args.at("serial");
3938
const auto driver = args.at("driver");
4039
Logger::info(LABEL, "update device, driver: {}, serial: {}", colored(GREEN, "{}", driver), colored(GREEN, "{}", serial));
@@ -44,19 +43,13 @@ void SdrDeviceReader::updateSoapyDevice(nlohmann::json& json, const SoapySDR::Kw
4443
throw std::runtime_error("open device failed");
4544
}
4645

47-
json["driver"] = driver;
48-
49-
const auto sampleRate = json.at("sample_rate").get<Frequency>();
50-
const auto sampleRates = getSampleRates(sdr);
51-
json["sample_rates"] = sampleRates;
52-
if (sampleRates.count(sampleRate) == 0) {
53-
json["sample_rate"] = getNearestElement(sampleRates, sampleRate);
54-
}
46+
device.driver = driver;
47+
device.sample_rates = getSampleRates(sdr);
5548

5649
SoapySDR::Device::unmake(sdr);
5750
}
5851

59-
void SdrDeviceReader::createSoapyDevices(nlohmann::json& json, const SoapySDR::Kwargs args) {
52+
Device SdrDeviceReader::createDevice(const SoapySDR::Kwargs args) {
6053
const auto serial = args.at("serial");
6154
const auto driver = args.at("driver");
6255
Logger::info(LABEL, "creating device, driver: {}, serial: {}", colored(GREEN, "{}", driver), colored(GREEN, "{}", serial));
@@ -66,23 +59,20 @@ void SdrDeviceReader::createSoapyDevices(nlohmann::json& json, const SoapySDR::K
6659
throw std::runtime_error("open device failed");
6760
}
6861

69-
json["driver"] = driver;
70-
json["serial"] = serial;
71-
json["enabled"] = true;
72-
json["start_recording_level"] = DEFAULT_RECORDING_START_LEVEL;
73-
json["stop_recording_level"] = DEFAULT_RECORDING_STOP_LEVEL;
74-
75-
const auto sampleRates = getSampleRates(sdr);
76-
json["sample_rates"] = sampleRates;
77-
78-
json["ranges"] = nlohmann::json::array();
79-
auto addSampleRate = [&sampleRates, &json](Frequency start, Frequency stop, Frequency sampleRate) {
80-
if (json.at("ranges").empty() && sampleRates.count(sampleRate)) {
81-
json["ranges"].push_back({{"start", start}, {"stop", stop}});
82-
json["sample_rate"] = sampleRate;
83-
return true;
84-
} else {
85-
return false;
62+
Device device;
63+
device.driver = driver;
64+
device.serial = serial;
65+
device.enabled = true;
66+
device.start_recording_level = DEFAULT_RECORDING_START_LEVEL;
67+
device.stop_recording_level = DEFAULT_RECORDING_STOP_LEVEL;
68+
device.sample_rates = getSampleRates(sdr);
69+
device.gains = getGains(sdr);
70+
71+
auto addSampleRate = [&device](Frequency start, Frequency stop, Frequency sampleRate) {
72+
const auto contains = std::find(device.sample_rates.begin(), device.sample_rates.end(), sampleRate) != device.sample_rates.end();
73+
if (device.ranges.empty() && contains) {
74+
device.ranges.emplace_back(start, stop);
75+
device.sample_rate = sampleRate;
8676
}
8777
};
8878

@@ -92,35 +82,26 @@ void SdrDeviceReader::createSoapyDevices(nlohmann::json& json, const SoapySDR::K
9282
addSampleRate(144000000, 146000000, 2000000);
9383
addSampleRate(144000000, 146000000, 1024000);
9484
addSampleRate(144000000, 146000000, 1000000);
95-
addSampleRate(144000000, 146000000, *sampleRates.rbegin());
96-
97-
json["gains"] = getGains(sdr);
98-
json["satellites"] = nlohmann::json::array();
85+
addSampleRate(144000000, 146000000, *device.sample_rates.rbegin());
9986

10087
SoapySDR::Device::unmake(sdr);
88+
return device;
10189
}
10290

103-
void SdrDeviceReader::scanSoapyDevices(nlohmann::json& json) {
91+
void SdrDeviceReader::updateDevices(std::vector<Device>& devices) {
10492
Logger::info(LABEL, "scanning connected devices");
10593
const SoapySDR::KwargsList results = SoapySDR::Device::enumerate("remote=");
10694
Logger::info(LABEL, "found {} devices:", colored(GREEN, "{}", results.size()));
10795

108-
for (auto& device : json.at("devices")) {
109-
device["driver"] = "";
110-
device["sample_rates"] = nlohmann::json::array();
111-
}
11296
for (uint32_t i = 0; i < results.size(); ++i) {
11397
try {
114-
auto& devices = json.at("devices");
11598
const auto serial = results[i].at("serial");
116-
const auto f = [serial](nlohmann::json& device) { return device.at("serial").get<std::string>() == serial; };
99+
const auto f = [serial](const Device& device) { return device.serial == serial; };
117100
const auto it = std::find_if(devices.begin(), devices.end(), f);
118101
if (it != devices.end()) {
119-
updateSoapyDevice(*it, results[i]);
102+
updateDevice(*it, results[i]);
120103
} else {
121-
nlohmann::json device;
122-
createSoapyDevices(device, results[i]);
123-
devices.push_back(device);
104+
devices.push_back(createDevice(results[i]));
124105
}
125106
} catch (const std::exception& exception) {
126107
Logger::warn(LABEL, "scan device exception: {}", exception.what());

sources/radio/sdr_device_reader.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
class SdrDeviceReader {
1010
private:
11-
static void updateSoapyDevice(nlohmann::json& json, const SoapySDR::Kwargs args);
12-
static void createSoapyDevices(nlohmann::json& json, const SoapySDR::Kwargs args);
11+
static void updateDevice(Device& device, const SoapySDR::Kwargs args);
12+
static Device createDevice(const SoapySDR::Kwargs args);
1313

1414
public:
15-
static void scanSoapyDevices(nlohmann::json& json);
15+
static void updateDevices(std::vector<Device>& devices);
1616
static void clearDevices(nlohmann::json& json);
17-
};
17+
};

0 commit comments

Comments
 (0)