3 * Copyright 2004 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_packet_sink.h>
28 #include <gr_io_signature.h>
31 #include <sys/types.h>
35 #include <gr_count_bits.h>
39 static const int DEFAULT_THRESHOLD = 12; // detect access code with up to DEFAULT_THRESHOLD bits wrong
42 gr_packet_sink::enter_search()
45 fprintf(stderr, "@ enter_search\n");
47 d_state = STATE_SYNC_SEARCH;
52 gr_packet_sink::enter_have_sync()
55 fprintf(stderr, "@ enter_have_sync\n");
57 d_state = STATE_HAVE_SYNC;
59 d_headerbitlen_cnt = 0;
63 gr_packet_sink::enter_have_header(int payload_len)
66 fprintf(stderr, "@ enter_have_header (payload_len = %d)\n", payload_len);
68 d_state = STATE_HAVE_HEADER;
69 d_packetlen = payload_len;
72 d_packet_byte_index = 0;
76 gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
77 gr_msg_queue_sptr target_queue, int threshold)
79 return gr_packet_sink_sptr (new gr_packet_sink (sync_vector, target_queue, threshold));
83 gr_packet_sink::gr_packet_sink (const std::vector<unsigned char>& sync_vector,
84 gr_msg_queue_sptr target_queue, int threshold)
85 : gr_sync_block ("packet_sink",
86 gr_make_io_signature (1, 1, sizeof(float)),
87 gr_make_io_signature (0, 0, 0)),
88 d_target_queue(target_queue), d_threshold(threshold == -1 ? DEFAULT_THRESHOLD : threshold)
93 d_sync_vector |= sync_vector[i];
99 gr_packet_sink::~gr_packet_sink ()
104 gr_packet_sink::work (int noutput_items,
105 gr_vector_const_void_star &input_items,
106 gr_vector_void_star &output_items)
108 float *inbuf = (float *) input_items[0];
112 fprintf(stderr,">>> Entering state machine\n"),fflush(stderr);
114 while (count<noutput_items) {
117 case STATE_SYNC_SEARCH: // Look for sync vector
119 fprintf(stderr,"SYNC Search, noutput=%d\n",noutput_items),fflush(stderr);
121 while (count < noutput_items) {
122 if(slice(inbuf[count++]))
123 d_shift_reg = (d_shift_reg << 1) | 1;
125 d_shift_reg = d_shift_reg << 1;
127 // Compute popcnt of putative sync vector
128 if(gr_count_bits64 (d_shift_reg ^ d_sync_vector) <= d_threshold) {
129 // Found it, set up for header decode
136 case STATE_HAVE_SYNC:
138 fprintf(stderr,"Header Search bitcnt=%d, header=0x%08x\n", d_headerbitlen_cnt, d_header),
141 while (count < noutput_items) { // Shift bits one at a time into header
142 if(slice(inbuf[count++]))
143 d_header = (d_header << 1) | 1;
145 d_header = d_header << 1;
147 if (++d_headerbitlen_cnt == HEADERBITLEN) {
150 fprintf(stderr, "got header: 0x%08x\n", d_header);
152 // we have a full header, check to see if it has been received properly
154 int payload_len = header_payload_len();
155 if (payload_len <= MAX_PKT_LEN) // reasonable?
156 enter_have_header(payload_len); // yes.
158 enter_search(); // no.
161 enter_search(); // no.
162 break; // we're in a new state
167 case STATE_HAVE_HEADER:
169 fprintf(stderr,"Packet Build\n"),fflush(stderr);
171 while (count < noutput_items) { // shift bits into bytes of packet one at a time
172 if(slice(inbuf[count++]))
173 d_packet_byte = (d_packet_byte << 1) | 1;
175 d_packet_byte = d_packet_byte << 1;
177 if (d_packet_byte_index++ == 7) { // byte is full so move to next byte
178 d_packet[d_packetlen_cnt++] = d_packet_byte;
179 d_packet_byte_index = 0;
181 if (d_packetlen_cnt == d_packetlen){ // packet is filled
184 gr_message_sptr msg = gr_make_message(0, 0, 0, d_packetlen_cnt);
185 memcpy(msg->msg(), d_packet, d_packetlen_cnt);
187 d_target_queue->insert_tail(msg); // send it
188 msg.reset(); // free it up
204 return noutput_items;