From 99b261d132d07fb4a23003cf50d160be0afb3e25 Mon Sep 17 00:00:00 2001 From: Romuald Conty Date: Tue, 20 Apr 2010 15:17:17 +0000 Subject: [PATCH] add freefare_free_tag() to free only one tag and add freefare_duplicate_tag() in order to copy a tag struct. --- libfreefare/freefare.c | 55 +++++++++++++++++++++++++++++++++++------- libfreefare/freefare.h | 2 ++ 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/libfreefare/freefare.c b/libfreefare/freefare.c index b3b63b2..853ff51 100644 --- a/libfreefare/freefare.c +++ b/libfreefare/freefare.c @@ -130,6 +130,32 @@ freefare_get_tags (nfc_device_t *device) return tags; } +/* + * Duplicate a tag + */ +MifareTag +freefare_duplicate_tag (MifareTag tag) +{ + MifareTag ret; + + /* Allocate memory for the MIFARE target */ + switch (tag->tag_info->type) { + case CLASSIC_1K: + case CLASSIC_4K: + ret = mifare_classic_tag_new (); + + break; + case ULTRALIGHT: + ret = mifare_ultralight_tag_new (); + break; + } + ret->device = tag->device; + ret->info = tag->info; + ret->active = tag->active; + ret->tag_info = tag->tag_info; + return ret; +} + /* * Returns the type of the provided tag. */ @@ -160,6 +186,25 @@ freefare_get_tag_uid (MifareTag tag) return res; } +/* + * Free the provided tag. + */ +void +freefare_free_tag (MifareTag tag) +{ + if (tag) { + switch (tag->tag_info->type) { + case CLASSIC_1K: + case CLASSIC_4K: + mifare_classic_tag_free (tag); + break; + case ULTRALIGHT: + mifare_ultralight_tag_free (tag); + break; + } + } +} + /* * Free the provided tag list. */ @@ -168,15 +213,7 @@ freefare_free_tags (MifareTag *tags) { if (tags) { for (int i=0; tags[i]; i++) { - switch (tags[i]->tag_info->type) { - case CLASSIC_1K: - case CLASSIC_4K: - mifare_classic_tag_free (tags[i]); - break; - case ULTRALIGHT: - mifare_ultralight_tag_free (tags[i]); - break; - } + freefare_free_tag(tags[i]); } free (tags); } diff --git a/libfreefare/freefare.h b/libfreefare/freefare.h index f1475fa..1f1ae89 100644 --- a/libfreefare/freefare.h +++ b/libfreefare/freefare.h @@ -52,9 +52,11 @@ typedef uint8_t MifareUltralightPageNumber; typedef unsigned char MifareUltralightPage[4]; MifareTag *freefare_get_tags (nfc_device_t *device); +MifareTag freefare_duplicate_tag (MifareTag tag); 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_tag (MifareTag tag); void freefare_free_tags (MifareTag *tags); int mifare_ultralight_connect (MifareTag tag);