From 7d1495940f1a892b1cb0b5bf5bd7d5914d083aa0 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Wed, 6 Jan 2010 23:42:06 +0000 Subject: [PATCH] add an accessor to UID contained in MifareClassicTag --- freefare.h | 2 ++ mifare_classic.c | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/freefare.h b/freefare.h index da57424..cb4e72b 100644 --- a/freefare.h +++ b/freefare.h @@ -61,6 +61,8 @@ int mifare_classic_get_trailer_block_permission (MifareClassicTag tag, const int mifare_classic_get_data_block_permission (MifareClassicTag tag, const MifareClassicBlockNumber block, const unsigned char permission, const MifareClassicKeyType key_type); int mifare_classic_format_sector (MifareClassicTag tag, const MifareSectorNumber sector); +char* mifare_classic_get_uid(MifareClassicTag tag); + void mifare_classic_trailer_block (MifareClassicBlock *block, const MifareClassicKey key_a, const uint8_t ab_0, const uint8_t ab_1, const uint8_t ab_2, const uint8_t ab_tb, const uint8_t gpb, const MifareClassicKey key_b); /* MIFARE Classic Access Bits */ diff --git a/mifare_classic.c b/mifare_classic.c index eea90ce..af7630b 100644 --- a/mifare_classic.c +++ b/mifare_classic.c @@ -693,6 +693,24 @@ mifare_classic_format_sector (MifareClassicTag tag, const MifareSectorNumber sec return 0; } +/* + * UID accessor + */ +char* +mifare_classic_get_uid(MifareClassicTag tag) +{ + char* uid = malloc((4 * 2) + 1); + for( uint8_t i = 0; i < 4; i++) { + unsigned char msb = (tag->info.abtUid[i] | 0xf0) >> 4; + unsigned char lsb = (tag->info.abtUid[i] | 0x0f); + + uid[i] = msb < 9 ? msb + '0' : msb + 'a'; + uid[i+1] = lsb < 9 ? lsb + '0' : lsb + 'a'; + } + uid[8] = '\0'; + return uid; +} + /* * Generates a MIFARE trailer block. */