GNU Radio 3.4.0 C++ API
gr_test.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2006 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_TEST_H
00024 #define INCLUDED_GR_TEST_H
00025 
00026 #include <gr_block.h>
00027 #include <string>
00028 #include "gr_test_types.h"
00029 
00030 class gr_test;
00031 typedef boost::shared_ptr<gr_test> gr_test_sptr;
00032 
00033 // public constructor
00034 gr_test_sptr gr_make_test (const std::string &name=std::string("gr_test"),
00035         int min_inputs=1, int max_inputs=1, unsigned int sizeof_input_item=1,
00036         int min_outputs=1, int max_outputs=1, unsigned int sizeof_output_item=1,
00037         unsigned int history=1,unsigned int output_multiple=1,double relative_rate=1.0,
00038         bool fixed_rate=true,gr_consume_type_t cons_type=CONSUME_NOUTPUT_ITEMS, gr_produce_type_t prod_type=PRODUCE_NOUTPUT_ITEMS);
00039 
00040 /*!
00041  * \brief Test class for testing runtime system (setting up buffers and such.)
00042  * \ingroup misc
00043  *
00044  * This block does not do any usefull actual data processing.
00045  * It just exposes setting all standard block parameters using the contructor or public methods.
00046  *
00047  * This block can be usefull when testing the runtime system.
00048  * You can force this block to have a large history, decimation 
00049  * factor and/or large output_multiple.
00050  * The runtime system should detect this and create large enough buffers
00051  * all through the signal chain.
00052  */
00053 class gr_test : public gr_block {
00054 
00055  public:
00056   
00057   ~gr_test (){}
00058   
00059 int general_work (int noutput_items,
00060                             gr_vector_int &ninput_items,
00061                             gr_vector_const_void_star &input_items,
00062                             gr_vector_void_star &output_items);
00063   // ----------------------------------------------------------------
00064   //            override these to define your behavior
00065   // ----------------------------------------------------------------
00066 
00067   /*!
00068    * \brief  Estimate input requirements given output request
00069    *
00070    * \param noutput_items           number of output items to produce
00071    * \param ninput_items_required   number of input items required on each input stream
00072    *
00073    * Given a request to product \p noutput_items, estimate the number of
00074    * data items required on each input stream.  The estimate doesn't have
00075    * to be exact, but should be close.
00076    */
00077    void forecast (int noutput_items,
00078                          gr_vector_int &ninput_items_required)
00079    {
00080      unsigned ninputs = ninput_items_required.size ();
00081      for (unsigned i = 0; i < ninputs; i++)
00082        ninput_items_required[i] = (int)((double)noutput_items / relative_rate()) + (int)history();
00083    }
00084 
00085 
00086   /*!
00087    * \brief Force check topology to return true or false.
00088    *
00089    * \param check_topology      value to return when check_topology is called (true or false)
00090    * default check_topology returns true
00091    *
00092    */
00093    void set_check_topology (bool check_topology){ d_check_topology=check_topology;}
00094 
00095   /*!
00096    * \brief Confirm that ninputs and noutputs is an acceptable combination.
00097    *
00098    * \param ninputs     number of input streams connected
00099    * \param noutputs    number of output streams connected
00100    *
00101    * \returns true if this is a valid configuration for this block.
00102    *
00103    * This function is called by the runtime system whenever the
00104    * topology changes.  Most classes do not need to override this.
00105    * This check is in addition to the constraints specified by the input
00106    * and output gr_io_signatures.
00107    */
00108   bool check_topology (int ninputs, int noutputs) { return d_check_topology;}
00109 
00110   // ----------------------------------------------------------------
00111   /*
00112    * The following two methods provide special case info to the
00113    * scheduler in the event that a block has a fixed input to output
00114    * ratio.  gr_sync_block, gr_sync_decimator and gr_sync_interpolator
00115    * override these.  If you're fixed rate, subclass one of those.
00116    */
00117   /*!
00118    * \brief Given ninput samples, return number of output samples that will be produced.
00119    * N.B. this is only defined if fixed_rate returns true.
00120    * Generally speaking, you don't need to override this.
00121    */
00122   int fixed_rate_ninput_to_noutput(int ninput) { return (int)((double)ninput/relative_rate()); }
00123 
00124   /*!
00125    * \brief Given noutput samples, return number of input samples required to produce noutput.
00126    * N.B. this is only defined if fixed_rate returns true.
00127    */
00128   int fixed_rate_noutput_to_ninput(int noutput)  { return (int)((double)noutput*relative_rate()); }
00129 
00130   /*!
00131    * \brief Set if fixed rate should return true.
00132    * N.B. This is normally a private method but we make it available here as public.
00133    */
00134   void set_fixed_rate_public(bool fixed_rate){ set_fixed_rate(fixed_rate);}
00135 
00136   /*!
00137    * \brief Set the consume pattern.
00138    *
00139    * \param cons_type   which consume pattern to use
00140    */
00141   void set_consume_type (gr_consume_type_t cons_type) { d_consume_type=cons_type;}
00142 
00143   /*!
00144    * \brief Set the consume limit.
00145    *
00146    * \param limit       min or maximum items to consume (depending on consume_type)
00147    */
00148   void set_consume_limit (unsigned int limit) { d_min_consume=limit; d_max_consume=limit;}
00149 
00150   /*!
00151    * \brief Set the produce pattern.
00152    *
00153    * \param prod_type   which produce pattern to use
00154    */
00155   void set_produce_type (gr_produce_type_t prod_type) { d_produce_type=prod_type;}
00156 
00157   /*!
00158    * \brief Set the produce limit.
00159    *
00160    * \param limit       min or maximum items to produce (depending on produce_type)
00161    */
00162   void set_produce_limit (unsigned int limit) { d_min_produce=limit; d_max_produce=limit;}
00163 
00164   // ----------------------------------------------------------------------------
00165 
00166 
00167   
00168  protected:
00169   unsigned int d_sizeof_input_item;
00170   unsigned int d_sizeof_output_item;
00171   bool d_check_topology;
00172   char d_temp;
00173   gr_consume_type_t d_consume_type;
00174   int d_min_consume;
00175   int d_max_consume;
00176   gr_produce_type_t d_produce_type;
00177   int d_min_produce;
00178   int d_max_produce;
00179   gr_test (const std::string &name,int min_inputs, int max_inputs, unsigned int sizeof_input_item,
00180                                    int min_outputs, int max_outputs, unsigned int sizeof_output_item,
00181                                    unsigned int history,unsigned int output_multiple,double relative_rate,
00182                                    bool fixed_rate,gr_consume_type_t cons_type, gr_produce_type_t prod_type);
00183 
00184 
00185 
00186   friend gr_test_sptr gr_make_test (const std::string &name,int min_inputs, int max_inputs, unsigned int sizeof_input_item,
00187                                    int min_outputs, int max_outputs, unsigned int sizeof_output_item,
00188                                    unsigned int history,unsigned int output_multiple,double relative_rate,
00189                                    bool fixed_rate,gr_consume_type_t cons_type, gr_produce_type_t prod_type);
00190 };
00191 
00192 
00193 
00194 #endif /* INCLUDED_GR_TEST_H */