GNU Radio Manual and C++ API Reference  3.7.13.4
The Free & Open Software Radio Ecosystem
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  int bps);
87  virtual ~header_format_default();
88 
89  /*!
90  * Creates a header from the access code and packet length and
91  * creates an output header as a PMT vector in the form:
92  *
93  * \verbatim
94  | access code | pkt len | pkt len |
95  \endverbatim
96  *
97  * \param nbytes_in The length (in bytes) of the \p input payload
98  * \param input An array of unsigned chars of the packet payload
99  * \param output A pmt::u8vector with the new header prepended
100  * onto the input data.
101  * \param info A pmt::dict containing meta data and info about
102  * the PDU (generally from the metadata portion of the
103  * input PDU). Data can be extracted from this for the
104  * header formatting or inserted.
105  */
106  virtual bool format(int nbytes_in,
107  const unsigned char *input,
108  pmt::pmt_t &output,
109  pmt::pmt_t &info);
110 
111  /*!
112  * Parses a header of the form:
113  *
114  * \verbatim
115  | access code | pkt len | pkt len | payload |
116  \endverbatim
117  *
118  * This is implemented as a state machine that starts off
119  * searching for the access code. Once found, the access code is
120  * used to find the start of the packet and the following
121  * header. This default header encodes the length of the payload
122  * a 16 bit integer twice. The state machine finds the header
123  * and checks that both payload length values are the same. It
124  * then goes into its final state that reads in the payload
125  * (based on the payload length) and produces a payload as a PMT
126  * u8 vector of packed bytes.
127  *
128  * \param nbits_in The number of bits in the input array.
129  * \param input The input as hard decision bits.
130  * \param info A vector of pmt::dicts to hold any meta data or
131  * info about the PDU. When parsing the header, the
132  * formatter can add info from the header into this dict.
133  * Each packet has a single PMT dictionary of info, so
134  * the vector length is the number of packets received
135  * extracted during one call to this parser function.
136  * \param nbits_processed Number of input bits actually
137  * processed; If all goes well, this is nbits_in. A
138  * premature return after a bad header could be less than
139  * this.
140  */
141  virtual bool parse(int nbits_in,
142  const unsigned char *input,
143  std::vector<pmt::pmt_t> &info,
144  int &nbits_processed);
145 
146  /*!
147  * Returns the length of the formatted header in bits.
148  */
149  virtual size_t header_nbits() const;
150 
151  /*!
152  * Updates the access code. Must be a string of 1's and 0's and
153  * <= 64 bits.
154  */
155  bool set_access_code(const std::string &access_code);
156 
157  /*!
158  * Returns the formatted access code as a 64-bit register.
159  */
160  unsigned long long access_code() const;
161 
162  /*!
163  * Sets the threshold for number of access code bits can be in
164  * error before detection. Defaults to 0.
165  */
166  void set_threshold(unsigned int thresh=0);
167 
168  /*!
169  * Returns threshold value for access code detection.
170  */
171  unsigned int threshold() const;
172 
173  /*!
174  * Factory to create an async packet header formatter; returns
175  * an sptr to the object.
176  *
177  * \param access_code An access code that is used to find and
178  * synchronize the start of a packet. Used in the parser and in
179  * other blocks like a corr_est block that helps trigger the
180  * receiver. Can be up to 64-bits long.
181  * \param threshold How many bits can be wrong in the access
182  * code and still count as correct.
183  * \param bps The number of bits/second used in the payload's
184  * modulator.
185  */
186  static sptr make(const std::string &access_code, int threshold,
187  int bps = 1);
188 
189  protected:
190  uint64_t d_access_code; //!< register to hold the access code
191  size_t d_access_code_len; //!< length in bits of the access code
192 
193  uint16_t d_bps; //!< bits/sec of payload modulation
194 
195  unsigned long long d_data_reg; //!< used to look for access_code
196  unsigned long long d_mask; /*!< masks access_code bits (top N bits are set where
197  N is the number of bits in the access code) */
198  unsigned int d_threshold; //!< how many bits may be wrong in sync vector
199 
200  int d_pkt_len; //!< Length of the packet to put into the output buffer
201  int d_pkt_count; //!< Number of bytes bits already received
202 
203  int d_nbits; //!< num bits processed since reset
204 
205  //! Access code found, start getting the header
206  virtual void enter_have_sync();
207 
208  //! Header found, setup for pulling in the hard decision bits
209  virtual void enter_have_header(int payload_len);
210 
211  //! Verify that the header is valid
212  virtual bool header_ok();
213 
214  /*! Get info from the header; return payload length and package
215  * rest of data in d_info dictionary.
216  */
217  virtual int header_payload();
218  };
219 
220  } // namespace digital
221 } // namespace gr
222 
223 #endif /* INCLUDED_DIGITAL_HEADER_FORMAT_DEFAULT_H */
uint64_t d_access_code
register to hold the access code
Definition: header_format_default.h:190
int d_pkt_count
Number of bytes bits already received.
Definition: header_format_default.h:201
size_t d_access_code_len
length in bits of the access code
Definition: header_format_default.h:191
unsigned long long d_mask
Definition: header_format_default.h:196
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:203
uint16_t d_bps
bits/sec of payload modulation
Definition: header_format_default.h:193
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:198
Include this header to use the message passing features.
Definition: logger.h:695
unsigned long long d_data_reg
used to look for access_code
Definition: header_format_default.h:195
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:200
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