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
|
/* DVB CI Transport Connection */
#include <lib/dvb_ci/dvbci_session.h>
eDVBCISession::eDVBCISession(eDVBCISlot *cislot)
{
slot = cislot;
}
int eDVBCISession::buildLengthField(unsigned char *pkt, int len)
{
if (len < 127)
{
*pkt++=len;
return 1;
} else if (len < 256)
{
*pkt++=0x81;
*pkt++=len;
return 2;
} else if (len < 65535)
{
*pkt++=0x82;
*pkt++=len>>8;
*pkt++=len;
return 3;
} else
{
printf("too big length\n");
exit(0);
}
}
int eDVBCISession::parseLengthField(const unsigned char *pkt, int &len)
{
len=0;
if (!(*pkt&0x80))
{
len = *pkt;
return 1;
}
for (int i=0; i<(pkt[0]&0x7F); ++i)
{
len <<= 8;
len |= pkt[i + 1];
}
return (pkt[0] & 0x7F) + 1;
}
void eDVBCISession::sendSPDU(eDVBCISlot *slot, unsigned char tag, const void *data, int len, unsigned short session_nb, const void *apdu,int alen)
{
unsigned char pkt[4096];
unsigned char *ptr=pkt;
*ptr++=tag;
ptr+=buildLengthField(ptr, len+2);
if (data)
memcpy(ptr, data, len);
ptr+=len;
*ptr++=session_nb>>8;
*ptr++=session_nb;
if (apdu)
memcpy(ptr, apdu, alen);
ptr+=alen;
slot->write(pkt, ptr - pkt);
}
void eDVBCISession::sendOpenSessionResponse(eDVBCISlot *slot, unsigned char session_status, const unsigned char *resource_identifier, unsigned short session_nb)
{
char pkt[6];
pkt[0]=session_status;
printf("sendOpenSessionResponse\n");
memcpy(pkt + 1, resource_identifier, 4);
sendSPDU(slot, 0x92, pkt, 5, session_nb);
}
eDVBCISession *eDVBCISession::createSession(eDVBCISlot *slot, const unsigned char *resource_identifier, unsigned char &status)
{
eDVBCISession *session;
unsigned long tag;
unsigned short session_nb;
for (session_nb=1; session_nb < SLMS; ++session_nb)
if (!sessions[session_nb-1])
break;
if (session_nb == SLMS)
{
status=0xF3;
return 0;
}
tag = resource_identifier[0] << 24;
tag|= resource_identifier[1] << 16;
tag|= resource_identifier[2] << 8;
tag|= resource_identifier[3];
switch (tag)
{
case 0x00010041:
// session=new eDVBCIResourceManagerSession;
printf("RESOURCE MANAGER\n");
break;
case 0x00020041:
// session=eDVBCIModule::getInstance()->application_manager = new eDVBCIApplicationManagerSession;
printf("APPLICATION MANAGER\n");
break;
case 0x00030041:
// session=eDVBCIModule::getInstance()->ca_manager=new eDVBCICAManagerSession;
printf("CA MANAGER\n");
break;
case 0x00240041:
// session=new eDVBCIDateTimeSession;
printf("DATE-TIME\n");
break;
case 0x00400041:
// session=new eDVBCIMMISession;
printf("MMI\n");
break;
case 0x00100041:
// session=new eDVBCIAuthSession;
printf("AuthSession\n");
break;
case 0x00200041:
default:
printf("unknown resource type %02x %02x %02x %02x\n", resource_identifier[0], resource_identifier[1], resource_identifier[2],resource_identifier[3]);
session=0;
status=0xF0;
}
if (!session)
{
printf("unknown session.. expect crash\n");
return 0;
}
printf("new session_nb: %d\n", session_nb);
session->session_nb = session_nb;
if (session)
{
printf("session ok, status %02x\n", session->status);
status = session->getStatus();
if (status)
{
delete session;
session = 0;
}
sessions[session_nb - 1] = session;
session->slot = slot;
}
session->state = stateInCreation;
return session;
}
void eDVBCISession::receiveData(const unsigned char *ptr, size_t len)
{
const unsigned char *pkt = (const unsigned char*)ptr;
unsigned char tag = *pkt++;
int llen, hlen;
llen = parseLengthField(pkt, hlen);
pkt += llen;
eDVBCISession *session = NULL;
if(tag == 0x91)
{
unsigned char status;
session = createSession(slot, pkt, status);
sendOpenSessionResponse(slot, status, pkt, session?session->session_nb:0);
if (session)
{
session->state=stateStarted;
session->action=1;
}
}
}
|