From 9eb05a5c14bc2360aacc9bb1ab07bd8e4825665b Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 19 Aug 2018 11:20:54 +0200 Subject: [PATCH] SDRdaemon: add initial source or sink device --- sdrdaemon/sdrdaemonmain.cpp | 201 +++++++++++++++++++++++++++++++++- sdrdaemon/sdrdaemonmain.h | 24 ++++ sdrdaemon/sdrdaemonparser.cpp | 17 ++- 3 files changed, 235 insertions(+), 7 deletions(-) diff --git a/sdrdaemon/sdrdaemonmain.cpp b/sdrdaemon/sdrdaemonmain.cpp index 6f5b35f15..e89b4fc07 100644 --- a/sdrdaemon/sdrdaemonmain.cpp +++ b/sdrdaemon/sdrdaemonmain.cpp @@ -26,6 +26,10 @@ #include "dsp/dspengine.h" #include "dsp/dspdevicesourceengine.h" +#include "dsp/dspdevicesinkengine.h" +#include "device/devicesourceapi.h" +#include "device/devicesinkapi.h" +#include "device/deviceenumerator.h" #include "plugin/pluginmanager.h" #include "util/message.h" #include "loggerwithfile.h" @@ -73,11 +77,66 @@ SDRDaemonMain::SDRDaemonMain(qtwebapp::LoggerWithFile *logger, const SDRDaemonPa m_apiServer = new SDRDaemon::WebAPIServer(parser.getServerAddress(), parser.getServerPort(), m_requestMapper); m_apiServer->start(); + m_tx = parser.getTx(); + m_deviceType = parser.getDeviceType(); + m_deviceSerial = parser.hasSerial() ? parser.getSerial() : ""; + m_deviceSequence = parser.hasSequence() ? parser.getSequence() : -1; + m_deviceSourceEngine = 0; + m_deviceSinkEngine = 0; + m_deviceSourceAPI = 0; + m_deviceSinkAPI = 0; + + if (m_tx) + { + if (addSinkDevice()) + { + QString msg(tr("SDRDaemonMain::SDRDaemonMain: set sink %1").arg(m_deviceType)); + if (m_deviceSerial.length() > 0) { + msg += tr(" ser: %1").arg(m_deviceSerial); + } else if (m_deviceSequence >= 0) { + msg += tr(" seq: %1").arg(m_deviceSequence); + } else { + msg += " first device"; + } + QDebug info = qInfo(); + info.noquote(); + info << msg; + } + else + { + qCritical("SDRDaemonMain::SDRDaemonMain: sink device not found aborting"); + emit finished(); + } + } + else + { + if (addSourceDevice()) + { + QString msg(tr("SDRDaemonMain::SDRDaemonMain: set source %1").arg(m_deviceType)); + if (m_deviceSerial.length() > 0) { + msg += tr(" ser: %1").arg(m_deviceSerial); + } else if (m_deviceSequence >= 0) { + msg += tr(" seq: %1").arg(m_deviceSequence); + } else { + msg += " first device"; + } + QDebug info = qInfo(); + info.noquote(); + info << msg; + } + else + { + qCritical("SDRDaemonMain::SDRDaemonMain: source device not found aborting"); + emit finished(); + } + } + qDebug() << "SDRDaemonMain::SDRDaemonMain: end"; } SDRDaemonMain::~SDRDaemonMain() { + removeDevice(); m_apiServer->stop(); m_settings.save(); delete m_apiServer; @@ -86,7 +145,7 @@ SDRDaemonMain::~SDRDaemonMain() delete m_pluginManager; - qDebug() << "SDRDaemonMain::~SDRdaemon: end"; + qDebug() << "SDRDaemonMain::~SDRDaemonMain: end"; delete m_logger; } @@ -147,6 +206,146 @@ void SDRDaemonMain::setLoggingOptions() } } +bool SDRDaemonMain::addSinkDevice() +{ + DSPDeviceSinkEngine *dspDeviceSinkEngine = m_dspEngine->addDeviceSinkEngine(); + dspDeviceSinkEngine->start(); + + uint dspDeviceSinkEngineUID = dspDeviceSinkEngine->getUID(); + char uidCStr[16]; + sprintf(uidCStr, "UID:%d", dspDeviceSinkEngineUID); + + m_deviceSourceEngine = 0; + m_deviceSinkEngine = dspDeviceSinkEngine; + + m_deviceSinkAPI = new DeviceSinkAPI(0, dspDeviceSinkEngine); + int deviceIndex = getDeviceIndex(); + + if (deviceIndex >= 0) + { + PluginInterface::SamplingDevice samplingDevice = DeviceEnumerator::instance()->getTxSamplingDevice(deviceIndex); + m_deviceSinkAPI->setSampleSinkSequence(samplingDevice.sequence); + m_deviceSinkAPI->setNbItems(samplingDevice.deviceNbItems); + m_deviceSinkAPI->setItemIndex(samplingDevice.deviceItemIndex); + m_deviceSinkAPI->setHardwareId(samplingDevice.hardwareId); + m_deviceSinkAPI->setSampleSinkId(samplingDevice.id); + m_deviceSinkAPI->setSampleSinkSerial(samplingDevice.serial); + m_deviceSinkAPI->setSampleSinkDisplayName(samplingDevice.displayedName); + m_deviceSinkAPI->setSampleSinkPluginInterface(DeviceEnumerator::instance()->getTxPluginInterface(deviceIndex)); + + DeviceSampleSink *sink = m_deviceSinkAPI->getPluginInterface()->createSampleSinkPluginInstanceOutput( + m_deviceSinkAPI->getSampleSinkId(), m_deviceSinkAPI); + m_deviceSinkAPI->setSampleSink(sink); + return true; + } + + return false; +} + +bool SDRDaemonMain::addSourceDevice() +{ + DSPDeviceSourceEngine *dspDeviceSourceEngine = m_dspEngine->addDeviceSourceEngine(); + dspDeviceSourceEngine->start(); + + uint dspDeviceSourceEngineUID = dspDeviceSourceEngine->getUID(); + char uidCStr[16]; + sprintf(uidCStr, "UID:%d", dspDeviceSourceEngineUID); + + m_deviceSourceAPI = new DeviceSourceAPI(0, dspDeviceSourceEngine); + int deviceIndex = getDeviceIndex(); + + if (deviceIndex >= 0) + { + PluginInterface::SamplingDevice samplingDevice = DeviceEnumerator::instance()->getRxSamplingDevice(deviceIndex); + m_deviceSourceAPI->setSampleSourceSequence(samplingDevice.sequence); + m_deviceSourceAPI->setNbItems(samplingDevice.deviceNbItems); + m_deviceSourceAPI->setItemIndex(samplingDevice.deviceItemIndex); + m_deviceSourceAPI->setHardwareId(samplingDevice.hardwareId); + m_deviceSourceAPI->setSampleSourceId(samplingDevice.id); + m_deviceSourceAPI->setSampleSourceSerial(samplingDevice.serial); + m_deviceSourceAPI->setSampleSourceDisplayName(samplingDevice.displayedName); + m_deviceSourceAPI->setSampleSourcePluginInterface(DeviceEnumerator::instance()->getRxPluginInterface(deviceIndex)); + + DeviceSampleSource *source = m_deviceSourceAPI->getPluginInterface()->createSampleSourcePluginInstanceInput( + m_deviceSourceAPI->getSampleSourceId(), m_deviceSourceAPI); + m_deviceSourceAPI->setSampleSource(source); + return true; + } + + return false; +} + +void SDRDaemonMain::removeDevice() +{ + if (m_deviceSourceEngine) // source set + { + m_deviceSourceEngine->stopAcquistion(); + + // deletes old UI and input object + m_deviceSourceAPI->resetSampleSourceId(); + m_deviceSourceAPI->getPluginInterface()->deleteSampleSourcePluginInstanceInput( + m_deviceSourceAPI->getSampleSource()); + m_deviceSourceAPI->clearBuddiesLists(); // clear old API buddies lists + + m_deviceSourceEngine->stop(); + m_dspEngine->removeLastDeviceSourceEngine(); + + delete m_deviceSourceAPI; + m_deviceSourceAPI = 0; + } + else if (m_deviceSinkEngine) // sink set + { + m_deviceSinkEngine->stopGeneration(); + + // deletes old UI and output object + m_deviceSinkAPI->resetSampleSinkId(); + m_deviceSinkAPI->getPluginInterface()->deleteSampleSinkPluginInstanceOutput( + m_deviceSinkAPI->getSampleSink()); + m_deviceSinkAPI->clearBuddiesLists(); // clear old API buddies lists + + m_deviceSinkEngine->stop(); + m_dspEngine->removeLastDeviceSinkEngine(); + + delete m_deviceSinkAPI; + m_deviceSinkAPI = 0; + } +} + +int SDRDaemonMain::getDeviceIndex() +{ + int nbSamplingDevices = m_tx ? DeviceEnumerator::instance()->getNbTxSamplingDevices() : DeviceEnumerator::instance()->getNbRxSamplingDevices(); + + for (int i = 0; i < nbSamplingDevices; i++) + { + PluginInterface::SamplingDevice samplingDevice = m_tx ? DeviceEnumerator::instance()->getTxSamplingDevice(i) : DeviceEnumerator::instance()->getRxSamplingDevice(i); + if (samplingDevice.hardwareId == m_deviceType) + { + if (m_deviceSerial.length() > 0) + { + if (samplingDevice.serial == m_deviceSerial) { + return i; + } else { + continue; + } + } + else if (m_deviceSequence >= 0) + { + if (samplingDevice.sequence == m_deviceSequence) { + return i; + } else { + continue; + } + } + else + { + return i; + } + } + } + + return -1; // not found +} + bool SDRDaemonMain::handleMessage(const Message& cmd __attribute__((unused))) { return false; diff --git a/sdrdaemon/sdrdaemonmain.h b/sdrdaemon/sdrdaemonmain.h index 0bde35914..f57b16f1c 100644 --- a/sdrdaemon/sdrdaemonmain.h +++ b/sdrdaemon/sdrdaemonmain.h @@ -43,6 +43,10 @@ class DSPEngine; class PluginManager; class Message; class WebAPIAdapterDaemon; +class DSPDeviceSourceEngine; +class DeviceSourceAPI; +class DSPDeviceSinkEngine; +class DeviceSinkAPI; class SDRDaemonMain : public QObject { Q_OBJECT @@ -51,6 +55,15 @@ public: ~SDRDaemonMain(); static SDRDaemonMain *getInstance() { return m_instance; } // Main Core is de facto a singleton so this just returns its reference + MessageQueue* getInputMessageQueue() { return &m_inputMessageQueue; } + + const QTimer& getMasterTimer() const { return m_masterTimer; } + const SDRDaemonSettings& getSettings() const { return m_settings; } + + bool addSourceDevice(); + bool addSinkDevice(); + void removeDevice(); + friend class WebAPIAdapterDaemon; signals: @@ -70,8 +83,19 @@ private: SDRDaemon::WebAPIServer *m_apiServer; WebAPIAdapterDaemon *m_apiAdapter; + bool m_tx; + QString m_deviceType; + QString m_deviceSerial; + int m_deviceSequence; + + DSPDeviceSourceEngine *m_deviceSourceEngine; + DeviceSourceAPI *m_deviceSourceAPI; + DSPDeviceSinkEngine *m_deviceSinkEngine; + DeviceSinkAPI *m_deviceSinkAPI; + void loadSettings(); void setLoggingOptions(); + int getDeviceIndex(); bool handleMessage(const Message& cmd); private slots: diff --git a/sdrdaemon/sdrdaemonparser.cpp b/sdrdaemon/sdrdaemonparser.cpp index b0c9958ff..a925bc15f 100644 --- a/sdrdaemon/sdrdaemonparser.cpp +++ b/sdrdaemon/sdrdaemonparser.cpp @@ -52,8 +52,8 @@ SDRDaemonParser::SDRDaemonParser() : m_serialOption(QStringList() << "s" << "serial", "Device serial number.", "serial"), - m_sequenceOption(QStringList() << "n" << "sequence", - "Device sequence number in enumeration for the same device type.", + m_sequenceOption(QStringList() << "i" << "sequence", + "Device sequence index in enumeration for the same device type.", "sequence") { @@ -140,27 +140,31 @@ void SDRDaemonParser::parse(const QCoreApplication& app) qWarning() << "SDRDaemonParser::parse: data port invalid. Defaulting to " << m_dataPort; } - // hardware Id + // device type QString deviceType = m_parser.value(m_deviceTypeOption); - QRegExp deviceTypeRegex("^[A-Z][a-z][A-Za-z0-9]+$"); + QRegExp deviceTypeRegex("^[A-Z][A-Za-z0-9]+$"); QRegExpValidator deviceTypeValidator(deviceTypeRegex); if (deviceTypeValidator.validate(deviceType, pos) == QValidator::Acceptable) { m_deviceType = deviceType; + qDebug() << "SDRDaemonParser::parse: device type: " << m_deviceType; } else { - qWarning() << "SDRDaemonParser::parse: device type invalid. Defaulting to " << deviceType; + qWarning() << "SDRDaemonParser::parse: device type invalid. Defaulting to " << m_deviceType; } // tx m_tx = m_parser.isSet(m_txOption); + qDebug() << "SDRDaemonParser::parse: tx: " << m_tx; // serial m_hasSerial = m_parser.isSet(m_serialOption); - if (m_hasSerial) { + if (m_hasSerial) + { m_serial = m_parser.value(m_serialOption); + qDebug() << "SDRDaemonParser::parse: serial: " << m_serial; } // sequence @@ -173,6 +177,7 @@ void SDRDaemonParser::parse(const QCoreApplication& app) if (ok && (sequence >= 0) && (sequence < 65536)) { m_sequence = sequence; + qDebug() << "SDRDaemonParser::parse: sequence: " << m_sequence; } else { qWarning() << "SDRDaemonParser::parse: sequence invalid. Defaulting to " << m_sequence; }