2022-07-19 05:10:20 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
2024-02-27 09:03:34 -05:00
|
|
|
// Copyright (C) 2022-2024 Jon Beniston, M7RCE <jon@beniston.com> //
|
2023-11-18 04:07:41 -05:00
|
|
|
// Copyright (C) 2022 Edouard Griffiths, F4EXB <f4exb06@gmail.com> //
|
|
|
|
// Copyright (C) 2022 Jiří Pinkava <jiri.pinkava@rossum.ai> //
|
2022-07-19 05:10:20 -04:00
|
|
|
// //
|
|
|
|
// 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 //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef PLUGINS_SAMPLESOURCE_REMOTETCPINPUT_REMOTETCPINPUTUDPHANDLER_H_
|
|
|
|
#define PLUGINS_SAMPLESOURCE_REMOTETCPINPUT_REMOTETCPINPUTUDPHANDLER_H_
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QTcpSocket>
|
2024-10-08 12:20:18 -04:00
|
|
|
#include <QWebSocket>
|
2022-07-19 05:10:20 -04:00
|
|
|
#include <QHostAddress>
|
2022-09-15 15:59:42 -04:00
|
|
|
#include <QRecursiveMutex>
|
2022-07-19 05:10:20 -04:00
|
|
|
#include <QDateTime>
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
#include <FLAC/stream_decoder.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
|
2022-07-19 05:10:20 -04:00
|
|
|
#include "util/messagequeue.h"
|
2024-09-22 05:44:30 -04:00
|
|
|
#include "util/movingaverage.h"
|
2024-10-08 12:20:18 -04:00
|
|
|
#include "util/socket.h"
|
2024-09-22 05:44:30 -04:00
|
|
|
#include "dsp/replaybuffer.h"
|
2022-07-19 05:10:20 -04:00
|
|
|
#include "remotetcpinputsettings.h"
|
|
|
|
#include "../../channelrx/remotetcpsink/remotetcpprotocol.h"
|
2024-02-27 09:03:34 -05:00
|
|
|
#include "spyserver.h"
|
2022-07-19 05:10:20 -04:00
|
|
|
|
|
|
|
class SampleSinkFifo;
|
|
|
|
class MessageQueue;
|
|
|
|
class DeviceAPI;
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
class FIFO {
|
|
|
|
public:
|
|
|
|
|
2024-10-08 17:19:29 -04:00
|
|
|
explicit FIFO(qsizetype elements = 10);
|
2024-09-22 05:44:30 -04:00
|
|
|
|
|
|
|
qsizetype write(quint8 *data, qsizetype elements);
|
|
|
|
qsizetype read(quint8 *data, qsizetype elements);
|
|
|
|
qsizetype readPtr(quint8 **data, qsizetype elements);
|
|
|
|
void read(qsizetype elements);
|
|
|
|
void resize(qsizetype elements); // Sets capacity
|
|
|
|
void clear();
|
|
|
|
qsizetype fill() const { return m_fill; } // Number of elements in use
|
|
|
|
bool empty() const { return m_fill == 0; }
|
|
|
|
bool full() const { return m_fill == m_data.size(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
qsizetype m_readPtr;
|
|
|
|
qsizetype m_writePtr;
|
|
|
|
qsizetype m_fill;
|
|
|
|
|
|
|
|
QByteArray m_data;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2022-07-19 05:10:20 -04:00
|
|
|
class RemoteTCPInputTCPHandler : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
class MsgConfigureTcpHandler : public Message {
|
|
|
|
MESSAGE_CLASS_DECLARATION
|
|
|
|
|
|
|
|
public:
|
|
|
|
const RemoteTCPInputSettings& getSettings() const { return m_settings; }
|
2022-10-25 18:40:33 -04:00
|
|
|
const QList<QString>& getSettingsKeys() const { return m_settingsKeys; }
|
2022-07-19 05:10:20 -04:00
|
|
|
bool getForce() const { return m_force; }
|
|
|
|
|
2022-10-25 18:40:33 -04:00
|
|
|
static MsgConfigureTcpHandler* create(const RemoteTCPInputSettings& settings, const QList<QString>& settingsKeys, bool force)
|
2022-07-19 05:10:20 -04:00
|
|
|
{
|
2022-10-25 18:40:33 -04:00
|
|
|
return new MsgConfigureTcpHandler(settings, settingsKeys, force);
|
2022-07-19 05:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RemoteTCPInputSettings m_settings;
|
2022-10-25 18:40:33 -04:00
|
|
|
QList<QString> m_settingsKeys;
|
2022-07-19 05:10:20 -04:00
|
|
|
bool m_force;
|
|
|
|
|
2022-10-25 18:40:33 -04:00
|
|
|
MsgConfigureTcpHandler(const RemoteTCPInputSettings& settings, const QList<QString>& settingsKeys, bool force) :
|
2022-07-19 05:10:20 -04:00
|
|
|
Message(),
|
|
|
|
m_settings(settings),
|
2022-10-25 18:40:33 -04:00
|
|
|
m_settingsKeys(settingsKeys),
|
2022-07-19 05:10:20 -04:00
|
|
|
m_force(force)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
class MsgReportRemoteDevice : public Message {
|
|
|
|
MESSAGE_CLASS_DECLARATION
|
|
|
|
|
|
|
|
public:
|
|
|
|
RemoteTCPProtocol::Device getDevice() const { return m_device; }
|
|
|
|
QString getProtocol() const { return m_protocol; }
|
2024-09-22 05:44:30 -04:00
|
|
|
bool getIQOnly() const { return m_iqOnly; }
|
|
|
|
bool getRemoteControl() const { return m_remoteControl; }
|
2024-02-27 09:03:34 -05:00
|
|
|
int getMaxGain() const { return m_maxGain; }
|
2022-07-19 05:10:20 -04:00
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
static MsgReportRemoteDevice* create(RemoteTCPProtocol::Device device, const QString& protocol, bool iqOnly, bool remoteControl, int maxGain = 0)
|
2022-07-19 05:10:20 -04:00
|
|
|
{
|
2024-09-22 05:44:30 -04:00
|
|
|
return new MsgReportRemoteDevice(device, protocol, iqOnly, remoteControl, maxGain);
|
2022-07-19 05:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
RemoteTCPProtocol::Device m_device;
|
|
|
|
QString m_protocol;
|
2024-09-22 05:44:30 -04:00
|
|
|
bool m_iqOnly;
|
|
|
|
bool m_remoteControl;
|
2024-02-27 09:03:34 -05:00
|
|
|
int m_maxGain;
|
2022-07-19 05:10:20 -04:00
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
MsgReportRemoteDevice(RemoteTCPProtocol::Device device, const QString& protocol, bool iqOnly, bool remoteControl, int maxGain) :
|
2022-07-19 05:10:20 -04:00
|
|
|
Message(),
|
|
|
|
m_device(device),
|
2024-02-27 09:03:34 -05:00
|
|
|
m_protocol(protocol),
|
2024-09-22 05:44:30 -04:00
|
|
|
m_iqOnly(iqOnly),
|
|
|
|
m_remoteControl(remoteControl),
|
2024-02-27 09:03:34 -05:00
|
|
|
m_maxGain(maxGain)
|
2022-07-19 05:10:20 -04:00
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
class MsgReportConnection : public Message {
|
|
|
|
MESSAGE_CLASS_DECLARATION
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool getConnected() const { return m_connected; }
|
|
|
|
|
|
|
|
static MsgReportConnection* create(bool connected)
|
|
|
|
{
|
|
|
|
return new MsgReportConnection(connected);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
bool m_connected;
|
|
|
|
|
|
|
|
MsgReportConnection(bool connected) :
|
|
|
|
Message(),
|
|
|
|
m_connected(connected)
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
RemoteTCPInputTCPHandler(SampleSinkFifo* sampleFifo, DeviceAPI *deviceAPI, ReplayBuffer<FixReal> *replayBuffer);
|
2022-07-19 05:10:20 -04:00
|
|
|
~RemoteTCPInputTCPHandler();
|
|
|
|
MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; }
|
|
|
|
void setMessageQueueToInput(MessageQueue *queue) { m_messageQueueToInput = queue; }
|
|
|
|
void setMessageQueueToGUI(MessageQueue *queue) { m_messageQueueToGUI = queue; }
|
|
|
|
void reset();
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
int getBufferGauge() const { return 0; }
|
2024-09-22 05:44:30 -04:00
|
|
|
void processCommands();
|
|
|
|
|
|
|
|
FLAC__StreamDecoderReadStatus flacRead(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
|
|
|
|
FLAC__StreamDecoderWriteStatus flacWrite(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *const buffer[]);
|
|
|
|
void flacError(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
|
|
|
|
|
|
|
|
void getMagSqLevels(double& avg, double& peak, int& nbSamples)
|
|
|
|
{
|
|
|
|
if (m_magsqCount > 0)
|
|
|
|
{
|
|
|
|
m_magsq = m_magsqSum / m_magsqCount;
|
|
|
|
m_magSqLevelStore.m_magsq = m_magsq;
|
|
|
|
m_magSqLevelStore.m_magsqPeak = m_magsqPeak;
|
|
|
|
}
|
|
|
|
|
|
|
|
avg = m_magSqLevelStore.m_magsq;
|
|
|
|
peak = m_magSqLevelStore.m_magsqPeak;
|
|
|
|
nbSamples = m_magsqCount == 0 ? 1 : m_magsqCount;
|
|
|
|
|
|
|
|
m_magsqSum = 0.0f;
|
|
|
|
m_magsqPeak = 0.0f;
|
|
|
|
m_magsqCount = 0;
|
|
|
|
}
|
2022-07-19 05:10:20 -04:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
void dataReadyRead();
|
|
|
|
void connected();
|
|
|
|
void disconnected();
|
|
|
|
void errorOccurred(QAbstractSocket::SocketError socketError);
|
2024-10-08 12:20:18 -04:00
|
|
|
void sslErrors(const QList<QSslError> &errors);
|
2022-07-19 05:10:20 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
struct MagSqLevelsStore
|
|
|
|
{
|
|
|
|
MagSqLevelsStore() :
|
|
|
|
m_magsq(1e-12),
|
|
|
|
m_magsqPeak(1e-12)
|
|
|
|
{}
|
|
|
|
double m_magsq;
|
|
|
|
double m_magsqPeak;
|
|
|
|
};
|
|
|
|
|
2022-07-19 05:10:20 -04:00
|
|
|
DeviceAPI *m_deviceAPI;
|
|
|
|
bool m_running;
|
2024-10-08 12:20:18 -04:00
|
|
|
Socket *m_dataSocket;
|
|
|
|
QTcpSocket *m_tcpSocket;
|
|
|
|
QWebSocket *m_webSocket;
|
2022-07-19 05:10:20 -04:00
|
|
|
char *m_tcpBuf;
|
|
|
|
SampleSinkFifo *m_sampleFifo;
|
2024-09-22 05:44:30 -04:00
|
|
|
ReplayBuffer<FixReal> *m_replayBuffer;
|
2022-07-19 05:10:20 -04:00
|
|
|
MessageQueue m_inputMessageQueue; //!< Queue for asynchronous inbound communication
|
|
|
|
MessageQueue *m_messageQueueToInput;
|
|
|
|
MessageQueue *m_messageQueueToGUI;
|
|
|
|
bool m_readMetaData;
|
|
|
|
bool m_fillBuffer;
|
|
|
|
QTimer m_timer;
|
|
|
|
QTimer m_reconnectTimer;
|
|
|
|
QDateTime m_prevDateTime;
|
2022-11-03 11:57:00 -04:00
|
|
|
bool m_sdra;
|
2024-02-27 09:03:34 -05:00
|
|
|
bool m_spyServer;
|
|
|
|
RemoteTCPProtocol::Device m_device;
|
|
|
|
SpyServerProtocol::Header m_spyServerHeader;
|
|
|
|
enum {HEADER, DATA} m_state; //!< FSM for reading Spy Server packets
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
RemoteTCPProtocol::Command m_command;
|
|
|
|
quint32 m_commandLength;
|
2022-07-19 05:10:20 -04:00
|
|
|
|
|
|
|
int32_t *m_converterBuffer;
|
|
|
|
uint32_t m_converterBufferNbSamples;
|
|
|
|
|
2022-09-15 15:59:42 -04:00
|
|
|
QRecursiveMutex m_mutex;
|
2022-07-19 05:10:20 -04:00
|
|
|
RemoteTCPInputSettings m_settings;
|
|
|
|
|
2024-09-22 05:44:30 -04:00
|
|
|
bool m_remoteControl;
|
|
|
|
bool m_iqOnly;
|
|
|
|
QByteArray m_compressedData;
|
|
|
|
|
|
|
|
// FLAC decompression
|
|
|
|
qint64 m_compressedFrames;
|
|
|
|
qint64 m_uncompressedFrames;
|
|
|
|
FIFO m_uncompressedData;
|
|
|
|
FLAC__StreamDecoder *m_decoder;
|
|
|
|
int m_remainingSamples;
|
|
|
|
|
|
|
|
// Zlib decompression
|
|
|
|
z_stream m_zStream;
|
|
|
|
QByteArray m_zOutBuf;
|
|
|
|
static const int m_zBufSize = 32768+128; //
|
|
|
|
|
|
|
|
bool m_blacklisted;
|
|
|
|
|
|
|
|
double m_magsq;
|
|
|
|
double m_magsqSum;
|
|
|
|
double m_magsqPeak;
|
|
|
|
int m_magsqCount;
|
|
|
|
MagSqLevelsStore m_magSqLevelStore;
|
|
|
|
MovingAverageUtil<Real, double, 16> m_movingAverage;
|
|
|
|
|
2022-07-19 05:10:20 -04:00
|
|
|
bool handleMessage(const Message& message);
|
2024-10-08 12:20:18 -04:00
|
|
|
void connectToHost(const QString& address, quint16 port, const QString& protocol);
|
2024-09-22 05:44:30 -04:00
|
|
|
//void disconnectFromHost();
|
2022-07-19 05:10:20 -04:00
|
|
|
void cleanup();
|
|
|
|
void clearBuffer();
|
2024-09-22 05:44:30 -04:00
|
|
|
void sendCommand(RemoteTCPProtocol::Command cmd, quint32 value);
|
|
|
|
void sendCommandFloat(RemoteTCPProtocol::Command cmd, float value);
|
2022-07-19 05:10:20 -04:00
|
|
|
void setSampleRate(int sampleRate);
|
|
|
|
void setCenterFrequency(quint64 frequency);
|
|
|
|
void setTunerAGC(bool agc);
|
|
|
|
void setTunerGain(int gain);
|
2022-11-03 11:57:00 -04:00
|
|
|
void setGainByIndex(int gain);
|
2022-07-19 05:10:20 -04:00
|
|
|
void setFreqCorrection(int correction);
|
|
|
|
void setIFGain(quint16 stage, quint16 gain);
|
|
|
|
void setAGC(bool agc);
|
|
|
|
void setDirectSampling(bool enabled);
|
|
|
|
void setDCOffsetRemoval(bool enabled);
|
|
|
|
void setIQCorrection(bool enabled);
|
|
|
|
void setBiasTee(bool enabled);
|
|
|
|
void setBandwidth(int bandwidth);
|
|
|
|
void setDecimation(int dec);
|
|
|
|
void setChannelSampleRate(int dec);
|
|
|
|
void setChannelFreqOffset(int offset);
|
|
|
|
void setChannelGain(int gain);
|
|
|
|
void setSampleBitDepth(int sampleBits);
|
2024-09-22 05:44:30 -04:00
|
|
|
void setSquelchEnabled(bool enabled);
|
|
|
|
void setSquelch(float squelch);
|
|
|
|
void setSquelchGate(float squelchGate);
|
|
|
|
void sendMessage(const QString& callsign, const QString& text, bool broadcast);
|
2022-10-25 18:40:33 -04:00
|
|
|
void applySettings(const RemoteTCPInputSettings& settings, const QList<QString>& settingsKeys, bool force = false);
|
2024-02-27 09:03:34 -05:00
|
|
|
void processMetaData();
|
|
|
|
void spyServerConnect();
|
|
|
|
void spyServerSet(int setting, int value);
|
|
|
|
void spyServerSetIQFormat(int sampleBits);
|
|
|
|
void spyServerSetStreamIQ();
|
|
|
|
void processSpyServerMetaData();
|
|
|
|
void processSpyServerDevice(const SpyServerProtocol::Device* ssDevice);
|
|
|
|
void processSpyServerState(const SpyServerProtocol::State* ssState, bool initial);
|
|
|
|
void processSpyServerData(int requiredBytes, bool clear);
|
2024-09-22 05:44:30 -04:00
|
|
|
void processDecompressedData(int requiredSamples);
|
|
|
|
void processUncompressedData(const char *inBuf, int nbSamples);
|
|
|
|
void processDecompressedZlibData(const char *inBuf, int nbSamples);
|
|
|
|
void calcPower(const Sample *iq, int nbSamples);
|
|
|
|
void sendSettings(const RemoteTCPInputSettings& settings, const QStringList& settingsKeys);
|
2022-07-19 05:10:20 -04:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void started();
|
|
|
|
void finished();
|
|
|
|
void handleInputMessages();
|
|
|
|
void processData();
|
|
|
|
void reconnect();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* PLUGINS_SAMPLESOURCE_REMOTETCPINPUT_REMOTETCPINPUTUDPHANDLER_H_ */
|