Imported Upstream version 3.2.2
[debian/gnuradio] / gr-pager / src / pager_flex_deinterleave.cc
1 /*
2  * Copyright 2004,2006 Free Software Foundation, Inc.
3  * 
4  * This file is part of GNU Radio
5  * 
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  * 
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <pager_flex_deinterleave.h>
27 #include <pageri_bch3221.h>
28 #include <pageri_util.h>
29 #include <gr_io_signature.h>
30
31 pager_flex_deinterleave_sptr pager_make_flex_deinterleave()
32 {
33     return pager_flex_deinterleave_sptr(new pager_flex_deinterleave());
34 }
35
36 pager_flex_deinterleave::pager_flex_deinterleave() :
37     gr_sync_decimator("flex_deinterleave",
38     gr_make_io_signature(1, 1, sizeof(unsigned char)),
39     gr_make_io_signature(1, 1, sizeof(gr_int32)), 32)
40 {
41     set_output_multiple(8); // One FLEX block at a time
42 }
43
44 int pager_flex_deinterleave::work(int noutput_items,
45     gr_vector_const_void_star &input_items,
46     gr_vector_void_star &output_items)
47 {
48     const unsigned char *in = (const unsigned char *)input_items[0];
49     gr_int32 *out = (gr_int32 *)output_items[0];    
50
51     // FLEX codewords are interleaved in blocks of 256 bits or 8, 32 bit
52     // codes.  To deinterleave we parcel each incoming bit into the MSB
53     // of each codeword, then switch to MSB-1, etc.  This is done by shifting
54     // in the bits from the right on each codeword as the bits come in.
55     // When we are done we have a FLEX block of eight codewords, ready for
56     // conversion to data words.
57     //
58     // FLEX data words are recovered by reversing the bit order of the code
59     // word, masking off the (reversed) ECC, and inverting the remainder of 
60     // the bits (!).
61     //
62     // The data portion of a FLEX frame consists of 11 of these deinterleaved
63     // and converted blocks.
64     //
65     // set_output_multiple garauntees we have output space for at least
66     // eight data words, and 256 bits are supplied on input
67
68     int i, j;
69     for (i = 0; i < 32; i++) {
70         for (j = 0; j < 8; j++) {
71             d_codewords[j] <<= 1;
72             d_codewords[j]  |= *in++;
73         }
74     }
75
76     // Now convert code words into data words  
77     for (j = 0; j < 8; j++) {
78         gr_int32 codeword = d_codewords[j];
79         
80         // Apply BCH 32,21 error correction
81         // TODO: mark dataword when codeword fails ECC
82         pageri_bch3221(codeword);
83         
84         // Reverse bit order
85         codeword = pageri_reverse_bits32(codeword);
86
87         // Mask off ECC then invert lower 21 bits
88         codeword = (codeword & 0x001FFFFF)^0x001FFFFF;
89
90         *out++ = codeword;
91     }
92     
93     return j;
94 }