diff --git a/src/anticol.c b/src/anticol.c index 4bc8d83..3b7c3ba 100644 --- a/src/anticol.c +++ b/src/anticol.c @@ -30,21 +30,21 @@ along with this program. If not, see #define SAK_FLAG_ATS_SUPPORTED 0x20 -static byte abtRx[MAX_FRAME_LEN]; +static byte_t abtRx[MAX_FRAME_LEN]; static uint32_t uiRxBits; static uint32_t uiRxLen; -static byte abtUid[10]; +static byte_t abtUid[10]; static uint32_t uiUidLen = 4; static dev_info* pdi; // ISO14443A Anti-Collision Commands -byte abtReqa [1] = { 0x26 }; -byte abtSelectAll [2] = { 0x93,0x20 }; -byte abtSelectTag [9] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; -byte abtRats [4] = { 0xe0,0x50,0xbc,0xa5 }; -byte abtHalt [4] = { 0x50,0x00,0x57,0xcd }; +byte_t abtReqa [1] = { 0x26 }; +byte_t abtSelectAll [2] = { 0x93,0x20 }; +byte_t abtSelectTag [9] = { 0x93,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; +byte_t abtRats [4] = { 0xe0,0x50,0xbc,0xa5 }; +byte_t abtHalt [4] = { 0x50,0x00,0x57,0xcd }; -bool transmit_bits(const byte* pbtTx, const uint32_t uiTxBits) +bool transmit_bits(const byte_t* pbtTx, const uint32_t uiTxBits) { // Show transmitted command printf("R: "); print_hex_bits(pbtTx,uiTxBits); @@ -60,7 +60,7 @@ bool transmit_bits(const byte* pbtTx, const uint32_t uiTxBits) } -bool transmit_bytes(const byte* pbtTx, const uint32_t uiTxLen) +bool transmit_bytes(const byte_t* pbtTx, const uint32_t uiTxLen) { // Show transmitted command printf("R: "); print_hex(pbtTx,uiTxLen); diff --git a/src/bitutils.c b/src/bitutils.c index a2aa292..03cf72a 100644 --- a/src/bitutils.c +++ b/src/bitutils.c @@ -22,7 +22,7 @@ along with this program. If not, see #include "bitutils.h" -const static byte OddParity[256] = { +const static byte_t OddParity[256] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, @@ -41,7 +41,7 @@ const static byte OddParity[256] = { 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 }; -const static byte ByteMirror[256] = { +const static byte_t ByteMirror[256] = { 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, @@ -64,12 +64,12 @@ const static byte ByteMirror[256] = { 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff }; -byte oddparity(const byte bt) +byte_t oddparity(const byte_t bt) { return OddParity[bt]; } -void oddparity_bytes(const byte* pbtData, const uint32_t uiLen, byte* pbtPar) +void oddparity_bytes(const byte_t* pbtData, const uint32_t uiLen, byte_t* pbtPar) { uint32_t uiByteNr; @@ -80,24 +80,12 @@ void oddparity_bytes(const byte* pbtData, const uint32_t uiLen, byte* pbtPar) } } -byte mirror(byte bt) +byte_t mirror(byte_t bt) { return ByteMirror[bt]; } -uint32_t mirror32(uint32_t ui32Bits) -{ - mirror_bytes((byte*)&ui32Bits,4); - return ui32Bits; -} - -uint64_t mirror64(uint64_t ui64Bits) -{ - mirror_bytes((byte*)&ui64Bits,8); - return ui64Bits; -} - -void mirror_bytes(byte *pbts, uint32_t uiLen) +void mirror_bytes(byte_t *pbts, uint32_t uiLen) { uint32_t btNr; @@ -108,6 +96,18 @@ void mirror_bytes(byte *pbts, uint32_t uiLen) } } +uint32_t mirror32(uint32_t ui32Bits) +{ + mirror_bytes((byte_t*)&ui32Bits,4); + return ui32Bits; +} + +uint64_t mirror64(uint64_t ui64Bits) +{ + mirror_bytes((byte_t*)&ui64Bits,8); + return ui64Bits; +} + uint32_t swap_endian32(const void* pui32) { uint32_t ui32N = *((uint32_t*)pui32); @@ -120,23 +120,23 @@ uint64_t swap_endian64(const void* pui64) return (((ui64N&0xFF)<<56)+((ui64N&0xFF00)<<40)+((ui64N&0xFF0000)<<24)+((ui64N&0xFF000000)<<8)+((ui64N&0xFF00000000ull)>>8)+((ui64N&0xFF0000000000ull)>>24)+((ui64N&0xFF000000000000ull)>>40)+((ui64N&0xFF00000000000000ull)>>56)); } -void append_iso14443a_crc(byte* pbtData, uint32_t uiLen) +void append_iso14443a_crc(byte_t* pbtData, uint32_t uiLen) { - byte bt; + byte_t bt; uint32_t wCrc = 0x6363; do { bt = *pbtData++; - bt = (bt^(byte)(wCrc & 0x00FF)); + bt = (bt^(byte_t)(wCrc & 0x00FF)); bt = (bt^(bt<<4)); wCrc = (wCrc >> 8)^((uint32_t)bt << 8)^((uint32_t)bt<<3)^((uint32_t)bt>>4); } while (--uiLen); - *pbtData++ = (byte) (wCrc & 0xFF); - *pbtData = (byte) ((wCrc >> 8) & 0xFF); + *pbtData++ = (byte_t) (wCrc & 0xFF); + *pbtData = (byte_t) ((wCrc >> 8) & 0xFF); } -void print_hex(const byte* pbtData, const uint32_t uiBytes) +void print_hex(const byte_t* pbtData, const uint32_t uiBytes) { uint32_t uiPos; @@ -147,7 +147,7 @@ void print_hex(const byte* pbtData, const uint32_t uiBytes) printf("\n"); } -void print_hex_bits(const byte* pbtData, const uint32_t uiBits) +void print_hex_bits(const byte_t* pbtData, const uint32_t uiBits) { uint32_t uiPos; uint32_t uiBytes = uiBits/8; @@ -163,7 +163,7 @@ void print_hex_bits(const byte* pbtData, const uint32_t uiBits) printf("\n"); } -void print_hex_par(const byte* pbtData, const uint32_t uiBits, const byte* pbtDataPar) +void print_hex_par(const byte_t* pbtData, const uint32_t uiBits, const byte_t* pbtDataPar) { uint32_t uiPos; uint32_t uiBytes = uiBits/8; diff --git a/src/bitutils.h b/src/bitutils.h index b8021e0..97bdb16 100644 --- a/src/bitutils.h +++ b/src/bitutils.h @@ -23,24 +23,24 @@ along with this program. If not, see #include -#include "defines.h" +#include "types.h" -byte oddparity(const byte bt); -void oddparity_bytes(const byte* pbtData, const uint32_t uiLen, byte* pbtPar); +byte_t oddparity(const byte_t bt); +void oddparity_byte_ts(const byte_t* pbtData, const uint32_t uiLen, byte_t* pbtPar); -byte mirror(byte bt); +byte_t mirror(byte_t bt); uint32_t mirror32(uint32_t ui32Bits); uint64_t mirror64(uint64_t ui64Bits); -void mirror_bytes(byte *pbts, uint32_t uiLen); +void mirror_byte_ts(byte_t *pbts, uint32_t uiLen); uint32_t swap_endian32(const void* pui32); uint64_t swap_endian64(const void* pui64); -void append_iso14443a_crc(byte* pbtData, uint32_t uiLen); +void append_iso14443a_crc(byte_t* pbtData, uint32_t uiLen); -void print_hex(const byte* pbtData, const uint32_t uiLen); -void print_hex_bits(const byte* pbtData, const uint32_t uiBits); -void print_hex_par(const byte* pbtData, const uint32_t uiBits, const byte* pbtDataPar); +void print_hex(const byte_t* pbtData, const uint32_t uiLen); +void print_hex_bits(const byte_t* pbtData, const uint32_t uiBits); +void print_hex_par(const byte_t* pbtData, const uint32_t uiBits, const byte_t* pbtDataPar); #endif // _LIBNFC_BITUTILS_H_ diff --git a/src/defines.h b/src/defines.h index 8792560..a604a7b 100644 --- a/src/defines.h +++ b/src/defines.h @@ -23,8 +23,6 @@ along with this program. If not, see // #define DEBUG /* DEBUG flag can also be enabled using ./configure --enable-debug */ -typedef unsigned char byte; - typedef void* dev_spec; // Device connection specification #define INVALID_DEVICE_INFO 0 #define MAX_FRAME_LEN 264 @@ -32,8 +30,8 @@ typedef void* dev_spec; // Device connection specification #define MAX_DEVICES 16 // Useful macros -#define MIN(a,b) (((a) < (b)) ? (a) : (b)) -#define MAX(a,b) (((a) > (b)) ? (a) : (b)) +//#define MIN(a,b) (((a) < (b)) ? (a) : (b)) +//#define MAX(a,b) (((a) > (b)) ? (a) : (b)) #define INNER_XOR8(n) {n ^= (n >> 4); n ^= (n >> 2); n ^= (n >> 1); n &= 0x01; } #define INNER_XOR32(n) {n ^= (n >> 16); n ^= (n >> 8); INNER_XOR8(n); } #define INNER_XOR64(n) {n ^= (n >> 32); INNER_XOR32(n); } diff --git a/src/dev_acr122.c b/src/dev_acr122.c index 91c5e0d..6ed013b 100644 --- a/src/dev_acr122.c +++ b/src/dev_acr122.c @@ -55,13 +55,13 @@ typedef struct { SCARD_IO_REQUEST ioCard; } dev_spec_acr122; -static byte abtTxBuf[ACR122_WRAP_LEN+ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00 }; -static byte abtRxCmd[5] = { 0xFF,0xC0,0x00,0x00 }; -static byte uiRxCmdLen = sizeof(abtRxCmd); -static byte abtRxBuf[ACR122_RESPONSE_LEN]; +static byte_t abtTxBuf[ACR122_WRAP_LEN+ACR122_COMMAND_LEN] = { 0xFF, 0x00, 0x00, 0x00 }; +static byte_t abtRxCmd[5] = { 0xFF,0xC0,0x00,0x00 }; +static byte_t uiRxCmdLen = sizeof(abtRxCmd); +static byte_t abtRxBuf[ACR122_RESPONSE_LEN]; static size_t ulRxBufLen; -static byte abtGetFw[5] = { 0xFF,0x00,0x48,0x00,0x00 }; -static byte abtLed[9] = { 0xFF,0x00,0x40,0x05,0x04,0x00,0x00,0x00,0x00 }; +static byte_t abtGetFw[5] = { 0xFF,0x00,0x48,0x00,0x00 }; +static byte_t abtLed[9] = { 0xFF,0x00,0x40,0x05,0x04,0x00,0x00,0x00,0x00 }; dev_info* dev_acr122_connect(const uint32_t uiIndex) { @@ -178,7 +178,7 @@ void dev_acr122_disconnect(dev_info* pdi) free(pdi); } -bool dev_acr122_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen) +bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) { dev_spec_acr122* pdsa = (dev_spec_acr122*)ds; @@ -247,7 +247,7 @@ char* dev_acr122_firmware(const dev_spec ds) { uiResult = SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtGetFw,sizeof(abtGetFw),abtFw,ulFwLen,(void*)&ulFwLen); } else { - uiResult = SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtGetFw,sizeof(abtGetFw),NULL,(byte*)abtFw,(void*)&ulFwLen); + uiResult = SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtGetFw,sizeof(abtGetFw),NULL,(byte_t*)abtFw,(void*)&ulFwLen); } #ifdef DEBUG @@ -263,13 +263,13 @@ char* dev_acr122_firmware(const dev_spec ds) bool dev_acr122_led_red(const dev_spec ds, bool bOn) { dev_spec_acr122* pdsa = (dev_spec_acr122*)ds; - byte abtBuf[2]; + byte_t abtBuf[2]; size_t ulBufLen = sizeof(abtBuf); if (pdsa->ioCard.dwProtocol == SCARD_PROTOCOL_UNDEFINED) { return (SCardControl(pdsa->hCard,IOCTL_CCID_ESCAPE_SCARD_CTL_CODE,abtLed,sizeof(abtLed),abtBuf,ulBufLen,(void*)&ulBufLen) == SCARD_S_SUCCESS); } else { - return (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtLed,sizeof(abtLed),NULL,(byte*)abtBuf,(void*)&ulBufLen) == SCARD_S_SUCCESS); + return (SCardTransmit(pdsa->hCard,&(pdsa->ioCard),abtLed,sizeof(abtLed),NULL,(byte_t*)abtBuf,(void*)&ulBufLen) == SCARD_S_SUCCESS); } } diff --git a/src/dev_acr122.h b/src/dev_acr122.h index 3fd6b48..3f9c9dc 100644 --- a/src/dev_acr122.h +++ b/src/dev_acr122.h @@ -32,7 +32,7 @@ dev_info* dev_acr122_connect(const uint32_t uiIndex); void dev_acr122_disconnect(dev_info* pdi); // Callback function used by libnfc to transmit commands to the PN53X chip -bool dev_acr122_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen); +bool dev_acr122_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); // Various additional features this device supports char* dev_acr122_firmware(const dev_spec ds); diff --git a/src/dev_arygon.c b/src/dev_arygon.c index 717427c..997ac41 100644 --- a/src/dev_arygon.c +++ b/src/dev_arygon.c @@ -34,7 +34,7 @@ along with this program. If not, see #define BUFFER_LENGTH 256 #define USB_TIMEOUT 30000 -static byte abtTxBuf[BUFFER_LENGTH] = { 0x32, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" +static byte_t abtTxBuf[BUFFER_LENGTH] = { 0x32, 0x00, 0x00, 0xff }; // Every packet must start with "00 00 ff" dev_info* dev_arygon_connect(const uint32_t uiIndex) { @@ -88,9 +88,9 @@ void dev_arygon_disconnect(dev_info* pdi) free(pdi); } -bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen) +bool dev_arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) { - byte abtRxBuf[BUFFER_LENGTH]; + byte_t abtRxBuf[BUFFER_LENGTH]; uint32_t uiRxBufLen = BUFFER_LENGTH; uint32_t uiPos; diff --git a/src/dev_arygon.h b/src/dev_arygon.h index 8408dea..885b7dc 100644 --- a/src/dev_arygon.h +++ b/src/dev_arygon.h @@ -29,7 +29,7 @@ dev_info* dev_arygon_connect(const uint32_t uiIndex); void dev_arygon_disconnect(dev_info* pdi); // Callback function used by libnfc to transmit commands to the PN53X chip -bool dev_arygon_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen); +bool dev_arygon_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); #endif // _LIBNFC_DEV_ARYGON_H_ diff --git a/src/dev_pn531.c b/src/dev_pn531.c index f83ff35..82e7620 100644 --- a/src/dev_pn531.c +++ b/src/dev_pn531.c @@ -171,7 +171,7 @@ void dev_pn531_disconnect(dev_info* pdi) free(pdi); } -bool dev_pn531_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen) +bool dev_pn531_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) { uint32_t uiPos = 0; int ret = 0; @@ -179,25 +179,25 @@ bool dev_pn531_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t u dev_spec_pn531* pdsp = (dev_spec_pn531*)ds; // Packet length = data length (len) + checksum (1) + end of stream marker (1) - buffer[3] = uiTxLen; - // Packet length checksum - buffer[4] = BUFFER_LENGTH - buffer[3]; + buffer[3] = uiTxLen; + // Packet length checksum + buffer[4] = BUFFER_LENGTH - buffer[3]; // Copy the PN53X command into the packet buffer memmove(buffer+5,pbtTx,uiTxLen); // Calculate data payload checksum - buffer[uiTxLen+5] = 0; + buffer[uiTxLen+5] = 0; for(uiPos=0; uiPos < uiTxLen; uiPos++) { buffer[uiTxLen+5] -= buffer[uiPos+5]; } // End of stream marker - buffer[uiTxLen+6] = 0; + buffer[uiTxLen+6] = 0; #ifdef DEBUG printf("Tx: "); - print_hex((byte*)buffer,uiTxLen+7); + print_hex((byte_t*)buffer,uiTxLen+7); #endif ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, buffer, uiTxLen+7, USB_TIMEOUT); @@ -220,7 +220,7 @@ bool dev_pn531_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t u #ifdef DEBUG printf("Rx: "); - print_hex((byte*)buf,ret); + print_hex((byte_t*)buf,ret); #endif if( ret == 6 ) @@ -236,7 +236,7 @@ bool dev_pn531_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t u #ifdef DEBUG printf("Rx: "); - print_hex((byte*)buf,ret); + print_hex((byte_t*)buf,ret); #endif } diff --git a/src/dev_pn531.h b/src/dev_pn531.h index 5b7ed98..51279e8 100644 --- a/src/dev_pn531.h +++ b/src/dev_pn531.h @@ -32,7 +32,7 @@ dev_info* dev_pn531_connect(const uint32_t uiIndex); void dev_pn531_disconnect(dev_info* pdi); // Callback function used by libnfc to transmit commands to the PN53X chip -bool dev_pn531_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen); +bool dev_pn531_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); #endif // _LIBNFC_DEV_PN531_H_ diff --git a/src/dev_pn533.c b/src/dev_pn533.c index 97f24f7..d73897a 100644 --- a/src/dev_pn533.c +++ b/src/dev_pn533.c @@ -166,7 +166,7 @@ void dev_pn533_disconnect(dev_info* pdi) free(pdi); } -bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen) +bool dev_pn533_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) { uint32_t uiPos = 0; int ret = 0; @@ -192,7 +192,7 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t u #ifdef DEBUG printf("Tx: "); - print_hex((byte*)buffer,uiTxLen+7); + print_hex((byte_t*)buffer,uiTxLen+7); #endif ret = usb_bulk_write(pdsp->pudh, pdsp->uiEndPointOut, buffer, uiTxLen+7, USB_TIMEOUT); @@ -215,7 +215,7 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t u #ifdef DEBUG printf("Rx: "); - print_hex((byte*)buf,ret); + print_hex((byte_t*)buf,ret); #endif if( ret == 6 ) @@ -231,7 +231,7 @@ bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t u #ifdef DEBUG printf("Rx: "); - print_hex((byte*)buf,ret); + print_hex((byte_t*)buf,ret); #endif } diff --git a/src/dev_pn533.h b/src/dev_pn533.h index 619247c..8bf0587 100644 --- a/src/dev_pn533.h +++ b/src/dev_pn533.h @@ -29,7 +29,7 @@ dev_info* dev_pn533_connect(const uint32_t uiIndex); void dev_pn533_disconnect(dev_info* pdi); // Callback function used by libnfc to transmit commands to the PN53X chip -bool dev_pn533_transceive(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen); +bool dev_pn533_transceive(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); #endif // _LIBNFC_DEV_PN533_H_ diff --git a/src/emulate.c b/src/emulate.c index afd1348..d501fae 100644 --- a/src/emulate.c +++ b/src/emulate.c @@ -26,18 +26,18 @@ along with this program. If not, see #include "libnfc.h" -static byte abtRecv[MAX_FRAME_LEN]; +static byte_t abtRecv[MAX_FRAME_LEN]; static uint32_t uiRecvBits; static dev_info* pdi; // ISO14443A Anti-Collision response -byte abtAtqa [2] = { 0x04,0x00 }; -byte abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 }; -byte abtSak [9] = { 0x08,0xb6,0xdd }; +byte_t abtAtqa [2] = { 0x04,0x00 }; +byte_t abtUidBcc [5] = { 0xDE,0xAD,0xBE,0xAF,0x62 }; +byte_t abtSak [9] = { 0x08,0xb6,0xdd }; int main(int argc, const char* argv[]) { - byte* pbtTx = NULL; + byte_t* pbtTx = NULL; uint32_t uiTxBits; // Try to open the NFC reader diff --git a/src/libnfc.c b/src/libnfc.c index 6026f0f..8aa1b40 100644 --- a/src/libnfc.c +++ b/src/libnfc.c @@ -58,44 +58,44 @@ along with this program. If not, see #define PARAM_NO_AMBLE 0x40 // PN53X configuration -byte pncmd_get_firmware_version [ 2] = { 0xD4,0x02 }; -byte pncmd_get_general_status [ 2] = { 0xD4,0x04 }; -byte pncmd_get_register [ 4] = { 0xD4,0x06 }; -byte pncmd_set_register [ 5] = { 0xD4,0x08 }; -byte pncmd_set_parameters [ 3] = { 0xD4,0x12 }; +byte_t pncmd_get_firmware_version [ 2] = { 0xD4,0x02 }; +byte_t pncmd_get_general_status [ 2] = { 0xD4,0x04 }; +byte_t pncmd_get_register [ 4] = { 0xD4,0x06 }; +byte_t pncmd_set_register [ 5] = { 0xD4,0x08 }; +byte_t pncmd_set_parameters [ 3] = { 0xD4,0x12 }; // RF field configuration -byte pncmd_rf_configure_field [ 4] = { 0xD4,0x32,0x01 }; -byte pncmd_rf_configure_timing [ 4] = { 0xD4,0x32,0x02 }; -byte pncmd_rf_configure_retry_data [ 4] = { 0xD4,0x32,0x04 }; -byte pncmd_rf_configure_retry_select [ 6] = { 0xD4,0x32,0x05 }; +byte_t pncmd_rf_configure_field [ 4] = { 0xD4,0x32,0x01 }; +byte_t pncmd_rf_configure_timing [ 4] = { 0xD4,0x32,0x02 }; +byte_t pncmd_rf_configure_retry_data [ 4] = { 0xD4,0x32,0x04 }; +byte_t pncmd_rf_configure_retry_select [ 6] = { 0xD4,0x32,0x05 }; // Reader -byte pncmd_reader_list_passive [264] = { 0xD4,0x4A }; -byte pncmd_reader_select [ 3] = { 0xD4,0x54 }; -byte pncmd_reader_deselect [ 3] = { 0xD4,0x44,0x00 }; -byte pncmd_reader_release [ 3] = { 0xD4,0x52,0x00 }; -byte pncmd_reader_set_baud_rate [ 5] = { 0xD4,0x4E }; -byte pncmd_reader_exchange_data [265] = { 0xD4,0x40 }; -byte pncmd_reader_auto_poll [ 5] = { 0xD4,0x60 }; +byte_t pncmd_reader_list_passive [264] = { 0xD4,0x4A }; +byte_t pncmd_reader_select [ 3] = { 0xD4,0x54 }; +byte_t pncmd_reader_deselect [ 3] = { 0xD4,0x44,0x00 }; +byte_t pncmd_reader_release [ 3] = { 0xD4,0x52,0x00 }; +byte_t pncmd_reader_set_baud_rate [ 5] = { 0xD4,0x4E }; +byte_t pncmd_reader_exchange_data [265] = { 0xD4,0x40 }; +byte_t pncmd_reader_auto_poll [ 5] = { 0xD4,0x60 }; // Target -byte pncmd_target_get_data [ 2] = { 0xD4,0x86 }; -byte pncmd_target_init [ 39] = { 0xD4,0x8C }; -byte pncmd_target_virtual_card [ 4] = { 0xD4,0x14 }; -byte pncmd_target_receive [ 2] = { 0xD4,0x88 }; -byte pncmd_target_send [264] = { 0xD4,0x90 }; -byte pncmd_target_get_status [ 2] = { 0xD4,0x8A }; +byte_t pncmd_target_get_data [ 2] = { 0xD4,0x86 }; +byte_t pncmd_target_init [ 39] = { 0xD4,0x8C }; +byte_t pncmd_target_virtual_card [ 4] = { 0xD4,0x14 }; +byte_t pncmd_target_receive [ 2] = { 0xD4,0x88 }; +byte_t pncmd_target_send [264] = { 0xD4,0x90 }; +byte_t pncmd_target_get_status [ 2] = { 0xD4,0x8A }; // Exchange raw data frames -byte pncmd_exchange_raw_data [266] = { 0xD4,0x42 }; +byte_t pncmd_exchange_raw_data [266] = { 0xD4,0x42 }; // Global buffers used for communication with the PN53X chip #define MAX_FRAME_LEN 264 -static byte abtRx[MAX_FRAME_LEN]; +static byte_t abtRx[MAX_FRAME_LEN]; static uint32_t uiRxLen; -bool pn53x_transceive(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxLen) +bool pn53x_transceive(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen) { // Reset the receiving buffer uiRxLen = MAX_FRAME_LEN; @@ -110,7 +110,7 @@ bool pn53x_transceive(const dev_info* pdi, const byte* pbtTx, const uint32_t uiT return true; } -byte pn53x_get_reg(const dev_info* pdi, uint16_t ui16Reg) +byte_t pn53x_get_reg(const dev_info* pdi, uint16_t ui16Reg) { uint8_t ui8Value; uint32_t uiValueLen = 1; @@ -148,10 +148,10 @@ bool pn53x_set_tx_bits(const dev_info* pdi, uint8_t ui8Bits) return true; } -bool pn53x_wrap_frame(const byte* pbtTx, const uint32_t uiTxBits, const byte* pbtTxPar, byte* pbtFrame, uint32_t* puiFrameBits) +bool pn53x_wrap_frame(const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtFrame, uint32_t* puiFrameBits) { - byte btFrame; - byte btData; + byte_t btFrame; + byte_t btData; uint32_t uiBitPos; uint32_t uiDataPos = 0; uint32_t uiBitsLeft = uiTxBits; @@ -204,13 +204,13 @@ bool pn53x_wrap_frame(const byte* pbtTx, const uint32_t uiTxBits, const byte* pb } } -bool pn53x_unwrap_frame(const byte* pbtFrame, const uint32_t uiFrameBits, byte* pbtRx, uint32_t* puiRxBits, byte* pbtRxPar) +bool pn53x_unwrap_frame(const byte_t* pbtFrame, const uint32_t uiFrameBits, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar) { - byte btFrame; - byte btData; + byte_t btFrame; + byte_t btData; uint8_t uiBitPos; uint32_t uiDataPos = 0; - byte* pbtFramePos = (byte*) pbtFrame; + byte_t* pbtFramePos = (byte_t*) pbtFrame; uint32_t uiBitsLeft = uiFrameBits; // Make sure we should frame at least something @@ -254,7 +254,7 @@ dev_info* nfc_connect() { dev_info* pdi; uint32_t uiDev; - byte abtFw[4]; + byte_t abtFw[4]; uint32_t uiFwLen = sizeof(abtFw); // Search through the device list for an available device @@ -312,7 +312,7 @@ void nfc_disconnect(dev_info* pdi) bool nfc_configure(dev_info* pdi, const dev_config_option dco, const bool bEnable) { - byte btValue; + byte_t btValue; // Make sure we are dealing with a active device if (!pdi->bActive) return false; @@ -384,7 +384,7 @@ bool nfc_reader_init(const dev_info* pdi) return true; } -bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti) +bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte_t* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti) { // Make sure we are dealing with a active device if (!pdi->bActive) return false; @@ -485,7 +485,7 @@ bool nfc_reader_deselect(const dev_info* pdi) return (pdi->pdc->transceive(pdi->ds,pncmd_reader_deselect,3,NULL,NULL)); } -bool nfc_reader_transceive_bits(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxBits, const byte* pbtTxPar, byte* pbtRx, uint32_t* puiRxBits, byte* pbtRxPar) +bool nfc_reader_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar) { uint32_t uiFrameBits = 0; uint32_t uiFrameBytes = 0; @@ -539,7 +539,7 @@ bool nfc_reader_transceive_bits(const dev_info* pdi, const byte* pbtTx, const ui return true; } -bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen) +bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen) { // We can not just send bytes without parity if while the PN53X expects we handled them if (!pdi->bPar) return false; @@ -608,7 +608,7 @@ bool nfc_reader_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8 } // When available, copy the parameter bytes - if (uiParamLen) memcpy(pncmd_reader_exchange_data+5,(byte*)pmp,uiParamLen); + if (uiParamLen) memcpy(pncmd_reader_exchange_data+5,(byte_t*)pmp,uiParamLen); // Fire the mifare command if (!pn53x_transceive(pdi,pncmd_reader_exchange_data,5+uiParamLen)) return false; @@ -620,7 +620,7 @@ bool nfc_reader_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8 return true; } -bool nfc_target_init(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxBits) +bool nfc_target_init(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxBits) { uint8_t ui8Bits; @@ -670,7 +670,7 @@ bool nfc_target_init(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxBits) return true; } -bool nfc_target_receive_bits(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxBits, byte* pbtRxPar) +bool nfc_target_receive_bits(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar) { uint32_t uiFrameBits; uint8_t ui8Bits; @@ -700,7 +700,7 @@ bool nfc_target_receive_bits(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxBi return true; } -bool nfc_target_receive_bytes(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxLen) +bool nfc_target_receive_bytes(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxLen) { // Try to gather a received frame from the reader if (!pn53x_transceive(pdi,pncmd_target_receive,2)) return false; @@ -715,7 +715,7 @@ bool nfc_target_receive_bytes(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxL return true; } -bool nfc_target_send_bits(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxBits, const byte* pbtTxPar) +bool nfc_target_send_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar) { uint32_t uiFrameBits = 0; uint32_t uiFrameBytes = 0; @@ -750,7 +750,7 @@ bool nfc_target_send_bits(const dev_info* pdi, const byte* pbtTx, const uint32_t } -bool nfc_target_send_bytes(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxLen) +bool nfc_target_send_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen) { // We can not just send bytes without parity if while the PN53X expects we handled them if (!pdi->bPar) return false; diff --git a/src/libnfc.h b/src/libnfc.h index 051b603..b5d9822 100644 --- a/src/libnfc.h +++ b/src/libnfc.h @@ -33,17 +33,17 @@ void nfc_disconnect(dev_info* pdi); bool nfc_configure(dev_info* pdi, const dev_config_option dco, const bool bEnable); bool nfc_reader_init(const dev_info* pdi); -bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti); +bool nfc_reader_select(const dev_info* pdi, const init_modulation im, const byte_t* pbtInitData, const uint32_t uiInitDataLen, tag_info* pti); bool nfc_reader_deselect(const dev_info* pdi); -bool nfc_reader_transceive_bits(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxBits, const byte* pbtTxPar, byte* pbtRx, uint32_t* puiRxBits, byte* pbtRxPar); -bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen); +bool nfc_reader_transceive_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar); +bool nfc_reader_transceive_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); bool nfc_reader_mifare_cmd(const dev_info* pdi, const mifare_cmd mc, const uint8_t ui8Block, mifare_param* pmp); -bool nfc_target_init(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxBits); -bool nfc_target_receive_bits(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxBits, byte* pbtRxPar); -bool nfc_target_receive_bytes(const dev_info* pdi, byte* pbtRx, uint32_t* puiRxLen); -bool nfc_target_send_bits(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxBits, const byte* pbtTxPar); -bool nfc_target_send_bytes(const dev_info* pdi, const byte* pbtTx, const uint32_t uiTxLen); +bool nfc_target_init(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxBits); +bool nfc_target_receive_bits(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxBits, byte_t* pbtRxPar); +bool nfc_target_receive_bytes(const dev_info* pdi, byte_t* pbtRx, uint32_t* puiRxLen); +bool nfc_target_send_bits(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxBits, const byte_t* pbtTxPar); +bool nfc_target_send_bytes(const dev_info* pdi, const byte_t* pbtTx, const uint32_t uiTxLen); #endif // _LIBNFC_H_ diff --git a/src/list.c b/src/list.c index 808c886..649939b 100644 --- a/src/list.c +++ b/src/list.c @@ -27,7 +27,7 @@ along with this program. If not, see #include "libnfc.h" static dev_info* pdi; -static byte abtFelica[5] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; +static byte_t abtFelica[5] = { 0x00, 0xff, 0xff, 0x00, 0x00 }; int main(int argc, const char* argv[]) { diff --git a/src/mftool.c b/src/mftool.c index 315c0aa..fbeb4ce 100644 --- a/src/mftool.c +++ b/src/mftool.c @@ -231,7 +231,7 @@ int main(int argc, const char* argv[]) { bool b4K; bool bReadAction; - byte* pbtUID; + byte_t* pbtUID; FILE* pfKeys; FILE* pfDump; diff --git a/src/mifaretag.h b/src/mifaretag.h index ae62c0a..dcea842 100644 --- a/src/mifaretag.h +++ b/src/mifaretag.h @@ -24,21 +24,21 @@ along with this program. If not, see #include "defines.h" typedef struct { - byte abtUID[4]; - byte btBCC; - byte btUnknown; - byte abtATQA[2]; - byte abtUnknown[8]; + byte_t abtUID[4]; + byte_t btBCC; + byte_t btUnknown; + byte_t abtATQA[2]; + byte_t abtUnknown[8]; } mifare_block_manufacturer; typedef struct { - byte abtData[16]; + byte_t abtData[16]; } mifare_block_data; typedef struct { - byte abtKeyA[6]; - byte abtAccessBits[4]; - byte abtKeyB[6]; + byte_t abtKeyA[6]; + byte_t abtAccessBits[4]; + byte_t abtKeyB[6]; } mifare_block_trailer; typedef union { diff --git a/src/relay.c b/src/relay.c index 092f39c..aca9b8c 100644 --- a/src/relay.c +++ b/src/relay.c @@ -25,11 +25,11 @@ along with this program. If not, see #include "libnfc.h" -static byte abtReaderRx[MAX_FRAME_LEN]; -static byte abtReaderRxPar[MAX_FRAME_LEN]; +static byte_t abtReaderRx[MAX_FRAME_LEN]; +static byte_t abtReaderRxPar[MAX_FRAME_LEN]; static uint32_t uiReaderRxBits; -static byte abtTagRx[MAX_FRAME_LEN]; -static byte abtTagRxPar[MAX_FRAME_LEN]; +static byte_t abtTagRx[MAX_FRAME_LEN]; +static byte_t abtTagRxPar[MAX_FRAME_LEN]; static uint32_t uiTagRxBits; static dev_info* pdiReader; static dev_info* pdiTag; diff --git a/src/rs232.c b/src/rs232.c index ffe395f..a8be31f 100644 --- a/src/rs232.c +++ b/src/rs232.c @@ -101,7 +101,7 @@ bool rs232_cts(const serial_port sp) return (status & TIOCM_CTS); } -bool rs232_receive(const serial_port sp, byte* pbtRx, uint32_t* puiRxLen) +bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen) { int iResult; uint32_t uiCount = 0; @@ -132,7 +132,7 @@ bool rs232_receive(const serial_port sp, byte* pbtRx, uint32_t* puiRxLen) } } -bool rs232_send(const serial_port sp, const byte* pbtTx, const uint32_t uiTxLen) +bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen) { int iResult; iResult = write(((serial_port_unix*)sp)->fd,pbtTx,uiTxLen); @@ -209,12 +209,12 @@ bool rs232_cts(const serial_port sp) return (dwStatus & MS_CTS_ON); } -bool rs232_receive(const serial_port sp, byte* pbtRx, uint32_t* puiRxLen) +bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen) { return (ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*puiRxLen,(LPDWORD)puiRxLen,NULL) != NULL); } -bool rs232_send(const serial_port sp, const byte* pbtTx, const uint32_t uiTxLen) +bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen) { DWORD dwTxLen; return (WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,uiTxLen,&dwTxLen,NULL) != NULL); diff --git a/src/rs232.h b/src/rs232.h index c4280f2..e1c6518 100644 --- a/src/rs232.h +++ b/src/rs232.h @@ -49,8 +49,8 @@ typedef void* serial_port; serial_port rs232_open(const char* pcPortName); void rs232_close(const serial_port sp); bool rs232_cts(const serial_port sp); -bool rs232_receive(const serial_port sp, byte* pbtRx, uint32_t* puiRxLen); -bool rs232_send(const serial_port sp, const byte* pbtTx, const uint32_t uiTxLen); +bool rs232_receive(const serial_port sp, byte_t* pbtRx, uint32_t* puiRxLen); +bool rs232_send(const serial_port sp, const byte_t* pbtTx, const uint32_t uiTxLen); #endif // _LIBNFC_RS232_H_ diff --git a/src/types.h b/src/types.h index 1e5c452..0babe2a 100644 --- a/src/types.h +++ b/src/types.h @@ -26,7 +26,9 @@ along with this program. If not, see #include "defines.h" -// Compiler directive, set struct alignment to 1 byte for compatibility +typedef uint8_t byte_t; + +// Compiler directive, set struct alignment to 1 byte_t for compatibility #pragma pack(1) typedef enum { @@ -51,7 +53,7 @@ typedef struct { struct dev_callbacks { const char* acDriver; // Driver description dev_info* (*connect)(const uint32_t uiIndex); - bool (*transceive)(const dev_spec ds, const byte* pbtTx, const uint32_t uiTxLen, byte* pbtRx, uint32_t* puiRxLen); + bool (*transceive)(const dev_spec ds, const byte_t* pbtTx, const uint32_t uiTxLen, byte_t* pbtRx, uint32_t* puiRxLen); void (*disconnect)(dev_info* pdi); }; @@ -77,37 +79,37 @@ typedef enum { }init_modulation; typedef struct { - byte abtAtqa[2]; - byte btSak; + byte_t abtAtqa[2]; + byte_t btSak; uint32_t uiUidLen; - byte abtUid[10]; + byte_t abtUid[10]; uint32_t uiAtsLen; - byte abtAts[36]; + byte_t abtAts[36]; }tag_info_iso14443a; typedef struct { uint32_t uiLen; - byte btResCode; - byte abtId[8]; - byte abtPad[8]; - byte abtSysCode[2]; + byte_t btResCode; + byte_t abtId[8]; + byte_t abtPad[8]; + byte_t abtSysCode[2]; }tag_info_felica; typedef struct { - byte abtAtqb[12]; - byte abtId[4]; - byte btParam1; - byte btParam2; - byte btParam3; - byte btParam4; - byte btCid; + byte_t abtAtqb[12]; + byte_t abtId[4]; + byte_t btParam1; + byte_t btParam2; + byte_t btParam3; + byte_t btParam4; + byte_t btCid; uint32_t uiInfLen; - byte abtInf[64]; + byte_t abtInf[64]; }tag_info_iso14443b; typedef struct { - byte btSensRes[2]; - byte btId[4]; + byte_t btSensRes[2]; + byte_t btId[4]; }tag_info_jewel; typedef union { @@ -133,16 +135,16 @@ typedef enum { // MIFARE Classic command params typedef struct { - byte abtKey[6]; - byte abtUid[4]; + byte_t abtKey[6]; + byte_t abtUid[4]; }mifare_param_auth; typedef struct { - byte abtData[16]; + byte_t abtData[16]; }mifare_param_data; typedef struct { - byte abtValue[4]; + byte_t abtValue[4]; }mifare_param_value; typedef union {