GNU Radio Manual and C++ API Reference  3.7.13.4
The Free & Open Software Radio Ecosystem
header_buffer.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_BUFFER_H
23 #define INCLUDED_DIGITAL_HEADER_BUFFER_H
24 
25 #include <gnuradio/digital/api.h>
26 #include <vector>
27 #include <stdint.h>
28 
29 namespace gr {
30  namespace digital {
31 
32  /*!
33  * \brief Helper class for handling payload headers.
34  * \ingroup packet_operators_blk
35  *
36  * \details
37  *
38  * This class is used by the header format blocks (e.g.,
39  * digital::header_format_default) to make it easier to deal with
40  * payload headers. This class functions in two different ways
41  * depending on if it is used in a transmitter or receiver. When
42  * used in a transmitter, this class helps us build headers out of
43  * the fields of the protocol. When used in a receiver, this class
44  * helps us parse the received bits into the protocol's fields.
45  *
46  * This page describes how to work with the different modes,
47  * transmit or receive. The class is instructed as to which mode
48  * it is in by how the constructor is called. If the constructor
49  * is passed a valid array (non NULL), then it is in transmit mode
50  * and will pack this buffer with the header fields. If that
51  * buffer is NULL, the object is in receive mode.
52  *
53  * \section header_buffer_tx Transmit Mode
54  *
55  * When passed a valid buffer in the constructor, this object is in
56  * transmit mode. We can then use the add_field[N] functions to
57  * add new fields to this header. The buffer MUST be large enough
58  * to hold the full header. As this class is meant to work mostly
59  * with the digital::header_format_default and child
60  * classes, the header length can be read from
61  * digital::header_format_default::header_nbytes().
62  *
63  * Each field is a specific length of 8, 16, 32, or 64 bits that
64  * are to be transmitted in network byte order. We can adjust the
65  * direction of the bytes by setting the byte-swap flag, \p bs, to
66  * true or false.
67  *
68  * The length argument (\p len) for all add_field[N] calls is the
69  * number of bytes actually accounted for in the data
70  * structure. Often, we would use the full size of the field,
71  * which is sizeof(dtype), and the add_field[N] call defaults to
72  * len=N. Occasionally, we may need to use fewer bytes than
73  * actually represented by the data type. An example would be the
74  * access code used in the header_format_default, which is a
75  * uint64_t type but may have fewer bytes used in the actual
76  * access code.
77  *
78  * The function that calls this class is expected to handle the
79  * memory handling of the buffer -- both allocating and
80  * deallocating.
81  *
82  * As simple example of using this class in transmit mode:
83  *
84  * \verbatim
85  uint8_t* buffer = (uint8_t*)volk_malloc(header_nbytes(),
86  volk_get_alignment());
87 
88  header_buffer hdr(buffer);
89  hdr.add_field64(sync_word, sync_word_len);
90  hdr.add_field16(payload_length);
91  hdr.add_field8(header_flags);
92  hdr.add_field8(header_options);
93 
94  // Do something with the header
95 
96  volk_free(buffer);
97  \endverbatim
98  *
99  * In this example, the header contains four fields:
100  *
101  * \verbatim
102  |0 15|16 23|24 31|
103  | sync word |
104  | |
105  | length | flags | options |
106  \endverbatim
107  *
108  * The sync word can be up to 64-bits, but the add_field64 is also
109  * passed the number of actual bytes in the sync word and so could
110  * be fewer.
111  *
112  * \section header_buffer_rx Receive Mode
113  *
114  * In receive mode, we build up the header as bits are received by
115  * inserting them with insert_bit. We can find out how long the
116  * current header is, in bits, using the call to length(). If the
117  * header is of the appropriate length, we can then start
118  * extracting the fields from it. When we are done with the
119  * current header, call clear() to reset the internal buffer to
120  * empty, which will mean that length() returns 0.
121  *
122  * The header fields are extracted using the extract_field[N]
123  * functions. Like the add_field[N] functions, we specify the size
124  * (in bits) of the field we are extracting. We pass this function
125  * the bit-position of the expected field in the received header
126  * buffer. The extract_field[N] assumes that the number of bits
127  * for the field is N, but we can tell the function to use fewer
128  * bits if we want. Setting the length parameter of these
129  * functions greater than N is illegal, and it will throw an
130  * error.
131  *
132  * For example, given a header of | length | seq. num. | where the
133  * length is 16 bits and the sequence number is 32 bits, we would
134  * use:
135  *
136  * \verbatim
137  uint16_t len = d_hdr_reg.extract_field16(0);
138  uint32_t seq = d_hdr_reg.extract_field32(16);
139  \endverbatim
140  *
141  * The extract_field functions are specific to data types of the
142  * field and the number of bits for each field is inferred by the
143  * data type. So extract_field16 assumes we want all 16 bits in
144  * the field represented.
145  *
146  * Some headers have fields that are not standard sizes of
147  * integers, like a 1 bit, 4 bit, or even 12 bit fields. We can
148  * ask for fewer bits for each field. say:
149  *
150  * \verbatim
151  |0 15|16 19|20 31|
152  | len | flags | options |
153  \endverbatim
154  *
155  * We would use the following extraction functions:
156  *
157  * \verbatim
158  uint16_t len = d_hdr_reg.extract_field16(0);
159  uint8_t flags = d_hdr_reg.extract_field8(16, 4);
160  uint16_t opts = d_hdr_reg.extract_field16(20, 12);
161  \endverbatim
162  *
163  * \sa header_format_default
164  * \sa header_format_counter
165  * \sa header_format_crc
166  */
168  {
169  private:
170  size_t d_offset;
171  uint8_t *d_buffer;
172 
173  std::vector<bool> d_input;
174 
175  public:
176  /*!
177  * Create a header buffer object with a pre-allocated buffer, \p
178  * buffer, to hold the formatted header data.
179  *
180  * If \p buffer is set to NULL, then this object is in receive
181  * mode meant to receive bits from an incoming data stream and
182  * provide the ability to extract fields. In this mode, calls to
183  * add_field are invalid and will be nops.
184  */
185  header_buffer(uint8_t *buffer=NULL);
186 
187  /*!
188  * Class destructor.
189  */
190  ~header_buffer();
191 
192  /*!
193  * Clears the header.
194  *
195  * In transmit mode, this resets the current offset so new
196  * add_field functions start adding data to the start of the
197  * buffer.
198  *
199  * In receive mode, this clears the buffer that we have inserted
200  * bits in to.
201  */
202  void clear();
203 
204 
205  /*!
206  * In transmit mode, this returns the length of the data in
207  * the buffer (not the allocated buffer length).
208  *
209  * In receiving mode, this returns the current length in bits of
210  * the received header.
211  */
212  size_t length() const;
213 
214  /*!
215  * Returns a constant pointer to the buffer.
216  */
217  const uint8_t* header() const;
218 
219  /*!
220  * Add an 8-bit field to the header.
221  *
222  * \param data The 8-bit data item.
223  * \param len Length (in bits) of \p data.
224  * \param bs Set to 'true' to byte swap the data.
225  */
226  void add_field8(uint8_t data, int len=8, bool bs=false);
227 
228  /*!
229  * Add an 16-bit field to the header.
230  *
231  * \param data The 16-bit data item.
232  * \param len Length (in bits) of \p data.
233  * \param bs Set to 'true' to byte swap the data.
234  */
235  void add_field16(uint16_t data, int len=16, bool bs=false);
236 
237  /*!
238  * Add an 32-bit field to the header.
239  *
240  * \param data The 32-bit data item.
241  * \param len Length (in bits) of \p data.
242  * \param bs Set to 'true' to byte swap the data.
243  */
244  void add_field32(uint32_t data, int len=32, bool bs=false);
245 
246  /*!
247  * Add an 64-bit field to the header.
248  *
249  * \param data The 64-bit data item.
250  * \param len Length (in bits) of \p data.
251  * \param bs Set to 'true' to byte swap the data.
252  */
253  void add_field64(uint64_t data, int len=64, bool bs=false);
254 
255 
256 
257  /*****************************************************
258  * Receive mode to build a header from bits *
259  *****************************************************/
260 
261  /*!
262  * Insert a new bit on the back of the input buffer. This
263  * function is used in receive mode to add new bits as they are
264  * received for later use of the extract_field functions.
265  *
266  * \param bit New bit to add.
267  */
268  void insert_bit(int bit);
269 
270  /*!
271  * Returns up to an 8-bit field in the packet header.
272  *
273  * \param pos Bit position of the start of the field.
274  * \param len The number of bits in the field.
275  * \param bs Set to 'true' to byte swap the data.
276  */
277  uint8_t extract_field8(int pos, int len=8, bool bs=false);
278 
279  /*!
280  * Returns up to a 16-bit field in the packet header.
281  *
282  * \param pos Bit position of the start of the field.
283  * \param len The number of bits in the field.
284  * \param bs Set to 'true' to byte swap the data.
285  */
286  uint16_t extract_field16(int pos, int len=16, bool bs=false);
287 
288  /*!
289  * Returns up to a 32-bit field in the packet header.
290  *
291  * \param pos Bit position of the start of the field.
292  * \param len The number of bits in the field.
293  * \param bs Set to 'true' to byte swap the data.
294  */
295  uint32_t extract_field32(int pos, int len=32, bool bs=false);
296 
297  /*!
298  * Returns up to a 64-bit field in the packet header.
299  *
300  * \param pos Bit position of the start of the field.
301  * \param len The number of bits in the field.
302  * \param bs Set to 'true' to byte swap the data.
303  */
304  uint64_t extract_field64(int pos, int len=64, bool bs=false);
305  };
306 
307  } // namespace digital
308 } // namespace gr
309 
310 #endif /* INCLUDED_DIGITAL_HEADER_BUFFER_H */
#define DIGITAL_API
Definition: gr-digital/include/gnuradio/digital/api.h:30
Single writer, multiple reader fifo.
Definition: buffer.h:55
Include this header to use the message passing features.
Definition: logger.h:695
Helper class for handling payload headers.
Definition: header_buffer.h:167
PMT_API size_t length(const pmt_t &v)
Return the number of elements in v.