wfview/udphandler.h

230 wiersze
5.1 KiB
C
Czysty Zwykły widok Historia

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>
#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
#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();
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();
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);
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;
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;
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;
quint16 localPort=0;
quint16 port=0;
QTimer *pkt7Timer=Q_NULLPTR; // Send pkt7 packets every 3 seconds
QTimer *pkt0Timer=Q_NULLPTR; // Send pkt0 packets every 1000ms.
2021-02-12 20:42:56 +00:00
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:
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:
udpAudio(QHostAddress local, QHostAddress ip, quint16 aport, quint16 buffer, quint16 rxsample, quint8 rxcodec, quint16 txsample, quint8 txcodec);
~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: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);
void haveChangeBufferSize(quint16 value);
public slots:
void changeBufferSize(quint16 value);
2021-02-11 19:18:35 +00:00
2021-02-03 20:00:40 +00:00
private:
2021-02-12 23:56:02 +00:00
void sendTxAudio();
2021-02-03 20:00:40 +00:00
void DataReceived();
QAudioFormat format;
quint16 bufferSize;
quint16 rxSampleRate;
quint16 txSampleRate;
quint8 rxCodec;
quint8 txCodec;
quint8 rxChannelCount = 1;
bool rxIsUlawCodec = false;
2021-02-12 14:28:55 +00:00
quint8 rxNumSamples = 8;
quint8 txChannelCount = 1;
bool txIsUlawCodec = false;
2021-02-12 14:28:55 +00:00
quint8 txNumSamples = 8;
2021-02-03 20:00:40 +00:00
bool sentPacketConnect2 = false;
uint16_t sendAudioSeq = 0;
2021-02-13 11:04:26 +00:00
audioHandler* rxaudio=Q_NULLPTR;
QThread* rxAudioThread=Q_NULLPTR;
2021-02-08 08:31:48 +00:00
2021-02-13 11:04:26 +00:00
audioHandler* txaudio=Q_NULLPTR;
QThread* txAudioThread=Q_NULLPTR;
2021-02-13 11:04:26 +00:00
QTimer* txAudioTimer=Q_NULLPTR;
2021-02-12 23:56:02 +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:
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();
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);
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
void haveNetworkError(QString, QString);
2021-02-05 20:26:18 +00:00
void haveNetworkStatus(QString);
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;
quint16 aport;
quint16 sport;
quint16 rxSampleRate;
quint16 txSampleRate;
quint16 rxBufferSize;
quint8 rxCodec;
quint8 txCodec;
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
};
#endif