From c78ad19210b2b0be373d9f0f8497bab02d14e21c Mon Sep 17 00:00:00 2001 From: Phil Taylor Date: Mon, 19 Apr 2021 17:26:26 +0100 Subject: [PATCH] Initial commit of rigctld (doesn't currently do anything useful!) --- logcategories.cpp | 1 + logcategories.h | 1 + rigctld.cpp | 118 +++++++++++++++++++++++++++++++++++++++++ rigctld.h | 66 +++++++++++++++++++++++ wfmain.cpp | 21 +++++++- wfmain.h | 4 ++ wfview.pro | 6 ++- wfview.vcxproj | 2 + wfview.vcxproj.filters | 7 +++ 9 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 rigctld.cpp create mode 100644 rigctld.h diff --git a/logcategories.cpp b/logcategories.cpp index f3042f1..96d0a97 100644 --- a/logcategories.cpp +++ b/logcategories.cpp @@ -7,3 +7,4 @@ Q_LOGGING_CATEGORY(logRig, "rig") Q_LOGGING_CATEGORY(logAudio, "audio") Q_LOGGING_CATEGORY(logUdp, "udp") Q_LOGGING_CATEGORY(logUdpServer, "udp.server") +Q_LOGGING_CATEGORY(logRigCtlD, "rigctld") diff --git a/logcategories.h b/logcategories.h index 2076bad..a76de51 100644 --- a/logcategories.h +++ b/logcategories.h @@ -10,5 +10,6 @@ Q_DECLARE_LOGGING_CATEGORY(logRig) Q_DECLARE_LOGGING_CATEGORY(logAudio) Q_DECLARE_LOGGING_CATEGORY(logUdp) Q_DECLARE_LOGGING_CATEGORY(logUdpServer) +Q_DECLARE_LOGGING_CATEGORY(logRigCtlD) #endif // LOGCATEGORIES_H diff --git a/rigctld.cpp b/rigctld.cpp new file mode 100644 index 0000000..801cf12 --- /dev/null +++ b/rigctld.cpp @@ -0,0 +1,118 @@ +#include "rigctld.h" +#include "logcategories.h" + + + +rigCtlD::rigCtlD(QObject* parent) : + QTcpServer(parent) +{ +} + +rigCtlD::~rigCtlD() +{ +} + +int rigCtlD::startServer(qint16 port) +{ + if (!this->listen(QHostAddress::Any, port)) { + qDebug(logRigCtlD()) << "could not start on port " << port; + return -1; + } + else + { + qDebug(logRigCtlD()) << "started on port " << port; + } + return 0; +} + +void rigCtlD::incomingConnection(qintptr socket) { + rigCtlClient* client = new rigCtlClient(socket, rigCaps, this); + connect(this, SIGNAL(onStopped()), client, SLOT(closeSocket())); +} + + +void rigCtlD::stopServer() +{ + qDebug(logRigCtlD()) << "stopping server"; + emit onStopped(); +} + +void rigCtlD::receiveRigCaps(rigCapabilities caps) +{ + qDebug(logRigCtlD()) << "Got rigcaps for:" << caps.modelName; + this->rigCaps = caps; +} + +rigCtlClient::rigCtlClient(int socketId, rigCapabilities caps, rigCtlD *parent) : QObject(parent) +{ + commandBuffer.clear(); + sessionId = socketId; + rigCaps = caps; + socket = new QTcpSocket(this); + if (!socket->setSocketDescriptor(sessionId)) + { + qDebug(logRigCtlD()) << " error binding socket: " << sessionId; + return; + } + connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()), Qt::DirectConnection); + connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected()), Qt::DirectConnection); + connect(parent, SIGNAL(sendData(QString)), this, SLOT(sendData(QString)), Qt::DirectConnection); + qDebug(logRigCtlD()) << " session connected: " << sessionId; +} + +void rigCtlClient::socketReadyRead() +{ + QByteArray data = socket->readAll(); + commandBuffer.append(data); + if (commandBuffer.endsWith('\n')) + { + // Process command + qDebug(logRigCtlD()) << sessionId << "command received" << commandBuffer; + QString cmd = commandBuffer[0]; + + if (cmd.toLower() == "q") + { + closeSocket(); + } + else if (cmd == "1") + { + dumpCaps(); + } + commandBuffer.clear(); + } +} + +void rigCtlClient::socketDisconnected() +{ + qDebug(logRigCtlD()) << sessionId << "disconnected"; + socket->deleteLater(); + this->deleteLater(); +} + +void rigCtlClient::closeSocket() +{ + socket->close(); +} + +void rigCtlClient::sendData(QString data) +{ + if (socket != Q_NULLPTR && socket->isValid() && socket->isOpen()) + { + socket->write(data.toLatin1()); + } +} + + +void rigCtlClient::dumpCaps() +{ + sendData(QString("Caps dump for model: %1\n").arg(rigCaps.modelID)); + sendData(QString("Model Name:\t%1\n").arg(rigCaps.modelName)); + sendData(QString("Mfg Name:\tIcom\n")); + sendData(QString("Backend version:\t0.1\n")); + sendData(QString("Backend copyright:\t2021\n")); + sendData(QString("Rig type:\tTransceiver\n")); + sendData(QString("PTT type:\tRig capable\n")); + sendData(QString("DCD type:\tRig capable\n")); + sendData(QString("Port type:\tNetwork link\n")); + +} \ No newline at end of file diff --git a/rigctld.h b/rigctld.h new file mode 100644 index 0000000..b0b3366 --- /dev/null +++ b/rigctld.h @@ -0,0 +1,66 @@ +#ifndef RIGCTLD_H +#define RIGCTLD_H + +#include +#include +#include +#include +#include +#include + +#include "rigcommander.h" + + +class rigCtlD : public QTcpServer +{ + Q_OBJECT + +public: + explicit rigCtlD(QObject *parent=Q_NULLPTR); + virtual ~rigCtlD(); + + int startServer(qint16 port); + void stopServer(); + rigCapabilities rigCaps; + +signals: + void onStarted(); + void onStopped(); + void sendData(QString data); + +public slots: + virtual void incomingConnection(qintptr socketDescriptor); + void receiveRigCaps(rigCapabilities caps); + + +}; + + +class rigCtlClient : public QObject +{ + Q_OBJECT + +public: + + explicit rigCtlClient(int socket, rigCapabilities caps, rigCtlD* parent = Q_NULLPTR); + int getSocketId(); + + +public slots: + void socketReadyRead(); + void socketDisconnected(); + void closeSocket(); + void sendData(QString data); + +protected: + int sessionId; + QTcpSocket* socket = Q_NULLPTR; + QString commandBuffer; + +private: + void dumpCaps(); + rigCapabilities rigCaps; + +}; + +#endif diff --git a/wfmain.cpp b/wfmain.cpp index 2b7d193..d548001 100644 --- a/wfmain.cpp +++ b/wfmain.cpp @@ -203,6 +203,13 @@ wfmain::wfmain(const QString serialPortCL, const QString hostCL, QWidget *parent } + // Start rigctld + if (prefs.enableRigCtlD) { + rigCtl = new rigCtlD(this); + + rigCtl->startServer(prefs.rigCtlPort); + connect(this, SIGNAL(sendRigCaps(rigCapabilities)), rigCtl, SLOT(receiveRigCaps(rigCapabilities))); + } plot = ui->plot; // rename it waterfall. wf = ui->waterfall; tracer = new QCPItemTracer(plot); @@ -569,6 +576,9 @@ wfmain::~wfmain() serverThread->quit(); serverThread->wait(); } + if (rigCtl != Q_NULLPTR) { + delete rigCtl; + } delete rpt; delete ui; } @@ -754,6 +764,8 @@ void wfmain::setDefPrefs() defPrefs.serialPortBaud = 115200; defPrefs.enablePTT = false; defPrefs.niceTS = true; + defPrefs.enableRigCtlD = false; + defPrefs.rigCtlPort = 4533; udpDefPrefs.ipAddress = QString(""); udpDefPrefs.controlLANPort = 50001; @@ -811,7 +823,10 @@ void wfmain::loadSettings() prefs.enableLAN = settings.value("EnableLAN", defPrefs.enableLAN).toBool(); ui->lanEnableBtn->setChecked(prefs.enableLAN); ui->connectBtn->setEnabled(prefs.enableLAN); - + + prefs.enableRigCtlD = settings.value("EnableRigCtlD", defPrefs.enableRigCtlD).toBool(); + prefs.rigCtlPort = settings.value("RigCtlPort", defPrefs.rigCtlPort).toInt(); + udpPrefs.ipAddress = settings.value("IPAddress", udpDefPrefs.ipAddress).toString(); ui->ipAddressTxt->setEnabled(ui->lanEnableBtn->isChecked()); ui->ipAddressTxt->setText(udpPrefs.ipAddress); @@ -983,6 +998,8 @@ void wfmain::saveSettings() settings.beginGroup("LAN"); settings.setValue("EnableLAN", prefs.enableLAN); + settings.setValue("EnableRigCtlD", prefs.enableRigCtlD); + settings.setValue("RigCtlPort", prefs.rigCtlPort); settings.setValue("IPAddress", udpPrefs.ipAddress); settings.setValue("ControlLANPort", udpPrefs.controlLANPort); settings.setValue("SerialLANPort", udpPrefs.serialLANPort); @@ -998,7 +1015,7 @@ void wfmain::saveSettings() settings.setValue("AudioOutput", udpPrefs.audioOutput); settings.setValue("AudioInput", udpPrefs.audioInput); settings.setValue("ResampleQuality", udpPrefs.resampleQuality); - settings.setValue("clientName", udpPrefs.clientName); + settings.setValue("ClientName", udpPrefs.clientName); settings.endGroup(); // Memory channels diff --git a/wfmain.h b/wfmain.h index bef0feb..8a8905c 100644 --- a/wfmain.h +++ b/wfmain.h @@ -23,6 +23,7 @@ #include "udpserversetup.h" #include "udpserver.h" #include "qledlabel.h" +#include "rigctld.h" #include #include @@ -550,6 +551,8 @@ private: bool enablePTT; bool niceTS; bool enableLAN; + bool enableRigCtlD; + quint16 rigCtlPort; } prefs; preferences defPrefs; @@ -618,6 +621,7 @@ private: udpServerSetup *srv; udpServer* udp = Q_NULLPTR; + rigCtlD* rigCtl = Q_NULLPTR; QThread* serverThread = Q_NULLPTR; void bandStackBtnClick(); diff --git a/wfview.pro b/wfview.pro index 1c39103..a39f4b4 100644 --- a/wfview.pro +++ b/wfview.pro @@ -100,7 +100,8 @@ SOURCES += main.cpp\ qledlabel.cpp \ pttyhandler.cpp \ resampler/resample.c \ - repeatersetup.cpp + repeatersetup.cpp \ + rigctld.cpp HEADERS += wfmain.h \ commhandler.h \ @@ -122,7 +123,8 @@ HEADERS += wfmain.h \ resampler/arch.h \ resampler/resample_sse.h \ repeatersetup.h \ - repeaterattributes.h + repeaterattributes.h \ + rigctld.h FORMS += wfmain.ui \ diff --git a/wfview.vcxproj b/wfview.vcxproj index c5126dc..8263893 100644 --- a/wfview.vcxproj +++ b/wfview.vcxproj @@ -213,6 +213,7 @@ + @@ -241,6 +242,7 @@ + diff --git a/wfview.vcxproj.filters b/wfview.vcxproj.filters index a35b0ec..38f6294 100644 --- a/wfview.vcxproj.filters +++ b/wfview.vcxproj.filters @@ -114,6 +114,9 @@ Source Files + + Source Files + @@ -167,6 +170,9 @@ Header Files + + Header Files + @@ -341,6 +347,7 @@ +