make git status useful again
[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                 dig1 = *pos;
61                 dig2 = *(pos+1);
62
63                 *res++ = parse_hexdigit(dig1) << 4 | parse_hexdigit(dig2);
64         }
65
66         return (res - result);
67 }
68
69 struct rfid_reader_handle *rh;
70 struct rfid_layer2_handle *l2h;
71 struct rfid_protocol_handle *ph;
72
73 int reader_init(void) 
74 {
75         printf("opening reader handle OpenPCD, CM5x21\n");
76         rh = rfid_reader_open(NULL, RFID_READER_OPENPCD);
77         if (!rh) {
78                 fprintf(stderr, "No OpenPCD found\n");
79                 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
80                 if (!rh) {
81                         fprintf(stderr, "No Omnikey Cardman 5x21 found\n");
82                         return -1;
83                 }
84         }
85         return 0;
86 }
87
88 int l2_init(int layer2)
89 {
90         int rc;
91
92         printf("opening layer2 handle\n");
93         l2h = rfid_layer2_init(rh, layer2);
94         if (!l2h) {
95                 fprintf(stderr, "error during layer2(%d)_init (0=14a,1=14b,3=15)\n",layer2);
96                 return -1;
97         }
98
99         printf("running layer2 anticol(_open)\n");
100         rc = rfid_layer2_open(l2h);
101         if (rc < 0) {
102                 fprintf(stderr, "error during layer2_open\n");
103                 return rc;
104         }
105
106         return 0;
107 }
108
109 int l3_init(int protocol)
110 {
111         printf("running layer3 (ats)\n");
112         ph = rfid_protocol_init(l2h, protocol);
113         if (!ph) {
114                 fprintf(stderr, "error during protocol_init\n");
115                 return -1;
116         }
117         if (rfid_protocol_open(ph) < 0) {
118                 fprintf(stderr, "error during protocol_open\n");
119                 return -1;
120         }
121
122         printf("we now have layer3 up and running\n");
123
124         return 0;
125 }
126