Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_correlate_access_code_bb.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 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 2, 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_correlate_access_code_bb.h>
28 #include <gr_io_signature.h>
29 #include <stdexcept>
30 #include <gr_count_bits.h>
31
32 #define VERBOSE 0
33
34
35 gr_correlate_access_code_bb_sptr
36 gr_make_correlate_access_code_bb (const std::string &access_code, int threshold)
37 {
38   return gr_correlate_access_code_bb_sptr (new gr_correlate_access_code_bb (access_code, threshold));
39 }
40
41
42 gr_correlate_access_code_bb::gr_correlate_access_code_bb (
43   const std::string &access_code, int threshold)
44   : gr_sync_block ("correlate_access_code_bb",
45                    gr_make_io_signature (1, 1, sizeof(char)),
46                    gr_make_io_signature (1, 1, sizeof(char))),
47     d_data_reg(0), d_flag_reg(0), d_flag_bit(0), d_mask(0),
48     d_threshold(threshold), d_flip(0)
49
50 {
51   if (!set_access_code(access_code)){
52     fprintf(stderr, "gr_correlate_access_code_bb: access_code is > 64 bits\n");
53     throw std::out_of_range ("access_code is > 64 bits");
54   }
55 }
56
57 gr_correlate_access_code_bb::~gr_correlate_access_code_bb ()
58 {
59 }
60
61 bool
62 gr_correlate_access_code_bb::set_access_code(
63   const std::string &access_code)
64 {
65   unsigned len = access_code.length();  // # of bytes in string
66   if (len > 64)
67     return false;
68
69   // set len top bits to 1.
70   d_mask = ((~0ULL) >> (64 - len)) << (64 - len);
71
72   d_flag_bit = 1LL << (64 - len);       // Where we or-in new flag values.
73                                         // new data always goes in 0x0000000000000001
74   d_access_code = 0;
75   for (unsigned i=0; i < 64; i++){
76     d_access_code <<= 1;
77     if (i < len)
78       d_access_code |= access_code[i] & 1;      // look at LSB only
79   }
80
81   return true;
82 }
83
84 int
85 gr_correlate_access_code_bb::work (int noutput_items,
86                                    gr_vector_const_void_star &input_items,
87                                    gr_vector_void_star &output_items)
88 {
89   const unsigned char *in = (const unsigned char *) input_items[0];
90   unsigned char *out = (unsigned char *) output_items[0];
91   
92   for (int i = 0; i < noutput_items; i++){
93
94     // compute output value
95     unsigned int t = 0;
96
97     t |= d_flip ^ (((d_data_reg >> 63) & 0x1) << 0);
98     t |= ((d_flag_reg >> 63) & 0x1) << 1;       // flag bit
99     out[i] = t;
100     
101     // compute hamming distance between desired access code and current data
102     unsigned long long wrong_bits = 0;
103     unsigned int nwrong = d_threshold+1;
104     int new_flag = 0;
105
106     wrong_bits  = (d_data_reg ^ d_access_code) & d_mask;
107     nwrong = gr_count_bits64(wrong_bits);
108
109     // test for access code with up to threshold errors or its compelement
110     new_flag = (nwrong <= d_threshold) || (nwrong >= (64-d_threshold));
111
112 #if 0   
113     if(new_flag) {
114       printf("%llx  ==>  %llx  :  d_flip=%u\n", d_access_code, d_data_reg, d_flip);
115     }
116 #endif
117
118     // shift in new data and new flag
119     d_data_reg = (d_data_reg << 1) | (in[i] & 0x1);
120     d_flag_reg = (d_flag_reg << 1);
121     if (new_flag) {
122       d_flag_reg |= d_flag_bit;
123       d_flip = nwrong >= (64-d_threshold);   // flip bits if this is true
124     }
125   }
126
127   return noutput_items;
128 }
129