source: branches/smartreader/csctapi/ct_slot.c@ 1271

Last change on this file since 1271 was 1271, checked in by rorothetroll, 13 years ago

resync with trunk

File size: 7.9 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 /* Initiaice ICC */
124 slot->icc = ICC_Async_New ();
125
126 if (slot->icc == NULL)
127 return ERR_MEMORY;
128
129 if (ICC_Async_Init (slot->icc) != ICC_ASYNC_OK)
130 {
131 ICC_Async_Delete (slot->icc);
132
133 slot->icc = NULL;
134 slot->icc_type = CT_SLOT_NULL;
135 return ERR_TRANS;
136
137 /* Synchronous card present */
138// slot->icc_type = CT_SLOT_ICC_SYNC;
139 }
140 else
141 {
142 /* Asyncronous card present */
143 slot->icc_type = CT_SLOT_ICC_ASYNC;
144 }
145
146
147 /* Initialise protocol */
148 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
149 {
150 pps = PPS_New((ICC_Async *) slot->icc);
151
152 if (pps == NULL)
153 {
154 ICC_Async_Close ((ICC_Async *) slot->icc);
155 ICC_Async_Delete ((ICC_Async *) slot->icc);
156
157 slot->icc = NULL;
158 slot->icc_type = CT_SLOT_NULL;
159 return ERR_MEMORY;
160 }
161
162 /* Prepare PPS request */
163 if ((userdata != NULL) && (length > 0))
164 memcpy (buffer, userdata, buffer_len = MIN(length, PPS_MAX_LENGTH));
165
166 /* Do PPS */
167 if (PPS_Perform (pps, buffer, &buffer_len) != PPS_OK)
168 {
169 PPS_Delete (pps);
170
171 ICC_Async_Close ((ICC_Async *) slot->icc);
172 ICC_Async_Delete ((ICC_Async *) slot->icc);
173
174 slot->icc = NULL;
175 slot->icc_type = CT_SLOT_NULL;
176 slot->protocol_type = CT_SLOT_NULL;
177
178 return ERR_TRANS;
179 }
180
181 slot->protocol_type = (PPS_GetProtocolParameters (pps))->t;
182 slot->protocol = PPS_GetProtocol (pps);
183
184
185 PPS_Delete (pps);
186 }
187
188 return OK;
189}
190
191char CT_Slot_Release (CT_Slot * slot)
192{
193 char ret;
194
195 ret = OK;
196
197 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0)
198 {
199 if (Protocol_T0_Close ((Protocol_T0 *) slot->protocol) != PROTOCOL_T0_OK)
200 ret = ERR_TRANS;
201
202 Protocol_T0_Delete ((Protocol_T0 *) slot->protocol);
203 }
204 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1)
205 {
206 if (Protocol_T1_Close ((Protocol_T1 *) slot->protocol) != PROTOCOL_T1_OK)
207 ret = ERR_TRANS;
208
209 Protocol_T1_Delete ((Protocol_T1 *) slot->protocol);
210 }
211 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14)
212 {
213 if (Protocol_T14_Close ((Protocol_T14 *) slot->protocol) != PROTOCOL_T14_OK)
214 ret = ERR_TRANS;
215
216 Protocol_T14_Delete ((Protocol_T14 *) slot->protocol);
217 }
218
219 slot->protocol = NULL;
220 slot->protocol_type = CT_SLOT_NULL;
221
222 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
223 {
224 if (ICC_Async_Close ((ICC_Async *) slot->icc) != ICC_ASYNC_OK)
225 ret = ERR_TRANS;
226
227 ICC_Async_Delete ((ICC_Async *) slot->icc);
228 }
229
230 slot->icc = NULL;
231 slot->icc_type = CT_SLOT_NULL;
232
233 return ret;
234}
235
236char CT_Slot_Command (CT_Slot * slot, APDU_Cmd * cmd, APDU_Rsp ** rsp)
237{
238 BYTE buffer[2];
239 char ret;
240
241 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0) /* T=0 protocol ICC */
242 {
243 if (Protocol_T0_Command ((Protocol_T0 *) slot->protocol, cmd, rsp) != PROTOCOL_T0_OK)
244 ret = ERR_TRANS;
245 else
246 ret = OK;
247 }
248 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1) /* T=1 protocol ICC */
249 {
250 if (Protocol_T1_Command ((Protocol_T1 *) slot->protocol, cmd, rsp) != PROTOCOL_T1_OK)
251 ret = ERR_TRANS;
252 else
253 ret = OK;
254 }
255 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14) /* T=14 protocol ICC */
256 {
257 if (Protocol_T14_Command ((Protocol_T14 *) slot->protocol, cmd, rsp) != PROTOCOL_T14_OK)
258 ret = ERR_TRANS;
259 else
260 ret = OK;
261 }
262 else if (slot->protocol_type == CT_SLOT_NULL) /* Card removed */
263 {
264 buffer[0] = CTBCS_SW1_ICC_ERROR;
265 buffer[1] = CTBCS_SW2_ICC_ERROR;
266
267 (*rsp) = APDU_Rsp_New (buffer, 2);
268 ret = OK;
269 }
270 else /* Other protocol */
271 {
272 (*rsp) = NULL;
273 ret = ERR_HTSI;
274 }
275
276 return ret;
277}
278
279int CT_Slot_GetICCType (CT_Slot * slot)
280{
281 return slot->icc_type;
282}
283
284void * CT_Slot_GetICC (CT_Slot * slot)
285{
286 return slot->icc;
287}
288
289void * CT_Slot_GetAtr (CT_Slot * slot)
290{
291 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
292 return ((void *) ICC_Async_GetAtr((ICC_Async *) slot->icc));
293
294 return NULL;
295}
296
297bool CT_Slot_IsLast (CT_Slot * slot)
298{
299 //return (IFD_Towitoko_GetSlot(slot->ifd) >= IFD_Towitoko_GetNumSlots()-1);
300 return 1; //GetSlot always returns 0, and GetNumSlots returns always 1
301}
302
303void CT_Slot_GetType (CT_Slot * slot, BYTE * buffer, int len)
304{
305 //IFD_Towitoko_GetDescription (slot->ifd, buffer, len)
306 buffer="dummy";
307 len=5;
308}
309
310char CT_Slot_Close (CT_Slot * slot)
311{
312 char ret;
313
314 ret = OK;
315
316 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0)
317 {
318 if (Protocol_T0_Close ((Protocol_T0 *) slot->protocol) != PROTOCOL_T0_OK)
319 ret = ERR_TRANS;
320
321 Protocol_T0_Delete ((Protocol_T0 *) slot->protocol);
322 }
323 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1)
324 {
325 if (Protocol_T1_Close ((Protocol_T1 *) slot->protocol) != PROTOCOL_T1_OK)
326 ret = ERR_TRANS;
327
328 Protocol_T1_Delete ((Protocol_T1 *) slot->protocol);
329 }
330 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14)
331 {
332 if (Protocol_T14_Close ((Protocol_T14 *) slot->protocol) != PROTOCOL_T14_OK)
333 ret = ERR_TRANS;
334
335 Protocol_T14_Delete ((Protocol_T14 *) slot->protocol);
336 }
337
338 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
339 {
340 if (ICC_Async_Close ((ICC_Async *) slot->icc) != ICC_ASYNC_OK)
341 ret = ERR_TRANS;
342
343 ICC_Async_Delete ((ICC_Async *) slot->icc);
344 }
345
346 if (!Phoenix_Close ())
347 ret = ERR_TRANS;
348
349 CT_Slot_Clear (slot);
350
351 return ret;
352}
353
354void CT_Slot_Delete (CT_Slot * slot)
355{
356 free (slot);
357}
358
359/*
360 * Not exported functions definition
361 */
362
363static void CT_Slot_Clear (CT_Slot * slot)
364{
365 slot->icc = NULL;
366 slot->protocol = NULL;
367 slot->icc_type = CT_SLOT_NULL;
368 slot->protocol_type = CT_SLOT_NULL;
369}
Note: See TracBrowser for help on using the repository browser.