-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDVBTable.cpp
More file actions
142 lines (134 loc) · 3.99 KB
/
Copy pathDVBTable.cpp
File metadata and controls
142 lines (134 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "DVBTable.h"
#include "ProgramAssociationTable.h"
#include "ProgramMapTable.h"
#include "ServiceDescriptionTable.h"
#include "NetworkInformationTable.h"
#include "Util.h"
#include <iostream>
#include <cstring>
#include <cassert>
#include <cxxabi.h>
extern "C" {
#include <fcntl.h>
#include <unistd.h>
#include <arpa/inet.h>
}
using namespace std;
DVBTable *DVBTable::read(int fd) {
DVBTable *t=new DVBTable(fd);
switch(t->tableId()) {
case ProgramAssociation:
return new ProgramAssociationTable(t);
case ProgramMap:
return new ProgramMapTable(t);
case ServiceDescription:
case ServiceDescriptionOther:
return new ServiceDescriptionTable(t);
case NetworkInformation:
case NetworkInformationOther:
return new NetworkInformationTable(t);
case Invalid:
std::cerr << "Received Invalid table" << std::endl;
delete t;
return nullptr;
default:
std::cerr << "Received table with unknown id " << static_cast<int>(t->tableId()) << std::endl;
}
return t;
}
DVBTable::DVBTable(int fd):_data(nullptr) {
ssize_t count;
unsigned char header[8];
do {
if(!Util::waitForData(fd, 20000)) {
_tableId = Invalid;
return;
}
count = ::read(fd, header, sizeof(header));
if(count != sizeof(header)) {
std::cerr << "Incomplete packet, got " << count << ", expected at least " << sizeof(header) << " from " << fd << std::endl;
if(count<0)
std::cerr << strerror(errno) << std::endl;
ioctl(fd, DMX_STOP);
sleep(1);
ioctl(fd, DMX_START);
continue;
}
_tableId = header[0];
_sectionSyntaxIndicator = (header[1]&0b10000000)>>7;
_private = (header[1]&0b01000000)>>6;
// The next 12 bits are the section length (number of
// following bytes including CRC). We change it to
// data length by subtracting the CRC and the rest of
// the header...
_dataLength = (((header[1]&0xf)<<8)|header[2]);
if(_dataLength < 9) {
std::cerr << "Length smaller than header+CRC" << std::endl;
ioctl(fd, DMX_STOP);
sleep(1);
ioctl(fd, DMX_START);
continue;
}
_dataLength -= 9;
_number = (header[3]<<8)|header[4];
_version = (header[5]&0b00111110)>>1;
_currentNext = header[5]&1;
_section = header[6];
_lastSection = header[7];
_data = new unsigned char[_dataLength];
count = ::read(fd, _data, _dataLength);
if(count != _dataLength) {
std::cerr << "Got unexpected data: Expected " << _dataLength << " bytes, got " << count << std::endl;
ioctl(fd, DMX_STOP);
sleep(1);
ioctl(fd, DMX_START);
continue;
}
count = ::read(fd, &_crc, 4);
if(count != 4) {
std::cerr << "Incomplete CRC" << std::endl;
ioctl(fd, DMX_STOP);
sleep(1);
ioctl(fd, DMX_START);
continue;
}
} while(count != 4);
_crc = ntohl(_crc);
_calculatedCrc = Util::crc32(header, sizeof(header));
_calculatedCrc = Util::crc32(_data, _dataLength, _calculatedCrc);
if(_crc != _calculatedCrc) {
Util::SaveIOState sis(std::cerr);
std::cerr << "CRC mismatch! Given: " << hex << _crc << ", calculated: " << _calculatedCrc << std::endl;
}
}
DVBTable::DVBTable(DVBTable *t):
_tableId(std::move(t->_tableId)),
_sectionSyntaxIndicator(std::move(t->_sectionSyntaxIndicator)),
_private(std::move(t->_private)),
_dataLength(std::move(t->_dataLength)),
_number(std::move(t->_number)),
_version(std::move(t->_version)),
_currentNext(std::move(t->_currentNext)),
_section(std::move(t->_section)),
_lastSection(std::move(t->_lastSection)),
_data(std::move(t->_data)),
_crc(std::move(t->_crc)),
_calculatedCrc(std::move(t->_calculatedCrc))
{
t->_data = nullptr;
delete t;
}
bool DVBTable::ok() const {
return _sectionSyntaxIndicator && (_crc == _calculatedCrc);
}
DVBTable::~DVBTable() {
if(_data)
delete[] _data;
}
void DVBTable::dump(std::ostream &where, std::string const &indent) const {
Util::SaveIOState ios(where);
where << indent << "DVB Table type " << hex << static_cast<int>(_tableId) << std::endl;
where << indent << "Section " << dec << static_cast<int>(_section) << "/" << static_cast<int>(_lastSection) << std::endl;
if(_data)
Util::hexdump(_data, _dataLength, where, indent);
}