source: trunk/csctapi/ct_slot.c@ 1279

Last change on this file since 1279 was 1279, checked in by dingo35, 11 years ago

Remove ICC_Async dynamic data structure, cleanup some parameters in t0 and t1

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