- only link with openct if we need it
[librfid] / utils / common.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <libgen.h>
7
8 #include <librfid/rfid.h>
9 #include <librfid/rfid_scan.h>
10 #include <librfid/rfid_reader.h>
11 #include <librfid/rfid_layer2.h>
12 #include <librfid/rfid_protocol.h>
13
14 #include <librfid/rfid_protocol_mifare_classic.h>
15 #include <librfid/rfid_protocol_mifare_ul.h>
16
17 #include "librfid-tool.h"
18
19 const char *
20 hexdump(const void *data, unsigned int len)
21 {
22         static char string[1024];
23         unsigned char *d = (unsigned char *) data;
24         unsigned int i, left;
25
26         string[0] = '\0';
27         left = sizeof(string);
28         for (i = 0; len--; i += 3) {
29                 if (i >= sizeof(string) -4)
30                         break;
31                 snprintf(string+i, 4, " %02x", *d++);
32         }
33         return string;
34 }
35
36 static char parse_hexdigit(const char hexdigit)
37 {
38         if (hexdigit <= '9' && hexdigit >= '0')
39                 return hexdigit - '0';
40         if (hexdigit <= 'f' && hexdigit >= 'a')
41                 return 10 + (hexdigit - 'a');
42
43         return 0;
44 }
45
46 int
47 hexread(unsigned char *result, const unsigned char *in, unsigned int len)
48 {
49         const unsigned char *pos;
50         char dig1, dig2;
51         unsigned char *res = result;
52
53         for (pos = in; pos-in <= len-2; pos+=2) {
54                 if (*pos == ':') {
55                         pos++;
56                         continue;
57                 }
58                 dig1 = *pos;
59                 dig2 = *(pos+1);
60
61                 *res++ = parse_hexdigit(dig1) << 4 | parse_hexdigit(dig2);
62         }
63
64         return (res - result);
65 }
66
67 struct rfid_reader_handle *rh;
68 struct rfid_layer2_handle *l2h;
69 struct rfid_protocol_handle *ph;
70
71 int reader_init(void) 
72 {
73         printf("opening reader handle\n");
74         rh = rfid_reader_open(NULL, RFID_READER_CM5121);
75         if (!rh) {
76                 fprintf(stderr, "No Omnikey Cardman 5121 found\n");
77                 rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
78                 if (!rh) {
79                         fprintf(stderr, "No OpenPCD found either\n");
80                         return -1;
81                 }
82         }
83         return 0;
84 }
85
86 int l2_init(int layer2)
87 {
88         unsigned char buf[0x3f];
89         int rc;
90
91         printf("opening layer2 handle\n");
92         l2h = rfid_layer2_init(rh, layer2);
93         if (!l2h) {
94                 fprintf(stderr, "error during iso14443a_init\n");
95                 return -1;
96         }
97
98         printf("running layer2 anticol\n");
99         rc = rfid_layer2_open(l2h);
100         if (rc < 0) {
101                 fprintf(stderr, "error during layer2_open\n");
102                 return rc;
103         }
104
105         return 0;
106 }
107
108 int l3_init(int protocol)
109 {
110         printf("running layer3 (ats)\n");
111         ph = rfid_protocol_init(l2h, protocol);
112         if (!ph) {
113                 fprintf(stderr, "error during protocol_init\n");
114                 return -1;
115         }
116         if (rfid_protocol_open(ph) < 0) {
117                 fprintf(stderr, "error during protocol_open\n");
118                 return -1;
119         }
120
121         printf("we now have layer3 up and running\n");
122
123         return 0;
124 }
125