Enhance logging system:
* Removes internal DBG/WARN/ERR macros (but keep them in examples); * Removes almost all DEBUG define references; * Uses the flexible log4c library to log all messages.
This commit is contained in:
+9
-11
@@ -72,6 +72,8 @@
|
||||
#define ACR122_COMMAND_LEN 266
|
||||
#define ACR122_RESPONSE_LEN 268
|
||||
|
||||
#define LOG_CATEGORY "libnfc.driver.acr122"
|
||||
|
||||
const struct pn53x_io acr122_io;
|
||||
|
||||
char *acr122_firmware (nfc_device_t *pnd);
|
||||
@@ -148,7 +150,7 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev
|
||||
|
||||
// Test if context succeeded
|
||||
if (!(pscc = acr122_get_scardcontext ())) {
|
||||
DBG ("%s", "PCSC context not found");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PCSC context not found");
|
||||
return false;
|
||||
}
|
||||
// Retrieve the string array of all available pcsc readers
|
||||
@@ -176,7 +178,7 @@ acr122_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev
|
||||
pnddDevices[*pszDeviceFound].uiBusIndex = uiBusIndex;
|
||||
(*pszDeviceFound)++;
|
||||
} else {
|
||||
DBG ("PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "PCSC device [%s] is not NFC capable or not supported by libnfc.", acDeviceNames + szPos);
|
||||
}
|
||||
|
||||
// Find next device name position
|
||||
@@ -199,7 +201,7 @@ acr122_connect (const nfc_device_desc_t * pndd)
|
||||
|
||||
SCARDCONTEXT *pscc;
|
||||
|
||||
DBG ("Attempt to connect to %s", pndd->acDevice);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to %s", pndd->acDevice);
|
||||
// Test if context succeeded
|
||||
if (!(pscc = acr122_get_scardcontext ()))
|
||||
goto error;
|
||||
@@ -208,7 +210,7 @@ acr122_connect (const nfc_device_desc_t * pndd)
|
||||
// Connect to ACR122 firmware version >2.0
|
||||
if (SCardConnect (*pscc, pndd->acDevice, SCARD_SHARE_DIRECT, 0, &(DRIVER_DATA (pnd)->hCard), (void *) &(DRIVER_DATA (pnd)->ioCard.dwProtocol)) != SCARD_S_SUCCESS) {
|
||||
// We can not connect to this device.
|
||||
DBG ("%s", "PCSC connect failed");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "PCSC connect failed");
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -262,9 +264,7 @@ acr122_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData)
|
||||
const size_t szTxBuf = szData + 6;
|
||||
byte_t abtTxBuf[ACR122_WRAP_LEN + ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00, szData + 1, 0xD4 };
|
||||
memcpy (abtTxBuf + 6, pbtData, szData);
|
||||
#ifdef DEBUG
|
||||
PRINT_HEX ("TX", abtTxBuf, szTxBuf);
|
||||
#endif
|
||||
LOG_HEX ("TX", abtTxBuf, szTxBuf);
|
||||
|
||||
DRIVER_DATA (pnd)->szRx = 0;
|
||||
|
||||
@@ -341,9 +341,7 @@ acr122_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szData)
|
||||
* We already have the PN532 answer, it was saved by acr122_send().
|
||||
*/
|
||||
}
|
||||
#ifdef DEBUG
|
||||
PRINT_HEX ("RX", DRIVER_DATA (pnd)->abtRx, DRIVER_DATA (pnd)->szRx);
|
||||
#endif
|
||||
LOG_HEX ("RX", DRIVER_DATA (pnd)->abtRx, DRIVER_DATA (pnd)->szRx);
|
||||
|
||||
// Make sure we have an emulated answer that fits the return buffer
|
||||
if (DRIVER_DATA (pnd)->szRx < 4 || (DRIVER_DATA (pnd)->szRx - 4) > szData) {
|
||||
@@ -375,7 +373,7 @@ acr122_firmware (nfc_device_t *pnd)
|
||||
}
|
||||
|
||||
if (uiResult != SCARD_S_SUCCESS) {
|
||||
ERR ("No ACR122 firmware received, Error: %08x", uiResult);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "No ACR122 firmware received, Error: %08x", uiResult);
|
||||
}
|
||||
|
||||
return abtFw;
|
||||
|
||||
+27
-29
@@ -65,6 +65,7 @@
|
||||
|
||||
#define ARYGON_DEFAULT_SPEED 9600
|
||||
#define ARYGON_DRIVER_NAME "ARYGON"
|
||||
#define LOG_CATEGORY "libnfc.driver.arygon"
|
||||
|
||||
#define DRIVER_DATA(pnd) ((struct arygon_data*)(pnd->driver_data))
|
||||
|
||||
@@ -96,7 +97,7 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev
|
||||
(void) pnddDevices;
|
||||
(void) szDevices;
|
||||
*pszDeviceFound = 0;
|
||||
DBG ("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
|
||||
return false;
|
||||
#else /* SERIAL_AUTOPROBE_ENABLED */
|
||||
*pszDeviceFound = 0;
|
||||
@@ -108,7 +109,7 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev
|
||||
|
||||
while ((acPort = acPorts[iDevice++])) {
|
||||
sp = uart_open (acPort);
|
||||
DBG ("Trying to find ARYGON device on serial port: %s at %d bauds.", acPort, ARYGON_DEFAULT_SPEED);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find ARYGON device on serial port: %s at %d bauds.", acPort, ARYGON_DEFAULT_SPEED);
|
||||
|
||||
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) {
|
||||
// We need to flush input to be sure first reply does not comes from older byte transceive
|
||||
@@ -141,13 +142,10 @@ arygon_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * pszDev
|
||||
if ((*pszDeviceFound) >= szDevices)
|
||||
break;
|
||||
}
|
||||
# ifdef DEBUG
|
||||
if (sp == INVALID_SERIAL_PORT)
|
||||
DBG ("Invalid serial port: %s", acPort);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Invalid serial port: %s", acPort);
|
||||
if (sp == CLAIMED_SERIAL_PORT)
|
||||
DBG ("Serial port already claimed: %s", acPort);
|
||||
# endif
|
||||
/* DEBUG */
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Serial port already claimed: %s", acPort);
|
||||
}
|
||||
iDevice = 0;
|
||||
while ((acPort = acPorts[iDevice++])) {
|
||||
@@ -164,13 +162,13 @@ arygon_connect (const nfc_device_desc_t * pndd)
|
||||
serial_port sp;
|
||||
nfc_device_t *pnd = NULL;
|
||||
|
||||
DBG ("Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed);
|
||||
sp = uart_open (pndd->acPort);
|
||||
|
||||
if (sp == INVALID_SERIAL_PORT)
|
||||
ERR ("Invalid serial port: %s", pndd->acPort);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", pndd->acPort);
|
||||
if (sp == CLAIMED_SERIAL_PORT)
|
||||
ERR ("Serial port already claimed: %s", pndd->acPort);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", pndd->acPort);
|
||||
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT))
|
||||
return NULL;
|
||||
|
||||
@@ -248,7 +246,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat
|
||||
size_t szFrame = 0;
|
||||
if (szData > PN53x_NORMAL_FRAME__DATA_MAX_LEN) {
|
||||
// ARYGON Reader with PN532 equipped does not support extended frame (bug in ARYGON firmware?)
|
||||
DBG ("ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ARYGON device does not support more than %d bytes as payload (requested: %zd)", PN53x_NORMAL_FRAME__DATA_MAX_LEN, szData);
|
||||
pnd->iLastError = EDEVNOTSUP;
|
||||
return false;
|
||||
}
|
||||
@@ -260,7 +258,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat
|
||||
|
||||
int res = uart_send (DRIVER_DATA (pnd)->port, abtFrame, szFrame + 1);
|
||||
if (res != 0) {
|
||||
ERR ("%s", "Unable to transmit data. (TX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)");
|
||||
pnd->iLastError = res;
|
||||
return false;
|
||||
}
|
||||
@@ -268,7 +266,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat
|
||||
byte_t abtRxBuf[6];
|
||||
res = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, sizeof (abtRxBuf), 0);
|
||||
if (res != 0) {
|
||||
ERR ("%s", "Unable to read ACK");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK");
|
||||
pnd->iLastError = res;
|
||||
return false;
|
||||
}
|
||||
@@ -276,7 +274,7 @@ arygon_tama_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szDat
|
||||
if (pn53x_check_ack_frame (pnd, abtRxBuf, sizeof(abtRxBuf))) {
|
||||
// The PN53x is running the sent command
|
||||
} else if (0 == memcmp(arygon_error_unknown_mode, abtRxBuf, sizeof(abtRxBuf))) {
|
||||
ERR( "Bad frame format." );
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Bad frame format." );
|
||||
// We have already read 6 bytes and arygon_error_unknown_mode is 10 bytes long
|
||||
// so we have to read 4 remaining bytes to be synchronized at the next receiving pass.
|
||||
uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 4, 0);
|
||||
@@ -333,13 +331,13 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
}
|
||||
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
|
||||
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
|
||||
ERR ("%s", "Frame preamble+start code mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -347,7 +345,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) {
|
||||
// Error frame
|
||||
uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 3, 0);
|
||||
ERR ("%s", "Application level error detected");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected");
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return -1;
|
||||
} else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) {
|
||||
@@ -358,7 +356,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
// Normal frame
|
||||
if (256 != (abtRxBuf[3] + abtRxBuf[4])) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -368,7 +366,7 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
}
|
||||
|
||||
if (len > szDataLen) {
|
||||
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -376,18 +374,18 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
// TFI + PD0 (CC+1)
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0);
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (abtRxBuf[0] != 0xD5) {
|
||||
ERR ("%s", "TFI Mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) {
|
||||
ERR ("%s", "Command Code verification failed");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -395,14 +393,14 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
if (len) {
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, pbtData, len, 0);
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA (pnd)->port, abtRxBuf, 2, 0);
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -413,13 +411,13 @@ arygon_tama_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLe
|
||||
}
|
||||
|
||||
if (btDCS != abtRxBuf[0]) {
|
||||
ERR ("%s", "Data checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0x00 != abtRxBuf[1]) {
|
||||
ERR ("%s", "Frame postamble mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -437,12 +435,12 @@ arygon_firmware (nfc_device_t * pnd, char * str)
|
||||
|
||||
int res = uart_send (DRIVER_DATA (pnd)->port, arygon_firmware_version_cmd, sizeof (arygon_firmware_version_cmd));
|
||||
if (res != 0) {
|
||||
DBG ("Unable to send ARYGON firmware command.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to send ARYGON firmware command.");
|
||||
return;
|
||||
}
|
||||
res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0);
|
||||
if (res != 0) {
|
||||
DBG ("Unable to retrieve ARYGON firmware version.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Unable to retrieve ARYGON firmware version.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -469,7 +467,7 @@ arygon_reset_tama (nfc_device_t * pnd)
|
||||
// or arygon_error_unknown_mode (ie. in case of the first byte was bad-transmitted)
|
||||
res = uart_receive (DRIVER_DATA (pnd)->port, abtRx, szRx, 0);
|
||||
if (res != 0) {
|
||||
DBG ("No reply to 'reset TAMA' command.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "No reply to 'reset TAMA' command.");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
+21
-27
@@ -45,6 +45,7 @@
|
||||
|
||||
#define PN532_UART_DEFAULT_SPEED 115200
|
||||
#define PN532_UART_DRIVER_NAME "PN532_UART"
|
||||
#define LOG_CATEGORY "libnfc.driver.pn532_uart"
|
||||
|
||||
int pn532_uart_ack (nfc_device_t * pnd);
|
||||
int pn532_uart_wakeup (nfc_device_t * pnd);
|
||||
@@ -72,7 +73,7 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps
|
||||
(void) pnddDevices;
|
||||
(void) szDevices;
|
||||
*pszDeviceFound = 0;
|
||||
DBG ("%s", "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_INFO, "Serial auto-probing have been disabled at compile time. Skipping autoprobe.");
|
||||
return false;
|
||||
#else /* SERIAL_AUTOPROBE_ENABLED */
|
||||
*pszDeviceFound = 0;
|
||||
@@ -84,7 +85,7 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps
|
||||
|
||||
while ((acPort = acPorts[iDevice++])) {
|
||||
sp = uart_open (acPort);
|
||||
DBG ("Trying to find PN532 device on serial port: %s at %d bauds.", acPort, PN532_UART_DEFAULT_SPEED);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Trying to find PN532 device on serial port: %s at %d bauds.", acPort, PN532_UART_DEFAULT_SPEED);
|
||||
|
||||
if ((sp != INVALID_SERIAL_PORT) && (sp != CLAIMED_SERIAL_PORT)) {
|
||||
// We need to flush input to be sure first reply does not comes from older byte transceive
|
||||
@@ -126,13 +127,6 @@ pn532_uart_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * ps
|
||||
if ((*pszDeviceFound) >= szDevices)
|
||||
break;
|
||||
}
|
||||
# ifdef DEBUG
|
||||
if (sp == INVALID_SERIAL_PORT)
|
||||
DBG ("Invalid serial port: %s", acPort);
|
||||
if (sp == CLAIMED_SERIAL_PORT)
|
||||
DBG ("Serial port already claimed: %s", acPort);
|
||||
# endif
|
||||
/* DEBUG */
|
||||
}
|
||||
|
||||
iDevice = 0;
|
||||
@@ -150,13 +144,13 @@ pn532_uart_connect (const nfc_device_desc_t * pndd)
|
||||
serial_port sp;
|
||||
nfc_device_t *pnd = NULL;
|
||||
|
||||
DBG ("Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Attempt to connect to: %s at %d bauds.", pndd->acPort, pndd->uiSpeed);
|
||||
sp = uart_open (pndd->acPort);
|
||||
|
||||
if (sp == INVALID_SERIAL_PORT)
|
||||
ERR ("Invalid serial port: %s", pndd->acPort);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Invalid serial port: %s", pndd->acPort);
|
||||
if (sp == CLAIMED_SERIAL_PORT)
|
||||
ERR ("Serial port already claimed: %s", pndd->acPort);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Serial port already claimed: %s", pndd->acPort);
|
||||
if ((sp == CLAIMED_SERIAL_PORT) || (sp == INVALID_SERIAL_PORT))
|
||||
return NULL;
|
||||
|
||||
@@ -266,7 +260,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData
|
||||
|
||||
int res = uart_send (DRIVER_DATA(pnd)->port, abtFrame, szFrame);
|
||||
if (res != 0) {
|
||||
ERR ("%s", "Unable to transmit data. (TX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to transmit data. (TX)");
|
||||
pnd->iLastError = res;
|
||||
return false;
|
||||
}
|
||||
@@ -274,7 +268,7 @@ pn532_uart_send (nfc_device_t * pnd, const byte_t * pbtData, const size_t szData
|
||||
byte_t abtRxBuf[6];
|
||||
res = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 6, 0);
|
||||
if (res != 0) {
|
||||
ERR ("%s", "Unable to read ACK");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to read ACK");
|
||||
pnd->iLastError = res;
|
||||
return false;
|
||||
}
|
||||
@@ -318,13 +312,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
}
|
||||
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
|
||||
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
|
||||
ERR ("%s", "Frame preamble+start code mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -332,7 +326,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
if ((0x01 == abtRxBuf[3]) && (0xff == abtRxBuf[4])) {
|
||||
// Error frame
|
||||
uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 3, 0);
|
||||
ERR ("%s", "Application level error detected");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected");
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return -1;
|
||||
} else if ((0xff == abtRxBuf[3]) && (0xff == abtRxBuf[4])) {
|
||||
@@ -344,7 +338,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
len = (abtRxBuf[0] << 8) + abtRxBuf[1] - 2;
|
||||
if (((abtRxBuf[0] + abtRxBuf[1] + abtRxBuf[2]) % 256) != 0) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -352,7 +346,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
// Normal frame
|
||||
if (256 != (abtRxBuf[3] + abtRxBuf[4])) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -362,7 +356,7 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
}
|
||||
|
||||
if (len > szDataLen) {
|
||||
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -370,18 +364,18 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
// TFI + PD0 (CC+1)
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0);
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (abtRxBuf[0] != 0xD5) {
|
||||
ERR ("%s", "TFI Mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (abtRxBuf[1] != CHIP_DATA (pnd)->ui8LastCommand + 1) {
|
||||
ERR ("%s", "Command Code verification failed");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -389,14 +383,14 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
if (len) {
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, pbtData, len, 0);
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
pnd->iLastError = uart_receive (DRIVER_DATA(pnd)->port, abtRxBuf, 2, 0);
|
||||
if (pnd->iLastError != 0) {
|
||||
ERR ("%s", "Unable to receive data. (RX)");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Unable to receive data. (RX)");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -407,13 +401,13 @@ pn532_uart_receive (nfc_device_t * pnd, byte_t * pbtData, const size_t szDataLen
|
||||
}
|
||||
|
||||
if (btDCS != abtRxBuf[0]) {
|
||||
ERR ("%s", "Data checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0x00 != abtRxBuf[1]) {
|
||||
ERR ("%s", "Frame postamble mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
+28
-28
@@ -59,6 +59,7 @@ Thanks to d18c7db and Okko for example code
|
||||
#include "drivers/pn53x_usb.h"
|
||||
|
||||
#define PN53X_USB_DRIVER_NAME "PN53x USB"
|
||||
#define LOG_CATEGORY "libnfc.driver.pn53x_usb"
|
||||
|
||||
#define USB_INFINITE_TIMEOUT 0
|
||||
|
||||
@@ -92,9 +93,9 @@ pn53x_usb_bulk_read (struct pn53x_usb_data *data, byte_t abtRx[], const size_t s
|
||||
{
|
||||
int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, USB_INFINITE_TIMEOUT);
|
||||
if (res > 0) {
|
||||
PRINT_HEX ("RX", abtRx, res);
|
||||
LOG_HEX ("RX", abtRx, res);
|
||||
} else if (res < 0) {
|
||||
ERR ("Unable to read from USB (%s", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s", _usb_strerror (res));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -104,10 +105,10 @@ pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_
|
||||
{
|
||||
int res = usb_bulk_read (data->pudh, data->uiEndPointIn, (char *) abtRx, szRx, timeout);
|
||||
if (res > 0) {
|
||||
PRINT_HEX ("RX", abtRx, res);
|
||||
LOG_HEX ("RX", abtRx, res);
|
||||
} else if (res < 0) {
|
||||
if (-res != USB_TIMEDOUT) {
|
||||
ERR ("Unable to read from USB (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to read from USB (%s)", _usb_strerror (res));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
@@ -116,7 +117,7 @@ pn53x_usb_bulk_read_ex (struct pn53x_usb_data *data, byte_t abtRx[], const size_
|
||||
int
|
||||
pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t szTx)
|
||||
{
|
||||
PRINT_HEX ("TX", abtTx, szTx);
|
||||
LOG_HEX ("TX", abtTx, szTx);
|
||||
int res = usb_bulk_write (data->pudh, data->uiEndPointOut, (char *) abtTx, szTx, USB_INFINITE_TIMEOUT);
|
||||
if (res > 0) {
|
||||
// HACK This little hack is a well know problem of USB, see http://www.libusb.org/ticket/6 for more details
|
||||
@@ -124,7 +125,7 @@ pn53x_usb_bulk_write (struct pn53x_usb_data *data, byte_t abtTx[], const size_t
|
||||
usb_bulk_write (data->pudh, data->uiEndPointOut, "\0", 0, USB_INFINITE_TIMEOUT);
|
||||
}
|
||||
} else {
|
||||
ERR ("Unable to write to USB (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to write to USB (%s)", _usb_strerror (res));
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -199,14 +200,14 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz
|
||||
// number of changes since previous call to this function (total of new
|
||||
// busses and busses removed).
|
||||
if ((res = usb_find_busses () < 0)) {
|
||||
ERR ("Unable to find USB busses (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB busses (%s)", _usb_strerror (res));
|
||||
return false;
|
||||
}
|
||||
// usb_find_devices will find all of the devices on each bus. This should be
|
||||
// called after usb_find_busses. Returns the number of changes since the
|
||||
// previous call to this function (total of new device and devices removed).
|
||||
if ((res = usb_find_devices () < 0)) {
|
||||
ERR ("Unable to find USB devices (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to find USB devices (%s)", _usb_strerror (res));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -219,7 +220,6 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz
|
||||
|
||||
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
|
||||
for (size_t n = 0; n < sizeof (pn53x_usb_supported_devices) / sizeof (struct pn53x_usb_supported_device); n++) {
|
||||
//DBG("Checking device %04x:%04x (%04x:%04x)",dev->descriptor.idVendor,dev->descriptor.idProduct);
|
||||
if ((pn53x_usb_supported_devices[n].vendor_id == dev->descriptor.idVendor) &&
|
||||
(pn53x_usb_supported_devices[n].product_id == dev->descriptor.idProduct)) {
|
||||
// Make sure there are 2 endpoints available
|
||||
@@ -238,7 +238,7 @@ pn53x_usb_probe (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t * psz
|
||||
// Set configuration
|
||||
int res = usb_set_configuration (udev, 1);
|
||||
if (res < 0) {
|
||||
ERR ("Unable to set USB configuration (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res));
|
||||
usb_close (udev);
|
||||
// we failed to use the device
|
||||
continue;
|
||||
@@ -307,7 +307,7 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd)
|
||||
|
||||
for (bus = usb_get_busses (); bus; bus = bus->next) {
|
||||
for (dev = bus->devices; dev; dev = dev->next, uiBusIndex--) {
|
||||
DBG ("Checking device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Checking device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
|
||||
if (uiBusIndex == 0) {
|
||||
// Open the USB device
|
||||
data.pudh = usb_open (dev);
|
||||
@@ -316,9 +316,9 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd)
|
||||
// Set configuration
|
||||
int res = usb_set_configuration (data.pudh, 1);
|
||||
if (res < 0) {
|
||||
ERR ("Unable to set USB configuration (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to set USB configuration (%s)", _usb_strerror (res));
|
||||
if (EPERM == -res) {
|
||||
WARN ("Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_WARN, "Please double check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
|
||||
}
|
||||
usb_close (data.pudh);
|
||||
// we failed to use the specified device
|
||||
@@ -327,7 +327,7 @@ pn53x_usb_connect (const nfc_device_desc_t *pndd)
|
||||
|
||||
res = usb_claim_interface (data.pudh, 0);
|
||||
if (res < 0) {
|
||||
ERR ("Unable to claim USB interface (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to claim USB interface (%s)", _usb_strerror (res));
|
||||
usb_close (data.pudh);
|
||||
// we failed to use the specified device
|
||||
return NULL;
|
||||
@@ -401,11 +401,11 @@ pn53x_usb_disconnect (nfc_device_t * pnd)
|
||||
|
||||
int res;
|
||||
if ((res = usb_release_interface (DRIVER_DATA (pnd)->pudh, 0)) < 0) {
|
||||
ERR ("Unable to release USB interface (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to release USB interface (%s)", _usb_strerror (res));
|
||||
}
|
||||
|
||||
if ((res = usb_close (DRIVER_DATA (pnd)->pudh)) < 0) {
|
||||
ERR ("Unable to close USB connection (%s)", _usb_strerror (res));
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to close USB connection (%s)", _usb_strerror (res));
|
||||
}
|
||||
pn53x_data_free (pnd);
|
||||
nfc_device_free (pnd);
|
||||
@@ -502,7 +502,7 @@ read:
|
||||
|
||||
const byte_t pn53x_preamble[3] = { 0x00, 0x00, 0xff };
|
||||
if (0 != (memcmp (abtRxBuf, pn53x_preamble, 3))) {
|
||||
ERR ("%s", "Frame preamble+start code mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame preamble+start code mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -510,7 +510,7 @@ read:
|
||||
|
||||
if ((0x01 == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
|
||||
// Error frame
|
||||
ERR ("%s", "Application level error detected");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Application level error detected");
|
||||
pnd->iLastError = EFRAISERRFRAME;
|
||||
return -1;
|
||||
} else if ((0xff == abtRxBuf[offset]) && (0xff == abtRxBuf[offset + 1])) {
|
||||
@@ -521,7 +521,7 @@ read:
|
||||
len = (abtRxBuf[offset] << 8) + abtRxBuf[offset + 1] - 2;
|
||||
if (((abtRxBuf[offset] + abtRxBuf[offset + 1] + abtRxBuf[offset + 2]) % 256) != 0) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -530,7 +530,7 @@ read:
|
||||
// Normal frame
|
||||
if (256 != (abtRxBuf[offset] + abtRxBuf[offset + 1])) {
|
||||
// TODO: Retry
|
||||
ERR ("%s", "Length checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Length checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -541,21 +541,21 @@ read:
|
||||
}
|
||||
|
||||
if (len > szDataLen) {
|
||||
ERR ("Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "Unable to receive data: buffer too small. (szDataLen: %zu, len: %zu)", szDataLen, len);
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TFI + PD0 (CC+1)
|
||||
if (abtRxBuf[offset] != 0xD5) {
|
||||
ERR ("%s", "TFI Mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "TFI Mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 1;
|
||||
|
||||
if (abtRxBuf[offset] != CHIP_DATA (pnd)->ui8LastCommand + 1) {
|
||||
ERR ("%s", "Command Code verification failed");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Command Code verification failed");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -571,14 +571,14 @@ read:
|
||||
}
|
||||
|
||||
if (btDCS != abtRxBuf[offset]) {
|
||||
ERR ("%s", "Data checksum mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Data checksum mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
offset += 1;
|
||||
|
||||
if (0x00 != abtRxBuf[offset]) {
|
||||
ERR ("%s", "Frame postamble mismatch");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_ERROR, "%s", "Frame postamble mismatch");
|
||||
pnd->iLastError = ECOMIO;
|
||||
return -1;
|
||||
}
|
||||
@@ -603,7 +603,7 @@ pn53x_usb_init (nfc_device_t *pnd)
|
||||
// ...and we don't care about error
|
||||
pnd->iLastError = 0;
|
||||
if (SONY_RCS360 == DRIVER_DATA (pnd)->model) {
|
||||
DBG ("SONY RC-S360 initialization.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "SONY RC-S360 initialization.");
|
||||
const byte_t abtCmd2[] = { 0x18, 0x01 };
|
||||
pn53x_transceive (pnd, abtCmd2, sizeof (abtCmd2), NULL, NULL);
|
||||
pn53x_usb_ack (pnd);
|
||||
@@ -613,7 +613,7 @@ pn53x_usb_init (nfc_device_t *pnd)
|
||||
return false;
|
||||
|
||||
if (ASK_LOGO == DRIVER_DATA (pnd)->model) {
|
||||
DBG ("ASK LoGO initialization.");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "ASK LoGO initialization.");
|
||||
/* Internal registers */
|
||||
/* Disable 100mA current limit, Power on Secure IC (SVDD) */
|
||||
pn53x_write_register (pnd, PN53X_REG_Control_switch_rng, 0xFF, SYMBOL_CURLIMOFF | SYMBOL_SIC_SWITCH_EN | SYMBOL_RANDOM_DATAREADY);
|
||||
@@ -660,7 +660,7 @@ pn53x_usb_configure (nfc_device_t * pnd, const nfc_device_option_t ndo, const bo
|
||||
case ASK_LOGO:
|
||||
if (NDO_ACTIVATE_FIELD == ndo) {
|
||||
/* Switch on/off LED2 and Progressive Field GPIO according to ACTIVATE_FIELD option */
|
||||
DBG ("Switch progressive field %s", bEnable ? "On" : "Off");
|
||||
log_put (LOG_CATEGORY, NFC_PRIORITY_TRACE, "Switch progressive field %s", bEnable ? "On" : "Off");
|
||||
if (!pn53x_write_register (pnd, PN53X_SFR_P3, _BV(P31) | _BV(P34), bEnable ? _BV (P34) : _BV (P31)))
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user