GNU Radio 3.6.3 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);
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  * \brief Return the maximum number of output items this block will
256  * handle during a call to work.
257  */
258  int max_noutput_items();
259 
260  /*!
261  * \brief Set the maximum number of ouput items htis block will
262  * handle during a call to work.
263  *
264  * \param m the maximum noutput_items this block will handle.
265  */
266  void set_max_noutput_items(int m);
267 
268  /*!
269  * \brief Clear the switch for using the max_noutput_items value of this block.
270  *
271  * When is_set_max_noutput_items() returns 'true', the scheduler
272  * will use the value returned by max_noutput_items() to limit the
273  * size of the number of items possible for this block's work
274  * function. If is_set_max_notput_items() returns 'false', then the
275  * scheduler ignores the internal value and uses the value set
276  * globally in the top_block.
277  *
278  * Use this value to clear the 'is_set' flag so the scheduler will
279  * ignore this. Use the set_max_noutput_items(m) call to both set a
280  * new value for max_noutput_items and to reenable its use in the
281  * scheduler.
282  */
283  void unset_max_noutput_items();
284 
285  /*!
286  * \brief Ask the block if the flag is or is not set to use the
287  * internal value of max_noutput_items during a call to work.
288  */
289  bool is_set_max_noutput_items();
290 
291  /*
292  * Used to expand the vectors that hold the min/max buffer sizes.
293  *
294  * Specifically, when -1 is used, the vectors are just initialized
295  * with 1 value; this is used by the flat_flowgraph to expand when
296  * required to add a new value for new ports on these blocks.
297  */
298  void expand_minmax_buffer(int port) {
299  if((size_t)port >= d_max_output_buffer.size())
300  set_max_output_buffer(port, -1);
301  if((size_t)port >= d_min_output_buffer.size())
302  set_min_output_buffer(port, -1);
303  }
304 
305  /*!
306  * \brief Returns max buffer size on output port \p i.
307  */
308  long max_output_buffer(size_t i) {
309  if(i >= d_max_output_buffer.size())
310  throw std::invalid_argument("gr_basic_block::max_output_buffer: port out of range.");
311  return d_max_output_buffer[i];
312  }
313 
314  /*!
315  * \brief Sets max buffer size on all output ports.
316  */
317  void set_max_output_buffer(long max_output_buffer) {
318  for(int i = 0; i < output_signature()->max_streams(); i++) {
319  set_max_output_buffer(i, max_output_buffer);
320  }
321  }
322 
323  /*!
324  * \brief Sets max buffer size on output port \p port.
325  */
326  void set_max_output_buffer(int port, long max_output_buffer) {
327  if((size_t)port >= d_max_output_buffer.size())
328  d_max_output_buffer.push_back(max_output_buffer);
329  else
330  d_max_output_buffer[port] = max_output_buffer;
331  }
332 
333  /*!
334  * \brief Returns min buffer size on output port \p i.
335  */
336  long min_output_buffer(size_t i) {
337  if(i >= d_min_output_buffer.size())
338  throw std::invalid_argument("gr_basic_block::min_output_buffer: port out of range.");
339  return d_min_output_buffer[i];
340  }
341 
342  /*!
343  * \brief Sets min buffer size on all output ports.
344  */
345  void set_min_output_buffer(long min_output_buffer) {
346  for(int i=0; i<output_signature()->max_streams(); i++) {
347  set_min_output_buffer(i, min_output_buffer);
348  }
349  }
350 
351  /*!
352  * \brief Sets min buffer size on output port \p port.
353  */
354  void set_min_output_buffer(int port, long min_output_buffer) {
355  if((size_t)port >= d_min_output_buffer.size())
356  d_min_output_buffer.push_back(min_output_buffer);
357  else
358  d_min_output_buffer[port] = min_output_buffer;
359  }
360 
361  // ----------------------------------------------------------------------------
362 
363  private:
364 
365  int d_output_multiple;
366  bool d_output_multiple_set;
367  int d_unaligned;
368  bool d_is_unaligned;
369  double d_relative_rate; // approx output_rate / input_rate
370  gr_block_detail_sptr d_detail; // implementation details
371  unsigned d_history;
372  bool d_fixed_rate;
373  bool d_max_noutput_items_set; // if d_max_noutput_items is valid
374  int d_max_noutput_items; // value of max_noutput_items for this block
375  tag_propagation_policy_t d_tag_propagation_policy; // policy for moving tags downstream
376 
377  protected:
378  gr_block (void){} //allows pure virtual interface sub-classes
379  gr_block (const std::string &name,
380  gr_io_signature_sptr input_signature,
381  gr_io_signature_sptr output_signature);
382 
383  void set_fixed_rate(bool fixed_rate){ d_fixed_rate = fixed_rate; }
384 
385 
386  /*!
387  * \brief Adds a new tag onto the given output buffer.
388  *
389  * \param which_output an integer of which output stream to attach the tag
390  * \param abs_offset a uint64 number of the absolute item number
391  * assicated with the tag. Can get from nitems_written.
392  * \param key the tag key as a PMT symbol
393  * \param value any PMT holding any value for the given key
394  * \param srcid optional source ID specifier; defaults to PMT_F
395  */
396  inline void add_item_tag(unsigned int which_output,
397  uint64_t abs_offset,
398  const pmt::pmt_t &key,
399  const pmt::pmt_t &value,
400  const pmt::pmt_t &srcid=pmt::PMT_F)
401  {
402  gr_tag_t tag;
403  tag.offset = abs_offset;
404  tag.key = key;
405  tag.value = value;
406  tag.srcid = srcid;
407  this->add_item_tag(which_output, tag);
408  }
409 
410  /*!
411  * \brief Adds a new tag onto the given output buffer.
412  *
413  * \param which_output an integer of which output stream to attach the tag
414  * \param tag the tag object to add
415  */
416  void add_item_tag(unsigned int which_output, const gr_tag_t &tag);
417 
418  /*!
419  * \brief Given a [start,end), returns a vector of all tags in the range.
420  *
421  * Range of counts is from start to end-1.
422  *
423  * Tags are tuples of:
424  * (item count, source id, key, value)
425  *
426  * \param v a vector reference to return tags into
427  * \param which_input an integer of which input stream to pull from
428  * \param abs_start a uint64 count of the start of the range of interest
429  * \param abs_end a uint64 count of the end of the range of interest
430  */
431  void get_tags_in_range(std::vector<gr_tag_t> &v,
432  unsigned int which_input,
433  uint64_t abs_start,
434  uint64_t abs_end);
435 
436  /*!
437  * \brief Given a [start,end), returns a vector of all tags in the range
438  * with a given key.
439  *
440  * Range of counts is from start to end-1.
441  *
442  * Tags are tuples of:
443  * (item count, source id, key, value)
444  *
445  * \param v a vector reference to return tags into
446  * \param which_input an integer of which input stream to pull from
447  * \param abs_start a uint64 count of the start of the range of interest
448  * \param abs_end a uint64 count of the end of the range of interest
449  * \param key a PMT symbol key to filter only tags of this key
450  */
451  void get_tags_in_range(std::vector<gr_tag_t> &v,
452  unsigned int which_input,
453  uint64_t abs_start,
454  uint64_t abs_end,
455  const pmt::pmt_t &key);
456 
457  std::vector<long> d_max_output_buffer;
458  std::vector<long> d_min_output_buffer;
459 
460 
461  // These are really only for internal use, but leaving them public avoids
462  // having to work up an ever-varying list of friend GR_CORE_APIs
463 
464  public:
465  gr_block_detail_sptr detail () const { return d_detail; }
466  void set_detail (gr_block_detail_sptr detail) { d_detail = detail; }
467 };
468 
469 typedef std::vector<gr_block_sptr> gr_block_vector_t;
470 typedef std::vector<gr_block_sptr>::iterator gr_block_viter_t;
471 
472 inline gr_block_sptr cast_to_block_sptr(gr_basic_block_sptr p)
473 {
474  return boost::dynamic_pointer_cast<gr_block, gr_basic_block>(p);
475 }
476 
477 
478 std::ostream&
479 operator << (std::ostream& os, const gr_block *m);
480 
481 #endif /* INCLUDED_GR_BLOCK_H */