17b2f9aa7d84d4ce92993d17bad253cecb74cbfa
[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 DynamicTLVLengthParser 
55         : public detail::packet::IntParserOps<DynamicTLVLengthParser, boost::uint32_t>,
56           public PacketParserBase
57     {
58     public:
59         DynamicTLVLengthParser(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         DynamicTLVLengthParser const & operator= (value_type other);
70         size_type bytes() const;
71         void init() const;
72
73     private:
74         typedef FlagParser      <    0 > ExtendedLengthFlagParser;
75         typedef UIntFieldParser < 1, 8 > FixedLengthParser;
76
77         ExtendedLengthFlagParser extended_length_flag() const {
78             return parse<ExtendedLengthFlagParser>( 0 );
79         }
80
81         FixedLengthParser fixed_length_field() const {
82             return parse<FixedLengthParser>( 0 );
83         }
84         
85         void resize(size_type size);
86     };  
87         
88     /** \brief parse TLVPacket Packet
89     
90         \todo document me
91      
92         \see TLVPacketType
93      */
94     template <class TypeParser, class LengthParser>
95     struct TLVPacketParser : public PacketParserBase
96     {
97 #       include SENF_PARSER()
98         
99         SENF_PARSER_FIELD( type,   TypeParser );
100         SENF_PARSER_FIELD( length, LengthParser );
101         
102         SENF_PARSER_FINALIZE(TLVPacketParser);
103     };
104     
105     /** \brief generic TLV Packet type
106         
107         \todo document me
108         
109         \ingroup protocolbundle_mpegdvb
110      */
111     template <class TypeParser, class LengthParser>
112     struct TLVPacketType
113         : public PacketTypeBase
114     {
115         typedef ConcretePacket<TLVPacketType<TypeParser, LengthParser> > packet;
116         typedef TLVPacketParser<TypeParser, LengthParser> parser;
117
118         static optional_range nextPacketRange(packet p);
119         static size_type initSize();
120         
121         static void finalize(packet p);
122         
123         static void dump(packet p, std::ostream & os);
124     };
125     
126     typedef ConcretePacket<TLVPacketType<UInt32Parser, DynamicTLVLengthParser> > MIHInfoElement;
127 }
128
129
130 ///////////////////////////////hh.e////////////////////////////////////////
131 //#include "TLVPacket.cci"
132 #include "TLVPacket.ct"
133 //#include "TLVPacket.cti"
134 #endif
135
136 \f
137 // Local Variables:
138 // mode: c++
139 // fill-column: 100
140 // c-file-style: "senf"
141 // indent-tabs-mode: nil
142 // ispell-local-dictionary: "american"
143 // compile-command: "scons -u test"
144 // comment-column: 40
145 // End: