summaryrefslogtreecommitdiff
path: root/gr-digital/lib/ofdm_insert_preamble_impl.cc
blob: dcc3a900a4393ca1161a85e6c09615184d2a27ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -*- c++ -*- */
/*
 * Copyright 2007,2010-2012 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 * 
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "ofdm_insert_preamble_impl.h"
#include <gnuradio/io_signature.h>
#include <stdexcept>
#include <iostream>
#include <string>

namespace gr {
  namespace digital {
    
    ofdm_insert_preamble::sptr
    ofdm_insert_preamble::make(int fft_length,
			       const std::vector<std::vector<gr_complex> > &preamble)
    {
      return gnuradio::get_initial_sptr
	(new ofdm_insert_preamble_impl(fft_length, preamble));
    }

    ofdm_insert_preamble_impl::ofdm_insert_preamble_impl(int fft_length,
        const std::vector<std::vector<gr_complex> > &preamble)
      : block("ofdm_insert_preamble",
		 io_signature::make2(1, 2,
				       sizeof(gr_complex)*fft_length,
				       sizeof(char)),
		 io_signature::make2(1, 2,
				       sizeof(gr_complex)*fft_length,
				       sizeof(char))),
	d_fft_length(fft_length),
	d_state(ST_IDLE),
	d_nsymbols_output(0),
	d_pending_flag(0),
	d_preamble(preamble)
    {
      // sanity check preamble symbols
      for(size_t i = 0; i < d_preamble.size(); i++) {
	if(d_preamble[i].size() != (size_t) d_fft_length)
	  throw std::invalid_argument("ofdm_insert_preamble_impl: invalid length for preamble symbol");
      }

      enter_idle();
    }


    ofdm_insert_preamble_impl::~ofdm_insert_preamble_impl()
    {
    }

    void
    ofdm_insert_preamble_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required)
    {
      ninput_items_required[0] = noutput_items;
    }

    int
    ofdm_insert_preamble_impl::general_work(int noutput_items,
					    gr_vector_int &ninput_items_v,
					    gr_vector_const_void_star &input_items,
					    gr_vector_void_star &output_items)
    {
      int ninput_items = ninput_items_v.size() == 2 ?
	std::min(ninput_items_v[0], ninput_items_v[1]) : ninput_items_v[0];

      const gr_complex *in_sym = (const gr_complex *)input_items[0];
      const unsigned char *in_flag = 0;

      if(input_items.size() == 2)
	in_flag = (const unsigned char*)input_items[1];

      gr_complex *out_sym = (gr_complex*)output_items[0];
      unsigned char *out_flag = 0;
      if(output_items.size() == 2)
	out_flag = (unsigned char*)output_items[1];

      int no = 0;	// number items output
      int ni = 0;	// number items read from input

#define write_out_flag() 			\
      do {                                      \
        if(out_flag)				\
	  out_flag[no] = d_pending_flag;	\
        d_pending_flag = 0; 			\
      } while(0)

      while(no < noutput_items && ni < ninput_items) {
	switch(d_state) {
	case ST_IDLE:
	  if(in_flag && in_flag[ni] & 0x1)	// this is first symbol of new payload
	    enter_preamble();
	  else
	    ni++;			// eat one input symbol
	  break;
      
	case ST_PREAMBLE:
	  assert(!in_flag || in_flag[ni] & 0x1);
	  if(d_nsymbols_output >= (int) d_preamble.size()) {
	    // we've output all the preamble
	    enter_first_payload();
	  }
	  else {
	    memcpy(&out_sym[no * d_fft_length],
		   &d_preamble[d_nsymbols_output][0],
		   d_fft_length*sizeof(gr_complex));

	    write_out_flag();
	    no++;
	    d_nsymbols_output++;
	  }
	  break;
      
	case ST_FIRST_PAYLOAD:
	  // copy first payload symbol from input to output
	  memcpy(&out_sym[no * d_fft_length],
		 &in_sym[ni * d_fft_length],
		 d_fft_length * sizeof(gr_complex));

	  write_out_flag();
	  no++;
	  ni++;
	  enter_payload();
	  break;
      
	case ST_PAYLOAD:
	  if(in_flag && in_flag[ni] & 0x1) {	// this is first symbol of a new payload
	    enter_preamble();
	    break;
	  }

	  // copy a symbol from input to output
	  memcpy(&out_sym[no * d_fft_length],
		 &in_sym[ni * d_fft_length],
		 d_fft_length * sizeof(gr_complex));

	  write_out_flag();
	  no++;
	  ni++;
	  break;

	default:
	  std::cerr << "ofdm_insert_preamble_impl: (can't happen) invalid state, resetting\n";
	  enter_idle();
	}
      }

    consume_each(ni);
    return no;
  }

  void
  ofdm_insert_preamble_impl::enter_idle()
  {
    d_state = ST_IDLE;
    d_nsymbols_output = 0;
    d_pending_flag = 0;
  }

  void
  ofdm_insert_preamble_impl::enter_preamble()
  {
    d_state = ST_PREAMBLE;
    d_nsymbols_output = 0;
    d_pending_flag = 1;
  }

  void
  ofdm_insert_preamble_impl::enter_first_payload()
  {
    d_state = ST_FIRST_PAYLOAD;
  }

  void
  ofdm_insert_preamble_impl::enter_payload()
  {
    d_state = ST_PAYLOAD;
  }

  } /* namespace digital */
} /* namespace gr */