/////////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2015-2018 Edouard Griffiths, F4EXB. // // 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 #include #include "SWGChannelSettings.h" #include "SWGVORDemodSettings.h" #include "SWGChannelReport.h" #include "SWGVORDemodReport.h" #include "dsp/dspengine.h" #include "dsp/dspcommands.h" #include "device/deviceapi.h" #include "feature/feature.h" #include "util/db.h" #include "maincore.h" #include "vordemodscreport.h" #include "vordemodsc.h" MESSAGE_CLASS_DEFINITION(VORDemodSC::MsgConfigureVORDemod, Message) const char * const VORDemodSC::m_channelIdURI = "sdrangel.channel.vordemodsc"; const char * const VORDemodSC::m_channelId = "VORDemodSC"; VORDemodSC::VORDemodSC(DeviceAPI *deviceAPI) : ChannelAPI(m_channelIdURI, ChannelAPI::StreamSingleSink), m_deviceAPI(deviceAPI), m_basebandSampleRate(0) { setObjectName(m_channelId); m_basebandSink = new VORDemodSCBaseband(); m_basebandSink->setMessageQueueToChannel(getInputMessageQueue()); m_basebandSink->moveToThread(&m_thread); applySettings(m_settings, true); m_deviceAPI->addChannelSink(this); m_deviceAPI->addChannelSinkAPI(this); m_networkManager = new QNetworkAccessManager(); connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*))); } VORDemodSC::~VORDemodSC() { qDebug("VORDemodSC::~VORDemodSC"); disconnect(m_networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(networkManagerFinished(QNetworkReply*))); delete m_networkManager; m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSink(this); if (m_basebandSink->isRunning()) { stop(); } delete m_basebandSink; } uint32_t VORDemodSC::getNumberOfDeviceStreams() const { return m_deviceAPI->getNbSourceStreams(); } void VORDemodSC::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst) { (void) firstOfBurst; m_basebandSink->feed(begin, end); } void VORDemodSC::start() { qDebug("VORDemodSC::start"); m_basebandSink->reset(); m_basebandSink->startWork(); m_thread.start(); DSPSignalNotification *dspMsg = new DSPSignalNotification(m_basebandSampleRate, m_centerFrequency); m_basebandSink->getInputMessageQueue()->push(dspMsg); VORDemodSCBaseband::MsgConfigureVORDemodBaseband *msg = VORDemodSCBaseband::MsgConfigureVORDemodBaseband::create(m_settings, true); m_basebandSink->getInputMessageQueue()->push(msg); } void VORDemodSC::stop() { qDebug("VORDemodSC::stop"); m_basebandSink->stopWork(); m_thread.quit(); m_thread.wait(); } bool VORDemodSC::handleMessage(const Message& cmd) { if (MsgConfigureVORDemod::match(cmd)) { MsgConfigureVORDemod& cfg = (MsgConfigureVORDemod&) cmd; qDebug() << "VORDemodSC::handleMessage: MsgConfigureVORDemod"; applySettings(cfg.getSettings(), cfg.getForce()); return true; } else if (DSPSignalNotification::match(cmd)) { DSPSignalNotification& notif = (DSPSignalNotification&) cmd; m_basebandSampleRate = notif.getSampleRate(); m_centerFrequency = notif.getCenterFrequency(); // Forward to the sink DSPSignalNotification* rep = new DSPSignalNotification(notif); // make a copy qDebug() << "VORDemodSC::handleMessage: DSPSignalNotification"; m_basebandSink->getInputMessageQueue()->push(rep); // Forward to GUI if any if (m_guiMessageQueue) { rep = new DSPSignalNotification(notif); m_guiMessageQueue->push(rep); } return true; } else if (VORDemodSCReport::MsgReportRadial::match(cmd)) { VORDemodSCReport::MsgReportRadial& report = (VORDemodSCReport::MsgReportRadial&) cmd; m_radial = report.getRadial(); m_refMag = report.getRefMag(); m_varMag = report.getVarMag(); if (m_guiMessageQueue) { VORDemodSCReport::MsgReportRadial *msg = new VORDemodSCReport::MsgReportRadial(report); m_guiMessageQueue->push(msg); } MessagePipes& messagePipes = MainCore::instance()->getMessagePipes(); QList *reportMessageQueues = messagePipes.getMessageQueues(this, "report"); if (reportMessageQueues) { sendChannelReport(reportMessageQueues); } return true; } else if (VORDemodSCReport::MsgReportIdent::match(cmd)) { VORDemodSCReport::MsgReportIdent& report = (VORDemodSCReport::MsgReportIdent&) cmd; m_morseIdent = report.getIdent(); if (m_guiMessageQueue) { VORDemodSCReport::MsgReportIdent *msg = new VORDemodSCReport::MsgReportIdent(report); m_guiMessageQueue->push(msg); } MessagePipes& messagePipes = MainCore::instance()->getMessagePipes(); QList *reportMessageQueues = messagePipes.getMessageQueues(this, "report"); if (reportMessageQueues) { sendChannelReport(reportMessageQueues); } return true; } else { return false; } } void VORDemodSC::applySettings(const VORDemodSCSettings& settings, bool force) { qDebug() << "VORDemodSC::applySettings:" << " m_inputFrequencyOffset: " << settings.m_inputFrequencyOffset << " m_navId: " << settings.m_navId << " m_volume: " << settings.m_volume << " m_squelch: " << settings.m_squelch << " m_audioMute: " << settings.m_audioMute << " m_audioDeviceName: " << settings.m_audioDeviceName << " m_streamIndex: " << settings.m_streamIndex << " m_useReverseAPI: " << settings.m_useReverseAPI << " m_reverseAPIAddress: " << settings.m_reverseAPIAddress << " m_reverseAPIPort: " << settings.m_reverseAPIPort << " m_reverseAPIDeviceIndex: " << settings.m_reverseAPIDeviceIndex << " m_reverseAPIChannelIndex: " << settings.m_reverseAPIChannelIndex << " force: " << force; QList reverseAPIKeys; if ((m_settings.m_inputFrequencyOffset != settings.m_inputFrequencyOffset) || force) { reverseAPIKeys.append("inputFrequencyOffset"); } if ((m_settings.m_navId != settings.m_navId) || force) { reverseAPIKeys.append("navId"); } if ((m_settings.m_squelch != settings.m_squelch) || force) { reverseAPIKeys.append("squelch"); } if ((settings.m_audioDeviceName != m_settings.m_audioDeviceName) || force) { reverseAPIKeys.append("audioDeviceName"); } if ((m_settings.m_audioMute != settings.m_audioMute) || force) { reverseAPIKeys.append("audioMute"); } if ((m_settings.m_volume != settings.m_volume) || force) { reverseAPIKeys.append("volume"); } if (m_settings.m_streamIndex != settings.m_streamIndex) { if (m_deviceAPI->getSampleMIMO()) // change of stream is possible for MIMO devices only { m_deviceAPI->removeChannelSinkAPI(this); m_deviceAPI->removeChannelSink(this, m_settings.m_streamIndex); m_deviceAPI->addChannelSink(this, settings.m_streamIndex); m_deviceAPI->addChannelSinkAPI(this); } reverseAPIKeys.append("streamIndex"); } if ((m_settings.m_identThreshold != settings.m_identThreshold) || force) { reverseAPIKeys.append("identThreshold"); } VORDemodSCBaseband::MsgConfigureVORDemodBaseband *msg = VORDemodSCBaseband::MsgConfigureVORDemodBaseband::create(settings, force); m_basebandSink->getInputMessageQueue()->push(msg); if (settings.m_useReverseAPI) { bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) || (m_settings.m_reverseAPIAddress != settings.m_reverseAPIAddress) || (m_settings.m_reverseAPIPort != settings.m_reverseAPIPort) || (m_settings.m_reverseAPIDeviceIndex != settings.m_reverseAPIDeviceIndex) || (m_settings.m_reverseAPIChannelIndex != settings.m_reverseAPIChannelIndex); webapiReverseSendSettings(reverseAPIKeys, settings, fullUpdate || force); } if (m_featuresSettingsFeedback.size() > 0) { featuresSendSettings(reverseAPIKeys, settings, force); } m_settings = settings; } QByteArray VORDemodSC::serialize() const { return m_settings.serialize(); } bool VORDemodSC::deserialize(const QByteArray& data) { if (m_settings.deserialize(data)) { MsgConfigureVORDemod *msg = MsgConfigureVORDemod::create(m_settings, true); m_inputMessageQueue.push(msg); return true; } else { m_settings.resetToDefaults(); MsgConfigureVORDemod *msg = MsgConfigureVORDemod::create(m_settings, true); m_inputMessageQueue.push(msg); return false; } } int VORDemodSC::webapiSettingsGet( SWGSDRangel::SWGChannelSettings& response, QString& errorMessage) { (void) errorMessage; response.setVorDemodScSettings(new SWGSDRangel::SWGVORDemodSCSettings()); response.getVorDemodScSettings()->init(); webapiFormatChannelSettings(response, m_settings); return 200; } int VORDemodSC::webapiSettingsPutPatch( bool force, const QStringList& channelSettingsKeys, SWGSDRangel::SWGChannelSettings& response, QString& errorMessage) { (void) errorMessage; VORDemodSCSettings settings = m_settings; webapiUpdateChannelSettings(settings, channelSettingsKeys, response); MsgConfigureVORDemod *msg = MsgConfigureVORDemod::create(settings, force); m_inputMessageQueue.push(msg); qDebug("VORDemodSC::webapiSettingsPutPatch: forward to GUI: %p", m_guiMessageQueue); if (m_guiMessageQueue) // forward to GUI if any { MsgConfigureVORDemod *msgToGUI = MsgConfigureVORDemod::create(settings, force); m_guiMessageQueue->push(msgToGUI); } webapiFormatChannelSettings(response, settings); return 200; } void VORDemodSC::webapiUpdateChannelSettings( VORDemodSCSettings& settings, const QStringList& channelSettingsKeys, SWGSDRangel::SWGChannelSettings& response) { if (channelSettingsKeys.contains("inputFrequencyOffset")) { settings.m_inputFrequencyOffset = response.getVorDemodScSettings()->getInputFrequencyOffset(); } if (channelSettingsKeys.contains("navId")) { settings.m_navId = response.getVorDemodScSettings()->getNavId(); } if (channelSettingsKeys.contains("audioMute")) { settings.m_audioMute = response.getVorDemodScSettings()->getAudioMute() != 0; } if (channelSettingsKeys.contains("rgbColor")) { settings.m_rgbColor = response.getVorDemodScSettings()->getRgbColor(); } if (channelSettingsKeys.contains("squelch")) { settings.m_squelch = response.getVorDemodScSettings()->getSquelch(); } if (channelSettingsKeys.contains("title")) { settings.m_title = *response.getVorDemodScSettings()->getTitle(); } if (channelSettingsKeys.contains("volume")) { settings.m_volume = response.getVorDemodScSettings()->getVolume(); } if (channelSettingsKeys.contains("audioDeviceName")) { settings.m_audioDeviceName = *response.getVorDemodScSettings()->getAudioDeviceName(); } if (channelSettingsKeys.contains("streamIndex")) { settings.m_streamIndex = response.getVorDemodScSettings()->getStreamIndex(); } if (channelSettingsKeys.contains("useReverseAPI")) { settings.m_useReverseAPI = response.getVorDemodScSettings()->getUseReverseApi() != 0; } if (channelSettingsKeys.contains("reverseAPIAddress")) { settings.m_reverseAPIAddress = *response.getVorDemodScSettings()->getReverseApiAddress(); } if (channelSettingsKeys.contains("reverseAPIPort")) { settings.m_reverseAPIPort = response.getVorDemodScSettings()->getReverseApiPort(); } if (channelSettingsKeys.contains("reverseAPIDeviceIndex")) { settings.m_reverseAPIDeviceIndex = response.getVorDemodScSettings()->getReverseApiDeviceIndex(); } if (channelSettingsKeys.contains("reverseAPIChannelIndex")) { settings.m_reverseAPIChannelIndex = response.getVorDemodScSettings()->getReverseApiChannelIndex(); } if (channelSettingsKeys.contains("identThreshold")) { settings.m_identThreshold = response.getVorDemodScSettings()->getIdentThreshold(); } } int VORDemodSC::webapiReportGet( SWGSDRangel::SWGChannelReport& response, QString& errorMessage) { (void) errorMessage; response.setVorDemodScReport(new SWGSDRangel::SWGVORDemodSCReport()); response.getVorDemodScReport()->init(); webapiFormatChannelReport(response); return 200; } void VORDemodSC::webapiFormatChannelSettings(SWGSDRangel::SWGChannelSettings& response, const VORDemodSCSettings& settings) { response.getVorDemodScSettings()->setInputFrequencyOffset(settings.m_inputFrequencyOffset); response.getVorDemodScSettings()->setNavId(settings.m_navId); response.getVorDemodScSettings()->setAudioMute(settings.m_audioMute ? 1 : 0); response.getVorDemodScSettings()->setRgbColor(settings.m_rgbColor); response.getVorDemodScSettings()->setSquelch(settings.m_squelch); response.getVorDemodScSettings()->setVolume(settings.m_volume); if (response.getVorDemodScSettings()->getTitle()) { *response.getVorDemodScSettings()->getTitle() = settings.m_title; } else { response.getVorDemodScSettings()->setTitle(new QString(settings.m_title)); } if (response.getVorDemodScSettings()->getAudioDeviceName()) { *response.getVorDemodScSettings()->getAudioDeviceName() = settings.m_audioDeviceName; } else { response.getVorDemodScSettings()->setAudioDeviceName(new QString(settings.m_audioDeviceName)); } response.getVorDemodScSettings()->setStreamIndex(settings.m_streamIndex); response.getVorDemodScSettings()->setUseReverseApi(settings.m_useReverseAPI ? 1 : 0); if (response.getVorDemodScSettings()->getReverseApiAddress()) { *response.getVorDemodScSettings()->getReverseApiAddress() = settings.m_reverseAPIAddress; } else { response.getVorDemodScSettings()->setReverseApiAddress(new QString(settings.m_reverseAPIAddress)); } response.getVorDemodScSettings()->setReverseApiPort(settings.m_reverseAPIPort); response.getVorDemodScSettings()->setReverseApiDeviceIndex(settings.m_reverseAPIDeviceIndex); response.getVorDemodScSettings()->setReverseApiChannelIndex(settings.m_reverseAPIChannelIndex); response.getVorDemodScSettings()->setIdentThreshold(settings.m_identThreshold); } void VORDemodSC::webapiFormatChannelReport(SWGSDRangel::SWGChannelReport& response) { double magsqAvg, magsqPeak; int nbMagsqSamples; getMagSqLevels(magsqAvg, magsqPeak, nbMagsqSamples); response.getVorDemodScReport()->setChannelPowerDb(CalcDb::dbPower(magsqAvg)); response.getVorDemodScReport()->setSquelch(m_basebandSink->getSquelchOpen() ? 1 : 0); response.getVorDemodScReport()->setAudioSampleRate(m_basebandSink->getAudioSampleRate()); response.getVorDemodScReport()->setNavId(m_settings.m_navId); response.getVorDemodScReport()->setRadial(m_radial); response.getVorDemodScReport()->setRefMag(m_refMag); response.getVorDemodScReport()->setVarMag(m_varMag); float refMagDB = std::round(20.0*std::log10(m_refMag)); float varMagDB = std::round(20.0*std::log10(m_varMag)); bool validRefMag = refMagDB > m_settings.m_refThresholdDB; bool validVarMag = varMagDB > m_settings.m_varThresholdDB; response.getVorDemodScReport()->setValidRadial(validRefMag && validVarMag ? 1 : 0); response.getVorDemodScReport()->setValidRefMag(validRefMag ? 1 : 0); response.getVorDemodScReport()->setValidVarMag(validVarMag ? 1 : 0); if (response.getVorDemodScReport()->getMorseIdent()) { *response.getVorDemodScReport()->getMorseIdent() = m_morseIdent; } else { response.getVorDemodScReport()->setMorseIdent(new QString(m_morseIdent)); } } void VORDemodSC::webapiReverseSendSettings(QList& channelSettingsKeys, const VORDemodSCSettings& settings, bool force) { SWGSDRangel::SWGChannelSettings *swgChannelSettings = new SWGSDRangel::SWGChannelSettings(); webapiFormatChannelSettings(channelSettingsKeys, swgChannelSettings, settings, force); QString channelSettingsURL = QString("http://%1:%2/sdrangel/deviceset/%3/channel/%4/settings") .arg(settings.m_reverseAPIAddress) .arg(settings.m_reverseAPIPort) .arg(settings.m_reverseAPIDeviceIndex) .arg(settings.m_reverseAPIChannelIndex); m_networkRequest.setUrl(QUrl(channelSettingsURL)); m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QBuffer *buffer = new QBuffer(); buffer->open((QBuffer::ReadWrite)); buffer->write(swgChannelSettings->asJson().toUtf8()); buffer->seek(0); // Always use PATCH to avoid passing reverse API settings QNetworkReply *reply = m_networkManager->sendCustomRequest(m_networkRequest, "PATCH", buffer); buffer->setParent(reply); delete swgChannelSettings; } void VORDemodSC::featuresSendSettings(QList& channelSettingsKeys, const VORDemodSCSettings& settings, bool force) { QList::iterator it = m_featuresSettingsFeedback.begin(); MainCore *mainCore = MainCore::instance(); for (; it != m_featuresSettingsFeedback.end(); ++it) { if (mainCore->existsFeature(*it)) { SWGSDRangel::SWGChannelSettings *swgChannelSettings = new SWGSDRangel::SWGChannelSettings(); webapiFormatChannelSettings(channelSettingsKeys, swgChannelSettings, settings, force); Feature::MsgChannelSettings *msg = Feature::MsgChannelSettings::create( this, channelSettingsKeys, swgChannelSettings, force ); (*it)->getInputMessageQueue()->push(msg); } else { m_featuresSettingsFeedback.removeOne(*it); } } } void VORDemodSC::sendChannelReport(QList *messageQueues) { QList::iterator it = messageQueues->begin(); for (; it != messageQueues->end(); ++it) { SWGSDRangel::SWGChannelReport *swgChannelReport = new SWGSDRangel::SWGChannelReport(); swgChannelReport->setDirection(0); swgChannelReport->setChannelType(new QString(m_channelId)); swgChannelReport->setVorDemodScReport(new SWGSDRangel::SWGVORDemodSCReport()); webapiFormatChannelReport(*swgChannelReport); MainCore::MsgChannelReport *msg = MainCore::MsgChannelReport::create(this, swgChannelReport); (*it)->push(msg); } } void VORDemodSC::webapiFormatChannelSettings( QList& channelSettingsKeys, SWGSDRangel::SWGChannelSettings *swgChannelSettings, const VORDemodSCSettings& settings, bool force ) { swgChannelSettings->setDirection(0); // Single sink (Rx) swgChannelSettings->setOriginatorChannelIndex(getIndexInDeviceSet()); swgChannelSettings->setOriginatorDeviceSetIndex(getDeviceSetIndex()); swgChannelSettings->setChannelType(new QString("VORDemodSC")); swgChannelSettings->setVorDemodScSettings(new SWGSDRangel::SWGVORDemodSCSettings()); SWGSDRangel::SWGVORDemodSCSettings *swgVORDemodSCSettings = swgChannelSettings->getVorDemodScSettings(); // transfer data that has been modified. When force is on transfer all data except reverse API data if (channelSettingsKeys.contains("inputFrequencyOffset") || force) { swgVORDemodSCSettings->setInputFrequencyOffset(settings.m_inputFrequencyOffset); } if (channelSettingsKeys.contains("navId") || force) { swgVORDemodSCSettings->setNavId(settings.m_navId); } if (channelSettingsKeys.contains("audioMute") || force) { swgVORDemodSCSettings->setAudioMute(settings.m_audioMute ? 1 : 0); } if (channelSettingsKeys.contains("rgbColor") || force) { swgVORDemodSCSettings->setRgbColor(settings.m_rgbColor); } if (channelSettingsKeys.contains("squelch") || force) { swgVORDemodSCSettings->setSquelch(settings.m_squelch); } if (channelSettingsKeys.contains("title") || force) { swgVORDemodSCSettings->setTitle(new QString(settings.m_title)); } if (channelSettingsKeys.contains("volume") || force) { swgVORDemodSCSettings->setVolume(settings.m_volume); } if (channelSettingsKeys.contains("audioDeviceName") || force) { swgVORDemodSCSettings->setAudioDeviceName(new QString(settings.m_audioDeviceName)); } if (channelSettingsKeys.contains("streamIndex") || force) { swgVORDemodSCSettings->setStreamIndex(settings.m_streamIndex); } if (channelSettingsKeys.contains("identThreshold") || force) { swgVORDemodSCSettings->setAudioMute(settings.m_identThreshold); } } void VORDemodSC::networkManagerFinished(QNetworkReply *reply) { QNetworkReply::NetworkError replyError = reply->error(); if (replyError) { qWarning() << "VORDemodSC::networkManagerFinished:" << " error(" << (int) replyError << "): " << replyError << ": " << reply->errorString(); } else { QString answer = reply->readAll(); answer.chop(1); // remove last \n qDebug("VORDemodSC::networkManagerFinished: reply:\n%s", answer.toStdString().c_str()); } reply->deleteLater(); }