4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
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.
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.
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.
23 // Definition of non-inline non-template functions
29 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
31 #include <linux/dvb/dmx.h>
33 #include "Scheduler/Scheduler.hh"
34 #include "Packets/DefaultBundle/EthernetPacket.hh"
35 #include "Packets/MPEGDVBBundle/TransportPacket.hh"
36 #include "Packets/MPEGDVBBundle/SNDUPacket.hh"
37 #include "Utils/membind.hh"
38 #include "Utils/hexdump.hh"
39 #include "Socket/Protocols/DVB/DVBDemuxHandles.hh"
40 #include "Packets/ParseInt.hh"
41 #include "Packets/Packet.hh"
42 #include "Packets/PacketData.hh"
43 #include "Packets/ParseInt.hh"
51 ///////////////////////////////cc.p////////////////////////////////////////
56 struct dmx_pes_filter_params pes_filter;
57 memset(&pes_filter, 0, sizeof (struct dmx_pes_filter_params));
59 pes_filter.input = DMX_IN_FRONTEND;
60 pes_filter.output = DMX_OUT_TS_TAP;
61 pes_filter.pes_type = DMX_PES_OTHER;
62 pes_filter.flags = DMX_IMMEDIATE_START;
63 demuxHandle.protocol().setPESFilter( &pes_filter );
65 senf::Scheduler::instance().add(
66 dvrHandle, senf::membind(&ULEdec::handleEvent, this));
68 this->receiver_state = Idle;
69 this->priv_sndu_type_1 = false;
72 void ULEdec::handleEvent(senf::FileHandle, senf::Scheduler::EventId event)
74 senf::TransportPacket ts_packet (
75 senf::TransportPacket::create(188, senf::TransportPacket::noinit));
76 dvrHandle.read( ts_packet.data() );
78 // Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control.
79 if ( (ts_packet->sync_byte() != TS_SYNC) ||
80 (ts_packet->transport_error_indicator() == true) ||
81 (ts_packet->transport_scrmbl_ctrl() != 0))
83 std::cerr << "invalid ts packet\n";
84 // drop partly decoded SNDU, reset state, resync on PUSI.
88 handleTSPacket(ts_packet);
91 void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
93 senf::PacketData & payloadData (ts_packet.next().data());
94 iterator payload_start = payloadData.begin();
95 iterator payload_end = payloadData.end();
97 senf::hexdump(payload_start, payload_end, std::cout);
99 // Synchronize continuity counter
100 this->priv_tscc = ts_packet->continuity_counter();
102 switch (ts_packet->pusi()) {
104 switch (this->receiver_state) {
108 payload_start = readContSNDUPacket( payload_start, payload_end );
109 if (isSDNUPacketComplete()) {
111 this->receiver_state = Idle;
112 switch (std::distance( payload_start, payload_end ))
117 if ( (*payload_start++ | *payload_start++) != ULE_END_INDICATOR )
118 std::cerr << "delimiting error 1\n";
120 BOOST_ASSERT( std::distance( payload_start, payload_end ) == 0 );
126 // a PUSI value of 1 indicates the presence of a Payload Pointer.
127 unsigned char payload_pointer = *payload_start++;
128 if (payload_pointer > 181) {
129 std::cerr << "invalid payload_pointer\n";
130 this->receiver_state = Idle;
133 switch (this->receiver_state) {
135 payload_start += payload_pointer;
138 // Reassembly Payload Pointer Checking
139 if (snduPacketBytesLeft() != payload_pointer) {
141 std::cerr << "delimiting error 2\n";
142 payload_start += payload_pointer;
144 payload_start = readContSNDUPacket( payload_start, payload_end );
145 BOOST_ASSERT( isSDNUPacketComplete() );
150 payload_start = readNewSNDUPacket( payload_start, payload_end );
151 if (! isSDNUPacketComplete()) {
152 BOOST_ASSERT( std::distance( payload_start, payload_end ) == 0 );
153 this->receiver_state = Reassembly;
157 this->receiver_state = Idle;
158 } while (std::distance(payload_start, payload_end) < 2 );
165 ULEdec::iterator ULEdec::readContSNDUPacket(iterator i_start, iterator i_end)
167 if (priv_sndu_type_1) {
168 this->snduPacket->type() |= *i_start++;
170 i_start = readRawSNDUPacketData(i_start, i_end);
176 ULEdec::iterator ULEdec::readNewSNDUPacket(iterator i_start, iterator i_end)
179 senf::Packet::size_type sndu_length;
180 sndu_length = *i_start++ << 8 | *i_start++;
181 if (sndu_length & 0x8000) {
182 sndu_length &= 0x7FFF;
185 this->snduPacket = senf::SNDUPacket::create(sndu_length+4);
186 this->snduPacket->d_bit() = dbit;
187 this->snduPacket->length() = sndu_length;
188 this->snduPacketData_iter = this->snduPacket.data().begin() + 2;
189 this->priv_sndu_type_1 = false;
191 switch (std::distance(i_start, i_end)) {
193 this->priv_sndu_type_1 = true;
194 this->snduPacket->type() = *i_start++;
195 this->snduPacketData_iter++;
200 this->snduPacket->type() = *i_start++ | *i_start++;
201 this->snduPacketData_iter += 2;
202 i_start = readRawSNDUPacketData(i_start, i_end);
209 ULEdec::iterator ULEdec::readRawSNDUPacketData(iterator i_start, iterator i_end)
211 unsigned char how_much = std::min(
212 snduPacketBytesLeft(), std::distance( i_start, i_end ) );
213 copy_n(i_start, how_much, this->snduPacketData_iter);
215 this->snduPacketData_iter += how_much;
220 void ULEdec::handleSNDUPacket()
222 this->snduPacket.dump(std::cout);
226 inline bool ULEdec::isSDNUPacketComplete()
228 return this->snduPacketData_iter == this->snduPacket.data().end();
231 inline ULEdec::iterator::difference_type ULEdec::snduPacketBytesLeft()
233 return std::distance( this->snduPacketData_iter, this->snduPacket.data().end() );
237 int main(int argc, char const * argv[])
241 senf::Scheduler::instance().process();
243 catch (std::exception const & ex) {
244 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
250 ///////////////////////////////cc.e////////////////////////////////////////
257 // c-file-style: "senf"
258 // indent-tabs-mode: nil
259 // ispell-local-dictionary: "american"
260 // compile-command: "scons -u test"
261 // comment-column: 40