source: branches/monitor-improvement/csctapi/icc_async.c@ 1189

Last change on this file since 1189 was 1189, checked in by alno, 12 years ago

WebIf:

  • Merging revisions 1180-1185 of trunk
File size: 10.2 KB
Line 
1/*
2 icc_async.c
3 Asynchronous ICC's handling functions
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
7
8 This version is modified by doz21 to work in a special manner ;)
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25#include "defines.h"
26#include "../globals.h"
27#include "icc_async.h"
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include "ifd.h"
32
33/*
34 * Not exported constants definition
35 */
36#define ICC_ASYNC_MAX_TRANSMIT 255
37#define ICC_ASYNC_BAUDRATE 9600
38
39/*
40 * Not exported functions declaration
41 */
42
43static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
44static void ICC_Async_Clear (ICC_Async * icc);
45
46/*
47 * Exported functions definition
48 */
49
50ICC_Async *ICC_Async_New (void)
51{
52 ICC_Async *icc;
53
54 /* Allocate memory */
55 icc = (ICC_Async *) malloc (sizeof (ICC_Async));
56
57 if (icc != NULL)
58 ICC_Async_Clear (icc);
59
60 return icc;
61}
62
63int ICC_Async_Init (ICC_Async * icc, IFD * ifd)
64{
65#ifndef ICC_TYPE_SYNC
66 unsigned np=0;
67
68 /* LED Red */
69 if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
70 return ICC_ASYNC_IFD_ERROR;
71
72 /* Initialize Baudrate */
73 if (IFD_Towitoko_SetBaudrate (ifd, ICC_ASYNC_BAUDRATE)!= IFD_TOWITOKO_OK)
74 return ICC_ASYNC_IFD_ERROR;
75
76 /* Activate ICC */
77 if (IFD_Towitoko_ActivateICC (ifd) != IFD_TOWITOKO_OK)
78 return ICC_ASYNC_IFD_ERROR;
79 /* Reset ICC */
80#ifdef COOL
81 if (ifd->io->com == RTYP_SCI) {
82 if (!Cool_Reset(&(icc->atr)))
83 {
84 icc->atr = NULL;
85 return ICC_ASYNC_IFD_ERROR;
86 }
87 }
88 else
89#endif
90 if (IFD_Towitoko_ResetAsyncICC (ifd, &(icc->atr)) != IFD_TOWITOKO_OK)
91 {
92 icc->atr = NULL;
93 return ICC_ASYNC_IFD_ERROR;
94 }
95 /* Get ICC convention */
96 if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
97 {
98 ATR_Delete (icc->atr);
99 icc->atr = NULL;
100 icc->convention = 0;
101
102 return ICC_ASYNC_ATR_ERROR;
103 }
104
105 icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
106
107 ATR_GetNumberOfProtocols (icc->atr, &np);
108
109 /*
110 * Get protocol offered by interface bytes T*2 if available,
111 * (that is, if TD1 is available), * otherwise use default T=0
112 */
113/* if (np>1)
114 ATR_GetProtocolType (icc->atr, 1, &(icc->protocol_type));
115
116#ifdef DEBUG_ICC
117 printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
118#endif
119 *///really should let PPS handle this
120 /* LED Green */
121 if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
122 {
123 ATR_Delete (icc->atr);
124 icc->atr = NULL;
125 icc->convention = 0;
126
127 return ICC_ASYNC_IFD_ERROR;
128 }
129
130 /* Initialize member variables */
131 icc->baudrate = ICC_ASYNC_BAUDRATE;
132 icc->ifd = ifd;
133
134#ifdef NO_PAR_SWITCH
135 if (icc->convention == ATR_CONVENTION_INVERSE)
136 {
137 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
138 return ICC_ASYNC_IFD_ERROR;
139 }
140 else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
141 {
142 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
143 return ICC_ASYNC_IFD_ERROR;
144 }
145 else
146 {
147 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
148 return ICC_ASYNC_IFD_ERROR;
149 }
150#ifdef COOL
151 if (icc->ifd->io->com != RTYP_SCI)
152#endif
153 IO_Serial_Flush(ifd->io);
154#endif
155 return ICC_ASYNC_OK;
156#else
157 return ICC_ASYNC_ATR_ERROR;
158#endif
159}
160
161int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
162{
163 icc->timings.block_delay = timings->block_delay;
164 icc->timings.char_delay = timings->char_delay;
165 icc->timings.block_timeout = timings->block_timeout;
166 icc->timings.char_timeout = timings->char_timeout;
167/* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
168 cs_debug("SetTimings: T1: chardelay %d, chartimeout CWT %d, blockdelay BGT??? %d, blocktimeout BWT %d",timings->char_delay,timings->char_timeout, timings->block_delay, timings->block_timeout);
169 else
170 cs_debug("SetTimings: T0/T14: chardelay %d, chartimeout WWT %d, blockdelay %d, blocktimeout %d",timings->char_delay,timings->char_timeout, timings->block_delay, timings->block_timeout);*/
171
172#ifdef SCI_DEV
173#include <sys/ioctl.h>
174#include "sci_global.h"
175#include "sci_ioctl.h"
176 if (icc->ifd->io->com == RTYP_SCI) {
177 SCI_PARAMETERS params;
178 if (ioctl(icc->ifd->io->fd, IOCTL_GET_PARAMETERS, &params) < 0 )
179 return ICC_ASYNC_IFD_ERROR;
180 switch (icc->protocol_type) {
181 case ATR_PROTOCOL_TYPE_T1:
182 params.BWT = icc->timings.block_timeout;
183 params.CWT = icc->timings.char_timeout;
184 //params.BGT = icc->timings.block_delay; load into params.EGT??
185 break;
186 case ATR_PROTOCOL_TYPE_T0:
187 case ATR_PROTOCOL_TYPE_T14:
188 default:
189 params.WWT = icc->timings.char_timeout;
190 break;
191 }
192 if (ioctl(icc->ifd->io->fd, IOCTL_SET_PARAMETERS, &params)!=0)
193 return ICC_ASYNC_IFD_ERROR;
194
195 cs_debug("Set Timings: T=%d fs=%lu ETU=%d WWT=%d CWT=%d BWT=%d EGT=%d clock=%d check=%d P=%d I=%d U=%d", (int)params.T, params.fs, (int)params.ETU, (int)params.WWT, (int)params.CWT, (int)params.BWT, (int)params.EGT, (int)params.clock_stop_polarity, (int)params.check, (int)params.P, (int)params.I, (int)params.U);
196 }
197#endif
198 return ICC_ASYNC_OK;
199}
200
201int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
202{
203 timings->block_delay = icc->timings.block_delay;
204 timings->char_delay = icc->timings.char_delay;
205 timings->block_timeout = icc->timings.block_timeout;
206 timings->char_timeout = icc->timings.char_timeout;
207
208 return ICC_ASYNC_OK;
209}
210
211int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
212{
213 icc->baudrate = baudrate;
214/*#ifdef COOL
215 if (icc->ifd->io->com==RTYP_SCI) {
216 typedef unsigned long u_int32;
217 u_int32 clk;
218 //clk = 357*10000; // MHZ
219 //clk = baudrate * 3570000L / 9600L;
220 clk = 500*10000;
221 if (cnxt_smc_set_clock_freq(icc->ifd->io->handle, clk))
222 return ICC_ASYNC_IFD_ERROR;
223 printf("set clock to %lu Hz\n", clk);
224 return ICC_ASYNC_OK;
225 }
226 else
227#endif*/
228 if (IFD_Towitoko_SetBaudrate (icc->ifd, baudrate) != IFD_TOWITOKO_OK)
229 return ICC_ASYNC_IFD_ERROR;
230
231 return ICC_ASYNC_OK;
232}
233
234int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
235{
236 (*baudrate) = icc->baudrate;
237 return ICC_ASYNC_OK;
238}
239
240int ICC_Async_BeginTransmission (ICC_Async * icc)
241{
242 /* Setup parity for this ICC */
243#ifndef NO_PAR_SWITCH
244 if (icc->convention == ATR_CONVENTION_INVERSE)
245 {
246 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
247 return ICC_ASYNC_IFD_ERROR;
248 }
249 else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
250 {
251 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
252 return ICC_ASYNC_IFD_ERROR;
253 }
254 else
255 {
256 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
257 return ICC_ASYNC_IFD_ERROR;
258 }
259
260 /* Setup baudrate for this ICC */
261/* if (IFD_Towitoko_SetBaudrate (icc->ifd, icc->baudrate)!= IFD_TOWITOKO_OK)
262 return ICC_ASYNC_IFD_ERROR;
263*/
264#endif
265 return ICC_ASYNC_OK;
266}
267
268int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
269{
270 BYTE *buffer = NULL, *sent;
271 IFD_Timings timings;
272
273 if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
274 {
275 buffer = (BYTE *) calloc(sizeof (BYTE), size);
276 memcpy (buffer, data, size);
277 ICC_Async_InvertBuffer (size, buffer);
278 sent = buffer;
279 }
280 else
281 {
282 sent = data;
283 }
284
285 timings.block_delay = icc->timings.block_delay;
286 timings.char_delay = icc->timings.char_delay;
287
288#ifdef COOL
289 if (icc->ifd->io->com == RTYP_SCI) {
290 if (!Cool_Transmit(sent, size))
291 return ICC_ASYNC_IFD_ERROR;
292 }
293 else
294#endif
295 if (IFD_Towitoko_Transmit (icc->ifd, &timings, size, sent) != IFD_TOWITOKO_OK)
296 return ICC_ASYNC_IFD_ERROR;
297
298 if (icc->convention == ATR_CONVENTION_INVERSE)
299 free (buffer);
300
301 return ICC_ASYNC_OK;
302}
303
304int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
305{
306 IFD_Timings timings;
307
308 timings.block_timeout = icc->timings.block_timeout;
309 timings.char_timeout = icc->timings.char_timeout;
310
311#ifdef COOL
312 if (icc->ifd->io->com == RTYP_SCI) {
313 if (!Cool_Receive(data, size))
314 return ICC_ASYNC_IFD_ERROR;
315 }
316 else
317#else
318 if (IFD_Towitoko_Receive (icc->ifd, &timings, size, data) != IFD_TOWITOKO_OK)
319 return ICC_ASYNC_IFD_ERROR;
320#endif
321
322 if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
323 ICC_Async_InvertBuffer (size, data);
324
325 return ICC_ASYNC_OK;
326}
327
328int ICC_Async_EndTransmission (ICC_Async * icc)
329{
330#ifndef NO_PAR_SWITCH
331 /* Restore parity */
332 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
333 return ICC_ASYNC_IFD_ERROR;
334#endif
335
336 return ICC_ASYNC_OK;
337}
338
339ATR * ICC_Async_GetAtr (ICC_Async * icc)
340{
341 return icc->atr;
342}
343
344IFD * ICC_Async_GetIFD (ICC_Async * icc)
345{
346 return icc->ifd;
347}
348
349int ICC_Async_Close (ICC_Async * icc)
350{
351 /* Dectivate ICC */
352 if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
353 return ICC_ASYNC_IFD_ERROR;
354
355 /* LED Off */
356 if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
357 return ICC_ASYNC_IFD_ERROR;
358
359 /* Delete atr */
360 ATR_Delete (icc->atr);
361
362 ICC_Async_Clear (icc);
363
364 return ICC_ASYNC_OK;
365}
366
367unsigned long ICC_Async_GetClockRate (ICC_Async * icc)
368{
369 switch (icc->ifd->io->cardmhz) {
370 case 357:
371 case 358:
372 return (372L * 9600L);
373 case 368:
374 return (384L * 9600L);
375 default:
376 return icc->ifd->io->cardmhz * 10000L;
377 }
378}
379
380void ICC_Async_Delete (ICC_Async * icc)
381{
382 free (icc);
383}
384
385/*
386 * Not exported functions definition
387 */
388
389static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
390{
391 uint i;
392
393 for (i = 0; i < size; i++)
394 buffer[i] = ~(INVERT_BYTE (buffer[i]));
395}
396
397static void ICC_Async_Clear (ICC_Async * icc)
398{
399 icc->ifd = NULL;
400 icc->atr = NULL;
401 icc->baudrate = 0L;
402 icc->convention = 0;
403 icc->protocol_type = -1;
404 icc->timings.block_delay = 0;
405 icc->timings.char_delay = 0;
406 icc->timings.block_timeout = 0;
407 icc->timings.char_timeout = 0;
408}
Note: See TracBrowser for help on using the repository browser.