From d989561df56211af7e95cc69b0c2b259f0e827b1 Mon Sep 17 00:00:00 2001 From: srcejon Date: Tue, 5 Dec 2023 12:32:53 +0000 Subject: [PATCH] Use common code for creating and processing device and channel Ids. --- plugins/channelrx/demodadsb/adsbdemodgui.cpp | 23 +--- plugins/channelrx/freqscanner/freqscanner.cpp | 24 +--- plugins/channelrx/freqscanner/freqscanner.h | 4 +- .../channelrx/freqscanner/freqscannergui.cpp | 12 +- .../radioastronomy/radioastronomy.cpp | 12 +- .../channelrx/radioastronomy/radioastronomy.h | 8 +- .../radioastronomy/radioastronomygui.cpp | 16 +-- .../feature/startracker/startrackerworker.cpp | 7 +- sdrbase/maincore.cpp | 127 ++++++++++++++++++ sdrbase/maincore.h | 13 +- 10 files changed, 173 insertions(+), 73 deletions(-) diff --git a/plugins/channelrx/demodadsb/adsbdemodgui.cpp b/plugins/channelrx/demodadsb/adsbdemodgui.cpp index d71460a7f..0388c84f8 100644 --- a/plugins/channelrx/demodadsb/adsbdemodgui.cpp +++ b/plugins/channelrx/demodadsb/adsbdemodgui.cpp @@ -577,12 +577,7 @@ void AircraftModel::findOnMap(int index) // Get list of frequeny scanners to use in menu QStringList AirportModel::getFreqScanners() const { - QStringList list; - std::vector channels = MainCore::instance()->getChannels("sdrangel.channel.freqscanner"); - for (const auto channel : channels) { - list.append(QString("R%1:%2").arg(channel->getDeviceSetIndex()).arg(channel->getIndexInDeviceSet())); - } - return list; + return MainCore::instance()->getChannelIds("sdrangel.channel.freqscanner"); } // Send airport frequencies to frequency scanner with given id (Rn:n) @@ -592,14 +587,10 @@ void AirportModel::sendToFreqScanner(int index, const QString& id) return; } const AirportInformation *airport = m_airports[index]; + unsigned int deviceSet, channelIndex; - const QRegularExpression re("R([0-9]+):([0-9]+)"); - QRegularExpressionMatch match = re.match(id); - if (match.hasMatch()) + if (MainCore::getDeviceAndChannelIndexFromId(id, deviceSet, channelIndex)) { - int deviceSet = match.capturedTexts()[1].toInt(); - int channelIndex = match.capturedTexts()[2].toInt(); - QJsonArray array; for (const auto airportFrequency : airport->m_frequencies) { @@ -867,12 +858,10 @@ bool NavAidModel::setData(const QModelIndex &index, const QVariant& value, int r // Set selected AM Demod to the given frequency (used to tune to ATC selected from airports on map) bool ADSBDemodGUI::setFrequency(qint64 targetFrequencyHz) { - const QRegularExpression re("R([0-9]+):([0-9]+)"); - QRegularExpressionMatch match = re.match(m_settings.m_amDemod); - if (match.hasMatch()) + unsigned int deviceSet, channelIndex; + + if (MainCore::getDeviceAndChannelIndexFromId(m_settings.m_amDemod, deviceSet, channelIndex)) { - int deviceSet = match.capturedTexts()[1].toInt(); - int channelIndex = match.capturedTexts()[2].toInt(); const int halfChannelBW = 20000/2; int dcOffset = halfChannelBW; diff --git a/plugins/channelrx/freqscanner/freqscanner.cpp b/plugins/channelrx/freqscanner/freqscanner.cpp index 274defd91..f59856762 100644 --- a/plugins/channelrx/freqscanner/freqscanner.cpp +++ b/plugins/channelrx/freqscanner/freqscanner.cpp @@ -23,7 +23,6 @@ #include #include #include -#include #include #include @@ -67,8 +66,8 @@ FreqScanner::FreqScanner(DeviceAPI *deviceAPI) : m_basebandSink(nullptr), m_running(false), m_basebandSampleRate(0), - m_scanDeviceSetIndex(-1), - m_scanChannelIndex(-1), + m_scanDeviceSetIndex(0), + m_scanChannelIndex(0), m_state(IDLE), m_timeoutTimer(this) { @@ -670,28 +669,19 @@ void FreqScanner::muteAll(const FreqScannerSettings& settings) } } - const QRegExp re("R([0-9]+):([0-9]+)"); for (const auto& channel : channels) { - if (re.indexIn(channel) >= 0) - { - int deviceSetIndex = re.capturedTexts()[1].toInt(); - int scanChannelIndex = re.capturedTexts()[2].toInt(); - ChannelWebAPIUtils::setAudioMute(deviceSetIndex, scanChannelIndex, true); + unsigned int deviceSetIndex, channelIndex; + + if (MainCore::getDeviceAndChannelIndexFromId(channel, deviceSetIndex, channelIndex)) { + ChannelWebAPIUtils::setAudioMute(deviceSetIndex, channelIndex, true); } } } void FreqScanner::applyChannelSetting(const QString& channel) { - const QRegExp re("R([0-9]+):([0-9]+)"); - if (re.indexIn(channel) >= 0) - { - m_scanDeviceSetIndex = re.capturedTexts()[1].toInt(); - m_scanChannelIndex = re.capturedTexts()[2].toInt(); - } - else - { + if (!MainCore::getDeviceAndChannelIndexFromId(channel, m_scanDeviceSetIndex, m_scanChannelIndex)) { qDebug() << "FreqScanner::applySettings: Failed to parse channel" << channel; } } diff --git a/plugins/channelrx/freqscanner/freqscanner.h b/plugins/channelrx/freqscanner/freqscanner.h index 9bfa0d0fb..e0e7ba2c6 100644 --- a/plugins/channelrx/freqscanner/freqscanner.h +++ b/plugins/channelrx/freqscanner/freqscanner.h @@ -370,8 +370,8 @@ private: QHash m_availableChannels; - int m_scanDeviceSetIndex; - int m_scanChannelIndex; + unsigned int m_scanDeviceSetIndex; + unsigned int m_scanChannelIndex; qint64 m_activeFrequency; QDateTime m_minFFTStartTime; int m_scannerSampleRate; diff --git a/plugins/channelrx/freqscanner/freqscannergui.cpp b/plugins/channelrx/freqscanner/freqscannergui.cpp index 7454e3ad6..8563e183e 100644 --- a/plugins/channelrx/freqscanner/freqscannergui.cpp +++ b/plugins/channelrx/freqscanner/freqscannergui.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include "device/deviceset.h" @@ -41,6 +40,7 @@ #include "gui/int64delegate.h" #include "gui/glspectrum.h" #include "channel/channelwebapiutils.h" +#include "maincore.h" #include "freqscannergui.h" #include "freqscanneraddrangedialog.h" @@ -935,15 +935,13 @@ void FreqScannerGUI::table_customContextMenuRequested(QPoint pos) qint64 frequency = ui->table->item(row, COL_FREQUENCY)->text().toLongLong(); FreqScannerSettings::FrequencySettings *frequencySettings = m_settings.getFrequencySettings(frequency); QString channel = m_settings.getChannel(frequencySettings); - const QRegExp re("R([0-9]+):([0-9]+)"); - if (re.indexIn(channel) >= 0) - { - int scanDeviceSetIndex = re.capturedTexts()[1].toInt(); - int scanChannelIndex = re.capturedTexts()[2].toInt(); + unsigned int scanDeviceSetIndex, scanChannelIndex; + if (MainCore::getDeviceAndChannelIndexFromId(channel, scanDeviceSetIndex, scanChannelIndex)) + { ButtonSwitch *startStop = ui->startStop; - QAction* findChannelMapAction = new QAction(QString("Tune R%1:%2 to %3").arg(scanDeviceSetIndex).arg(scanChannelIndex).arg(frequency), tableContextMenu); + QAction* findChannelMapAction = new QAction(QString("Tune %1 to %2").arg(channel).arg(frequency), tableContextMenu); connect(findChannelMapAction, &QAction::triggered, this, [this, scanDeviceSetIndex, scanChannelIndex, frequency, startStop]()->void { // Stop scanning diff --git a/plugins/channelrx/radioastronomy/radioastronomy.cpp b/plugins/channelrx/radioastronomy/radioastronomy.cpp index da610551e..bee6a2249 100644 --- a/plugins/channelrx/radioastronomy/radioastronomy.cpp +++ b/plugins/channelrx/radioastronomy/radioastronomy.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include @@ -479,12 +478,8 @@ void RadioAstronomy::sweepStart() m_sweep1 = m_sweep1Start; m_sweep2 = m_settings.m_sweep2Start; - const QRegExp re("F([0-9]+):([0-9]+)"); - if (re.indexIn(m_settings.m_starTracker) >= 0) + if (MainCore::getFeatureIndexFromId(m_settings.m_starTracker, m_starTrackerFeatureSetIndex, m_starTrackerFeatureIndex)) { - m_starTrackerFeatureSetIndex = re.capturedTexts()[1].toInt(); - m_starTrackerFeatureIndex = re.capturedTexts()[2].toInt(); - if (m_settings.m_sweepType == RadioAstronomySettings::SWP_AZEL) { ChannelWebAPIUtils::patchFeatureSetting(m_starTrackerFeatureSetIndex, m_starTrackerFeatureIndex, "target", "Custom Az/El"); } else if (m_settings.m_sweepType == RadioAstronomySettings::SWP_LB) { @@ -499,11 +494,8 @@ void RadioAstronomy::sweepStart() sweep2(); callOnStartTime(&RadioAstronomy::sweep1); } - else if (re.indexIn(m_settings.m_rotator) >= 0) + else if (MainCore::getFeatureIndexFromId(m_settings.m_rotator, m_rotatorFeatureSetIndex, m_rotatorFeatureIndex)) { - m_rotatorFeatureSetIndex = re.capturedTexts()[1].toInt(); - m_rotatorFeatureIndex = re.capturedTexts()[2].toInt(); - sweep2(); callOnStartTime(&RadioAstronomy::sweep1); } diff --git a/plugins/channelrx/radioastronomy/radioastronomy.h b/plugins/channelrx/radioastronomy/radioastronomy.h index 3e937aa3c..d287fe9ba 100644 --- a/plugins/channelrx/radioastronomy/radioastronomy.h +++ b/plugins/channelrx/radioastronomy/radioastronomy.h @@ -454,10 +454,10 @@ private: QNetworkAccessManager *m_networkManager; QNetworkRequest m_networkRequest; - int m_starTrackerFeatureSetIndex; - int m_starTrackerFeatureIndex; - int m_rotatorFeatureSetIndex; - int m_rotatorFeatureIndex; + unsigned int m_starTrackerFeatureSetIndex; + unsigned int m_starTrackerFeatureIndex; + unsigned int m_rotatorFeatureSetIndex; + unsigned int m_rotatorFeatureIndex; float m_sweep1; // Current sweep position float m_sweep2; diff --git a/plugins/channelrx/radioastronomy/radioastronomygui.cpp b/plugins/channelrx/radioastronomy/radioastronomygui.cpp index feff288ee..af8304c6b 100644 --- a/plugins/channelrx/radioastronomy/radioastronomygui.cpp +++ b/plugins/channelrx/radioastronomy/radioastronomygui.cpp @@ -2744,12 +2744,10 @@ void RadioAstronomyGUI::on_rotator_currentTextChanged(const QString& text) void RadioAstronomyGUI::setColumnPrecisionFromRotator() { // Match rotator precision - const QRegExp re("F([0-9]+):([0-9]+)"); - if (re.indexIn(m_settings.m_rotator) >= 0) - { - int featureSetIndex = re.capturedTexts()[1].toInt(); - int featureIndex = re.capturedTexts()[2].toInt(); + unsigned int featureSetIndex, featureIndex; + if (MainCore::getFeatureIndexFromId(m_settings.m_rotator, featureSetIndex, featureIndex)) + { int precision = 0; if (ChannelWebAPIUtils::getFeatureSetting(featureSetIndex, featureIndex, "precision", precision)) { @@ -4687,12 +4685,10 @@ void RadioAstronomyGUI::addFFT(FFTMeasurement *fft, bool skipCalcs) void RadioAstronomyGUI::getRotatorData(FFTMeasurement *fft) { - const QRegExp re("F([0-9]+):([0-9]+)"); - if (re.indexIn(m_settings.m_rotator) >= 0) - { - int rotatorFeatureSetIndex = re.capturedTexts()[1].toInt(); - int rotatorFeatureIndex = re.capturedTexts()[2].toInt(); + unsigned int rotatorFeatureSetIndex, rotatorFeatureIndex; + if (MainCore::getFeatureIndexFromId(m_settings.m_rotator, rotatorFeatureSetIndex, rotatorFeatureIndex)) + { SWGSDRangel::SWGFeatureReport featureReport; double value; qDebug() << m_settings.m_rotator << rotatorFeatureSetIndex << rotatorFeatureIndex; diff --git a/plugins/feature/startracker/startrackerworker.cpp b/plugins/feature/startracker/startrackerworker.cpp index 367a599a0..81516b39e 100644 --- a/plugins/feature/startracker/startrackerworker.cpp +++ b/plugins/feature/startracker/startrackerworker.cpp @@ -501,13 +501,10 @@ void StarTrackerWorker::update() { // Get Az/El from Satellite Tracker double azimuth, elevation; + unsigned int satelliteTrackerFeatureSetIndex,satelliteTrackerFeatureIndex; - const QRegExp re("F([0-9]+):([0-9]+)"); - if (re.indexIn(m_settings.m_target) >= 0) + if (MainCore::getFeatureIndexFromId(m_settings.m_target, satelliteTrackerFeatureSetIndex, satelliteTrackerFeatureIndex)) { - int satelliteTrackerFeatureSetIndex = re.capturedTexts()[1].toInt(); - int satelliteTrackerFeatureIndex = re.capturedTexts()[2].toInt(); - if (ChannelWebAPIUtils::getFeatureReportValue(satelliteTrackerFeatureSetIndex, satelliteTrackerFeatureIndex, "targetAzimuth", azimuth) && ChannelWebAPIUtils::getFeatureReportValue(satelliteTrackerFeatureSetIndex, satelliteTrackerFeatureIndex, "targetElevation", elevation)) { diff --git a/sdrbase/maincore.cpp b/sdrbase/maincore.cpp index 5ab0c32df..d1d899d34 100644 --- a/sdrbase/maincore.cpp +++ b/sdrbase/maincore.cpp @@ -435,6 +435,121 @@ void MainCore::updateWakeLock() } #endif +QChar MainCore::getDeviceSetTypeId(const DeviceSet* deviceSet) +{ + if (deviceSet->m_deviceMIMOEngine) { + return 'M'; + } else if (deviceSet->m_deviceSinkEngine) { + return 'T'; + } else if (deviceSet->m_deviceSourceEngine) { + return 'R'; + } else { + return 'X'; // Unknown + } +} + +QString MainCore::getDeviceSetId(const DeviceSet* deviceSet) +{ + QChar type = getDeviceSetTypeId(deviceSet); + + return QString("%1%2").arg(type).arg(deviceSet->getIndex()); +} + +QString MainCore::getChannelId(const ChannelAPI* channel) +{ + std::vector deviceSets = getDeviceSets(); + DeviceSet* deviceSet = deviceSets[channel->getDeviceSetIndex()]; + QString deviceSetId = getDeviceSetId(deviceSet); + int index = channel->getIndexInDeviceSet(); + // FIXME: if (deviceSet->m_deviceMIMOEngine) { + // we should append stream index. E.g. "M0:0.0" However, only ChannelGUI seems to know what it is + return QString("%1:%2").arg(deviceSetId).arg(index); +} + +QStringList MainCore::getDeviceSetIds(bool rx, bool tx, bool mimo) +{ + QStringList list; + std::vector deviceSets = getDeviceSets(); + + for (const auto deviceSet : deviceSets) + { + DSPDeviceSourceEngine *deviceSourceEngine = deviceSet->m_deviceSourceEngine; + DSPDeviceSinkEngine *deviceSinkEngine = deviceSet->m_deviceSinkEngine; + DSPDeviceMIMOEngine *deviceMIMOEngine = deviceSet->m_deviceMIMOEngine; + + if (((deviceSourceEngine != nullptr) && rx) + || ((deviceSinkEngine != nullptr) && tx) + || ((deviceMIMOEngine != nullptr) && mimo)) + { + list.append(getDeviceSetId(deviceSet)); + } + } + return list; +} + +bool MainCore::getDeviceSetTypeFromId(const QString& deviceSetId, QChar &type) +{ + if (!deviceSetId.isEmpty()) + { + type = deviceSetId[0]; + return (type == 'R') || (type == 'T') || (type == 'M'); + } + else + { + return false; + } +} + +bool MainCore::getDeviceSetIndexFromId(const QString& deviceSetId, unsigned int &deviceSetIndex) +{ + const QRegularExpression re("[RTM]([0-9]+)"); + QRegularExpressionMatch match = re.match(deviceSetId); + + if (match.hasMatch()) + { + deviceSetIndex = match.capturedTexts()[1].toInt(); + return true; + } + else + { + return false; + } +} + +bool MainCore::getDeviceAndChannelIndexFromId(const QString& channelId, unsigned int &deviceSetIndex, unsigned int &channelIndex) +{ + const QRegularExpression re("[RTM]([0-9]+):([0-9]+)"); + QRegularExpressionMatch match = re.match(channelId); + + if (match.hasMatch()) + { + deviceSetIndex = match.capturedTexts()[1].toInt(); + channelIndex = match.capturedTexts()[2].toInt(); + return true; + } + else + { + return false; + } +} + +bool MainCore::getFeatureIndexFromId(const QString& featureId, unsigned int &featureSetIndex, unsigned int &featureIndex) +{ + const QRegularExpression re("[F]([0-9]+):([0-9]+)"); + QRegularExpressionMatch match = re.match(featureId); + + if (match.hasMatch()) + { + featureSetIndex = match.capturedTexts()[1].toInt(); + featureIndex = match.capturedTexts()[2].toInt(); + return true; + } + else + { + return false; + } +} + std::vector MainCore::getChannels(const QString& uri) { std::vector channels; @@ -452,3 +567,15 @@ std::vector MainCore::getChannels(const QString& uri) return channels; } + +QStringList MainCore::getChannelIds(const QString& uri) +{ + QStringList list; + std::vector channels = getChannels(uri); + + for (const auto channel : channels) { + list.append(getChannelId(channel)); + } + + return list; +} diff --git a/sdrbase/maincore.h b/sdrbase/maincore.h index 4979d0788..ee3e1e569 100644 --- a/sdrbase/maincore.h +++ b/sdrbase/maincore.h @@ -857,7 +857,6 @@ public: PluginManager *getPluginManager() const { return m_pluginManager; } std::vector& getDeviceSets() { return m_deviceSets; } std::vector& getFeatureeSets() { return m_featureSets; } - std::vector getChannels(const QString& uri); //!< Get all channels from any device set with the given URI void setLoggingOptions(); DeviceAPI *getDevice(unsigned int deviceSetIndex); ChannelAPI *getChannel(unsigned int deviceSetIndex, int channelIndex); @@ -887,6 +886,18 @@ public: // Position const QGeoPositionInfo& getPosition() const; + // Ids + QChar getDeviceSetTypeId(const DeviceSet* deviceSet); //!< Get Type Id (E.g. 'R', 'T' or 'M') for the given device set + QString getDeviceSetId(const DeviceSet* deviceSet); //!< Get Id (E.g. "R2") for the given device set + QString getChannelId(const ChannelAPI* channel); //!< Get Id (E.g. "R1:2") for the given channel + static bool getDeviceSetTypeFromId(const QString& deviceSetId, QChar &type); //!< "R1" -> 'R' + static bool getDeviceSetIndexFromId(const QString& deviceSetId, unsigned int &deviceSetIndex); //!< "R1" -> 1 + static bool getDeviceAndChannelIndexFromId(const QString& channelId, unsigned int &deviceSetIndex, unsigned int &channelIndex); //!< "R3:4" -> 3, 4 + static bool getFeatureIndexFromId(const QString& featureId, unsigned int &featureSetIndex, unsigned int &featureIndex); //!< "F0:2" -> 0, 2 + QStringList getDeviceSetIds(bool rx, bool tx, bool mimo); //!< Get list of all device set Ids. E.g: {"R0", "R1", "T1", "M2"} + std::vector getChannels(const QString& uri); //!< Get all channels from any device set with the given URI + QStringList getChannelIds(const QString& uri); //!< Get all Ids for channels from any device set with the given URI. E.g. "sdrangel.channel.xyzdemod" -> {"R2:1", "M0:0.1"} + friend class MainServer; friend class MainWindow; friend class WebAPIAdapter;