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 | #include "ifd.h"
|
---|
39 |
|
---|
40 | /* Card status *///FIXME simplify this + duplicate in icc_async.h
|
---|
41 | #define IFD_TOWITOKO_CARD(status) (((status) & 0x40) == 0x40)
|
---|
42 | #define IFD_TOWITOKO_CHANGE(status) (((status) & 0x80) == 0x80)
|
---|
43 |
|
---|
44 | /* Try first asynchronous init and if it fails try synchronous */
|
---|
45 | //#undef ICC_PROBE_ASYNC_FIRST
|
---|
46 | //#define ICC_PROBE_ASYNC_FIRST
|
---|
47 |
|
---|
48 | /*
|
---|
49 | * Not exported functions declaration
|
---|
50 | */
|
---|
51 |
|
---|
52 | static void CT_Slot_Clear (CT_Slot * slot);
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Exported functions definition
|
---|
56 | */
|
---|
57 |
|
---|
58 | CT_Slot * CT_Slot_New ()
|
---|
59 | {
|
---|
60 | CT_Slot *slot;
|
---|
61 |
|
---|
62 | slot = (CT_Slot *) malloc (sizeof (CT_Slot));
|
---|
63 |
|
---|
64 | if (slot != NULL)
|
---|
65 | CT_Slot_Clear (slot);
|
---|
66 |
|
---|
67 | return slot;
|
---|
68 | }
|
---|
69 |
|
---|
70 | char CT_Slot_Init ()
|
---|
71 | {
|
---|
72 | if (!Phoenix_Init())
|
---|
73 | return ERR_TRANS;
|
---|
74 |
|
---|
75 | return OK;
|
---|
76 | }
|
---|
77 |
|
---|
78 | char CT_Slot_Check (uint timeout, bool * card, bool * change)
|
---|
79 | {
|
---|
80 | BYTE status;
|
---|
81 | #ifdef HAVE_NANOSLEEP
|
---|
82 | struct timespec req_ts;
|
---|
83 |
|
---|
84 | req_ts.tv_sec = 1;
|
---|
85 | req_ts.tv_nsec = 0;
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | /* Do first time check */
|
---|
89 | if (ICC_Async_GetStatus (&status) != ICC_ASYNC_OK)
|
---|
90 | {
|
---|
91 | return ERR_TRANS;
|
---|
92 | }
|
---|
93 |
|
---|
94 | (*change) = IFD_TOWITOKO_CHANGE (status);
|
---|
95 |
|
---|
96 | while ((timeout > 0) && (!IFD_TOWITOKO_CARD (status)))
|
---|
97 | {
|
---|
98 | timeout --;
|
---|
99 |
|
---|
100 | #ifdef HAVE_NANOSLEEP
|
---|
101 | /* Sleep one second */
|
---|
102 | nanosleep (&req_ts,NULL);
|
---|
103 | #else
|
---|
104 | usleep (1000);
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | if (ICC_Async_GetStatus (&status) != ICC_ASYNC_OK)
|
---|
108 | return ERR_TRANS;
|
---|
109 |
|
---|
110 | (*change) |= IFD_TOWITOKO_CHANGE (status);
|
---|
111 | }
|
---|
112 |
|
---|
113 | (*card) = IFD_TOWITOKO_CARD (status);
|
---|
114 |
|
---|
115 | return OK;
|
---|
116 | }
|
---|
117 |
|
---|
118 | char CT_Slot_Probe (CT_Slot * slot, BYTE * userdata, unsigned length)
|
---|
119 | {
|
---|
120 | //PPS * pps;
|
---|
121 | BYTE buffer[PPS_MAX_LENGTH];
|
---|
122 | unsigned buffer_len = 0;
|
---|
123 |
|
---|
124 | if (ICC_Async_Init () != ICC_ASYNC_OK)
|
---|
125 | {
|
---|
126 | return ERR_TRANS;
|
---|
127 |
|
---|
128 | /* Synchronous card present */
|
---|
129 | // slot->icc_type = CT_SLOT_ICC_SYNC;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | /* Asyncronous card present */
|
---|
134 | slot->icc_type = CT_SLOT_ICC_ASYNC;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /* Initialise protocol */
|
---|
139 | if (slot->icc_type == CT_SLOT_ICC_ASYNC)
|
---|
140 | {
|
---|
141 | PPS_New();
|
---|
142 |
|
---|
143 | /* Prepare PPS request */
|
---|
144 | if ((userdata != NULL) && (length > 0))
|
---|
145 | memcpy (buffer, userdata, buffer_len = MIN(length, PPS_MAX_LENGTH));
|
---|
146 |
|
---|
147 | /* Do PPS */
|
---|
148 | if (PPS_Perform (buffer, &buffer_len) != PPS_OK)
|
---|
149 | {
|
---|
150 | ICC_Async_Close ();
|
---|
151 |
|
---|
152 | slot->icc_type = CT_SLOT_NULL;
|
---|
153 | slot->protocol_type = CT_SLOT_NULL;
|
---|
154 |
|
---|
155 | return ERR_TRANS;
|
---|
156 | }
|
---|
157 |
|
---|
158 | slot->protocol_type = (PPS_GetProtocolParameters ())->t;
|
---|
159 | }
|
---|
160 |
|
---|
161 | return OK;
|
---|
162 | }
|
---|
163 |
|
---|
164 | char CT_Slot_Release (CT_Slot * slot)
|
---|
165 | {
|
---|
166 | char ret;
|
---|
167 |
|
---|
168 | ret = OK;
|
---|
169 |
|
---|
170 | slot->protocol_type = CT_SLOT_NULL;
|
---|
171 |
|
---|
172 | if (slot->icc_type == CT_SLOT_ICC_ASYNC)
|
---|
173 | {
|
---|
174 | if (ICC_Async_Close () != ICC_ASYNC_OK)
|
---|
175 | ret = ERR_TRANS;
|
---|
176 | }
|
---|
177 |
|
---|
178 | slot->icc_type = CT_SLOT_NULL;
|
---|
179 |
|
---|
180 | return ret;
|
---|
181 | }
|
---|
182 |
|
---|
183 | char CT_Slot_Command (CT_Slot * slot, APDU_Cmd * cmd, APDU_Rsp ** rsp)
|
---|
184 | {
|
---|
185 | BYTE buffer[2];
|
---|
186 | char ret;
|
---|
187 |
|
---|
188 | if (slot->protocol_type == CT_SLOT_PROTOCOL_T0) /* T=0 protocol ICC */
|
---|
189 | {
|
---|
190 | if (Protocol_T0_Command (cmd, rsp) != PROTOCOL_T0_OK)
|
---|
191 | ret = ERR_TRANS;
|
---|
192 | else
|
---|
193 | ret = OK;
|
---|
194 | }
|
---|
195 | else if (slot->protocol_type == CT_SLOT_PROTOCOL_T1) /* T=1 protocol ICC */
|
---|
196 | {
|
---|
197 | if (Protocol_T1_Command (cmd, rsp) != PROTOCOL_T1_OK)
|
---|
198 | ret = ERR_TRANS;
|
---|
199 | else
|
---|
200 | ret = OK;
|
---|
201 | }
|
---|
202 | else if (slot->protocol_type == CT_SLOT_PROTOCOL_T14) /* T=14 protocol ICC */
|
---|
203 | {
|
---|
204 | if (Protocol_T14_Command (cmd, rsp) != PROTOCOL_T0_OK)
|
---|
205 | ret = ERR_TRANS;
|
---|
206 | else
|
---|
207 | ret = OK;
|
---|
208 | }
|
---|
209 | else if (slot->protocol_type == CT_SLOT_NULL) /* Card removed */
|
---|
210 | {
|
---|
211 | buffer[0] = CTBCS_SW1_ICC_ERROR;
|
---|
212 | buffer[1] = CTBCS_SW2_ICC_ERROR;
|
---|
213 |
|
---|
214 | (*rsp) = APDU_Rsp_New (buffer, 2);
|
---|
215 | ret = OK;
|
---|
216 | }
|
---|
217 | else /* Other protocol */
|
---|
218 | {
|
---|
219 | (*rsp) = NULL;
|
---|
220 | ret = ERR_HTSI;
|
---|
221 | }
|
---|
222 |
|
---|
223 | return ret;
|
---|
224 | }
|
---|
225 |
|
---|
226 | int CT_Slot_GetICCType (CT_Slot * slot)
|
---|
227 | {
|
---|
228 | return slot->icc_type;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void * CT_Slot_GetAtr (CT_Slot * slot)
|
---|
232 | {
|
---|
233 | if (slot->icc_type == CT_SLOT_ICC_ASYNC)
|
---|
234 | return ((void *) atr );
|
---|
235 | //return ((void *) ICC_Async_GetAtr((ICC_Async *) slot->icc));
|
---|
236 |
|
---|
237 | return NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | void CT_Slot_GetType (BYTE * buffer, int len)
|
---|
241 | {
|
---|
242 | //IFD_Towitoko_GetDescription (slot->ifd, buffer, len)
|
---|
243 | char temp[5] = "dummy";
|
---|
244 | len=5;
|
---|
245 | buffer = (BYTE *) temp;
|
---|
246 | }
|
---|
247 |
|
---|
248 | char CT_Slot_Close (CT_Slot * slot)
|
---|
249 | {
|
---|
250 | char ret;
|
---|
251 |
|
---|
252 | ret = OK;
|
---|
253 |
|
---|
254 | if (slot->icc_type == CT_SLOT_ICC_ASYNC)
|
---|
255 | {
|
---|
256 | if (ICC_Async_Close () != ICC_ASYNC_OK)
|
---|
257 | ret = ERR_TRANS;
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (!Phoenix_Close ())
|
---|
261 | ret = ERR_TRANS;
|
---|
262 |
|
---|
263 | CT_Slot_Clear (slot);
|
---|
264 |
|
---|
265 | return ret;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void CT_Slot_Delete (CT_Slot * slot)
|
---|
269 | {
|
---|
270 | free (slot);
|
---|
271 | }
|
---|
272 |
|
---|
273 | /*
|
---|
274 | * Not exported functions definition
|
---|
275 | */
|
---|
276 |
|
---|
277 | static void CT_Slot_Clear (CT_Slot * slot)
|
---|
278 | {
|
---|
279 | slot->icc_type = CT_SLOT_NULL;
|
---|
280 | slot->protocol_type = CT_SLOT_NULL;
|
---|
281 | }
|
---|