2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2
4 * as published by the Free Software Foundation
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <librfid/rfid.h>
27 #include <librfid/rfid_reader.h>
28 #include <librfid/rfid_layer2.h>
29 #include <librfid/rfid_protocol.h>
31 #include <librfid/rfid_protocol_mifare_classic.h>
32 #include <librfid/rfid_protocol_mifare_ul.h>
34 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
37 hexdump(const void *data, unsigned int len)
39 static char string[2048];
40 unsigned char *d = (unsigned char *) data;
41 unsigned int i, left, llen = len;
44 left = sizeof(string);
45 for (i = 0; llen--; i += 3) {
46 if (i >= sizeof(string) -4)
48 snprintf(string+i, 4, " %02x", *d++);
51 if (i >= sizeof(string) -2)
53 snprintf(string+i, 2, " ");
56 d = (unsigned char *) data;
57 for (; llen--; i += 1) {
58 if (i >= sizeof(string) -2)
60 snprintf(string+i, 2, "%c", isprint(*d) ? *d : '.');
66 static struct rfid_reader_handle *rh;
67 static struct rfid_layer2_handle *l2h;
68 static struct rfid_protocol_handle *ph;
72 unsigned char buf[0x3f];
75 printf("initializing librfid\n");
78 printf("opening reader handle\n");
79 rh = rfid_reader_open(NULL, RFID_READER_CM5121);
81 fprintf(stderr, "error, no cm5121 handle\n");
85 printf("opening layer2 handle\n");
86 l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443A);
87 //l2h = rfid_layer2_init(rh, RFID_LAYER2_ISO14443B);
89 fprintf(stderr, "error during iso14443a_init\n");
93 //rc632_register_dump(rh->ah, buf);
95 printf("running layer2 anticol\n");
96 rc = rfid_layer2_open(l2h);
98 fprintf(stderr, "error during layer2_open\n");
105 static int l3(int protocol)
107 printf("running layer3 (ats)\n");
108 ph = rfid_protocol_init(l2h, protocol);
110 fprintf(stderr, "error during protocol_init\n");
113 if (rfid_protocol_open(ph) < 0) {
114 fprintf(stderr, "error during protocol_open\n");
118 printf("we now have layer3 up and running\n");
123 static int send_command(char* sbuf, int slen, char* rbuf, int rlen)
130 fprintf(stderr, "?? %s (%i bytes)\n", hexdump(sbuf, slen), slen);
131 fprintf(stderr, "Execute? (Yes/No/All/Exit) ");
132 answer = getc(stdin);
133 if(answer != '\n') do {
135 } while(c != '\n' && c != EOF);
137 case 'y': // Fall-through
142 case 'n': // Fall-through
146 case 'a': // Fall-through
150 case 'e': // Fall-through
155 return 0; // Default to 'n'
160 printf(">> %s (%i bytes)\n", hexdump(sbuf, slen), slen);
162 rv = rfid_protocol_transceive(ph, sbuf, slen, rbuf, &rlen, 0, 0);
164 fprintf(stderr, "Error from transceive: %i\n", rv);
168 printf("<< %s (%i bytes)\n", hexdump(rbuf, rlen), rlen);
170 if(rlen < 2 || rbuf[rlen-2] != (char)0x90 || rbuf[rlen-1] != 0x00) {
171 fprintf(stderr, "SW is not 90 00. Ignore (i) or Abort (a)? ");
172 answer = getc(stdin);
173 if(answer != '\n') do {
175 } while(c != '\n' && c != EOF);
177 case 'i': // Fall-through
182 case 'a': // Fall-through
187 return -1; // Default to 'a'
194 static char *nextline(FILE* fh)
196 int buflen = 1024; // FIXME Might want to increase dynamically?
197 char *buffer = malloc(buflen);
203 if (!fgets(buffer, buflen, fh)) {
212 static int unhexchar(char c) {
213 if ((c - '0') >= 0 && (c - '0') < 10)
215 if ((c - 'a') >= 0 && (c - 'a') < 6)
217 if ((c - 'A') >= 0 && (c - 'A') < 6)
222 int make_command( const char *line, char **buffer, int *blen )
224 int len = strlen(line), pos;
225 *buffer = malloc( len );
232 for(pos = 0; pos < len; pos++) {
233 if(!isxdigit(line[pos]))
238 (*buffer)[*blen] = unhexchar(line[pos]) * 16 + unhexchar(line[pos+1]);
245 int main(int argc, char **argv)
248 int slen, rlen, retval = 0;
249 char *next, *sbuf, *rbuf;
252 fprintf(stderr, "Syntax: %s scriptfile\n", argv[0]);
256 fh = fopen(argv[1], "r");
265 if (l3(RFID_PROTOCOL_TCL) < 0)
268 printf("Protocol T=CL\n");
269 /* we've established T=CL at this point */
271 while (next = nextline(fh)) {
272 if (!(strlen(next) >= 2 && strncmp(next, "//", 2) == 0)) {
273 if (make_command(next, &sbuf, &slen)) {
275 rbuf = calloc(rlen, 1);
277 retval = send_command(sbuf, slen, rbuf, rlen);
289 rfid_reader_close(rh);