first beta - patch sent to librfid list
[rfid/librfid.git] / src / rfid_reader_cm5121_openct.c
1 /* CM5121 backend for OpenCT virtual slot */
2
3 #include <stdio.h>
4
5 #include <librfid/rfid_asic.h>
6 #include <openct/openct.h>
7
8 /* FIXME: get rid of this global crap.  In fact this needs to become part of
9  * struct rfid_reader_handle  */
10 static ct_lock_handle lock;
11 static int slot = 1;
12
13 /* this is the sole function required by rfid_reader_cm5121.c */
14 int 
15 PC_to_RDR_Escape(void *handle, 
16                  const unsigned char *tx_buf, unsigned int tx_len,
17                  unsigned char *rx_buf, unsigned int *rx_len)
18 {
19         int rc;
20         ct_handle *h = (ct_handle *) handle;
21
22         rc = ct_card_transact(h, 1, tx_buf, tx_len, rx_buf, *rx_len);
23         if (rc >= 0) {
24                 *rx_len = rc;
25                 return 0;
26         }
27
28         return rc;
29 }
30
31
32 int cm5121_source_init(struct rfid_asic_transport_handle *rath)
33 {
34         struct ct_handle *h;
35         int rc;
36         unsigned char atr[64];
37
38         h = ct_reader_connect(0);
39         if (!h)
40                 return -1;
41
42         printf("acquiring card lock\n");
43         rc = ct_card_lock(h, slot, IFD_LOCK_EXCLUSIVE, &lock);
44         if (rc < 0) {
45                 fprintf(stderr, "error, no card lock\n");
46                 return -1;
47         }
48
49         rc = ct_card_reset(h, slot, atr, sizeof(atr));
50         if (rc < 0) {
51                 fprintf(stderr, "error, can't reset virtual card\n");
52                 return -1;
53         }
54
55         rath->data = h;
56
57         return 0;
58 }
59