Add missing buffer allocator hint to gr_ofdm_sampler.cc
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_sampler.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007,2008,2010 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 <gr_ofdm_sampler.h>
28 #include <gr_io_signature.h>
29 #include <gr_expj.h>
30 #include <cstdio>
31
32 gr_ofdm_sampler_sptr
33 gr_make_ofdm_sampler (unsigned int fft_length, 
34                       unsigned int symbol_length,
35                       unsigned int timeout)
36 {
37   return gr_ofdm_sampler_sptr (new gr_ofdm_sampler (fft_length, symbol_length, timeout));
38 }
39
40 gr_ofdm_sampler::gr_ofdm_sampler (unsigned int fft_length, 
41                                   unsigned int symbol_length,
42                                   unsigned int timeout)
43   : gr_block ("ofdm_sampler",
44               gr_make_io_signature2 (2, 2, sizeof (gr_complex), sizeof(char)),
45               gr_make_io_signature2 (2, 2, sizeof (gr_complex)*fft_length, sizeof(char)*fft_length)),
46     d_state(STATE_NO_SIG), d_timeout_max(timeout), d_fft_length(fft_length), d_symbol_length(symbol_length)
47 {
48   set_relative_rate(1.0/(double) fft_length);   // buffer allocator hint
49 }
50
51 void
52 gr_ofdm_sampler::forecast (int noutput_items, gr_vector_int &ninput_items_required)
53 {
54   // FIXME do we need more
55   //int nreqd  = (noutput_items-1) * d_symbol_length + d_fft_length;
56   int nreqd  = d_symbol_length + d_fft_length;
57   unsigned ninputs = ninput_items_required.size ();
58   for (unsigned i = 0; i < ninputs; i++)
59     ninput_items_required[i] = nreqd;
60 }
61
62
63 int
64 gr_ofdm_sampler::general_work (int noutput_items,
65                                gr_vector_int &ninput_items,
66                                gr_vector_const_void_star &input_items,
67                                gr_vector_void_star &output_items)
68 {
69   const gr_complex *iptr = (const gr_complex *) input_items[0];
70   const char *trigger = (const char *) input_items[1];
71
72   gr_complex *optr = (gr_complex *) output_items[0];
73   char *outsig = (char *) output_items[1];
74
75   //FIXME: we only process a single OFDM symbol at a time; after the preamble, we can 
76   // process a few at a time as long as we always look out for the next preamble.
77
78   unsigned int index=d_fft_length;  // start one fft length into the input so we can always look back this far
79
80   outsig[0] = 0; // set output to no signal by default
81
82   // Search for a preamble trigger signal during the next symbol length
83   while((d_state != STATE_PREAMBLE) && (index <= (d_symbol_length+d_fft_length))) {
84     if(trigger[index]) {
85       outsig[0] = 1; // tell the next block there is a preamble coming
86       d_state = STATE_PREAMBLE;
87     }
88     else
89       index++;
90   }
91   
92   unsigned int i, pos, ret;
93   switch(d_state) {
94   case(STATE_PREAMBLE):
95     // When we found a preamble trigger, get it and set the symbol boundary here
96     for(i = (index - d_fft_length + 1); i <= index; i++) {
97       *optr++ = iptr[i];
98     }
99     
100     d_timeout = d_timeout_max; // tell the system to expect at least this many symbols for a frame
101     d_state = STATE_FRAME;
102     consume_each(index - d_fft_length + 1); // consume up to one fft_length away to keep the history
103     ret = 1;
104     break;
105     
106   case(STATE_FRAME):
107     // use this state when we have processed a preamble and are getting the rest of the frames
108     //FIXME: we could also have a power squelch system here to enter STATE_NO_SIG if no power is received
109
110     // skip over fft length history and cyclic prefix
111     pos = d_symbol_length;         // keeps track of where we are in the input buffer
112     while(pos < d_symbol_length + d_fft_length) {
113       *optr++ = iptr[pos++];
114     }
115
116     if(d_timeout-- == 0) {
117       printf("TIMEOUT\n");
118       d_state = STATE_NO_SIG;
119     }
120
121     consume_each(d_symbol_length); // jump up by 1 fft length and the cyclic prefix length
122     ret = 1;
123     break;
124
125   case(STATE_NO_SIG):
126   default:
127     consume_each(index-d_fft_length); // consume everything we've gone through so far leaving the fft length history
128     ret = 0;
129     break;
130   }
131
132   return ret;
133 }