GNU Radio 3.5.1 C++ API
gr_block.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004,2007,2009,2010 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_BLOCK_H
00024 #define INCLUDED_GR_BLOCK_H
00025 
00026 #include <gr_core_api.h>
00027 #include <gr_basic_block.h>
00028 #include <gr_tags.h>
00029 
00030 /*!
00031  * \brief The abstract base class for all 'terminal' processing blocks.
00032  * \ingroup base_blk
00033  *
00034  * A signal processing flow is constructed by creating a tree of 
00035  * hierarchical blocks, which at any level may also contain terminal nodes
00036  * that actually implement signal processing functions. This is the base
00037  * class for all such leaf nodes.
00038  
00039  * Blocks have a set of input streams and output streams.  The
00040  * input_signature and output_signature define the number of input
00041  * streams and output streams respectively, and the type of the data
00042  * items in each stream.
00043  *
00044  * Although blocks may consume data on each input stream at a
00045  * different rate, all outputs streams must produce data at the same
00046  * rate.  That rate may be different from any of the input rates.
00047  *
00048  * User derived blocks override two methods, forecast and general_work,
00049  * to implement their signal processing behavior. forecast is called
00050  * by the system scheduler to determine how many items are required on
00051  * each input stream in order to produce a given number of output
00052  * items.
00053  *
00054  * general_work is called to perform the signal processing in the block.
00055  * It reads the input items and writes the output items.
00056  */
00057 
00058 class GR_CORE_API gr_block : public gr_basic_block {
00059 
00060  public:
00061   
00062   //! Magic return values from general_work
00063   enum {
00064     WORK_CALLED_PRODUCE = -2,
00065     WORK_DONE = -1
00066   };
00067 
00068   enum tag_propagation_policy_t {
00069     TPP_DONT = 0,
00070     TPP_ALL_TO_ALL = 1,
00071     TPP_ONE_TO_ONE = 2
00072   };
00073 
00074   virtual ~gr_block ();
00075 
00076   /*!
00077    * Assume block computes y_i = f(x_i, x_i-1, x_i-2, x_i-3...)
00078    * History is the number of x_i's that are examined to produce one y_i.
00079    * This comes in handy for FIR filters, where we use history to
00080    * ensure that our input contains the appropriate "history" for the
00081    * filter.   History should be equal to the number of filter taps.
00082    */
00083   unsigned history () const { return d_history; }
00084   void  set_history (unsigned history) { d_history = history; }
00085   
00086   /*!
00087    * \brief Return true if this block has a fixed input to output rate.
00088    *
00089    * If true, then fixed_rate_in_to_out and fixed_rate_out_to_in may be called.
00090    */
00091   bool fixed_rate() const { return d_fixed_rate; }
00092 
00093   // ----------------------------------------------------------------
00094   //            override these to define your behavior
00095   // ----------------------------------------------------------------
00096 
00097   /*!
00098    * \brief  Estimate input requirements given output request
00099    *
00100    * \param noutput_items           number of output items to produce
00101    * \param ninput_items_required   number of input items required on each input stream
00102    *
00103    * Given a request to product \p noutput_items, estimate the number of
00104    * data items required on each input stream.  The estimate doesn't have
00105    * to be exact, but should be close.
00106    */
00107   virtual void forecast (int noutput_items,
00108                          gr_vector_int &ninput_items_required);
00109 
00110   /*!
00111    * \brief compute output items from input items
00112    *
00113    * \param noutput_items       number of output items to write on each output stream
00114    * \param ninput_items        number of input items available on each input stream
00115    * \param input_items         vector of pointers to the input items, one entry per input stream
00116    * \param output_items        vector of pointers to the output items, one entry per output stream
00117    *
00118    * \returns number of items actually written to each output stream, or -1 on EOF.
00119    * It is OK to return a value less than noutput_items.  -1 <= return value <= noutput_items
00120    *
00121    * general_work must call consume or consume_each to indicate how many items
00122    * were consumed on each input stream.
00123    */
00124   virtual int general_work (int noutput_items,
00125                             gr_vector_int &ninput_items,
00126                             gr_vector_const_void_star &input_items,
00127                             gr_vector_void_star &output_items) = 0;
00128 
00129   /*!
00130    * \brief Called to enable drivers, etc for i/o devices.
00131    *
00132    * This allows a block to enable an associated driver to begin
00133    * transfering data just before we start to execute the scheduler.
00134    * The end result is that this reduces latency in the pipeline when
00135    * dealing with audio devices, usrps, etc.
00136    */
00137   virtual bool start();
00138 
00139   /*!
00140    * \brief Called to disable drivers, etc for i/o devices.
00141    */
00142   virtual bool stop();
00143 
00144   // ----------------------------------------------------------------
00145 
00146   /*!
00147    * \brief Constrain the noutput_items argument passed to forecast and general_work
00148    *
00149    * set_output_multiple causes the scheduler to ensure that the noutput_items
00150    * argument passed to forecast and general_work will be an integer multiple
00151    * of \param multiple  The default value of output multiple is 1.
00152    */
00153   void set_output_multiple (int multiple);
00154   int  output_multiple () const { return d_output_multiple; }
00155 
00156   /*!
00157    * \brief Tell the scheduler \p how_many_items of input stream \p which_input were consumed.
00158    */
00159   void consume (int which_input, int how_many_items);
00160 
00161   /*!
00162    * \brief Tell the scheduler \p how_many_items were consumed on each input stream.
00163    */
00164   void consume_each (int how_many_items);
00165 
00166   /*!
00167    * \brief Tell the scheduler \p how_many_items were produced on output stream \p which_output.
00168    *
00169    * If the block's general_work method calls produce, \p general_work must return WORK_CALLED_PRODUCE.
00170    */
00171   void produce (int which_output, int how_many_items);
00172 
00173   /*!
00174    * \brief Set the approximate output rate / input rate
00175    *
00176    * Provide a hint to the buffer allocator and scheduler.
00177    * The default relative_rate is 1.0
00178    *
00179    * decimators have relative_rates < 1.0
00180    * interpolators have relative_rates > 1.0
00181    */
00182   void  set_relative_rate (double relative_rate);
00183 
00184   /*!
00185    * \brief return the approximate output rate / input rate
00186    */
00187   double relative_rate () const { return d_relative_rate; }
00188 
00189   /*
00190    * The following two methods provide special case info to the
00191    * scheduler in the event that a block has a fixed input to output
00192    * ratio.  gr_sync_block, gr_sync_decimator and gr_sync_interpolator
00193    * override these.  If you're fixed rate, subclass one of those.
00194    */
00195   /*!
00196    * \brief Given ninput samples, return number of output samples that will be produced.
00197    * N.B. this is only defined if fixed_rate returns true.
00198    * Generally speaking, you don't need to override this.
00199    */
00200   virtual int fixed_rate_ninput_to_noutput(int ninput);
00201 
00202   /*!
00203    * \brief Given noutput samples, return number of input samples required to produce noutput.
00204    * N.B. this is only defined if fixed_rate returns true.
00205    * Generally speaking, you don't need to override this.
00206    */
00207   virtual int fixed_rate_noutput_to_ninput(int noutput);
00208 
00209   /*!
00210    * \brief Return the number of items read on input stream which_input
00211    */
00212   uint64_t nitems_read(unsigned int which_input);
00213 
00214   /*!
00215    * \brief  Return the number of items written on output stream which_output
00216    */
00217   uint64_t nitems_written(unsigned int which_output);
00218 
00219   /*!
00220    * \brief Asks for the policy used by the scheduler to moved tags downstream.
00221    */
00222   tag_propagation_policy_t tag_propagation_policy();
00223 
00224   /*!
00225    * \brief Set the policy by the scheduler to determine how tags are moved downstream.
00226    */
00227   void set_tag_propagation_policy(tag_propagation_policy_t p);
00228 
00229   // ----------------------------------------------------------------------------
00230 
00231  private:
00232 
00233   int                   d_output_multiple;
00234   double                d_relative_rate;        // approx output_rate / input_rate
00235   gr_block_detail_sptr  d_detail;               // implementation details
00236   unsigned              d_history;
00237   bool                  d_fixed_rate;
00238   tag_propagation_policy_t d_tag_propagation_policy; // policy for moving tags downstream
00239     
00240  protected:
00241   gr_block (void){} //allows pure virtual interface sub-classes
00242   gr_block (const std::string &name,
00243             gr_io_signature_sptr input_signature,
00244             gr_io_signature_sptr output_signature);
00245 
00246   void set_fixed_rate(bool fixed_rate){ d_fixed_rate = fixed_rate; }
00247 
00248   
00249   /*!
00250    * \brief  Adds a new tag onto the given output buffer.
00251    * 
00252    * \param which_output an integer of which output stream to attach the tag
00253    * \param abs_offset   a uint64 number of the absolute item number
00254    *                     assicated with the tag. Can get from nitems_written.
00255    * \param key          the tag key as a PMT symbol
00256    * \param value        any PMT holding any value for the given key
00257    * \param srcid        optional source ID specifier; defaults to PMT_F
00258    */
00259   inline void add_item_tag(unsigned int which_output,
00260                     uint64_t abs_offset,
00261                     const pmt::pmt_t &key,
00262                     const pmt::pmt_t &value,
00263                     const pmt::pmt_t &srcid=pmt::PMT_F)
00264     {
00265         gr_tag_t tag;
00266         tag.offset = abs_offset;
00267         tag.key = key;
00268         tag.value = value;
00269         tag.srcid = srcid;
00270         this->add_item_tag(which_output, tag);
00271     }
00272 
00273  /*!
00274    * \brief  Adds a new tag onto the given output buffer.
00275    *
00276    * \param which_output an integer of which output stream to attach the tag
00277    * \param tag the tag object to add
00278    */
00279   void add_item_tag(unsigned int which_output, const gr_tag_t &tag);
00280 
00281   /*!
00282    * \brief Given a [start,end), returns a vector of all tags in the range.
00283    *
00284    * Range of counts is from start to end-1.
00285    *
00286    * Tags are tuples of:
00287    *      (item count, source id, key, value)
00288    *
00289    * \param v            a vector reference to return tags into
00290    * \param which_input  an integer of which input stream to pull from
00291    * \param abs_start    a uint64 count of the start of the range of interest
00292    * \param abs_end      a uint64 count of the end of the range of interest
00293    */
00294   void get_tags_in_range(std::vector<gr_tag_t> &v,
00295                          unsigned int which_input,
00296                          uint64_t abs_start,
00297                          uint64_t abs_end);
00298   
00299   /*!
00300    * \brief Given a [start,end), returns a vector of all tags in the range
00301    * with a given key.
00302    *
00303    * Range of counts is from start to end-1.
00304    *
00305    * Tags are tuples of:
00306    *      (item count, source id, key, value)
00307    *
00308    * \param v            a vector reference to return tags into
00309    * \param which_input  an integer of which input stream to pull from
00310    * \param abs_start    a uint64 count of the start of the range of interest
00311    * \param abs_end      a uint64 count of the end of the range of interest
00312    * \param key          a PMT symbol key to filter only tags of this key
00313    */
00314   void get_tags_in_range(std::vector<gr_tag_t> &v,
00315                          unsigned int which_input,
00316                          uint64_t abs_start,
00317                          uint64_t abs_end,
00318                          const pmt::pmt_t &key);
00319 
00320   // These are really only for internal use, but leaving them public avoids
00321   // having to work up an ever-varying list of friend GR_CORE_APIs
00322 
00323  public:
00324   gr_block_detail_sptr detail () const { return d_detail; }
00325   void set_detail (gr_block_detail_sptr detail) { d_detail = detail; }
00326 };
00327 
00328 typedef std::vector<gr_block_sptr> gr_block_vector_t;
00329 typedef std::vector<gr_block_sptr>::iterator gr_block_viter_t;
00330 
00331 inline gr_block_sptr cast_to_block_sptr(gr_basic_block_sptr p)
00332 {
00333   return boost::dynamic_pointer_cast<gr_block, gr_basic_block>(p);
00334 }
00335 
00336 
00337 std::ostream&
00338 operator << (std::ostream& os, const gr_block *m);
00339 
00340 #endif /* INCLUDED_GR_BLOCK_H */