wfview/udphandler.h

184 wiersze
3.7 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>
// Allow easy endian-ness conversions
#include <QtEndian>
// Needed for audio
#include <QtMultimedia/QAudioOutput>
2021-02-03 20:00:40 +00:00
#include <QBuffer>
#include <QDebug>
// 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);
QString parseNullTerminatedString(QByteArray c, int s);
QUdpSocket* udp=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 = 0;
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-04 20:09:09 +00:00
time_t lastPacket0Sent = 0;
time_t lastPacket7Sent = 0;
2021-02-05 21:23:00 +00:00
int latency = 0;
2021-02-03 20:00:40 +00:00
QString username = "";
QString password = "";
QHostAddress radioIP;
QHostAddress localIP;
bool isAuthenticated = false;
2021-02-04 20:09:09 +00:00
int localPort=0;
int port=0;
2021-02-03 20:00:40 +00:00
QTimer *pkt7Timer=nullptr; // Send pkt7 packets every 3 seconds
QTimer *pkt0Timer=nullptr; // Send pkt0 packets every 1000ms.
QTimer *periodic=nullptr; // Send pkt0 packets every 1000ms.
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-03 20:10:21 +00:00
udpSerial(QHostAddress local, QHostAddress ip, int 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-03 20:10:21 +00:00
udpAudio(QHostAddress local, QHostAddress ip, int aport);
~udpAudio();
2021-02-03 20:00:40 +00:00
private:
void DataReceived();
QBuffer* buffer;
QAudioOutput* audio;
QAudioFormat format;
bool sentPacketConnect2 = false;
uint16_t sendAudioSeq = 0;
};
// Class to handle the connection/disconnection of the radio.
class udpHandler: public udpBase
{
Q_OBJECT
public:
udpHandler(QHostAddress ip, int cport, int sport, int aport, QString username, QString password);
~udpHandler();
udpSerial *serial=nullptr;
udpAudio *audio=nullptr;
bool serialAndAudioOpened = false;
public slots:
void receiveDataFromUserToRig(QByteArray); // This slot will send data on to
void receiveFromSerialStream(QByteArray);
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);
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;
int aport;
int sport;
int reauthInterval = 60000;
QTimer reauthTimer;
QString devname;
};
#endif