Socket: Introduce sub-directory structure for concrete protocols
[senf.git] / Examples / DVBAdapter / ULEdec.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@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 // Definition of non-inline non-template functions
24
25 #include <string>
26 #include <iostream>
27 #include <iomanip>
28 #include <sys/ioctl.h>
29 #include <linux/sockios.h>
30 #include <linux/dvb/dmx.h> 
31
32 #include "Scheduler/Scheduler.hh"
33 #include "Packets/DefaultBundle/EthernetPacket.hh"
34 #include "Packets/MPEGDVBBundle/TransportPacket.hh"
35 #include "Utils/membind.hh"
36 #include "Socket/Protocols/DVB/DVBDemuxHandles.hh"
37 #include "Packets/ParseInt.hh"
38 #include "Packets/Packet.hh"
39 #include "Packets/PacketData.hh"
40
41 #define PID 271
42
43 #define prefix_
44 ///////////////////////////////cc.p////////////////////////////////////////
45
46 namespace {
47
48     static const unsigned BLOCK_SIZE = 16;
49
50     template <class Iterator>
51     void hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
52     {
53         unsigned offset (0);
54         std::string ascii;
55         for (; i != i_end; ++i, ++offset) {
56             switch (offset % BLOCK_SIZE) {
57             case 0:
58                 if (!ascii.empty()) {
59                     stream << "  " << ascii << "\n";
60                     ascii = "";
61                 }
62                 stream << "  "
63                           << std::hex << std::setw(4) << std::setfill('0')
64                           << offset << ' ';
65                 break;
66             case BLOCK_SIZE/2:
67                 stream << " ";
68                 ascii += ' ';
69                 break;
70             }
71             stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
72                       << unsigned(*i);
73             ascii += (*i >= ' ' && *i < 126) ? *i : '.';
74         }
75         if (!ascii.empty()) {
76             for (; (offset % BLOCK_SIZE) != 0; ++offset) {
77                 if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
78                     stream << " ";
79                 stream << "   ";
80             }
81             stream << "  " << ascii << "\n";
82         }
83         stream << std::dec;
84     }
85 }
86
87
88 class MySniffer
89 {
90     senf::DVBDemuxPESHandle demuxHandle;
91     senf::DVBDvrHandle dvrHandle;
92
93 public:
94     MySniffer()
95     {
96         struct dmx_pes_filter_params pes_filter;
97         memset(&pes_filter, 0, sizeof (struct dmx_pes_filter_params));
98         pes_filter.pid = PID;
99         pes_filter.input  = DMX_IN_FRONTEND;
100         pes_filter.output = DMX_OUT_TS_TAP;
101         pes_filter.pes_type = DMX_PES_OTHER;
102         pes_filter.flags = DMX_IMMEDIATE_START;
103
104         demuxHandle.protocol().setPESFilter( &pes_filter );
105         
106         senf::Scheduler::instance().add(
107             dvrHandle, senf::membind(&MySniffer::dumpSection, this));
108     }
109
110 private:
111     void dumpSection(senf::FileHandle /* ignored */, senf::Scheduler::EventId event)
112     {
113         std::string data (dvrHandle.read());
114         senf::TransportPacket packet (senf::TransportPacket::create(data));
115         packet.dump(std::cout);
116         senf::PacketData & packetData (packet.last().data());
117         hexdump(packetData.begin(), packetData.end(), std::cout);
118     }
119 };
120
121 int main(int argc, char const * argv[])
122 {
123     try {
124         MySniffer sniffer;
125         senf::Scheduler::instance().process();
126     }
127     catch (std::exception const & ex) {
128         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
129     }
130     return 0;
131 }
132
133
134 ///////////////////////////////cc.e////////////////////////////////////////
135 #undef prefix_
136
137 \f
138 // Local Variables:
139 // mode: c++
140 // fill-column: 100
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"
145 // comment-column: 40
146 // End: