for david: added first tlv-packet prototype
tho [Thu, 20 Sep 2007 12:25:14 +0000 (12:25 +0000)]
git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@451 270642c3-0616-0410-b53a-bc976706d245

Packets/MPEGDVBBundle/TLVPacket.cc [new file with mode: 0644]
Packets/MPEGDVBBundle/TLVPacket.hh [new file with mode: 0644]
Packets/MPEGDVBBundle/TLVPacket.test.cc [new file with mode: 0644]

diff --git a/Packets/MPEGDVBBundle/TLVPacket.cc b/Packets/MPEGDVBBundle/TLVPacket.cc
new file mode 100644 (file)
index 0000000..d93d5c2
--- /dev/null
@@ -0,0 +1,80 @@
+// $Id: SNDUPacket.cc 423 2007-08-31 22:05:37Z g0dil $
+//
+// Copyright (C) 2007
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief DSMCCSection non-inline non-template implementation */
+
+#include "TLVPacket.hh"
+//#include "TLVPacket.ih"
+
+// Custom includes
+#include <iomanip>
+#include "../../Utils/hexdump.hh"
+#include "../../Packets/DefaultBundle/EthernetPacket.hh"
+
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+
+prefix_ senf::PacketParserBase::size_type senf::Parse_TLVPacket::bytes()
+    const
+{
+    return 4 + senf::bytes( length() );
+}
+
+//prefix_ void senf::TLVPacketType::init(packet p)
+//{
+//    p->init();
+//}
+
+prefix_ void senf::TLVPacketType::dump(packet p, std::ostream & os)
+{
+    os << "TLVPacket:\n"
+       << std::dec
+       << "  type: " <<  unsigned(p->type()) << "\n"
+       << "  length: " << unsigned(p->length()) << "\n";
+}
+
+prefix_ senf::PacketParserBase::size_type senf::TLVPacketType::initSize()
+{
+    return 4 + 1;
+}
+
+prefix_ senf::PacketParserBase::size_type senf::TLVPacketType::initHeadSize()
+{
+    return 4 + 1;
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
diff --git a/Packets/MPEGDVBBundle/TLVPacket.hh b/Packets/MPEGDVBBundle/TLVPacket.hh
new file mode 100644 (file)
index 0000000..08de4cd
--- /dev/null
@@ -0,0 +1,180 @@
+// $Id: SNDUPacket.hh 423 2007-08-31 22:05:37Z g0dil $
+//
+// Copyright (C) 2007
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief SNDUPacket public header */
+
+#ifndef HH_TLVPacket_
+#define HH_TLVPacket_ 1
+
+// Custom includes
+#include <algorithm>
+#include "../../Packets/PacketType.hh"
+#include "../../Packets/ParseInt.hh"
+#include "../../Packets/PacketParser.hh"
+
+//#include "TLVPacket.mpp"
+///////////////////////////////hh.p////////////////////////////////////////
+
+namespace senf {
+
+
+    struct UnsuportedTLVPacketException : public std::exception
+    { 
+        virtual char const * what() const throw() { 
+            return "length of length can be max. 4 bytes. Sorry.";
+        }
+    };
+
+    struct Parse_TLVPacketLength 
+        : public detail::packet::ParseIntOps<Parse_TLVPacketLength, boost::uint32_t>,
+          public PacketParserBase
+    {
+#       ifndef DOXYGEN
+        
+        SENF_PACKET_PARSER_NO_INIT(Parse_TLVPacketLength);
+
+        typedef Parse_Flag      <    0 > Parse_extended_length_flag;
+        typedef Parse_UIntField < 1, 8 > Parse_fixed_length;
+
+        SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
+            ((OverlayField)( extended_length_flag, Parse_extended_length_flag ))
+            ((Field       )( fixed_length_field,   Parse_fixed_length         ))
+        );
+
+#       endif
+        
+        typedef boost::uint32_t value_type;
+        
+        value_type value() const {
+            switch( bytes() ) {
+                case 1:
+                    return fixed_length_field().value();
+                case 2:
+                    return parse<Parse_UInt8>( 1 ).value();
+                case 3:
+                    return parse<Parse_UInt16>( 1 ).value();
+                case 4:
+                    return parse<Parse_UInt24>( 1 ).value();
+                case 5:
+                    return parse<Parse_UInt32>( 1 ).value();
+                default:
+                    throw(UnsuportedTLVPacketException()); 
+            };
+        }
+        
+        size_type bytes() const {
+            if ( extended_length_flag() )
+                return 1 + fixed_length_field();
+            else
+                return 1;
+        }
+        
+        void init() const {
+            defaultInit();
+            extended_length_flag() = 0;
+        }
+        
+    };  
+        
+    /** \brief parse TLVPacket Packet
+    
+        XXX
+        
+        \see TLVPacketType
+     */
+    struct Parse_TLVPacket : public PacketParserBase
+    {
+#       ifndef DOXYGEN
+        
+        SENF_PACKET_PARSER_INIT(Parse_TLVPacket);
+        
+        SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS(
+            ((Field)( type,   Parse_UInt32          ))
+            ((Field)( length, Parse_TLVPacketLength ))
+        );
+        
+#       endif
+        
+//        Parse_UInt32 type() const { 
+//            return parse<Parse_UInt32>( 0 ); 
+//        }
+        
+//        Parse_TLVPacketLength length() const {
+//            return parse<Parse_TLVPacketLength>( 4 );
+//        }
+        
+        PacketParserBase::size_type bytes() const;
+        
+        static const size_type init_bytes = 4+1; // 4 bytes type + 1 byte length
+
+    };
+
+    /** \brief TLV Packet
+        
+        \par Packet type (typedef):
+            \ref TLVPacket
+
+        \par Fields:
+            \ref Parse_TLVPacket
+
+        \ingroup protocolbundle_mpegdvb
+     */
+    struct TLVPacketType
+        : public PacketTypeBase,
+          public PacketTypeMixin<TLVPacketType>
+    {
+        typedef PacketTypeMixin<TLVPacketType> mixin;
+        typedef ConcretePacket<TLVPacketType> packet;
+        typedef Parse_TLVPacket parser;
+
+        using mixin::nextPacketRange;
+        using mixin::init;
+        
+        
+        static void dump(packet p, std::ostream & os);
+        
+        static PacketParserBase::size_type initSize();
+        static PacketParserBase::size_type initHeadSize();
+    };
+        
+    typedef TLVPacketType::packet TLVPacket;
+    
+}
+
+
+///////////////////////////////hh.e////////////////////////////////////////
+//#include "TLVPacket.cci"
+//#include "TLVPacket.ct"
+//#include "TLVPacket.cti"
+#endif
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End:
diff --git a/Packets/MPEGDVBBundle/TLVPacket.test.cc b/Packets/MPEGDVBBundle/TLVPacket.test.cc
new file mode 100644 (file)
index 0000000..e0755bc
--- /dev/null
@@ -0,0 +1,73 @@
+// $Id: TransportPacket.test.cc 389 2007-08-10 15:06:54Z tho $
+//
+// Copyright (C) 2007
+// Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
+// Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
+//     Thorsten Horstmann <thorsten.horstmann@fokus.fraunhofer.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+// Unit tests
+
+//#include "TLVPacket.test.hh"
+//#include "TLVPacket.test.ih"
+
+// Custom includes
+#include "TLVPacket.hh"
+#include <senf/Packets.hh>
+
+#include <boost/test/auto_unit_test.hpp>
+#include <boost/test/test_tools.hpp>
+
+#define prefix_
+///////////////////////////////cc.p////////////////////////////////////////
+
+using namespace senf;
+
+BOOST_AUTO_UNIT_TEST(tlvPacket_parse_packet_with_extended_length)
+{
+    unsigned char data[] = { 
+        0x01, 0x23, 0x45, 0x67, // type
+        0x81, // first and last bit set => one byte length following
+        0x0A, // length (10 bytes value)
+        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A  // value (payload)
+    };
+            
+    senf::TLVPacket p (senf::TLVPacket::create(data));
+    
+#include <iostream>
+    p.dump(std::cout);
+
+    BOOST_CHECK_EQUAL( p->type(), 0x01234567u );
+    BOOST_CHECK_EQUAL( p->length(), 0x0Au );
+
+    senf::PacketData & p_value (p.next().data());
+//    BOOST_CHECK_EQUAL( p_value.size(), 0x0Au);
+}
+
+///////////////////////////////cc.e////////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// comment-column: 40
+// End: