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
27 #include <linux/dvb/dmx.h>
28 #include "Packets/PacketData.hh"
29 #include "Utils/hexdump.hh"
30 #include "Utils/membind.hh"
35 ///////////////////////////////cc.p////////////////////////////////////////
38 ULEdec::ULEdec(unsigned short adapter, unsigned short device)
39 : demuxHandle( adapter, device ), dvrHandle( adapter, device )
41 struct dmx_pes_filter_params pes_filter;
42 memset(&pes_filter, 0, sizeof (struct dmx_pes_filter_params));
44 pes_filter.input = DMX_IN_FRONTEND;
45 pes_filter.output = DMX_OUT_TS_TAP;
46 pes_filter.pes_type = DMX_PES_OTHER;
47 pes_filter.flags = DMX_IMMEDIATE_START;
48 demuxHandle.protocol().setPESFilter( &pes_filter );
50 senf::Scheduler::instance().add(
51 dvrHandle, senf::membind(&ULEdec::handleEvent, this));
53 this->receiver_state = Idle;
54 this->priv_sndu_type_1 = false;
57 void ULEdec::handleEvent(senf::FileHandle, senf::Scheduler::EventId event)
59 senf::TransportPacket ts_packet (
60 senf::TransportPacket::create(188, senf::TransportPacket::noinit));
61 dvrHandle.read( ts_packet.data() );
63 // Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control.
64 if ( (ts_packet->sync_byte() != TRANSPORT_PACKET_SYNC_BYTE) ||
65 (ts_packet->transport_error_indicator() == true) ||
66 (ts_packet->transport_scrmbl_ctrl() != 0))
68 std::cerr << "invalid ts packet\n";
69 // drop partly decoded SNDU, reset state, resync on PUSI.
73 handleTSPacket(ts_packet);
76 void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
78 senf::PacketData & payloadData (ts_packet.next().data());
79 iterator payload_iter = payloadData.begin();
80 iterator payload_end = payloadData.end();
82 std::cout << "New TS Packet:\n"
83 << "----------------------------------------------------------------------------\n";
84 senf::hexdump(payload_iter, payload_end, std::cout);
85 std::cout << "----------------------------------------------------------------------------\n";
87 // Synchronize continuity counter
88 this->priv_tscc = ts_packet->continuity_counter();
90 switch (ts_packet->pusi()) {
92 switch (this->receiver_state) {
96 payload_iter = readContSNDUPacket( payload_iter, payload_end );
97 if (isSDNUPacketComplete()) {
98 this->receiver_state = Idle;
101 } catch (ULEdecException const & ex) {
102 std::cerr << ex.what() << "\n";
105 switch (std::distance( payload_iter, payload_end ))
110 if ( (*payload_iter++ << 8 | *payload_iter++) != ULE_END_INDICATOR )
111 std::cerr << "delimiting error 1\n";
113 BOOST_ASSERT( std::distance( payload_iter, payload_end ) == 0 );
119 // a PUSI value of 1 indicates the presence of a Payload Pointer.
120 unsigned char payload_pointer = *payload_iter++;
121 if (payload_pointer > 181) {
122 std::cerr << "invalid payload_pointer\n";
123 this->receiver_state = Idle;
126 switch (this->receiver_state) {
128 std::advance(payload_iter, payload_pointer);
131 // Reassembly Payload Pointer Checking
132 if (snduPacketBytesLeft() != payload_pointer) {
134 std::cerr << "delimiting error 2\n";
135 std::advance(payload_iter, payload_pointer);
137 payload_iter = readContSNDUPacket( payload_iter, payload_end );
138 BOOST_ASSERT( isSDNUPacketComplete() );
141 } catch (ULEdecException const & ex) {
142 std::cerr << ex.what() << "\n";
143 this->receiver_state = Idle;
148 this->receiver_state = Idle;
151 payload_iter = readNewSNDUPacket( payload_iter, payload_end );
152 if (! isSDNUPacketComplete()) {
153 BOOST_ASSERT( std::distance( payload_iter, payload_end ) == 0 );
154 this->receiver_state = Reassembly;
158 } while (std::distance(payload_iter, payload_end) < 2 );
160 catch (ULEdecException const & ex) {
161 std::cerr << ex.what() << "\n";
170 ULEdec::iterator ULEdec::readContSNDUPacket(iterator i, iterator const i_end)
172 if (priv_sndu_type_1) {
173 this->snduPacket->type() |= *i++;
175 return readRawSNDUPacketData(i, i_end);
179 ULEdec::iterator ULEdec::readNewSNDUPacket(iterator i, iterator const i_end)
182 senf::Packet::size_type sndu_length = *i++ << 8 | *i++;
183 if (sndu_length & 0x8000) {
184 sndu_length &= 0x7FFF;
187 if (sndu_length < 5 || sndu_length == 0xffff) {
188 throw ULEdecException("SNDU length error");
190 this->snduPacket = senf::SNDUPacket::create(sndu_length+4);
191 this->snduPacket->d_bit() = dbit;
192 this->snduPacket->length() = sndu_length;
193 this->snduPacketData_iter = boost::next(this->snduPacket.data().begin(), 2);
194 this->priv_sndu_type_1 = false;
196 switch (std::distance(i, i_end)) {
198 this->priv_sndu_type_1 = true;
199 this->snduPacket->type() = *i++ << 8;
200 this->snduPacketData_iter++;
205 this->snduPacket->type() = *i++ << 8 | *i++;
206 std::advance(this->snduPacketData_iter, 2);
207 i = readRawSNDUPacketData(i, i_end);
214 ULEdec::iterator ULEdec::readRawSNDUPacketData(iterator i, iterator i_end)
216 unsigned char how_much = std::min(
217 snduPacketBytesLeft(), std::distance(i, i_end) );
218 copy_n(i, how_much, this->snduPacketData_iter);
219 std::advance(i, how_much);
220 std::advance(this->snduPacketData_iter, how_much);
225 void ULEdec::handleSNDUPacket()
227 this->snduPacket.dump(std::cout);
228 std::cout << "----------------------------------------------------------------------------\n\n";
229 if (this->snduPacket->crc() != this->snduPacket->calcCrc()) {
230 throw ULEdecException("CRC Error");
232 // senf::Packet nextPacket = this->snduPacket.next();
234 // nextPacket.data().begin(),
235 // nextPacket.data().end(),
241 inline bool ULEdec::isSDNUPacketComplete()
243 return this->snduPacketData_iter == this->snduPacket.data().end();
246 inline ULEdec::iterator::difference_type ULEdec::snduPacketBytesLeft()
248 return std::distance( this->snduPacketData_iter, this->snduPacket.data().end() );
252 int main(int argc, char const * argv[])
256 senf::Scheduler::instance().process();
258 catch (std::exception const & ex) {
259 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
265 ///////////////////////////////cc.e////////////////////////////////////////
272 // c-file-style: "senf"
273 // indent-tabs-mode: nil
274 // ispell-local-dictionary: "american"
275 // compile-command: "scons -u test"
276 // comment-column: 40