3 * Copyright 2004,2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
27 #include <gr_correlate_access_code_bb.h>
28 #include <gr_io_signature.h>
30 #include <gr_count_bits.h>
35 gr_correlate_access_code_bb_sptr
36 gr_make_correlate_access_code_bb (const std::string &access_code, int threshold)
38 return gr_correlate_access_code_bb_sptr (new gr_correlate_access_code_bb (access_code, threshold));
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)
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");
57 gr_correlate_access_code_bb::~gr_correlate_access_code_bb ()
62 gr_correlate_access_code_bb::set_access_code(
63 const std::string &access_code)
65 unsigned len = access_code.length(); // # of bytes in string
69 // set len top bits to 1.
70 d_mask = ((~0ULL) >> (64 - len)) << (64 - len);
72 d_flag_bit = 1LL << (64 - len); // Where we or-in new flag values.
73 // new data always goes in 0x0000000000000001
75 for (unsigned i=0; i < 64; i++){
78 d_access_code |= access_code[i] & 1; // look at LSB only
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)
89 const unsigned char *in = (const unsigned char *) input_items[0];
90 unsigned char *out = (unsigned char *) output_items[0];
92 for (int i = 0; i < noutput_items; i++){
94 // compute output value
97 t |= ((d_data_reg >> 63) & 0x1) << 0;
98 t |= ((d_flag_reg >> 63) & 0x1) << 1; // flag bit
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;
106 wrong_bits = (d_data_reg ^ d_access_code) & d_mask;
107 nwrong = gr_count_bits64(wrong_bits);
109 // test for access code with up to threshold errors
110 new_flag = (nwrong <= d_threshold);
114 printf("%llx ==> %llx : d_flip=%u\n", d_access_code, d_data_reg, d_flip);
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);
122 d_flag_reg |= d_flag_bit;
126 return noutput_items;