1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
/*
* ---------
* |.**> <**.| CardContact
* |* *| Software & System Consulting
* |* *| Minden, Germany
* |**> <**| Copyright (c) 1999. All rights reserved
* ---------
*
* See file LICENSE for details on licensing
*
* Abstract : Defines tools/procedures ECO5000 reader
*
* Author : Frank Thater (FTH)
*
* Last modified: 12/10/1999
*
*****************************************************************************/
#include <stdio.h>
#include <string.h>
#ifdef LIMITED
#include <time.h>
#endif
#include "sercom.h"
#include "eco5000.h"
#include "ctapi.h"
#include "defines.h"
/*
* Send a command to the ECO 5000 / Carddrive 330 reader and wait for the
* reponse. If <outbyteslen> and <outbytes> are defined, then data is
* send to the reader after the command byte. If <inbytes> is defined, then
* either the number of bytes specified in <expinbytes> is read or the number
* indicated in the first byte of the reponse returned from the reader. To
* activate the later case, <expinbytes> shall be set to 0
*
*/
int ecoCommand (struct eco5000_t *ctx,
unsigned char com,
int outbyteslen,
unsigned char *outbytes,
int expinbytes,
unsigned char *inbytes)
{
int rc, retry;
int i = 0;
unsigned char temp;
/* If the transparent mode is still switched on, then */
/* we need to switch to command mode first */
if(ctx->RXStatus == ON) {
if (com == RX_ON)
return 0;
rs232LineControl(ctx->fh, 0, 0); /* Set DTR and RTS off */
if (ctx->Indirect)
rs232Mode(ctx->fh, -1, 'E', -1, -1, 200);
ctx->RXStatus = OFF;
if (com != RX_OFF) {
rc = ecoCommand(ctx, RX_OFF, 0, NULL, 0, NULL);
if (rc < 0)
return rc;
}
}
if (!((com == RX_ON) && (ctx->disableRX_ON))) {
retry = 3; /* Sometimes we get parity errors */
while (1) {
if (rs232Write(ctx->fh, &com, 1) <= 0) { /* Write command byte */
#ifdef DEBUG
printf("[ecoCommand] Error writing command byte\n");
#endif
return ERR_TRANS;
}
temp = 0;
if (rs232Read(ctx->fh, &temp, 1) <= 0) { /* Read acknowledge */
#ifdef DEBUG
printf("[ecoCommand] Error reading command acknowledge\n");
#endif
return ERR_TRANS;
}
if ((temp == NACK_PAR) && retry--) {
rs232Drain(ctx->fh);
rs232Flush(ctx->fh);
continue; /* Retry if parity error */
} else if (temp == NACK_CMD)
return ERR_TRANS;
if ((outbyteslen != 0) && (outbytes != NULL)) {
for (i = 0; i < outbyteslen; i++)
if (rs232Write(ctx->fh, &outbytes[i], 1) <=0)
return ERR_TRANS;
temp = 0;
if (rs232Read(ctx->fh, &temp, 1) <= 0) {
#ifdef DEBUG
printf("[ecoCommand] Error reading command acknowledge\n");
#endif
return ERR_TRANS;
}
if ((temp == NACK_PAR) && retry--) {
rs232Drain(ctx->fh);
rs232Flush(ctx->fh);
continue;
} else if (temp == NACK_CMD)
return ERR_TRANS;
}
break;
} /* while (1) */
/* If we expect data from the reader, then inbytes points to a buffer */
/* If the reader return a variable number of bytes, we set expinbytes */
/* to zero. In the later case, the first byte transmitted is the number */
/* of byte following in the response */
if (inbytes != NULL) {
if (!expinbytes) {
if (rs232Read(ctx->fh, inbytes, 1) < 1)
return ERR_CT;
expinbytes = *inbytes++;
}
if (rs232Read(ctx->fh, inbytes, expinbytes) < expinbytes)
return ERR_CT;
}
} /* if */
/* After sending an RX_ON or SET_RESET_RX_ON we are back in transparent */
/* mode. Set RTS accordingly */
if ((com == RX_ON) || (com == SET_RESET_RX_ON)) {
ctx->RXStatus = ON;
if (ctx->Indirect)
rs232Mode(ctx->fh, -1, 'O', -1, -1, -1);
rs232LineControl(ctx->fh, 0, 1); /* Set RTS on */
}
return temp;
}
int ecoChangeBaudrate(struct eco5000_t *ctx, int baudrate)
{
int rc;
char buff;
buff = 0xFF - 14318000 / (32 * baudrate); /* 115200 = 0xFC */
if ((rc = ecoCommand(ctx, SET_BAUDRATE, 1, &buff, 0, NULL)) < 0)
return rc;
rs232Mode(ctx->fh, baudrate, 0, -1, -1, -1);
ctx->Baud = baudrate;
return 0;
}
int getFirmware (struct eco5000_t *ctx)
{
int response;
unsigned char buffer[FWSIZE + 2];
if((response = ecoCommand(ctx, RESET, 0, NULL, 0, NULL)) <= 0)
return response;
if((response = ecoCommand(ctx, GET_VERSION, 0, NULL, 0, buffer)) <= 0)
return response;
buffer[buffer[0] + 1] = '\0';
strcpy(ctx->Firmware, buffer + 1);
#ifdef LIMITED
{
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp->tm_mon > LIMITED)
return -1;
}
#endif
return response;
}
/*
* Decode the APDU to determine the case of the command,
* determine the length of outgoing data (lc), the outgoing
* data and the length of expected data.
*
* If any of the parameter lc, data or le is set the NULL,
* then the parameter is ignored.
*
* Return < 0 if APDU has error or
* 1 for Case 1
* 2 for Case 2S
* 3 for Case 3S
* 4 for Case 4S
* 5 for Case 2E
* 6 for Case 3E
* 7 for Case 4E
*/
int DecodeAPDU(unsigned int len, unsigned char *cmd,
unsigned int *lc, unsigned char **data, unsigned int *le)
{
int apducase,ext;
unsigned int l;
ext = 0; /* Short is default */
if (len < 4) /* Need at least 4 bytes */
return -1;
cmd += 4;
len -= 4;
if (len == 0) { /* Case 1 */
apducase = 1;
} else {
l = 0;
if (!*cmd && (len > 1)) { /* Extended format */
if (len < 3) /* requires 3 byte lc field minimum */
return -1;
ext = 3;
cmd++;
l = (unsigned int)*cmd << 8;
cmd++;
len -= 2;
}
l += *cmd;
len--;
cmd++;
/* cmd and len is now at body part */
if (!len) { /* Case 2 short or extended */
apducase = 2 + ext;
if (!l) { /* l is le, for which 0 means ... */
if (ext)
l = 65536; /* ... either 65536 or ... */
else
l = 256; /* ... 256 bytes expected */
}
if (le) /* Parameter requested by caller ? */
*le = l;
} else { /* Somethings left in the body */
if (len < l) /* l is lc now */
return -1;
if (lc) /* Parameter requested by caller ? */
*lc = l;
if (data) /* Parameter requested by caller ? */
*data = cmd;
len -= l; /* Move past body */
cmd += l;
if (!len) /* Case 3 short or extended */
apducase = 3 + ext;
else { /* Case 4, le is following */
apducase = 4 + ext;
l = 0;
if (ext) { /* le is 3 byte in extended mode */
if (len != 3)
return -1;
cmd++;
l = (unsigned int)*cmd << 8;
cmd++;
len -= 2;
}
if (len != 1)
return -1;
l += (unsigned int)*cmd;
if (!l) { /* 0 on le means maximum, which ... */
if (ext)
l = 65536; /* is 65536 in extended and ... */
else
l = 256; /* 256 in short mode */
}
if (le) /* le requested by caller ? */
*le = l;
}
}
}
return apducase;
}
/*
* Invert a region of memory by swapping the bit order and sign
*
*/
void invert(unsigned char *buff, int len)
{
unsigned char o,n;
int i;
while (len--) {
o = *buff;
n = 0;
for (i = 8; i; i--) {
n <<= 1;
n |= (o & 1) ^ 1;
o >>= 1;
}
*buff = n;
buff++;
}
}
/*
* Read a block of data from the ICC and invert if in indirect mode
*
*/
int iccRead(HANDLE fh, int inverse, unsigned char *buff, int len)
{
int rc;
rc = rs232Read(fh, buff, len);
if (rc <= 0)
return rc;
if (inverse)
invert(buff, rc);
return rc;
}
/*
* Write a block of data to the ICC and invert before, if in indirect mode
*
*/
int iccWrite(HANDLE fh, int inverse, unsigned char *buff, int len)
{
unsigned char tmp[MAX_APDULEN];
if (inverse) {
memcpy(tmp, buff, len);
invert(tmp, len);
return rs232Write(fh, tmp, len);
} else
return rs232Write(fh, buff, len);
}
|