Imported Upstream version 3.2.2
[debian/gnuradio] / usrp2 / host / include / usrp2 / rx_nop_handler.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 Free Software Foundation, Inc.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifndef INCLUDED_RX_NOP_HANDLER_H
20 #define INCLUDED_RX_NOP_HANDLER_H
21
22 #include <usrp2/rx_sample_handler.h>
23 #include <boost/shared_ptr.hpp>
24
25 /*!
26  * \ingroup usrp2
27  *
28  * Base class for receive handlers that must copy into potentially limited
29  * range destination buffers.
30  *
31  * Maintains counters for number of items copied, times invoked, and test
32  * for whether maximum has been reached.
33  *
34  * Derived classes should override the () operator, but call this
35  * parent class method at some point at the start of their own operations.
36  */ 
37
38 namespace usrp2 {
39
40   class rx_nop_handler : public rx_sample_handler
41   {
42     uint64_t    d_max_samples;
43     uint64_t    d_max_quantum;
44     uint64_t    d_nsamples;
45     uint64_t    d_nframes;
46     
47   protected:
48     bool        d_err;
49     
50   public:
51     
52     // Shared pointer to an instance of this class
53     typedef boost::shared_ptr<rx_nop_handler> sptr;
54
55     /*!
56      * Constructor
57      *
58      * \param max_samples  Maximum number of samples to copy. Use zero for no maximum.
59      * \param max_quantum  Maximum number of samples required to accept in one call.
60      *                     Use 0 to indicate no maximum.
61      */
62     rx_nop_handler(uint64_t max_samples, uint64_t max_quantum=0)
63       : d_max_samples(max_samples), d_max_quantum(max_quantum), 
64         d_nsamples(0), d_nframes(0), d_err(false) {}
65       
66     /*!
67      * Destructor.  Derived classes must implement their own, non-inline destructor.
68      */
69     virtual ~rx_nop_handler();
70       
71     /*!
72      * \brief Returns number of frames this copier was called with
73      */
74     uint64_t nframes() const { return d_nframes; }
75
76     /*!
77      * \brief Returns actual number of samples copied
78      */
79     uint64_t nsamples() const { return d_nsamples; }
80
81     /*!
82      * \brief Returns maximum number of samples that will be copied
83      */
84     uint64_t max_samples() const { return d_max_samples; }
85
86     /*!
87      * Returns true if an error has occurred. Derived classes must set d_err to true
88      * when an error occurs in the () operator
89      */
90     bool has_errored_p() const { return d_err; }
91
92     /*!
93      * \brief Returns true if this instance has reached the maximum number of samples
94      */
95     bool has_finished_p() const 
96     { return d_max_samples == 0 ? false : d_nsamples >= d_max_samples-d_max_quantum; }
97       
98
99     /*!
100      * Function operator invoked by USRP2 RX API. Derived classes must override this method
101      * but then invoke it at the start of their processing.  This operator will always be
102      * called at least once.
103      *
104      * \param items points to the first 32-bit word of uninterpreted sample data in the frame.
105      * \param nitems is the number of entries in the frame in units of uint32_t's.
106      * \param metadata is the additional per frame data provided by the USRP2 FPGA.
107      *
108      * \p items points to the raw sample data received off of the ethernet.  The data is
109      * packed into big-endian 32-bit unsigned ints for transport, but the actual format
110      * of the data is dependent on the current configuration of the USRP2.  The most common
111      * format is 16-bit I & Q, with I in the top of the 32-bit word.
112      *
113      * \returns true if the object wants to be called again with new data;
114      * false if no additional data is wanted.
115      */
116     virtual bool operator()(const uint32_t *items, size_t nitems, const rx_metadata *metadata)
117     {
118       // printf("W0: %08x  TS: %08x\n", metadata->word0, metadata->timestamp);
119       // printf("I0: %08x\n", items[0]);
120       
121       d_nsamples += nitems;
122       d_nframes++;
123       
124       return !has_finished_p();
125     }
126   };
127   
128 } /* namespace usrp2 */
129
130 #endif /* INCLUDED_RX_NOP_HANDLER */