From 574b068b3f6483164bf59d8c7def2290cb8ece4c Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Tue, 30 Mar 2010 16:18:58 +0000 Subject: [PATCH] New API function freefare_get_tag_uid(). - Deprecates mifare_classic_get_uid() and mifare_ultralight_get_uid(). --- libfreefare/Makefile.am | 1 + libfreefare/freefare.3 | 6 +++++- libfreefare/freefare.c | 12 ++++++++++++ libfreefare/freefare.h | 1 + libfreefare/mifare_classic.c | 2 +- libfreefare/mifare_ultralight.c | 2 +- test/test_mifare_classic.c | 12 ++++++++++++ test/test_mifare_ultralight.c | 12 ++++++++++++ 8 files changed, 45 insertions(+), 3 deletions(-) diff --git a/libfreefare/Makefile.am b/libfreefare/Makefile.am index 6862ed6..d3f5065 100644 --- a/libfreefare/Makefile.am +++ b/libfreefare/Makefile.am @@ -21,6 +21,7 @@ man_MANS = freefare.3 \ linkedman = \ freefare.3 freefare_get_tags.3 \ freefare.3 freefare_get_tag_type.3 \ + freefare.3 freefare_get_tag_uid.3 \ freefare.3 freefare_get_tag_friendly_name.3 \ freefare.3 freefare_free_tags.3 \ mifare_ultralight.3 mifare_ultralight_connect.3 \ diff --git a/libfreefare/freefare.3 b/libfreefare/freefare.3 index ad11671..2d7870e 100644 --- a/libfreefare/freefare.3 +++ b/libfreefare/freefare.3 @@ -28,6 +28,7 @@ .Nm freefare_get_tags , .Nm freefare_get_tag_type , .Nm freefare_get_tag_friendly_name , +.Nm freefare_get_tag_uid , .Nm freefare_free_tags .Nd Generic target manipulation functions .\" _ _ _ @@ -52,6 +53,8 @@ Mifare card manipulation library (libfreefare, \-lfreefare) .Fn freefare_get_tag_type "MifareTag tag" .Ft "const char *" .Fn freefare_get_tag_friendly_name "MifareTag tag" +.Ft "char *" +.Fn freefare_get_tag_uid "MifareTag tag" .Ft void .Fn freefare_free_tags "MifareTag *tags" .\" ____ _ _ _ @@ -78,7 +81,8 @@ This list has to be freed after usage using Information about a given .Vt MifareTag can be gathered using the -.Fn freefare_get_tag_type +.Fn freefare_get_tag_type , +.Fn freefare_get_tag_uid and .Fn freefare_get_tag_friendly_name functions. diff --git a/libfreefare/freefare.c b/libfreefare/freefare.c index 6db094d..b3b63b2 100644 --- a/libfreefare/freefare.c +++ b/libfreefare/freefare.c @@ -148,6 +148,18 @@ freefare_get_tag_friendly_name (MifareTag tag) return tag->tag_info->friendly_name; } +/* + * Returns the UID of the provided tag. + */ +char * +freefare_get_tag_uid (MifareTag tag) +{ + char *res = malloc (2 * tag->info.szUidLen + 1); + for (int i =0; i < tag->info.szUidLen; i++) + snprintf (res + 2*i, 3, "%02x", tag->info.abtUid[i]); + return res; +} + /* * Free the provided tag list. */ diff --git a/libfreefare/freefare.h b/libfreefare/freefare.h index e0cc22c..bccb4ce 100644 --- a/libfreefare/freefare.h +++ b/libfreefare/freefare.h @@ -54,6 +54,7 @@ typedef unsigned char MifareUltralightPage[4]; MifareTag *freefare_get_tags (nfc_device_t *device); enum mifare_tag_type freefare_get_tag_type (MifareTag tag); const char *freefare_get_tag_friendly_name (MifareTag tag); +char *freefare_get_tag_uid (MifareTag tag); void freefare_free_tags (MifareTag *tags); int mifare_ultralight_connect (MifareTag tag); diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index 8590ee9..68fa3bd 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -173,7 +173,7 @@ mifare_classic_connect (MifareTag tag) ASSERT_MIFARE_CLASSIC (tag); nfc_target_info_t pnti; - if (nfc_initiator_select_tag (tag->device, NM_ISO14443A_106, tag->info.abtUid, 4, &pnti)) { + if (nfc_initiator_select_tag (tag->device, NM_ISO14443A_106, tag->info.abtUid, tag->info.szUidLen, &pnti)) { tag->active = 1; } else { errno = EIO; diff --git a/libfreefare/mifare_ultralight.c b/libfreefare/mifare_ultralight.c index ea15967..9e4f7c4 100644 --- a/libfreefare/mifare_ultralight.c +++ b/libfreefare/mifare_ultralight.c @@ -84,7 +84,7 @@ mifare_ultralight_connect (MifareTag tag) ASSERT_MIFARE_ULTRALIGHT (tag); nfc_target_info_t pnti; - if (nfc_initiator_select_tag (tag->device, NM_ISO14443A_106, tag->info.abtUid, 7, &pnti)) { + if (nfc_initiator_select_tag (tag->device, NM_ISO14443A_106, tag->info.abtUid, tag->info.szUidLen, &pnti)) { tag->active = 1; for (int i = 0; i < MIFARE_ULTRALIGHT_PAGE_COUNT; i++) MIFARE_ULTRALIGHT(tag)->cached_pages[i] = 0; diff --git a/test/test_mifare_classic.c b/test/test_mifare_classic.c index 1c7c596..6e9ca5a 100644 --- a/test/test_mifare_classic.c +++ b/test/test_mifare_classic.c @@ -357,3 +357,15 @@ test_mifare_classic_sector_boundaries (void) } } } + +void +test_mifare_classic_get_uid_freefare_get_tag_uid (void) +{ + char *s1 = mifare_classic_get_uid (tag); + char *s2 = freefare_get_tag_uid (tag); + + cut_assert_equal_string (s1, s2, cut_message ("UID don't match")); + + free (s1); + free (s2); +} diff --git a/test/test_mifare_ultralight.c b/test/test_mifare_ultralight.c index caaf891..52bda59 100644 --- a/test/test_mifare_ultralight.c +++ b/test/test_mifare_ultralight.c @@ -153,3 +153,15 @@ test_mifare_ultralight_get_uid (void) free (uid); } + +void +test_mifare_ultralight_get_uid_freefare_get_tag_uid (void) +{ + char *s1 = mifare_ultralight_get_uid (tag); + char *s2 = freefare_get_tag_uid (tag); + + cut_assert_equal_string (s1, s2, cut_message ("UID don't match")); + + free (s1); + free (s2); +}