more work on TLV & MIH Packet
[senf.git] / Packets / 80221Bundle / 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_SENF_Packets_80221Bundle_TLVPacket_
27 #define HH_SENF_Packets_80221Bundle_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         \ingroup protocolbundle_80221
44     */
45     struct UnsuportedTLVPacketException : public senf::Exception
46     { UnsuportedTLVPacketException() 
47           : senf::Exception("length of length can be max. 4 bytes. Sorry."){} };
48
49     /** \brief xxx
50         \todo document me
51         
52         \ingroup protocolbundle_80221
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 #       include SENF_PARSER()
74         
75         SENF_PARSER_PRIVATE_BITFIELD ( extended_length_flag, 1,  bool     );
76         SENF_PARSER_PRIVATE_BITFIELD ( fixed_length_field,   7,  unsigned );
77         
78     private:
79
80 //        typedef FlagParser      <    0 > ExtendedLengthFlagParser;
81 //        typedef UIntFieldParser < 1, 8 > FixedLengthParser;
82 //
83 //        ExtendedLengthFlagParser extended_length_flag() const {
84 //            return parse<ExtendedLengthFlagParser>( 0 );
85 //        }
86 //
87 //        FixedLengthParser fixed_length_field() const {
88 //            return parse<FixedLengthParser>( 0 );
89 //        }
90         
91         void resize(size_type size, SafePacketParserWrapper<DynamicTLVLengthParser> &safeThis);
92     };  
93         
94     /** \brief parse TLVPacket Packet
95     
96         \todo document me
97      
98         \see TLVPacketType
99         
100         \ingroup protocolbundle_80221
101      */
102     struct GenericTLVPacketParser : public PacketParserBase
103     {
104 #       include SENF_PARSER()
105         
106         SENF_PARSER_FIELD    ( type,   UInt8Parser                );
107         SENF_PARSER_FIELD_RO ( length, DynamicTLVLengthParser     );
108         SENF_PARSER_VECTOR   ( value,  bytes(length), UInt8Parser );
109         
110         SENF_PARSER_FINALIZE( GenericTLVPacketParser );
111     };
112     
113     /** \brief generic TLV Packet type
114         
115         \todo document me
116         
117         \ingroup protocolbundle_80221
118      */
119     struct GenericTLVPacketType
120         : public PacketTypeBase,
121           public PacketTypeMixin<GenericTLVPacketType>
122     {
123         typedef PacketTypeMixin<GenericTLVPacketType> mixin;
124         typedef ConcretePacket<GenericTLVPacketType> packet;
125         typedef GenericTLVPacketParser parser;
126
127 //        static optional_range nextPacketRange(packet p);
128         using mixin::nextPacketRange;
129         using mixin::init;
130         using mixin::initSize;
131         
132 //        static void finalize(packet p);
133         static void dump(packet p, std::ostream & os);
134     };
135     
136     typedef ConcretePacket<GenericTLVPacketType> GenericTLVPacket;
137 }
138
139
140 ///////////////////////////////hh.e////////////////////////////////////////
141 //#include "TLVPacket.cci"
142 //#include "TLVPacket.ct"
143 //#include "TLVPacket.cti"
144 #endif
145
146 \f
147 // Local Variables:
148 // mode: c++
149 // fill-column: 100
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // comment-column: 40
155 // End: