2021-02-03 20:00:40 +00:00
|
|
|
#ifndef UDPHANDLER_H
|
|
|
|
#define UDPHANDLER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QNetworkDatagram>
|
|
|
|
#include <QHostInfo>
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QMutex>
|
2021-02-06 10:54:20 +00:00
|
|
|
#include <QDateTime>
|
2021-02-11 19:18:35 +00:00
|
|
|
#include <QByteArray>
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
// Allow easy endian-ness conversions
|
|
|
|
#include <QtEndian>
|
|
|
|
|
|
|
|
// Needed for audio
|
2021-02-03 22:10:37 +00:00
|
|
|
#include <QtMultimedia/QAudioOutput>
|
2021-02-03 20:00:40 +00:00
|
|
|
#include <QBuffer>
|
2021-02-08 08:31:48 +00:00
|
|
|
#include <QThread>
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
#include "audiohandler.h"
|
2021-02-08 08:31:48 +00:00
|
|
|
|
2021-02-03 20:00:40 +00:00
|
|
|
// Parent class that contains all common items.
|
|
|
|
class udpBase : public QObject
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
2021-02-04 20:09:09 +00:00
|
|
|
~udpBase();
|
|
|
|
|
2021-02-04 20:20:23 +00:00
|
|
|
void init();
|
|
|
|
|
2021-02-03 20:00:40 +00:00
|
|
|
qint64 SendTrackedPacket(QByteArray d);
|
|
|
|
qint64 SendPacketConnect();
|
|
|
|
qint64 SendPacketConnect2();
|
|
|
|
qint64 SendPacketDisconnect();
|
2021-02-04 13:12:08 +00:00
|
|
|
void SendPkt0Idle(bool tracked, quint16 seq);
|
2021-02-03 20:00:40 +00:00
|
|
|
void SendPkt7Idle();
|
|
|
|
void PurgeOldEntries();
|
|
|
|
|
2021-02-04 18:52:00 +00:00
|
|
|
void DataReceived(QByteArray r);
|
|
|
|
|
2021-02-03 20:00:40 +00:00
|
|
|
unsigned char* Passcode(QString str);
|
2021-02-07 17:40:38 +00:00
|
|
|
QByteArray parseNullTerminatedString(QByteArray c, int s);
|
2021-02-07 12:59:41 +00:00
|
|
|
QUdpSocket* udp=Q_NULLPTR;
|
2021-02-03 20:00:40 +00:00
|
|
|
uint32_t localSID = 0;
|
|
|
|
uint32_t remoteSID = 0;
|
|
|
|
char authID[6] = { 0, 0, 0, 0, 0, 0 };
|
|
|
|
char a8replyID[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
uint16_t authInnerSendSeq = 0;
|
2021-02-06 10:54:20 +00:00
|
|
|
uint16_t innerSendSeq = 0x8304; // Not sure why?
|
2021-02-03 20:00:40 +00:00
|
|
|
uint16_t sendSeqB = 0;
|
|
|
|
uint16_t sendSeq = 1;
|
2021-02-05 20:26:18 +00:00
|
|
|
uint16_t lastReceivedSeq = 0;
|
2021-02-03 20:00:40 +00:00
|
|
|
uint16_t pkt0SendSeq = 0;
|
|
|
|
uint16_t pkt7SendSeq = 0;
|
|
|
|
uint16_t periodicSeq = 0;
|
2021-02-06 10:54:20 +00:00
|
|
|
QDateTime lastPacket0Sent;
|
|
|
|
QDateTime lastPacket7Sent;
|
|
|
|
quint64 latency = 0;
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
QString username = "";
|
|
|
|
QString password = "";
|
|
|
|
QHostAddress radioIP;
|
|
|
|
QHostAddress localIP;
|
|
|
|
bool isAuthenticated = false;
|
2021-02-08 16:53:26 +00:00
|
|
|
quint16 localPort=0;
|
|
|
|
quint16 port=0;
|
2021-02-07 12:59:41 +00:00
|
|
|
QTimer *pkt7Timer=Q_NULLPTR; // Send pkt7 packets every 3 seconds
|
|
|
|
QTimer *pkt0Timer=Q_NULLPTR; // Send pkt0 packets every 1000ms.
|
|
|
|
QTimer *periodic=Q_NULLPTR; // Send pkt0 packets every 1000ms.
|
2021-02-03 20:00:40 +00:00
|
|
|
bool periodicRunning = false;
|
|
|
|
bool sentPacketConnect2 = false;
|
|
|
|
time_t lastReceived = time(0);
|
|
|
|
QMutex mutex;
|
|
|
|
|
|
|
|
struct SEQBUFENTRY {
|
|
|
|
time_t timeSent;
|
|
|
|
uint16_t seqNum;
|
|
|
|
QByteArray data;
|
|
|
|
};
|
|
|
|
|
|
|
|
QList <SEQBUFENTRY> txSeqBuf = QList<SEQBUFENTRY>();
|
|
|
|
QList <SEQBUFENTRY> seqBuf = QList<SEQBUFENTRY>();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Class for all (pseudo) serial communications
|
|
|
|
class udpSerial : public udpBase
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-02-08 16:53:26 +00:00
|
|
|
udpSerial(QHostAddress local, QHostAddress ip, quint16 sport);
|
2021-02-03 20:00:40 +00:00
|
|
|
QMutex serialmutex;
|
|
|
|
|
|
|
|
signals:
|
|
|
|
//void ReceiveSerial(QByteArray);
|
|
|
|
int Receive(QByteArray);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
int Send(QByteArray d);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
void DataReceived();
|
|
|
|
void SendIdle();
|
|
|
|
void SendPeriodic();
|
|
|
|
qint64 SendPacketOpenClose(bool close);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Class for all audio communications.
|
|
|
|
class udpAudio : public udpBase
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-02-09 12:43:28 +00:00
|
|
|
udpAudio(QHostAddress local, QHostAddress ip, quint16 aport, quint16 buffer, quint16 rxsample, quint8 rxcodec, quint16 txsample, quint8 txcodec);
|
2021-02-05 17:40:58 +00:00
|
|
|
~udpAudio();
|
2021-02-07 17:40:38 +00:00
|
|
|
QAudioOutput* audio;
|
2021-02-08 08:31:48 +00:00
|
|
|
|
|
|
|
signals:
|
2021-02-08 12:38:30 +00:00
|
|
|
void haveAudioData(QByteArray data);
|
2021-02-11 19:47:29 +00:00
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
void setupTxAudio(const quint8 samples, const quint8 channels, const quint16 samplerate, const quint16 bufferSize, const bool isUlaw, const bool isInput);
|
|
|
|
void setupRxAudio(const quint8 samples, const quint8 channels, const quint16 samplerate, const quint16 bufferSize, const bool isUlaw, const bool isInput);
|
2021-02-11 19:47:29 +00:00
|
|
|
|
2021-02-08 16:53:26 +00:00
|
|
|
void haveChangeBufferSize(quint16 value);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void changeBufferSize(quint16 value);
|
2021-02-11 19:18:35 +00:00
|
|
|
void sendTxAudio(QByteArray d);
|
|
|
|
|
2021-02-03 20:00:40 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
void DataReceived();
|
|
|
|
QAudioFormat format;
|
2021-02-08 16:53:26 +00:00
|
|
|
quint16 bufferSize;
|
2021-02-09 12:43:28 +00:00
|
|
|
quint16 rxSampleRate;
|
|
|
|
quint16 txSampleRate;
|
|
|
|
quint8 rxCodec;
|
|
|
|
quint8 txCodec;
|
|
|
|
quint8 rxChannelCount = 1;
|
|
|
|
bool rxIsUlawCodec = false;
|
|
|
|
quint8 rxNumSamples = 16;
|
|
|
|
quint8 txChannelCount = 1;
|
|
|
|
bool txIsUlawCodec = false;
|
|
|
|
quint8 txNumSamples = 16;
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
bool sentPacketConnect2 = false;
|
|
|
|
uint16_t sendAudioSeq = 0;
|
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
audioHandler* rxaudio;
|
|
|
|
QThread* rxAudioThread;
|
2021-02-08 08:31:48 +00:00
|
|
|
|
2021-02-11 19:18:35 +00:00
|
|
|
audioHandler* txaudio;
|
|
|
|
QThread* txAudioThread;
|
2021-02-10 17:32:56 +00:00
|
|
|
|
2021-02-03 20:00:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Class to handle the connection/disconnection of the radio.
|
|
|
|
class udpHandler: public udpBase
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-02-09 12:43:28 +00:00
|
|
|
udpHandler(QString ip, quint16 cport, quint16 sport, quint16 aport, QString username, QString password,
|
|
|
|
quint16 buffer, quint16 rxsample, quint8 rxcodec, quint16 txsample, quint8 txcodec);
|
2021-02-03 20:00:40 +00:00
|
|
|
~udpHandler();
|
|
|
|
|
2021-02-07 12:59:41 +00:00
|
|
|
udpSerial *serial=Q_NULLPTR;
|
|
|
|
udpAudio *audio=Q_NULLPTR;
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
bool serialAndAudioOpened = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
void receiveDataFromUserToRig(QByteArray); // This slot will send data on to
|
|
|
|
void receiveFromSerialStream(QByteArray);
|
2021-02-08 16:53:26 +00:00
|
|
|
void changeBufferSize(quint16 value);
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void RigConnected(const QString&);
|
|
|
|
void haveDataFromPort(QByteArray data); // emit this when we have data, connect to rigcommander
|
2021-02-04 19:53:48 +00:00
|
|
|
void haveNetworkError(QString, QString);
|
2021-02-05 20:26:18 +00:00
|
|
|
void haveNetworkStatus(QString);
|
2021-02-08 16:53:26 +00:00
|
|
|
void haveChangeBufferSize(quint16 value);
|
2021-02-03 20:00:40 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
qint64 SendRequestSerialAndAudio();
|
|
|
|
qint64 SendPacketLogin();
|
|
|
|
qint64 SendPacketAuth(uint8_t magic);
|
|
|
|
void ReAuth();
|
|
|
|
void DataReceived();
|
|
|
|
bool gotA8ReplyID = false;
|
|
|
|
bool gotAuthOK = false;
|
|
|
|
|
|
|
|
bool sentPacketLogin = false;
|
|
|
|
bool sentPacketConnect = false;
|
|
|
|
bool sentPacketConnect2 = false;
|
|
|
|
|
2021-02-07 17:40:38 +00:00
|
|
|
bool radioInUse = false;
|
|
|
|
|
2021-02-08 16:53:26 +00:00
|
|
|
quint16 aport;
|
|
|
|
quint16 sport;
|
2021-02-09 12:43:28 +00:00
|
|
|
quint16 rxSampleRate;
|
|
|
|
quint16 txSampleRate;
|
|
|
|
quint16 rxBufferSize;
|
|
|
|
quint8 rxCodec;
|
|
|
|
quint8 txCodec;
|
|
|
|
|
2021-02-08 16:53:26 +00:00
|
|
|
quint16 reauthInterval = 60000;
|
2021-02-03 20:00:40 +00:00
|
|
|
QTimer reauthTimer;
|
2021-02-07 17:40:38 +00:00
|
|
|
QByteArray devName;
|
|
|
|
QByteArray compName;
|
2021-02-03 20:00:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-02-03 22:10:37 +00:00
|
|
|
#endif
|