From 4f0227ddcb76a0c57452a2de52eada9f692df65d Mon Sep 17 00:00:00 2001 From: Audrey Diacre Date: Wed, 25 Jan 2012 09:58:16 +0000 Subject: [PATCH] update to use libnfc's trunk --- examples/mifare-classic-format.c | 20 +++-- examples/mifare-classic-read-ndef.c | 23 +++--- examples/mifare-classic-write-ndef.c | 24 +++--- examples/mifare-desfire-access.c | 22 +++--- examples/mifare-desfire-ev1-configure-ats.c | 22 +++--- ...mifare-desfire-ev1-configure-default-key.c | 22 +++--- .../mifare-desfire-ev1-configure-random-uid.c | 22 +++--- examples/mifare-desfire-format.c | 22 +++--- examples/mifare-desfire-info.c | 22 +++--- examples/mifare-desfire-write-ndef.c | 24 +++--- libfreefare/freefare.c | 68 ++++++++--------- libfreefare/freefare.h | 6 +- libfreefare/freefare_internal.h | 6 +- libfreefare/mifare_classic.c | 10 +-- libfreefare/mifare_desfire.c | 10 +-- libfreefare/mifare_ultralight.c | 30 ++++---- test/mifare_classic_fixture.c | 62 +++++++-------- test/mifare_desfire_ev1_fixture.c | 75 ++++++++++--------- test/mifare_desfire_fixture.c | 75 ++++++++++--------- test/mifare_ultralight_fixture.c | 60 ++++++++------- 20 files changed, 330 insertions(+), 295 deletions(-) diff --git a/examples/mifare-classic-format.c b/examples/mifare-classic-format.c index 883e97f..5a84e4b 100644 --- a/examples/mifare-classic-format.c +++ b/examples/mifare-classic-format.c @@ -139,7 +139,7 @@ main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "fhy")) != -1) { @@ -161,25 +161,28 @@ main(int argc, char *argv[]) } argc -= optind; argv += optind; + + nfc_connstring devices[8]; - nfc_device_desc_t devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); + device = nfc_open (NULL, devices[d]); if (!device) { - warnx ("nfc_connect() failed."); + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare Classic tag."); } @@ -242,8 +245,9 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } + nfc_exit(NULL); exit (error); } diff --git a/examples/mifare-classic-read-ndef.c b/examples/mifare-classic-read-ndef.c index c71e0b5..f244b70 100644 --- a/examples/mifare-classic-read-ndef.c +++ b/examples/mifare-classic-read-ndef.c @@ -59,7 +59,7 @@ int main(int argc, char *argv[]) { int error = 0; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; Mad mad; @@ -102,24 +102,26 @@ main(int argc, char *argv[]) } } - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count= nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing MIFARE classic tag."); } @@ -206,8 +208,9 @@ error: } fclose (ndef_stream); freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + + nfc_exit(NULL); exit (error); } diff --git a/examples/mifare-classic-write-ndef.c b/examples/mifare-classic-write-ndef.c index d172329..2a9d711 100644 --- a/examples/mifare-classic-write-ndef.c +++ b/examples/mifare-classic-write-ndef.c @@ -101,7 +101,7 @@ search_sector_key (MifareTag tag, MifareClassicSectorNumber sector, MifareClassi } int -fix_mad_trailer_block (nfc_device_t *device, MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey key, MifareClassicKeyType key_type) +fix_mad_trailer_block (nfc_device *device, MifareTag tag, MifareClassicSectorNumber sector, MifareClassicKey key, MifareClassicKeyType key_type) { MifareClassicBlock block; mifare_classic_trailer_block (&block, mad_public_key_a, 0x0, 0x1, 0x1, 0x6, 0x00, default_keyb); @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) { int error = 0; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; Mad mad; MifareClassicKey transport_key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; @@ -191,24 +191,26 @@ main(int argc, char *argv[]) err (EXIT_FAILURE, "malloc"); } - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing MIFARE classic tag."); } @@ -399,10 +401,10 @@ error: } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } free (card_write_keys); - + nfc_exit(NULL); exit (error); } diff --git a/examples/mifare-desfire-access.c b/examples/mifare-desfire-access.c index 0c36d3b..5da2e0c 100644 --- a/examples/mifare-desfire-access.c +++ b/examples/mifare-desfire-access.c @@ -33,30 +33,32 @@ int main(int argc, char *argv[]) { int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; if (argc > 1) errx (EXIT_FAILURE, "usage: %s", argv[0]); - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing tags."); } @@ -128,9 +130,9 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + nfc_exit(NULL); exit (error); } /* main() */ diff --git a/examples/mifare-desfire-ev1-configure-ats.c b/examples/mifare-desfire-ev1-configure-ats.c index 812c731..2e8fa79 100644 --- a/examples/mifare-desfire-ev1-configure-ats.c +++ b/examples/mifare-desfire-ev1-configure-ats.c @@ -53,7 +53,7 @@ main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "hy")) != -1) { @@ -73,24 +73,26 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; (!error) && (d < device_count); d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); } @@ -146,9 +148,9 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + nfc_exit(NULL); exit (error); } /* main() */ diff --git a/examples/mifare-desfire-ev1-configure-default-key.c b/examples/mifare-desfire-ev1-configure-default-key.c index 4a82748..66dbeac 100644 --- a/examples/mifare-desfire-ev1-configure-default-key.c +++ b/examples/mifare-desfire-ev1-configure-default-key.c @@ -53,7 +53,7 @@ main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "hy")) != -1) { @@ -73,24 +73,26 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; (!error) && (d < device_count); d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); } @@ -230,8 +232,8 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + nfc_exit(NULL); exit (error); } diff --git a/examples/mifare-desfire-ev1-configure-random-uid.c b/examples/mifare-desfire-ev1-configure-random-uid.c index ba159d5..459400b 100644 --- a/examples/mifare-desfire-ev1-configure-random-uid.c +++ b/examples/mifare-desfire-ev1-configure-random-uid.c @@ -50,7 +50,7 @@ main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "hy")) != -1) { @@ -70,24 +70,26 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; (!error) && (d < device_count); d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); } @@ -182,9 +184,9 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + nfc_exit(NULL); exit (error); } /* main() */ diff --git a/examples/mifare-desfire-format.c b/examples/mifare-desfire-format.c index 71d7a64..7fd14cf 100644 --- a/examples/mifare-desfire-format.c +++ b/examples/mifare-desfire-format.c @@ -50,7 +50,7 @@ main(int argc, char *argv[]) { int ch; int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; while ((ch = getopt (argc, argv, "hy")) != -1) { @@ -70,24 +70,26 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; (!error) && (d < device_count); d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing Mifare DESFire tags."); } @@ -141,9 +143,9 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + nfc_exit(NULL); exit (error); } /* main() */ diff --git a/examples/mifare-desfire-info.c b/examples/mifare-desfire-info.c index ec6fcb8..4d5da04 100644 --- a/examples/mifare-desfire-info.c +++ b/examples/mifare-desfire-info.c @@ -31,30 +31,32 @@ int main(int argc, char *argv[]) { int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; if (argc > 1) errx (EXIT_FAILURE, "usage: %s", argv[0]); - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing tags."); } @@ -138,9 +140,9 @@ main(int argc, char *argv[]) } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } - + nfc_exit(NULL); exit (error); } /* main() */ diff --git a/examples/mifare-desfire-write-ndef.c b/examples/mifare-desfire-write-ndef.c index 3152dae..b552f17 100644 --- a/examples/mifare-desfire-write-ndef.c +++ b/examples/mifare-desfire-write-ndef.c @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) { int error = EXIT_SUCCESS; - nfc_device_t *device = NULL; + nfc_device *device = NULL; MifareTag *tags = NULL; printf ("NOTE: This application turns Mifare DESFire targets into NFC Forum Type 4 Tags.\n"); @@ -56,24 +56,27 @@ main(int argc, char *argv[]) if (argc > 1) errx (EXIT_FAILURE, "usage: %s", argv[0]); - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) errx (EXIT_FAILURE, "No NFC device found."); for (size_t d = 0; d < device_count; d++) { - device = nfc_connect (&(devices[d])); - if (!device) { - warnx ("nfc_connect() failed."); + device = nfc_open (NULL, devices[d]); + + if (!device) { + warnx ("nfc_open() failed."); error = EXIT_FAILURE; continue; } tags = freefare_get_tags (device); if (!tags) { - nfc_disconnect (device); + nfc_close (device); errx (EXIT_FAILURE, "Error listing tags."); } @@ -159,7 +162,7 @@ main(int argc, char *argv[]) // Mapping Version equal to 10h,MLe equal to 003Bh, MLc equal to 0034h, and NDEF File Control TLV // equal to T =04h, L=06h, V=E1 04 (NDEF ISO FID=E104h) 0E E0 (NDEF File size =3808 Bytes) 00 (free read access) // 00 free write access - byte_t capability_container_file_content[15] = { + uint8_t capability_container_file_content[15] = { 0x00, 0x0F, // CCLEN: Size of this capability container.CCLEN values are between 000Fh and FFFEh 0x10, // Mapping version 0x00, 0x3B, // MLe: Maximum data size that can be read using a single ReadBinary command. MLe = 000Fh-FFFFh @@ -193,7 +196,8 @@ main(int argc, char *argv[]) free (tag_uid); } freefare_free_tags (tags); - nfc_disconnect (device); + nfc_close (device); } + nfc_exit(NULL); exit (error); } diff --git a/libfreefare/freefare.c b/libfreefare/freefare.c index f1f712d..3a9bc38 100644 --- a/libfreefare/freefare.c +++ b/libfreefare/freefare.c @@ -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 diff --git a/libfreefare/freefare.h b/libfreefare/freefare.h index 591269c..5083e21 100644 --- a/libfreefare/freefare.h +++ b/libfreefare/freefare.h @@ -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]; diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index cd0984a..9474adf 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -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; }; diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index 0a8d034..afcf54a 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -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; diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index d6f76e1..31f3ccb 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -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; diff --git a/libfreefare/mifare_ultralight.c b/libfreefare/mifare_ultralight.c index e755d6a..a31442c 100644 --- a/libfreefare/mifare_ultralight.c +++ b/libfreefare/mifare_ultralight.c @@ -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; } diff --git a/test/mifare_classic_fixture.c b/test/mifare_classic_fixture.c index 4e4fa4f..dee2e41 100644 --- a/test/mifare_classic_fixture.c +++ b/test/mifare_classic_fixture.c @@ -22,7 +22,7 @@ #include "mifare_classic_fixture.h" -static nfc_device_t *device = NULL; +static nfc_device *device = NULL; static MifareTag *tags = NULL; MifareTag tag = NULL; @@ -30,38 +30,38 @@ void cut_setup (void) { int res; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) - cut_omit ("No device found"); + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) + cut_omit ("No device found"); for (size_t i = 0; i < device_count; i++) { + device = nfc_open (NULL, devices[i]); + if (!device) + cut_omit ("nfc_open() failed."); - device = nfc_connect (&(devices[i])); - if (!device) - cut_omit ("nfc_connect() failed"); + tags = freefare_get_tags (device); + cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); - tags = freefare_get_tags (device); - cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); - - tag = NULL; - for (int i=0; tags[i]; i++) { - if ((freefare_get_tag_type(tags[i]) == CLASSIC_1K) || - (freefare_get_tag_type(tags[i]) == CLASSIC_4K)) { - tag = tags[i]; - res = mifare_classic_connect (tag); - cut_assert_equal_int (0, res, cut_message ("mifare_classic_connect() failed")); - return; - } - } - nfc_disconnect (device); - device = NULL; - freefare_free_tags (tags); - tags = NULL; + tag = NULL; + for (int i=0; tags[i]; i++) { + if ((freefare_get_tag_type(tags[i]) == CLASSIC_1K) || + (freefare_get_tag_type(tags[i]) == CLASSIC_4K)) { + tag = tags[i]; + res = mifare_classic_connect (tag); + cut_assert_equal_int (0, res, cut_message ("mifare_classic_connect() failed")); + return; + } + } + nfc_close (device); + device = NULL; + freefare_free_tags (tags); + tags = NULL; } - cut_omit ("No MIFARE Classic tag on NFC device"); } @@ -69,14 +69,16 @@ void cut_teardown (void) { if (tag) - mifare_classic_disconnect (tag); + mifare_classic_disconnect (tag); if (tags) { - freefare_free_tags (tags); - tags = NULL; + freefare_free_tags (tags); + tags = NULL; } if (device) - nfc_disconnect (device); + nfc_close (device); + + nfc_exit(NULL); } diff --git a/test/mifare_desfire_ev1_fixture.c b/test/mifare_desfire_ev1_fixture.c index 4cf9e49..234b0de 100644 --- a/test/mifare_desfire_ev1_fixture.c +++ b/test/mifare_desfire_ev1_fixture.c @@ -22,7 +22,7 @@ #include "mifare_desfire_ev1_fixture.h" -static nfc_device_t *device = NULL; +static nfc_device *device = NULL; static MifareTag *tags = NULL; MifareTag tag = NULL; @@ -30,47 +30,48 @@ void cut_setup (void) { int res; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) - cut_omit ("No device found"); + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) + cut_omit ("No device found"); for (size_t i = 0; i < device_count; i++) { - device = nfc_connect (&(devices[i])); - if (!device) - cut_omit ("nfc_connect() failed"); + device = nfc_open (NULL, devices[i]); + if (!device) + cut_omit ("nfc_open() failed."); - tags = freefare_get_tags (device); - cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); + tags = freefare_get_tags (device); + cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); - tag = NULL; - for (int i=0; tags[i]; i++) { - if (freefare_get_tag_type(tags[i]) == DESFIRE) { - tag = tags[i]; - res = mifare_desfire_connect (tag); - cut_assert_equal_int (0, res, cut_message ("mifare_desfire_connect() failed")); + tag = NULL; + for (int i=0; tags[i]; i++) { + if (freefare_get_tag_type(tags[i]) == DESFIRE) { + tag = tags[i]; + res = mifare_desfire_connect (tag); + cut_assert_equal_int (0, res, cut_message ("mifare_desfire_connect() failed")); - struct mifare_desfire_version_info version_info; - res = mifare_desfire_get_version (tag, &version_info); - cut_assert_equal_int (0, res, cut_message ("mifare_desfire_get_version")); + struct mifare_desfire_version_info version_info; + res = mifare_desfire_get_version (tag, &version_info); + cut_assert_equal_int (0, res, cut_message ("mifare_desfire_get_version")); - if ((version_info.hardware.version_major >= 1) && - (version_info.software.version_major >= 1)) { - return; - } + if ((version_info.hardware.version_major >= 1) && + (version_info.software.version_major >= 1)) { + return; + } - mifare_desfire_disconnect (tag); - } - } - nfc_disconnect (device); - device = NULL; - freefare_free_tags (tags); - tags = NULL; + mifare_desfire_disconnect (tag); + } + } + nfc_close (device); + device = NULL; + freefare_free_tags (tags); + tags = NULL; } - cut_omit ("No MIFARE DESFire EV1 tag on NFC device"); } @@ -78,14 +79,16 @@ void cut_teardown (void) { if (tag) - mifare_desfire_disconnect (tag); + mifare_desfire_disconnect (tag); if (tags) { - freefare_free_tags (tags); - tags = NULL; + freefare_free_tags (tags); + tags = NULL; } - if (device) - nfc_disconnect (device); + if (device) + nfc_close (device); + + nfc_exit(NULL); } diff --git a/test/mifare_desfire_fixture.c b/test/mifare_desfire_fixture.c index aad01b3..c62899b 100644 --- a/test/mifare_desfire_fixture.c +++ b/test/mifare_desfire_fixture.c @@ -22,60 +22,63 @@ #include "mifare_desfire_fixture.h" -static nfc_device_t *device = NULL; +static nfc_device *device = NULL; static MifareTag *tags = NULL; MifareTag tag = NULL; void cut_setup (void) { - int res; - nfc_device_desc_t devices[8]; - size_t device_count; + int res; + nfc_connstring devices[8]; + size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) - cut_omit ("No device found"); + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) + cut_omit ("No device found"); - for (size_t i = 0; i < device_count; i++) { + for (size_t i = 0; i < device_count; i++) { - device = nfc_connect (&(devices[i])); - if (!device) - cut_omit ("nfc_connect() failed"); + device = nfc_open (NULL, devices[i]); + if (!device) + cut_omit ("nfc_open() failed."); - tags = freefare_get_tags (device); - cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); + tags = freefare_get_tags (device); + cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); - tag = NULL; - for (int i=0; tags[i]; i++) { - if (freefare_get_tag_type(tags[i]) == DESFIRE) { - tag = tags[i]; - res = mifare_desfire_connect (tag); - cut_assert_equal_int (0, res, cut_message ("mifare_desfire_connect() failed")); - return; - } - } - nfc_disconnect (device); - device = NULL; - freefare_free_tags (tags); - tags = NULL; + tag = NULL; + for (int i=0; tags[i]; i++) { + if (freefare_get_tag_type(tags[i]) == DESFIRE) { + tag = tags[i]; + res = mifare_desfire_connect (tag); + cut_assert_equal_int (0, res, cut_message ("mifare_desfire_connect() failed")); + return; + } } - - cut_omit ("No MIFARE DESFire tag on NFC device"); + nfc_close (device); + device = NULL; + freefare_free_tags (tags); + tags = NULL; + } + cut_omit ("No MIFARE DESFire tag on NFC device"); } void cut_teardown (void) { - if (tag) - mifare_desfire_disconnect (tag); + if (tag) + mifare_desfire_disconnect (tag); - if (tags) { - freefare_free_tags (tags); - tags = NULL; - } + if (tags) { + freefare_free_tags (tags); + tags = NULL; + } - if (device) - nfc_disconnect (device); + if (device) + nfc_close (device); + + nfc_exit(NULL); } diff --git a/test/mifare_ultralight_fixture.c b/test/mifare_ultralight_fixture.c index 5dabd37..0e01255 100644 --- a/test/mifare_ultralight_fixture.c +++ b/test/mifare_ultralight_fixture.c @@ -22,7 +22,7 @@ #include "mifare_ultralight_fixture.h" -static nfc_device_t *device = NULL; +static nfc_device *device = NULL; static MifareTag *tags = NULL; MifareTag tag = NULL; @@ -30,36 +30,38 @@ void cut_setup (void) { int res; - nfc_device_desc_t devices[8]; + nfc_connstring devices[8]; size_t device_count; + + nfc_init(NULL); - nfc_list_devices (devices, 8, &device_count); - if (!device_count) - cut_omit ("No device found"); + device_count = nfc_list_devices (NULL, devices, 8); + if (device_count <= 0) + cut_omit ("No device found"); for (size_t i = 0; i < device_count; i++) { - device = nfc_connect (&(devices[i])); - if (!device) - cut_omit ("nfc_connect() failed"); + device = nfc_open (NULL, devices[i]); + if (!device) + cut_omit ("nfc_open() failed."); - tags = freefare_get_tags (device); - cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); + tags = freefare_get_tags (device); + cut_assert_not_null (tags, cut_message ("freefare_get_tags() failed")); - tag = NULL; - for (int i=0; tags[i]; i++) { - if ((freefare_get_tag_type(tags[i]) == ULTRALIGHT) || - (freefare_get_tag_type(tags[i]) == ULTRALIGHT_C)) { - tag = tags[i]; - res = mifare_ultralight_connect (tag); - cut_assert_equal_int (0, res, cut_message ("mifare_ultralight_connect() failed")); - return; - } - } - nfc_disconnect (device); - device = NULL; - freefare_free_tags (tags); - tags = NULL; + tag = NULL; + for (int i=0; tags[i]; i++) { + if ((freefare_get_tag_type(tags[i]) == ULTRALIGHT) || + (freefare_get_tag_type(tags[i]) == ULTRALIGHT_C)) { + tag = tags[i]; + res = mifare_ultralight_connect (tag); + cut_assert_equal_int (0, res, cut_message ("mifare_ultralight_connect() failed")); + return; + } + } + nfc_close (device); + device = NULL; + freefare_free_tags (tags); + tags = NULL; } cut_omit ("No MIFARE UltraLight tag on NFC device"); @@ -69,14 +71,16 @@ void cut_teardown (void) { if (tag) - mifare_ultralight_disconnect (tag); + mifare_ultralight_disconnect (tag); if (tags) { - freefare_free_tags (tags); - tags = NULL; + freefare_free_tags (tags); + tags = NULL; } if (device) - nfc_disconnect (device); + nfc_close (device); + + nfc_exit(NULL); }