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>
40 static const int DEFAULT_THRESHOLD = 12; // detect access code with up to DEFAULT_THRESHOLD bits wrong
43 gr_packet_sink::enter_search()
46 fprintf(stderr, "@ enter_search\n");
48 d_state = STATE_SYNC_SEARCH;
53 gr_packet_sink::enter_have_sync()
56 fprintf(stderr, "@ enter_have_sync\n");
58 d_state = STATE_HAVE_SYNC;
60 d_headerbitlen_cnt = 0;
64 gr_packet_sink::enter_have_header(int payload_len)
67 fprintf(stderr, "@ enter_have_header (payload_len = %d)\n", payload_len);
69 d_state = STATE_HAVE_HEADER;
70 d_packetlen = payload_len;
73 d_packet_byte_index = 0;
77 gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
78 gr_msg_queue_sptr target_queue, int threshold)
80 return gr_packet_sink_sptr (new gr_packet_sink (sync_vector, target_queue, threshold));
84 gr_packet_sink::gr_packet_sink (const std::vector<unsigned char>& sync_vector,
85 gr_msg_queue_sptr target_queue, int threshold)
86 : gr_sync_block ("packet_sink",
87 gr_make_io_signature (1, 1, sizeof(float)),
88 gr_make_io_signature (0, 0, 0)),
89 d_target_queue(target_queue), d_threshold(threshold == -1 ? DEFAULT_THRESHOLD : threshold)
94 d_sync_vector |= sync_vector[i];
100 gr_packet_sink::~gr_packet_sink ()
105 gr_packet_sink::work (int noutput_items,
106 gr_vector_const_void_star &input_items,
107 gr_vector_void_star &output_items)
109 float *inbuf = (float *) input_items[0];
113 fprintf(stderr,">>> Entering state machine\n"),fflush(stderr);
115 while (count<noutput_items) {
118 case STATE_SYNC_SEARCH: // Look for sync vector
120 fprintf(stderr,"SYNC Search, noutput=%d\n",noutput_items),fflush(stderr);
122 while (count < noutput_items) {
123 if(slice(inbuf[count++]))
124 d_shift_reg = (d_shift_reg << 1) | 1;
126 d_shift_reg = d_shift_reg << 1;
128 // Compute popcnt of putative sync vector
129 if(gr_count_bits64 (d_shift_reg ^ d_sync_vector) <= d_threshold) {
130 // Found it, set up for header decode
137 case STATE_HAVE_SYNC:
139 fprintf(stderr,"Header Search bitcnt=%d, header=0x%08x\n", d_headerbitlen_cnt, d_header),
142 while (count < noutput_items) { // Shift bits one at a time into header
143 if(slice(inbuf[count++]))
144 d_header = (d_header << 1) | 1;
146 d_header = d_header << 1;
148 if (++d_headerbitlen_cnt == HEADERBITLEN) {
151 fprintf(stderr, "got header: 0x%08x\n", d_header);
153 // we have a full header, check to see if it has been received properly
155 int payload_len = header_payload_len();
156 if (payload_len <= MAX_PKT_LEN) // reasonable?
157 enter_have_header(payload_len); // yes.
159 enter_search(); // no.
162 enter_search(); // no.
163 break; // we're in a new state
168 case STATE_HAVE_HEADER:
170 fprintf(stderr,"Packet Build\n"),fflush(stderr);
172 while (count < noutput_items) { // shift bits into bytes of packet one at a time
173 if(slice(inbuf[count++]))
174 d_packet_byte = (d_packet_byte << 1) | 1;
176 d_packet_byte = d_packet_byte << 1;
178 if (d_packet_byte_index++ == 7) { // byte is full so move to next byte
179 d_packet[d_packetlen_cnt++] = d_packet_byte;
180 d_packet_byte_index = 0;
182 if (d_packetlen_cnt == d_packetlen){ // packet is filled
185 gr_message_sptr msg = gr_make_message(0, 0, 0, d_packetlen_cnt);
186 memcpy(msg->msg(), d_packet, d_packetlen_cnt);
188 d_target_queue->insert_tail(msg); // send it
189 msg.reset(); // free it up
205 return noutput_items;