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