GNU Radio Manual and C++ API Reference  3.7.10
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
header_format_default.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /* Copyright 2015-2016 Free Software Foundation, Inc.
3  *
4  * This file is part of GNU Radio
5  *
6  * GNU Radio is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU Radio is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GNU Radio; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #ifndef INCLUDED_DIGITAL_HEADER_FORMAT_DEFAULT_H
23 #define INCLUDED_DIGITAL_HEADER_FORMAT_DEFAULT_H
24 
25 #include <pmt/pmt.h>
26 #include <gnuradio/digital/api.h>
29 #include <gnuradio/logger.h>
30 #include <boost/enable_shared_from_this.hpp>
31 
32 namespace gr {
33  namespace digital {
34 
35  /*!
36  * \brief Default header formatter for PDU formatting.
37  * \ingroup packet_operators_blk
38  *
39  * \details
40  * Used to handle the default packet header.
41  *
42  * See the parent class header_format_base for details of how
43  * these classes operate.
44  *
45  * The default header created in this base class consists of an
46  * access code and the packet length. The length is encoded as a
47  * 16-bit value repeated twice:
48  *
49  * \verbatim
50  | access code | hdr | payload |
51  \endverbatim
52  *
53  * Where the access code is <= 64 bits and hdr is:
54  *
55  * \verbatim
56  | 0 -- 15 | 16 -- 31 |
57  | pkt len | pkt len |
58  \endverbatim
59  *
60  * The access code and header are formatted for network byte order.
61  *
62  * This header generator does not calculate or append a CRC to the
63  * packet. Use the CRC32 Async block for that before adding the
64  * header. The header's length will then measure the payload plus
65  * the CRC length (4 bytes for a CRC32).
66  *
67  * The default header parser produces a PMT dictionary that
68  * contains the following keys. All formatter blocks MUST produce
69  * these two values in any dictionary.
70  *
71  * \li "payload symbols": the number of symbols in the
72  * payload. The payload decoder will have to know how this relates
73  * to the number of bits received. This block knows nothing about
74  * the payload modulation or the number of bits/symbol. Use the
75  * gr::digital::header_format_counter for that purpose.
76  *
77  * \sa header_format_counter
78  * \sa header_format_crc
79  * \sa header_format_ofdm
80  */
82  : public header_format_base
83  {
84  public:
85  header_format_default(const std::string &access_code, int threshold);
86  virtual ~header_format_default();
87 
88  /*!
89  * Creates a header from the access code and packet length and
90  * creates an output header as a PMT vector in the form:
91  *
92  * \verbatim
93  | access code | pkt len | pkt len |
94  \endverbatim
95  *
96  * \param nbytes_in The length (in bytes) of the \p input payload
97  * \param input An array of unsigned chars of the packet payload
98  * \param output A pmt::u8vector with the new header prepended
99  * onto the input data.
100  * \param info A pmt::dict containing meta data and info about
101  * the PDU (generally from the metadata portion of the
102  * input PDU). Data can be extracted from this for the
103  * header formatting or inserted.
104  */
105  virtual bool format(int nbytes_in,
106  const unsigned char *input,
107  pmt::pmt_t &output,
108  pmt::pmt_t &info);
109 
110  /*!
111  * Parses a header of the form:
112  *
113  * \verbatim
114  | access code | pkt len | pkt len | payload |
115  \endverbatim
116  *
117  * This is implemented as a state machine that starts off
118  * searching for the access code. Once found, the access code is
119  * used to find the start of the packet and the following
120  * header. This default header encodes the length of the payload
121  * a 16 bit integer twice. The state machine finds the header
122  * and checks that both payload length values are the same. It
123  * then goes into its final state that reads in the payload
124  * (based on the payload length) and produces a payload as a PMT
125  * u8 vector of packed bytes.
126  *
127  * \param nbits_in The number of bits in the input array.
128  * \param input The input as hard decision bits.
129  * \param info A vector of pmt::dicts to hold any meta data or
130  * info about the PDU. When parsing the header, the
131  * formatter can add info from the header into this dict.
132  * Each packet has a single PMT dictionary of info, so
133  * the vector length is the number of packets received
134  * extracted during one call to this parser function.
135  * \param nbits_processed Number of input bits actually
136  * processed; If all goes well, this is nbits_in. A
137  * premature return after a bad header could be less than
138  * this.
139  */
140  virtual bool parse(int nbits_in,
141  const unsigned char *input,
142  std::vector<pmt::pmt_t> &info,
143  int &nbits_processed);
144 
145  /*!
146  * Returns the length of the formatted header in bits.
147  */
148  virtual size_t header_nbits() const;
149 
150  /*!
151  * Updates the access code. Must be a string of 1's and 0's and
152  * <= 64 bits.
153  */
154  bool set_access_code(const std::string &access_code);
155 
156  /*!
157  * Returns the formatted access code as a 64-bit register.
158  */
159  unsigned long long access_code() const;
160 
161  /*!
162  * Sets the threshold for number of access code bits can be in
163  * error before detection. Defaults to 0.
164  */
165  void set_threshold(unsigned int thresh=0);
166 
167  /*!
168  * Returns threshold value for access code detection.
169  */
170  unsigned int threshold() const;
171 
172  /*!
173  * Factory to create an async packet header formatter; returns
174  * an sptr to the object.
175  *
176  * \param access_code An access code that is used to find and
177  * synchronize the start of a packet. Used in the parser and in
178  * other blocks like a corr_est block that helps trigger the
179  * receiver. Can be up to 64-bits long.
180  * \param threshold How many bits can be wrong in the access
181  * code and still count as correct.
182  */
183  static sptr make(const std::string &access_code, int threshold);
184 
185  protected:
186  uint64_t d_access_code; //!< register to hold the access code
187  size_t d_access_code_len; //!< length in bits of the access code
188 
189  unsigned long long d_data_reg; //!< used to look for access_code
190  unsigned long long d_mask; /*!< masks access_code bits (top N bits are set where
191  N is the number of bits in the access code) */
192  unsigned int d_threshold; //!< how many bits may be wrong in sync vector
193 
194  int d_pkt_len; //!< Length of the packet to put into the output buffer
195  int d_pkt_count; //!< Number of bytes bits already received
196 
197  int d_nbits; //!< num bits processed since reset
198 
199  //! Access code found, start getting the header
200  virtual void enter_have_sync();
201 
202  //! Header found, setup for pulling in the hard decision bits
203  virtual void enter_have_header(int payload_len);
204 
205  //! Verify that the header is valid
206  virtual bool header_ok();
207 
208  /*! Get info from the header; return payload length and package
209  * rest of data in d_info dictionary.
210  */
211  virtual int header_payload();
212  };
213 
214  } // namespace digital
215 } // namespace gr
216 
217 #endif /* INCLUDED_DIGITAL_HEADER_FORMAT_DEFAULT_H */
uint64_t d_access_code
register to hold the access code
Definition: header_format_default.h:186
int d_pkt_count
Number of bytes bits already received.
Definition: header_format_default.h:195
size_t d_access_code_len
length in bits of the access code
Definition: header_format_default.h:187
unsigned long long d_mask
Definition: header_format_default.h:190
Base header formatter class.
Definition: header_format_base.h:123
#define DIGITAL_API
Definition: gr-digital/include/gnuradio/digital/api.h:30
int d_nbits
num bits processed since reset
Definition: header_format_default.h:197
Default header formatter for PDU formatting.
Definition: header_format_default.h:81
unsigned int d_threshold
how many bits may be wrong in sync vector
Definition: header_format_default.h:192
Include this header to use the message passing features.
Definition: logger.h:131
unsigned long long d_data_reg
used to look for access_code
Definition: header_format_default.h:189
boost::shared_ptr< header_format_base > sptr
Definition: header_format_base.h:127
int d_pkt_len
Length of the packet to put into the output buffer.
Definition: header_format_default.h:194
boost::intrusive_ptr< pmt_base > pmt_t
typedef for shared pointer (transparent reference counting). See http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Definition: pmt.h:56