8#include "core/exception.h"
9#include "dsp/base/stream.h"
10#include "nlohmann/json.hpp"
47 std::shared_ptr<DSPStream> fifo =
nullptr;
48 std::shared_ptr<void> blkdata =
nullptr;
51 uint64_t samplerate = 0;
56 inline void add_param_simple(nlohmann::ordered_json &p, std::string
id, std::string type, std::string name =
"")
63 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,
64 std::string name =
"")
69 p[id][
"range"] = {min, max, step};
72 inline void add_param_list(nlohmann::ordered_json &p, std::string
id, std::string type, nlohmann::json list, std::string name =
"")
103 const std::string d_id;
106 std::vector<BlockIO> inputs;
107 std::vector<BlockIO> outputs;
133 if (i >= (
int)inputs.size())
134 throw satdump_exception(
"Input index " + std::to_string(i) +
" does not exist for " + d_id +
"!");
135 if (inputs[i].type != f.type)
136 throw satdump_exception(
"Input type " + std::to_string(inputs[i].type) +
" not compatible with " + std::to_string(f.type) +
" for " + d_id +
"!");
137 inputs[i].fifo = f.fifo;
149 if (i >= (
int)outputs.size())
150 throw satdump_exception(
"Ouput index " + std::to_string(i) +
" does not exist for " + d_id +
"!");
152 outputs[i].fifo = std::make_shared<DSPStream>(nbuf);
173 std::mutex blk_th_mtx;
177 while (blk_should_run)
199 bool is_work_running() {
return blk_should_run; }
208 Block(std::string
id, std::vector<BlockIO> in = {}, std::vector<BlockIO> out = {}) : d_id(id), inputs(in), outputs(out)
210 blk_should_run =
false;
216 if (blk_should_run || blk_th.joinable())
217 throw satdump_exception(
"Block wasn't properly stopped before destructor was called!");
230 virtual bool is_async() {
return inputs.size() == 0; }
269 virtual nlohmann::json
get_cfg(std::string key) = 0;
281 for (
auto &v2 : v.items())
282 p[v2.key()] =
get_cfg(v2.key());
312 for (
auto &i : v.items())
322 template <
typename T>
323 void setValFromJSONIfExists(T &v, nlohmann::json p)
343 if (blk_should_run || blk_th.joinable())
344 throw satdump_exception(
"Block wasn't properly stopped before start() was called again!");
348 blk_should_run =
true;
349 blk_th = std::thread(&Block::run,
this);
352 std::string th_id = d_id;
354 pthread_setname_np(blk_th.native_handle(), th_id.c_str());
372 virtual void stop(
bool stop_now =
false,
bool force =
false)
377 blk_should_run =
false;
380 if (blk_th.joinable())
383 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:241
virtual void init()
Applies current parameters to the block. This is called automatically once in start(),...
Definition block.h:335
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 std::vector< BlockIO > get_inputs()
Get the block's input configurations and streams. You should not modify them.
Definition block.h:115
virtual void start()
Starts this block's internal thread and loop.
Definition block.h:341
virtual 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:131
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:187
nlohmann::json get_cfg()
Get parameters of the block as JSON. Unlike get_cfg(key), this returns every single available paramet...
Definition block.h:277
virtual bool work()=0
The actual looping work function meant to handle all the DSP (well, in most blocks)
virtual BlockIO get_output(int i, int nbuf)
Get one of the block's outputs, creating the fifo it if nbuf != 0.
Definition block.h:147
virtual std::vector< BlockIO > get_outputs()
Get the block's output configurations and streams. You should not modify them.
Definition block.h:122
virtual nlohmann::ordered_json get_cfg_list()
Get parameters LIST of the block's parameters. This does not contain actual values,...
Definition block.h:262
Block(std::string id, std::vector< BlockIO > in={}, std::vector< BlockIO > out={})
Generic constructor, to be overloaded.
Definition block.h:208
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:309
virtual void stop(bool stop_now=false, bool force=false)
Stops the block, or rather tells the internal loop it should exit & joins the thread to wait....
Definition block.h:372
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:169
virtual bool is_async()
Returns true if a block is async. An async block is a block that has outputs without inputs or inputs...
Definition block.h:230
Block IO helper class.
Definition block.h:44