forked from manio/vdr-plugin-dvbapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCCIAdapter.cpp
398 lines (373 loc) · 10.1 KB
/
SCCIAdapter.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
* 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 <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <linux/dvb/ca.h>
#include <vdr/channels.h>
#include <vdr/ci.h>
#include <vdr/dvbdevice.h>
#include <vdr/dvbci.h>
#include <vdr/thread.h>
#include "SCCIAdapter.h"
#include "Frame.h"
#include "Log.h"
// from vdr's ci.c
#define T_CREATE_TC 0x82
#define T_RCV 0x81
#define T_DATA_LAST 0xA0
SCCIAdapter::SCCIAdapter(cDevice *Device, int cardIndex, int cafd)
{
for (int i = 0; i < MAX_SOCKETS; i++)
{
sids[i] = 0;
sockets[i] = 0;
}
this->cardIndex = cardIndex;
device = Device;
capmt = new CAPMT;
fd_ca = cafd;
decsa = new DeCSA(cardIndex);
UDPSocket::bindx(this);
memset(version, 1, sizeof(version));
memset(slots, 0, sizeof(slots));
memset(caids, 0, sizeof(caids));
caidsLength = 0;
Channels.Lock(false);
for (cChannel *channel = Channels.First(); channel; channel = Channels.Next(channel))
{
if (!channel->GroupSep() && channel->Ca() >= CA_ENCRYPTED_MIN)
{
for (const int *ids = channel->Caids(); *ids; ids++)
addCaid(0, caidsLength, (unsigned short) *ids);
}
}
Channels.Unlock();
rb = new cRingBufferLinear(KILOBYTE(8), 6 + LEN_OFF, false, "SC-CI adapter read");
if (rb)
{
rb->SetTimeouts(0, CAM_READ_TIMEOUT);
frame.SetRb(rb);
}
INFOLOG("%s: built caid table with %i caids for %i channels", __FUNCTION__, caidsLength, Channels.Count());
SetDescription("SC-CI adapter on device %d", cardIndex);
for (int i = 0; i < MAX_CI_SLOTS && i * MAX_CI_SLOT_CAIDS < caidsLength; i++)
slots[i] = new SCCAMSlot(this, cardIndex, i);
Start();
}
int SCCIAdapter::addCaid(int offset, int limit, unsigned short caid)
{
if ((caids[offset] == caid))
return offset;
if ((limit == 0) || (caidsLength == 0))
{
if ((caid > caids[offset]) && (offset < caidsLength))
offset++;
if (offset < caidsLength)
memmove(&caids[offset + 1], &caids[offset], (caidsLength - offset) * sizeof(int));
caidsLength++;
caids[offset] = caid;
return offset;
}
if (caid > caids[offset])
{
offset = offset + limit;
if (offset > caidsLength)
offset = caidsLength;
}
else if (caid < caids[offset])
{
offset = offset - limit;
if (offset < 0)
offset = 0;
}
if (limit == 1)
limit = 0;
else
limit = ceil(((float) limit) / 2.0f);
if (offset + limit > caidsLength)
offset = caidsLength - limit;
return addCaid(offset, limit, caid);
}
int SCCIAdapter::GetCaids(int slot, unsigned short *Caids, int max)
{
cMutexLock lock(&ciMutex);
if (Caids)
{
int i;
for (i = 0; i < MAX_CI_SLOT_CAIDS && i < max && slot * MAX_CI_SLOT_CAIDS + i < caidsLength && caids[slot * MAX_CI_SLOT_CAIDS + i]; i++)
Caids[i] = caids[slot * MAX_CI_SLOT_CAIDS + i];
Caids[i] = 0;
}
return version[slot];
}
int SCCIAdapter::Read(unsigned char *Buffer, int MaxLength)
{
cMutexLock lock(&ciMutex);
if (rb && Buffer && MaxLength > 0)
{
int s;
unsigned char *data = frame.Get(s);
if (data)
{
if (s <= MaxLength)
memcpy(Buffer, data, s);
else
ERRORLOG("internal: sc-ci %d rb frame size exceeded %d", cardIndex, s);
frame.Del();
if (Buffer[2] != 0x80)
readTimer.Set();
return s;
}
}
else
cCondWait::SleepMs(CAM_READ_TIMEOUT);
if (readTimer.Elapsed() > 2000)
readTimer.Set();
return 0;
}
#define TPDU(data,slot) do { unsigned char *_d=(data); _d[0]=(slot); _d[1]=tcid; } while(0)
#define TAG(data,tag,len) do { unsigned char *_d=(data); _d[0]=(tag); _d[1]=(len); } while(0)
#define SB_TAG(data,sb) do { unsigned char *_d=(data); _d[0]=0x80; _d[1]=0x02; _d[2]=tcid; _d[3]=(sb); } while(0)
void SCCIAdapter::Write(const unsigned char *buff, int len)
{
cMutexLock lock(&ciMutex);
if (buff && len >= 5)
{
struct TPDU *tpdu = (struct TPDU *) buff;
int slot = tpdu->slot;
if (slots[slot])
{
Frame *slotframe = slots[slot]->getFrame();
switch (tpdu->tag)
{
case T_RCV:
{
int s;
unsigned char *d = slotframe->Get(s);
if (d)
{
unsigned char *b;
if ((b = frame.GetBuff(s + 6)))
{
TPDU(b, slot);
memcpy(b + 2, d, s);
slotframe->Del(); // delete from rb before Avail()
SB_TAG(b + 2 + s, slotframe->Avail() > 0 ? 0x80 : 0x00);
frame.Put();
}
else
slotframe->Del();
}
break;
}
case T_CREATE_TC:
{
tcid = tpdu->data[0];
unsigned char *b;
static const unsigned char reqCAS[] = { 0xA0, 0x07, 0x01, 0x91, 0x04, 0x00, 0x03, 0x00, 0x41 };
if ((b = slotframe->GetBuff(sizeof(reqCAS))))
{
memcpy(b, reqCAS, sizeof(reqCAS));
b[2] = tcid;
slotframe->Put();
}
if ((b = frame.GetBuff(9)))
{
TPDU(b, slot);
TAG(&b[2], 0x83, 0x01);
b[4] = tcid;
SB_TAG(&b[5], slotframe->Avail() > 0 ? 0x80 : 0x00);
frame.Put();
}
break;
}
case T_DATA_LAST:
{
slots[slot]->Process(buff, len);
unsigned char *b;
if ((b = frame.GetBuff(6)))
{
TPDU(b, slot);
SB_TAG(&b[2], slotframe->Avail() > 0 ? 0x80 : 0x00);
frame.Put();
}
break;
}
}
}
}
else
DEBUGLOG("%d: short write (buff=%d len=%d)", cardIndex, buff != 0, len);
}
SCCIAdapter::~SCCIAdapter()
{
DEBUGLOG("%s", __FUNCTION__);
Cancel(3);
for (int i = 0; i < MAX_SOCKETS; i++)
{
if (sockets[i] != 0)
{
close(sockets[i]);
sockets[i] = 0;
}
}
ciMutex.Lock();
delete rb;
rb = 0;
ciMutex.Unlock();
if (decsa)
delete decsa;
if (capmt != 0)
delete capmt;
capmt = 0;
}
bool SCCIAdapter::Reset(int Slot)
{
cMutexLock lock(&ciMutex);
return true;
//return slots[Slot]->Reset();
}
eModuleStatus SCCIAdapter::ModuleStatus(int Slot)
{
cMutexLock lock(&ciMutex);
return (slots[Slot]) ? slots[Slot]->Status() : msNone;
}
bool SCCIAdapter::Assign(cDevice *Device, bool Query)
{
return Device ? (Device == device) : true;
}
void SCCIAdapter::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
*/
int i;
//for (i = 0; i < MAX_SOCKETS; i++)
//DEBUGLOG("%s: SOCKETS TABLE DUMP [%d]: sid=%d socket=%d", __FUNCTION__, i, sids[i], sockets[i]);
if (sid == 0)
{
DEBUGLOG("%s: got empty SID - returning from function", __FUNCTION__);
return;
}
if (ca_lm == 0x04 || ca_lm == 0x03) //adding new sid
{
int found = 0;
for (i = 0; i < MAX_SOCKETS; i++)
{
if (sids[i] == sid)
{
found = 1;
break;
}
}
if (found)
DEBUGLOG("%s: found sid, reusing socket, i=%d", __FUNCTION__, i);
else //not found - adding to first free in table
{
for (i = 0; i < MAX_SOCKETS; i++)
{
if (sids[i] == 0)
{
sids[i] = sid;
break;
}
}
}
if (i == MAX_SOCKETS)
{
ERRORLOG("%s: no free space for new SID!!!", __FUNCTION__);
return;
}
else
{
sids[i] = sid;
DEBUGLOG("%s: added: i=%d", __FUNCTION__, i);
}
}
else if (ca_lm == 0x05) //removing sid
{
for (i = 0; i < MAX_SOCKETS; i++)
{
if (sids[i] == sid)
break;
}
if (i == MAX_SOCKETS)
{
ERRORLOG("%s: socket to close not found", __FUNCTION__);
return;
}
//closing socket (oscam handles this as event and stop decrypting)
DEBUGLOG("%s: closing socket i=%d, socket_fd=%d", __FUNCTION__, i, sockets[i]);
sids[i] = 0;
if (sockets[i] > 0)
close(sockets[i]);
sockets[i] = 0;
return;
}
else
{
ERRORLOG("%s: unhandled ca_lm request type = %d", __FUNCTION__, ca_lm);
return;
}
sockets[i] = capmt->send(card_index, sid, sockets[i], vdr_caPMT, vdr_caPMTLen);
initialCaDscr = true;
}
bool SCCIAdapter::DeCSASetCaDescr(ca_descr_t *ca_descr)
{
DEBUGLOG("%s: index=%d", __FUNCTION__, ca_descr->index);
#ifdef WITH_UFS9XX
if (fd_ca >= 0)
{
cMutexLock lock(&cafdMutex);
return ioctl(fd_ca, CA_SET_DESCR, ca_descr) >= 0;
}
#endif
if (ca_descr->index == (unsigned) -1)
{
DEBUGLOG("%s: removal request - ignoring", __FUNCTION__);
return true;
}
bool ret = decsa->SetDescr(ca_descr, initialCaDscr);
initialCaDscr = false;
return ret;
}
bool SCCIAdapter::DeCSASetCaPid(ca_pid_t *ca_pid)
{
DEBUGLOG("%s: PID=%d, index=%d", __FUNCTION__, ca_pid->pid, ca_pid->index);
#ifdef WITH_UFS9XX
if (fd_ca >= 0)
{
cMutexLock lock(&cafdMutex);
return ioctl(fd_ca, CA_SET_PID, ca_pid) >= 0;
}
#endif
if (ca_pid->index == -1)
{
DEBUGLOG("%s: removal request - ignoring", __FUNCTION__);
return true;
}
return decsa->SetCaPid(ca_pid);
}