diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index a6d3e5a..dda8915 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -35,6 +35,7 @@ #include #include #include +#include #include @@ -49,6 +50,16 @@ static nfc_device_t *pnd; static bool quiet_output = false; static bool init_mfc_auth = false; +void +intr_hdlr (void) +{ + printf ("\nQuitting...\n"); + if (pnd != NULL) { + nfc_disconnect(pnd); + } + exit (EXIT_FAILURE); +} + bool target_io( const nfc_target_t nt, const byte_t * pbtInput, const size_t szInput, byte_t * pbtOutput, size_t *pszOutput ) { @@ -139,6 +150,12 @@ main (int argc, char *argv[]) { const char *acLibnfcVersion; +#ifdef WIN32 + signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr); +#else + signal (SIGINT, (void (*)()) intr_hdlr); +#endif + // Try to open the NFC reader pnd = nfc_connect (NULL); diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 3233558..b1ccca6 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -37,6 +37,7 @@ #include #include #include +#include #include @@ -54,6 +55,16 @@ byte_t abtAtqa[2] = { 0x04, 0x00 }; byte_t abtUidBcc[5] = { 0xDE, 0xAD, 0xBE, 0xAF, 0x62 }; byte_t abtSak[9] = { 0x08, 0xb6, 0xdd }; +void +intr_hdlr (void) +{ + printf ("\nQuitting...\n"); + if (pnd != NULL) { + nfc_disconnect(pnd); + } + exit (EXIT_FAILURE); +} + void print_usage (char *argv[]) { @@ -99,6 +110,12 @@ main (int argc, char *argv[]) } } +#ifdef WIN32 + signal (SIGINT, (void (__cdecl *) (int)) intr_hdlr); +#else + signal (SIGINT, (void (*)()) intr_hdlr); +#endif + // Try to open the NFC device pnd = nfc_connect (NULL); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 94765e2..191417d 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -45,6 +45,11 @@ Thanks to d18c7db and Okko for example code #define BUFFER_LENGTH 256 #define USB_TIMEOUT 30000 +// TODO Move this HACK1 into an upper level in order to benefit to other devices that use PN53x +static const byte_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 }; + +void pn53x_usb_ack (nfc_device_t * pnd); + // Find transfer endpoints for bulk transfers void get_end_points (struct usb_device *dev, usb_spec_t * pus) @@ -195,13 +200,8 @@ pn53x_usb_connect (const nfc_device_desc_t * pndd, const char *target_name, int pnd->nds = (nfc_device_spec_t) pus; pnd->bActive = true; - // TODO Move this HACK1 into an upper level in order to benefit to other devices that use PN53x // HACK1: Send first an ACK as Abort command, to reset chip before talking to it: - uint8_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 }; -#ifdef DEBUG - PRINT_HEX ("TX", ack_frame, 6); -#endif - usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) ack_frame, 6, USB_TIMEOUT); + pn53x_usb_ack (pnd); // HACK2: Then send a GetFirmware command to resync USB toggle bit between host & device // in case host used set_configuration and expects the device to have reset its toggle bit, which PN53x doesn't do @@ -262,6 +262,8 @@ pn53x_usb_disconnect (nfc_device_t * pnd) usb_spec_t *pus = (usb_spec_t *) pnd->nds; int ret; + pn53x_usb_ack (pnd); + if ((ret = usb_release_interface (pus->pudh, 0)) < 0) { ERR ("usb_release_interface failed (%i)", ret); } @@ -369,3 +371,13 @@ pn53x_usb_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szT return true; } + +void +pn53x_usb_ack (nfc_device_t * pnd) +{ + usb_spec_t *pus = (usb_spec_t *) pnd->nds; +#ifdef DEBUG + PRINT_HEX ("TX", ack_frame, sizeof (ack_frame)); +#endif + usb_bulk_write (pus->pudh, pus->uiEndPointOut, (char *) ack_frame, sizeof (ack_frame), USB_TIMEOUT); +}