GNU Radio 3.6.5 C++ API

packet_header_default.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /* Copyright 2012 Free Software Foundation, Inc.
00003  * 
00004  * This file is part of GNU Radio
00005  * 
00006  * GNU Radio is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3, or (at your option)
00009  * any later version.
00010  * 
00011  * GNU Radio is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with GNU Radio; see the file COPYING.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #ifndef INCLUDED_DIGITAL_PACKET_HEADER_DEFAULT_H
00023 #define INCLUDED_DIGITAL_PACKET_HEADER_DEFAULT_H
00024 
00025 #include <gr_tags.h>
00026 #include <digital/api.h>
00027 #include <boost/enable_shared_from_this.hpp>
00028 
00029 namespace gr {
00030   namespace digital {
00031 
00032     /*!
00033      * \brief Default header formatter for digital packet transmission.
00034      * \ingroup packet_operators_blk
00035      *
00036      * \details
00037      * For bursty/packetized digital transmission, packets are usually prepended
00038      * with a packet header, containing the number of bytes etc.
00039      * This class is not a block, but a tool to create these packet header.
00040      *
00041      * This is a default packet header (see header_formatter()) for a description
00042      * on the header format). To create other header, derive packet header creator
00043      * classes from this function.
00044      *
00045      * gr::digital::packet_headergenerator_bb uses header generators derived from
00046      * this class to create packet headers from data streams.
00047      */
00048     class DIGITAL_API packet_header_default : public boost::enable_shared_from_this<gr::digital::packet_header_default>
00049     {
00050      public:
00051       typedef boost::shared_ptr<packet_header_default> sptr;
00052 
00053       packet_header_default(
00054                       long header_len,
00055                       const std::string &len_tag_key="packet_len",
00056                       const std::string &num_tag_key="packet_num",
00057                       int bits_per_byte=1);
00058       ~packet_header_default();
00059 
00060       sptr base() { return shared_from_this(); };
00061       sptr formatter() { return shared_from_this(); };
00062 
00063       void set_header_num(unsigned header_num) { d_header_number = header_num; };
00064       long header_len() { return d_header_len; };
00065       pmt::pmt_t len_tag_key() { return d_len_tag_key; };
00066 
00067       /*!
00068        * \brief Encodes the header information in the given tags into bits and places them into \p out
00069        *
00070        * Uses the following header format:
00071        * Bits 0-11: The packet length (what was stored in the tag with key \p len_tag_key)
00072        * Bits 12-27: The header number (counts up everytime this function is called)
00073        * Bit 28: Even parity bit
00074        * All other bits: Are set to zero
00075        *
00076        * If the header length is smaller than 29, bits are simply left out. For this
00077        * reason, they always start with the LSB.
00078        */
00079       virtual bool header_formatter(
00080           long packet_len,
00081           unsigned char *out,
00082           const std::vector<gr_tag_t> &tags=std::vector<gr_tag_t>()
00083       );
00084 
00085       /*!
00086        * \brief Inverse function to header_formatter().
00087        *
00088        * Reads the bit stream in \p header and writes a corresponding tag into \p tags.
00089        */
00090       virtual bool header_parser(
00091         const unsigned char *header,
00092         std::vector<gr_tag_t> &tags);
00093 
00094       static sptr make(
00095               long header_len,
00096               const std::string &len_tag_key="packet_len",
00097               const std::string &num_tag_key="packet_num",
00098               int bits_per_byte=1);
00099 
00100     protected:
00101       long d_header_len;
00102       pmt::pmt_t d_len_tag_key;
00103       pmt::pmt_t d_num_tag_key;
00104       int d_bits_per_byte;
00105       unsigned d_header_number;
00106       unsigned d_mask;
00107     };
00108 
00109   } // namespace digital
00110 } // namespace gr
00111 
00112 #endif /* INCLUDED_DIGITAL_PACKET_HEADER_DEFAULT_H */
00113