[3181] | 1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
[1171] | 2 | #ifdef COOL
|
---|
| 3 | /*
|
---|
| 4 | ifd_cool.c
|
---|
| 5 | This module provides IFD handling functions for Coolstream internal reader.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | #include <stdio.h>
|
---|
| 9 | #include <time.h>
|
---|
[1183] | 10 | #include <string.h>
|
---|
[1171] | 11 | #include"ifd_cool.h"
|
---|
[1478] | 12 | #include"../globals.h"
|
---|
| 13 | #include"icc_async.h"
|
---|
[1171] | 14 |
|
---|
| 15 | void * handle;
|
---|
| 16 |
|
---|
| 17 | unsigned char cardbuffer[256];
|
---|
| 18 | int cardbuflen = 0;
|
---|
| 19 |
|
---|
[2866] | 20 | int Cool_Init (char *device)
|
---|
[1171] | 21 | {
|
---|
[2866] | 22 | int reader_nb = 0;
|
---|
[1171] | 23 | if (cnxt_kal_initialize ())
|
---|
| 24 | return FALSE;
|
---|
| 25 |
|
---|
| 26 | if (cnxt_drv_init ())
|
---|
| 27 | return FALSE;
|
---|
| 28 |
|
---|
| 29 | if (cnxt_smc_init (NULL) != 1)
|
---|
| 30 | return FALSE;
|
---|
[2866] | 31 |
|
---|
| 32 | // this is to stay compatible with olfer config.
|
---|
[2869] | 33 | if(!strlen(device))
|
---|
[2866] | 34 | reader_nb=0;
|
---|
| 35 | else
|
---|
[2868] | 36 | reader_nb=atoi((const char *)device);
|
---|
[2866] | 37 | if(reader_nb>1) {
|
---|
| 38 | // there are only 2 readers in the coolstream : 0 or 1
|
---|
| 39 | cs_log("Coolstream reader device can only be 0 or 1");
|
---|
| 40 | return FALSE;
|
---|
| 41 | }
|
---|
| 42 | if (cnxt_smc_open (&handle, &reader_nb))
|
---|
[1171] | 43 | return FALSE;
|
---|
| 44 |
|
---|
| 45 | return OK;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
[1182] | 49 | int Cool_GetStatus (int * in)
|
---|
| 50 | {
|
---|
| 51 | int state;
|
---|
[1477] | 52 | call (cnxt_smc_get_state(handle, &state));
|
---|
[1182] | 53 | //state = 0 no card, 1 = not ready, 2 = ready
|
---|
| 54 | if (state)
|
---|
[1183] | 55 | *in = 1; //CARD, even if not ready report card is in, or it will never get activated
|
---|
[1182] | 56 | else
|
---|
| 57 | *in = 0; //NOCARD
|
---|
[1171] | 58 | return OK;
|
---|
| 59 | }
|
---|
[1182] | 60 |
|
---|
[1371] | 61 | int Cool_Reset (ATR * atr)
|
---|
[1171] | 62 | {
|
---|
[1477] | 63 | call (Cool_SetClockrate(357));
|
---|
[1171] | 64 |
|
---|
| 65 | //reset card
|
---|
| 66 | int timeout = 5000; // Timout in ms?
|
---|
[1478] | 67 | call (cnxt_smc_reset_card (handle, ATR_TIMEOUT, NULL, NULL));
|
---|
[1171] | 68 |
|
---|
[2561] | 69 | cs_sleepms(50);
|
---|
| 70 |
|
---|
[1171] | 71 | int n = 40;
|
---|
| 72 | unsigned char buf[40];
|
---|
[1477] | 73 | call (cnxt_smc_get_atr (handle, buf, &n));
|
---|
[1182] | 74 |
|
---|
[1477] | 75 | call (!ATR_InitFromArray (atr, buf, n) == ATR_OK);
|
---|
[1182] | 76 | {
|
---|
[1584] | 77 | cs_sleepms(50);
|
---|
[1182] | 78 | return OK;
|
---|
| 79 | }
|
---|
[1171] | 80 | }
|
---|
| 81 |
|
---|
| 82 | int Cool_Transmit (BYTE * sent, unsigned size)
|
---|
| 83 | {
|
---|
| 84 | cardbuflen = 256;//it needs to know max buffer size to respond?
|
---|
[1478] | 85 | call (cnxt_smc_read_write(handle, FALSE, sent, size, cardbuffer, &cardbuflen, 50, 0));
|
---|
| 86 | //call (cnxt_smc_read_write(handle, FALSE, sent, size, cardbuffer, &cardbuflen, read_timeout, 0));
|
---|
[1399] | 87 | cs_ddump(sent, size, "COOL IO: Transmit: ");
|
---|
[1183] | 88 | return OK;
|
---|
[1171] | 89 | }
|
---|
| 90 |
|
---|
[1183] | 91 | int Cool_Receive (BYTE * data, unsigned size)
|
---|
| 92 | {
|
---|
| 93 | if (size > cardbuflen)
|
---|
| 94 | size = cardbuflen; //never read past end of buffer
|
---|
| 95 | memcpy(data,cardbuffer,size);
|
---|
| 96 | cardbuflen -= size;
|
---|
| 97 | memmove(cardbuffer,cardbuffer+size,cardbuflen);
|
---|
[1419] | 98 | cs_ddump(data, size, "COOL IO: Receive: ");
|
---|
[1171] | 99 | return OK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[1468] | 102 | int Cool_SetClockrate (int mhz)
|
---|
[1171] | 103 | {
|
---|
| 104 | typedef unsigned long u_int32;
|
---|
| 105 | u_int32 clk;
|
---|
[1183] | 106 | clk = mhz * 10000;
|
---|
[1477] | 107 | call (cnxt_smc_set_clock_freq (handle, clk));
|
---|
[1399] | 108 | cs_debug("COOL: Clock succesfully set to %i0 kHz", mhz);
|
---|
[1171] | 109 | return OK;
|
---|
| 110 | }
|
---|
[1389] | 111 |
|
---|
| 112 | int Cool_WriteSettings (unsigned long BWT, unsigned long CWT, unsigned long EGT, unsigned long BGT)
|
---|
| 113 | {
|
---|
| 114 | struct
|
---|
| 115 | {
|
---|
| 116 | unsigned short CardActTime; //card activation time (in clock cycles = 1/54Mhz)
|
---|
| 117 | unsigned short CardDeactTime; //card deactivation time (in clock cycles = 1/54Mhz)
|
---|
| 118 | unsigned short ATRSTime; //ATR first char timeout in clock cycles (1/f)
|
---|
| 119 | unsigned short ATRDTime; //ATR duration in ETU
|
---|
| 120 | unsigned long BWT;
|
---|
| 121 | unsigned long CWT;
|
---|
| 122 | unsigned char EGT;
|
---|
| 123 | unsigned char BGT;
|
---|
| 124 | } params;
|
---|
| 125 | params.BWT = BWT;
|
---|
| 126 | params.CWT = CWT;
|
---|
| 127 | params.EGT = EGT;
|
---|
| 128 | params.BGT = BGT;
|
---|
[1477] | 129 | call (cnxt_smc_set_config_timeout(handle, params));
|
---|
[1399] | 130 | cs_debug("COOL WriteSettings OK");
|
---|
[1400] | 131 | return OK;
|
---|
[1389] | 132 | }
|
---|
[2555] | 133 |
|
---|
[2556] | 134 | int Cool_FastReset ()
|
---|
[2555] | 135 | {
|
---|
| 136 | int n = 40;
|
---|
| 137 | unsigned char buf[40];
|
---|
| 138 |
|
---|
| 139 | //reset card
|
---|
| 140 | call (cnxt_smc_reset_card (handle, ATR_TIMEOUT, NULL, NULL));
|
---|
| 141 |
|
---|
[2561] | 142 | cs_sleepms(50);
|
---|
| 143 |
|
---|
[2555] | 144 | call (cnxt_smc_get_atr (handle, buf, &n));
|
---|
[2556] | 145 |
|
---|
| 146 | return 0;
|
---|
[2555] | 147 | }
|
---|
| 148 |
|
---|
[1171] | 149 | #endif
|
---|