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