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