source: branches/monitor-improvement/csctapi/ct_slot.c@ 1257

Last change on this file since 1257 was 1257, checked in by alno, 11 years ago

WebIf:

  • Merged Rev 1252-1253 of trunk
File size: 8.0 KB
Line 
1/*
2 ct_slot.c
3 Card Terminal Slot handling functions
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 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 "ct_slot.h"
27#include "ifd_towitoko.h"
28#include "icc_async.h"
29#include "protocol_t0.h"
30#include "protocol_t1.h"
31#include "pps.h"
32#include <stdlib.h>
33#include <unistd.h>
34#include <string.h>
35#include <time.h>
36#ifdef HAVE_SYS_TIME_H
37#include <sys/time.h>
38#endif
39
40/* Try first asynchronous init and if it fails try synchronous */
41//#undef ICC_PROBE_ASYNC_FIRST
42//#define ICC_PROBE_ASYNC_FIRST
43
44/*
45 * Not exported functions declaration
46 */
47
48static void CT_Slot_Clear (CT_Slot * slot);
49
50/*
51 * Exported functions definition
52 */
53
54CT_Slot * CT_Slot_New ()
55{
56 CT_Slot *slot;
57
58 slot = (CT_Slot *) malloc (sizeof (CT_Slot));
59
60 if (slot != NULL)
61 CT_Slot_Clear (slot);
62
63 return slot;
64}
65
66char CT_Slot_Init (CT_Slot * slot, int sn)
67{
68 slot->ifd = IFD_Towitoko_New();
69
70 if (slot->ifd == NULL)
71 return ERR_MEMORY;
72
73 if (IFD_Towitoko_Init (slot->ifd, sn) != IFD_TOWITOKO_OK)
74 {
75 IFD_Towitoko_Delete (slot->ifd);
76 slot->ifd = NULL;
77 return ERR_TRANS;
78 }
79
80 return OK;
81}
82
83char CT_Slot_Check (CT_Slot * slot, uint timeout, bool * card, bool * change)
84{
85 BYTE status;
86#ifdef HAVE_NANOSLEEP
87 struct timespec req_ts;
88
89 req_ts.tv_sec = 1;
90 req_ts.tv_nsec = 0;
91#endif
92
93 /* Do first time check */
94 if (ICC_Async_GetStatus (&status) != ICC_ASYNC_OK)
95 {
96 return ERR_TRANS;
97 }
98
99 (*change) = IFD_TOWITOKO_CHANGE (status);
100
101 while ((timeout > 0) && (!IFD_TOWITOKO_CARD (status)))
102 {
103 timeout --;
104
105#ifdef HAVE_NANOSLEEP
106 /* Sleep one second */
107 nanosleep (&req_ts,NULL);
108#else
109 usleep (1000);
110#endif
111
112 if (ICC_Async_GetStatus (&status) != ICC_ASYNC_OK)
113 return ERR_TRANS;
114
115 (*change) |= IFD_TOWITOKO_CHANGE (status);
116 }
117
118 (*card) = IFD_TOWITOKO_CARD (status);
119
120 return OK;
121}
122
123char CT_Slot_Probe (CT_Slot * slot, BYTE * userdata, unsigned length)
124{
125 PPS * pps;
126 BYTE buffer[PPS_MAX_LENGTH];
127 unsigned buffer_len = 0;
128
129 /* Initiaice ICC */
130 slot->icc = ICC_Async_New ();
131
132 if (slot->icc == NULL)
133 return ERR_MEMORY;
134
135 if (ICC_Async_Init (slot->icc, slot->ifd) != ICC_ASYNC_OK)
136 {
137 ICC_Async_Delete (slot->icc);
138
139 slot->icc = NULL;
140 slot->icc_type = CT_SLOT_NULL;
141 return ERR_TRANS;
142
143 /* Synchronous card present */
144// slot->icc_type = CT_SLOT_ICC_SYNC;
145 }
146 else
147 {
148 /* Asyncronous card present */
149 slot->icc_type = CT_SLOT_ICC_ASYNC;
150 }
151
152
153 /* Initialise protocol */
154 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
155 {
156 pps = PPS_New((ICC_Async *) slot->icc);
157
158 if (pps == NULL)
159 {
160 ICC_Async_Close ((ICC_Async *) slot->icc);
161 ICC_Async_Delete ((ICC_Async *) slot->icc);
162
163 slot->icc = NULL;
164 slot->icc_type = CT_SLOT_NULL;
165 return ERR_MEMORY;
166 }
167
168 /* Prepare PPS request */
169 if ((userdata != NULL) && (length > 0))
170 memcpy (buffer, userdata, buffer_len = MIN(length, PPS_MAX_LENGTH));
171
172 /* Do PPS */
173 if (PPS_Perform (pps, buffer, &buffer_len) != PPS_OK)
174 {
175 PPS_Delete (pps);
176
177 ICC_Async_Close ((ICC_Async *) slot->icc);
178 ICC_Async_Delete ((ICC_Async *) slot->icc);
179
180 slot->icc = NULL;
181 slot->icc_type = CT_SLOT_NULL;
182 slot->protocol_type = CT_SLOT_NULL;
183
184 return ERR_TRANS;
185 }
186
187 slot->protocol_type = (PPS_GetProtocolParameters (pps))->t;
188 slot->protocol = PPS_GetProtocol (pps);
189
190
191 PPS_Delete (pps);
192 }
193
194 return OK;
195}
196
197char CT_Slot_Release (CT_Slot * slot)
198{
199 char ret;
200
201 ret = OK;
202
203 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0)
204 {
205 if (Protocol_T0_Close ((Protocol_T0 *) slot->protocol) != PROTOCOL_T0_OK)
206 ret = ERR_TRANS;
207
208 Protocol_T0_Delete ((Protocol_T0 *) slot->protocol);
209 }
210 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1)
211 {
212 if (Protocol_T1_Close ((Protocol_T1 *) slot->protocol) != PROTOCOL_T1_OK)
213 ret = ERR_TRANS;
214
215 Protocol_T1_Delete ((Protocol_T1 *) slot->protocol);
216 }
217 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14)
218 {
219 if (Protocol_T14_Close ((Protocol_T14 *) slot->protocol) != PROTOCOL_T14_OK)
220 ret = ERR_TRANS;
221
222 Protocol_T14_Delete ((Protocol_T14 *) slot->protocol);
223 }
224
225 slot->protocol = NULL;
226 slot->protocol_type = CT_SLOT_NULL;
227
228 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
229 {
230 if (ICC_Async_Close ((ICC_Async *) slot->icc) != ICC_ASYNC_OK)
231 ret = ERR_TRANS;
232
233 ICC_Async_Delete ((ICC_Async *) slot->icc);
234 }
235
236 slot->icc = NULL;
237 slot->icc_type = CT_SLOT_NULL;
238
239 return ret;
240}
241
242char CT_Slot_Command (CT_Slot * slot, APDU_Cmd * cmd, APDU_Rsp ** rsp)
243{
244 BYTE buffer[2];
245 char ret;
246
247 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0) /* T=0 protocol ICC */
248 {
249 if (Protocol_T0_Command ((Protocol_T0 *) slot->protocol, cmd, rsp) != PROTOCOL_T0_OK)
250 ret = ERR_TRANS;
251 else
252 ret = OK;
253 }
254 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1) /* T=1 protocol ICC */
255 {
256 if (Protocol_T1_Command ((Protocol_T1 *) slot->protocol, cmd, rsp) != PROTOCOL_T1_OK)
257 ret = ERR_TRANS;
258 else
259 ret = OK;
260 }
261 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14) /* T=14 protocol ICC */
262 {
263 if (Protocol_T14_Command ((Protocol_T14 *) slot->protocol, cmd, rsp) != PROTOCOL_T14_OK)
264 ret = ERR_TRANS;
265 else
266 ret = OK;
267 }
268 else if (slot->protocol_type == CT_SLOT_NULL) /* Card removed */
269 {
270 buffer[0] = CTBCS_SW1_ICC_ERROR;
271 buffer[1] = CTBCS_SW2_ICC_ERROR;
272
273 (*rsp) = APDU_Rsp_New (buffer, 2);
274 ret = OK;
275 }
276 else /* Other protocol */
277 {
278 (*rsp) = NULL;
279 ret = ERR_HTSI;
280 }
281
282 return ret;
283}
284
285int CT_Slot_GetICCType (CT_Slot * slot)
286{
287 return slot->icc_type;
288}
289
290void * CT_Slot_GetICC (CT_Slot * slot)
291{
292 return slot->icc;
293}
294
295void * CT_Slot_GetAtr (CT_Slot * slot)
296{
297 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
298 return ((void *) ICC_Async_GetAtr((ICC_Async *) slot->icc));
299
300 return NULL;
301}
302
303bool CT_Slot_IsLast (CT_Slot * slot)
304{
305 return (IFD_Towitoko_GetSlot(slot->ifd) >= IFD_Towitoko_GetNumSlots()-1);
306}
307
308void CT_Slot_GetType (CT_Slot * slot, BYTE * buffer, int len)
309{
310 IFD_Towitoko_GetDescription (slot->ifd, buffer, len);
311}
312
313char CT_Slot_Close (CT_Slot * slot)
314{
315 char ret;
316
317 ret = OK;
318
319 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0)
320 {
321 if (Protocol_T0_Close ((Protocol_T0 *) slot->protocol) != PROTOCOL_T0_OK)
322 ret = ERR_TRANS;
323
324 Protocol_T0_Delete ((Protocol_T0 *) slot->protocol);
325 }
326 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1)
327 {
328 if (Protocol_T1_Close ((Protocol_T1 *) slot->protocol) != PROTOCOL_T1_OK)
329 ret = ERR_TRANS;
330
331 Protocol_T1_Delete ((Protocol_T1 *) slot->protocol);
332 }
333 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14)
334 {
335 if (Protocol_T14_Close ((Protocol_T14 *) slot->protocol) != PROTOCOL_T14_OK)
336 ret = ERR_TRANS;
337
338 Protocol_T14_Delete ((Protocol_T14 *) slot->protocol);
339 }
340
341 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
342 {
343 if (ICC_Async_Close ((ICC_Async *) slot->icc) != ICC_ASYNC_OK)
344 ret = ERR_TRANS;
345
346 ICC_Async_Delete ((ICC_Async *) slot->icc);
347 }
348
349 if (slot->ifd != NULL)
350 {
351 if (IFD_Towitoko_Close (slot->ifd) != IFD_TOWITOKO_OK)
352 ret = ERR_TRANS;
353
354 IFD_Towitoko_Delete (slot->ifd);
355 }
356
357 CT_Slot_Clear (slot);
358
359 return ret;
360}
361
362void CT_Slot_Delete (CT_Slot * slot)
363{
364 free (slot);
365}
366
367/*
368 * Not exported functions definition
369 */
370
371static void CT_Slot_Clear (CT_Slot * slot)
372{
373 slot->ifd = NULL;
374 slot->icc = NULL;
375 slot->protocol = NULL;
376 slot->icc_type = CT_SLOT_NULL;
377 slot->protocol_type = CT_SLOT_NULL;
378}
Note: See TracBrowser for help on using the repository browser.