GNU Radio 3.4.0 C++ API
rx_nop_handler.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2008 Free Software Foundation, Inc.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 #ifndef INCLUDED_RX_NOP_HANDLER_H
00020 #define INCLUDED_RX_NOP_HANDLER_H
00021 
00022 #include <usrp2/rx_sample_handler.h>
00023 #include <boost/shared_ptr.hpp>
00024 
00025 /*!
00026  * \ingroup usrp2
00027  *
00028  * Base class for receive handlers that must copy into potentially limited
00029  * range destination buffers.
00030  *
00031  * Maintains counters for number of items copied, times invoked, and test
00032  * for whether maximum has been reached.
00033  *
00034  * Derived classes should override the () operator, but call this
00035  * parent class method at some point at the start of their own operations.
00036  */ 
00037 
00038 namespace usrp2 {
00039 
00040   class rx_nop_handler : public rx_sample_handler
00041   {
00042     uint64_t    d_max_samples;
00043     uint64_t    d_max_quantum;
00044     uint64_t    d_nsamples;
00045     uint64_t    d_nframes;
00046     
00047   protected:
00048     bool        d_err;
00049     
00050   public:
00051     
00052     // Shared pointer to an instance of this class
00053     typedef boost::shared_ptr<rx_nop_handler> sptr;
00054 
00055     /*!
00056      * Constructor
00057      *
00058      * \param max_samples  Maximum number of samples to copy. Use zero for no maximum.
00059      * \param max_quantum  Maximum number of samples required to accept in one call.
00060      *                     Use 0 to indicate no maximum.
00061      */
00062     rx_nop_handler(uint64_t max_samples, uint64_t max_quantum=0)
00063       : d_max_samples(max_samples), d_max_quantum(max_quantum), 
00064         d_nsamples(0), d_nframes(0), d_err(false) {}
00065       
00066     /*!
00067      * Destructor.  Derived classes must implement their own, non-inline destructor.
00068      */
00069     virtual ~rx_nop_handler();
00070       
00071     /*!
00072      * \brief Returns number of frames this copier was called with
00073      */
00074     uint64_t nframes() const { return d_nframes; }
00075 
00076     /*!
00077      * \brief Returns actual number of samples copied
00078      */
00079     uint64_t nsamples() const { return d_nsamples; }
00080 
00081     /*!
00082      * \brief Returns maximum number of samples that will be copied
00083      */
00084     uint64_t max_samples() const { return d_max_samples; }
00085 
00086     /*!
00087      * Returns true if an error has occurred. Derived classes must set d_err to true
00088      * when an error occurs in the () operator
00089      */
00090     bool has_errored_p() const { return d_err; }
00091 
00092     /*!
00093      * \brief Returns true if this instance has reached the maximum number of samples
00094      */
00095     bool has_finished_p() const 
00096     { return d_max_samples == 0 ? false : d_nsamples >= d_max_samples-d_max_quantum; }
00097       
00098 
00099     /*!
00100      * Function operator invoked by USRP2 RX API. Derived classes must override this method
00101      * but then invoke it at the start of their processing.  This operator will always be
00102      * called at least once.
00103      *
00104      * \param items points to the first 32-bit word of uninterpreted sample data in the frame.
00105      * \param nitems is the number of entries in the frame in units of uint32_t's.
00106      * \param metadata is the additional per frame data provided by the USRP2 FPGA.
00107      *
00108      * \p items points to the raw sample data received off of the ethernet.  The data is
00109      * packed into big-endian 32-bit unsigned ints for transport, but the actual format
00110      * of the data is dependent on the current configuration of the USRP2.  The most common
00111      * format is 16-bit I & Q, with I in the top of the 32-bit word.
00112      *
00113      * \returns true if the object wants to be called again with new data;
00114      * false if no additional data is wanted.
00115      */
00116     virtual bool operator()(const uint32_t *items, size_t nitems, const rx_metadata *metadata)
00117     {
00118       // printf("W0: %08x  TS: %08x\n", metadata->word0, metadata->timestamp);
00119       // printf("I0: %08x\n", items[0]);
00120       
00121       d_nsamples += nitems;
00122       d_nframes++;
00123       
00124       return !has_finished_p();
00125     }
00126   };
00127   
00128 } /* namespace usrp2 */
00129 
00130 #endif /* INCLUDED_RX_NOP_HANDLER */