Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_ofdm_insert_preamble.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2007 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <gr_ofdm_insert_preamble.h>
27 #include <gr_io_signature.h>
28 #include <stdexcept>
29 #include <iostream>
30 #include <string.h>
31
32 gr_ofdm_insert_preamble_sptr
33 gr_make_ofdm_insert_preamble(int fft_length,
34                              const std::vector<std::vector<gr_complex> > &preamble)
35 {
36   return gr_ofdm_insert_preamble_sptr(new gr_ofdm_insert_preamble(fft_length,
37                                                                   preamble));
38 }
39
40 gr_ofdm_insert_preamble::gr_ofdm_insert_preamble
41        (int fft_length,
42         const std::vector<std::vector<gr_complex> > &preamble)
43   : gr_block("ofdm_insert_preamble",
44              gr_make_io_signature2(2, 2,
45                                    sizeof(gr_complex)*fft_length,
46                                    sizeof(char)),
47              gr_make_io_signature2(1, 2,
48                                    sizeof(gr_complex)*fft_length,
49                                    sizeof(char))),
50     d_fft_length(fft_length),
51     d_preamble(preamble),
52     d_state(ST_IDLE),
53     d_nsymbols_output(0),
54     d_pending_flag(0)
55 {
56   // sanity check preamble symbols
57   for (size_t i = 0; i < d_preamble.size(); i++){
58     if (d_preamble[i].size() != (size_t) d_fft_length)
59       throw std::invalid_argument("gr_ofdm_insert_preamble: invalid length for preamble symbol");
60   }
61
62   enter_idle();
63 }
64
65
66 gr_ofdm_insert_preamble::~gr_ofdm_insert_preamble()
67 {
68 }
69
70 int
71 gr_ofdm_insert_preamble::general_work (int noutput_items,
72                                        gr_vector_int &ninput_items_v,
73                                        gr_vector_const_void_star &input_items,
74                                        gr_vector_void_star &output_items)
75 {
76   int ninput_items = std::min(ninput_items_v[0], ninput_items_v[1]);
77   const gr_complex *in_sym = (const gr_complex *) input_items[0];
78   const unsigned char *in_flag = (const unsigned char *) input_items[1];
79
80   gr_complex *out_sym = (gr_complex *) output_items[0];
81   unsigned char *out_flag = 0;
82   if (output_items.size() == 2)
83     out_flag = (unsigned char *) output_items[1];
84
85
86   int no = 0;   // number items output
87   int ni = 0;   // number items read from input
88
89
90 #define write_out_flag()                        \
91   do { if (out_flag)                            \
92           out_flag[no] = d_pending_flag;        \
93        d_pending_flag = 0;                      \
94   } while(0)
95
96
97   while (no < noutput_items && ni < ninput_items){
98     switch(d_state){
99     case ST_IDLE:
100       if (in_flag[ni] & 0x1)    // this is first symbol of new payload
101         enter_preamble();
102       else
103         ni++;                   // eat one input symbol
104       break;
105       
106     case ST_PREAMBLE:
107       assert(in_flag[ni] & 0x1);
108       if (d_nsymbols_output >= (int) d_preamble.size()){
109         // we've output all the preamble
110         enter_first_payload();
111       }
112       else {
113         memcpy(&out_sym[no * d_fft_length],
114                &d_preamble[d_nsymbols_output][0],
115                d_fft_length*sizeof(gr_complex));
116
117         write_out_flag();
118         no++;
119         d_nsymbols_output++;
120       }
121       break;
122       
123     case ST_FIRST_PAYLOAD:
124       // copy first payload symbol from input to output
125       memcpy(&out_sym[no * d_fft_length],
126              &in_sym[ni * d_fft_length],
127              d_fft_length * sizeof(gr_complex));
128
129       write_out_flag();
130       no++;
131       ni++;
132       enter_payload();
133       break;
134       
135     case ST_PAYLOAD:
136       if (in_flag[ni] & 0x1){   // this is first symbol of a new payload
137         enter_preamble();
138         break;
139       }
140
141       // copy a symbol from input to output
142       memcpy(&out_sym[no * d_fft_length],
143              &in_sym[ni * d_fft_length],
144              d_fft_length * sizeof(gr_complex));
145
146       write_out_flag();
147       no++;
148       ni++;
149       break;
150
151     default:
152       std::cerr << "gr_ofdm_insert_preamble: (can't happen) invalid state, resetting\n";
153       enter_idle();
154     }
155   }
156
157   consume_each(ni);
158   return no;
159 }
160
161 void
162 gr_ofdm_insert_preamble::enter_idle()
163 {
164   d_state = ST_IDLE;
165   d_nsymbols_output = 0;
166   d_pending_flag = 0;
167 }
168
169 void
170 gr_ofdm_insert_preamble::enter_preamble()
171 {
172   d_state = ST_PREAMBLE;
173   d_nsymbols_output = 0;
174   d_pending_flag = 1;
175 }
176
177 void
178 gr_ofdm_insert_preamble::enter_first_payload()
179 {
180   d_state = ST_FIRST_PAYLOAD;
181 }
182
183 void
184 gr_ofdm_insert_preamble::enter_payload()
185 {
186   d_state = ST_PAYLOAD;
187 }