GNU Radio 3.6.2 C++ API
gr_block.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2007,2009,2010 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_GR_BLOCK_H
24 #define INCLUDED_GR_BLOCK_H
25 
26 #include <gr_core_api.h>
27 #include <gr_basic_block.h>
28 #include <gr_tags.h>
29 
30 /*!
31  * \brief The abstract base class for all 'terminal' processing blocks.
32  * \ingroup base_blk
33  *
34  * A signal processing flow is constructed by creating a tree of
35  * hierarchical blocks, which at any level may also contain terminal nodes
36  * that actually implement signal processing functions. This is the base
37  * class for all such leaf nodes.
38 
39  * Blocks have a set of input streams and output streams. The
40  * input_signature and output_signature define the number of input
41  * streams and output streams respectively, and the type of the data
42  * items in each stream.
43  *
44  * Although blocks may consume data on each input stream at a
45  * different rate, all outputs streams must produce data at the same
46  * rate. That rate may be different from any of the input rates.
47  *
48  * User derived blocks override two methods, forecast and general_work,
49  * to implement their signal processing behavior. forecast is called
50  * by the system scheduler to determine how many items are required on
51  * each input stream in order to produce a given number of output
52  * items.
53  *
54  * general_work is called to perform the signal processing in the block.
55  * It reads the input items and writes the output items.
56  */
57 
59 
60  public:
61 
62  //! Magic return values from general_work
63  enum {
64  WORK_CALLED_PRODUCE = -2,
65  WORK_DONE = -1
66  };
67 
69  TPP_DONT = 0,
70  TPP_ALL_TO_ALL = 1,
71  TPP_ONE_TO_ONE = 2
72  };
73 
74  virtual ~gr_block ();
75 
76  /*!
77  * Assume block computes y_i = f(x_i, x_i-1, x_i-2, x_i-3...)
78  * History is the number of x_i's that are examined to produce one y_i.
79  * This comes in handy for FIR filters, where we use history to
80  * ensure that our input contains the appropriate "history" for the
81  * filter. History should be equal to the number of filter taps.
82  */
83  unsigned history () const { return d_history; }
84  void set_history (unsigned history) { d_history = history; }
85 
86  /*!
87  * \brief Return true if this block has a fixed input to output rate.
88  *
89  * If true, then fixed_rate_in_to_out and fixed_rate_out_to_in may be called.
90  */
91  bool fixed_rate() const { return d_fixed_rate; }
92 
93  // ----------------------------------------------------------------
94  // override these to define your behavior
95  // ----------------------------------------------------------------
96 
97  /*!
98  * \brief Estimate input requirements given output request
99  *
100  * \param noutput_items number of output items to produce
101  * \param ninput_items_required number of input items required on each input stream
102  *
103  * Given a request to product \p noutput_items, estimate the number of
104  * data items required on each input stream. The estimate doesn't have
105  * to be exact, but should be close.
106  */
107  virtual void forecast (int noutput_items,
108  gr_vector_int &ninput_items_required);
109 
110  /*!
111  * \brief compute output items from input items
112  *
113  * \param noutput_items number of output items to write on each output stream
114  * \param ninput_items number of input items available on each input stream
115  * \param input_items vector of pointers to the input items, one entry per input stream
116  * \param output_items vector of pointers to the output items, one entry per output stream
117  *
118  * \returns number of items actually written to each output stream, or -1 on EOF.
119  * It is OK to return a value less than noutput_items. -1 <= return value <= noutput_items
120  *
121  * general_work must call consume or consume_each to indicate how many items
122  * were consumed on each input stream.
123  */
124  virtual int general_work (int noutput_items,
125  gr_vector_int &ninput_items,
126  gr_vector_const_void_star &input_items,
127  gr_vector_void_star &output_items) = 0;
128 
129  /*!
130  * \brief Called to enable drivers, etc for i/o devices.
131  *
132  * This allows a block to enable an associated driver to begin
133  * transfering data just before we start to execute the scheduler.
134  * The end result is that this reduces latency in the pipeline when
135  * dealing with audio devices, usrps, etc.
136  */
137  virtual bool start();
138 
139  /*!
140  * \brief Called to disable drivers, etc for i/o devices.
141  */
142  virtual bool stop();
143 
144  // ----------------------------------------------------------------
145 
146  /*!
147  * \brief Constrain the noutput_items argument passed to forecast and general_work
148  *
149  * set_output_multiple causes the scheduler to ensure that the noutput_items
150  * argument passed to forecast and general_work will be an integer multiple
151  * of \param multiple The default value of output multiple is 1.
152  */
153  void set_output_multiple (int multiple);
154  int output_multiple () const { return d_output_multiple; }
155  bool output_multiple_set () const { return d_output_multiple_set; }
156 
157  /*!
158  * \brief Constrains buffers to work on a set item alignment (for SIMD)
159  *
160  * set_alignment_multiple causes the scheduler to ensure that the noutput_items
161  * argument passed to forecast and general_work will be an integer multiple
162  * of \param multiple The default value is 1.
163  *
164  * This control is similar to the output_multiple setting, except
165  * that if the number of items passed to the block is less than the
166  * output_multiple, this value is ignored and the block can produce
167  * like normal. The d_unaligned value is set to the number of items
168  * the block is off by. In the next call to general_work, the
169  * noutput_items is set to d_unaligned or less until
170  * d_unaligned==0. The buffers are now aligned again and the aligned
171  * calls can be performed again.
172  */
173  void set_alignment (int multiple);
174  int alignment () const { return d_output_multiple; }
175 
176  void set_unaligned (int na);
177  int unaligned () const { return d_unaligned; }
178  void set_is_unaligned (bool u);
179  bool is_unaligned () const { return d_is_unaligned; }
180 
181  /*!
182  * \brief Tell the scheduler \p how_many_items of input stream \p which_input were consumed.
183  */
184  void consume (int which_input, int how_many_items);
185 
186  /*!
187  * \brief Tell the scheduler \p how_many_items were consumed on each input stream.
188  */
189  void consume_each (int how_many_items);
190 
191  /*!
192  * \brief Tell the scheduler \p how_many_items were produced on output stream \p which_output.
193  *
194  * If the block's general_work method calls produce, \p general_work must return WORK_CALLED_PRODUCE.
195  */
196  void produce (int which_output, int how_many_items);
197 
198  /*!
199  * \brief Set the approximate output rate / input rate
200  *
201  * Provide a hint to the buffer allocator and scheduler.
202  * The default relative_rate is 1.0
203  *
204  * decimators have relative_rates < 1.0
205  * interpolators have relative_rates > 1.0
206  */
207  void set_relative_rate (double relative_rate);
208 
209  /*!
210  * \brief return the approximate output rate / input rate
211  */
212  double relative_rate () const { return d_relative_rate; }
213 
214  /*
215  * The following two methods provide special case info to the
216  * scheduler in the event that a block has a fixed input to output
217  * ratio. gr_sync_block, gr_sync_decimator and gr_sync_interpolator
218  * override these. If you're fixed rate, subclass one of those.
219  */
220  /*!
221  * \brief Given ninput samples, return number of output samples that will be produced.
222  * N.B. this is only defined if fixed_rate returns true.
223  * Generally speaking, you don't need to override this.
224  */
225  virtual int fixed_rate_ninput_to_noutput(int ninput);
226 
227  /*!
228  * \brief Given noutput samples, return number of input samples required to produce noutput.
229  * N.B. this is only defined if fixed_rate returns true.
230  * Generally speaking, you don't need to override this.
231  */
232  virtual int fixed_rate_noutput_to_ninput(int noutput);
233 
234  /*!
235  * \brief Return the number of items read on input stream which_input
236  */
237  uint64_t nitems_read(unsigned int which_input);
238 
239  /*!
240  * \brief Return the number of items written on output stream which_output
241  */
242  uint64_t nitems_written(unsigned int which_output);
243 
244  /*!
245  * \brief Asks for the policy used by the scheduler to moved tags downstream.
246  */
247  tag_propagation_policy_t tag_propagation_policy();
248 
249  /*!
250  * \brief Set the policy by the scheduler to determine how tags are moved downstream.
251  */
252  void set_tag_propagation_policy(tag_propagation_policy_t p);
253 
254  // ----------------------------------------------------------------------------
255 
256  private:
257 
258  int d_output_multiple;
259  bool d_output_multiple_set;
260  int d_unaligned;
261  bool d_is_unaligned;
262  double d_relative_rate; // approx output_rate / input_rate
263  gr_block_detail_sptr d_detail; // implementation details
264  unsigned d_history;
265  bool d_fixed_rate;
266  tag_propagation_policy_t d_tag_propagation_policy; // policy for moving tags downstream
267 
268  protected:
269  gr_block (void){} //allows pure virtual interface sub-classes
270  gr_block (const std::string &name,
271  gr_io_signature_sptr input_signature,
272  gr_io_signature_sptr output_signature);
273 
274  void set_fixed_rate(bool fixed_rate){ d_fixed_rate = fixed_rate; }
275 
276 
277  /*!
278  * \brief Adds a new tag onto the given output buffer.
279  *
280  * \param which_output an integer of which output stream to attach the tag
281  * \param abs_offset a uint64 number of the absolute item number
282  * assicated with the tag. Can get from nitems_written.
283  * \param key the tag key as a PMT symbol
284  * \param value any PMT holding any value for the given key
285  * \param srcid optional source ID specifier; defaults to PMT_F
286  */
287  inline void add_item_tag(unsigned int which_output,
288  uint64_t abs_offset,
289  const pmt::pmt_t &key,
290  const pmt::pmt_t &value,
291  const pmt::pmt_t &srcid=pmt::PMT_F)
292  {
293  gr_tag_t tag;
294  tag.offset = abs_offset;
295  tag.key = key;
296  tag.value = value;
297  tag.srcid = srcid;
298  this->add_item_tag(which_output, tag);
299  }
300 
301  /*!
302  * \brief Adds a new tag onto the given output buffer.
303  *
304  * \param which_output an integer of which output stream to attach the tag
305  * \param tag the tag object to add
306  */
307  void add_item_tag(unsigned int which_output, const gr_tag_t &tag);
308 
309  /*!
310  * \brief Given a [start,end), returns a vector of all tags in the range.
311  *
312  * Range of counts is from start to end-1.
313  *
314  * Tags are tuples of:
315  * (item count, source id, key, value)
316  *
317  * \param v a vector reference to return tags into
318  * \param which_input an integer of which input stream to pull from
319  * \param abs_start a uint64 count of the start of the range of interest
320  * \param abs_end a uint64 count of the end of the range of interest
321  */
322  void get_tags_in_range(std::vector<gr_tag_t> &v,
323  unsigned int which_input,
324  uint64_t abs_start,
325  uint64_t abs_end);
326 
327  /*!
328  * \brief Given a [start,end), returns a vector of all tags in the range
329  * with a given key.
330  *
331  * Range of counts is from start to end-1.
332  *
333  * Tags are tuples of:
334  * (item count, source id, key, value)
335  *
336  * \param v a vector reference to return tags into
337  * \param which_input an integer of which input stream to pull from
338  * \param abs_start a uint64 count of the start of the range of interest
339  * \param abs_end a uint64 count of the end of the range of interest
340  * \param key a PMT symbol key to filter only tags of this key
341  */
342  void get_tags_in_range(std::vector<gr_tag_t> &v,
343  unsigned int which_input,
344  uint64_t abs_start,
345  uint64_t abs_end,
346  const pmt::pmt_t &key);
347 
348  // These are really only for internal use, but leaving them public avoids
349  // having to work up an ever-varying list of friend GR_CORE_APIs
350 
351  public:
352  gr_block_detail_sptr detail () const { return d_detail; }
353  void set_detail (gr_block_detail_sptr detail) { d_detail = detail; }
354 };
355 
356 typedef std::vector<gr_block_sptr> gr_block_vector_t;
357 typedef std::vector<gr_block_sptr>::iterator gr_block_viter_t;
358 
359 inline gr_block_sptr cast_to_block_sptr(gr_basic_block_sptr p)
360 {
361  return boost::dynamic_pointer_cast<gr_block, gr_basic_block>(p);
362 }
363 
364 
365 std::ostream&
366 operator << (std::ostream& os, const gr_block *m);
367 
368 #endif /* INCLUDED_GR_BLOCK_H */