Parser for TLVPacket is working now, thanx to Stefan for the hint.
[senf.git] / Packets / MPEGDVBBundle / TLVPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.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/PacketType.hh"
32 #include "../../Packets/ParseInt.hh"
33 #include "../../Packets/PacketParser.hh"
34
35 //#include "TLVPacket.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39
40     struct UnsuportedTLVPacketException : public std::exception
41     { 
42         virtual char const * what() const throw() { 
43             return "length of length can be max. 4 bytes. Sorry.";
44         }
45     };
46
47     struct Parse_TLVPacketLength 
48         : public detail::packet::ParseIntOps<Parse_TLVPacketLength, boost::uint32_t>,
49           public PacketParserBase
50     {
51 #       ifndef DOXYGEN
52         
53         SENF_PACKET_PARSER_NO_INIT(Parse_TLVPacketLength);
54
55 #       endif
56
57         typedef boost::uint32_t value_type;
58         
59         typedef Parse_Flag      <    0 > Parse_extended_length_flag;
60         typedef Parse_UIntField < 1, 8 > Parse_fixed_length;
61
62         Parse_extended_length_flag extended_length_flag() const {
63             return parse<Parse_extended_length_flag>( 0 );
64         }
65
66         Parse_fixed_length fixed_length_field() const {
67             return parse<Parse_fixed_length>( 0 );
68         }
69         
70         value_type value() const {
71             switch( bytes() ) {
72                 case 1:
73                     return fixed_length_field().value();
74                 case 2:
75                     return parse<Parse_UInt8>( 1 ).value();
76                 case 3:
77                     return parse<Parse_UInt16>( 1 ).value();
78                 case 4:
79                     return parse<Parse_UInt24>( 1 ).value();
80                 case 5:
81                     return parse<Parse_UInt32>( 1 ).value();
82                 default:
83                     throw(UnsuportedTLVPacketException()); 
84             };
85         }
86         
87         static const size_type init_bytes = 1;
88
89         size_type bytes() const {
90             if ( extended_length_flag() )
91                 return 1 + fixed_length_field();
92             else
93                 return 1;
94         }
95         
96         void init() const {
97             defaultInit();
98             extended_length_flag() = 0;
99         }
100         
101     };  
102         
103     /** \brief parse TLVPacket Packet
104     
105         XXX
106         
107         \see TLVPacketType
108      */
109     struct Parse_TLVPacket : public PacketParserBase
110     {
111 #       ifndef DOXYGEN
112         
113         SENF_PACKET_PARSER_INIT(Parse_TLVPacket);
114         
115         SENF_PACKET_PARSER_DEFINE_FIELDS(
116             ((Field)( type,   Parse_UInt32          ))
117             ((Field)( length, Parse_TLVPacketLength ))
118         );
119         
120 #       endif
121     };
122
123     /** \brief TLV Packet
124         
125         \par Packet type (typedef):
126             \ref TLVPacket
127
128         \par Fields:
129             \ref Parse_TLVPacket
130
131         \ingroup protocolbundle_mpegdvb
132      */
133     struct TLVPacketType
134         : public PacketTypeBase
135     {
136         typedef ConcretePacket<TLVPacketType> packet;
137         typedef Parse_TLVPacket parser;
138
139         static optional_range nextPacketRange(packet p);
140         
141         static void dump(packet p, std::ostream & os);
142     };
143         
144     typedef TLVPacketType::packet TLVPacket;
145     
146 }
147
148
149 ///////////////////////////////hh.e////////////////////////////////////////
150 //#include "TLVPacket.cci"
151 //#include "TLVPacket.ct"
152 //#include "TLVPacket.cti"
153 #endif
154
155 \f
156 // Local Variables:
157 // mode: c++
158 // fill-column: 100
159 // c-file-style: "senf"
160 // indent-tabs-mode: nil
161 // ispell-local-dictionary: "american"
162 // compile-command: "scons -u test"
163 // comment-column: 40
164 // End: