From 49cb082c7ee1bca2f6721ac97512888358cf5b56 Mon Sep 17 00:00:00 2001 From: Jon Beniston Date: Tue, 8 Sep 2020 15:47:20 +0100 Subject: [PATCH] Add rigctrl plugin. plugins/misc/rigctrl - Add rigctrl plugin. sdrbase/plugin/pluginapi.h/.cpp - Add misc plugin registration. sdrbase/plugin/pluginmanager.h/.cpp - Add misc plugin registration. sdrbase/plugin/plugininterface.h/.cpp - Add top level UI and global settings serialization callbacks. sdrbase/settings/mainsettings.h/cpp - Allow plugins to save global settings in main settings file. sdrgui/mainwindow.cpp - Load settings after plugins are loaded, to allow plugin settings to be loaded and saved. sdrsrv/maincore.cpp - Support loading/saving of plugin settings. --- plugins/CMakeLists.txt | 1 + plugins/misc/CMakeLists.txt | 3 + plugins/misc/rigctrl/CMakeLists.txt | 52 +++ plugins/misc/rigctrl/readme.md | 59 +++ plugins/misc/rigctrl/rigctrl.cpp | 507 +++++++++++++++++++++++ plugins/misc/rigctrl/rigctrl.h | 67 +++ plugins/misc/rigctrl/rigctrlgui.cpp | 52 +++ plugins/misc/rigctrl/rigctrlgui.h | 45 ++ plugins/misc/rigctrl/rigctrlgui.ui | 178 ++++++++ plugins/misc/rigctrl/rigctrlplugin.cpp | 109 +++++ plugins/misc/rigctrl/rigctrlplugin.h | 53 +++ plugins/misc/rigctrl/rigctrlsettings.cpp | 98 +++++ plugins/misc/rigctrl/rigctrlsettings.h | 42 ++ sdrbase/plugin/pluginapi.cpp | 5 + sdrbase/plugin/pluginapi.h | 15 + sdrbase/plugin/plugininterface.h | 19 + sdrbase/plugin/pluginmanager.cpp | 9 + sdrbase/plugin/pluginmanager.h | 4 + sdrbase/settings/mainsettings.cpp | 35 +- sdrbase/settings/mainsettings.h | 6 +- sdrgui/mainwindow.cpp | 22 +- sdrsrv/maincore.cpp | 6 +- 22 files changed, 1373 insertions(+), 14 deletions(-) create mode 100644 plugins/misc/CMakeLists.txt create mode 100644 plugins/misc/rigctrl/CMakeLists.txt create mode 100644 plugins/misc/rigctrl/readme.md create mode 100644 plugins/misc/rigctrl/rigctrl.cpp create mode 100644 plugins/misc/rigctrl/rigctrl.h create mode 100644 plugins/misc/rigctrl/rigctrlgui.cpp create mode 100644 plugins/misc/rigctrl/rigctrlgui.h create mode 100644 plugins/misc/rigctrl/rigctrlgui.ui create mode 100644 plugins/misc/rigctrl/rigctrlplugin.cpp create mode 100644 plugins/misc/rigctrl/rigctrlplugin.h create mode 100644 plugins/misc/rigctrl/rigctrlsettings.cpp create mode 100644 plugins/misc/rigctrl/rigctrlsettings.h diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt index 0a39be051..fac3b679c 100644 --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -42,3 +42,4 @@ add_subdirectory(channelrx) add_subdirectory(channeltx) add_subdirectory(samplesource) add_subdirectory(samplesink) +add_subdirectory(misc) diff --git a/plugins/misc/CMakeLists.txt b/plugins/misc/CMakeLists.txt new file mode 100644 index 000000000..3577c0ad1 --- /dev/null +++ b/plugins/misc/CMakeLists.txt @@ -0,0 +1,3 @@ +project(misc) + +add_subdirectory(rigctrl) diff --git a/plugins/misc/rigctrl/CMakeLists.txt b/plugins/misc/rigctrl/CMakeLists.txt new file mode 100644 index 000000000..f803c68db --- /dev/null +++ b/plugins/misc/rigctrl/CMakeLists.txt @@ -0,0 +1,52 @@ +project(rigctrl) + +set(rigctrl_SOURCES + rigctrl.cpp + rigctrlsettings.cpp + rigctrlplugin.cpp +) + +set(rigctrl_HEADERS + rigctrl.h + rigctrlsettings.h + rigctrlplugin.h +) + +include_directories( + ${CMAKE_SOURCE_DIR}/swagger/sdrangel/code/qt5/client +) + +if(NOT SERVER_MODE) + set(rigctrl_SOURCES + ${rigctrl_SOURCES} + rigctrlgui.cpp + rigctrlgui.ui + ) + set(rigctrl_HEADERS + ${rigctrl_HEADERS} + rigctrlgui.h + ) + set(TARGET_NAME rigctrl) + set(TARGET_LIB "Qt5::Widgets") + set(TARGET_LIB_GUI "sdrgui") + set(INSTALL_FOLDER ${INSTALL_PLUGINS_DIR}) +else() + set(TARGET_NAME rigctrlsrv) + set(TARGET_LIB "") + set(TARGET_LIB_GUI "") + set(INSTALL_FOLDER ${INSTALL_PLUGINSSRV_DIR}) +endif() + +add_library(${TARGET_NAME} SHARED + ${rigctrl_SOURCES} +) + +target_link_libraries(${TARGET_NAME} + Qt5::Core + ${TARGET_LIB} + sdrbase + ${TARGET_LIB_GUI} + swagger +) + +install(TARGETS ${TARGET_NAME} DESTINATION ${INSTALL_FOLDER}) diff --git a/plugins/misc/rigctrl/readme.md b/plugins/misc/rigctrl/readme.md new file mode 100644 index 000000000..a8f578205 --- /dev/null +++ b/plugins/misc/rigctrl/readme.md @@ -0,0 +1,59 @@ +

Rigctrl plugin

+ +

Introduction

+ +The rigctrl plugin allows SDRangel to be controlled via ![Hamlib](http://hamlib.sourceforge.net/manuals/hamlib.html)'s rigctrld protocol. This allows other software that implements the rigctrld protocol, such at the satelite tracking software GPredict, to control SDRangel, to adjust for doppler or to automatically switch between different satellite frequencies and modes. + +

Interface

+ +

Enable rigctrl server

+ +Checking this option will enable the rigctrl server in SDRangel. The default is disabled. + +

API Address

+ +The rigctrl plugin using the SDRangel REST API to control SDRangel. Please specify the API address of the SDRangel instance to control. The default is http://127.0.0.1:8091. + +

Port

+ +The rigctrl plugin opens a TCP port to receive commands from a rigctrl client on. Please specify a free TCP port number. The default rigctrld port is 4532. + +

Max Frequency Offset

+ +The maximum frequency offset controls whether the center frequency or frequency offset is adjusted when a new frequency is received by a rigctrl command. +If the difference between the new frequency and the current center frequency is less than this value, the input offset (in the demodulator) will be adjusted. +If the difference is greater than this value, the center frequency will be set to the received frequency. +To only ever set the center frequency, set this value to 0. The default value is 10000. + +

Device Index

+ +The device index specifies the SDRangel device set that will be controlled by received rigctrl commands. Defaults to 0. + +

Channel Index

+ +The channel index specifies the SDRangel channel that will be controlled by received rigctrl commands. Defaults to 0. + +

Supported rigctrl Commands

+ +The following rigctrl commands are supported: + + + +

Example rigctrl Session

+ +Run SDRangel and from the Preferences menu select rigctrl. Check "Enable rigctrl server" and press OK. + +In a terminal window, run: + +
+telnet localhost 4532
+set_mode AM, 1000
+set_freq 100000000
+set_powerstat 1
+
diff --git a/plugins/misc/rigctrl/rigctrl.cpp b/plugins/misc/rigctrl/rigctrl.cpp new file mode 100644 index 000000000..8d3dc9759 --- /dev/null +++ b/plugins/misc/rigctrl/rigctrl.cpp @@ -0,0 +1,507 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "rigctrl.h" + +#include +#include +#include + +// Length of buffers +#define CMD_LENGTH 1024 +#define URL_LENGTH 1024 +#define RESPONSE_LENGTH 1024 +#define DATA_LENGTH 1024 + +// Hamlib rigctrl error codes +enum rig_errcode_e { + RIG_OK = 0, /*!< No error, operation completed successfully */ + RIG_EINVAL = -1, /*!< invalid parameter */ + RIG_ECONF = -2, /*!< invalid configuration (serial,..) */ + RIG_ENOMEM = -3, /*!< memory shortage */ + RIG_ENIMPL = -4, /*!< function not implemented, but will be */ + RIG_ETIMEOUT = -5, /*!< communication timed out */ + RIG_EIO = -6, /*!< IO error, including open failed */ + RIG_EINTERNAL = -7, /*!< Internal Hamlib error, huh! */ + RIG_EPROTO = -8, /*!< Protocol error */ + RIG_ERJCTED = -9, /*!< Command rejected by the rig */ + RIG_ETRUNC = -10, /*!< Command performed, but arg truncated */ + RIG_ENAVAIL = -11, /*!< function not available */ + RIG_ENTARGET = -12, /*!< VFO not targetable */ + RIG_BUSERROR = -13, /*!< Error talking on the bus */ + RIG_BUSBUSY = -14, /*!< Collision on the bus */ + RIG_EARG = -15, /*!< NULL RIG handle or any invalid pointer parameter in get arg */ + RIG_EVFO = -16, /*!< Invalid VFO */ + RIG_EDOM = -17 /*!< Argument out of domain of func */ +}; + +// Map rigctrl mode names to SDRangel modem names +const static struct mode_demod { + const char *mode; + const char *modem; +} mode_map[] = { + {"FM", "NFMDemod"}, + {"WFM", "WFMDemod"}, + {"AM", "AMDemod"}, + {"LSB", "SSBDemod"}, + {"USB", "SSBDemod"}, + {nullptr, nullptr} +}; + +// Get double value from within nested JSON object +static double getSubObjectDouble(QJsonObject &json, const QString &key) +{ + double value = -1.0; + + for (QJsonObject::const_iterator it = json.begin(); it != json.end(); it++) { + QJsonValue jsonValue = it.value(); + if (jsonValue.isObject()) { + QJsonObject settings = jsonValue.toObject(); + if (settings.contains(key)) { + value = settings[key].toDouble(); + return value; + } + } + } + + return value; +} + +// Set double value withing nested JSON object +static bool setSubObjectDouble(QJsonObject &json, const QString &key, double value) +{ + for (QJsonObject::iterator it = json.begin(); it != json.end(); it++) { + QJsonValue jsonValue = it.value(); + if (jsonValue.isObject()) { + QJsonObject settings = jsonValue.toObject(); + if (settings.contains(key)) { + settings[key] = value; + it.value() = settings; + return true; + } + } + } + return false; +} + +RigCtrl::RigCtrl() : + m_settings(), + m_state(idle), + m_tcpServer(nullptr), + m_clientConnection(nullptr) +{ + m_netman = new QNetworkAccessManager(this); + connect(m_netman, &QNetworkAccessManager::finished, this, &RigCtrl::processAPIResponse); + + setSettings(&m_settings); +} + +RigCtrl::~RigCtrl() +{ + if (m_clientConnection != nullptr) { + m_clientConnection->close(); + delete m_clientConnection; + } + if (m_tcpServer != nullptr) { + m_tcpServer->close(); + delete m_tcpServer; + } + delete m_netman; +} + +void RigCtrl::getSettings(RigCtrlSettings *settings) +{ + *settings = m_settings; +} + +// Is there a potential for this to be called while in getCommand or processAPIResponse? +void RigCtrl::setSettings(RigCtrlSettings *settings) +{ + bool disabled = (!settings->m_enabled) && m_settings.m_enabled; + bool enabled = settings->m_enabled && !m_settings.m_enabled; + bool portChanged = settings->m_rigCtrlPort != m_settings.m_rigCtrlPort; + + if (disabled || portChanged) { + if (m_clientConnection != nullptr) { + m_clientConnection->close(); + delete m_clientConnection; + m_clientConnection = nullptr; + } + if (m_tcpServer != nullptr) { + m_tcpServer->close(); + delete m_tcpServer; + m_tcpServer = nullptr; + } + } + + if (enabled || portChanged) { + qDebug() << "RigCtrl enabled on port " << settings->m_rigCtrlPort; + m_tcpServer = new QTcpServer(this); + if(!m_tcpServer->listen(QHostAddress::Any, settings->m_rigCtrlPort)) { + qDebug() << "RigCtrl failed to listen on port " << settings->m_rigCtrlPort << ". Check it is not already in use."; + } else { + connect(m_tcpServer, &QTcpServer::newConnection, this, &RigCtrl::acceptConnection); + } + } + + m_settings = *settings; +} + +QByteArray RigCtrl::serialize() const +{ + return m_settings.serialize(); +} + +bool RigCtrl::deserialize(const QByteArray& data) +{ + bool success = true; + RigCtrlSettings settings; + + if (!settings.deserialize(data)) { + success = false; + } else { + setSettings(&settings); + } + + return success; +} + +// Accept connection from rigctrl client +void RigCtrl::acceptConnection() +{ + m_clientConnection = m_tcpServer->nextPendingConnection(); + if (!m_clientConnection) { + return; + } + connect(m_clientConnection, &QIODevice::readyRead, this, &RigCtrl::getCommand); + connect(m_clientConnection, &QAbstractSocket::disconnected, m_clientConnection, &QObject::deleteLater); +} + +// Get rigctrl command and start processing it +void RigCtrl::getCommand() +{ + char cmd[CMD_LENGTH]; + char url[URL_LENGTH]; + char response[RESPONSE_LENGTH]; + qint64 len; + QNetworkRequest request; + char *p; + int i, l; + + // Get rigctrld command from client + len = m_clientConnection->readLine(cmd, sizeof(cmd)); + if (len != -1) { + //qDebug() << "RigCtrl::getCommand - " << cmd; + + if (!strncmp(cmd, "F ", 2) || !strncmp(cmd, "set_freq ", 9)) { + // Set frequency + m_targetFrequency = atof(cmd[0] == 'F' ? &cmd[2] : &cmd[9]); + // Get current centre frequency + sprintf(url, "%s/deviceset/%d/device/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + m_state = set_freq; + } else if (!strncmp(cmd, "f", 1) || !strncmp(cmd, "get_freq", 8)) { + // Get frequency - need to add centerFrequency and inputFrequencyOffset + sprintf(url, "%s/deviceset/%d/device/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + m_state = get_freq_center; + } else if (!strncmp(cmd, "M ?", 3) || !(strncmp(cmd, "set_mode ?", 10))) { + // Return list of modes supported + p = response; + for (i = 0; mode_map[i].mode != nullptr; i++) { + p += sprintf(p, "%s ", mode_map[i].mode); + } + p += sprintf(p, "\n"); + m_clientConnection->write(response, strlen(response)); + } else if (!strncmp(cmd, "M ", 2) || !(strncmp(cmd, "set_mode ", 9))) { + // Set mode + // Map rigctrl mode name to SDRangel modem name + m_targetModem = nullptr; + m_targetBW = -1; + p = cmd[0] == 'M' ? &cmd[2] : &cmd[9]; + for (i = 0; mode_map[i].mode != nullptr; i++) { + l = strlen(mode_map[i].mode); + if (!strncmp(p, mode_map[i].mode, l)) { + m_targetModem = mode_map[i].modem; + p += l; + break; + } + } + // Save bandwidth, if given + while(isspace(*p)) { + p++; + } + if (*p == ',') { + p++; + m_targetBW = atoi(p); + } + if (mode_map[i].modem != nullptr) { + // Delete current modem + sprintf(url, "%s/deviceset/%d/channel/%d", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex, m_settings.m_channelIndex); + request.setUrl(QUrl(url)); + m_netman->sendCustomRequest(request, "DELETE"); + m_state = set_mode_mod; + } else { + sprintf(response, "RPRT %d\n", RIG_EINVAL); + m_clientConnection->write(response, strlen(response)); + } + } else if (!strncmp(cmd, "set_powerstat 0", 15)) { + // Power off radio + sprintf(url, "%s/deviceset/%d/device/run", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->sendCustomRequest(request, "DELETE"); + m_state = set_power_off; + } else if (!strncmp(cmd, "set_powerstat 1", 15)) { + // Power on radio + sprintf(url, "%s/deviceset/%d/device/run", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->post(request, ""); + m_state = set_power_on; + } else if (!strncmp(cmd, "get_powerstat", 13)) { + // Return if powered on or off + sprintf(url, "%s/deviceset/%d/device/run", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + m_state = get_power; + } else { + // Unimplemented command + sprintf(response, "RPRT %d\n", RIG_ENIMPL); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + } + } +} + +// Process reply from SDRangel API server +void RigCtrl::processAPIResponse(QNetworkReply *reply) +{ + double freq; + char response[RESPONSE_LENGTH]; + char url[URL_LENGTH]; + char data[DATA_LENGTH]; + QNetworkRequest request; + + if (reply->error() == QNetworkReply::NoError) { + QString answer = reply->readAll(); + //qDebug("RigCtrl::processAPIResponse - '%s'", qUtf8Printable(answer)); + + QJsonDocument jsonResponse = QJsonDocument::fromJson(answer.toUtf8()); + QJsonObject jsonObj = jsonResponse.object(); + + switch (m_state) { + + case get_freq_center: + // Reply with current center frequency + freq = getSubObjectDouble(jsonObj, "centerFrequency"); + if (freq >= 0.0) { + m_targetFrequency = freq; + sprintf(url, "%s/deviceset/%d/channel/%d/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex, m_settings.m_channelIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + } else { + sprintf(response, "RPRT %d\n", RIG_ENIMPL); // File source doesn't have centre frequency + } + m_state = get_freq_offset; + break; + + case get_freq_offset: + freq = m_targetFrequency + getSubObjectDouble(jsonObj, "inputFrequencyOffset"); + sprintf(response, "%u\n", (unsigned)freq); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + + case set_freq: + // Check if target requency is within max offset from current center frequency + freq = getSubObjectDouble(jsonObj, "centerFrequency"); + if (fabs(freq - m_targetFrequency) > m_settings.m_maxFrequencyOffset) { + // Update centerFrequency + setSubObjectDouble(jsonObj, "centerFrequency", m_targetFrequency); + sprintf(url, "%s/deviceset/%d/device/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->sendCustomRequest(request, "PATCH", QJsonDocument(jsonObj).toJson()); + m_state = set_freq_center; + } else { + // In range, so update inputFrequencyOffset + m_targetOffset = m_targetFrequency - freq; + // Get settings containg inputFrequencyOffset, so we can patch them + sprintf(url, "%s/deviceset/%d/channel/%d/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex, m_settings.m_channelIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + m_state = set_freq_set_offset; + } + break; + + case set_freq_no_offset: + // Update centerFrequency, without trying to set offset + setSubObjectDouble(jsonObj, "centerFrequency", m_targetFrequency); + sprintf(url, "%s/deviceset/%d/device/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->sendCustomRequest(request, "PATCH", QJsonDocument(jsonObj).toJson()); + m_state = set_freq_center_no_offset; + break; + + case set_freq_center: + // Check whether frequency was set as expected + freq = getSubObjectDouble(jsonObj, "centerFrequency"); + if (freq == m_targetFrequency) { + // Set inputFrequencyOffset to 0 + m_targetOffset = 0; + sprintf(url, "%s/deviceset/%d/channel/%d/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex, m_settings.m_channelIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + m_state = set_freq_set_offset; + } else { + sprintf(response, "RPRT %d\n", RIG_EINVAL); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + } + break; + + case set_freq_center_no_offset: + // Check whether frequency was set as expected + freq = getSubObjectDouble(jsonObj, "centerFrequency"); + if (freq == m_targetFrequency) { + sprintf(response, "RPRT 0\n"); + } else { + sprintf(response, "RPRT %d\n", RIG_EINVAL); + } + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + + case set_freq_set_offset: + // Patch inputFrequencyOffset + if (setSubObjectDouble(jsonObj, "inputFrequencyOffset", m_targetOffset)) { + sprintf(url, "%s/deviceset/%d/channel/%d/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex, m_settings.m_channelIndex); + request.setUrl(QUrl(url)); + m_netman->sendCustomRequest(request, "PATCH", QJsonDocument(jsonObj).toJson()); + m_state = set_freq_offset; + } else { + // No inputFrequencyOffset + sprintf(response, "RPRT %d\n", RIG_EINVAL); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + } + break; + + case set_freq_offset: + freq = getSubObjectDouble(jsonObj, "inputFrequencyOffset"); + if (freq == m_targetOffset) { + sprintf(response, "RPRT 0\n"); + } else { + sprintf(response, "RPRT %d\n", RIG_EINVAL); + } + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + + case set_mode_mod: + // Create new modem + sprintf(url, "%s/deviceset/%d/channel", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + sprintf(data, "{ \"channelType\": \"%s\", \"direction\": 0, \"originatorDeviceSetIndex\": %d}\n", m_targetModem, m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); + m_netman->post(request, data); + if (m_targetBW >= 0) { + m_state = set_mode_settings; + } else { + m_state = set_mode_reply; + } + break; + + case set_mode_settings: + // Set modem bandwidth + sprintf(url, "%s/deviceset/%d/channel/%d/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex, m_settings.m_channelIndex); + sprintf(data, "{ \"channelType\": \"%s\", \"%sSettings\": {\"rfBandwidth\":%d}}\n", m_targetModem, m_targetModem, m_targetBW); + request.setUrl(QUrl(url)); + request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); + m_netman->sendCustomRequest(request, "PATCH", data); + m_state = set_mode_reply; + break; + + case set_mode_reply: + sprintf(response, "RPRT 0\n"); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + + case get_power: + if (!jsonObj["state"].toString().compare("running")) { + sprintf(response, "1\n"); + } else { + sprintf(response, "0\n"); + } + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + + case set_power_on: + case set_power_off: + // Reply contains previous state, not current state, so assume it worked + sprintf(response, "RPRT 0\n"); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + } + + } else { + //qDebug("RigCtrl::processAPIResponse - got error: '%s'", qUtf8Printable(reply->errorString())); + //qDebug() << reply->readAll(); + + switch (m_state) { + case get_freq_offset: + // Probably no modem enabled on the specified channel + sprintf(response, "%u\n", (unsigned)m_targetFrequency); + m_clientConnection->write(response, strlen(response)); + m_state = idle; + break; + + case set_freq_set_offset: + // Probably no demodulator enabled on the specified channel + // Just set as center frequency + sprintf(url, "%s/deviceset/%d/device/settings", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + m_netman->get(request); + m_state = set_freq_no_offset; + break; + + case set_mode_mod: + // Probably no modem on channel to delete, so continue to try to create one + sprintf(url, "%s/deviceset/%d/channel", qUtf8Printable(m_settings.m_APIAddress), m_settings.m_deviceIndex); + sprintf(data, "{ \"channelType\": \"%s\", \"direction\": 0, \"originatorDeviceSetIndex\": %d}\n", m_targetModem, m_settings.m_deviceIndex); + request.setUrl(QUrl(url)); + request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json"); + m_netman->post(request, data); + if (m_targetBW >= 0) { + m_state = set_mode_settings; + } else { + m_state = set_mode_reply; + } + break; + + default: + sprintf(response, "RPRT %d\n", RIG_EIO); + m_clientConnection->write(response, strlen(response)); + break; + } + } + + reply->deleteLater(); +} diff --git a/plugins/misc/rigctrl/rigctrl.h b/plugins/misc/rigctrl/rigctrl.h new file mode 100644 index 000000000..38b61f88a --- /dev/null +++ b/plugins/misc/rigctrl/rigctrl.h @@ -0,0 +1,67 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_RIGCTRL_H +#define INCLUDE_RIGCTRL_H + +#include +#include + +#include "rigctrlsettings.h" + +class RigCtrl : public QObject { + Q_OBJECT + +public: + + RigCtrl(); + ~RigCtrl(); + void getSettings(RigCtrlSettings *settings); + void setSettings(RigCtrlSettings *settings); + QByteArray serialize() const; + bool deserialize(const QByteArray& data); + +private slots: + void acceptConnection(); + void getCommand(); + void processAPIResponse(QNetworkReply *reply); + +protected: + QTcpServer *m_tcpServer; + QTcpSocket *m_clientConnection; + QNetworkAccessManager *m_netman; + + enum RigCtrlState + { + idle, + set_freq, set_freq_no_offset, set_freq_center, set_freq_center_no_offset, set_freq_set_offset, set_freq_offset, + get_freq_center, get_freq_offset, + set_mode_mod, set_mode_settings, set_mode_reply, + get_power, + set_power_on, set_power_off + }; + RigCtrlState m_state; + + double m_targetFrequency; + double m_targetOffset; + const char *m_targetModem; + int m_targetBW; + + RigCtrlSettings m_settings; +}; + +#endif // INCLUDE_RIGCTRL_H diff --git a/plugins/misc/rigctrl/rigctrlgui.cpp b/plugins/misc/rigctrl/rigctrlgui.cpp new file mode 100644 index 000000000..da2aa38ad --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlgui.cpp @@ -0,0 +1,52 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "rigctrlgui.h" +#include "ui_rigctrlgui.h" +#include + +RigCtrlGUI::RigCtrlGUI(RigCtrl *rigCtrl, QWidget* parent) : + m_rigCtrl(rigCtrl), + QDialog(parent), + ui(new Ui::RigCtrlGUI) +{ + ui->setupUi(this); + m_rigCtrl->getSettings(&m_settings); + ui->enable->setChecked(m_settings.m_enabled); + ui->api->setText(QString(m_settings.m_APIAddress)); + ui->rigCtrlPort->setValue(m_settings.m_rigCtrlPort); + ui->maxFrequencyOffset->setValue(m_settings.m_maxFrequencyOffset); + ui->deviceIndex->setValue(m_settings.m_deviceIndex); + ui->channelIndex->setValue(m_settings.m_channelIndex); +} + +RigCtrlGUI::~RigCtrlGUI() +{ + delete ui; +} + +void RigCtrlGUI::accept() +{ + m_settings.m_enabled = ui->enable->isChecked(); + m_settings.m_APIAddress = ui->api->text(); + m_settings.m_rigCtrlPort = ui->rigCtrlPort->value(); + m_settings.m_maxFrequencyOffset = ui->maxFrequencyOffset->value(); + m_settings.m_deviceIndex = ui->deviceIndex->value(); + m_settings.m_channelIndex = ui->channelIndex->value(); + m_rigCtrl->setSettings(&m_settings); + QDialog::accept(); +} diff --git a/plugins/misc/rigctrl/rigctrlgui.h b/plugins/misc/rigctrl/rigctrlgui.h new file mode 100644 index 000000000..1d53c264b --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlgui.h @@ -0,0 +1,45 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_RIGCTRLGUI_H +#define INCLUDE_RIGCTRLGUI_H + +#include +#include "rigctrlsettings.h" +#include "rigctrl.h" + +namespace Ui { + class RigCtrlGUI; +} + +class RigCtrlGUI : public QDialog { + Q_OBJECT + +public: + explicit RigCtrlGUI(RigCtrl *rigCtrl, QWidget *parent = 0); + ~RigCtrlGUI(); + +private slots: + void accept(); + +private: + Ui::RigCtrlGUI* ui; + RigCtrlSettings m_settings; + RigCtrl *m_rigCtrl; +}; + +#endif // INCLUDE_RIGCTRLGUI_H diff --git a/plugins/misc/rigctrl/rigctrlgui.ui b/plugins/misc/rigctrl/rigctrlgui.ui new file mode 100644 index 000000000..b5c83afd7 --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlgui.ui @@ -0,0 +1,178 @@ + + + RigCtrlGUI + + + + 0 + 0 + 351 + 235 + + + + + Liberation Sans + 9 + + + + rigctrl Preferences + + + + + + + + + Select to enable rigctrl server. + + + Enable rigctrl server + + + + + + + rigctrl Port + + + + + + + Device Index + + + + + + + Index of the device that should be controlled by rigctrl commands. +Default is 0. + + + + + + + Index of the channel that is to be controlled by rigctrl commands. +Default is 0. + + + + + + + Channel Index + + + + + + + API Address + + + + + + + URL of SDRangel API server to control. +Default is http://127.0.0.1:8091/sdrangel + + + http://127.0.0.1:8091/sdrangel + + + + + + + Max Frequency Offset + + + + + + + Controls whether the center frequency or frequency offset is adjusted when a new frequency is received via a rigctrl command. +If the difference between the new frequency and the current center frequency is less than this value, the offset will be adjusted. If it is greater than this value, the center frequency will be set to the new frequency. +To only ever set the center frequency, set this value to 0. +Default is 10000. + + + 9999999 + + + + + + + TCP port to listen for rigctrl commands on. +Default is 4532. + + + 1024 + + + 65536 + + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + buttonBox + + + + + buttonBox + accepted() + RigCtrlGUI + accept() + + + 257 + 194 + + + 157 + 203 + + + + + buttonBox + rejected() + RigCtrlGUI + reject() + + + 314 + 194 + + + 286 + 203 + + + + + diff --git a/plugins/misc/rigctrl/rigctrlplugin.cpp b/plugins/misc/rigctrl/rigctrlplugin.cpp new file mode 100644 index 000000000..ca29475ac --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlplugin.cpp @@ -0,0 +1,109 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include +#include +#include +#include +#include +#include + +#include "plugin/pluginapi.h" +#include "util/simpleserializer.h" + +#include "rigctrl.h" +#ifdef SERVER_MODE +#else +#include "rigctrlgui.h" +#endif +#include "rigctrlplugin.h" + +const PluginDescriptor RigCtrlPlugin::m_pluginDescriptor = { + QString("rigctrl"), + QString("rigctrl Server"), + QString("4.15.5"), + QString("(c) Jon Beniston, M7RCE"), + QString("https://github.com/f4exb/sdrangel"), + true, + QString("https://github.com/srcejon/sdrangel/tree/rigctrl") +}; + +RigCtrlPlugin::RigCtrlPlugin(RigCtrl *rigCtrl, QObject* parent) : + m_rigCtrl(rigCtrl), + QObject(parent) +{ +} + +const PluginDescriptor& RigCtrlPlugin::getPluginDescriptor() const +{ + return m_pluginDescriptor; +} + +void RigCtrlPlugin::initPlugin(PluginAPI* pluginAPI) +{ + m_rigCtrl = new RigCtrl(); + pluginAPI->registerMiscPlugin(RIGCTRL_DEVICE_TYPE_ID, this); +} + + +#ifdef SERVER_MODE +bool RigCtrlPlugin::createTopLevelGUI( + QMainWindow* mainWindow + ) +{ + return true; +} +#else +bool RigCtrlPlugin::createTopLevelGUI( + QMainWindow* mainWindow + ) +{ + m_mainWindow = mainWindow; + QMenuBar *menuBar = mainWindow->menuBar(); + + QMenu *prefMenu = menuBar->findChild("menuPreferences", Qt::FindDirectChildrenOnly); + if (prefMenu == nullptr) { + qDebug() << "RigCtrlPlugin::createTopLevelGUI - Failed to find Preferences menu"; + return false; + } + + QAction *prefAction; + prefAction = new QAction(tr("rigctrl"), this); + prefAction->setStatusTip(tr("Display preferences for rigctrl")); + prefMenu->addAction(prefAction); + connect(prefAction, &QAction::triggered, this, &RigCtrlPlugin::showRigCtrlUI); + + return true; +} + +void RigCtrlPlugin::showRigCtrlUI() +{ + RigCtrlGUI dlg(m_rigCtrl, m_mainWindow); + dlg.exec(); +} + +#endif + +QByteArray RigCtrlPlugin::serializeGlobalSettings() const +{ + return m_rigCtrl->serialize(); +} + +bool RigCtrlPlugin::deserializeGlobalSettings(const QByteArray& data) +{ + return m_rigCtrl->deserialize(data); +} diff --git a/plugins/misc/rigctrl/rigctrlplugin.h b/plugins/misc/rigctrl/rigctrlplugin.h new file mode 100644 index 000000000..bfe4237b3 --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlplugin.h @@ -0,0 +1,53 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_RIGCTRLPLUGIN_H +#define INCLUDE_RIGCTRLPLUGIN_H + +#include +#include "plugin/plugininterface.h" +#include "rigctrl.h" + +class PluginAPI; + +#define RIGCTRL_DEVICE_TYPE_ID "sdrangel.misc.rigctrl" + +class RigCtrlPlugin : public QObject, public PluginInterface { + Q_OBJECT + Q_INTERFACES(PluginInterface) + Q_PLUGIN_METADATA(IID RIGCTRL_DEVICE_TYPE_ID) + +public: + explicit RigCtrlPlugin(RigCtrl *rigCtrl = NULL, QObject* parent = NULL); + + const PluginDescriptor& getPluginDescriptor() const; + void initPlugin(PluginAPI* pluginAPI); + + virtual bool createTopLevelGUI(QMainWindow* mainWindow); + virtual QByteArray serializeGlobalSettings() const; + virtual bool deserializeGlobalSettings(const QByteArray& data); + +private slots: + void showRigCtrlUI(); + +private: + static const PluginDescriptor m_pluginDescriptor; + QMainWindow* m_mainWindow; + RigCtrl *m_rigCtrl; +}; + +#endif // INCLUDE_RIGCTRLPLUGIN_H diff --git a/plugins/misc/rigctrl/rigctrlsettings.cpp b/plugins/misc/rigctrl/rigctrlsettings.cpp new file mode 100644 index 000000000..e9dc48d04 --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlsettings.cpp @@ -0,0 +1,98 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#include "rigctrlsettings.h" + +#include +#include +#include "util/simpleserializer.h" + +#define ENABLED_DEFAULT false +#define API_ADDRESS_DEFAULT "http://127.0.0.1:8091/sdrangel" +#define RIG_CTRL_PORT_DEFAULT 4532 +#define MAX_FREQUENCY_OFFSET_DEFAULT 10000 +#define DEVICE_INDEX_DEFAULT 0 +#define CHANNEL_INDEX_DEFAULT 0 + +void RigCtrlSettings::resetToDefaults() +{ + m_enabled = ENABLED_DEFAULT; + m_APIAddress = API_ADDRESS_DEFAULT; + m_rigCtrlPort = RIG_CTRL_PORT_DEFAULT; + m_maxFrequencyOffset = MAX_FREQUENCY_OFFSET_DEFAULT; + m_deviceIndex = DEVICE_INDEX_DEFAULT; + m_channelIndex = CHANNEL_INDEX_DEFAULT; +} + +QByteArray RigCtrlSettings::serialize() const +{ + SimpleSerializer s(1); + + s.writeBool(1, m_enabled); + s.writeString(2, m_APIAddress); + s.writeS32(3, m_rigCtrlPort); + s.writeS32(4, m_maxFrequencyOffset); + s.writeS32(5, m_deviceIndex); + s.writeS32(6, m_channelIndex); + + return s.final(); +} + +bool RigCtrlSettings::deserialize(const QByteArray& data) +{ + SimpleDeserializer d(data); + + if (!d.isValid()) + { + resetToDefaults(); + return false; + } + + if (d.getVersion() == 1) + { + d.readBool(1, &m_enabled, ENABLED_DEFAULT); + d.readString(2, &m_APIAddress, API_ADDRESS_DEFAULT); + d.readS32(3, &m_rigCtrlPort, RIG_CTRL_PORT_DEFAULT); + d.readS32(4, &m_maxFrequencyOffset, MAX_FREQUENCY_OFFSET_DEFAULT); + d.readS32(5, &m_deviceIndex, DEVICE_INDEX_DEFAULT); + d.readS32(6, &m_channelIndex, CHANNEL_INDEX_DEFAULT); + + if (!((m_rigCtrlPort > 1023) && (m_rigCtrlPort < 65536))) { + qDebug() << "RigCtrlSettings::deserialize invalid port number ignored"; + m_rigCtrlPort = RIG_CTRL_PORT_DEFAULT; + } + if (m_maxFrequencyOffset < 0) { + qDebug() << "RigCtrlSettings::deserialize invalid max frequency offset ignored"; + m_maxFrequencyOffset = MAX_FREQUENCY_OFFSET_DEFAULT; + } + if (m_deviceIndex < 0) { + qDebug() << "RigCtrlSettings::deserialize invalid device index ignored"; + m_deviceIndex = DEVICE_INDEX_DEFAULT; + } + if (m_channelIndex < 0) { + qDebug() << "RigCtrlSettings::deserialize invalid channel index ignored"; + m_deviceIndex = CHANNEL_INDEX_DEFAULT; + } + + return true; + } + else + { + resetToDefaults(); + return false; + } +} diff --git a/plugins/misc/rigctrl/rigctrlsettings.h b/plugins/misc/rigctrl/rigctrlsettings.h new file mode 100644 index 000000000..3784a4c81 --- /dev/null +++ b/plugins/misc/rigctrl/rigctrlsettings.h @@ -0,0 +1,42 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Jon Beniston, M7RCE // +// // +// This program is free software; you can redistribute it and/or modify // +// it under the terms of the GNU General Public License as published by // +// the Free Software Foundation as version 3 of the License, or // +// (at your option) any later version. // +// // +// This program is distributed in the hope that it will be useful, // +// but WITHOUT ANY WARRANTY; without even the implied warranty of // +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // +// GNU General Public License V3 for more details. // +// // +// You should have received a copy of the GNU General Public License // +// along with this program. If not, see . // +/////////////////////////////////////////////////////////////////////////////////// + +#ifndef INCLUDE_RIGCTRLSETTINGS_H +#define INCLUDE_RIGCTRLSETTINGS_H + +#include +#include + +struct RigCtrlSettings { + + bool m_enabled; + QString m_APIAddress; + int m_rigCtrlPort; + int m_maxFrequencyOffset; + int m_deviceIndex; + int m_channelIndex; + + RigCtrlSettings() + { + resetToDefaults(); + }; + void resetToDefaults(); + QByteArray serialize() const; + bool deserialize(const QByteArray& data); +}; + +#endif /* INCLUDE_RIGCTRLSETTINGS_H */ diff --git a/sdrbase/plugin/pluginapi.cpp b/sdrbase/plugin/pluginapi.cpp index 5a939b1c9..0ccbe81e8 100644 --- a/sdrbase/plugin/pluginapi.cpp +++ b/sdrbase/plugin/pluginapi.cpp @@ -46,6 +46,11 @@ void PluginAPI::registerSampleMIMO(const QString& mimoName, PluginInterface* plu m_pluginManager->registerSampleMIMO(mimoName, plugin); } +void PluginAPI::registerMiscPlugin(const QString& id, PluginInterface* plugin) +{ + m_pluginManager->registerMiscPlugin(id, plugin); +} + PluginAPI::PluginAPI(PluginManager* pluginManager) : m_pluginManager(pluginManager) { diff --git a/sdrbase/plugin/pluginapi.h b/sdrbase/plugin/pluginapi.h index 78f0c71ec..8c61a5823 100644 --- a/sdrbase/plugin/pluginapi.h +++ b/sdrbase/plugin/pluginapi.h @@ -45,6 +45,18 @@ public: typedef QList ChannelRegistrations; + struct MiscPluginRegistration + { + QString m_id; + PluginInterface* m_plugin; + MiscPluginRegistration(const QString& id, PluginInterface* plugin) : + m_id(id), + m_plugin(plugin) + { } + }; + + typedef QList MiscPluginRegistrations; + // Rx Channel stuff void registerRxChannel(const QString& channelIdURI, const QString& channelId, PluginInterface* plugin); ChannelRegistrations *getRxChannelRegistrations(); @@ -66,6 +78,9 @@ public: // Sample MIMO stuff void registerSampleMIMO(const QString& sinkName, PluginInterface* plugin); + // Access to top-level GUI and main settings. id should be unique among plugins. + void registerMiscPlugin(const QString& id, PluginInterface* plugin); + protected: PluginManager* m_pluginManager; diff --git a/sdrbase/plugin/plugininterface.h b/sdrbase/plugin/plugininterface.h index a3f4d3a0c..243e421f1 100644 --- a/sdrbase/plugin/plugininterface.h +++ b/sdrbase/plugin/plugininterface.h @@ -3,6 +3,7 @@ #include #include +#include #include "export.h" @@ -311,6 +312,24 @@ public: virtual void deleteSampleMIMOPluginInstanceGUI(PluginInstanceGUI *ui); virtual void deleteSampleMIMOPluginInstanceMIMO(DeviceSampleMIMO *mimo); + // Callback to allow plugin to add elements to top-level GUI (such as menu items) + virtual bool createTopLevelGUI(QMainWindow* mainWindow) + { + return true; + } + + // Serialise global plugin settings (i.e. settings that are not per-instance) + virtual QByteArray serializeGlobalSettings() const + { + QByteArray empty; + return empty; + } + + virtual bool deserializeGlobalSettings(const QByteArray& data) + { + return true; + } + // all devices virtual DeviceWebAPIAdapter* createDeviceWebAPIAdapter() const { diff --git a/sdrbase/plugin/pluginmanager.cpp b/sdrbase/plugin/pluginmanager.cpp index cd67a176e..44e623155 100644 --- a/sdrbase/plugin/pluginmanager.cpp +++ b/sdrbase/plugin/pluginmanager.cpp @@ -189,6 +189,15 @@ void PluginManager::registerSampleMIMO(const QString& mimoName, PluginInterface* )); } +void PluginManager::registerMiscPlugin(const QString& id, PluginInterface* plugin) +{ + qDebug() << "PluginManager::registerMiscPlugin " + << plugin->getPluginDescriptor().displayedName.toStdString().c_str() + << " with id " << id.toStdString().c_str(); + + m_miscPluginRegistrations.append(PluginAPI::MiscPluginRegistration(id, plugin)); +} + void PluginManager::loadPluginsDir(const QDir& dir) { QDir pluginsDir(dir); diff --git a/sdrbase/plugin/pluginmanager.h b/sdrbase/plugin/pluginmanager.h index fbe7c8da7..c34ae7df8 100644 --- a/sdrbase/plugin/pluginmanager.h +++ b/sdrbase/plugin/pluginmanager.h @@ -70,6 +70,7 @@ public: void registerSampleSource(const QString& sourceName, PluginInterface* plugin); void registerSampleSink(const QString& sinkName, PluginInterface* plugin); void registerSampleMIMO(const QString& mimoName, PluginInterface* plugin); + void registerMiscPlugin(const QString& id, PluginInterface* plugin); PluginAPI::SamplingDeviceRegistrations& getSourceDeviceRegistrations() { return m_sampleSourceRegistrations; } PluginAPI::SamplingDeviceRegistrations& getSinkDeviceRegistrations() { return m_sampleSinkRegistrations; } @@ -77,6 +78,7 @@ public: PluginAPI::ChannelRegistrations *getRxChannelRegistrations() { return &m_rxChannelRegistrations; } PluginAPI::ChannelRegistrations *getTxChannelRegistrations() { return &m_txChannelRegistrations; } PluginAPI::ChannelRegistrations *getMIMOChannelRegistrations() { return &m_mimoChannelRegistrations; } + PluginAPI::MiscPluginRegistrations *getMiscPluginRegistrations() { return &m_miscPluginRegistrations; } void createRxChannelInstance(int channelPluginIndex, DeviceUISet *deviceUISet, DeviceAPI *deviceAPI); void listRxChannels(QList& list); @@ -130,6 +132,8 @@ private: PluginAPI::SamplingDeviceRegistrations m_sampleSinkRegistrations; //!< Output sink plugins (one per device kind) register here PluginAPI::SamplingDeviceRegistrations m_sampleMIMORegistrations; //!< MIMO sink plugins (one per device kind) register here + PluginAPI::MiscPluginRegistrations m_miscPluginRegistrations; // #include +#include #include @@ -41,7 +42,7 @@ int MainSettings::getFileFormat() const return (int) s.format(); } -void MainSettings::load() +void MainSettings::load(PluginManager *pluginManager) { QSettings s; @@ -92,13 +93,33 @@ void MainSettings::load() s.endGroup(); } + else if (groups[i].startsWith("plugin-")) + { + int j; + + // Find plugin with matching ID + PluginAPI::MiscPluginRegistrations *miscPluginRegistrations = pluginManager->getMiscPluginRegistrations(); + for (j = 0; j < miscPluginRegistrations->count(); j++) + { + if (groups[i].indexOf((*miscPluginRegistrations)[j].m_id) == 7) + { + s.beginGroup(groups[i]); + (*miscPluginRegistrations)[j].m_plugin->deserializeGlobalSettings(qUncompress(QByteArray::fromBase64(s.value("data").toByteArray()))); + s.endGroup(); + break; + } + } + if (j == miscPluginRegistrations->count()) { + qDebug() << "Failed to find plugin to deserialize for " << groups[i]; + } + } } m_hardwareDeviceUserArgs.deserialize(qUncompress(QByteArray::fromBase64(s.value("hwDeviceUserArgs").toByteArray()))); m_limeRFEUSBCalib.deserialize(qUncompress(QByteArray::fromBase64(s.value("limeRFEUSBCalib").toByteArray()))); } -void MainSettings::save() const +void MainSettings::save(PluginManager *pluginManager) const { QSettings s; @@ -141,6 +162,16 @@ void MainSettings::save() const s.setValue("hwDeviceUserArgs", qCompress(m_hardwareDeviceUserArgs.serialize()).toBase64()); s.setValue("limeRFEUSBCalib", qCompress(m_limeRFEUSBCalib.serialize()).toBase64()); + + // Plugin global settings + PluginAPI::MiscPluginRegistrations *miscPluginRegistrations = pluginManager->getMiscPluginRegistrations(); + for (int i = 0; i < miscPluginRegistrations->count(); i++) + { + QString group = QString("plugin-%1").arg((*miscPluginRegistrations)[i].m_id); + s.beginGroup(group); + s.setValue("data", qCompress((*miscPluginRegistrations)[i].m_plugin->serializeGlobalSettings()).toBase64()); + s.endGroup(); + } } void MainSettings::initialize() diff --git a/sdrbase/settings/mainsettings.h b/sdrbase/settings/mainsettings.h index 3ebfac9be..5090ba5ed 100644 --- a/sdrbase/settings/mainsettings.h +++ b/sdrbase/settings/mainsettings.h @@ -7,18 +7,20 @@ #include "preferences.h" #include "preset.h" #include "export.h" +#include "plugin/pluginmanager.h" class Command; class AudioDeviceManager; class AMBEEngine; + class SDRBASE_API MainSettings { public: MainSettings(); ~MainSettings(); - void load(); - void save() const; + void load(PluginManager *pluginManager); + void save(PluginManager *pluginManager) const; void resetToDefaults(); void initialize(); diff --git a/sdrgui/mainwindow.cpp b/sdrgui/mainwindow.cpp index dc77a1e00..adee01c88 100644 --- a/sdrgui/mainwindow.cpp +++ b/sdrgui/mainwindow.cpp @@ -195,8 +195,6 @@ MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parse splash->showStatusMessage("load settings...", Qt::white); qDebug() << "MainWindow::MainWindow: load settings..."; - loadSettings(); - splash->showStatusMessage("load plugins...", Qt::white); qDebug() << "MainWindow::MainWindow: load plugins..."; @@ -204,6 +202,9 @@ MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parse m_pluginManager->loadPlugins(QString("plugins")); m_pluginManager->loadPluginsNonDiscoverable(m_settings.getDeviceUserArgs()); + // Load settings after plugins have loaded, as they can include misc plugin settings + loadSettings(); + splash->showStatusMessage("load file input...", Qt::white); qDebug() << "MainWindow::MainWindow: select SampleSource from settings or default (file input)..."; @@ -261,6 +262,13 @@ MainWindow::MainWindow(qtwebapp::LoggerWithFile *logger, const MainParser& parse delete splash; + // Allow plugins to create top-level GUI elements + PluginAPI::MiscPluginRegistrations *miscPluginRegistrations = m_pluginManager->getMiscPluginRegistrations(); + for (int i = 0; i < miscPluginRegistrations->count(); i++) + { + (*miscPluginRegistrations)[i].m_plugin->createTopLevelGUI(this); + } + qDebug() << "MainWindow::MainWindow: end"; } @@ -635,7 +643,7 @@ void MainWindow::loadSettings() { qDebug() << "MainWindow::loadSettings"; - m_settings.load(); + m_settings.load(m_pluginManager); m_settings.sortPresets(); int middleIndex = m_settings.getPresetCount() / 2; QTreeWidgetItem *treeItem; @@ -752,7 +760,7 @@ void MainWindow::closeEvent(QCloseEvent *closeEvent) qDebug("MainWindow::closeEvent"); savePresetSettings(m_settings.getWorkingPreset(), 0); - m_settings.save(); + m_settings.save(m_pluginManager); while (m_deviceUIs.size() > 0) { @@ -895,7 +903,7 @@ bool MainWindow::handleMessage(const Message& cmd) savePresetSettings(notif.getPreset(), notif.getDeviceSetIndex()); if (notif.isNewPreset()) { ui->presetTree->setCurrentItem(addPresetToTree(notif.getPreset())); } m_settings.sortPresets(); - m_settings.save(); + m_settings.save(m_pluginManager); return true; } else if (MsgDeletePreset::match(cmd)) @@ -1232,7 +1240,7 @@ void MainWindow::on_commandOutput_clicked() void MainWindow::on_commandsSave_clicked() { saveCommandSettings(); - m_settings.save(); + m_settings.save(m_pluginManager); } void MainWindow::commandKeysConnect(QObject *object, const char *slot) @@ -1489,7 +1497,7 @@ void MainWindow::on_presetImport_clicked() void MainWindow::on_settingsSave_clicked() { savePresetSettings(m_settings.getWorkingPreset(), ui->tabInputsView->currentIndex()); - m_settings.save(); + m_settings.save(m_pluginManager); } void MainWindow::on_presetLoad_clicked() diff --git a/sdrsrv/maincore.cpp b/sdrsrv/maincore.cpp index d290979db..df0423925 100644 --- a/sdrsrv/maincore.cpp +++ b/sdrsrv/maincore.cpp @@ -97,7 +97,7 @@ MainCore::~MainCore() } m_apiServer->stop(); - m_settings.save(); + m_settings.save(m_pluginManager); delete m_apiServer; delete m_requestMapper; delete m_apiAdapter; @@ -131,7 +131,7 @@ bool MainCore::handleMessage(const Message& cmd) MsgSavePreset& notif = (MsgSavePreset&) cmd; savePresetSettings(notif.getPreset(), notif.getDeviceSetIndex()); m_settings.sortPresets(); - m_settings.save(); + m_settings.save(m_pluginManager); return true; } else if (MsgDeletePreset::match(cmd)) @@ -215,7 +215,7 @@ void MainCore::loadSettings() { qDebug() << "MainCore::loadSettings"; - m_settings.load(); + m_settings.load(m_pluginManager); m_settings.sortPresets(); setLoggingOptions(); }