SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
block.h
Go to the documentation of this file.
1#pragma once
2
6
7#include "base/dsp_buffer.h"
8#include "core/exception.h"
9#include "dsp/base/stream.h"
10#include "nlohmann/json.hpp"
11#include <mutex>
12#include <thread>
13#include <vector>
14
15namespace satdump
16{
17 namespace ndsp // TODOREWORK back to normal DSP!
18 {
26 {
27 DSP_SAMPLE_TYPE_CF32,
28 DSP_SAMPLE_TYPE_F32,
29 DSP_SAMPLE_TYPE_S8,
30 };
31
41 struct BlockIO
42 {
43 std::string name;
44 BlockIOType type;
45 std::shared_ptr<DSPStream> fifo = nullptr;
46 std::shared_ptr<void> blkdata = nullptr;
47
48 // TODOREWORK DOCUMENT
49 uint64_t samplerate = 0;
50 double frequency = 0;
51 };
52
53 // TODOREWORK cleanup!!!!
54 inline void add_param_simple(nlohmann::ordered_json &p, std::string id, std::string type, std::string name = "")
55 {
56 p[id]["type"] = type;
57 if (name != "")
58 p[id]["name"] = name;
59 }
60
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 = "")
63 {
64 p[id]["type"] = type;
65 if (name != "")
66 p[id]["name"] = name;
67 p[id]["range"] = {min, max, step};
68 }
69
70 inline void add_param_list(nlohmann::ordered_json &p, std::string id, std::string type, nlohmann::json list, std::string name = "")
71 {
72 p[id]["type"] = type;
73 if (name != "")
74 p[id]["name"] = name;
75 p[id]["list"] = list;
76 }
77
98 class Block
99 {
100 public:
101 const std::string d_id;
102
103 protected:
104 std::vector<BlockIO> inputs;
105 std::vector<BlockIO> outputs;
106
107 public:
113 std::vector<BlockIO> get_inputs() { return inputs; }
114
120 std::vector<BlockIO> get_outputs() { return outputs; }
121
129 void set_input(BlockIO f, int i)
130 {
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;
136 }
137
145 BlockIO get_output(int i, int nbuf)
146 {
147 if (i >= (int)outputs.size())
148 throw satdump_exception("Ouput index " + std::to_string(i) + " does not exist for " + d_id + "!");
149 if (nbuf > 0)
150 outputs[i].fifo = std::make_shared<DSPStream>(nbuf);
151 return outputs[i];
152 }
153
167 void link(Block *ptr, int output_index, int input_index, int nbuf) { set_input(ptr->get_output(output_index, nbuf), input_index); }
168
169 private:
170 bool blk_should_run;
171 std::mutex blk_th_mtx;
172 std::thread blk_th;
173 void run()
174 {
175 while (blk_should_run)
176 if (work())
177 break;
178 }
179
180 protected:
186
194 virtual bool work() = 0;
195
196 // TODOREWORK
197 bool is_work_running() { return blk_should_run; }
198
199 public:
206 Block(std::string id, std::vector<BlockIO> in = {}, std::vector<BlockIO> out = {}) : d_id(id), inputs(in), outputs(out)
207 {
208 blk_should_run = false;
209 work_should_exit = false;
210 }
211
212 virtual ~Block()
213 {
214 if (blk_should_run || blk_th.joinable())
215 throw satdump_exception("Block wasn't properly stopped before destructor was called!");
216 }
217
218 public:
227 {
228 RES_OK = 0,
229 RES_LISTUPD = 1,
230 RES_IOUPD = 2,
231 RES_ERR = 3,
232 };
233
248 virtual nlohmann::ordered_json get_cfg_list() { return {}; }
249
255 virtual nlohmann::json get_cfg(std::string key) = 0;
256
263 nlohmann::json get_cfg()
264 {
265 nlohmann::json p;
266 auto v = get_cfg_list();
267 for (auto &v2 : v.items())
268 p[v2.key()] = get_cfg(v2.key());
269 return p;
270 }
271
286 virtual cfg_res_t set_cfg(std::string key, nlohmann::json v) = 0;
287
295 cfg_res_t set_cfg(nlohmann::json v)
296 {
297 cfg_res_t r = RES_OK;
298 for (auto &i : v.items())
299 {
300 cfg_res_t r2 = set_cfg(i.key(), i.value());
301 if (r2 > r)
302 r = r2;
303 }
304 return r;
305 }
306
307 protected:
308 template <typename T>
309 void setValFromJSONIfExists(T &v, nlohmann::json p)
310 {
311 if (!p.is_null())
312 v = p.get<T>();
313 }
314
315 protected:
321 virtual void init() {}
322
323 public:
327 virtual void start()
328 {
329 if (blk_should_run || blk_th.joinable())
330 throw satdump_exception("Block wasn't properly stopped before start() was called again!");
331
332 init();
333 work_should_exit = false;
334 blk_should_run = true;
335 blk_th = std::thread(&Block::run, this);
336#ifndef _WIN32
337#ifndef __APPLE__
338 std::string th_id = d_id;
339 th_id.resize(15);
340 pthread_setname_np(blk_th.native_handle(), th_id.c_str());
341#endif
342#endif
343 }
344
351 virtual void stop(bool stop_now = false)
352 { // TODOREWORK allow sending terminator in this function?
353 if (stop_now)
354 work_should_exit = true;
355
356 blk_th_mtx.lock();
357 if (blk_th.joinable())
358 blk_th.join();
359 blk_th_mtx.unlock();
360 blk_should_run = false;
361 }
362 };
363 } // namespace ndsp
364} // namespace satdump
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