Skip to content

Commit

Permalink
make style
Browse files Browse the repository at this point in the history
  • Loading branch information
doegox committed May 21, 2020
1 parent f52d04e commit 4525cd1
Show file tree
Hide file tree
Showing 9 changed files with 490 additions and 505 deletions.
146 changes: 73 additions & 73 deletions libnfc/chips/pn53x.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
//
Expand All @@ -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;;
Expand Down
4 changes: 2 additions & 2 deletions libnfc/drivers/acr122_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand Down
46 changes: 19 additions & 27 deletions libnfc/drivers/pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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];
Expand All @@ -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;
Expand All @@ -809,23 +806,23 @@ 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;
apdu_data[3] = pbtTx[1];//block index
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;
apdu_data[3] = pbtTx[1];//block index
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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion libnfc/drivers/pn53x_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 4525cd1

Please sign in to comment.