NEW FILE HEADER / COPYRIGHT FORMAT
[senf.git] / Packets / MPEGDVBBundle / TLVPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS) 
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY 
6 //     Thorsten Horstmann <tho@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief TLVPacket public header */
25
26 #ifndef HH_TLVPacket_
27 #define HH_TLVPacket_ 1
28
29 // Custom includes
30 #include <algorithm>
31 #include "../../Packets/Packets.hh"
32
33 //#include "TLVPacket.mpp"
34 ///////////////////////////////hh.p////////////////////////////////////////
35
36 namespace senf {
37
38     /** \brief xxx
39         
40         \todo document me
41         \todo add usefull exceptions strings
42          
43     */
44     struct UnsuportedTLVPacketException : public std::exception
45     { 
46         virtual char const * what() const throw() { 
47             return "length of length can be max. 4 bytes. Sorry.";
48         }
49     };
50
51     /** \brief xxx
52         \todo document me
53     */
54     class TLVPacketLengthParser 
55         : public detail::packet::IntParserOps<TLVPacketLengthParser, boost::uint32_t>,
56           public PacketParserBase
57     {
58     public:
59         TLVPacketLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
60
61         typedef boost::uint32_t value_type;
62         static const size_type init_bytes = 1;
63         static value_type const min_value = 0;
64         static value_type const max_value = 4294967295u;
65
66         value_type value() const;
67         void value(value_type const & v);
68         
69         TLVPacketLengthParser const & operator= (value_type other);
70     
71         size_type bytes() const;
72             
73         void init() const;
74
75     private:
76         typedef FlagParser      <    0 > ExtendedLengthFlagParser;
77         typedef UIntFieldParser < 1, 8 > FixedLengthParser;
78
79         ExtendedLengthFlagParser extended_length_flag() const {
80             return parse<ExtendedLengthFlagParser>( 0 );
81         }
82
83         FixedLengthParser fixed_length_field() const {
84             return parse<FixedLengthParser>( 0 );
85         }
86         
87         void resize(size_type size);
88     };  
89         
90     /** \brief parse TLVPacket Packet
91     
92         \todo document me
93      
94         \see TLVPacketType
95      */
96     template <class LengthParser>
97     struct TLVPacketParser : public PacketParserBase
98     {
99 #       include SENF_PARSER()
100         
101         SENF_PARSER_FIELD( type,   UInt32Parser );
102         SENF_PARSER_FIELD( length, LengthParser );
103         
104         SENF_PARSER_FINALIZE(TLVPacketParser);
105     };
106     
107     /** \brief generic TLV Packet type
108         
109         \todo document me
110         
111         \ingroup protocolbundle_mpegdvb
112      */
113     template <class LengthParser>
114     struct TLVPacketType
115         : public PacketTypeBase
116     {
117 #ifndef DOXYGEN
118         typedef ConcretePacket<TLVPacketType<LengthParser> > packet;
119         typedef TLVPacketParser<LengthParser> parser;
120 #endif
121         static optional_range nextPacketRange(packet p);
122         static size_type initSize();
123         
124         static void finalize(packet p);
125         
126         static void dump(packet p, std::ostream & os);
127     };
128
129     typedef TLVPacketType<TLVPacketLengthParser>::packet TLVPacket;
130     
131     typedef ConcretePacket<TLVPacketType<UInt8Parser> >  TLVFix8Packet;
132     typedef ConcretePacket<TLVPacketType<UInt16Parser> > TLVFix16Packet;
133     typedef ConcretePacket<TLVPacketType<UInt24Parser> > TLVFix24Packet;
134     typedef ConcretePacket<TLVPacketType<UInt32Parser> > TLVFix32Packet;
135 }
136
137
138 ///////////////////////////////hh.e////////////////////////////////////////
139 //#include "TLVPacket.cci"
140 #include "TLVPacket.ct"
141 //#include "TLVPacket.cti"
142 #endif
143
144 \f
145 // Local Variables:
146 // mode: c++
147 // fill-column: 100
148 // c-file-style: "senf"
149 // indent-tabs-mode: nil
150 // ispell-local-dictionary: "american"
151 // compile-command: "scons -u test"
152 // comment-column: 40
153 // End: