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 2, 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>
35 gr_framer_sink_1::enter_search()
38 fprintf(stderr, "@ enter_search\n");
40 d_state = STATE_SYNC_SEARCH;
44 gr_framer_sink_1::enter_have_sync()
47 fprintf(stderr, "@ enter_have_sync\n");
49 d_state = STATE_HAVE_SYNC;
51 d_headerbitlen_cnt = 0;
55 gr_framer_sink_1::enter_have_header(int payload_len, int whitener_offset)
58 fprintf(stderr, "@ enter_have_header (payload_len = %d) (offset = %d)\n", payload_len, whitener_offset);
60 if (payload_len < 4) { // should be at least the length of the CRC field
65 d_state = STATE_HAVE_HEADER;
66 d_packetlen = payload_len;
67 d_packet_whitener_offset = whitener_offset;
70 d_packet_byte_index = 0;
74 gr_make_framer_sink_1(gr_msg_queue_sptr target_queue)
76 return gr_framer_sink_1_sptr(new gr_framer_sink_1(target_queue));
80 gr_framer_sink_1::gr_framer_sink_1(gr_msg_queue_sptr target_queue)
81 : gr_sync_block ("framer_sink_1",
82 gr_make_io_signature (1, 1, sizeof(unsigned char)),
83 gr_make_io_signature (0, 0, 0)),
84 d_target_queue(target_queue)
89 gr_framer_sink_1::~gr_framer_sink_1 ()
94 gr_framer_sink_1::work (int noutput_items,
95 gr_vector_const_void_star &input_items,
96 gr_vector_void_star &output_items)
98 const unsigned char *in = (const unsigned char *) input_items[0];
102 fprintf(stderr,">>> Entering state machine\n");
104 while (count < noutput_items){
107 case STATE_SYNC_SEARCH: // Look for flag indicating beginning of pkt
109 fprintf(stderr,"SYNC Search, noutput=%d\n", noutput_items);
111 while (count < noutput_items) {
112 if (in[count] & 0x2){ // Found it, set up for header decode
120 case STATE_HAVE_SYNC:
122 fprintf(stderr,"Header Search bitcnt=%d, header=0x%08x\n",
123 d_headerbitlen_cnt, d_header);
125 while (count < noutput_items) { // Shift bits one at a time into header
126 d_header = (d_header << 1) | (in[count++] & 0x1);
127 if (++d_headerbitlen_cnt == HEADERBITLEN) {
130 fprintf(stderr, "got header: 0x%08x\n", d_header);
132 // we have a full header, check to see if it has been received properly
136 header_payload(&payload_len, &payload_offset);
137 enter_have_header(payload_len, payload_offset);
140 enter_search(); // no.
141 break; // we're in a new state
146 case STATE_HAVE_HEADER:
148 fprintf(stderr,"Packet Build\n");
150 while (count < noutput_items) { // shift bits into bytes of packet one at a time
151 d_packet_byte = (d_packet_byte << 1) | (in[count++] & 0x1);
152 if (d_packet_byte_index++ == 7) { // byte is full so move to next byte
153 d_packet[d_packetlen_cnt++] = d_packet_byte;
154 d_packet_byte_index = 0;
156 if (d_packetlen_cnt == d_packetlen){ // packet is filled
159 // NOTE: passing header field as arg1 is not scalable
160 gr_message_sptr msg = gr_make_message(0, d_packet_whitener_offset, 0, d_packetlen_cnt);
161 memcpy(msg->msg(), d_packet, d_packetlen_cnt);
163 d_target_queue->insert_tail(msg); // send it
164 msg.reset(); // free it up
180 return noutput_items;