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 "icc_async.h"
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <stdio.h>
|
---|
29 | #include <string.h>
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * Not exported constants definition
|
---|
33 | */
|
---|
34 | #define ICC_ASYNC_MAX_TRANSMIT 255
|
---|
35 | #define ICC_ASYNC_BAUDRATE 9600
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Not exported functions declaration
|
---|
39 | */
|
---|
40 |
|
---|
41 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
|
---|
42 | static void ICC_Async_Clear (ICC_Async * icc);
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Exported functions definition
|
---|
46 | */
|
---|
47 |
|
---|
48 | ICC_Async *ICC_Async_New (void)
|
---|
49 | {
|
---|
50 | ICC_Async *icc;
|
---|
51 |
|
---|
52 | /* Allocate memory */
|
---|
53 | icc = (ICC_Async *) malloc (sizeof (ICC_Async));
|
---|
54 |
|
---|
55 | if (icc != NULL)
|
---|
56 | ICC_Async_Clear (icc);
|
---|
57 |
|
---|
58 | return icc;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int ICC_Async_Init (ICC_Async * icc, IFD * ifd)
|
---|
62 | {
|
---|
63 | #ifndef ICC_TYPE_SYNC
|
---|
64 | unsigned np=0;
|
---|
65 |
|
---|
66 | /* LED Red */
|
---|
67 | if (IFD_Towitoko_SetLED (ifd, IFD_TOWITOKO_LED_RED) != IFD_TOWITOKO_OK)
|
---|
68 | return ICC_ASYNC_IFD_ERROR;
|
---|
69 |
|
---|
70 | /* Initialize Baudrate */
|
---|
71 | // if (IFD_Towitoko_SetBaudrate (ifd, ICC_ASYNC_BAUDRATE)!= IFD_TOWITOKO_OK)
|
---|
72 | if (IFD_Towitoko_SetBaudrate (ifd, 9600)!= IFD_TOWITOKO_OK)
|
---|
73 | return ICC_ASYNC_IFD_ERROR;
|
---|
74 |
|
---|
75 | /* Activate ICC */
|
---|
76 | if (IFD_Towitoko_ActivateICC (ifd) != IFD_TOWITOKO_OK)
|
---|
77 | return ICC_ASYNC_IFD_ERROR;
|
---|
78 | /* Reset ICC */
|
---|
79 | if (IFD_Towitoko_ResetAsyncICC (ifd, &(icc->atr)) != IFD_TOWITOKO_OK)
|
---|
80 | {
|
---|
81 | icc->atr = NULL;
|
---|
82 | return ICC_ASYNC_IFD_ERROR;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /* Get ICC convention */
|
---|
86 | if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
|
---|
87 | {
|
---|
88 | ATR_Delete (icc->atr);
|
---|
89 | icc->atr = NULL;
|
---|
90 | icc->convention = 0;
|
---|
91 |
|
---|
92 | return ICC_ASYNC_ATR_ERROR;
|
---|
93 | }
|
---|
94 |
|
---|
95 | icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
|
---|
96 |
|
---|
97 | ATR_GetNumberOfProtocols (icc->atr, &np);
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * Get protocol offered by interface bytes T*2 if available,
|
---|
101 | * (that is, if TD1 is available), * otherwise use default T=0
|
---|
102 | */
|
---|
103 | if (np>1)
|
---|
104 | ATR_GetProtocolType (icc->atr, 2, &(icc->protocol_type));
|
---|
105 |
|
---|
106 | #ifdef DEBUG_ICC
|
---|
107 | printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | /* LED Green */
|
---|
111 | if (IFD_Towitoko_SetLED (ifd, IFD_TOWITOKO_LED_GREEN) != IFD_TOWITOKO_OK)
|
---|
112 | {
|
---|
113 | ATR_Delete (icc->atr);
|
---|
114 | icc->atr = NULL;
|
---|
115 | icc->convention = 0;
|
---|
116 |
|
---|
117 | return ICC_ASYNC_IFD_ERROR;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Initialize member variables */
|
---|
121 | icc->baudrate = ICC_ASYNC_BAUDRATE;
|
---|
122 | icc->ifd = ifd;
|
---|
123 |
|
---|
124 | #ifdef NO_PAR_SWITCH
|
---|
125 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
126 | {
|
---|
127 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
|
---|
128 | return ICC_ASYNC_IFD_ERROR;
|
---|
129 | }
|
---|
130 | else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
131 | {
|
---|
132 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
133 | return ICC_ASYNC_IFD_ERROR;
|
---|
134 | }
|
---|
135 | else
|
---|
136 | {
|
---|
137 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
|
---|
138 | return ICC_ASYNC_IFD_ERROR;
|
---|
139 | }
|
---|
140 | IO_Serial_Flush(ifd->io);
|
---|
141 | #endif
|
---|
142 | return ICC_ASYNC_OK;
|
---|
143 | #else
|
---|
144 | return ICC_ASYNC_ATR_ERROR;
|
---|
145 | #endif
|
---|
146 | }
|
---|
147 |
|
---|
148 | int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
|
---|
149 | {
|
---|
150 | icc->timings.block_delay = timings->block_delay;
|
---|
151 | icc->timings.char_delay = timings->char_delay;
|
---|
152 | icc->timings.block_timeout = timings->block_timeout;
|
---|
153 | icc->timings.char_timeout = timings->char_timeout;
|
---|
154 |
|
---|
155 | return ICC_ASYNC_OK;
|
---|
156 | }
|
---|
157 |
|
---|
158 | int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
|
---|
159 | {
|
---|
160 | timings->block_delay = icc->timings.block_delay;
|
---|
161 | timings->char_delay = icc->timings.char_delay;
|
---|
162 | timings->block_timeout = icc->timings.block_timeout;
|
---|
163 | timings->char_timeout = icc->timings.char_timeout;
|
---|
164 |
|
---|
165 | return ICC_ASYNC_OK;
|
---|
166 | }
|
---|
167 |
|
---|
168 | int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
|
---|
169 | {
|
---|
170 | icc->baudrate = baudrate;
|
---|
171 |
|
---|
172 | return ICC_ASYNC_OK;
|
---|
173 | }
|
---|
174 |
|
---|
175 | int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
|
---|
176 | {
|
---|
177 | (*baudrate) = icc->baudrate;
|
---|
178 |
|
---|
179 | return ICC_ASYNC_OK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | int ICC_Async_BeginTransmission (ICC_Async * icc)
|
---|
183 | {
|
---|
184 | /* Setup parity for this ICC */
|
---|
185 | #ifndef NO_PAR_SWITCH
|
---|
186 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
187 | {
|
---|
188 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
|
---|
189 | return ICC_ASYNC_IFD_ERROR;
|
---|
190 | }
|
---|
191 | else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
192 | {
|
---|
193 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
194 | return ICC_ASYNC_IFD_ERROR;
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
|
---|
199 | return ICC_ASYNC_IFD_ERROR;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /* Setup baudrate for this ICC */
|
---|
203 | /* if (IFD_Towitoko_SetBaudrate (icc->ifd, icc->baudrate)!= IFD_TOWITOKO_OK)
|
---|
204 | return ICC_ASYNC_IFD_ERROR;
|
---|
205 | */
|
---|
206 | #endif
|
---|
207 | return ICC_ASYNC_OK;
|
---|
208 | }
|
---|
209 |
|
---|
210 | int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
|
---|
211 | {
|
---|
212 | BYTE *buffer = NULL, *sent;
|
---|
213 | IFD_Timings timings;
|
---|
214 |
|
---|
215 | if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
|
---|
216 | {
|
---|
217 | buffer = (BYTE *) calloc(sizeof (BYTE), size);
|
---|
218 | memcpy (buffer, data, size);
|
---|
219 | ICC_Async_InvertBuffer (size, buffer);
|
---|
220 | sent = buffer;
|
---|
221 | }
|
---|
222 | else
|
---|
223 | {
|
---|
224 | sent = data;
|
---|
225 | }
|
---|
226 |
|
---|
227 | timings.block_delay = icc->timings.block_delay;
|
---|
228 | timings.char_delay = icc->timings.char_delay;
|
---|
229 |
|
---|
230 | if (IFD_Towitoko_Transmit (icc->ifd, &timings, size, sent) != IFD_TOWITOKO_OK)
|
---|
231 | return ICC_ASYNC_IFD_ERROR;
|
---|
232 |
|
---|
233 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
234 | free (buffer);
|
---|
235 |
|
---|
236 | return ICC_ASYNC_OK;
|
---|
237 | }
|
---|
238 |
|
---|
239 | int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
|
---|
240 | {
|
---|
241 | IFD_Timings timings;
|
---|
242 |
|
---|
243 | timings.block_timeout = icc->timings.block_timeout;
|
---|
244 | timings.char_timeout = icc->timings.char_timeout;
|
---|
245 |
|
---|
246 | if (IFD_Towitoko_Receive (icc->ifd, &timings, size, data) != IFD_TOWITOKO_OK)
|
---|
247 | return ICC_ASYNC_IFD_ERROR;
|
---|
248 |
|
---|
249 | if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
|
---|
250 | ICC_Async_InvertBuffer (size, data);
|
---|
251 |
|
---|
252 | return ICC_ASYNC_OK;
|
---|
253 | }
|
---|
254 |
|
---|
255 | int ICC_Async_EndTransmission (ICC_Async * icc)
|
---|
256 | {
|
---|
257 | #ifndef NO_PAR_SWITCH
|
---|
258 | /* Restore parity */
|
---|
259 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
260 | return ICC_ASYNC_IFD_ERROR;
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | return ICC_ASYNC_OK;
|
---|
264 | }
|
---|
265 |
|
---|
266 | ATR * ICC_Async_GetAtr (ICC_Async * icc)
|
---|
267 | {
|
---|
268 | return icc->atr;
|
---|
269 | }
|
---|
270 |
|
---|
271 | IFD * ICC_Async_GetIFD (ICC_Async * icc)
|
---|
272 | {
|
---|
273 | return icc->ifd;
|
---|
274 | }
|
---|
275 |
|
---|
276 | int ICC_Async_Close (ICC_Async * icc)
|
---|
277 | {
|
---|
278 | /* Dectivate ICC */
|
---|
279 | if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
|
---|
280 | return ICC_ASYNC_IFD_ERROR;
|
---|
281 |
|
---|
282 | /* LED Off */
|
---|
283 | if (IFD_Towitoko_SetLED (icc->ifd, IFD_TOWITOKO_LED_OFF) != IFD_TOWITOKO_OK)
|
---|
284 | return ICC_ASYNC_IFD_ERROR;
|
---|
285 |
|
---|
286 | /* Delete atr */
|
---|
287 | ATR_Delete (icc->atr);
|
---|
288 |
|
---|
289 | ICC_Async_Clear (icc);
|
---|
290 |
|
---|
291 | return ICC_ASYNC_OK;
|
---|
292 | }
|
---|
293 |
|
---|
294 | unsigned long ICC_Async_GetClockRate (ICC_Async * icc)
|
---|
295 | {
|
---|
296 | return IFD_Towitoko_GetClockRate(icc->ifd);
|
---|
297 | }
|
---|
298 |
|
---|
299 | void ICC_Async_Delete (ICC_Async * icc)
|
---|
300 | {
|
---|
301 | free (icc);
|
---|
302 | }
|
---|
303 |
|
---|
304 | /*
|
---|
305 | * Not exported functions definition
|
---|
306 | */
|
---|
307 |
|
---|
308 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
|
---|
309 | {
|
---|
310 | int i;
|
---|
311 |
|
---|
312 | for (i = 0; i < size; i++)
|
---|
313 | buffer[i] = ~(INVERT_BYTE (buffer[i]));
|
---|
314 | }
|
---|
315 |
|
---|
316 | static void ICC_Async_Clear (ICC_Async * icc)
|
---|
317 | {
|
---|
318 | icc->ifd = NULL;
|
---|
319 | icc->atr = NULL;
|
---|
320 | icc->baudrate = 0L;
|
---|
321 | icc->convention = 0;
|
---|
322 | icc->protocol_type = -1;
|
---|
323 | icc->timings.block_delay = 0;
|
---|
324 | icc->timings.char_delay = 0;
|
---|
325 | icc->timings.block_timeout = 0;
|
---|
326 | icc->timings.char_timeout = 0;
|
---|
327 | }
|
---|