GNU Radio 3.7.2.1 C++ API
packet_header_default.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /* Copyright 2012 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_PACKET_HEADER_DEFAULT_H
23 #define INCLUDED_DIGITAL_PACKET_HEADER_DEFAULT_H
24 
25 #include <gnuradio/tags.h>
26 #include <gnuradio/digital/api.h>
27 #include <boost/enable_shared_from_this.hpp>
28 #include <boost/crc.hpp>
29 
30 namespace gr {
31  namespace digital {
32 
33  /*!
34  * \brief Default header formatter for digital packet transmission.
35  * \ingroup packet_operators_blk
36  *
37  * \details
38  * For bursty/packetized digital transmission, packets are usually prepended
39  * with a packet header, containing the number of bytes etc.
40  * This class is not a block, but a tool to create these packet header.
41  *
42  * This is a default packet header (see header_formatter()) for a description
43  * on the header format). To create other header, derive packet header creator
44  * classes from this function.
45  *
46  * gr::digital::packet_headergenerator_bb uses header generators derived from
47  * this class to create packet headers from data streams.
48  */
49  class DIGITAL_API packet_header_default : public boost::enable_shared_from_this<gr::digital::packet_header_default>
50  {
51  public:
53 
55  long header_len,
56  const std::string &len_tag_key="packet_len",
57  const std::string &num_tag_key="packet_num",
58  int bits_per_byte=1);
59  virtual ~packet_header_default();
60 
61  sptr base() { return shared_from_this(); };
62  sptr formatter() { return shared_from_this(); };
63 
64  void set_header_num(unsigned header_num) { d_header_number = header_num; };
65  long header_len() { return d_header_len; };
66  pmt::pmt_t len_tag_key() { return d_len_tag_key; };
67 
68  /*!
69  * \brief Encodes the header information in the given tags into bits and places them into \p out
70  *
71  * Uses the following header format:
72  * Bits 0-11: The packet length (what was stored in the tag with key \p len_tag_key)
73  * Bits 12-23: The header number (counts up everytime this function is called)
74  * Bit 24-31: 8-Bit CRC
75  * All other bits: Are set to zero
76  *
77  * If the header length is smaller than 32, bits are simply left out. For this
78  * reason, they always start with the LSB.
79  *
80  * However, it is recommended to stay above 32 Bits, in order to have a working
81  * CRC.
82  */
83  virtual bool header_formatter(
84  long packet_len,
85  unsigned char *out,
86  const std::vector<tag_t> &tags=std::vector<tag_t>()
87  );
88 
89  /*!
90  * \brief Inverse function to header_formatter().
91  *
92  * Reads the bit stream in \p header and writes a corresponding tag into \p tags.
93  */
94  virtual bool header_parser(
95  const unsigned char *header,
96  std::vector<tag_t> &tags);
97 
98  static sptr make(
99  long header_len,
100  const std::string &len_tag_key="packet_len",
101  const std::string &num_tag_key="packet_num",
102  int bits_per_byte=1);
103 
104  protected:
109  unsigned d_header_number;
110  unsigned d_mask;
111  boost::crc_optimal<8, 0x07, 0xFF, 0x00, false, false> d_crc_impl;
112  };
113 
114  } // namespace digital
115 } // namespace gr
116 
117 #endif /* INCLUDED_DIGITAL_PACKET_HEADER_DEFAULT_H */
118