wfview/udphandler.h

200 wiersze
4.0 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-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-08 08:31:48 +00:00
#include "rxaudiohandler.h"
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;
2021-02-04 20:09:09 +00:00
int localPort=0;
int port=0;
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-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-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-08 08:31:48 +00:00
void setupAudio(const QAudioFormat format, const int bufferSize);
2021-02-03 20:00:40 +00:00
private:
void DataReceived();
QBuffer* buffer;
QAudioFormat format;
bool sentPacketConnect2 = false;
uint16_t sendAudioSeq = 0;
2021-02-08 08:31:48 +00:00
rxAudioHandler* rxaudio;
QThread* rxAudioThread;
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, int cport, int sport, int aport, QString username, QString password);
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);
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;
2021-02-07 17:40:38 +00:00
bool radioInUse = false;
2021-02-03 20:00:40 +00:00
int aport;
int sport;
int reauthInterval = 60000;
QTimer reauthTimer;
2021-02-07 17:40:38 +00:00
QByteArray devName;
QByteArray compName;
2021-02-03 20:00:40 +00:00
};
#endif