pn53x: factorize & generalize CHIP_DATA() usage

This commit is contained in:
Philippe Teuwen 2011-04-04 09:26:10 +00:00
parent 7314b7d598
commit 5600007dd3
6 changed files with 20 additions and 24 deletions

View file

@ -45,8 +45,6 @@
#include <sys/param.h> #include <sys/param.h>
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
// TODO: reorder functions according to header // TODO: reorder functions according to header
// TODO: Count max bytes for InJumpForDEP reply // TODO: Count max bytes for InJumpForDEP reply
@ -136,9 +134,9 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b
} }
// Call the send/receice callback functions of the current driver // Call the send/receice callback functions of the current driver
if (!((struct pn53x_data*)(pnd->chip_data))->io->send (pnd, pbtTx, szTx)) if (!CHIP_DATA(pnd)->io->send (pnd, pbtTx, szTx))
return false; return false;
int res = ((struct pn53x_data*)(pnd->chip_data))->io->receive (pnd, pbtRx, *pszRx); int res = CHIP_DATA(pnd)->io->receive (pnd, pbtRx, *pszRx);
if (res < 0) { if (res < 0) {
return false; return false;
} }
@ -170,7 +168,7 @@ pn53x_transceive (nfc_device_t * pnd, const byte_t * pbtTx, const size_t szTx, b
default: default:
pnd->iLastError = 0; pnd->iLastError = 0;
} }
if (((struct pn53x_data*)(pnd->chip_data))->type == PN533) { if (CHIP_DATA(pnd)->type == PN533) {
if ((pbtTx[0] == ReadRegister) || (pbtTx[0] == WriteRegister)) { if ((pbtTx[0] == ReadRegister) || (pbtTx[0] == WriteRegister)) {
// PN533 prepends its answer by a status byte // PN533 prepends its answer by a status byte
pnd->iLastError = pbtRx[0] & 0x3f; pnd->iLastError = pbtRx[0] & 0x3f;
@ -187,7 +185,7 @@ pn53x_read_register (nfc_device_t * pnd, uint16_t ui16Reg, uint8_t * ui8Value)
byte_t abtRegValue[2]; byte_t abtRegValue[2];
size_t szRegValue = sizeof (abtRegValue); size_t szRegValue = sizeof (abtRegValue);
if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue)) { if (pn53x_transceive (pnd, abtCmd, sizeof (abtCmd), abtRegValue, &szRegValue)) {
if (((struct pn53x_data*)(pnd->chip_data))->type == PN533) { if (CHIP_DATA(pnd)->type == PN533) {
// PN533 prepends its answer by a status byte // PN533 prepends its answer by a status byte
if (abtRegValue[0] == 0x00) { if (abtRegValue[0] == 0x00) {
*ui8Value = abtRegValue[1]; *ui8Value = abtRegValue[1];
@ -533,7 +531,7 @@ pn53x_initiator_select_passive_target (nfc_device_t * pnd,
if (pnt) { if (pnt) {
pnt->nm = nm; pnt->nm = nm;
// Fill the tag info struct with the values corresponding to this init modulation // Fill the tag info struct with the values corresponding to this init modulation
if (!pn53x_decode_target_data (abtTargetsData + 1, szTargetsData - 1, ((struct pn53x_data*)(pnd->chip_data))->type, nm.nmt, &(pnt->nti))) { if (!pn53x_decode_target_data (abtTargetsData + 1, szTargetsData - 1, CHIP_DATA(pnd)->type, nm.nmt, &(pnt->nti))) {
return false; return false;
} }
} }
@ -611,7 +609,7 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd,
} }
break; break;
case PM_JEWEL_106: case PM_JEWEL_106:
if(((struct pn53x_data*)(pnd->chip_data))->type == PN531) { if(CHIP_DATA(pnd)->type == PN531) {
// These modulations are not supported by pn531 // These modulations are not supported by pn531
pnd->iLastError = DENOTSUP; pnd->iLastError = DENOTSUP;
return false; return false;
@ -620,7 +618,7 @@ pn53x_InListPassiveTarget (nfc_device_t * pnd,
case PM_ISO14443B_212: case PM_ISO14443B_212:
case PM_ISO14443B_424: case PM_ISO14443B_424:
case PM_ISO14443B_847: case PM_ISO14443B_847:
if((((struct pn53x_data*)(pnd->chip_data))->type != PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) { if((CHIP_DATA(pnd)->type != PN533) || (!(pnd->btSupportByte & SUPPORT_ISO14443B))) {
// These modulations are not supported by pn531 neither pn532 // These modulations are not supported by pn531 neither pn532
pnd->iLastError = DENOTSUP; pnd->iLastError = DENOTSUP;
return false; return false;
@ -681,7 +679,7 @@ pn53x_InAutoPoll (nfc_device_t * pnd,
const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes, const pn53x_target_type_t * ppttTargetTypes, const size_t szTargetTypes,
const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound) const byte_t btPollNr, const byte_t btPeriod, nfc_target_t * pntTargets, size_t * pszTargetFound)
{ {
if (((struct pn53x_data*)(pnd->chip_data))->type != PN532) { if (CHIP_DATA(pnd)->type != PN532) {
// This function is not supported by pn531 neither pn533 // This function is not supported by pn531 neither pn533
pnd->iLastError = DENOTSUP; pnd->iLastError = DENOTSUP;
return false; return false;
@ -711,7 +709,7 @@ pn53x_InAutoPoll (nfc_device_t * pnd,
pntTargets[0].nm = pn53x_ptt_to_nm(ptt); pntTargets[0].nm = pn53x_ptt_to_nm(ptt);
// AutoPollTargetData length // AutoPollTargetData length
ln = *(pbt++); ln = *(pbt++);
pn53x_decode_target_data (pbt, ln, ((struct pn53x_data*)(pnd->chip_data))->type, pntTargets[0].nm.nmt, &(pntTargets[0].nti)); pn53x_decode_target_data (pbt, ln, CHIP_DATA(pnd)->type, pntTargets[0].nm.nmt, &(pntTargets[0].nti));
pbt += ln; pbt += ln;
if (abtRx[0] > 1) { if (abtRx[0] > 1) {
@ -721,7 +719,7 @@ pn53x_InAutoPoll (nfc_device_t * pnd,
pntTargets[1].nm = pn53x_ptt_to_nm(ptt); pntTargets[1].nm = pn53x_ptt_to_nm(ptt);
// AutoPollTargetData length // AutoPollTargetData length
ln = *(pbt++); ln = *(pbt++);
pn53x_decode_target_data (pbt, ln, ((struct pn53x_data*)(pnd->chip_data))->type, pntTargets[1].nm.nmt, &(pntTargets[1].nti)); pn53x_decode_target_data (pbt, ln, CHIP_DATA(pnd)->type, pntTargets[1].nm.nmt, &(pntTargets[1].nti));
} }
} }
} }
@ -811,12 +809,12 @@ pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[18])
} }
// Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC // Determine which version of chip it is: PN531 will return only 2 bytes, while others return 4 bytes and have the first to tell the version IC
if (szFwLen == 2) { if (szFwLen == 2) {
((struct pn53x_data*)(pnd->chip_data))->type = PN531; CHIP_DATA(pnd)->type = PN531;
} else if (szFwLen == 4) { } else if (szFwLen == 4) {
if (abtFw[0] == 0x32) { // PN532 version IC if (abtFw[0] == 0x32) { // PN532 version IC
((struct pn53x_data*)(pnd->chip_data))->type = PN532; CHIP_DATA(pnd)->type = PN532;
} else if (abtFw[0] == 0x33) { // PN532 version IC } else if (abtFw[0] == 0x33) { // PN532 version IC
((struct pn53x_data*)(pnd->chip_data))->type = PN533; CHIP_DATA(pnd)->type = PN533;
} else { } else {
// Unknown version IC // Unknown version IC
return false; return false;
@ -826,7 +824,7 @@ pn53x_get_firmware_version (nfc_device_t * pnd, char abtFirmwareText[18])
return false; return false;
} }
// Convert firmware info in text, PN531 gives 2 bytes info, but PN532 and PN533 gives 4 // Convert firmware info in text, PN531 gives 2 bytes info, but PN532 and PN533 gives 4
switch (((struct pn53x_data*)(pnd->chip_data))->type) { switch (CHIP_DATA(pnd)->type) {
case PN531: case PN531:
snprintf (abtFirmwareText, 18, "PN531 v%d.%d", abtFw[0], abtFw[1]); snprintf (abtFirmwareText, 18, "PN531 v%d.%d", abtFw[0], abtFw[1]);
pnd->btSupportByte = SUPPORT_ISO14443A | SUPPORT_ISO18092; pnd->btSupportByte = SUPPORT_ISO14443A | SUPPORT_ISO18092;
@ -1201,7 +1199,7 @@ pn53x_target_init (nfc_device_t * pnd, nfc_target_t * pnt, byte_t * pbtRx, size_
return false; return false;
} }
pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false); pn53x_set_parameters (pnd, PARAM_AUTO_ATR_RES, false);
if (((struct pn53x_data*)(pnd->chip_data))->type == PN532) { // We have a PN532 if (CHIP_DATA(pnd)->type == PN532) { // We have a PN532
if ((pnt->nti.nai.btSak & SAK_ISO14443_4_COMPLIANT) && (pnd->bAutoIso14443_4)) { if ((pnt->nti.nai.btSak & SAK_ISO14443_4_COMPLIANT) && (pnd->bAutoIso14443_4)) {
// We have a ISO14443-4 tag to emulate and NDO_AUTO_14443_4A option is enabled // We have a ISO14443-4 tag to emulate and NDO_AUTO_14443_4A option is enabled
ptm |= PTM_ISO14443_4_PICC_ONLY; // We add ISO14443-4 restriction ptm |= PTM_ISO14443_4_PICC_ONLY; // We add ISO14443-4 restriction
@ -1390,7 +1388,7 @@ pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm,
memcpy(abtCmd+26, pbtNFCID3t, 10); memcpy(abtCmd+26, pbtNFCID3t, 10);
} }
// General Bytes (ISO/IEC 18092) // General Bytes (ISO/IEC 18092)
if (((struct pn53x_data*)(pnd->chip_data))->type == PN531) { if (CHIP_DATA(pnd)->type == PN531) {
if (szGBt) { if (szGBt) {
memcpy (abtCmd+36, pbtGBt, szGBt); memcpy (abtCmd+36, pbtGBt, szGBt);
szOptionalBytes = szGBt; szOptionalBytes = szGBt;
@ -1403,7 +1401,7 @@ pn53x_TgInitAsTarget (nfc_device_t * pnd, pn53x_target_mode_t ptm,
szOptionalBytes = szGBt + 1; szOptionalBytes = szGBt + 1;
} }
// Historical bytes (ISO/IEC 14443-4) // Historical bytes (ISO/IEC 14443-4)
if (((struct pn53x_data*)(pnd->chip_data))->type != PN531) { // PN531 does not handle Historical Bytes if (CHIP_DATA(pnd)->type != PN531) { // PN531 does not handle Historical Bytes
abtCmd[36+szOptionalBytes] = (byte_t)(szTkt); abtCmd[36+szOptionalBytes] = (byte_t)(szTkt);
if (szTkt) { if (szTkt) {
memcpy (abtCmd+37+szOptionalBytes, pbtTkt, szTkt); memcpy (abtCmd+37+szOptionalBytes, pbtTkt, szTkt);

View file

@ -133,6 +133,8 @@ struct pn53x_data {
uint8_t ui8LastCommand; uint8_t ui8LastCommand;
}; };
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
/* PN53x specific types */ /* PN53x specific types */
/** /**
* @enum pn53x_modulation_t * @enum pn53x_modulation_t

View file

@ -91,7 +91,6 @@ struct acr122_data {
size_t szRx; size_t szRx;
}; };
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
#define DRIVER_DATA(pnd) ((struct acr122_data*)(pnd->driver_data)) #define DRIVER_DATA(pnd) ((struct acr122_data*)(pnd->driver_data))
static SCARDCONTEXT _SCardContext; static SCARDCONTEXT _SCardContext;

View file

@ -66,7 +66,6 @@
#define ARYGON_DRIVER_NAME "ARYGON" #define ARYGON_DRIVER_NAME "ARYGON"
#define DRIVER_DATA(pnd) ((struct arygon_data*)(pnd->driver_data)) #define DRIVER_DATA(pnd) ((struct arygon_data*)(pnd->driver_data))
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
const struct pn53x_io arygon_tama_io; const struct pn53x_io arygon_tama_io;

View file

@ -57,7 +57,6 @@ struct pn532_uart_data {
serial_port port; serial_port port;
}; };
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
#define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data)) #define DRIVER_DATA(pnd) ((struct pn532_uart_data*)(pnd->driver_data))
bool bool

View file

@ -49,7 +49,6 @@ Thanks to d18c7db and Okko for example code
#define PN53X_USB_DRIVER_NAME "PN53x USB" #define PN53X_USB_DRIVER_NAME "PN53x USB"
#define USB_TIMEOUT 0 #define USB_TIMEOUT 0
#define CHIP_DATA(pnd) ((struct pn53x_data*)(pnd->chip_data))
#define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data)) #define DRIVER_DATA(pnd) ((struct pn53x_usb_data*)(pnd->driver_data))
typedef enum { typedef enum {
@ -361,7 +360,7 @@ pn53x_usb_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
} }
if (pn53x_check_ack_frame (pnd, abtRxBuf, res)) { if (pn53x_check_ack_frame (pnd, abtRxBuf, res)) {
((struct pn53x_data*)(pnd->chip_data))->state = EXECUTE; CHIP_DATA(pnd)->state = EXECUTE;
} else { } else {
return false; return false;
} }