2015-09-13 22:18:29 -04:00
|
|
|
#include "SoapySDRThread.h"
|
|
|
|
#include "CubicSDRDefs.h"
|
|
|
|
#include <vector>
|
|
|
|
#include "CubicSDR.h"
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> SDRThread::factories;
|
|
|
|
std::vector<std::string> SDRThread::modules;
|
|
|
|
std::vector<SDRDeviceInfo *> SDRThread::devs;
|
|
|
|
|
|
|
|
|
|
|
|
SDRThread::SDRThread() : IOThread() {
|
2015-10-03 21:35:11 -04:00
|
|
|
device = NULL;
|
|
|
|
|
|
|
|
deviceConfig.store(NULL);
|
|
|
|
deviceInfo.store(NULL);
|
|
|
|
|
2015-09-13 22:18:29 -04:00
|
|
|
sampleRate.store(DEFAULT_SAMPLE_RATE);
|
2015-10-03 21:35:11 -04:00
|
|
|
frequency.store(0);
|
|
|
|
offset.store(0);
|
|
|
|
ppm.store(0);
|
|
|
|
direct_sampling_mode.store(0);
|
|
|
|
|
|
|
|
numElems.store(0);
|
|
|
|
|
|
|
|
rate_changed.store(false);
|
|
|
|
freq_changed.store(false);
|
|
|
|
offset_changed.store(false);
|
|
|
|
ppm_changed .store(false);
|
|
|
|
direct_sampling_changed.store(false);
|
|
|
|
device_changed.store(false);
|
|
|
|
|
|
|
|
hasPPM.store(false);
|
|
|
|
hasHardwareDC.store(false);
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SDRThread::~SDRThread() {
|
2015-10-03 21:35:11 -04:00
|
|
|
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<SDRDeviceInfo *> *SDRThread::enumerate_devices() {
|
|
|
|
|
|
|
|
if (SDRThread::devs.size()) {
|
|
|
|
return &SDRThread::devs;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "\tLoading modules... " << std::flush;
|
|
|
|
SoapySDR::loadModules();
|
|
|
|
std::cout << "done" << std::endl;
|
|
|
|
|
|
|
|
if (SDRThread::factories.size()) {
|
|
|
|
SDRThread::factories.erase(SDRThread::factories.begin(), SDRThread::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;
|
|
|
|
SDRThread::factories.push_back(it->first);
|
|
|
|
}
|
|
|
|
if (factories.empty()) {
|
|
|
|
std::cout << "No factories found!" << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
std::vector<SoapySDR::Kwargs> results = SoapySDR::Device::enumerate();
|
2015-09-26 20:39:33 -04:00
|
|
|
|
|
|
|
// Remote driver test..
|
|
|
|
/* * /
|
|
|
|
SDRDeviceInfo *remoteDev = new SDRDeviceInfo();
|
|
|
|
remoteDev->setDriver("remote");
|
|
|
|
remoteDev->setName("SoapySDR Remote Test");
|
2015-09-30 23:45:06 -04:00
|
|
|
|
2015-09-26 20:39:33 -04:00
|
|
|
SoapySDR::Kwargs remoteArgs;
|
|
|
|
remoteArgs["driver"] = "remote";
|
2015-09-30 23:45:06 -04:00
|
|
|
// remoteArgs["remote"] = "127.0.0.1";
|
|
|
|
remoteArgs["remote"] = "192.168.1.103";
|
2015-09-26 20:39:33 -04:00
|
|
|
remoteArgs["remote:driver"] = "rtlsdr";
|
|
|
|
remoteArgs["buffers"] = "6";
|
|
|
|
remoteArgs["buflen"] = "16384";
|
|
|
|
remoteDev->setDeviceArgs(remoteArgs);
|
2015-09-30 23:45:06 -04:00
|
|
|
|
|
|
|
SoapySDR::Kwargs streamArgs;
|
|
|
|
streamArgs["remote:mtu"] = "8192";
|
|
|
|
streamArgs["remote:format"] = "CS8";
|
|
|
|
streamArgs["remote:window"] = "16384000";
|
|
|
|
remoteDev->setStreamArgs(streamArgs);
|
|
|
|
|
2015-09-26 20:39:33 -04:00
|
|
|
SDRThread::devs.push_back(remoteDev);
|
|
|
|
// */
|
2015-09-13 22:18:29 -04:00
|
|
|
|
|
|
|
for (size_t i = 0; i < results.size(); i++) {
|
|
|
|
std::cout << "Found device " << i << std::endl;
|
|
|
|
SDRDeviceInfo *dev = new SDRDeviceInfo();
|
|
|
|
for (SoapySDR::Kwargs::const_iterator it = results[i].begin(); it != results[i].end(); ++it) {
|
|
|
|
std::cout << " " << it->first << " = " << it->second << std::endl;
|
|
|
|
if (it->first == "driver") {
|
|
|
|
dev->setDriver(it->second);
|
|
|
|
} else if (it->first == "label") {
|
|
|
|
dev->setName(it->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 21:48:37 -04:00
|
|
|
dev->setDeviceArgs(results[i]);
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-09-20 21:48:37 -04:00
|
|
|
std::cout << "Make device " << i << std::endl;
|
2015-09-13 22:18:29 -04:00
|
|
|
try {
|
|
|
|
SoapySDR::Device *device = SoapySDR::Device::make(dev->getDeviceArgs());
|
|
|
|
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-09-30 23:45:06 -04:00
|
|
|
|
|
|
|
if (device->hasDCOffsetMode(SOAPY_SDR_RX, 0)) {
|
|
|
|
device->setDCOffsetMode(SOAPY_SDR_RX, 0, true);
|
|
|
|
std::cout << "Hardware DC offset support detected; internal DC offset correction will be disabled." << std::endl;
|
|
|
|
dev->setHardwareDC(true);
|
|
|
|
} else {
|
|
|
|
dev->setHardwareDC(false);
|
|
|
|
}
|
|
|
|
|
2015-09-13 22:18:29 -04:00
|
|
|
SoapySDR::Device::unmake(device);
|
2015-09-30 23:45:06 -04:00
|
|
|
|
2015-09-13 22:18:29 -04:00
|
|
|
dev->setAvailable(true);
|
|
|
|
} catch (const std::exception &ex) {
|
|
|
|
std::cerr << "Error making device: " << ex.what() << std::endl;
|
|
|
|
dev->setAvailable(false);
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
SDRThread::devs.push_back(dev);
|
|
|
|
}
|
|
|
|
if (results.empty()) {
|
|
|
|
std::cout << "No devices found!" << std::endl;
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
return &SDRThread::devs;
|
|
|
|
}
|
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
void SDRThread::init() {
|
|
|
|
SDRDeviceInfo *devInfo = deviceInfo.load();
|
|
|
|
deviceConfig.store(wxGetApp().getConfig()->getDevice(devInfo->getDeviceId()));
|
|
|
|
DeviceConfig *devConfig = deviceConfig.load();
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
frequency = wxGetApp().getConfig()->getCenterFreq();
|
|
|
|
ppm.store(devConfig->getPPM());
|
|
|
|
direct_sampling_mode.store(devConfig->getDirectSampling());
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
std::string driverName = devInfo->getDriver();
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
offset = devConfig->getOffset();
|
2015-09-13 22:18:29 -04:00
|
|
|
wxGetApp().setSwapIQ(devConfig->getIQSwap());
|
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
SoapySDR::Kwargs args = devInfo->getDeviceArgs();
|
2015-09-26 01:41:30 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
args["direct_samp"] = std::to_string(devConfig->getDirectSampling());
|
2015-09-26 01:41:30 -04:00
|
|
|
|
|
|
|
if (driverName == "rtl" || driverName == "rtlsdr") {
|
2015-10-03 21:35:11 -04:00
|
|
|
args["buffers"] = "6";
|
|
|
|
args["buflen"] = "16384";
|
2015-09-26 01:41:30 -04:00
|
|
|
hasPPM = true;
|
2015-10-03 21:35:11 -04:00
|
|
|
} else {
|
|
|
|
hasPPM = false;
|
2015-09-26 01:41:30 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
device = SoapySDR::Device::make(args);
|
|
|
|
stream = device->setupStream(SOAPY_SDR_RX,"CF32", std::vector<size_t>(), devInfo->getStreamArgs());
|
2015-09-30 23:45:06 -04:00
|
|
|
|
|
|
|
device->activateStream(stream);
|
2015-09-13 22:18:29 -04:00
|
|
|
device->setSampleRate(SOAPY_SDR_RX,0,sampleRate.load());
|
|
|
|
device->setFrequency(SOAPY_SDR_RX,0,"RF",frequency - offset.load());
|
2015-09-26 01:41:30 -04:00
|
|
|
if (hasPPM) {
|
|
|
|
device->setFrequency(SOAPY_SDR_RX,0,"CORR",ppm);
|
|
|
|
}
|
2015-09-30 23:45:06 -04:00
|
|
|
device->setGainMode(SOAPY_SDR_RX,0,true);
|
2015-10-03 21:35:11 -04:00
|
|
|
hasHardwareDC = devInfo->hasHardwareDC();
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-09-22 21:03:23 -04:00
|
|
|
numElems = getOptimalElementCount(sampleRate.load(), 60);
|
|
|
|
|
|
|
|
buffs[0] = malloc(numElems * 2 * sizeof(float));
|
2015-10-03 21:35:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRThread::deinit() {
|
|
|
|
device->deactivateStream(stream);
|
|
|
|
device->closeStream(stream);
|
|
|
|
SoapySDR::Device::unmake(device);
|
|
|
|
free(buffs[0]);
|
|
|
|
}
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
void SDRThread::readStream(SDRThreadIQDataQueue* iqDataOutQueue) {
|
2015-09-13 22:18:29 -04:00
|
|
|
int flags;
|
|
|
|
long long timeNs;
|
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
SDRThreadIQData *dataOut = buffers.getBuffer();
|
|
|
|
if (dataOut->data.size() != numElems * 2) {
|
|
|
|
dataOut->data.resize(numElems * 2);
|
|
|
|
}
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
int n_read = 0;
|
|
|
|
while (n_read != numElems) {
|
|
|
|
int n_stream_read = device->readStream(stream, buffs, numElems-n_read, flags, timeNs);
|
|
|
|
if (n_stream_read > 0) {
|
|
|
|
memcpy(&dataOut->data[n_read * 2], buffs[0], n_stream_read * sizeof(float) * 2);
|
|
|
|
n_read += n_stream_read;
|
|
|
|
} else {
|
|
|
|
dataOut->data.resize(n_read);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// std::cout << n_read << std::endl;
|
|
|
|
|
|
|
|
if (n_read > 0) {
|
|
|
|
dataOut->setRefCount(1);
|
|
|
|
dataOut->frequency = frequency;
|
|
|
|
dataOut->sampleRate = sampleRate.load();
|
|
|
|
dataOut->dcCorrected = hasHardwareDC;
|
|
|
|
|
|
|
|
iqDataOutQueue->push(dataOut);
|
|
|
|
}
|
|
|
|
}
|
2015-09-13 22:18:29 -04:00
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
void SDRThread::readLoop() {
|
|
|
|
SDRThreadIQDataQueue* iqDataOutQueue = (SDRThreadIQDataQueue*) getOutputQueue("IQDataOutput");
|
|
|
|
|
|
|
|
if (iqDataOutQueue == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!terminated.load()) {
|
|
|
|
if (offset_changed.load()) {
|
|
|
|
if (!freq_changed.load()) {
|
|
|
|
frequency.store(frequency.load());
|
|
|
|
freq_changed.store(true);
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
2015-10-03 21:35:11 -04:00
|
|
|
offset_changed.store(false);
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
2015-10-03 21:35:11 -04:00
|
|
|
if (rate_changed.load()) {
|
|
|
|
device->setSampleRate(SOAPY_SDR_RX,0,sampleRate.load());
|
|
|
|
sampleRate.store(device->getSampleRate(SOAPY_SDR_RX,0));
|
|
|
|
numElems.store(getOptimalElementCount(sampleRate.load(), 60));
|
|
|
|
free(buffs[0]);
|
|
|
|
buffs[0] = malloc(numElems.load() * 2 * sizeof(float));
|
|
|
|
rate_changed.store(false);
|
2015-09-26 20:39:33 -04:00
|
|
|
}
|
2015-10-03 21:35:11 -04:00
|
|
|
if (ppm_changed.load() && hasPPM.load()) {
|
|
|
|
device->setFrequency(SOAPY_SDR_RX,0,"CORR",ppm.load());
|
|
|
|
direct_sampling_changed.store(false);
|
2015-09-26 20:39:33 -04:00
|
|
|
}
|
2015-10-03 21:35:11 -04:00
|
|
|
if (freq_changed.load()) {
|
|
|
|
device->setFrequency(SOAPY_SDR_RX,0,"RF",frequency.load() - offset.load());
|
|
|
|
freq_changed.store(false);
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
2015-10-03 21:35:11 -04:00
|
|
|
if (direct_sampling_changed.load()) {
|
|
|
|
// rtlsdr_set_direct_sampling(dev, direct_sampling_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
readStream(iqDataOutQueue);
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
2015-09-14 20:31:39 -04:00
|
|
|
buffers.purge();
|
2015-10-03 21:35:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SDRThread::run() {
|
|
|
|
//#ifdef __APPLE__
|
|
|
|
// pthread_t tID = pthread_self(); // ID of this thread
|
|
|
|
// int priority = sched_get_priority_max( SCHED_FIFO) - 1;
|
|
|
|
// sched_param prio = { priority }; // scheduling priority of thread
|
|
|
|
// pthread_setschedparam(tID, SCHED_FIFO, &prio);
|
|
|
|
//#endif
|
|
|
|
|
|
|
|
std::cout << "SDR thread starting." << std::endl;
|
|
|
|
terminated.store(false);
|
|
|
|
|
|
|
|
if (deviceInfo.load() != NULL) {
|
|
|
|
std::cout << "device init()" << std::endl;
|
|
|
|
init();
|
|
|
|
std::cout << "starting readLoop()" << std::endl;
|
|
|
|
readLoop();
|
|
|
|
std::cout << "readLoop() ended." << std::endl;
|
|
|
|
deinit();
|
|
|
|
std::cout << "device deinit()" << std::endl;
|
|
|
|
} else {
|
|
|
|
std::cout << "Device setting not found, enumerating." << std::endl;
|
|
|
|
SDRThread::enumerate_devices();
|
|
|
|
std::cout << "Reporting enumeration complete." << std::endl;
|
|
|
|
wxGetApp().sdrThreadNotify(SDRThread::SDR_THREAD_DEVICES_READY, "Devices Ready.");
|
|
|
|
terminated.store(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-13 22:18:29 -04:00
|
|
|
std::cout << "SDR thread done." << std::endl;
|
2015-10-03 21:35:11 -04:00
|
|
|
|
|
|
|
if (!terminated.load()) {
|
|
|
|
terminated.store(true);
|
|
|
|
wxGetApp().sdrThreadNotify(SDRThread::SDR_THREAD_TERMINATED, "Done.");
|
|
|
|
}
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
SDRDeviceInfo *SDRThread::getDevice() {
|
|
|
|
return deviceInfo.load();
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
|
|
|
|
2015-10-03 21:35:11 -04:00
|
|
|
void SDRThread::setDevice(SDRDeviceInfo *dev) {
|
|
|
|
deviceInfo.store(dev);
|
|
|
|
deviceConfig.store(wxGetApp().getConfig()->getDevice(dev->getDeviceId()));
|
2015-09-13 22:18:29 -04:00
|
|
|
}
|
2015-09-22 21:03:23 -04:00
|
|
|
|
|
|
|
int SDRThread::getOptimalElementCount(long long sampleRate, int fps) {
|
|
|
|
int elemCount = (int)floor((double)sampleRate/(double)fps);
|
|
|
|
elemCount = int(ceil((double)elemCount/512.0)*512.0);
|
2015-10-03 21:35:11 -04:00
|
|
|
std::cout << "Calculated optimal element count of " << elemCount << std::endl;
|
2015-09-22 21:03:23 -04:00
|
|
|
return elemCount;
|
|
|
|
}
|
2015-10-03 21:35:11 -04:00
|
|
|
|
|
|
|
void SDRThread::setFrequency(long long freq) {
|
|
|
|
if (freq < sampleRate.load() / 2) {
|
|
|
|
freq = sampleRate.load() / 2;
|
|
|
|
}
|
|
|
|
frequency.store(freq);
|
|
|
|
freq_changed.store(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
long long SDRThread::getFrequency() {
|
|
|
|
return frequency.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDRThread::setOffset(long long ofs) {
|
|
|
|
offset.store(ofs);
|
|
|
|
offset_changed.store(true);
|
|
|
|
std::cout << "Set offset: " << offset.load() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long SDRThread::getOffset() {
|
|
|
|
return offset.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDRThread::setSampleRate(int rate) {
|
|
|
|
sampleRate.store(rate);
|
|
|
|
rate_changed = true;
|
|
|
|
std::cout << "Set sample rate: " << sampleRate.load() << std::endl;
|
|
|
|
}
|
|
|
|
int SDRThread::getSampleRate() {
|
|
|
|
return sampleRate.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDRThread::setPPM(int ppm) {
|
|
|
|
this->ppm.store(ppm);
|
|
|
|
ppm_changed.store(true);
|
|
|
|
std::cout << "Set PPM: " << this->ppm.load() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SDRThread::getPPM() {
|
|
|
|
return ppm.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDRThread::setDirectSampling(int dsMode) {
|
|
|
|
direct_sampling_mode.store(dsMode);
|
|
|
|
direct_sampling_changed.store(true);
|
|
|
|
std::cout << "Set direct sampling mode: " << this->direct_sampling_mode.load() << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SDRThread::getDirectSampling() {
|
|
|
|
return direct_sampling_mode.load();
|
|
|
|
}
|