GNU Radio 3.6.1 C++ API
digital_packet_sink.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2005,2012 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 #ifndef INCLUDED_GR_PACKET_SINK_H
24 #define INCLUDED_GR_PACKET_SINK_H
25 
26 #include <digital_api.h>
27 #include <gr_sync_block.h>
28 #include <gr_msg_queue.h>
29 
32 
34 digital_make_packet_sink(const std::vector<unsigned char>& sync_vector,
35  gr_msg_queue_sptr target_queue,
36  int threshold = -1); // -1 -> use default
37 
38 /*!
39  * \brief process received bits looking for packet sync, header, and process bits into packet
40  * \ingroup sink_blk
41  *
42  * input: stream of symbols to be sliced.
43  *
44  * output: none. Pushes assembled packet into target queue
45  *
46  * The packet sink takes in a stream of binary symbols that are sliced
47  * around 0. The bits are then checked for the \p sync_vector to
48  * determine find and decode the packet. It then expects a fixed
49  * length header of 2 16-bit shorts containing the payload length,
50  * followed by the payload. If the 2 16-bit shorts are not identical,
51  * this packet is ignored. Better algs are welcome.
52  *
53  * This block is not very useful anymore as it only works with 2-level
54  * modulations such as BPSK or GMSK. The block can generally be
55  * replaced with a correlate access code and frame sink blocks.
56  *
57  * \param sync_vector The synchronization vector as a vector of 1's and 0's.
58  * \param target_queue The message queue that packets are sent to.
59  * \param threshold Number of bits that can be incorrect in the \p sync_vector.
60  */
62 {
64  digital_make_packet_sink(const std::vector<unsigned char>& sync_vector,
65  gr_msg_queue_sptr target_queue,
66  int threshold);
67 
68  private:
69  enum state_t {STATE_SYNC_SEARCH, STATE_HAVE_SYNC, STATE_HAVE_HEADER};
70 
71  static const int MAX_PKT_LEN = 4096;
72  static const int HEADERBITLEN = 32;
73 
74  gr_msg_queue_sptr d_target_queue; // where to send the packet when received
75  unsigned long long d_sync_vector; // access code to locate start of packet
76  unsigned int d_threshold; // how many bits may be wrong in sync vector
77 
78  state_t d_state;
79 
80  unsigned long long d_shift_reg; // used to look for sync_vector
81 
82  unsigned int d_header; // header bits
83  int d_headerbitlen_cnt; // how many so far
84 
85  unsigned char d_packet[MAX_PKT_LEN]; // assembled payload
86  unsigned char d_packet_byte; // byte being assembled
87  int d_packet_byte_index; // which bit of d_packet_byte we're working on
88  int d_packetlen; // length of packet
89  int d_packetlen_cnt; // how many so far
90 
91  protected:
92  digital_packet_sink(const std::vector<unsigned char>& sync_vector,
93  gr_msg_queue_sptr target_queue,
94  int threshold);
95 
96  void enter_search();
97  void enter_have_sync();
98  void enter_have_header(int payload_len);
99 
100  int slice(float x) { return x > 0 ? 1 : 0; }
101 
102  bool header_ok()
103  {
104  // confirm that two copies of header info are identical
105  return ((d_header >> 16) ^ (d_header & 0xffff)) == 0;
106  }
107 
108  int header_payload_len()
109  {
110  // header consists of two 16-bit shorts in network byte order
111  int t = (d_header >> 16) & 0xffff;
112  return t;
113  }
114 
115  public:
117 
118  int work(int noutput_items,
119  gr_vector_const_void_star &input_items,
120  gr_vector_void_star &output_items);
121 
122 
123  //! return true if we detect carrier
124  bool carrier_sensed() const
125  {
126  return d_state != STATE_SYNC_SEARCH;
127  }
128 
129 };
130 
131 #endif /* INCLUDED_GR_PACKET_SINK_H */