2016-01-24 17:38:55 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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"
|
2016-01-25 02:40:44 -05:00
|
|
|
#include "dsp/samplefifo.h"
|
2016-01-24 17:38:55 -05:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2016-01-27 22:54:16 -05:00
|
|
|
SDRdaemonBuffer(uint32_t blockSize);
|
2016-01-24 17:38:55 -05:00
|
|
|
~SDRdaemonBuffer();
|
2016-01-27 22:54:16 -05:00
|
|
|
bool readMeta(char *array, uint32_t length); //!< Attempt to read meta. Returns true if meta block
|
|
|
|
void writeData(char *array, uint32_t length); //!< Write data into buffer.
|
2016-01-29 02:17:34 -05:00
|
|
|
uint8_t *readData(uint32_t length); //!< Read data from buffer
|
2016-01-24 17:38:55 -05:00
|
|
|
const MetaData& getCurrentMeta() const { return m_currentMeta; }
|
2016-01-27 22:54:16 -05:00
|
|
|
void updateBlockCounts(uint32_t nbBytesReceived);
|
2016-01-26 17:48:52 -05:00
|
|
|
bool isSync() const { return m_sync; }
|
2016-01-24 17:38:55 -05:00
|
|
|
|
|
|
|
private:
|
2016-01-27 02:24:00 -05:00
|
|
|
void updateLZ4Sizes(uint32_t frameSize);
|
2016-01-27 22:54:16 -05:00
|
|
|
void writeDataLZ4(const char *array, uint32_t length);
|
|
|
|
void writeDataUncompressed(const char *array, uint32_t length);
|
|
|
|
void writeToRawBufferLZ4(const char *array, uint32_t originalLength);
|
|
|
|
void writeToRawBufferUncompressed(const char *array, uint32_t length);
|
2016-02-02 02:21:16 -05:00
|
|
|
void updateBufferSize(uint32_t sampleRate, uint32_t frameSize);
|
2016-01-24 17:38:55 -05:00
|
|
|
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
|
|
|
|
|
2016-01-27 02:40:54 -05:00
|
|
|
uint32_t m_inCount; //!< Current position of uncompressed input
|
2016-01-26 02:27:45 -05:00
|
|
|
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
|
2016-01-27 02:24:00 -05:00
|
|
|
uint32_t m_frameSize; //!< Size in bytes of one uncompressed frame
|
2016-01-24 17:38:55 -05:00
|
|
|
uint32_t m_nbDecodes;
|
|
|
|
uint32_t m_nbSuccessfulDecodes;
|
|
|
|
uint32_t m_nbCRCOK;
|
|
|
|
uint64_t m_dataCRC;
|
|
|
|
|
2016-01-26 02:27:45 -05:00
|
|
|
uint32_t m_sampleRate; //!< Current sample rate in Hz
|
|
|
|
uint8_t m_sampleBytes; //!< Current number of bytes per I or Q sample
|
|
|
|
uint8_t m_sampleBits; //!< Current number of effective bits per sample
|
|
|
|
|
2016-01-27 22:54:16 -05:00
|
|
|
uint32_t m_rawCount; //!< Current write position in the raw samples buffer
|
|
|
|
uint32_t m_readCount; //!< Current read position in the raw samples buffer
|
|
|
|
uint32_t m_rawSize; //!< Size of the raw samples buffer in bytes
|
2016-01-26 02:27:45 -05:00
|
|
|
uint8_t *m_rawBuffer; //!< Buffer for raw samples obtained from UDP (I/Q not in a formal I/Q structure)
|
2016-01-29 02:17:34 -05:00
|
|
|
uint8_t *m_frameBuffer; //!< Buffer to build a frame length of raw samples
|
2016-01-27 21:30:36 -05:00
|
|
|
uint32_t m_bytesInBlock; //!< Number of bytes received in the current UDP block
|
|
|
|
uint32_t m_nbBlocks; //!< Number of UDP blocks received in the current frame
|
2016-01-24 17:38:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* PLUGINS_SAMPLESOURCE_SDRDAEMON_SDRDAEMONBUFFER_H_ */
|