update to use libnfc's trunk
This commit is contained in:
parent
21008cda5d
commit
4f0227ddcb
20 changed files with 330 additions and 295 deletions
|
|
@ -41,7 +41,7 @@ struct supported_tag supported_tags[] = {
|
|||
* Automagically allocate a MifareTag given a device and target info.
|
||||
*/
|
||||
MifareTag
|
||||
freefare_tag_new (nfc_device_t *device, nfc_iso14443a_info_t nai)
|
||||
freefare_tag_new (nfc_device *device, nfc_iso14443a_info nai)
|
||||
{
|
||||
bool found = false;
|
||||
struct supported_tag *tag_info;
|
||||
|
|
@ -110,7 +110,7 @@ freefare_tag_new (nfc_device_t *device, nfc_iso14443a_info_t nai)
|
|||
* The list has to be freed using the freefare_free_tags() function.
|
||||
*/
|
||||
MifareTag *
|
||||
freefare_get_tags (nfc_device_t *device)
|
||||
freefare_get_tags (nfc_device *device)
|
||||
{
|
||||
MifareTag *tags = NULL;
|
||||
int tag_count = 0;
|
||||
|
|
@ -118,24 +118,24 @@ freefare_get_tags (nfc_device_t *device)
|
|||
nfc_initiator_init(device);
|
||||
|
||||
// Drop the field for a while
|
||||
nfc_configure(device,NDO_ACTIVATE_FIELD,false);
|
||||
nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,false);
|
||||
|
||||
// Configure the CRC and Parity settings
|
||||
nfc_configure(device,NDO_HANDLE_CRC,true);
|
||||
nfc_configure(device,NDO_HANDLE_PARITY,true);
|
||||
nfc_configure(device,NDO_AUTO_ISO14443_4,true);
|
||||
nfc_device_set_property_bool(device,NP_HANDLE_CRC,true);
|
||||
nfc_device_set_property_bool(device,NP_HANDLE_PARITY,true);
|
||||
nfc_device_set_property_bool(device,NP_AUTO_ISO14443_4,true);
|
||||
|
||||
// Enable field so more power consuming cards can power themselves up
|
||||
nfc_configure(device,NDO_ACTIVATE_FIELD,true);
|
||||
nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,true);
|
||||
|
||||
// Poll for a ISO14443A (MIFARE) tag
|
||||
nfc_target_t candidates[MAX_CANDIDATES];
|
||||
nfc_target candidates[MAX_CANDIDATES];
|
||||
size_t candidates_count;
|
||||
nfc_modulation_t modulation = {
|
||||
nfc_modulation modulation = {
|
||||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106
|
||||
};
|
||||
if (!nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES, &candidates_count))
|
||||
if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0)
|
||||
return NULL;
|
||||
|
||||
tags = malloc(sizeof (void *));
|
||||
|
|
@ -185,7 +185,7 @@ freefare_get_tag_uid (MifareTag tag)
|
|||
{
|
||||
char *res = malloc (2 * tag->info.szUidLen + 1);
|
||||
for (size_t i =0; i < tag->info.szUidLen; i++)
|
||||
snprintf (res + 2*i, 3, "%02x", tag->info.abtUid[i]);
|
||||
snprintf (res + 2*i, 3, "%02x", tag->info.abtUid[i]);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
@ -196,19 +196,19 @@ 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 DESFIRE:
|
||||
mifare_desfire_tag_free (tag);
|
||||
break;
|
||||
case ULTRALIGHT:
|
||||
case ULTRALIGHT_C:
|
||||
mifare_ultralight_tag_free (tag);
|
||||
break;
|
||||
}
|
||||
switch (tag->tag_info->type) {
|
||||
case CLASSIC_1K:
|
||||
case CLASSIC_4K:
|
||||
mifare_classic_tag_free (tag);
|
||||
break;
|
||||
case DESFIRE:
|
||||
mifare_desfire_tag_free (tag);
|
||||
break;
|
||||
case ULTRALIGHT:
|
||||
case ULTRALIGHT_C:
|
||||
mifare_ultralight_tag_free (tag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -216,18 +216,17 @@ const char *
|
|||
freefare_strerror (MifareTag tag)
|
||||
{
|
||||
const char *p = "Unkown error";
|
||||
if (tag->device->iLastError > 0) {
|
||||
p = nfc_strerror (tag->device);
|
||||
if (nfc_device_get_last_error (tag->device) < 0) {
|
||||
p = nfc_strerror (tag->device);
|
||||
} else {
|
||||
if (tag->tag_info->type == DESFIRE) {
|
||||
if (MIFARE_DESFIRE (tag)->last_pcd_error) {
|
||||
p = mifare_desfire_error_lookup (MIFARE_DESFIRE (tag)->last_pcd_error);
|
||||
} else if (MIFARE_DESFIRE (tag)->last_picc_error) {
|
||||
p = mifare_desfire_error_lookup (MIFARE_DESFIRE (tag)->last_picc_error);
|
||||
}
|
||||
}
|
||||
if (tag->tag_info->type == DESFIRE) {
|
||||
if (MIFARE_DESFIRE (tag)->last_pcd_error) {
|
||||
p = mifare_desfire_error_lookup (MIFARE_DESFIRE (tag)->last_pcd_error);
|
||||
} else if (MIFARE_DESFIRE (tag)->last_picc_error) {
|
||||
p = mifare_desfire_error_lookup (MIFARE_DESFIRE (tag)->last_picc_error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
|
@ -257,7 +256,6 @@ freefare_free_tags (MifareTag *tags)
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Low-level API
|
||||
|
|
|
|||
|
|
@ -52,8 +52,8 @@ typedef struct mifare_desfire_key *MifareDESFireKey;
|
|||
typedef uint8_t MifareUltralightPageNumber;
|
||||
typedef unsigned char MifareUltralightPage[4];
|
||||
|
||||
MifareTag *freefare_get_tags (nfc_device_t *device);
|
||||
MifareTag freefare_tag_new (nfc_device_t *device, nfc_iso14443a_info_t nai);
|
||||
MifareTag *freefare_get_tags (nfc_device *device);
|
||||
MifareTag freefare_tag_new (nfc_device *device, nfc_iso14443a_info nai);
|
||||
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);
|
||||
|
|
@ -71,7 +71,7 @@ int mifare_ultralight_read (MifareTag tag, const MifareUltralightPageNumber pa
|
|||
int mifare_ultralight_write (MifareTag tag, const MifareUltralightPageNumber page, const MifareUltralightPage data);
|
||||
|
||||
int mifare_ultralightc_authenticate (MifareTag tag, const MifareDESFireKey key);
|
||||
bool is_mifare_ultralightc_on_reader (nfc_device_t *device, nfc_iso14443a_info_t nai);
|
||||
bool is_mifare_ultralightc_on_reader (nfc_device *device, nfc_iso14443a_info nai);
|
||||
|
||||
typedef unsigned char MifareClassicBlock[16];
|
||||
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ struct supported_tag {
|
|||
uint8_t ATS_min_length;
|
||||
uint8_t ATS_compare_length;
|
||||
uint8_t ATS[5];
|
||||
bool (*check_tag_on_reader) (nfc_device_t *, nfc_iso14443a_info_t);
|
||||
bool (*check_tag_on_reader) (nfc_device *, nfc_iso14443a_info);
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
@ -185,8 +185,8 @@ struct supported_tag {
|
|||
* mifare_*_connect() function.
|
||||
*/
|
||||
struct mifare_tag {
|
||||
nfc_device_t *device;
|
||||
nfc_iso14443a_info_t info;
|
||||
nfc_device *device;
|
||||
nfc_iso14443a_info info;
|
||||
const struct supported_tag *tag_info;
|
||||
int active;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@
|
|||
do { \
|
||||
errno = 0; \
|
||||
DEBUG_XFER (msg, __##msg##_n, "===> "); \
|
||||
if (!(nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, NULL))) { \
|
||||
if ((nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, 0) < 0)) { \
|
||||
if (disconnect) { \
|
||||
tag->active = false; \
|
||||
} \
|
||||
|
|
@ -228,12 +228,12 @@ mifare_classic_connect (MifareTag tag)
|
|||
ASSERT_INACTIVE (tag);
|
||||
ASSERT_MIFARE_CLASSIC (tag);
|
||||
|
||||
nfc_target_t pnti;
|
||||
nfc_modulation_t modulation = {
|
||||
nfc_target pnti;
|
||||
nfc_modulation modulation = {
|
||||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106
|
||||
};
|
||||
if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti)) {
|
||||
if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti) >= 0) {
|
||||
tag->active = 1;
|
||||
} else {
|
||||
errno = EIO;
|
||||
|
|
@ -251,7 +251,7 @@ mifare_classic_disconnect (MifareTag tag)
|
|||
ASSERT_ACTIVE (tag);
|
||||
ASSERT_MIFARE_CLASSIC (tag);
|
||||
|
||||
if (nfc_initiator_deselect_target (tag->device)) {
|
||||
if (nfc_initiator_deselect_target (tag->device) >= 0) {
|
||||
tag->active = 0;
|
||||
} else {
|
||||
errno = EIO;
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ static ssize_t read_data (MifareTag tag, uint8_t command, uint8_t file_no, off_
|
|||
MIFARE_DESFIRE (tag)->last_picc_error = OPERATION_OK; \
|
||||
MIFARE_DESFIRE (tag)->last_pcd_error = OPERATION_OK; \
|
||||
DEBUG_XFER (__msg, __len, "===> "); \
|
||||
if (!(nfc_initiator_transceive_bytes (tag->device, __msg, __len, __res, &__##res##_n, NULL))) { \
|
||||
if ((nfc_initiator_transceive_bytes (tag->device, __msg, __len, __res, &__##res##_n, 0)) < 0) { \
|
||||
return errno = EIO, -1; \
|
||||
} \
|
||||
DEBUG_XFER (__res, __##res##_n, "<=== "); \
|
||||
|
|
@ -276,12 +276,12 @@ mifare_desfire_connect (MifareTag tag)
|
|||
ASSERT_INACTIVE (tag);
|
||||
ASSERT_MIFARE_DESFIRE (tag);
|
||||
|
||||
nfc_target_t pnti;
|
||||
nfc_modulation_t modulation = {
|
||||
nfc_target pnti;
|
||||
nfc_modulation modulation = {
|
||||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106
|
||||
};
|
||||
if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti)) {
|
||||
if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti) >= 0) {
|
||||
tag->active = 1;
|
||||
free (MIFARE_DESFIRE (tag)->session_key);
|
||||
MIFARE_DESFIRE (tag)->session_key = NULL;
|
||||
|
|
@ -308,7 +308,7 @@ mifare_desfire_disconnect (MifareTag tag)
|
|||
free (MIFARE_DESFIRE (tag)->session_key);
|
||||
MIFARE_DESFIRE(tag)->session_key = NULL;
|
||||
|
||||
if (nfc_initiator_deselect_target (tag->device)) {
|
||||
if (nfc_initiator_deselect_target (tag->device) >= 0) {
|
||||
tag->active = 0;
|
||||
}
|
||||
return 0;
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@
|
|||
do { \
|
||||
errno = 0; \
|
||||
DEBUG_XFER (msg, __##msg##_n, "===> "); \
|
||||
if (!(nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, NULL))) { \
|
||||
if ((nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, 0)) < 0) { \
|
||||
return errno = EIO, -1; \
|
||||
} \
|
||||
DEBUG_XFER (res, __##res##_n, "<=== "); \
|
||||
|
|
@ -71,17 +71,17 @@
|
|||
#define ULTRALIGHT_TRANSCEIVE_RAW(tag, msg, res) \
|
||||
do { \
|
||||
errno = 0; \
|
||||
if (!nfc_configure (tag->device, NDO_EASY_FRAMING, false)) { \
|
||||
if (nfc_device_set_property_bool (tag->device, NP_EASY_FRAMING, false) < 0) { \
|
||||
errno = EIO; \
|
||||
return -1; \
|
||||
} \
|
||||
DEBUG_XFER (msg, __##msg##_n, "===> "); \
|
||||
if (!(nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, NULL))) { \
|
||||
nfc_configure (tag->device, NDO_EASY_FRAMING, true); \
|
||||
if ((nfc_initiator_transceive_bytes (tag->device, msg, __##msg##_n, res, &__##res##_n, 0)) < 0) { \
|
||||
nfc_device_set_property_bool (tag->device, NP_EASY_FRAMING, true); \
|
||||
return errno = EIO, -1; \
|
||||
} \
|
||||
DEBUG_XFER (res, __##res##_n, "<=== "); \
|
||||
if (!nfc_configure (tag->device, NDO_EASY_FRAMING, true)) { \
|
||||
if (nfc_device_set_property_bool (tag->device, NP_EASY_FRAMING, true) < 0) { \
|
||||
errno = EIO; \
|
||||
return -1; \
|
||||
} \
|
||||
|
|
@ -129,12 +129,12 @@ mifare_ultralight_connect (MifareTag tag)
|
|||
ASSERT_INACTIVE (tag);
|
||||
ASSERT_MIFARE_ULTRALIGHT (tag);
|
||||
|
||||
nfc_target_t pnti;
|
||||
nfc_modulation_t modulation = {
|
||||
nfc_target pnti;
|
||||
nfc_modulation modulation = {
|
||||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106
|
||||
};
|
||||
if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti)) {
|
||||
if (nfc_initiator_select_passive_target (tag->device, modulation, tag->info.abtUid, tag->info.szUidLen, &pnti) >= 0) {
|
||||
tag->active = 1;
|
||||
for (int i = 0; i < MIFARE_ULTRALIGHT_MAX_PAGE_COUNT; i++)
|
||||
MIFARE_ULTRALIGHT(tag)->cached_pages[i] = 0;
|
||||
|
|
@ -154,7 +154,7 @@ mifare_ultralight_disconnect (MifareTag tag)
|
|||
ASSERT_ACTIVE (tag);
|
||||
ASSERT_MIFARE_ULTRALIGHT (tag);
|
||||
|
||||
if (nfc_initiator_deselect_target (tag->device)) {
|
||||
if (nfc_initiator_deselect_target (tag->device) >= 0) {
|
||||
tag->active = 0;
|
||||
} else {
|
||||
errno = EIO;
|
||||
|
|
@ -307,7 +307,7 @@ mifare_ultralightc_authenticate (MifareTag tag, const MifareDESFireKey key)
|
|||
* Callback for freefare_tag_new to test presence of a MIFARE UltralightC on the reader.
|
||||
*/
|
||||
bool
|
||||
is_mifare_ultralightc_on_reader (nfc_device_t *device, nfc_iso14443a_info_t nai)
|
||||
is_mifare_ultralightc_on_reader (nfc_device *device, nfc_iso14443a_info nai)
|
||||
{
|
||||
bool ret;
|
||||
uint8_t cmd_step1[2];
|
||||
|
|
@ -315,16 +315,16 @@ is_mifare_ultralightc_on_reader (nfc_device_t *device, nfc_iso14443a_info_t nai)
|
|||
cmd_step1[0] = 0x1A;
|
||||
cmd_step1[1] = 0x00;
|
||||
|
||||
nfc_target_t pnti;
|
||||
nfc_modulation_t modulation = {
|
||||
nfc_target pnti;
|
||||
nfc_modulation modulation = {
|
||||
.nmt = NMT_ISO14443A,
|
||||
.nbr = NBR_106
|
||||
};
|
||||
nfc_initiator_select_passive_target (device, modulation, nai.abtUid, nai.szUidLen, &pnti);
|
||||
nfc_configure (device, NDO_EASY_FRAMING, false);
|
||||
nfc_device_set_property_bool (device, NP_EASY_FRAMING, false);
|
||||
size_t n;
|
||||
ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, &n, NULL);
|
||||
nfc_configure (device, NDO_EASY_FRAMING, true);
|
||||
ret = nfc_initiator_transceive_bytes (device, cmd_step1, sizeof (cmd_step1), res_step1, &n, 0);
|
||||
nfc_device_set_property_bool (device, NP_EASY_FRAMING, true);
|
||||
nfc_initiator_deselect_target (device);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue