From 2da53a43badc7bd578ab5df850648f14807aecd2 Mon Sep 17 00:00:00 2001 From: laforge Date: Tue, 28 Nov 2006 10:06:24 +0000 Subject: - fix mifare write support - make mifare auth more reliable - add mifare permission / access bit en/decoding - some more 15693 bits - add new 'mifare-tool' program git-svn-id: https://svn.gnumonks.org/trunk/librfid@1928 e0336214-984f-0b4b-a45f-81c69e1f0ede --- utils/common.c | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 utils/common.c (limited to 'utils/common.c') diff --git a/utils/common.c b/utils/common.c new file mode 100644 index 0000000..2966ab4 --- /dev/null +++ b/utils/common.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#include "librfid-tool.h" + +const char * +hexdump(const void *data, unsigned int len) +{ + static char string[1024]; + unsigned char *d = (unsigned char *) data; + unsigned int i, left; + + string[0] = '\0'; + left = sizeof(string); + for (i = 0; len--; i += 3) { + if (i >= sizeof(string) -4) + break; + snprintf(string+i, 4, " %02x", *d++); + } + return string; +} + +static char parse_hexdigit(const char hexdigit) +{ + if (hexdigit <= '9' && hexdigit >= '0') + return hexdigit - '0'; + if (hexdigit <= 'f' && hexdigit >= 'a') + return 10 + (hexdigit - 'a'); + + return 0; +} + +int +hexread(unsigned char *result, const unsigned char *in, unsigned int len) +{ + const unsigned char *pos; + char dig1, dig2; + unsigned char *res = result; + + for (pos = in; pos-in <= len-2; pos+=2) { + if (*pos == ':') { + pos++; + continue; + } + dig1 = *pos; + dig2 = *(pos+1); + + *res++ = parse_hexdigit(dig1) << 4 | parse_hexdigit(dig2); + } + + return (res - result); +} + +struct rfid_reader_handle *rh; +struct rfid_layer2_handle *l2h; +struct rfid_protocol_handle *ph; + +int reader_init(void) +{ + printf("opening reader handle\n"); + rh = rfid_reader_open(NULL, RFID_READER_CM5121); + if (!rh) { + fprintf(stderr, "No Omnikey Cardman 5121 found\n"); + rh = rfid_reader_open(NULL, RFID_READER_OPENPCD); + if (!rh) { + fprintf(stderr, "No OpenPCD found either\n"); + return -1; + } + } + return 0; +} + +int l2_init(int layer2) +{ + unsigned char buf[0x3f]; + int rc; + + printf("opening layer2 handle\n"); + l2h = rfid_layer2_init(rh, layer2); + if (!l2h) { + fprintf(stderr, "error during iso14443a_init\n"); + return -1; + } + + printf("running layer2 anticol\n"); + rc = rfid_layer2_open(l2h); + if (rc < 0) { + fprintf(stderr, "error during layer2_open\n"); + return rc; + } + + return 0; +} + +int l3_init(int protocol) +{ + printf("running layer3 (ats)\n"); + ph = rfid_protocol_init(l2h, protocol); + if (!ph) { + fprintf(stderr, "error during protocol_init\n"); + return -1; + } + if (rfid_protocol_open(ph) < 0) { + fprintf(stderr, "error during protocol_open\n"); + return -1; + } + + printf("we now have layer3 up and running\n"); + + return 0; +} + -- cgit v1.2.3