Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_framer_sink_1.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004,2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
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)
10  * any later version.
11  * 
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.
16  * 
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.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include <gr_framer_sink_1.h>
28 #include <gr_io_signature.h>
29 #include <cstdio>
30 #include <stdexcept>
31
32 #define VERBOSE 0
33
34 inline void
35 gr_framer_sink_1::enter_search()
36 {
37   if (VERBOSE)
38     fprintf(stderr, "@ enter_search\n");
39
40   d_state = STATE_SYNC_SEARCH;
41 }
42     
43 inline void
44 gr_framer_sink_1::enter_have_sync()
45 {
46   if (VERBOSE)
47     fprintf(stderr, "@ enter_have_sync\n");
48
49   d_state = STATE_HAVE_SYNC;
50   d_header = 0;
51   d_headerbitlen_cnt = 0;
52 }
53
54 inline void
55 gr_framer_sink_1::enter_have_header(int payload_len, int whitener_offset)
56 {
57   if (VERBOSE)
58     fprintf(stderr, "@ enter_have_header (payload_len = %d) (offset = %d)\n", payload_len, whitener_offset);
59
60   d_state = STATE_HAVE_HEADER;
61   d_packetlen = payload_len;
62   d_packet_whitener_offset = whitener_offset;
63   d_packetlen_cnt = 0;
64   d_packet_byte = 0;
65   d_packet_byte_index = 0;
66 }
67
68 gr_framer_sink_1_sptr
69 gr_make_framer_sink_1(gr_msg_queue_sptr target_queue)
70 {
71   return gr_framer_sink_1_sptr(new gr_framer_sink_1(target_queue));
72 }
73
74
75 gr_framer_sink_1::gr_framer_sink_1(gr_msg_queue_sptr target_queue)
76   : gr_sync_block ("framer_sink_1",
77                    gr_make_io_signature (1, 1, sizeof(unsigned char)),
78                    gr_make_io_signature (0, 0, 0)),
79     d_target_queue(target_queue)
80 {
81   enter_search();
82 }
83
84 gr_framer_sink_1::~gr_framer_sink_1 ()
85 {
86 }
87
88 int
89 gr_framer_sink_1::work (int noutput_items,
90                         gr_vector_const_void_star &input_items,
91                         gr_vector_void_star &output_items)
92 {
93   const unsigned char *in = (const unsigned char *) input_items[0];
94   int count=0;
95   
96   if (VERBOSE)
97     fprintf(stderr,">>> Entering state machine\n");
98
99   while (count < noutput_items){
100     switch(d_state) {
101       
102     case STATE_SYNC_SEARCH:    // Look for flag indicating beginning of pkt
103       if (VERBOSE)
104         fprintf(stderr,"SYNC Search, noutput=%d\n", noutput_items);
105
106       while (count < noutput_items) {
107         if (in[count] & 0x2){  // Found it, set up for header decode
108           enter_have_sync();
109           break;
110         }
111         count++;
112       }
113       break;
114
115     case STATE_HAVE_SYNC:
116       if (VERBOSE)
117         fprintf(stderr,"Header Search bitcnt=%d, header=0x%08x\n",
118                 d_headerbitlen_cnt, d_header);
119
120       while (count < noutput_items) {   // Shift bits one at a time into header
121         d_header = (d_header << 1) | (in[count++] & 0x1);
122         if (++d_headerbitlen_cnt == HEADERBITLEN) {
123
124           if (VERBOSE)
125             fprintf(stderr, "got header: 0x%08x\n", d_header);
126
127           // we have a full header, check to see if it has been received properly
128           if (header_ok()){
129             int payload_len;
130             int whitener_offset;
131             header_payload(&payload_len, &whitener_offset);
132             enter_have_header(payload_len, whitener_offset);
133
134             if (d_packetlen == 0){          // check for zero-length payload
135               // build a zero-length message
136               // NOTE: passing header field as arg1 is not scalable
137               gr_message_sptr msg =
138                 gr_make_message(0, d_packet_whitener_offset, 0, 0);
139               
140               d_target_queue->insert_tail(msg);         // send it
141               msg.reset();                              // free it up
142
143               enter_search();                           
144             }
145           }
146           else
147             enter_search();                             // bad header
148           break;                                        // we're in a new state
149         }
150       }
151       break;
152       
153     case STATE_HAVE_HEADER:
154       if (VERBOSE)
155         fprintf(stderr,"Packet Build\n");
156
157       while (count < noutput_items) {   // shift bits into bytes of packet one at a time
158         d_packet_byte = (d_packet_byte << 1) | (in[count++] & 0x1);
159         if (d_packet_byte_index++ == 7) {               // byte is full so move to next byte
160           d_packet[d_packetlen_cnt++] = d_packet_byte;
161           d_packet_byte_index = 0;
162
163           if (d_packetlen_cnt == d_packetlen){          // packet is filled
164
165             // build a message
166             // NOTE: passing header field as arg1 is not scalable
167             gr_message_sptr msg =
168               gr_make_message(0, d_packet_whitener_offset, 0, d_packetlen_cnt);
169             memcpy(msg->msg(), d_packet, d_packetlen_cnt);
170
171             d_target_queue->insert_tail(msg);           // send it
172             msg.reset();                                // free it up
173
174             enter_search();
175             break;
176           }
177         }
178       }
179       break;
180
181     default:
182       assert(0);
183
184     } // switch
185
186   }   // while
187
188   return noutput_items;
189 }