-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCAPMT.cpp
288 lines (253 loc) · 8.17 KB
/
CAPMT.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* vdr-plugin-dvbapi - softcam dvbapi plugin for VDR
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "CAPMT.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include "Log.h"
#include "DVBAPI.h"
CAPMT *capmt = NULL;
CAPMT::CAPMT()
{
memset(&caPMT, 0, sizeof(caPMT));
}
CAPMT::~CAPMT()
{
cMutexLock lock(&mutex);
vector<pmtobj>::iterator it;
for (it=pmt.begin(); it!=pmt.end(); ++it)
{
if (it->data)
delete[] it->data;
}
pmt.clear();
}
void CAPMT::ProcessSIDRequest(int card_index, int sid, int ca_lm, const unsigned char *vdr_caPMT, int vdr_caPMTLen)
{
/*
here is what i found so far analyzing AOT_CA_PMT frame from vdr
lm=4 prg=X: request for X SID to be decrypted (added)
lm=5 prg=X: request for X SID to stop decryption (removed)
lm=3 prg=0: this is sent when changing transponder or during epg scan (also before new channel but ONLY if it is on different transponder)
lm=3 prg=X: it seems that this is sent when starting vdr with active timers
*/
cMutexLock lock(&mutex);
bool removed = false;
if (sid == 0)
{
DEBUGLOG("%s: got empty SID - returning from function", __FUNCTION__);
return;
}
//removind the PMT if exists
vector<pmtobj>::iterator it;
for (it = pmt.begin(); it != pmt.end(); ++it)
{
if (it->sid == sid && it->adapter == card_index)
{
if (it->data)
delete[] it->data;
pmt.erase(it);
removed = true;
break;
}
}
//nothing to update/remove
if (ca_lm == 0x05 && !removed && !vdr_caPMTLen)
return;
//adding new or updating existing PMT data
if (ca_lm == 0x04 || ca_lm == 0x03)
{
pmtobj pmto;
pmto.sid = sid;
pmto.len = vdr_caPMTLen;
pmto.adapter = card_index;
if (vdr_caPMTLen > 0)
{
int length;
int offset = 0;
pmto.pilen[0] = vdr_caPMT[4]; //reserved+program_info_length
pmto.pilen[1] = vdr_caPMT[5]; //reserved+program_info_length (+1 for ca_pmt_cmd_id, +4 for above CAPMT_DESC_DEMUX)
//obtaining program_info_length
int ilen = (vdr_caPMT[4] << 8) + vdr_caPMT[5];
//checking if we need to start copying 1 byte further and omit ca_pmt_cmd_id which we already have
if (ilen > 0)
{
offset = 1;
pmto.pilen[1] -= 1; //-1 for ca_pmt_cmd_id which we have counted
}
length = vdr_caPMTLen; //ca_pmt data length
pmto.len = length - 6 - offset;
unsigned char *pmt_data = new unsigned char[pmto.len];
memcpy(pmt_data, vdr_caPMT + 6 + offset, pmto.len); //copy ca_pmt data from vdr
pmto.data = pmt_data;
}
else
{
DEBUGLOG("CA_PMT doesn't contain CA descriptors");
return;
}
pmt.push_back(pmto);
}
SendAll();
}
void CAPMT::SendAll()
{
cMutexLock lock(&mutex);
vector<pmtobj>::iterator it;
if (pmt.empty())
SockHandler->SendStopDescrambling();
else
{
//sending complete PMT objects
int lm = LIST_FIRST;
for (it = pmt.begin(); it != pmt.end();)
{
int sid = it->sid;
pmtobj *pmto = &*it;
++it;
if (it == pmt.end())
lm |= LIST_LAST;
send(pmto->adapter, sid, lm, pmto);
lm = LIST_MORE;
}
}
}
void CAPMT::send(const int adapter, const int sid, int ca_lm, const pmtobj *pmt)
{
int length_field;
int toWrite;
/////// preparing capmt data to send
// http://cvs.tuxbox.org/lists/tuxbox-cvs-0208/msg00434.html
DEBUGLOG("%s: channelSid=0x%x (%d)", __FUNCTION__, sid, sid);
//ca_pmt_tag
caPMT[0] = 0x9F;
caPMT[1] = 0x80;
caPMT[2] = 0x32;
caPMT[3] = 0x82; //2 following bytes for size
caPMT[6] = ca_lm; //list management
caPMT[7] = sid >> 8; //program_number
caPMT[8] = sid & 0xff; //program_number
caPMT[9] = 0; //version_number, current_next_indicator
caPMT[12] = 0x01; //ca_pmt_cmd_id = CAPMT_CMD_OK_DESCRAMBLING
//adding own descriptor with demux and adapter_id
caPMT[13] = 0x83; //adapter_device_descriptor
caPMT[14] = 0x01; //length
caPMT[15] = (char) adapter + AdapterIndexOffset; //adapter id
caPMT[16] = 0x86; //demux_device_descriptor
caPMT[17] = 0x01; //length
caPMT[18] = 0x00; //demux id
caPMT[19] = 0x87; //ca_device_descriptor
caPMT[20] = 0x01; //length
caPMT[21] = (char) adapter + AdapterIndexOffset; //ca id
//adding CA_PMT from vdr
caPMT[10] = pmt->pilen[0]; //reserved+program_info_length
caPMT[11] = pmt->pilen[1] + 1 + 3 + 3 + 3; //reserved+program_info_length (+1 for ca_pmt_cmd_id, +3 +3 +3 are three descriptors)
memcpy(caPMT + 22, pmt->data, pmt->len); //copy ca_pmt data from vdr
length_field = 22 + pmt->len - 6; //-6 = 3 bytes for AOT_CA_PMT and 3 for size
//calculating length_field()
caPMT[4] = length_field >> 8;
caPMT[5] = length_field & 0xff;
//number of bytes in packet to send (adding 3 bytes of ca_pmt_tag and 3 bytes of length_field)
toWrite = length_field + 6;
//sending data
SockHandler->Write(caPMT, toWrite);
}
uint16_t CAPMT::GetCAIDFromPid(int adapter_index, int pid, int& sid)
{
sid = 0;
if (pid <= 0)
return 0;
//DEBUGLOG("%s: adapter_index:%d pid:%d", __FUNCTION__, adapter_index, pid);
cMutexLock lock(&mutex);
vector<pmtobj>::iterator it;
if (!pmt.empty())
{
for (it = pmt.begin(); it != pmt.end(); ++it)
{
//DEBUGLOG("%s: TEST adapter_index:%d pid:%d SID:%04X CAID:%04X", __FUNCTION__, it->adapter, it->pid, it->sid, it->caid);
if (it->pid == pid && it->adapter == adapter_index)
{
sid = it->sid;
//DEBUGLOG("%s: FOUND adapter_index:%d pid:%d SID:%04X CAID:%04X", __FUNCTION__, it->adapter, it->pid, it->sid, it->caid);
return it->caid;
}
}
}
return 0;
}
uint16_t CAPMT::GetCAIDFromSid(int adapter_index, int sid)
{
cMutexLock lock(&mutex);
vector<pmtobj>::iterator it;
if (!pmt.empty())
for (it = pmt.begin(); it != pmt.end(); ++it)
if (it->sid == sid && it->adapter == adapter_index)
return it->caid;
return 0;
}
void CAPMT::UpdateEcmInfo(int adapter_index, int sid, uint16_t caid, uint16_t pid, uint32_t prid, uint32_t ecmtime, char *cardsystem, char *reader, char *from, char *protocol, int8_t hops)
{
cMutexLock lock(&mutex);
vector<pmtobj>::iterator it;
if (!pmt.empty())
{
for (it = pmt.begin(); it != pmt.end(); ++it)
{
if (it->sid == sid && it->adapter == adapter_index)
{
DEBUGLOG("%s: PMTO update, adapter=%d, SID=%04X", __FUNCTION__, adapter_index, sid);
it->caid = caid;
it->pid = pid;
it->prid = prid;
it->ecmtime = ecmtime;
it->cardsystem = cardsystem;
it->reader = reader;
it->from = from;
it->protocol = protocol;
it->hops = hops;
break;
}
}
}
}
bool CAPMT::FillEcmInfo(sDVBAPIEcmInfo *ecminfo)
{
cMutexLock lock(&mutex);
vector<pmtobj>::iterator it;
if (!pmt.empty())
{
for (it = pmt.begin(); it != pmt.end(); ++it)
{
if (it->sid == ecminfo->sid)
{
DEBUGLOG("%s: PMTO match - fill, adapter=%d, SID=%04X", __FUNCTION__, it->adapter, it->sid);
ecminfo->caid = it->caid;
ecminfo->pid = it->pid;
ecminfo->prid = it->prid;
ecminfo->ecmtime = it->ecmtime;
ecminfo->cardsystem = it->cardsystem;
ecminfo->reader = it->reader;
ecminfo->from = it->from;
ecminfo->protocol = it->protocol;
ecminfo->hops = it->hops;
return true;
}
}
}
return false;
}