From 4525cd1c32871e287500a384de283fb89d256738 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Thu, 21 May 2020 15:06:17 +0200 Subject: [PATCH] make style --- libnfc/chips/pn53x.c | 146 ++++---- libnfc/drivers/acr122_usb.c | 4 +- libnfc/drivers/pcsc.c | 46 +-- libnfc/drivers/pn53x_usb.c | 2 +- libnfc/drivers/pn71xx.c | 691 ++++++++++++++++++------------------ libnfc/nfc-internal.c | 6 +- libnfc/nfc.c | 5 +- utils/nfc-mfclassic.c | 77 ++-- utils/nfc-mfultralight.c | 18 +- 9 files changed, 490 insertions(+), 505 deletions(-) diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 90df2aba..abf6682c 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -580,8 +580,8 @@ pn53x_decode_target_data(const uint8_t *pbtRawData, size_t szRawData, pn53x_type case NMT_ISO14443BICLASS: // Store the UID - for(uint8_t i= 0 ; i < 8 ; ++i) - pnti->nhi.abtUID[7 - i]= pbtRawData[i]; + for (uint8_t i = 0 ; i < 8 ; ++i) + pnti->nhi.abtUID[7 - i] = pbtRawData[i]; break; case NMT_ISO14443B2CT: @@ -1060,51 +1060,51 @@ pn53x_initiator_init(struct nfc_device *pnd) // iclass requires special modulation settings void pn53x_initiator_init_iclass_modulation(struct nfc_device *pnd) { - // send a bunch of low level commands reverse engineered from a working iClass reader - // original device was using a PN512 - // - // // TxModeReg - Defines the data rate and framing during transmission. - //// set bit 4 for target mode? - RxWaitRF Set to logic 1, the counter for RxWait starts only if an external RF field is detected in Target mode for NFCIP-1 or in Card Communication mode - //pn512_write_register(0x12, "\x03", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_TxMode, 0x03); - // - // // RxModeReg - Defines the data rate and framing during reception. - //pn512_write_register(0x13, "\x03", 1, false); - // addy changed to set bit 3 - RxNoErr (put data in fifo before flagging read end) - //pn512_write_register(0x13, "\x0B", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_RxMode, 0x0B); - - // ManualRCVReg - Allows manual fine tuning of the internal receiver. - //pn512_write_register(0x1d, "\x10", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_ManualRCV, 0x10); - - // RFCfgReg - Configures the receiver gain and RF level detector sensitivity. - //pn512_write_register(0x26, "\x70", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_RFCfg, 0x70); - - // GsNOffReg - Selects the conductance for the N-driver of the antenna driver pins TX1 and TX2 when the driver is switched off. - //pn512_write_register(0x23, "\x88", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_GsNOFF, 0x88); - - // GsNOnReg - Selects the conductance for the N-driver of the antenna driver pins TX1 and TX2 when the driver is switched on. - //pn512_write_register(0x27, "\xf8", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_GsNOn, 0xf8); - - // CWGsPReg - Defines the conductance of the P-driver during times of no modulation. - //pn512_write_register(0x28, "\x3f", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_CWGsP, 0x3f); - - // ModGsPReg - Defines the driver P-output conductance during modulation. - //pn512_write_register(0x29, "\x10", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_ModGsP, 0x10); - - // TReloadReg (MSB) - Describes the MSB of the 16-bit long timer reload value. - //pn512_write_register(0x2c, "\x69", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_TReloadVal_hi, 0x69); - - // TReloadReg (LSB) - Describes the LSB of the 16-bit long timer reload value. - //pn512_write_register(0x2d, "\xf0", 1, false); - pn53x_WriteRegister(pnd, PN53X_REG_CIU_TReloadVal_lo, 0xf0); + // send a bunch of low level commands reverse engineered from a working iClass reader + // original device was using a PN512 + // + // // TxModeReg - Defines the data rate and framing during transmission. + //// set bit 4 for target mode? - RxWaitRF Set to logic 1, the counter for RxWait starts only if an external RF field is detected in Target mode for NFCIP-1 or in Card Communication mode + //pn512_write_register(0x12, "\x03", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_TxMode, 0x03); + // + // // RxModeReg - Defines the data rate and framing during reception. + //pn512_write_register(0x13, "\x03", 1, false); + // addy changed to set bit 3 - RxNoErr (put data in fifo before flagging read end) + //pn512_write_register(0x13, "\x0B", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_RxMode, 0x0B); + + // ManualRCVReg - Allows manual fine tuning of the internal receiver. + //pn512_write_register(0x1d, "\x10", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_ManualRCV, 0x10); + + // RFCfgReg - Configures the receiver gain and RF level detector sensitivity. + //pn512_write_register(0x26, "\x70", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_RFCfg, 0x70); + + // GsNOffReg - Selects the conductance for the N-driver of the antenna driver pins TX1 and TX2 when the driver is switched off. + //pn512_write_register(0x23, "\x88", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_GsNOFF, 0x88); + + // GsNOnReg - Selects the conductance for the N-driver of the antenna driver pins TX1 and TX2 when the driver is switched on. + //pn512_write_register(0x27, "\xf8", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_GsNOn, 0xf8); + + // CWGsPReg - Defines the conductance of the P-driver during times of no modulation. + //pn512_write_register(0x28, "\x3f", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_CWGsP, 0x3f); + + // ModGsPReg - Defines the driver P-output conductance during modulation. + //pn512_write_register(0x29, "\x10", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_ModGsP, 0x10); + + // TReloadReg (MSB) - Describes the MSB of the 16-bit long timer reload value. + //pn512_write_register(0x2c, "\x69", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_TReloadVal_hi, 0x69); + + // TReloadReg (LSB) - Describes the LSB of the 16-bit long timer reload value. + //pn512_write_register(0x2d, "\xf0", 1, false); + pn53x_WriteRegister(pnd, PN53X_REG_CIU_TReloadVal_lo, 0xf0); } int @@ -1202,10 +1202,10 @@ pn53x_initiator_select_passive_target_ext(struct nfc_device *pnd, } szTargetsData = 6; // u16 UID_LSB, u8 prod code, u8 fab code, u16 UID_MSB } else if (nm.nmt == NMT_ISO14443BICLASS) { - pn53x_initiator_init_iclass_modulation(pnd); - // + pn53x_initiator_init_iclass_modulation(pnd); + // // Some work to do before getting the UID... - // send ICLASS_ACTIVATE_ALL command - will get timeout as we don't expect response + // send ICLASS_ACTIVATE_ALL command - will get timeout as we don't expect response uint8_t abtReqt[] = { 0x0a }; // iClass ACTIVATE_ALL uint8_t abtAnticol[11]; if ((res = pn53x_initiator_transceive_bytes(pnd, abtReqt, sizeof(abtReqt), NULL, 0, timeout)) < 0) { @@ -1214,35 +1214,35 @@ pn53x_initiator_select_passive_target_ext(struct nfc_device *pnd, // continue; //} else // return res; - } - // do select - returned anticol contains 'handle' for tag if present - abtReqt[0]= 0x0c; // iClass SELECT - abtAnticol[0]= 0x81; // iClass ANTICOL + } + // do select - returned anticol contains 'handle' for tag if present + abtReqt[0] = 0x0c; // iClass SELECT + abtAnticol[0] = 0x81; // iClass ANTICOL if ((res = pn53x_initiator_transceive_bytes(pnd, abtReqt, sizeof(abtReqt), &abtAnticol[1], sizeof(abtAnticol) - 1, timeout)) < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "timeout on iClass anticol"); return res; - } - // write back anticol handle to get UID + } + // write back anticol handle to get UID if ((res = pn53x_initiator_transceive_bytes(pnd, abtAnticol, 9, abtTargetsData, 10, timeout)) < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "timeout on iClass get UID"); return res; - } + } log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "iClass raw UID: %02x %02x %02x %02x %02x %02x %02x %02x", abtTargetsData[0], abtTargetsData[1], abtTargetsData[2], abtTargetsData[3], abtTargetsData[4], abtTargetsData[5], abtTargetsData[6], abtTargetsData[7]); - szTargetsData = 8; - nttmp.nm = nm; - if ((res = pn53x_decode_target_data(abtTargetsData, szTargetsData, CHIP_DATA(pnd)->type, nm.nmt, &(nttmp.nti))) < 0) { - return res; - } - } else { - - if ((res = pn53x_initiator_transceive_bytes(pnd, pbtInitData, szInitData, abtTargetsData, sizeof(abtTargetsData), timeout)) < 0) { - if ((res == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) { // Chip timeout - continue; - } else + szTargetsData = 8; + nttmp.nm = nm; + if ((res = pn53x_decode_target_data(abtTargetsData, szTargetsData, CHIP_DATA(pnd)->type, nm.nmt, &(nttmp.nti))) < 0) { return res; + } + } else { + + if ((res = pn53x_initiator_transceive_bytes(pnd, pbtInitData, szInitData, abtTargetsData, sizeof(abtTargetsData), timeout)) < 0) { + if ((res == NFC_ERFTRANS) && (CHIP_DATA(pnd)->last_status_byte == 0x01)) { // Chip timeout + continue; + } else + return res; + } + szTargetsData = (size_t)res; } - szTargetsData = (size_t)res; - } nttmp.nm = nm; if ((res = pn53x_decode_target_data(abtTargetsData, szTargetsData, CHIP_DATA(pnd)->type, nm.nmt, &(nttmp.nti))) < 0) { @@ -2249,7 +2249,7 @@ static int pn53x_ISO14443B_SR_is_present(struct nfc_device *pnd) static int pn53x_ISO14443B_ICLASS_is_present(struct nfc_device *pnd) { - int timeout= 300; + int timeout = 300; log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s", "target_is_present(): Ping B iClass"); pn53x_initiator_init_iclass_modulation(pnd); // @@ -2261,8 +2261,8 @@ static int pn53x_ISO14443B_ICLASS_is_present(struct nfc_device *pnd) log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "got expected timeout on iClass activate all"); } // do select - returned anticol contains 'handle' for tag if present - abtReqt[0]= 0x0c; // iClass SELECT - abtAnticol[0]= 0x81; // iClass ANTICOL + abtReqt[0] = 0x0c; // iClass SELECT + abtAnticol[0] = 0x81; // iClass ANTICOL if (pn53x_initiator_transceive_bytes(pnd, abtReqt, sizeof(abtReqt), &abtAnticol[1], sizeof(abtAnticol) - 1, timeout) < 0) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "timeout on iClass anticol"); return NFC_ETGRELEASED;; diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 8620fe25..b42af0e3 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -331,7 +331,7 @@ acr122_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const // acr122_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s Name %s", bus->dirname, dev->filename, acr122_usb_supported_devices[n].name); usb_close(udev); - if(snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename) >= (int)sizeof(nfc_connstring)) { + if (snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", ACR122_USB_DRIVER_NAME, bus->dirname, dev->filename) >= (int)sizeof(nfc_connstring)) { // truncation occurred, skipping that one continue; } @@ -430,7 +430,7 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) goto free_mem; } - // Check if there are more than 0 alternative interfaces and claim the first one + // Check if there are more than 0 alternative interfaces and claim the first one if (dev->config->interface->altsetting->bAlternateSetting > 0) { res = usb_set_altinterface(data.pudh, 0); if (res < 0) { diff --git a/libnfc/drivers/pcsc.c b/libnfc/drivers/pcsc.c index 09a4e62c..231f869c 100644 --- a/libnfc/drivers/pcsc.c +++ b/libnfc/drivers/pcsc.c @@ -138,8 +138,7 @@ int pcsc_transmit(struct nfc_device *pnd, const uint8_t *tx, const size_t tx_len DWORD dw_rx_len = *rx_len; //in libfreefare, tx_len = 1, and it leads to 0x80100008 error, with PC/SC reader, the input tx_len at least two bytes for the SW value //so if found the reader is Feitian reader, we set to 2 - if (dw_rx_len == 1 && is_pcsc_reader_vendor_feitian(pnd)) - { + if (dw_rx_len == 1 && is_pcsc_reader_vendor_feitian(pnd)) { dw_rx_len = 2; } @@ -201,15 +200,14 @@ uint8_t pcsc_get_icc_type(const struct nfc_device *pnd) return it; } -bool is_pcsc_reader_vendor(const struct nfc_device *pnd, const char * target_vendor_name) +bool is_pcsc_reader_vendor(const struct nfc_device *pnd, const char *target_vendor_name) { bool isTarget = false; - if (pnd == NULL || strlen(pnd->name) == 0) - { + if (pnd == NULL || strlen(pnd->name) == 0) { return isTarget; } - return isTarget = (strstr(pnd->name, target_vendor_name)) ? true:false; + return isTarget = (strstr(pnd->name, target_vendor_name)) ? true : false; } bool is_pcsc_reader_vendor_feitian(const struct nfc_device *pnd) @@ -340,15 +338,14 @@ int pcsc_props_to_target(struct nfc_device *pnd, uint8_t it, const uint8_t *patr memcpy(pnt->nti.nai.abtUid, puid, szuid); pnt->nti.nai.szUidLen = szuid; } - if (is_pcsc_reader_vendor_feitian(pnd)) - { + if (is_pcsc_reader_vendor_feitian(pnd)) { uint8_t atqa[2]; - pcsc_get_atqa(pnd,atqa,sizeof(atqa)); + pcsc_get_atqa(pnd, atqa, sizeof(atqa)); //memcpy(pnt->nti.nai.abtAtqa,atqa,2); pnt->nti.nai.abtAtqa[0] = atqa[1]; pnt->nti.nai.abtAtqa[1] = atqa[0]; uint8_t sak[1]; - pcsc_get_sak(pnd,sak,sizeof(sak)); + pcsc_get_sak(pnd, sak, sizeof(sak)); pnt->nti.nai.btSak = sak[0]; uint8_t ats[256]; int ats_len = pcsc_get_ats(pnd, ats, sizeof(ats)); @@ -776,7 +773,7 @@ int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx // FIXME: timeout is not handled (void) timeout; - if (is_pcsc_reader_vendor_feitian(pnd)){ + if (is_pcsc_reader_vendor_feitian(pnd)) { LOG_HEX(NFC_LOG_GROUP_COM, "not feitian reader pcsc apdu send", pbtTx, szTx); uint8_t apdu_data[256]; @@ -797,7 +794,7 @@ int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx apdu_data[4] = szTx - 2; memcpy(apdu_data + 5, pbtTx + 2, szTx - 2); send_size = 5 + szTx - 2; - } else if (pbtTx[0] == 0x60 || pbtTx[0] == 0x61 || pbtTx[0] == 0x1A){//Auth command + } else if (pbtTx[0] == 0x60 || pbtTx[0] == 0x61 || pbtTx[0] == 0x1A) { //Auth command apdu_data[0] = 0xFF; apdu_data[1] = 0x86; apdu_data[2] = 0x00; @@ -809,7 +806,7 @@ int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx apdu_data[8] = pbtTx[0];//type a or type b apdu_data[9] = 0x01; send_size = 10; - } else if (pbtTx[0] == 0xC0){//DECREMENT cmd + } else if (pbtTx[0] == 0xC0) { //DECREMENT cmd apdu_data[0] = 0xFF; apdu_data[1] = 0xD7; apdu_data[2] = 0x00; @@ -817,7 +814,7 @@ int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx apdu_data[4] = 0x05; memcpy(apdu_data + 5, pbtTx + 2, szTx - 2); send_size = 5 + szTx - 2; - } else if (pbtTx[0] == 0xC1){//INCREMENT cmd + } else if (pbtTx[0] == 0xC1) { //INCREMENT cmd apdu_data[0] = 0xFF; apdu_data[1] = 0xD7; apdu_data[2] = 0x00; @@ -825,7 +822,7 @@ int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx apdu_data[4] = 0x05; memcpy(apdu_data + 5, pbtTx + 2, szTx - 2); send_size = 5 + szTx - 2; - } else if (pbtTx[0] == 0xC2){//STORE cmd + } else if (pbtTx[0] == 0xC2) { //STORE cmd apdu_data[0] = 0xFF; apdu_data[1] = 0xD8; apdu_data[2] = 0x00; @@ -840,7 +837,7 @@ int pcsc_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx LOG_HEX(NFC_LOG_GROUP_COM, "feitian reader pcsc apdu send:", apdu_data, send_size); pnd->last_error = pcsc_transmit(pnd, apdu_data, send_size, resp, &resp_len); LOG_HEX(NFC_LOG_GROUP_COM, "feitian reader pcsc apdu received:", resp, resp_len); - + memcpy(pbtRx, resp, resp_len); } else { pnd->last_error = pcsc_transmit(pnd, pbtTx, szTx, pbtRx, &resp_len); @@ -938,7 +935,7 @@ pcsc_get_information_about(nfc_device *pnd, char **pbuf) struct pcsc_data *data = pnd->driver_data; LPBYTE name = NULL, version = NULL, type = NULL, serial = NULL; DWORD name_len = SCARD_AUTOALLOCATE, version_len = SCARD_AUTOALLOCATE, - type_len = SCARD_AUTOALLOCATE, serial_len = SCARD_AUTOALLOCATE; + type_len = SCARD_AUTOALLOCATE, serial_len = SCARD_AUTOALLOCATE; int res = NFC_SUCCESS; SCARDCONTEXT *pscc; @@ -974,27 +971,22 @@ pcsc_get_information_about(nfc_device *pnd, char **pbuf) error: #ifdef __APPLE__ - if (pscc != NULL) - { + if (pscc != NULL) { SCardReleaseContext(*pscc); } - if (name != NULL) - { + if (name != NULL) { free(name); name = NULL; } - if (type != NULL) - { + if (type != NULL) { free(type); type = NULL; } - if (version != NULL) - { + if (version != NULL) { free(version); version = NULL; } - if (serial != NULL) - { + if (serial != NULL) { free(serial); serial = NULL; } diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 456fa6be..d1796663 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -337,7 +337,7 @@ pn53x_usb_scan(const nfc_context *context, nfc_connstring connstrings[], const s // pn53x_usb_get_usb_device_name (dev, udev, pnddDevices[device_found].acDevice, sizeof (pnddDevices[device_found].acDevice)); log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "device found: Bus %s Device %s", bus->dirname, dev->filename); usb_close(udev); - if(snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename) >= (int)sizeof(nfc_connstring)) { + if (snprintf(connstrings[device_found], sizeof(nfc_connstring), "%s:%s:%s", PN53X_USB_DRIVER_NAME, bus->dirname, dev->filename) >= (int)sizeof(nfc_connstring)) { // truncation occurred, skipping that one continue; } diff --git a/libnfc/drivers/pn71xx.c b/libnfc/drivers/pn71xx.c index 82459081..39e722db 100644 --- a/libnfc/drivers/pn71xx.c +++ b/libnfc/drivers/pn71xx.c @@ -58,16 +58,16 @@ static void onTagDeparture(void); static size_t pn71xx_scan(const nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len) { - size_t device_found = 0; + size_t device_found = 0; - if ((context == NULL) || (connstrings_len == 0)) return 0; + if ((context == NULL) || (connstrings_len == 0)) return 0; - if (nfcManager_doInitialize() == 0) { - nfc_connstring connstring = "pn71xx"; - memcpy(connstrings[device_found++], connstring, sizeof(nfc_connstring)); - } + if (nfcManager_doInitialize() == 0) { + nfc_connstring connstring = "pn71xx"; + memcpy(connstrings[device_found++], connstring, sizeof(nfc_connstring)); + } - return device_found; + return device_found; } /** @@ -77,12 +77,12 @@ pn71xx_scan(const nfc_context *context, nfc_connstring connstrings[], const size */ static void pn71xx_close(nfc_device *pnd) -{ - nfcManager_disableDiscovery(); - nfcManager_deregisterTagCallback(); - nfcManager_doDeinitialize(); - nfc_device_free(pnd); - pnd = NULL; +{ + nfcManager_disableDiscovery(); + nfcManager_deregisterTagCallback(); + nfcManager_doDeinitialize(); + nfc_device_free(pnd); + pnd = NULL; } /** @@ -95,157 +95,150 @@ pn71xx_close(nfc_device *pnd) static nfc_device * pn71xx_open(const nfc_context *context, const nfc_connstring connstring) { - nfc_device *pnd; - - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "open: %s", connstring); + nfc_device *pnd; + + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "open: %s", connstring); - pnd = nfc_device_new(context, connstring); - if (!pnd) { - perror("malloc"); - return NULL; - } + pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } - pnd->driver = &pn71xx_driver; - strcpy(pnd->name, "pn71xx-device"); - strcpy(pnd->connstring, connstring); + pnd->driver = &pn71xx_driver; + strcpy(pnd->name, "pn71xx-device"); + strcpy(pnd->connstring, connstring); - TagCB.onTagArrival = onTagArrival; - TagCB.onTagDeparture = onTagDeparture; - nfcManager_registerTagCallback(&TagCB); + TagCB.onTagArrival = onTagArrival; + TagCB.onTagDeparture = onTagDeparture; + nfcManager_registerTagCallback(&TagCB); - nfcManager_enableDiscovery(DEFAULT_NFA_TECH_MASK, 1, 0, 0); + nfcManager_enableDiscovery(DEFAULT_NFA_TECH_MASK, 1, 0, 0); - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "wait 1 seconds for polling"); - sleep(1); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "wait 1 seconds for polling"); + sleep(1); - return pnd; + return pnd; } /** ------------------------------------------------------------------------ */ /** ------------------------------------------------------------------------ */ static bool IsTechnology(nfc_tag_info_t *TagInfo, nfc_modulation_type nmt) { - switch (nmt) { - case NMT_ISO14443A: - if (TagInfo->technology == TARGET_TYPE_ISO14443_4 - || TagInfo->technology == TARGET_TYPE_ISO14443_3A - || TagInfo->technology == TARGET_TYPE_MIFARE_CLASSIC - || TagInfo->technology == TARGET_TYPE_MIFARE_UL) - return true; - break; - - case NMT_ISO14443B: - case NMT_ISO14443BI: - case NMT_ISO14443B2SR: - case NMT_ISO14443B2CT: - if (TagInfo->technology == TARGET_TYPE_ISO14443_3B) - return true; - break; - - case NMT_FELICA: - if (TagInfo->technology == TARGET_TYPE_FELICA) - return true; - break; - - case NMT_JEWEL: - if (TagInfo->technology == TARGET_TYPE_ISO14443_3A - && TagInfo->protocol == NFA_PROTOCOL_T1T) - return true; - break; - - default: - return false; - } - return false; + switch (nmt) { + case NMT_ISO14443A: + if (TagInfo->technology == TARGET_TYPE_ISO14443_4 + || TagInfo->technology == TARGET_TYPE_ISO14443_3A + || TagInfo->technology == TARGET_TYPE_MIFARE_CLASSIC + || TagInfo->technology == TARGET_TYPE_MIFARE_UL) + return true; + break; + + case NMT_ISO14443B: + case NMT_ISO14443BI: + case NMT_ISO14443B2SR: + case NMT_ISO14443B2CT: + if (TagInfo->technology == TARGET_TYPE_ISO14443_3B) + return true; + break; + + case NMT_FELICA: + if (TagInfo->technology == TARGET_TYPE_FELICA) + return true; + break; + + case NMT_JEWEL: + if (TagInfo->technology == TARGET_TYPE_ISO14443_3A + && TagInfo->protocol == NFA_PROTOCOL_T1T) + return true; + break; + + default: + return false; + } + return false; } -static void BufferPrintBytes(char* buffer, unsigned int buflen, const uint8_t *data, unsigned int datalen) +static void BufferPrintBytes(char *buffer, unsigned int buflen, const uint8_t *data, unsigned int datalen) { - int cx = 0; - for(unsigned int i = 0x00; i < datalen; i++) { - cx += snprintf(buffer + cx, buflen - cx, "%02X ", data[i]); - } + int cx = 0; + for (unsigned int i = 0x00; i < datalen; i++) { + cx += snprintf(buffer + cx, buflen - cx, "%02X ", data[i]); + } } -static void PrintTagInfo (nfc_tag_info_t *TagInfo) +static void PrintTagInfo(nfc_tag_info_t *TagInfo) { - switch (TagInfo->technology) - { - case TARGET_TYPE_UNKNOWN: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type Unknown'"); - } break; - case TARGET_TYPE_ISO14443_3A: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type 3A'"); - } break; - case TARGET_TYPE_ISO14443_3B: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type 3B'"); - } break; - case TARGET_TYPE_ISO14443_4: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type 4A'"); - } break; - case TARGET_TYPE_FELICA: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type F'"); - } break; - case TARGET_TYPE_ISO15693: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type V'"); - } break; - case TARGET_TYPE_NDEF: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type NDEF'"); - } break; - case TARGET_TYPE_NDEF_FORMATABLE: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type Formatable'"); - } break; - case TARGET_TYPE_MIFARE_CLASSIC: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A - Mifare Classic'"); - } break; - case TARGET_TYPE_MIFARE_UL: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A - Mifare Ul'"); - } break; - case TARGET_TYPE_KOVIO_BARCODE: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A - Kovio Barcode'"); - } break; - case TARGET_TYPE_ISO14443_3A_3B: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A/B'"); - } break; - default: - { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type %d (Unknown or not supported)'\n", TagInfo->technology); - } break; - } - /*32 is max UID len (Kovio tags)*/ - if((0x00 != TagInfo->uid_length) && (32 >= TagInfo->uid_length)) - { - char buffer [100]; - int cx = 0; - - if(4 == TagInfo->uid_length || 7 == TagInfo->uid_length || 10 == TagInfo->uid_length) - { - cx += snprintf(buffer + cx, sizeof(buffer) - cx, "NFCID1 : \t'"); - } - else if(8 == TagInfo->uid_length) - { - cx += snprintf(buffer + cx, sizeof(buffer) - cx, "NFCID2 : \t'"); - } - else - { - cx += snprintf(buffer + cx, sizeof(buffer) - cx, "UID : \t'"); - } - - BufferPrintBytes(buffer + cx, sizeof(buffer) - cx, (unsigned char*) TagInfo->uid, TagInfo->uid_length); - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s'", buffer); - } + switch (TagInfo->technology) { + case TARGET_TYPE_UNKNOWN: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type Unknown'"); + } + break; + case TARGET_TYPE_ISO14443_3A: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type 3A'"); + } + break; + case TARGET_TYPE_ISO14443_3B: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type 3B'"); + } + break; + case TARGET_TYPE_ISO14443_4: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type 4A'"); + } + break; + case TARGET_TYPE_FELICA: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type F'"); + } + break; + case TARGET_TYPE_ISO15693: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type V'"); + } + break; + case TARGET_TYPE_NDEF: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type NDEF'"); + } + break; + case TARGET_TYPE_NDEF_FORMATABLE: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type Formatable'"); + } + break; + case TARGET_TYPE_MIFARE_CLASSIC: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A - Mifare Classic'"); + } + break; + case TARGET_TYPE_MIFARE_UL: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A - Mifare Ul'"); + } + break; + case TARGET_TYPE_KOVIO_BARCODE: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A - Kovio Barcode'"); + } + break; + case TARGET_TYPE_ISO14443_3A_3B: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type A/B'"); + } + break; + default: { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "'Type %d (Unknown or not supported)'\n", TagInfo->technology); + } + break; + } + /*32 is max UID len (Kovio tags)*/ + if ((0x00 != TagInfo->uid_length) && (32 >= TagInfo->uid_length)) { + char buffer [100]; + int cx = 0; + + if (4 == TagInfo->uid_length || 7 == TagInfo->uid_length || 10 == TagInfo->uid_length) { + cx += snprintf(buffer + cx, sizeof(buffer) - cx, "NFCID1 : \t'"); + } else if (8 == TagInfo->uid_length) { + cx += snprintf(buffer + cx, sizeof(buffer) - cx, "NFCID2 : \t'"); + } else { + cx += snprintf(buffer + cx, sizeof(buffer) - cx, "UID : \t'"); + } + + BufferPrintBytes(buffer + cx, sizeof(buffer) - cx, (unsigned char *) TagInfo->uid, TagInfo->uid_length); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%s'", buffer); + } } /** ------------------------------------------------------------------------ */ @@ -253,194 +246,194 @@ static void PrintTagInfo (nfc_tag_info_t *TagInfo) static void onTagArrival(nfc_tag_info_t *pTagInfo) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "tag found"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "tag found"); - TagInfo = malloc(sizeof(nfc_tag_info_t)); - memcpy(TagInfo, pTagInfo, sizeof(nfc_tag_info_t)); + TagInfo = malloc(sizeof(nfc_tag_info_t)); + memcpy(TagInfo, pTagInfo, sizeof(nfc_tag_info_t)); - PrintTagInfo(TagInfo); + PrintTagInfo(TagInfo); } static void onTagDeparture(void) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "tag lost"); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "tag lost"); - free(TagInfo); - TagInfo = NULL; + free(TagInfo); + TagInfo = NULL; } static int pn71xx_initiator_init(struct nfc_device *pnd) { - if (pnd == NULL) return NFC_EIO; - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + return NFC_SUCCESS; } static int pn71xx_initiator_select_passive_target(struct nfc_device *pnd, - const nfc_modulation nm, - const uint8_t *pbtInitData, const size_t szInitData, - nfc_target *pnt) + const nfc_modulation nm, + const uint8_t *pbtInitData, const size_t szInitData, + nfc_target *pnt) { - if (pnd == NULL) return NFC_EIO; - - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "select_passive_target"); - - if (TagInfo) { - - nfc_target nttmp; - memset(&nttmp, 0x00, sizeof(nfc_target)); - nttmp.nm = nm; - - void* uidPtr = NULL; - unsigned int maxLen = 0; - - switch (nm.nmt) { - case NMT_ISO14443A: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 10; - uidPtr = nttmp.nti.nai.abtUid; - - if (TagInfo->technology == TARGET_TYPE_MIFARE_CLASSIC) { - nttmp.nti.nai.btSak = 0x08; - } else { - // make hardcoded desfire for freefare lib check - nttmp.nti.nai.btSak = 0x20; - nttmp.nti.nai.szAtsLen = 5; - memcpy (nttmp.nti.nai.abtAts, "\x75\x77\x81\x02", 4); - } - } - break; - - case NMT_ISO14443B: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 4; - uidPtr = nttmp.nti.nbi.abtPupi; - } - break; - - case NMT_ISO14443BI: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 4; - uidPtr = nttmp.nti.nii.abtDIV; - } - break; - - case NMT_ISO14443B2SR: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 8; - uidPtr = nttmp.nti.nsi.abtUID; - } - break; - - case NMT_ISO14443B2CT: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 4; - uidPtr = nttmp.nti.nci.abtUID; - } - break; - - case NMT_FELICA: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 8; - uidPtr = nttmp.nti.nfi.abtId; - } - break; - - case NMT_JEWEL: - if (IsTechnology(TagInfo, nm.nmt)) { - maxLen = 4; - uidPtr = nttmp.nti.nji.btId; - } - break; - - default: - return 0; - } - - if (uidPtr && TagInfo->uid_length) { - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "target found"); - int len = TagInfo->uid_length > maxLen ? maxLen : TagInfo->uid_length; - memcpy(uidPtr, TagInfo->uid, len); - if (nm.nmt == NMT_ISO14443A) - nttmp.nti.nai.szUidLen = len; - - // Is a tag info struct available - if (pnt) { - memcpy(pnt, &nttmp, sizeof(nfc_target)); - } - return 1; - } - } - - return 0; + if (pnd == NULL) return NFC_EIO; + + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "select_passive_target"); + + if (TagInfo) { + + nfc_target nttmp; + memset(&nttmp, 0x00, sizeof(nfc_target)); + nttmp.nm = nm; + + void *uidPtr = NULL; + unsigned int maxLen = 0; + + switch (nm.nmt) { + case NMT_ISO14443A: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 10; + uidPtr = nttmp.nti.nai.abtUid; + + if (TagInfo->technology == TARGET_TYPE_MIFARE_CLASSIC) { + nttmp.nti.nai.btSak = 0x08; + } else { + // make hardcoded desfire for freefare lib check + nttmp.nti.nai.btSak = 0x20; + nttmp.nti.nai.szAtsLen = 5; + memcpy(nttmp.nti.nai.abtAts, "\x75\x77\x81\x02", 4); + } + } + break; + + case NMT_ISO14443B: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 4; + uidPtr = nttmp.nti.nbi.abtPupi; + } + break; + + case NMT_ISO14443BI: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 4; + uidPtr = nttmp.nti.nii.abtDIV; + } + break; + + case NMT_ISO14443B2SR: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 8; + uidPtr = nttmp.nti.nsi.abtUID; + } + break; + + case NMT_ISO14443B2CT: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 4; + uidPtr = nttmp.nti.nci.abtUID; + } + break; + + case NMT_FELICA: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 8; + uidPtr = nttmp.nti.nfi.abtId; + } + break; + + case NMT_JEWEL: + if (IsTechnology(TagInfo, nm.nmt)) { + maxLen = 4; + uidPtr = nttmp.nti.nji.btId; + } + break; + + default: + return 0; + } + + if (uidPtr && TagInfo->uid_length) { + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "target found"); + int len = TagInfo->uid_length > maxLen ? maxLen : TagInfo->uid_length; + memcpy(uidPtr, TagInfo->uid, len); + if (nm.nmt == NMT_ISO14443A) + nttmp.nti.nai.szUidLen = len; + + // Is a tag info struct available + if (pnt) { + memcpy(pnt, &nttmp, sizeof(nfc_target)); + } + return 1; + } + } + + return 0; } static int pn71xx_initiator_deselect_target(struct nfc_device *pnd) { - if (pnd == NULL) return NFC_EIO; - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "deselect_passive_target"); - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "deselect_passive_target"); + return NFC_SUCCESS; } static int pn71xx_initiator_transceive_bytes(struct nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx, -const size_t szRx, int timeout) + const size_t szRx, int timeout) { - if (pnd == NULL) return NFC_EIO; - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "transceive_bytes timeout=%d", timeout); + if (pnd == NULL) return NFC_EIO; + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "transceive_bytes timeout=%d", timeout); + + if (!TagInfo) return NFC_EINVARG; - if (!TagInfo) return NFC_EINVARG; + char buffer[500]; + BufferPrintBytes(buffer, sizeof(buffer), pbtTx, szTx); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "===> %s", buffer); - char buffer[500]; - BufferPrintBytes(buffer, sizeof(buffer), pbtTx, szTx); - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "===> %s", buffer); + int received = nfcTag_transceive(TagInfo->handle, (uint8_t *) pbtTx, szTx, pbtRx, szRx, 500); + if (received <= 0) + return NFC_EIO; - int received = nfcTag_transceive(TagInfo->handle, (uint8_t *) pbtTx, szTx, pbtRx, szRx, 500); - if (received <= 0) - return NFC_EIO; + BufferPrintBytes(buffer, sizeof(buffer), pbtRx, received); + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "<=== %s", buffer); - BufferPrintBytes(buffer, sizeof(buffer), pbtRx, received); - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "<=== %s", buffer); - - return received; + return received; } static int pn71xx_initiator_poll_target(struct nfc_device *pnd, - const nfc_modulation *pnmModulations, const size_t szModulations, - const uint8_t uiPollNr, const uint8_t uiPeriod, - nfc_target *pnt) + const nfc_modulation *pnmModulations, const size_t szModulations, + const uint8_t uiPollNr, const uint8_t uiPeriod, + nfc_target *pnt) { - static int periodFactor = 150000; - int period = uiPeriod * periodFactor; - - if (pnd == NULL) return 0; - - for (int j = 0; j < uiPollNr; j++) { - for (unsigned int i = 0; i < szModulations; i++) { - const nfc_modulation nm = pnmModulations[i]; - - nfc_target nt; - int res = pn71xx_initiator_select_passive_target(pnd, nm, 0, 0, &nt); - if (res > 0 && pnt) { - memcpy(pnt, &nt, sizeof(nfc_target)); - return res; - } - } - usleep(period); - } - - return 0; + static int periodFactor = 150000; + int period = uiPeriod * periodFactor; + + if (pnd == NULL) return 0; + + for (int j = 0; j < uiPollNr; j++) { + for (unsigned int i = 0; i < szModulations; i++) { + const nfc_modulation nm = pnmModulations[i]; + + nfc_target nt; + int res = pn71xx_initiator_select_passive_target(pnd, nm, 0, 0, &nt); + if (res > 0 && pnt) { + memcpy(pnt, &nt, sizeof(nfc_target)); + return res; + } + } + usleep(period); + } + + return 0; } static int pn71xx_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt) { - if ((pnd == NULL) || (pnt == NULL)) return 1; - return !TagInfo; + if ((pnd == NULL) || (pnt == NULL)) return 1; + return !TagInfo; } @@ -449,17 +442,17 @@ pn71xx_initiator_target_is_present(struct nfc_device *pnd, const nfc_target *pnt static int pn71xx_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt) { - if (pnd == NULL) return NFC_EIO; - - switch (mode) { - case N_TARGET: - *supported_mt = (nfc_modulation_type *)pn71xx_supported_modulation_as_target; - break; - case N_INITIATOR: - *supported_mt = (nfc_modulation_type *)pn71xx_supported_modulation_as_initiator; - break; - default: - return NFC_EINVARG; + if (pnd == NULL) return NFC_EIO; + + switch (mode) { + case N_TARGET: + *supported_mt = (nfc_modulation_type *)pn71xx_supported_modulation_as_target; + break; + case N_INITIATOR: + *supported_mt = (nfc_modulation_type *)pn71xx_supported_modulation_as_initiator; + break; + default: + return NFC_EINVARG; } return NFC_SUCCESS; } @@ -467,32 +460,32 @@ pn71xx_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_ static int pn71xx_get_supported_baud_rate(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br) { - if (pnd == NULL) return NFC_EIO; - if (mode) {} - - switch (nmt) { - case NMT_FELICA: - *supported_br = (nfc_baud_rate *)pn71xx_felica_supported_baud_rates; - break; - case NMT_ISO14443A: - *supported_br = (nfc_baud_rate *)pn71xx_iso14443a_supported_baud_rates; - break; - case NMT_ISO14443B: - case NMT_ISO14443BI: - case NMT_ISO14443B2SR: - case NMT_ISO14443B2CT: - *supported_br = (nfc_baud_rate *)pn71xx_iso14443b_supported_baud_rates; - break; - case NMT_JEWEL: - *supported_br = (nfc_baud_rate *)pn71xx_jewel_supported_baud_rates; - break; - case NMT_DEP: - *supported_br = (nfc_baud_rate *)pn71xx_dep_supported_baud_rates; - break; - default: - return NFC_EINVARG; - } - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + if (mode) {} + + switch (nmt) { + case NMT_FELICA: + *supported_br = (nfc_baud_rate *)pn71xx_felica_supported_baud_rates; + break; + case NMT_ISO14443A: + *supported_br = (nfc_baud_rate *)pn71xx_iso14443a_supported_baud_rates; + break; + case NMT_ISO14443B: + case NMT_ISO14443BI: + case NMT_ISO14443B2SR: + case NMT_ISO14443B2CT: + *supported_br = (nfc_baud_rate *)pn71xx_iso14443b_supported_baud_rates; + break; + case NMT_JEWEL: + *supported_br = (nfc_baud_rate *)pn71xx_jewel_supported_baud_rates; + break; + case NMT_DEP: + *supported_br = (nfc_baud_rate *)pn71xx_dep_supported_baud_rates; + break; + default: + return NFC_EINVARG; + } + return NFC_SUCCESS; } /** ------------------------------------------------------------------------ */ @@ -501,29 +494,29 @@ pn71xx_get_supported_baud_rate(nfc_device *pnd, const nfc_mode mode, const nfc_m static int pn71xx_set_property_bool(struct nfc_device *pnd, const nfc_property property, const bool bEnable) { - if (pnd == NULL) return NFC_EIO; - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + return NFC_SUCCESS; } static int pn71xx_set_property_int(struct nfc_device *pnd, const nfc_property property, const int value) { - if (pnd == NULL) return NFC_EIO; - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + return NFC_SUCCESS; } static int pn71xx_get_information_about(nfc_device *pnd, char **pbuf) { - static const char* info = "PN71XX nfc driver using libnfc-nci userspace library"; - size_t buflen = strlen(info) + 1; + static const char *info = "PN71XX nfc driver using libnfc-nci userspace library"; + size_t buflen = strlen(info) + 1; + + if (pnd == NULL) return NFC_EIO; - if (pnd == NULL) return NFC_EIO; - - *pbuf = malloc(buflen); - memcpy(*pbuf, info, buflen); + *pbuf = malloc(buflen); + memcpy(*pbuf, info, buflen); - return buflen; + return buflen; } /** * @brief Abort any pending operation @@ -534,25 +527,25 @@ pn71xx_get_information_about(nfc_device *pnd, char **pbuf) static int pn71xx_abort_command(nfc_device *pnd) { - if (pnd == NULL) return NFC_EIO; - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "abort_command"); - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "abort_command"); + return NFC_SUCCESS; } static int pn71xx_idle(struct nfc_device *pnd) { - if (pnd == NULL) return NFC_EIO; - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "idle"); - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "idle"); + return NFC_SUCCESS; } static int pn71xx_PowerDown(struct nfc_device *pnd) { - if (pnd == NULL) return NFC_EIO; - log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "PowerDown"); - return NFC_SUCCESS; + if (pnd == NULL) return NFC_EIO; + log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "PowerDown"); + return NFC_SUCCESS; } /** ------------------------------------------------------------------------ */ diff --git a/libnfc/nfc-internal.c b/libnfc/nfc-internal.c index b57b97cb..7ed834d2 100644 --- a/libnfc/nfc-internal.c +++ b/libnfc/nfc-internal.c @@ -174,17 +174,17 @@ prepare_initiator_data(const nfc_modulation nm, uint8_t **ppbtInitiatorData, siz // Application Family Identifier (AFI) must equals 0x00 in order to wakeup all ISO14443-B PICCs (see ISO/IEC 14443-3) *ppbtInitiatorData = (uint8_t *) "\x00"; *pszInitiatorData = 1; - break; + break; case NMT_ISO14443BI: // APGEN *ppbtInitiatorData = (uint8_t *) "\x01\x0b\x3f\x80"; *pszInitiatorData = 4; - break; + break; case NMT_FELICA: // polling payload must be present (see ISO/IEC 18092 11.2.2.5) *ppbtInitiatorData = (uint8_t *) "\x00\xff\xff\x01\x00"; *pszInitiatorData = 5; - break; + break; case NMT_ISO14443A: case NMT_ISO14443B2CT: case NMT_ISO14443B2SR: diff --git a/libnfc/nfc.c b/libnfc/nfc.c index 8c12578e..2d56bf60 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -140,7 +140,7 @@ struct nfc_driver_list { const struct nfc_driver_list *nfc_drivers = NULL; // descritions for debugging -const char * nfc_property_name[] = { +const char *nfc_property_name[] = { "NP_TIMEOUT_COMMAND", "NP_TIMEOUT_ATR", "NP_TIMEOUT_COM", @@ -206,8 +206,7 @@ nfc_device_validate_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_m int nfc_register_driver(const struct nfc_driver *ndr) { - if (!ndr) - { + if (!ndr) { log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "nfc_register_driver returning NFC_EINVARG"); return NFC_EINVARG; } diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index faa4dc54..9c4cb2e8 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -10,7 +10,7 @@ * See AUTHORS file for a more comprehensive list of contributors. * Additional contributors of this file: * Copyright (C) 2011-2013 Adam Laurie - * Copyright (C) 2018-2019 Danielle Bruneo + * Copyright (C) 2018-2019 Danielle Bruneo * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -218,9 +218,9 @@ authenticate(uint32_t uiBlock) // Try to authenticate for the current sector if (nfc_initiator_mifare_cmd(pnd, mc, uiBlock, &mp)) { - return true; + return true; } - // If formatting or not using key file, try to guess the right key + // If formatting or not using key file, try to guess the right key } else if (bFormatCard || !bUseKeyFile) { for (size_t key_index = 0; key_index < num_keys; key_index++) { memcpy(mp.mpa.abtKey, keys + (key_index * 6), 6); @@ -494,7 +494,7 @@ write_card(int write_block_zero) bFailure = true; printf("Failure to write to data block %i\n", uiBlock); } - + } else { printf("Failure during write process.\n"); } @@ -510,19 +510,19 @@ write_card(int write_block_zero) if (write_block_zero || magic2 || magic3) { for (uiBlock = 0; uiBlock < 4; uiBlock++) { - // The first block 0x00 is read only, skip this + // The first block 0x00 is read only, skip this if (uiBlock == 0) { - //If the card is not magic, we're gonna skip over + //If the card is not magic, we're gonna skip over if (write_block_zero || magic2 || magic3) { - //NOP + //NOP } else { continue; } - } + } if (is_first_block(uiBlock)) { if (bFailure) { - // When a failure occured we need to redo the anti-collision + // When a failure occured we need to redo the anti-collision if (nfc_initiator_select_passive_target(pnd, nmMifare, NULL, 0, &nt) <= 0) { printf("!\nError: tag was removed\n"); return false; @@ -530,27 +530,27 @@ write_card(int write_block_zero) bFailure = false; } - fflush(stdout); - // Try to authenticate for the current sector - // If we are are writing to a chinese magic card, we've already unlocked - // If we're writing to a One Time Write, we need to authenticate - // If we're writing something else, we'll need to authenticate + fflush(stdout); + // Try to authenticate for the current sector + // If we are are writing to a chinese magic card, we've already unlocked + // If we're writing to a One Time Write, we need to authenticate + // If we're writing something else, we'll need to authenticate if ((write_block_zero && magic3) || !write_block_zero) { if (!authenticate(uiBlock) && !bTolerateFailures) { printf("!\nError: authentication failed for block %02x\n", uiBlock); return false; - } - } + } + } } - // Make sure a earlier write did not fail + // Make sure a earlier write did not fail if (!bFailure) { - // Try to write the data block + // Try to write the data block if (bFormatCard && uiBlock) memset(mp.mpd.abtData, 0x00, sizeof(mp.mpd.abtData)); else memcpy(mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData, sizeof(mp.mpd.abtData)); - // do not write a block 0 with incorrect BCC - card will be made invalid! + // do not write a block 0 with incorrect BCC - card will be made invalid! if (uiBlock == 0) { if ((mp.mpd.abtData[0] ^ mp.mpd.abtData[1] ^ mp.mpd.abtData[2] ^ mp.mpd.abtData[3] ^ mp.mpd.abtData[4]) != 0x00 && !magic2) { printf("!\nError: incorrect BCC in MFD file!\n"); @@ -562,15 +562,15 @@ write_card(int write_block_zero) bFailure = true; printf("Failure to write to data block %i\n", uiBlock); } - + } else { printf("Failure during write process.\n"); } - // Show if the write went well for each block + // Show if the write went well for each block print_success_or_failure(bFailure, &uiWriteBlocks); if ((! bTolerateFailures) && bFailure) - return false; + return false; } @@ -620,7 +620,8 @@ print_usage(const char *pcProgramName) } -bool is_directwrite(){ +bool is_directwrite() +{ printf("Checking if Badge is DirectWrite...\n"); // Set default keys @@ -629,28 +630,28 @@ bool is_directwrite(){ memcpy(mtDump.amb[0].mbt.abtKeyB, default_key, sizeof(default_key)); // Temporarly override bUseKeyFile - bool orig_bUseKeyFile=bUseKeyFile; - bUseKeyFile=false; + bool orig_bUseKeyFile = bUseKeyFile; + bUseKeyFile = false; // Try to authenticate for the current sector if (!authenticate(0)) { printf("!\nError: authentication failed for block 0x%02x\n", 0); - bUseKeyFile=orig_bUseKeyFile; + bUseKeyFile = orig_bUseKeyFile; return false; } // restore bUseKeyFile - bUseKeyFile=orig_bUseKeyFile; + bUseKeyFile = orig_bUseKeyFile; // Try to read block 0 uint8_t original_b0[16]; if (nfc_initiator_mifare_cmd(pnd, MC_READ, 0, &mp)) { memcpy(original_b0, mp.mpd.abtData, sizeof(mp.mpd.abtData)); printf(" Original Block 0: "); - for(int i=0;i<16;i++){ + for (int i = 0; i < 16; i++) { printf("%02x", original_b0[i]); } printf("\n"); printf(" Original UID: %02x%02x%02x%02x\n", - original_b0[0], original_b0[1], original_b0[2], original_b0[3]); + original_b0[0], original_b0[1], original_b0[2], original_b0[3]); } else { printf("!\nError: unable to read block 0x%02x\n", 0); return false; @@ -845,22 +846,22 @@ main(int argc, const char *argv[]) } } else printf("RATS support: no\n"); - printf("Guessing size: seems to be a %lu-byte card\n", (unsigned long) ((uiBlocks + 1) * sizeof(mifare_classic_block))); + printf("Guessing size: seems to be a %lu-byte card\n", (unsigned long)((uiBlocks + 1) * sizeof(mifare_classic_block))); //If size is 4k check for direct-write card if (uiBlocks == 0xff) { - if (is_directwrite()){ - printf("Card is DirectWrite\n"); - magic3=true; - unlock=0; - } else { - printf("Card is not DirectWrite\n"); - } + if (is_directwrite()) { + printf("Card is DirectWrite\n"); + magic3 = true; + unlock = 0; + } else { + printf("Card is not DirectWrite\n"); + } } //Check to see if we have a One Time Write badge (magic3) if (pbtUID[0] == 0xaa && pbtUID[1] == 0x55 && - pbtUID[2] == 0xc3 && pbtUID[3] == 0x96) { + pbtUID[2] == 0xc3 && pbtUID[3] == 0x96) { printf("Card appears to be a One Time Write Card..\n"); magic3 = true; unlock = 0; diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index d115fec0..b954c0e2 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -300,12 +300,12 @@ static bool check_magic(void) if (nfc_initiator_mifare_cmd(pnd, MC_READ, 0, &mp)) { memcpy(original_b0, mp.mpd.abtData, 12); printf(" Original Block 0 (Pages 0-2): "); - for(int i=0;i<12;i++){ + for (int i = 0; i < 12; i++) { printf("%02x", original_b0[i]); } printf("\n"); printf(" Original UID: %02x%02x%02x%02x%02x%02x%02x\n", - original_b0[0], original_b0[1], original_b0[2], original_b0[4], original_b0[5], original_b0[6], original_b0[7]); + original_b0[0], original_b0[1], original_b0[2], original_b0[4], original_b0[5], original_b0[6], original_b0[7]); } else { printf("!\nError: unable to read block 0x%02x\n", 0); directWrite = false; @@ -313,8 +313,8 @@ static bool check_magic(void) printf(" Attempt to write Block 0 (pages 0-2) ...\n"); for (uint32_t page = 0; page <= 2; page++) { printf(" Writing Page %i:", page); - memcpy(mp.mpd.abtData, original_b0 + page*4, 4); - for(int i=0;i<4;i++){ + memcpy(mp.mpd.abtData, original_b0 + page * 4, 4); + for (int i = 0; i < 4; i++) { printf(" %02x", mp.mpd.abtData[i]); } printf("\n"); @@ -324,7 +324,7 @@ static bool check_magic(void) break; } } - if(directWrite){ + if (directWrite) { printf(" Block 0 written successfully\n"); printf("Card is DirectWrite\n"); return true; @@ -407,9 +407,9 @@ write_card(bool write_otp, bool write_lock, bool write_dyn_lock, bool write_uid) } // NTAG and MF0UL21 have Dynamic Lock Bytes if (((iEV1Type == EV1_UL21 && page == 0x24) || \ - (iNTAGType == NTAG_213 && page == 0x28) || \ - (iNTAGType == NTAG_215 && page == 0x82) || \ - (iNTAGType == NTAG_216 && page == 0xe2)) && (!write_dyn_lock)) { + (iNTAGType == NTAG_213 && page == 0x28) || \ + (iNTAGType == NTAG_215 && page == 0x82) || \ + (iNTAGType == NTAG_216 && page == 0xe2)) && (!write_dyn_lock)) { printf("s"); uiSkippedPages++; continue; @@ -655,7 +655,7 @@ main(int argc, const char *argv[]) iEV1Type = EV1_UL11; } else if (abtRx[6] == 0x0e) { printf("EV1 type: MF0UL21 (128 user bytes)\n"); - uiBlocks = 41; + uiBlocks = 41; iDumpSize = uiBlocks * 4; iEV1Type = EV1_UL21; } else if (abtRx[6] == 0x0f) {