4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Thorsten Horstmann <thorsten.horstmann@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 <boost/format.hpp>
29 #include <senf/Packets.hh>
30 #include <senf/Utils/hexdump.hh>
31 #include <senf/Utils/membind.hh>
34 #define TRANSPORT_PACKET_SIZE 188
35 // max. payload_pointer = ts packet payload size ( = ts packet size - ts header - payload_pointer)
36 // - 2 bytes min. sndu header
37 #define MAX_PAYLOAD_POINTER ( TRANSPORT_PACKET_SIZE - 4 - 1 - 2 )
40 ///////////////////////////////cc.p////////////////////////////////////////
43 ULEdec::ULEdec(unsigned short adapter, unsigned short device)
44 : demuxHandle( adapter, device ), dvrHandle( adapter, device )
46 struct dmx_pes_filter_params pes_filter;
47 memset(&pes_filter, 0, sizeof (struct dmx_pes_filter_params));
49 pes_filter.input = DMX_IN_FRONTEND;
50 pes_filter.output = DMX_OUT_TS_TAP;
51 pes_filter.pes_type = DMX_PES_OTHER;
52 pes_filter.flags = DMX_IMMEDIATE_START;
53 demuxHandle.protocol().setPESFilter( &pes_filter );
55 senf::Scheduler::instance().add(
56 dvrHandle, senf::membind(&ULEdec::handleEvent, this));
58 this->receiver_state = Idle;
59 this->priv_sndu_type_1 = false;
62 void ULEdec::handleEvent(senf::Scheduler::EventId event)
64 senf::TransportPacket ts_packet (
65 senf::TransportPacket::create(188, senf::TransportPacket::noinit));
66 dvrHandle.read( ts_packet.data() );
68 // Check TS error conditions: sync_byte, transport_error_indicator, scrambling_control.
69 if ( (ts_packet->sync_byte() != TRANSPORT_PACKET_SYNC_BYTE) ||
70 (ts_packet->transport_error_indicator() == true) ||
71 (ts_packet->transport_scrmbl_ctrl() != 0))
73 std::cerr << "invalid ts packet\n";
74 // drop partly decoded SNDU, reset state, resync on PUSI.
78 handleTSPacket(ts_packet);
81 void ULEdec::handleTSPacket(senf::TransportPacket ts_packet)
83 senf::PacketData & payloadData (ts_packet.next().data());
84 iterator payload_iter = payloadData.begin();
85 iterator payload_end = payloadData.end();
87 std::cout << "New TS Packet:\n"
88 << "----------------------------------------------------------------------------\n";
89 senf::hexdump(payload_iter, payload_end, std::cout);
90 std::cout << "----------------------------------------------------------------------------\n";
92 // Synchronize continuity counter
93 this->priv_tscc = ts_packet->continuity_counter();
95 switch (ts_packet->pusi()) {
97 switch (this->receiver_state) {
101 payload_iter = readContSNDUPacket( payload_iter, payload_end );
102 if (isSDNUPacketComplete()) {
103 this->receiver_state = Idle;
106 } catch (ULEdecException const & ex) {
107 std::cerr << ex.what() << "\n";
110 switch (std::distance( payload_iter, payload_end ))
115 if ( (*payload_iter++ << 8 | *payload_iter++) != ULE_END_INDICATOR )
116 std::cerr << "delimiting error\n";
118 BOOST_ASSERT( std::distance( payload_iter, payload_end ) == 0 );
124 // a PUSI value of 1 indicates the presence of a Payload Pointer.
125 unsigned char payload_pointer = *payload_iter++;
126 if (payload_pointer > MAX_PAYLOAD_POINTER) {
127 std::cerr << str( boost::format(
128 "invalid payload_pointer (%d)\n") % unsigned(payload_pointer) ) ;
129 this->receiver_state = Idle;
132 switch (this->receiver_state) {
134 std::advance(payload_iter, payload_pointer);
137 // Reassembly Payload Pointer Checking
138 if (snduPacketBytesLeft() != payload_pointer) {
140 std::cerr << str( boost::format(
141 "delimiting error: bytes left in SNDU packet != payload_pointer ("
142 "(%d != %d)\n") % snduPacketBytesLeft() % payload_pointer );
143 std::advance(payload_iter, payload_pointer);
145 payload_iter = readContSNDUPacket( payload_iter, payload_end );
146 BOOST_ASSERT( isSDNUPacketComplete() );
149 } catch (ULEdecException const & ex) {
150 std::cerr << ex.what() << "\n";
151 this->receiver_state = Idle;
156 this->receiver_state = Idle;
159 payload_iter = readNewSNDUPacket( payload_iter, payload_end );
160 if (! isSDNUPacketComplete()) {
161 BOOST_ASSERT( std::distance( payload_iter, payload_end ) == 0 );
162 this->receiver_state = Reassembly;
166 } while (std::distance(payload_iter, payload_end) < 2 );
168 catch (ULEdecException const & ex) {
169 std::cerr << ex.what() << "\n";
178 ULEdec::iterator ULEdec::readContSNDUPacket(iterator i, iterator const i_end)
180 if (priv_sndu_type_1) {
181 this->snduPacket->type() |= *i++;
183 return readRawSNDUPacketData(i, i_end);
187 ULEdec::iterator ULEdec::readNewSNDUPacket(iterator i, iterator const i_end)
190 senf::Packet::size_type sndu_length = *i++ << 8 | *i++;
191 if (sndu_length & 0x8000) {
192 sndu_length &= 0x7FFF;
195 if (sndu_length < 5 || sndu_length == 0xffff) {
196 throw ULEdecException( str( boost::format(
197 "SNDU length error. length=%d") % sndu_length) );
199 this->snduPacket = senf::SNDUPacket::create(sndu_length+4);
202 this->snduPacket->withoutDestination();
203 // else not needed since default on newly created packet is withDestination()
205 this->snduPacket->length() = sndu_length;
206 this->snduPacketData_iter = boost::next(this->snduPacket.data().begin(), 2);
207 this->priv_sndu_type_1 = false;
209 switch (std::distance(i, i_end)) {
211 this->priv_sndu_type_1 = true;
212 this->snduPacket->type() = *i++ << 8;
213 this->snduPacketData_iter++;
218 this->snduPacket->type() = *i++ << 8 | *i++;
219 std::advance(this->snduPacketData_iter, 2);
220 i = readRawSNDUPacketData(i, i_end);
227 ULEdec::iterator ULEdec::readRawSNDUPacketData(iterator i, iterator i_end)
229 unsigned char how_much = std::min(
230 snduPacketBytesLeft(), std::distance(i, i_end) );
231 copy_n(i, how_much, this->snduPacketData_iter);
232 std::advance(i, how_much);
233 std::advance(this->snduPacketData_iter, how_much);
238 void ULEdec::handleSNDUPacket()
240 this->snduPacket.dump(std::cout);
241 std::cout << "----------------------------------------------------------------------------\n\n";
242 if (this->snduPacket->crc() != this->snduPacket->calcCrc()) {
243 throw ULEdecException( str( boost::format(
244 "CRC Error. received crc:%d calculated crc:%d")
245 % this->snduPacket->crc() % this->snduPacket->calcCrc() ) );
247 // senf::Packet nextPacket = this->snduPacket.next();
249 // nextPacket.data().begin(),
250 // nextPacket.data().end(),
256 inline bool ULEdec::isSDNUPacketComplete()
258 return this->snduPacketData_iter == this->snduPacket.data().end();
261 inline ULEdec::iterator::difference_type ULEdec::snduPacketBytesLeft()
263 return std::distance( this->snduPacketData_iter, this->snduPacket.data().end() );
267 int main(int argc, char const * argv[])
271 senf::Scheduler::instance().process();
273 catch (std::exception const & ex) {
274 std::cerr << senf::prettyName(typeid(ex)) << ": " << ex.what() << "\n";
280 ///////////////////////////////cc.e////////////////////////////////////////
287 // c-file-style: "senf"
288 // indent-tabs-mode: nil
289 // ispell-local-dictionary: "american"
290 // compile-command: "scons -U"
291 // comment-column: 40