node.hh
#ifndef __SDR_NODE_HH__
#define __SDR_NODE_HH__
#include <set>
#include <list>
#include <iostream>
#include <complex>
#include <stdint.h>
#include <pthread.h>
#include "buffer.hh"
#include "queue.hh"
#include "exception.hh"
namespace sdr {
class Config
{
public:
typedef enum {
Type_UNDEFINED = 0,
Type_u8,
Type_s8,
Type_u16,
Type_s16,
Type_f32,
Type_f64,
Type_cu8,
Type_cs8,
Type_cu16,
Type_cs16,
Type_cf32,
Type_cf64
} Type;
public:
Config();
Config(Type type, double sampleRate, size_t bufferSize, size_t numBuffers);
Config(const Config &other);
const Config &operator= (const Config &other);
bool operator== (const Config &other) const;
inline bool hasType() const { return Type_UNDEFINED != _type; }
inline Type type() const { return _type; }
inline void setType(Type type) { _type = type; }
inline bool hasSampleRate() const { return 0 != _sampleRate; }
inline double sampleRate() const { return _sampleRate; }
inline void setSampleRate(double rate) { _sampleRate = rate; }
inline bool hasBufferSize() const { return 0 != _bufferSize; }
inline size_t bufferSize() const { return _bufferSize; }
inline void setBufferSize(size_t size) { _bufferSize = size; }
inline bool hasNumBuffers() const { return 0 != _numBuffers; }
inline size_t numBuffers() const { return _numBuffers; }
inline void setNumBuffers(size_t N) { _numBuffers = N; }
template <typename T> static inline Type typeId();
protected:
Type _type;
double _sampleRate;
size_t _bufferSize;
size_t _numBuffers;
};
template <>
inline Config::Type Config::typeId<uint8_t>() { return Type_u8; }
template <>
inline Config::Type Config::typeId<int8_t>() { return Type_s8; }
template <>
inline Config::Type Config::typeId<uint16_t>() { return Type_u16; }
template <>
inline Config::Type Config::typeId<int16_t>() { return Type_s16; }
template <>
inline Config::Type Config::typeId<float>() { return Type_f32; }
template <>
inline Config::Type Config::typeId<double>() { return Type_f64; }
template <>
inline Config::Type Config::typeId< std::complex<uint8_t> >() { return Type_cu8; }
template <>
inline Config::Type Config::typeId< std::complex<int8_t> >() { return Type_cs8; }
template <>
inline Config::Type Config::typeId< std::complex<uint16_t> >() { return Type_cu16; }
template <>
inline Config::Type Config::typeId< std::complex<int16_t> >() { return Type_cs16; }
template <>
inline Config::Type Config::typeId< std::complex<float> >() { return Type_cf32; }
template <>
inline Config::Type Config::typeId< std::complex<double> >() { return Type_cf64; }
inline const char *typeName(Config::Type type) {
switch (type) {
case Config::Type_UNDEFINED: return "UNDEFINED";
case Config::Type_u8: return "uint8";
case Config::Type_s8: return "int8";
case Config::Type_u16: return "uint16";
case Config::Type_s16: return "int16";
case Config::Type_f32: return "float";
case Config::Type_f64: return "double";
case Config::Type_cu8: return "complex uint8";
case Config::Type_cs8: return "complex int8";
case Config::Type_cu16: return "complex uint16";
case Config::Type_cs16: return "complex int16";
case Config::Type_cf32: return "complex float";
case Config::Type_cf64: return "complex double";
}
return "unknown";
}
inline std::ostream &operator<<(std::ostream &stream, Config::Type type) {
stream << typeName(type) << " (" << (int)type << ")";
return stream;
}
class SinkBase
{
public:
SinkBase();
virtual ~SinkBase();
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite) = 0;
virtual void config(const Config &src_cfg) = 0;
};
template <class Scalar>
class Sink: public SinkBase
{
public:
Sink() : SinkBase() { }
virtual ~Sink() { }
virtual void process(const Buffer<Scalar> &buffer, bool allow_overwrite) = 0;
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite) {
this->process(Buffer<Scalar>(buffer), allow_overwrite);
}
};
class Source
{
public:
Source();
virtual ~Source();
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false);
void connect(SinkBase *sink, bool direct=false);
void disconnect(SinkBase *sink);
virtual void setConfig(const Config &config);
virtual double sampleRate() const;
virtual Config::Type type() const;
template <class T>
void addEOS(T* instance, void (T::*function)()) {
_eos.push_back(new Delegate<T>(instance, function));
}
protected:
void signalEOS();
void propagateConfig(const Config &config);
protected:
Config _config;
std::map<SinkBase *, bool> _sinks;
std::list<DelegateInterface *> _eos;
};
class BlockingSource: public Source {
public:
BlockingSource(bool parallel=false, bool connect_idle=true, bool stop_queue_on_eos=false);
virtual ~BlockingSource();
virtual void next() = 0;
inline bool isActive() const { return _is_active; }
virtual void start();
virtual void stop();
protected:
void _parallel_main();
void _nonvirt_idle_cb();
protected:
bool _is_active;
bool _is_parallel;
pthread_t _thread;
private:
static void *_pthread_main_wrapper(void *);
};
class Proxy: public SinkBase, public Source
{
public:
Proxy();
virtual ~Proxy();
virtual void config(const Config &src_cfg);
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite);
};
}
#endif // __SDR_NODE_HH__
sdr::Config
A collection of configuration information that is send by a source to all connected sinks to properga...
Definition: node.hh:35
sdr::Config::Type_cf32
Complex (aka I/Q) type of 32bit floats aka. std::complex<float>.
Definition: node.hh:51
sdr::Sink::process
virtual void process(const Buffer< Scalar > &buffer, bool allow_overwrite)=0
Needs to be implemented by any sub-type to process the received data.
sdr::RawBuffer
Base class of all buffers, represents an untyped array of bytes.
Definition: buffer.hh:32
sdr::BlockingSource::~BlockingSource
virtual ~BlockingSource()
Destructor.
Definition: node.cc:149
sdr::Source::_config
Config _config
Holds the source configuration, this can be updated by calling setConfig.
Definition: node.hh:253
sdr::Config::_sampleRate
double _sampleRate
Holds the sample rate of the source.
Definition: node.hh:100
sdr::Delegate
Specific delegate to a method of an object .
Definition: queue.hh:27
sdr::Source::propagateConfig
void propagateConfig(const Config &config)
Propagates the given configuration to all connected sinks.
Definition: node.cc:108
sdr::Config::Config
Config()
Empty constructor, will result into an invalid configuration.
Definition: node.cc:9
sdr::Source::Source
Source()
Constructor.
Definition: node.cc:56
sdr::Config::operator=
const Config & operator=(const Config &other)
Assignment operator.
Definition: node.cc:29
sdr::Config::Type_s16
Real signed 16b ints.
Definition: node.hh:44
sdr::Source::send
virtual void send(const RawBuffer &buffer, bool allow_overwrite=false)
Sends the given buffer to all connected sinks.
Definition: node.cc:67
sdr::Source::_eos
std::list< DelegateInterface * > _eos
The connected EOS singal handlers.
Definition: node.hh:257
sdr::BlockingSource
Iterface of a blocking source.
Definition: node.hh:267
sdr::Sink
Typed sink.
Definition: node.hh:192
sdr::Config::Type_cu16
Complex (aka I/Q) type of unsigned 16b ints.
Definition: node.hh:49
sdr::BlockingSource::_is_parallel
bool _is_parallel
If true, the surce is processed in parallel.
Definition: node.hh:303
sdr
Definition: autocast.hh:8
sdr::Config::numBuffers
size_t numBuffers() const
Returns the max.
Definition: node.hh:89
sdr::Config::_type
Type _type
Holds the type of the source.
Definition: node.hh:98
sdr::Config::Type_s8
Real signed 8b ints.
Definition: node.hh:42
sdr::Config::hasSampleRate
bool hasSampleRate() const
If true, the configuration has a sample rate.
Definition: node.hh:75
sdr::Source
Generic source class.
Definition: node.hh:213
sdr::Config::Type_u8
Real unsigned 8b ints.
Definition: node.hh:41
sdr::BlockingSource::_parallel_main
void _parallel_main()
The parallel main loop.
Definition: node.cc:172
sdr::Source::signalEOS
void signalEOS()
Signals the EOS.
Definition: node.cc:127
sdr::Proxy::~Proxy
virtual ~Proxy()
Destructor.
Definition: node.cc:203
sdr::Config::hasType
bool hasType() const
If true, the configuration has a type.
Definition: node.hh:69
sdr::Source::_sinks
std::map< SinkBase *, bool > _sinks
The connected sinks.
Definition: node.hh:255
sdr::Config::Type_f64
Real 64b floats aka. "double".
Definition: node.hh:46
sdr::Source::sampleRate
virtual double sampleRate() const
Returns the configured sample rate or 0 otherwise.
Definition: node.cc:122
sdr::Sink::~Sink
virtual ~Sink()
Drestructor.
Definition: node.hh:198
sdr::BlockingSource::isActive
bool isActive() const
Returns true if the source is active.
Definition: node.hh:286
sdr::Config::Type_u16
Real unsigned 16b ints.
Definition: node.hh:43
sdr::Source::setConfig
virtual void setConfig(const Config &config)
Stores the configuration and propergates it if the configuration has been changed.
Definition: node.cc:98
sdr::BlockingSource::_is_active
bool _is_active
If true, the source is active.
Definition: node.hh:301
sdr::Proxy::config
virtual void config(const Config &src_cfg)
Configures the node.
Definition: node.cc:208
sdr::BlockingSource::next
virtual void next()=0
This method gets called either by the Queue on idle events or by a thread to read more data from the ...
sdr::SinkBase::config
virtual void config(const Config &src_cfg)=0
Needs to be implemented by any sub-type to check and perform the configuration of the node...
sdr::Source::connect
void connect(SinkBase *sink, bool direct=false)
Connect this source to a sink.
Definition: node.cc:87
sdr::SinkBase::handleBuffer
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite)=0
Needs to be implemented by any sub-type to process the received data.
sdr::Config::setNumBuffers
void setNumBuffers(size_t N)
Sets the max.
Definition: node.hh:91
sdr::Config::_bufferSize
size_t _bufferSize
Holds the max.
Definition: node.hh:102
sdr::Config::type
Type type() const
Returns the type.
Definition: node.hh:71
sdr::Proxy::Proxy
Proxy()
Constructor.
Definition: node.cc:197
sdr::BlockingSource::start
virtual void start()
This function starts the input stream.
Definition: node.cc:154
sdr::BlockingSource::_thread
pthread_t _thread
The thread of the source.
Definition: node.hh:305
sdr::Config::setType
void setType(Type type)
Sets the type.
Definition: node.hh:73
sdr::Sink::Sink
Sink()
Constructor.
Definition: node.hh:196
sdr::Config::typeId
static Type typeId()
Returns the type-id of the template type.
sdr::SinkBase::SinkBase
SinkBase()
Constructor.
Definition: node.cc:45
sdr::Config::Type_f32
Real 32b floats aka. "float".
Definition: node.hh:45
sdr::Config::hasNumBuffers
bool hasNumBuffers() const
If true, the configuration has a number of buffers.
Definition: node.hh:87
sdr::Config::bufferSize
size_t bufferSize() const
Returns the max.
Definition: node.hh:83
sdr::BlockingSource::stop
virtual void stop()
This function stops the input stream.
Definition: node.cc:162
sdr::Config::Type
Type
The type IDs.
Definition: node.hh:39
sdr::SinkBase
Basic interface of all Sinks.
Definition: node.hh:174
sdr::Source::~Source
virtual ~Source()
Destructor.
Definition: node.cc:62
sdr::Config::setSampleRate
void setSampleRate(double rate)
Sets the sample rate.
Definition: node.hh:79
sdr::Config::Type_cs8
Complex (aka I/Q) type of signed 8b ints.
Definition: node.hh:48
sdr::Proxy::handleBuffer
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite)
Forwards the buffer.
Definition: node.cc:213
sdr::Config::operator==
bool operator==(const Config &other) const
Coparison operator.
Definition: node.cc:36
sdr::Config::Type_cs16
Complex (aka I/Q) type of signed 16b ints.
Definition: node.hh:50
sdr::Config::Type_cu8
Complex (aka I/Q) type of unsigned 8b ints.
Definition: node.hh:47
sdr::Source::type
virtual Config::Type type() const
Returns the configured source type or Config::Type_UNDEFINED otherwise.
Definition: node.cc:117
sdr::Proxy
A NOP node.
Definition: node.hh:315
sdr::BlockingSource::BlockingSource
BlockingSource(bool parallel=false, bool connect_idle=true, bool stop_queue_on_eos=false)
Constructor.
Definition: node.cc:137
sdr::Source::disconnect
void disconnect(SinkBase *sink)
Disconnect a sink again.
Definition: node.cc:93
sdr::Source::addEOS
void addEOS(T *instance, void(T::*function)())
Adds a callback to the end-of-stream signal of the source.
Definition: node.hh:240
sdr::BlockingSource::_nonvirt_idle_cb
void _nonvirt_idle_cb()
The non-virtual idle callback.
Definition: node.cc:179
sdr::Sink::handleBuffer
virtual void handleBuffer(const RawBuffer &buffer, bool allow_overwrite)
Re-implemented from SinkBase.
Definition: node.hh:205
sdr::SinkBase::~SinkBase
virtual ~SinkBase()
Destructor.
Definition: node.cc:49
sdr::Buffer< Scalar >
sdr::Config::setBufferSize
void setBufferSize(size_t size)
Sets the max.
Definition: node.hh:85
sdr::Config::hasBufferSize
bool hasBufferSize() const
If true, the configuration has a buffer size.
Definition: node.hh:81
sdr::Config::Type_cf64
Complex (aka I/Q) type of 64bit floats aka. std::complex<double>.
Definition: node.hh:52
sdr::Config::_numBuffers
size_t _numBuffers
Holds the max.
Definition: node.hh:104
sdr::Config::sampleRate
double sampleRate() const
Returns the sample rate.
Definition: node.hh:77