added some documentation for PacketData
[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 "Packets/MPEGDVBBundle/SNDUPacket.hh"
36 #include "Utils/membind.hh"
37 #include "Socket/Protocols/DVB/DVBDemuxHandles.hh"
38 #include "Packets/ParseInt.hh"
39 #include "Packets/Packet.hh"
40 #include "Packets/PacketData.hh"
41 #include "Packets/ParseInt.hh"
42
43 #include "ULEdec.hh"
44
45 #define PID 271
46 #define TS_SYNC 0x47
47
48 #define prefix_
49 ///////////////////////////////cc.p////////////////////////////////////////
50
51 template <class Iterator>
52 void ULEdec::hexdump(Iterator i, Iterator const & i_end, std::ostream& stream)
53 {
54     unsigned offset (0);
55     std::string ascii;
56     for (; i != i_end; ++i, ++offset) {
57         switch (offset % BLOCK_SIZE) {
58         case 0:
59             if (!ascii.empty()) {
60                 stream << "  " << ascii << "\n";
61                 ascii = "";
62             }
63             stream << "  "
64                       << std::hex << std::setw(4) << std::setfill('0')
65                       << offset << ' ';
66             break;
67         case BLOCK_SIZE/2:
68             stream << " ";
69             ascii += ' ';
70             break;
71         }
72         stream << ' ' << std::hex << std::setw(2) << std::setfill('0')
73                   << unsigned(*i);
74         ascii += (*i >= ' ' && *i < 126) ? *i : '.';
75     }
76     if (!ascii.empty()) {
77         for (; (offset % BLOCK_SIZE) != 0; ++offset) {
78             if ((offset % BLOCK_SIZE) == BLOCK_SIZE/2)
79                 stream << " ";
80             stream << "   ";
81         }
82         stream << "  " << ascii << "\n";
83     }
84     stream << std::dec;
85 }
86
87
88 ULEdec::ULEdec()
89 {
90     struct dmx_pes_filter_params pes_filter;
91     memset(&pes_filter, 0, sizeof (struct dmx_pes_filter_params));
92     pes_filter.pid = PID;
93     pes_filter.input  = DMX_IN_FRONTEND;
94     pes_filter.output = DMX_OUT_TS_TAP;
95     pes_filter.pes_type = DMX_PES_OTHER;
96     pes_filter.flags = DMX_IMMEDIATE_START;
97     demuxHandle.protocol().setPESFilter( &pes_filter );
98     
99     senf::Scheduler::instance().add(
100         dvrHandle, senf::membind(&ULEdec::handleEvent, this));
101     
102     this->receiver_state = 1; // Idle
103 }
104
105 void ULEdec::handleEvent(senf::FileHandle, senf::Scheduler::EventId event)
106 {
107     senf::TransportPacket ts_packet (
108             senf::TransportPacket::create(188, senf::TransportPacket::noinit));
109     dvrHandle.read( ts_packet.data() );
110    
111     // Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control.
112     if ( (ts_packet->sync_byte() != TS_SYNC) || 
113          (ts_packet->transport_error_indicator() == true) || 
114          (ts_packet->transport_scrmbl_ctrl() != 0)) 
115     {
116         std::cerr << "invalid ts packet\n";
117         // drop partly decoded SNDU, reset state, resync on PUSI.
118         return;
119     }
120     
121     handleTSPacket(ts_packet);
122 }
123     
124 void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
125 {
126     unsigned char payload_pointer;
127
128     senf::PacketData &ts_payload (ts_packet.next().data());
129     BOOST_ASSERT( ts_payload.size() == 184 );
130     
131     switch (this->receiver_state) {
132     case 1: { // Idle State
133         // resync on PUSI
134         if (ts_packet->pusi() == 0) 
135             return; // skip packet
136         
137         // Synchronize continuity counter
138         this->priv_tscc = ts_packet->continuity_counter();
139
140         // a PUSI value of 1 indicates the presence of a Payload Pointer.
141         payload_pointer = ts_payload[0];
142         if (payload_pointer>181) {
143             std::cerr << "invalid payload_pointer\n";
144             return;
145         }
146         payload_pointer++;
147         
148         bool dbit = false;
149         senf::Packet::size_type sndu_length;
150         while (payload_pointer < 184) {
151             sndu_length = ts_payload[payload_pointer] << 8 | ts_payload[payload_pointer+1];
152             if (sndu_length & 0x8000) {
153                 sndu_length &= 0x7FFF;
154                 dbit = true;
155             }
156             this->snduPacket = senf::SNDUPacket::create(sndu_length+2);
157             this->snduPacket->d_bit() = dbit;
158             this->snduPacket->length() = sndu_length;
159
160             return;
161             
162             payload_pointer += 2;
163             
164             
165         }
166         
167         
168         
169         // 
170         break;
171     }
172     case 2: { // Reassembly State
173         break;
174     }
175     }
176 }
177
178
179 int main(int argc, char const * argv[])
180 {
181     try {
182         ULEdec decoder;
183         senf::Scheduler::instance().process();
184     }
185     catch (std::exception const & ex) {
186         std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
187     }
188     return 0;
189 }
190
191
192 ///////////////////////////////cc.e////////////////////////////////////////
193 #undef prefix_
194
195 \f
196 // Local Variables:
197 // mode: c++
198 // fill-column: 100
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u test"
203 // comment-column: 40
204 // End: