]> git.gag.com Git - debian/gnuradio/blob - gr-vrt/src/vrt_source_32fc.cc
Merged VRT work-in-progress from eb/vrt2 (11518:11598) into trunk.
[debian/gnuradio] / gr-vrt / src / vrt_source_32fc.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008,2009 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <vrt_source_32fc.h>
28 #include <vrt/expanded_header.h>
29 #include <vrt/copiers.h>
30 #include <gr_io_signature.h>
31 #include <missing_pkt_checker.h>
32 #include <iostream>
33
34 #define VERBOSE 1               // define to 0 or 1
35
36
37 class rx_32fc_handler : public vrt::rx_packet_handler
38 {
39   int                                 d_noutput_items;
40   std::complex<float>                *d_out;
41   int                                *d_oo;             // output index
42   std::vector< std::complex<float> > &d_remainder;
43   missing_pkt_checker                &d_checker;
44   
45
46 public:
47
48   rx_32fc_handler(int noutput_items, std::complex<float> *out,
49                   int *oo, std::vector< std::complex<float> > &remainder,
50                   missing_pkt_checker &checker)
51     : d_noutput_items(noutput_items), d_out(out),
52       d_oo(oo), d_remainder(remainder), d_checker(checker) {}
53
54   ~rx_32fc_handler();
55
56   bool operator()(const uint32_t *payload,
57                   size_t n32_bit_words,
58                   const vrt::expanded_header *hdr);
59 };
60
61 rx_32fc_handler::~rx_32fc_handler()
62 {
63 }
64
65 bool
66 rx_32fc_handler::operator()(const uint32_t *payload,
67                             size_t n32_bit_words,
68                             const vrt::expanded_header *hdr)
69 {
70   int nmissing = d_checker.check(hdr);
71   if (VERBOSE && nmissing != 0){
72     std::cerr << "S" << nmissing;
73   }
74
75   // copy as many as will fit into the output buffer.
76
77   size_t n = std::min(n32_bit_words, (size_t)(d_noutput_items - *d_oo));
78   vrt::copy_net_16sc_to_host_32fc(n, payload, &d_out[*d_oo]);
79   *d_oo += n;
80
81   // if there are any left over, copy them into remainder and tell
82   // our caller we're had enough for now.
83
84   size_t r = n32_bit_words - n;
85   if (r > 0){
86     assert(d_remainder.size() == 0);
87     d_remainder.resize(r);
88     vrt::copy_net_16sc_to_host_32fc(r, &payload[n], &d_remainder[0]);
89     return false;               // Stop calling us.
90   }
91
92   return true;                  // Keep calling us, we've got more room
93 }
94
95
96 // ------------------------------------------------------------------------
97
98 vrt_source_32fc::vrt_source_32fc(const char *name)
99
100   : vrt_source_base(name,
101                     gr_make_io_signature(1, 1, sizeof(gr_complex)))
102 {
103 }
104
105 vrt_source_32fc::~vrt_source_32fc()
106 {
107   if (VERBOSE){
108     std::cerr << "\nvrt_source_32fc: npackets = " << d_checker.npackets()
109               << " nwrong_pkt_cnt = " << d_checker.nwrong_pkt_cnt()
110               << " nmissing_pkt_est = " << d_checker.nmissing_pkt_est()
111               << std::endl;
112   }
113 }
114
115 int
116 vrt_source_32fc::work(int noutput_items,
117                       gr_vector_const_void_star &input_items,
118                       gr_vector_void_star &output_items)
119 {
120   gr_complex *out = (gr_complex *)output_items[0];
121   int   oo = 0;
122
123   // Handle any samples left over from the last call.
124   int t = std::min(noutput_items, (int)d_remainder.size());
125   if (t != 0){
126     for (int i = 0; i < t; i++)
127       out[i] = d_remainder[i];
128     d_remainder.erase(d_remainder.begin(), d_remainder.begin()+t);
129     oo = t;
130   }
131   if (noutput_items - oo == 0)
132     return oo;
133   
134   // While we've got room, and there are packets, handle them
135   rx_32fc_handler h(noutput_items, out, &oo, d_remainder, d_checker);
136   bool ok = vrt_rx()->rx_packets(&h);
137
138   if (!ok){
139     std::cerr << "vrt_source_32fc: vrt::rx_packets() failed" << std::endl;
140     return -1;  // say we're done
141   }
142
143   return oo;
144 }