libfreefare/test/mifare_classic_test.c

86 lines
1.3 KiB
C
Raw Normal View History

2009-12-19 03:06:21 +01:00
#include "test.h"
static nfc_device_t *device = NULL;
static MifareClassicTag *tags = NULL;
2009-12-19 03:06:21 +01:00
int
mifare_classic_test_setup (MifareClassicTag *tag)
{
int res = 0;
*tag = NULL;
2009-12-19 03:06:21 +01:00
device = nfc_connect (NULL);
if (!device)
res = -1;
if (0 == res) {
tags = mifare_classic_get_tags (device);
if (!tags) {
2009-12-19 03:06:21 +01:00
nfc_disconnect (device);
device = NULL;
2009-12-19 03:06:21 +01:00
res = -2;
}
}
if (0 == res) {
if (!tags[0]) {
mifare_classic_free_tags (tags);
tags = NULL;
nfc_disconnect (device);
device = NULL;
res = -4;
}
}
2009-12-19 03:06:21 +01:00
if (0 == res) {
*tag = tags[0];
res = mifare_classic_connect (*tag);
if (res != 0) {
mifare_classic_disconnect (*tag);
nfc_disconnect (device);
*tag = NULL;
device = NULL;
2009-12-19 03:06:21 +01:00
res = -3;
}
}
return res;
}
int
mifare_classic_test_teardown (MifareClassicTag tag)
{
int res;
if (tag)
res = mifare_classic_disconnect (tag);
2009-12-19 03:06:21 +01:00
if (0 == res) {
if (tags)
mifare_classic_free_tags (tags);
2009-12-19 03:06:21 +01:00
if (device)
nfc_disconnect (device);
2009-12-19 03:06:21 +01:00
}
return res;
}
int
read_data_block (char *filename, MifareClassicBlock *block)
{
FILE *f = fopen (filename, "r");
if (f == NULL)
return -1;
char buffer[17];
fgets(buffer, 17, f);
memcpy (block, buffer, 16);
fclose (f);
return 0;
}