2014-10-27 20:05:40 -04:00
|
|
|
#include "SDRThread.h"
|
2014-10-30 22:51:33 -04:00
|
|
|
#include "CubicSDRDefs.h"
|
2014-10-28 21:39:59 -04:00
|
|
|
#include <vector>
|
2014-11-16 19:02:40 -05:00
|
|
|
#include "CubicSDR.h"
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
SDRThread::SDRThread(SDRThreadCommandQueue* pQueue) :
|
2015-01-12 00:40:43 -05:00
|
|
|
commandQueue(pQueue), iqDataOutQueue(NULL), terminated(false), offset(0), deviceId(-1) {
|
2014-10-27 20:05:40 -04:00
|
|
|
dev = NULL;
|
2015-07-08 01:07:39 -04:00
|
|
|
sampleRate.store(DEFAULT_SAMPLE_RATE);
|
2014-10-27 20:05:40 -04:00
|
|
|
}
|
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
SDRThread::~SDRThread() {
|
|
|
|
rtlsdr_close(dev);
|
2014-10-27 20:05:40 -04:00
|
|
|
}
|
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
int SDRThread::enumerate_rtl(std::vector<SDRDeviceInfo *> *devs) {
|
2014-11-09 20:55:21 -05:00
|
|
|
|
|
|
|
int first_available = -1;
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2014-10-27 21:22:29 -04:00
|
|
|
char manufact[256], product[256], serial[256];
|
|
|
|
|
2014-10-27 20:05:40 -04:00
|
|
|
unsigned int rtl_count = rtlsdr_get_device_count();
|
|
|
|
|
|
|
|
std::cout << "RTL Devices: " << rtl_count << std::endl;
|
|
|
|
|
|
|
|
for (int i = 0; i < rtl_count; i++) {
|
2015-01-12 00:40:43 -05:00
|
|
|
std::string deviceName(rtlsdr_get_device_name(i));
|
|
|
|
std::string deviceManufacturer;
|
|
|
|
std::string deviceProduct;
|
|
|
|
std::string deviceTuner;
|
|
|
|
std::string deviceSerial;
|
|
|
|
|
|
|
|
bool deviceAvailable = false;
|
|
|
|
std::cout << "Device #" << i << ": " << deviceName << std::endl;
|
2014-10-27 20:05:40 -04:00
|
|
|
if (rtlsdr_get_device_usb_strings(i, manufact, product, serial) == 0) {
|
|
|
|
std::cout << "\tManufacturer: " << manufact << ", Product Name: " << product << ", Serial: " << serial << std::endl;
|
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceSerial = serial;
|
|
|
|
deviceAvailable = true;
|
|
|
|
deviceProduct = product;
|
|
|
|
deviceManufacturer = manufact;
|
|
|
|
|
|
|
|
rtlsdr_dev_t *devTest;
|
|
|
|
rtlsdr_open(&devTest, i);
|
2014-10-27 20:05:40 -04:00
|
|
|
|
|
|
|
std::cout << "\t Tuner type: ";
|
2015-01-12 00:40:43 -05:00
|
|
|
switch (rtlsdr_get_tuner_type(devTest)) {
|
2014-10-27 20:05:40 -04:00
|
|
|
case RTLSDR_TUNER_UNKNOWN:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Unknown";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_E4000:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Elonics E4000";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_FC0012:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Fitipower FC0012";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_FC0013:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Fitipower FC0013";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_FC2580:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Fitipower FC2580";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_R820T:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Rafael Micro R820T";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_R828D:
|
2015-01-12 00:40:43 -05:00
|
|
|
deviceTuner = "Rafael Micro R828D";
|
2014-10-27 20:05:40 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
std::cout << deviceTuner << std::endl;
|
2014-10-27 20:05:40 -04:00
|
|
|
/*
|
|
|
|
int num_gains = rtlsdr_get_tuner_gains(dev, NULL);
|
|
|
|
|
|
|
|
int *gains = (int *)malloc(sizeof(int) * num_gains);
|
|
|
|
rtlsdr_get_tuner_gains(dev, gains);
|
|
|
|
|
|
|
|
std::cout << "\t Valid gains: ";
|
|
|
|
for (int g = 0; g < num_gains; g++) {
|
|
|
|
if (g > 0) {
|
|
|
|
std::cout << ", ";
|
|
|
|
}
|
|
|
|
std::cout << ((float)gains[g]/10.0f);
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
free(gains);
|
|
|
|
*/
|
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
rtlsdr_close(devTest);
|
2014-11-09 20:55:21 -05:00
|
|
|
if (first_available == -1) {
|
|
|
|
first_available = i;
|
|
|
|
}
|
2014-10-27 20:05:40 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
std::cout << "\tUnable to access device #" << i << " (in use?)" << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
if (devs != NULL) {
|
|
|
|
SDRDeviceInfo *devInfo = new SDRDeviceInfo();
|
|
|
|
devInfo->setName(deviceName);
|
|
|
|
devInfo->setAvailable(deviceAvailable);
|
|
|
|
devInfo->setProduct(deviceProduct);
|
|
|
|
devInfo->setSerial(deviceSerial);
|
|
|
|
devInfo->setManufacturer(deviceManufacturer);
|
|
|
|
devs->push_back(devInfo);
|
|
|
|
}
|
2014-10-27 20:05:40 -04:00
|
|
|
}
|
|
|
|
|
2014-11-09 20:55:21 -05:00
|
|
|
return first_available;
|
|
|
|
|
2014-10-27 20:05:40 -04:00
|
|
|
}
|
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
void SDRThread::threadMain() {
|
2014-12-18 20:11:25 -05:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_t tID = pthread_self(); // ID of this thread
|
2014-12-24 00:11:41 -05:00
|
|
|
int priority = sched_get_priority_max( SCHED_FIFO) - 1;
|
2015-01-12 00:40:43 -05:00
|
|
|
sched_param prio = { priority }; // scheduling priority of thread
|
2014-12-18 20:11:25 -05:00
|
|
|
pthread_setschedparam(tID, SCHED_FIFO, &prio);
|
|
|
|
#endif
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "SDR thread initializing.." << std::endl;
|
|
|
|
|
2015-01-03 17:07:39 -05:00
|
|
|
int devCount = rtlsdr_get_device_count();
|
2015-07-08 01:07:39 -04:00
|
|
|
|
2015-04-13 21:18:45 -04:00
|
|
|
std::vector<SDRDeviceInfo *> devs;
|
2015-01-12 00:40:43 -05:00
|
|
|
if (deviceId == -1) {
|
2015-04-13 21:18:45 -04:00
|
|
|
deviceId = enumerate_rtl(&devs);
|
2015-04-21 23:50:51 -04:00
|
|
|
} else {
|
|
|
|
enumerate_rtl(&devs);
|
2015-01-12 00:40:43 -05:00
|
|
|
}
|
2014-10-31 19:10:53 -04:00
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
if (deviceId == -1) {
|
2014-11-09 20:55:21 -05:00
|
|
|
std::cout << "No devices found.. SDR Thread exiting.." << std::endl;
|
2014-11-22 20:57:06 -05:00
|
|
|
return;
|
2014-11-09 20:55:21 -05:00
|
|
|
} else {
|
2015-01-12 00:40:43 -05:00
|
|
|
std::cout << "Using device #" << deviceId << std::endl;
|
2014-10-31 19:10:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-08 01:07:39 -04:00
|
|
|
DeviceConfig *devConfig = wxGetApp().getConfig()->getDevice(devs[deviceId]->getDeviceId());
|
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
signed char buf[BUF_SIZE];
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2015-01-04 17:11:20 -05:00
|
|
|
long long frequency = DEFAULT_FREQ;
|
2015-07-08 01:07:39 -04:00
|
|
|
int ppm = devConfig->getPPM();
|
|
|
|
int direct_sampling_mode = devConfig->getDirectSampling();;
|
2015-06-11 17:18:36 -04:00
|
|
|
int buf_size = BUF_SIZE;
|
2015-07-08 01:07:39 -04:00
|
|
|
offset.store(devConfig->getOffset());
|
|
|
|
wxGetApp().setSwapIQ(devConfig->getIQSwap());
|
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
rtlsdr_open(&dev, deviceId);
|
2015-07-08 01:07:39 -04:00
|
|
|
rtlsdr_set_sample_rate(dev, sampleRate.load());
|
|
|
|
rtlsdr_set_center_freq(dev, frequency - offset.load());
|
2015-04-13 21:18:45 -04:00
|
|
|
rtlsdr_set_freq_correction(dev, ppm);
|
2014-10-28 22:59:17 -04:00
|
|
|
rtlsdr_set_agc_mode(dev, 1);
|
2014-11-29 13:58:20 -05:00
|
|
|
rtlsdr_set_offset_tuning(dev, 0);
|
2014-10-27 20:05:40 -04:00
|
|
|
rtlsdr_reset_buffer(dev);
|
|
|
|
|
2015-01-11 17:08:16 -05:00
|
|
|
// sampleRate = rtlsdr_get_sample_rate(dev);
|
2014-10-30 22:51:33 -04:00
|
|
|
|
2015-07-08 01:07:39 -04:00
|
|
|
std::cout << "Sample Rate is: " << sampleRate.load() << std::endl;
|
2014-10-30 22:51:33 -04:00
|
|
|
|
2014-10-27 20:05:40 -04:00
|
|
|
int n_read;
|
2014-10-30 22:51:33 -04:00
|
|
|
double seconds = 0.0;
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "SDR thread started.." << std::endl;
|
2014-12-24 00:11:41 -05:00
|
|
|
|
|
|
|
std::deque<SDRThreadIQData *> buffers;
|
|
|
|
std::deque<SDRThreadIQData *>::iterator buffers_i;
|
|
|
|
|
2014-11-23 19:39:27 -05:00
|
|
|
while (!terminated) {
|
2015-01-03 17:07:39 -05:00
|
|
|
SDRThreadCommandQueue *cmdQueue = commandQueue.load();
|
2014-11-29 13:58:20 -05:00
|
|
|
|
|
|
|
if (!cmdQueue->empty()) {
|
2014-11-04 19:52:11 -05:00
|
|
|
bool freq_changed = false;
|
2015-01-06 00:57:57 -05:00
|
|
|
bool offset_changed = false;
|
2015-01-11 17:08:16 -05:00
|
|
|
bool rate_changed = false;
|
2015-01-12 00:40:43 -05:00
|
|
|
bool device_changed = false;
|
2015-04-13 21:18:45 -04:00
|
|
|
bool ppm_changed = false;
|
2015-05-30 23:14:16 -04:00
|
|
|
bool direct_sampling_changed = false;
|
2015-04-13 21:18:45 -04:00
|
|
|
long long new_freq = frequency;
|
2015-07-08 01:07:39 -04:00
|
|
|
long long new_offset = offset.load();
|
|
|
|
long long new_rate = sampleRate.load();
|
2015-04-13 21:18:45 -04:00
|
|
|
int new_device = deviceId;
|
|
|
|
int new_ppm = ppm;
|
2014-11-04 19:52:11 -05:00
|
|
|
|
2014-11-29 13:58:20 -05:00
|
|
|
while (!cmdQueue->empty()) {
|
2014-11-22 20:57:06 -05:00
|
|
|
SDRThreadCommand command;
|
2014-11-29 13:58:20 -05:00
|
|
|
cmdQueue->pop(command);
|
2014-11-22 20:57:06 -05:00
|
|
|
|
|
|
|
switch (command.cmd) {
|
|
|
|
case SDRThreadCommand::SDR_THREAD_CMD_TUNE:
|
2014-11-04 19:52:11 -05:00
|
|
|
freq_changed = true;
|
2015-01-04 17:11:20 -05:00
|
|
|
new_freq = command.llong_value;
|
2015-07-08 01:07:39 -04:00
|
|
|
if (new_freq < sampleRate.load() / 2) {
|
|
|
|
new_freq = sampleRate.load() / 2;
|
2015-01-04 13:20:31 -05:00
|
|
|
}
|
2015-02-10 18:28:35 -05:00
|
|
|
// std::cout << "Set frequency: " << new_freq << std::endl;
|
2014-11-04 19:52:11 -05:00
|
|
|
break;
|
2015-01-06 00:57:57 -05:00
|
|
|
case SDRThreadCommand::SDR_THREAD_CMD_SET_OFFSET:
|
|
|
|
offset_changed = true;
|
|
|
|
new_offset = command.llong_value;
|
|
|
|
std::cout << "Set offset: " << new_offset << std::endl;
|
|
|
|
break;
|
2015-01-11 17:08:16 -05:00
|
|
|
case SDRThreadCommand::SDR_THREAD_CMD_SET_SAMPLERATE:
|
|
|
|
rate_changed = true;
|
|
|
|
new_rate = command.llong_value;
|
2015-06-11 17:18:36 -04:00
|
|
|
if (new_rate <= 250000) {
|
|
|
|
buf_size = BUF_SIZE/4;
|
|
|
|
} else if (new_rate < 1500000) {
|
|
|
|
buf_size = BUF_SIZE/2;
|
|
|
|
} else {
|
|
|
|
buf_size = BUF_SIZE;
|
|
|
|
}
|
2015-01-11 17:08:16 -05:00
|
|
|
std::cout << "Set sample rate: " << new_rate << std::endl;
|
|
|
|
break;
|
2015-01-12 00:40:43 -05:00
|
|
|
case SDRThreadCommand::SDR_THREAD_CMD_SET_DEVICE:
|
|
|
|
device_changed = true;
|
|
|
|
new_device = (int) command.llong_value;
|
|
|
|
std::cout << "Set device: " << new_device << std::endl;
|
|
|
|
break;
|
2015-04-13 21:18:45 -04:00
|
|
|
case SDRThreadCommand::SDR_THREAD_CMD_SET_PPM:
|
|
|
|
ppm_changed = true;
|
|
|
|
new_ppm = (int) command.llong_value;
|
2015-04-23 21:28:49 -04:00
|
|
|
//std::cout << "Set PPM: " << new_ppm << std::endl;
|
2015-04-13 21:18:45 -04:00
|
|
|
break;
|
2015-05-30 23:14:16 -04:00
|
|
|
case SDRThreadCommand::SDR_THREAD_CMD_SET_DIRECT_SAMPLING:
|
|
|
|
direct_sampling_mode = (int)command.llong_value;
|
|
|
|
direct_sampling_changed = true;
|
|
|
|
break;
|
2014-12-24 00:11:41 -05:00
|
|
|
default:
|
2014-12-23 01:12:14 -05:00
|
|
|
break;
|
2014-11-04 18:39:08 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 19:52:11 -05:00
|
|
|
|
2015-01-12 00:40:43 -05:00
|
|
|
if (device_changed) {
|
|
|
|
rtlsdr_close(dev);
|
|
|
|
rtlsdr_open(&dev, new_device);
|
2015-07-08 01:07:39 -04:00
|
|
|
rtlsdr_set_sample_rate(dev, sampleRate.load());
|
|
|
|
rtlsdr_set_center_freq(dev, frequency - offset.load());
|
2015-04-13 21:18:45 -04:00
|
|
|
rtlsdr_set_freq_correction(dev, ppm);
|
2015-01-12 00:40:43 -05:00
|
|
|
rtlsdr_set_agc_mode(dev, 1);
|
|
|
|
rtlsdr_set_offset_tuning(dev, 0);
|
2015-05-30 23:14:16 -04:00
|
|
|
rtlsdr_set_direct_sampling(dev, direct_sampling_mode);
|
2015-01-12 00:40:43 -05:00
|
|
|
rtlsdr_reset_buffer(dev);
|
|
|
|
}
|
2015-06-11 19:59:08 -04:00
|
|
|
if (offset_changed) {
|
|
|
|
if (!freq_changed) {
|
|
|
|
new_freq = frequency;
|
|
|
|
freq_changed = true;
|
|
|
|
}
|
2015-07-08 01:07:39 -04:00
|
|
|
offset.store(new_offset);
|
2015-01-06 00:57:57 -05:00
|
|
|
}
|
2015-01-11 17:08:16 -05:00
|
|
|
if (rate_changed) {
|
|
|
|
rtlsdr_set_sample_rate(dev, new_rate);
|
2015-06-11 17:18:36 -04:00
|
|
|
rtlsdr_reset_buffer(dev);
|
2015-07-08 01:07:39 -04:00
|
|
|
sampleRate.store(rtlsdr_get_sample_rate(dev));
|
2015-01-14 22:14:57 -05:00
|
|
|
}
|
|
|
|
if (freq_changed) {
|
2014-11-17 18:14:37 -05:00
|
|
|
frequency = new_freq;
|
2015-07-08 01:07:39 -04:00
|
|
|
rtlsdr_set_center_freq(dev, frequency - offset.load());
|
2014-11-04 19:52:11 -05:00
|
|
|
}
|
2015-04-13 21:18:45 -04:00
|
|
|
if (ppm_changed) {
|
|
|
|
ppm = new_ppm;
|
|
|
|
rtlsdr_set_freq_correction(dev, ppm);
|
|
|
|
}
|
2015-05-30 23:14:16 -04:00
|
|
|
if (direct_sampling_changed) {
|
|
|
|
rtlsdr_set_direct_sampling(dev, direct_sampling_mode);
|
|
|
|
}
|
2014-11-04 18:39:08 -05:00
|
|
|
}
|
2014-11-04 17:25:04 -05:00
|
|
|
|
2015-06-11 17:18:36 -04:00
|
|
|
rtlsdr_read_sync(dev, buf, buf_size, &n_read);
|
2014-10-31 01:37:01 -04:00
|
|
|
|
2014-12-24 00:11:41 -05:00
|
|
|
SDRThreadIQData *dataOut = NULL;
|
|
|
|
|
|
|
|
for (buffers_i = buffers.begin(); buffers_i != buffers.end(); buffers_i++) {
|
|
|
|
if ((*buffers_i)->getRefCount() <= 0) {
|
|
|
|
dataOut = (*buffers_i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataOut == NULL) {
|
|
|
|
dataOut = new SDRThreadIQData;
|
|
|
|
buffers.push_back(dataOut);
|
|
|
|
}
|
|
|
|
|
2014-12-24 01:28:33 -05:00
|
|
|
// std::lock_guard < std::mutex > lock(dataOut->m_mutex);
|
2014-12-24 00:11:41 -05:00
|
|
|
dataOut->setRefCount(1);
|
2014-12-23 01:59:03 -05:00
|
|
|
dataOut->frequency = frequency;
|
2015-07-08 01:07:39 -04:00
|
|
|
dataOut->sampleRate = sampleRate.load();
|
2014-10-28 22:59:17 -04:00
|
|
|
|
2014-12-24 03:03:34 -05:00
|
|
|
if (dataOut->data.capacity() < n_read) {
|
|
|
|
dataOut->data.reserve(n_read);
|
|
|
|
}
|
|
|
|
|
2014-12-24 00:11:41 -05:00
|
|
|
if (dataOut->data.size() != n_read) {
|
|
|
|
dataOut->data.resize(n_read);
|
|
|
|
}
|
|
|
|
|
2015-02-05 19:45:01 -05:00
|
|
|
memcpy(&dataOut->data[0], buf, n_read);
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2015-07-08 01:07:39 -04:00
|
|
|
double time_slice = (double) n_read / (double) sampleRate.load();
|
2014-11-22 20:57:06 -05:00
|
|
|
seconds += time_slice;
|
|
|
|
|
2015-01-24 22:01:47 -05:00
|
|
|
if (iqDataOutQueue.load() != NULL) {
|
2014-11-29 13:58:20 -05:00
|
|
|
iqDataOutQueue.load()->push(dataOut);
|
2014-10-27 20:05:40 -04:00
|
|
|
}
|
2014-11-22 20:57:06 -05:00
|
|
|
}
|
2014-12-24 00:11:41 -05:00
|
|
|
|
|
|
|
while (!buffers.empty()) {
|
|
|
|
SDRThreadIQData *iqDataDel = buffers.front();
|
|
|
|
buffers.pop_front();
|
2014-12-24 01:28:33 -05:00
|
|
|
// std::lock_guard < std::mutex > lock(iqDataDel->m_mutex);
|
|
|
|
// delete iqDataDel;
|
2014-12-24 00:11:41 -05:00
|
|
|
}
|
|
|
|
|
2014-11-30 17:11:29 -05:00
|
|
|
std::cout << "SDR thread done." << std::endl;
|
2014-10-27 20:05:40 -04:00
|
|
|
}
|
|
|
|
|
2014-11-23 19:39:27 -05:00
|
|
|
void SDRThread::terminate() {
|
|
|
|
terminated = true;
|
|
|
|
}
|