GNU Radio Manual and C++ API Reference  3.7.4
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
long header_len()
Definition: packet_header_default.h:65
boost::crc_optimal< 8, 0x07, 0xFF, 0x00, false, false > d_crc_impl
Definition: packet_header_default.h:111
sptr base()
Definition: packet_header_default.h:61
sptr formatter()
Definition: packet_header_default.h:62
#define DIGITAL_API
Definition: gr-digital/include/gnuradio/digital/api.h:30
void set_header_num(unsigned header_num)
Definition: packet_header_default.h:64
unsigned d_mask
Definition: packet_header_default.h:110
shared_ptr documentation stub
Definition: shared_ptr_docstub.h:15
pmt::pmt_t d_len_tag_key
Definition: packet_header_default.h:106
boost::shared_ptr< packet_header_default > sptr
Definition: packet_header_default.h:52
pmt::pmt_t d_num_tag_key
Definition: packet_header_default.h:107
int d_bits_per_byte
Definition: packet_header_default.h:108
unsigned d_header_number
Definition: packet_header_default.h:109
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
long d_header_len
Definition: packet_header_default.h:105
Default header formatter for digital packet transmission.
Definition: packet_header_default.h:49
pmt::pmt_t len_tag_key()
Definition: packet_header_default.h:66