-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCCAMSlot.cpp
More file actions
231 lines (211 loc) · 5.92 KB
/
SCCAMSlot.cpp
File metadata and controls
231 lines (211 loc) · 5.92 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
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
#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 "SCCAMSlot.h"
// from vdr's ci.c
#define AOT_CA_INFO_ENQ 0x9F8030
#define AOT_CA_INFO 0x9F8031
#define AOT_CA_PMT 0x9f8032
SCCAMSlot::SCCAMSlot(SCCIAdapter *sCCIAdapter, int cardIndex, int slot) :cCamSlot(sCCIAdapter),checkTimer(-SLOT_CAID_CHECK-1000),rb(KILOBYTE(4),5+LEN_OFF,false,"SC-CI slot answer")
{
this->sCCIAdapter=sCCIAdapter;
this->cardIndex=cardIndex;
this->slot=slot;
version=0; caids[0]=0; doReply=false; lastStatus=msReset;
frame.SetRb(&rb);
Reset(false);
}
eModuleStatus SCCAMSlot::Status(void)
{
eModuleStatus status;
if(reset)
{
status=msReset;
if(resetTimer.TimedOut())
reset=false;
}
else if(caids[0])
status=msReady;
else
{
status=msPresent; //msNone;
Check();
}
if(status!=lastStatus)
{
static const char *stext[] = { "none","reset","present","ready" };
isyslog("DVBAPI: SCCAMSlot::Status %d.%d: status '%s'",cardIndex,slot,stext[status]);
lastStatus=status;
}
return status;
}
bool SCCAMSlot::Reset(bool log)
{
isyslog("DVBAPI: SCCAMSlot::Reset log=%i",log);
resetTimer.Set(SLOT_RESET_TIME);
rb.Clear();
// if(log) PRINTF(L_CORE_CI,"%d.%d: reset",cardIndex,slot);
reset=true;
isyslog("DVBAPI: SCCAMSlot::Reset Done");
return reset;
}
bool SCCAMSlot::Check(void)
{
// isyslog("DVBAPI: SCCAMSlot::Check");
bool res=false;
bool dr=true;//ciadapter->CamSoftCSA() || ScSetup.ConcurrentFF>0;
if(dr!=doReply && !IsDecrypting())
{
isyslog("DVBAPI: SCCAMSlot::Check %d.%d: doReply changed, reset triggered",cardIndex,slot);
Reset(false);
doReply=dr;
}
if(checkTimer.TimedOut())
{
if(version!=sCCIAdapter->GetCaids(slot,0,0))
{
version=sCCIAdapter->GetCaids(slot,caids,MAX_CI_SLOT_CAIDS);
// isyslog("DVBAPI: SCCAMSlot::Check %d.%d: now using CAIDs version %d",cardIndex,slot,version);
res=true;
}
checkTimer.Set(SLOT_CAID_CHECK);
}
return res;
}
int SCCAMSlot::GetLength(const unsigned char * &data)
{
// isyslog("DVBAPI: SCCAMSlot::GetLength");
int len=*data++;
if(len&TDPU_SIZE_INDICATOR) {
int i;
for(i=len&~TDPU_SIZE_INDICATOR, len=0; i>0; i--) len=(len<<8) + *data++;
}
return len;
}
int SCCAMSlot::LengthSize(int n)
{
// isyslog("DVBAPI: SCCAMSlot::LengthSize");
return n<TDPU_SIZE_INDICATOR?1:3;
}
void SCCAMSlot::SetSize(int n, unsigned char * &p)
{
// isyslog("DVBAPI: SCCAMSlot::SetSize");
if(n<TDPU_SIZE_INDICATOR) *p++=n;
else { *p++=2|TDPU_SIZE_INDICATOR; *p++=n>>8; *p++=n&0xFF; }
}
void SCCAMSlot::CaInfo(int tcid, int cid)
{
int cn=0;
for(int i=0; caids[i]; i++)
cn+=2;
int n=cn+8+LengthSize(cn);
unsigned char *p;
if(!(p=frame.GetBuff(n+1+LengthSize(n))))
return;
*p++=0xa0;
SetSize(n,p);
*p++=tcid;
*p++=0x90;
*p++=0x02;
*p++=cid>>8;
*p++=cid&0xff;
*p++=0x9f;
*p++=0x80;
*p++=(unsigned char)AOT_CA_INFO;
SetSize(cn,p);
for(int i=0; caids[i]; i++)
{
*p++=caids[i]>>8;
*p++=caids[i]&0xff;
}
frame.Put();
isyslog("DVBAPI: SCCAMSlot::CaInfo %i.%i sending CA info",cardIndex,slot);
}
void SCCAMSlot::Process(const unsigned char *data, int len)
{
const unsigned char *save=data;
data+=3;
int dlen=GetLength(data);
if(dlen>len-(data-save)) {
isyslog("DVBAPI: SCCAMSlot::Process %d.%d TDPU length exceeds data length",cardIndex,slot);
dlen=len-(data-save);
}
int tcid=data[0];
// isyslog("DVBAPI: SCCAMSlot::Process len=%d tcid=%i",len,tcid);
if(Check())
CaInfo(tcid,0x01);
if(dlen<8 || data[1]!=0x90)
return;
int cid=(data[3]<<8)+data[4];
int tag=(data[5]<<16)+(data[6]<<8)+data[7];
data+=8;
dlen=GetLength(data);
if(dlen>len-(data-save)) {
isyslog("DVBAPI: SCCAMSlot::Process %d.%d tag length exceeds data length",cardIndex,slot);
dlen=len-(data-save);
}
switch(tag) {
case AOT_CA_INFO_ENQ:
CaInfo(tcid,cid);
break;
case AOT_CA_PMT:
if(dlen>=6) {
int ca_lm=data[0];
int ci_cmd=-1;
int sid=(data[1]<<8)+data[2];
int ilen=(data[4]<<8)+data[5];
isyslog("DVBAPI: SCCAMSlot::Process %d.%d CA_PMT decoding len=%x lm=%x prg=%d len=%x",cardIndex,slot,dlen,ca_lm,sid,ilen);
data+=6; dlen-=6;
if(ilen>0 && dlen>=ilen) {
ci_cmd=data[0];
}
data+=ilen; dlen-=ilen;
while(dlen>=5) {
ilen=(data[3]<<8)+data[4];
data+=5; dlen-=5;
if(ilen>0 && dlen>=ilen) {
ci_cmd=data[0];
}
data+=ilen; dlen-=ilen;
}
isyslog("DVBAPI: SCCAMSlot::Process %d.%d got CA pmt ciCmd=%d caLm=%d",cardIndex,slot,ci_cmd,ca_lm);
if(doReply && (ci_cmd==0x03 || (ci_cmd==0x01 && ca_lm==0x03))) {
unsigned char *b;
if((b=frame.GetBuff(4+11))) {
b[0]=0xa0; b[2]=tcid;
b[3]=0x90;
b[4]=0x02; b[5]=cid<<8; b[6]=cid&0xff;
b[7]=0x9f; b[8]=0x80; b[9]=0x33; // AOT_CA_PMT_REPLY
b[11]=sid<<8;
b[12]=sid&0xff;
b[13]=0x00;
b[14]=0x81; // CA_ENABLE
b[10]=4; b[1]=4+9;
frame.Put();
isyslog("DVBAPI: SCCAMSlot::Process %d.%d answer to query",cardIndex,slot);
}
}
if(sid!=0) {
if(ci_cmd==0x04) {
isyslog("DVBAPI: SCCAMSlot::Process %d.%d stop decrypt",cardIndex,slot);
}
if(ci_cmd==0x01 || (ci_cmd==-1 && (ca_lm==0x04 || ca_lm==0x05))) {
isyslog("DVBAPI: SCCAMSlot::Process %d.%d set CAM decrypt (SID %d)",cardIndex,slot,sid);
sCCIAdapter->GetDevice()->GetCAPMT()->send(sid);
sCCIAdapter->GetDevice()->SetReady(true);
}
}
}
break;
}
}