8#include "core/exception.h"
9#include "dsp/base/stream.h"
10#include "nlohmann/json.hpp"
45 std::shared_ptr<DSPStream> fifo =
nullptr;
46 std::shared_ptr<void> blkdata =
nullptr;
49 uint64_t samplerate = 0;
54 inline void add_param_simple(nlohmann::ordered_json &p, std::string
id, std::string type, std::string name =
"")
61 inline void add_param_range(nlohmann::ordered_json &p, std::string
id, std::string type, nlohmann::ordered_json min, nlohmann::ordered_json max, nlohmann::ordered_json step,
62 std::string name =
"")
67 p[id][
"range"] = {min, max, step};
70 inline void add_param_list(nlohmann::ordered_json &p, std::string
id, std::string type, nlohmann::json list, std::string name =
"")
101 const std::string d_id;
104 std::vector<BlockIO> inputs;
105 std::vector<BlockIO> outputs;
131 if (i >= (
int)inputs.size())
132 throw satdump_exception(
"Input index " + std::to_string(i) +
" does not exist for " + d_id +
"!");
133 if (inputs[i].type != f.type)
134 throw satdump_exception(
"Input type " + std::to_string(inputs[i].type) +
" not compatible with " + std::to_string(f.type) +
" for " + d_id +
"!");
135 inputs[i].fifo = f.fifo;
147 if (i >= (
int)outputs.size())
148 throw satdump_exception(
"Ouput index " + std::to_string(i) +
" does not exist for " + d_id +
"!");
150 outputs[i].fifo = std::make_shared<DSPStream>(nbuf);
171 std::mutex blk_th_mtx;
175 while (blk_should_run)
197 bool is_work_running() {
return blk_should_run; }
206 Block(std::string
id, std::vector<BlockIO> in = {}, std::vector<BlockIO> out = {}) : d_id(id), inputs(in), outputs(out)
208 blk_should_run =
false;
214 if (blk_should_run || blk_th.joinable())
215 throw satdump_exception(
"Block wasn't properly stopped before destructor was called!");
255 virtual nlohmann::json
get_cfg(std::string key) = 0;
267 for (
auto &v2 : v.items())
268 p[v2.key()] =
get_cfg(v2.key());
298 for (
auto &i : v.items())
308 template <
typename T>
309 void setValFromJSONIfExists(T &v, nlohmann::json p)
329 if (blk_should_run || blk_th.joinable())
330 throw satdump_exception(
"Block wasn't properly stopped before start() was called again!");
334 blk_should_run =
true;
335 blk_th = std::thread(&Block::run,
this);
338 std::string th_id = d_id;
340 pthread_setname_np(blk_th.native_handle(), th_id.c_str());
351 virtual void stop(
bool stop_now =
false)
357 if (blk_th.joinable())
360 blk_should_run =
false;
BlockIOType
Block IO types.
Definition block.h:26
virtual nlohmann::json get_cfg(std::string key)=0
Get parameters of the block as JSON.
cfg_res_t
set_cfg status.
Definition block.h:227
virtual void init()
Applies current parameters to the block. This is called automatically once in start(),...
Definition block.h:321
virtual cfg_res_t set_cfg(std::string key, nlohmann::json v)=0
Set parameters of the block from JSON, including potentially IO configurations for blocks that may ha...
virtual void start()
Starts this block's internal thread and loop.
Definition block.h:327
void set_input(BlockIO f, int i)
Link an input to an output stream of some sort. This also checks the type of the BlockIO to ensure (s...
Definition block.h:129
bool work_should_exit
Used to signal to a block it should exit in order to stop(). Usually only needed in sources.
Definition block.h:185
nlohmann::json get_cfg()
Get parameters of the block as JSON. Unlike get_cfg(key), this returns every single available paramet...
Definition block.h:263
virtual bool work()=0
The actual looping work function meant to handle all the DSP (well, in most blocks)
virtual nlohmann::ordered_json get_cfg_list()
Get parameters LIST of the block's parameters. This does not contain actual values,...
Definition block.h:248
Block(std::string id, std::vector< BlockIO > in={}, std::vector< BlockIO > out={})
Generic constructor, to be overloaded.
Definition block.h:206
BlockIO get_output(int i, int nbuf)
Get one of the block's outputs, creating the fifo it if nbuf != 0.
Definition block.h:145
virtual void stop(bool stop_now=false)
Stops the block, or rather tells the internal loop it should exit & joins the thread to wait....
Definition block.h:351
std::vector< BlockIO > get_inputs()
Get the block's input configurations and streams. You should not modify them.
Definition block.h:113
cfg_res_t set_cfg(nlohmann::json v)
Set parameters of the block from JSON. Essentially the same as set_cfg(key, v), except this will set ...
Definition block.h:295
std::vector< BlockIO > get_outputs()
Get the block's output configurations and streams. You should not modify them.
Definition block.h:120
void link(Block *ptr, int output_index, int input_index, int nbuf)
Link a block's output to another input, more or less just a warper around set_input and set_output.
Definition block.h:167
Block IO helper class.
Definition block.h:42