source: trunk/csctapi/ct_slot.c@ 1285

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

Isolate T0 and T14 timing aspects and move them to pps.c

File size: 6.1 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 slot->protocol = NULL;
186 slot->protocol_type = CT_SLOT_NULL;
187
188 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
189 {
190 if (ICC_Async_Close () != ICC_ASYNC_OK)
191 ret = ERR_TRANS;
192 }
193
194 slot->icc = NULL;
195 slot->icc_type = CT_SLOT_NULL;
196
197 return ret;
198}
199
200char CT_Slot_Command (CT_Slot * slot, APDU_Cmd * cmd, APDU_Rsp ** rsp)
201{
202 BYTE buffer[2];
203 char ret;
204
205 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0) /* T=0 protocol ICC */
206 {
207 if (Protocol_T0_Command (cmd, rsp) != PROTOCOL_T0_OK)
208 ret = ERR_TRANS;
209 else
210 ret = OK;
211 }
212 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1) /* T=1 protocol ICC */
213 {
214 if (Protocol_T1_Command (cmd, rsp) != PROTOCOL_T1_OK)
215 ret = ERR_TRANS;
216 else
217 ret = OK;
218 }
219 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14) /* T=14 protocol ICC */
220 {
221 if (Protocol_T14_Command (cmd, rsp) != PROTOCOL_T0_OK)
222 ret = ERR_TRANS;
223 else
224 ret = OK;
225 }
226 else if (slot->protocol_type == CT_SLOT_NULL) /* Card removed */
227 {
228 buffer[0] = CTBCS_SW1_ICC_ERROR;
229 buffer[1] = CTBCS_SW2_ICC_ERROR;
230
231 (*rsp) = APDU_Rsp_New (buffer, 2);
232 ret = OK;
233 }
234 else /* Other protocol */
235 {
236 (*rsp) = NULL;
237 ret = ERR_HTSI;
238 }
239
240 return ret;
241}
242
243int CT_Slot_GetICCType (CT_Slot * slot)
244{
245 return slot->icc_type;
246}
247
248void * CT_Slot_GetICC (CT_Slot * slot)
249{
250 return slot->icc;
251}
252
253void * CT_Slot_GetAtr (CT_Slot * slot)
254{
255 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
256 return ((void *) atr );
257 //return ((void *) ICC_Async_GetAtr((ICC_Async *) slot->icc));
258
259 return NULL;
260}
261
262bool CT_Slot_IsLast (CT_Slot * slot)
263{
264 //return (IFD_Towitoko_GetSlot(slot->ifd) >= IFD_Towitoko_GetNumSlots()-1);
265 return 1; //GetSlot always returns 0, and GetNumSlots returns always 1
266}
267
268void CT_Slot_GetType (CT_Slot * slot, BYTE * buffer, int len)
269{
270 //IFD_Towitoko_GetDescription (slot->ifd, buffer, len)
271 buffer="dummy";
272 len=5;
273}
274
275char CT_Slot_Close (CT_Slot * slot)
276{
277 char ret;
278
279 ret = OK;
280
281 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
282 {
283 if (ICC_Async_Close () != ICC_ASYNC_OK)
284 ret = ERR_TRANS;
285 }
286
287 if (!Phoenix_Close ())
288 ret = ERR_TRANS;
289
290 CT_Slot_Clear (slot);
291
292 return ret;
293}
294
295void CT_Slot_Delete (CT_Slot * slot)
296{
297 free (slot);
298}
299
300/*
301 * Not exported functions definition
302 */
303
304static void CT_Slot_Clear (CT_Slot * slot)
305{
306 slot->icc = NULL;
307 slot->protocol = NULL;
308 slot->icc_type = CT_SLOT_NULL;
309 slot->protocol_type = CT_SLOT_NULL;
310}
Note: See TracBrowser for help on using the repository browser.