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_framer_sink_1.h>
28 #include <gr_io_signature.h>
36 gr_framer_sink_1::enter_search()
39 fprintf(stderr, "@ enter_search\n");
41 d_state = STATE_SYNC_SEARCH;
45 gr_framer_sink_1::enter_have_sync()
48 fprintf(stderr, "@ enter_have_sync\n");
50 d_state = STATE_HAVE_SYNC;
52 d_headerbitlen_cnt = 0;
56 gr_framer_sink_1::enter_have_header(int payload_len, int whitener_offset)
59 fprintf(stderr, "@ enter_have_header (payload_len = %d) (offset = %d)\n", payload_len, whitener_offset);
61 d_state = STATE_HAVE_HEADER;
62 d_packetlen = payload_len;
63 d_packet_whitener_offset = whitener_offset;
66 d_packet_byte_index = 0;
70 gr_make_framer_sink_1(gr_msg_queue_sptr target_queue)
72 return gr_framer_sink_1_sptr(new gr_framer_sink_1(target_queue));
76 gr_framer_sink_1::gr_framer_sink_1(gr_msg_queue_sptr target_queue)
77 : gr_sync_block ("framer_sink_1",
78 gr_make_io_signature (1, 1, sizeof(unsigned char)),
79 gr_make_io_signature (0, 0, 0)),
80 d_target_queue(target_queue)
85 gr_framer_sink_1::~gr_framer_sink_1 ()
90 gr_framer_sink_1::work (int noutput_items,
91 gr_vector_const_void_star &input_items,
92 gr_vector_void_star &output_items)
94 const unsigned char *in = (const unsigned char *) input_items[0];
98 fprintf(stderr,">>> Entering state machine\n");
100 while (count < noutput_items){
103 case STATE_SYNC_SEARCH: // Look for flag indicating beginning of pkt
105 fprintf(stderr,"SYNC Search, noutput=%d\n", noutput_items);
107 while (count < noutput_items) {
108 if (in[count] & 0x2){ // Found it, set up for header decode
116 case STATE_HAVE_SYNC:
118 fprintf(stderr,"Header Search bitcnt=%d, header=0x%08x\n",
119 d_headerbitlen_cnt, d_header);
121 while (count < noutput_items) { // Shift bits one at a time into header
122 d_header = (d_header << 1) | (in[count++] & 0x1);
123 if (++d_headerbitlen_cnt == HEADERBITLEN) {
126 fprintf(stderr, "got header: 0x%08x\n", d_header);
128 // we have a full header, check to see if it has been received properly
132 header_payload(&payload_len, &whitener_offset);
133 enter_have_header(payload_len, whitener_offset);
135 if (d_packetlen == 0){ // check for zero-length payload
136 // build a zero-length message
137 // NOTE: passing header field as arg1 is not scalable
138 gr_message_sptr msg =
139 gr_make_message(0, d_packet_whitener_offset, 0, 0);
141 d_target_queue->insert_tail(msg); // send it
142 msg.reset(); // free it up
148 enter_search(); // bad header
149 break; // we're in a new state
154 case STATE_HAVE_HEADER:
156 fprintf(stderr,"Packet Build\n");
158 while (count < noutput_items) { // shift bits into bytes of packet one at a time
159 d_packet_byte = (d_packet_byte << 1) | (in[count++] & 0x1);
160 if (d_packet_byte_index++ == 7) { // byte is full so move to next byte
161 d_packet[d_packetlen_cnt++] = d_packet_byte;
162 d_packet_byte_index = 0;
164 if (d_packetlen_cnt == d_packetlen){ // packet is filled
167 // NOTE: passing header field as arg1 is not scalable
168 gr_message_sptr msg =
169 gr_make_message(0, d_packet_whitener_offset, 0, d_packetlen_cnt);
170 memcpy(msg->msg(), d_packet, d_packetlen_cnt);
172 d_target_queue->insert_tail(msg); // send it
173 msg.reset(); // free it up
189 return noutput_items;