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>
37 gr_correlate_access_code_bb_sptr
38 gr_make_correlate_access_code_bb (const std::string &access_code, int threshold)
40 return gr_correlate_access_code_bb_sptr (new gr_correlate_access_code_bb (access_code, threshold));
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)
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");
59 gr_correlate_access_code_bb::~gr_correlate_access_code_bb ()
64 gr_correlate_access_code_bb::set_access_code(
65 const std::string &access_code)
67 unsigned len = access_code.length(); // # of bytes in string
71 // set len top bits to 1.
72 d_mask = ((~0ULL) >> (64 - len)) << (64 - len);
74 d_flag_bit = 1LL << (64 - len); // Where we or-in new flag values.
75 // new data always goes in 0x0000000000000001
77 for (unsigned i=0; i < 64; i++){
80 d_access_code |= access_code[i] & 1; // look at LSB only
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)
91 const unsigned char *in = (const unsigned char *) input_items[0];
92 unsigned char *out = (unsigned char *) output_items[0];
94 for (int i = 0; i < noutput_items; i++){
96 // compute output value
99 t |= ((d_data_reg >> 63) & 0x1) << 0;
100 t |= ((d_flag_reg >> 63) & 0x1) << 1; // flag bit
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;
108 wrong_bits = (d_data_reg ^ d_access_code) & d_mask;
109 nwrong = gr_count_bits64(wrong_bits);
111 // test for access code with up to threshold errors
112 new_flag = (nwrong <= d_threshold);
116 fprintf(stderr, "access code found: %llx\n", d_access_code);
119 fprintf(stderr, "%llx ==> %llx\n", d_access_code, d_data_reg);
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);
127 d_flag_reg |= d_flag_bit;
131 return noutput_items;