Better error messages and more udp code cleanup

merge-requests/1/head
Phil Taylor 2021-02-04 19:53:48 +00:00
rodzic 33b55f8bbf
commit fe7fc46301
3 zmienionych plików z 51 dodań i 60 usunięć

Wyświetl plik

@ -67,7 +67,7 @@ rigCommander::rigCommander(unsigned char rigCivAddr, QString rigSerialPort, quin
// data from the program to the comm port:
connect(this, SIGNAL(dataForComm(QByteArray)), comm, SLOT(receiveDataFromUserToRig(QByteArray)));
connect(comm, SIGNAL(haveSerialPortError(QString,QString)), this, SLOT(handleSerialPortError(QString,QString)));
connect(comm, SIGNAL(haveSerialPortError(QString, QString)), this, SLOT(handleSerialPortError(QString, QString)));
connect(this, SIGNAL(getMoreDebug()), comm, SLOT(debugThis()));
pttAllowed = true; // This is for developing, set to false for "safe" debugging. Set to true for deployment.
@ -114,6 +114,9 @@ rigCommander::rigCommander(unsigned char rigCivAddr, QHostAddress ip, int cport,
// data from the program to the comm port:
connect(this, SIGNAL(dataForComm(QByteArray)), udp, SLOT(receiveDataFromUserToRig(QByteArray)));
// Connect for errors/alerts
connect(udp, SIGNAL(haveNetworkError(QString, QString)), this, SLOT(handleSerialPortError(QString, QString)));
//connect(this, SIGNAL(getMoreDebug()), comm, SLOT(debugThis()));
pttAllowed = true; // This is for developing, set to false for "safe" debugging. Set to true for deployment.

Wyświetl plik

@ -12,48 +12,40 @@ udpHandler::udpHandler(QHostAddress ip, int cport, int sport, int aport,QString
this->sport = sport;
this->username = username;
this->password = password;
udp = new QUdpSocket(this);
udp->bind(); // Bind to random port.
localPort = udp->localPort();
qDebug() << "ControlStream bound to local port:" << localPort << " remote port:" << port;
QUdpSocket::connect(udp, &QUdpSocket::readyRead, this, &udpHandler::DataReceived);
// Convoluted way to find the external IP address, there must be a better way????
QString localhostname = QHostInfo::localHostName();
QList<QHostAddress> hostList = QHostInfo::fromName(localhostname).addresses();
foreach(const QHostAddress & address, hostList) {
if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false) {
foreach(const QHostAddress & address, hostList)
{
if (address.protocol() == QAbstractSocket::IPv4Protocol && address.isLoopback() == false)
{
localIP = QHostAddress(address.toString());
}
}
uint32_t addr = QHostAddress(localIP).toIPv4Address();
localSID = (addr >> 8 & 0xff) << 24 | (addr & 0xff) << 16 | (localPort & 0xffff);
udpBase::udpBase(); // Perform connection
QUdpSocket::connect(udp, &QUdpSocket::readyRead, this, &udpHandler::DataReceived);
connect(&reauthTimer, &QTimer::timeout, this, QOverload<>::of(&udpHandler::ReAuth));
SendPacketConnect(); // First connect packet
}
udpHandler::~udpHandler()
{
if (isAuthenticated)
{
if (serial != nullptr)
delete serial;
if (audio != nullptr)
delete audio;
if (serial != nullptr)
delete serial;
qDebug() << "Sending Control De-Auth";
qDebug() << "Sending De-Auth packet to radio";
SendPacketAuth(0x01);
SendPacketDisconnect();
}
udp->close();
delete udp;
qDebug() << "Closing udpHandler";
}
void udpHandler::ReAuth()
@ -111,9 +103,11 @@ void udpHandler::DataReceived()
{
if (r.mid(48, 3) == QByteArrayLiteral("\xff\xff\xff"))
if (!serialAndAudioOpened) {
emit haveNetworkError(radioIP.toString(), "Auth failed, try rebooting the radio.");
qDebug() << "Auth failed, try rebooting the radio.";
}
if (r.mid(48, 3) == QByteArrayLiteral("\x00\x00\x00") && r[64] == (char)0x01) {
emit haveNetworkError(radioIP.toString(), "Got radio disconnected.");
qDebug() << "Got radio disconnected.";
}
}
@ -125,11 +119,13 @@ void udpHandler::DataReceived()
{
if (r.mid(48, 4) == QByteArrayLiteral("\xff\xff\xff\xfe"))
{
emit haveNetworkError(radioIP.toString(), "Invalid Username/Password");
qDebug() << "Invalid Username/Password";
}
else if (!isAuthenticated)
{
emit haveNetworkError(radioIP.toString(), "Radio Login OK!");
qDebug() << "Login OK!";
authID[0] = r[26];
@ -322,30 +318,14 @@ qint64 udpHandler::SendPacketAuth(uint8_t magic)
// (pseudo) serial class
udpSerial::udpSerial(QHostAddress local, QHostAddress ip, int sport) {
qDebug() << "Starting udpSerial";
localIP = local;
port = sport;
radioIP = ip;
udp = new QUdpSocket(this);
udp->bind(); // Bind to random port.
udpBase::udpBase(); // Perform connection
localPort = udp->localPort();
qDebug() << "Serial Stream bound to local port:" << localPort << " remote port:" << port;
uint32_t addr =local.toIPv4Address();
localSID = (addr >> 8 & 0xff) << 24 | (addr & 0xff) << 16 | (localPort & 0xffff);
QUdpSocket::connect(udp, &QUdpSocket::readyRead, this, &udpSerial::DataReceived);
SendPacketConnect(); // Need to first get the remote address
}
udpSerial::~udpSerial()
{
qDebug() << "Closing udpSerial";
SendPacketOpenClose(true);
SendPacketDisconnect();
//_sleep(100);
udp->close();
delete udp;
SendPacketConnect(); // First connect packet
}
@ -443,7 +423,8 @@ void udpSerial::DataReceived()
break;
default:
if (r.length() > 21) {
uint16_t gotSeq = qFromLittleEndian<quint16>(r.mid(6, 2));
// We should probably check for missing packets?
//uint16_t gotSeq = qFromLittleEndian<quint16>(r.mid(6, 2));
quint8 temp = r[0] - 0x15;
if ((quint8)r[16] == 0xc1 && (quint8)r[17] == temp)
@ -457,21 +438,17 @@ void udpSerial::DataReceived()
}
// Audio stream
udpAudio::udpAudio(QHostAddress local, QHostAddress ip, int aport)
{
qDebug() << "Starting udpAudio";
localIP = local;
port = aport;
radioIP = ip;
udp = new QUdpSocket(this);
udp->bind(); // Bind to random port.
localPort = udp->localPort();
qDebug() << "Audio Stream bound to local port:" << localPort << " remote port:" << port;
QUdpSocket::connect(udp, &QUdpSocket::readyRead, this, &udpAudio::DataReceived);
uint32_t addr = local.toIPv4Address();
localSID = (addr >> 8 & 0xff) << 24 | (addr & 0xff) << 16 | (localPort & 0xffff);
udpBase::udpBase(); // Perform connection
QUdpSocket::connect(udp, &QUdpSocket::readyRead, this, &udpAudio::DataReceived);
SendPacketConnect(); // First connect packet
@ -498,19 +475,10 @@ udpAudio::udpAudio(QHostAddress local, QHostAddress ip, int aport)
buffer->seek(0);
audio->start(buffer);
}
udpAudio::~udpAudio()
{
qDebug() << "Closing udpAudio";
SendPacketDisconnect();
udp->close();
delete udp;
}
void udpAudio::DataReceived()
{
while (udp->hasPendingDatagrams()) {
@ -570,6 +538,25 @@ void udpAudio::DataReceived()
}
udpBase::udpBase()
{
udp = new QUdpSocket(this);
udp->bind(); // Bind to random port.
localPort = udp->localPort();
qDebug() << "UDP Stream bound to local port:" << localPort << " remote port:" << port;
uint32_t addr = localIP.toIPv4Address();
localSID = (addr >> 8 & 0xff) << 24 | (addr & 0xff) << 16 | (localPort & 0xffff);
}
udpBase::~udpBase()
{
qDebug() << "Closing UDP stream :" << radioIP.toString() << ":" << port;
SendPacketDisconnect();
if (udp != nullptr) {
udp->close();
delete udp;
}
}
// Base class!

Wyświetl plik

@ -23,6 +23,8 @@ class udpBase : public QObject
{
public:
udpBase::udpBase();
udpBase::~udpBase();
qint64 SendTrackedPacket(QByteArray d);
qint64 SendPacketConnect();
qint64 SendPacketConnect2();
@ -35,7 +37,7 @@ public:
unsigned char* Passcode(QString str);
QString parseNullTerminatedString(QByteArray c, int s);
QUdpSocket* udp;
QUdpSocket* udp=nullptr;
uint32_t localSID = 0;
uint32_t remoteSID = 0;
char authID[6] = { 0, 0, 0, 0, 0, 0 };
@ -84,7 +86,6 @@ class udpSerial : public udpBase
public:
udpSerial(QHostAddress local, QHostAddress ip, int sport);
~udpSerial();
QMutex serialmutex;
signals:
@ -110,7 +111,6 @@ class udpAudio : public udpBase
public:
udpAudio(QHostAddress local, QHostAddress ip, int aport);
~udpAudio();
private:
@ -152,6 +152,7 @@ public slots:
signals:
void RigConnected(const QString&);
void haveDataFromPort(QByteArray data); // emit this when we have data, connect to rigcommander
void haveNetworkError(QString, QString);
private: