source: trunk/csctapi/ct_slot.c@ 1288

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

Remove dynamic datastructure PPS

File size: 5.7 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_New();
141
142 /* Prepare PPS request */
143 if ((userdata != NULL) && (length > 0))
144 memcpy (buffer, userdata, buffer_len = MIN(length, PPS_MAX_LENGTH));
145
146 /* Do PPS */
147 if (PPS_Perform (buffer, &buffer_len) != PPS_OK)
148 {
149 ICC_Async_Close ();
150
151 slot->icc_type = CT_SLOT_NULL;
152 slot->protocol_type = CT_SLOT_NULL;
153
154 return ERR_TRANS;
155 }
156
157 slot->protocol_type = (PPS_GetProtocolParameters ())->t;
158 }
159
160 return OK;
161}
162
163char CT_Slot_Release (CT_Slot * slot)
164{
165 char ret;
166
167 ret = OK;
168
169 slot->protocol_type = CT_SLOT_NULL;
170
171 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
172 {
173 if (ICC_Async_Close () != ICC_ASYNC_OK)
174 ret = ERR_TRANS;
175 }
176
177 slot->icc_type = CT_SLOT_NULL;
178
179 return ret;
180}
181
182char CT_Slot_Command (CT_Slot * slot, APDU_Cmd * cmd, APDU_Rsp ** rsp)
183{
184 BYTE buffer[2];
185 char ret;
186
187 if (slot->protocol_type == CT_SLOT_PROTOCOL_T0) /* T=0 protocol ICC */
188 {
189 if (Protocol_T0_Command (cmd, rsp) != PROTOCOL_T0_OK)
190 ret = ERR_TRANS;
191 else
192 ret = OK;
193 }
194 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1) /* T=1 protocol ICC */
195 {
196 if (Protocol_T1_Command (cmd, rsp) != PROTOCOL_T1_OK)
197 ret = ERR_TRANS;
198 else
199 ret = OK;
200 }
201 else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14) /* T=14 protocol ICC */
202 {
203 if (Protocol_T14_Command (cmd, rsp) != PROTOCOL_T0_OK)
204 ret = ERR_TRANS;
205 else
206 ret = OK;
207 }
208 else if (slot->protocol_type == CT_SLOT_NULL) /* Card removed */
209 {
210 buffer[0] = CTBCS_SW1_ICC_ERROR;
211 buffer[1] = CTBCS_SW2_ICC_ERROR;
212
213 (*rsp) = APDU_Rsp_New (buffer, 2);
214 ret = OK;
215 }
216 else /* Other protocol */
217 {
218 (*rsp) = NULL;
219 ret = ERR_HTSI;
220 }
221
222 return ret;
223}
224
225int CT_Slot_GetICCType (CT_Slot * slot)
226{
227 return slot->icc_type;
228}
229
230void * CT_Slot_GetAtr (CT_Slot * slot)
231{
232 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
233 return ((void *) atr );
234 //return ((void *) ICC_Async_GetAtr((ICC_Async *) slot->icc));
235
236 return NULL;
237}
238
239bool CT_Slot_IsLast (CT_Slot * slot)
240{
241 //return (IFD_Towitoko_GetSlot(slot->ifd) >= IFD_Towitoko_GetNumSlots()-1);
242 return 1; //GetSlot always returns 0, and GetNumSlots returns always 1
243}
244
245void CT_Slot_GetType (CT_Slot * slot, BYTE * buffer, int len)
246{
247 //IFD_Towitoko_GetDescription (slot->ifd, buffer, len)
248 buffer="dummy";
249 len=5;
250}
251
252char CT_Slot_Close (CT_Slot * slot)
253{
254 char ret;
255
256 ret = OK;
257
258 if (slot->icc_type == CT_SLOT_ICC_ASYNC)
259 {
260 if (ICC_Async_Close () != ICC_ASYNC_OK)
261 ret = ERR_TRANS;
262 }
263
264 if (!Phoenix_Close ())
265 ret = ERR_TRANS;
266
267 CT_Slot_Clear (slot);
268
269 return ret;
270}
271
272void CT_Slot_Delete (CT_Slot * slot)
273{
274 free (slot);
275}
276
277/*
278 * Not exported functions definition
279 */
280
281static void CT_Slot_Clear (CT_Slot * slot)
282{
283 slot->icc_type = CT_SLOT_NULL;
284 slot->protocol_type = CT_SLOT_NULL;
285}
Note: See TracBrowser for help on using the repository browser.