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