2015-10-04 16:07:14 -04:00
|
|
|
#include "SDREnumerator.h"
|
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include <vector>
|
|
|
|
#include "CubicSDR.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> SDREnumerator::factories;
|
|
|
|
std::vector<std::string> SDREnumerator::modules;
|
2015-10-05 02:21:08 -04:00
|
|
|
std::vector<std::string> SDREnumerator::remotes;
|
|
|
|
std::map< std::string, std::vector<SDRDeviceInfo *> > SDREnumerator::devs;
|
|
|
|
bool SDREnumerator::soapy_initialized = false;
|
|
|
|
bool SDREnumerator::has_remote = false;
|
2015-10-04 16:07:14 -04:00
|
|
|
|
|
|
|
SDREnumerator::SDREnumerator() : IOThread() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
SDREnumerator::~SDREnumerator() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
std::vector<SDRDeviceInfo *> *SDREnumerator::enumerate_devices(std::string remoteAddr, bool noInit) {
|
2015-10-04 16:07:14 -04:00
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
if (SDREnumerator::devs[remoteAddr].size()) {
|
|
|
|
return &SDREnumerator::devs[remoteAddr];
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
if (noInit) {
|
|
|
|
return NULL;
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
2015-10-05 02:21:08 -04:00
|
|
|
|
|
|
|
if (!soapy_initialized) {
|
|
|
|
std::cout << "SoapySDR init.." << std::endl;
|
|
|
|
std::cout << "\tAPI Version: v" << SoapySDR::getAPIVersion() << std::endl;
|
|
|
|
std::cout << "\tABI Version: v" << SoapySDR::getABIVersion() << std::endl;
|
|
|
|
std::cout << "\tInstall root: " << SoapySDR::getRootPath() << std::endl;
|
|
|
|
|
2015-11-08 16:45:06 -05:00
|
|
|
std::cout << "\tLoading modules... " << std::endl;
|
2015-11-12 18:38:38 -05:00
|
|
|
|
|
|
|
std::string userModPath = wxGetApp().getModulePath();
|
|
|
|
|
|
|
|
if (userModPath != "") {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Loading SoapySDR modules from " + userModPath + "..");
|
|
|
|
std::vector<std::string> localMods = SoapySDR::listModules(userModPath);
|
|
|
|
for (std::vector<std::string>::iterator mods_i = localMods.begin(); mods_i != localMods.end(); mods_i++) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Initializing user specified SoapySDR module " + (*mods_i) + "..");
|
|
|
|
std::cout << "Initializing user specified SoapySDR module " << (*mods_i) << ".." << std::endl;
|
|
|
|
SoapySDR::loadModule(*mods_i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef BUNDLE_SOAPY_MODS
|
|
|
|
bool localModPref = wxGetApp().getUseLocalMod();
|
|
|
|
if (localModPref) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Loading SoapySDR modules..");
|
|
|
|
std::cout << "Checking local system SoapySDR modules.." << std::flush;
|
|
|
|
SoapySDR::loadModules();
|
|
|
|
}
|
2015-10-29 22:22:10 -04:00
|
|
|
|
2015-11-12 18:38:38 -05:00
|
|
|
wxFileName exePath = wxFileName(wxStandardPaths::Get().GetExecutablePath());
|
|
|
|
std::vector<std::string> localMods = SoapySDR::listModules(exePath.GetPath().ToStdString() + "/modules/");
|
|
|
|
for (std::vector<std::string>::iterator mods_i = localMods.begin(); mods_i != localMods.end(); mods_i++) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Initializing bundled SoapySDR module " + (*mods_i) + "..");
|
|
|
|
std::cout << "Loading bundled SoapySDR module " << (*mods_i) << ".." << std::endl;
|
|
|
|
SoapySDR::loadModule(*mods_i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!localModPref) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Loading SoapySDR modules..");
|
|
|
|
std::cout << "Checking system SoapySDR modules.." << std::flush;
|
|
|
|
SoapySDR::loadModules();
|
|
|
|
}
|
|
|
|
#else
|
2015-11-08 16:45:06 -05:00
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Loading SoapySDR modules..");
|
|
|
|
SoapySDR::loadModules();
|
2015-11-12 18:38:38 -05:00
|
|
|
#endif
|
2015-11-08 16:45:06 -05:00
|
|
|
|
2015-11-12 18:38:38 -05:00
|
|
|
}
|
2015-11-08 16:45:06 -05:00
|
|
|
// modules = SoapySDR::listModules();
|
|
|
|
// for (size_t i = 0; i < modules.size(); i++) {
|
|
|
|
// std::cout << "\tModule found: " << modules[i] << std::endl;
|
|
|
|
// }
|
|
|
|
// if (modules.empty()) {
|
|
|
|
// std::cout << "No modules found!" << std::endl;
|
|
|
|
// }
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
if (SDREnumerator::factories.size()) {
|
|
|
|
SDREnumerator::factories.erase(SDREnumerator::factories.begin(), SDREnumerator::factories.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\tAvailable factories...";
|
|
|
|
SoapySDR::FindFunctions factories = SoapySDR::Registry::listFindFunctions();
|
|
|
|
for (SoapySDR::FindFunctions::const_iterator it = factories.begin(); it != factories.end(); ++it) {
|
|
|
|
if (it != factories.begin()) {
|
|
|
|
std::cout << ", ";
|
|
|
|
}
|
|
|
|
std::cout << it->first;
|
|
|
|
|
|
|
|
if (it->first == "remote") {
|
|
|
|
has_remote = true;
|
|
|
|
}
|
|
|
|
SDREnumerator::factories.push_back(it->first);
|
|
|
|
}
|
|
|
|
if (factories.empty()) {
|
|
|
|
std::cout << "No factories found!" << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
soapy_initialized = true;
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
std::vector<SoapySDR::Kwargs> results;
|
|
|
|
SoapySDR::Kwargs enumArgs;
|
|
|
|
bool isRemote = false;
|
2015-10-04 16:07:14 -04:00
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
if (remoteAddr.length()) {
|
|
|
|
std::cout << "Enumerating remote address: " << remoteAddr << std::endl;
|
|
|
|
enumArgs["driver"] = "remote";
|
|
|
|
enumArgs["remote"] = remoteAddr;
|
|
|
|
isRemote = true;
|
|
|
|
|
|
|
|
results = SoapySDR::Device::enumerate(enumArgs);
|
|
|
|
} else {
|
|
|
|
results = SoapySDR::Device::enumerate();
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
if (isRemote) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, std::string("Opening remote server ") + remoteAddr + "..");
|
|
|
|
}
|
2015-10-04 16:07:14 -04:00
|
|
|
for (size_t i = 0; i < results.size(); i++) {
|
|
|
|
SDRDeviceInfo *dev = new SDRDeviceInfo();
|
2015-10-05 02:21:08 -04:00
|
|
|
|
|
|
|
SoapySDR::Kwargs deviceArgs = results[i];
|
|
|
|
|
|
|
|
for (SoapySDR::Kwargs::const_iterator it = deviceArgs.begin(); it != deviceArgs.end(); ++it) {
|
2015-10-04 16:07:14 -04:00
|
|
|
std::cout << " " << it->first << " = " << it->second << std::endl;
|
|
|
|
if (it->first == "driver") {
|
|
|
|
dev->setDriver(it->second);
|
2015-10-11 03:46:16 -04:00
|
|
|
} else if (it->first == "label" || it->first == "device") {
|
2015-10-04 16:07:14 -04:00
|
|
|
dev->setName(it->second);
|
2015-10-11 03:46:16 -04:00
|
|
|
}
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
2015-11-11 00:16:26 -05:00
|
|
|
|
2015-12-06 00:32:32 -05:00
|
|
|
DeviceConfig *cfg = wxGetApp().getConfig()->getDevice(dev->getDeviceId());
|
|
|
|
|
2015-11-11 00:16:26 -05:00
|
|
|
if (deviceArgs.count("remote")) {
|
|
|
|
isRemote = true;
|
|
|
|
} else {
|
|
|
|
isRemote = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->setRemote(isRemote);
|
2015-10-04 16:07:14 -04:00
|
|
|
|
|
|
|
std::cout << "Make device " << i << std::endl;
|
|
|
|
try {
|
2015-12-06 00:32:32 -05:00
|
|
|
SoapySDR::Device *device = SoapySDR::Device::make(deviceArgs);
|
2015-10-04 16:07:14 -04:00
|
|
|
SoapySDR::Kwargs info = device->getHardwareInfo();
|
|
|
|
for (SoapySDR::Kwargs::const_iterator it = info.begin(); it != info.end(); ++it) {
|
|
|
|
std::cout << " " << it->first << "=" << it->second << std::endl;
|
|
|
|
if (it->first == "hardware") {
|
|
|
|
dev->setHardware(it->second);
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 00:16:26 -05:00
|
|
|
|
2015-11-10 19:25:41 -05:00
|
|
|
if (isRemote) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Querying remote " + remoteAddr + " device #" + std::to_string(i) + ": " + dev-> getName());
|
|
|
|
} else {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, std::string("Querying device #") + std::to_string(i) + ": " + dev->getName());
|
|
|
|
}
|
2015-12-06 00:32:32 -05:00
|
|
|
|
|
|
|
SoapySDR::ArgInfoList settingsInfo = device->getSettingInfo();
|
|
|
|
|
|
|
|
ConfigSettings devSettings = cfg->getSettings();
|
|
|
|
if (devSettings.size()) {
|
|
|
|
for (ConfigSettings::const_iterator set_i = devSettings.begin(); set_i != devSettings.end(); set_i++) {
|
|
|
|
deviceArgs[set_i->first] = set_i->second;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < settingsInfo.size(); j++) {
|
|
|
|
if (deviceArgs.find(settingsInfo[j].key) != deviceArgs.end()) {
|
|
|
|
settingsInfo[j].value = deviceArgs[settingsInfo[j].key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 16:07:14 -04:00
|
|
|
|
2015-12-06 00:32:32 -05:00
|
|
|
dev->setDeviceArgs(deviceArgs);
|
|
|
|
dev->setSettingsInfo(settingsInfo);
|
|
|
|
|
2015-10-04 19:55:52 -04:00
|
|
|
int numChan = device->getNumChannels(SOAPY_SDR_RX);
|
|
|
|
for (int i = 0; i < numChan; i++) {
|
|
|
|
SDRDeviceChannel *chan = new SDRDeviceChannel();
|
|
|
|
|
|
|
|
SoapySDR::RangeList rfRange = device->getFrequencyRange(SOAPY_SDR_RX, i);
|
|
|
|
double rfMin = rfRange[0].minimum();
|
|
|
|
double rfMax = rfRange[rfRange.size()-1].maximum();
|
|
|
|
chan->setChannel(i);
|
|
|
|
chan->setFullDuplex(device->getFullDuplex(SOAPY_SDR_RX, i));
|
|
|
|
chan->setRx(true);
|
|
|
|
chan->setTx(false);
|
|
|
|
chan->getRFRange().setLow(rfMin);
|
|
|
|
chan->getRFRange().setHigh(rfMax);
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
std::vector<std::string> freqs = device->listFrequencies(SOAPY_SDR_RX,i);
|
|
|
|
if (std::find(freqs.begin(), freqs.end(), "CORR") != freqs.end()) {
|
|
|
|
chan->setCORR(true);
|
|
|
|
} else {
|
|
|
|
chan->setCORR(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device->hasDCOffsetMode(SOAPY_SDR_RX, i)) {
|
|
|
|
chan->setHardwareDC(true);
|
|
|
|
} else {
|
|
|
|
chan->setHardwareDC(false);
|
|
|
|
}
|
|
|
|
|
2015-10-04 19:55:52 -04:00
|
|
|
std::vector<double> rates = device->listSampleRates(SOAPY_SDR_RX, i);
|
2015-10-20 23:00:10 -04:00
|
|
|
for (std::vector<double>::iterator i = rates.begin(); i != rates.end(); i++) {
|
|
|
|
chan->getSampleRates().push_back((long)(*i));
|
|
|
|
}
|
2015-10-23 02:53:51 -04:00
|
|
|
|
2015-12-06 00:32:32 -05:00
|
|
|
ConfigSettings devStreamOpts = cfg->getStreamOpts();
|
|
|
|
if (devStreamOpts.size()) {
|
|
|
|
dev->setStreamArgs(devStreamOpts);
|
|
|
|
}
|
|
|
|
|
|
|
|
SoapySDR::ArgInfoList optArgs = device->getStreamArgsInfo(SOAPY_SDR_RX, i);
|
|
|
|
|
|
|
|
if (devStreamOpts.size()) {
|
|
|
|
for (int j = 0, jMax = optArgs.size(); j < jMax; j++) {
|
|
|
|
if (devStreamOpts.find(optArgs[j].key) != devStreamOpts.end()) {
|
|
|
|
optArgs[j].value = devStreamOpts[optArgs[j].key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chan->setStreamArgsInfo(optArgs);
|
2015-10-23 02:53:51 -04:00
|
|
|
|
2015-10-27 01:56:49 -04:00
|
|
|
std::vector<std::string> gainNames = device->listGains(SOAPY_SDR_RX, i);
|
|
|
|
|
|
|
|
for (std::vector<std::string>::iterator gname = gainNames.begin(); gname!= gainNames.end(); gname++) {
|
|
|
|
chan->addGain((*gname),device->getGainRange(SOAPY_SDR_RX, i, (*gname)));
|
|
|
|
}
|
|
|
|
|
2015-10-04 19:55:52 -04:00
|
|
|
dev->addChannel(chan);
|
|
|
|
}
|
|
|
|
|
2015-10-04 16:07:14 -04:00
|
|
|
SoapySDR::Device::unmake(device);
|
|
|
|
|
|
|
|
dev->setAvailable(true);
|
|
|
|
} catch (const std::exception &ex) {
|
|
|
|
std::cerr << "Error making device: " << ex.what() << std::endl;
|
2015-11-10 19:25:41 -05:00
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, std::string("Error querying device #") + std::to_string(i));
|
2015-10-04 16:07:14 -04:00
|
|
|
dev->setAvailable(false);
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
SDREnumerator::devs[remoteAddr].push_back(dev);
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
2015-10-05 02:21:08 -04:00
|
|
|
if (SDREnumerator::devs[remoteAddr].empty()) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, std::string("No devices found!"));
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
return &SDREnumerator::devs[remoteAddr];
|
2015-10-04 16:07:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SDREnumerator::run() {
|
|
|
|
|
|
|
|
std::cout << "SDR enumerator starting." << std::endl;
|
|
|
|
terminated.store(false);
|
2015-10-05 02:21:08 -04:00
|
|
|
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Scanning local devices, please wait..");
|
|
|
|
SDREnumerator::enumerate_devices("");
|
2015-10-04 16:07:14 -04:00
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
if (remotes.size()) {
|
|
|
|
std::vector<std::string>::iterator remote_i;
|
|
|
|
for (remote_i = remotes.begin(); remote_i != remotes.end(); remote_i++) {
|
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_MESSAGE, "Scanning devices at " + (*remote_i) + ", please wait..");
|
|
|
|
SDREnumerator::enumerate_devices(*remote_i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 16:07:14 -04:00
|
|
|
std::cout << "Reporting enumeration complete." << std::endl;
|
|
|
|
terminated.store(true);
|
2015-10-05 02:21:08 -04:00
|
|
|
wxGetApp().sdrEnumThreadNotify(SDREnumerator::SDR_ENUM_DEVICES_READY, "Finished scanning devices.");
|
2015-10-04 16:07:14 -04:00
|
|
|
std::cout << "SDR enumerator done." << std::endl;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-05 02:21:08 -04:00
|
|
|
|
|
|
|
void SDREnumerator::addRemote(std::string remoteAddr) {
|
|
|
|
std::vector<std::string>::iterator remote_i = std::find(remotes.begin(), remotes.end(), remoteAddr);
|
|
|
|
|
|
|
|
if (remote_i != remotes.end()) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
remotes.push_back(remoteAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDREnumerator::removeRemote(std::string remoteAddr) {
|
|
|
|
std::vector<std::string>::iterator remote_i = std::find(remotes.begin(), remotes.end(), remoteAddr);
|
|
|
|
|
|
|
|
if (remote_i != remotes.end()) {
|
|
|
|
if (devs.find(*remote_i) != devs.end()) {
|
|
|
|
while (devs[*remote_i].size()) {
|
|
|
|
SDRDeviceInfo *devRemove = devs[*remote_i].back();
|
|
|
|
devs[*remote_i].pop_back();
|
|
|
|
delete devRemove;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
remotes.erase(remote_i);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> &SDREnumerator::getRemotes() {
|
|
|
|
return remotes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SDREnumerator::hasRemoteModule() {
|
|
|
|
return SDREnumerator::has_remote;
|
|
|
|
}
|