for david: added first tlv-packet prototype
[senf.git] / Packets / MPEGDVBBundle / TLVPacket.hh
1 // $Id: SNDUPacket.hh 423 2007-08-31 22:05:37Z g0dil $
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 SNDUPacket 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
41     struct UnsuportedTLVPacketException : public std::exception
42     { 
43         virtual char const * what() const throw() { 
44             return "length of length can be max. 4 bytes. Sorry.";
45         }
46     };
47
48     struct Parse_TLVPacketLength 
49         : public detail::packet::ParseIntOps<Parse_TLVPacketLength, boost::uint32_t>,
50           public PacketParserBase
51     {
52 #       ifndef DOXYGEN
53         
54         SENF_PACKET_PARSER_NO_INIT(Parse_TLVPacketLength);
55
56         typedef Parse_Flag      <    0 > Parse_extended_length_flag;
57         typedef Parse_UIntField < 1, 8 > Parse_fixed_length;
58
59         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
60             ((OverlayField)( extended_length_flag, Parse_extended_length_flag ))
61             ((Field       )( fixed_length_field,   Parse_fixed_length         ))
62         );
63
64 #       endif
65         
66         typedef boost::uint32_t value_type;
67         
68         value_type value() const {
69             switch( bytes() ) {
70                 case 1:
71                     return fixed_length_field().value();
72                 case 2:
73                     return parse<Parse_UInt8>( 1 ).value();
74                 case 3:
75                     return parse<Parse_UInt16>( 1 ).value();
76                 case 4:
77                     return parse<Parse_UInt24>( 1 ).value();
78                 case 5:
79                     return parse<Parse_UInt32>( 1 ).value();
80                 default:
81                     throw(UnsuportedTLVPacketException()); 
82             };
83         }
84         
85         size_type bytes() const {
86             if ( extended_length_flag() )
87                 return 1 + fixed_length_field();
88             else
89                 return 1;
90         }
91         
92         void init() const {
93             defaultInit();
94             extended_length_flag() = 0;
95         }
96         
97     };  
98         
99     /** \brief parse TLVPacket Packet
100     
101         XXX
102         
103         \see TLVPacketType
104      */
105     struct Parse_TLVPacket : public PacketParserBase
106     {
107 #       ifndef DOXYGEN
108         
109         SENF_PACKET_PARSER_INIT(Parse_TLVPacket);
110         
111         SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
112             ((Field)( type,   Parse_UInt32          ))
113             ((Field)( length, Parse_TLVPacketLength ))
114         );
115         
116 #       endif
117         
118 //        Parse_UInt32 type() const { 
119 //            return parse<Parse_UInt32>( 0 ); 
120 //        }
121         
122 //        Parse_TLVPacketLength length() const {
123 //            return parse<Parse_TLVPacketLength>( 4 );
124 //        }
125         
126         PacketParserBase::size_type bytes() const;
127         
128         static const size_type init_bytes = 4+1; // 4 bytes type + 1 byte length
129
130     };
131
132     /** \brief TLV Packet
133         
134         \par Packet type (typedef):
135             \ref TLVPacket
136
137         \par Fields:
138             \ref Parse_TLVPacket
139
140         \ingroup protocolbundle_mpegdvb
141      */
142     struct TLVPacketType
143         : public PacketTypeBase,
144           public PacketTypeMixin<TLVPacketType>
145     {
146         typedef PacketTypeMixin<TLVPacketType> mixin;
147         typedef ConcretePacket<TLVPacketType> packet;
148         typedef Parse_TLVPacket parser;
149
150         using mixin::nextPacketRange;
151         using mixin::init;
152         
153         
154         static void dump(packet p, std::ostream & os);
155         
156         static PacketParserBase::size_type initSize();
157         static PacketParserBase::size_type initHeadSize();
158     };
159         
160     typedef TLVPacketType::packet TLVPacket;
161     
162 }
163
164
165 ///////////////////////////////hh.e////////////////////////////////////////
166 //#include "TLVPacket.cci"
167 //#include "TLVPacket.ct"
168 //#include "TLVPacket.cti"
169 #endif
170
171 \f
172 // Local Variables:
173 // mode: c++
174 // fill-column: 100
175 // c-file-style: "senf"
176 // indent-tabs-mode: nil
177 // ispell-local-dictionary: "american"
178 // compile-command: "scons -u test"
179 // comment-column: 40
180 // End: