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