GNU Radio C++ API Reference  g90d26cb
The Free & Open Software Radio Ecosystem
tagged_stream_block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2013 Free Software Foundation, Inc.
4  * Copyright 2025 Marcus Müller <mmueller@gnuradio.org>
5  *
6  * This file is part of GNU Radio
7  *
8  * SPDX-License-Identifier: GPL-3.0-or-later
9  *
10  */
11 
12 #ifndef INCLUDED_GR_RUNTIME_TAGGED_STREAM_BLOCK_H
13 #define INCLUDED_GR_RUNTIME_TAGGED_STREAM_BLOCK_H
14 
15 #include <gnuradio/api.h>
16 #include <gnuradio/block.h>
17 
18 namespace gr {
19 
20 /*!
21  * \brief Block that operates on PDUs in form of tagged streams
22  * \ingroup base_blk
23  *
24  * Override work to provide the signal processing implementation.
25  */
27 {
28 private:
30  d_length_tag_key; //!< This is the key for the tag that stores the PDU length
32  d_n_input_items_reqd; //!< How many input items do I need to process the next PDU?
33 
34 protected:
35  std::string d_length_tag_key_str;
36  tagged_stream_block(void) {} // allows pure virtual interface sub-classes
37  tagged_stream_block(const std::string& name,
38  gr::io_signature::sptr input_signature,
39  gr::io_signature::sptr output_signature,
40  const std::string& length_tag_key);
41 
42  /*!
43  * \brief Given a [start,end), returns a vector of all tags in the range.
44  *
45  * \see block::get_tags_in_range
46  *
47  * Does the same as block::get_tags_in_range, but omits the length_tag_key-tagged
48  * tags.
49  *
50  * In context of a class derived from tagged_stream_block, `get_tags_in_range` gets
51  * resolved to this implementation. Cast to the `gr::block` base class to access the
52  * original (i.e., length tag key-including) `block::get_tags_in_range`.
53  *
54  * \param v a vector reference to return tags into
55  * \param which_input an integer of which input stream to pull from
56  * \param abs_start a uint64 count of the start of the range of interest
57  * \param abs_end a uint64 count of the end of the range of interest
58  */
59  void get_tags_in_range(std::vector<tag_t>& v,
60  unsigned int which_input,
61  uint64_t abs_start,
62  uint64_t abs_end);
63  /*!
64  * \brief Given a [start,end), returns a vector of all tags in the range.
65  *
66  * \see block::get_tags_in_range
67  *
68  * Does the same as block::get_tags_in_range, but omits the length_tag_key-tagged
69  * tags.
70  *
71  * In context of a class derived from tagged_stream_block, `get_tags_in_range` gets
72  * resolved to this implementation. Cast to the `gr::block` base class to access the
73  * original (i.e., length tag key-including) `block::get_tags_in_range`.
74  *
75  * \param v a vector reference to return tags into
76  * \param which_input an integer of which input stream to pull from
77  * \param abs_start a uint64 count of the start of the range of interest
78  * \param abs_end a uint64 count of the end of the range of interest
79  * \param key a PMT symbol key to filter only tags of this key
80  */
81  void get_tags_in_range(std::vector<tag_t>& v,
82  unsigned int which_input,
83  uint64_t abs_start,
84  uint64_t abs_end,
85  const pmt::pmt_t& key);
86  /*!
87  * \brief Gets all tags within the relative window of the current call to work.
88  *
89  * \see block::get_tags_in_window
90  *
91  * Does the same as block::get_tags_in_window, but omits the length_tag_key-tagged
92  * tags.
93  *
94  * In context of a class derived from tagged_stream_block, `get_tags_in_window` gets
95  * resolved to this implementation. Cast to the `gr::block` base class to access the
96  * original (i.e., length tag key-including) `block::get_tags_in_window`.
97  *
98  *
99  * \param v a vector reference to return tags into
100  * \param which_input an integer of which input stream to pull from
101  * \param rel_start a uint64 count of the start of the range of interest
102  * \param rel_end a uint64 count of the end of the range of interest
103  */
104  void get_tags_in_window(std::vector<tag_t>& v,
105  unsigned int which_input,
106  uint64_t rel_start,
107  uint64_t rel_end);
108 
109  /*!
110  * \brief Gets all tags within the relative window of the current call to work,
111  * matching \p key
112  *
113  * \see block::get_tags_in_window
114  *
115  * Does the same as block::get_tags_in_window, but omits the length_tag_key-tagged
116  * tags.
117  *
118  * In context of a class derived from tagged_stream_block, `get_tags_in_window` gets
119  * resolved to this implementation. Cast to the `gr::block` base class to access the
120  * original (i.e., length tag key-including) `block::get_tags_in_window`.
121  *
122  * \param v a vector reference to return tags into
123  * \param which_input an integer of which input stream to pull from
124  * \param rel_start a uint64 count of the start of the range of interest
125  * \param rel_end a uint64 count of the end of the range of interest
126  * \param key a PMT symbol key to filter only tags of this key
127  */
128  void get_tags_in_window(std::vector<tag_t>& v,
129  unsigned int which_input,
130  uint64_t rel_start,
131  uint64_t rel_end,
132  const pmt::pmt_t& key);
133 
134  /*!
135  * \brief Parse all tags on the first sample of a PDU, return the
136  * number of items per input and prune the length tags.
137  *
138  * In most cases, you don't need to override this, unless the
139  * number of items read is not directly coded in one single tag.
140  *
141  * Default behaviour:
142  * - Go through all input ports
143  * - On every input port, search for the tag with the key specified in \p
144  * length_tag_key
145  * - Copy that value as an int to the corresponding position in \p n_input_items_reqd
146  * - Remove the length tag.
147  *
148  * \param[in] tags All the tags found on the first item of every input port.
149  * \param[out] n_input_items_reqd Number of items which will be read from every input
150  */
151  virtual void parse_length_tags(const std::vector<std::vector<tag_t>>& tags,
152  gr_vector_int& n_input_items_reqd);
153 
154  /*!
155  * \brief Calculate the number of output items.
156  *
157  * This is basically the inverse function to forecast(): Given a
158  * number of input items, it returns the maximum number of output
159  * items.
160  *
161  * You most likely need to override this function, unless your
162  * block is a sync block or integer interpolator/decimator.
163  */
164  virtual int calculate_output_stream_length(const gr_vector_int& ninput_items);
165 
166  /*!
167  * \brief Set the new length tags on the output stream
168  *
169  * Default behaviour: Set a tag with key \p length_tag_key and the
170  * number of produced items on every output port.
171  *
172  * For anything else, override this.
173  *
174  * \param n_produced Length of the new PDU
175  * \param n_ports Number of output ports
176  */
177  virtual void update_length_tags(int n_produced, int n_ports);
178 
179 public:
180  /*! \brief Don't override this.
181  */
182  void /* final */ forecast(int noutput_items,
183  gr_vector_int& ninput_items_required) override;
184 
185  bool check_topology(int ninputs, int /* noutputs */) override;
186 
187  /*!
188  * - Reads the number of input items from the tags using parse_length_tags()
189  * - Checks there's enough data on the input and output buffers
190  * - If not, inform the scheduler and do nothing
191  * - Calls work() with the exact number of items per PDU
192  * - Updates the tags using update_length_tags()
193  */
194  int general_work(int noutput_items,
195  gr_vector_int& ninput_items,
196  gr_vector_const_void_star& input_items,
197  gr_vector_void_star& output_items) override;
198 
199  /*!
200  * \brief Just like gr::block::general_work, but makes sure the input is valid
201  *
202  * The user must override work to define the signal processing
203  * code. Check the documentation for general_work() to see what
204  * happens here.
205  *
206  * Like gr::sync_block, this calls consume() for you (it consumes
207  * ninput_items[i] items from the i-th port).
208  *
209  * A note on tag propagation: The PDU length tags are handled by
210  * other functions, but all other tags are handled just as in any
211  * other \p gr::block. So, most likely, you either set the tag
212  * propagation policy to TPP_DONT and handle the tag propagation
213  * manually, or you propagate tags through the scheduler and don't
214  * do anything here.
215  *
216  * \param noutput_items The size of the writable output buffer
217  * \param ninput_items The exact size of the items on every input for this particular
218  * PDU. These will be consumed if a length tag key is provided! \param input_items See
219  * gr::block \param output_items See gr::block
220  */
221  virtual int work(int noutput_items,
222  gr_vector_int& ninput_items,
223  gr_vector_const_void_star& input_items,
224  gr_vector_void_star& output_items) = 0;
225 };
226 
227 } /* namespace gr */
228 
229 #endif /* INCLUDED_GR_RUNTIME_TAGGED_STREAM_BLOCK_H */
The abstract base class for all 'terminal' processing blocks.
Definition: gnuradio-runtime/include/gnuradio/block.h:65
std::shared_ptr< io_signature > sptr
Definition: io_signature.h:52
Block that operates on PDUs in form of tagged streams.
Definition: tagged_stream_block.h:27
std::string d_length_tag_key_str
Definition: tagged_stream_block.h:35
int general_work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items) override
void get_tags_in_window(std::vector< tag_t > &v, unsigned int which_input, uint64_t rel_start, uint64_t rel_end)
Gets all tags within the relative window of the current call to work.
void get_tags_in_range(std::vector< tag_t > &v, unsigned int which_input, uint64_t abs_start, uint64_t abs_end)
Given a [start,end), returns a vector of all tags in the range.
tagged_stream_block(const std::string &name, gr::io_signature::sptr input_signature, gr::io_signature::sptr output_signature, const std::string &length_tag_key)
tagged_stream_block(void)
Definition: tagged_stream_block.h:36
virtual void parse_length_tags(const std::vector< std::vector< tag_t >> &tags, gr_vector_int &n_input_items_reqd)
Parse all tags on the first sample of a PDU, return the number of items per input and prune the lengt...
void get_tags_in_range(std::vector< tag_t > &v, unsigned int which_input, uint64_t abs_start, uint64_t abs_end, const pmt::pmt_t &key)
Given a [start,end), returns a vector of all tags in the range.
bool check_topology(int ninputs, int) override
Confirm that ninputs and noutputs is an acceptable combination.
virtual int calculate_output_stream_length(const gr_vector_int &ninput_items)
Calculate the number of output items.
void forecast(int noutput_items, gr_vector_int &ninput_items_required) override
Don't override this.
virtual void update_length_tags(int n_produced, int n_ports)
Set the new length tags on the output stream.
void get_tags_in_window(std::vector< tag_t > &v, unsigned int which_input, uint64_t rel_start, uint64_t rel_end, const pmt::pmt_t &key)
Gets all tags within the relative window of the current call to work, matching key.
virtual int work(int noutput_items, gr_vector_int &ninput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)=0
Just like gr::block::general_work, but makes sure the input is valid.
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29
std::shared_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting).
Definition: pmt.h:85
Definition: cc_common.h:35
std::vector< const void * > gr_vector_const_void_star
Definition: types.h:28
std::vector< void * > gr_vector_void_star
Definition: types.h:27
std::vector< int > gr_vector_int
Definition: types.h:23