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) :
|
2014-11-29 13:58:20 -05:00
|
|
|
m_pQueue(pQueue), iqDataOutQueue(NULL), terminated(false) {
|
2014-10-27 20:05:40 -04:00
|
|
|
dev = NULL;
|
2014-11-04 18:39:08 -05:00
|
|
|
sample_rate = SRATE;
|
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
|
|
|
}
|
|
|
|
|
2014-11-09 20:55:21 -05:00
|
|
|
int SDRThread::enumerate_rtl() {
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
std::cout << "Device #" << i << ": " << rtlsdr_get_device_name(i) << std::endl;
|
|
|
|
if (rtlsdr_get_device_usb_strings(i, manufact, product, serial) == 0) {
|
|
|
|
std::cout << "\tManufacturer: " << manufact << ", Product Name: " << product << ", Serial: " << serial << std::endl;
|
|
|
|
|
|
|
|
rtlsdr_open(&dev, i);
|
|
|
|
|
|
|
|
std::cout << "\t Tuner type: ";
|
|
|
|
switch (rtlsdr_get_tuner_type(dev)) {
|
|
|
|
case RTLSDR_TUNER_UNKNOWN:
|
|
|
|
std::cout << "Unknown";
|
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_E4000:
|
|
|
|
std::cout << "Elonics E4000";
|
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_FC0012:
|
|
|
|
std::cout << "Fitipower FC0012";
|
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_FC0013:
|
|
|
|
std::cout << "Fitipower FC0013";
|
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_FC2580:
|
|
|
|
std::cout << "Fitipower FC2580";
|
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_R820T:
|
|
|
|
std::cout << "Rafael Micro R820T";
|
|
|
|
break;
|
|
|
|
case RTLSDR_TUNER_R828D:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
/*
|
|
|
|
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);
|
|
|
|
*/
|
|
|
|
|
|
|
|
rtlsdr_close(dev);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
int priority = sched_get_priority_max( SCHED_FIFO )-1;
|
|
|
|
sched_param prio = { priority }; // scheduling priority of thread
|
|
|
|
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;
|
|
|
|
|
2014-10-31 19:10:53 -04:00
|
|
|
int dev_count = rtlsdr_get_device_count();
|
2014-11-09 20:55:21 -05:00
|
|
|
int first_available = enumerate_rtl();
|
2014-10-31 19:10:53 -04:00
|
|
|
|
2014-11-09 20:55:21 -05:00
|
|
|
if (first_available == -1) {
|
|
|
|
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 {
|
|
|
|
std::cout << "Using first available RTL-SDR device #" << first_available << std::endl;
|
2014-10-31 19:10:53 -04:00
|
|
|
}
|
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
signed char buf[BUF_SIZE];
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2014-11-17 18:14:37 -05:00
|
|
|
unsigned int frequency = DEFAULT_FREQ;
|
|
|
|
unsigned int bandwidth = SRATE;
|
|
|
|
|
2014-11-09 20:55:21 -05:00
|
|
|
rtlsdr_open(&dev, first_available);
|
2014-11-17 18:14:37 -05:00
|
|
|
rtlsdr_set_sample_rate(dev, bandwidth);
|
|
|
|
rtlsdr_set_center_freq(dev, frequency);
|
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);
|
|
|
|
|
2014-10-30 22:51:33 -04:00
|
|
|
sample_rate = rtlsdr_get_sample_rate(dev);
|
|
|
|
|
|
|
|
std::cout << "Sample Rate is: " << sample_rate << std::endl;
|
|
|
|
|
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-11-23 19:39:27 -05:00
|
|
|
while (!terminated) {
|
2014-11-29 13:58:20 -05:00
|
|
|
SDRThreadCommandQueue *cmdQueue = m_pQueue.load();
|
|
|
|
|
|
|
|
if (!cmdQueue->empty()) {
|
2014-11-04 19:52:11 -05:00
|
|
|
bool freq_changed = false;
|
|
|
|
float new_freq;
|
|
|
|
|
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:
|
|
|
|
std::cout << "Set frequency: " << command.int_value << std::endl;
|
2014-11-04 19:52:11 -05:00
|
|
|
freq_changed = true;
|
2014-11-22 20:57:06 -05:00
|
|
|
new_freq = command.int_value;
|
2014-11-04 19:52:11 -05:00
|
|
|
break;
|
2014-11-04 18:39:08 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 19:52:11 -05:00
|
|
|
|
|
|
|
if (freq_changed) {
|
2014-11-17 18:14:37 -05:00
|
|
|
frequency = new_freq;
|
|
|
|
rtlsdr_set_center_freq(dev, frequency);
|
2014-11-04 19:52:11 -05:00
|
|
|
}
|
2014-11-04 18:39:08 -05:00
|
|
|
}
|
2014-11-04 17:25:04 -05:00
|
|
|
|
2014-10-27 20:05:40 -04:00
|
|
|
rtlsdr_read_sync(dev, buf, BUF_SIZE, &n_read);
|
2014-10-31 01:37:01 -04:00
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
std::vector<signed char> new_buffer;
|
2014-10-28 22:59:17 -04:00
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
for (int i = 0; i < n_read; i++) {
|
|
|
|
new_buffer.push_back(buf[i] - 127);
|
|
|
|
}
|
2014-10-27 20:05:40 -04:00
|
|
|
|
2014-11-22 20:57:06 -05:00
|
|
|
double time_slice = (double) n_read / (double) sample_rate;
|
|
|
|
seconds += time_slice;
|
|
|
|
|
|
|
|
SDRThreadIQData dataOut;
|
|
|
|
dataOut.frequency = frequency;
|
|
|
|
dataOut.bandwidth = bandwidth;
|
|
|
|
dataOut.data = new_buffer;
|
|
|
|
|
|
|
|
if (iqDataOutQueue != 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-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;
|
|
|
|
}
|