Instead of copying n fields, setup a pointer to the relevant supported_tag information in mifare_tag structures.

This commit is contained in:
Romain Tartiere 2010-03-01 14:19:48 +00:00
parent 0d4744001a
commit 0f72871eb3
2 changed files with 16 additions and 20 deletions

View file

@ -23,12 +23,6 @@
#include "freefare_internal.h" #include "freefare_internal.h"
struct supported_tag {
uint8_t ATQA[2], SAK;
enum mifare_tag_type type;
const char *friendly_name;
};
struct supported_tag supported_tags[] = { struct supported_tag supported_tags[] = {
{ { 0x00, 0x44 }, 0x00, ULTRALIGHT, "Mifare UltraLight" }, { { 0x00, 0x44 }, 0x00, ULTRALIGHT, "Mifare UltraLight" },
{ { 0x00, 0x04 }, 0x08, CLASSIC_1K, "Mifare Classic 1k" }, { { 0x00, 0x04 }, 0x08, CLASSIC_1K, "Mifare Classic 1k" },
@ -81,16 +75,14 @@ freefare_get_tags (nfc_device_t *device)
while (nfc_initiator_select_tag(device,NM_ISO14443A_106,NULL,0,&target_info)) { while (nfc_initiator_select_tag(device,NM_ISO14443A_106,NULL,0,&target_info)) {
bool found = false; bool found = false;
enum mifare_tag_type type; struct supported_tag *tag_info;
const char *friendly_name;
for (int i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) { for (int i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) {
if ((target_info.nai.abtAtqa[0] == supported_tags[i].ATQA[0]) && if ((target_info.nai.abtAtqa[0] == supported_tags[i].ATQA[0]) &&
(target_info.nai.abtAtqa[1] == supported_tags[i].ATQA[1]) && (target_info.nai.abtAtqa[1] == supported_tags[i].ATQA[1]) &&
(target_info.nai.btSak == supported_tags[i].SAK)) { (target_info.nai.btSak == supported_tags[i].SAK)) {
type = supported_tags[i].type; tag_info = &(supported_tags[i]);
friendly_name = supported_tags[i].friendly_name;
found = true; found = true;
break; break;
} }
@ -109,7 +101,7 @@ freefare_get_tags (nfc_device_t *device)
return tags; // FAIL! Return what has been found so far. return tags; // FAIL! Return what has been found so far.
/* Allocate memory for the found MIFARE target */ /* Allocate memory for the found MIFARE target */
switch (type) { switch (tag_info->type) {
case CLASSIC_1K: case CLASSIC_1K:
case CLASSIC_4K: case CLASSIC_4K:
tags[tag_count-1] = mifare_classic_tag_new (); tags[tag_count-1] = mifare_classic_tag_new ();
@ -129,8 +121,7 @@ freefare_get_tags (nfc_device_t *device)
(tags[tag_count-1])->device = device; (tags[tag_count-1])->device = device;
(tags[tag_count-1])->info = target_info.nai; (tags[tag_count-1])->info = target_info.nai;
(tags[tag_count-1])->active = 0; (tags[tag_count-1])->active = 0;
(tags[tag_count-1])->type = type; (tags[tag_count-1])->tag_info = tag_info;
(tags[tag_count-1])->friendly_name = friendly_name;
tags[tag_count] = NULL; tags[tag_count] = NULL;
nfc_initiator_deselect_tag (device); nfc_initiator_deselect_tag (device);
@ -145,7 +136,7 @@ freefare_get_tags (nfc_device_t *device)
enum mifare_tag_type enum mifare_tag_type
freefare_get_tag_type (MifareTag tag) freefare_get_tag_type (MifareTag tag)
{ {
return tag->type; return tag->tag_info->type;
} }
/* /*
@ -154,7 +145,7 @@ freefare_get_tag_type (MifareTag tag)
const char * const char *
freefare_get_tag_friendly_name (MifareTag tag) freefare_get_tag_friendly_name (MifareTag tag)
{ {
return tag->friendly_name; return tag->tag_info->friendly_name;
} }
/* /*
@ -165,7 +156,7 @@ freefare_free_tags (MifareTag *tags)
{ {
if (tags) { if (tags) {
for (int i=0; tags[i]; i++) { for (int i=0; tags[i]; i++) {
switch (tags[i]->type) { switch (tags[i]->tag_info->type) {
case CLASSIC_1K: case CLASSIC_1K:
case CLASSIC_4K: case CLASSIC_4K:
mifare_classic_tag_free (tags[i]); mifare_classic_tag_free (tags[i]);

View file

@ -51,6 +51,12 @@ MifareClassicBlockNumber mifare_classic_last_sector_block (MifareClassicBlockNu
#define MIFARE_ULTRALIGHT_PAGE_COUNT 16 #define MIFARE_ULTRALIGHT_PAGE_COUNT 16
struct supported_tag {
uint8_t ATQA[2], SAK;
enum mifare_tag_type type;
const char *friendly_name;
};
/* /*
* This structure is common to all supported MIFARE targets but shall not be * This structure is common to all supported MIFARE targets but shall not be
* used directly (it's some kind of abstract class). All members in this * used directly (it's some kind of abstract class). All members in this
@ -62,8 +68,7 @@ MifareClassicBlockNumber mifare_classic_last_sector_block (MifareClassicBlockNu
struct mifare_tag { struct mifare_tag {
nfc_device_t *device; nfc_device_t *device;
nfc_iso14443a_info_t info; nfc_iso14443a_info_t info;
enum mifare_tag_type type; const struct supported_tag *tag_info;
const char *friendly_name;
int active; int active;
}; };
@ -101,8 +106,8 @@ struct mifare_ultralight_tag {
#define ASSERT_ACTIVE(tag) do { if (!tag->active) return errno = ENXIO, -1; } while (0) #define ASSERT_ACTIVE(tag) do { if (!tag->active) return errno = ENXIO, -1; } while (0)
#define ASSERT_INACTIVE(tag) do { if (tag->active) return errno = ENXIO, -1; } while (0) #define ASSERT_INACTIVE(tag) do { if (tag->active) return errno = ENXIO, -1; } while (0)
#define ASSERT_MIFARE_ULTRALIGHT(tag) do { if (tag->type != ULTRALIGHT) return errno = ENODEV, -1; } while (0) #define ASSERT_MIFARE_ULTRALIGHT(tag) do { if (tag->tag_info->type != ULTRALIGHT) return errno = ENODEV, -1; } while (0)
#define ASSERT_MIFARE_CLASSIC(tag) do { if ((tag->type != CLASSIC_1K) && (tag->type != CLASSIC_4K)) return errno = ENODEV, -1; } while (0) #define ASSERT_MIFARE_CLASSIC(tag) do { if ((tag->tag_info->type != CLASSIC_1K) && (tag->tag_info->type != CLASSIC_4K)) return errno = ENODEV, -1; } while (0)
/* /*
* MifareTag cast macros * MifareTag cast macros