Packets documentation updates
[senf.git] / Packets / 80221Bundle / MIHPacket.hh
1 // $Id$
2 //
3 // Copyright (C) 2009
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 MIH protocol public header */
25
26 #ifndef HH_SENF_Packets_80221Bundle_MIHPacket_
27 #define HH_SENF_Packets_80221Bundle_MIHPacket_ 1
28
29 // Custom includes
30 #include "../../Packets/Packets.hh"
31 #include "../../Socket/Protocols/Raw/MACAddress.hh"
32 #include "../../Socket/Protocols/INet/INet4Address.hh"
33 #include "../../Socket/Protocols/INet/INet6Address.hh"
34 #include "TLVPacket.hh"
35 #include <boost/function_output_iterator.hpp>
36 #include <boost/iterator/filter_iterator.hpp>
37
38
39 //#include "MIHPacket.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
41
42 namespace senf {
43
44     // the maximum length of a MIHF_ID is 253 octets (see F.3.11 in 802.21)
45     // we could set maxLengthValue in INIT, but for the most MIHF_IDs the default
46     // maximum length of 127 should be enough.
47     // The user must call mihPacket->src_mihfId().maxLengthValue( 253) before 
48     // setting longer MIHF_IDs
49     class MIHFId_TLVParser : public BaseTLVPacketParser
50     {
51     #   include SENF_PARSER()        
52         SENF_PARSER_INHERIT  ( BaseTLVPacketParser );
53         SENF_PARSER_SKIP     ( length(), 0         );
54         SENF_PARSER_FINALIZE ( MIHFId_TLVParser    );
55                 
56         std::string asString() const;
57         void setString(std::string const &id);
58         
59         senf::MACAddress asMACAddress() const;
60         void setMACAddress(senf::MACAddress const &mac);
61
62         senf::INet4Address asINet4Address() const;
63         void setINet4Address(senf::INet4Address const &addr);
64         
65         senf::INet6Address asINet6Address() const;
66         void setINet6Address(senf::INet6Address const &addr);
67
68     private:
69         template <class OutputIterator>
70         struct binaryNAIEncoder {
71             binaryNAIEncoder(OutputIterator &i) : i_(i) {}
72             void operator()(const boost::uint8_t &v) const {
73                 *i_++ = '\\'; 
74                 *i_++ = v;
75             }
76             OutputIterator &i_;
77         };
78         template <class OutputIterator>
79         static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> > getNAIEncodedOutputIterator(OutputIterator i) {
80             return boost::make_function_output_iterator(binaryNAIEncoder<OutputIterator>(i));
81         }
82         
83         struct binaryNAIDecoder {
84             binaryNAIDecoder() : readNextByte_(true) {}
85             bool operator()(const boost::uint8_t &v) { 
86                 readNextByte_ = readNextByte_ ? false : true;
87                 return readNextByte_;
88             }
89             bool readNextByte_;
90         };
91         template <class Iterator>
92         static boost::filter_iterator<binaryNAIDecoder, Iterator> getNAIDecodedIterator(Iterator begin, Iterator end) {
93             return boost::make_filter_iterator<binaryNAIDecoder>(begin, end);
94         }
95     };
96
97     /** \brief Parse a MIH packet
98
99         Parser implementing the MIH header. The fields implemented are:
100         \image html MIHPacket.png
101         
102         \see MIHPacketType
103      */
104     struct MIHPacketParser : public PacketParserBase
105     {
106     #   include SENF_PARSER()
107         
108         SENF_PARSER_BITFIELD_RO ( version,       4,  unsigned );
109         SENF_PARSER_BITFIELD    ( ackRequest,    1,  bool     );
110         SENF_PARSER_BITFIELD    ( ackResponse,   1,  bool     );
111         SENF_PARSER_BITFIELD    ( uir,           1,  bool     );
112         SENF_PARSER_BITFIELD    ( moreFragment,  1,  bool     );
113         SENF_PARSER_BITFIELD    ( fragmentNr,    7,  unsigned );
114         SENF_PARSER_SKIP_BITS   ( 1                           );
115         
116         // MIH message ID (MID)
117         SENF_PARSER_BITFIELD ( sid,     4,  unsigned );
118         SENF_PARSER_BITFIELD ( opcode,  2,  unsigned );
119         SENF_PARSER_BITFIELD ( aid,    10,  unsigned );
120         
121         SENF_PARSER_SKIP_BITS ( 4                           );
122         SENF_PARSER_BITFIELD  ( transactionId, 12, unsigned );
123         SENF_PARSER_FIELD_RO  ( payloadLength, UInt16Parser );
124         
125         // Source MIHF Id
126         SENF_PARSER_FIELD ( src_mihfId, MIHFId_TLVParser );
127         // Destination MIHF Id
128         SENF_PARSER_FIELD ( dst_mihfId, MIHFId_TLVParser );
129         
130         SENF_PARSER_FINALIZE ( MIHPacketParser );
131         
132         SENF_PARSER_INIT() {
133             version_() = 1;
134             src_mihfId().type() = 1;
135             dst_mihfId().type() = 2;
136         }
137         
138         friend class MIHPacketType;
139     };
140     
141     /** \brief MIH packet
142
143         \par Packet type (typedef):
144             \ref MIHPacket
145
146         \par Fields:
147             \ref MIHPacketParser
148         
149         \ingroup protocolbundle_80221
150      */
151     struct MIHPacketType
152         : public PacketTypeBase,
153           public PacketTypeMixin<MIHPacketType>
154     {
155 #ifndef DOXYGEN
156         typedef PacketTypeMixin<MIHPacketType> mixin;
157 #endif
158         typedef ConcretePacket<MIHPacketType> packet; ///< MIH packet typedef
159         typedef MIHPacketParser parser;               ///< typedef to the parser of MIH packet
160
161         using mixin::nextPacketRange;
162         using mixin::init;
163         using mixin::initSize;
164
165         /** \brief Dump given MIH packet in readable form to given output stream */
166         static void dump(packet p, std::ostream &os);
167         static void finalize(packet p);
168         static factory_t nextPacketType(packet p);
169     };
170
171     /** \brief MIH packet typedef */
172     typedef ConcretePacket<MIHPacketType> MIHPacket;
173     
174     
175     struct MIHPayloadPacketParser : public PacketParserBase
176     {
177     #   include SENF_PARSER()
178         SENF_PARSER_LIST ( tlv_list, packetSize(), GenericTLVPacketParser );
179         
180         SENF_PARSER_FINALIZE ( MIHPayloadPacketParser );
181     };
182     
183     struct MIHPayloadPacketType
184         : public PacketTypeBase,
185           public PacketTypeMixin<MIHPayloadPacketType>
186     {
187 #ifndef DOXYGEN
188         typedef PacketTypeMixin<MIHPayloadPacketType> mixin;
189 #endif
190         typedef ConcretePacket<MIHPayloadPacketType> packet; ///< MIH Payload packet typedef 
191         typedef MIHPayloadPacketParser parser; ///< typedef to the parser of MIH Payload packet
192
193         using mixin::nextPacketRange;
194         using mixin::init;
195         using mixin::initSize;
196
197         /** \brief Dump given MIHPayload in readable form to given output stream */
198         static void dump(packet p, std::ostream &os);
199     };
200         
201      /** \brief MIH Payload packet typedef */
202     typedef ConcretePacket<MIHPayloadPacketType> MIHPayloadPacket;
203 }
204
205
206 ///////////////////////////////hh.e////////////////////////////////////////
207 #endif
208 #ifndef SENF_PACKETS_DECL_ONLY
209 //#include "MIHPacket.cci"
210 //#include "MIHPacket.ct"
211 //#include "MIHPacket.cti"
212 #endif
213
214 \f
215 // Local Variables:
216 // mode: c++
217 // fill-column: 100
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // comment-column: 40
223 // End: