From 00d70b65aa7a3456e045122185b9e3570606d7dc Mon Sep 17 00:00:00 2001 From: f4exb Date: Mon, 1 Jun 2020 10:41:27 +0200 Subject: [PATCH] SigMF recoding: first implementation of recording with RTLSDR --- CMakeLists.txt | 7 +- cmake/Modules/FindLibSigMF.cmake | 36 +++ plugins/samplesource/rtlsdr/rtlsdrgui.cpp | 6 +- plugins/samplesource/rtlsdr/rtlsdrinput.cpp | 87 ++++-- plugins/samplesource/rtlsdr/rtlsdrinput.h | 8 +- sdrbase/CMakeLists.txt | 14 +- sdrbase/dsp/filerecord.cpp | 4 +- sdrbase/dsp/filerecord.h | 3 +- sdrbase/dsp/filerecordinterface.cpp | 39 +++ sdrbase/dsp/filerecordinterface.h | 20 +- sdrbase/dsp/sigmf_forward.h | 64 +++++ sdrbase/dsp/sigmffilerecord.cpp | 238 ++++++++++++++++ sdrbase/dsp/sigmffilerecord.h | 73 +++++ sdrbase/util/sha512.h | 298 ++++++++++++++++++++ 14 files changed, 869 insertions(+), 28 deletions(-) create mode 100644 cmake/Modules/FindLibSigMF.cmake create mode 100644 sdrbase/dsp/sigmf_forward.h create mode 100644 sdrbase/dsp/sigmffilerecord.cpp create mode 100644 sdrbase/dsp/sigmffilerecord.h create mode 100644 sdrbase/util/sha512.h diff --git a/CMakeLists.txt b/CMakeLists.txt index fe84dd4a7..a9b80fd02 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ project(sdrangel) list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/Modules) # disable only when needed -set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) @@ -324,6 +324,11 @@ find_package(Boost REQUIRED) find_package(FFTW3F REQUIRED) find_package(LibUSB REQUIRED) # used by so many packages find_package(OpenCV OPTIONAL_COMPONENTS core highgui imgproc imgcodecs videoio) # channeltx/modatv +find_package(LibSigMF) # SigMF recording files support + +if (LIBSIGMF_FOUND) + add_definitions(-DHAS_LIBSIGMF) +endif (LIBSIGMF_FOUND) # macOS compatibility if(APPLE) diff --git a/cmake/Modules/FindLibSigMF.cmake b/cmake/Modules/FindLibSigMF.cmake new file mode 100644 index 000000000..9db6f4855 --- /dev/null +++ b/cmake/Modules/FindLibSigMF.cmake @@ -0,0 +1,36 @@ +# Find libdsdcc + +if (NOT LIBSIGMF_FOUND) + + pkg_check_modules(LIBSIGMF_PKG libsigmf) + + find_path (LIBSIGMF_INCLUDE_DIR + NAMES libsigmf/sigmf_sdrangel_generated.h + HINTS ${LIBSIGMF_DIR}/include + ${LIBSIGMF_PKG_INCLUDE_DIRS} + PATHS /usr/include/libsigmf + /usr/local/include/libsigmf + ) + + find_library (LIBSIGMF_LIBRARIES + NAMES libsigmf + HINTS ${LIBSIGMF_DIR}/lib + ${LIBSIGMF_DIR}/lib64 + ${LIBSIGMF_PKG_LIBRARY_DIRS} + PATHS /usr/lib + /usr/lib64 + /usr/local/lib + /usr/local/lib64 + ) + + if (LIBSIGMF_INCLUDE_DIR AND LIBSIGMF_LIBRARIES) + set(LIBSIGMF_FOUND TRUE CACHE INTERNAL "libsigmf found") + message(STATUS "Found libsigmf: ${LIBSIGMF_INCLUDE_DIR}, ${LIBSIGMF_LIBRARIES}") + else (LIBSIGMF_INCLUDE_DIR AND LIBSIGMF_LIBRARIES) + set(LIBSIGMF_FOUND FALSE CACHE INTERNAL "libdsdcc found") + message(STATUS "libsigmf not found.") + endif (LIBSIGMF_INCLUDE_DIR AND LIBSIGMF_LIBRARIES) + + mark_as_advanced(LIBSIGMF_INCLUDE_DIR LIBSIGMF_LIBRARIES) + +endif (NOT LIBSIGMF_FOUND) diff --git a/plugins/samplesource/rtlsdr/rtlsdrgui.cpp b/plugins/samplesource/rtlsdr/rtlsdrgui.cpp index ffa7e6fd1..33ad4feb9 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrgui.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrgui.cpp @@ -583,11 +583,15 @@ void RTLSDRGui::openDeviceSettingsDialog(const QPoint& p) void RTLSDRGui::openFileRecordDialog(const QPoint& p) { + if (ui->record->isChecked()) { // do not fire up dialog if recording is on-going + return; + } + QFileDialog fileDialog( this, tr("Save I/Q record file"), m_settings.m_fileRecordName, - tr("SDR I/Q Files (*.sdriq)") + tr("SDR I/Q Files (*.sdriq);;SigMF Files (*.sigmf-meta);;All files (*.*)") ); fileDialog.setOptions(QFileDialog::DontUseNativeDialog); diff --git a/plugins/samplesource/rtlsdr/rtlsdrinput.cpp b/plugins/samplesource/rtlsdr/rtlsdrinput.cpp index d2f1778bd..c6e7602dd 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrinput.cpp +++ b/plugins/samplesource/rtlsdr/rtlsdrinput.cpp @@ -36,6 +36,9 @@ #include "dsp/dspcommands.h" #include "dsp/dspengine.h" #include "dsp/filerecord.h" +#ifdef HAS_LIBSIGMF +#include "dsp/sigmffilerecord.h" +#endif MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgConfigureRTLSDR, Message) MESSAGE_CLASS_DEFINITION(RTLSDRInput::MsgFileRecord, Message) @@ -322,31 +325,57 @@ bool RTLSDRInput::handleMessage(const Message& message) MsgFileRecord& conf = (MsgFileRecord&) message; qDebug() << "RTLSDRInput::handleMessage: MsgFileRecord: " << conf.getStartStop(); - if (conf.getStartStop()) + QString fileBase; + FileRecordInterface::RecordType recordType = FileRecordInterface::guessTypeFromFileName(m_settings.m_fileRecordName, fileBase); + +#ifdef HAS_LIBSIGMF + if (recordType == FileRecordInterface::RecordTypeSigMF) { - if (m_fileSink) + if (conf.getStartStop()) + { + if (!m_fileSink) { + m_fileSink = new SigMFFileRecord(fileBase, m_deviceAPI->getHardwareId()); + } + + m_deviceAPI->addAncillarySink(m_fileSink); + m_fileSink->startRecording(); + } + else { m_deviceAPI->removeAncillarySink(m_fileSink); - delete m_fileSink; + m_fileSink->stopRecording(); } - - if (m_settings.m_fileRecordName.size() != 0) { - m_fileSink = new FileRecord(m_settings.m_fileRecordName); - } else { - m_fileSink = new FileRecord(FileRecordInterface::genUniqueFileName(m_deviceAPI->getDeviceUID())); - } - - m_deviceAPI->addAncillarySink(m_fileSink); - m_fileSink->startRecording(); } else { - m_fileSink->stopRecording(); - m_deviceAPI->removeAncillarySink(m_fileSink); - delete m_fileSink; - m_fileSink = nullptr; - } +#endif + if (conf.getStartStop()) + { + if (m_fileSink) + { + m_deviceAPI->removeAncillarySink(m_fileSink); + delete m_fileSink; + } + if (m_settings.m_fileRecordName.size() != 0) { + m_fileSink = new FileRecord(m_settings.m_fileRecordName); + } else { + m_fileSink = new FileRecord(FileRecordInterface::genUniqueFileName(m_deviceAPI->getDeviceUID())); + } + + m_deviceAPI->addAncillarySink(m_fileSink); + m_fileSink->startRecording(); + } + else + { + m_fileSink->stopRecording(); + m_deviceAPI->removeAncillarySink(m_fileSink); + delete m_fileSink; + m_fileSink = nullptr; + } +#ifdef HAS_LIBSIGMF + } +#endif return true; } else if (MsgStartStop::match(message)) @@ -563,6 +592,30 @@ bool RTLSDRInput::applySettings(const RTLSDRSettings& settings, bool force) } } + if ((m_settings.m_fileRecordName != settings.m_fileRecordName) || force) + { + reverseAPIKeys.append("fileRecordName"); + QString fileBase; + FileRecordInterface::RecordType recordType = FileRecordInterface::guessTypeFromFileName(settings.m_fileRecordName, fileBase); +#ifdef HAS_LIBSIGMF + if (recordType == FileRecordInterface::RecordTypeSigMF) + { + if (m_fileSink) { + m_fileSink->setFileName(fileBase); + } + } + else + { + if (m_fileSink) + { + m_deviceAPI->removeAncillarySink(m_fileSink); + delete m_fileSink; + m_fileSink = nullptr; + } + } +#endif + } + if (settings.m_useReverseAPI) { bool fullUpdate = ((m_settings.m_useReverseAPI != settings.m_useReverseAPI) && settings.m_useReverseAPI) || diff --git a/plugins/samplesource/rtlsdr/rtlsdrinput.h b/plugins/samplesource/rtlsdr/rtlsdrinput.h index 7beb9da88..c51643db2 100644 --- a/plugins/samplesource/rtlsdr/rtlsdrinput.h +++ b/plugins/samplesource/rtlsdr/rtlsdrinput.h @@ -23,10 +23,12 @@ #include #include -#include -#include "rtlsdrsettings.h" #include +#include "dsp/devicesamplesource.h" +#include "dsp/filerecordinterface.h" +#include "rtlsdrsettings.h" + class DeviceAPI; class RTLSDRThread; class FileRecord; @@ -168,7 +170,7 @@ public: private: DeviceAPI *m_deviceAPI; - FileRecord *m_fileSink; //!< File sink to record device I/Q output + FileRecordInterface *m_fileSink; //!< File sink to record device I/Q output QMutex m_mutex; RTLSDRSettings m_settings; rtlsdr_dev_t* m_dev; diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt index e5ee119be..ce72b08b9 100644 --- a/sdrbase/CMakeLists.txt +++ b/sdrbase/CMakeLists.txt @@ -6,7 +6,6 @@ if(WIN32) endif() find_package(Opus REQUIRED) -find_package(LibSigMF) if(FFTW3F_FOUND) set(sdrbase_SOURCES @@ -46,6 +45,19 @@ if (LIMESUITE_FOUND) set(sdrbase_LIMERFE_LIB ${LIMESUITE_LIBRARY}) endif (LIMESUITE_FOUND) +if (LIBSIGMF_FOUND) + set(sdrbase_SOURCES + ${sdrbase_SOURCES} + dsp/sigmffilerecord.cpp + ) + set(sdrbase_HEADERS + ${sdrbase_HEADERS} + dsp/sigmffilerecord.h + ) + include_directories(${LIBSIGMF_INCLUDE_DIR}) + set(sdrbase_LIBSIGMF_LIB ${LIBSIGMF_LIBRARIES}) +endif (LIBSIGMF_FOUND) + # serialdv now required add_definitions(-DDSD_USE_SERIALDV) include_directories(${LIBSERIALDV_INCLUDE_DIR}) diff --git a/sdrbase/dsp/filerecord.cpp b/sdrbase/dsp/filerecord.cpp index 5fca6b04a..e2302fcb6 100644 --- a/sdrbase/dsp/filerecord.cpp +++ b/sdrbase/dsp/filerecord.cpp @@ -27,7 +27,7 @@ #include "filerecord.h" FileRecord::FileRecord() : - BasebandSampleSink(), + FileRecordInterface(), m_fileName("test.sdriq"), m_sampleRate(0), m_centerFrequency(0), @@ -39,7 +39,7 @@ FileRecord::FileRecord() : } FileRecord::FileRecord(const QString& filename) : - BasebandSampleSink(), + FileRecordInterface(), m_fileName(filename), m_sampleRate(0), m_centerFrequency(0), diff --git a/sdrbase/dsp/filerecord.h b/sdrbase/dsp/filerecord.h index 69490e0a5..ec0bd5ee6 100644 --- a/sdrbase/dsp/filerecord.h +++ b/sdrbase/dsp/filerecord.h @@ -23,13 +23,12 @@ #include #include -#include "dsp/basebandsamplesink.h" #include "dsp/filerecordinterface.h" #include "export.h" class Message; -class SDRBASE_API FileRecord : public BasebandSampleSink, public FileRecordInterface { +class SDRBASE_API FileRecord : public FileRecordInterface { public: #pragma pack(push, 1) diff --git a/sdrbase/dsp/filerecordinterface.cpp b/sdrbase/dsp/filerecordinterface.cpp index a06fa8bb1..4dde783a8 100644 --- a/sdrbase/dsp/filerecordinterface.cpp +++ b/sdrbase/dsp/filerecordinterface.cpp @@ -21,6 +21,13 @@ #include "filerecordinterface.h" +FileRecordInterface::FileRecordInterface() : + BasebandSampleSink() +{} + +FileRecordInterface::~FileRecordInterface() +{} + QString FileRecordInterface::genUniqueFileName(unsigned int deviceUID, int istream) { if (istream < 0) { @@ -29,3 +36,35 @@ QString FileRecordInterface::genUniqueFileName(unsigned int deviceUID, int istre return QString("rec%1_%2_%3.sdriq").arg(deviceUID).arg(istream).arg(QDateTime::currentDateTimeUtc().toString("yyyy-MM-ddTHH_mm_ss_zzz")); } } + +FileRecordInterface::RecordType FileRecordInterface::guessTypeFromFileName(const QString& fileName, QString& fileBase) +{ + QStringList dotBreakout = fileName.split(QLatin1Char('.')); + + if (dotBreakout.length() > 1) + { + QString extension = dotBreakout.last(); + dotBreakout.removeLast(); + + if (extension == "sdriq") + { + fileBase = dotBreakout.join(QLatin1Char('.')); + return RecordTypeSdrIQ; + } + else if (extension == "sigmf-meta") + { + fileBase = dotBreakout.join(QLatin1Char('.')); + return RecordTypeSigMF; + } + else + { + fileBase = fileName; + return RecordTypeUndefined; + } + } + else + { + fileBase = fileName; + return RecordTypeUndefined; + } +} diff --git a/sdrbase/dsp/filerecordinterface.h b/sdrbase/dsp/filerecordinterface.h index 6762c420f..e1c228a77 100644 --- a/sdrbase/dsp/filerecordinterface.h +++ b/sdrbase/dsp/filerecordinterface.h @@ -22,15 +22,33 @@ #include +#include "dsp/basebandsamplesink.h" #include "export.h" -class SDRBASE_API FileRecordInterface { +class SDRBASE_API FileRecordInterface : public BasebandSampleSink { public: + enum RecordType + { + RecordTypeUndefined = 0, + RecordTypeSdrIQ, + RecordTypeSigMF + }; + + FileRecordInterface(); + virtual ~FileRecordInterface(); + + virtual void start() = 0; + virtual void stop() = 0; + virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly) = 0; + virtual bool handleMessage(const Message& cmd) = 0; //!< Processing of a message. Returns true if message has actually been processed + virtual void setFileName(const QString &filename) = 0; virtual void startRecording() = 0; virtual void stopRecording() = 0; virtual bool isRecording() const = 0; + static QString genUniqueFileName(unsigned int deviceUID, int istream = -1); + static RecordType guessTypeFromFileName(const QString& fileName, QString& fileBase); }; diff --git a/sdrbase/dsp/sigmf_forward.h b/sdrbase/dsp/sigmf_forward.h new file mode 100644 index 000000000..e440fa65b --- /dev/null +++ b/sdrbase/dsp/sigmf_forward.h @@ -0,0 +1,64 @@ +/* + * Copyright 2019 DeepSig Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBSIGMF_SIGMF_FORWARD_H +#define LIBSIGMF_SIGMF_FORWARD_H + +namespace sigmf { + + template + class Global; + + template + class Captures; + + template + class Annotations; + + template + class SigMFVector : public std::vector { + public: + T &create_new() { + T new_element; + this->emplace_back(new_element); + return this->back(); + } + }; + + template + struct SigMF { + GlobalType global; + SigMFVector captures; + SigMFVector annotations; + }; + +} + +// Missing bits... + +namespace core { + class DescrT; +} +namespace sdrangel { + class DescrT; +} + +namespace sigmf { + template class Capture; + template class Annotation; +} + +#endif // LIBSIGMF_SIGMF_FORWARD_H \ No newline at end of file diff --git a/sdrbase/dsp/sigmffilerecord.cpp b/sdrbase/dsp/sigmffilerecord.cpp new file mode 100644 index 000000000..5ecddec43 --- /dev/null +++ b/sdrbase/dsp/sigmffilerecord.cpp @@ -0,0 +1,238 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Edouard Griffiths, F4EXB // +// // +// File recorder in SigMF format single channel for SI plugins // +// // +// 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 "libsigmf/sigmf_core_generated.h" +#include "libsigmf/sigmf_sdrangel_generated.h" +#include "libsigmf/sigmf.h" + +#include "dsp/dspcommands.h" +#include "util/sha512.h" + +#include "sigmffilerecord.h" + +SigMFFileRecord::SigMFFileRecord() : + FileRecordInterface(), + m_fileName("test"), + m_sampleRate(0), + m_centerFrequency(0), + m_recordOn(false), + m_recordStart(true), + m_sampleStart(0), + m_sampleCount(0) +{ + qDebug("SigMFFileRecord::SigMFFileRecord: test"); + setObjectName("SigMFFileSink"); + m_metaRecord = new sigmf::SigMF, + sigmf::Capture, + sigmf::Annotation >(); +} + +SigMFFileRecord::SigMFFileRecord(const QString& fileName, const QString& hardwareId) : + FileRecordInterface(), + m_hardwareId(hardwareId), + m_fileName(fileName), + m_sampleRate(0), + m_centerFrequency(0), + m_recordOn(false), + m_recordStart(true), + m_sampleStart(0), + m_sampleCount(0) +{ + qDebug("SigMFFileRecord::SigMFFileRecord: %s", qPrintable(fileName)); + setObjectName("SigMFFileSink"); + m_metaRecord = new sigmf::SigMF, + sigmf::Capture, + sigmf::Annotation >(); +} + +SigMFFileRecord::~SigMFFileRecord() +{ + qDebug("SigMFFileRecord::~SigMFFileRecord"); + + stopRecording(); + + if (m_metaFile.is_open()) { + m_metaFile.close(); + } + + if (m_sampleFile.is_open()) { + m_sampleFile.close(); + } + + delete m_metaRecord; +} + +void SigMFFileRecord::setFileName(const QString& fileName) +{ + if (!m_recordOn) + { + qDebug("SigMFFileRecord::setFileName: %s", qPrintable(fileName)); + + if (m_metaFile.is_open()) { + m_metaFile.close(); + } + + if (m_sampleFile.is_open()) { + m_sampleFile.close(); + } + + m_fileName = fileName; + m_recordStart = true; + } +} + +void SigMFFileRecord::startRecording() +{ + + if (m_recordStart) + { + qDebug("SigMFFileRecord::startRecording: new record %s", qPrintable(m_fileName)); + clearMeta(); + m_sampleFileName = m_fileName + ".sigmf-data"; + m_metaFileName = m_fileName + ".sigmf-meta"; + m_sampleFile.open(m_sampleFileName.toStdString().c_str(), std::ios::binary); + m_metaFile.open(m_metaFileName.toStdString().c_str(), std::ofstream::out); + makeHeader(); + m_recordStart = false; + } + else + { + qDebug("SigMFFileRecord::startRecording: start new capture"); + } + + m_recordOn = true; + m_sampleCount = 0; +} + +void SigMFFileRecord::stopRecording() +{ + if (m_recordOn) + { + qDebug("SigMFFileRecord::stopRecording: file previous capture"); + makeCapture(); + m_recordOn = false; + } +} + +void SigMFFileRecord::makeHeader() +{ + m_metaRecord->global.access().author = "SDRangel"; + m_metaRecord->global.access().description = "SDRangel SigMF I/Q recording file"; + m_metaRecord->global.access().sample_rate = m_sampleRate; + m_metaRecord->global.access().hw = m_hardwareId.toStdString(); + m_metaRecord->global.access().recorder = QString(QCoreApplication::applicationName()).toStdString(); + m_metaRecord->global.access().version = "0.0.2"; + m_metaRecord->global.access().version = QString(QCoreApplication::applicationVersion()).toStdString(); + m_metaRecord->global.access().qt_version = QT_VERSION_STR; + m_metaRecord->global.access().rx_bits = SDR_RX_SAMP_SZ; + m_metaRecord->global.access().arch = QString(QSysInfo::currentCpuArchitecture()).toStdString(); + m_metaRecord->global.access().os = QString(QSysInfo::prettyProductName()).toStdString(); + QString endianSuffix = QSysInfo::ByteOrder == QSysInfo::LittleEndian ? "le" : "be"; + int size = 8*sizeof(FixReal); + m_metaRecord->global.access().datatype = QString("ci%1_%2").arg(size).arg(endianSuffix).toStdString(); +} + +void SigMFFileRecord::makeCapture() +{ + if (m_sampleCount) + { + qDebug("SigMFFileRecord::makeCapture: m_sampleStart: %llu m_sampleCount: %llu", m_sampleStart, m_sampleCount); + // Flush samples to disk + m_sampleFile.flush(); + // calculate SHA512 and write it to header + m_metaRecord->global.access().sha512 = sw::sha512::file(m_sampleFileName.toStdString()); + // Add new capture + auto recording_capture = sigmf::Capture(); + recording_capture.get().frequency = m_centerFrequency; + recording_capture.get().sample_start = m_sampleStart; + recording_capture.get().length = m_sampleCount; + QDateTime utcnow = QDateTime::currentDateTimeUtc(); + recording_capture.get().datetime = utcnow.toString("yyyy-MM-ddTHH:mm:ss.zzzZ").toStdString(); + recording_capture.get().sample_rate = m_sampleRate; + recording_capture.get().tsms = utcnow.toMSecsSinceEpoch(); + m_metaRecord->captures.emplace_back(recording_capture); + m_sampleStart += m_sampleCount; + // Flush meta to disk + m_metaFile.seekp(0); + std::string jsonRecord = json(*m_metaRecord).dump(2); + m_metaFile << jsonRecord; + m_metaFile.flush(); + } + else + { + qDebug("SigMFFileRecord::makeCapture: skipped because of no samples"); + } +} + +void SigMFFileRecord::clearMeta() +{ + m_metaRecord->captures.clear(); +} + +void SigMFFileRecord::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly) +{ + (void) positiveOnly; + // if no recording is active, send the samples to /dev/null + if(!m_recordOn) + return; + + if (begin < end) // if there is something to put out + { + m_sampleFile.write(reinterpret_cast(&*(begin)), (end - begin)*sizeof(Sample)); + m_sampleCount += end - begin; + } +} + +void SigMFFileRecord::start() +{ +} + +void SigMFFileRecord::stop() +{ + stopRecording(); +} + +bool SigMFFileRecord::handleMessage(const Message& message) +{ + if (DSPSignalNotification::match(message)) + { + if (m_recordOn) { + makeCapture(); + m_sampleCount = 0; + } + + DSPSignalNotification& notif = (DSPSignalNotification&) message; + m_sampleRate = notif.getSampleRate(); + m_centerFrequency = notif.getCenterFrequency(); + qDebug() << "SigMFFileRecord::handleMessage: DSPSignalNotification: " + << " m_inputSampleRate: " << m_sampleRate + << " m_centerFrequency: " << m_centerFrequency; + + return true; + } + else + { + return false; + } +} \ No newline at end of file diff --git a/sdrbase/dsp/sigmffilerecord.h b/sdrbase/dsp/sigmffilerecord.h new file mode 100644 index 000000000..b42f1c0fa --- /dev/null +++ b/sdrbase/dsp/sigmffilerecord.h @@ -0,0 +1,73 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2020 Edouard Griffiths, F4EXB // +// // +// File recorder in SigMF format single channel for SI plugins // +// // +// 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_SIGMF_FILERECORD_H +#define INCLUDE_SIGMF_FILERECORD_H + +#include +#include +#include +#include + +#include "dsp/sigmf_forward.h" +#include "dsp/filerecordinterface.h" +#include "export.h" + +class Message; + +class SDRBASE_API SigMFFileRecord : public FileRecordInterface { +public: + SigMFFileRecord(); + SigMFFileRecord(const QString& filename, const QString& hardwareId); + virtual ~SigMFFileRecord(); + + virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool positiveOnly); + virtual void start(); + virtual void stop(); + virtual bool handleMessage(const Message& message); + + virtual void setFileName(const QString& filename); + virtual void startRecording(); + virtual void stopRecording(); + virtual bool isRecording() const { return m_recordOn; } + + void setHardwareId(const QString& hardwareId) { m_hardwareId = hardwareId; } + +private: + QString m_hardwareId; + QString m_fileName; + QString m_sampleFileName; + QString m_metaFileName; + quint32 m_sampleRate; + quint64 m_centerFrequency; + bool m_recordOn; + bool m_recordStart; + std::ofstream m_metaFile; + std::ofstream m_sampleFile; + quint64 m_sampleStart; + quint64 m_sampleCount; + sigmf::SigMF, + sigmf::Capture, + sigmf::Annotation > *m_metaRecord; + void makeHeader(); + void makeCapture(); + void clearMeta(); +}; + +#endif // INCLUDE_SIGMF_FILERECORD_H diff --git a/sdrbase/util/sha512.h b/sdrbase/util/sha512.h new file mode 100644 index 000000000..7d9327891 --- /dev/null +++ b/sdrbase/util/sha512.h @@ -0,0 +1,298 @@ +/** + * @file sha512.hh + * @author Stefan Wilhelm (stfwi) + * @ccflags + * @ldflags + * @platform linux, bsd, windows + * @standard >= c++98 + * + * SHA512 calculation class template. + * + * ------------------------------------------------------------------------------------- + * +++ BSD license header +++ + * Copyright (c) 2010, 2012, Stefan Wilhelm (stfwi, ) + * All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: (1) Redistributions + * of source code must retain the above copyright notice, this list of conditions + * and the following disclaimer. (2) Redistributions in binary form must reproduce + * the above copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the distribution. + * (3) Neither the name of the project nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific + * prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS + * AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT + * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY + * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * ------------------------------------------------------------------------------------- + * 01-2013: (stfwi) Class to template class, reformatting + */ +#ifndef SHA512_HH +#define SHA512_HH + +#if defined(OS_WIN) || defined (_WINDOWS_) || defined(_WIN32) || defined(__MSC_VER) +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include + +namespace sw { namespace detail { + +/** + * @class basic_sha512 + * @template + */ +template +class basic_sha512 +{ +public: + + /** + * Types + */ + typedef std::basic_string str_t; + +public: + + /** + * Constructor + */ + basic_sha512() + { clear(); } + + /** + * Destructor + */ + ~basic_sha512() + { ; } + +public: + + /** + * Clear/reset all internal buffers and states. + */ + void clear() + { + sum_[0] = 0x6a09e667f3bcc908; sum_[1] = 0xbb67ae8584caa73b; + sum_[2] = 0x3c6ef372fe94f82b; sum_[3] = 0xa54ff53a5f1d36f1; + sum_[4] = 0x510e527fade682d1; sum_[5] = 0x9b05688c2b3e6c1f; + sum_[6] = 0x1f83d9abfb41bd6b; sum_[7] = 0x5be0cd19137e2179; + sz_ = 0; iterations_ = 0; memset(&block_, 0, sizeof(block_)); + } + + /** + * Push new binary data into the internal buf_ and recalculate the checksum. + * @param const void* data + * @param size_t size + */ + void update(const void* data, size_t size) + { + unsigned nb, n, n_tail; + const uint8_t *p; + n = 128 - sz_; + n_tail = size < n ? size : n; + memcpy(&block_[sz_], data, n_tail); + if (sz_ + size < 128) { sz_ += size; return; } + n = size - n_tail; + nb = n >> 7; + p = (const uint8_t*) data + n_tail; + transform(block_, 1); + transform(p, nb); + n_tail = n & 0x7f; + memcpy(block_, &p[nb << 7], n_tail); + sz_ = n_tail; + iterations_ += (nb+1) << 7; + } + + /** + * Finanlise checksum, return hex string. + * @return str_t + */ + str_t final_data() + { + #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN)) + #define U32_B(x,b) *((b)+0)=(uint8_t)((x)); *((b)+1)=(uint8_t)((x)>>8); \ + *((b)+2)=(uint8_t)((x)>>16); *((b)+3)=(uint8_t)((x)>>24); + #else + #define U32_B(x,b) *((b)+3)=(uint8_t)((x)); *((b)+2)=(uint8_t)((x)>>8); \ + *((b)+1)=(uint8_t)((x)>>16); *((b)+0)=(uint8_t)((x)>>24); + #endif + unsigned nb, n; + uint64_t n_total; + nb = 1 + ((0x80-17) < (sz_ & 0x7f)); + n_total = (iterations_ + sz_) << 3; + n = nb << 7; + memset(block_ + sz_, 0, n - sz_); + block_[sz_] = 0x80; + U32_B(n_total, block_ + n-4); + transform(block_, nb); + std::basic_stringstream ss; // hex string + for (unsigned i = 0; i < 8; ++i) { + ss << std::hex << std::setfill('0') << std::setw(16) << (sum_[i]); + } + clear(); + return ss.str(); + #undef U32_B + } + +public: + + /** + * Calculates the SHA256 for a given string. + * @param const str_t & s + * @return str_t + */ + static str_t calculate(const str_t & s) + { + basic_sha512 r; + r.update(s.data(), s.length()); + return r.final_data(); + } + + /** + * Calculates the SHA256 for a given C-string. + * @param const char* s + * @return str_t + */ + static str_t calculate(const void* data, size_t size) + { basic_sha512 r; r.update(data, size); return r.final_data(); } + + /** + * Calculates the SHA256 for a stream. Returns an empty string on error. + * @param std::istream & is + * @return str_t + */ + static str_t calculate(std::istream & is) + { + basic_sha512 r; + char data[64]; + while(is.good() && is.read(data, sizeof(data)).good()) { + r.update(data, sizeof(data)); + } + if(!is.eof()) return str_t(); + if(is.gcount()) r.update(data, is.gcount()); + return r.final_data(); + } + + /** + * Calculates the SHA256 checksum for a given file, either read binary or as text. + * @param const str_t & path + * @param bool binary = true + * @return str_t + */ + static str_t file(const str_t & path, bool binary=true) + { + std::ifstream fs; + fs.open(path.c_str(), binary ? (std::ios::in|std::ios::binary) : (std::ios::in)); + str_t s = calculate(fs); + fs.close(); + return s; + } + +private: + + /** + * Performs the SHA256 transformation on a given block + * @param uint32_t *block + */ + void transform(const uint8_t *data, size_t size) + { + #define SR(x, n) (x >> n) + #define RR(x, n) ((x >> n) | (x << ((sizeof(x) << 3) - n))) + #define RL(x, n) ((x << n) | (x >> ((sizeof(x) << 3) - n))) + #define CH(x, y, z) ((x & y) ^ (~x & z)) + #define MJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) + #define F1(x) (RR(x, 28) ^ RR(x, 34) ^ RR(x, 39)) + #define F2(x) (RR(x, 14) ^ RR(x, 18) ^ RR(x, 41)) + #define F3(x) (RR(x, 1) ^ RR(x, 8) ^ SR(x, 7)) + #define F4(x) (RR(x, 19) ^ RR(x, 61) ^ SR(x, 6)) + #if (defined (BYTE_ORDER)) && (defined (BIG_ENDIAN)) && ((BYTE_ORDER == BIG_ENDIAN)) + #define B_U64(b,x) *(x)=((uint64_t)*((b)+0))|((uint64_t)*((b)+1)<<8)|\ + ((uint64_t)*((b)+2)<<16)|((uint64_t)*((b)+3)<<24)|((uint64_t)*((b)+4)<<32)|\ + ((uint64_t)*((b)+5)<<40)|((uint64_t)*((b)+6)<<48)|((uint64_t)*((b)+7)<<56); + #else + #define B_U64(b,x) *(x)=((uint64_t)*((b)+7))|((uint64_t)*((b)+6)<<8)|\ + ((uint64_t)*((b)+5)<<16)|((uint64_t)*((b)+4)<<24)|((uint64_t)*((b)+3)<<32)|\ + ((uint64_t)*((b)+2)<<40)|((uint64_t)*((b)+1)<<48)|((uint64_t)*((b)+0)<<56); + #endif + uint64_t t, u, v[8], w[80]; + const uint8_t *tblock; + unsigned j; + for(unsigned i = 0; i < size; ++i) { + tblock = data + (i << 7); + for(j = 0; j < 16; ++j) B_U64(&tblock[j<<3], &w[j]); + for(j = 16; j < 80; ++j) w[j] = F4(w[j-2]) + w[j-7] + F3(w[j-15]) + w[j-16]; + for(j = 0; j < 8; ++j) v[j] = sum_[j]; + for(j = 0; j < 80; ++j) { + t = v[7] + F2(v[4]) + CH(v[4], v[5], v[6]) + lut_[j] + w[j]; + u = F1(v[0]) + MJ(v[0], v[1], v[2]); v[7] = v[6]; v[6] = v[5]; v[5] = v[4]; + v[4] = v[3] + t; v[3] = v[2]; v[2] = v[1]; v[1] = v[0]; v[0] = t + u; + } + for(j = 0; j < 8; ++j) sum_[j] += v[j]; + } + #undef SR + #undef RR + #undef RL + #undef CH + #undef MJ + #undef F1 + #undef F2 + #undef F3 + #undef F4 + #undef B_U64 + } + +private: + + uint64_t iterations_; // Number of iterations + uint64_t sum_[8]; // Intermediate checksum buffer + unsigned sz_; // Number of currently stored bytes in the block + uint8_t block_[256]; + static const uint64_t lut_[80]; // Lookup table +}; + +template +const uint64_t basic_sha512::lut_[80] = { + 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, + 0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, + 0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, + 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694, + 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, + 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, + 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, + 0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70, + 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df, + 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, + 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, + 0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, + 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, + 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, + 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, + 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, + 0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, + 0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b, + 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c, + 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817 +}; + +}} + +namespace sw { + typedef detail::basic_sha512<> sha512; +} + +#endif \ No newline at end of file