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
|
/*
* ---------
* |.**> <**.| CardContact
* |* *| Software & System Consulting
* |* *| Minden, Germany
* |**> <**| Copyright (c) 1999. All rights reserved
* ---------
*
* See file LICENSE for details on licensing
*
* Abstract : Main API interface according to MKT specification
*
* Author : Frank Thater (FTH)
*
* Last modified: 04/04/2000
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "defines.h"
#include "ctapi.h"
#include "sercom.h"
#include "eco5000.h"
#include "ctbcs.h"
#include "ecotools.h"
/* Handle up to 8 ECO readers */
struct eco5000_t *ecoTable[MAX_READER] = {NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL};
/*
* Locate matching card terminal number in table of active readers.
*
*/
static int LookupReader(unsigned short ctn)
{
int i;
for (i = 0; i < MAX_READER; i++) {
if (ecoTable[i] && (ecoTable[i]->ctn == ctn)) {
return i;
}
}
return -1;
}
/*
* Initialise the interface to the card reader attached
* to the port number specified in <pn>
*
*/
char LINKAGE CT_init (unsigned short ctn, unsigned short pn)
{
int rc;
HANDLE fh;
char device[20];
struct eco5000_t *ctx;
rc = LookupReader(ctn);
if (rc >= 0)
return ERR_CT;
#ifdef WIN32
sprintf(device, "COM%d", pn);
#else
if (pn >= 64)
strcpy(device, "/dev/smartcard");
else
sprintf(device, "/dev/ttyS%d", pn);
#endif
#ifdef DEBUG
printf("Opening reader on %s\n", device);
#endif
rc = rs232Open(&fh, device, 9600, 'E', 8, 1, 200);
if (rc < 0) {
return ERR_CT; /* Could not allocate port */
}
for (rc = 0; (rc < MAX_READER) && ecoTable[rc]; rc++);
if (rc == MAX_READER)
return ERR_MEMORY;
ctx = malloc(sizeof(struct eco5000_t));
if (!ctx)
return ERR_MEMORY;
ctx->ctn = ctn;
ctx->fh = fh;
ctx->CTModFunc = NULL;
ctx->RXStatus = OFF;
ctx->Indirect = FALSE;
ctx->Baud = 9600;
ctx->Data.card = NULL;
ctx->Data.t0 = NULL;
ctx->Data.t1 = NULL;
if (getFirmware (ctx) <= 0) {
free(ctx);
return ERR_CT;
}
ecoTable[rc] = ctx;
#ifdef DEBUG
if(strcmp(ctx->Firmware, ECO_V201) == 0)
printf("\nOld Firmware detected!\n");
else
printf("\nNew Firmware detected!\n");
printf("\nECO 5000 initialized, Firmware Version %s\n\n", ctx->Firmware);
#endif
return OK;
}
/*
* Close the interface and free all allocated resources
*
*/
char LINKAGE CT_close(unsigned short ctn)
{
int rc;
struct eco5000_t *ctx;
struct memorycard_t *pt;
rc = LookupReader(ctn);
if (rc < 0)
return ERR_CT;
ctx = ecoTable[rc];
if (!ctx)
return ERR_CT;
if (ctx->Data.card != NULL) {
pt = ctx->Data.card;
#ifdef DEBUG
printf("\nCleaning up handling structure ...\n");
#endif
if (pt != NULL)
free(pt);
}
rs232Mode(ctx->fh, 9600, 'E', 8, 1, 100);
rs232Close(ctx->fh);
free(ctx);
ecoTable[rc] = NULL;
return OK;
}
/*
* Pass a command to the reader driver and receive the response
*
*/
char LINKAGE CT_data(unsigned short ctn, unsigned char *dad, unsigned char *sad,
unsigned short lc, unsigned char *cmd, unsigned short *lr,
unsigned char *rsp )
{
int rc;
unsigned int ilr;
struct eco5000_t *ctx;
rc = LookupReader(ctn);
if (rc < 0)
return ERR_CT;
ctx = ecoTable[rc];
if (!ctx)
return ERR_CT;
ilr = (int)*lr; /* Overcome problem with lr size */
rc = 0;
if (*dad == 1) {
*sad = 1; /* Source Reader */
*dad = 2; /* Destination Host */
/*******************/
/* CT-BCS Commands */
/*******************/
if(cmd[0] == 0x20) {
switch(cmd[1]) {
case 0x12: /* Request ICC */
rc = RequestICC(ctx, lc, cmd, &ilr, rsp);
break;
case 0x11: /* Resets the Card/Terminal returns ATR */
if (cmd[2]) {
rc = ResetCard(ctx, lc, cmd, &ilr, rsp);
} else {
rc = ResetTerminal(ctx, &ilr, rsp);
}
break;
case 0x13: /* Get Status - Gets reader status */
rc = GetStatus(ctx, cmd, &ilr, rsp);
break;
case 0x15: /* Eject ICC - Deactivates Reader */
rc = EjectICC(ctx, lc, cmd, &ilr, rsp);
break;
default: /* Wrong instruction for CTAPI */
rsp[0] = HIGH(WRONG_INSTRUCTION);
rsp[1] = LOW(WRONG_INSTRUCTION);
ilr = 2;
}
}
else /* Wrong class for CTAPI */
{
rsp[0] = HIGH(CLASS_NOT_SUPPORTED);
rsp[1] = LOW(CLASS_NOT_SUPPORTED);
ilr = 2;
}
} else if (*dad == 0) { /* This command goes to the card */
// Don't get confused here this is for the return saying
// the source was the card and the destination the host
*sad = 0; /* Source Smartcard */
*dad = 2; /* Destination Host */
if (*ctx->CTModFunc)
rc = (*ctx->CTModFunc)(ctx, lc, cmd, &ilr, rsp);
else {
*sad = 1; /* Shows that response comes from CTAPI */
*dad = 2;
rc = 0;
rsp[0] = HIGH(COMMUNICATION_NOT_POSSIBLE);
rsp[1] = LOW(COMMUNICATION_NOT_POSSIBLE);
ilr = 2;
}
} else {
rc = ERR_INVALID; /* Invalid SAD/DAD Address */
ilr = 0;
}
*lr = ilr;
return rc;
}
|