python support by Kushal Das
[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 #include "common.h"
19
20 const char *
21 hexdump(const void *data, unsigned int len)
22 {
23         static char string[1024];
24         unsigned char *d = (unsigned char *) data;
25         unsigned int i, left;
26
27         string[0] = '\0';
28         left = sizeof(string);
29         for (i = 0; len--; i += 3) {
30                 if (i >= sizeof(string) -4)
31                         break;
32                 snprintf(string+i, 4, " %02x", *d++);
33         }
34         return string;
35 }
36
37 static char parse_hexdigit(const char hexdigit)
38 {
39         if (hexdigit <= '9' && hexdigit >= '0')
40                 return hexdigit - '0';
41         if (hexdigit <= 'f' && hexdigit >= 'a')
42                 return 10 + (hexdigit - 'a');
43
44         return 0;
45 }
46
47 int
48 hexread(unsigned char *result, const unsigned char *in, unsigned int len)
49 {
50         const unsigned char *pos;
51         char dig1, dig2;
52         unsigned char *res = result;
53
54         for (pos = in; pos-in <= len-2; pos+=2) {
55                 if (*pos == ':') {
56                         pos++;
57                         continue;
58                 }
59                 dig1 = *pos;
60                 dig2 = *(pos+1);
61
62                 *res++ = parse_hexdigit(dig1) << 4 | parse_hexdigit(dig2);
63         }
64
65         return (res - result);
66 }
67
68 struct rfid_reader_handle *rh;
69 struct rfid_layer2_handle *l2h;
70 struct rfid_protocol_handle *ph;
71
72 int reader_init(void) 
73 {
74         printf("opening reader handle\n");
75         rh = rfid_reader_open(NULL, RFID_READER_CM5121);
76         if (!rh) {
77                 fprintf(stderr, "No Omnikey Cardman 5121 found\n");
78                 rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
79                 if (!rh) {
80                         fprintf(stderr, "No OpenPCD found either\n");
81                         return -1;
82                 }
83         }
84         return 0;
85 }
86
87 int l2_init(int layer2)
88 {
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