2016-02-16 20:22:05 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2015 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// This program is free software; you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// This program is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QDebug>
|
2016-02-20 17:02:34 -05:00
|
|
|
#include <QTimer>
|
2016-02-16 20:22:05 -05:00
|
|
|
#include <unistd.h>
|
2016-05-12 17:45:27 -04:00
|
|
|
|
|
|
|
#include "plugin/pluginapi.h"
|
2016-02-17 13:42:26 -05:00
|
|
|
#include "dsp/dspcommands.h"
|
|
|
|
#include "dsp/dspengine.h"
|
2016-02-16 20:22:05 -05:00
|
|
|
#include "sdrdaemonudphandler.h"
|
|
|
|
#include "sdrdaemoninput.h"
|
|
|
|
|
2016-05-12 17:45:27 -04:00
|
|
|
SDRdaemonUDPHandler::SDRdaemonUDPHandler(SampleFifo *sampleFifo, MessageQueue *outputMessageQueueToGUI, PluginAPI *pluginAPI) :
|
|
|
|
m_pluginAPI(pluginAPI),
|
2016-02-19 21:41:20 -05:00
|
|
|
m_sdrDaemonBuffer(m_rateDivider),
|
2016-02-16 20:22:05 -05:00
|
|
|
m_dataSocket(0),
|
|
|
|
m_dataAddress(QHostAddress::LocalHost),
|
2016-03-26 16:40:54 -04:00
|
|
|
m_remoteAddress(QHostAddress::LocalHost),
|
2016-02-16 20:22:05 -05:00
|
|
|
m_dataPort(9090),
|
|
|
|
m_dataConnected(false),
|
|
|
|
m_udpBuf(0),
|
|
|
|
m_udpReadBytes(0),
|
|
|
|
m_sampleFifo(sampleFifo),
|
|
|
|
m_samplerate(0),
|
|
|
|
m_centerFrequency(0),
|
|
|
|
m_tv_sec(0),
|
|
|
|
m_tv_usec(0),
|
|
|
|
m_outputMessageQueueToGUI(outputMessageQueueToGUI),
|
|
|
|
m_tickCount(0),
|
2016-02-20 17:02:34 -05:00
|
|
|
m_samplesCount(0),
|
2016-03-15 01:13:52 -04:00
|
|
|
m_timer(0),
|
2016-03-15 13:06:02 -04:00
|
|
|
m_throttlems(SDRDAEMON_THROTTLE_MS),
|
|
|
|
m_readLengthSamples(0),
|
|
|
|
m_readLength(0),
|
2016-03-16 13:46:16 -04:00
|
|
|
m_throttleToggle(false),
|
2016-03-19 01:14:06 -04:00
|
|
|
m_rateDivider(1000/SDRDAEMON_THROTTLE_MS),
|
|
|
|
m_autoCorrBuffer(false)
|
2016-02-16 20:22:05 -05:00
|
|
|
{
|
2016-02-19 21:41:20 -05:00
|
|
|
m_udpBuf = new char[SDRdaemonBuffer::m_udpPayloadSize];
|
2016-02-16 20:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
SDRdaemonUDPHandler::~SDRdaemonUDPHandler()
|
|
|
|
{
|
|
|
|
stop();
|
|
|
|
delete[] m_udpBuf;
|
2016-03-16 13:46:16 -04:00
|
|
|
#ifdef USE_INTERNAL_TIMER
|
|
|
|
if (m_timer) {
|
|
|
|
delete m_timer;
|
|
|
|
}
|
|
|
|
#endif
|
2016-02-16 20:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRdaemonUDPHandler::start()
|
|
|
|
{
|
2016-02-20 17:02:34 -05:00
|
|
|
qDebug("SDRdaemonUDPHandler::start");
|
|
|
|
|
2016-02-16 20:22:05 -05:00
|
|
|
if (!m_dataSocket)
|
|
|
|
{
|
|
|
|
m_dataSocket = new QUdpSocket(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_dataConnected)
|
|
|
|
{
|
|
|
|
if (m_dataSocket->bind(m_dataAddress, m_dataPort))
|
|
|
|
{
|
2016-02-20 17:02:34 -05:00
|
|
|
qDebug("SDRdaemonUDPHandler::start: bind data socket to %s:%d", m_dataAddress.toString().toStdString().c_str(), m_dataPort);
|
2016-02-16 20:22:05 -05:00
|
|
|
connect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()), Qt::QueuedConnection); // , Qt::QueuedConnection
|
|
|
|
m_dataConnected = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("SDRdaemonUDPHandler::start: cannot bind data port %d", m_dataPort);
|
|
|
|
m_dataConnected = false;
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 16:24:25 -05:00
|
|
|
|
|
|
|
// Need to notify the DSP engine to actually start
|
|
|
|
DSPSignalNotification *notif = new DSPSignalNotification(m_samplerate, m_centerFrequency * 1000); // Frequency in Hz for the DSP engine
|
2016-05-12 17:45:27 -04:00
|
|
|
m_pluginAPI->getDeviceInputMessageQueue()->push(notif);
|
2016-03-15 13:06:02 -04:00
|
|
|
m_elapsedTimer.start();
|
2016-02-16 20:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRdaemonUDPHandler::stop()
|
|
|
|
{
|
|
|
|
qDebug("SDRdaemonUDPHandler::stop");
|
|
|
|
|
|
|
|
if (m_dataConnected) {
|
|
|
|
disconnect(m_dataSocket, SIGNAL(readyRead()), this, SLOT(dataReadyRead()));
|
|
|
|
m_dataConnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_dataSocket)
|
|
|
|
{
|
|
|
|
delete m_dataSocket;
|
|
|
|
m_dataSocket = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-20 17:02:34 -05:00
|
|
|
void SDRdaemonUDPHandler::configureUDPLink(const QString& address, quint16 port)
|
|
|
|
{
|
|
|
|
qDebug("SDRdaemonUDPHandler::configureUDPLink: %s:%d", address.toStdString().c_str(), port);
|
|
|
|
bool addressOK = m_dataAddress.setAddress(address);
|
|
|
|
|
|
|
|
if (!addressOK)
|
|
|
|
{
|
|
|
|
qWarning("SDRdaemonUDPHandler::configureUDPLink: invalid address %s. Set to localhost.", address.toStdString().c_str());
|
|
|
|
m_dataAddress = QHostAddress::LocalHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
stop();
|
|
|
|
m_dataPort = port;
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2016-02-16 20:22:05 -05:00
|
|
|
void SDRdaemonUDPHandler::dataReadyRead()
|
|
|
|
{
|
|
|
|
while (m_dataSocket->hasPendingDatagrams())
|
|
|
|
{
|
|
|
|
qint64 pendingDataSize = m_dataSocket->pendingDatagramSize();
|
2016-03-26 16:40:54 -04:00
|
|
|
m_udpReadBytes = m_dataSocket->readDatagram(m_udpBuf, pendingDataSize, &m_remoteAddress, 0);
|
2016-02-18 16:26:47 -05:00
|
|
|
processData();
|
2016-02-16 20:22:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDRdaemonUDPHandler::processData()
|
|
|
|
{
|
|
|
|
if (m_udpReadBytes < 0)
|
|
|
|
{
|
|
|
|
qDebug() << "SDRdaemonThread::processData: read failed";
|
|
|
|
}
|
|
|
|
else if (m_udpReadBytes > 0)
|
|
|
|
{
|
|
|
|
m_sdrDaemonBuffer.updateBlockCounts(m_udpReadBytes);
|
|
|
|
|
|
|
|
if (m_sdrDaemonBuffer.readMeta(m_udpBuf, m_udpReadBytes))
|
|
|
|
{
|
|
|
|
const SDRdaemonBuffer::MetaData& metaData = m_sdrDaemonBuffer.getCurrentMeta();
|
|
|
|
bool change = false;
|
|
|
|
m_tv_sec = metaData.m_tv_sec;
|
|
|
|
m_tv_usec = metaData.m_tv_usec;
|
|
|
|
|
2016-02-22 06:10:13 -05:00
|
|
|
uint32_t sampleRate = m_sdrDaemonBuffer.getSampleRate();
|
|
|
|
|
|
|
|
if (m_samplerate != sampleRate)
|
2016-02-16 20:22:05 -05:00
|
|
|
{
|
2016-02-22 06:10:13 -05:00
|
|
|
setSamplerate(sampleRate);
|
|
|
|
m_samplerate = sampleRate;
|
2016-02-16 20:22:05 -05:00
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_centerFrequency != metaData.m_centerFrequency)
|
|
|
|
{
|
|
|
|
m_centerFrequency = metaData.m_centerFrequency;
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change)
|
|
|
|
{
|
2016-02-17 18:33:04 -05:00
|
|
|
DSPSignalNotification *notif = new DSPSignalNotification(m_samplerate, m_centerFrequency * 1000); // Frequency in Hz for the DSP engine
|
2016-05-12 17:45:27 -04:00
|
|
|
m_pluginAPI->getDeviceInputMessageQueue()->push(notif);
|
2016-02-16 20:22:05 -05:00
|
|
|
SDRdaemonInput::MsgReportSDRdaemonStreamData *report = SDRdaemonInput::MsgReportSDRdaemonStreamData::create(
|
2016-02-22 09:03:16 -05:00
|
|
|
m_sdrDaemonBuffer.getSampleRateStream(),
|
2016-02-17 13:42:26 -05:00
|
|
|
m_samplerate,
|
2016-02-20 04:10:11 -05:00
|
|
|
m_centerFrequency * 1000, // Frequency in Hz for the GUI
|
2016-02-17 13:42:26 -05:00
|
|
|
m_tv_sec,
|
|
|
|
m_tv_usec);
|
2016-02-16 20:22:05 -05:00
|
|
|
m_outputMessageQueueToGUI->push(report);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_sdrDaemonBuffer.isSync())
|
|
|
|
{
|
|
|
|
m_sdrDaemonBuffer.writeData(m_udpBuf, m_udpReadBytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SDRdaemonUDPHandler::setSamplerate(uint32_t samplerate)
|
|
|
|
{
|
|
|
|
qDebug() << "SDRdaemonUDPHandler::setSamplerate:"
|
|
|
|
<< " new:" << samplerate
|
|
|
|
<< " old:" << m_samplerate;
|
|
|
|
|
|
|
|
m_samplerate = samplerate;
|
|
|
|
}
|
|
|
|
|
2016-02-20 17:02:34 -05:00
|
|
|
void SDRdaemonUDPHandler::connectTimer(const QTimer* timer)
|
2016-02-16 20:22:05 -05:00
|
|
|
{
|
|
|
|
qDebug() << "SDRdaemonUDPHandler::connectTimer";
|
2016-03-16 13:46:16 -04:00
|
|
|
#ifdef USE_INTERNAL_TIMER
|
|
|
|
#warning "Uses internal timer"
|
|
|
|
m_timer = new QTimer();
|
2016-03-17 04:17:02 -04:00
|
|
|
m_timer->start(50);
|
2016-03-16 17:40:54 -04:00
|
|
|
m_throttlems = m_timer->interval();
|
|
|
|
connect(m_timer, SIGNAL(timeout()), this, SLOT(tick()));
|
2016-03-16 13:46:16 -04:00
|
|
|
#else
|
2016-03-16 17:40:54 -04:00
|
|
|
m_throttlems = timer->interval();
|
|
|
|
connect(timer, SIGNAL(timeout()), this, SLOT(tick()));
|
2016-03-16 13:46:16 -04:00
|
|
|
#endif
|
|
|
|
m_rateDivider = 1000 / m_throttlems;
|
2016-02-16 20:22:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void SDRdaemonUDPHandler::tick()
|
|
|
|
{
|
2016-03-15 13:06:02 -04:00
|
|
|
// auto throttling
|
|
|
|
int throttlems = m_elapsedTimer.restart();
|
|
|
|
|
|
|
|
if (throttlems != m_throttlems)
|
|
|
|
{
|
|
|
|
m_throttlems = throttlems;
|
|
|
|
m_readLengthSamples = (m_sdrDaemonBuffer.getSampleRate() * (m_throttlems+(m_throttleToggle ? 1 : 0))) / 1000;
|
2016-03-18 09:03:34 -04:00
|
|
|
m_readLengthSamples += m_sdrDaemonBuffer.getRWBalanceCorrection();
|
2016-03-15 13:06:02 -04:00
|
|
|
m_readLength = m_readLengthSamples * SDRdaemonBuffer::m_iqSampleSize;
|
|
|
|
m_throttleToggle = !m_throttleToggle;
|
|
|
|
}
|
2016-03-15 01:13:52 -04:00
|
|
|
|
2016-03-19 01:14:06 -04:00
|
|
|
if (m_autoCorrBuffer) {
|
|
|
|
m_readLengthSamples += m_sdrDaemonBuffer.getRWBalanceCorrection();
|
|
|
|
}
|
|
|
|
|
2016-02-16 20:22:05 -05:00
|
|
|
// read samples directly feeding the SampleFifo (no callback)
|
2016-03-15 13:06:02 -04:00
|
|
|
m_sampleFifo->write(reinterpret_cast<quint8*>(m_sdrDaemonBuffer.readData(m_readLength)), m_readLength);
|
|
|
|
m_samplesCount += m_readLengthSamples;
|
2016-02-16 20:22:05 -05:00
|
|
|
|
|
|
|
if (m_tickCount < m_rateDivider)
|
|
|
|
{
|
|
|
|
m_tickCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_tickCount = 0;
|
|
|
|
SDRdaemonInput::MsgReportSDRdaemonStreamTiming *report = SDRdaemonInput::MsgReportSDRdaemonStreamTiming::create(
|
|
|
|
m_tv_sec,
|
2016-02-23 08:57:40 -05:00
|
|
|
m_tv_usec,
|
2016-02-23 12:09:20 -05:00
|
|
|
m_sdrDaemonBuffer.isSyncLocked(),
|
2016-02-23 12:19:35 -05:00
|
|
|
m_sdrDaemonBuffer.getFrameSize(),
|
2016-03-20 09:40:40 -04:00
|
|
|
m_sdrDaemonBuffer.getBufferLengthInSecs(),
|
2016-02-23 12:29:08 -05:00
|
|
|
m_sdrDaemonBuffer.isLz4Compressed(),
|
2016-02-23 13:27:47 -05:00
|
|
|
m_sdrDaemonBuffer.getCompressionRatio(),
|
|
|
|
m_sdrDaemonBuffer.getLz4DataCRCOK(),
|
2016-03-17 10:41:48 -04:00
|
|
|
m_sdrDaemonBuffer.getLz4SuccessfulDecodes(),
|
|
|
|
m_sdrDaemonBuffer.getBufferGauge());
|
2016-02-16 20:22:05 -05:00
|
|
|
m_outputMessageQueueToGUI->push(report);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|