GNU Radio 3.6.5 C++ API

gr_tagged_stream_block.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2013 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_TAGGED_STREAM_BLOCK_H
00024 #define INCLUDED_GR_TAGGED_STREAM_BLOCK_H
00025 
00026 #include <gr_core_api.h>
00027 #include <gr_block.h>
00028 
00029 /*!
00030  * \brief Block that operates on PDUs in form of tagged streams
00031  * \ingroup base_blk
00032  *
00033  * Override work to provide the signal processing implementation.
00034  */
00035 class GR_CORE_API gr_tagged_stream_block : public gr_block
00036 {
00037  private:
00038   pmt::pmt_t d_length_tag_key; //!< This is the key for the tag that stores the PDU length
00039   gr_vector_int d_n_input_items_reqd; //!< How many input items do I need to process the next PDU?
00040 
00041  protected:
00042   std::string d_length_tag_key_str;
00043   gr_tagged_stream_block (void){} //allows pure virtual interface sub-classes
00044   gr_tagged_stream_block (const std::string &name,
00045                  gr_io_signature_sptr input_signature,
00046                  gr_io_signature_sptr output_signature,
00047                  const std::string &length_tag_key);
00048 
00049   /*!
00050    * \brief Parse all tags on the first sample of a PDU, return the number of items per input
00051    *        and prune the length tags.
00052    *
00053    * In most cases, you don't need to override this, unless the number of items read
00054    * is not directly coded in one single tag.
00055    *
00056    * Default behaviour:
00057    * - Go through all input ports
00058    * - On every input port, search for the tag with the key specified in \p length_tag_key
00059    * - Copy that value as an int to the corresponding position in \p n_input_items_reqd
00060    * - Remove the length tag.
00061    *
00062    * \param[in] tags All the tags found on the first item of every input port.
00063    * \param[out] n_input_items_reqd Number of items which will be read from every input
00064    */
00065   virtual void parse_length_tags(
00066       const std::vector<std::vector<gr_tag_t> > &tags,
00067       gr_vector_int &n_input_items_reqd
00068   );
00069 
00070   /*!
00071    * \brief Calculate the number of output items.
00072    *
00073    * This is basically the inverse function to forecast(): Given a number of input
00074    * items, it returns the maximum number of output items.
00075    *
00076    * You most likely need to override this function, unless your block is a sync
00077    * block or integer interpolator/decimator.
00078    *
00079    */
00080   virtual int calculate_output_stream_length(const gr_vector_int &ninput_items);
00081 
00082   /*!
00083    * \brief Set the new length tags on the output stream
00084    *
00085    * Default behaviour: Set a tag with key \p length_tag_key and
00086    * the number of produced items on every output port.
00087    *
00088    * For anything else, override this.
00089    *
00090    * \param n_produced Length of the new PDU
00091    * \param n_ports Number of output ports
00092    */
00093   virtual void update_length_tags(int n_produced, int n_ports);
00094 
00095  public:
00096 
00097   /*! \brief Don't override this.
00098    */
00099   void /* final */ forecast (int noutput_items, gr_vector_int &ninput_items_required);
00100 
00101   /*!
00102    * - Reads the number of input items from the tags using parse_length_tags()
00103    * - Checks there's enough data on the input and output buffers
00104    * - If not, inform the scheduler and do nothing
00105    * - Calls work() with the exact number of items per PDU
00106    * - Updates the tags using update_length_tags()
00107    */
00108   int general_work (int noutput_items,
00109                      gr_vector_int &ninput_items,
00110                      gr_vector_const_void_star &input_items,
00111                      gr_vector_void_star &output_items);
00112 
00113   /*!
00114    * \brief Just like gr_block::general_work, but makes sure the input is valid
00115    *
00116    * The user must override work to define the signal processing code.
00117    * Check the documentation for general_work() to see what happens here.
00118    *
00119    * Like gr_sync_block, this calls consume() for you (it consumes ninput_items[i]
00120    * items from the i-th port).
00121    *
00122    * A note on tag propagation: The PDU length tags are handled by other functions,
00123    * but all other tags are handled just as in any other \p gr_block. So, most likely,
00124    * you either set the tag propagation policy to TPP_DONT and handle the tag
00125    * propagation manually, or you propagate tags through the scheduler and don't
00126    * do anything here.
00127    *
00128    * \param noutput_items The size of the writable output buffer
00129    * \param ninput_items The exact size of the items on every input for this particular PDU.
00130    *                     These will be consumed if a length tag key is provided!
00131    * \param input_items See gr_block
00132    * \param output_items See gr_block
00133    */
00134   virtual int work (int noutput_items,
00135                     gr_vector_int &ninput_items,
00136                     gr_vector_const_void_star &input_items,
00137                     gr_vector_void_star &output_items) = 0;
00138 
00139 };
00140 
00141 #endif /* INCLUDED_GR_TAGGED_STREAM_BLOCK_H */
00142