-
Notifications
You must be signed in to change notification settings - Fork 7
/
protocol_t1.c
330 lines (277 loc) · 9.65 KB
/
protocol_t1.c
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
#include "../globals.h"
#include "../oscam-time.h"
#ifdef WITH_CARDREADER
#include "icc_async.h"
#define OK 0
#define ERROR 1
/* Buffer sizes */
#define T1_BLOCK_MAX_SIZE 259
/* Types of block */
#define T1_BLOCK_I 0x00
#define T1_BLOCK_R_OK 0x80
#define T1_BLOCK_R_EDC_ERR 0x81
#define T1_BLOCK_R_OTHER_ERR 0x82
#define T1_BLOCK_S_RESYNCH_REQ 0xC0
#define T1_BLOCK_S_RESYNCH_RES 0xE0
#define T1_BLOCK_S_IFS_REQ 0xC1
#define T1_BLOCK_S_IFS_RES 0xE1
#define T1_BLOCK_S_ABORT_REQ 0xC2
#define T1_BLOCK_S_ABORT_RES 0xE2
#define T1_BLOCK_S_WTX_REQ 0xC3
#define T1_BLOCK_S_WTX_RES 0xE3
#define T1_BLOCK_S_VPP_ERR 0xE4
#define T1_BLOCK_NAD 0x00
#define T1_Block_GetNS(a) ((a[1] >> 6) & 0x01)
#define T1_Block_GetMore(a) ((a[1] >> 5) & 0x01)
#define T1_Block_GetNR(a) ((a[1] >> 4) & 0x01)
#define T1_Block_GetLen(a) a[2]
static unsigned char T1_Block_LRC(unsigned char *data, uint32_t length)
{
unsigned char lrc = 0x00;
uint32_t i;
for(i = 0; i < length; i++)
{
lrc ^= data[i];
}
return lrc;
}
static int32_t T1_Block_SendIBlock(struct s_reader *reader, uint8_t *block_data, unsigned char len, unsigned char *inf, unsigned char ns, int32_t more, uint32_t timeout)
{
int length = len + 4;
block_data[0] = T1_BLOCK_NAD;
block_data[1] = T1_BLOCK_I | ((ns << 6) & 0x40);
if(more)
{
block_data[1] |= 0x20;
}
block_data[2] = len;
if(len != 0x00)
{
memcpy(block_data + 3, inf, len);
}
block_data[len + 3] = T1_Block_LRC(block_data, len + 3);
return ICC_Async_Transmit(reader, length, 0, block_data, 0, timeout);
}
static int32_t T1_Block_SendRBlock(struct s_reader *reader, uint8_t *block_data, unsigned char type, unsigned char nr, uint32_t timeout)
{
int length = 4;
block_data[0] = T1_BLOCK_NAD;
block_data[1] = type | ((nr << 4) & 0x10);
block_data[2] = 0x00;
block_data[3] = T1_Block_LRC(block_data, 3);
return ICC_Async_Transmit(reader, length, 0, block_data, 0, timeout);
}
static int32_t T1_Block_SendSBlock(struct s_reader *reader, uint8_t *block_data, unsigned char type, unsigned char len, unsigned char *inf, uint32_t timeout)
{
int length = 4 + len;
block_data[0] = T1_BLOCK_NAD;
block_data[1] = type;
block_data[2] = len;
if(len != 0x00)
{
memcpy(block_data + 3, inf, len);
}
block_data[len + 3] = T1_Block_LRC(block_data, len + 3);
return ICC_Async_Transmit(reader, length, 0, block_data, 0, timeout);
}
static int32_t Protocol_T1_ReceiveBlock(struct s_reader *reader, uint8_t *block_data, uint32_t *block_length, uint8_t *rsp_type, uint32_t timeout)
{
int32_t ret, length;
/* Receive four mandatory bytes */
if(ICC_Async_Receive(reader, 4, block_data, 0, timeout))
{
ret = ERROR;
}
else
{
length = block_data[2];
if(length != 0x00)
{
*block_length = (length + 4 > T1_BLOCK_MAX_SIZE) ? T1_BLOCK_MAX_SIZE : length + 4;
/* Receive remaining bytes */
if(ICC_Async_Receive(reader, *block_length - 4, block_data + 4, 0, timeout))
{
ret = ERROR;
}
else
{
ret = OK;
}
}
else
{
ret = OK;
*block_length = 4;
}
}
*rsp_type = ((block_data[1] & 0x80) == T1_BLOCK_I) ? T1_BLOCK_I : (block_data[1] & 0xEF);
return ret;
}
int32_t Protocol_T1_Command(struct s_reader *reader, unsigned char *command, uint16_t command_len, unsigned char *rsp, uint16_t *lr)
{
uint8_t block_data[T1_BLOCK_MAX_SIZE];
uint8_t rsp_type, bytes, nr, wtx;
uint16_t counter;
int32_t ret, timeout;
bool more;
uint32_t block_length = 0;
if(command[1] == T1_BLOCK_S_IFS_REQ)
{
uint8_t inf = command[3];
/* Create an IFS request S-Block */
timeout = ICC_Async_GetTimings(reader, reader->CWT); // we are going to send: CWT timeout
//cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
ret = T1_Block_SendSBlock(reader, block_data, T1_BLOCK_S_IFS_REQ, 1, &inf, timeout);
if(ret == ERROR)
{
rdr_log_dbg(reader, D_IFD, "Protocol: Sending block S(IFS request, %d)", inf);
}
/* Receive a block */
timeout = ICC_Async_GetTimings(reader, reader->BWT); // we are going to receive so set Block Waiting Timeout!
//cs_sleepus(reader->block_delay); // we were sending, now receiving so wait BGT time
ret = Protocol_T1_ReceiveBlock(reader, block_data, &block_length, &rsp_type, timeout);
if(ret == OK)
{
/* Positive IFS Response S-Block received */
if(rsp_type == T1_BLOCK_S_IFS_RES)
{
rdr_log_dbg(reader, D_IFD, "Protocol: Received block S(IFS response, %d)", block_data[3]);
}
}
return ret;
}
else if(command[1] == T1_BLOCK_S_RESYNCH_REQ)
{
/* Create an Resynch request S-Block */
timeout = ICC_Async_GetTimings(reader, reader->CWT); // we are going to send: CWT timeout
//cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
ret = T1_Block_SendSBlock(reader, block_data, T1_BLOCK_S_RESYNCH_REQ, 0, NULL, timeout);
if(ret == ERROR)
{
rdr_log_dbg(reader, D_IFD, "Protocol: Sending block S(RESYNCH request)");
}
/* Receive a block */
timeout = ICC_Async_GetTimings(reader, reader->BWT); // we are going to receive so set Block Waiting Timeout!
//cs_sleepus(reader->block_delay); // we were sending, now receiving so wait BGT time
ret = Protocol_T1_ReceiveBlock(reader, block_data, &block_length, &rsp_type, timeout);
if(ret == OK)
{
/* Positive IFS Response S-Block received */
if(rsp_type == T1_BLOCK_S_RESYNCH_RES)
{
rdr_log_dbg(reader, D_IFD, "Protocol: Received block S(RESYNCH response)");
reader->ns = 0;
}
}
return ret;
}
/* Calculate the number of bytes to send */
counter = 0;
bytes = MIN(command_len, reader->ifsc);
/* See if chaining is needed */
more = (command_len > reader->ifsc);
/* Increment ns */
reader->ns = (reader->ns == 1) ? 0 : 1; //toggle from 0 to 1 and back
/* Create an I-Block */
timeout = ICC_Async_GetTimings(reader, reader->CWT); // we are going to send: CWT timeout
//cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
ret = T1_Block_SendIBlock(reader, block_data, bytes, command, reader->ns, more, timeout);
rdr_log_dbg(reader, D_IFD, "Sending block I(%d,%d)", reader->ns, more);
while((ret == OK) && more)
{
/* Receive a block */
timeout = ICC_Async_GetTimings(reader, reader->BWT); // we are going to receive so set Block Waiting Timeout!
//cs_sleepus(reader->block_delay); // we were sending, now receiving so wait BGT time
ret = Protocol_T1_ReceiveBlock(reader, block_data, &block_length, &rsp_type, timeout);
if(ret == OK)
{
/* Positive ACK R-Block received */
if(rsp_type == T1_BLOCK_R_OK)
{
rdr_log_dbg(reader, D_IFD, "Protocol: Received block R(%d)", T1_Block_GetNR(block_data));
/* Increment ns */
reader->ns = (reader->ns == 1) ? 0 : 1; //toggle from 0 to 1 and back
/* Calculate the number of bytes to send */
counter += bytes;
bytes = MIN(command_len - counter, reader->ifsc);
/* See if chaining is needed */
more = (command_len - counter > reader->ifsc);
/* Send an I-Block */
timeout = ICC_Async_GetTimings(reader, reader->CWT); // we are going to send: CWT timeout
//cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
ret = T1_Block_SendIBlock(reader, block_data, bytes, command + counter, reader->ns, more, timeout);
rdr_log_dbg(reader, D_IFD, "Protocol: Sending block I(%d,%d)", reader->ns, more);
}
else
{
rdr_log_dbg(reader, D_TRACE, "ERROR: T1 Command %02X not implemented", rsp_type);
return ERROR;
}
}
else
{
rdr_log_dbg(reader, D_TRACE, "ERROR: T1 Command returned error");
return ERROR;
}
}
/* Reset counter */
counter = 0;
more = 1;
wtx = 1;
while((ret == OK) && more)
{
/* Receive a block */
timeout = ICC_Async_GetTimings(reader, wtx * reader->BWT); // we are going to receive so set Block Waiting Timeout!
//cs_sleepus(reader->block_delay); // we were sending, now receiving so wait BGT time
ret = Protocol_T1_ReceiveBlock(reader, block_data, &block_length, &rsp_type, timeout);
wtx = 1; // reset WTX value since its only valid for first received I block
if(ret == OK)
{
if(rsp_type == T1_BLOCK_I)
{
rdr_log_dbg(reader, D_IFD, "Protocol: Received block I(%d,%d)", T1_Block_GetNS(block_data), T1_Block_GetMore(block_data));
bytes = T1_Block_GetLen(block_data);
/* Calculate nr */
nr = (T1_Block_GetNS(block_data) + 1) % 2;
if(counter + bytes > T1_BLOCK_MAX_SIZE)
{
return ERROR;
}
memcpy(rsp + counter, block_data + 3, bytes);
counter += bytes;
/* See if chaining is requested */
more = T1_Block_GetMore(block_data);
if(more)
{
/* Send R-Block */
timeout = ICC_Async_GetTimings(reader, reader->CWT); // we are going to send: CWT timeout
//cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
ret = T1_Block_SendRBlock(reader, block_data, T1_BLOCK_R_OK, nr, timeout);
rdr_log_dbg(reader, D_IFD, "Protocol: Sending block R(%d)", nr);
}
}
else if(rsp_type == T1_BLOCK_S_WTX_REQ) /* WTX Request S-Block received */
{
/* Get wtx multiplier */
wtx = block_data[3];
rdr_log_dbg(reader, D_IFD, "Protocol: Received block S(WTX request, %d)", wtx);
/* Send an WTX response S-Block */
timeout = ICC_Async_GetTimings(reader, reader->CWT); // we are going to send: CWT timeout
//cs_sleepus(reader->block_delay); // we were receiving, now sending so wait BGT time
ret = T1_Block_SendSBlock(reader, block_data, T1_BLOCK_S_WTX_RES, 1, &wtx, timeout);
rdr_log_dbg(reader, D_IFD, "Protocol: Sending block S(WTX response, %d)", wtx);
}
else
{
rdr_log_dbg(reader, D_TRACE, "ERROR: T1 Command %02X not implemented in Receive Block", rsp_type);
ret = ERROR; //not implemented
}
}
}
if(ret == OK)
{
*lr = counter;
}
return ret;
}
#endif