Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_packet_sink.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005 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 2, 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 #ifndef INCLUDED_GR_PACKET_SINK_H
24 #define INCLUDED_GR_PACKET_SINK_H
25
26 #include <gr_sync_block.h>
27 #include <gr_msg_queue.h>
28
29 class gr_packet_sink;
30 typedef boost::shared_ptr<gr_packet_sink> gr_packet_sink_sptr;
31
32 gr_packet_sink_sptr 
33 gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
34                      gr_msg_queue_sptr target_queue,
35                      int threshold = -1                 // -1 -> use default
36                      );
37 /*!
38  * \brief process received  bits looking for packet sync, header, and process bits into packet
39  * \ingroup sink
40  */
41 class gr_packet_sink : public gr_sync_block
42 {
43   friend gr_packet_sink_sptr 
44   gr_make_packet_sink (const std::vector<unsigned char>& sync_vector,
45                        gr_msg_queue_sptr target_queue,
46                        int threshold);
47
48  private:
49   enum state_t {STATE_SYNC_SEARCH, STATE_HAVE_SYNC, STATE_HAVE_HEADER};
50
51   static const int MAX_PKT_LEN    = 4096;
52   static const int HEADERBITLEN   = 32;
53
54   gr_msg_queue_sptr  d_target_queue;            // where to send the packet when received
55   unsigned long long d_sync_vector;             // access code to locate start of packet
56   unsigned int       d_threshold;               // how many bits may be wrong in sync vector
57
58   state_t            d_state;
59
60   unsigned long long d_shift_reg;               // used to look for sync_vector
61
62   unsigned int       d_header;                  // header bits
63   int                d_headerbitlen_cnt;        // how many so far
64
65   unsigned char      d_packet[MAX_PKT_LEN];     // assembled payload
66   unsigned char      d_packet_byte;             // byte being assembled
67   int                d_packet_byte_index;       // which bit of d_packet_byte we're working on
68   int                d_packetlen;               // length of packet
69   int                d_packetlen_cnt;           // how many so far
70
71  protected:
72   gr_packet_sink(const std::vector<unsigned char>& sync_vector, 
73                  gr_msg_queue_sptr target_queue,
74                  int threshold);
75
76   void enter_search();
77   void enter_have_sync();
78   void enter_have_header(int payload_len);
79   
80   int slice(float x) { return x > 0 ? 1 : 0; }
81
82   bool header_ok()
83   {
84     // confirm that two copies of header info are identical
85     return ((d_header >> 16) ^ (d_header & 0xffff)) == 0;
86   }
87
88   int header_payload_len()
89   {
90     // header consists of two 16-bit shorts in network byte order
91     int t = (d_header >> 16) & 0xffff;
92     return t;
93   }
94
95  public:
96   ~gr_packet_sink();
97
98   int work(int noutput_items,
99            gr_vector_const_void_star &input_items,
100            gr_vector_void_star &output_items);
101
102
103   //! return true if we detect carrier
104   bool carrier_sensed() const
105   {
106     return d_state != STATE_SYNC_SEARCH;
107   }
108
109 };
110
111 #endif /* INCLUDED_GR_PACKET_SINK_H */