1 /* $Id: sc7816.c,v 1.4 2001/09/28 16:20:24 rees Exp $ */
2
3 /*
4 copyright 2000
5 the regents of the university of michigan
6 all rights reserved
7
8 permission is granted to use, copy, create derivative works
9 and redistribute this software and such derivative works
10 for any purpose, so long as the name of the university of
11 michigan is not used in any advertising or publicity
12 pertaining to the use or distribution of this software
13 without specific, written prior authorization. if the
14 above copyright notice or any other identification of the
15 university of michigan is included in any copy of any
16 portion of this software, then the disclaimer below must
17 also be included.
18
19 this software is provided as is, without representation
20 from the university of michigan as to its fitness for any
21 purpose, and without warranty by the university of
22 michigan of any kind, either express or implied, including
23 without limitation the implied warranties of
24 merchantability and fitness for a particular purpose. the
25 regents of the university of michigan shall not be liable
26 for any damages, including special, indirect, incidental, or
27 consequential damages, with respect to any claim arising
28 out of or in connection with the use of the software, even
29 if it has been or is hereafter advised of the possibility of
30 such damages.
31 */
32
33 /*
34 * sc7816 library for use with pc/sc ifd drivers
35 *
36 * Jim Rees
37 * Mukesh Agrawal
38 * University of Michigan CITI, August 2000
39 */
40
41 #ifdef __palmos__
42 #include <Common.h>
43 #include <System/SysAll.h>
44 #include <System/Unix/unix_stdlib.h>
DBUpdateReaders(char * readerconf,int (callback)(int rn,unsigned long channelId,unsigned long lun,char * driverFile))45 #include <System/Unix/unix_string.h>
46 #include <UI/UIAll.h>
47 #include "field.h"
48 #else
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <string.h>
53 #endif
54
55 #ifdef SCPERF
56 #define SCPERF_FIRST_APPEARANCE
57 #endif /* SCPERF */
58
59 #include "sectok.h"
60 #include "sc7816.h"
61
62 char *scerrtab[] = {
63 "ok",
64 "no such tty",
65 "out of memory",
66 "timeout",
67 "slag!",
68 "card type not supported",
69 "no card in reader",
70 "not implemented",
71 "error loading driver",
72 "communications error",
73 "reader not open",
74 "unknown error",
75 };
76
77 int
78 scxopen(int rn, int flags, int *ep, char *config_path, char *driver_path)
79 {
80 int r, sw;
81
82 flags ^= STONOWAIT;
83
84 r = sectok_xopen(rn, flags, config_path, driver_path, &sw);
85
86 if (ep) {
87 if (sectok_r1(sw) == 0x06)
88 *ep = sectok_r2(sw);
89 else
90 *ep = SCECOMM;
91 }
92
93 return r;
94 }
95
96 int
97 scopen(int rn, int flags, int *ep)
98 {
99 return scxopen(rn, flags, ep, NULL, NULL);
100 }
101
102 int
103 scxreset(int fd, int flags, unsigned char *atr, int *ep)
104 {
105 int r, sw;
106
107 r = sectok_reset(fd, flags, atr, &sw);
108
109 if (ep) {
110 if (sectok_swOK(sw))
111 *ep = SCEOK;
112 else if (sectok_r1(sw) == 0x06)
113 *ep = sectok_r2(sw);
114 else
115 *ep = SCESLAG;
116 }
117
118 return r;
119 }
120
121 int
122 screset(int fd, unsigned char *atr, int *ep)
123 {
124 return scxreset(fd, 0, atr, ep);
125 }
126
127 int
128 scrw(int fd, int cla, int ins, int p1, int p2, int ilen, unsigned char *ibuf, int olen, unsigned char *obuf, int *sw1p, int *sw2p)
129 {
130 int n, sw;
131
132 n = sectok_apdu(fd, cla, ins, p1, p2, ilen, ibuf, olen, obuf, &sw);
133 *sw1p = sectok_r1(sw);
134 *sw2p = sectok_r2(sw);
135 return n;
136 }
137
138 int
139 scwrite(int fd, int cla, int ins, int p1, int p2, int p3, unsigned char *buf, int *sw1p, int *sw2p)
140 {
141 int rv;
142 #ifdef SCPERF
143 char *scperf_buf;
144
145 scperf_buf = malloc (64);
146
147 sprintf (scperf_buf, "scwrite (ins %02x, p3 %02x) start", ins, p3);
148 SetTime(scperf_buf);
149 #endif /* SCPERF */
150 rv = scrw(fd, cla, ins, p1, p2, p3, buf, 0, NULL, sw1p, sw2p);
151
152 #ifdef SCPERF
153 SetTime("scwrite() end");
154 #endif /* SCPERF */
155 return (rv >= 0) ? p3 : -1;
156 }
157
158 int
159 scread(int fd, int cla, int ins, int p1, int p2, int p3, unsigned char *buf, int *sw1p, int *sw2p)
160 {
161 int rv;
162 #ifdef SCPERF
163 char *scperf_buf;
164
165 scperf_buf = malloc (64);
166
167 sprintf (scperf_buf, "scread (ins %02x, p3 %02x) start", ins, p3);
168 SetTime(scperf_buf);
169 #endif /* SCPERF */
170 rv = scrw(fd, cla, ins, p1, p2, 0, NULL, p3, buf, sw1p, sw2p);
171
172 #ifdef SCPERF
173 SetTime("scread() end");
174 #endif /* SCPERF */
175 return rv;
176 }
177
178 void
179 print_r1r2(int r1, int r2)
180 {
181 printf("%s\n", get_r1r2s(r1, r2));
182 }
183
184 char *
185 get_r1r2s(int r1, int r2)
186 {
187 return sectok_get_sw(sectok_mksw(r1, r2));
188 }
189
190 #ifndef __palmos__
191 int
192 fdump_reply(FILE *f, unsigned char *p, int n, int r1, int r2)
193 {
194 return sectok_fdump_reply(f, p, n, sectok_mksw(r1, r2));
195 }
196
197 int
198 dump_reply(unsigned char *p, int n, int r1, int r2)
199 {
200 return sectok_fdump_reply(stdout, p, n, sectok_mksw(r1, r2));
201 }
202 #endif
203