From 7a6882829a681554e88a385a480bb1faa968ac40 Mon Sep 17 00:00:00 2001 From: f4exb Date: Sun, 24 Jan 2016 23:38:55 +0100 Subject: [PATCH] SDRdaemon plugin: new classes --- CMakeLists.txt | 4 +- include/util/CRC64.h | 38 +++ plugins/samplesource/sdrdaemon/CMakeLists.txt | 2 + .../sdrdaemon/sdrdaemonbuffer.cpp | 232 ++++++++++++++++ .../samplesource/sdrdaemon/sdrdaemonbuffer.h | 94 +++++++ .../samplesource/sdrdaemon/sdrdaemongui.ui | 250 +++++++++++++----- .../samplesource/sdrdaemon/sdrdaemonthread.h | 4 + sdrbase/util/CRC64.cpp | 159 +++++++++++ 8 files changed, 717 insertions(+), 66 deletions(-) create mode 100644 include/util/CRC64.h create mode 100644 plugins/samplesource/sdrdaemon/sdrdaemonbuffer.cpp create mode 100644 plugins/samplesource/sdrdaemon/sdrdaemonbuffer.h create mode 100644 sdrbase/util/CRC64.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c1d4e5687..9ce27a3db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ set(sdrbase_SOURCES sdrbase/settings/preset.cpp sdrbase/settings/mainsettings.cpp + sdrbase/util/CRC64.cpp sdrbase/util/db.cpp sdrbase/util/message.cpp sdrbase/util/messagequeue.cpp @@ -208,7 +209,8 @@ set(sdrbase_HEADERS include/settings/preset.h include/settings/mainsettings.h - include/util/db.h + include/util/CRC64.h + include/util/db.h include/util/export.h include/util/message.h include/util/messagequeue.h diff --git a/include/util/CRC64.h b/include/util/CRC64.h new file mode 100644 index 000000000..bede1df55 --- /dev/null +++ b/include/util/CRC64.h @@ -0,0 +1,38 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2016 Edouard Griffiths, F4EXB // +// // +// 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 // +// // +// 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_CRC64_H_ +#define INCLUDE_CRC64_H_ + +#include + +class CRC64 +{ +public: + CRC64(); + ~CRC64(); + uint64_t calculate_crc(uint8_t *stream, int length); + +private: + void build_crc_table(); + + uint64_t m_crcTable[256]; + static const uint64_t m_poly; +}; + + + +#endif /* INCLUDE_CRC64_H_ */ diff --git a/plugins/samplesource/sdrdaemon/CMakeLists.txt b/plugins/samplesource/sdrdaemon/CMakeLists.txt index 0ae1cb3b0..2fc7bb122 100644 --- a/plugins/samplesource/sdrdaemon/CMakeLists.txt +++ b/plugins/samplesource/sdrdaemon/CMakeLists.txt @@ -3,6 +3,7 @@ project(sdrdaemon) find_package(LZ4) set(sdrdaemon_SOURCES + sdrdaemonbuffer.cpp sdrdaemongui.cpp sdrdaemoninput.cpp sdrdaemonplugin.cpp @@ -10,6 +11,7 @@ set(sdrdaemon_SOURCES ) set(sdrdaemon_HEADERS + sdrdaemonbuffer.h sdrdaemongui.h sdrdaemoninput.h sdrdaemonplugin.h diff --git a/plugins/samplesource/sdrdaemon/sdrdaemonbuffer.cpp b/plugins/samplesource/sdrdaemon/sdrdaemonbuffer.cpp new file mode 100644 index 000000000..a468eaa1f --- /dev/null +++ b/plugins/samplesource/sdrdaemon/sdrdaemonbuffer.cpp @@ -0,0 +1,232 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2016 Edouard Griffiths, F4EXB // +// // +// 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 // +// // +// 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 "sdrdaemonbuffer.h" + +SDRdaemonBuffer::SDRdaemonBuffer(std::size_t blockSize) : + m_blockSize(blockSize), + m_sync(false), + m_lz4(false), + m_lz4InBuffer(0), + m_lz4InCount(0), + m_lz4InSize(0), + m_lz4OutBuffer(0), + m_lz4OutSize(0), + m_nbDecodes(0), + m_nbSuccessfulDecodes(0), + m_nbCRCOK(0), + m_dataCRC(0) +{ + m_buf = new uint8_t[blockSize]; + m_currentMeta.init(); +} + +SDRdaemonBuffer::~SDRdaemonBuffer() +{ + delete[] m_buf; +} + +bool SDRdaemonBuffer::writeAndRead(uint8_t *array, std::size_t length, uint8_t *data, std::size_t& dataLength) +{ + assert(length == m_blockSize); // TODO: allow fragmented blocks with larger size + MetaData *metaData = (MetaData *) array; + + if (m_crc64.calculate_crc(array, sizeof(MetaData) - 8) == metaData->m_crc) + { + dataLength = 0; + memcpy((void *) &m_dataCRC, (const void *) &array[sizeof(MetaData)], 8); + + if (!(m_currentMeta == *metaData)) + { + std::cerr << "SDRdaemonBuffer::writeAndRead: "; + printMeta(metaData); + } + + m_currentMeta = *metaData; + + // sanity checks + if (metaData->m_blockSize == m_blockSize) // sent blocksize matches given blocksize + { + if (metaData->m_sampleBytes & 0x10) + { + m_lz4 = true; + updateSizes(metaData); + } + else + { + m_lz4 = false; + } + + m_sync = true; + } + else + { + m_sync = false; + } + + return false; + } + else + { + if (m_sync) + { + if (m_lz4) + { + return writeAndReadLZ4(array, length, data, dataLength); + } + else + { + std::memcpy((void *) data, (const void *) array, length); + dataLength = length; + return true; + } + } + else + { + dataLength = 0; + return false; + } + } +} + +bool SDRdaemonBuffer::writeAndReadLZ4(uint8_t *array, std::size_t length, uint8_t *data, std::size_t& dataLength) +{ + if (m_lz4InCount + length < m_lz4InSize) + { + std::memcpy((void *) &m_lz4InBuffer[m_lz4InCount], (const void *) array, length); // copy data in compressed Buffer + dataLength = 0; + m_lz4InCount += length; + } + else + { + std::memcpy((void *) &m_lz4InBuffer[m_lz4InCount], (const void *) array, m_lz4InSize - m_lz4InCount); // copy rest of data in compressed Buffer + m_lz4InCount += length; + } + + if (m_lz4InCount >= m_lz4InSize) // full input compressed block retrieved + { + if (m_nbDecodes == 100) + { + std::cerr << "SDRdaemonBuffer::writeAndReadLZ4:" + << " decoding: " << m_nbCRCOK + << ":" << m_nbSuccessfulDecodes + << "/" << m_nbDecodes + << std::endl; + + m_nbDecodes = 0; + m_nbSuccessfulDecodes = 0; + m_nbCRCOK = 0; + } + + uint64_t crc64 = m_crc64.calculate_crc(m_lz4InBuffer, m_lz4InSize); + //uint64_t crc64 = 0x0123456789ABCDEF; + + if (memcmp(&crc64, &m_dataCRC, 8) == 0) + { + m_nbCRCOK++; + } + + int compressedSize = LZ4_decompress_fast((const char*) m_lz4InBuffer, (char*) m_lz4OutBuffer, m_lz4OutSize); + m_nbDecodes++; + + if (compressedSize == m_lz4InSize) + { + /* + std::cerr << "SDRdaemonBuffer::writeAndReadLZ4: decoding OK:" + << " read: " << compressedSize + << " expected: " << m_lz4InSize + << " out: " << m_lz4OutSize + << std::endl; + */ + std::memcpy((void *) data, (const void *) m_lz4OutBuffer, m_lz4OutSize); // send what is in buffer + dataLength = m_lz4OutSize; + m_nbSuccessfulDecodes++; + } + else + { +// std::cerr << "SDRdaemonBuffer::writeAndReadLZ4: decoding error:" +// << " read: " << compressedSize +// << " expected: " << m_lz4InSize +// << " out: " << m_lz4OutSize +// << std::endl; + + //if (compressedSize > 0) + //{ + std::memcpy((void *) data, (const void *) m_lz4OutBuffer, m_lz4OutSize); // send what is in buffer + dataLength = m_lz4OutSize; + //} + //else + //{ + // dataLength = 0; + //} + } + + m_lz4InCount = 0; + } + + return dataLength != 0; +} + +void SDRdaemonBuffer::updateSizes(MetaData *metaData) +{ + m_lz4InSize = metaData->m_nbBytes; // compressed input size + uint32_t sampleBytes = metaData->m_sampleBytes & 0x0F; + uint32_t originalSize = sampleBytes * 2 * metaData->m_nbSamples * metaData->m_nbBlocks; + + if (originalSize != m_lz4OutSize) + { + uint32_t masInputSize = LZ4_compressBound(originalSize); + + if (m_lz4InBuffer) { + delete[] m_lz4InBuffer; + } + + m_lz4InBuffer = new uint8_t[m_lz4InSize]; // provide extra space for a full UDP block + + if (m_lz4OutBuffer) { + delete[] m_lz4OutBuffer; + } + + m_lz4OutBuffer = new uint8_t[originalSize]; + m_lz4OutSize = originalSize; + } + + m_lz4InCount = 0; +} + + +void SDRdaemonBuffer::printMeta(MetaData *metaData) +{ + std::cerr + << "|" << metaData->m_centerFrequency + << ":" << metaData->m_sampleRate + << ":" << (int) (metaData->m_sampleBytes & 0xF) + << ":" << (int) metaData->m_sampleBits + << ":" << metaData->m_blockSize + << ":" << metaData->m_nbSamples + << "||" << metaData->m_nbBlocks + << ":" << metaData->m_nbBytes + << "|" << metaData->m_tv_sec + << ":" << metaData->m_tv_usec + << std::endl; +} + + + + diff --git a/plugins/samplesource/sdrdaemon/sdrdaemonbuffer.h b/plugins/samplesource/sdrdaemon/sdrdaemonbuffer.h new file mode 100644 index 000000000..4a8e0de6a --- /dev/null +++ b/plugins/samplesource/sdrdaemon/sdrdaemonbuffer.h @@ -0,0 +1,94 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2016 Edouard Griffiths, F4EXB // +// // +// 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 // +// // +// 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 PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_ +#define PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_ + +#include +#include +#include +#include +#include "util/CRC64.h" + +class SDRdaemonBuffer +{ +public: +#pragma pack(push, 1) + struct MetaData + { + // critical data + uint64_t m_centerFrequency; //!< center frequency in Hz + uint32_t m_sampleRate; //!< sample rate in Hz + uint8_t m_sampleBytes; //!< MSB(4): indicators, LSB(4) number of bytes per sample + uint8_t m_sampleBits; //!< number of effective bits per sample + uint16_t m_blockSize; //!< payload size + uint32_t m_nbSamples; //!< number of samples in a hardware block + // end of critical data + uint16_t m_nbBlocks; //!< number of hardware blocks in the frame + uint32_t m_nbBytes; //!< total number of bytes in the frame + uint32_t m_tv_sec; //!< seconds of timestamp at start time of frame processing + uint32_t m_tv_usec; //!< microseconds of timestamp at start time of frame processing + uint64_t m_crc; //!< 64 bit CRC of the above + + bool operator==(const MetaData& rhs) + { + return (memcmp((const void *) this, (const void *) &rhs, 20) == 0); // Only the 20 first bytes are relevant (critical) + } + + void init() + { + memset((void *) this, 0, sizeof(MetaData)); + } + + void operator=(const MetaData& rhs) + { + memcpy((void *) this, (const void *) &rhs, sizeof(MetaData)); + } + }; +#pragma pack(pop) + + SDRdaemonBuffer(std::size_t blockSize); + ~SDRdaemonBuffer(); + bool writeAndRead(uint8_t *array, std::size_t length, uint8_t *data, std::size_t& dataLength); + const MetaData& getCurrentMeta() const { return m_currentMeta; } + +private: + bool writeAndReadLZ4(uint8_t *array, std::size_t length, uint8_t *data, std::size_t& dataLength); + void updateSizes(MetaData *metaData); + void printMeta(MetaData *metaData); + + std::size_t m_blockSize; //!< UDP block (payload) size + bool m_sync; //!< Meta data acquired (Stream synchronized) + bool m_lz4; //!< Stream is compressed with LZ4 + MetaData m_currentMeta; //!< Stored current meta data + CRC64 m_crc64; //!< CRC64 calculator + uint8_t *m_buf; //!< UDP block buffer + + uint8_t *m_lz4InBuffer; //!< Buffer for LZ4 compressed input + uint32_t m_lz4InCount; //!< Current position in LZ4 input buffer + uint32_t m_lz4InSize; //!< Size in bytes of the LZ4 input data + uint8_t *m_lz4OutBuffer; //!< Buffer for LZ4 uncompressed output + uint32_t m_lz4OutSize; //!< Size in bytes of the LZ4 output data (original uncomressed data) + uint32_t m_nbDecodes; + uint32_t m_nbSuccessfulDecodes; + uint32_t m_nbCRCOK; + uint64_t m_dataCRC; + +}; + + + +#endif /* PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_ */ diff --git a/plugins/samplesource/sdrdaemon/sdrdaemongui.ui b/plugins/samplesource/sdrdaemon/sdrdaemongui.ui index 9dcf1e942..228619b6a 100644 --- a/plugins/samplesource/sdrdaemon/sdrdaemongui.ui +++ b/plugins/samplesource/sdrdaemon/sdrdaemongui.ui @@ -6,8 +6,8 @@ 0 0 - 198 - 133 + 343 + 207 @@ -23,22 +23,13 @@ - BladeRF + SDRdaemon 3 - - 2 - - - 2 - - - 2 - - + 2 @@ -119,6 +110,187 @@ + + + + + + Addr: + + + + + + + + 120 + 0 + + + + + + + + Port: + + + + + + + + 50 + 0 + + + + + + + + + 30 + 16777215 + + + + OK + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + + + + 50 + 0 + + + + + 8 + + + + Record sample rate + + + 0k + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Vertical + + + + + + + false + + + Record absolute time + + + 20150101 00:00:00.000 + + + + + + + + + + + Dec. + + + + + + + Decimation factor + + + 6 + + + 1 + + + 0 + + + Qt::Horizontal + + + + + + + 1 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 50 + 0 + + + + 0k + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + @@ -172,58 +344,6 @@ - - - - - - - 8 - - - - Record sample rate - - - 0k - - - - - - - Qt::Horizontal - - - - 40 - 20 - - - - - - - - Qt::Vertical - - - - - - - false - - - Record absolute time - - - 20150101 00:00:00.000 - - - - - diff --git a/plugins/samplesource/sdrdaemon/sdrdaemonthread.h b/plugins/samplesource/sdrdaemon/sdrdaemonthread.h index 9d3ff1d3a..3464a095e 100644 --- a/plugins/samplesource/sdrdaemon/sdrdaemonthread.h +++ b/plugins/samplesource/sdrdaemon/sdrdaemonthread.h @@ -21,6 +21,7 @@ #include #include #include + #include #include #include @@ -29,6 +30,8 @@ #define SDRDAEMON_THROTTLE_MS 50 +class QUdpSocket; + class SDRdaemonThread : public QThread { Q_OBJECT @@ -50,6 +53,7 @@ private: bool m_running; std::ifstream* m_ifstream; + QUdpSocket *m_dataSocket; quint8 *m_buf; std::size_t m_bufsize; std::size_t m_chunksize; diff --git a/sdrbase/util/CRC64.cpp b/sdrbase/util/CRC64.cpp new file mode 100644 index 000000000..e07cf0d14 --- /dev/null +++ b/sdrbase/util/CRC64.cpp @@ -0,0 +1,159 @@ +/////////////////////////////////////////////////////////////////////////////////// +// Copyright (C) 2016 Edouard Griffiths, F4EXB // +// // +// 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 // +// // +// 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 "util/CRC64.h" + +/** +* poly is: x^64 + x^62 + x^57 + x^55 + x^54 + x^53 + x^52 + x^47 + x^46 + x^45 + x^40 + x^39 + +* x^38 + x^37 + x^35 + x^33 + x^32 + x^31 + x^29 + x^27 + x^24 + x^23 + x^22 + x^21 + +* x^19 + x^17 + x^13 + x^12 + x^10 + x^9 + x^7 + x^4 + x^1 + 1 +* +* represented here with lsb = highest degree term +* +* 1100100101101100010101111001010111010111100001110000111101000010_ +* || | | || || | | |||| | | ||| | |||| ||| |||| | | | +* || | | || || | | |||| | | ||| | |||| ||| |||| | | +- x^64 (implied) +* || | | || || | | |||| | | ||| | |||| ||| |||| | | +* || | | || || | | |||| | | ||| | |||| ||| |||| | +--- x^62 +* || | | || || | | |||| | | ||| | |||| ||| |||| +-------- x^57 +* ....................................................................... +* || +* |+---------------------------------------------------------------- x^1 +* +----------------------------------------------------------------- x^0 (1) +*/ +const uint64_t CRC64::m_poly = 0xC96C5795D7870F42ull; + +CRC64::CRC64() +{ + build_crc_table(); +} + +CRC64::~CRC64() +{} + +/** +* input is dividend: as 0000000000000000000000000000000000000000000000000000000000000000<8-bit byte> +* where the lsb of the 8-bit byte is the coefficient of the highest degree term (x^71) of the dividend +* so division is really for input byte * x^64 +* +* you may wonder how 72 bits will fit in 64-bit data type... well as the shift-right occurs, 0's are supplied +* on the left (most significant) side ... when the 8 shifts are done, the right side (where the input +* byte was placed) is discarded +* +* when done, table[XX] (where XX is a byte) is equal to the CRC of 00 00 00 00 00 00 00 00 XX +*/ +void CRC64::build_crc_table() +{ + for(int i = 0; i < 256; ++i) + { + uint64_t crc = i; + + for(unsigned int j = 0; j < 8; ++j) + { + if(crc & 1) // is current coefficient set? + { + crc >>= 1; // yes, then assume it gets zero'd (by implied x^64 coefficient of dividend) + crc ^= m_poly; // and add rest of the divisor + } + else // no? then move to next coefficient + { + crc >>= 1; + } + } + + m_crcTable[i] = crc; + } +} + +/** +* will give an example CRC calculation for input array {0xDE, 0xAD} +* +* each byte represents a group of 8 coefficients for 8 dividend terms +* +* the actual polynomial dividend is: +* +* = DE AD 00 00 00 00 00 00 00 00 (hex) +* = 11011110 10101101 0000000000000000000...0 (binary) +* || |||| | | || | +* || |||| | | || +------------------------ x^71 +* || |||| | | |+-------------------------- x^69 +* || |||| | | +--------------------------- x^68 +* || |||| | +----------------------------- x^66 +* || |||| +------------------------------- x^64 +* || |||| +* || |||+---------------------------------- x^78 +* || ||+----------------------------------- x^77 +* || |+------------------------------------ x^76 +* || +------------------------------------- x^75 +* |+--------------------------------------- x^73 +* +---------------------------------------- x^72 +* +* +* the basic idea behind how the table lookup results can be used with one +* another is that: +* +* Mod(A * x^n, P(x)) = Mod(x^n * Mod(A, P(X)), P(X)) +* +* in other words, an input data shifted towards the higher degree terms +* changes the pre-computed crc of the input data by shifting it also +* the same amount towards higher degree terms (mod the polynomial) +* +* here is an example: +* +* 1) input: +* +* 00 00 00 00 00 00 00 00 AD DE +* +* 2) index crc table for byte DE (really for dividend 00 00 00 00 00 00 00 00 DE) +* +* we get A8B4AFBDC5A6ACA4 +* +* 3) apply that to the input stream: +* +* 00 00 00 00 00 00 00 00 AD DE +* A8 B4 AF BD C5 A6 AC A4 +* ----------------------------- +* 00 A8 B4 AF BD C5 A6 AC 09 +* +* 4) index crc table for byte 09 (really for dividend 00 00 00 00 00 00 00 00 09) +* +* we get 448FCBB7FCB9E309 +* +* 5) apply that to the input stream +* +* 00 A8 B4 AF BD C5 A6 AC 09 +* 44 8F CB B7 FC B9 E3 09 +* -------------------------- +* 44 27 7F 18 41 7C 45 A5 +* +*/ +uint64_t CRC64::calculate_crc(uint8_t *stream, int length) +{ + uint64_t crc = 0; + + for (int i = 0 ; i < length; ++i) + { + uint8_t index = stream[i] ^ crc; + uint64_t lookup = m_crcTable[index]; + + crc >>= 8; + crc ^= lookup; + } + + return crc; +} + +