1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-07 08:24:43 -04:00

SDRdaemon plugin: new classes

This commit is contained in:
f4exb
2016-01-24 23:38:55 +01:00
parent 1850452601
commit 7a6882829a
8 changed files with 717 additions and 66 deletions
@@ -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
@@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include <cassert>
#include <cstring>
#include <iostream>
#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;
}
@@ -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 <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#ifndef PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_
#define PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_
#include <stdint.h>
#include <cstring>
#include <cstddef>
#include <lz4.h>
#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_ */
+185 -65
View File
@@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>198</width>
<height>133</height>
<width>343</width>
<height>207</height>
</rect>
</property>
<property name="sizePolicy">
@@ -23,22 +23,13 @@
</font>
</property>
<property name="windowTitle">
<string>BladeRF</string>
<string>SDRdaemon</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>3</number>
</property>
<property name="leftMargin">
<number>2</number>
</property>
<property name="topMargin">
<number>2</number>
</property>
<property name="rightMargin">
<number>2</number>
</property>
<property name="bottomMargin">
<property name="margin">
<number>2</number>
</property>
<item>
@@ -119,6 +110,187 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="addressLayout">
<item>
<widget class="QLabel" name="addressLabel">
<property name="text">
<string>Addr:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="address">
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="portLabel">
<property name="text">
<string>Port:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="port">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="addressApplyButton">
<property name="maximumSize">
<size>
<width>30</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string>OK</string>
</property>
</widget>
</item>
<item>
<spacer name="iaddressSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="rateTimeLayout">
<item>
<widget class="QLabel" name="sampleRateText">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="font">
<font>
<pointsize>8</pointsize>
</font>
</property>
<property name="toolTip">
<string>Record sample rate</string>
</property>
<property name="text">
<string>0k</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="Line" name="absTimeLine">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="absTimeText">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Record absolute time</string>
</property>
<property name="text">
<string>20150101 00:00:00.000</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="decimLayout">
<item>
<widget class="QLabel" name="label_decim">
<property name="text">
<string>Dec.</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="decim">
<property name="toolTip">
<string>Decimation factor</string>
</property>
<property name="maximum">
<number>6</number>
</property>
<property name="pageStep">
<number>1</number>
</property>
<property name="value">
<number>0</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="decimText">
<property name="text">
<string>1</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="decimRate">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>0k</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="fileSelectionLayout">
<item>
@@ -172,58 +344,6 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="rateTimeLayout">
<item>
<widget class="QLabel" name="sampleRateText">
<property name="font">
<font>
<pointsize>8</pointsize>
</font>
</property>
<property name="toolTip">
<string>Record sample rate</string>
</property>
<property name="text">
<string>0k</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="Line" name="absTimeLine">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="absTimeText">
<property name="enabled">
<bool>false</bool>
</property>
<property name="toolTip">
<string>Record absolute time</string>
</property>
<property name="text">
<string>20150101 00:00:00.000</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_rate">
<property name="orientation">
@@ -21,6 +21,7 @@
#include <QMutex>
#include <QWaitCondition>
#include <QTimer>
#include <iostream>
#include <fstream>
#include <cstdlib>
@@ -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;